xref: /petsc/src/snes/interface/snes.c (revision 71f87433c2d5ca8a2055e7bac4e3ff8159d57028)
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__
134a2ae208SSatish Balay #define __FUNCT__ "SNESView"
147e2c5f70SBarry Smith /*@C
159b94acceSBarry Smith    SNESView - Prints the SNES data structure.
169b94acceSBarry Smith 
174c49b128SBarry Smith    Collective on SNES
18fee21e36SBarry Smith 
19c7afd0dbSLois Curfman McInnes    Input Parameters:
20c7afd0dbSLois Curfman McInnes +  SNES - the SNES context
21c7afd0dbSLois Curfman McInnes -  viewer - visualization context
22c7afd0dbSLois Curfman McInnes 
239b94acceSBarry Smith    Options Database Key:
24c8a8ba5cSLois Curfman McInnes .  -snes_view - Calls SNESView() at end of SNESSolve()
259b94acceSBarry Smith 
269b94acceSBarry Smith    Notes:
279b94acceSBarry Smith    The available visualization contexts include
28b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
29b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
30c8a8ba5cSLois Curfman McInnes          output where only the first processor opens
31c8a8ba5cSLois Curfman McInnes          the file.  All other processors send their
32c8a8ba5cSLois Curfman McInnes          data to the first processor to print.
339b94acceSBarry Smith 
343e081fefSLois Curfman McInnes    The user can open an alternative visualization context with
35b0a32e0cSBarry Smith    PetscViewerASCIIOpen() - output to a specified file.
369b94acceSBarry Smith 
3736851e7fSLois Curfman McInnes    Level: beginner
3836851e7fSLois Curfman McInnes 
399b94acceSBarry Smith .keywords: SNES, view
409b94acceSBarry Smith 
41b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
429b94acceSBarry Smith @*/
4363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESView(SNES snes,PetscViewer viewer)
449b94acceSBarry Smith {
459b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
46dfbe8321SBarry Smith   PetscErrorCode      ierr;
4794b7f48cSBarry Smith   KSP                 ksp;
48e060cb09SBarry Smith   SNESType            type;
4932077d6dSBarry Smith   PetscTruth          iascii,isstring;
509b94acceSBarry Smith 
513a40ed3dSBarry Smith   PetscFunctionBegin;
524482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
533050cee2SBarry Smith   if (!viewer) {
543050cee2SBarry Smith     ierr = PetscViewerASCIIGetStdout(snes->comm,&viewer);CHKERRQ(ierr);
553050cee2SBarry Smith   }
564482741eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE,2);
57c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,viewer,2);
5874679c65SBarry Smith 
5932077d6dSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
60b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);CHKERRQ(ierr);
6132077d6dSBarry Smith   if (iascii) {
623a7fca6bSBarry Smith     if (snes->prefix) {
633a7fca6bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:(%s)\n",snes->prefix);CHKERRQ(ierr);
643a7fca6bSBarry Smith     } else {
65b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:\n");CHKERRQ(ierr);
663a7fca6bSBarry Smith     }
67454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
68454a90a3SBarry Smith     if (type) {
69b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: %s\n",type);CHKERRQ(ierr);
70184914b5SBarry Smith     } else {
71b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: not set yet\n");CHKERRQ(ierr);
72184914b5SBarry Smith     }
73e7788613SBarry Smith     if (snes->ops->view) {
74b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
75e7788613SBarry Smith       ierr = (*snes->ops->view)(snes,viewer);CHKERRQ(ierr);
76b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
770ef38995SBarry Smith     }
7877431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum iterations=%D, maximum function evaluations=%D\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr);
79a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  tolerances: relative=%G, absolute=%G, solution=%G\n",
8070441072SBarry Smith                  snes->rtol,snes->abstol,snes->xtol);CHKERRQ(ierr);
8177431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",snes->linear_its);CHKERRQ(ierr);
8277431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of function evaluations=%D\n",snes->nfuncs);CHKERRQ(ierr);
839b94acceSBarry Smith     if (snes->ksp_ewconv) {
849b94acceSBarry Smith       kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
859b94acceSBarry Smith       if (kctx) {
8677431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"  Eisenstat-Walker computation of KSP relative tolerance (version %D)\n",kctx->version);CHKERRQ(ierr);
87a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    rtol_0=%G, rtol_max=%G, threshold=%G\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr);
88a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    gamma=%G, alpha=%G, alpha2=%G\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr);
899b94acceSBarry Smith       }
909b94acceSBarry Smith     }
910f5bd95cSBarry Smith   } else if (isstring) {
92454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
93b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr);
9419bcc07fSBarry Smith   }
9594b7f48cSBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
96b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
9794b7f48cSBarry Smith   ierr = KSPView(ksp,viewer);CHKERRQ(ierr);
98b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
993a40ed3dSBarry Smith   PetscFunctionReturn(0);
1009b94acceSBarry Smith }
1019b94acceSBarry Smith 
10276b2cf59SMatthew Knepley /*
10376b2cf59SMatthew Knepley   We retain a list of functions that also take SNES command
10476b2cf59SMatthew Knepley   line options. These are called at the end SNESSetFromOptions()
10576b2cf59SMatthew Knepley */
10676b2cf59SMatthew Knepley #define MAXSETFROMOPTIONS 5
107a7cc72afSBarry Smith static PetscInt numberofsetfromoptions;
1086849ba73SBarry Smith static PetscErrorCode (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES);
10976b2cf59SMatthew Knepley 
110e74ef692SMatthew Knepley #undef __FUNCT__
111e74ef692SMatthew Knepley #define __FUNCT__ "SNESAddOptionsChecker"
112ac226902SBarry Smith /*@C
11376b2cf59SMatthew Knepley   SNESAddOptionsChecker - Adds an additional function to check for SNES options.
11476b2cf59SMatthew Knepley 
11576b2cf59SMatthew Knepley   Not Collective
11676b2cf59SMatthew Knepley 
11776b2cf59SMatthew Knepley   Input Parameter:
11876b2cf59SMatthew Knepley . snescheck - function that checks for options
11976b2cf59SMatthew Knepley 
12076b2cf59SMatthew Knepley   Level: developer
12176b2cf59SMatthew Knepley 
12276b2cf59SMatthew Knepley .seealso: SNESSetFromOptions()
12376b2cf59SMatthew Knepley @*/
12463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAddOptionsChecker(PetscErrorCode (*snescheck)(SNES))
12576b2cf59SMatthew Knepley {
12676b2cf59SMatthew Knepley   PetscFunctionBegin;
12776b2cf59SMatthew Knepley   if (numberofsetfromoptions >= MAXSETFROMOPTIONS) {
12877431f27SBarry Smith     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %D allowed", MAXSETFROMOPTIONS);
12976b2cf59SMatthew Knepley   }
13076b2cf59SMatthew Knepley   othersetfromoptions[numberofsetfromoptions++] = snescheck;
13176b2cf59SMatthew Knepley   PetscFunctionReturn(0);
13276b2cf59SMatthew Knepley }
13376b2cf59SMatthew Knepley 
1344a2ae208SSatish Balay #undef __FUNCT__
1354a2ae208SSatish Balay #define __FUNCT__ "SNESSetFromOptions"
1369b94acceSBarry Smith /*@
13794b7f48cSBarry Smith    SNESSetFromOptions - Sets various SNES and KSP parameters from user options.
1389b94acceSBarry Smith 
139c7afd0dbSLois Curfman McInnes    Collective on SNES
140c7afd0dbSLois Curfman McInnes 
1419b94acceSBarry Smith    Input Parameter:
1429b94acceSBarry Smith .  snes - the SNES context
1439b94acceSBarry Smith 
14436851e7fSLois Curfman McInnes    Options Database Keys:
1456831982aSBarry Smith +  -snes_type <type> - ls, tr, umls, umtr, test
14682738288SBarry Smith .  -snes_stol - convergence tolerance in terms of the norm
14782738288SBarry Smith                 of the change in the solution between steps
14870441072SBarry Smith .  -snes_atol <abstol> - absolute tolerance of residual norm
149b39c3a46SLois Curfman McInnes .  -snes_rtol <rtol> - relative decrease in tolerance norm from initial
150b39c3a46SLois Curfman McInnes .  -snes_max_it <max_it> - maximum number of iterations
151b39c3a46SLois Curfman McInnes .  -snes_max_funcs <max_funcs> - maximum number of function evaluations
15250ffb88aSMatthew Knepley .  -snes_max_fail <max_fail> - maximum number of failures
153b39c3a46SLois Curfman McInnes .  -snes_trtol <trtol> - trust region tolerance
1542492ecdbSBarry Smith .  -snes_no_convergence_test - skip convergence test in nonlinear
15582738288SBarry Smith                                solver; hence iterations will continue until max_it
1561fbbfb26SLois Curfman McInnes                                or some other criterion is reached. Saves expense
15782738288SBarry Smith                                of convergence test
158e8105e01SRichard Katz .  -snes_monitor <optional filename> - prints residual norm at each iteration. if no
159e8105e01SRichard Katz                                        filename given prints to stdout
160a6570f20SBarry Smith .  -snes_monitor_solution - plots solution at each iteration
161a6570f20SBarry Smith .  -snes_monitor_residual - plots residual (not its norm) at each iteration
162a6570f20SBarry Smith .  -snes_monitor_solution_update - plots update to solution at each iteration
163a6570f20SBarry Smith .  -snes_monitor_draw - plots residual norm at each iteration
164e24b481bSBarry Smith .  -snes_fd - use finite differences to compute Jacobian; very slow, only for testing
1655968eb51SBarry Smith .  -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration
166fee2055bSBarry Smith -  -snes_converged_reason - print the reason for convergence/divergence after each solve
16782738288SBarry Smith 
16882738288SBarry Smith     Options Database for Eisenstat-Walker method:
1694b27c08aSLois Curfman McInnes +  -snes_ksp_ew_conv - use Eisenstat-Walker method for determining linear system convergence
1704b27c08aSLois Curfman McInnes .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
17136851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
17236851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
17336851e7fSLois Curfman McInnes .  -snes_ksp_ew_gamma <gamma> - Sets gamma
17436851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha <alpha> - Sets alpha
17536851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
17636851e7fSLois Curfman McInnes -  -snes_ksp_ew_threshold <threshold> - Sets threshold
17782738288SBarry Smith 
17811ca99fdSLois Curfman McInnes    Notes:
17911ca99fdSLois Curfman McInnes    To see all options, run your program with the -help option or consult
18011ca99fdSLois Curfman McInnes    the users manual.
18183e2fdc7SBarry Smith 
18236851e7fSLois Curfman McInnes    Level: beginner
18336851e7fSLois Curfman McInnes 
1849b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database
1859b94acceSBarry Smith 
18669ed319cSSatish Balay .seealso: SNESSetOptionsPrefix()
1879b94acceSBarry Smith @*/
18863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFromOptions(SNES snes)
1899b94acceSBarry Smith {
19094b7f48cSBarry Smith   KSP                     ksp;
191186905e3SBarry Smith   SNES_KSP_EW_ConvCtx     *kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
192f1af5d2fSBarry Smith   PetscTruth              flg;
193dfbe8321SBarry Smith   PetscErrorCode          ierr;
19477431f27SBarry Smith   PetscInt                i;
1952fc52814SBarry Smith   const char              *deft;
196e8105e01SRichard Katz   char                    type[256], monfilename[PETSC_MAX_PATH_LEN];
19723d894e5SBarry Smith   PetscViewerASCIIMonitor monviewer;
1989b94acceSBarry Smith 
1993a40ed3dSBarry Smith   PetscFunctionBegin;
2004482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
201ca161407SBarry Smith 
202b0a32e0cSBarry Smith   ierr = PetscOptionsBegin(snes->comm,snes->prefix,"Nonlinear solver (SNES) options","SNES");CHKERRQ(ierr);
203186905e3SBarry Smith     if (snes->type_name) {
204186905e3SBarry Smith       deft = snes->type_name;
205186905e3SBarry Smith     } else {
2064b27c08aSLois Curfman McInnes       deft = SNESLS;
207d64ed03dSBarry Smith     }
2084bbc92c1SBarry Smith 
209186905e3SBarry Smith     if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
210b0a32e0cSBarry Smith     ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr);
211d64ed03dSBarry Smith     if (flg) {
212186905e3SBarry Smith       ierr = SNESSetType(snes,type);CHKERRQ(ierr);
213186905e3SBarry Smith     } else if (!snes->type_name) {
214186905e3SBarry Smith       ierr = SNESSetType(snes,deft);CHKERRQ(ierr);
215d64ed03dSBarry Smith     }
216909c8a9fSBarry Smith     ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr);
21793c39befSBarry Smith 
21887828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_stol","Stop if step length less then","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr);
21970441072SBarry Smith     ierr = PetscOptionsReal("-snes_atol","Stop if function norm less then","SNESSetTolerances",snes->abstol,&snes->abstol,0);CHKERRQ(ierr);
220186905e3SBarry Smith 
22187828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less then","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr);
222b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr);
223b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr);
22450ffb88aSMatthew Knepley     ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr);
2255968eb51SBarry Smith     ierr = PetscOptionsName("-snes_converged_reason","Print reason for converged or diverged","SNESSolve",&flg);CHKERRQ(ierr);
2265968eb51SBarry Smith     if (flg) {
2275968eb51SBarry Smith       snes->printreason = PETSC_TRUE;
2285968eb51SBarry Smith     }
229186905e3SBarry Smith 
230*71f87433Sdalcinl     ierr = PetscOptionsTruth("-snes_ksp_ew","Use Eisentat-Walker linear system convergence test","SNES_KSP_SetUseEW",snes->ksp_ewconv,&snes->ksp_ewconv,PETSC_NULL);CHKERRQ(ierr);
231*71f87433Sdalcinl     ierr = PetscOptionsTruth("-snes_ksp_ew_conv","Use Eisentat-Walker linear system convergence test","SNES_KSP_SetUseEW",snes->ksp_ewconv,&snes->ksp_ewconv,PETSC_NULL);CHKERRQ(ierr);
232186905e3SBarry Smith 
233cf502942SBarry Smith     ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNES_KSP_SetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr);
23487828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNES_KSP_SetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr);
23587828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNES_KSP_SetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr);
23687828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNES_KSP_SetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr);
23787828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNES_KSP_SetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr);
23887828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNES_KSP_SetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr);
23987828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNES_KSP_SetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr);
240186905e3SBarry Smith 
241b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_no_convergence_test","Don't test for convergence","None",&flg);CHKERRQ(ierr);
242e7788613SBarry Smith     if (flg) {snes->ops->converged = 0;}
243a6570f20SBarry Smith     ierr = PetscOptionsName("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",&flg);CHKERRQ(ierr);
244a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);}
245eabae89aSBarry Smith 
246a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor","Monitor norm of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
247e8105e01SRichard Katz     if (flg) {
24823d894e5SBarry Smith       ierr = PetscViewerASCIIMonitorCreate(snes->comm,monfilename,0,&monviewer);CHKERRQ(ierr);
24923d894e5SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefault,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
250e8105e01SRichard Katz     }
251eabae89aSBarry Smith 
252a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_ratiomonitor","Monitor ratios of norms of function","SNESMonitorSetRatio","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
253eabae89aSBarry Smith     if (flg) {
25423d894e5SBarry Smith       ierr = PetscViewerASCIIMonitorCreate(snes->comm,monfilename,0,&monviewer);CHKERRQ(ierr);
255f1bef1bcSMatthew Knepley       ierr = SNESMonitorSetRatio(snes,monviewer);CHKERRQ(ierr);
256e8105e01SRichard Katz     }
257eabae89aSBarry Smith 
258a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor_short","Monitor norm of function (fewer digits)","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
259eabae89aSBarry Smith     if (flg) {
26023d894e5SBarry Smith       ierr = PetscViewerASCIIMonitorCreate(snes->comm,monfilename,0,&monviewer);CHKERRQ(ierr);
26123d894e5SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefaultShort,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
262eabae89aSBarry Smith     }
263eabae89aSBarry Smith 
264a6570f20SBarry Smith     ierr = PetscOptionsName("-snes_monitor_solution","Plot solution at each iteration","SNESMonitorSolution",&flg);CHKERRQ(ierr);
265a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolution,0,0);CHKERRQ(ierr);}
266a6570f20SBarry Smith     ierr = PetscOptionsName("-snes_monitor_solution_update","Plot correction at each iteration","SNESMonitorSolutionUpdate",&flg);CHKERRQ(ierr);
267a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolutionUpdate,0,0);CHKERRQ(ierr);}
268a6570f20SBarry Smith     ierr = PetscOptionsName("-snes_monitor_residual","Plot residual at each iteration","SNESMonitorResidual",&flg);CHKERRQ(ierr);
269a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorResidual,0,0);CHKERRQ(ierr);}
270a6570f20SBarry Smith     ierr = PetscOptionsName("-snes_monitor_draw","Plot function norm at each iteration","SNESMonitorLG",&flg);CHKERRQ(ierr);
271a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
272e24b481bSBarry Smith 
273b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",&flg);CHKERRQ(ierr);
2744b27c08aSLois Curfman McInnes     if (flg) {
275186905e3SBarry Smith       ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr);
276ae15b995SBarry Smith       ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr);
2779b94acceSBarry Smith     }
278639f9d9dSBarry Smith 
27976b2cf59SMatthew Knepley     for(i = 0; i < numberofsetfromoptions; i++) {
28076b2cf59SMatthew Knepley       ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr);
28176b2cf59SMatthew Knepley     }
28276b2cf59SMatthew Knepley 
283e7788613SBarry Smith     if (snes->ops->setfromoptions) {
284e7788613SBarry Smith       ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr);
285639f9d9dSBarry Smith     }
286b0a32e0cSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
2874bbc92c1SBarry Smith 
28894b7f48cSBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
28994b7f48cSBarry Smith   ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);
29093993e2dSLois Curfman McInnes 
2913a40ed3dSBarry Smith   PetscFunctionReturn(0);
2929b94acceSBarry Smith }
2939b94acceSBarry Smith 
294a847f771SSatish Balay 
2954a2ae208SSatish Balay #undef __FUNCT__
2964a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext"
2979b94acceSBarry Smith /*@
2989b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
2999b94acceSBarry Smith    the nonlinear solvers.
3009b94acceSBarry Smith 
301fee21e36SBarry Smith    Collective on SNES
302fee21e36SBarry Smith 
303c7afd0dbSLois Curfman McInnes    Input Parameters:
304c7afd0dbSLois Curfman McInnes +  snes - the SNES context
305c7afd0dbSLois Curfman McInnes -  usrP - optional user context
306c7afd0dbSLois Curfman McInnes 
30736851e7fSLois Curfman McInnes    Level: intermediate
30836851e7fSLois Curfman McInnes 
3099b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
3109b94acceSBarry Smith 
3119b94acceSBarry Smith .seealso: SNESGetApplicationContext()
3129b94acceSBarry Smith @*/
31363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetApplicationContext(SNES snes,void *usrP)
3149b94acceSBarry Smith {
3153a40ed3dSBarry Smith   PetscFunctionBegin;
3164482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
3179b94acceSBarry Smith   snes->user		= usrP;
3183a40ed3dSBarry Smith   PetscFunctionReturn(0);
3199b94acceSBarry Smith }
32074679c65SBarry Smith 
3214a2ae208SSatish Balay #undef __FUNCT__
3224a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext"
3239b94acceSBarry Smith /*@C
3249b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
3259b94acceSBarry Smith    nonlinear solvers.
3269b94acceSBarry Smith 
327c7afd0dbSLois Curfman McInnes    Not Collective
328c7afd0dbSLois Curfman McInnes 
3299b94acceSBarry Smith    Input Parameter:
3309b94acceSBarry Smith .  snes - SNES context
3319b94acceSBarry Smith 
3329b94acceSBarry Smith    Output Parameter:
3339b94acceSBarry Smith .  usrP - user context
3349b94acceSBarry Smith 
33536851e7fSLois Curfman McInnes    Level: intermediate
33636851e7fSLois Curfman McInnes 
3379b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
3389b94acceSBarry Smith 
3399b94acceSBarry Smith .seealso: SNESSetApplicationContext()
3409b94acceSBarry Smith @*/
34163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetApplicationContext(SNES snes,void **usrP)
3429b94acceSBarry Smith {
3433a40ed3dSBarry Smith   PetscFunctionBegin;
3444482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
3459b94acceSBarry Smith   *usrP = snes->user;
3463a40ed3dSBarry Smith   PetscFunctionReturn(0);
3479b94acceSBarry Smith }
34874679c65SBarry Smith 
3494a2ae208SSatish Balay #undef __FUNCT__
3504a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber"
3519b94acceSBarry Smith /*@
352c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
353c8228a4eSBarry Smith    at this time.
3549b94acceSBarry Smith 
355c7afd0dbSLois Curfman McInnes    Not Collective
356c7afd0dbSLois Curfman McInnes 
3579b94acceSBarry Smith    Input Parameter:
3589b94acceSBarry Smith .  snes - SNES context
3599b94acceSBarry Smith 
3609b94acceSBarry Smith    Output Parameter:
3619b94acceSBarry Smith .  iter - iteration number
3629b94acceSBarry Smith 
363c8228a4eSBarry Smith    Notes:
364c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
365c8228a4eSBarry Smith 
366c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
36708405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
36808405cd6SLois Curfman McInnes .vb
36908405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
37008405cd6SLois Curfman McInnes       if (!(it % 2)) {
37108405cd6SLois Curfman McInnes         [compute Jacobian here]
37208405cd6SLois Curfman McInnes       }
37308405cd6SLois Curfman McInnes .ve
374c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
37508405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
376c8228a4eSBarry Smith 
37736851e7fSLois Curfman McInnes    Level: intermediate
37836851e7fSLois Curfman McInnes 
3792b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number,
3802b668275SBarry Smith 
3812b668275SBarry Smith .seealso:   SNESGetFunctionNorm(), SNESGetNumberLinearIterations()
3829b94acceSBarry Smith @*/
38363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetIterationNumber(SNES snes,PetscInt* iter)
3849b94acceSBarry Smith {
3853a40ed3dSBarry Smith   PetscFunctionBegin;
3864482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
3874482741eSBarry Smith   PetscValidIntPointer(iter,2);
3889b94acceSBarry Smith   *iter = snes->iter;
3893a40ed3dSBarry Smith   PetscFunctionReturn(0);
3909b94acceSBarry Smith }
39174679c65SBarry Smith 
3924a2ae208SSatish Balay #undef __FUNCT__
3934a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm"
3949b94acceSBarry Smith /*@
3959b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
3969b94acceSBarry Smith    with SNESSSetFunction().
3979b94acceSBarry Smith 
398c7afd0dbSLois Curfman McInnes    Collective on SNES
399c7afd0dbSLois Curfman McInnes 
4009b94acceSBarry Smith    Input Parameter:
4019b94acceSBarry Smith .  snes - SNES context
4029b94acceSBarry Smith 
4039b94acceSBarry Smith    Output Parameter:
4049b94acceSBarry Smith .  fnorm - 2-norm of function
4059b94acceSBarry Smith 
40636851e7fSLois Curfman McInnes    Level: intermediate
40736851e7fSLois Curfman McInnes 
4089b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
409a86d99e1SLois Curfman McInnes 
4102b668275SBarry Smith .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetNumberLinearIterations()
4119b94acceSBarry Smith @*/
412*71f87433Sdalcinl PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunctionNorm(SNES snes,PetscReal *fnorm)
4139b94acceSBarry Smith {
4143a40ed3dSBarry Smith   PetscFunctionBegin;
4154482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
4164482741eSBarry Smith   PetscValidScalarPointer(fnorm,2);
4179b94acceSBarry Smith   *fnorm = snes->norm;
4183a40ed3dSBarry Smith   PetscFunctionReturn(0);
4199b94acceSBarry Smith }
42074679c65SBarry Smith 
4214a2ae208SSatish Balay #undef __FUNCT__
4224a2ae208SSatish Balay #define __FUNCT__ "SNESGetNumberUnsuccessfulSteps"
4239b94acceSBarry Smith /*@
4249b94acceSBarry Smith    SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps
4259b94acceSBarry Smith    attempted by the nonlinear solver.
4269b94acceSBarry Smith 
427c7afd0dbSLois Curfman McInnes    Not Collective
428c7afd0dbSLois Curfman McInnes 
4299b94acceSBarry Smith    Input Parameter:
4309b94acceSBarry Smith .  snes - SNES context
4319b94acceSBarry Smith 
4329b94acceSBarry Smith    Output Parameter:
4339b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
4349b94acceSBarry Smith 
435c96a6f78SLois Curfman McInnes    Notes:
436c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
437c96a6f78SLois Curfman McInnes 
43836851e7fSLois Curfman McInnes    Level: intermediate
43936851e7fSLois Curfman McInnes 
4409b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
4419b94acceSBarry Smith @*/
44263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNumberUnsuccessfulSteps(SNES snes,PetscInt* nfails)
4439b94acceSBarry Smith {
4443a40ed3dSBarry Smith   PetscFunctionBegin;
4454482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
4464482741eSBarry Smith   PetscValidIntPointer(nfails,2);
44750ffb88aSMatthew Knepley   *nfails = snes->numFailures;
44850ffb88aSMatthew Knepley   PetscFunctionReturn(0);
44950ffb88aSMatthew Knepley }
45050ffb88aSMatthew Knepley 
45150ffb88aSMatthew Knepley #undef __FUNCT__
45250ffb88aSMatthew Knepley #define __FUNCT__ "SNESSetMaximumUnsuccessfulSteps"
45350ffb88aSMatthew Knepley /*@
45450ffb88aSMatthew Knepley    SNESSetMaximumUnsuccessfulSteps - Sets the maximum number of unsuccessful steps
45550ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
45650ffb88aSMatthew Knepley 
45750ffb88aSMatthew Knepley    Not Collective
45850ffb88aSMatthew Knepley 
45950ffb88aSMatthew Knepley    Input Parameters:
46050ffb88aSMatthew Knepley +  snes     - SNES context
46150ffb88aSMatthew Knepley -  maxFails - maximum of unsuccessful steps
46250ffb88aSMatthew Knepley 
46350ffb88aSMatthew Knepley    Level: intermediate
46450ffb88aSMatthew Knepley 
46550ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
46650ffb88aSMatthew Knepley @*/
46763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaximumUnsuccessfulSteps(SNES snes, PetscInt maxFails)
46850ffb88aSMatthew Knepley {
46950ffb88aSMatthew Knepley   PetscFunctionBegin;
4704482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
47150ffb88aSMatthew Knepley   snes->maxFailures = maxFails;
47250ffb88aSMatthew Knepley   PetscFunctionReturn(0);
47350ffb88aSMatthew Knepley }
47450ffb88aSMatthew Knepley 
47550ffb88aSMatthew Knepley #undef __FUNCT__
47650ffb88aSMatthew Knepley #define __FUNCT__ "SNESGetMaximumUnsuccessfulSteps"
47750ffb88aSMatthew Knepley /*@
47850ffb88aSMatthew Knepley    SNESGetMaximumUnsuccessfulSteps - Gets the maximum number of unsuccessful steps
47950ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
48050ffb88aSMatthew Knepley 
48150ffb88aSMatthew Knepley    Not Collective
48250ffb88aSMatthew Knepley 
48350ffb88aSMatthew Knepley    Input Parameter:
48450ffb88aSMatthew Knepley .  snes     - SNES context
48550ffb88aSMatthew Knepley 
48650ffb88aSMatthew Knepley    Output Parameter:
48750ffb88aSMatthew Knepley .  maxFails - maximum of unsuccessful steps
48850ffb88aSMatthew Knepley 
48950ffb88aSMatthew Knepley    Level: intermediate
49050ffb88aSMatthew Knepley 
49150ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
49250ffb88aSMatthew Knepley @*/
49363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaximumUnsuccessfulSteps(SNES snes, PetscInt *maxFails)
49450ffb88aSMatthew Knepley {
49550ffb88aSMatthew Knepley   PetscFunctionBegin;
4964482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
4974482741eSBarry Smith   PetscValidIntPointer(maxFails,2);
49850ffb88aSMatthew Knepley   *maxFails = snes->maxFailures;
4993a40ed3dSBarry Smith   PetscFunctionReturn(0);
5009b94acceSBarry Smith }
501a847f771SSatish Balay 
5024a2ae208SSatish Balay #undef __FUNCT__
5033d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures"
5043d4c4710SBarry Smith /*@
5053d4c4710SBarry Smith    SNESGetLinearSolveFailures - Gets the number of failed (non-converged)
5063d4c4710SBarry Smith    linear solvers.
5073d4c4710SBarry Smith 
5083d4c4710SBarry Smith    Not Collective
5093d4c4710SBarry Smith 
5103d4c4710SBarry Smith    Input Parameter:
5113d4c4710SBarry Smith .  snes - SNES context
5123d4c4710SBarry Smith 
5133d4c4710SBarry Smith    Output Parameter:
5143d4c4710SBarry Smith .  nfails - number of failed solves
5153d4c4710SBarry Smith 
5163d4c4710SBarry Smith    Notes:
5173d4c4710SBarry Smith    This counter is reset to zero for each successive call to SNESSolve().
5183d4c4710SBarry Smith 
5193d4c4710SBarry Smith    Level: intermediate
5203d4c4710SBarry Smith 
5213d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
5223d4c4710SBarry Smith @*/
5233d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails)
5243d4c4710SBarry Smith {
5253d4c4710SBarry Smith   PetscFunctionBegin;
5263d4c4710SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
5273d4c4710SBarry Smith   PetscValidIntPointer(nfails,2);
5283d4c4710SBarry Smith   *nfails = snes->numLinearSolveFailures;
5293d4c4710SBarry Smith   PetscFunctionReturn(0);
5303d4c4710SBarry Smith }
5313d4c4710SBarry Smith 
5323d4c4710SBarry Smith #undef __FUNCT__
5333d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures"
5343d4c4710SBarry Smith /*@
5353d4c4710SBarry Smith    SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts
5363d4c4710SBarry Smith    allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE
5373d4c4710SBarry Smith 
5383d4c4710SBarry Smith    Collective on SNES
5393d4c4710SBarry Smith 
5403d4c4710SBarry Smith    Input Parameters:
5413d4c4710SBarry Smith +  snes     - SNES context
5423d4c4710SBarry Smith -  maxFails - maximum allowed linear solve failures
5433d4c4710SBarry Smith 
5443d4c4710SBarry Smith    Level: intermediate
5453d4c4710SBarry Smith 
5463d4c4710SBarry Smith    Notes: By default this is 1; that is SNES returns on the first failed linear solve
5473d4c4710SBarry Smith 
5483d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
5493d4c4710SBarry Smith 
5503d4c4710SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures()
5513d4c4710SBarry Smith @*/
5523d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails)
5533d4c4710SBarry Smith {
5543d4c4710SBarry Smith   PetscFunctionBegin;
5553d4c4710SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
5563d4c4710SBarry Smith   snes->maxLinearSolveFailures = maxFails;
5573d4c4710SBarry Smith   PetscFunctionReturn(0);
5583d4c4710SBarry Smith }
5593d4c4710SBarry Smith 
5603d4c4710SBarry Smith #undef __FUNCT__
5613d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures"
5623d4c4710SBarry Smith /*@
5633d4c4710SBarry Smith    SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that
5643d4c4710SBarry Smith      are allowed before SNES terminates
5653d4c4710SBarry Smith 
5663d4c4710SBarry Smith    Not Collective
5673d4c4710SBarry Smith 
5683d4c4710SBarry Smith    Input Parameter:
5693d4c4710SBarry Smith .  snes     - SNES context
5703d4c4710SBarry Smith 
5713d4c4710SBarry Smith    Output Parameter:
5723d4c4710SBarry Smith .  maxFails - maximum of unsuccessful solves allowed
5733d4c4710SBarry Smith 
5743d4c4710SBarry Smith    Level: intermediate
5753d4c4710SBarry Smith 
5763d4c4710SBarry Smith    Notes: By default this is 1; that is SNES returns on the first failed linear solve
5773d4c4710SBarry Smith 
5783d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
5793d4c4710SBarry Smith 
5803d4c4710SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures()
5813d4c4710SBarry Smith @*/
5823d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails)
5833d4c4710SBarry Smith {
5843d4c4710SBarry Smith   PetscFunctionBegin;
5853d4c4710SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
5863d4c4710SBarry Smith   PetscValidIntPointer(maxFails,2);
5873d4c4710SBarry Smith   *maxFails = snes->maxLinearSolveFailures;
5883d4c4710SBarry Smith   PetscFunctionReturn(0);
5893d4c4710SBarry Smith }
5903d4c4710SBarry Smith 
5913d4c4710SBarry Smith #undef __FUNCT__
5924a2ae208SSatish Balay #define __FUNCT__ "SNESGetNumberLinearIterations"
593c96a6f78SLois Curfman McInnes /*@
594c96a6f78SLois Curfman McInnes    SNESGetNumberLinearIterations - Gets the total number of linear iterations
595c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
596c96a6f78SLois Curfman McInnes 
597c7afd0dbSLois Curfman McInnes    Not Collective
598c7afd0dbSLois Curfman McInnes 
599c96a6f78SLois Curfman McInnes    Input Parameter:
600c96a6f78SLois Curfman McInnes .  snes - SNES context
601c96a6f78SLois Curfman McInnes 
602c96a6f78SLois Curfman McInnes    Output Parameter:
603c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
604c96a6f78SLois Curfman McInnes 
605c96a6f78SLois Curfman McInnes    Notes:
606c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
607c96a6f78SLois Curfman McInnes 
60836851e7fSLois Curfman McInnes    Level: intermediate
60936851e7fSLois Curfman McInnes 
610c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
6112b668275SBarry Smith 
6122b668275SBarry Smith .seealso:  SNESGetIterationNumber(), SNESGetFunctionNorm()
613c96a6f78SLois Curfman McInnes @*/
61463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNumberLinearIterations(SNES snes,PetscInt* lits)
615c96a6f78SLois Curfman McInnes {
6163a40ed3dSBarry Smith   PetscFunctionBegin;
6174482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
6184482741eSBarry Smith   PetscValidIntPointer(lits,2);
619c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
6203a40ed3dSBarry Smith   PetscFunctionReturn(0);
621c96a6f78SLois Curfman McInnes }
622c96a6f78SLois Curfman McInnes 
6234a2ae208SSatish Balay #undef __FUNCT__
62494b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP"
62552baeb72SSatish Balay /*@
62694b7f48cSBarry Smith    SNESGetKSP - Returns the KSP context for a SNES solver.
6279b94acceSBarry Smith 
62894b7f48cSBarry Smith    Not Collective, but if SNES object is parallel, then KSP object is parallel
629c7afd0dbSLois Curfman McInnes 
6309b94acceSBarry Smith    Input Parameter:
6319b94acceSBarry Smith .  snes - the SNES context
6329b94acceSBarry Smith 
6339b94acceSBarry Smith    Output Parameter:
63494b7f48cSBarry Smith .  ksp - the KSP context
6359b94acceSBarry Smith 
6369b94acceSBarry Smith    Notes:
63794b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
6389b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
6392999313aSBarry Smith    PC contexts as well.
6409b94acceSBarry Smith 
64136851e7fSLois Curfman McInnes    Level: beginner
64236851e7fSLois Curfman McInnes 
64394b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
6449b94acceSBarry Smith 
6452999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
6469b94acceSBarry Smith @*/
64763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetKSP(SNES snes,KSP *ksp)
6489b94acceSBarry Smith {
6493a40ed3dSBarry Smith   PetscFunctionBegin;
6504482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
6514482741eSBarry Smith   PetscValidPointer(ksp,2);
65294b7f48cSBarry Smith   *ksp = snes->ksp;
6533a40ed3dSBarry Smith   PetscFunctionReturn(0);
6549b94acceSBarry Smith }
65582bf6240SBarry Smith 
6564a2ae208SSatish Balay #undef __FUNCT__
6572999313aSBarry Smith #define __FUNCT__ "SNESSetKSP"
6582999313aSBarry Smith /*@
6592999313aSBarry Smith    SNESSetKSP - Sets a KSP context for the SNES object to use
6602999313aSBarry Smith 
6612999313aSBarry Smith    Not Collective, but the SNES and KSP objects must live on the same MPI_Comm
6622999313aSBarry Smith 
6632999313aSBarry Smith    Input Parameters:
6642999313aSBarry Smith +  snes - the SNES context
6652999313aSBarry Smith -  ksp - the KSP context
6662999313aSBarry Smith 
6672999313aSBarry Smith    Notes:
6682999313aSBarry Smith    The SNES object already has its KSP object, you can obtain with SNESGetKSP()
6692999313aSBarry Smith    so this routine is rarely needed.
6702999313aSBarry Smith 
6712999313aSBarry Smith    The KSP object that is already in the SNES object has its reference count
6722999313aSBarry Smith    decreased by one.
6732999313aSBarry Smith 
6742999313aSBarry Smith    Level: developer
6752999313aSBarry Smith 
6762999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
6772999313aSBarry Smith 
6782999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
6792999313aSBarry Smith @*/
6802999313aSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetKSP(SNES snes,KSP ksp)
6812999313aSBarry Smith {
6822999313aSBarry Smith   PetscErrorCode ierr;
6832999313aSBarry Smith 
6842999313aSBarry Smith   PetscFunctionBegin;
6852999313aSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
6862999313aSBarry Smith   PetscValidHeaderSpecific(ksp,KSP_COOKIE,2);
6872999313aSBarry Smith   PetscCheckSameComm(snes,1,ksp,2);
6887dcf0eaaSdalcinl   ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr);
689906ed7ccSBarry Smith   if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);}
6902999313aSBarry Smith   snes->ksp = ksp;
6912999313aSBarry Smith   PetscFunctionReturn(0);
6922999313aSBarry Smith }
6932999313aSBarry Smith 
6942999313aSBarry Smith #undef __FUNCT__
6954a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc"
6966849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj)
697e24b481bSBarry Smith {
698e24b481bSBarry Smith   PetscFunctionBegin;
699e24b481bSBarry Smith   PetscFunctionReturn(0);
700e24b481bSBarry Smith }
701e24b481bSBarry Smith 
7029b94acceSBarry Smith /* -----------------------------------------------------------*/
7034a2ae208SSatish Balay #undef __FUNCT__
7044a2ae208SSatish Balay #define __FUNCT__ "SNESCreate"
70552baeb72SSatish Balay /*@
7069b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
7079b94acceSBarry Smith 
708c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
709c7afd0dbSLois Curfman McInnes 
710c7afd0dbSLois Curfman McInnes    Input Parameters:
711906ed7ccSBarry Smith .  comm - MPI communicator
7129b94acceSBarry Smith 
7139b94acceSBarry Smith    Output Parameter:
7149b94acceSBarry Smith .  outsnes - the new SNES context
7159b94acceSBarry Smith 
716c7afd0dbSLois Curfman McInnes    Options Database Keys:
717c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
718c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
719c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
720c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
721c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
722c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
723c1f60f51SBarry Smith 
72436851e7fSLois Curfman McInnes    Level: beginner
72536851e7fSLois Curfman McInnes 
7269b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
7279b94acceSBarry Smith 
7284b27c08aSLois Curfman McInnes .seealso: SNESSolve(), SNESDestroy(), SNES
7299b94acceSBarry Smith @*/
73063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESCreate(MPI_Comm comm,SNES *outsnes)
7319b94acceSBarry Smith {
732dfbe8321SBarry Smith   PetscErrorCode      ierr;
7339b94acceSBarry Smith   SNES                snes;
7349b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
73537fcc0dbSBarry Smith 
7363a40ed3dSBarry Smith   PetscFunctionBegin;
737ed1caa07SMatthew Knepley   PetscValidPointer(outsnes,2);
7388ba1e511SMatthew Knepley   *outsnes = PETSC_NULL;
7398ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
7408ba1e511SMatthew Knepley   ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr);
7418ba1e511SMatthew Knepley #endif
7428ba1e511SMatthew Knepley 
743e7788613SBarry Smith   ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr);
744e24b481bSBarry Smith   snes->bops->publish     = SNESPublish_Petsc;
7459b94acceSBarry Smith   snes->max_its           = 50;
7469750a799SBarry Smith   snes->max_funcs	  = 10000;
7479b94acceSBarry Smith   snes->norm		  = 0.0;
748b4874afaSBarry Smith   snes->rtol		  = 1.e-8;
749b4874afaSBarry Smith   snes->ttol              = 0.0;
75070441072SBarry Smith   snes->abstol		  = 1.e-50;
7519b94acceSBarry Smith   snes->xtol		  = 1.e-8;
7524b27c08aSLois Curfman McInnes   snes->deltatol	  = 1.e-12;
7539b94acceSBarry Smith   snes->nfuncs            = 0;
75450ffb88aSMatthew Knepley   snes->numFailures       = 0;
75550ffb88aSMatthew Knepley   snes->maxFailures       = 1;
7567a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
757639f9d9dSBarry Smith   snes->numbermonitors    = 0;
7589b94acceSBarry Smith   snes->data              = 0;
759e7788613SBarry Smith   snes->ops->view         = 0;
7604dc4c822SBarry Smith   snes->setupcalled       = PETSC_FALSE;
761186905e3SBarry Smith   snes->ksp_ewconv        = PETSC_FALSE;
7626f24a144SLois Curfman McInnes   snes->vwork             = 0;
7636f24a144SLois Curfman McInnes   snes->nwork             = 0;
764758f92a0SBarry Smith   snes->conv_hist_len     = 0;
765758f92a0SBarry Smith   snes->conv_hist_max     = 0;
766758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
767758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
768758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
769184914b5SBarry Smith   snes->reason            = SNES_CONVERGED_ITERATING;
7709b94acceSBarry Smith 
7713d4c4710SBarry Smith   snes->numLinearSolveFailures = 0;
7723d4c4710SBarry Smith   snes->maxLinearSolveFailures = 1;
7733d4c4710SBarry Smith 
7749b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
775b0a32e0cSBarry Smith   ierr = PetscNew(SNES_KSP_EW_ConvCtx,&kctx);CHKERRQ(ierr);
77652e6d16bSBarry Smith   ierr = PetscLogObjectMemory(snes,sizeof(SNES_KSP_EW_ConvCtx));CHKERRQ(ierr);
7779b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
7789b94acceSBarry Smith   kctx->version     = 2;
7799b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
7809b94acceSBarry Smith                              this was too large for some test cases */
7819b94acceSBarry Smith   kctx->rtol_last   = 0;
7829b94acceSBarry Smith   kctx->rtol_max    = .9;
7839b94acceSBarry Smith   kctx->gamma       = 1.0;
784*71f87433Sdalcinl   kctx->alpha       = .5*(1.0 + sqrt(5.0));
785*71f87433Sdalcinl   kctx->alpha2      = kctx->alpha;
7869b94acceSBarry Smith   kctx->threshold   = .1;
7879b94acceSBarry Smith   kctx->lresid_last = 0;
7889b94acceSBarry Smith   kctx->norm_last   = 0;
7899b94acceSBarry Smith 
79094b7f48cSBarry Smith   ierr = KSPCreate(comm,&snes->ksp);CHKERRQ(ierr);
79152e6d16bSBarry Smith   ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr);
7925334005bSBarry Smith 
7939b94acceSBarry Smith   *outsnes = snes;
79400036973SBarry Smith   ierr = PetscPublishAll(snes);CHKERRQ(ierr);
7953a40ed3dSBarry Smith   PetscFunctionReturn(0);
7969b94acceSBarry Smith }
7979b94acceSBarry Smith 
7984a2ae208SSatish Balay #undef __FUNCT__
7994a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction"
8009b94acceSBarry Smith /*@C
8019b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
8029b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
8039b94acceSBarry Smith    equations.
8049b94acceSBarry Smith 
805fee21e36SBarry Smith    Collective on SNES
806fee21e36SBarry Smith 
807c7afd0dbSLois Curfman McInnes    Input Parameters:
808c7afd0dbSLois Curfman McInnes +  snes - the SNES context
809c7afd0dbSLois Curfman McInnes .  r - vector to store function value
810de044059SHong Zhang .  func - function evaluation routine
811c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
812c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
8139b94acceSBarry Smith 
814c7afd0dbSLois Curfman McInnes    Calling sequence of func:
8158d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
816c7afd0dbSLois Curfman McInnes 
817313e4042SLois Curfman McInnes .  f - function vector
818c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
8199b94acceSBarry Smith 
8209b94acceSBarry Smith    Notes:
8219b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
8229b94acceSBarry Smith $      f'(x) x = -f(x),
823c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
8249b94acceSBarry Smith 
82536851e7fSLois Curfman McInnes    Level: beginner
82636851e7fSLois Curfman McInnes 
8279b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
8289b94acceSBarry Smith 
829a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
8309b94acceSBarry Smith @*/
83163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx)
8329b94acceSBarry Smith {
8333a40ed3dSBarry Smith   PetscFunctionBegin;
8344482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
8354482741eSBarry Smith   PetscValidHeaderSpecific(r,VEC_COOKIE,2);
836c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,r,2);
837184914b5SBarry Smith 
838e7788613SBarry Smith   snes->ops->computefunction = func;
8399b94acceSBarry Smith   snes->vec_func             = snes->vec_func_always = r;
8409b94acceSBarry Smith   snes->funP                 = ctx;
8413a40ed3dSBarry Smith   PetscFunctionReturn(0);
8429b94acceSBarry Smith }
8439b94acceSBarry Smith 
8443ab0aad5SBarry Smith /* --------------------------------------------------------------- */
8453ab0aad5SBarry Smith #undef __FUNCT__
8463ab0aad5SBarry Smith #define __FUNCT__ "SNESSetRhs"
8473ab0aad5SBarry Smith /*@C
8483ab0aad5SBarry Smith    SNESSetRhs - Sets the vector for solving F(x) = rhs. If rhs is not set
8493ab0aad5SBarry Smith    it assumes a zero right hand side.
8503ab0aad5SBarry Smith 
8513ab0aad5SBarry Smith    Collective on SNES
8523ab0aad5SBarry Smith 
8533ab0aad5SBarry Smith    Input Parameters:
8543ab0aad5SBarry Smith +  snes - the SNES context
8553ab0aad5SBarry Smith -  rhs - the right hand side vector or PETSC_NULL for a zero right hand side
8563ab0aad5SBarry Smith 
8573ab0aad5SBarry Smith    Level: intermediate
8583ab0aad5SBarry Smith 
8593ab0aad5SBarry Smith .keywords: SNES, nonlinear, set, function, right hand side
8603ab0aad5SBarry Smith 
8611096aae1SMatthew Knepley .seealso: SNESGetRhs(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
8623ab0aad5SBarry Smith @*/
86363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetRhs(SNES snes,Vec rhs)
8643ab0aad5SBarry Smith {
865dfbe8321SBarry Smith   PetscErrorCode ierr;
8663ab0aad5SBarry Smith 
8673ab0aad5SBarry Smith   PetscFunctionBegin;
8683ab0aad5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
8693ab0aad5SBarry Smith   if (rhs) {
8703ab0aad5SBarry Smith     PetscValidHeaderSpecific(rhs,VEC_COOKIE,2);
8713ab0aad5SBarry Smith     PetscCheckSameComm(snes,1,rhs,2);
8723ab0aad5SBarry Smith     ierr = PetscObjectReference((PetscObject)rhs);CHKERRQ(ierr);
8733ab0aad5SBarry Smith   }
8743ab0aad5SBarry Smith   if (snes->afine) {
8753ab0aad5SBarry Smith     ierr = VecDestroy(snes->afine);CHKERRQ(ierr);
8763ab0aad5SBarry Smith   }
8773ab0aad5SBarry Smith   snes->afine = rhs;
8783ab0aad5SBarry Smith   PetscFunctionReturn(0);
8793ab0aad5SBarry Smith }
8803ab0aad5SBarry Smith 
8814a2ae208SSatish Balay #undef __FUNCT__
8821096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs"
8831096aae1SMatthew Knepley /*@C
8841096aae1SMatthew Knepley    SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set
8851096aae1SMatthew Knepley    it assumes a zero right hand side.
8861096aae1SMatthew Knepley 
8871096aae1SMatthew Knepley    Collective on SNES
8881096aae1SMatthew Knepley 
8891096aae1SMatthew Knepley    Input Parameter:
8901096aae1SMatthew Knepley .  snes - the SNES context
8911096aae1SMatthew Knepley 
8921096aae1SMatthew Knepley    Output Parameter:
8931096aae1SMatthew Knepley .  rhs - the right hand side vector or PETSC_NULL for a zero right hand side
8941096aae1SMatthew Knepley 
8951096aae1SMatthew Knepley    Level: intermediate
8961096aae1SMatthew Knepley 
8971096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side
8981096aae1SMatthew Knepley 
8991096aae1SMatthew Knepley .seealso: SNESSetRhs(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
9001096aae1SMatthew Knepley @*/
9011096aae1SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetRhs(SNES snes,Vec *rhs)
9021096aae1SMatthew Knepley {
9031096aae1SMatthew Knepley   PetscFunctionBegin;
9041096aae1SMatthew Knepley   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
9051096aae1SMatthew Knepley   PetscValidPointer(rhs,2);
9061096aae1SMatthew Knepley   *rhs = snes->afine;
9071096aae1SMatthew Knepley   PetscFunctionReturn(0);
9081096aae1SMatthew Knepley }
9091096aae1SMatthew Knepley 
9101096aae1SMatthew Knepley #undef __FUNCT__
9114a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction"
9129b94acceSBarry Smith /*@
91336851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
9149b94acceSBarry Smith                          SNESSetFunction().
9159b94acceSBarry Smith 
916c7afd0dbSLois Curfman McInnes    Collective on SNES
917c7afd0dbSLois Curfman McInnes 
9189b94acceSBarry Smith    Input Parameters:
919c7afd0dbSLois Curfman McInnes +  snes - the SNES context
920c7afd0dbSLois Curfman McInnes -  x - input vector
9219b94acceSBarry Smith 
9229b94acceSBarry Smith    Output Parameter:
9233638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
9249b94acceSBarry Smith 
9251bffabb2SLois Curfman McInnes    Notes:
92636851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
92736851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
92836851e7fSLois Curfman McInnes    themselves.
92936851e7fSLois Curfman McInnes 
93036851e7fSLois Curfman McInnes    Level: developer
93136851e7fSLois Curfman McInnes 
9329b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
9339b94acceSBarry Smith 
934a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
9359b94acceSBarry Smith @*/
93663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeFunction(SNES snes,Vec x,Vec y)
9379b94acceSBarry Smith {
938dfbe8321SBarry Smith   PetscErrorCode ierr;
9399b94acceSBarry Smith 
9403a40ed3dSBarry Smith   PetscFunctionBegin;
9414482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
9424482741eSBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE,2);
9434482741eSBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE,3);
944c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,x,2);
945c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,3);
946184914b5SBarry Smith 
947d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
948e7788613SBarry Smith   if (snes->ops->computefunction) {
949d64ed03dSBarry Smith     PetscStackPush("SNES user function");
950e9a2bbcdSBarry Smith     CHKMEMQ;
951e7788613SBarry Smith     ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP);
952e9a2bbcdSBarry Smith     CHKMEMQ;
953d64ed03dSBarry Smith     PetscStackPop;
954d5e45103SBarry Smith     if (PetscExceptionValue(ierr)) {
95519717074SBarry Smith       PetscErrorCode pierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(pierr);
95619717074SBarry Smith     }
957d5e45103SBarry Smith     CHKERRQ(ierr);
9581096aae1SMatthew Knepley   } else if (snes->afine) {
9591096aae1SMatthew Knepley     ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr);
9601096aae1SMatthew Knepley   } else {
9611096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() before SNESComputeFunction(), likely called from SNESSolve().");
9621096aae1SMatthew Knepley   }
9633ab0aad5SBarry Smith   if (snes->afine) {
964016dedfbSBarry Smith     ierr = VecAXPY(y,-1.0,snes->afine);CHKERRQ(ierr);
9653ab0aad5SBarry Smith   }
966ae3c334cSLois Curfman McInnes   snes->nfuncs++;
967d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
9683a40ed3dSBarry Smith   PetscFunctionReturn(0);
9699b94acceSBarry Smith }
9709b94acceSBarry Smith 
9714a2ae208SSatish Balay #undef __FUNCT__
9724a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian"
97362fef451SLois Curfman McInnes /*@
97462fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
97562fef451SLois Curfman McInnes    set with SNESSetJacobian().
97662fef451SLois Curfman McInnes 
977c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
978c7afd0dbSLois Curfman McInnes 
97962fef451SLois Curfman McInnes    Input Parameters:
980c7afd0dbSLois Curfman McInnes +  snes - the SNES context
981c7afd0dbSLois Curfman McInnes -  x - input vector
98262fef451SLois Curfman McInnes 
98362fef451SLois Curfman McInnes    Output Parameters:
984c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
98562fef451SLois Curfman McInnes .  B - optional preconditioning matrix
9862b668275SBarry Smith -  flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER)
987fee21e36SBarry Smith 
98862fef451SLois Curfman McInnes    Notes:
98962fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
99062fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
99162fef451SLois Curfman McInnes 
99294b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
993dc5a77f8SLois Curfman McInnes    flag parameter.
99462fef451SLois Curfman McInnes 
99536851e7fSLois Curfman McInnes    Level: developer
99636851e7fSLois Curfman McInnes 
99762fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
99862fef451SLois Curfman McInnes 
9992b668275SBarry Smith .seealso:  SNESSetJacobian(), KSPSetOperators(), MatStructure
100062fef451SLois Curfman McInnes @*/
100163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
10029b94acceSBarry Smith {
1003dfbe8321SBarry Smith   PetscErrorCode ierr;
10043a40ed3dSBarry Smith 
10053a40ed3dSBarry Smith   PetscFunctionBegin;
10064482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
10074482741eSBarry Smith   PetscValidHeaderSpecific(X,VEC_COOKIE,2);
10084482741eSBarry Smith   PetscValidPointer(flg,5);
1009c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,X,2);
1010e7788613SBarry Smith   if (!snes->ops->computejacobian) PetscFunctionReturn(0);
1011d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1012c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1013d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
1014dc67937bSBarry Smith   CHKMEMQ;
1015e7788613SBarry Smith   ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
1016dc67937bSBarry Smith   CHKMEMQ;
1017d64ed03dSBarry Smith   PetscStackPop;
1018d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
10196d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
10206ce558aeSBarry Smith   /* PetscValidHeaderSpecific(*A,MAT_COOKIE,3);
10216ce558aeSBarry Smith     PetscValidHeaderSpecific(*B,MAT_COOKIE,4);   */
10223a40ed3dSBarry Smith   PetscFunctionReturn(0);
10239b94acceSBarry Smith }
10249b94acceSBarry Smith 
10254a2ae208SSatish Balay #undef __FUNCT__
10264a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian"
10279b94acceSBarry Smith /*@C
10289b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1029044dda88SLois Curfman McInnes    location to store the matrix.
10309b94acceSBarry Smith 
1031c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1032c7afd0dbSLois Curfman McInnes 
10339b94acceSBarry Smith    Input Parameters:
1034c7afd0dbSLois Curfman McInnes +  snes - the SNES context
10359b94acceSBarry Smith .  A - Jacobian matrix
10369b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
10379b94acceSBarry Smith .  func - Jacobian evaluation routine
1038c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
10392cd2dfdcSLois Curfman McInnes          Jacobian evaluation routine (may be PETSC_NULL)
10409b94acceSBarry Smith 
10419b94acceSBarry Smith    Calling sequence of func:
10428d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
10439b94acceSBarry Smith 
1044c7afd0dbSLois Curfman McInnes +  x - input vector
10459b94acceSBarry Smith .  A - Jacobian matrix
10469b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1047ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
10482b668275SBarry Smith    structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER
1049c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
10509b94acceSBarry Smith 
10519b94acceSBarry Smith    Notes:
105294b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
10532cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1054ac21db08SLois Curfman McInnes 
1055ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
10569b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
10579b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
10589b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
10599b94acceSBarry Smith    throughout the global iterations.
10609b94acceSBarry Smith 
106136851e7fSLois Curfman McInnes    Level: beginner
106236851e7fSLois Curfman McInnes 
10639b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
10649b94acceSBarry Smith 
10652b668275SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatSNESMFComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure
10669b94acceSBarry Smith @*/
106763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
10689b94acceSBarry Smith {
1069dfbe8321SBarry Smith   PetscErrorCode ierr;
10703a7fca6bSBarry Smith 
10713a40ed3dSBarry Smith   PetscFunctionBegin;
10724482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
10734482741eSBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_COOKIE,2);
10744482741eSBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_COOKIE,3);
1075c9780b6fSBarry Smith   if (A) PetscCheckSameComm(snes,1,A,2);
1076c9780b6fSBarry Smith   if (B) PetscCheckSameComm(snes,1,B,2);
1077e7788613SBarry Smith   if (func) snes->ops->computejacobian = func;
10783a7fca6bSBarry Smith   if (ctx)  snes->jacP                 = ctx;
10793a7fca6bSBarry Smith   if (A) {
10807dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
10813a7fca6bSBarry Smith     if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
10829b94acceSBarry Smith     snes->jacobian = A;
10833a7fca6bSBarry Smith   }
10843a7fca6bSBarry Smith   if (B) {
10857dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
10863a7fca6bSBarry Smith     if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
10879b94acceSBarry Smith     snes->jacobian_pre = B;
10883a7fca6bSBarry Smith   }
10893a40ed3dSBarry Smith   PetscFunctionReturn(0);
10909b94acceSBarry Smith }
109162fef451SLois Curfman McInnes 
10924a2ae208SSatish Balay #undef __FUNCT__
10934a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian"
1094c2aafc4cSSatish Balay /*@C
1095b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1096b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1097b4fd4287SBarry Smith 
1098c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1099c7afd0dbSLois Curfman McInnes 
1100b4fd4287SBarry Smith    Input Parameter:
1101b4fd4287SBarry Smith .  snes - the nonlinear solver context
1102b4fd4287SBarry Smith 
1103b4fd4287SBarry Smith    Output Parameters:
1104c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1105b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
110670e92668SMatthew Knepley .  func - location to put Jacobian function (or PETSC_NULL)
110770e92668SMatthew Knepley -  ctx - location to stash Jacobian ctx (or PETSC_NULL)
1108fee21e36SBarry Smith 
110936851e7fSLois Curfman McInnes    Level: advanced
111036851e7fSLois Curfman McInnes 
1111b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1112b4fd4287SBarry Smith @*/
111370e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx)
1114b4fd4287SBarry Smith {
11153a40ed3dSBarry Smith   PetscFunctionBegin;
11164482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1117b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
1118b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
1119e7788613SBarry Smith   if (func) *func = snes->ops->computejacobian;
112070e92668SMatthew Knepley   if (ctx)  *ctx  = snes->jacP;
11213a40ed3dSBarry Smith   PetscFunctionReturn(0);
1122b4fd4287SBarry Smith }
1123b4fd4287SBarry Smith 
11249b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
112563dd3a1aSKris Buschelman EXTERN PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*);
11269b94acceSBarry Smith 
11274a2ae208SSatish Balay #undef __FUNCT__
11284a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp"
11299b94acceSBarry Smith /*@
11309b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1131272ac6f2SLois Curfman McInnes    of a nonlinear solver.
11329b94acceSBarry Smith 
1133fee21e36SBarry Smith    Collective on SNES
1134fee21e36SBarry Smith 
1135c7afd0dbSLois Curfman McInnes    Input Parameters:
113670e92668SMatthew Knepley .  snes - the SNES context
1137c7afd0dbSLois Curfman McInnes 
1138272ac6f2SLois Curfman McInnes    Notes:
1139272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1140272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1141272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1142272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1143272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1144272ac6f2SLois Curfman McInnes 
114536851e7fSLois Curfman McInnes    Level: advanced
114636851e7fSLois Curfman McInnes 
11479b94acceSBarry Smith .keywords: SNES, nonlinear, setup
11489b94acceSBarry Smith 
11499b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
11509b94acceSBarry Smith @*/
115170e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUp(SNES snes)
11529b94acceSBarry Smith {
1153dfbe8321SBarry Smith   PetscErrorCode ierr;
1154*71f87433Sdalcinl   PetscTruth     flg;
11553a40ed3dSBarry Smith 
11563a40ed3dSBarry Smith   PetscFunctionBegin;
11574482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
11584dc4c822SBarry Smith   if (snes->setupcalled) PetscFunctionReturn(0);
11599b94acceSBarry Smith 
1160b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_mf_operator",&flg);CHKERRQ(ierr);
1161c1f60f51SBarry Smith   /*
1162c1f60f51SBarry Smith       This version replaces the user provided Jacobian matrix with a
1163dfa02198SLois Curfman McInnes       matrix-free version but still employs the user-provided preconditioner matrix
1164c1f60f51SBarry Smith   */
1165c1f60f51SBarry Smith   if (flg) {
1166c1f60f51SBarry Smith     Mat J;
11675a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
11685a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
1169ae15b995SBarry Smith     ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr);
11703a7fca6bSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
11713a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
1172c1f60f51SBarry Smith   }
117345fc7adcSBarry Smith 
117403c60df9SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_MAT_SINGLE) && !defined(PETSC_USE_LONG_DOUBLE) && !defined(PETSC_USE_INT)
117545fc7adcSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_mf_operator2",&flg);CHKERRQ(ierr);
117645fc7adcSBarry Smith   if (flg) {
117745fc7adcSBarry Smith     Mat J;
117845fc7adcSBarry Smith     ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_sol,&J);CHKERRQ(ierr);
117945fc7adcSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
118045fc7adcSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
118145fc7adcSBarry Smith   }
118232a4b47aSMatthew Knepley #endif
118345fc7adcSBarry Smith 
1184b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_mf",&flg);CHKERRQ(ierr);
1185c1f60f51SBarry Smith   /*
1186dfa02198SLois Curfman McInnes       This version replaces both the user-provided Jacobian and the user-
1187c1f60f51SBarry Smith       provided preconditioner matrix with the default matrix free version.
1188c1f60f51SBarry Smith    */
118931615d04SLois Curfman McInnes   if (flg) {
1190272ac6f2SLois Curfman McInnes     Mat  J;
1191b5d62d44SBarry Smith     KSP ksp;
119294b7f48cSBarry Smith     PC   pc;
1193f3ef73ceSBarry Smith 
11945a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
11953a7fca6bSBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
1196ae15b995SBarry Smith     ierr = PetscInfo(snes,"Setting default matrix-free operator and preconditioner routines;\nThat is no preconditioner is being used.\n");CHKERRQ(ierr);
11973a7fca6bSBarry Smith     ierr = SNESSetJacobian(snes,J,J,MatSNESMFComputeJacobian,snes->funP);CHKERRQ(ierr);
11983a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
11993a7fca6bSBarry Smith 
1200f3ef73ceSBarry Smith     /* force no preconditioner */
120194b7f48cSBarry Smith     ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
1202b5d62d44SBarry Smith     ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
1203a9815358SBarry Smith     ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&flg);CHKERRQ(ierr);
1204a9815358SBarry Smith     if (!flg) {
1205f3ef73ceSBarry Smith       ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
1206272ac6f2SLois Curfman McInnes     }
1207a9815358SBarry Smith   }
1208f3ef73ceSBarry Smith 
12091096aae1SMatthew Knepley   if (!snes->vec_func && !snes->afine) {
12101096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
12111096aae1SMatthew Knepley   }
1212e7788613SBarry Smith   if (!snes->ops->computefunction && !snes->afine) {
12131096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
12141096aae1SMatthew Knepley   }
121529bbc08cSBarry Smith   if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetJacobian() first \n or use -snes_mf option");
1216a8c6a408SBarry Smith   if (snes->vec_func == snes->vec_sol) {
121729bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
1218a8c6a408SBarry Smith   }
1219a703fe33SLois Curfman McInnes 
1220410397dcSLisandro Dalcin   if (!snes->type_name) {
1221410397dcSLisandro Dalcin     ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr);
1222410397dcSLisandro Dalcin   }
1223*71f87433Sdalcinl 
1224410397dcSLisandro Dalcin   if (snes->ops->setup) {
1225410397dcSLisandro Dalcin     ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr);
1226410397dcSLisandro Dalcin   }
12277aaed0d8SBarry Smith   snes->setupcalled = PETSC_TRUE;
12283a40ed3dSBarry Smith   PetscFunctionReturn(0);
12299b94acceSBarry Smith }
12309b94acceSBarry Smith 
12314a2ae208SSatish Balay #undef __FUNCT__
12324a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
123352baeb72SSatish Balay /*@
12349b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
12359b94acceSBarry Smith    with SNESCreate().
12369b94acceSBarry Smith 
1237c7afd0dbSLois Curfman McInnes    Collective on SNES
1238c7afd0dbSLois Curfman McInnes 
12399b94acceSBarry Smith    Input Parameter:
12409b94acceSBarry Smith .  snes - the SNES context
12419b94acceSBarry Smith 
124236851e7fSLois Curfman McInnes    Level: beginner
124336851e7fSLois Curfman McInnes 
12449b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
12459b94acceSBarry Smith 
124663a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
12479b94acceSBarry Smith @*/
124863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDestroy(SNES snes)
12499b94acceSBarry Smith {
12506849ba73SBarry Smith   PetscErrorCode ierr;
12513a40ed3dSBarry Smith 
12523a40ed3dSBarry Smith   PetscFunctionBegin;
12534482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
12543a40ed3dSBarry Smith   if (--snes->refct > 0) PetscFunctionReturn(0);
1255d4bb536fSBarry Smith 
1256be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
12570f5bd95cSBarry Smith   ierr = PetscObjectDepublish(snes);CHKERRQ(ierr);
1258be0abb6dSBarry Smith 
1259e7788613SBarry Smith   if (snes->ops->destroy) {ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr);}
126005b42c5fSBarry Smith   ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);
12613a7fca6bSBarry Smith   if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
12623a7fca6bSBarry Smith   if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
12633ab0aad5SBarry Smith   if (snes->afine) {ierr = VecDestroy(snes->afine);CHKERRQ(ierr);}
126494b7f48cSBarry Smith   ierr = KSPDestroy(snes->ksp);CHKERRQ(ierr);
1265522c5e43SBarry Smith   if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);}
1266a6570f20SBarry Smith   ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);
1267a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr);
12683a40ed3dSBarry Smith   PetscFunctionReturn(0);
12699b94acceSBarry Smith }
12709b94acceSBarry Smith 
12719b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
12729b94acceSBarry Smith 
12734a2ae208SSatish Balay #undef __FUNCT__
12744a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
12759b94acceSBarry Smith /*@
1276d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
12779b94acceSBarry Smith 
1278c7afd0dbSLois Curfman McInnes    Collective on SNES
1279c7afd0dbSLois Curfman McInnes 
12809b94acceSBarry Smith    Input Parameters:
1281c7afd0dbSLois Curfman McInnes +  snes - the SNES context
128270441072SBarry Smith .  abstol - absolute convergence tolerance
128333174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
128433174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
128533174efeSLois Curfman McInnes            of the change in the solution between steps
128633174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1287c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1288fee21e36SBarry Smith 
128933174efeSLois Curfman McInnes    Options Database Keys:
129070441072SBarry Smith +    -snes_atol <abstol> - Sets abstol
1291c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1292c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1293c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1294c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
12959b94acceSBarry Smith 
1296d7a720efSLois Curfman McInnes    Notes:
12979b94acceSBarry Smith    The default maximum number of iterations is 50.
12989b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
12999b94acceSBarry Smith 
130036851e7fSLois Curfman McInnes    Level: intermediate
130136851e7fSLois Curfman McInnes 
130233174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
13039b94acceSBarry Smith 
13042492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance()
13059b94acceSBarry Smith @*/
130663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf)
13079b94acceSBarry Smith {
13083a40ed3dSBarry Smith   PetscFunctionBegin;
13094482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
131070441072SBarry Smith   if (abstol != PETSC_DEFAULT)  snes->abstol      = abstol;
1311d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1312d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1313d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1314d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
13153a40ed3dSBarry Smith   PetscFunctionReturn(0);
13169b94acceSBarry Smith }
13179b94acceSBarry Smith 
13184a2ae208SSatish Balay #undef __FUNCT__
13194a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
13209b94acceSBarry Smith /*@
132133174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
132233174efeSLois Curfman McInnes 
1323c7afd0dbSLois Curfman McInnes    Not Collective
1324c7afd0dbSLois Curfman McInnes 
132533174efeSLois Curfman McInnes    Input Parameters:
1326c7afd0dbSLois Curfman McInnes +  snes - the SNES context
132770441072SBarry Smith .  abstol - absolute convergence tolerance
132833174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
132933174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
133033174efeSLois Curfman McInnes            of the change in the solution between steps
133133174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1332c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1333fee21e36SBarry Smith 
133433174efeSLois Curfman McInnes    Notes:
133533174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
133633174efeSLois Curfman McInnes 
133736851e7fSLois Curfman McInnes    Level: intermediate
133836851e7fSLois Curfman McInnes 
133933174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
134033174efeSLois Curfman McInnes 
134133174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
134233174efeSLois Curfman McInnes @*/
134363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetTolerances(SNES snes,PetscReal *abstol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf)
134433174efeSLois Curfman McInnes {
13453a40ed3dSBarry Smith   PetscFunctionBegin;
13464482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
134770441072SBarry Smith   if (abstol)  *abstol  = snes->abstol;
134833174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
134933174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
135033174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
135133174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
13523a40ed3dSBarry Smith   PetscFunctionReturn(0);
135333174efeSLois Curfman McInnes }
135433174efeSLois Curfman McInnes 
13554a2ae208SSatish Balay #undef __FUNCT__
13564a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
135733174efeSLois Curfman McInnes /*@
13589b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
13599b94acceSBarry Smith 
1360fee21e36SBarry Smith    Collective on SNES
1361fee21e36SBarry Smith 
1362c7afd0dbSLois Curfman McInnes    Input Parameters:
1363c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1364c7afd0dbSLois Curfman McInnes -  tol - tolerance
1365c7afd0dbSLois Curfman McInnes 
13669b94acceSBarry Smith    Options Database Key:
1367c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
13689b94acceSBarry Smith 
136936851e7fSLois Curfman McInnes    Level: intermediate
137036851e7fSLois Curfman McInnes 
13719b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
13729b94acceSBarry Smith 
13732492ecdbSBarry Smith .seealso: SNESSetTolerances()
13749b94acceSBarry Smith @*/
137563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
13769b94acceSBarry Smith {
13773a40ed3dSBarry Smith   PetscFunctionBegin;
13784482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
13799b94acceSBarry Smith   snes->deltatol = tol;
13803a40ed3dSBarry Smith   PetscFunctionReturn(0);
13819b94acceSBarry Smith }
13829b94acceSBarry Smith 
1383df9fa365SBarry Smith /*
1384df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1385df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1386df9fa365SBarry Smith    macros instead of functions
1387df9fa365SBarry Smith */
13884a2ae208SSatish Balay #undef __FUNCT__
1389a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG"
1390a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx)
1391ce1608b8SBarry Smith {
1392dfbe8321SBarry Smith   PetscErrorCode ierr;
1393ce1608b8SBarry Smith 
1394ce1608b8SBarry Smith   PetscFunctionBegin;
13954482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1396a6570f20SBarry Smith   ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1397ce1608b8SBarry Smith   PetscFunctionReturn(0);
1398ce1608b8SBarry Smith }
1399ce1608b8SBarry Smith 
14004a2ae208SSatish Balay #undef __FUNCT__
1401a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate"
1402a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1403df9fa365SBarry Smith {
1404dfbe8321SBarry Smith   PetscErrorCode ierr;
1405df9fa365SBarry Smith 
1406df9fa365SBarry Smith   PetscFunctionBegin;
1407a6570f20SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1408df9fa365SBarry Smith   PetscFunctionReturn(0);
1409df9fa365SBarry Smith }
1410df9fa365SBarry Smith 
14114a2ae208SSatish Balay #undef __FUNCT__
1412a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy"
1413a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGDestroy(PetscDrawLG draw)
1414df9fa365SBarry Smith {
1415dfbe8321SBarry Smith   PetscErrorCode ierr;
1416df9fa365SBarry Smith 
1417df9fa365SBarry Smith   PetscFunctionBegin;
1418a6570f20SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
1419df9fa365SBarry Smith   PetscFunctionReturn(0);
1420df9fa365SBarry Smith }
1421df9fa365SBarry Smith 
14229b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
14239b94acceSBarry Smith 
14244a2ae208SSatish Balay #undef __FUNCT__
1425a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet"
14269b94acceSBarry Smith /*@C
1427a6570f20SBarry Smith    SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every
14289b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
14299b94acceSBarry Smith    progress.
14309b94acceSBarry Smith 
1431fee21e36SBarry Smith    Collective on SNES
1432fee21e36SBarry Smith 
1433c7afd0dbSLois Curfman McInnes    Input Parameters:
1434c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1435c7afd0dbSLois Curfman McInnes .  func - monitoring routine
1436b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
1437e8105e01SRichard Katz           monitor routine (use PETSC_NULL if no context is desired)
1438b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1439b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
14409b94acceSBarry Smith 
1441c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1442a7cc72afSBarry Smith $     int func(SNES snes,PetscInt its, PetscReal norm,void *mctx)
1443c7afd0dbSLois Curfman McInnes 
1444c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1445c7afd0dbSLois Curfman McInnes .    its - iteration number
1446c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
144740a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
14489b94acceSBarry Smith 
14499665c990SLois Curfman McInnes    Options Database Keys:
1450a6570f20SBarry Smith +    -snes_monitor        - sets SNESMonitorDefault()
1451a6570f20SBarry Smith .    -snes_monitor_draw    - sets line graph monitor,
1452a6570f20SBarry Smith                             uses SNESMonitorLGCreate()
1453a6570f20SBarry Smith _    -snes_monitor_cancel - cancels all monitors that have
1454c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
1455a6570f20SBarry Smith                             calls to SNESMonitorSet(), but
1456c7afd0dbSLois Curfman McInnes                             does not cancel those set via
1457c7afd0dbSLois Curfman McInnes                             the options database.
14589665c990SLois Curfman McInnes 
1459639f9d9dSBarry Smith    Notes:
14606bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
1461a6570f20SBarry Smith    SNESMonitorSet() multiple times; all will be called in the
14626bc08f3fSLois Curfman McInnes    order in which they were set.
1463639f9d9dSBarry Smith 
146436851e7fSLois Curfman McInnes    Level: intermediate
146536851e7fSLois Curfman McInnes 
14669b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
14679b94acceSBarry Smith 
1468a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel()
14699b94acceSBarry Smith @*/
1470b90d0a6eSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void*))
14719b94acceSBarry Smith {
1472b90d0a6eSBarry Smith   PetscInt i;
1473b90d0a6eSBarry Smith 
14743a40ed3dSBarry Smith   PetscFunctionBegin;
14754482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1476639f9d9dSBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) {
147729bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1478639f9d9dSBarry Smith   }
1479b90d0a6eSBarry Smith   for (i=0; i<snes->numbermonitors;i++) {
1480b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) PetscFunctionReturn(0);
1481b90d0a6eSBarry Smith 
1482b90d0a6eSBarry Smith     /* check if both default monitors that share common ASCII viewer */
1483b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitor == SNESMonitorDefault) {
1484b90d0a6eSBarry Smith       if (mctx && snes->monitorcontext[i]) {
1485b90d0a6eSBarry Smith         PetscErrorCode          ierr;
1486b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer1 = (PetscViewerASCIIMonitor) mctx;
1487b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer2 = (PetscViewerASCIIMonitor) snes->monitorcontext[i];
1488b90d0a6eSBarry Smith         if (viewer1->viewer == viewer2->viewer) {
1489b90d0a6eSBarry Smith           ierr = (*monitordestroy)(mctx);CHKERRQ(ierr);
1490b90d0a6eSBarry Smith           PetscFunctionReturn(0);
1491b90d0a6eSBarry Smith         }
1492b90d0a6eSBarry Smith       }
1493b90d0a6eSBarry Smith     }
1494b90d0a6eSBarry Smith   }
1495b90d0a6eSBarry Smith   snes->monitor[snes->numbermonitors]           = monitor;
1496b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
1497639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
14983a40ed3dSBarry Smith   PetscFunctionReturn(0);
14999b94acceSBarry Smith }
15009b94acceSBarry Smith 
15014a2ae208SSatish Balay #undef __FUNCT__
1502a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel"
15035cd90555SBarry Smith /*@C
1504a6570f20SBarry Smith    SNESMonitorCancel - Clears all the monitor functions for a SNES object.
15055cd90555SBarry Smith 
1506c7afd0dbSLois Curfman McInnes    Collective on SNES
1507c7afd0dbSLois Curfman McInnes 
15085cd90555SBarry Smith    Input Parameters:
15095cd90555SBarry Smith .  snes - the SNES context
15105cd90555SBarry Smith 
15111a480d89SAdministrator    Options Database Key:
1512a6570f20SBarry Smith .  -snes_monitor_cancel - cancels all monitors that have been hardwired
1513a6570f20SBarry Smith     into a code by calls to SNESMonitorSet(), but does not cancel those
1514c7afd0dbSLois Curfman McInnes     set via the options database
15155cd90555SBarry Smith 
15165cd90555SBarry Smith    Notes:
15175cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
15185cd90555SBarry Smith 
151936851e7fSLois Curfman McInnes    Level: intermediate
152036851e7fSLois Curfman McInnes 
15215cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
15225cd90555SBarry Smith 
1523a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet()
15245cd90555SBarry Smith @*/
1525a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorCancel(SNES snes)
15265cd90555SBarry Smith {
1527d952e501SBarry Smith   PetscErrorCode ierr;
1528d952e501SBarry Smith   PetscInt       i;
1529d952e501SBarry Smith 
15305cd90555SBarry Smith   PetscFunctionBegin;
15314482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1532d952e501SBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
1533d952e501SBarry Smith     if (snes->monitordestroy[i]) {
1534d952e501SBarry Smith       ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr);
1535d952e501SBarry Smith     }
1536d952e501SBarry Smith   }
15375cd90555SBarry Smith   snes->numbermonitors = 0;
15385cd90555SBarry Smith   PetscFunctionReturn(0);
15395cd90555SBarry Smith }
15405cd90555SBarry Smith 
15414a2ae208SSatish Balay #undef __FUNCT__
15424a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
15439b94acceSBarry Smith /*@C
15449b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
15459b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
15469b94acceSBarry Smith 
1547fee21e36SBarry Smith    Collective on SNES
1548fee21e36SBarry Smith 
1549c7afd0dbSLois Curfman McInnes    Input Parameters:
1550c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1551c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
1552c7afd0dbSLois Curfman McInnes -  cctx - [optional] context for private data for the convergence routine
1553c7afd0dbSLois Curfman McInnes           (may be PETSC_NULL)
15549b94acceSBarry Smith 
1555c7afd0dbSLois Curfman McInnes    Calling sequence of func:
155606ee9f85SBarry Smith $     PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
1557c7afd0dbSLois Curfman McInnes 
1558c7afd0dbSLois Curfman McInnes +    snes - the SNES context
155906ee9f85SBarry Smith .    it - current iteration (0 is the first and is before any Newton step)
1560c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
1561184914b5SBarry Smith .    reason - reason for convergence/divergence
1562c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
15634b27c08aSLois Curfman McInnes .    gnorm - 2-norm of current step
15644b27c08aSLois Curfman McInnes -    f - 2-norm of function
15659b94acceSBarry Smith 
156636851e7fSLois Curfman McInnes    Level: advanced
156736851e7fSLois Curfman McInnes 
15689b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
15699b94acceSBarry Smith 
15704b27c08aSLois Curfman McInnes .seealso: SNESConverged_LS(), SNESConverged_TR()
15719b94acceSBarry Smith @*/
157206ee9f85SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx)
15739b94acceSBarry Smith {
15743a40ed3dSBarry Smith   PetscFunctionBegin;
15754482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1576e7788613SBarry Smith   (snes)->ops->converged = func;
15779b94acceSBarry Smith   (snes)->cnvP           = cctx;
15783a40ed3dSBarry Smith   PetscFunctionReturn(0);
15799b94acceSBarry Smith }
15809b94acceSBarry Smith 
15814a2ae208SSatish Balay #undef __FUNCT__
15824a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
158352baeb72SSatish Balay /*@
1584184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
1585184914b5SBarry Smith 
1586184914b5SBarry Smith    Not Collective
1587184914b5SBarry Smith 
1588184914b5SBarry Smith    Input Parameter:
1589184914b5SBarry Smith .  snes - the SNES context
1590184914b5SBarry Smith 
1591184914b5SBarry Smith    Output Parameter:
1592e090d566SSatish Balay .  reason - negative value indicates diverged, positive value converged, see petscsnes.h or the
1593184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
1594184914b5SBarry Smith 
1595184914b5SBarry Smith    Level: intermediate
1596184914b5SBarry Smith 
1597184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
1598184914b5SBarry Smith 
1599184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
1600184914b5SBarry Smith 
16014b27c08aSLois Curfman McInnes .seealso: SNESSetConvergenceTest(), SNESConverged_LS(), SNESConverged_TR(), SNESConvergedReason
1602184914b5SBarry Smith @*/
160363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
1604184914b5SBarry Smith {
1605184914b5SBarry Smith   PetscFunctionBegin;
16064482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
16074482741eSBarry Smith   PetscValidPointer(reason,2);
1608184914b5SBarry Smith   *reason = snes->reason;
1609184914b5SBarry Smith   PetscFunctionReturn(0);
1610184914b5SBarry Smith }
1611184914b5SBarry Smith 
16124a2ae208SSatish Balay #undef __FUNCT__
16134a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
1614c9005455SLois Curfman McInnes /*@
1615c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
1616c9005455SLois Curfman McInnes 
1617fee21e36SBarry Smith    Collective on SNES
1618fee21e36SBarry Smith 
1619c7afd0dbSLois Curfman McInnes    Input Parameters:
1620c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
1621c7afd0dbSLois Curfman McInnes .  a   - array to hold history
1622cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
1623758f92a0SBarry Smith .  na  - size of a and its
162464731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
1625758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
1626c7afd0dbSLois Curfman McInnes 
1627c9005455SLois Curfman McInnes    Notes:
16284b27c08aSLois Curfman McInnes    If set, this array will contain the function norms computed
1629c9005455SLois Curfman McInnes    at each step.
1630c9005455SLois Curfman McInnes 
1631c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
1632c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
1633c9005455SLois Curfman McInnes    during the section of code that is being timed.
1634c9005455SLois Curfman McInnes 
163536851e7fSLois Curfman McInnes    Level: intermediate
163636851e7fSLois Curfman McInnes 
1637c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
1638758f92a0SBarry Smith 
163908405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
1640758f92a0SBarry Smith 
1641c9005455SLois Curfman McInnes @*/
1642a562a398SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscTruth reset)
1643c9005455SLois Curfman McInnes {
16443a40ed3dSBarry Smith   PetscFunctionBegin;
16454482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
16464482741eSBarry Smith   if (na)  PetscValidScalarPointer(a,2);
1647a562a398SLisandro Dalcin   if (its) PetscValidIntPointer(its,3);
1648c9005455SLois Curfman McInnes   snes->conv_hist       = a;
1649758f92a0SBarry Smith   snes->conv_hist_its   = its;
1650758f92a0SBarry Smith   snes->conv_hist_max   = na;
1651758f92a0SBarry Smith   snes->conv_hist_reset = reset;
1652758f92a0SBarry Smith   PetscFunctionReturn(0);
1653758f92a0SBarry Smith }
1654758f92a0SBarry Smith 
16554a2ae208SSatish Balay #undef __FUNCT__
16564a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
16570c4c9dddSBarry Smith /*@C
1658758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
1659758f92a0SBarry Smith 
1660758f92a0SBarry Smith    Collective on SNES
1661758f92a0SBarry Smith 
1662758f92a0SBarry Smith    Input Parameter:
1663758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
1664758f92a0SBarry Smith 
1665758f92a0SBarry Smith    Output Parameters:
1666758f92a0SBarry Smith .  a   - array to hold history
1667758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
1668758f92a0SBarry Smith          negative if not converged) for each solve.
1669758f92a0SBarry Smith -  na  - size of a and its
1670758f92a0SBarry Smith 
1671758f92a0SBarry Smith    Notes:
1672758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
1673758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
1674758f92a0SBarry Smith 
1675758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
1676758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
1677758f92a0SBarry Smith    during the section of code that is being timed.
1678758f92a0SBarry Smith 
1679758f92a0SBarry Smith    Level: intermediate
1680758f92a0SBarry Smith 
1681758f92a0SBarry Smith .keywords: SNES, get, convergence, history
1682758f92a0SBarry Smith 
1683758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
1684758f92a0SBarry Smith 
1685758f92a0SBarry Smith @*/
168663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na)
1687758f92a0SBarry Smith {
1688758f92a0SBarry Smith   PetscFunctionBegin;
16894482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1690758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
1691758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
1692758f92a0SBarry Smith   if (na)  *na  = snes->conv_hist_len;
16933a40ed3dSBarry Smith   PetscFunctionReturn(0);
1694c9005455SLois Curfman McInnes }
1695c9005455SLois Curfman McInnes 
1696e74ef692SMatthew Knepley #undef __FUNCT__
1697e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
1698ac226902SBarry Smith /*@C
169976b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
17007e4bb74cSBarry Smith   at the beginning o every iteration of the nonlinear solve. Specifically
17017e4bb74cSBarry Smith   it is called just before the Jacobian is "evaluated".
170276b2cf59SMatthew Knepley 
170376b2cf59SMatthew Knepley   Collective on SNES
170476b2cf59SMatthew Knepley 
170576b2cf59SMatthew Knepley   Input Parameters:
170676b2cf59SMatthew Knepley . snes - The nonlinear solver context
170776b2cf59SMatthew Knepley . func - The function
170876b2cf59SMatthew Knepley 
170976b2cf59SMatthew Knepley   Calling sequence of func:
1710b5d30489SBarry Smith . func (SNES snes, PetscInt step);
171176b2cf59SMatthew Knepley 
171276b2cf59SMatthew Knepley . step - The current step of the iteration
171376b2cf59SMatthew Knepley 
171476b2cf59SMatthew Knepley   Level: intermediate
171576b2cf59SMatthew Knepley 
171676b2cf59SMatthew Knepley .keywords: SNES, update
1717b5d30489SBarry Smith 
171876b2cf59SMatthew Knepley .seealso SNESDefaultUpdate(), SNESSetRhsBC(), SNESSetSolutionBC()
171976b2cf59SMatthew Knepley @*/
172063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt))
172176b2cf59SMatthew Knepley {
172276b2cf59SMatthew Knepley   PetscFunctionBegin;
17234482741eSBarry Smith   PetscValidHeaderSpecific(snes, SNES_COOKIE,1);
1724e7788613SBarry Smith   snes->ops->update = func;
172576b2cf59SMatthew Knepley   PetscFunctionReturn(0);
172676b2cf59SMatthew Knepley }
172776b2cf59SMatthew Knepley 
1728e74ef692SMatthew Knepley #undef __FUNCT__
1729e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
173076b2cf59SMatthew Knepley /*@
173176b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
173276b2cf59SMatthew Knepley 
173376b2cf59SMatthew Knepley   Not collective
173476b2cf59SMatthew Knepley 
173576b2cf59SMatthew Knepley   Input Parameters:
173676b2cf59SMatthew Knepley . snes - The nonlinear solver context
173776b2cf59SMatthew Knepley . step - The current step of the iteration
173876b2cf59SMatthew Knepley 
1739205452f4SMatthew Knepley   Level: intermediate
1740205452f4SMatthew Knepley 
174176b2cf59SMatthew Knepley .keywords: SNES, update
1742a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC()
174376b2cf59SMatthew Knepley @*/
174463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultUpdate(SNES snes, PetscInt step)
174576b2cf59SMatthew Knepley {
174676b2cf59SMatthew Knepley   PetscFunctionBegin;
174776b2cf59SMatthew Knepley   PetscFunctionReturn(0);
174876b2cf59SMatthew Knepley }
174976b2cf59SMatthew Knepley 
17504a2ae208SSatish Balay #undef __FUNCT__
17514a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
17529b94acceSBarry Smith /*
17539b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
17549b94acceSBarry Smith    positive parameter delta.
17559b94acceSBarry Smith 
17569b94acceSBarry Smith     Input Parameters:
1757c7afd0dbSLois Curfman McInnes +   snes - the SNES context
17589b94acceSBarry Smith .   y - approximate solution of linear system
17599b94acceSBarry Smith .   fnorm - 2-norm of current function
1760c7afd0dbSLois Curfman McInnes -   delta - trust region size
17619b94acceSBarry Smith 
17629b94acceSBarry Smith     Output Parameters:
1763c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
17649b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
17659b94acceSBarry Smith     region, and exceeds zero otherwise.
1766c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
17679b94acceSBarry Smith 
17689b94acceSBarry Smith     Note:
17694b27c08aSLois Curfman McInnes     For non-trust region methods such as SNESLS, the parameter delta
17709b94acceSBarry Smith     is set to be the maximum allowable step size.
17719b94acceSBarry Smith 
17729b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
17739b94acceSBarry Smith */
1774dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
17759b94acceSBarry Smith {
1776064f8208SBarry Smith   PetscReal      nrm;
1777ea709b57SSatish Balay   PetscScalar    cnorm;
1778dfbe8321SBarry Smith   PetscErrorCode ierr;
17793a40ed3dSBarry Smith 
17803a40ed3dSBarry Smith   PetscFunctionBegin;
17814482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
17824482741eSBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE,2);
1783c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,2);
1784184914b5SBarry Smith 
1785064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
1786064f8208SBarry Smith   if (nrm > *delta) {
1787064f8208SBarry Smith      nrm = *delta/nrm;
1788064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
1789064f8208SBarry Smith      cnorm = nrm;
17902dcb1b2aSMatthew Knepley      ierr = VecScale(y,cnorm);CHKERRQ(ierr);
17919b94acceSBarry Smith      *ynorm = *delta;
17929b94acceSBarry Smith   } else {
17939b94acceSBarry Smith      *gpnorm = 0.0;
1794064f8208SBarry Smith      *ynorm = nrm;
17959b94acceSBarry Smith   }
17963a40ed3dSBarry Smith   PetscFunctionReturn(0);
17979b94acceSBarry Smith }
17989b94acceSBarry Smith 
17994a2ae208SSatish Balay #undef __FUNCT__
18004a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
18016ce558aeSBarry Smith /*@C
1802f69a0ea3SMatthew Knepley    SNESSolve - Solves a nonlinear system F(x) = b.
1803f69a0ea3SMatthew Knepley    Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX().
18049b94acceSBarry Smith 
1805c7afd0dbSLois Curfman McInnes    Collective on SNES
1806c7afd0dbSLois Curfman McInnes 
1807b2002411SLois Curfman McInnes    Input Parameters:
1808c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1809f69a0ea3SMatthew Knepley .  b - the constant part of the equation, or PETSC_NULL to use zero.
181070e92668SMatthew Knepley -  x - the solution vector, or PETSC_NULL if it was set with SNESSetSolution()
18119b94acceSBarry Smith 
1812b2002411SLois Curfman McInnes    Notes:
18138ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
18148ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
18158ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
18168ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
18178ddd3da0SLois Curfman McInnes 
181836851e7fSLois Curfman McInnes    Level: beginner
181936851e7fSLois Curfman McInnes 
18209b94acceSBarry Smith .keywords: SNES, nonlinear, solve
18219b94acceSBarry Smith 
182270e92668SMatthew Knepley .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian(), SNESSetRhs(), SNESSetSolution()
18239b94acceSBarry Smith @*/
1824f69a0ea3SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSolve(SNES snes,Vec b,Vec x)
18259b94acceSBarry Smith {
1826dfbe8321SBarry Smith   PetscErrorCode ierr;
1827f1af5d2fSBarry Smith   PetscTruth     flg;
1828eabae89aSBarry Smith   char           filename[PETSC_MAX_PATH_LEN];
1829eabae89aSBarry Smith   PetscViewer    viewer;
1830052efed2SBarry Smith 
18313a40ed3dSBarry Smith   PetscFunctionBegin;
18324482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1833e7788613SBarry Smith   if (!snes->ops->solve) SETERRQ(PETSC_ERR_ORDER,"SNESSetType() or SNESSetFromOptions() must be called before SNESSolve()");
183474637425SBarry Smith 
1835f69a0ea3SMatthew Knepley   if (b) {
1836f69a0ea3SMatthew Knepley     ierr = SNESSetRhs(snes, b); CHKERRQ(ierr);
18371096aae1SMatthew Knepley     if (!snes->vec_func) {
18381096aae1SMatthew Knepley       Vec r;
18391096aae1SMatthew Knepley 
18401096aae1SMatthew Knepley       ierr = VecDuplicate(b, &r); CHKERRQ(ierr);
18411096aae1SMatthew Knepley       ierr = SNESSetFunction(snes, r, PETSC_NULL, PETSC_NULL); CHKERRQ(ierr);
18421096aae1SMatthew Knepley     }
1843f69a0ea3SMatthew Knepley   }
184470e92668SMatthew Knepley   if (x) {
1845f69a0ea3SMatthew Knepley     PetscValidHeaderSpecific(x,VEC_COOKIE,3);
1846f69a0ea3SMatthew Knepley     PetscCheckSameComm(snes,1,x,3);
184770e92668SMatthew Knepley   } else {
184870e92668SMatthew Knepley     ierr = SNESGetSolution(snes, &x); CHKERRQ(ierr);
184970e92668SMatthew Knepley     if (!x) {
185070e92668SMatthew Knepley       ierr = VecDuplicate(snes->vec_func_always, &x); CHKERRQ(ierr);
185170e92668SMatthew Knepley     }
185270e92668SMatthew Knepley   }
185370e92668SMatthew Knepley   snes->vec_sol = snes->vec_sol_always = x;
185470e92668SMatthew Knepley   if (!snes->setupcalled) {
185570e92668SMatthew Knepley     ierr = SNESSetUp(snes);CHKERRQ(ierr);
185670e92668SMatthew Knepley   }
1857abc0a331SBarry Smith   if (snes->conv_hist_reset) snes->conv_hist_len = 0;
1858d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
185950ffb88aSMatthew Knepley   snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
1860d5e45103SBarry Smith 
1861e7788613SBarry Smith   ierr = PetscExceptionTry1((*(snes)->ops->solve)(snes),PETSC_ERR_ARG_DOMAIN);
1862d5e45103SBarry Smith   if (PetscExceptionValue(ierr)) {
186338f152feSBarry Smith     /* this means that a caller above me has also tryed this exception so I don't handle it here, pass it up */
186419717074SBarry Smith     PetscErrorCode pierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(pierr);
1865c671dbe7SSatish Balay   } else if (PetscExceptionCaught(ierr,PETSC_ERR_ARG_DOMAIN)) {
1866d5e45103SBarry Smith     /* translate exception into SNES not converged reason */
1867d5e45103SBarry Smith     snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN;
186838f152feSBarry Smith     ierr = 0;
186938f152feSBarry Smith   }
187038f152feSBarry Smith   CHKERRQ(ierr);
1871d5e45103SBarry Smith 
1872d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
1873eabae89aSBarry Smith   ierr = PetscOptionsGetString(snes->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1874eabae89aSBarry Smith   if (flg && !PetscPreLoadingOn) {
1875eabae89aSBarry Smith     ierr = PetscViewerASCIIOpen(snes->comm,filename,&viewer);CHKERRQ(ierr);
1876eabae89aSBarry Smith     ierr = SNESView(snes,viewer);CHKERRQ(ierr);
1877eabae89aSBarry Smith     ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr);
1878eabae89aSBarry Smith   }
1879eabae89aSBarry Smith 
1880da9b6338SBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_test_local_min",&flg);CHKERRQ(ierr);
1881da9b6338SBarry Smith   if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); }
18825968eb51SBarry Smith   if (snes->printreason) {
18835968eb51SBarry Smith     if (snes->reason > 0) {
18849dcbbd2bSBarry Smith       ierr = PetscPrintf(snes->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
18855968eb51SBarry Smith     } else {
18869dcbbd2bSBarry Smith       ierr = PetscPrintf(snes->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
18875968eb51SBarry Smith     }
18885968eb51SBarry Smith   }
18895968eb51SBarry Smith 
18903a40ed3dSBarry Smith   PetscFunctionReturn(0);
18919b94acceSBarry Smith }
18929b94acceSBarry Smith 
18939b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
18949b94acceSBarry Smith 
18954a2ae208SSatish Balay #undef __FUNCT__
18964a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
189782bf6240SBarry Smith /*@C
18984b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
18999b94acceSBarry Smith 
1900fee21e36SBarry Smith    Collective on SNES
1901fee21e36SBarry Smith 
1902c7afd0dbSLois Curfman McInnes    Input Parameters:
1903c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1904454a90a3SBarry Smith -  type - a known method
1905c7afd0dbSLois Curfman McInnes 
1906c7afd0dbSLois Curfman McInnes    Options Database Key:
1907454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
1908c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
1909ae12b187SLois Curfman McInnes 
19109b94acceSBarry Smith    Notes:
1911e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
19124b27c08aSLois Curfman McInnes +    SNESLS - Newton's method with line search
1913c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
19144b27c08aSLois Curfman McInnes .    SNESTR - Newton's method with trust region
1915c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
19169b94acceSBarry Smith 
1917ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
1918ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
1919ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
1920ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
1921ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
1922ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
1923ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
1924ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
1925ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
1926b0a32e0cSBarry Smith   appropriate method.
192736851e7fSLois Curfman McInnes 
192836851e7fSLois Curfman McInnes   Level: intermediate
1929a703fe33SLois Curfman McInnes 
1930454a90a3SBarry Smith .keywords: SNES, set, type
1931435da068SBarry Smith 
1932435da068SBarry Smith .seealso: SNESType, SNESCreate()
1933435da068SBarry Smith 
19349b94acceSBarry Smith @*/
1935e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetType(SNES snes,SNESType type)
19369b94acceSBarry Smith {
1937dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(SNES);
19386831982aSBarry Smith   PetscTruth     match;
19393a40ed3dSBarry Smith 
19403a40ed3dSBarry Smith   PetscFunctionBegin;
19414482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
19424482741eSBarry Smith   PetscValidCharPointer(type,2);
194382bf6240SBarry Smith 
19446831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
19450f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
194682bf6240SBarry Smith 
194782bf6240SBarry Smith   if (snes->setupcalled) {
19484dc4c822SBarry Smith     snes->setupcalled = PETSC_FALSE;
1949e7788613SBarry Smith     ierr              = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr);
195082bf6240SBarry Smith     snes->data        = 0;
195182bf6240SBarry Smith   }
19527d1a2b2bSBarry Smith 
19539b94acceSBarry Smith   /* Get the function pointers for the iterative method requested */
195482bf6240SBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
1955b9617806SBarry Smith   ierr =  PetscFListFind(snes->comm,SNESList,type,(void (**)(void)) &r);CHKERRQ(ierr);
1956958c9bccSBarry Smith   if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type);
195705b42c5fSBarry Smith   ierr = PetscFree(snes->data);CHKERRQ(ierr);
195882bf6240SBarry Smith   snes->data = 0;
19593a40ed3dSBarry Smith   ierr = (*r)(snes);CHKERRQ(ierr);
1960454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
19613a40ed3dSBarry Smith   PetscFunctionReturn(0);
19629b94acceSBarry Smith }
19639b94acceSBarry Smith 
1964a847f771SSatish Balay 
19659b94acceSBarry Smith /* --------------------------------------------------------------------- */
19664a2ae208SSatish Balay #undef __FUNCT__
19674a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
196852baeb72SSatish Balay /*@
19699b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
1970f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
19719b94acceSBarry Smith 
1972fee21e36SBarry Smith    Not Collective
1973fee21e36SBarry Smith 
197436851e7fSLois Curfman McInnes    Level: advanced
197536851e7fSLois Curfman McInnes 
19769b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
19779b94acceSBarry Smith 
19789b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
19799b94acceSBarry Smith @*/
198063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegisterDestroy(void)
19819b94acceSBarry Smith {
1982dfbe8321SBarry Smith   PetscErrorCode ierr;
198382bf6240SBarry Smith 
19843a40ed3dSBarry Smith   PetscFunctionBegin;
198582bf6240SBarry Smith   if (SNESList) {
1986b0a32e0cSBarry Smith     ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
198782bf6240SBarry Smith     SNESList = 0;
19889b94acceSBarry Smith   }
19894c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
19903a40ed3dSBarry Smith   PetscFunctionReturn(0);
19919b94acceSBarry Smith }
19929b94acceSBarry Smith 
19934a2ae208SSatish Balay #undef __FUNCT__
19944a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
19959b94acceSBarry Smith /*@C
19969a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
19979b94acceSBarry Smith 
1998c7afd0dbSLois Curfman McInnes    Not Collective
1999c7afd0dbSLois Curfman McInnes 
20009b94acceSBarry Smith    Input Parameter:
20014b0e389bSBarry Smith .  snes - nonlinear solver context
20029b94acceSBarry Smith 
20039b94acceSBarry Smith    Output Parameter:
20043a7fca6bSBarry Smith .  type - SNES method (a character string)
20059b94acceSBarry Smith 
200636851e7fSLois Curfman McInnes    Level: intermediate
200736851e7fSLois Curfman McInnes 
2008454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
20099b94acceSBarry Smith @*/
201063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetType(SNES snes,SNESType *type)
20119b94acceSBarry Smith {
20123a40ed3dSBarry Smith   PetscFunctionBegin;
20134482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
20144482741eSBarry Smith   PetscValidPointer(type,2);
2015454a90a3SBarry Smith   *type = snes->type_name;
20163a40ed3dSBarry Smith   PetscFunctionReturn(0);
20179b94acceSBarry Smith }
20189b94acceSBarry Smith 
20194a2ae208SSatish Balay #undef __FUNCT__
20204a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
202152baeb72SSatish Balay /*@
20229b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
20239b94acceSBarry Smith    stored.
20249b94acceSBarry Smith 
2025c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2026c7afd0dbSLois Curfman McInnes 
20279b94acceSBarry Smith    Input Parameter:
20289b94acceSBarry Smith .  snes - the SNES context
20299b94acceSBarry Smith 
20309b94acceSBarry Smith    Output Parameter:
20319b94acceSBarry Smith .  x - the solution
20329b94acceSBarry Smith 
203370e92668SMatthew Knepley    Level: intermediate
203436851e7fSLois Curfman McInnes 
20359b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
20369b94acceSBarry Smith 
203770e92668SMatthew Knepley .seealso: SNESSetSolution(), SNESGetFunction(), SNESGetSolutionUpdate()
20389b94acceSBarry Smith @*/
203963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolution(SNES snes,Vec *x)
20409b94acceSBarry Smith {
20413a40ed3dSBarry Smith   PetscFunctionBegin;
20424482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
20434482741eSBarry Smith   PetscValidPointer(x,2);
20449b94acceSBarry Smith   *x = snes->vec_sol_always;
20453a40ed3dSBarry Smith   PetscFunctionReturn(0);
20469b94acceSBarry Smith }
20479b94acceSBarry Smith 
20484a2ae208SSatish Balay #undef __FUNCT__
204970e92668SMatthew Knepley #define __FUNCT__ "SNESSetSolution"
20507e4bb74cSBarry Smith /*@
205170e92668SMatthew Knepley    SNESSetSolution - Sets the vector where the approximate solution is stored.
205270e92668SMatthew Knepley 
205370e92668SMatthew Knepley    Not Collective, but Vec is parallel if SNES is parallel
205470e92668SMatthew Knepley 
205570e92668SMatthew Knepley    Input Parameters:
205670e92668SMatthew Knepley +  snes - the SNES context
205770e92668SMatthew Knepley -  x - the solution
205870e92668SMatthew Knepley 
205970e92668SMatthew Knepley    Output Parameter:
206070e92668SMatthew Knepley 
206170e92668SMatthew Knepley    Level: intermediate
206270e92668SMatthew Knepley 
206342219521SBarry Smith    Notes: this is not normally used, rather one simply calls SNESSolve() with
206442219521SBarry Smith           the appropriate solution vector.
206542219521SBarry Smith 
206670e92668SMatthew Knepley .keywords: SNES, nonlinear, set, solution
206770e92668SMatthew Knepley 
206870e92668SMatthew Knepley .seealso: SNESGetSolution(), SNESGetFunction(), SNESGetSolutionUpdate()
206970e92668SMatthew Knepley @*/
207070e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSetSolution(SNES snes,Vec x)
207170e92668SMatthew Knepley {
207270e92668SMatthew Knepley   PetscFunctionBegin;
207370e92668SMatthew Knepley   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
207470e92668SMatthew Knepley   PetscValidHeaderSpecific(x,VEC_COOKIE,2);
207570e92668SMatthew Knepley   PetscCheckSameComm(snes,1,x,2);
207670e92668SMatthew Knepley   snes->vec_sol_always = x;
207770e92668SMatthew Knepley   PetscFunctionReturn(0);
207870e92668SMatthew Knepley }
207970e92668SMatthew Knepley 
208070e92668SMatthew Knepley #undef __FUNCT__
20814a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
208252baeb72SSatish Balay /*@
20839b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
20849b94acceSBarry Smith    stored.
20859b94acceSBarry Smith 
2086c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2087c7afd0dbSLois Curfman McInnes 
20889b94acceSBarry Smith    Input Parameter:
20899b94acceSBarry Smith .  snes - the SNES context
20909b94acceSBarry Smith 
20919b94acceSBarry Smith    Output Parameter:
20929b94acceSBarry Smith .  x - the solution update
20939b94acceSBarry Smith 
209436851e7fSLois Curfman McInnes    Level: advanced
209536851e7fSLois Curfman McInnes 
20969b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
20979b94acceSBarry Smith 
20989b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction
20999b94acceSBarry Smith @*/
210063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolutionUpdate(SNES snes,Vec *x)
21019b94acceSBarry Smith {
21023a40ed3dSBarry Smith   PetscFunctionBegin;
21034482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
21044482741eSBarry Smith   PetscValidPointer(x,2);
21059b94acceSBarry Smith   *x = snes->vec_sol_update_always;
21063a40ed3dSBarry Smith   PetscFunctionReturn(0);
21079b94acceSBarry Smith }
21089b94acceSBarry Smith 
21094a2ae208SSatish Balay #undef __FUNCT__
21104a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
21119b94acceSBarry Smith /*@C
21123638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
21139b94acceSBarry Smith 
2114c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2115c7afd0dbSLois Curfman McInnes 
21169b94acceSBarry Smith    Input Parameter:
21179b94acceSBarry Smith .  snes - the SNES context
21189b94acceSBarry Smith 
21199b94acceSBarry Smith    Output Parameter:
21207bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
212170e92668SMatthew Knepley .  func - the function (or PETSC_NULL)
212270e92668SMatthew Knepley -  ctx - the function context (or PETSC_NULL)
21239b94acceSBarry Smith 
212436851e7fSLois Curfman McInnes    Level: advanced
212536851e7fSLois Curfman McInnes 
2126a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
21279b94acceSBarry Smith 
21284b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution()
21299b94acceSBarry Smith @*/
213070e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx)
21319b94acceSBarry Smith {
21323a40ed3dSBarry Smith   PetscFunctionBegin;
21334482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
21347bf4e008SBarry Smith   if (r)    *r    = snes->vec_func_always;
2135e7788613SBarry Smith   if (func) *func = snes->ops->computefunction;
213670e92668SMatthew Knepley   if (ctx)  *ctx  = snes->funP;
21373a40ed3dSBarry Smith   PetscFunctionReturn(0);
21389b94acceSBarry Smith }
21399b94acceSBarry Smith 
21404a2ae208SSatish Balay #undef __FUNCT__
21414a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
21423c7409f5SSatish Balay /*@C
21433c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2144d850072dSLois Curfman McInnes    SNES options in the database.
21453c7409f5SSatish Balay 
2146fee21e36SBarry Smith    Collective on SNES
2147fee21e36SBarry Smith 
2148c7afd0dbSLois Curfman McInnes    Input Parameter:
2149c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2150c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2151c7afd0dbSLois Curfman McInnes 
2152d850072dSLois Curfman McInnes    Notes:
2153a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2154c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2155d850072dSLois Curfman McInnes 
215636851e7fSLois Curfman McInnes    Level: advanced
215736851e7fSLois Curfman McInnes 
21583c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2159a86d99e1SLois Curfman McInnes 
2160a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
21613c7409f5SSatish Balay @*/
216263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetOptionsPrefix(SNES snes,const char prefix[])
21633c7409f5SSatish Balay {
2164dfbe8321SBarry Smith   PetscErrorCode ierr;
21653c7409f5SSatish Balay 
21663a40ed3dSBarry Smith   PetscFunctionBegin;
21674482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2168639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
216994b7f48cSBarry Smith   ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
21703a40ed3dSBarry Smith   PetscFunctionReturn(0);
21713c7409f5SSatish Balay }
21723c7409f5SSatish Balay 
21734a2ae208SSatish Balay #undef __FUNCT__
21744a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
21753c7409f5SSatish Balay /*@C
2176f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2177d850072dSLois Curfman McInnes    SNES options in the database.
21783c7409f5SSatish Balay 
2179fee21e36SBarry Smith    Collective on SNES
2180fee21e36SBarry Smith 
2181c7afd0dbSLois Curfman McInnes    Input Parameters:
2182c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2183c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2184c7afd0dbSLois Curfman McInnes 
2185d850072dSLois Curfman McInnes    Notes:
2186a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2187c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2188d850072dSLois Curfman McInnes 
218936851e7fSLois Curfman McInnes    Level: advanced
219036851e7fSLois Curfman McInnes 
21913c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2192a86d99e1SLois Curfman McInnes 
2193a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
21943c7409f5SSatish Balay @*/
219563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAppendOptionsPrefix(SNES snes,const char prefix[])
21963c7409f5SSatish Balay {
2197dfbe8321SBarry Smith   PetscErrorCode ierr;
21983c7409f5SSatish Balay 
21993a40ed3dSBarry Smith   PetscFunctionBegin;
22004482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2201639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
220294b7f48cSBarry Smith   ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
22033a40ed3dSBarry Smith   PetscFunctionReturn(0);
22043c7409f5SSatish Balay }
22053c7409f5SSatish Balay 
22064a2ae208SSatish Balay #undef __FUNCT__
22074a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
22089ab63eb5SSatish Balay /*@C
22093c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
22103c7409f5SSatish Balay    SNES options in the database.
22113c7409f5SSatish Balay 
2212c7afd0dbSLois Curfman McInnes    Not Collective
2213c7afd0dbSLois Curfman McInnes 
22143c7409f5SSatish Balay    Input Parameter:
22153c7409f5SSatish Balay .  snes - the SNES context
22163c7409f5SSatish Balay 
22173c7409f5SSatish Balay    Output Parameter:
22183c7409f5SSatish Balay .  prefix - pointer to the prefix string used
22193c7409f5SSatish Balay 
22209ab63eb5SSatish Balay    Notes: On the fortran side, the user should pass in a string 'prifix' of
22219ab63eb5SSatish Balay    sufficient length to hold the prefix.
22229ab63eb5SSatish Balay 
222336851e7fSLois Curfman McInnes    Level: advanced
222436851e7fSLois Curfman McInnes 
22253c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2226a86d99e1SLois Curfman McInnes 
2227a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
22283c7409f5SSatish Balay @*/
2229e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetOptionsPrefix(SNES snes,const char *prefix[])
22303c7409f5SSatish Balay {
2231dfbe8321SBarry Smith   PetscErrorCode ierr;
22323c7409f5SSatish Balay 
22333a40ed3dSBarry Smith   PetscFunctionBegin;
22344482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2235639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
22363a40ed3dSBarry Smith   PetscFunctionReturn(0);
22373c7409f5SSatish Balay }
22383c7409f5SSatish Balay 
2239b2002411SLois Curfman McInnes 
22404a2ae208SSatish Balay #undef __FUNCT__
22414a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
22423cea93caSBarry Smith /*@C
22433cea93caSBarry Smith   SNESRegister - See SNESRegisterDynamic()
22443cea93caSBarry Smith 
22457f6c08e0SMatthew Knepley   Level: advanced
22463cea93caSBarry Smith @*/
224763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES))
2248b2002411SLois Curfman McInnes {
2249e2d1d2b7SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
2250dfbe8321SBarry Smith   PetscErrorCode ierr;
2251b2002411SLois Curfman McInnes 
2252b2002411SLois Curfman McInnes   PetscFunctionBegin;
2253b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2254c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
2255b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2256b2002411SLois Curfman McInnes }
2257da9b6338SBarry Smith 
2258da9b6338SBarry Smith #undef __FUNCT__
2259da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin"
226063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESTestLocalMin(SNES snes)
2261da9b6338SBarry Smith {
2262dfbe8321SBarry Smith   PetscErrorCode ierr;
226377431f27SBarry Smith   PetscInt       N,i,j;
2264da9b6338SBarry Smith   Vec            u,uh,fh;
2265da9b6338SBarry Smith   PetscScalar    value;
2266da9b6338SBarry Smith   PetscReal      norm;
2267da9b6338SBarry Smith 
2268da9b6338SBarry Smith   PetscFunctionBegin;
2269da9b6338SBarry Smith   ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr);
2270da9b6338SBarry Smith   ierr = VecDuplicate(u,&uh);CHKERRQ(ierr);
2271da9b6338SBarry Smith   ierr = VecDuplicate(u,&fh);CHKERRQ(ierr);
2272da9b6338SBarry Smith 
2273da9b6338SBarry Smith   /* currently only works for sequential */
2274da9b6338SBarry Smith   ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n");
2275da9b6338SBarry Smith   ierr = VecGetSize(u,&N);CHKERRQ(ierr);
2276da9b6338SBarry Smith   for (i=0; i<N; i++) {
2277da9b6338SBarry Smith     ierr = VecCopy(u,uh);CHKERRQ(ierr);
227877431f27SBarry Smith     ierr  = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr);
2279da9b6338SBarry Smith     for (j=-10; j<11; j++) {
2280ccae9161SBarry Smith       value = PetscSign(j)*exp(PetscAbs(j)-10.0);
2281da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
22823ab0aad5SBarry Smith       ierr  = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr);
2283da9b6338SBarry Smith       ierr  = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr);
228477431f27SBarry Smith       ierr  = PetscPrintf(PETSC_COMM_WORLD,"       j norm %D %18.16e\n",j,norm);CHKERRQ(ierr);
2285da9b6338SBarry Smith       value = -value;
2286da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
2287da9b6338SBarry Smith     }
2288da9b6338SBarry Smith   }
2289da9b6338SBarry Smith   ierr = VecDestroy(uh);CHKERRQ(ierr);
2290da9b6338SBarry Smith   ierr = VecDestroy(fh);CHKERRQ(ierr);
2291da9b6338SBarry Smith   PetscFunctionReturn(0);
2292da9b6338SBarry Smith }
2293*71f87433Sdalcinl 
2294*71f87433Sdalcinl #undef __FUNCT__
2295*71f87433Sdalcinl #define __FUNCT__ "SNES_KSP_SetUseEW"
2296*71f87433Sdalcinl /*@
2297*71f87433Sdalcinl    SNES_KSP_SetUseEW - Sets SNES use Eisenstat-Walker method for
2298*71f87433Sdalcinl    computing relative tolerance for linear solvers within an inexact
2299*71f87433Sdalcinl    Newton method.
2300*71f87433Sdalcinl 
2301*71f87433Sdalcinl    Collective on SNES
2302*71f87433Sdalcinl 
2303*71f87433Sdalcinl    Input Parameters:
2304*71f87433Sdalcinl +  snes - SNES context
2305*71f87433Sdalcinl -  flag - PETSC_TRUE or PETSC_FALSE
2306*71f87433Sdalcinl 
2307*71f87433Sdalcinl    Notes:
2308*71f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
2309*71f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
2310*71f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
2311*71f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
2312*71f87433Sdalcinl    solver.
2313*71f87433Sdalcinl 
2314*71f87433Sdalcinl    Level: advanced
2315*71f87433Sdalcinl 
2316*71f87433Sdalcinl    Reference:
2317*71f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
2318*71f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
2319*71f87433Sdalcinl 
2320*71f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
2321*71f87433Sdalcinl 
2322*71f87433Sdalcinl .seealso: SNES_KSP_GetUseEW(), SNES_KSP_GetParametersEW(), SNES_KSP_SetParametersEW()
2323*71f87433Sdalcinl @*/
2324*71f87433Sdalcinl PetscErrorCode PETSCSNES_DLLEXPORT SNES_KSP_SetUseEW(SNES snes,PetscTruth flag)
2325*71f87433Sdalcinl {
2326*71f87433Sdalcinl   PetscFunctionBegin;
2327*71f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2328*71f87433Sdalcinl   snes->ksp_ewconv = flag;
2329*71f87433Sdalcinl   PetscFunctionReturn(0);
2330*71f87433Sdalcinl }
2331*71f87433Sdalcinl 
2332*71f87433Sdalcinl #undef __FUNCT__
2333*71f87433Sdalcinl #define __FUNCT__ "SNES_KSP_GetUseEW"
2334*71f87433Sdalcinl /*@
2335*71f87433Sdalcinl    SNES_KSP_GetUseEW - Gets if SNES is using Eisenstat-Walker method
2336*71f87433Sdalcinl    for computing relative tolerance for linear solvers within an
2337*71f87433Sdalcinl    inexact Newton method.
2338*71f87433Sdalcinl 
2339*71f87433Sdalcinl    Not Collective
2340*71f87433Sdalcinl 
2341*71f87433Sdalcinl    Input Parameter:
2342*71f87433Sdalcinl .  snes - SNES context
2343*71f87433Sdalcinl 
2344*71f87433Sdalcinl    Output Parameter:
2345*71f87433Sdalcinl .  flag - PETSC_TRUE or PETSC_FALSE
2346*71f87433Sdalcinl 
2347*71f87433Sdalcinl    Notes:
2348*71f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
2349*71f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
2350*71f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
2351*71f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
2352*71f87433Sdalcinl    solver.
2353*71f87433Sdalcinl 
2354*71f87433Sdalcinl    Level: advanced
2355*71f87433Sdalcinl 
2356*71f87433Sdalcinl    Reference:
2357*71f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
2358*71f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
2359*71f87433Sdalcinl 
2360*71f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
2361*71f87433Sdalcinl 
2362*71f87433Sdalcinl .seealso: SNES_KSP_SetUseEW(), SNES_KSP_GetParametersEW(), SNES_KSP_SetParametersEW()
2363*71f87433Sdalcinl @*/
2364*71f87433Sdalcinl PetscErrorCode PETSCSNES_DLLEXPORT SNES_KSP_GetUseEW(SNES snes, PetscTruth *flag)
2365*71f87433Sdalcinl {
2366*71f87433Sdalcinl   PetscFunctionBegin;
2367*71f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2368*71f87433Sdalcinl   PetscValidPointer(flag,2);
2369*71f87433Sdalcinl   *flag = snes->ksp_ewconv;
2370*71f87433Sdalcinl   PetscFunctionReturn(0);
2371*71f87433Sdalcinl }
2372*71f87433Sdalcinl 
2373*71f87433Sdalcinl #undef __FUNCT__
2374*71f87433Sdalcinl #define __FUNCT__ "SNES_KSP_SetParametersEW"
2375*71f87433Sdalcinl /*@
2376*71f87433Sdalcinl    SNES_KSP_SetParametersEW - Sets parameters for Eisenstat-Walker
2377*71f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
2378*71f87433Sdalcinl    Newton method.
2379*71f87433Sdalcinl 
2380*71f87433Sdalcinl    Collective on SNES
2381*71f87433Sdalcinl 
2382*71f87433Sdalcinl    Input Parameters:
2383*71f87433Sdalcinl +    snes - SNES context
2384*71f87433Sdalcinl .    version - version 1, 2 (default is 2) or 3
2385*71f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
2386*71f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
2387*71f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
2388*71f87433Sdalcinl              (0 <= gamma2 <= 1)
2389*71f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
2390*71f87433Sdalcinl .    alpha2 - power for safeguard
2391*71f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
2392*71f87433Sdalcinl 
2393*71f87433Sdalcinl    Note:
2394*71f87433Sdalcinl    Version 3 was contributed by Luis Chacon, June 2006.
2395*71f87433Sdalcinl 
2396*71f87433Sdalcinl    Use PETSC_DEFAULT to retain the default for any of the parameters.
2397*71f87433Sdalcinl 
2398*71f87433Sdalcinl    Level: advanced
2399*71f87433Sdalcinl 
2400*71f87433Sdalcinl    Reference:
2401*71f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
2402*71f87433Sdalcinl    inexact Newton method", Utah State University Math. Stat. Dept. Res.
2403*71f87433Sdalcinl    Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput.
2404*71f87433Sdalcinl 
2405*71f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters
2406*71f87433Sdalcinl 
2407*71f87433Sdalcinl .seealso: SNES_KSP_SetUseEW(), SNES_KSP_GetUseEW(), SNES_KSP_GetParametersEW()
2408*71f87433Sdalcinl @*/
2409*71f87433Sdalcinl PetscErrorCode PETSCSNES_DLLEXPORT SNES_KSP_SetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max,
2410*71f87433Sdalcinl 							    PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold)
2411*71f87433Sdalcinl {
2412*71f87433Sdalcinl   SNES_KSP_EW_ConvCtx *kctx;
2413*71f87433Sdalcinl   PetscFunctionBegin;
2414*71f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2415*71f87433Sdalcinl   kctx = (SNES_KSP_EW_ConvCtx*)snes->kspconvctx;
2416*71f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
2417*71f87433Sdalcinl 
2418*71f87433Sdalcinl   if (version != PETSC_DEFAULT)   kctx->version   = version;
2419*71f87433Sdalcinl   if (rtol_0 != PETSC_DEFAULT)    kctx->rtol_0    = rtol_0;
2420*71f87433Sdalcinl   if (rtol_max != PETSC_DEFAULT)  kctx->rtol_max  = rtol_max;
2421*71f87433Sdalcinl   if (gamma != PETSC_DEFAULT)     kctx->gamma     = gamma;
2422*71f87433Sdalcinl   if (alpha != PETSC_DEFAULT)     kctx->alpha     = alpha;
2423*71f87433Sdalcinl   if (alpha2 != PETSC_DEFAULT)    kctx->alpha2    = alpha2;
2424*71f87433Sdalcinl   if (threshold != PETSC_DEFAULT) kctx->threshold = threshold;
2425*71f87433Sdalcinl 
2426*71f87433Sdalcinl   if (kctx->version < 1 || kctx->version > 3) {
2427*71f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version);
2428*71f87433Sdalcinl   }
2429*71f87433Sdalcinl   if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) {
2430*71f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0);
2431*71f87433Sdalcinl   }
2432*71f87433Sdalcinl   if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) {
2433*71f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max);
2434*71f87433Sdalcinl   }
2435*71f87433Sdalcinl   if (kctx->gamma < 0.0 || kctx->gamma > 1.0) {
2436*71f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma);
2437*71f87433Sdalcinl   }
2438*71f87433Sdalcinl   if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) {
2439*71f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha);
2440*71f87433Sdalcinl   }
2441*71f87433Sdalcinl   if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) {
2442*71f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold);
2443*71f87433Sdalcinl   }
2444*71f87433Sdalcinl   PetscFunctionReturn(0);
2445*71f87433Sdalcinl }
2446*71f87433Sdalcinl 
2447*71f87433Sdalcinl #undef __FUNCT__
2448*71f87433Sdalcinl #define __FUNCT__ "SNES_KSP_GetParametersEW"
2449*71f87433Sdalcinl /*@
2450*71f87433Sdalcinl    SNES_KSP_GetParametersEW - Gets parameters for Eisenstat-Walker
2451*71f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
2452*71f87433Sdalcinl    Newton method.
2453*71f87433Sdalcinl 
2454*71f87433Sdalcinl    Not Collective
2455*71f87433Sdalcinl 
2456*71f87433Sdalcinl    Input Parameters:
2457*71f87433Sdalcinl      snes - SNES context
2458*71f87433Sdalcinl 
2459*71f87433Sdalcinl    Output Parameters:
2460*71f87433Sdalcinl +    version - version 1, 2 (default is 2) or 3
2461*71f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
2462*71f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
2463*71f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
2464*71f87433Sdalcinl              (0 <= gamma2 <= 1)
2465*71f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
2466*71f87433Sdalcinl .    alpha2 - power for safeguard
2467*71f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
2468*71f87433Sdalcinl 
2469*71f87433Sdalcinl    Level: advanced
2470*71f87433Sdalcinl 
2471*71f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters
2472*71f87433Sdalcinl 
2473*71f87433Sdalcinl .seealso: SNES_KSP_SetUseEW(), SNES_KSP_GetUseEW(), SNES_KSP_SetParametersEW()
2474*71f87433Sdalcinl @*/
2475*71f87433Sdalcinl PetscErrorCode PETSCSNES_DLLEXPORT SNES_KSP_GetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max,
2476*71f87433Sdalcinl 							    PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold)
2477*71f87433Sdalcinl {
2478*71f87433Sdalcinl   SNES_KSP_EW_ConvCtx *kctx;
2479*71f87433Sdalcinl   PetscFunctionBegin;
2480*71f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2481*71f87433Sdalcinl   kctx = (SNES_KSP_EW_ConvCtx*)snes->kspconvctx;
2482*71f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
2483*71f87433Sdalcinl   if(version)   *version   = kctx->version;
2484*71f87433Sdalcinl   if(rtol_0)    *rtol_0    = kctx->rtol_0;
2485*71f87433Sdalcinl   if(rtol_max)  *rtol_max  = kctx->rtol_max;
2486*71f87433Sdalcinl   if(gamma)     *gamma     = kctx->gamma;
2487*71f87433Sdalcinl   if(alpha)     *alpha     = kctx->alpha;
2488*71f87433Sdalcinl   if(alpha2)    *alpha2    = kctx->alpha2;
2489*71f87433Sdalcinl   if(threshold) *threshold = kctx->threshold;
2490*71f87433Sdalcinl   PetscFunctionReturn(0);
2491*71f87433Sdalcinl }
2492*71f87433Sdalcinl 
2493*71f87433Sdalcinl #undef __FUNCT__
2494*71f87433Sdalcinl #define __FUNCT__ "SNES_KSP_EW_PreSolve"
2495*71f87433Sdalcinl static PetscErrorCode SNES_KSP_EW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x)
2496*71f87433Sdalcinl {
2497*71f87433Sdalcinl   PetscErrorCode       ierr;
2498*71f87433Sdalcinl   SNES_KSP_EW_ConvCtx *kctx = (SNES_KSP_EW_ConvCtx*)snes->kspconvctx;
2499*71f87433Sdalcinl   PetscReal            rtol=PETSC_DEFAULT,stol;
2500*71f87433Sdalcinl 
2501*71f87433Sdalcinl   PetscFunctionBegin;
2502*71f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
2503*71f87433Sdalcinl   if (!snes->iter) { /* first time in, so use the original user rtol */
2504*71f87433Sdalcinl     rtol = kctx->rtol_0;
2505*71f87433Sdalcinl   } else {
2506*71f87433Sdalcinl     if (kctx->version == 1) {
2507*71f87433Sdalcinl       rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last;
2508*71f87433Sdalcinl       if (rtol < 0.0) rtol = -rtol;
2509*71f87433Sdalcinl       stol = pow(kctx->rtol_last,kctx->alpha2);
2510*71f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
2511*71f87433Sdalcinl     } else if (kctx->version == 2) {
2512*71f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
2513*71f87433Sdalcinl       stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha);
2514*71f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
2515*71f87433Sdalcinl     } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */
2516*71f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
2517*71f87433Sdalcinl       /* safeguard: avoid sharp decrease of rtol */
2518*71f87433Sdalcinl       stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha);
2519*71f87433Sdalcinl       stol = PetscMax(rtol,stol);
2520*71f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
2521*71f87433Sdalcinl       /* safeguard: avoid oversolving */
2522*71f87433Sdalcinl       stol = kctx->gamma*(snes->ttol)/snes->norm;
2523*71f87433Sdalcinl       stol = PetscMax(rtol,stol);
2524*71f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
2525*71f87433Sdalcinl     } else SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version);
2526*71f87433Sdalcinl   }
2527*71f87433Sdalcinl   /* safeguard: avoid rtol greater than one */
2528*71f87433Sdalcinl   rtol = PetscMin(rtol,kctx->rtol_max);
2529*71f87433Sdalcinl   ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
2530*71f87433Sdalcinl   ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr);
2531*71f87433Sdalcinl   PetscFunctionReturn(0);
2532*71f87433Sdalcinl }
2533*71f87433Sdalcinl 
2534*71f87433Sdalcinl #undef __FUNCT__
2535*71f87433Sdalcinl #define __FUNCT__ "SNES_KSP_EW_PostSolve"
2536*71f87433Sdalcinl static PetscErrorCode SNES_KSP_EW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x)
2537*71f87433Sdalcinl {
2538*71f87433Sdalcinl   PetscErrorCode      ierr;
2539*71f87433Sdalcinl   SNES_KSP_EW_ConvCtx *kctx = (SNES_KSP_EW_ConvCtx*)snes->kspconvctx;
2540*71f87433Sdalcinl   PCSide              pcside;
2541*71f87433Sdalcinl   Vec                 lres;
2542*71f87433Sdalcinl 
2543*71f87433Sdalcinl   PetscFunctionBegin;
2544*71f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
2545*71f87433Sdalcinl   ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr);
2546*71f87433Sdalcinl   ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr);
2547*71f87433Sdalcinl   if (kctx->version == 1) {
2548*71f87433Sdalcinl     ierr = KSPGetPreconditionerSide(ksp,&pcside);CHKERRQ(ierr);
2549*71f87433Sdalcinl     if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */
2550*71f87433Sdalcinl       /* KSP residual is true linear residual */
2551*71f87433Sdalcinl       ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr);
2552*71f87433Sdalcinl     } else {
2553*71f87433Sdalcinl       /* KSP residual is preconditioned residual */
2554*71f87433Sdalcinl       /* compute true linear residual norm */
2555*71f87433Sdalcinl       ierr = VecDuplicate(b,&lres);CHKERRQ(ierr);
2556*71f87433Sdalcinl       ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr);
2557*71f87433Sdalcinl       ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr);
2558*71f87433Sdalcinl       ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr);
2559*71f87433Sdalcinl       ierr = VecDestroy(lres);CHKERRQ(ierr);
2560*71f87433Sdalcinl     }
2561*71f87433Sdalcinl   }
2562*71f87433Sdalcinl   PetscFunctionReturn(0);
2563*71f87433Sdalcinl }
2564*71f87433Sdalcinl 
2565*71f87433Sdalcinl #undef __FUNCT__
2566*71f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve"
2567*71f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x)
2568*71f87433Sdalcinl {
2569*71f87433Sdalcinl   PetscErrorCode ierr;
2570*71f87433Sdalcinl 
2571*71f87433Sdalcinl   PetscFunctionBegin;
2572*71f87433Sdalcinl   if (snes->ksp_ewconv) { ierr = SNES_KSP_EW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr);  }
2573*71f87433Sdalcinl   ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);
2574*71f87433Sdalcinl   if (snes->ksp_ewconv) { ierr = SNES_KSP_EW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); }
2575*71f87433Sdalcinl   PetscFunctionReturn(0);
2576*71f87433Sdalcinl }
2577