xref: /petsc/src/snes/interface/snes.c (revision 7c4f633dc6bb6149cca88d301ead35a99e103cbb)
163dd3a1aSKris Buschelman #define PETSCSNES_DLL
29b94acceSBarry Smith 
3*7c4f633dSBarry Smith #include "private/snesimpl.h"      /*I "petscsnes.h"  I*/
49b94acceSBarry Smith 
54c49b128SBarry Smith PetscTruth SNESRegisterAllCalled = PETSC_FALSE;
68ba1e511SMatthew Knepley PetscFList SNESList              = PETSC_NULL;
78ba1e511SMatthew Knepley 
88ba1e511SMatthew Knepley /* Logging support */
9166c7f25SBarry Smith PetscCookie PETSCSNES_DLLEXPORT SNES_COOKIE;
10166c7f25SBarry Smith PetscLogEvent  SNES_Solve, SNES_LineSearch, SNES_FunctionEval, SNES_JacobianEval;
11a09944afSBarry Smith 
12a09944afSBarry Smith #undef __FUNCT__
134936397dSBarry Smith #define __FUNCT__ "SNESSetFunctionDomainError"
14e725d27bSBarry Smith /*@
154936397dSBarry Smith    SNESSetFunctionDomainError - tells SNES that the input vector to your FormFunction is not
164936397dSBarry Smith      in the functions domain. For example, negative pressure.
174936397dSBarry Smith 
184936397dSBarry Smith    Collective on SNES
194936397dSBarry Smith 
204936397dSBarry Smith    Input Parameters:
214936397dSBarry Smith .  SNES - the SNES context
224936397dSBarry Smith 
2328529972SSatish Balay    Level: advanced
244936397dSBarry Smith 
254936397dSBarry Smith .keywords: SNES, view
264936397dSBarry Smith 
274936397dSBarry Smith .seealso: SNESCreate(), SNESSetFunction()
284936397dSBarry Smith @*/
294936397dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFunctionDomainError(SNES snes)
304936397dSBarry Smith {
314936397dSBarry Smith   PetscFunctionBegin;
324936397dSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
334936397dSBarry Smith   snes->domainerror = PETSC_TRUE;
344936397dSBarry Smith   PetscFunctionReturn(0);
354936397dSBarry Smith }
364936397dSBarry Smith 
374936397dSBarry Smith #undef __FUNCT__
384a2ae208SSatish Balay #define __FUNCT__ "SNESView"
397e2c5f70SBarry Smith /*@C
409b94acceSBarry Smith    SNESView - Prints the SNES data structure.
419b94acceSBarry Smith 
424c49b128SBarry Smith    Collective on SNES
43fee21e36SBarry Smith 
44c7afd0dbSLois Curfman McInnes    Input Parameters:
45c7afd0dbSLois Curfman McInnes +  SNES - the SNES context
46c7afd0dbSLois Curfman McInnes -  viewer - visualization context
47c7afd0dbSLois Curfman McInnes 
489b94acceSBarry Smith    Options Database Key:
49c8a8ba5cSLois Curfman McInnes .  -snes_view - Calls SNESView() at end of SNESSolve()
509b94acceSBarry Smith 
519b94acceSBarry Smith    Notes:
529b94acceSBarry Smith    The available visualization contexts include
53b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
54b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
55c8a8ba5cSLois Curfman McInnes          output where only the first processor opens
56c8a8ba5cSLois Curfman McInnes          the file.  All other processors send their
57c8a8ba5cSLois Curfman McInnes          data to the first processor to print.
589b94acceSBarry Smith 
593e081fefSLois Curfman McInnes    The user can open an alternative visualization context with
60b0a32e0cSBarry Smith    PetscViewerASCIIOpen() - output to a specified file.
619b94acceSBarry Smith 
6236851e7fSLois Curfman McInnes    Level: beginner
6336851e7fSLois Curfman McInnes 
649b94acceSBarry Smith .keywords: SNES, view
659b94acceSBarry Smith 
66b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
679b94acceSBarry Smith @*/
6863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESView(SNES snes,PetscViewer viewer)
699b94acceSBarry Smith {
70fa9f3622SBarry Smith   SNESKSPEW           *kctx;
71dfbe8321SBarry Smith   PetscErrorCode      ierr;
7294b7f48cSBarry Smith   KSP                 ksp;
73a313700dSBarry Smith   const SNESType      type;
7432077d6dSBarry Smith   PetscTruth          iascii,isstring;
759b94acceSBarry Smith 
763a40ed3dSBarry Smith   PetscFunctionBegin;
774482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
783050cee2SBarry Smith   if (!viewer) {
797adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&viewer);CHKERRQ(ierr);
803050cee2SBarry Smith   }
814482741eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE,2);
82c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,viewer,2);
8374679c65SBarry Smith 
8432077d6dSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
85b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);CHKERRQ(ierr);
8632077d6dSBarry Smith   if (iascii) {
877adad957SLisandro Dalcin     if (((PetscObject)snes)->prefix) {
887adad957SLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:(%s)\n",((PetscObject)snes)->prefix);CHKERRQ(ierr);
893a7fca6bSBarry Smith     } else {
90b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:\n");CHKERRQ(ierr);
913a7fca6bSBarry Smith     }
92454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
93454a90a3SBarry Smith     if (type) {
94b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: %s\n",type);CHKERRQ(ierr);
95184914b5SBarry Smith     } else {
96b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: not set yet\n");CHKERRQ(ierr);
97184914b5SBarry Smith     }
98e7788613SBarry Smith     if (snes->ops->view) {
99b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
100e7788613SBarry Smith       ierr = (*snes->ops->view)(snes,viewer);CHKERRQ(ierr);
101b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1020ef38995SBarry Smith     }
10377431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum iterations=%D, maximum function evaluations=%D\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr);
104a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  tolerances: relative=%G, absolute=%G, solution=%G\n",
10570441072SBarry Smith                  snes->rtol,snes->abstol,snes->xtol);CHKERRQ(ierr);
10677431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",snes->linear_its);CHKERRQ(ierr);
10777431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of function evaluations=%D\n",snes->nfuncs);CHKERRQ(ierr);
1089b94acceSBarry Smith     if (snes->ksp_ewconv) {
109fa9f3622SBarry Smith       kctx = (SNESKSPEW *)snes->kspconvctx;
1109b94acceSBarry Smith       if (kctx) {
11177431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"  Eisenstat-Walker computation of KSP relative tolerance (version %D)\n",kctx->version);CHKERRQ(ierr);
112a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    rtol_0=%G, rtol_max=%G, threshold=%G\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr);
113a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    gamma=%G, alpha=%G, alpha2=%G\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr);
1149b94acceSBarry Smith       }
1159b94acceSBarry Smith     }
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
178ddf469c8SBarry Smith .  -snes_max_linear_solve_fail - number of linear solver failures before SNESSolve() stops
179a8054027SBarry Smith .  -snes_lag_preconditioner <lag> - how often preconditioner is rebuilt (use -1 to never rebuild)
180e35cf81dSBarry Smith .  -snes_lag_jacobian <lag> - how often Jacobian is rebuilt (use -1 to never rebuild)
181b39c3a46SLois Curfman McInnes .  -snes_trtol <trtol> - trust region tolerance
1822492ecdbSBarry Smith .  -snes_no_convergence_test - skip convergence test in nonlinear
18382738288SBarry Smith                                solver; hence iterations will continue until max_it
1841fbbfb26SLois Curfman McInnes                                or some other criterion is reached. Saves expense
18582738288SBarry Smith                                of convergence test
186e8105e01SRichard Katz .  -snes_monitor <optional filename> - prints residual norm at each iteration. if no
187e8105e01SRichard Katz                                        filename given prints to stdout
188a6570f20SBarry Smith .  -snes_monitor_solution - plots solution at each iteration
189a6570f20SBarry Smith .  -snes_monitor_residual - plots residual (not its norm) at each iteration
190a6570f20SBarry Smith .  -snes_monitor_solution_update - plots update to solution at each iteration
191a6570f20SBarry Smith .  -snes_monitor_draw - plots residual norm at each iteration
192e24b481bSBarry Smith .  -snes_fd - use finite differences to compute Jacobian; very slow, only for testing
1935968eb51SBarry Smith .  -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration
194fee2055bSBarry Smith -  -snes_converged_reason - print the reason for convergence/divergence after each solve
19582738288SBarry Smith 
19682738288SBarry Smith     Options Database for Eisenstat-Walker method:
197fa9f3622SBarry Smith +  -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence
1984b27c08aSLois Curfman McInnes .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
19936851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
20036851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
20136851e7fSLois Curfman McInnes .  -snes_ksp_ew_gamma <gamma> - Sets gamma
20236851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha <alpha> - Sets alpha
20336851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
20436851e7fSLois Curfman McInnes -  -snes_ksp_ew_threshold <threshold> - Sets threshold
20582738288SBarry Smith 
20611ca99fdSLois Curfman McInnes    Notes:
20711ca99fdSLois Curfman McInnes    To see all options, run your program with the -help option or consult
20811ca99fdSLois Curfman McInnes    the users manual.
20983e2fdc7SBarry Smith 
21036851e7fSLois Curfman McInnes    Level: beginner
21136851e7fSLois Curfman McInnes 
2129b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database
2139b94acceSBarry Smith 
21469ed319cSSatish Balay .seealso: SNESSetOptionsPrefix()
2159b94acceSBarry Smith @*/
21663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFromOptions(SNES snes)
2179b94acceSBarry Smith {
218f1af5d2fSBarry Smith   PetscTruth              flg;
219a8054027SBarry Smith   PetscInt                i,indx,lag;
22085385478SLisandro Dalcin   const char              *deft = SNESLS;
22185385478SLisandro Dalcin   const char              *convtests[] = {"default","skip"};
22285385478SLisandro Dalcin   SNESKSPEW               *kctx = NULL;
223e8105e01SRichard Katz   char                    type[256], monfilename[PETSC_MAX_PATH_LEN];
22423d894e5SBarry Smith   PetscViewerASCIIMonitor monviewer;
22585385478SLisandro Dalcin   PetscErrorCode          ierr;
2269b94acceSBarry Smith 
2273a40ed3dSBarry Smith   PetscFunctionBegin;
2284482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
229ca161407SBarry Smith 
2307adad957SLisandro Dalcin   ierr = PetscOptionsBegin(((PetscObject)snes)->comm,((PetscObject)snes)->prefix,"Nonlinear solver (SNES) options","SNES");CHKERRQ(ierr);
231186905e3SBarry Smith     if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
2327adad957SLisandro Dalcin     if (((PetscObject)snes)->type_name) { deft = ((PetscObject)snes)->type_name; }
233b0a32e0cSBarry Smith     ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr);
234d64ed03dSBarry Smith     if (flg) {
235186905e3SBarry Smith       ierr = SNESSetType(snes,type);CHKERRQ(ierr);
2367adad957SLisandro Dalcin     } else if (!((PetscObject)snes)->type_name) {
237186905e3SBarry Smith       ierr = SNESSetType(snes,deft);CHKERRQ(ierr);
238d64ed03dSBarry Smith     }
239909c8a9fSBarry Smith     ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr);
24093c39befSBarry Smith 
24187828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_stol","Stop if step length less then","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr);
24270441072SBarry Smith     ierr = PetscOptionsReal("-snes_atol","Stop if function norm less then","SNESSetTolerances",snes->abstol,&snes->abstol,0);CHKERRQ(ierr);
243186905e3SBarry Smith 
24487828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less then","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr);
245b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr);
246b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr);
24750ffb88aSMatthew Knepley     ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr);
248ddf469c8SBarry Smith     ierr = PetscOptionsInt("-snes_max_linear_solve_fail","Maximum failures in linear solves allowed","SNESSetMaxLinearSolveFailures",snes->maxLinearSolveFailures,&snes->maxLinearSolveFailures,PETSC_NULL);CHKERRQ(ierr);
24985385478SLisandro Dalcin 
250a8054027SBarry Smith     ierr = PetscOptionsInt("-snes_lag_preconditioner","How often to rebuild preconditioner","SNESSetLagPreconditioner",snes->lagpreconditioner,&lag,&flg);CHKERRQ(ierr);
251a8054027SBarry Smith     if (flg) {
252a8054027SBarry Smith       ierr = SNESSetLagPreconditioner(snes,lag);CHKERRQ(ierr);
253a8054027SBarry Smith     }
254e35cf81dSBarry Smith     ierr = PetscOptionsInt("-snes_lag_jacobian","How often to rebuild Jacobian","SNESSetLagJacobian",snes->lagjacobian,&lag,&flg);CHKERRQ(ierr);
255e35cf81dSBarry Smith     if (flg) {
256e35cf81dSBarry Smith       ierr = SNESSetLagJacobian(snes,lag);CHKERRQ(ierr);
257e35cf81dSBarry Smith     }
258a8054027SBarry Smith 
25985385478SLisandro Dalcin     ierr = PetscOptionsEList("-snes_convergence_test","Convergence test","SNESSetConvergenceTest",convtests,2,"default",&indx,&flg);CHKERRQ(ierr);
26085385478SLisandro Dalcin     if (flg) {
26185385478SLisandro Dalcin       switch (indx) {
2627f7931b9SBarry Smith       case 0: ierr = SNESSetConvergenceTest(snes,SNESDefaultConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break;
2637f7931b9SBarry Smith       case 1: ierr = SNESSetConvergenceTest(snes,SNESSkipConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);    break;
26485385478SLisandro Dalcin       }
26585385478SLisandro Dalcin     }
26685385478SLisandro Dalcin 
2675968eb51SBarry Smith     ierr = PetscOptionsName("-snes_converged_reason","Print reason for converged or diverged","SNESSolve",&flg);CHKERRQ(ierr);
2685968eb51SBarry Smith     if (flg) {
2695968eb51SBarry Smith       snes->printreason = PETSC_TRUE;
2705968eb51SBarry Smith     }
271186905e3SBarry Smith 
27285385478SLisandro Dalcin     kctx = (SNESKSPEW *)snes->kspconvctx;
27385385478SLisandro Dalcin 
274fa9f3622SBarry Smith     ierr = PetscOptionsTruth("-snes_ksp_ew","Use Eisentat-Walker linear system convergence test","SNESKSPSetUseEW",snes->ksp_ewconv,&snes->ksp_ewconv,PETSC_NULL);CHKERRQ(ierr);
275186905e3SBarry Smith 
276fa9f3622SBarry Smith     ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNESKSPSetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr);
277fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNESKSPSetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr);
278fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNESKSPSetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr);
279fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNESKSPSetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr);
280fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNESKSPSetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr);
281fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNESKSPSetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr);
282fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNESKSPSetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr);
283186905e3SBarry Smith 
284a6570f20SBarry Smith     ierr = PetscOptionsName("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",&flg);CHKERRQ(ierr);
285a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);}
286eabae89aSBarry Smith 
287a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor","Monitor norm of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
288e8105e01SRichard Katz     if (flg) {
289050a712dSBarry Smith       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr);
29023d894e5SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefault,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
291e8105e01SRichard Katz     }
292eabae89aSBarry Smith 
293b271bb04SBarry Smith     ierr = PetscOptionsString("-snes_monitor_range","Monitor range of elements of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
294b271bb04SBarry Smith     if (flg) {
295b271bb04SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorRange,0,0);CHKERRQ(ierr);
296b271bb04SBarry Smith     }
297b271bb04SBarry Smith 
298a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_ratiomonitor","Monitor ratios of norms of function","SNESMonitorSetRatio","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
299eabae89aSBarry Smith     if (flg) {
300050a712dSBarry Smith       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr);
301f1bef1bcSMatthew Knepley       ierr = SNESMonitorSetRatio(snes,monviewer);CHKERRQ(ierr);
302e8105e01SRichard Katz     }
303eabae89aSBarry Smith 
304a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor_short","Monitor norm of function (fewer digits)","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
305eabae89aSBarry Smith     if (flg) {
306050a712dSBarry Smith       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr);
30723d894e5SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefaultShort,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
308eabae89aSBarry Smith     }
309eabae89aSBarry Smith 
310a6570f20SBarry Smith     ierr = PetscOptionsName("-snes_monitor_solution","Plot solution at each iteration","SNESMonitorSolution",&flg);CHKERRQ(ierr);
311a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolution,0,0);CHKERRQ(ierr);}
312a6570f20SBarry Smith     ierr = PetscOptionsName("-snes_monitor_solution_update","Plot correction at each iteration","SNESMonitorSolutionUpdate",&flg);CHKERRQ(ierr);
313a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolutionUpdate,0,0);CHKERRQ(ierr);}
314a6570f20SBarry Smith     ierr = PetscOptionsName("-snes_monitor_residual","Plot residual at each iteration","SNESMonitorResidual",&flg);CHKERRQ(ierr);
315a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorResidual,0,0);CHKERRQ(ierr);}
316a6570f20SBarry Smith     ierr = PetscOptionsName("-snes_monitor_draw","Plot function norm at each iteration","SNESMonitorLG",&flg);CHKERRQ(ierr);
317a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
318b271bb04SBarry Smith     ierr = PetscOptionsName("-snes_monitor_range_draw","Plot function range at each iteration","SNESMonitorLG",&flg);CHKERRQ(ierr);
319b271bb04SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLGRange,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
320e24b481bSBarry Smith 
321b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",&flg);CHKERRQ(ierr);
3224b27c08aSLois Curfman McInnes     if (flg) {
323186905e3SBarry Smith       ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr);
324ae15b995SBarry Smith       ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr);
3259b94acceSBarry Smith     }
326639f9d9dSBarry Smith 
32776b2cf59SMatthew Knepley     for(i = 0; i < numberofsetfromoptions; i++) {
32876b2cf59SMatthew Knepley       ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr);
32976b2cf59SMatthew Knepley     }
33076b2cf59SMatthew Knepley 
331e7788613SBarry Smith     if (snes->ops->setfromoptions) {
332e7788613SBarry Smith       ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr);
333639f9d9dSBarry Smith     }
334b0a32e0cSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
3354bbc92c1SBarry Smith 
3361cee3971SBarry Smith 
3371cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
33885385478SLisandro Dalcin   ierr = KSPSetFromOptions(snes->ksp);CHKERRQ(ierr);
33993993e2dSLois Curfman McInnes 
3403a40ed3dSBarry Smith   PetscFunctionReturn(0);
3419b94acceSBarry Smith }
3429b94acceSBarry Smith 
343a847f771SSatish Balay 
3444a2ae208SSatish Balay #undef __FUNCT__
3454a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext"
3469b94acceSBarry Smith /*@
3479b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
3489b94acceSBarry Smith    the nonlinear solvers.
3499b94acceSBarry Smith 
350fee21e36SBarry Smith    Collective on SNES
351fee21e36SBarry Smith 
352c7afd0dbSLois Curfman McInnes    Input Parameters:
353c7afd0dbSLois Curfman McInnes +  snes - the SNES context
354c7afd0dbSLois Curfman McInnes -  usrP - optional user context
355c7afd0dbSLois Curfman McInnes 
35636851e7fSLois Curfman McInnes    Level: intermediate
35736851e7fSLois Curfman McInnes 
3589b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
3599b94acceSBarry Smith 
3609b94acceSBarry Smith .seealso: SNESGetApplicationContext()
3619b94acceSBarry Smith @*/
36263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetApplicationContext(SNES snes,void *usrP)
3639b94acceSBarry Smith {
3643a40ed3dSBarry Smith   PetscFunctionBegin;
3654482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
3669b94acceSBarry Smith   snes->user		= usrP;
3673a40ed3dSBarry Smith   PetscFunctionReturn(0);
3689b94acceSBarry Smith }
36974679c65SBarry Smith 
3704a2ae208SSatish Balay #undef __FUNCT__
3714a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext"
3729b94acceSBarry Smith /*@C
3739b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
3749b94acceSBarry Smith    nonlinear solvers.
3759b94acceSBarry Smith 
376c7afd0dbSLois Curfman McInnes    Not Collective
377c7afd0dbSLois Curfman McInnes 
3789b94acceSBarry Smith    Input Parameter:
3799b94acceSBarry Smith .  snes - SNES context
3809b94acceSBarry Smith 
3819b94acceSBarry Smith    Output Parameter:
3829b94acceSBarry Smith .  usrP - user context
3839b94acceSBarry Smith 
38436851e7fSLois Curfman McInnes    Level: intermediate
38536851e7fSLois Curfman McInnes 
3869b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
3879b94acceSBarry Smith 
3889b94acceSBarry Smith .seealso: SNESSetApplicationContext()
3899b94acceSBarry Smith @*/
39063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetApplicationContext(SNES snes,void **usrP)
3919b94acceSBarry Smith {
3923a40ed3dSBarry Smith   PetscFunctionBegin;
3934482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
3949b94acceSBarry Smith   *usrP = snes->user;
3953a40ed3dSBarry Smith   PetscFunctionReturn(0);
3969b94acceSBarry Smith }
39774679c65SBarry Smith 
3984a2ae208SSatish Balay #undef __FUNCT__
3994a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber"
4009b94acceSBarry Smith /*@
401c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
402c8228a4eSBarry Smith    at this time.
4039b94acceSBarry Smith 
404c7afd0dbSLois Curfman McInnes    Not Collective
405c7afd0dbSLois Curfman McInnes 
4069b94acceSBarry Smith    Input Parameter:
4079b94acceSBarry Smith .  snes - SNES context
4089b94acceSBarry Smith 
4099b94acceSBarry Smith    Output Parameter:
4109b94acceSBarry Smith .  iter - iteration number
4119b94acceSBarry Smith 
412c8228a4eSBarry Smith    Notes:
413c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
414c8228a4eSBarry Smith 
415c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
41608405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
41708405cd6SLois Curfman McInnes .vb
41808405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
41908405cd6SLois Curfman McInnes       if (!(it % 2)) {
42008405cd6SLois Curfman McInnes         [compute Jacobian here]
42108405cd6SLois Curfman McInnes       }
42208405cd6SLois Curfman McInnes .ve
423c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
42408405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
425c8228a4eSBarry Smith 
42636851e7fSLois Curfman McInnes    Level: intermediate
42736851e7fSLois Curfman McInnes 
4282b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number,
4292b668275SBarry Smith 
430b850b91aSLisandro Dalcin .seealso:   SNESGetFunctionNorm(), SNESGetLinearSolveIterations()
4319b94acceSBarry Smith @*/
43263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetIterationNumber(SNES snes,PetscInt* iter)
4339b94acceSBarry Smith {
4343a40ed3dSBarry Smith   PetscFunctionBegin;
4354482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
4364482741eSBarry Smith   PetscValidIntPointer(iter,2);
4379b94acceSBarry Smith   *iter = snes->iter;
4383a40ed3dSBarry Smith   PetscFunctionReturn(0);
4399b94acceSBarry Smith }
44074679c65SBarry Smith 
4414a2ae208SSatish Balay #undef __FUNCT__
4424a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm"
4439b94acceSBarry Smith /*@
4449b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
4459b94acceSBarry Smith    with SNESSSetFunction().
4469b94acceSBarry Smith 
447c7afd0dbSLois Curfman McInnes    Collective on SNES
448c7afd0dbSLois Curfman McInnes 
4499b94acceSBarry Smith    Input Parameter:
4509b94acceSBarry Smith .  snes - SNES context
4519b94acceSBarry Smith 
4529b94acceSBarry Smith    Output Parameter:
4539b94acceSBarry Smith .  fnorm - 2-norm of function
4549b94acceSBarry Smith 
45536851e7fSLois Curfman McInnes    Level: intermediate
45636851e7fSLois Curfman McInnes 
4579b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
458a86d99e1SLois Curfman McInnes 
459b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations()
4609b94acceSBarry Smith @*/
46171f87433Sdalcinl PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunctionNorm(SNES snes,PetscReal *fnorm)
4629b94acceSBarry Smith {
4633a40ed3dSBarry Smith   PetscFunctionBegin;
4644482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
4654482741eSBarry Smith   PetscValidScalarPointer(fnorm,2);
4669b94acceSBarry Smith   *fnorm = snes->norm;
4673a40ed3dSBarry Smith   PetscFunctionReturn(0);
4689b94acceSBarry Smith }
46974679c65SBarry Smith 
4704a2ae208SSatish Balay #undef __FUNCT__
471b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures"
4729b94acceSBarry Smith /*@
473b850b91aSLisandro Dalcin    SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps
4749b94acceSBarry Smith    attempted by the nonlinear solver.
4759b94acceSBarry Smith 
476c7afd0dbSLois Curfman McInnes    Not Collective
477c7afd0dbSLois Curfman McInnes 
4789b94acceSBarry Smith    Input Parameter:
4799b94acceSBarry Smith .  snes - SNES context
4809b94acceSBarry Smith 
4819b94acceSBarry Smith    Output Parameter:
4829b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
4839b94acceSBarry Smith 
484c96a6f78SLois Curfman McInnes    Notes:
485c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
486c96a6f78SLois Curfman McInnes 
48736851e7fSLois Curfman McInnes    Level: intermediate
48836851e7fSLois Curfman McInnes 
4899b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
49058ebbce7SBarry Smith 
491e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
49258ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures()
4939b94acceSBarry Smith @*/
494b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails)
4959b94acceSBarry Smith {
4963a40ed3dSBarry Smith   PetscFunctionBegin;
4974482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
4984482741eSBarry Smith   PetscValidIntPointer(nfails,2);
49950ffb88aSMatthew Knepley   *nfails = snes->numFailures;
50050ffb88aSMatthew Knepley   PetscFunctionReturn(0);
50150ffb88aSMatthew Knepley }
50250ffb88aSMatthew Knepley 
50350ffb88aSMatthew Knepley #undef __FUNCT__
504b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures"
50550ffb88aSMatthew Knepley /*@
506b850b91aSLisandro Dalcin    SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps
50750ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
50850ffb88aSMatthew Knepley 
50950ffb88aSMatthew Knepley    Not Collective
51050ffb88aSMatthew Knepley 
51150ffb88aSMatthew Knepley    Input Parameters:
51250ffb88aSMatthew Knepley +  snes     - SNES context
51350ffb88aSMatthew Knepley -  maxFails - maximum of unsuccessful steps
51450ffb88aSMatthew Knepley 
51550ffb88aSMatthew Knepley    Level: intermediate
51650ffb88aSMatthew Knepley 
51750ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
51858ebbce7SBarry Smith 
519e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
52058ebbce7SBarry Smith           SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
52150ffb88aSMatthew Knepley @*/
522b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails)
52350ffb88aSMatthew Knepley {
52450ffb88aSMatthew Knepley   PetscFunctionBegin;
5254482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
52650ffb88aSMatthew Knepley   snes->maxFailures = maxFails;
52750ffb88aSMatthew Knepley   PetscFunctionReturn(0);
52850ffb88aSMatthew Knepley }
52950ffb88aSMatthew Knepley 
53050ffb88aSMatthew Knepley #undef __FUNCT__
531b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures"
53250ffb88aSMatthew Knepley /*@
533b850b91aSLisandro Dalcin    SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps
53450ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
53550ffb88aSMatthew Knepley 
53650ffb88aSMatthew Knepley    Not Collective
53750ffb88aSMatthew Knepley 
53850ffb88aSMatthew Knepley    Input Parameter:
53950ffb88aSMatthew Knepley .  snes     - SNES context
54050ffb88aSMatthew Knepley 
54150ffb88aSMatthew Knepley    Output Parameter:
54250ffb88aSMatthew Knepley .  maxFails - maximum of unsuccessful steps
54350ffb88aSMatthew Knepley 
54450ffb88aSMatthew Knepley    Level: intermediate
54550ffb88aSMatthew Knepley 
54650ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
54758ebbce7SBarry Smith 
548e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
54958ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
55058ebbce7SBarry Smith 
55150ffb88aSMatthew Knepley @*/
552b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails)
55350ffb88aSMatthew Knepley {
55450ffb88aSMatthew Knepley   PetscFunctionBegin;
5554482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
5564482741eSBarry Smith   PetscValidIntPointer(maxFails,2);
55750ffb88aSMatthew Knepley   *maxFails = snes->maxFailures;
5583a40ed3dSBarry Smith   PetscFunctionReturn(0);
5599b94acceSBarry Smith }
560a847f771SSatish Balay 
5614a2ae208SSatish Balay #undef __FUNCT__
5622541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals"
5632541af92SBarry Smith /*@
5642541af92SBarry Smith    SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations
5652541af92SBarry Smith      done by SNES.
5662541af92SBarry Smith 
5672541af92SBarry Smith    Not Collective
5682541af92SBarry Smith 
5692541af92SBarry Smith    Input Parameter:
5702541af92SBarry Smith .  snes     - SNES context
5712541af92SBarry Smith 
5722541af92SBarry Smith    Output Parameter:
5732541af92SBarry Smith .  nfuncs - number of evaluations
5742541af92SBarry Smith 
5752541af92SBarry Smith    Level: intermediate
5762541af92SBarry Smith 
5772541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
57858ebbce7SBarry Smith 
579e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures()
5802541af92SBarry Smith @*/
5812541af92SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs)
5822541af92SBarry Smith {
5832541af92SBarry Smith   PetscFunctionBegin;
5842541af92SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
5852541af92SBarry Smith   PetscValidIntPointer(nfuncs,2);
5862541af92SBarry Smith   *nfuncs = snes->nfuncs;
5872541af92SBarry Smith   PetscFunctionReturn(0);
5882541af92SBarry Smith }
5892541af92SBarry Smith 
5902541af92SBarry Smith #undef __FUNCT__
5913d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures"
5923d4c4710SBarry Smith /*@
5933d4c4710SBarry Smith    SNESGetLinearSolveFailures - Gets the number of failed (non-converged)
5943d4c4710SBarry Smith    linear solvers.
5953d4c4710SBarry Smith 
5963d4c4710SBarry Smith    Not Collective
5973d4c4710SBarry Smith 
5983d4c4710SBarry Smith    Input Parameter:
5993d4c4710SBarry Smith .  snes - SNES context
6003d4c4710SBarry Smith 
6013d4c4710SBarry Smith    Output Parameter:
6023d4c4710SBarry Smith .  nfails - number of failed solves
6033d4c4710SBarry Smith 
6043d4c4710SBarry Smith    Notes:
6053d4c4710SBarry Smith    This counter is reset to zero for each successive call to SNESSolve().
6063d4c4710SBarry Smith 
6073d4c4710SBarry Smith    Level: intermediate
6083d4c4710SBarry Smith 
6093d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
61058ebbce7SBarry Smith 
611e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures()
6123d4c4710SBarry Smith @*/
6133d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails)
6143d4c4710SBarry Smith {
6153d4c4710SBarry Smith   PetscFunctionBegin;
6163d4c4710SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
6173d4c4710SBarry Smith   PetscValidIntPointer(nfails,2);
6183d4c4710SBarry Smith   *nfails = snes->numLinearSolveFailures;
6193d4c4710SBarry Smith   PetscFunctionReturn(0);
6203d4c4710SBarry Smith }
6213d4c4710SBarry Smith 
6223d4c4710SBarry Smith #undef __FUNCT__
6233d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures"
6243d4c4710SBarry Smith /*@
6253d4c4710SBarry Smith    SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts
6263d4c4710SBarry Smith    allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE
6273d4c4710SBarry Smith 
6283d4c4710SBarry Smith    Collective on SNES
6293d4c4710SBarry Smith 
6303d4c4710SBarry Smith    Input Parameters:
6313d4c4710SBarry Smith +  snes     - SNES context
6323d4c4710SBarry Smith -  maxFails - maximum allowed linear solve failures
6333d4c4710SBarry Smith 
6343d4c4710SBarry Smith    Level: intermediate
6353d4c4710SBarry Smith 
636a6796414SBarry Smith    Notes: By default this is 0; that is SNES returns on the first failed linear solve
6373d4c4710SBarry Smith 
6383d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
6393d4c4710SBarry Smith 
64058ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations()
6413d4c4710SBarry Smith @*/
6423d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails)
6433d4c4710SBarry Smith {
6443d4c4710SBarry Smith   PetscFunctionBegin;
6453d4c4710SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
6463d4c4710SBarry Smith   snes->maxLinearSolveFailures = maxFails;
6473d4c4710SBarry Smith   PetscFunctionReturn(0);
6483d4c4710SBarry Smith }
6493d4c4710SBarry Smith 
6503d4c4710SBarry Smith #undef __FUNCT__
6513d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures"
6523d4c4710SBarry Smith /*@
6533d4c4710SBarry Smith    SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that
6543d4c4710SBarry Smith      are allowed before SNES terminates
6553d4c4710SBarry Smith 
6563d4c4710SBarry Smith    Not Collective
6573d4c4710SBarry Smith 
6583d4c4710SBarry Smith    Input Parameter:
6593d4c4710SBarry Smith .  snes     - SNES context
6603d4c4710SBarry Smith 
6613d4c4710SBarry Smith    Output Parameter:
6623d4c4710SBarry Smith .  maxFails - maximum of unsuccessful solves allowed
6633d4c4710SBarry Smith 
6643d4c4710SBarry Smith    Level: intermediate
6653d4c4710SBarry Smith 
6663d4c4710SBarry Smith    Notes: By default this is 1; that is SNES returns on the first failed linear solve
6673d4c4710SBarry Smith 
6683d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
6693d4c4710SBarry Smith 
670e1c61ce8SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(),
6713d4c4710SBarry Smith @*/
6723d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails)
6733d4c4710SBarry Smith {
6743d4c4710SBarry Smith   PetscFunctionBegin;
6753d4c4710SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
6763d4c4710SBarry Smith   PetscValidIntPointer(maxFails,2);
6773d4c4710SBarry Smith   *maxFails = snes->maxLinearSolveFailures;
6783d4c4710SBarry Smith   PetscFunctionReturn(0);
6793d4c4710SBarry Smith }
6803d4c4710SBarry Smith 
6813d4c4710SBarry Smith #undef __FUNCT__
682b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations"
683c96a6f78SLois Curfman McInnes /*@
684b850b91aSLisandro Dalcin    SNESGetLinearSolveIterations - Gets the total number of linear iterations
685c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
686c96a6f78SLois Curfman McInnes 
687c7afd0dbSLois Curfman McInnes    Not Collective
688c7afd0dbSLois Curfman McInnes 
689c96a6f78SLois Curfman McInnes    Input Parameter:
690c96a6f78SLois Curfman McInnes .  snes - SNES context
691c96a6f78SLois Curfman McInnes 
692c96a6f78SLois Curfman McInnes    Output Parameter:
693c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
694c96a6f78SLois Curfman McInnes 
695c96a6f78SLois Curfman McInnes    Notes:
696c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
697c96a6f78SLois Curfman McInnes 
69836851e7fSLois Curfman McInnes    Level: intermediate
69936851e7fSLois Curfman McInnes 
700c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
7012b668275SBarry Smith 
70258ebbce7SBarry Smith .seealso:  SNESGetIterationNumber(), SNESGetFunctionNorm()S, NESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures()
703c96a6f78SLois Curfman McInnes @*/
704b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveIterations(SNES snes,PetscInt* lits)
705c96a6f78SLois Curfman McInnes {
7063a40ed3dSBarry Smith   PetscFunctionBegin;
7074482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
7084482741eSBarry Smith   PetscValidIntPointer(lits,2);
709c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
7103a40ed3dSBarry Smith   PetscFunctionReturn(0);
711c96a6f78SLois Curfman McInnes }
712c96a6f78SLois Curfman McInnes 
7134a2ae208SSatish Balay #undef __FUNCT__
71494b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP"
71552baeb72SSatish Balay /*@
71694b7f48cSBarry Smith    SNESGetKSP - Returns the KSP context for a SNES solver.
7179b94acceSBarry Smith 
71894b7f48cSBarry Smith    Not Collective, but if SNES object is parallel, then KSP object is parallel
719c7afd0dbSLois Curfman McInnes 
7209b94acceSBarry Smith    Input Parameter:
7219b94acceSBarry Smith .  snes - the SNES context
7229b94acceSBarry Smith 
7239b94acceSBarry Smith    Output Parameter:
72494b7f48cSBarry Smith .  ksp - the KSP context
7259b94acceSBarry Smith 
7269b94acceSBarry Smith    Notes:
72794b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
7289b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
7292999313aSBarry Smith    PC contexts as well.
7309b94acceSBarry Smith 
73136851e7fSLois Curfman McInnes    Level: beginner
73236851e7fSLois Curfman McInnes 
73394b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
7349b94acceSBarry Smith 
7352999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
7369b94acceSBarry Smith @*/
73763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetKSP(SNES snes,KSP *ksp)
7389b94acceSBarry Smith {
7391cee3971SBarry Smith   PetscErrorCode ierr;
7401cee3971SBarry Smith 
7413a40ed3dSBarry Smith   PetscFunctionBegin;
7424482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
7434482741eSBarry Smith   PetscValidPointer(ksp,2);
7441cee3971SBarry Smith 
7451cee3971SBarry Smith   if (!snes->ksp) {
7461cee3971SBarry Smith     ierr = KSPCreate(((PetscObject)snes)->comm,&snes->ksp);CHKERRQ(ierr);
7471cee3971SBarry Smith     ierr = PetscObjectIncrementTabLevel((PetscObject)snes->ksp,(PetscObject)snes,1);CHKERRQ(ierr);
7481cee3971SBarry Smith     ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr);
7491cee3971SBarry Smith   }
75094b7f48cSBarry Smith   *ksp = snes->ksp;
7513a40ed3dSBarry Smith   PetscFunctionReturn(0);
7529b94acceSBarry Smith }
75382bf6240SBarry Smith 
7544a2ae208SSatish Balay #undef __FUNCT__
7552999313aSBarry Smith #define __FUNCT__ "SNESSetKSP"
7562999313aSBarry Smith /*@
7572999313aSBarry Smith    SNESSetKSP - Sets a KSP context for the SNES object to use
7582999313aSBarry Smith 
7592999313aSBarry Smith    Not Collective, but the SNES and KSP objects must live on the same MPI_Comm
7602999313aSBarry Smith 
7612999313aSBarry Smith    Input Parameters:
7622999313aSBarry Smith +  snes - the SNES context
7632999313aSBarry Smith -  ksp - the KSP context
7642999313aSBarry Smith 
7652999313aSBarry Smith    Notes:
7662999313aSBarry Smith    The SNES object already has its KSP object, you can obtain with SNESGetKSP()
7672999313aSBarry Smith    so this routine is rarely needed.
7682999313aSBarry Smith 
7692999313aSBarry Smith    The KSP object that is already in the SNES object has its reference count
7702999313aSBarry Smith    decreased by one.
7712999313aSBarry Smith 
7722999313aSBarry Smith    Level: developer
7732999313aSBarry Smith 
7742999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
7752999313aSBarry Smith 
7762999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
7772999313aSBarry Smith @*/
7782999313aSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetKSP(SNES snes,KSP ksp)
7792999313aSBarry Smith {
7802999313aSBarry Smith   PetscErrorCode ierr;
7812999313aSBarry Smith 
7822999313aSBarry Smith   PetscFunctionBegin;
7832999313aSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
7842999313aSBarry Smith   PetscValidHeaderSpecific(ksp,KSP_COOKIE,2);
7852999313aSBarry Smith   PetscCheckSameComm(snes,1,ksp,2);
7867dcf0eaaSdalcinl   ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr);
787906ed7ccSBarry Smith   if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);}
7882999313aSBarry Smith   snes->ksp = ksp;
7892999313aSBarry Smith   PetscFunctionReturn(0);
7902999313aSBarry Smith }
7912999313aSBarry Smith 
7927adad957SLisandro Dalcin #if 0
7932999313aSBarry Smith #undef __FUNCT__
7944a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc"
7956849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj)
796e24b481bSBarry Smith {
797e24b481bSBarry Smith   PetscFunctionBegin;
798e24b481bSBarry Smith   PetscFunctionReturn(0);
799e24b481bSBarry Smith }
8007adad957SLisandro Dalcin #endif
801e24b481bSBarry Smith 
8029b94acceSBarry Smith /* -----------------------------------------------------------*/
8034a2ae208SSatish Balay #undef __FUNCT__
8044a2ae208SSatish Balay #define __FUNCT__ "SNESCreate"
80552baeb72SSatish Balay /*@
8069b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
8079b94acceSBarry Smith 
808c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
809c7afd0dbSLois Curfman McInnes 
810c7afd0dbSLois Curfman McInnes    Input Parameters:
811906ed7ccSBarry Smith .  comm - MPI communicator
8129b94acceSBarry Smith 
8139b94acceSBarry Smith    Output Parameter:
8149b94acceSBarry Smith .  outsnes - the new SNES context
8159b94acceSBarry Smith 
816c7afd0dbSLois Curfman McInnes    Options Database Keys:
817c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
818c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
819c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
820c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
821c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
822c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
823c1f60f51SBarry Smith 
82436851e7fSLois Curfman McInnes    Level: beginner
82536851e7fSLois Curfman McInnes 
8269b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
8279b94acceSBarry Smith 
828a8054027SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNES, SNESSetLagPreconditioner()
829a8054027SBarry Smith 
8309b94acceSBarry Smith @*/
83163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESCreate(MPI_Comm comm,SNES *outsnes)
8329b94acceSBarry Smith {
833dfbe8321SBarry Smith   PetscErrorCode      ierr;
8349b94acceSBarry Smith   SNES                snes;
835fa9f3622SBarry Smith   SNESKSPEW           *kctx;
83637fcc0dbSBarry Smith 
8373a40ed3dSBarry Smith   PetscFunctionBegin;
838ed1caa07SMatthew Knepley   PetscValidPointer(outsnes,2);
8398ba1e511SMatthew Knepley   *outsnes = PETSC_NULL;
8408ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
8418ba1e511SMatthew Knepley   ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr);
8428ba1e511SMatthew Knepley #endif
8438ba1e511SMatthew Knepley 
844e7788613SBarry Smith   ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr);
8457adad957SLisandro Dalcin 
84685385478SLisandro Dalcin   snes->ops->converged    = SNESDefaultConverged;
8479b94acceSBarry Smith   snes->max_its           = 50;
8489750a799SBarry Smith   snes->max_funcs	  = 10000;
8499b94acceSBarry Smith   snes->norm		  = 0.0;
850b4874afaSBarry Smith   snes->rtol		  = 1.e-8;
851b4874afaSBarry Smith   snes->ttol              = 0.0;
85270441072SBarry Smith   snes->abstol		  = 1.e-50;
8539b94acceSBarry Smith   snes->xtol		  = 1.e-8;
8544b27c08aSLois Curfman McInnes   snes->deltatol	  = 1.e-12;
8559b94acceSBarry Smith   snes->nfuncs            = 0;
85650ffb88aSMatthew Knepley   snes->numFailures       = 0;
85750ffb88aSMatthew Knepley   snes->maxFailures       = 1;
8587a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
859e35cf81dSBarry Smith   snes->lagjacobian       = 1;
860a8054027SBarry Smith   snes->lagpreconditioner = 1;
861639f9d9dSBarry Smith   snes->numbermonitors    = 0;
8629b94acceSBarry Smith   snes->data              = 0;
8634dc4c822SBarry Smith   snes->setupcalled       = PETSC_FALSE;
864186905e3SBarry Smith   snes->ksp_ewconv        = PETSC_FALSE;
8656f24a144SLois Curfman McInnes   snes->vwork             = 0;
8666f24a144SLois Curfman McInnes   snes->nwork             = 0;
867758f92a0SBarry Smith   snes->conv_hist_len     = 0;
868758f92a0SBarry Smith   snes->conv_hist_max     = 0;
869758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
870758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
871758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
872184914b5SBarry Smith   snes->reason            = SNES_CONVERGED_ITERATING;
8739b94acceSBarry Smith 
8743d4c4710SBarry Smith   snes->numLinearSolveFailures = 0;
8753d4c4710SBarry Smith   snes->maxLinearSolveFailures = 1;
8763d4c4710SBarry Smith 
8779b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
87838f2d2fdSLisandro Dalcin   ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr);
8799b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
8809b94acceSBarry Smith   kctx->version     = 2;
8819b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
8829b94acceSBarry Smith                              this was too large for some test cases */
8839b94acceSBarry Smith   kctx->rtol_last   = 0;
8849b94acceSBarry Smith   kctx->rtol_max    = .9;
8859b94acceSBarry Smith   kctx->gamma       = 1.0;
88671f87433Sdalcinl   kctx->alpha       = .5*(1.0 + sqrt(5.0));
88771f87433Sdalcinl   kctx->alpha2      = kctx->alpha;
8889b94acceSBarry Smith   kctx->threshold   = .1;
8899b94acceSBarry Smith   kctx->lresid_last = 0;
8909b94acceSBarry Smith   kctx->norm_last   = 0;
8919b94acceSBarry Smith 
8929b94acceSBarry Smith   *outsnes = snes;
89300036973SBarry Smith   ierr = PetscPublishAll(snes);CHKERRQ(ierr);
8943a40ed3dSBarry Smith   PetscFunctionReturn(0);
8959b94acceSBarry Smith }
8969b94acceSBarry Smith 
8974a2ae208SSatish Balay #undef __FUNCT__
8984a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction"
8999b94acceSBarry Smith /*@C
9009b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
9019b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
9029b94acceSBarry Smith    equations.
9039b94acceSBarry Smith 
904fee21e36SBarry Smith    Collective on SNES
905fee21e36SBarry Smith 
906c7afd0dbSLois Curfman McInnes    Input Parameters:
907c7afd0dbSLois Curfman McInnes +  snes - the SNES context
908c7afd0dbSLois Curfman McInnes .  r - vector to store function value
909de044059SHong Zhang .  func - function evaluation routine
910c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
911c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
9129b94acceSBarry Smith 
913c7afd0dbSLois Curfman McInnes    Calling sequence of func:
9148d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
915c7afd0dbSLois Curfman McInnes 
916313e4042SLois Curfman McInnes .  f - function vector
917c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
9189b94acceSBarry Smith 
9199b94acceSBarry Smith    Notes:
9209b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
9219b94acceSBarry Smith $      f'(x) x = -f(x),
922c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
9239b94acceSBarry Smith 
92436851e7fSLois Curfman McInnes    Level: beginner
92536851e7fSLois Curfman McInnes 
9269b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
9279b94acceSBarry Smith 
928a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
9299b94acceSBarry Smith @*/
93063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx)
9319b94acceSBarry Smith {
93285385478SLisandro Dalcin   PetscErrorCode ierr;
9333a40ed3dSBarry Smith   PetscFunctionBegin;
9344482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
9354482741eSBarry Smith   PetscValidHeaderSpecific(r,VEC_COOKIE,2);
936c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,r,2);
93785385478SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr);
93885385478SLisandro Dalcin   if (snes->vec_func) { ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr); }
939e7788613SBarry Smith   snes->ops->computefunction = func;
94085385478SLisandro Dalcin   snes->vec_func             = r;
9419b94acceSBarry Smith   snes->funP                 = ctx;
9423a40ed3dSBarry Smith   PetscFunctionReturn(0);
9439b94acceSBarry Smith }
9449b94acceSBarry Smith 
9453ab0aad5SBarry Smith /* --------------------------------------------------------------- */
9463ab0aad5SBarry Smith #undef __FUNCT__
9471096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs"
9481096aae1SMatthew Knepley /*@C
9491096aae1SMatthew Knepley    SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set
9501096aae1SMatthew Knepley    it assumes a zero right hand side.
9511096aae1SMatthew Knepley 
9521096aae1SMatthew Knepley    Collective on SNES
9531096aae1SMatthew Knepley 
9541096aae1SMatthew Knepley    Input Parameter:
9551096aae1SMatthew Knepley .  snes - the SNES context
9561096aae1SMatthew Knepley 
9571096aae1SMatthew Knepley    Output Parameter:
958bc08b0f1SBarry Smith .  rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null
9591096aae1SMatthew Knepley 
9601096aae1SMatthew Knepley    Level: intermediate
9611096aae1SMatthew Knepley 
9621096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side
9631096aae1SMatthew Knepley 
96485385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
9651096aae1SMatthew Knepley @*/
9661096aae1SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetRhs(SNES snes,Vec *rhs)
9671096aae1SMatthew Knepley {
9681096aae1SMatthew Knepley   PetscFunctionBegin;
9691096aae1SMatthew Knepley   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
9701096aae1SMatthew Knepley   PetscValidPointer(rhs,2);
97185385478SLisandro Dalcin   *rhs = snes->vec_rhs;
9721096aae1SMatthew Knepley   PetscFunctionReturn(0);
9731096aae1SMatthew Knepley }
9741096aae1SMatthew Knepley 
9751096aae1SMatthew Knepley #undef __FUNCT__
9764a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction"
9779b94acceSBarry Smith /*@
97836851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
9799b94acceSBarry Smith                          SNESSetFunction().
9809b94acceSBarry Smith 
981c7afd0dbSLois Curfman McInnes    Collective on SNES
982c7afd0dbSLois Curfman McInnes 
9839b94acceSBarry Smith    Input Parameters:
984c7afd0dbSLois Curfman McInnes +  snes - the SNES context
985c7afd0dbSLois Curfman McInnes -  x - input vector
9869b94acceSBarry Smith 
9879b94acceSBarry Smith    Output Parameter:
9883638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
9899b94acceSBarry Smith 
9901bffabb2SLois Curfman McInnes    Notes:
99136851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
99236851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
99336851e7fSLois Curfman McInnes    themselves.
99436851e7fSLois Curfman McInnes 
99536851e7fSLois Curfman McInnes    Level: developer
99636851e7fSLois Curfman McInnes 
9979b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
9989b94acceSBarry Smith 
999a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
10009b94acceSBarry Smith @*/
100163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeFunction(SNES snes,Vec x,Vec y)
10029b94acceSBarry Smith {
1003dfbe8321SBarry Smith   PetscErrorCode ierr;
10049b94acceSBarry Smith 
10053a40ed3dSBarry Smith   PetscFunctionBegin;
10064482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
10074482741eSBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE,2);
10084482741eSBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE,3);
1009c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,x,2);
1010c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,3);
1011184914b5SBarry Smith 
1012d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
1013e7788613SBarry Smith   if (snes->ops->computefunction) {
1014d64ed03dSBarry Smith     PetscStackPush("SNES user function");
1015e9a2bbcdSBarry Smith     CHKMEMQ;
1016e7788613SBarry Smith     ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP);
1017e9a2bbcdSBarry Smith     CHKMEMQ;
1018d64ed03dSBarry Smith     PetscStackPop;
1019d5e45103SBarry Smith     if (PetscExceptionValue(ierr)) {
102019717074SBarry Smith       PetscErrorCode pierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(pierr);
102119717074SBarry Smith     }
1022d5e45103SBarry Smith     CHKERRQ(ierr);
102385385478SLisandro Dalcin   } else if (snes->vec_rhs) {
10241096aae1SMatthew Knepley     ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr);
10251096aae1SMatthew Knepley   } else {
10261096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() before SNESComputeFunction(), likely called from SNESSolve().");
10271096aae1SMatthew Knepley   }
102885385478SLisandro Dalcin   if (snes->vec_rhs) {
102985385478SLisandro Dalcin     ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr);
10303ab0aad5SBarry Smith   }
1031ae3c334cSLois Curfman McInnes   snes->nfuncs++;
1032d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
10333a40ed3dSBarry Smith   PetscFunctionReturn(0);
10349b94acceSBarry Smith }
10359b94acceSBarry Smith 
10364a2ae208SSatish Balay #undef __FUNCT__
10374a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian"
103862fef451SLois Curfman McInnes /*@
103962fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
104062fef451SLois Curfman McInnes    set with SNESSetJacobian().
104162fef451SLois Curfman McInnes 
1042c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1043c7afd0dbSLois Curfman McInnes 
104462fef451SLois Curfman McInnes    Input Parameters:
1045c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1046c7afd0dbSLois Curfman McInnes -  x - input vector
104762fef451SLois Curfman McInnes 
104862fef451SLois Curfman McInnes    Output Parameters:
1049c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
105062fef451SLois Curfman McInnes .  B - optional preconditioning matrix
10512b668275SBarry Smith -  flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER)
1052fee21e36SBarry Smith 
1053e35cf81dSBarry Smith   Options Database Keys:
1054e35cf81dSBarry Smith +    -snes_lag_preconditioner <lag>
1055e35cf81dSBarry Smith -    -snes_lag_jacobian <lag>
1056e35cf81dSBarry Smith 
105762fef451SLois Curfman McInnes    Notes:
105862fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
105962fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
106062fef451SLois Curfman McInnes 
106194b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
1062dc5a77f8SLois Curfman McInnes    flag parameter.
106362fef451SLois Curfman McInnes 
106436851e7fSLois Curfman McInnes    Level: developer
106536851e7fSLois Curfman McInnes 
106662fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
106762fef451SLois Curfman McInnes 
1068e35cf81dSBarry Smith .seealso:  SNESSetJacobian(), KSPSetOperators(), MatStructure, SNESSetLagPreconditioner(), SNESSetLagJacobian()
106962fef451SLois Curfman McInnes @*/
107063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
10719b94acceSBarry Smith {
1072dfbe8321SBarry Smith   PetscErrorCode ierr;
1073ebd3b9afSBarry Smith   PetscTruth     flag;
10743a40ed3dSBarry Smith 
10753a40ed3dSBarry Smith   PetscFunctionBegin;
10764482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
10774482741eSBarry Smith   PetscValidHeaderSpecific(X,VEC_COOKIE,2);
10784482741eSBarry Smith   PetscValidPointer(flg,5);
1079c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,X,2);
1080e7788613SBarry Smith   if (!snes->ops->computejacobian) PetscFunctionReturn(0);
1081ebd3b9afSBarry Smith 
1082ebd3b9afSBarry Smith   /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */
1083ebd3b9afSBarry Smith 
1084fe3ffe1eSBarry Smith   if (snes->lagjacobian == -2) {
1085fe3ffe1eSBarry Smith     snes->lagjacobian = -1;
1086fe3ffe1eSBarry Smith     ierr = PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n");CHKERRQ(ierr);
1087fe3ffe1eSBarry Smith   } else if (snes->lagjacobian == -1) {
1088e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1089e35cf81dSBarry Smith     ierr = PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");CHKERRQ(ierr);
1090ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1091ebd3b9afSBarry Smith     if (flag) {
1092ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1093ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1094ebd3b9afSBarry Smith     }
1095e35cf81dSBarry Smith     PetscFunctionReturn(0);
1096e35cf81dSBarry Smith   } else if (snes->lagjacobian > 1 && snes->iter % snes->lagjacobian) {
1097e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1098e35cf81dSBarry Smith     ierr = PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);CHKERRQ(ierr);
1099ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1100ebd3b9afSBarry Smith     if (flag) {
1101ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1102ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1103ebd3b9afSBarry Smith     }
1104e35cf81dSBarry Smith     PetscFunctionReturn(0);
1105e35cf81dSBarry Smith   }
1106e35cf81dSBarry Smith 
1107c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1108e35cf81dSBarry Smith   ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1109d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
1110dc67937bSBarry Smith   CHKMEMQ;
1111e7788613SBarry Smith   ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
1112dc67937bSBarry Smith   CHKMEMQ;
1113d64ed03dSBarry Smith   PetscStackPop;
1114d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1115a8054027SBarry Smith 
11163b4f5425SBarry Smith   if (snes->lagpreconditioner == -2) {
11173b4f5425SBarry Smith     ierr = PetscInfo(snes,"Rebuilding preconditioner exactly once since lag is -2\n");CHKERRQ(ierr);
11183b4f5425SBarry Smith     snes->lagpreconditioner = -1;
11193b4f5425SBarry Smith   } else if (snes->lagpreconditioner == -1) {
1120a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1121a8054027SBarry Smith     ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr);
1122a8054027SBarry Smith   } else if (snes->lagpreconditioner > 1 && snes->iter % snes->lagpreconditioner) {
1123a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1124a8054027SBarry Smith     ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr);
1125a8054027SBarry Smith   }
1126a8054027SBarry Smith 
11276d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
11286ce558aeSBarry Smith   /* PetscValidHeaderSpecific(*A,MAT_COOKIE,3);
11296ce558aeSBarry Smith     PetscValidHeaderSpecific(*B,MAT_COOKIE,4);   */
11303a40ed3dSBarry Smith   PetscFunctionReturn(0);
11319b94acceSBarry Smith }
11329b94acceSBarry Smith 
11334a2ae208SSatish Balay #undef __FUNCT__
11344a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian"
11359b94acceSBarry Smith /*@C
11369b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1137044dda88SLois Curfman McInnes    location to store the matrix.
11389b94acceSBarry Smith 
1139c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1140c7afd0dbSLois Curfman McInnes 
11419b94acceSBarry Smith    Input Parameters:
1142c7afd0dbSLois Curfman McInnes +  snes - the SNES context
11439b94acceSBarry Smith .  A - Jacobian matrix
11449b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
11459b94acceSBarry Smith .  func - Jacobian evaluation routine
1146c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
11472cd2dfdcSLois Curfman McInnes          Jacobian evaluation routine (may be PETSC_NULL)
11489b94acceSBarry Smith 
11499b94acceSBarry Smith    Calling sequence of func:
11508d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
11519b94acceSBarry Smith 
1152c7afd0dbSLois Curfman McInnes +  x - input vector
11539b94acceSBarry Smith .  A - Jacobian matrix
11549b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1155ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
11562b668275SBarry Smith    structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER
1157c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
11589b94acceSBarry Smith 
11599b94acceSBarry Smith    Notes:
116094b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
11612cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1162ac21db08SLois Curfman McInnes 
1163ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
11649b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
11659b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
11669b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
11679b94acceSBarry Smith    throughout the global iterations.
11689b94acceSBarry Smith 
116916913363SBarry Smith    If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on
117016913363SBarry Smith    each matrix.
117116913363SBarry Smith 
117236851e7fSLois Curfman McInnes    Level: beginner
117336851e7fSLois Curfman McInnes 
11749b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
11759b94acceSBarry Smith 
11763ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure
11779b94acceSBarry Smith @*/
117863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
11799b94acceSBarry Smith {
1180dfbe8321SBarry Smith   PetscErrorCode ierr;
11813a7fca6bSBarry Smith 
11823a40ed3dSBarry Smith   PetscFunctionBegin;
11834482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
11844482741eSBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_COOKIE,2);
11854482741eSBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_COOKIE,3);
1186c9780b6fSBarry Smith   if (A) PetscCheckSameComm(snes,1,A,2);
1187c9780b6fSBarry Smith   if (B) PetscCheckSameComm(snes,1,B,2);
1188e7788613SBarry Smith   if (func) snes->ops->computejacobian = func;
11893a7fca6bSBarry Smith   if (ctx)  snes->jacP                 = ctx;
11903a7fca6bSBarry Smith   if (A) {
11917dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
11923a7fca6bSBarry Smith     if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
11939b94acceSBarry Smith     snes->jacobian = A;
11943a7fca6bSBarry Smith   }
11953a7fca6bSBarry Smith   if (B) {
11967dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
11973a7fca6bSBarry Smith     if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
11989b94acceSBarry Smith     snes->jacobian_pre = B;
11993a7fca6bSBarry Smith   }
12003a40ed3dSBarry Smith   PetscFunctionReturn(0);
12019b94acceSBarry Smith }
120262fef451SLois Curfman McInnes 
12034a2ae208SSatish Balay #undef __FUNCT__
12044a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian"
1205c2aafc4cSSatish Balay /*@C
1206b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1207b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1208b4fd4287SBarry Smith 
1209c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1210c7afd0dbSLois Curfman McInnes 
1211b4fd4287SBarry Smith    Input Parameter:
1212b4fd4287SBarry Smith .  snes - the nonlinear solver context
1213b4fd4287SBarry Smith 
1214b4fd4287SBarry Smith    Output Parameters:
1215c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1216b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
121770e92668SMatthew Knepley .  func - location to put Jacobian function (or PETSC_NULL)
121870e92668SMatthew Knepley -  ctx - location to stash Jacobian ctx (or PETSC_NULL)
1219fee21e36SBarry Smith 
122036851e7fSLois Curfman McInnes    Level: advanced
122136851e7fSLois Curfman McInnes 
1222b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1223b4fd4287SBarry Smith @*/
122470e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx)
1225b4fd4287SBarry Smith {
12263a40ed3dSBarry Smith   PetscFunctionBegin;
12274482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1228b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
1229b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
1230e7788613SBarry Smith   if (func) *func = snes->ops->computejacobian;
123170e92668SMatthew Knepley   if (ctx)  *ctx  = snes->jacP;
12323a40ed3dSBarry Smith   PetscFunctionReturn(0);
1233b4fd4287SBarry Smith }
1234b4fd4287SBarry Smith 
12359b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
123663dd3a1aSKris Buschelman EXTERN PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*);
12379b94acceSBarry Smith 
12384a2ae208SSatish Balay #undef __FUNCT__
12394a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp"
12409b94acceSBarry Smith /*@
12419b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1242272ac6f2SLois Curfman McInnes    of a nonlinear solver.
12439b94acceSBarry Smith 
1244fee21e36SBarry Smith    Collective on SNES
1245fee21e36SBarry Smith 
1246c7afd0dbSLois Curfman McInnes    Input Parameters:
124770e92668SMatthew Knepley .  snes - the SNES context
1248c7afd0dbSLois Curfman McInnes 
1249272ac6f2SLois Curfman McInnes    Notes:
1250272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1251272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1252272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1253272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1254272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1255272ac6f2SLois Curfman McInnes 
125636851e7fSLois Curfman McInnes    Level: advanced
125736851e7fSLois Curfman McInnes 
12589b94acceSBarry Smith .keywords: SNES, nonlinear, setup
12599b94acceSBarry Smith 
12609b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
12619b94acceSBarry Smith @*/
126270e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUp(SNES snes)
12639b94acceSBarry Smith {
1264dfbe8321SBarry Smith   PetscErrorCode ierr;
126571f87433Sdalcinl   PetscTruth     flg;
12663a40ed3dSBarry Smith 
12673a40ed3dSBarry Smith   PetscFunctionBegin;
12684482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
12694dc4c822SBarry Smith   if (snes->setupcalled) PetscFunctionReturn(0);
12709b94acceSBarry Smith 
12717adad957SLisandro Dalcin   if (!((PetscObject)snes)->type_name) {
127285385478SLisandro Dalcin     ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr);
127385385478SLisandro Dalcin   }
127485385478SLisandro Dalcin 
12757adad957SLisandro Dalcin   ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf_operator",&flg);CHKERRQ(ierr);
1276c1f60f51SBarry Smith   /*
1277c1f60f51SBarry Smith       This version replaces the user provided Jacobian matrix with a
1278dfa02198SLois Curfman McInnes       matrix-free version but still employs the user-provided preconditioner matrix
1279c1f60f51SBarry Smith   */
1280c1f60f51SBarry Smith   if (flg) {
1281c1f60f51SBarry Smith     Mat J;
1282fef1beadSBarry Smith     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
12833ec795f1SBarry Smith     ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr);
1284ae15b995SBarry Smith     ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr);
12853a7fca6bSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
12863a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
1287c1f60f51SBarry Smith   }
128845fc7adcSBarry Smith 
128903c60df9SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_MAT_SINGLE) && !defined(PETSC_USE_LONG_DOUBLE) && !defined(PETSC_USE_INT)
12907adad957SLisandro Dalcin   ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf_operator2",&flg);CHKERRQ(ierr);
129145fc7adcSBarry Smith   if (flg) {
129245fc7adcSBarry Smith     Mat J;
129345fc7adcSBarry Smith     ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_sol,&J);CHKERRQ(ierr);
129475396ef9SLisandro Dalcin     ierr = PetscInfo(snes,"Setting default matrix-free operator routines (version 2)\n");CHKERRQ(ierr);
129545fc7adcSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
129645fc7adcSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
129745fc7adcSBarry Smith   }
129832a4b47aSMatthew Knepley #endif
129945fc7adcSBarry Smith 
13007adad957SLisandro Dalcin   ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf",&flg);CHKERRQ(ierr);
1301c1f60f51SBarry Smith   /*
1302dfa02198SLois Curfman McInnes       This version replaces both the user-provided Jacobian and the user-
1303c1f60f51SBarry Smith       provided preconditioner matrix with the default matrix free version.
1304c1f60f51SBarry Smith    */
130531615d04SLois Curfman McInnes   if (flg) {
1306272ac6f2SLois Curfman McInnes     Mat  J;
1307b5d62d44SBarry Smith     KSP ksp;
130894b7f48cSBarry Smith     PC   pc;
130975396ef9SLisandro Dalcin     /* create and set matrix-free operator */
1310fef1beadSBarry Smith     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
13113ec795f1SBarry Smith     ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr);
131275396ef9SLisandro Dalcin     ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr);
13133ec795f1SBarry Smith     ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr);
13143a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
1315f3ef73ceSBarry Smith     /* force no preconditioner */
131694b7f48cSBarry Smith     ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
1317b5d62d44SBarry Smith     ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
1318a9815358SBarry Smith     ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&flg);CHKERRQ(ierr);
1319a9815358SBarry Smith     if (!flg) {
132075396ef9SLisandro Dalcin       ierr = PetscInfo(snes,"Setting default matrix-free preconditioner routines;\nThat is no preconditioner is being used\n");CHKERRQ(ierr);
1321f3ef73ceSBarry Smith       ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
1322272ac6f2SLois Curfman McInnes     }
1323a9815358SBarry Smith   }
1324f3ef73ceSBarry Smith 
132585385478SLisandro Dalcin   if (!snes->vec_func && !snes->vec_rhs) {
13261096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
13271096aae1SMatthew Knepley   }
132885385478SLisandro Dalcin   if (!snes->ops->computefunction && !snes->vec_rhs) {
13291096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
13301096aae1SMatthew Knepley   }
1331a8c6a408SBarry Smith   if (snes->vec_func == snes->vec_sol) {
133229bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
1333a8c6a408SBarry Smith   }
1334a703fe33SLois Curfman McInnes 
1335410397dcSLisandro Dalcin   if (snes->ops->setup) {
1336410397dcSLisandro Dalcin     ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr);
1337410397dcSLisandro Dalcin   }
13387aaed0d8SBarry Smith   snes->setupcalled = PETSC_TRUE;
13393a40ed3dSBarry Smith   PetscFunctionReturn(0);
13409b94acceSBarry Smith }
13419b94acceSBarry Smith 
13424a2ae208SSatish Balay #undef __FUNCT__
13434a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
134452baeb72SSatish Balay /*@
13459b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
13469b94acceSBarry Smith    with SNESCreate().
13479b94acceSBarry Smith 
1348c7afd0dbSLois Curfman McInnes    Collective on SNES
1349c7afd0dbSLois Curfman McInnes 
13509b94acceSBarry Smith    Input Parameter:
13519b94acceSBarry Smith .  snes - the SNES context
13529b94acceSBarry Smith 
135336851e7fSLois Curfman McInnes    Level: beginner
135436851e7fSLois Curfman McInnes 
13559b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
13569b94acceSBarry Smith 
135763a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
13589b94acceSBarry Smith @*/
135963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDestroy(SNES snes)
13609b94acceSBarry Smith {
13616849ba73SBarry Smith   PetscErrorCode ierr;
13623a40ed3dSBarry Smith 
13633a40ed3dSBarry Smith   PetscFunctionBegin;
13644482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
13657adad957SLisandro Dalcin   if (--((PetscObject)snes)->refct > 0) PetscFunctionReturn(0);
1366d4bb536fSBarry Smith 
1367be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
13680f5bd95cSBarry Smith   ierr = PetscObjectDepublish(snes);CHKERRQ(ierr);
1369e7788613SBarry Smith   if (snes->ops->destroy) {ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr);}
137085385478SLisandro Dalcin 
137185385478SLisandro Dalcin   if (snes->vec_rhs) {ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr);}
137285385478SLisandro Dalcin   if (snes->vec_sol) {ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr);}
137385385478SLisandro Dalcin   if (snes->vec_func) {ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr);}
13743a7fca6bSBarry Smith   if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
13753a7fca6bSBarry Smith   if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
13761cee3971SBarry Smith   if (snes->ksp) {ierr = KSPDestroy(snes->ksp);CHKERRQ(ierr);}
137785385478SLisandro Dalcin   ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);
1378522c5e43SBarry Smith   if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);}
1379a6570f20SBarry Smith   ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);
13807f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);}
1381a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr);
13823a40ed3dSBarry Smith   PetscFunctionReturn(0);
13839b94acceSBarry Smith }
13849b94acceSBarry Smith 
13859b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
13869b94acceSBarry Smith 
13874a2ae208SSatish Balay #undef __FUNCT__
1388a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner"
1389a8054027SBarry Smith /*@
1390a8054027SBarry Smith    SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve.
1391a8054027SBarry Smith 
1392a8054027SBarry Smith    Collective on SNES
1393a8054027SBarry Smith 
1394a8054027SBarry Smith    Input Parameters:
1395a8054027SBarry Smith +  snes - the SNES context
1396a8054027SBarry Smith -  lag - -1 indicates NEVER rebuild, 1 means rebuild every time the Jacobian is computed within a single nonlinear solve, 2 means every second time
13973b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1398a8054027SBarry Smith 
1399a8054027SBarry Smith    Options Database Keys:
1400a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1401a8054027SBarry Smith 
1402a8054027SBarry Smith    Notes:
1403a8054027SBarry Smith    The default is 1
1404a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1405a8054027SBarry Smith    If  -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use
1406a8054027SBarry Smith 
1407a8054027SBarry Smith    Level: intermediate
1408a8054027SBarry Smith 
1409a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1410a8054027SBarry Smith 
1411e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian()
1412a8054027SBarry Smith 
1413a8054027SBarry Smith @*/
1414a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagPreconditioner(SNES snes,PetscInt lag)
1415a8054027SBarry Smith {
1416a8054027SBarry Smith   PetscFunctionBegin;
1417a8054027SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
14183b4f5425SBarry Smith   if (lag < -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1419a8054027SBarry Smith   if (!lag) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1420a8054027SBarry Smith   snes->lagpreconditioner = lag;
1421a8054027SBarry Smith   PetscFunctionReturn(0);
1422a8054027SBarry Smith }
1423a8054027SBarry Smith 
1424a8054027SBarry Smith #undef __FUNCT__
1425a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner"
1426a8054027SBarry Smith /*@
1427a8054027SBarry Smith    SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt
1428a8054027SBarry Smith 
1429a8054027SBarry Smith    Collective on SNES
1430a8054027SBarry Smith 
1431a8054027SBarry Smith    Input Parameter:
1432a8054027SBarry Smith .  snes - the SNES context
1433a8054027SBarry Smith 
1434a8054027SBarry Smith    Output Parameter:
1435a8054027SBarry Smith .   lag - -1 indicates NEVER rebuild, 1 means rebuild every time the Jacobian is computed within a single nonlinear solve, 2 means every second time
14363b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1437a8054027SBarry Smith 
1438a8054027SBarry Smith    Options Database Keys:
1439a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1440a8054027SBarry Smith 
1441a8054027SBarry Smith    Notes:
1442a8054027SBarry Smith    The default is 1
1443a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1444a8054027SBarry Smith 
1445a8054027SBarry Smith    Level: intermediate
1446a8054027SBarry Smith 
1447a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1448a8054027SBarry Smith 
1449a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner()
1450a8054027SBarry Smith 
1451a8054027SBarry Smith @*/
1452a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagPreconditioner(SNES snes,PetscInt *lag)
1453a8054027SBarry Smith {
1454a8054027SBarry Smith   PetscFunctionBegin;
1455a8054027SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1456a8054027SBarry Smith   *lag = snes->lagpreconditioner;
1457a8054027SBarry Smith   PetscFunctionReturn(0);
1458a8054027SBarry Smith }
1459a8054027SBarry Smith 
1460a8054027SBarry Smith #undef __FUNCT__
1461e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian"
1462e35cf81dSBarry Smith /*@
1463e35cf81dSBarry Smith    SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how
1464e35cf81dSBarry Smith      often the preconditioner is rebuilt.
1465e35cf81dSBarry Smith 
1466e35cf81dSBarry Smith    Collective on SNES
1467e35cf81dSBarry Smith 
1468e35cf81dSBarry Smith    Input Parameters:
1469e35cf81dSBarry Smith +  snes - the SNES context
1470e35cf81dSBarry Smith -  lag - -1 indicates NEVER rebuild, 1 means rebuild every time the Jacobian is computed within a single nonlinear solve, 2 means every second time
1471fe3ffe1eSBarry Smith          the Jacobian is built etc. -2 means rebuild at next chance but then never again
1472e35cf81dSBarry Smith 
1473e35cf81dSBarry Smith    Options Database Keys:
1474e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1475e35cf81dSBarry Smith 
1476e35cf81dSBarry Smith    Notes:
1477e35cf81dSBarry Smith    The default is 1
1478e35cf81dSBarry Smith    The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1479fe3ffe1eSBarry Smith    If  -1 is used before the very first nonlinear solve the CODE WILL FAIL! because no Jacobian is used, use -2 to indicate you want it recomputed
1480fe3ffe1eSBarry Smith    at the next Newton step but never again (unless it is reset to another value)
1481e35cf81dSBarry Smith 
1482e35cf81dSBarry Smith    Level: intermediate
1483e35cf81dSBarry Smith 
1484e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1485e35cf81dSBarry Smith 
1486e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian()
1487e35cf81dSBarry Smith 
1488e35cf81dSBarry Smith @*/
1489e35cf81dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagJacobian(SNES snes,PetscInt lag)
1490e35cf81dSBarry Smith {
1491e35cf81dSBarry Smith   PetscFunctionBegin;
1492e35cf81dSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1493fe3ffe1eSBarry Smith   if (lag < -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1494e35cf81dSBarry Smith   if (!lag) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1495e35cf81dSBarry Smith   snes->lagjacobian = lag;
1496e35cf81dSBarry Smith   PetscFunctionReturn(0);
1497e35cf81dSBarry Smith }
1498e35cf81dSBarry Smith 
1499e35cf81dSBarry Smith #undef __FUNCT__
1500e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian"
1501e35cf81dSBarry Smith /*@
1502e35cf81dSBarry Smith    SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt
1503e35cf81dSBarry Smith 
1504e35cf81dSBarry Smith    Collective on SNES
1505e35cf81dSBarry Smith 
1506e35cf81dSBarry Smith    Input Parameter:
1507e35cf81dSBarry Smith .  snes - the SNES context
1508e35cf81dSBarry Smith 
1509e35cf81dSBarry Smith    Output Parameter:
1510e35cf81dSBarry Smith .   lag - -1 indicates NEVER rebuild, 1 means rebuild every time the Jacobian is computed within a single nonlinear solve, 2 means every second time
1511e35cf81dSBarry Smith          the Jacobian is built etc.
1512e35cf81dSBarry Smith 
1513e35cf81dSBarry Smith    Options Database Keys:
1514e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1515e35cf81dSBarry Smith 
1516e35cf81dSBarry Smith    Notes:
1517e35cf81dSBarry Smith    The default is 1
1518e35cf81dSBarry Smith    The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1519e35cf81dSBarry Smith 
1520e35cf81dSBarry Smith    Level: intermediate
1521e35cf81dSBarry Smith 
1522e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1523e35cf81dSBarry Smith 
1524e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner()
1525e35cf81dSBarry Smith 
1526e35cf81dSBarry Smith @*/
1527e35cf81dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagJacobian(SNES snes,PetscInt *lag)
1528e35cf81dSBarry Smith {
1529e35cf81dSBarry Smith   PetscFunctionBegin;
1530e35cf81dSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1531e35cf81dSBarry Smith   *lag = snes->lagjacobian;
1532e35cf81dSBarry Smith   PetscFunctionReturn(0);
1533e35cf81dSBarry Smith }
1534e35cf81dSBarry Smith 
1535e35cf81dSBarry Smith #undef __FUNCT__
15364a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
15379b94acceSBarry Smith /*@
1538d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
15399b94acceSBarry Smith 
1540c7afd0dbSLois Curfman McInnes    Collective on SNES
1541c7afd0dbSLois Curfman McInnes 
15429b94acceSBarry Smith    Input Parameters:
1543c7afd0dbSLois Curfman McInnes +  snes - the SNES context
154470441072SBarry Smith .  abstol - absolute convergence tolerance
154533174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
154633174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
154733174efeSLois Curfman McInnes            of the change in the solution between steps
154833174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1549c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1550fee21e36SBarry Smith 
155133174efeSLois Curfman McInnes    Options Database Keys:
155270441072SBarry Smith +    -snes_atol <abstol> - Sets abstol
1553c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1554c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1555c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1556c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
15579b94acceSBarry Smith 
1558d7a720efSLois Curfman McInnes    Notes:
15599b94acceSBarry Smith    The default maximum number of iterations is 50.
15609b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
15619b94acceSBarry Smith 
156236851e7fSLois Curfman McInnes    Level: intermediate
156336851e7fSLois Curfman McInnes 
156433174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
15659b94acceSBarry Smith 
15662492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance()
15679b94acceSBarry Smith @*/
156863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf)
15699b94acceSBarry Smith {
15703a40ed3dSBarry Smith   PetscFunctionBegin;
15714482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
157270441072SBarry Smith   if (abstol != PETSC_DEFAULT)  snes->abstol      = abstol;
1573d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1574d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1575d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1576d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
15773a40ed3dSBarry Smith   PetscFunctionReturn(0);
15789b94acceSBarry Smith }
15799b94acceSBarry Smith 
15804a2ae208SSatish Balay #undef __FUNCT__
15814a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
15829b94acceSBarry Smith /*@
158333174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
158433174efeSLois Curfman McInnes 
1585c7afd0dbSLois Curfman McInnes    Not Collective
1586c7afd0dbSLois Curfman McInnes 
158733174efeSLois Curfman McInnes    Input Parameters:
1588c7afd0dbSLois Curfman McInnes +  snes - the SNES context
158985385478SLisandro Dalcin .  atol - absolute convergence tolerance
159033174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
159133174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
159233174efeSLois Curfman McInnes            of the change in the solution between steps
159333174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1594c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1595fee21e36SBarry Smith 
159633174efeSLois Curfman McInnes    Notes:
159733174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
159833174efeSLois Curfman McInnes 
159936851e7fSLois Curfman McInnes    Level: intermediate
160036851e7fSLois Curfman McInnes 
160133174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
160233174efeSLois Curfman McInnes 
160333174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
160433174efeSLois Curfman McInnes @*/
160585385478SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf)
160633174efeSLois Curfman McInnes {
16073a40ed3dSBarry Smith   PetscFunctionBegin;
16084482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
160985385478SLisandro Dalcin   if (atol)  *atol  = snes->abstol;
161033174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
161133174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
161233174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
161333174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
16143a40ed3dSBarry Smith   PetscFunctionReturn(0);
161533174efeSLois Curfman McInnes }
161633174efeSLois Curfman McInnes 
16174a2ae208SSatish Balay #undef __FUNCT__
16184a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
161933174efeSLois Curfman McInnes /*@
16209b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
16219b94acceSBarry Smith 
1622fee21e36SBarry Smith    Collective on SNES
1623fee21e36SBarry Smith 
1624c7afd0dbSLois Curfman McInnes    Input Parameters:
1625c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1626c7afd0dbSLois Curfman McInnes -  tol - tolerance
1627c7afd0dbSLois Curfman McInnes 
16289b94acceSBarry Smith    Options Database Key:
1629c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
16309b94acceSBarry Smith 
163136851e7fSLois Curfman McInnes    Level: intermediate
163236851e7fSLois Curfman McInnes 
16339b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
16349b94acceSBarry Smith 
16352492ecdbSBarry Smith .seealso: SNESSetTolerances()
16369b94acceSBarry Smith @*/
163763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
16389b94acceSBarry Smith {
16393a40ed3dSBarry Smith   PetscFunctionBegin;
16404482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
16419b94acceSBarry Smith   snes->deltatol = tol;
16423a40ed3dSBarry Smith   PetscFunctionReturn(0);
16439b94acceSBarry Smith }
16449b94acceSBarry Smith 
1645df9fa365SBarry Smith /*
1646df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1647df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1648df9fa365SBarry Smith    macros instead of functions
1649df9fa365SBarry Smith */
16504a2ae208SSatish Balay #undef __FUNCT__
1651a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG"
1652a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx)
1653ce1608b8SBarry Smith {
1654dfbe8321SBarry Smith   PetscErrorCode ierr;
1655ce1608b8SBarry Smith 
1656ce1608b8SBarry Smith   PetscFunctionBegin;
16574482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1658a6570f20SBarry Smith   ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1659ce1608b8SBarry Smith   PetscFunctionReturn(0);
1660ce1608b8SBarry Smith }
1661ce1608b8SBarry Smith 
16624a2ae208SSatish Balay #undef __FUNCT__
1663a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate"
1664a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1665df9fa365SBarry Smith {
1666dfbe8321SBarry Smith   PetscErrorCode ierr;
1667df9fa365SBarry Smith 
1668df9fa365SBarry Smith   PetscFunctionBegin;
1669a6570f20SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1670df9fa365SBarry Smith   PetscFunctionReturn(0);
1671df9fa365SBarry Smith }
1672df9fa365SBarry Smith 
16734a2ae208SSatish Balay #undef __FUNCT__
1674a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy"
1675a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGDestroy(PetscDrawLG draw)
1676df9fa365SBarry Smith {
1677dfbe8321SBarry Smith   PetscErrorCode ierr;
1678df9fa365SBarry Smith 
1679df9fa365SBarry Smith   PetscFunctionBegin;
1680a6570f20SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
1681df9fa365SBarry Smith   PetscFunctionReturn(0);
1682df9fa365SBarry Smith }
1683df9fa365SBarry Smith 
1684b271bb04SBarry Smith extern PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorRange_Private(SNES,PetscInt,PetscReal*);
1685b271bb04SBarry Smith #undef __FUNCT__
1686b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRange"
1687b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRange(SNES snes,PetscInt n,PetscReal rnorm,void *monctx)
1688b271bb04SBarry Smith {
1689b271bb04SBarry Smith   PetscDrawLG      lg;
1690b271bb04SBarry Smith   PetscErrorCode   ierr;
1691b271bb04SBarry Smith   PetscReal        x,y,per;
1692b271bb04SBarry Smith   PetscViewer      v = (PetscViewer)monctx;
1693b271bb04SBarry Smith   static PetscReal prev; /* should be in the context */
1694b271bb04SBarry Smith   PetscDraw        draw;
1695b271bb04SBarry Smith   PetscFunctionBegin;
1696b271bb04SBarry Smith   if (!monctx) {
1697b271bb04SBarry Smith     MPI_Comm    comm;
1698b271bb04SBarry Smith 
1699b271bb04SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr);
1700b271bb04SBarry Smith     v      = PETSC_VIEWER_DRAW_(comm);
1701b271bb04SBarry Smith   }
1702b271bb04SBarry Smith   ierr   = PetscViewerDrawGetDrawLG(v,0,&lg);CHKERRQ(ierr);
1703b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1704b271bb04SBarry Smith   ierr   = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1705b271bb04SBarry Smith   ierr   = PetscDrawSetTitle(draw,"Residual norm");CHKERRQ(ierr);
1706b271bb04SBarry Smith   x = (PetscReal) n;
1707b271bb04SBarry Smith   if (rnorm > 0.0) y = log10(rnorm); else y = -15.0;
1708b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1709b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1710b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1711b271bb04SBarry Smith   }
1712b271bb04SBarry Smith 
1713b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,1,&lg);CHKERRQ(ierr);
1714b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1715b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1716b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"% elemts > .2*max elemt");CHKERRQ(ierr);
1717b271bb04SBarry Smith   ierr =  SNESMonitorRange_Private(snes,n,&per);CHKERRQ(ierr);
1718b271bb04SBarry Smith   x = (PetscReal) n;
1719b271bb04SBarry Smith   y = 100.0*per;
1720b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1721b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1722b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1723b271bb04SBarry Smith   }
1724b271bb04SBarry Smith 
1725b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,2,&lg);CHKERRQ(ierr);
1726b271bb04SBarry Smith   if (!n) {prev = rnorm;ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1727b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1728b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm");CHKERRQ(ierr);
1729b271bb04SBarry Smith   x = (PetscReal) n;
1730b271bb04SBarry Smith   y = (prev - rnorm)/prev;
1731b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1732b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1733b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1734b271bb04SBarry Smith   }
1735b271bb04SBarry Smith 
1736b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,3,&lg);CHKERRQ(ierr);
1737b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1738b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1739b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");CHKERRQ(ierr);
1740b271bb04SBarry Smith   x = (PetscReal) n;
1741b271bb04SBarry Smith   y = (prev - rnorm)/(prev*per);
1742b271bb04SBarry Smith   if (n > 2) { /*skip initial crazy value */
1743b271bb04SBarry Smith     ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1744b271bb04SBarry Smith   }
1745b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1746b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1747b271bb04SBarry Smith   }
1748b271bb04SBarry Smith   prev = rnorm;
1749b271bb04SBarry Smith   PetscFunctionReturn(0);
1750b271bb04SBarry Smith }
1751b271bb04SBarry Smith 
1752b271bb04SBarry Smith #undef __FUNCT__
1753b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeCreate"
1754b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRangeCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1755b271bb04SBarry Smith {
1756b271bb04SBarry Smith   PetscErrorCode ierr;
1757b271bb04SBarry Smith 
1758b271bb04SBarry Smith   PetscFunctionBegin;
1759b271bb04SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1760b271bb04SBarry Smith   PetscFunctionReturn(0);
1761b271bb04SBarry Smith }
1762b271bb04SBarry Smith 
1763b271bb04SBarry Smith #undef __FUNCT__
1764b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeDestroy"
1765b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRangeDestroy(PetscDrawLG draw)
1766b271bb04SBarry Smith {
1767b271bb04SBarry Smith   PetscErrorCode ierr;
1768b271bb04SBarry Smith 
1769b271bb04SBarry Smith   PetscFunctionBegin;
1770b271bb04SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
1771b271bb04SBarry Smith   PetscFunctionReturn(0);
1772b271bb04SBarry Smith }
1773b271bb04SBarry Smith 
17749b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
17759b94acceSBarry Smith 
17764a2ae208SSatish Balay #undef __FUNCT__
1777a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet"
17789b94acceSBarry Smith /*@C
1779a6570f20SBarry Smith    SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every
17809b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
17819b94acceSBarry Smith    progress.
17829b94acceSBarry Smith 
1783fee21e36SBarry Smith    Collective on SNES
1784fee21e36SBarry Smith 
1785c7afd0dbSLois Curfman McInnes    Input Parameters:
1786c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1787c7afd0dbSLois Curfman McInnes .  func - monitoring routine
1788b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
1789e8105e01SRichard Katz           monitor routine (use PETSC_NULL if no context is desired)
1790b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1791b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
17929b94acceSBarry Smith 
1793c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1794a7cc72afSBarry Smith $     int func(SNES snes,PetscInt its, PetscReal norm,void *mctx)
1795c7afd0dbSLois Curfman McInnes 
1796c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1797c7afd0dbSLois Curfman McInnes .    its - iteration number
1798c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
179940a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
18009b94acceSBarry Smith 
18019665c990SLois Curfman McInnes    Options Database Keys:
1802a6570f20SBarry Smith +    -snes_monitor        - sets SNESMonitorDefault()
1803a6570f20SBarry Smith .    -snes_monitor_draw    - sets line graph monitor,
1804a6570f20SBarry Smith                             uses SNESMonitorLGCreate()
1805a6570f20SBarry Smith _    -snes_monitor_cancel - cancels all monitors that have
1806c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
1807a6570f20SBarry Smith                             calls to SNESMonitorSet(), but
1808c7afd0dbSLois Curfman McInnes                             does not cancel those set via
1809c7afd0dbSLois Curfman McInnes                             the options database.
18109665c990SLois Curfman McInnes 
1811639f9d9dSBarry Smith    Notes:
18126bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
1813a6570f20SBarry Smith    SNESMonitorSet() multiple times; all will be called in the
18146bc08f3fSLois Curfman McInnes    order in which they were set.
1815639f9d9dSBarry Smith 
1816025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each SNES object
1817025f1a04SBarry Smith 
181836851e7fSLois Curfman McInnes    Level: intermediate
181936851e7fSLois Curfman McInnes 
18209b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
18219b94acceSBarry Smith 
1822a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel()
18239b94acceSBarry Smith @*/
1824b90d0a6eSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void*))
18259b94acceSBarry Smith {
1826b90d0a6eSBarry Smith   PetscInt i;
1827b90d0a6eSBarry Smith 
18283a40ed3dSBarry Smith   PetscFunctionBegin;
18294482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1830639f9d9dSBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) {
183129bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1832639f9d9dSBarry Smith   }
1833b90d0a6eSBarry Smith   for (i=0; i<snes->numbermonitors;i++) {
1834b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) PetscFunctionReturn(0);
1835b90d0a6eSBarry Smith 
1836b90d0a6eSBarry Smith     /* check if both default monitors that share common ASCII viewer */
1837b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitor == SNESMonitorDefault) {
1838b90d0a6eSBarry Smith       if (mctx && snes->monitorcontext[i]) {
1839b90d0a6eSBarry Smith         PetscErrorCode          ierr;
1840b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer1 = (PetscViewerASCIIMonitor) mctx;
1841b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer2 = (PetscViewerASCIIMonitor) snes->monitorcontext[i];
1842b90d0a6eSBarry Smith         if (viewer1->viewer == viewer2->viewer) {
1843b90d0a6eSBarry Smith           ierr = (*monitordestroy)(mctx);CHKERRQ(ierr);
1844b90d0a6eSBarry Smith           PetscFunctionReturn(0);
1845b90d0a6eSBarry Smith         }
1846b90d0a6eSBarry Smith       }
1847b90d0a6eSBarry Smith     }
1848b90d0a6eSBarry Smith   }
1849b90d0a6eSBarry Smith   snes->monitor[snes->numbermonitors]           = monitor;
1850b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
1851639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
18523a40ed3dSBarry Smith   PetscFunctionReturn(0);
18539b94acceSBarry Smith }
18549b94acceSBarry Smith 
18554a2ae208SSatish Balay #undef __FUNCT__
1856a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel"
18575cd90555SBarry Smith /*@C
1858a6570f20SBarry Smith    SNESMonitorCancel - Clears all the monitor functions for a SNES object.
18595cd90555SBarry Smith 
1860c7afd0dbSLois Curfman McInnes    Collective on SNES
1861c7afd0dbSLois Curfman McInnes 
18625cd90555SBarry Smith    Input Parameters:
18635cd90555SBarry Smith .  snes - the SNES context
18645cd90555SBarry Smith 
18651a480d89SAdministrator    Options Database Key:
1866a6570f20SBarry Smith .  -snes_monitor_cancel - cancels all monitors that have been hardwired
1867a6570f20SBarry Smith     into a code by calls to SNESMonitorSet(), but does not cancel those
1868c7afd0dbSLois Curfman McInnes     set via the options database
18695cd90555SBarry Smith 
18705cd90555SBarry Smith    Notes:
18715cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
18725cd90555SBarry Smith 
187336851e7fSLois Curfman McInnes    Level: intermediate
187436851e7fSLois Curfman McInnes 
18755cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
18765cd90555SBarry Smith 
1877a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet()
18785cd90555SBarry Smith @*/
1879a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorCancel(SNES snes)
18805cd90555SBarry Smith {
1881d952e501SBarry Smith   PetscErrorCode ierr;
1882d952e501SBarry Smith   PetscInt       i;
1883d952e501SBarry Smith 
18845cd90555SBarry Smith   PetscFunctionBegin;
18854482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1886d952e501SBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
1887d952e501SBarry Smith     if (snes->monitordestroy[i]) {
1888d952e501SBarry Smith       ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr);
1889d952e501SBarry Smith     }
1890d952e501SBarry Smith   }
18915cd90555SBarry Smith   snes->numbermonitors = 0;
18925cd90555SBarry Smith   PetscFunctionReturn(0);
18935cd90555SBarry Smith }
18945cd90555SBarry Smith 
18954a2ae208SSatish Balay #undef __FUNCT__
18964a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
18979b94acceSBarry Smith /*@C
18989b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
18999b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
19009b94acceSBarry Smith 
1901fee21e36SBarry Smith    Collective on SNES
1902fee21e36SBarry Smith 
1903c7afd0dbSLois Curfman McInnes    Input Parameters:
1904c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1905c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
19067f7931b9SBarry Smith .  cctx - [optional] context for private data for the convergence routine  (may be PETSC_NULL)
19077f7931b9SBarry Smith -  destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran)
19089b94acceSBarry Smith 
1909c7afd0dbSLois Curfman McInnes    Calling sequence of func:
191006ee9f85SBarry Smith $     PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
1911c7afd0dbSLois Curfman McInnes 
1912c7afd0dbSLois Curfman McInnes +    snes - the SNES context
191306ee9f85SBarry Smith .    it - current iteration (0 is the first and is before any Newton step)
1914c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
1915184914b5SBarry Smith .    reason - reason for convergence/divergence
1916c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
19174b27c08aSLois Curfman McInnes .    gnorm - 2-norm of current step
19184b27c08aSLois Curfman McInnes -    f - 2-norm of function
19199b94acceSBarry Smith 
192036851e7fSLois Curfman McInnes    Level: advanced
192136851e7fSLois Curfman McInnes 
19229b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
19239b94acceSBarry Smith 
192485385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged()
19259b94acceSBarry Smith @*/
19267f7931b9SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*))
19279b94acceSBarry Smith {
19287f7931b9SBarry Smith   PetscErrorCode ierr;
19297f7931b9SBarry Smith 
19303a40ed3dSBarry Smith   PetscFunctionBegin;
19314482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
193285385478SLisandro Dalcin   if (!func) func = SNESSkipConverged;
19337f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {
19347f7931b9SBarry Smith     ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);
19357f7931b9SBarry Smith   }
193685385478SLisandro Dalcin   snes->ops->converged        = func;
19377f7931b9SBarry Smith   snes->ops->convergeddestroy = destroy;
193885385478SLisandro Dalcin   snes->cnvP                  = cctx;
19393a40ed3dSBarry Smith   PetscFunctionReturn(0);
19409b94acceSBarry Smith }
19419b94acceSBarry Smith 
19424a2ae208SSatish Balay #undef __FUNCT__
19434a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
194452baeb72SSatish Balay /*@
1945184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
1946184914b5SBarry Smith 
1947184914b5SBarry Smith    Not Collective
1948184914b5SBarry Smith 
1949184914b5SBarry Smith    Input Parameter:
1950184914b5SBarry Smith .  snes - the SNES context
1951184914b5SBarry Smith 
1952184914b5SBarry Smith    Output Parameter:
19534d0a8057SBarry Smith .  reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the
1954184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
1955184914b5SBarry Smith 
1956184914b5SBarry Smith    Level: intermediate
1957184914b5SBarry Smith 
1958184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
1959184914b5SBarry Smith 
1960184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
1961184914b5SBarry Smith 
196285385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason
1963184914b5SBarry Smith @*/
196463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
1965184914b5SBarry Smith {
1966184914b5SBarry Smith   PetscFunctionBegin;
19674482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
19684482741eSBarry Smith   PetscValidPointer(reason,2);
1969184914b5SBarry Smith   *reason = snes->reason;
1970184914b5SBarry Smith   PetscFunctionReturn(0);
1971184914b5SBarry Smith }
1972184914b5SBarry Smith 
19734a2ae208SSatish Balay #undef __FUNCT__
19744a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
1975c9005455SLois Curfman McInnes /*@
1976c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
1977c9005455SLois Curfman McInnes 
1978fee21e36SBarry Smith    Collective on SNES
1979fee21e36SBarry Smith 
1980c7afd0dbSLois Curfman McInnes    Input Parameters:
1981c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
1982c7afd0dbSLois Curfman McInnes .  a   - array to hold history
1983cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
1984758f92a0SBarry Smith .  na  - size of a and its
198564731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
1986758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
1987c7afd0dbSLois Curfman McInnes 
1988c9005455SLois Curfman McInnes    Notes:
19894b27c08aSLois Curfman McInnes    If set, this array will contain the function norms computed
1990c9005455SLois Curfman McInnes    at each step.
1991c9005455SLois Curfman McInnes 
1992c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
1993c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
1994c9005455SLois Curfman McInnes    during the section of code that is being timed.
1995c9005455SLois Curfman McInnes 
199636851e7fSLois Curfman McInnes    Level: intermediate
199736851e7fSLois Curfman McInnes 
1998c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
1999758f92a0SBarry Smith 
200008405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
2001758f92a0SBarry Smith 
2002c9005455SLois Curfman McInnes @*/
2003a562a398SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscTruth reset)
2004c9005455SLois Curfman McInnes {
20053a40ed3dSBarry Smith   PetscFunctionBegin;
20064482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
20074482741eSBarry Smith   if (na)  PetscValidScalarPointer(a,2);
2008a562a398SLisandro Dalcin   if (its) PetscValidIntPointer(its,3);
2009c9005455SLois Curfman McInnes   snes->conv_hist       = a;
2010758f92a0SBarry Smith   snes->conv_hist_its   = its;
2011758f92a0SBarry Smith   snes->conv_hist_max   = na;
2012a12bc48fSLisandro Dalcin   snes->conv_hist_len   = 0;
2013758f92a0SBarry Smith   snes->conv_hist_reset = reset;
2014758f92a0SBarry Smith   PetscFunctionReturn(0);
2015758f92a0SBarry Smith }
2016758f92a0SBarry Smith 
20174a2ae208SSatish Balay #undef __FUNCT__
20184a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
20190c4c9dddSBarry Smith /*@C
2020758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
2021758f92a0SBarry Smith 
2022758f92a0SBarry Smith    Collective on SNES
2023758f92a0SBarry Smith 
2024758f92a0SBarry Smith    Input Parameter:
2025758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
2026758f92a0SBarry Smith 
2027758f92a0SBarry Smith    Output Parameters:
2028758f92a0SBarry Smith .  a   - array to hold history
2029758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
2030758f92a0SBarry Smith          negative if not converged) for each solve.
2031758f92a0SBarry Smith -  na  - size of a and its
2032758f92a0SBarry Smith 
2033758f92a0SBarry Smith    Notes:
2034758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
2035758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
2036758f92a0SBarry Smith 
2037758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
2038758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
2039758f92a0SBarry Smith    during the section of code that is being timed.
2040758f92a0SBarry Smith 
2041758f92a0SBarry Smith    Level: intermediate
2042758f92a0SBarry Smith 
2043758f92a0SBarry Smith .keywords: SNES, get, convergence, history
2044758f92a0SBarry Smith 
2045758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
2046758f92a0SBarry Smith 
2047758f92a0SBarry Smith @*/
204863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na)
2049758f92a0SBarry Smith {
2050758f92a0SBarry Smith   PetscFunctionBegin;
20514482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2052758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
2053758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
2054758f92a0SBarry Smith   if (na)  *na  = snes->conv_hist_len;
20553a40ed3dSBarry Smith   PetscFunctionReturn(0);
2056c9005455SLois Curfman McInnes }
2057c9005455SLois Curfman McInnes 
2058e74ef692SMatthew Knepley #undef __FUNCT__
2059e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
2060ac226902SBarry Smith /*@C
206176b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
20627e4bb74cSBarry Smith   at the beginning o every iteration of the nonlinear solve. Specifically
20637e4bb74cSBarry Smith   it is called just before the Jacobian is "evaluated".
206476b2cf59SMatthew Knepley 
206576b2cf59SMatthew Knepley   Collective on SNES
206676b2cf59SMatthew Knepley 
206776b2cf59SMatthew Knepley   Input Parameters:
206876b2cf59SMatthew Knepley . snes - The nonlinear solver context
206976b2cf59SMatthew Knepley . func - The function
207076b2cf59SMatthew Knepley 
207176b2cf59SMatthew Knepley   Calling sequence of func:
2072b5d30489SBarry Smith . func (SNES snes, PetscInt step);
207376b2cf59SMatthew Knepley 
207476b2cf59SMatthew Knepley . step - The current step of the iteration
207576b2cf59SMatthew Knepley 
207676b2cf59SMatthew Knepley   Level: intermediate
207776b2cf59SMatthew Knepley 
207876b2cf59SMatthew Knepley .keywords: SNES, update
2079b5d30489SBarry Smith 
208085385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve()
208176b2cf59SMatthew Knepley @*/
208263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt))
208376b2cf59SMatthew Knepley {
208476b2cf59SMatthew Knepley   PetscFunctionBegin;
20854482741eSBarry Smith   PetscValidHeaderSpecific(snes, SNES_COOKIE,1);
2086e7788613SBarry Smith   snes->ops->update = func;
208776b2cf59SMatthew Knepley   PetscFunctionReturn(0);
208876b2cf59SMatthew Knepley }
208976b2cf59SMatthew Knepley 
2090e74ef692SMatthew Knepley #undef __FUNCT__
2091e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
209276b2cf59SMatthew Knepley /*@
209376b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
209476b2cf59SMatthew Knepley 
209576b2cf59SMatthew Knepley   Not collective
209676b2cf59SMatthew Knepley 
209776b2cf59SMatthew Knepley   Input Parameters:
209876b2cf59SMatthew Knepley . snes - The nonlinear solver context
209976b2cf59SMatthew Knepley . step - The current step of the iteration
210076b2cf59SMatthew Knepley 
2101205452f4SMatthew Knepley   Level: intermediate
2102205452f4SMatthew Knepley 
210376b2cf59SMatthew Knepley .keywords: SNES, update
2104a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC()
210576b2cf59SMatthew Knepley @*/
210663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultUpdate(SNES snes, PetscInt step)
210776b2cf59SMatthew Knepley {
210876b2cf59SMatthew Knepley   PetscFunctionBegin;
210976b2cf59SMatthew Knepley   PetscFunctionReturn(0);
211076b2cf59SMatthew Knepley }
211176b2cf59SMatthew Knepley 
21124a2ae208SSatish Balay #undef __FUNCT__
21134a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
21149b94acceSBarry Smith /*
21159b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
21169b94acceSBarry Smith    positive parameter delta.
21179b94acceSBarry Smith 
21189b94acceSBarry Smith     Input Parameters:
2119c7afd0dbSLois Curfman McInnes +   snes - the SNES context
21209b94acceSBarry Smith .   y - approximate solution of linear system
21219b94acceSBarry Smith .   fnorm - 2-norm of current function
2122c7afd0dbSLois Curfman McInnes -   delta - trust region size
21239b94acceSBarry Smith 
21249b94acceSBarry Smith     Output Parameters:
2125c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
21269b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
21279b94acceSBarry Smith     region, and exceeds zero otherwise.
2128c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
21299b94acceSBarry Smith 
21309b94acceSBarry Smith     Note:
21314b27c08aSLois Curfman McInnes     For non-trust region methods such as SNESLS, the parameter delta
21329b94acceSBarry Smith     is set to be the maximum allowable step size.
21339b94acceSBarry Smith 
21349b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
21359b94acceSBarry Smith */
2136dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
21379b94acceSBarry Smith {
2138064f8208SBarry Smith   PetscReal      nrm;
2139ea709b57SSatish Balay   PetscScalar    cnorm;
2140dfbe8321SBarry Smith   PetscErrorCode ierr;
21413a40ed3dSBarry Smith 
21423a40ed3dSBarry Smith   PetscFunctionBegin;
21434482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
21444482741eSBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE,2);
2145c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,2);
2146184914b5SBarry Smith 
2147064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
2148064f8208SBarry Smith   if (nrm > *delta) {
2149064f8208SBarry Smith      nrm = *delta/nrm;
2150064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
2151064f8208SBarry Smith      cnorm = nrm;
21522dcb1b2aSMatthew Knepley      ierr = VecScale(y,cnorm);CHKERRQ(ierr);
21539b94acceSBarry Smith      *ynorm = *delta;
21549b94acceSBarry Smith   } else {
21559b94acceSBarry Smith      *gpnorm = 0.0;
2156064f8208SBarry Smith      *ynorm = nrm;
21579b94acceSBarry Smith   }
21583a40ed3dSBarry Smith   PetscFunctionReturn(0);
21599b94acceSBarry Smith }
21609b94acceSBarry Smith 
21614a2ae208SSatish Balay #undef __FUNCT__
21624a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
21636ce558aeSBarry Smith /*@C
2164f69a0ea3SMatthew Knepley    SNESSolve - Solves a nonlinear system F(x) = b.
2165f69a0ea3SMatthew Knepley    Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX().
21669b94acceSBarry Smith 
2167c7afd0dbSLois Curfman McInnes    Collective on SNES
2168c7afd0dbSLois Curfman McInnes 
2169b2002411SLois Curfman McInnes    Input Parameters:
2170c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2171f69a0ea3SMatthew Knepley .  b - the constant part of the equation, or PETSC_NULL to use zero.
217285385478SLisandro Dalcin -  x - the solution vector.
21739b94acceSBarry Smith 
2174b2002411SLois Curfman McInnes    Notes:
21758ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
21768ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
21778ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
21788ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
21798ddd3da0SLois Curfman McInnes 
218036851e7fSLois Curfman McInnes    Level: beginner
218136851e7fSLois Curfman McInnes 
21829b94acceSBarry Smith .keywords: SNES, nonlinear, solve
21839b94acceSBarry Smith 
218485385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian()
21859b94acceSBarry Smith @*/
2186f69a0ea3SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSolve(SNES snes,Vec b,Vec x)
21879b94acceSBarry Smith {
2188dfbe8321SBarry Smith   PetscErrorCode ierr;
2189f1af5d2fSBarry Smith   PetscTruth     flg;
2190eabae89aSBarry Smith   char           filename[PETSC_MAX_PATH_LEN];
2191eabae89aSBarry Smith   PetscViewer    viewer;
2192052efed2SBarry Smith 
21933a40ed3dSBarry Smith   PetscFunctionBegin;
21944482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2195f69a0ea3SMatthew Knepley   PetscValidHeaderSpecific(x,VEC_COOKIE,3);
2196f69a0ea3SMatthew Knepley   PetscCheckSameComm(snes,1,x,3);
219785385478SLisandro Dalcin   if (b) PetscValidHeaderSpecific(b,VEC_COOKIE,2);
219885385478SLisandro Dalcin   if (b) PetscCheckSameComm(snes,1,b,2);
219985385478SLisandro Dalcin 
220085385478SLisandro Dalcin   /* set solution vector */
220185385478SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);
220285385478SLisandro Dalcin   if (snes->vec_sol) { ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr); }
220385385478SLisandro Dalcin   snes->vec_sol = x;
220485385478SLisandro Dalcin   /* set afine vector if provided */
220585385478SLisandro Dalcin   if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); }
220685385478SLisandro Dalcin   if (snes->vec_rhs) { ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr); }
220785385478SLisandro Dalcin   snes->vec_rhs = b;
220885385478SLisandro Dalcin 
220985385478SLisandro Dalcin   if (!snes->vec_func && snes->vec_rhs) {
221085385478SLisandro Dalcin     ierr = VecDuplicate(b, &snes->vec_func); CHKERRQ(ierr);
221170e92668SMatthew Knepley   }
22123f149594SLisandro Dalcin 
221370e92668SMatthew Knepley   ierr = SNESSetUp(snes);CHKERRQ(ierr);
22143f149594SLisandro Dalcin 
2215abc0a331SBarry Smith   if (snes->conv_hist_reset) snes->conv_hist_len = 0;
221650ffb88aSMatthew Knepley   snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
2217d5e45103SBarry Smith 
22183f149594SLisandro Dalcin   ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
22194936397dSBarry Smith   ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr);
222085385478SLisandro Dalcin   ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
22214936397dSBarry Smith   if (snes->domainerror){
22224936397dSBarry Smith     snes->reason      = SNES_DIVERGED_FUNCTION_DOMAIN;
22234936397dSBarry Smith     snes->domainerror = PETSC_FALSE;
22244936397dSBarry Smith   }
222585385478SLisandro Dalcin 
22263f149594SLisandro Dalcin   if (!snes->reason) {
22273f149594SLisandro Dalcin     SETERRQ(PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason");
22283f149594SLisandro Dalcin   }
22293f149594SLisandro Dalcin 
22307adad957SLisandro Dalcin   ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
2231eabae89aSBarry Smith   if (flg && !PetscPreLoadingOn) {
22327adad957SLisandro Dalcin     ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr);
2233eabae89aSBarry Smith     ierr = SNESView(snes,viewer);CHKERRQ(ierr);
2234eabae89aSBarry Smith     ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr);
2235eabae89aSBarry Smith   }
2236eabae89aSBarry Smith 
22377adad957SLisandro Dalcin   ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg);CHKERRQ(ierr);
2238da9b6338SBarry Smith   if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); }
22395968eb51SBarry Smith   if (snes->printreason) {
22405968eb51SBarry Smith     if (snes->reason > 0) {
22417adad957SLisandro Dalcin       ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
22425968eb51SBarry Smith     } else {
22437adad957SLisandro Dalcin       ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
22445968eb51SBarry Smith     }
22455968eb51SBarry Smith   }
22465968eb51SBarry Smith 
22473a40ed3dSBarry Smith   PetscFunctionReturn(0);
22489b94acceSBarry Smith }
22499b94acceSBarry Smith 
22509b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
22519b94acceSBarry Smith 
22524a2ae208SSatish Balay #undef __FUNCT__
22534a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
225482bf6240SBarry Smith /*@C
22554b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
22569b94acceSBarry Smith 
2257fee21e36SBarry Smith    Collective on SNES
2258fee21e36SBarry Smith 
2259c7afd0dbSLois Curfman McInnes    Input Parameters:
2260c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2261454a90a3SBarry Smith -  type - a known method
2262c7afd0dbSLois Curfman McInnes 
2263c7afd0dbSLois Curfman McInnes    Options Database Key:
2264454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
2265c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
2266ae12b187SLois Curfman McInnes 
22679b94acceSBarry Smith    Notes:
2268e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
22694b27c08aSLois Curfman McInnes +    SNESLS - Newton's method with line search
2270c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
22714b27c08aSLois Curfman McInnes .    SNESTR - Newton's method with trust region
2272c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
22739b94acceSBarry Smith 
2274ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
2275ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
2276ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
2277ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
2278ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
2279ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
2280ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
2281ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
2282ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
2283b0a32e0cSBarry Smith   appropriate method.
228436851e7fSLois Curfman McInnes 
228536851e7fSLois Curfman McInnes   Level: intermediate
2286a703fe33SLois Curfman McInnes 
2287454a90a3SBarry Smith .keywords: SNES, set, type
2288435da068SBarry Smith 
2289435da068SBarry Smith .seealso: SNESType, SNESCreate()
2290435da068SBarry Smith 
22919b94acceSBarry Smith @*/
2292a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetType(SNES snes,const SNESType type)
22939b94acceSBarry Smith {
2294dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(SNES);
22956831982aSBarry Smith   PetscTruth     match;
22963a40ed3dSBarry Smith 
22973a40ed3dSBarry Smith   PetscFunctionBegin;
22984482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
22994482741eSBarry Smith   PetscValidCharPointer(type,2);
230082bf6240SBarry Smith 
23016831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
23020f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
230392ff6ae8SBarry Smith 
23047adad957SLisandro Dalcin   ierr =  PetscFListFind(SNESList,((PetscObject)snes)->comm,type,(void (**)(void)) &r);CHKERRQ(ierr);
2305958c9bccSBarry Smith   if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type);
230675396ef9SLisandro Dalcin   /* Destroy the previous private SNES context */
230775396ef9SLisandro Dalcin   if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); }
230875396ef9SLisandro Dalcin   /* Reinitialize function pointers in SNESOps structure */
230975396ef9SLisandro Dalcin   snes->ops->setup          = 0;
231075396ef9SLisandro Dalcin   snes->ops->solve          = 0;
231175396ef9SLisandro Dalcin   snes->ops->view           = 0;
231275396ef9SLisandro Dalcin   snes->ops->setfromoptions = 0;
231375396ef9SLisandro Dalcin   snes->ops->destroy        = 0;
231475396ef9SLisandro Dalcin   /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */
231575396ef9SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
23163a40ed3dSBarry Smith   ierr = (*r)(snes);CHKERRQ(ierr);
2317454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
23183a40ed3dSBarry Smith   PetscFunctionReturn(0);
23199b94acceSBarry Smith }
23209b94acceSBarry Smith 
2321a847f771SSatish Balay 
23229b94acceSBarry Smith /* --------------------------------------------------------------------- */
23234a2ae208SSatish Balay #undef __FUNCT__
23244a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
232552baeb72SSatish Balay /*@
23269b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
2327f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
23289b94acceSBarry Smith 
2329fee21e36SBarry Smith    Not Collective
2330fee21e36SBarry Smith 
233136851e7fSLois Curfman McInnes    Level: advanced
233236851e7fSLois Curfman McInnes 
23339b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
23349b94acceSBarry Smith 
23359b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
23369b94acceSBarry Smith @*/
233763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegisterDestroy(void)
23389b94acceSBarry Smith {
2339dfbe8321SBarry Smith   PetscErrorCode ierr;
234082bf6240SBarry Smith 
23413a40ed3dSBarry Smith   PetscFunctionBegin;
23421441b1d3SBarry Smith   ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
23434c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
23443a40ed3dSBarry Smith   PetscFunctionReturn(0);
23459b94acceSBarry Smith }
23469b94acceSBarry Smith 
23474a2ae208SSatish Balay #undef __FUNCT__
23484a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
23499b94acceSBarry Smith /*@C
23509a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
23519b94acceSBarry Smith 
2352c7afd0dbSLois Curfman McInnes    Not Collective
2353c7afd0dbSLois Curfman McInnes 
23549b94acceSBarry Smith    Input Parameter:
23554b0e389bSBarry Smith .  snes - nonlinear solver context
23569b94acceSBarry Smith 
23579b94acceSBarry Smith    Output Parameter:
23583a7fca6bSBarry Smith .  type - SNES method (a character string)
23599b94acceSBarry Smith 
236036851e7fSLois Curfman McInnes    Level: intermediate
236136851e7fSLois Curfman McInnes 
2362454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
23639b94acceSBarry Smith @*/
2364a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetType(SNES snes,const SNESType *type)
23659b94acceSBarry Smith {
23663a40ed3dSBarry Smith   PetscFunctionBegin;
23674482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
23684482741eSBarry Smith   PetscValidPointer(type,2);
23697adad957SLisandro Dalcin   *type = ((PetscObject)snes)->type_name;
23703a40ed3dSBarry Smith   PetscFunctionReturn(0);
23719b94acceSBarry Smith }
23729b94acceSBarry Smith 
23734a2ae208SSatish Balay #undef __FUNCT__
23744a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
237552baeb72SSatish Balay /*@
23769b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
23779b94acceSBarry Smith    stored.
23789b94acceSBarry Smith 
2379c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2380c7afd0dbSLois Curfman McInnes 
23819b94acceSBarry Smith    Input Parameter:
23829b94acceSBarry Smith .  snes - the SNES context
23839b94acceSBarry Smith 
23849b94acceSBarry Smith    Output Parameter:
23859b94acceSBarry Smith .  x - the solution
23869b94acceSBarry Smith 
238770e92668SMatthew Knepley    Level: intermediate
238836851e7fSLois Curfman McInnes 
23899b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
23909b94acceSBarry Smith 
239185385478SLisandro Dalcin .seealso:  SNESGetSolutionUpdate(), SNESGetFunction()
23929b94acceSBarry Smith @*/
239363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolution(SNES snes,Vec *x)
23949b94acceSBarry Smith {
23953a40ed3dSBarry Smith   PetscFunctionBegin;
23964482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
23974482741eSBarry Smith   PetscValidPointer(x,2);
239885385478SLisandro Dalcin   *x = snes->vec_sol;
239970e92668SMatthew Knepley   PetscFunctionReturn(0);
240070e92668SMatthew Knepley }
240170e92668SMatthew Knepley 
240270e92668SMatthew Knepley #undef __FUNCT__
24034a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
240452baeb72SSatish Balay /*@
24059b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
24069b94acceSBarry Smith    stored.
24079b94acceSBarry Smith 
2408c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2409c7afd0dbSLois Curfman McInnes 
24109b94acceSBarry Smith    Input Parameter:
24119b94acceSBarry Smith .  snes - the SNES context
24129b94acceSBarry Smith 
24139b94acceSBarry Smith    Output Parameter:
24149b94acceSBarry Smith .  x - the solution update
24159b94acceSBarry Smith 
241636851e7fSLois Curfman McInnes    Level: advanced
241736851e7fSLois Curfman McInnes 
24189b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
24199b94acceSBarry Smith 
242085385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction()
24219b94acceSBarry Smith @*/
242263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolutionUpdate(SNES snes,Vec *x)
24239b94acceSBarry Smith {
24243a40ed3dSBarry Smith   PetscFunctionBegin;
24254482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
24264482741eSBarry Smith   PetscValidPointer(x,2);
242785385478SLisandro Dalcin   *x = snes->vec_sol_update;
24283a40ed3dSBarry Smith   PetscFunctionReturn(0);
24299b94acceSBarry Smith }
24309b94acceSBarry Smith 
24314a2ae208SSatish Balay #undef __FUNCT__
24324a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
24339b94acceSBarry Smith /*@C
24343638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
24359b94acceSBarry Smith 
2436c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2437c7afd0dbSLois Curfman McInnes 
24389b94acceSBarry Smith    Input Parameter:
24399b94acceSBarry Smith .  snes - the SNES context
24409b94acceSBarry Smith 
24419b94acceSBarry Smith    Output Parameter:
24427bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
244370e92668SMatthew Knepley .  func - the function (or PETSC_NULL)
244470e92668SMatthew Knepley -  ctx - the function context (or PETSC_NULL)
24459b94acceSBarry Smith 
244636851e7fSLois Curfman McInnes    Level: advanced
244736851e7fSLois Curfman McInnes 
2448a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
24499b94acceSBarry Smith 
24504b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution()
24519b94acceSBarry Smith @*/
245270e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx)
24539b94acceSBarry Smith {
24543a40ed3dSBarry Smith   PetscFunctionBegin;
24554482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
245685385478SLisandro Dalcin   if (r)    *r    = snes->vec_func;
2457e7788613SBarry Smith   if (func) *func = snes->ops->computefunction;
245870e92668SMatthew Knepley   if (ctx)  *ctx  = snes->funP;
24593a40ed3dSBarry Smith   PetscFunctionReturn(0);
24609b94acceSBarry Smith }
24619b94acceSBarry Smith 
24624a2ae208SSatish Balay #undef __FUNCT__
24634a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
24643c7409f5SSatish Balay /*@C
24653c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2466d850072dSLois Curfman McInnes    SNES options in the database.
24673c7409f5SSatish Balay 
2468fee21e36SBarry Smith    Collective on SNES
2469fee21e36SBarry Smith 
2470c7afd0dbSLois Curfman McInnes    Input Parameter:
2471c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2472c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2473c7afd0dbSLois Curfman McInnes 
2474d850072dSLois Curfman McInnes    Notes:
2475a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2476c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2477d850072dSLois Curfman McInnes 
247836851e7fSLois Curfman McInnes    Level: advanced
247936851e7fSLois Curfman McInnes 
24803c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2481a86d99e1SLois Curfman McInnes 
2482a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
24833c7409f5SSatish Balay @*/
248463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetOptionsPrefix(SNES snes,const char prefix[])
24853c7409f5SSatish Balay {
2486dfbe8321SBarry Smith   PetscErrorCode ierr;
24873c7409f5SSatish Balay 
24883a40ed3dSBarry Smith   PetscFunctionBegin;
24894482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2490639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
24911cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
249294b7f48cSBarry Smith   ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
24933a40ed3dSBarry Smith   PetscFunctionReturn(0);
24943c7409f5SSatish Balay }
24953c7409f5SSatish Balay 
24964a2ae208SSatish Balay #undef __FUNCT__
24974a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
24983c7409f5SSatish Balay /*@C
2499f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2500d850072dSLois Curfman McInnes    SNES options in the database.
25013c7409f5SSatish Balay 
2502fee21e36SBarry Smith    Collective on SNES
2503fee21e36SBarry Smith 
2504c7afd0dbSLois Curfman McInnes    Input Parameters:
2505c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2506c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2507c7afd0dbSLois Curfman McInnes 
2508d850072dSLois Curfman McInnes    Notes:
2509a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2510c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2511d850072dSLois Curfman McInnes 
251236851e7fSLois Curfman McInnes    Level: advanced
251336851e7fSLois Curfman McInnes 
25143c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2515a86d99e1SLois Curfman McInnes 
2516a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
25173c7409f5SSatish Balay @*/
251863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAppendOptionsPrefix(SNES snes,const char prefix[])
25193c7409f5SSatish Balay {
2520dfbe8321SBarry Smith   PetscErrorCode ierr;
25213c7409f5SSatish Balay 
25223a40ed3dSBarry Smith   PetscFunctionBegin;
25234482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2524639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
25251cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
252694b7f48cSBarry Smith   ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
25273a40ed3dSBarry Smith   PetscFunctionReturn(0);
25283c7409f5SSatish Balay }
25293c7409f5SSatish Balay 
25304a2ae208SSatish Balay #undef __FUNCT__
25314a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
25329ab63eb5SSatish Balay /*@C
25333c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
25343c7409f5SSatish Balay    SNES options in the database.
25353c7409f5SSatish Balay 
2536c7afd0dbSLois Curfman McInnes    Not Collective
2537c7afd0dbSLois Curfman McInnes 
25383c7409f5SSatish Balay    Input Parameter:
25393c7409f5SSatish Balay .  snes - the SNES context
25403c7409f5SSatish Balay 
25413c7409f5SSatish Balay    Output Parameter:
25423c7409f5SSatish Balay .  prefix - pointer to the prefix string used
25433c7409f5SSatish Balay 
25444ef407dbSRichard Tran Mills    Notes: On the fortran side, the user should pass in a string 'prefix' of
25459ab63eb5SSatish Balay    sufficient length to hold the prefix.
25469ab63eb5SSatish Balay 
254736851e7fSLois Curfman McInnes    Level: advanced
254836851e7fSLois Curfman McInnes 
25493c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2550a86d99e1SLois Curfman McInnes 
2551a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
25523c7409f5SSatish Balay @*/
2553e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetOptionsPrefix(SNES snes,const char *prefix[])
25543c7409f5SSatish Balay {
2555dfbe8321SBarry Smith   PetscErrorCode ierr;
25563c7409f5SSatish Balay 
25573a40ed3dSBarry Smith   PetscFunctionBegin;
25584482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2559639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
25603a40ed3dSBarry Smith   PetscFunctionReturn(0);
25613c7409f5SSatish Balay }
25623c7409f5SSatish Balay 
2563b2002411SLois Curfman McInnes 
25644a2ae208SSatish Balay #undef __FUNCT__
25654a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
25663cea93caSBarry Smith /*@C
25673cea93caSBarry Smith   SNESRegister - See SNESRegisterDynamic()
25683cea93caSBarry Smith 
25697f6c08e0SMatthew Knepley   Level: advanced
25703cea93caSBarry Smith @*/
257163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES))
2572b2002411SLois Curfman McInnes {
2573e2d1d2b7SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
2574dfbe8321SBarry Smith   PetscErrorCode ierr;
2575b2002411SLois Curfman McInnes 
2576b2002411SLois Curfman McInnes   PetscFunctionBegin;
2577b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2578c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
2579b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2580b2002411SLois Curfman McInnes }
2581da9b6338SBarry Smith 
2582da9b6338SBarry Smith #undef __FUNCT__
2583da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin"
258463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESTestLocalMin(SNES snes)
2585da9b6338SBarry Smith {
2586dfbe8321SBarry Smith   PetscErrorCode ierr;
258777431f27SBarry Smith   PetscInt       N,i,j;
2588da9b6338SBarry Smith   Vec            u,uh,fh;
2589da9b6338SBarry Smith   PetscScalar    value;
2590da9b6338SBarry Smith   PetscReal      norm;
2591da9b6338SBarry Smith 
2592da9b6338SBarry Smith   PetscFunctionBegin;
2593da9b6338SBarry Smith   ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr);
2594da9b6338SBarry Smith   ierr = VecDuplicate(u,&uh);CHKERRQ(ierr);
2595da9b6338SBarry Smith   ierr = VecDuplicate(u,&fh);CHKERRQ(ierr);
2596da9b6338SBarry Smith 
2597da9b6338SBarry Smith   /* currently only works for sequential */
2598da9b6338SBarry Smith   ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n");
2599da9b6338SBarry Smith   ierr = VecGetSize(u,&N);CHKERRQ(ierr);
2600da9b6338SBarry Smith   for (i=0; i<N; i++) {
2601da9b6338SBarry Smith     ierr = VecCopy(u,uh);CHKERRQ(ierr);
260277431f27SBarry Smith     ierr  = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr);
2603da9b6338SBarry Smith     for (j=-10; j<11; j++) {
2604ccae9161SBarry Smith       value = PetscSign(j)*exp(PetscAbs(j)-10.0);
2605da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
26063ab0aad5SBarry Smith       ierr  = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr);
2607da9b6338SBarry Smith       ierr  = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr);
260877431f27SBarry Smith       ierr  = PetscPrintf(PETSC_COMM_WORLD,"       j norm %D %18.16e\n",j,norm);CHKERRQ(ierr);
2609da9b6338SBarry Smith       value = -value;
2610da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
2611da9b6338SBarry Smith     }
2612da9b6338SBarry Smith   }
2613da9b6338SBarry Smith   ierr = VecDestroy(uh);CHKERRQ(ierr);
2614da9b6338SBarry Smith   ierr = VecDestroy(fh);CHKERRQ(ierr);
2615da9b6338SBarry Smith   PetscFunctionReturn(0);
2616da9b6338SBarry Smith }
261771f87433Sdalcinl 
261871f87433Sdalcinl #undef __FUNCT__
2619fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW"
262071f87433Sdalcinl /*@
2621fa9f3622SBarry Smith    SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for
262271f87433Sdalcinl    computing relative tolerance for linear solvers within an inexact
262371f87433Sdalcinl    Newton method.
262471f87433Sdalcinl 
262571f87433Sdalcinl    Collective on SNES
262671f87433Sdalcinl 
262771f87433Sdalcinl    Input Parameters:
262871f87433Sdalcinl +  snes - SNES context
262971f87433Sdalcinl -  flag - PETSC_TRUE or PETSC_FALSE
263071f87433Sdalcinl 
263171f87433Sdalcinl    Notes:
263271f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
263371f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
263471f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
263571f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
263671f87433Sdalcinl    solver.
263771f87433Sdalcinl 
263871f87433Sdalcinl    Level: advanced
263971f87433Sdalcinl 
264071f87433Sdalcinl    Reference:
264171f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
264271f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
264371f87433Sdalcinl 
264471f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
264571f87433Sdalcinl 
2646fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
264771f87433Sdalcinl @*/
2648fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetUseEW(SNES snes,PetscTruth flag)
264971f87433Sdalcinl {
265071f87433Sdalcinl   PetscFunctionBegin;
265171f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
265271f87433Sdalcinl   snes->ksp_ewconv = flag;
265371f87433Sdalcinl   PetscFunctionReturn(0);
265471f87433Sdalcinl }
265571f87433Sdalcinl 
265671f87433Sdalcinl #undef __FUNCT__
2657fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW"
265871f87433Sdalcinl /*@
2659fa9f3622SBarry Smith    SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method
266071f87433Sdalcinl    for computing relative tolerance for linear solvers within an
266171f87433Sdalcinl    inexact Newton method.
266271f87433Sdalcinl 
266371f87433Sdalcinl    Not Collective
266471f87433Sdalcinl 
266571f87433Sdalcinl    Input Parameter:
266671f87433Sdalcinl .  snes - SNES context
266771f87433Sdalcinl 
266871f87433Sdalcinl    Output Parameter:
266971f87433Sdalcinl .  flag - PETSC_TRUE or PETSC_FALSE
267071f87433Sdalcinl 
267171f87433Sdalcinl    Notes:
267271f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
267371f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
267471f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
267571f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
267671f87433Sdalcinl    solver.
267771f87433Sdalcinl 
267871f87433Sdalcinl    Level: advanced
267971f87433Sdalcinl 
268071f87433Sdalcinl    Reference:
268171f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
268271f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
268371f87433Sdalcinl 
268471f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
268571f87433Sdalcinl 
2686fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
268771f87433Sdalcinl @*/
2688fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetUseEW(SNES snes, PetscTruth *flag)
268971f87433Sdalcinl {
269071f87433Sdalcinl   PetscFunctionBegin;
269171f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
269271f87433Sdalcinl   PetscValidPointer(flag,2);
269371f87433Sdalcinl   *flag = snes->ksp_ewconv;
269471f87433Sdalcinl   PetscFunctionReturn(0);
269571f87433Sdalcinl }
269671f87433Sdalcinl 
269771f87433Sdalcinl #undef __FUNCT__
2698fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW"
269971f87433Sdalcinl /*@
2700fa9f3622SBarry Smith    SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker
270171f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
270271f87433Sdalcinl    Newton method.
270371f87433Sdalcinl 
270471f87433Sdalcinl    Collective on SNES
270571f87433Sdalcinl 
270671f87433Sdalcinl    Input Parameters:
270771f87433Sdalcinl +    snes - SNES context
270871f87433Sdalcinl .    version - version 1, 2 (default is 2) or 3
270971f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
271071f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
271171f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
271271f87433Sdalcinl              (0 <= gamma2 <= 1)
271371f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
271471f87433Sdalcinl .    alpha2 - power for safeguard
271571f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
271671f87433Sdalcinl 
271771f87433Sdalcinl    Note:
271871f87433Sdalcinl    Version 3 was contributed by Luis Chacon, June 2006.
271971f87433Sdalcinl 
272071f87433Sdalcinl    Use PETSC_DEFAULT to retain the default for any of the parameters.
272171f87433Sdalcinl 
272271f87433Sdalcinl    Level: advanced
272371f87433Sdalcinl 
272471f87433Sdalcinl    Reference:
272571f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
272671f87433Sdalcinl    inexact Newton method", Utah State University Math. Stat. Dept. Res.
272771f87433Sdalcinl    Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput.
272871f87433Sdalcinl 
272971f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters
273071f87433Sdalcinl 
2731fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW()
273271f87433Sdalcinl @*/
2733fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max,
273471f87433Sdalcinl 							    PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold)
273571f87433Sdalcinl {
2736fa9f3622SBarry Smith   SNESKSPEW *kctx;
273771f87433Sdalcinl   PetscFunctionBegin;
273871f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2739fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
274071f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
274171f87433Sdalcinl 
274271f87433Sdalcinl   if (version != PETSC_DEFAULT)   kctx->version   = version;
274371f87433Sdalcinl   if (rtol_0 != PETSC_DEFAULT)    kctx->rtol_0    = rtol_0;
274471f87433Sdalcinl   if (rtol_max != PETSC_DEFAULT)  kctx->rtol_max  = rtol_max;
274571f87433Sdalcinl   if (gamma != PETSC_DEFAULT)     kctx->gamma     = gamma;
274671f87433Sdalcinl   if (alpha != PETSC_DEFAULT)     kctx->alpha     = alpha;
274771f87433Sdalcinl   if (alpha2 != PETSC_DEFAULT)    kctx->alpha2    = alpha2;
274871f87433Sdalcinl   if (threshold != PETSC_DEFAULT) kctx->threshold = threshold;
274971f87433Sdalcinl 
275071f87433Sdalcinl   if (kctx->version < 1 || kctx->version > 3) {
275171f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version);
275271f87433Sdalcinl   }
275371f87433Sdalcinl   if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) {
275471f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0);
275571f87433Sdalcinl   }
275671f87433Sdalcinl   if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) {
275771f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max);
275871f87433Sdalcinl   }
275971f87433Sdalcinl   if (kctx->gamma < 0.0 || kctx->gamma > 1.0) {
276071f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma);
276171f87433Sdalcinl   }
276271f87433Sdalcinl   if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) {
276371f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha);
276471f87433Sdalcinl   }
276571f87433Sdalcinl   if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) {
276671f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold);
276771f87433Sdalcinl   }
276871f87433Sdalcinl   PetscFunctionReturn(0);
276971f87433Sdalcinl }
277071f87433Sdalcinl 
277171f87433Sdalcinl #undef __FUNCT__
2772fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW"
277371f87433Sdalcinl /*@
2774fa9f3622SBarry Smith    SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker
277571f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
277671f87433Sdalcinl    Newton method.
277771f87433Sdalcinl 
277871f87433Sdalcinl    Not Collective
277971f87433Sdalcinl 
278071f87433Sdalcinl    Input Parameters:
278171f87433Sdalcinl      snes - SNES context
278271f87433Sdalcinl 
278371f87433Sdalcinl    Output Parameters:
278471f87433Sdalcinl +    version - version 1, 2 (default is 2) or 3
278571f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
278671f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
278771f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
278871f87433Sdalcinl              (0 <= gamma2 <= 1)
278971f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
279071f87433Sdalcinl .    alpha2 - power for safeguard
279171f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
279271f87433Sdalcinl 
279371f87433Sdalcinl    Level: advanced
279471f87433Sdalcinl 
279571f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters
279671f87433Sdalcinl 
2797fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW()
279871f87433Sdalcinl @*/
2799fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max,
280071f87433Sdalcinl 							    PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold)
280171f87433Sdalcinl {
2802fa9f3622SBarry Smith   SNESKSPEW *kctx;
280371f87433Sdalcinl   PetscFunctionBegin;
280471f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2805fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
280671f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
280771f87433Sdalcinl   if(version)   *version   = kctx->version;
280871f87433Sdalcinl   if(rtol_0)    *rtol_0    = kctx->rtol_0;
280971f87433Sdalcinl   if(rtol_max)  *rtol_max  = kctx->rtol_max;
281071f87433Sdalcinl   if(gamma)     *gamma     = kctx->gamma;
281171f87433Sdalcinl   if(alpha)     *alpha     = kctx->alpha;
281271f87433Sdalcinl   if(alpha2)    *alpha2    = kctx->alpha2;
281371f87433Sdalcinl   if(threshold) *threshold = kctx->threshold;
281471f87433Sdalcinl   PetscFunctionReturn(0);
281571f87433Sdalcinl }
281671f87433Sdalcinl 
281771f87433Sdalcinl #undef __FUNCT__
2818fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve"
2819fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x)
282071f87433Sdalcinl {
282171f87433Sdalcinl   PetscErrorCode ierr;
2822fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
282371f87433Sdalcinl   PetscReal      rtol=PETSC_DEFAULT,stol;
282471f87433Sdalcinl 
282571f87433Sdalcinl   PetscFunctionBegin;
282671f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
282771f87433Sdalcinl   if (!snes->iter) { /* first time in, so use the original user rtol */
282871f87433Sdalcinl     rtol = kctx->rtol_0;
282971f87433Sdalcinl   } else {
283071f87433Sdalcinl     if (kctx->version == 1) {
283171f87433Sdalcinl       rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last;
283271f87433Sdalcinl       if (rtol < 0.0) rtol = -rtol;
283371f87433Sdalcinl       stol = pow(kctx->rtol_last,kctx->alpha2);
283471f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
283571f87433Sdalcinl     } else if (kctx->version == 2) {
283671f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
283771f87433Sdalcinl       stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha);
283871f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
283971f87433Sdalcinl     } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */
284071f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
284171f87433Sdalcinl       /* safeguard: avoid sharp decrease of rtol */
284271f87433Sdalcinl       stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha);
284371f87433Sdalcinl       stol = PetscMax(rtol,stol);
284471f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
284571f87433Sdalcinl       /* safeguard: avoid oversolving */
284671f87433Sdalcinl       stol = kctx->gamma*(snes->ttol)/snes->norm;
284771f87433Sdalcinl       stol = PetscMax(rtol,stol);
284871f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
284971f87433Sdalcinl     } else SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version);
285071f87433Sdalcinl   }
285171f87433Sdalcinl   /* safeguard: avoid rtol greater than one */
285271f87433Sdalcinl   rtol = PetscMin(rtol,kctx->rtol_max);
285371f87433Sdalcinl   ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
285471f87433Sdalcinl   ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr);
285571f87433Sdalcinl   PetscFunctionReturn(0);
285671f87433Sdalcinl }
285771f87433Sdalcinl 
285871f87433Sdalcinl #undef __FUNCT__
2859fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve"
2860fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x)
286171f87433Sdalcinl {
286271f87433Sdalcinl   PetscErrorCode ierr;
2863fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
286471f87433Sdalcinl   PCSide         pcside;
286571f87433Sdalcinl   Vec            lres;
286671f87433Sdalcinl 
286771f87433Sdalcinl   PetscFunctionBegin;
286871f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
286971f87433Sdalcinl   ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr);
287071f87433Sdalcinl   ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr);
287171f87433Sdalcinl   if (kctx->version == 1) {
287271f87433Sdalcinl     ierr = KSPGetPreconditionerSide(ksp,&pcside);CHKERRQ(ierr);
287371f87433Sdalcinl     if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */
287471f87433Sdalcinl       /* KSP residual is true linear residual */
287571f87433Sdalcinl       ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr);
287671f87433Sdalcinl     } else {
287771f87433Sdalcinl       /* KSP residual is preconditioned residual */
287871f87433Sdalcinl       /* compute true linear residual norm */
287971f87433Sdalcinl       ierr = VecDuplicate(b,&lres);CHKERRQ(ierr);
288071f87433Sdalcinl       ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr);
288171f87433Sdalcinl       ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr);
288271f87433Sdalcinl       ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr);
288371f87433Sdalcinl       ierr = VecDestroy(lres);CHKERRQ(ierr);
288471f87433Sdalcinl     }
288571f87433Sdalcinl   }
288671f87433Sdalcinl   PetscFunctionReturn(0);
288771f87433Sdalcinl }
288871f87433Sdalcinl 
288971f87433Sdalcinl #undef __FUNCT__
289071f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve"
289171f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x)
289271f87433Sdalcinl {
289371f87433Sdalcinl   PetscErrorCode ierr;
289471f87433Sdalcinl 
289571f87433Sdalcinl   PetscFunctionBegin;
2896fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr);  }
289771f87433Sdalcinl   ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);
2898fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); }
289971f87433Sdalcinl   PetscFunctionReturn(0);
290071f87433Sdalcinl }
2901