xref: /petsc/src/snes/interface/snes.c (revision 7f7931b9d9a589c65ad7731d5a63b104a80e0f5a)
163dd3a1aSKris Buschelman #define PETSCSNES_DLL
29b94acceSBarry Smith 
3b9147fbbSdalcinl #include "include/private/snesimpl.h"      /*I "petscsnes.h"  I*/
49b94acceSBarry Smith 
54c49b128SBarry Smith PetscTruth SNESRegisterAllCalled = PETSC_FALSE;
68ba1e511SMatthew Knepley PetscFList SNESList              = PETSC_NULL;
78ba1e511SMatthew Knepley 
88ba1e511SMatthew Knepley /* Logging support */
963dd3a1aSKris Buschelman PetscCookie PETSCSNES_DLLEXPORT SNES_COOKIE = 0;
106849ba73SBarry Smith PetscEvent  SNES_Solve = 0, SNES_LineSearch = 0, SNES_FunctionEval = 0, SNES_JacobianEval = 0;
11a09944afSBarry Smith 
12a09944afSBarry Smith #undef __FUNCT__
134936397dSBarry Smith #define __FUNCT__ "SNESSetFunctionDomainError"
144936397dSBarry Smith /*@C
154936397dSBarry Smith    SNESSetFunctionDomainError - tells SNES that the input vector to your FormFunction is not
164936397dSBarry Smith      in the functions domain. For example, negative pressure.
174936397dSBarry Smith 
184936397dSBarry Smith    Collective on SNES
194936397dSBarry Smith 
204936397dSBarry Smith    Input Parameters:
214936397dSBarry Smith .  SNES - the SNES context
224936397dSBarry Smith 
2328529972SSatish Balay    Level: advanced
244936397dSBarry Smith 
254936397dSBarry Smith .keywords: SNES, view
264936397dSBarry Smith 
274936397dSBarry Smith .seealso: SNESCreate(), SNESSetFunction()
284936397dSBarry Smith @*/
294936397dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFunctionDomainError(SNES snes)
304936397dSBarry Smith {
314936397dSBarry Smith   PetscFunctionBegin;
324936397dSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
334936397dSBarry Smith   snes->domainerror = PETSC_TRUE;
344936397dSBarry Smith   PetscFunctionReturn(0);
354936397dSBarry Smith }
364936397dSBarry Smith 
374936397dSBarry Smith #undef __FUNCT__
384a2ae208SSatish Balay #define __FUNCT__ "SNESView"
397e2c5f70SBarry Smith /*@C
409b94acceSBarry Smith    SNESView - Prints the SNES data structure.
419b94acceSBarry Smith 
424c49b128SBarry Smith    Collective on SNES
43fee21e36SBarry Smith 
44c7afd0dbSLois Curfman McInnes    Input Parameters:
45c7afd0dbSLois Curfman McInnes +  SNES - the SNES context
46c7afd0dbSLois Curfman McInnes -  viewer - visualization context
47c7afd0dbSLois Curfman McInnes 
489b94acceSBarry Smith    Options Database Key:
49c8a8ba5cSLois Curfman McInnes .  -snes_view - Calls SNESView() at end of SNESSolve()
509b94acceSBarry Smith 
519b94acceSBarry Smith    Notes:
529b94acceSBarry Smith    The available visualization contexts include
53b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
54b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
55c8a8ba5cSLois Curfman McInnes          output where only the first processor opens
56c8a8ba5cSLois Curfman McInnes          the file.  All other processors send their
57c8a8ba5cSLois Curfman McInnes          data to the first processor to print.
589b94acceSBarry Smith 
593e081fefSLois Curfman McInnes    The user can open an alternative visualization context with
60b0a32e0cSBarry Smith    PetscViewerASCIIOpen() - output to a specified file.
619b94acceSBarry Smith 
6236851e7fSLois Curfman McInnes    Level: beginner
6336851e7fSLois Curfman McInnes 
649b94acceSBarry Smith .keywords: SNES, view
659b94acceSBarry Smith 
66b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
679b94acceSBarry Smith @*/
6863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESView(SNES snes,PetscViewer viewer)
699b94acceSBarry Smith {
70fa9f3622SBarry Smith   SNESKSPEW           *kctx;
71dfbe8321SBarry Smith   PetscErrorCode      ierr;
7294b7f48cSBarry Smith   KSP                 ksp;
73e060cb09SBarry Smith   SNESType            type;
7432077d6dSBarry Smith   PetscTruth          iascii,isstring;
759b94acceSBarry Smith 
763a40ed3dSBarry Smith   PetscFunctionBegin;
774482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
783050cee2SBarry Smith   if (!viewer) {
797adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&viewer);CHKERRQ(ierr);
803050cee2SBarry Smith   }
814482741eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE,2);
82c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,viewer,2);
8374679c65SBarry Smith 
8432077d6dSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
85b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);CHKERRQ(ierr);
8632077d6dSBarry Smith   if (iascii) {
877adad957SLisandro Dalcin     if (((PetscObject)snes)->prefix) {
887adad957SLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:(%s)\n",((PetscObject)snes)->prefix);CHKERRQ(ierr);
893a7fca6bSBarry Smith     } else {
90b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:\n");CHKERRQ(ierr);
913a7fca6bSBarry Smith     }
92454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
93454a90a3SBarry Smith     if (type) {
94b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: %s\n",type);CHKERRQ(ierr);
95184914b5SBarry Smith     } else {
96b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: not set yet\n");CHKERRQ(ierr);
97184914b5SBarry Smith     }
98e7788613SBarry Smith     if (snes->ops->view) {
99b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
100e7788613SBarry Smith       ierr = (*snes->ops->view)(snes,viewer);CHKERRQ(ierr);
101b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1020ef38995SBarry Smith     }
10377431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum iterations=%D, maximum function evaluations=%D\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr);
104a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  tolerances: relative=%G, absolute=%G, solution=%G\n",
10570441072SBarry Smith                  snes->rtol,snes->abstol,snes->xtol);CHKERRQ(ierr);
10677431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",snes->linear_its);CHKERRQ(ierr);
10777431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of function evaluations=%D\n",snes->nfuncs);CHKERRQ(ierr);
1089b94acceSBarry Smith     if (snes->ksp_ewconv) {
109fa9f3622SBarry Smith       kctx = (SNESKSPEW *)snes->kspconvctx;
1109b94acceSBarry Smith       if (kctx) {
11177431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"  Eisenstat-Walker computation of KSP relative tolerance (version %D)\n",kctx->version);CHKERRQ(ierr);
112a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    rtol_0=%G, rtol_max=%G, threshold=%G\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr);
113a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    gamma=%G, alpha=%G, alpha2=%G\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr);
1149b94acceSBarry Smith       }
1159b94acceSBarry Smith     }
1160f5bd95cSBarry Smith   } else if (isstring) {
117454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
118b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr);
11919bcc07fSBarry Smith   }
12094b7f48cSBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
121b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
12294b7f48cSBarry Smith   ierr = KSPView(ksp,viewer);CHKERRQ(ierr);
123b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1243a40ed3dSBarry Smith   PetscFunctionReturn(0);
1259b94acceSBarry Smith }
1269b94acceSBarry Smith 
12776b2cf59SMatthew Knepley /*
12876b2cf59SMatthew Knepley   We retain a list of functions that also take SNES command
12976b2cf59SMatthew Knepley   line options. These are called at the end SNESSetFromOptions()
13076b2cf59SMatthew Knepley */
13176b2cf59SMatthew Knepley #define MAXSETFROMOPTIONS 5
132a7cc72afSBarry Smith static PetscInt numberofsetfromoptions;
1336849ba73SBarry Smith static PetscErrorCode (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES);
13476b2cf59SMatthew Knepley 
135e74ef692SMatthew Knepley #undef __FUNCT__
136e74ef692SMatthew Knepley #define __FUNCT__ "SNESAddOptionsChecker"
137ac226902SBarry Smith /*@C
13876b2cf59SMatthew Knepley   SNESAddOptionsChecker - Adds an additional function to check for SNES options.
13976b2cf59SMatthew Knepley 
14076b2cf59SMatthew Knepley   Not Collective
14176b2cf59SMatthew Knepley 
14276b2cf59SMatthew Knepley   Input Parameter:
14376b2cf59SMatthew Knepley . snescheck - function that checks for options
14476b2cf59SMatthew Knepley 
14576b2cf59SMatthew Knepley   Level: developer
14676b2cf59SMatthew Knepley 
14776b2cf59SMatthew Knepley .seealso: SNESSetFromOptions()
14876b2cf59SMatthew Knepley @*/
14963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAddOptionsChecker(PetscErrorCode (*snescheck)(SNES))
15076b2cf59SMatthew Knepley {
15176b2cf59SMatthew Knepley   PetscFunctionBegin;
15276b2cf59SMatthew Knepley   if (numberofsetfromoptions >= MAXSETFROMOPTIONS) {
15377431f27SBarry Smith     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %D allowed", MAXSETFROMOPTIONS);
15476b2cf59SMatthew Knepley   }
15576b2cf59SMatthew Knepley   othersetfromoptions[numberofsetfromoptions++] = snescheck;
15676b2cf59SMatthew Knepley   PetscFunctionReturn(0);
15776b2cf59SMatthew Knepley }
15876b2cf59SMatthew Knepley 
1594a2ae208SSatish Balay #undef __FUNCT__
1604a2ae208SSatish Balay #define __FUNCT__ "SNESSetFromOptions"
1619b94acceSBarry Smith /*@
16294b7f48cSBarry Smith    SNESSetFromOptions - Sets various SNES and KSP parameters from user options.
1639b94acceSBarry Smith 
164c7afd0dbSLois Curfman McInnes    Collective on SNES
165c7afd0dbSLois Curfman McInnes 
1669b94acceSBarry Smith    Input Parameter:
1679b94acceSBarry Smith .  snes - the SNES context
1689b94acceSBarry Smith 
16936851e7fSLois Curfman McInnes    Options Database Keys:
1706831982aSBarry Smith +  -snes_type <type> - ls, tr, umls, umtr, test
17182738288SBarry Smith .  -snes_stol - convergence tolerance in terms of the norm
17282738288SBarry Smith                 of the change in the solution between steps
17370441072SBarry Smith .  -snes_atol <abstol> - absolute tolerance of residual norm
174b39c3a46SLois Curfman McInnes .  -snes_rtol <rtol> - relative decrease in tolerance norm from initial
175b39c3a46SLois Curfman McInnes .  -snes_max_it <max_it> - maximum number of iterations
176b39c3a46SLois Curfman McInnes .  -snes_max_funcs <max_funcs> - maximum number of function evaluations
17750ffb88aSMatthew Knepley .  -snes_max_fail <max_fail> - maximum number of failures
178b39c3a46SLois Curfman McInnes .  -snes_trtol <trtol> - trust region tolerance
1792492ecdbSBarry Smith .  -snes_no_convergence_test - skip convergence test in nonlinear
18082738288SBarry Smith                                solver; hence iterations will continue until max_it
1811fbbfb26SLois Curfman McInnes                                or some other criterion is reached. Saves expense
18282738288SBarry Smith                                of convergence test
183e8105e01SRichard Katz .  -snes_monitor <optional filename> - prints residual norm at each iteration. if no
184e8105e01SRichard Katz                                        filename given prints to stdout
185a6570f20SBarry Smith .  -snes_monitor_solution - plots solution at each iteration
186a6570f20SBarry Smith .  -snes_monitor_residual - plots residual (not its norm) at each iteration
187a6570f20SBarry Smith .  -snes_monitor_solution_update - plots update to solution at each iteration
188a6570f20SBarry Smith .  -snes_monitor_draw - plots residual norm at each iteration
189e24b481bSBarry Smith .  -snes_fd - use finite differences to compute Jacobian; very slow, only for testing
1905968eb51SBarry Smith .  -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration
191fee2055bSBarry Smith -  -snes_converged_reason - print the reason for convergence/divergence after each solve
19282738288SBarry Smith 
19382738288SBarry Smith     Options Database for Eisenstat-Walker method:
194fa9f3622SBarry Smith +  -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence
1954b27c08aSLois Curfman McInnes .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
19636851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
19736851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
19836851e7fSLois Curfman McInnes .  -snes_ksp_ew_gamma <gamma> - Sets gamma
19936851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha <alpha> - Sets alpha
20036851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
20136851e7fSLois Curfman McInnes -  -snes_ksp_ew_threshold <threshold> - Sets threshold
20282738288SBarry Smith 
20311ca99fdSLois Curfman McInnes    Notes:
20411ca99fdSLois Curfman McInnes    To see all options, run your program with the -help option or consult
20511ca99fdSLois Curfman McInnes    the users manual.
20683e2fdc7SBarry Smith 
20736851e7fSLois Curfman McInnes    Level: beginner
20836851e7fSLois Curfman McInnes 
2099b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database
2109b94acceSBarry Smith 
21169ed319cSSatish Balay .seealso: SNESSetOptionsPrefix()
2129b94acceSBarry Smith @*/
21363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFromOptions(SNES snes)
2149b94acceSBarry Smith {
215f1af5d2fSBarry Smith   PetscTruth              flg;
21685385478SLisandro Dalcin   PetscInt                i,indx;
21785385478SLisandro Dalcin   const char              *deft = SNESLS;
21885385478SLisandro Dalcin   const char              *convtests[] = {"default","skip"};
21985385478SLisandro Dalcin   SNESKSPEW               *kctx = NULL;
220e8105e01SRichard Katz   char                    type[256], monfilename[PETSC_MAX_PATH_LEN];
22123d894e5SBarry Smith   PetscViewerASCIIMonitor monviewer;
22285385478SLisandro Dalcin   PetscErrorCode          ierr;
2239b94acceSBarry Smith 
2243a40ed3dSBarry Smith   PetscFunctionBegin;
2254482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
226ca161407SBarry Smith 
2277adad957SLisandro Dalcin   ierr = PetscOptionsBegin(((PetscObject)snes)->comm,((PetscObject)snes)->prefix,"Nonlinear solver (SNES) options","SNES");CHKERRQ(ierr);
228186905e3SBarry Smith     if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
2297adad957SLisandro Dalcin     if (((PetscObject)snes)->type_name) { deft = ((PetscObject)snes)->type_name; }
230b0a32e0cSBarry Smith     ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr);
231d64ed03dSBarry Smith     if (flg) {
232186905e3SBarry Smith       ierr = SNESSetType(snes,type);CHKERRQ(ierr);
2337adad957SLisandro Dalcin     } else if (!((PetscObject)snes)->type_name) {
234186905e3SBarry Smith       ierr = SNESSetType(snes,deft);CHKERRQ(ierr);
235d64ed03dSBarry Smith     }
236909c8a9fSBarry Smith     ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr);
23793c39befSBarry Smith 
23887828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_stol","Stop if step length less then","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr);
23970441072SBarry Smith     ierr = PetscOptionsReal("-snes_atol","Stop if function norm less then","SNESSetTolerances",snes->abstol,&snes->abstol,0);CHKERRQ(ierr);
240186905e3SBarry Smith 
24187828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less then","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr);
242b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr);
243b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr);
24450ffb88aSMatthew Knepley     ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr);
24585385478SLisandro Dalcin 
24685385478SLisandro Dalcin     ierr = PetscOptionsEList("-snes_convergence_test","Convergence test","SNESSetConvergenceTest",convtests,2,"default",&indx,&flg);CHKERRQ(ierr);
24785385478SLisandro Dalcin     if (flg) {
24885385478SLisandro Dalcin       switch (indx) {
249*7f7931b9SBarry Smith       case 0: ierr = SNESSetConvergenceTest(snes,SNESDefaultConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break;
250*7f7931b9SBarry Smith       case 1: ierr = SNESSetConvergenceTest(snes,SNESSkipConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);    break;
25185385478SLisandro Dalcin       }
25285385478SLisandro Dalcin     }
25385385478SLisandro Dalcin 
2545968eb51SBarry Smith     ierr = PetscOptionsName("-snes_converged_reason","Print reason for converged or diverged","SNESSolve",&flg);CHKERRQ(ierr);
2555968eb51SBarry Smith     if (flg) {
2565968eb51SBarry Smith       snes->printreason = PETSC_TRUE;
2575968eb51SBarry Smith     }
258186905e3SBarry Smith 
25985385478SLisandro Dalcin     kctx = (SNESKSPEW *)snes->kspconvctx;
26085385478SLisandro Dalcin 
261fa9f3622SBarry Smith     ierr = PetscOptionsTruth("-snes_ksp_ew","Use Eisentat-Walker linear system convergence test","SNESKSPSetUseEW",snes->ksp_ewconv,&snes->ksp_ewconv,PETSC_NULL);CHKERRQ(ierr);
262186905e3SBarry Smith 
263fa9f3622SBarry Smith     ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNESKSPSetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr);
264fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNESKSPSetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr);
265fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNESKSPSetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr);
266fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNESKSPSetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr);
267fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNESKSPSetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr);
268fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNESKSPSetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr);
269fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNESKSPSetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr);
270186905e3SBarry Smith 
271a6570f20SBarry Smith     ierr = PetscOptionsName("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",&flg);CHKERRQ(ierr);
272a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);}
273eabae89aSBarry Smith 
274a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor","Monitor norm of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
275e8105e01SRichard Katz     if (flg) {
2767adad957SLisandro Dalcin       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,0,&monviewer);CHKERRQ(ierr);
27723d894e5SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefault,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
278e8105e01SRichard Katz     }
279eabae89aSBarry Smith 
280a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_ratiomonitor","Monitor ratios of norms of function","SNESMonitorSetRatio","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
281eabae89aSBarry Smith     if (flg) {
2827adad957SLisandro Dalcin       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,0,&monviewer);CHKERRQ(ierr);
283f1bef1bcSMatthew Knepley       ierr = SNESMonitorSetRatio(snes,monviewer);CHKERRQ(ierr);
284e8105e01SRichard Katz     }
285eabae89aSBarry Smith 
286a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor_short","Monitor norm of function (fewer digits)","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
287eabae89aSBarry Smith     if (flg) {
2887adad957SLisandro Dalcin       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,0,&monviewer);CHKERRQ(ierr);
28923d894e5SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefaultShort,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
290eabae89aSBarry Smith     }
291eabae89aSBarry Smith 
292a6570f20SBarry Smith     ierr = PetscOptionsName("-snes_monitor_solution","Plot solution at each iteration","SNESMonitorSolution",&flg);CHKERRQ(ierr);
293a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolution,0,0);CHKERRQ(ierr);}
294a6570f20SBarry Smith     ierr = PetscOptionsName("-snes_monitor_solution_update","Plot correction at each iteration","SNESMonitorSolutionUpdate",&flg);CHKERRQ(ierr);
295a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolutionUpdate,0,0);CHKERRQ(ierr);}
296a6570f20SBarry Smith     ierr = PetscOptionsName("-snes_monitor_residual","Plot residual at each iteration","SNESMonitorResidual",&flg);CHKERRQ(ierr);
297a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorResidual,0,0);CHKERRQ(ierr);}
298a6570f20SBarry Smith     ierr = PetscOptionsName("-snes_monitor_draw","Plot function norm at each iteration","SNESMonitorLG",&flg);CHKERRQ(ierr);
299a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
300e24b481bSBarry Smith 
301b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",&flg);CHKERRQ(ierr);
3024b27c08aSLois Curfman McInnes     if (flg) {
303186905e3SBarry Smith       ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr);
304ae15b995SBarry Smith       ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr);
3059b94acceSBarry Smith     }
306639f9d9dSBarry Smith 
30776b2cf59SMatthew Knepley     for(i = 0; i < numberofsetfromoptions; i++) {
30876b2cf59SMatthew Knepley       ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr);
30976b2cf59SMatthew Knepley     }
31076b2cf59SMatthew Knepley 
311e7788613SBarry Smith     if (snes->ops->setfromoptions) {
312e7788613SBarry Smith       ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr);
313639f9d9dSBarry Smith     }
314b0a32e0cSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
3154bbc92c1SBarry Smith 
31685385478SLisandro Dalcin   ierr = KSPSetFromOptions(snes->ksp);CHKERRQ(ierr);
31793993e2dSLois Curfman McInnes 
3183a40ed3dSBarry Smith   PetscFunctionReturn(0);
3199b94acceSBarry Smith }
3209b94acceSBarry Smith 
321a847f771SSatish Balay 
3224a2ae208SSatish Balay #undef __FUNCT__
3234a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext"
3249b94acceSBarry Smith /*@
3259b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
3269b94acceSBarry Smith    the nonlinear solvers.
3279b94acceSBarry Smith 
328fee21e36SBarry Smith    Collective on SNES
329fee21e36SBarry Smith 
330c7afd0dbSLois Curfman McInnes    Input Parameters:
331c7afd0dbSLois Curfman McInnes +  snes - the SNES context
332c7afd0dbSLois Curfman McInnes -  usrP - optional user context
333c7afd0dbSLois Curfman McInnes 
33436851e7fSLois Curfman McInnes    Level: intermediate
33536851e7fSLois Curfman McInnes 
3369b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
3379b94acceSBarry Smith 
3389b94acceSBarry Smith .seealso: SNESGetApplicationContext()
3399b94acceSBarry Smith @*/
34063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetApplicationContext(SNES snes,void *usrP)
3419b94acceSBarry Smith {
3423a40ed3dSBarry Smith   PetscFunctionBegin;
3434482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
3449b94acceSBarry Smith   snes->user		= usrP;
3453a40ed3dSBarry Smith   PetscFunctionReturn(0);
3469b94acceSBarry Smith }
34774679c65SBarry Smith 
3484a2ae208SSatish Balay #undef __FUNCT__
3494a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext"
3509b94acceSBarry Smith /*@C
3519b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
3529b94acceSBarry Smith    nonlinear solvers.
3539b94acceSBarry Smith 
354c7afd0dbSLois Curfman McInnes    Not Collective
355c7afd0dbSLois Curfman McInnes 
3569b94acceSBarry Smith    Input Parameter:
3579b94acceSBarry Smith .  snes - SNES context
3589b94acceSBarry Smith 
3599b94acceSBarry Smith    Output Parameter:
3609b94acceSBarry Smith .  usrP - user context
3619b94acceSBarry Smith 
36236851e7fSLois Curfman McInnes    Level: intermediate
36336851e7fSLois Curfman McInnes 
3649b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
3659b94acceSBarry Smith 
3669b94acceSBarry Smith .seealso: SNESSetApplicationContext()
3679b94acceSBarry Smith @*/
36863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetApplicationContext(SNES snes,void **usrP)
3699b94acceSBarry Smith {
3703a40ed3dSBarry Smith   PetscFunctionBegin;
3714482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
3729b94acceSBarry Smith   *usrP = snes->user;
3733a40ed3dSBarry Smith   PetscFunctionReturn(0);
3749b94acceSBarry Smith }
37574679c65SBarry Smith 
3764a2ae208SSatish Balay #undef __FUNCT__
3774a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber"
3789b94acceSBarry Smith /*@
379c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
380c8228a4eSBarry Smith    at this time.
3819b94acceSBarry Smith 
382c7afd0dbSLois Curfman McInnes    Not Collective
383c7afd0dbSLois Curfman McInnes 
3849b94acceSBarry Smith    Input Parameter:
3859b94acceSBarry Smith .  snes - SNES context
3869b94acceSBarry Smith 
3879b94acceSBarry Smith    Output Parameter:
3889b94acceSBarry Smith .  iter - iteration number
3899b94acceSBarry Smith 
390c8228a4eSBarry Smith    Notes:
391c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
392c8228a4eSBarry Smith 
393c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
39408405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
39508405cd6SLois Curfman McInnes .vb
39608405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
39708405cd6SLois Curfman McInnes       if (!(it % 2)) {
39808405cd6SLois Curfman McInnes         [compute Jacobian here]
39908405cd6SLois Curfman McInnes       }
40008405cd6SLois Curfman McInnes .ve
401c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
40208405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
403c8228a4eSBarry Smith 
40436851e7fSLois Curfman McInnes    Level: intermediate
40536851e7fSLois Curfman McInnes 
4062b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number,
4072b668275SBarry Smith 
408b850b91aSLisandro Dalcin .seealso:   SNESGetFunctionNorm(), SNESGetLinearSolveIterations()
4099b94acceSBarry Smith @*/
41063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetIterationNumber(SNES snes,PetscInt* iter)
4119b94acceSBarry Smith {
4123a40ed3dSBarry Smith   PetscFunctionBegin;
4134482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
4144482741eSBarry Smith   PetscValidIntPointer(iter,2);
4159b94acceSBarry Smith   *iter = snes->iter;
4163a40ed3dSBarry Smith   PetscFunctionReturn(0);
4179b94acceSBarry Smith }
41874679c65SBarry Smith 
4194a2ae208SSatish Balay #undef __FUNCT__
4204a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm"
4219b94acceSBarry Smith /*@
4229b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
4239b94acceSBarry Smith    with SNESSSetFunction().
4249b94acceSBarry Smith 
425c7afd0dbSLois Curfman McInnes    Collective on SNES
426c7afd0dbSLois Curfman McInnes 
4279b94acceSBarry Smith    Input Parameter:
4289b94acceSBarry Smith .  snes - SNES context
4299b94acceSBarry Smith 
4309b94acceSBarry Smith    Output Parameter:
4319b94acceSBarry Smith .  fnorm - 2-norm of function
4329b94acceSBarry Smith 
43336851e7fSLois Curfman McInnes    Level: intermediate
43436851e7fSLois Curfman McInnes 
4359b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
436a86d99e1SLois Curfman McInnes 
437b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations()
4389b94acceSBarry Smith @*/
43971f87433Sdalcinl PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunctionNorm(SNES snes,PetscReal *fnorm)
4409b94acceSBarry Smith {
4413a40ed3dSBarry Smith   PetscFunctionBegin;
4424482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
4434482741eSBarry Smith   PetscValidScalarPointer(fnorm,2);
4449b94acceSBarry Smith   *fnorm = snes->norm;
4453a40ed3dSBarry Smith   PetscFunctionReturn(0);
4469b94acceSBarry Smith }
44774679c65SBarry Smith 
4484a2ae208SSatish Balay #undef __FUNCT__
449b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures"
4509b94acceSBarry Smith /*@
451b850b91aSLisandro Dalcin    SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps
4529b94acceSBarry Smith    attempted by the nonlinear solver.
4539b94acceSBarry Smith 
454c7afd0dbSLois Curfman McInnes    Not Collective
455c7afd0dbSLois Curfman McInnes 
4569b94acceSBarry Smith    Input Parameter:
4579b94acceSBarry Smith .  snes - SNES context
4589b94acceSBarry Smith 
4599b94acceSBarry Smith    Output Parameter:
4609b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
4619b94acceSBarry Smith 
462c96a6f78SLois Curfman McInnes    Notes:
463c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
464c96a6f78SLois Curfman McInnes 
46536851e7fSLois Curfman McInnes    Level: intermediate
46636851e7fSLois Curfman McInnes 
4679b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
46858ebbce7SBarry Smith 
46958ebbce7SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolverIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
47058ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures()
4719b94acceSBarry Smith @*/
472b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails)
4739b94acceSBarry Smith {
4743a40ed3dSBarry Smith   PetscFunctionBegin;
4754482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
4764482741eSBarry Smith   PetscValidIntPointer(nfails,2);
47750ffb88aSMatthew Knepley   *nfails = snes->numFailures;
47850ffb88aSMatthew Knepley   PetscFunctionReturn(0);
47950ffb88aSMatthew Knepley }
48050ffb88aSMatthew Knepley 
48150ffb88aSMatthew Knepley #undef __FUNCT__
482b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures"
48350ffb88aSMatthew Knepley /*@
484b850b91aSLisandro Dalcin    SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps
48550ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
48650ffb88aSMatthew Knepley 
48750ffb88aSMatthew Knepley    Not Collective
48850ffb88aSMatthew Knepley 
48950ffb88aSMatthew Knepley    Input Parameters:
49050ffb88aSMatthew Knepley +  snes     - SNES context
49150ffb88aSMatthew Knepley -  maxFails - maximum of unsuccessful steps
49250ffb88aSMatthew Knepley 
49350ffb88aSMatthew Knepley    Level: intermediate
49450ffb88aSMatthew Knepley 
49550ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
49658ebbce7SBarry Smith 
49758ebbce7SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolverIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
49858ebbce7SBarry Smith           SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
49950ffb88aSMatthew Knepley @*/
500b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails)
50150ffb88aSMatthew Knepley {
50250ffb88aSMatthew Knepley   PetscFunctionBegin;
5034482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
50450ffb88aSMatthew Knepley   snes->maxFailures = maxFails;
50550ffb88aSMatthew Knepley   PetscFunctionReturn(0);
50650ffb88aSMatthew Knepley }
50750ffb88aSMatthew Knepley 
50850ffb88aSMatthew Knepley #undef __FUNCT__
509b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures"
51050ffb88aSMatthew Knepley /*@
511b850b91aSLisandro Dalcin    SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps
51250ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
51350ffb88aSMatthew Knepley 
51450ffb88aSMatthew Knepley    Not Collective
51550ffb88aSMatthew Knepley 
51650ffb88aSMatthew Knepley    Input Parameter:
51750ffb88aSMatthew Knepley .  snes     - SNES context
51850ffb88aSMatthew Knepley 
51950ffb88aSMatthew Knepley    Output Parameter:
52050ffb88aSMatthew Knepley .  maxFails - maximum of unsuccessful steps
52150ffb88aSMatthew Knepley 
52250ffb88aSMatthew Knepley    Level: intermediate
52350ffb88aSMatthew Knepley 
52450ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
52558ebbce7SBarry Smith 
52658ebbce7SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolverIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
52758ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
52858ebbce7SBarry Smith 
52950ffb88aSMatthew Knepley @*/
530b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails)
53150ffb88aSMatthew Knepley {
53250ffb88aSMatthew Knepley   PetscFunctionBegin;
5334482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
5344482741eSBarry Smith   PetscValidIntPointer(maxFails,2);
53550ffb88aSMatthew Knepley   *maxFails = snes->maxFailures;
5363a40ed3dSBarry Smith   PetscFunctionReturn(0);
5379b94acceSBarry Smith }
538a847f771SSatish Balay 
5394a2ae208SSatish Balay #undef __FUNCT__
5402541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals"
5412541af92SBarry Smith /*@
5422541af92SBarry Smith    SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations
5432541af92SBarry Smith      done by SNES.
5442541af92SBarry Smith 
5452541af92SBarry Smith    Not Collective
5462541af92SBarry Smith 
5472541af92SBarry Smith    Input Parameter:
5482541af92SBarry Smith .  snes     - SNES context
5492541af92SBarry Smith 
5502541af92SBarry Smith    Output Parameter:
5512541af92SBarry Smith .  nfuncs - number of evaluations
5522541af92SBarry Smith 
5532541af92SBarry Smith    Level: intermediate
5542541af92SBarry Smith 
5552541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
55658ebbce7SBarry Smith 
55758ebbce7SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolverIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures()
5582541af92SBarry Smith @*/
5592541af92SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs)
5602541af92SBarry Smith {
5612541af92SBarry Smith   PetscFunctionBegin;
5622541af92SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
5632541af92SBarry Smith   PetscValidIntPointer(nfuncs,2);
5642541af92SBarry Smith   *nfuncs = snes->nfuncs;
5652541af92SBarry Smith   PetscFunctionReturn(0);
5662541af92SBarry Smith }
5672541af92SBarry Smith 
5682541af92SBarry Smith #undef __FUNCT__
5693d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures"
5703d4c4710SBarry Smith /*@
5713d4c4710SBarry Smith    SNESGetLinearSolveFailures - Gets the number of failed (non-converged)
5723d4c4710SBarry Smith    linear solvers.
5733d4c4710SBarry Smith 
5743d4c4710SBarry Smith    Not Collective
5753d4c4710SBarry Smith 
5763d4c4710SBarry Smith    Input Parameter:
5773d4c4710SBarry Smith .  snes - SNES context
5783d4c4710SBarry Smith 
5793d4c4710SBarry Smith    Output Parameter:
5803d4c4710SBarry Smith .  nfails - number of failed solves
5813d4c4710SBarry Smith 
5823d4c4710SBarry Smith    Notes:
5833d4c4710SBarry Smith    This counter is reset to zero for each successive call to SNESSolve().
5843d4c4710SBarry Smith 
5853d4c4710SBarry Smith    Level: intermediate
5863d4c4710SBarry Smith 
5873d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
58858ebbce7SBarry Smith 
58958ebbce7SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolverIterations(), SNESSetMaxLinearSolveFailures()
5903d4c4710SBarry Smith @*/
5913d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails)
5923d4c4710SBarry Smith {
5933d4c4710SBarry Smith   PetscFunctionBegin;
5943d4c4710SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
5953d4c4710SBarry Smith   PetscValidIntPointer(nfails,2);
5963d4c4710SBarry Smith   *nfails = snes->numLinearSolveFailures;
5973d4c4710SBarry Smith   PetscFunctionReturn(0);
5983d4c4710SBarry Smith }
5993d4c4710SBarry Smith 
6003d4c4710SBarry Smith #undef __FUNCT__
6013d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures"
6023d4c4710SBarry Smith /*@
6033d4c4710SBarry Smith    SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts
6043d4c4710SBarry Smith    allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE
6053d4c4710SBarry Smith 
6063d4c4710SBarry Smith    Collective on SNES
6073d4c4710SBarry Smith 
6083d4c4710SBarry Smith    Input Parameters:
6093d4c4710SBarry Smith +  snes     - SNES context
6103d4c4710SBarry Smith -  maxFails - maximum allowed linear solve failures
6113d4c4710SBarry Smith 
6123d4c4710SBarry Smith    Level: intermediate
6133d4c4710SBarry Smith 
6143d4c4710SBarry Smith    Notes: By default this is 1; that is SNES returns on the first failed linear solve
6153d4c4710SBarry Smith 
6163d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
6173d4c4710SBarry Smith 
61858ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations()
6193d4c4710SBarry Smith @*/
6203d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails)
6213d4c4710SBarry Smith {
6223d4c4710SBarry Smith   PetscFunctionBegin;
6233d4c4710SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
6243d4c4710SBarry Smith   snes->maxLinearSolveFailures = maxFails;
6253d4c4710SBarry Smith   PetscFunctionReturn(0);
6263d4c4710SBarry Smith }
6273d4c4710SBarry Smith 
6283d4c4710SBarry Smith #undef __FUNCT__
6293d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures"
6303d4c4710SBarry Smith /*@
6313d4c4710SBarry Smith    SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that
6323d4c4710SBarry Smith      are allowed before SNES terminates
6333d4c4710SBarry Smith 
6343d4c4710SBarry Smith    Not Collective
6353d4c4710SBarry Smith 
6363d4c4710SBarry Smith    Input Parameter:
6373d4c4710SBarry Smith .  snes     - SNES context
6383d4c4710SBarry Smith 
6393d4c4710SBarry Smith    Output Parameter:
6403d4c4710SBarry Smith .  maxFails - maximum of unsuccessful solves allowed
6413d4c4710SBarry Smith 
6423d4c4710SBarry Smith    Level: intermediate
6433d4c4710SBarry Smith 
6443d4c4710SBarry Smith    Notes: By default this is 1; that is SNES returns on the first failed linear solve
6453d4c4710SBarry Smith 
6463d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
6473d4c4710SBarry Smith 
64858ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolverIterations(), SNESSetMaxLinearSolveFailures(),
6493d4c4710SBarry Smith @*/
6503d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails)
6513d4c4710SBarry Smith {
6523d4c4710SBarry Smith   PetscFunctionBegin;
6533d4c4710SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
6543d4c4710SBarry Smith   PetscValidIntPointer(maxFails,2);
6553d4c4710SBarry Smith   *maxFails = snes->maxLinearSolveFailures;
6563d4c4710SBarry Smith   PetscFunctionReturn(0);
6573d4c4710SBarry Smith }
6583d4c4710SBarry Smith 
6593d4c4710SBarry Smith #undef __FUNCT__
660b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations"
661c96a6f78SLois Curfman McInnes /*@
662b850b91aSLisandro Dalcin    SNESGetLinearSolveIterations - Gets the total number of linear iterations
663c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
664c96a6f78SLois Curfman McInnes 
665c7afd0dbSLois Curfman McInnes    Not Collective
666c7afd0dbSLois Curfman McInnes 
667c96a6f78SLois Curfman McInnes    Input Parameter:
668c96a6f78SLois Curfman McInnes .  snes - SNES context
669c96a6f78SLois Curfman McInnes 
670c96a6f78SLois Curfman McInnes    Output Parameter:
671c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
672c96a6f78SLois Curfman McInnes 
673c96a6f78SLois Curfman McInnes    Notes:
674c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
675c96a6f78SLois Curfman McInnes 
67636851e7fSLois Curfman McInnes    Level: intermediate
67736851e7fSLois Curfman McInnes 
678c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
6792b668275SBarry Smith 
68058ebbce7SBarry Smith .seealso:  SNESGetIterationNumber(), SNESGetFunctionNorm()S, NESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures()
681c96a6f78SLois Curfman McInnes @*/
682b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveIterations(SNES snes,PetscInt* lits)
683c96a6f78SLois Curfman McInnes {
6843a40ed3dSBarry Smith   PetscFunctionBegin;
6854482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
6864482741eSBarry Smith   PetscValidIntPointer(lits,2);
687c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
6883a40ed3dSBarry Smith   PetscFunctionReturn(0);
689c96a6f78SLois Curfman McInnes }
690c96a6f78SLois Curfman McInnes 
6914a2ae208SSatish Balay #undef __FUNCT__
69294b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP"
69352baeb72SSatish Balay /*@
69494b7f48cSBarry Smith    SNESGetKSP - Returns the KSP context for a SNES solver.
6959b94acceSBarry Smith 
69694b7f48cSBarry Smith    Not Collective, but if SNES object is parallel, then KSP object is parallel
697c7afd0dbSLois Curfman McInnes 
6989b94acceSBarry Smith    Input Parameter:
6999b94acceSBarry Smith .  snes - the SNES context
7009b94acceSBarry Smith 
7019b94acceSBarry Smith    Output Parameter:
70294b7f48cSBarry Smith .  ksp - the KSP context
7039b94acceSBarry Smith 
7049b94acceSBarry Smith    Notes:
70594b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
7069b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
7072999313aSBarry Smith    PC contexts as well.
7089b94acceSBarry Smith 
70936851e7fSLois Curfman McInnes    Level: beginner
71036851e7fSLois Curfman McInnes 
71194b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
7129b94acceSBarry Smith 
7132999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
7149b94acceSBarry Smith @*/
71563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetKSP(SNES snes,KSP *ksp)
7169b94acceSBarry Smith {
7173a40ed3dSBarry Smith   PetscFunctionBegin;
7184482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
7194482741eSBarry Smith   PetscValidPointer(ksp,2);
72094b7f48cSBarry Smith   *ksp = snes->ksp;
7213a40ed3dSBarry Smith   PetscFunctionReturn(0);
7229b94acceSBarry Smith }
72382bf6240SBarry Smith 
7244a2ae208SSatish Balay #undef __FUNCT__
7252999313aSBarry Smith #define __FUNCT__ "SNESSetKSP"
7262999313aSBarry Smith /*@
7272999313aSBarry Smith    SNESSetKSP - Sets a KSP context for the SNES object to use
7282999313aSBarry Smith 
7292999313aSBarry Smith    Not Collective, but the SNES and KSP objects must live on the same MPI_Comm
7302999313aSBarry Smith 
7312999313aSBarry Smith    Input Parameters:
7322999313aSBarry Smith +  snes - the SNES context
7332999313aSBarry Smith -  ksp - the KSP context
7342999313aSBarry Smith 
7352999313aSBarry Smith    Notes:
7362999313aSBarry Smith    The SNES object already has its KSP object, you can obtain with SNESGetKSP()
7372999313aSBarry Smith    so this routine is rarely needed.
7382999313aSBarry Smith 
7392999313aSBarry Smith    The KSP object that is already in the SNES object has its reference count
7402999313aSBarry Smith    decreased by one.
7412999313aSBarry Smith 
7422999313aSBarry Smith    Level: developer
7432999313aSBarry Smith 
7442999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
7452999313aSBarry Smith 
7462999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
7472999313aSBarry Smith @*/
7482999313aSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetKSP(SNES snes,KSP ksp)
7492999313aSBarry Smith {
7502999313aSBarry Smith   PetscErrorCode ierr;
7512999313aSBarry Smith 
7522999313aSBarry Smith   PetscFunctionBegin;
7532999313aSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
7542999313aSBarry Smith   PetscValidHeaderSpecific(ksp,KSP_COOKIE,2);
7552999313aSBarry Smith   PetscCheckSameComm(snes,1,ksp,2);
7567dcf0eaaSdalcinl   ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr);
757906ed7ccSBarry Smith   if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);}
7582999313aSBarry Smith   snes->ksp = ksp;
7592999313aSBarry Smith   PetscFunctionReturn(0);
7602999313aSBarry Smith }
7612999313aSBarry Smith 
7627adad957SLisandro Dalcin #if 0
7632999313aSBarry Smith #undef __FUNCT__
7644a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc"
7656849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj)
766e24b481bSBarry Smith {
767e24b481bSBarry Smith   PetscFunctionBegin;
768e24b481bSBarry Smith   PetscFunctionReturn(0);
769e24b481bSBarry Smith }
7707adad957SLisandro Dalcin #endif
771e24b481bSBarry Smith 
7729b94acceSBarry Smith /* -----------------------------------------------------------*/
7734a2ae208SSatish Balay #undef __FUNCT__
7744a2ae208SSatish Balay #define __FUNCT__ "SNESCreate"
77552baeb72SSatish Balay /*@
7769b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
7779b94acceSBarry Smith 
778c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
779c7afd0dbSLois Curfman McInnes 
780c7afd0dbSLois Curfman McInnes    Input Parameters:
781906ed7ccSBarry Smith .  comm - MPI communicator
7829b94acceSBarry Smith 
7839b94acceSBarry Smith    Output Parameter:
7849b94acceSBarry Smith .  outsnes - the new SNES context
7859b94acceSBarry Smith 
786c7afd0dbSLois Curfman McInnes    Options Database Keys:
787c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
788c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
789c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
790c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
791c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
792c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
793c1f60f51SBarry Smith 
79436851e7fSLois Curfman McInnes    Level: beginner
79536851e7fSLois Curfman McInnes 
7969b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
7979b94acceSBarry Smith 
7984b27c08aSLois Curfman McInnes .seealso: SNESSolve(), SNESDestroy(), SNES
7999b94acceSBarry Smith @*/
80063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESCreate(MPI_Comm comm,SNES *outsnes)
8019b94acceSBarry Smith {
802dfbe8321SBarry Smith   PetscErrorCode      ierr;
8039b94acceSBarry Smith   SNES                snes;
804fa9f3622SBarry Smith   SNESKSPEW           *kctx;
80537fcc0dbSBarry Smith 
8063a40ed3dSBarry Smith   PetscFunctionBegin;
807ed1caa07SMatthew Knepley   PetscValidPointer(outsnes,2);
8088ba1e511SMatthew Knepley   *outsnes = PETSC_NULL;
8098ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
8108ba1e511SMatthew Knepley   ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr);
8118ba1e511SMatthew Knepley #endif
8128ba1e511SMatthew Knepley 
813e7788613SBarry Smith   ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr);
8147adad957SLisandro Dalcin 
81585385478SLisandro Dalcin   snes->ops->converged    = SNESDefaultConverged;
8169b94acceSBarry Smith   snes->max_its           = 50;
8179750a799SBarry Smith   snes->max_funcs	  = 10000;
8189b94acceSBarry Smith   snes->norm		  = 0.0;
819b4874afaSBarry Smith   snes->rtol		  = 1.e-8;
820b4874afaSBarry Smith   snes->ttol              = 0.0;
82170441072SBarry Smith   snes->abstol		  = 1.e-50;
8229b94acceSBarry Smith   snes->xtol		  = 1.e-8;
8234b27c08aSLois Curfman McInnes   snes->deltatol	  = 1.e-12;
8249b94acceSBarry Smith   snes->nfuncs            = 0;
82550ffb88aSMatthew Knepley   snes->numFailures       = 0;
82650ffb88aSMatthew Knepley   snes->maxFailures       = 1;
8277a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
828639f9d9dSBarry Smith   snes->numbermonitors    = 0;
8299b94acceSBarry Smith   snes->data              = 0;
8304dc4c822SBarry Smith   snes->setupcalled       = PETSC_FALSE;
831186905e3SBarry Smith   snes->ksp_ewconv        = PETSC_FALSE;
8326f24a144SLois Curfman McInnes   snes->vwork             = 0;
8336f24a144SLois Curfman McInnes   snes->nwork             = 0;
834758f92a0SBarry Smith   snes->conv_hist_len     = 0;
835758f92a0SBarry Smith   snes->conv_hist_max     = 0;
836758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
837758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
838758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
839184914b5SBarry Smith   snes->reason            = SNES_CONVERGED_ITERATING;
8409b94acceSBarry Smith 
8413d4c4710SBarry Smith   snes->numLinearSolveFailures = 0;
8423d4c4710SBarry Smith   snes->maxLinearSolveFailures = 1;
8433d4c4710SBarry Smith 
8449b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
84538f2d2fdSLisandro Dalcin   ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr);
8469b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
8479b94acceSBarry Smith   kctx->version     = 2;
8489b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
8499b94acceSBarry Smith                              this was too large for some test cases */
8509b94acceSBarry Smith   kctx->rtol_last   = 0;
8519b94acceSBarry Smith   kctx->rtol_max    = .9;
8529b94acceSBarry Smith   kctx->gamma       = 1.0;
85371f87433Sdalcinl   kctx->alpha       = .5*(1.0 + sqrt(5.0));
85471f87433Sdalcinl   kctx->alpha2      = kctx->alpha;
8559b94acceSBarry Smith   kctx->threshold   = .1;
8569b94acceSBarry Smith   kctx->lresid_last = 0;
8579b94acceSBarry Smith   kctx->norm_last   = 0;
8589b94acceSBarry Smith 
85994b7f48cSBarry Smith   ierr = KSPCreate(comm,&snes->ksp);CHKERRQ(ierr);
86052e6d16bSBarry Smith   ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr);
8615334005bSBarry Smith 
8629b94acceSBarry Smith   *outsnes = snes;
86300036973SBarry Smith   ierr = PetscPublishAll(snes);CHKERRQ(ierr);
8643a40ed3dSBarry Smith   PetscFunctionReturn(0);
8659b94acceSBarry Smith }
8669b94acceSBarry Smith 
8674a2ae208SSatish Balay #undef __FUNCT__
8684a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction"
8699b94acceSBarry Smith /*@C
8709b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
8719b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
8729b94acceSBarry Smith    equations.
8739b94acceSBarry Smith 
874fee21e36SBarry Smith    Collective on SNES
875fee21e36SBarry Smith 
876c7afd0dbSLois Curfman McInnes    Input Parameters:
877c7afd0dbSLois Curfman McInnes +  snes - the SNES context
878c7afd0dbSLois Curfman McInnes .  r - vector to store function value
879de044059SHong Zhang .  func - function evaluation routine
880c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
881c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
8829b94acceSBarry Smith 
883c7afd0dbSLois Curfman McInnes    Calling sequence of func:
8848d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
885c7afd0dbSLois Curfman McInnes 
886313e4042SLois Curfman McInnes .  f - function vector
887c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
8889b94acceSBarry Smith 
8899b94acceSBarry Smith    Notes:
8909b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
8919b94acceSBarry Smith $      f'(x) x = -f(x),
892c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
8939b94acceSBarry Smith 
89436851e7fSLois Curfman McInnes    Level: beginner
89536851e7fSLois Curfman McInnes 
8969b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
8979b94acceSBarry Smith 
898a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
8999b94acceSBarry Smith @*/
90063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx)
9019b94acceSBarry Smith {
90285385478SLisandro Dalcin   PetscErrorCode ierr;
9033a40ed3dSBarry Smith   PetscFunctionBegin;
9044482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
9054482741eSBarry Smith   PetscValidHeaderSpecific(r,VEC_COOKIE,2);
906c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,r,2);
90785385478SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr);
90885385478SLisandro Dalcin   if (snes->vec_func) { ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr); }
909e7788613SBarry Smith   snes->ops->computefunction = func;
91085385478SLisandro Dalcin   snes->vec_func             = r;
9119b94acceSBarry Smith   snes->funP                 = ctx;
9123a40ed3dSBarry Smith   PetscFunctionReturn(0);
9139b94acceSBarry Smith }
9149b94acceSBarry Smith 
9153ab0aad5SBarry Smith /* --------------------------------------------------------------- */
9163ab0aad5SBarry Smith #undef __FUNCT__
9171096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs"
9181096aae1SMatthew Knepley /*@C
9191096aae1SMatthew Knepley    SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set
9201096aae1SMatthew Knepley    it assumes a zero right hand side.
9211096aae1SMatthew Knepley 
9221096aae1SMatthew Knepley    Collective on SNES
9231096aae1SMatthew Knepley 
9241096aae1SMatthew Knepley    Input Parameter:
9251096aae1SMatthew Knepley .  snes - the SNES context
9261096aae1SMatthew Knepley 
9271096aae1SMatthew Knepley    Output Parameter:
928bc08b0f1SBarry Smith .  rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null
9291096aae1SMatthew Knepley 
9301096aae1SMatthew Knepley    Level: intermediate
9311096aae1SMatthew Knepley 
9321096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side
9331096aae1SMatthew Knepley 
93485385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
9351096aae1SMatthew Knepley @*/
9361096aae1SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetRhs(SNES snes,Vec *rhs)
9371096aae1SMatthew Knepley {
9381096aae1SMatthew Knepley   PetscFunctionBegin;
9391096aae1SMatthew Knepley   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
9401096aae1SMatthew Knepley   PetscValidPointer(rhs,2);
94185385478SLisandro Dalcin   *rhs = snes->vec_rhs;
9421096aae1SMatthew Knepley   PetscFunctionReturn(0);
9431096aae1SMatthew Knepley }
9441096aae1SMatthew Knepley 
9451096aae1SMatthew Knepley #undef __FUNCT__
9464a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction"
9479b94acceSBarry Smith /*@
94836851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
9499b94acceSBarry Smith                          SNESSetFunction().
9509b94acceSBarry Smith 
951c7afd0dbSLois Curfman McInnes    Collective on SNES
952c7afd0dbSLois Curfman McInnes 
9539b94acceSBarry Smith    Input Parameters:
954c7afd0dbSLois Curfman McInnes +  snes - the SNES context
955c7afd0dbSLois Curfman McInnes -  x - input vector
9569b94acceSBarry Smith 
9579b94acceSBarry Smith    Output Parameter:
9583638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
9599b94acceSBarry Smith 
9601bffabb2SLois Curfman McInnes    Notes:
96136851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
96236851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
96336851e7fSLois Curfman McInnes    themselves.
96436851e7fSLois Curfman McInnes 
96536851e7fSLois Curfman McInnes    Level: developer
96636851e7fSLois Curfman McInnes 
9679b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
9689b94acceSBarry Smith 
969a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
9709b94acceSBarry Smith @*/
97163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeFunction(SNES snes,Vec x,Vec y)
9729b94acceSBarry Smith {
973dfbe8321SBarry Smith   PetscErrorCode ierr;
9749b94acceSBarry Smith 
9753a40ed3dSBarry Smith   PetscFunctionBegin;
9764482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
9774482741eSBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE,2);
9784482741eSBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE,3);
979c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,x,2);
980c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,3);
981184914b5SBarry Smith 
982d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
983e7788613SBarry Smith   if (snes->ops->computefunction) {
984d64ed03dSBarry Smith     PetscStackPush("SNES user function");
985e9a2bbcdSBarry Smith     CHKMEMQ;
986e7788613SBarry Smith     ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP);
987e9a2bbcdSBarry Smith     CHKMEMQ;
988d64ed03dSBarry Smith     PetscStackPop;
989d5e45103SBarry Smith     if (PetscExceptionValue(ierr)) {
99019717074SBarry Smith       PetscErrorCode pierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(pierr);
99119717074SBarry Smith     }
992d5e45103SBarry Smith     CHKERRQ(ierr);
99385385478SLisandro Dalcin   } else if (snes->vec_rhs) {
9941096aae1SMatthew Knepley     ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr);
9951096aae1SMatthew Knepley   } else {
9961096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() before SNESComputeFunction(), likely called from SNESSolve().");
9971096aae1SMatthew Knepley   }
99885385478SLisandro Dalcin   if (snes->vec_rhs) {
99985385478SLisandro Dalcin     ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr);
10003ab0aad5SBarry Smith   }
1001ae3c334cSLois Curfman McInnes   snes->nfuncs++;
1002d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
10033a40ed3dSBarry Smith   PetscFunctionReturn(0);
10049b94acceSBarry Smith }
10059b94acceSBarry Smith 
10064a2ae208SSatish Balay #undef __FUNCT__
10074a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian"
100862fef451SLois Curfman McInnes /*@
100962fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
101062fef451SLois Curfman McInnes    set with SNESSetJacobian().
101162fef451SLois Curfman McInnes 
1012c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1013c7afd0dbSLois Curfman McInnes 
101462fef451SLois Curfman McInnes    Input Parameters:
1015c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1016c7afd0dbSLois Curfman McInnes -  x - input vector
101762fef451SLois Curfman McInnes 
101862fef451SLois Curfman McInnes    Output Parameters:
1019c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
102062fef451SLois Curfman McInnes .  B - optional preconditioning matrix
10212b668275SBarry Smith -  flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER)
1022fee21e36SBarry Smith 
102362fef451SLois Curfman McInnes    Notes:
102462fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
102562fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
102662fef451SLois Curfman McInnes 
102794b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
1028dc5a77f8SLois Curfman McInnes    flag parameter.
102962fef451SLois Curfman McInnes 
103036851e7fSLois Curfman McInnes    Level: developer
103136851e7fSLois Curfman McInnes 
103262fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
103362fef451SLois Curfman McInnes 
10342b668275SBarry Smith .seealso:  SNESSetJacobian(), KSPSetOperators(), MatStructure
103562fef451SLois Curfman McInnes @*/
103663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
10379b94acceSBarry Smith {
1038dfbe8321SBarry Smith   PetscErrorCode ierr;
10393a40ed3dSBarry Smith 
10403a40ed3dSBarry Smith   PetscFunctionBegin;
10414482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
10424482741eSBarry Smith   PetscValidHeaderSpecific(X,VEC_COOKIE,2);
10434482741eSBarry Smith   PetscValidPointer(flg,5);
1044c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,X,2);
1045e7788613SBarry Smith   if (!snes->ops->computejacobian) PetscFunctionReturn(0);
1046d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1047c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1048d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
1049dc67937bSBarry Smith   CHKMEMQ;
1050e7788613SBarry Smith   ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
1051dc67937bSBarry Smith   CHKMEMQ;
1052d64ed03dSBarry Smith   PetscStackPop;
1053d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
10546d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
10556ce558aeSBarry Smith   /* PetscValidHeaderSpecific(*A,MAT_COOKIE,3);
10566ce558aeSBarry Smith     PetscValidHeaderSpecific(*B,MAT_COOKIE,4);   */
10573a40ed3dSBarry Smith   PetscFunctionReturn(0);
10589b94acceSBarry Smith }
10599b94acceSBarry Smith 
10604a2ae208SSatish Balay #undef __FUNCT__
10614a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian"
10629b94acceSBarry Smith /*@C
10639b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1064044dda88SLois Curfman McInnes    location to store the matrix.
10659b94acceSBarry Smith 
1066c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1067c7afd0dbSLois Curfman McInnes 
10689b94acceSBarry Smith    Input Parameters:
1069c7afd0dbSLois Curfman McInnes +  snes - the SNES context
10709b94acceSBarry Smith .  A - Jacobian matrix
10719b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
10729b94acceSBarry Smith .  func - Jacobian evaluation routine
1073c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
10742cd2dfdcSLois Curfman McInnes          Jacobian evaluation routine (may be PETSC_NULL)
10759b94acceSBarry Smith 
10769b94acceSBarry Smith    Calling sequence of func:
10778d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
10789b94acceSBarry Smith 
1079c7afd0dbSLois Curfman McInnes +  x - input vector
10809b94acceSBarry Smith .  A - Jacobian matrix
10819b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1082ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
10832b668275SBarry Smith    structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER
1084c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
10859b94acceSBarry Smith 
10869b94acceSBarry Smith    Notes:
108794b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
10882cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1089ac21db08SLois Curfman McInnes 
1090ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
10919b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
10929b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
10939b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
10949b94acceSBarry Smith    throughout the global iterations.
10959b94acceSBarry Smith 
109636851e7fSLois Curfman McInnes    Level: beginner
109736851e7fSLois Curfman McInnes 
10989b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
10999b94acceSBarry Smith 
11003ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure
11019b94acceSBarry Smith @*/
110263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
11039b94acceSBarry Smith {
1104dfbe8321SBarry Smith   PetscErrorCode ierr;
11053a7fca6bSBarry Smith 
11063a40ed3dSBarry Smith   PetscFunctionBegin;
11074482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
11084482741eSBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_COOKIE,2);
11094482741eSBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_COOKIE,3);
1110c9780b6fSBarry Smith   if (A) PetscCheckSameComm(snes,1,A,2);
1111c9780b6fSBarry Smith   if (B) PetscCheckSameComm(snes,1,B,2);
1112e7788613SBarry Smith   if (func) snes->ops->computejacobian = func;
11133a7fca6bSBarry Smith   if (ctx)  snes->jacP                 = ctx;
11143a7fca6bSBarry Smith   if (A) {
11157dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
11163a7fca6bSBarry Smith     if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
11179b94acceSBarry Smith     snes->jacobian = A;
11183a7fca6bSBarry Smith   }
11193a7fca6bSBarry Smith   if (B) {
11207dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
11213a7fca6bSBarry Smith     if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
11229b94acceSBarry Smith     snes->jacobian_pre = B;
11233a7fca6bSBarry Smith   }
11243a40ed3dSBarry Smith   PetscFunctionReturn(0);
11259b94acceSBarry Smith }
112662fef451SLois Curfman McInnes 
11274a2ae208SSatish Balay #undef __FUNCT__
11284a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian"
1129c2aafc4cSSatish Balay /*@C
1130b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1131b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1132b4fd4287SBarry Smith 
1133c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1134c7afd0dbSLois Curfman McInnes 
1135b4fd4287SBarry Smith    Input Parameter:
1136b4fd4287SBarry Smith .  snes - the nonlinear solver context
1137b4fd4287SBarry Smith 
1138b4fd4287SBarry Smith    Output Parameters:
1139c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1140b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
114170e92668SMatthew Knepley .  func - location to put Jacobian function (or PETSC_NULL)
114270e92668SMatthew Knepley -  ctx - location to stash Jacobian ctx (or PETSC_NULL)
1143fee21e36SBarry Smith 
114436851e7fSLois Curfman McInnes    Level: advanced
114536851e7fSLois Curfman McInnes 
1146b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1147b4fd4287SBarry Smith @*/
114870e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx)
1149b4fd4287SBarry Smith {
11503a40ed3dSBarry Smith   PetscFunctionBegin;
11514482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1152b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
1153b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
1154e7788613SBarry Smith   if (func) *func = snes->ops->computejacobian;
115570e92668SMatthew Knepley   if (ctx)  *ctx  = snes->jacP;
11563a40ed3dSBarry Smith   PetscFunctionReturn(0);
1157b4fd4287SBarry Smith }
1158b4fd4287SBarry Smith 
11599b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
116063dd3a1aSKris Buschelman EXTERN PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*);
11619b94acceSBarry Smith 
11624a2ae208SSatish Balay #undef __FUNCT__
11634a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp"
11649b94acceSBarry Smith /*@
11659b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1166272ac6f2SLois Curfman McInnes    of a nonlinear solver.
11679b94acceSBarry Smith 
1168fee21e36SBarry Smith    Collective on SNES
1169fee21e36SBarry Smith 
1170c7afd0dbSLois Curfman McInnes    Input Parameters:
117170e92668SMatthew Knepley .  snes - the SNES context
1172c7afd0dbSLois Curfman McInnes 
1173272ac6f2SLois Curfman McInnes    Notes:
1174272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1175272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1176272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1177272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1178272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1179272ac6f2SLois Curfman McInnes 
118036851e7fSLois Curfman McInnes    Level: advanced
118136851e7fSLois Curfman McInnes 
11829b94acceSBarry Smith .keywords: SNES, nonlinear, setup
11839b94acceSBarry Smith 
11849b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
11859b94acceSBarry Smith @*/
118670e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUp(SNES snes)
11879b94acceSBarry Smith {
1188dfbe8321SBarry Smith   PetscErrorCode ierr;
118971f87433Sdalcinl   PetscTruth     flg;
11903a40ed3dSBarry Smith 
11913a40ed3dSBarry Smith   PetscFunctionBegin;
11924482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
11934dc4c822SBarry Smith   if (snes->setupcalled) PetscFunctionReturn(0);
11949b94acceSBarry Smith 
11957adad957SLisandro Dalcin   if (!((PetscObject)snes)->type_name) {
119685385478SLisandro Dalcin     ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr);
119785385478SLisandro Dalcin   }
119885385478SLisandro Dalcin 
11997adad957SLisandro Dalcin   ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf_operator",&flg);CHKERRQ(ierr);
1200c1f60f51SBarry Smith   /*
1201c1f60f51SBarry Smith       This version replaces the user provided Jacobian matrix with a
1202dfa02198SLois Curfman McInnes       matrix-free version but still employs the user-provided preconditioner matrix
1203c1f60f51SBarry Smith   */
1204c1f60f51SBarry Smith   if (flg) {
1205c1f60f51SBarry Smith     Mat J;
1206fef1beadSBarry Smith     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
12073ec795f1SBarry Smith     ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr);
1208ae15b995SBarry Smith     ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr);
12093a7fca6bSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
12103a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
1211c1f60f51SBarry Smith   }
121245fc7adcSBarry Smith 
121303c60df9SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_MAT_SINGLE) && !defined(PETSC_USE_LONG_DOUBLE) && !defined(PETSC_USE_INT)
12147adad957SLisandro Dalcin   ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf_operator2",&flg);CHKERRQ(ierr);
121545fc7adcSBarry Smith   if (flg) {
121645fc7adcSBarry Smith     Mat J;
121745fc7adcSBarry Smith     ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_sol,&J);CHKERRQ(ierr);
121875396ef9SLisandro Dalcin     ierr = PetscInfo(snes,"Setting default matrix-free operator routines (version 2)\n");CHKERRQ(ierr);
121945fc7adcSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
122045fc7adcSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
122145fc7adcSBarry Smith   }
122232a4b47aSMatthew Knepley #endif
122345fc7adcSBarry Smith 
12247adad957SLisandro Dalcin   ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf",&flg);CHKERRQ(ierr);
1225c1f60f51SBarry Smith   /*
1226dfa02198SLois Curfman McInnes       This version replaces both the user-provided Jacobian and the user-
1227c1f60f51SBarry Smith       provided preconditioner matrix with the default matrix free version.
1228c1f60f51SBarry Smith    */
122931615d04SLois Curfman McInnes   if (flg) {
1230272ac6f2SLois Curfman McInnes     Mat  J;
1231b5d62d44SBarry Smith     KSP ksp;
123294b7f48cSBarry Smith     PC   pc;
123375396ef9SLisandro Dalcin     /* create and set matrix-free operator */
1234fef1beadSBarry Smith     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
12353ec795f1SBarry Smith     ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr);
123675396ef9SLisandro Dalcin     ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr);
12373ec795f1SBarry Smith     ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr);
12383a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
1239f3ef73ceSBarry Smith     /* force no preconditioner */
124094b7f48cSBarry Smith     ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
1241b5d62d44SBarry Smith     ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
1242a9815358SBarry Smith     ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&flg);CHKERRQ(ierr);
1243a9815358SBarry Smith     if (!flg) {
124475396ef9SLisandro Dalcin       ierr = PetscInfo(snes,"Setting default matrix-free preconditioner routines;\nThat is no preconditioner is being used\n");CHKERRQ(ierr);
1245f3ef73ceSBarry Smith       ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
1246272ac6f2SLois Curfman McInnes     }
1247a9815358SBarry Smith   }
1248f3ef73ceSBarry Smith 
124985385478SLisandro Dalcin   if (!snes->vec_func && !snes->vec_rhs) {
12501096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
12511096aae1SMatthew Knepley   }
125285385478SLisandro Dalcin   if (!snes->ops->computefunction && !snes->vec_rhs) {
12531096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
12541096aae1SMatthew Knepley   }
125575396ef9SLisandro Dalcin   if (!snes->jacobian) {
125675396ef9SLisandro Dalcin     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetJacobian() first \n or use -snes_mf option");
125775396ef9SLisandro Dalcin   }
1258a8c6a408SBarry Smith   if (snes->vec_func == snes->vec_sol) {
125929bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
1260a8c6a408SBarry Smith   }
1261a703fe33SLois Curfman McInnes 
1262410397dcSLisandro Dalcin   if (snes->ops->setup) {
1263410397dcSLisandro Dalcin     ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr);
1264410397dcSLisandro Dalcin   }
12657aaed0d8SBarry Smith   snes->setupcalled = PETSC_TRUE;
12663a40ed3dSBarry Smith   PetscFunctionReturn(0);
12679b94acceSBarry Smith }
12689b94acceSBarry Smith 
12694a2ae208SSatish Balay #undef __FUNCT__
12704a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
127152baeb72SSatish Balay /*@
12729b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
12739b94acceSBarry Smith    with SNESCreate().
12749b94acceSBarry Smith 
1275c7afd0dbSLois Curfman McInnes    Collective on SNES
1276c7afd0dbSLois Curfman McInnes 
12779b94acceSBarry Smith    Input Parameter:
12789b94acceSBarry Smith .  snes - the SNES context
12799b94acceSBarry Smith 
128036851e7fSLois Curfman McInnes    Level: beginner
128136851e7fSLois Curfman McInnes 
12829b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
12839b94acceSBarry Smith 
128463a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
12859b94acceSBarry Smith @*/
128663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDestroy(SNES snes)
12879b94acceSBarry Smith {
12886849ba73SBarry Smith   PetscErrorCode ierr;
12893a40ed3dSBarry Smith 
12903a40ed3dSBarry Smith   PetscFunctionBegin;
12914482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
12927adad957SLisandro Dalcin   if (--((PetscObject)snes)->refct > 0) PetscFunctionReturn(0);
1293d4bb536fSBarry Smith 
1294be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
12950f5bd95cSBarry Smith   ierr = PetscObjectDepublish(snes);CHKERRQ(ierr);
1296e7788613SBarry Smith   if (snes->ops->destroy) {ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr);}
129785385478SLisandro Dalcin 
129885385478SLisandro Dalcin   if (snes->vec_rhs) {ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr);}
129985385478SLisandro Dalcin   if (snes->vec_sol) {ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr);}
130085385478SLisandro Dalcin   if (snes->vec_func) {ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr);}
13013a7fca6bSBarry Smith   if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
13023a7fca6bSBarry Smith   if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
130394b7f48cSBarry Smith   ierr = KSPDestroy(snes->ksp);CHKERRQ(ierr);
130485385478SLisandro Dalcin   ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);
1305522c5e43SBarry Smith   if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);}
1306a6570f20SBarry Smith   ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);
1307*7f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);}
1308a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr);
13093a40ed3dSBarry Smith   PetscFunctionReturn(0);
13109b94acceSBarry Smith }
13119b94acceSBarry Smith 
13129b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
13139b94acceSBarry Smith 
13144a2ae208SSatish Balay #undef __FUNCT__
13154a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
13169b94acceSBarry Smith /*@
1317d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
13189b94acceSBarry Smith 
1319c7afd0dbSLois Curfman McInnes    Collective on SNES
1320c7afd0dbSLois Curfman McInnes 
13219b94acceSBarry Smith    Input Parameters:
1322c7afd0dbSLois Curfman McInnes +  snes - the SNES context
132370441072SBarry Smith .  abstol - absolute convergence tolerance
132433174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
132533174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
132633174efeSLois Curfman McInnes            of the change in the solution between steps
132733174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1328c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1329fee21e36SBarry Smith 
133033174efeSLois Curfman McInnes    Options Database Keys:
133170441072SBarry Smith +    -snes_atol <abstol> - Sets abstol
1332c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1333c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1334c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1335c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
13369b94acceSBarry Smith 
1337d7a720efSLois Curfman McInnes    Notes:
13389b94acceSBarry Smith    The default maximum number of iterations is 50.
13399b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
13409b94acceSBarry Smith 
134136851e7fSLois Curfman McInnes    Level: intermediate
134236851e7fSLois Curfman McInnes 
134333174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
13449b94acceSBarry Smith 
13452492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance()
13469b94acceSBarry Smith @*/
134763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf)
13489b94acceSBarry Smith {
13493a40ed3dSBarry Smith   PetscFunctionBegin;
13504482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
135170441072SBarry Smith   if (abstol != PETSC_DEFAULT)  snes->abstol      = abstol;
1352d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1353d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1354d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1355d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
13563a40ed3dSBarry Smith   PetscFunctionReturn(0);
13579b94acceSBarry Smith }
13589b94acceSBarry Smith 
13594a2ae208SSatish Balay #undef __FUNCT__
13604a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
13619b94acceSBarry Smith /*@
136233174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
136333174efeSLois Curfman McInnes 
1364c7afd0dbSLois Curfman McInnes    Not Collective
1365c7afd0dbSLois Curfman McInnes 
136633174efeSLois Curfman McInnes    Input Parameters:
1367c7afd0dbSLois Curfman McInnes +  snes - the SNES context
136885385478SLisandro Dalcin .  atol - absolute convergence tolerance
136933174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
137033174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
137133174efeSLois Curfman McInnes            of the change in the solution between steps
137233174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1373c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1374fee21e36SBarry Smith 
137533174efeSLois Curfman McInnes    Notes:
137633174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
137733174efeSLois Curfman McInnes 
137836851e7fSLois Curfman McInnes    Level: intermediate
137936851e7fSLois Curfman McInnes 
138033174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
138133174efeSLois Curfman McInnes 
138233174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
138333174efeSLois Curfman McInnes @*/
138485385478SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf)
138533174efeSLois Curfman McInnes {
13863a40ed3dSBarry Smith   PetscFunctionBegin;
13874482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
138885385478SLisandro Dalcin   if (atol)  *atol  = snes->abstol;
138933174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
139033174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
139133174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
139233174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
13933a40ed3dSBarry Smith   PetscFunctionReturn(0);
139433174efeSLois Curfman McInnes }
139533174efeSLois Curfman McInnes 
13964a2ae208SSatish Balay #undef __FUNCT__
13974a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
139833174efeSLois Curfman McInnes /*@
13999b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
14009b94acceSBarry Smith 
1401fee21e36SBarry Smith    Collective on SNES
1402fee21e36SBarry Smith 
1403c7afd0dbSLois Curfman McInnes    Input Parameters:
1404c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1405c7afd0dbSLois Curfman McInnes -  tol - tolerance
1406c7afd0dbSLois Curfman McInnes 
14079b94acceSBarry Smith    Options Database Key:
1408c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
14099b94acceSBarry Smith 
141036851e7fSLois Curfman McInnes    Level: intermediate
141136851e7fSLois Curfman McInnes 
14129b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
14139b94acceSBarry Smith 
14142492ecdbSBarry Smith .seealso: SNESSetTolerances()
14159b94acceSBarry Smith @*/
141663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
14179b94acceSBarry Smith {
14183a40ed3dSBarry Smith   PetscFunctionBegin;
14194482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
14209b94acceSBarry Smith   snes->deltatol = tol;
14213a40ed3dSBarry Smith   PetscFunctionReturn(0);
14229b94acceSBarry Smith }
14239b94acceSBarry Smith 
1424df9fa365SBarry Smith /*
1425df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1426df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1427df9fa365SBarry Smith    macros instead of functions
1428df9fa365SBarry Smith */
14294a2ae208SSatish Balay #undef __FUNCT__
1430a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG"
1431a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx)
1432ce1608b8SBarry Smith {
1433dfbe8321SBarry Smith   PetscErrorCode ierr;
1434ce1608b8SBarry Smith 
1435ce1608b8SBarry Smith   PetscFunctionBegin;
14364482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1437a6570f20SBarry Smith   ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1438ce1608b8SBarry Smith   PetscFunctionReturn(0);
1439ce1608b8SBarry Smith }
1440ce1608b8SBarry Smith 
14414a2ae208SSatish Balay #undef __FUNCT__
1442a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate"
1443a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1444df9fa365SBarry Smith {
1445dfbe8321SBarry Smith   PetscErrorCode ierr;
1446df9fa365SBarry Smith 
1447df9fa365SBarry Smith   PetscFunctionBegin;
1448a6570f20SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1449df9fa365SBarry Smith   PetscFunctionReturn(0);
1450df9fa365SBarry Smith }
1451df9fa365SBarry Smith 
14524a2ae208SSatish Balay #undef __FUNCT__
1453a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy"
1454a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGDestroy(PetscDrawLG draw)
1455df9fa365SBarry Smith {
1456dfbe8321SBarry Smith   PetscErrorCode ierr;
1457df9fa365SBarry Smith 
1458df9fa365SBarry Smith   PetscFunctionBegin;
1459a6570f20SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
1460df9fa365SBarry Smith   PetscFunctionReturn(0);
1461df9fa365SBarry Smith }
1462df9fa365SBarry Smith 
14639b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
14649b94acceSBarry Smith 
14654a2ae208SSatish Balay #undef __FUNCT__
1466a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet"
14679b94acceSBarry Smith /*@C
1468a6570f20SBarry Smith    SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every
14699b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
14709b94acceSBarry Smith    progress.
14719b94acceSBarry Smith 
1472fee21e36SBarry Smith    Collective on SNES
1473fee21e36SBarry Smith 
1474c7afd0dbSLois Curfman McInnes    Input Parameters:
1475c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1476c7afd0dbSLois Curfman McInnes .  func - monitoring routine
1477b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
1478e8105e01SRichard Katz           monitor routine (use PETSC_NULL if no context is desired)
1479b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1480b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
14819b94acceSBarry Smith 
1482c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1483a7cc72afSBarry Smith $     int func(SNES snes,PetscInt its, PetscReal norm,void *mctx)
1484c7afd0dbSLois Curfman McInnes 
1485c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1486c7afd0dbSLois Curfman McInnes .    its - iteration number
1487c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
148840a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
14899b94acceSBarry Smith 
14909665c990SLois Curfman McInnes    Options Database Keys:
1491a6570f20SBarry Smith +    -snes_monitor        - sets SNESMonitorDefault()
1492a6570f20SBarry Smith .    -snes_monitor_draw    - sets line graph monitor,
1493a6570f20SBarry Smith                             uses SNESMonitorLGCreate()
1494a6570f20SBarry Smith _    -snes_monitor_cancel - cancels all monitors that have
1495c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
1496a6570f20SBarry Smith                             calls to SNESMonitorSet(), but
1497c7afd0dbSLois Curfman McInnes                             does not cancel those set via
1498c7afd0dbSLois Curfman McInnes                             the options database.
14999665c990SLois Curfman McInnes 
1500639f9d9dSBarry Smith    Notes:
15016bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
1502a6570f20SBarry Smith    SNESMonitorSet() multiple times; all will be called in the
15036bc08f3fSLois Curfman McInnes    order in which they were set.
1504639f9d9dSBarry Smith 
1505025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each SNES object
1506025f1a04SBarry Smith 
150736851e7fSLois Curfman McInnes    Level: intermediate
150836851e7fSLois Curfman McInnes 
15099b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
15109b94acceSBarry Smith 
1511a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel()
15129b94acceSBarry Smith @*/
1513b90d0a6eSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void*))
15149b94acceSBarry Smith {
1515b90d0a6eSBarry Smith   PetscInt i;
1516b90d0a6eSBarry Smith 
15173a40ed3dSBarry Smith   PetscFunctionBegin;
15184482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1519639f9d9dSBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) {
152029bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1521639f9d9dSBarry Smith   }
1522b90d0a6eSBarry Smith   for (i=0; i<snes->numbermonitors;i++) {
1523b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) PetscFunctionReturn(0);
1524b90d0a6eSBarry Smith 
1525b90d0a6eSBarry Smith     /* check if both default monitors that share common ASCII viewer */
1526b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitor == SNESMonitorDefault) {
1527b90d0a6eSBarry Smith       if (mctx && snes->monitorcontext[i]) {
1528b90d0a6eSBarry Smith         PetscErrorCode          ierr;
1529b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer1 = (PetscViewerASCIIMonitor) mctx;
1530b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer2 = (PetscViewerASCIIMonitor) snes->monitorcontext[i];
1531b90d0a6eSBarry Smith         if (viewer1->viewer == viewer2->viewer) {
1532b90d0a6eSBarry Smith           ierr = (*monitordestroy)(mctx);CHKERRQ(ierr);
1533b90d0a6eSBarry Smith           PetscFunctionReturn(0);
1534b90d0a6eSBarry Smith         }
1535b90d0a6eSBarry Smith       }
1536b90d0a6eSBarry Smith     }
1537b90d0a6eSBarry Smith   }
1538b90d0a6eSBarry Smith   snes->monitor[snes->numbermonitors]           = monitor;
1539b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
1540639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
15413a40ed3dSBarry Smith   PetscFunctionReturn(0);
15429b94acceSBarry Smith }
15439b94acceSBarry Smith 
15444a2ae208SSatish Balay #undef __FUNCT__
1545a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel"
15465cd90555SBarry Smith /*@C
1547a6570f20SBarry Smith    SNESMonitorCancel - Clears all the monitor functions for a SNES object.
15485cd90555SBarry Smith 
1549c7afd0dbSLois Curfman McInnes    Collective on SNES
1550c7afd0dbSLois Curfman McInnes 
15515cd90555SBarry Smith    Input Parameters:
15525cd90555SBarry Smith .  snes - the SNES context
15535cd90555SBarry Smith 
15541a480d89SAdministrator    Options Database Key:
1555a6570f20SBarry Smith .  -snes_monitor_cancel - cancels all monitors that have been hardwired
1556a6570f20SBarry Smith     into a code by calls to SNESMonitorSet(), but does not cancel those
1557c7afd0dbSLois Curfman McInnes     set via the options database
15585cd90555SBarry Smith 
15595cd90555SBarry Smith    Notes:
15605cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
15615cd90555SBarry Smith 
156236851e7fSLois Curfman McInnes    Level: intermediate
156336851e7fSLois Curfman McInnes 
15645cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
15655cd90555SBarry Smith 
1566a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet()
15675cd90555SBarry Smith @*/
1568a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorCancel(SNES snes)
15695cd90555SBarry Smith {
1570d952e501SBarry Smith   PetscErrorCode ierr;
1571d952e501SBarry Smith   PetscInt       i;
1572d952e501SBarry Smith 
15735cd90555SBarry Smith   PetscFunctionBegin;
15744482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1575d952e501SBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
1576d952e501SBarry Smith     if (snes->monitordestroy[i]) {
1577d952e501SBarry Smith       ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr);
1578d952e501SBarry Smith     }
1579d952e501SBarry Smith   }
15805cd90555SBarry Smith   snes->numbermonitors = 0;
15815cd90555SBarry Smith   PetscFunctionReturn(0);
15825cd90555SBarry Smith }
15835cd90555SBarry Smith 
15844a2ae208SSatish Balay #undef __FUNCT__
15854a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
15869b94acceSBarry Smith /*@C
15879b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
15889b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
15899b94acceSBarry Smith 
1590fee21e36SBarry Smith    Collective on SNES
1591fee21e36SBarry Smith 
1592c7afd0dbSLois Curfman McInnes    Input Parameters:
1593c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1594c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
1595*7f7931b9SBarry Smith .  cctx - [optional] context for private data for the convergence routine  (may be PETSC_NULL)
1596*7f7931b9SBarry Smith -  destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran)
15979b94acceSBarry Smith 
1598c7afd0dbSLois Curfman McInnes    Calling sequence of func:
159906ee9f85SBarry Smith $     PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
1600c7afd0dbSLois Curfman McInnes 
1601c7afd0dbSLois Curfman McInnes +    snes - the SNES context
160206ee9f85SBarry Smith .    it - current iteration (0 is the first and is before any Newton step)
1603c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
1604184914b5SBarry Smith .    reason - reason for convergence/divergence
1605c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
16064b27c08aSLois Curfman McInnes .    gnorm - 2-norm of current step
16074b27c08aSLois Curfman McInnes -    f - 2-norm of function
16089b94acceSBarry Smith 
160936851e7fSLois Curfman McInnes    Level: advanced
161036851e7fSLois Curfman McInnes 
16119b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
16129b94acceSBarry Smith 
161385385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged()
16149b94acceSBarry Smith @*/
1615*7f7931b9SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*))
16169b94acceSBarry Smith {
1617*7f7931b9SBarry Smith   PetscErrorCode ierr;
1618*7f7931b9SBarry Smith 
16193a40ed3dSBarry Smith   PetscFunctionBegin;
16204482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
162185385478SLisandro Dalcin   if (!func) func = SNESSkipConverged;
1622*7f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {
1623*7f7931b9SBarry Smith     ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);
1624*7f7931b9SBarry Smith   }
162585385478SLisandro Dalcin   snes->ops->converged        = func;
1626*7f7931b9SBarry Smith   snes->ops->convergeddestroy = destroy;
162785385478SLisandro Dalcin   snes->cnvP                  = cctx;
16283a40ed3dSBarry Smith   PetscFunctionReturn(0);
16299b94acceSBarry Smith }
16309b94acceSBarry Smith 
16314a2ae208SSatish Balay #undef __FUNCT__
16324a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
163352baeb72SSatish Balay /*@
1634184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
1635184914b5SBarry Smith 
1636184914b5SBarry Smith    Not Collective
1637184914b5SBarry Smith 
1638184914b5SBarry Smith    Input Parameter:
1639184914b5SBarry Smith .  snes - the SNES context
1640184914b5SBarry Smith 
1641184914b5SBarry Smith    Output Parameter:
1642e090d566SSatish Balay .  reason - negative value indicates diverged, positive value converged, see petscsnes.h or the
1643184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
1644184914b5SBarry Smith 
1645184914b5SBarry Smith    Level: intermediate
1646184914b5SBarry Smith 
1647184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
1648184914b5SBarry Smith 
1649184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
1650184914b5SBarry Smith 
165185385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason
1652184914b5SBarry Smith @*/
165363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
1654184914b5SBarry Smith {
1655184914b5SBarry Smith   PetscFunctionBegin;
16564482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
16574482741eSBarry Smith   PetscValidPointer(reason,2);
1658184914b5SBarry Smith   *reason = snes->reason;
1659184914b5SBarry Smith   PetscFunctionReturn(0);
1660184914b5SBarry Smith }
1661184914b5SBarry Smith 
16624a2ae208SSatish Balay #undef __FUNCT__
16634a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
1664c9005455SLois Curfman McInnes /*@
1665c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
1666c9005455SLois Curfman McInnes 
1667fee21e36SBarry Smith    Collective on SNES
1668fee21e36SBarry Smith 
1669c7afd0dbSLois Curfman McInnes    Input Parameters:
1670c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
1671c7afd0dbSLois Curfman McInnes .  a   - array to hold history
1672cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
1673758f92a0SBarry Smith .  na  - size of a and its
167464731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
1675758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
1676c7afd0dbSLois Curfman McInnes 
1677c9005455SLois Curfman McInnes    Notes:
16784b27c08aSLois Curfman McInnes    If set, this array will contain the function norms computed
1679c9005455SLois Curfman McInnes    at each step.
1680c9005455SLois Curfman McInnes 
1681c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
1682c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
1683c9005455SLois Curfman McInnes    during the section of code that is being timed.
1684c9005455SLois Curfman McInnes 
168536851e7fSLois Curfman McInnes    Level: intermediate
168636851e7fSLois Curfman McInnes 
1687c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
1688758f92a0SBarry Smith 
168908405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
1690758f92a0SBarry Smith 
1691c9005455SLois Curfman McInnes @*/
1692a562a398SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscTruth reset)
1693c9005455SLois Curfman McInnes {
16943a40ed3dSBarry Smith   PetscFunctionBegin;
16954482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
16964482741eSBarry Smith   if (na)  PetscValidScalarPointer(a,2);
1697a562a398SLisandro Dalcin   if (its) PetscValidIntPointer(its,3);
1698c9005455SLois Curfman McInnes   snes->conv_hist       = a;
1699758f92a0SBarry Smith   snes->conv_hist_its   = its;
1700758f92a0SBarry Smith   snes->conv_hist_max   = na;
1701758f92a0SBarry Smith   snes->conv_hist_reset = reset;
1702758f92a0SBarry Smith   PetscFunctionReturn(0);
1703758f92a0SBarry Smith }
1704758f92a0SBarry Smith 
17054a2ae208SSatish Balay #undef __FUNCT__
17064a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
17070c4c9dddSBarry Smith /*@C
1708758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
1709758f92a0SBarry Smith 
1710758f92a0SBarry Smith    Collective on SNES
1711758f92a0SBarry Smith 
1712758f92a0SBarry Smith    Input Parameter:
1713758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
1714758f92a0SBarry Smith 
1715758f92a0SBarry Smith    Output Parameters:
1716758f92a0SBarry Smith .  a   - array to hold history
1717758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
1718758f92a0SBarry Smith          negative if not converged) for each solve.
1719758f92a0SBarry Smith -  na  - size of a and its
1720758f92a0SBarry Smith 
1721758f92a0SBarry Smith    Notes:
1722758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
1723758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
1724758f92a0SBarry Smith 
1725758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
1726758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
1727758f92a0SBarry Smith    during the section of code that is being timed.
1728758f92a0SBarry Smith 
1729758f92a0SBarry Smith    Level: intermediate
1730758f92a0SBarry Smith 
1731758f92a0SBarry Smith .keywords: SNES, get, convergence, history
1732758f92a0SBarry Smith 
1733758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
1734758f92a0SBarry Smith 
1735758f92a0SBarry Smith @*/
173663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na)
1737758f92a0SBarry Smith {
1738758f92a0SBarry Smith   PetscFunctionBegin;
17394482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1740758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
1741758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
1742758f92a0SBarry Smith   if (na)  *na  = snes->conv_hist_len;
17433a40ed3dSBarry Smith   PetscFunctionReturn(0);
1744c9005455SLois Curfman McInnes }
1745c9005455SLois Curfman McInnes 
1746e74ef692SMatthew Knepley #undef __FUNCT__
1747e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
1748ac226902SBarry Smith /*@C
174976b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
17507e4bb74cSBarry Smith   at the beginning o every iteration of the nonlinear solve. Specifically
17517e4bb74cSBarry Smith   it is called just before the Jacobian is "evaluated".
175276b2cf59SMatthew Knepley 
175376b2cf59SMatthew Knepley   Collective on SNES
175476b2cf59SMatthew Knepley 
175576b2cf59SMatthew Knepley   Input Parameters:
175676b2cf59SMatthew Knepley . snes - The nonlinear solver context
175776b2cf59SMatthew Knepley . func - The function
175876b2cf59SMatthew Knepley 
175976b2cf59SMatthew Knepley   Calling sequence of func:
1760b5d30489SBarry Smith . func (SNES snes, PetscInt step);
176176b2cf59SMatthew Knepley 
176276b2cf59SMatthew Knepley . step - The current step of the iteration
176376b2cf59SMatthew Knepley 
176476b2cf59SMatthew Knepley   Level: intermediate
176576b2cf59SMatthew Knepley 
176676b2cf59SMatthew Knepley .keywords: SNES, update
1767b5d30489SBarry Smith 
176885385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve()
176976b2cf59SMatthew Knepley @*/
177063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt))
177176b2cf59SMatthew Knepley {
177276b2cf59SMatthew Knepley   PetscFunctionBegin;
17734482741eSBarry Smith   PetscValidHeaderSpecific(snes, SNES_COOKIE,1);
1774e7788613SBarry Smith   snes->ops->update = func;
177576b2cf59SMatthew Knepley   PetscFunctionReturn(0);
177676b2cf59SMatthew Knepley }
177776b2cf59SMatthew Knepley 
1778e74ef692SMatthew Knepley #undef __FUNCT__
1779e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
178076b2cf59SMatthew Knepley /*@
178176b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
178276b2cf59SMatthew Knepley 
178376b2cf59SMatthew Knepley   Not collective
178476b2cf59SMatthew Knepley 
178576b2cf59SMatthew Knepley   Input Parameters:
178676b2cf59SMatthew Knepley . snes - The nonlinear solver context
178776b2cf59SMatthew Knepley . step - The current step of the iteration
178876b2cf59SMatthew Knepley 
1789205452f4SMatthew Knepley   Level: intermediate
1790205452f4SMatthew Knepley 
179176b2cf59SMatthew Knepley .keywords: SNES, update
1792a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC()
179376b2cf59SMatthew Knepley @*/
179463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultUpdate(SNES snes, PetscInt step)
179576b2cf59SMatthew Knepley {
179676b2cf59SMatthew Knepley   PetscFunctionBegin;
179776b2cf59SMatthew Knepley   PetscFunctionReturn(0);
179876b2cf59SMatthew Knepley }
179976b2cf59SMatthew Knepley 
18004a2ae208SSatish Balay #undef __FUNCT__
18014a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
18029b94acceSBarry Smith /*
18039b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
18049b94acceSBarry Smith    positive parameter delta.
18059b94acceSBarry Smith 
18069b94acceSBarry Smith     Input Parameters:
1807c7afd0dbSLois Curfman McInnes +   snes - the SNES context
18089b94acceSBarry Smith .   y - approximate solution of linear system
18099b94acceSBarry Smith .   fnorm - 2-norm of current function
1810c7afd0dbSLois Curfman McInnes -   delta - trust region size
18119b94acceSBarry Smith 
18129b94acceSBarry Smith     Output Parameters:
1813c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
18149b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
18159b94acceSBarry Smith     region, and exceeds zero otherwise.
1816c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
18179b94acceSBarry Smith 
18189b94acceSBarry Smith     Note:
18194b27c08aSLois Curfman McInnes     For non-trust region methods such as SNESLS, the parameter delta
18209b94acceSBarry Smith     is set to be the maximum allowable step size.
18219b94acceSBarry Smith 
18229b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
18239b94acceSBarry Smith */
1824dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
18259b94acceSBarry Smith {
1826064f8208SBarry Smith   PetscReal      nrm;
1827ea709b57SSatish Balay   PetscScalar    cnorm;
1828dfbe8321SBarry Smith   PetscErrorCode ierr;
18293a40ed3dSBarry Smith 
18303a40ed3dSBarry Smith   PetscFunctionBegin;
18314482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
18324482741eSBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE,2);
1833c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,2);
1834184914b5SBarry Smith 
1835064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
1836064f8208SBarry Smith   if (nrm > *delta) {
1837064f8208SBarry Smith      nrm = *delta/nrm;
1838064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
1839064f8208SBarry Smith      cnorm = nrm;
18402dcb1b2aSMatthew Knepley      ierr = VecScale(y,cnorm);CHKERRQ(ierr);
18419b94acceSBarry Smith      *ynorm = *delta;
18429b94acceSBarry Smith   } else {
18439b94acceSBarry Smith      *gpnorm = 0.0;
1844064f8208SBarry Smith      *ynorm = nrm;
18459b94acceSBarry Smith   }
18463a40ed3dSBarry Smith   PetscFunctionReturn(0);
18479b94acceSBarry Smith }
18489b94acceSBarry Smith 
18494a2ae208SSatish Balay #undef __FUNCT__
18504a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
18516ce558aeSBarry Smith /*@C
1852f69a0ea3SMatthew Knepley    SNESSolve - Solves a nonlinear system F(x) = b.
1853f69a0ea3SMatthew Knepley    Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX().
18549b94acceSBarry Smith 
1855c7afd0dbSLois Curfman McInnes    Collective on SNES
1856c7afd0dbSLois Curfman McInnes 
1857b2002411SLois Curfman McInnes    Input Parameters:
1858c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1859f69a0ea3SMatthew Knepley .  b - the constant part of the equation, or PETSC_NULL to use zero.
186085385478SLisandro Dalcin -  x - the solution vector.
18619b94acceSBarry Smith 
1862b2002411SLois Curfman McInnes    Notes:
18638ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
18648ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
18658ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
18668ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
18678ddd3da0SLois Curfman McInnes 
186836851e7fSLois Curfman McInnes    Level: beginner
186936851e7fSLois Curfman McInnes 
18709b94acceSBarry Smith .keywords: SNES, nonlinear, solve
18719b94acceSBarry Smith 
187285385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian()
18739b94acceSBarry Smith @*/
1874f69a0ea3SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSolve(SNES snes,Vec b,Vec x)
18759b94acceSBarry Smith {
1876dfbe8321SBarry Smith   PetscErrorCode ierr;
1877f1af5d2fSBarry Smith   PetscTruth     flg;
1878eabae89aSBarry Smith   char           filename[PETSC_MAX_PATH_LEN];
1879eabae89aSBarry Smith   PetscViewer    viewer;
1880052efed2SBarry Smith 
18813a40ed3dSBarry Smith   PetscFunctionBegin;
18824482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1883f69a0ea3SMatthew Knepley   PetscValidHeaderSpecific(x,VEC_COOKIE,3);
1884f69a0ea3SMatthew Knepley   PetscCheckSameComm(snes,1,x,3);
188585385478SLisandro Dalcin   if (b) PetscValidHeaderSpecific(b,VEC_COOKIE,2);
188685385478SLisandro Dalcin   if (b) PetscCheckSameComm(snes,1,b,2);
188785385478SLisandro Dalcin 
188885385478SLisandro Dalcin   /* set solution vector */
188985385478SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);
189085385478SLisandro Dalcin   if (snes->vec_sol) { ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr); }
189185385478SLisandro Dalcin   snes->vec_sol = x;
189285385478SLisandro Dalcin   /* set afine vector if provided */
189385385478SLisandro Dalcin   if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); }
189485385478SLisandro Dalcin   if (snes->vec_rhs) { ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr); }
189585385478SLisandro Dalcin   snes->vec_rhs = b;
189685385478SLisandro Dalcin 
189785385478SLisandro Dalcin   if (!snes->vec_func && snes->vec_rhs) {
189885385478SLisandro Dalcin     ierr = VecDuplicate(b, &snes->vec_func); CHKERRQ(ierr);
189970e92668SMatthew Knepley   }
19003f149594SLisandro Dalcin 
190170e92668SMatthew Knepley   ierr = SNESSetUp(snes);CHKERRQ(ierr);
19023f149594SLisandro Dalcin 
1903abc0a331SBarry Smith   if (snes->conv_hist_reset) snes->conv_hist_len = 0;
190450ffb88aSMatthew Knepley   snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
1905d5e45103SBarry Smith 
19063f149594SLisandro Dalcin   ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
19074936397dSBarry Smith   ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr);
190885385478SLisandro Dalcin   ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
19094936397dSBarry Smith   if (snes->domainerror){
19104936397dSBarry Smith     snes->reason      = SNES_DIVERGED_FUNCTION_DOMAIN;
19114936397dSBarry Smith     snes->domainerror = PETSC_FALSE;
19124936397dSBarry Smith   }
191385385478SLisandro Dalcin 
19143f149594SLisandro Dalcin   if (!snes->reason) {
19153f149594SLisandro Dalcin     SETERRQ(PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason");
19163f149594SLisandro Dalcin   }
19173f149594SLisandro Dalcin 
19187adad957SLisandro Dalcin   ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1919eabae89aSBarry Smith   if (flg && !PetscPreLoadingOn) {
19207adad957SLisandro Dalcin     ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr);
1921eabae89aSBarry Smith     ierr = SNESView(snes,viewer);CHKERRQ(ierr);
1922eabae89aSBarry Smith     ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr);
1923eabae89aSBarry Smith   }
1924eabae89aSBarry Smith 
19257adad957SLisandro Dalcin   ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg);CHKERRQ(ierr);
1926da9b6338SBarry Smith   if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); }
19275968eb51SBarry Smith   if (snes->printreason) {
19285968eb51SBarry Smith     if (snes->reason > 0) {
19297adad957SLisandro Dalcin       ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
19305968eb51SBarry Smith     } else {
19317adad957SLisandro Dalcin       ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
19325968eb51SBarry Smith     }
19335968eb51SBarry Smith   }
19345968eb51SBarry Smith 
19353a40ed3dSBarry Smith   PetscFunctionReturn(0);
19369b94acceSBarry Smith }
19379b94acceSBarry Smith 
19389b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
19399b94acceSBarry Smith 
19404a2ae208SSatish Balay #undef __FUNCT__
19414a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
194282bf6240SBarry Smith /*@C
19434b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
19449b94acceSBarry Smith 
1945fee21e36SBarry Smith    Collective on SNES
1946fee21e36SBarry Smith 
1947c7afd0dbSLois Curfman McInnes    Input Parameters:
1948c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1949454a90a3SBarry Smith -  type - a known method
1950c7afd0dbSLois Curfman McInnes 
1951c7afd0dbSLois Curfman McInnes    Options Database Key:
1952454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
1953c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
1954ae12b187SLois Curfman McInnes 
19559b94acceSBarry Smith    Notes:
1956e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
19574b27c08aSLois Curfman McInnes +    SNESLS - Newton's method with line search
1958c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
19594b27c08aSLois Curfman McInnes .    SNESTR - Newton's method with trust region
1960c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
19619b94acceSBarry Smith 
1962ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
1963ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
1964ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
1965ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
1966ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
1967ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
1968ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
1969ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
1970ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
1971b0a32e0cSBarry Smith   appropriate method.
197236851e7fSLois Curfman McInnes 
197336851e7fSLois Curfman McInnes   Level: intermediate
1974a703fe33SLois Curfman McInnes 
1975454a90a3SBarry Smith .keywords: SNES, set, type
1976435da068SBarry Smith 
1977435da068SBarry Smith .seealso: SNESType, SNESCreate()
1978435da068SBarry Smith 
19799b94acceSBarry Smith @*/
1980e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetType(SNES snes,SNESType type)
19819b94acceSBarry Smith {
1982dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(SNES);
19836831982aSBarry Smith   PetscTruth     match;
19843a40ed3dSBarry Smith 
19853a40ed3dSBarry Smith   PetscFunctionBegin;
19864482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
19874482741eSBarry Smith   PetscValidCharPointer(type,2);
198882bf6240SBarry Smith 
19896831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
19900f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
199192ff6ae8SBarry Smith 
19927adad957SLisandro Dalcin   ierr =  PetscFListFind(SNESList,((PetscObject)snes)->comm,type,(void (**)(void)) &r);CHKERRQ(ierr);
1993958c9bccSBarry Smith   if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type);
199475396ef9SLisandro Dalcin   /* Destroy the previous private SNES context */
199575396ef9SLisandro Dalcin   if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); }
199675396ef9SLisandro Dalcin   /* Reinitialize function pointers in SNESOps structure */
199775396ef9SLisandro Dalcin   snes->ops->setup          = 0;
199875396ef9SLisandro Dalcin   snes->ops->solve          = 0;
199975396ef9SLisandro Dalcin   snes->ops->view           = 0;
200075396ef9SLisandro Dalcin   snes->ops->setfromoptions = 0;
200175396ef9SLisandro Dalcin   snes->ops->destroy        = 0;
200275396ef9SLisandro Dalcin   /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */
200375396ef9SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
20043a40ed3dSBarry Smith   ierr = (*r)(snes);CHKERRQ(ierr);
2005454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
20063a40ed3dSBarry Smith   PetscFunctionReturn(0);
20079b94acceSBarry Smith }
20089b94acceSBarry Smith 
2009a847f771SSatish Balay 
20109b94acceSBarry Smith /* --------------------------------------------------------------------- */
20114a2ae208SSatish Balay #undef __FUNCT__
20124a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
201352baeb72SSatish Balay /*@
20149b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
2015f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
20169b94acceSBarry Smith 
2017fee21e36SBarry Smith    Not Collective
2018fee21e36SBarry Smith 
201936851e7fSLois Curfman McInnes    Level: advanced
202036851e7fSLois Curfman McInnes 
20219b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
20229b94acceSBarry Smith 
20239b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
20249b94acceSBarry Smith @*/
202563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegisterDestroy(void)
20269b94acceSBarry Smith {
2027dfbe8321SBarry Smith   PetscErrorCode ierr;
202882bf6240SBarry Smith 
20293a40ed3dSBarry Smith   PetscFunctionBegin;
20301441b1d3SBarry Smith   ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
20314c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
20323a40ed3dSBarry Smith   PetscFunctionReturn(0);
20339b94acceSBarry Smith }
20349b94acceSBarry Smith 
20354a2ae208SSatish Balay #undef __FUNCT__
20364a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
20379b94acceSBarry Smith /*@C
20389a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
20399b94acceSBarry Smith 
2040c7afd0dbSLois Curfman McInnes    Not Collective
2041c7afd0dbSLois Curfman McInnes 
20429b94acceSBarry Smith    Input Parameter:
20434b0e389bSBarry Smith .  snes - nonlinear solver context
20449b94acceSBarry Smith 
20459b94acceSBarry Smith    Output Parameter:
20463a7fca6bSBarry Smith .  type - SNES method (a character string)
20479b94acceSBarry Smith 
204836851e7fSLois Curfman McInnes    Level: intermediate
204936851e7fSLois Curfman McInnes 
2050454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
20519b94acceSBarry Smith @*/
205263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetType(SNES snes,SNESType *type)
20539b94acceSBarry Smith {
20543a40ed3dSBarry Smith   PetscFunctionBegin;
20554482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
20564482741eSBarry Smith   PetscValidPointer(type,2);
20577adad957SLisandro Dalcin   *type = ((PetscObject)snes)->type_name;
20583a40ed3dSBarry Smith   PetscFunctionReturn(0);
20599b94acceSBarry Smith }
20609b94acceSBarry Smith 
20614a2ae208SSatish Balay #undef __FUNCT__
20624a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
206352baeb72SSatish Balay /*@
20649b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
20659b94acceSBarry Smith    stored.
20669b94acceSBarry Smith 
2067c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2068c7afd0dbSLois Curfman McInnes 
20699b94acceSBarry Smith    Input Parameter:
20709b94acceSBarry Smith .  snes - the SNES context
20719b94acceSBarry Smith 
20729b94acceSBarry Smith    Output Parameter:
20739b94acceSBarry Smith .  x - the solution
20749b94acceSBarry Smith 
207570e92668SMatthew Knepley    Level: intermediate
207636851e7fSLois Curfman McInnes 
20779b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
20789b94acceSBarry Smith 
207985385478SLisandro Dalcin .seealso:  SNESGetSolutionUpdate(), SNESGetFunction()
20809b94acceSBarry Smith @*/
208163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolution(SNES snes,Vec *x)
20829b94acceSBarry Smith {
20833a40ed3dSBarry Smith   PetscFunctionBegin;
20844482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
20854482741eSBarry Smith   PetscValidPointer(x,2);
208685385478SLisandro Dalcin   *x = snes->vec_sol;
208770e92668SMatthew Knepley   PetscFunctionReturn(0);
208870e92668SMatthew Knepley }
208970e92668SMatthew Knepley 
209070e92668SMatthew Knepley #undef __FUNCT__
20914a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
209252baeb72SSatish Balay /*@
20939b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
20949b94acceSBarry Smith    stored.
20959b94acceSBarry Smith 
2096c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2097c7afd0dbSLois Curfman McInnes 
20989b94acceSBarry Smith    Input Parameter:
20999b94acceSBarry Smith .  snes - the SNES context
21009b94acceSBarry Smith 
21019b94acceSBarry Smith    Output Parameter:
21029b94acceSBarry Smith .  x - the solution update
21039b94acceSBarry Smith 
210436851e7fSLois Curfman McInnes    Level: advanced
210536851e7fSLois Curfman McInnes 
21069b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
21079b94acceSBarry Smith 
210885385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction()
21099b94acceSBarry Smith @*/
211063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolutionUpdate(SNES snes,Vec *x)
21119b94acceSBarry Smith {
21123a40ed3dSBarry Smith   PetscFunctionBegin;
21134482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
21144482741eSBarry Smith   PetscValidPointer(x,2);
211585385478SLisandro Dalcin   *x = snes->vec_sol_update;
21163a40ed3dSBarry Smith   PetscFunctionReturn(0);
21179b94acceSBarry Smith }
21189b94acceSBarry Smith 
21194a2ae208SSatish Balay #undef __FUNCT__
21204a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
21219b94acceSBarry Smith /*@C
21223638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
21239b94acceSBarry Smith 
2124c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2125c7afd0dbSLois Curfman McInnes 
21269b94acceSBarry Smith    Input Parameter:
21279b94acceSBarry Smith .  snes - the SNES context
21289b94acceSBarry Smith 
21299b94acceSBarry Smith    Output Parameter:
21307bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
213170e92668SMatthew Knepley .  func - the function (or PETSC_NULL)
213270e92668SMatthew Knepley -  ctx - the function context (or PETSC_NULL)
21339b94acceSBarry Smith 
213436851e7fSLois Curfman McInnes    Level: advanced
213536851e7fSLois Curfman McInnes 
2136a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
21379b94acceSBarry Smith 
21384b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution()
21399b94acceSBarry Smith @*/
214070e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx)
21419b94acceSBarry Smith {
21423a40ed3dSBarry Smith   PetscFunctionBegin;
21434482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
214485385478SLisandro Dalcin   if (r)    *r    = snes->vec_func;
2145e7788613SBarry Smith   if (func) *func = snes->ops->computefunction;
214670e92668SMatthew Knepley   if (ctx)  *ctx  = snes->funP;
21473a40ed3dSBarry Smith   PetscFunctionReturn(0);
21489b94acceSBarry Smith }
21499b94acceSBarry Smith 
21504a2ae208SSatish Balay #undef __FUNCT__
21514a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
21523c7409f5SSatish Balay /*@C
21533c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2154d850072dSLois Curfman McInnes    SNES options in the database.
21553c7409f5SSatish Balay 
2156fee21e36SBarry Smith    Collective on SNES
2157fee21e36SBarry Smith 
2158c7afd0dbSLois Curfman McInnes    Input Parameter:
2159c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2160c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2161c7afd0dbSLois Curfman McInnes 
2162d850072dSLois Curfman McInnes    Notes:
2163a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2164c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2165d850072dSLois Curfman McInnes 
216636851e7fSLois Curfman McInnes    Level: advanced
216736851e7fSLois Curfman McInnes 
21683c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2169a86d99e1SLois Curfman McInnes 
2170a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
21713c7409f5SSatish Balay @*/
217263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetOptionsPrefix(SNES snes,const char prefix[])
21733c7409f5SSatish Balay {
2174dfbe8321SBarry Smith   PetscErrorCode ierr;
21753c7409f5SSatish Balay 
21763a40ed3dSBarry Smith   PetscFunctionBegin;
21774482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2178639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
217994b7f48cSBarry Smith   ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
21803a40ed3dSBarry Smith   PetscFunctionReturn(0);
21813c7409f5SSatish Balay }
21823c7409f5SSatish Balay 
21834a2ae208SSatish Balay #undef __FUNCT__
21844a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
21853c7409f5SSatish Balay /*@C
2186f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2187d850072dSLois Curfman McInnes    SNES options in the database.
21883c7409f5SSatish Balay 
2189fee21e36SBarry Smith    Collective on SNES
2190fee21e36SBarry Smith 
2191c7afd0dbSLois Curfman McInnes    Input Parameters:
2192c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2193c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2194c7afd0dbSLois Curfman McInnes 
2195d850072dSLois Curfman McInnes    Notes:
2196a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2197c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2198d850072dSLois Curfman McInnes 
219936851e7fSLois Curfman McInnes    Level: advanced
220036851e7fSLois Curfman McInnes 
22013c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2202a86d99e1SLois Curfman McInnes 
2203a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
22043c7409f5SSatish Balay @*/
220563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAppendOptionsPrefix(SNES snes,const char prefix[])
22063c7409f5SSatish Balay {
2207dfbe8321SBarry Smith   PetscErrorCode ierr;
22083c7409f5SSatish Balay 
22093a40ed3dSBarry Smith   PetscFunctionBegin;
22104482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2211639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
221294b7f48cSBarry Smith   ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
22133a40ed3dSBarry Smith   PetscFunctionReturn(0);
22143c7409f5SSatish Balay }
22153c7409f5SSatish Balay 
22164a2ae208SSatish Balay #undef __FUNCT__
22174a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
22189ab63eb5SSatish Balay /*@C
22193c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
22203c7409f5SSatish Balay    SNES options in the database.
22213c7409f5SSatish Balay 
2222c7afd0dbSLois Curfman McInnes    Not Collective
2223c7afd0dbSLois Curfman McInnes 
22243c7409f5SSatish Balay    Input Parameter:
22253c7409f5SSatish Balay .  snes - the SNES context
22263c7409f5SSatish Balay 
22273c7409f5SSatish Balay    Output Parameter:
22283c7409f5SSatish Balay .  prefix - pointer to the prefix string used
22293c7409f5SSatish Balay 
22309ab63eb5SSatish Balay    Notes: On the fortran side, the user should pass in a string 'prifix' of
22319ab63eb5SSatish Balay    sufficient length to hold the prefix.
22329ab63eb5SSatish Balay 
223336851e7fSLois Curfman McInnes    Level: advanced
223436851e7fSLois Curfman McInnes 
22353c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2236a86d99e1SLois Curfman McInnes 
2237a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
22383c7409f5SSatish Balay @*/
2239e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetOptionsPrefix(SNES snes,const char *prefix[])
22403c7409f5SSatish Balay {
2241dfbe8321SBarry Smith   PetscErrorCode ierr;
22423c7409f5SSatish Balay 
22433a40ed3dSBarry Smith   PetscFunctionBegin;
22444482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2245639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
22463a40ed3dSBarry Smith   PetscFunctionReturn(0);
22473c7409f5SSatish Balay }
22483c7409f5SSatish Balay 
2249b2002411SLois Curfman McInnes 
22504a2ae208SSatish Balay #undef __FUNCT__
22514a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
22523cea93caSBarry Smith /*@C
22533cea93caSBarry Smith   SNESRegister - See SNESRegisterDynamic()
22543cea93caSBarry Smith 
22557f6c08e0SMatthew Knepley   Level: advanced
22563cea93caSBarry Smith @*/
225763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES))
2258b2002411SLois Curfman McInnes {
2259e2d1d2b7SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
2260dfbe8321SBarry Smith   PetscErrorCode ierr;
2261b2002411SLois Curfman McInnes 
2262b2002411SLois Curfman McInnes   PetscFunctionBegin;
2263b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2264c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
2265b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2266b2002411SLois Curfman McInnes }
2267da9b6338SBarry Smith 
2268da9b6338SBarry Smith #undef __FUNCT__
2269da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin"
227063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESTestLocalMin(SNES snes)
2271da9b6338SBarry Smith {
2272dfbe8321SBarry Smith   PetscErrorCode ierr;
227377431f27SBarry Smith   PetscInt       N,i,j;
2274da9b6338SBarry Smith   Vec            u,uh,fh;
2275da9b6338SBarry Smith   PetscScalar    value;
2276da9b6338SBarry Smith   PetscReal      norm;
2277da9b6338SBarry Smith 
2278da9b6338SBarry Smith   PetscFunctionBegin;
2279da9b6338SBarry Smith   ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr);
2280da9b6338SBarry Smith   ierr = VecDuplicate(u,&uh);CHKERRQ(ierr);
2281da9b6338SBarry Smith   ierr = VecDuplicate(u,&fh);CHKERRQ(ierr);
2282da9b6338SBarry Smith 
2283da9b6338SBarry Smith   /* currently only works for sequential */
2284da9b6338SBarry Smith   ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n");
2285da9b6338SBarry Smith   ierr = VecGetSize(u,&N);CHKERRQ(ierr);
2286da9b6338SBarry Smith   for (i=0; i<N; i++) {
2287da9b6338SBarry Smith     ierr = VecCopy(u,uh);CHKERRQ(ierr);
228877431f27SBarry Smith     ierr  = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr);
2289da9b6338SBarry Smith     for (j=-10; j<11; j++) {
2290ccae9161SBarry Smith       value = PetscSign(j)*exp(PetscAbs(j)-10.0);
2291da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
22923ab0aad5SBarry Smith       ierr  = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr);
2293da9b6338SBarry Smith       ierr  = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr);
229477431f27SBarry Smith       ierr  = PetscPrintf(PETSC_COMM_WORLD,"       j norm %D %18.16e\n",j,norm);CHKERRQ(ierr);
2295da9b6338SBarry Smith       value = -value;
2296da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
2297da9b6338SBarry Smith     }
2298da9b6338SBarry Smith   }
2299da9b6338SBarry Smith   ierr = VecDestroy(uh);CHKERRQ(ierr);
2300da9b6338SBarry Smith   ierr = VecDestroy(fh);CHKERRQ(ierr);
2301da9b6338SBarry Smith   PetscFunctionReturn(0);
2302da9b6338SBarry Smith }
230371f87433Sdalcinl 
230471f87433Sdalcinl #undef __FUNCT__
2305fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW"
230671f87433Sdalcinl /*@
2307fa9f3622SBarry Smith    SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for
230871f87433Sdalcinl    computing relative tolerance for linear solvers within an inexact
230971f87433Sdalcinl    Newton method.
231071f87433Sdalcinl 
231171f87433Sdalcinl    Collective on SNES
231271f87433Sdalcinl 
231371f87433Sdalcinl    Input Parameters:
231471f87433Sdalcinl +  snes - SNES context
231571f87433Sdalcinl -  flag - PETSC_TRUE or PETSC_FALSE
231671f87433Sdalcinl 
231771f87433Sdalcinl    Notes:
231871f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
231971f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
232071f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
232171f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
232271f87433Sdalcinl    solver.
232371f87433Sdalcinl 
232471f87433Sdalcinl    Level: advanced
232571f87433Sdalcinl 
232671f87433Sdalcinl    Reference:
232771f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
232871f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
232971f87433Sdalcinl 
233071f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
233171f87433Sdalcinl 
2332fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
233371f87433Sdalcinl @*/
2334fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetUseEW(SNES snes,PetscTruth flag)
233571f87433Sdalcinl {
233671f87433Sdalcinl   PetscFunctionBegin;
233771f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
233871f87433Sdalcinl   snes->ksp_ewconv = flag;
233971f87433Sdalcinl   PetscFunctionReturn(0);
234071f87433Sdalcinl }
234171f87433Sdalcinl 
234271f87433Sdalcinl #undef __FUNCT__
2343fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW"
234471f87433Sdalcinl /*@
2345fa9f3622SBarry Smith    SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method
234671f87433Sdalcinl    for computing relative tolerance for linear solvers within an
234771f87433Sdalcinl    inexact Newton method.
234871f87433Sdalcinl 
234971f87433Sdalcinl    Not Collective
235071f87433Sdalcinl 
235171f87433Sdalcinl    Input Parameter:
235271f87433Sdalcinl .  snes - SNES context
235371f87433Sdalcinl 
235471f87433Sdalcinl    Output Parameter:
235571f87433Sdalcinl .  flag - PETSC_TRUE or PETSC_FALSE
235671f87433Sdalcinl 
235771f87433Sdalcinl    Notes:
235871f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
235971f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
236071f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
236171f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
236271f87433Sdalcinl    solver.
236371f87433Sdalcinl 
236471f87433Sdalcinl    Level: advanced
236571f87433Sdalcinl 
236671f87433Sdalcinl    Reference:
236771f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
236871f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
236971f87433Sdalcinl 
237071f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
237171f87433Sdalcinl 
2372fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
237371f87433Sdalcinl @*/
2374fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetUseEW(SNES snes, PetscTruth *flag)
237571f87433Sdalcinl {
237671f87433Sdalcinl   PetscFunctionBegin;
237771f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
237871f87433Sdalcinl   PetscValidPointer(flag,2);
237971f87433Sdalcinl   *flag = snes->ksp_ewconv;
238071f87433Sdalcinl   PetscFunctionReturn(0);
238171f87433Sdalcinl }
238271f87433Sdalcinl 
238371f87433Sdalcinl #undef __FUNCT__
2384fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW"
238571f87433Sdalcinl /*@
2386fa9f3622SBarry Smith    SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker
238771f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
238871f87433Sdalcinl    Newton method.
238971f87433Sdalcinl 
239071f87433Sdalcinl    Collective on SNES
239171f87433Sdalcinl 
239271f87433Sdalcinl    Input Parameters:
239371f87433Sdalcinl +    snes - SNES context
239471f87433Sdalcinl .    version - version 1, 2 (default is 2) or 3
239571f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
239671f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
239771f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
239871f87433Sdalcinl              (0 <= gamma2 <= 1)
239971f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
240071f87433Sdalcinl .    alpha2 - power for safeguard
240171f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
240271f87433Sdalcinl 
240371f87433Sdalcinl    Note:
240471f87433Sdalcinl    Version 3 was contributed by Luis Chacon, June 2006.
240571f87433Sdalcinl 
240671f87433Sdalcinl    Use PETSC_DEFAULT to retain the default for any of the parameters.
240771f87433Sdalcinl 
240871f87433Sdalcinl    Level: advanced
240971f87433Sdalcinl 
241071f87433Sdalcinl    Reference:
241171f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
241271f87433Sdalcinl    inexact Newton method", Utah State University Math. Stat. Dept. Res.
241371f87433Sdalcinl    Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput.
241471f87433Sdalcinl 
241571f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters
241671f87433Sdalcinl 
2417fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW()
241871f87433Sdalcinl @*/
2419fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max,
242071f87433Sdalcinl 							    PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold)
242171f87433Sdalcinl {
2422fa9f3622SBarry Smith   SNESKSPEW *kctx;
242371f87433Sdalcinl   PetscFunctionBegin;
242471f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2425fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
242671f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
242771f87433Sdalcinl 
242871f87433Sdalcinl   if (version != PETSC_DEFAULT)   kctx->version   = version;
242971f87433Sdalcinl   if (rtol_0 != PETSC_DEFAULT)    kctx->rtol_0    = rtol_0;
243071f87433Sdalcinl   if (rtol_max != PETSC_DEFAULT)  kctx->rtol_max  = rtol_max;
243171f87433Sdalcinl   if (gamma != PETSC_DEFAULT)     kctx->gamma     = gamma;
243271f87433Sdalcinl   if (alpha != PETSC_DEFAULT)     kctx->alpha     = alpha;
243371f87433Sdalcinl   if (alpha2 != PETSC_DEFAULT)    kctx->alpha2    = alpha2;
243471f87433Sdalcinl   if (threshold != PETSC_DEFAULT) kctx->threshold = threshold;
243571f87433Sdalcinl 
243671f87433Sdalcinl   if (kctx->version < 1 || kctx->version > 3) {
243771f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version);
243871f87433Sdalcinl   }
243971f87433Sdalcinl   if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) {
244071f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0);
244171f87433Sdalcinl   }
244271f87433Sdalcinl   if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) {
244371f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max);
244471f87433Sdalcinl   }
244571f87433Sdalcinl   if (kctx->gamma < 0.0 || kctx->gamma > 1.0) {
244671f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma);
244771f87433Sdalcinl   }
244871f87433Sdalcinl   if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) {
244971f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha);
245071f87433Sdalcinl   }
245171f87433Sdalcinl   if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) {
245271f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold);
245371f87433Sdalcinl   }
245471f87433Sdalcinl   PetscFunctionReturn(0);
245571f87433Sdalcinl }
245671f87433Sdalcinl 
245771f87433Sdalcinl #undef __FUNCT__
2458fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW"
245971f87433Sdalcinl /*@
2460fa9f3622SBarry Smith    SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker
246171f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
246271f87433Sdalcinl    Newton method.
246371f87433Sdalcinl 
246471f87433Sdalcinl    Not Collective
246571f87433Sdalcinl 
246671f87433Sdalcinl    Input Parameters:
246771f87433Sdalcinl      snes - SNES context
246871f87433Sdalcinl 
246971f87433Sdalcinl    Output Parameters:
247071f87433Sdalcinl +    version - version 1, 2 (default is 2) or 3
247171f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
247271f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
247371f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
247471f87433Sdalcinl              (0 <= gamma2 <= 1)
247571f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
247671f87433Sdalcinl .    alpha2 - power for safeguard
247771f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
247871f87433Sdalcinl 
247971f87433Sdalcinl    Level: advanced
248071f87433Sdalcinl 
248171f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters
248271f87433Sdalcinl 
2483fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW()
248471f87433Sdalcinl @*/
2485fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max,
248671f87433Sdalcinl 							    PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold)
248771f87433Sdalcinl {
2488fa9f3622SBarry Smith   SNESKSPEW *kctx;
248971f87433Sdalcinl   PetscFunctionBegin;
249071f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2491fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
249271f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
249371f87433Sdalcinl   if(version)   *version   = kctx->version;
249471f87433Sdalcinl   if(rtol_0)    *rtol_0    = kctx->rtol_0;
249571f87433Sdalcinl   if(rtol_max)  *rtol_max  = kctx->rtol_max;
249671f87433Sdalcinl   if(gamma)     *gamma     = kctx->gamma;
249771f87433Sdalcinl   if(alpha)     *alpha     = kctx->alpha;
249871f87433Sdalcinl   if(alpha2)    *alpha2    = kctx->alpha2;
249971f87433Sdalcinl   if(threshold) *threshold = kctx->threshold;
250071f87433Sdalcinl   PetscFunctionReturn(0);
250171f87433Sdalcinl }
250271f87433Sdalcinl 
250371f87433Sdalcinl #undef __FUNCT__
2504fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve"
2505fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x)
250671f87433Sdalcinl {
250771f87433Sdalcinl   PetscErrorCode ierr;
2508fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
250971f87433Sdalcinl   PetscReal      rtol=PETSC_DEFAULT,stol;
251071f87433Sdalcinl 
251171f87433Sdalcinl   PetscFunctionBegin;
251271f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
251371f87433Sdalcinl   if (!snes->iter) { /* first time in, so use the original user rtol */
251471f87433Sdalcinl     rtol = kctx->rtol_0;
251571f87433Sdalcinl   } else {
251671f87433Sdalcinl     if (kctx->version == 1) {
251771f87433Sdalcinl       rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last;
251871f87433Sdalcinl       if (rtol < 0.0) rtol = -rtol;
251971f87433Sdalcinl       stol = pow(kctx->rtol_last,kctx->alpha2);
252071f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
252171f87433Sdalcinl     } else if (kctx->version == 2) {
252271f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
252371f87433Sdalcinl       stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha);
252471f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
252571f87433Sdalcinl     } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */
252671f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
252771f87433Sdalcinl       /* safeguard: avoid sharp decrease of rtol */
252871f87433Sdalcinl       stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha);
252971f87433Sdalcinl       stol = PetscMax(rtol,stol);
253071f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
253171f87433Sdalcinl       /* safeguard: avoid oversolving */
253271f87433Sdalcinl       stol = kctx->gamma*(snes->ttol)/snes->norm;
253371f87433Sdalcinl       stol = PetscMax(rtol,stol);
253471f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
253571f87433Sdalcinl     } else SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version);
253671f87433Sdalcinl   }
253771f87433Sdalcinl   /* safeguard: avoid rtol greater than one */
253871f87433Sdalcinl   rtol = PetscMin(rtol,kctx->rtol_max);
253971f87433Sdalcinl   ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
254071f87433Sdalcinl   ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr);
254171f87433Sdalcinl   PetscFunctionReturn(0);
254271f87433Sdalcinl }
254371f87433Sdalcinl 
254471f87433Sdalcinl #undef __FUNCT__
2545fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve"
2546fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x)
254771f87433Sdalcinl {
254871f87433Sdalcinl   PetscErrorCode ierr;
2549fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
255071f87433Sdalcinl   PCSide         pcside;
255171f87433Sdalcinl   Vec            lres;
255271f87433Sdalcinl 
255371f87433Sdalcinl   PetscFunctionBegin;
255471f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
255571f87433Sdalcinl   ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr);
255671f87433Sdalcinl   ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr);
255771f87433Sdalcinl   if (kctx->version == 1) {
255871f87433Sdalcinl     ierr = KSPGetPreconditionerSide(ksp,&pcside);CHKERRQ(ierr);
255971f87433Sdalcinl     if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */
256071f87433Sdalcinl       /* KSP residual is true linear residual */
256171f87433Sdalcinl       ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr);
256271f87433Sdalcinl     } else {
256371f87433Sdalcinl       /* KSP residual is preconditioned residual */
256471f87433Sdalcinl       /* compute true linear residual norm */
256571f87433Sdalcinl       ierr = VecDuplicate(b,&lres);CHKERRQ(ierr);
256671f87433Sdalcinl       ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr);
256771f87433Sdalcinl       ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr);
256871f87433Sdalcinl       ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr);
256971f87433Sdalcinl       ierr = VecDestroy(lres);CHKERRQ(ierr);
257071f87433Sdalcinl     }
257171f87433Sdalcinl   }
257271f87433Sdalcinl   PetscFunctionReturn(0);
257371f87433Sdalcinl }
257471f87433Sdalcinl 
257571f87433Sdalcinl #undef __FUNCT__
257671f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve"
257771f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x)
257871f87433Sdalcinl {
257971f87433Sdalcinl   PetscErrorCode ierr;
258071f87433Sdalcinl 
258171f87433Sdalcinl   PetscFunctionBegin;
2582fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr);  }
258371f87433Sdalcinl   ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);
2584fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); }
258571f87433Sdalcinl   PetscFunctionReturn(0);
258671f87433Sdalcinl }
2587