xref: /petsc/src/snes/interface/snes.c (revision b710008a2fdd75e22a1346cc52bafd3cf35f9306)
163dd3a1aSKris Buschelman #define PETSCSNES_DLL
29b94acceSBarry Smith 
37c4f633dSBarry Smith #include "private/snesimpl.h"      /*I "petscsnes.h"  I*/
49b94acceSBarry Smith 
54c49b128SBarry Smith PetscTruth SNESRegisterAllCalled = PETSC_FALSE;
68ba1e511SMatthew Knepley PetscFList SNESList              = PETSC_NULL;
78ba1e511SMatthew Knepley 
88ba1e511SMatthew Knepley /* Logging support */
9166c7f25SBarry Smith PetscCookie PETSCSNES_DLLEXPORT SNES_COOKIE;
10166c7f25SBarry Smith PetscLogEvent  SNES_Solve, SNES_LineSearch, SNES_FunctionEval, SNES_JacobianEval;
11a09944afSBarry Smith 
12a09944afSBarry Smith #undef __FUNCT__
134936397dSBarry Smith #define __FUNCT__ "SNESSetFunctionDomainError"
14e725d27bSBarry Smith /*@
154936397dSBarry Smith    SNESSetFunctionDomainError - tells SNES that the input vector to your FormFunction is not
164936397dSBarry Smith      in the functions domain. For example, negative pressure.
174936397dSBarry Smith 
184936397dSBarry Smith    Collective on SNES
194936397dSBarry Smith 
204936397dSBarry Smith    Input Parameters:
214936397dSBarry Smith .  SNES - the SNES context
224936397dSBarry Smith 
2328529972SSatish Balay    Level: advanced
244936397dSBarry Smith 
254936397dSBarry Smith .keywords: SNES, view
264936397dSBarry Smith 
274936397dSBarry Smith .seealso: SNESCreate(), SNESSetFunction()
284936397dSBarry Smith @*/
294936397dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFunctionDomainError(SNES snes)
304936397dSBarry Smith {
314936397dSBarry Smith   PetscFunctionBegin;
324936397dSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
334936397dSBarry Smith   snes->domainerror = PETSC_TRUE;
344936397dSBarry Smith   PetscFunctionReturn(0);
354936397dSBarry Smith }
364936397dSBarry Smith 
374936397dSBarry Smith #undef __FUNCT__
384a2ae208SSatish Balay #define __FUNCT__ "SNESView"
397e2c5f70SBarry Smith /*@C
409b94acceSBarry Smith    SNESView - Prints the SNES data structure.
419b94acceSBarry Smith 
424c49b128SBarry Smith    Collective on SNES
43fee21e36SBarry Smith 
44c7afd0dbSLois Curfman McInnes    Input Parameters:
45c7afd0dbSLois Curfman McInnes +  SNES - the SNES context
46c7afd0dbSLois Curfman McInnes -  viewer - visualization context
47c7afd0dbSLois Curfman McInnes 
489b94acceSBarry Smith    Options Database Key:
49c8a8ba5cSLois Curfman McInnes .  -snes_view - Calls SNESView() at end of SNESSolve()
509b94acceSBarry Smith 
519b94acceSBarry Smith    Notes:
529b94acceSBarry Smith    The available visualization contexts include
53b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
54b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
55c8a8ba5cSLois Curfman McInnes          output where only the first processor opens
56c8a8ba5cSLois Curfman McInnes          the file.  All other processors send their
57c8a8ba5cSLois Curfman McInnes          data to the first processor to print.
589b94acceSBarry Smith 
593e081fefSLois Curfman McInnes    The user can open an alternative visualization context with
60b0a32e0cSBarry Smith    PetscViewerASCIIOpen() - output to a specified file.
619b94acceSBarry Smith 
6236851e7fSLois Curfman McInnes    Level: beginner
6336851e7fSLois Curfman McInnes 
649b94acceSBarry Smith .keywords: SNES, view
659b94acceSBarry Smith 
66b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
679b94acceSBarry Smith @*/
6863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESView(SNES snes,PetscViewer viewer)
699b94acceSBarry Smith {
70fa9f3622SBarry Smith   SNESKSPEW           *kctx;
71dfbe8321SBarry Smith   PetscErrorCode      ierr;
7294b7f48cSBarry Smith   KSP                 ksp;
73a313700dSBarry Smith   const SNESType      type;
7432077d6dSBarry Smith   PetscTruth          iascii,isstring;
759b94acceSBarry Smith 
763a40ed3dSBarry Smith   PetscFunctionBegin;
774482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
783050cee2SBarry Smith   if (!viewer) {
797adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&viewer);CHKERRQ(ierr);
803050cee2SBarry Smith   }
814482741eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE,2);
82c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,viewer,2);
8374679c65SBarry Smith 
8432077d6dSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
85b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);CHKERRQ(ierr);
8632077d6dSBarry Smith   if (iascii) {
877adad957SLisandro Dalcin     if (((PetscObject)snes)->prefix) {
887adad957SLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:(%s)\n",((PetscObject)snes)->prefix);CHKERRQ(ierr);
893a7fca6bSBarry Smith     } else {
90b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:\n");CHKERRQ(ierr);
913a7fca6bSBarry Smith     }
92454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
93454a90a3SBarry Smith     if (type) {
94b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: %s\n",type);CHKERRQ(ierr);
95184914b5SBarry Smith     } else {
96b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: not set yet\n");CHKERRQ(ierr);
97184914b5SBarry Smith     }
98e7788613SBarry Smith     if (snes->ops->view) {
99b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
100e7788613SBarry Smith       ierr = (*snes->ops->view)(snes,viewer);CHKERRQ(ierr);
101b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1020ef38995SBarry Smith     }
10377431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum iterations=%D, maximum function evaluations=%D\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr);
104a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  tolerances: relative=%G, absolute=%G, solution=%G\n",
10570441072SBarry Smith                  snes->rtol,snes->abstol,snes->xtol);CHKERRQ(ierr);
10677431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",snes->linear_its);CHKERRQ(ierr);
10777431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of function evaluations=%D\n",snes->nfuncs);CHKERRQ(ierr);
1089b94acceSBarry Smith     if (snes->ksp_ewconv) {
109fa9f3622SBarry Smith       kctx = (SNESKSPEW *)snes->kspconvctx;
1109b94acceSBarry Smith       if (kctx) {
11177431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"  Eisenstat-Walker computation of KSP relative tolerance (version %D)\n",kctx->version);CHKERRQ(ierr);
112a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    rtol_0=%G, rtol_max=%G, threshold=%G\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr);
113a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    gamma=%G, alpha=%G, alpha2=%G\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr);
1149b94acceSBarry Smith       }
1159b94acceSBarry Smith     }
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 
230186905e3SBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
231cce0b1b2SLisandro Dalcin   ierr = PetscOptionsBegin(((PetscObject)snes)->comm,((PetscObject)snes)->prefix,"Nonlinear solver (SNES) options","SNES");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 
1335*b710008aSBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes, &snes->ksp);CHKERRQ(ierr);}
1336*b710008aSBarry Smith 
1337410397dcSLisandro Dalcin   if (snes->ops->setup) {
1338410397dcSLisandro Dalcin     ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr);
1339410397dcSLisandro Dalcin   }
13407aaed0d8SBarry Smith   snes->setupcalled = PETSC_TRUE;
13413a40ed3dSBarry Smith   PetscFunctionReturn(0);
13429b94acceSBarry Smith }
13439b94acceSBarry Smith 
13444a2ae208SSatish Balay #undef __FUNCT__
13454a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
134652baeb72SSatish Balay /*@
13479b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
13489b94acceSBarry Smith    with SNESCreate().
13499b94acceSBarry Smith 
1350c7afd0dbSLois Curfman McInnes    Collective on SNES
1351c7afd0dbSLois Curfman McInnes 
13529b94acceSBarry Smith    Input Parameter:
13539b94acceSBarry Smith .  snes - the SNES context
13549b94acceSBarry Smith 
135536851e7fSLois Curfman McInnes    Level: beginner
135636851e7fSLois Curfman McInnes 
13579b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
13589b94acceSBarry Smith 
135963a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
13609b94acceSBarry Smith @*/
136163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDestroy(SNES snes)
13629b94acceSBarry Smith {
13636849ba73SBarry Smith   PetscErrorCode ierr;
13643a40ed3dSBarry Smith 
13653a40ed3dSBarry Smith   PetscFunctionBegin;
13664482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
13677adad957SLisandro Dalcin   if (--((PetscObject)snes)->refct > 0) PetscFunctionReturn(0);
1368d4bb536fSBarry Smith 
1369be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
13700f5bd95cSBarry Smith   ierr = PetscObjectDepublish(snes);CHKERRQ(ierr);
1371e7788613SBarry Smith   if (snes->ops->destroy) {ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr);}
137285385478SLisandro Dalcin 
137385385478SLisandro Dalcin   if (snes->vec_rhs) {ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr);}
137485385478SLisandro Dalcin   if (snes->vec_sol) {ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr);}
137585385478SLisandro Dalcin   if (snes->vec_func) {ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr);}
13763a7fca6bSBarry Smith   if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
13773a7fca6bSBarry Smith   if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
13781cee3971SBarry Smith   if (snes->ksp) {ierr = KSPDestroy(snes->ksp);CHKERRQ(ierr);}
137985385478SLisandro Dalcin   ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);
1380522c5e43SBarry Smith   if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);}
1381a6570f20SBarry Smith   ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);
13827f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);}
1383a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr);
13843a40ed3dSBarry Smith   PetscFunctionReturn(0);
13859b94acceSBarry Smith }
13869b94acceSBarry Smith 
13879b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
13889b94acceSBarry Smith 
13894a2ae208SSatish Balay #undef __FUNCT__
1390a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner"
1391a8054027SBarry Smith /*@
1392a8054027SBarry Smith    SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve.
1393a8054027SBarry Smith 
1394a8054027SBarry Smith    Collective on SNES
1395a8054027SBarry Smith 
1396a8054027SBarry Smith    Input Parameters:
1397a8054027SBarry Smith +  snes - the SNES context
1398a8054027SBarry 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
13993b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1400a8054027SBarry Smith 
1401a8054027SBarry Smith    Options Database Keys:
1402a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1403a8054027SBarry Smith 
1404a8054027SBarry Smith    Notes:
1405a8054027SBarry Smith    The default is 1
1406a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1407a8054027SBarry Smith    If  -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use
1408a8054027SBarry Smith 
1409a8054027SBarry Smith    Level: intermediate
1410a8054027SBarry Smith 
1411a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1412a8054027SBarry Smith 
1413e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian()
1414a8054027SBarry Smith 
1415a8054027SBarry Smith @*/
1416a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagPreconditioner(SNES snes,PetscInt lag)
1417a8054027SBarry Smith {
1418a8054027SBarry Smith   PetscFunctionBegin;
1419a8054027SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
14203b4f5425SBarry Smith   if (lag < -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1421a8054027SBarry Smith   if (!lag) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1422a8054027SBarry Smith   snes->lagpreconditioner = lag;
1423a8054027SBarry Smith   PetscFunctionReturn(0);
1424a8054027SBarry Smith }
1425a8054027SBarry Smith 
1426a8054027SBarry Smith #undef __FUNCT__
1427a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner"
1428a8054027SBarry Smith /*@
1429a8054027SBarry Smith    SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt
1430a8054027SBarry Smith 
1431a8054027SBarry Smith    Collective on SNES
1432a8054027SBarry Smith 
1433a8054027SBarry Smith    Input Parameter:
1434a8054027SBarry Smith .  snes - the SNES context
1435a8054027SBarry Smith 
1436a8054027SBarry Smith    Output Parameter:
1437a8054027SBarry 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
14383b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1439a8054027SBarry Smith 
1440a8054027SBarry Smith    Options Database Keys:
1441a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1442a8054027SBarry Smith 
1443a8054027SBarry Smith    Notes:
1444a8054027SBarry Smith    The default is 1
1445a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1446a8054027SBarry Smith 
1447a8054027SBarry Smith    Level: intermediate
1448a8054027SBarry Smith 
1449a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1450a8054027SBarry Smith 
1451a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner()
1452a8054027SBarry Smith 
1453a8054027SBarry Smith @*/
1454a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagPreconditioner(SNES snes,PetscInt *lag)
1455a8054027SBarry Smith {
1456a8054027SBarry Smith   PetscFunctionBegin;
1457a8054027SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1458a8054027SBarry Smith   *lag = snes->lagpreconditioner;
1459a8054027SBarry Smith   PetscFunctionReturn(0);
1460a8054027SBarry Smith }
1461a8054027SBarry Smith 
1462a8054027SBarry Smith #undef __FUNCT__
1463e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian"
1464e35cf81dSBarry Smith /*@
1465e35cf81dSBarry Smith    SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how
1466e35cf81dSBarry Smith      often the preconditioner is rebuilt.
1467e35cf81dSBarry Smith 
1468e35cf81dSBarry Smith    Collective on SNES
1469e35cf81dSBarry Smith 
1470e35cf81dSBarry Smith    Input Parameters:
1471e35cf81dSBarry Smith +  snes - the SNES context
1472e35cf81dSBarry 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
1473fe3ffe1eSBarry Smith          the Jacobian is built etc. -2 means rebuild at next chance but then never again
1474e35cf81dSBarry Smith 
1475e35cf81dSBarry Smith    Options Database Keys:
1476e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1477e35cf81dSBarry Smith 
1478e35cf81dSBarry Smith    Notes:
1479e35cf81dSBarry Smith    The default is 1
1480e35cf81dSBarry Smith    The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1481fe3ffe1eSBarry 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
1482fe3ffe1eSBarry Smith    at the next Newton step but never again (unless it is reset to another value)
1483e35cf81dSBarry Smith 
1484e35cf81dSBarry Smith    Level: intermediate
1485e35cf81dSBarry Smith 
1486e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1487e35cf81dSBarry Smith 
1488e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian()
1489e35cf81dSBarry Smith 
1490e35cf81dSBarry Smith @*/
1491e35cf81dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagJacobian(SNES snes,PetscInt lag)
1492e35cf81dSBarry Smith {
1493e35cf81dSBarry Smith   PetscFunctionBegin;
1494e35cf81dSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1495fe3ffe1eSBarry Smith   if (lag < -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1496e35cf81dSBarry Smith   if (!lag) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1497e35cf81dSBarry Smith   snes->lagjacobian = lag;
1498e35cf81dSBarry Smith   PetscFunctionReturn(0);
1499e35cf81dSBarry Smith }
1500e35cf81dSBarry Smith 
1501e35cf81dSBarry Smith #undef __FUNCT__
1502e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian"
1503e35cf81dSBarry Smith /*@
1504e35cf81dSBarry Smith    SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt
1505e35cf81dSBarry Smith 
1506e35cf81dSBarry Smith    Collective on SNES
1507e35cf81dSBarry Smith 
1508e35cf81dSBarry Smith    Input Parameter:
1509e35cf81dSBarry Smith .  snes - the SNES context
1510e35cf81dSBarry Smith 
1511e35cf81dSBarry Smith    Output Parameter:
1512e35cf81dSBarry 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
1513e35cf81dSBarry Smith          the Jacobian is built etc.
1514e35cf81dSBarry Smith 
1515e35cf81dSBarry Smith    Options Database Keys:
1516e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1517e35cf81dSBarry Smith 
1518e35cf81dSBarry Smith    Notes:
1519e35cf81dSBarry Smith    The default is 1
1520e35cf81dSBarry Smith    The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1521e35cf81dSBarry Smith 
1522e35cf81dSBarry Smith    Level: intermediate
1523e35cf81dSBarry Smith 
1524e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1525e35cf81dSBarry Smith 
1526e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner()
1527e35cf81dSBarry Smith 
1528e35cf81dSBarry Smith @*/
1529e35cf81dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagJacobian(SNES snes,PetscInt *lag)
1530e35cf81dSBarry Smith {
1531e35cf81dSBarry Smith   PetscFunctionBegin;
1532e35cf81dSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1533e35cf81dSBarry Smith   *lag = snes->lagjacobian;
1534e35cf81dSBarry Smith   PetscFunctionReturn(0);
1535e35cf81dSBarry Smith }
1536e35cf81dSBarry Smith 
1537e35cf81dSBarry Smith #undef __FUNCT__
15384a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
15399b94acceSBarry Smith /*@
1540d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
15419b94acceSBarry Smith 
1542c7afd0dbSLois Curfman McInnes    Collective on SNES
1543c7afd0dbSLois Curfman McInnes 
15449b94acceSBarry Smith    Input Parameters:
1545c7afd0dbSLois Curfman McInnes +  snes - the SNES context
154670441072SBarry Smith .  abstol - absolute convergence tolerance
154733174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
154833174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
154933174efeSLois Curfman McInnes            of the change in the solution between steps
155033174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1551c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1552fee21e36SBarry Smith 
155333174efeSLois Curfman McInnes    Options Database Keys:
155470441072SBarry Smith +    -snes_atol <abstol> - Sets abstol
1555c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1556c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1557c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1558c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
15599b94acceSBarry Smith 
1560d7a720efSLois Curfman McInnes    Notes:
15619b94acceSBarry Smith    The default maximum number of iterations is 50.
15629b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
15639b94acceSBarry Smith 
156436851e7fSLois Curfman McInnes    Level: intermediate
156536851e7fSLois Curfman McInnes 
156633174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
15679b94acceSBarry Smith 
15682492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance()
15699b94acceSBarry Smith @*/
157063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf)
15719b94acceSBarry Smith {
15723a40ed3dSBarry Smith   PetscFunctionBegin;
15734482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
157470441072SBarry Smith   if (abstol != PETSC_DEFAULT)  snes->abstol      = abstol;
1575d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1576d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1577d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1578d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
15793a40ed3dSBarry Smith   PetscFunctionReturn(0);
15809b94acceSBarry Smith }
15819b94acceSBarry Smith 
15824a2ae208SSatish Balay #undef __FUNCT__
15834a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
15849b94acceSBarry Smith /*@
158533174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
158633174efeSLois Curfman McInnes 
1587c7afd0dbSLois Curfman McInnes    Not Collective
1588c7afd0dbSLois Curfman McInnes 
158933174efeSLois Curfman McInnes    Input Parameters:
1590c7afd0dbSLois Curfman McInnes +  snes - the SNES context
159185385478SLisandro Dalcin .  atol - absolute convergence tolerance
159233174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
159333174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
159433174efeSLois Curfman McInnes            of the change in the solution between steps
159533174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1596c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1597fee21e36SBarry Smith 
159833174efeSLois Curfman McInnes    Notes:
159933174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
160033174efeSLois Curfman McInnes 
160136851e7fSLois Curfman McInnes    Level: intermediate
160236851e7fSLois Curfman McInnes 
160333174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
160433174efeSLois Curfman McInnes 
160533174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
160633174efeSLois Curfman McInnes @*/
160785385478SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf)
160833174efeSLois Curfman McInnes {
16093a40ed3dSBarry Smith   PetscFunctionBegin;
16104482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
161185385478SLisandro Dalcin   if (atol)  *atol  = snes->abstol;
161233174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
161333174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
161433174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
161533174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
16163a40ed3dSBarry Smith   PetscFunctionReturn(0);
161733174efeSLois Curfman McInnes }
161833174efeSLois Curfman McInnes 
16194a2ae208SSatish Balay #undef __FUNCT__
16204a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
162133174efeSLois Curfman McInnes /*@
16229b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
16239b94acceSBarry Smith 
1624fee21e36SBarry Smith    Collective on SNES
1625fee21e36SBarry Smith 
1626c7afd0dbSLois Curfman McInnes    Input Parameters:
1627c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1628c7afd0dbSLois Curfman McInnes -  tol - tolerance
1629c7afd0dbSLois Curfman McInnes 
16309b94acceSBarry Smith    Options Database Key:
1631c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
16329b94acceSBarry Smith 
163336851e7fSLois Curfman McInnes    Level: intermediate
163436851e7fSLois Curfman McInnes 
16359b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
16369b94acceSBarry Smith 
16372492ecdbSBarry Smith .seealso: SNESSetTolerances()
16389b94acceSBarry Smith @*/
163963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
16409b94acceSBarry Smith {
16413a40ed3dSBarry Smith   PetscFunctionBegin;
16424482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
16439b94acceSBarry Smith   snes->deltatol = tol;
16443a40ed3dSBarry Smith   PetscFunctionReturn(0);
16459b94acceSBarry Smith }
16469b94acceSBarry Smith 
1647df9fa365SBarry Smith /*
1648df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1649df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1650df9fa365SBarry Smith    macros instead of functions
1651df9fa365SBarry Smith */
16524a2ae208SSatish Balay #undef __FUNCT__
1653a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG"
1654a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx)
1655ce1608b8SBarry Smith {
1656dfbe8321SBarry Smith   PetscErrorCode ierr;
1657ce1608b8SBarry Smith 
1658ce1608b8SBarry Smith   PetscFunctionBegin;
16594482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1660a6570f20SBarry Smith   ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1661ce1608b8SBarry Smith   PetscFunctionReturn(0);
1662ce1608b8SBarry Smith }
1663ce1608b8SBarry Smith 
16644a2ae208SSatish Balay #undef __FUNCT__
1665a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate"
1666a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1667df9fa365SBarry Smith {
1668dfbe8321SBarry Smith   PetscErrorCode ierr;
1669df9fa365SBarry Smith 
1670df9fa365SBarry Smith   PetscFunctionBegin;
1671a6570f20SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1672df9fa365SBarry Smith   PetscFunctionReturn(0);
1673df9fa365SBarry Smith }
1674df9fa365SBarry Smith 
16754a2ae208SSatish Balay #undef __FUNCT__
1676a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy"
1677a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGDestroy(PetscDrawLG draw)
1678df9fa365SBarry Smith {
1679dfbe8321SBarry Smith   PetscErrorCode ierr;
1680df9fa365SBarry Smith 
1681df9fa365SBarry Smith   PetscFunctionBegin;
1682a6570f20SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
1683df9fa365SBarry Smith   PetscFunctionReturn(0);
1684df9fa365SBarry Smith }
1685df9fa365SBarry Smith 
1686b271bb04SBarry Smith extern PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorRange_Private(SNES,PetscInt,PetscReal*);
1687b271bb04SBarry Smith #undef __FUNCT__
1688b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRange"
1689b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRange(SNES snes,PetscInt n,PetscReal rnorm,void *monctx)
1690b271bb04SBarry Smith {
1691b271bb04SBarry Smith   PetscDrawLG      lg;
1692b271bb04SBarry Smith   PetscErrorCode   ierr;
1693b271bb04SBarry Smith   PetscReal        x,y,per;
1694b271bb04SBarry Smith   PetscViewer      v = (PetscViewer)monctx;
1695b271bb04SBarry Smith   static PetscReal prev; /* should be in the context */
1696b271bb04SBarry Smith   PetscDraw        draw;
1697b271bb04SBarry Smith   PetscFunctionBegin;
1698b271bb04SBarry Smith   if (!monctx) {
1699b271bb04SBarry Smith     MPI_Comm    comm;
1700b271bb04SBarry Smith 
1701b271bb04SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr);
1702b271bb04SBarry Smith     v      = PETSC_VIEWER_DRAW_(comm);
1703b271bb04SBarry Smith   }
1704b271bb04SBarry Smith   ierr   = PetscViewerDrawGetDrawLG(v,0,&lg);CHKERRQ(ierr);
1705b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1706b271bb04SBarry Smith   ierr   = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1707b271bb04SBarry Smith   ierr   = PetscDrawSetTitle(draw,"Residual norm");CHKERRQ(ierr);
1708b271bb04SBarry Smith   x = (PetscReal) n;
1709b271bb04SBarry Smith   if (rnorm > 0.0) y = log10(rnorm); else y = -15.0;
1710b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1711b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1712b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1713b271bb04SBarry Smith   }
1714b271bb04SBarry Smith 
1715b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,1,&lg);CHKERRQ(ierr);
1716b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1717b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1718b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"% elemts > .2*max elemt");CHKERRQ(ierr);
1719b271bb04SBarry Smith   ierr =  SNESMonitorRange_Private(snes,n,&per);CHKERRQ(ierr);
1720b271bb04SBarry Smith   x = (PetscReal) n;
1721b271bb04SBarry Smith   y = 100.0*per;
1722b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1723b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1724b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1725b271bb04SBarry Smith   }
1726b271bb04SBarry Smith 
1727b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,2,&lg);CHKERRQ(ierr);
1728b271bb04SBarry Smith   if (!n) {prev = rnorm;ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1729b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1730b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm");CHKERRQ(ierr);
1731b271bb04SBarry Smith   x = (PetscReal) n;
1732b271bb04SBarry Smith   y = (prev - rnorm)/prev;
1733b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1734b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1735b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1736b271bb04SBarry Smith   }
1737b271bb04SBarry Smith 
1738b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,3,&lg);CHKERRQ(ierr);
1739b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1740b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1741b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");CHKERRQ(ierr);
1742b271bb04SBarry Smith   x = (PetscReal) n;
1743b271bb04SBarry Smith   y = (prev - rnorm)/(prev*per);
1744b271bb04SBarry Smith   if (n > 2) { /*skip initial crazy value */
1745b271bb04SBarry Smith     ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1746b271bb04SBarry Smith   }
1747b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1748b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1749b271bb04SBarry Smith   }
1750b271bb04SBarry Smith   prev = rnorm;
1751b271bb04SBarry Smith   PetscFunctionReturn(0);
1752b271bb04SBarry Smith }
1753b271bb04SBarry Smith 
1754b271bb04SBarry Smith #undef __FUNCT__
1755b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeCreate"
1756b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRangeCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1757b271bb04SBarry Smith {
1758b271bb04SBarry Smith   PetscErrorCode ierr;
1759b271bb04SBarry Smith 
1760b271bb04SBarry Smith   PetscFunctionBegin;
1761b271bb04SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1762b271bb04SBarry Smith   PetscFunctionReturn(0);
1763b271bb04SBarry Smith }
1764b271bb04SBarry Smith 
1765b271bb04SBarry Smith #undef __FUNCT__
1766b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeDestroy"
1767b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRangeDestroy(PetscDrawLG draw)
1768b271bb04SBarry Smith {
1769b271bb04SBarry Smith   PetscErrorCode ierr;
1770b271bb04SBarry Smith 
1771b271bb04SBarry Smith   PetscFunctionBegin;
1772b271bb04SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
1773b271bb04SBarry Smith   PetscFunctionReturn(0);
1774b271bb04SBarry Smith }
1775b271bb04SBarry Smith 
17769b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
17779b94acceSBarry Smith 
17784a2ae208SSatish Balay #undef __FUNCT__
1779a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet"
17809b94acceSBarry Smith /*@C
1781a6570f20SBarry Smith    SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every
17829b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
17839b94acceSBarry Smith    progress.
17849b94acceSBarry Smith 
1785fee21e36SBarry Smith    Collective on SNES
1786fee21e36SBarry Smith 
1787c7afd0dbSLois Curfman McInnes    Input Parameters:
1788c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1789c7afd0dbSLois Curfman McInnes .  func - monitoring routine
1790b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
1791e8105e01SRichard Katz           monitor routine (use PETSC_NULL if no context is desired)
1792b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1793b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
17949b94acceSBarry Smith 
1795c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1796a7cc72afSBarry Smith $     int func(SNES snes,PetscInt its, PetscReal norm,void *mctx)
1797c7afd0dbSLois Curfman McInnes 
1798c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1799c7afd0dbSLois Curfman McInnes .    its - iteration number
1800c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
180140a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
18029b94acceSBarry Smith 
18039665c990SLois Curfman McInnes    Options Database Keys:
1804a6570f20SBarry Smith +    -snes_monitor        - sets SNESMonitorDefault()
1805a6570f20SBarry Smith .    -snes_monitor_draw    - sets line graph monitor,
1806a6570f20SBarry Smith                             uses SNESMonitorLGCreate()
1807a6570f20SBarry Smith _    -snes_monitor_cancel - cancels all monitors that have
1808c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
1809a6570f20SBarry Smith                             calls to SNESMonitorSet(), but
1810c7afd0dbSLois Curfman McInnes                             does not cancel those set via
1811c7afd0dbSLois Curfman McInnes                             the options database.
18129665c990SLois Curfman McInnes 
1813639f9d9dSBarry Smith    Notes:
18146bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
1815a6570f20SBarry Smith    SNESMonitorSet() multiple times; all will be called in the
18166bc08f3fSLois Curfman McInnes    order in which they were set.
1817639f9d9dSBarry Smith 
1818025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each SNES object
1819025f1a04SBarry Smith 
182036851e7fSLois Curfman McInnes    Level: intermediate
182136851e7fSLois Curfman McInnes 
18229b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
18239b94acceSBarry Smith 
1824a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel()
18259b94acceSBarry Smith @*/
1826b90d0a6eSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void*))
18279b94acceSBarry Smith {
1828b90d0a6eSBarry Smith   PetscInt i;
1829b90d0a6eSBarry Smith 
18303a40ed3dSBarry Smith   PetscFunctionBegin;
18314482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1832639f9d9dSBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) {
183329bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1834639f9d9dSBarry Smith   }
1835b90d0a6eSBarry Smith   for (i=0; i<snes->numbermonitors;i++) {
1836b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) PetscFunctionReturn(0);
1837b90d0a6eSBarry Smith 
1838b90d0a6eSBarry Smith     /* check if both default monitors that share common ASCII viewer */
1839b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitor == SNESMonitorDefault) {
1840b90d0a6eSBarry Smith       if (mctx && snes->monitorcontext[i]) {
1841b90d0a6eSBarry Smith         PetscErrorCode          ierr;
1842b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer1 = (PetscViewerASCIIMonitor) mctx;
1843b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer2 = (PetscViewerASCIIMonitor) snes->monitorcontext[i];
1844b90d0a6eSBarry Smith         if (viewer1->viewer == viewer2->viewer) {
1845b90d0a6eSBarry Smith           ierr = (*monitordestroy)(mctx);CHKERRQ(ierr);
1846b90d0a6eSBarry Smith           PetscFunctionReturn(0);
1847b90d0a6eSBarry Smith         }
1848b90d0a6eSBarry Smith       }
1849b90d0a6eSBarry Smith     }
1850b90d0a6eSBarry Smith   }
1851b90d0a6eSBarry Smith   snes->monitor[snes->numbermonitors]           = monitor;
1852b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
1853639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
18543a40ed3dSBarry Smith   PetscFunctionReturn(0);
18559b94acceSBarry Smith }
18569b94acceSBarry Smith 
18574a2ae208SSatish Balay #undef __FUNCT__
1858a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel"
18595cd90555SBarry Smith /*@C
1860a6570f20SBarry Smith    SNESMonitorCancel - Clears all the monitor functions for a SNES object.
18615cd90555SBarry Smith 
1862c7afd0dbSLois Curfman McInnes    Collective on SNES
1863c7afd0dbSLois Curfman McInnes 
18645cd90555SBarry Smith    Input Parameters:
18655cd90555SBarry Smith .  snes - the SNES context
18665cd90555SBarry Smith 
18671a480d89SAdministrator    Options Database Key:
1868a6570f20SBarry Smith .  -snes_monitor_cancel - cancels all monitors that have been hardwired
1869a6570f20SBarry Smith     into a code by calls to SNESMonitorSet(), but does not cancel those
1870c7afd0dbSLois Curfman McInnes     set via the options database
18715cd90555SBarry Smith 
18725cd90555SBarry Smith    Notes:
18735cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
18745cd90555SBarry Smith 
187536851e7fSLois Curfman McInnes    Level: intermediate
187636851e7fSLois Curfman McInnes 
18775cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
18785cd90555SBarry Smith 
1879a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet()
18805cd90555SBarry Smith @*/
1881a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorCancel(SNES snes)
18825cd90555SBarry Smith {
1883d952e501SBarry Smith   PetscErrorCode ierr;
1884d952e501SBarry Smith   PetscInt       i;
1885d952e501SBarry Smith 
18865cd90555SBarry Smith   PetscFunctionBegin;
18874482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1888d952e501SBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
1889d952e501SBarry Smith     if (snes->monitordestroy[i]) {
1890d952e501SBarry Smith       ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr);
1891d952e501SBarry Smith     }
1892d952e501SBarry Smith   }
18935cd90555SBarry Smith   snes->numbermonitors = 0;
18945cd90555SBarry Smith   PetscFunctionReturn(0);
18955cd90555SBarry Smith }
18965cd90555SBarry Smith 
18974a2ae208SSatish Balay #undef __FUNCT__
18984a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
18999b94acceSBarry Smith /*@C
19009b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
19019b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
19029b94acceSBarry Smith 
1903fee21e36SBarry Smith    Collective on SNES
1904fee21e36SBarry Smith 
1905c7afd0dbSLois Curfman McInnes    Input Parameters:
1906c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1907c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
19087f7931b9SBarry Smith .  cctx - [optional] context for private data for the convergence routine  (may be PETSC_NULL)
19097f7931b9SBarry Smith -  destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran)
19109b94acceSBarry Smith 
1911c7afd0dbSLois Curfman McInnes    Calling sequence of func:
191206ee9f85SBarry Smith $     PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
1913c7afd0dbSLois Curfman McInnes 
1914c7afd0dbSLois Curfman McInnes +    snes - the SNES context
191506ee9f85SBarry Smith .    it - current iteration (0 is the first and is before any Newton step)
1916c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
1917184914b5SBarry Smith .    reason - reason for convergence/divergence
1918c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
19194b27c08aSLois Curfman McInnes .    gnorm - 2-norm of current step
19204b27c08aSLois Curfman McInnes -    f - 2-norm of function
19219b94acceSBarry Smith 
192236851e7fSLois Curfman McInnes    Level: advanced
192336851e7fSLois Curfman McInnes 
19249b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
19259b94acceSBarry Smith 
192685385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged()
19279b94acceSBarry Smith @*/
19287f7931b9SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*))
19299b94acceSBarry Smith {
19307f7931b9SBarry Smith   PetscErrorCode ierr;
19317f7931b9SBarry Smith 
19323a40ed3dSBarry Smith   PetscFunctionBegin;
19334482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
193485385478SLisandro Dalcin   if (!func) func = SNESSkipConverged;
19357f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {
19367f7931b9SBarry Smith     ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);
19377f7931b9SBarry Smith   }
193885385478SLisandro Dalcin   snes->ops->converged        = func;
19397f7931b9SBarry Smith   snes->ops->convergeddestroy = destroy;
194085385478SLisandro Dalcin   snes->cnvP                  = cctx;
19413a40ed3dSBarry Smith   PetscFunctionReturn(0);
19429b94acceSBarry Smith }
19439b94acceSBarry Smith 
19444a2ae208SSatish Balay #undef __FUNCT__
19454a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
194652baeb72SSatish Balay /*@
1947184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
1948184914b5SBarry Smith 
1949184914b5SBarry Smith    Not Collective
1950184914b5SBarry Smith 
1951184914b5SBarry Smith    Input Parameter:
1952184914b5SBarry Smith .  snes - the SNES context
1953184914b5SBarry Smith 
1954184914b5SBarry Smith    Output Parameter:
19554d0a8057SBarry Smith .  reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the
1956184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
1957184914b5SBarry Smith 
1958184914b5SBarry Smith    Level: intermediate
1959184914b5SBarry Smith 
1960184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
1961184914b5SBarry Smith 
1962184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
1963184914b5SBarry Smith 
196485385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason
1965184914b5SBarry Smith @*/
196663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
1967184914b5SBarry Smith {
1968184914b5SBarry Smith   PetscFunctionBegin;
19694482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
19704482741eSBarry Smith   PetscValidPointer(reason,2);
1971184914b5SBarry Smith   *reason = snes->reason;
1972184914b5SBarry Smith   PetscFunctionReturn(0);
1973184914b5SBarry Smith }
1974184914b5SBarry Smith 
19754a2ae208SSatish Balay #undef __FUNCT__
19764a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
1977c9005455SLois Curfman McInnes /*@
1978c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
1979c9005455SLois Curfman McInnes 
1980fee21e36SBarry Smith    Collective on SNES
1981fee21e36SBarry Smith 
1982c7afd0dbSLois Curfman McInnes    Input Parameters:
1983c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
1984c7afd0dbSLois Curfman McInnes .  a   - array to hold history
1985cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
1986758f92a0SBarry Smith .  na  - size of a and its
198764731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
1988758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
1989c7afd0dbSLois Curfman McInnes 
1990c9005455SLois Curfman McInnes    Notes:
19914b27c08aSLois Curfman McInnes    If set, this array will contain the function norms computed
1992c9005455SLois Curfman McInnes    at each step.
1993c9005455SLois Curfman McInnes 
1994c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
1995c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
1996c9005455SLois Curfman McInnes    during the section of code that is being timed.
1997c9005455SLois Curfman McInnes 
199836851e7fSLois Curfman McInnes    Level: intermediate
199936851e7fSLois Curfman McInnes 
2000c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
2001758f92a0SBarry Smith 
200208405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
2003758f92a0SBarry Smith 
2004c9005455SLois Curfman McInnes @*/
2005a562a398SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscTruth reset)
2006c9005455SLois Curfman McInnes {
20073a40ed3dSBarry Smith   PetscFunctionBegin;
20084482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
20094482741eSBarry Smith   if (na)  PetscValidScalarPointer(a,2);
2010a562a398SLisandro Dalcin   if (its) PetscValidIntPointer(its,3);
2011c9005455SLois Curfman McInnes   snes->conv_hist       = a;
2012758f92a0SBarry Smith   snes->conv_hist_its   = its;
2013758f92a0SBarry Smith   snes->conv_hist_max   = na;
2014a12bc48fSLisandro Dalcin   snes->conv_hist_len   = 0;
2015758f92a0SBarry Smith   snes->conv_hist_reset = reset;
2016758f92a0SBarry Smith   PetscFunctionReturn(0);
2017758f92a0SBarry Smith }
2018758f92a0SBarry Smith 
20194a2ae208SSatish Balay #undef __FUNCT__
20204a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
20210c4c9dddSBarry Smith /*@C
2022758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
2023758f92a0SBarry Smith 
2024758f92a0SBarry Smith    Collective on SNES
2025758f92a0SBarry Smith 
2026758f92a0SBarry Smith    Input Parameter:
2027758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
2028758f92a0SBarry Smith 
2029758f92a0SBarry Smith    Output Parameters:
2030758f92a0SBarry Smith .  a   - array to hold history
2031758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
2032758f92a0SBarry Smith          negative if not converged) for each solve.
2033758f92a0SBarry Smith -  na  - size of a and its
2034758f92a0SBarry Smith 
2035758f92a0SBarry Smith    Notes:
2036758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
2037758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
2038758f92a0SBarry Smith 
2039758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
2040758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
2041758f92a0SBarry Smith    during the section of code that is being timed.
2042758f92a0SBarry Smith 
2043758f92a0SBarry Smith    Level: intermediate
2044758f92a0SBarry Smith 
2045758f92a0SBarry Smith .keywords: SNES, get, convergence, history
2046758f92a0SBarry Smith 
2047758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
2048758f92a0SBarry Smith 
2049758f92a0SBarry Smith @*/
205063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na)
2051758f92a0SBarry Smith {
2052758f92a0SBarry Smith   PetscFunctionBegin;
20534482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2054758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
2055758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
2056758f92a0SBarry Smith   if (na)  *na  = snes->conv_hist_len;
20573a40ed3dSBarry Smith   PetscFunctionReturn(0);
2058c9005455SLois Curfman McInnes }
2059c9005455SLois Curfman McInnes 
2060e74ef692SMatthew Knepley #undef __FUNCT__
2061e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
2062ac226902SBarry Smith /*@C
206376b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
20647e4bb74cSBarry Smith   at the beginning o every iteration of the nonlinear solve. Specifically
20657e4bb74cSBarry Smith   it is called just before the Jacobian is "evaluated".
206676b2cf59SMatthew Knepley 
206776b2cf59SMatthew Knepley   Collective on SNES
206876b2cf59SMatthew Knepley 
206976b2cf59SMatthew Knepley   Input Parameters:
207076b2cf59SMatthew Knepley . snes - The nonlinear solver context
207176b2cf59SMatthew Knepley . func - The function
207276b2cf59SMatthew Knepley 
207376b2cf59SMatthew Knepley   Calling sequence of func:
2074b5d30489SBarry Smith . func (SNES snes, PetscInt step);
207576b2cf59SMatthew Knepley 
207676b2cf59SMatthew Knepley . step - The current step of the iteration
207776b2cf59SMatthew Knepley 
207876b2cf59SMatthew Knepley   Level: intermediate
207976b2cf59SMatthew Knepley 
208076b2cf59SMatthew Knepley .keywords: SNES, update
2081b5d30489SBarry Smith 
208285385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve()
208376b2cf59SMatthew Knepley @*/
208463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt))
208576b2cf59SMatthew Knepley {
208676b2cf59SMatthew Knepley   PetscFunctionBegin;
20874482741eSBarry Smith   PetscValidHeaderSpecific(snes, SNES_COOKIE,1);
2088e7788613SBarry Smith   snes->ops->update = func;
208976b2cf59SMatthew Knepley   PetscFunctionReturn(0);
209076b2cf59SMatthew Knepley }
209176b2cf59SMatthew Knepley 
2092e74ef692SMatthew Knepley #undef __FUNCT__
2093e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
209476b2cf59SMatthew Knepley /*@
209576b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
209676b2cf59SMatthew Knepley 
209776b2cf59SMatthew Knepley   Not collective
209876b2cf59SMatthew Knepley 
209976b2cf59SMatthew Knepley   Input Parameters:
210076b2cf59SMatthew Knepley . snes - The nonlinear solver context
210176b2cf59SMatthew Knepley . step - The current step of the iteration
210276b2cf59SMatthew Knepley 
2103205452f4SMatthew Knepley   Level: intermediate
2104205452f4SMatthew Knepley 
210576b2cf59SMatthew Knepley .keywords: SNES, update
2106a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC()
210776b2cf59SMatthew Knepley @*/
210863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultUpdate(SNES snes, PetscInt step)
210976b2cf59SMatthew Knepley {
211076b2cf59SMatthew Knepley   PetscFunctionBegin;
211176b2cf59SMatthew Knepley   PetscFunctionReturn(0);
211276b2cf59SMatthew Knepley }
211376b2cf59SMatthew Knepley 
21144a2ae208SSatish Balay #undef __FUNCT__
21154a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
21169b94acceSBarry Smith /*
21179b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
21189b94acceSBarry Smith    positive parameter delta.
21199b94acceSBarry Smith 
21209b94acceSBarry Smith     Input Parameters:
2121c7afd0dbSLois Curfman McInnes +   snes - the SNES context
21229b94acceSBarry Smith .   y - approximate solution of linear system
21239b94acceSBarry Smith .   fnorm - 2-norm of current function
2124c7afd0dbSLois Curfman McInnes -   delta - trust region size
21259b94acceSBarry Smith 
21269b94acceSBarry Smith     Output Parameters:
2127c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
21289b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
21299b94acceSBarry Smith     region, and exceeds zero otherwise.
2130c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
21319b94acceSBarry Smith 
21329b94acceSBarry Smith     Note:
21334b27c08aSLois Curfman McInnes     For non-trust region methods such as SNESLS, the parameter delta
21349b94acceSBarry Smith     is set to be the maximum allowable step size.
21359b94acceSBarry Smith 
21369b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
21379b94acceSBarry Smith */
2138dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
21399b94acceSBarry Smith {
2140064f8208SBarry Smith   PetscReal      nrm;
2141ea709b57SSatish Balay   PetscScalar    cnorm;
2142dfbe8321SBarry Smith   PetscErrorCode ierr;
21433a40ed3dSBarry Smith 
21443a40ed3dSBarry Smith   PetscFunctionBegin;
21454482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
21464482741eSBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE,2);
2147c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,2);
2148184914b5SBarry Smith 
2149064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
2150064f8208SBarry Smith   if (nrm > *delta) {
2151064f8208SBarry Smith      nrm = *delta/nrm;
2152064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
2153064f8208SBarry Smith      cnorm = nrm;
21542dcb1b2aSMatthew Knepley      ierr = VecScale(y,cnorm);CHKERRQ(ierr);
21559b94acceSBarry Smith      *ynorm = *delta;
21569b94acceSBarry Smith   } else {
21579b94acceSBarry Smith      *gpnorm = 0.0;
2158064f8208SBarry Smith      *ynorm = nrm;
21599b94acceSBarry Smith   }
21603a40ed3dSBarry Smith   PetscFunctionReturn(0);
21619b94acceSBarry Smith }
21629b94acceSBarry Smith 
21634a2ae208SSatish Balay #undef __FUNCT__
21644a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
21656ce558aeSBarry Smith /*@C
2166f69a0ea3SMatthew Knepley    SNESSolve - Solves a nonlinear system F(x) = b.
2167f69a0ea3SMatthew Knepley    Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX().
21689b94acceSBarry Smith 
2169c7afd0dbSLois Curfman McInnes    Collective on SNES
2170c7afd0dbSLois Curfman McInnes 
2171b2002411SLois Curfman McInnes    Input Parameters:
2172c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2173f69a0ea3SMatthew Knepley .  b - the constant part of the equation, or PETSC_NULL to use zero.
217485385478SLisandro Dalcin -  x - the solution vector.
21759b94acceSBarry Smith 
2176b2002411SLois Curfman McInnes    Notes:
21778ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
21788ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
21798ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
21808ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
21818ddd3da0SLois Curfman McInnes 
218236851e7fSLois Curfman McInnes    Level: beginner
218336851e7fSLois Curfman McInnes 
21849b94acceSBarry Smith .keywords: SNES, nonlinear, solve
21859b94acceSBarry Smith 
218685385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian()
21879b94acceSBarry Smith @*/
2188f69a0ea3SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSolve(SNES snes,Vec b,Vec x)
21899b94acceSBarry Smith {
2190dfbe8321SBarry Smith   PetscErrorCode ierr;
2191f1af5d2fSBarry Smith   PetscTruth     flg;
2192eabae89aSBarry Smith   char           filename[PETSC_MAX_PATH_LEN];
2193eabae89aSBarry Smith   PetscViewer    viewer;
2194052efed2SBarry Smith 
21953a40ed3dSBarry Smith   PetscFunctionBegin;
21964482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2197f69a0ea3SMatthew Knepley   PetscValidHeaderSpecific(x,VEC_COOKIE,3);
2198f69a0ea3SMatthew Knepley   PetscCheckSameComm(snes,1,x,3);
219985385478SLisandro Dalcin   if (b) PetscValidHeaderSpecific(b,VEC_COOKIE,2);
220085385478SLisandro Dalcin   if (b) PetscCheckSameComm(snes,1,b,2);
220185385478SLisandro Dalcin 
220285385478SLisandro Dalcin   /* set solution vector */
220385385478SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);
220485385478SLisandro Dalcin   if (snes->vec_sol) { ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr); }
220585385478SLisandro Dalcin   snes->vec_sol = x;
220685385478SLisandro Dalcin   /* set afine vector if provided */
220785385478SLisandro Dalcin   if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); }
220885385478SLisandro Dalcin   if (snes->vec_rhs) { ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr); }
220985385478SLisandro Dalcin   snes->vec_rhs = b;
221085385478SLisandro Dalcin 
221185385478SLisandro Dalcin   if (!snes->vec_func && snes->vec_rhs) {
221285385478SLisandro Dalcin     ierr = VecDuplicate(b, &snes->vec_func); CHKERRQ(ierr);
221370e92668SMatthew Knepley   }
22143f149594SLisandro Dalcin 
221570e92668SMatthew Knepley   ierr = SNESSetUp(snes);CHKERRQ(ierr);
22163f149594SLisandro Dalcin 
2217abc0a331SBarry Smith   if (snes->conv_hist_reset) snes->conv_hist_len = 0;
221850ffb88aSMatthew Knepley   snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
2219d5e45103SBarry Smith 
22203f149594SLisandro Dalcin   ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
22214936397dSBarry Smith   ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr);
222285385478SLisandro Dalcin   ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
22234936397dSBarry Smith   if (snes->domainerror){
22244936397dSBarry Smith     snes->reason      = SNES_DIVERGED_FUNCTION_DOMAIN;
22254936397dSBarry Smith     snes->domainerror = PETSC_FALSE;
22264936397dSBarry Smith   }
222785385478SLisandro Dalcin 
22283f149594SLisandro Dalcin   if (!snes->reason) {
22293f149594SLisandro Dalcin     SETERRQ(PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason");
22303f149594SLisandro Dalcin   }
22313f149594SLisandro Dalcin 
22327adad957SLisandro Dalcin   ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
2233eabae89aSBarry Smith   if (flg && !PetscPreLoadingOn) {
22347adad957SLisandro Dalcin     ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr);
2235eabae89aSBarry Smith     ierr = SNESView(snes,viewer);CHKERRQ(ierr);
2236eabae89aSBarry Smith     ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr);
2237eabae89aSBarry Smith   }
2238eabae89aSBarry Smith 
22397adad957SLisandro Dalcin   ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg);CHKERRQ(ierr);
2240da9b6338SBarry Smith   if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); }
22415968eb51SBarry Smith   if (snes->printreason) {
22425968eb51SBarry Smith     if (snes->reason > 0) {
22437adad957SLisandro Dalcin       ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
22445968eb51SBarry Smith     } else {
22457adad957SLisandro Dalcin       ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
22465968eb51SBarry Smith     }
22475968eb51SBarry Smith   }
22485968eb51SBarry Smith 
22493a40ed3dSBarry Smith   PetscFunctionReturn(0);
22509b94acceSBarry Smith }
22519b94acceSBarry Smith 
22529b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
22539b94acceSBarry Smith 
22544a2ae208SSatish Balay #undef __FUNCT__
22554a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
225682bf6240SBarry Smith /*@C
22574b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
22589b94acceSBarry Smith 
2259fee21e36SBarry Smith    Collective on SNES
2260fee21e36SBarry Smith 
2261c7afd0dbSLois Curfman McInnes    Input Parameters:
2262c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2263454a90a3SBarry Smith -  type - a known method
2264c7afd0dbSLois Curfman McInnes 
2265c7afd0dbSLois Curfman McInnes    Options Database Key:
2266454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
2267c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
2268ae12b187SLois Curfman McInnes 
22699b94acceSBarry Smith    Notes:
2270e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
22714b27c08aSLois Curfman McInnes +    SNESLS - Newton's method with line search
2272c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
22734b27c08aSLois Curfman McInnes .    SNESTR - Newton's method with trust region
2274c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
22759b94acceSBarry Smith 
2276ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
2277ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
2278ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
2279ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
2280ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
2281ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
2282ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
2283ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
2284ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
2285b0a32e0cSBarry Smith   appropriate method.
228636851e7fSLois Curfman McInnes 
228736851e7fSLois Curfman McInnes   Level: intermediate
2288a703fe33SLois Curfman McInnes 
2289454a90a3SBarry Smith .keywords: SNES, set, type
2290435da068SBarry Smith 
2291435da068SBarry Smith .seealso: SNESType, SNESCreate()
2292435da068SBarry Smith 
22939b94acceSBarry Smith @*/
2294a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetType(SNES snes,const SNESType type)
22959b94acceSBarry Smith {
2296dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(SNES);
22976831982aSBarry Smith   PetscTruth     match;
22983a40ed3dSBarry Smith 
22993a40ed3dSBarry Smith   PetscFunctionBegin;
23004482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
23014482741eSBarry Smith   PetscValidCharPointer(type,2);
230282bf6240SBarry Smith 
23036831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
23040f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
230592ff6ae8SBarry Smith 
23067adad957SLisandro Dalcin   ierr =  PetscFListFind(SNESList,((PetscObject)snes)->comm,type,(void (**)(void)) &r);CHKERRQ(ierr);
2307958c9bccSBarry Smith   if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type);
230875396ef9SLisandro Dalcin   /* Destroy the previous private SNES context */
230975396ef9SLisandro Dalcin   if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); }
231075396ef9SLisandro Dalcin   /* Reinitialize function pointers in SNESOps structure */
231175396ef9SLisandro Dalcin   snes->ops->setup          = 0;
231275396ef9SLisandro Dalcin   snes->ops->solve          = 0;
231375396ef9SLisandro Dalcin   snes->ops->view           = 0;
231475396ef9SLisandro Dalcin   snes->ops->setfromoptions = 0;
231575396ef9SLisandro Dalcin   snes->ops->destroy        = 0;
231675396ef9SLisandro Dalcin   /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */
231775396ef9SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
23183a40ed3dSBarry Smith   ierr = (*r)(snes);CHKERRQ(ierr);
2319454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
23203a40ed3dSBarry Smith   PetscFunctionReturn(0);
23219b94acceSBarry Smith }
23229b94acceSBarry Smith 
2323a847f771SSatish Balay 
23249b94acceSBarry Smith /* --------------------------------------------------------------------- */
23254a2ae208SSatish Balay #undef __FUNCT__
23264a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
232752baeb72SSatish Balay /*@
23289b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
2329f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
23309b94acceSBarry Smith 
2331fee21e36SBarry Smith    Not Collective
2332fee21e36SBarry Smith 
233336851e7fSLois Curfman McInnes    Level: advanced
233436851e7fSLois Curfman McInnes 
23359b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
23369b94acceSBarry Smith 
23379b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
23389b94acceSBarry Smith @*/
233963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegisterDestroy(void)
23409b94acceSBarry Smith {
2341dfbe8321SBarry Smith   PetscErrorCode ierr;
234282bf6240SBarry Smith 
23433a40ed3dSBarry Smith   PetscFunctionBegin;
23441441b1d3SBarry Smith   ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
23454c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
23463a40ed3dSBarry Smith   PetscFunctionReturn(0);
23479b94acceSBarry Smith }
23489b94acceSBarry Smith 
23494a2ae208SSatish Balay #undef __FUNCT__
23504a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
23519b94acceSBarry Smith /*@C
23529a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
23539b94acceSBarry Smith 
2354c7afd0dbSLois Curfman McInnes    Not Collective
2355c7afd0dbSLois Curfman McInnes 
23569b94acceSBarry Smith    Input Parameter:
23574b0e389bSBarry Smith .  snes - nonlinear solver context
23589b94acceSBarry Smith 
23599b94acceSBarry Smith    Output Parameter:
23603a7fca6bSBarry Smith .  type - SNES method (a character string)
23619b94acceSBarry Smith 
236236851e7fSLois Curfman McInnes    Level: intermediate
236336851e7fSLois Curfman McInnes 
2364454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
23659b94acceSBarry Smith @*/
2366a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetType(SNES snes,const SNESType *type)
23679b94acceSBarry Smith {
23683a40ed3dSBarry Smith   PetscFunctionBegin;
23694482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
23704482741eSBarry Smith   PetscValidPointer(type,2);
23717adad957SLisandro Dalcin   *type = ((PetscObject)snes)->type_name;
23723a40ed3dSBarry Smith   PetscFunctionReturn(0);
23739b94acceSBarry Smith }
23749b94acceSBarry Smith 
23754a2ae208SSatish Balay #undef __FUNCT__
23764a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
237752baeb72SSatish Balay /*@
23789b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
23799b94acceSBarry Smith    stored.
23809b94acceSBarry Smith 
2381c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2382c7afd0dbSLois Curfman McInnes 
23839b94acceSBarry Smith    Input Parameter:
23849b94acceSBarry Smith .  snes - the SNES context
23859b94acceSBarry Smith 
23869b94acceSBarry Smith    Output Parameter:
23879b94acceSBarry Smith .  x - the solution
23889b94acceSBarry Smith 
238970e92668SMatthew Knepley    Level: intermediate
239036851e7fSLois Curfman McInnes 
23919b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
23929b94acceSBarry Smith 
239385385478SLisandro Dalcin .seealso:  SNESGetSolutionUpdate(), SNESGetFunction()
23949b94acceSBarry Smith @*/
239563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolution(SNES snes,Vec *x)
23969b94acceSBarry Smith {
23973a40ed3dSBarry Smith   PetscFunctionBegin;
23984482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
23994482741eSBarry Smith   PetscValidPointer(x,2);
240085385478SLisandro Dalcin   *x = snes->vec_sol;
240170e92668SMatthew Knepley   PetscFunctionReturn(0);
240270e92668SMatthew Knepley }
240370e92668SMatthew Knepley 
240470e92668SMatthew Knepley #undef __FUNCT__
24054a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
240652baeb72SSatish Balay /*@
24079b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
24089b94acceSBarry Smith    stored.
24099b94acceSBarry Smith 
2410c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2411c7afd0dbSLois Curfman McInnes 
24129b94acceSBarry Smith    Input Parameter:
24139b94acceSBarry Smith .  snes - the SNES context
24149b94acceSBarry Smith 
24159b94acceSBarry Smith    Output Parameter:
24169b94acceSBarry Smith .  x - the solution update
24179b94acceSBarry Smith 
241836851e7fSLois Curfman McInnes    Level: advanced
241936851e7fSLois Curfman McInnes 
24209b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
24219b94acceSBarry Smith 
242285385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction()
24239b94acceSBarry Smith @*/
242463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolutionUpdate(SNES snes,Vec *x)
24259b94acceSBarry Smith {
24263a40ed3dSBarry Smith   PetscFunctionBegin;
24274482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
24284482741eSBarry Smith   PetscValidPointer(x,2);
242985385478SLisandro Dalcin   *x = snes->vec_sol_update;
24303a40ed3dSBarry Smith   PetscFunctionReturn(0);
24319b94acceSBarry Smith }
24329b94acceSBarry Smith 
24334a2ae208SSatish Balay #undef __FUNCT__
24344a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
24359b94acceSBarry Smith /*@C
24363638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
24379b94acceSBarry Smith 
2438c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2439c7afd0dbSLois Curfman McInnes 
24409b94acceSBarry Smith    Input Parameter:
24419b94acceSBarry Smith .  snes - the SNES context
24429b94acceSBarry Smith 
24439b94acceSBarry Smith    Output Parameter:
24447bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
244570e92668SMatthew Knepley .  func - the function (or PETSC_NULL)
244670e92668SMatthew Knepley -  ctx - the function context (or PETSC_NULL)
24479b94acceSBarry Smith 
244836851e7fSLois Curfman McInnes    Level: advanced
244936851e7fSLois Curfman McInnes 
2450a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
24519b94acceSBarry Smith 
24524b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution()
24539b94acceSBarry Smith @*/
245470e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx)
24559b94acceSBarry Smith {
24563a40ed3dSBarry Smith   PetscFunctionBegin;
24574482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
245885385478SLisandro Dalcin   if (r)    *r    = snes->vec_func;
2459e7788613SBarry Smith   if (func) *func = snes->ops->computefunction;
246070e92668SMatthew Knepley   if (ctx)  *ctx  = snes->funP;
24613a40ed3dSBarry Smith   PetscFunctionReturn(0);
24629b94acceSBarry Smith }
24639b94acceSBarry Smith 
24644a2ae208SSatish Balay #undef __FUNCT__
24654a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
24663c7409f5SSatish Balay /*@C
24673c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2468d850072dSLois Curfman McInnes    SNES options in the database.
24693c7409f5SSatish Balay 
2470fee21e36SBarry Smith    Collective on SNES
2471fee21e36SBarry Smith 
2472c7afd0dbSLois Curfman McInnes    Input Parameter:
2473c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2474c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2475c7afd0dbSLois Curfman McInnes 
2476d850072dSLois Curfman McInnes    Notes:
2477a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2478c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2479d850072dSLois Curfman McInnes 
248036851e7fSLois Curfman McInnes    Level: advanced
248136851e7fSLois Curfman McInnes 
24823c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2483a86d99e1SLois Curfman McInnes 
2484a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
24853c7409f5SSatish Balay @*/
248663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetOptionsPrefix(SNES snes,const char prefix[])
24873c7409f5SSatish Balay {
2488dfbe8321SBarry Smith   PetscErrorCode ierr;
24893c7409f5SSatish Balay 
24903a40ed3dSBarry Smith   PetscFunctionBegin;
24914482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2492639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
24931cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
249494b7f48cSBarry Smith   ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
24953a40ed3dSBarry Smith   PetscFunctionReturn(0);
24963c7409f5SSatish Balay }
24973c7409f5SSatish Balay 
24984a2ae208SSatish Balay #undef __FUNCT__
24994a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
25003c7409f5SSatish Balay /*@C
2501f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2502d850072dSLois Curfman McInnes    SNES options in the database.
25033c7409f5SSatish Balay 
2504fee21e36SBarry Smith    Collective on SNES
2505fee21e36SBarry Smith 
2506c7afd0dbSLois Curfman McInnes    Input Parameters:
2507c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2508c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2509c7afd0dbSLois Curfman McInnes 
2510d850072dSLois Curfman McInnes    Notes:
2511a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2512c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2513d850072dSLois Curfman McInnes 
251436851e7fSLois Curfman McInnes    Level: advanced
251536851e7fSLois Curfman McInnes 
25163c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2517a86d99e1SLois Curfman McInnes 
2518a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
25193c7409f5SSatish Balay @*/
252063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAppendOptionsPrefix(SNES snes,const char prefix[])
25213c7409f5SSatish Balay {
2522dfbe8321SBarry Smith   PetscErrorCode ierr;
25233c7409f5SSatish Balay 
25243a40ed3dSBarry Smith   PetscFunctionBegin;
25254482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2526639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
25271cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
252894b7f48cSBarry Smith   ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
25293a40ed3dSBarry Smith   PetscFunctionReturn(0);
25303c7409f5SSatish Balay }
25313c7409f5SSatish Balay 
25324a2ae208SSatish Balay #undef __FUNCT__
25334a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
25349ab63eb5SSatish Balay /*@C
25353c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
25363c7409f5SSatish Balay    SNES options in the database.
25373c7409f5SSatish Balay 
2538c7afd0dbSLois Curfman McInnes    Not Collective
2539c7afd0dbSLois Curfman McInnes 
25403c7409f5SSatish Balay    Input Parameter:
25413c7409f5SSatish Balay .  snes - the SNES context
25423c7409f5SSatish Balay 
25433c7409f5SSatish Balay    Output Parameter:
25443c7409f5SSatish Balay .  prefix - pointer to the prefix string used
25453c7409f5SSatish Balay 
25464ef407dbSRichard Tran Mills    Notes: On the fortran side, the user should pass in a string 'prefix' of
25479ab63eb5SSatish Balay    sufficient length to hold the prefix.
25489ab63eb5SSatish Balay 
254936851e7fSLois Curfman McInnes    Level: advanced
255036851e7fSLois Curfman McInnes 
25513c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2552a86d99e1SLois Curfman McInnes 
2553a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
25543c7409f5SSatish Balay @*/
2555e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetOptionsPrefix(SNES snes,const char *prefix[])
25563c7409f5SSatish Balay {
2557dfbe8321SBarry Smith   PetscErrorCode ierr;
25583c7409f5SSatish Balay 
25593a40ed3dSBarry Smith   PetscFunctionBegin;
25604482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2561639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
25623a40ed3dSBarry Smith   PetscFunctionReturn(0);
25633c7409f5SSatish Balay }
25643c7409f5SSatish Balay 
2565b2002411SLois Curfman McInnes 
25664a2ae208SSatish Balay #undef __FUNCT__
25674a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
25683cea93caSBarry Smith /*@C
25693cea93caSBarry Smith   SNESRegister - See SNESRegisterDynamic()
25703cea93caSBarry Smith 
25717f6c08e0SMatthew Knepley   Level: advanced
25723cea93caSBarry Smith @*/
257363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES))
2574b2002411SLois Curfman McInnes {
2575e2d1d2b7SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
2576dfbe8321SBarry Smith   PetscErrorCode ierr;
2577b2002411SLois Curfman McInnes 
2578b2002411SLois Curfman McInnes   PetscFunctionBegin;
2579b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2580c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
2581b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2582b2002411SLois Curfman McInnes }
2583da9b6338SBarry Smith 
2584da9b6338SBarry Smith #undef __FUNCT__
2585da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin"
258663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESTestLocalMin(SNES snes)
2587da9b6338SBarry Smith {
2588dfbe8321SBarry Smith   PetscErrorCode ierr;
258977431f27SBarry Smith   PetscInt       N,i,j;
2590da9b6338SBarry Smith   Vec            u,uh,fh;
2591da9b6338SBarry Smith   PetscScalar    value;
2592da9b6338SBarry Smith   PetscReal      norm;
2593da9b6338SBarry Smith 
2594da9b6338SBarry Smith   PetscFunctionBegin;
2595da9b6338SBarry Smith   ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr);
2596da9b6338SBarry Smith   ierr = VecDuplicate(u,&uh);CHKERRQ(ierr);
2597da9b6338SBarry Smith   ierr = VecDuplicate(u,&fh);CHKERRQ(ierr);
2598da9b6338SBarry Smith 
2599da9b6338SBarry Smith   /* currently only works for sequential */
2600da9b6338SBarry Smith   ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n");
2601da9b6338SBarry Smith   ierr = VecGetSize(u,&N);CHKERRQ(ierr);
2602da9b6338SBarry Smith   for (i=0; i<N; i++) {
2603da9b6338SBarry Smith     ierr = VecCopy(u,uh);CHKERRQ(ierr);
260477431f27SBarry Smith     ierr  = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr);
2605da9b6338SBarry Smith     for (j=-10; j<11; j++) {
2606ccae9161SBarry Smith       value = PetscSign(j)*exp(PetscAbs(j)-10.0);
2607da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
26083ab0aad5SBarry Smith       ierr  = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr);
2609da9b6338SBarry Smith       ierr  = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr);
261077431f27SBarry Smith       ierr  = PetscPrintf(PETSC_COMM_WORLD,"       j norm %D %18.16e\n",j,norm);CHKERRQ(ierr);
2611da9b6338SBarry Smith       value = -value;
2612da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
2613da9b6338SBarry Smith     }
2614da9b6338SBarry Smith   }
2615da9b6338SBarry Smith   ierr = VecDestroy(uh);CHKERRQ(ierr);
2616da9b6338SBarry Smith   ierr = VecDestroy(fh);CHKERRQ(ierr);
2617da9b6338SBarry Smith   PetscFunctionReturn(0);
2618da9b6338SBarry Smith }
261971f87433Sdalcinl 
262071f87433Sdalcinl #undef __FUNCT__
2621fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW"
262271f87433Sdalcinl /*@
2623fa9f3622SBarry Smith    SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for
262471f87433Sdalcinl    computing relative tolerance for linear solvers within an inexact
262571f87433Sdalcinl    Newton method.
262671f87433Sdalcinl 
262771f87433Sdalcinl    Collective on SNES
262871f87433Sdalcinl 
262971f87433Sdalcinl    Input Parameters:
263071f87433Sdalcinl +  snes - SNES context
263171f87433Sdalcinl -  flag - PETSC_TRUE or PETSC_FALSE
263271f87433Sdalcinl 
263371f87433Sdalcinl    Notes:
263471f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
263571f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
263671f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
263771f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
263871f87433Sdalcinl    solver.
263971f87433Sdalcinl 
264071f87433Sdalcinl    Level: advanced
264171f87433Sdalcinl 
264271f87433Sdalcinl    Reference:
264371f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
264471f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
264571f87433Sdalcinl 
264671f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
264771f87433Sdalcinl 
2648fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
264971f87433Sdalcinl @*/
2650fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetUseEW(SNES snes,PetscTruth flag)
265171f87433Sdalcinl {
265271f87433Sdalcinl   PetscFunctionBegin;
265371f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
265471f87433Sdalcinl   snes->ksp_ewconv = flag;
265571f87433Sdalcinl   PetscFunctionReturn(0);
265671f87433Sdalcinl }
265771f87433Sdalcinl 
265871f87433Sdalcinl #undef __FUNCT__
2659fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW"
266071f87433Sdalcinl /*@
2661fa9f3622SBarry Smith    SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method
266271f87433Sdalcinl    for computing relative tolerance for linear solvers within an
266371f87433Sdalcinl    inexact Newton method.
266471f87433Sdalcinl 
266571f87433Sdalcinl    Not Collective
266671f87433Sdalcinl 
266771f87433Sdalcinl    Input Parameter:
266871f87433Sdalcinl .  snes - SNES context
266971f87433Sdalcinl 
267071f87433Sdalcinl    Output Parameter:
267171f87433Sdalcinl .  flag - PETSC_TRUE or PETSC_FALSE
267271f87433Sdalcinl 
267371f87433Sdalcinl    Notes:
267471f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
267571f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
267671f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
267771f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
267871f87433Sdalcinl    solver.
267971f87433Sdalcinl 
268071f87433Sdalcinl    Level: advanced
268171f87433Sdalcinl 
268271f87433Sdalcinl    Reference:
268371f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
268471f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
268571f87433Sdalcinl 
268671f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
268771f87433Sdalcinl 
2688fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
268971f87433Sdalcinl @*/
2690fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetUseEW(SNES snes, PetscTruth *flag)
269171f87433Sdalcinl {
269271f87433Sdalcinl   PetscFunctionBegin;
269371f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
269471f87433Sdalcinl   PetscValidPointer(flag,2);
269571f87433Sdalcinl   *flag = snes->ksp_ewconv;
269671f87433Sdalcinl   PetscFunctionReturn(0);
269771f87433Sdalcinl }
269871f87433Sdalcinl 
269971f87433Sdalcinl #undef __FUNCT__
2700fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW"
270171f87433Sdalcinl /*@
2702fa9f3622SBarry Smith    SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker
270371f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
270471f87433Sdalcinl    Newton method.
270571f87433Sdalcinl 
270671f87433Sdalcinl    Collective on SNES
270771f87433Sdalcinl 
270871f87433Sdalcinl    Input Parameters:
270971f87433Sdalcinl +    snes - SNES context
271071f87433Sdalcinl .    version - version 1, 2 (default is 2) or 3
271171f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
271271f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
271371f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
271471f87433Sdalcinl              (0 <= gamma2 <= 1)
271571f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
271671f87433Sdalcinl .    alpha2 - power for safeguard
271771f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
271871f87433Sdalcinl 
271971f87433Sdalcinl    Note:
272071f87433Sdalcinl    Version 3 was contributed by Luis Chacon, June 2006.
272171f87433Sdalcinl 
272271f87433Sdalcinl    Use PETSC_DEFAULT to retain the default for any of the parameters.
272371f87433Sdalcinl 
272471f87433Sdalcinl    Level: advanced
272571f87433Sdalcinl 
272671f87433Sdalcinl    Reference:
272771f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
272871f87433Sdalcinl    inexact Newton method", Utah State University Math. Stat. Dept. Res.
272971f87433Sdalcinl    Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput.
273071f87433Sdalcinl 
273171f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters
273271f87433Sdalcinl 
2733fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW()
273471f87433Sdalcinl @*/
2735fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max,
273671f87433Sdalcinl 							    PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold)
273771f87433Sdalcinl {
2738fa9f3622SBarry Smith   SNESKSPEW *kctx;
273971f87433Sdalcinl   PetscFunctionBegin;
274071f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2741fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
274271f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
274371f87433Sdalcinl 
274471f87433Sdalcinl   if (version != PETSC_DEFAULT)   kctx->version   = version;
274571f87433Sdalcinl   if (rtol_0 != PETSC_DEFAULT)    kctx->rtol_0    = rtol_0;
274671f87433Sdalcinl   if (rtol_max != PETSC_DEFAULT)  kctx->rtol_max  = rtol_max;
274771f87433Sdalcinl   if (gamma != PETSC_DEFAULT)     kctx->gamma     = gamma;
274871f87433Sdalcinl   if (alpha != PETSC_DEFAULT)     kctx->alpha     = alpha;
274971f87433Sdalcinl   if (alpha2 != PETSC_DEFAULT)    kctx->alpha2    = alpha2;
275071f87433Sdalcinl   if (threshold != PETSC_DEFAULT) kctx->threshold = threshold;
275171f87433Sdalcinl 
275271f87433Sdalcinl   if (kctx->version < 1 || kctx->version > 3) {
275371f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version);
275471f87433Sdalcinl   }
275571f87433Sdalcinl   if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) {
275671f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0);
275771f87433Sdalcinl   }
275871f87433Sdalcinl   if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) {
275971f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max);
276071f87433Sdalcinl   }
276171f87433Sdalcinl   if (kctx->gamma < 0.0 || kctx->gamma > 1.0) {
276271f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma);
276371f87433Sdalcinl   }
276471f87433Sdalcinl   if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) {
276571f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha);
276671f87433Sdalcinl   }
276771f87433Sdalcinl   if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) {
276871f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold);
276971f87433Sdalcinl   }
277071f87433Sdalcinl   PetscFunctionReturn(0);
277171f87433Sdalcinl }
277271f87433Sdalcinl 
277371f87433Sdalcinl #undef __FUNCT__
2774fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW"
277571f87433Sdalcinl /*@
2776fa9f3622SBarry Smith    SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker
277771f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
277871f87433Sdalcinl    Newton method.
277971f87433Sdalcinl 
278071f87433Sdalcinl    Not Collective
278171f87433Sdalcinl 
278271f87433Sdalcinl    Input Parameters:
278371f87433Sdalcinl      snes - SNES context
278471f87433Sdalcinl 
278571f87433Sdalcinl    Output Parameters:
278671f87433Sdalcinl +    version - version 1, 2 (default is 2) or 3
278771f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
278871f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
278971f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
279071f87433Sdalcinl              (0 <= gamma2 <= 1)
279171f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
279271f87433Sdalcinl .    alpha2 - power for safeguard
279371f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
279471f87433Sdalcinl 
279571f87433Sdalcinl    Level: advanced
279671f87433Sdalcinl 
279771f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters
279871f87433Sdalcinl 
2799fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW()
280071f87433Sdalcinl @*/
2801fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max,
280271f87433Sdalcinl 							    PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold)
280371f87433Sdalcinl {
2804fa9f3622SBarry Smith   SNESKSPEW *kctx;
280571f87433Sdalcinl   PetscFunctionBegin;
280671f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2807fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
280871f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
280971f87433Sdalcinl   if(version)   *version   = kctx->version;
281071f87433Sdalcinl   if(rtol_0)    *rtol_0    = kctx->rtol_0;
281171f87433Sdalcinl   if(rtol_max)  *rtol_max  = kctx->rtol_max;
281271f87433Sdalcinl   if(gamma)     *gamma     = kctx->gamma;
281371f87433Sdalcinl   if(alpha)     *alpha     = kctx->alpha;
281471f87433Sdalcinl   if(alpha2)    *alpha2    = kctx->alpha2;
281571f87433Sdalcinl   if(threshold) *threshold = kctx->threshold;
281671f87433Sdalcinl   PetscFunctionReturn(0);
281771f87433Sdalcinl }
281871f87433Sdalcinl 
281971f87433Sdalcinl #undef __FUNCT__
2820fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve"
2821fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x)
282271f87433Sdalcinl {
282371f87433Sdalcinl   PetscErrorCode ierr;
2824fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
282571f87433Sdalcinl   PetscReal      rtol=PETSC_DEFAULT,stol;
282671f87433Sdalcinl 
282771f87433Sdalcinl   PetscFunctionBegin;
282871f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
282971f87433Sdalcinl   if (!snes->iter) { /* first time in, so use the original user rtol */
283071f87433Sdalcinl     rtol = kctx->rtol_0;
283171f87433Sdalcinl   } else {
283271f87433Sdalcinl     if (kctx->version == 1) {
283371f87433Sdalcinl       rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last;
283471f87433Sdalcinl       if (rtol < 0.0) rtol = -rtol;
283571f87433Sdalcinl       stol = pow(kctx->rtol_last,kctx->alpha2);
283671f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
283771f87433Sdalcinl     } else if (kctx->version == 2) {
283871f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
283971f87433Sdalcinl       stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha);
284071f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
284171f87433Sdalcinl     } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */
284271f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
284371f87433Sdalcinl       /* safeguard: avoid sharp decrease of rtol */
284471f87433Sdalcinl       stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha);
284571f87433Sdalcinl       stol = PetscMax(rtol,stol);
284671f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
284771f87433Sdalcinl       /* safeguard: avoid oversolving */
284871f87433Sdalcinl       stol = kctx->gamma*(snes->ttol)/snes->norm;
284971f87433Sdalcinl       stol = PetscMax(rtol,stol);
285071f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
285171f87433Sdalcinl     } else SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version);
285271f87433Sdalcinl   }
285371f87433Sdalcinl   /* safeguard: avoid rtol greater than one */
285471f87433Sdalcinl   rtol = PetscMin(rtol,kctx->rtol_max);
285571f87433Sdalcinl   ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
285671f87433Sdalcinl   ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr);
285771f87433Sdalcinl   PetscFunctionReturn(0);
285871f87433Sdalcinl }
285971f87433Sdalcinl 
286071f87433Sdalcinl #undef __FUNCT__
2861fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve"
2862fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x)
286371f87433Sdalcinl {
286471f87433Sdalcinl   PetscErrorCode ierr;
2865fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
286671f87433Sdalcinl   PCSide         pcside;
286771f87433Sdalcinl   Vec            lres;
286871f87433Sdalcinl 
286971f87433Sdalcinl   PetscFunctionBegin;
287071f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
287171f87433Sdalcinl   ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr);
287271f87433Sdalcinl   ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr);
287371f87433Sdalcinl   if (kctx->version == 1) {
287471f87433Sdalcinl     ierr = KSPGetPreconditionerSide(ksp,&pcside);CHKERRQ(ierr);
287571f87433Sdalcinl     if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */
287671f87433Sdalcinl       /* KSP residual is true linear residual */
287771f87433Sdalcinl       ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr);
287871f87433Sdalcinl     } else {
287971f87433Sdalcinl       /* KSP residual is preconditioned residual */
288071f87433Sdalcinl       /* compute true linear residual norm */
288171f87433Sdalcinl       ierr = VecDuplicate(b,&lres);CHKERRQ(ierr);
288271f87433Sdalcinl       ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr);
288371f87433Sdalcinl       ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr);
288471f87433Sdalcinl       ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr);
288571f87433Sdalcinl       ierr = VecDestroy(lres);CHKERRQ(ierr);
288671f87433Sdalcinl     }
288771f87433Sdalcinl   }
288871f87433Sdalcinl   PetscFunctionReturn(0);
288971f87433Sdalcinl }
289071f87433Sdalcinl 
289171f87433Sdalcinl #undef __FUNCT__
289271f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve"
289371f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x)
289471f87433Sdalcinl {
289571f87433Sdalcinl   PetscErrorCode ierr;
289671f87433Sdalcinl 
289771f87433Sdalcinl   PetscFunctionBegin;
2898fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr);  }
289971f87433Sdalcinl   ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);
2900fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); }
290171f87433Sdalcinl   PetscFunctionReturn(0);
290271f87433Sdalcinl }
2903