xref: /petsc/src/snes/interface/snes.c (revision 98382af6b3ecae1110d47bdf49535bec6d180279)
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;
1265*98382af6SMatthew Knepley   PetscTruth     mf, mfOp, mfOp2;
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 
1275*98382af6SMatthew Knepley   ierr = PetscOptionsBegin(((PetscObject)snes)->comm,((PetscObject)snes)->prefix,"Nonlinear solver (SNES) auxiliary options","SNES_EX");CHKERRQ(ierr);
1276*98382af6SMatthew Knepley     ierr = PetscOptionsName("-snes_mf","Use a Matrix-Free Jacobian with no preconditioner matrix","MatCreateSNESMF",&mf);CHKERRQ(ierr);
1277*98382af6SMatthew Knepley     ierr = PetscOptionsName("-snes_mf_operator","Use a Matrix-Free Jacobian with user-provided preconditioner matrix","MatCreateSNESMF",&mfOp);CHKERRQ(ierr);
1278*98382af6SMatthew Knepley     ierr = PetscOptionsName("-snes_mf_operator2","Use a Matrix-Free (version 2) Jacobian with user-provided preconditioner matrix","MatCreateSNESMF",&mfOp2);CHKERRQ(ierr);
1279*98382af6SMatthew Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
1280*98382af6SMatthew Knepley 
1281c1f60f51SBarry Smith   /*
1282c1f60f51SBarry Smith       This version replaces the user provided Jacobian matrix with a
1283dfa02198SLois Curfman McInnes       matrix-free version but still employs the user-provided preconditioner matrix
1284c1f60f51SBarry Smith   */
1285*98382af6SMatthew Knepley   if (mfOp) {
1286c1f60f51SBarry Smith     Mat J;
1287fef1beadSBarry Smith     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
12883ec795f1SBarry Smith     ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr);
1289ae15b995SBarry Smith     ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr);
12903a7fca6bSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
12913a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
1292c1f60f51SBarry Smith   }
129345fc7adcSBarry Smith 
129403c60df9SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_MAT_SINGLE) && !defined(PETSC_USE_LONG_DOUBLE) && !defined(PETSC_USE_INT)
1295*98382af6SMatthew Knepley   if (mfOp2) {
129645fc7adcSBarry Smith     Mat J;
129745fc7adcSBarry Smith     ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_sol,&J);CHKERRQ(ierr);
129875396ef9SLisandro Dalcin     ierr = PetscInfo(snes,"Setting default matrix-free operator routines (version 2)\n");CHKERRQ(ierr);
129945fc7adcSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
130045fc7adcSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
130145fc7adcSBarry Smith   }
130232a4b47aSMatthew Knepley #endif
130345fc7adcSBarry Smith 
1304c1f60f51SBarry Smith   /*
1305dfa02198SLois Curfman McInnes       This version replaces both the user-provided Jacobian and the user-
1306c1f60f51SBarry Smith       provided preconditioner matrix with the default matrix free version.
1307c1f60f51SBarry Smith    */
1308*98382af6SMatthew Knepley   if (mf) {
1309272ac6f2SLois Curfman McInnes     Mat  J;
1310b5d62d44SBarry Smith     KSP ksp;
131194b7f48cSBarry Smith     PC   pc;
1312*98382af6SMatthew Knepley     PetscTruth flg;
131375396ef9SLisandro Dalcin     /* create and set matrix-free operator */
1314fef1beadSBarry Smith     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
13153ec795f1SBarry Smith     ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr);
131675396ef9SLisandro Dalcin     ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr);
13173ec795f1SBarry Smith     ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr);
13183a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
1319f3ef73ceSBarry Smith     /* force no preconditioner */
132094b7f48cSBarry Smith     ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
1321b5d62d44SBarry Smith     ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
1322a9815358SBarry Smith     ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&flg);CHKERRQ(ierr);
1323a9815358SBarry Smith     if (!flg) {
132475396ef9SLisandro Dalcin       ierr = PetscInfo(snes,"Setting default matrix-free preconditioner routines;\nThat is no preconditioner is being used\n");CHKERRQ(ierr);
1325f3ef73ceSBarry Smith       ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
1326272ac6f2SLois Curfman McInnes     }
1327a9815358SBarry Smith   }
1328f3ef73ceSBarry Smith 
132985385478SLisandro Dalcin   if (!snes->vec_func && !snes->vec_rhs) {
13301096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
13311096aae1SMatthew Knepley   }
133285385478SLisandro Dalcin   if (!snes->ops->computefunction && !snes->vec_rhs) {
13331096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
13341096aae1SMatthew Knepley   }
1335a8c6a408SBarry Smith   if (snes->vec_func == snes->vec_sol) {
133629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
1337a8c6a408SBarry Smith   }
1338a703fe33SLois Curfman McInnes 
1339b710008aSBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes, &snes->ksp);CHKERRQ(ierr);}
1340b710008aSBarry Smith 
1341410397dcSLisandro Dalcin   if (snes->ops->setup) {
1342410397dcSLisandro Dalcin     ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr);
1343410397dcSLisandro Dalcin   }
13447aaed0d8SBarry Smith   snes->setupcalled = PETSC_TRUE;
13453a40ed3dSBarry Smith   PetscFunctionReturn(0);
13469b94acceSBarry Smith }
13479b94acceSBarry Smith 
13484a2ae208SSatish Balay #undef __FUNCT__
13494a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
135052baeb72SSatish Balay /*@
13519b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
13529b94acceSBarry Smith    with SNESCreate().
13539b94acceSBarry Smith 
1354c7afd0dbSLois Curfman McInnes    Collective on SNES
1355c7afd0dbSLois Curfman McInnes 
13569b94acceSBarry Smith    Input Parameter:
13579b94acceSBarry Smith .  snes - the SNES context
13589b94acceSBarry Smith 
135936851e7fSLois Curfman McInnes    Level: beginner
136036851e7fSLois Curfman McInnes 
13619b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
13629b94acceSBarry Smith 
136363a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
13649b94acceSBarry Smith @*/
136563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDestroy(SNES snes)
13669b94acceSBarry Smith {
13676849ba73SBarry Smith   PetscErrorCode ierr;
13683a40ed3dSBarry Smith 
13693a40ed3dSBarry Smith   PetscFunctionBegin;
13704482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
13717adad957SLisandro Dalcin   if (--((PetscObject)snes)->refct > 0) PetscFunctionReturn(0);
1372d4bb536fSBarry Smith 
1373be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
13740f5bd95cSBarry Smith   ierr = PetscObjectDepublish(snes);CHKERRQ(ierr);
1375e7788613SBarry Smith   if (snes->ops->destroy) {ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr);}
137685385478SLisandro Dalcin 
137785385478SLisandro Dalcin   if (snes->vec_rhs) {ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr);}
137885385478SLisandro Dalcin   if (snes->vec_sol) {ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr);}
137985385478SLisandro Dalcin   if (snes->vec_func) {ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr);}
13803a7fca6bSBarry Smith   if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
13813a7fca6bSBarry Smith   if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
13821cee3971SBarry Smith   if (snes->ksp) {ierr = KSPDestroy(snes->ksp);CHKERRQ(ierr);}
138385385478SLisandro Dalcin   ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);
1384522c5e43SBarry Smith   if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);}
1385a6570f20SBarry Smith   ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);
13867f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);}
1387a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr);
13883a40ed3dSBarry Smith   PetscFunctionReturn(0);
13899b94acceSBarry Smith }
13909b94acceSBarry Smith 
13919b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
13929b94acceSBarry Smith 
13934a2ae208SSatish Balay #undef __FUNCT__
1394a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner"
1395a8054027SBarry Smith /*@
1396a8054027SBarry Smith    SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve.
1397a8054027SBarry Smith 
1398a8054027SBarry Smith    Collective on SNES
1399a8054027SBarry Smith 
1400a8054027SBarry Smith    Input Parameters:
1401a8054027SBarry Smith +  snes - the SNES context
1402a8054027SBarry 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
14033b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1404a8054027SBarry Smith 
1405a8054027SBarry Smith    Options Database Keys:
1406a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1407a8054027SBarry Smith 
1408a8054027SBarry Smith    Notes:
1409a8054027SBarry Smith    The default is 1
1410a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1411a8054027SBarry Smith    If  -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use
1412a8054027SBarry Smith 
1413a8054027SBarry Smith    Level: intermediate
1414a8054027SBarry Smith 
1415a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1416a8054027SBarry Smith 
1417e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian()
1418a8054027SBarry Smith 
1419a8054027SBarry Smith @*/
1420a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagPreconditioner(SNES snes,PetscInt lag)
1421a8054027SBarry Smith {
1422a8054027SBarry Smith   PetscFunctionBegin;
1423a8054027SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
14243b4f5425SBarry Smith   if (lag < -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1425a8054027SBarry Smith   if (!lag) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1426a8054027SBarry Smith   snes->lagpreconditioner = lag;
1427a8054027SBarry Smith   PetscFunctionReturn(0);
1428a8054027SBarry Smith }
1429a8054027SBarry Smith 
1430a8054027SBarry Smith #undef __FUNCT__
1431a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner"
1432a8054027SBarry Smith /*@
1433a8054027SBarry Smith    SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt
1434a8054027SBarry Smith 
1435a8054027SBarry Smith    Collective on SNES
1436a8054027SBarry Smith 
1437a8054027SBarry Smith    Input Parameter:
1438a8054027SBarry Smith .  snes - the SNES context
1439a8054027SBarry Smith 
1440a8054027SBarry Smith    Output Parameter:
1441a8054027SBarry 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
14423b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1443a8054027SBarry Smith 
1444a8054027SBarry Smith    Options Database Keys:
1445a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1446a8054027SBarry Smith 
1447a8054027SBarry Smith    Notes:
1448a8054027SBarry Smith    The default is 1
1449a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1450a8054027SBarry Smith 
1451a8054027SBarry Smith    Level: intermediate
1452a8054027SBarry Smith 
1453a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1454a8054027SBarry Smith 
1455a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner()
1456a8054027SBarry Smith 
1457a8054027SBarry Smith @*/
1458a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagPreconditioner(SNES snes,PetscInt *lag)
1459a8054027SBarry Smith {
1460a8054027SBarry Smith   PetscFunctionBegin;
1461a8054027SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1462a8054027SBarry Smith   *lag = snes->lagpreconditioner;
1463a8054027SBarry Smith   PetscFunctionReturn(0);
1464a8054027SBarry Smith }
1465a8054027SBarry Smith 
1466a8054027SBarry Smith #undef __FUNCT__
1467e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian"
1468e35cf81dSBarry Smith /*@
1469e35cf81dSBarry Smith    SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how
1470e35cf81dSBarry Smith      often the preconditioner is rebuilt.
1471e35cf81dSBarry Smith 
1472e35cf81dSBarry Smith    Collective on SNES
1473e35cf81dSBarry Smith 
1474e35cf81dSBarry Smith    Input Parameters:
1475e35cf81dSBarry Smith +  snes - the SNES context
1476e35cf81dSBarry 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
1477fe3ffe1eSBarry Smith          the Jacobian is built etc. -2 means rebuild at next chance but then never again
1478e35cf81dSBarry Smith 
1479e35cf81dSBarry Smith    Options Database Keys:
1480e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1481e35cf81dSBarry Smith 
1482e35cf81dSBarry Smith    Notes:
1483e35cf81dSBarry Smith    The default is 1
1484e35cf81dSBarry Smith    The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1485fe3ffe1eSBarry 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
1486fe3ffe1eSBarry Smith    at the next Newton step but never again (unless it is reset to another value)
1487e35cf81dSBarry Smith 
1488e35cf81dSBarry Smith    Level: intermediate
1489e35cf81dSBarry Smith 
1490e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1491e35cf81dSBarry Smith 
1492e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian()
1493e35cf81dSBarry Smith 
1494e35cf81dSBarry Smith @*/
1495e35cf81dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagJacobian(SNES snes,PetscInt lag)
1496e35cf81dSBarry Smith {
1497e35cf81dSBarry Smith   PetscFunctionBegin;
1498e35cf81dSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1499fe3ffe1eSBarry Smith   if (lag < -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1500e35cf81dSBarry Smith   if (!lag) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1501e35cf81dSBarry Smith   snes->lagjacobian = lag;
1502e35cf81dSBarry Smith   PetscFunctionReturn(0);
1503e35cf81dSBarry Smith }
1504e35cf81dSBarry Smith 
1505e35cf81dSBarry Smith #undef __FUNCT__
1506e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian"
1507e35cf81dSBarry Smith /*@
1508e35cf81dSBarry Smith    SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt
1509e35cf81dSBarry Smith 
1510e35cf81dSBarry Smith    Collective on SNES
1511e35cf81dSBarry Smith 
1512e35cf81dSBarry Smith    Input Parameter:
1513e35cf81dSBarry Smith .  snes - the SNES context
1514e35cf81dSBarry Smith 
1515e35cf81dSBarry Smith    Output Parameter:
1516e35cf81dSBarry 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
1517e35cf81dSBarry Smith          the Jacobian is built etc.
1518e35cf81dSBarry Smith 
1519e35cf81dSBarry Smith    Options Database Keys:
1520e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1521e35cf81dSBarry Smith 
1522e35cf81dSBarry Smith    Notes:
1523e35cf81dSBarry Smith    The default is 1
1524e35cf81dSBarry Smith    The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1525e35cf81dSBarry Smith 
1526e35cf81dSBarry Smith    Level: intermediate
1527e35cf81dSBarry Smith 
1528e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1529e35cf81dSBarry Smith 
1530e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner()
1531e35cf81dSBarry Smith 
1532e35cf81dSBarry Smith @*/
1533e35cf81dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagJacobian(SNES snes,PetscInt *lag)
1534e35cf81dSBarry Smith {
1535e35cf81dSBarry Smith   PetscFunctionBegin;
1536e35cf81dSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1537e35cf81dSBarry Smith   *lag = snes->lagjacobian;
1538e35cf81dSBarry Smith   PetscFunctionReturn(0);
1539e35cf81dSBarry Smith }
1540e35cf81dSBarry Smith 
1541e35cf81dSBarry Smith #undef __FUNCT__
15424a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
15439b94acceSBarry Smith /*@
1544d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
15459b94acceSBarry Smith 
1546c7afd0dbSLois Curfman McInnes    Collective on SNES
1547c7afd0dbSLois Curfman McInnes 
15489b94acceSBarry Smith    Input Parameters:
1549c7afd0dbSLois Curfman McInnes +  snes - the SNES context
155070441072SBarry Smith .  abstol - absolute convergence tolerance
155133174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
155233174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
155333174efeSLois Curfman McInnes            of the change in the solution between steps
155433174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1555c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1556fee21e36SBarry Smith 
155733174efeSLois Curfman McInnes    Options Database Keys:
155870441072SBarry Smith +    -snes_atol <abstol> - Sets abstol
1559c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1560c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1561c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1562c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
15639b94acceSBarry Smith 
1564d7a720efSLois Curfman McInnes    Notes:
15659b94acceSBarry Smith    The default maximum number of iterations is 50.
15669b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
15679b94acceSBarry Smith 
156836851e7fSLois Curfman McInnes    Level: intermediate
156936851e7fSLois Curfman McInnes 
157033174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
15719b94acceSBarry Smith 
15722492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance()
15739b94acceSBarry Smith @*/
157463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf)
15759b94acceSBarry Smith {
15763a40ed3dSBarry Smith   PetscFunctionBegin;
15774482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
157870441072SBarry Smith   if (abstol != PETSC_DEFAULT)  snes->abstol      = abstol;
1579d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1580d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1581d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1582d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
15833a40ed3dSBarry Smith   PetscFunctionReturn(0);
15849b94acceSBarry Smith }
15859b94acceSBarry Smith 
15864a2ae208SSatish Balay #undef __FUNCT__
15874a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
15889b94acceSBarry Smith /*@
158933174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
159033174efeSLois Curfman McInnes 
1591c7afd0dbSLois Curfman McInnes    Not Collective
1592c7afd0dbSLois Curfman McInnes 
159333174efeSLois Curfman McInnes    Input Parameters:
1594c7afd0dbSLois Curfman McInnes +  snes - the SNES context
159585385478SLisandro Dalcin .  atol - absolute convergence tolerance
159633174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
159733174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
159833174efeSLois Curfman McInnes            of the change in the solution between steps
159933174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1600c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1601fee21e36SBarry Smith 
160233174efeSLois Curfman McInnes    Notes:
160333174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
160433174efeSLois Curfman McInnes 
160536851e7fSLois Curfman McInnes    Level: intermediate
160636851e7fSLois Curfman McInnes 
160733174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
160833174efeSLois Curfman McInnes 
160933174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
161033174efeSLois Curfman McInnes @*/
161185385478SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf)
161233174efeSLois Curfman McInnes {
16133a40ed3dSBarry Smith   PetscFunctionBegin;
16144482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
161585385478SLisandro Dalcin   if (atol)  *atol  = snes->abstol;
161633174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
161733174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
161833174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
161933174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
16203a40ed3dSBarry Smith   PetscFunctionReturn(0);
162133174efeSLois Curfman McInnes }
162233174efeSLois Curfman McInnes 
16234a2ae208SSatish Balay #undef __FUNCT__
16244a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
162533174efeSLois Curfman McInnes /*@
16269b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
16279b94acceSBarry Smith 
1628fee21e36SBarry Smith    Collective on SNES
1629fee21e36SBarry Smith 
1630c7afd0dbSLois Curfman McInnes    Input Parameters:
1631c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1632c7afd0dbSLois Curfman McInnes -  tol - tolerance
1633c7afd0dbSLois Curfman McInnes 
16349b94acceSBarry Smith    Options Database Key:
1635c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
16369b94acceSBarry Smith 
163736851e7fSLois Curfman McInnes    Level: intermediate
163836851e7fSLois Curfman McInnes 
16399b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
16409b94acceSBarry Smith 
16412492ecdbSBarry Smith .seealso: SNESSetTolerances()
16429b94acceSBarry Smith @*/
164363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
16449b94acceSBarry Smith {
16453a40ed3dSBarry Smith   PetscFunctionBegin;
16464482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
16479b94acceSBarry Smith   snes->deltatol = tol;
16483a40ed3dSBarry Smith   PetscFunctionReturn(0);
16499b94acceSBarry Smith }
16509b94acceSBarry Smith 
1651df9fa365SBarry Smith /*
1652df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1653df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1654df9fa365SBarry Smith    macros instead of functions
1655df9fa365SBarry Smith */
16564a2ae208SSatish Balay #undef __FUNCT__
1657a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG"
1658a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx)
1659ce1608b8SBarry Smith {
1660dfbe8321SBarry Smith   PetscErrorCode ierr;
1661ce1608b8SBarry Smith 
1662ce1608b8SBarry Smith   PetscFunctionBegin;
16634482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1664a6570f20SBarry Smith   ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1665ce1608b8SBarry Smith   PetscFunctionReturn(0);
1666ce1608b8SBarry Smith }
1667ce1608b8SBarry Smith 
16684a2ae208SSatish Balay #undef __FUNCT__
1669a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate"
1670a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1671df9fa365SBarry Smith {
1672dfbe8321SBarry Smith   PetscErrorCode ierr;
1673df9fa365SBarry Smith 
1674df9fa365SBarry Smith   PetscFunctionBegin;
1675a6570f20SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1676df9fa365SBarry Smith   PetscFunctionReturn(0);
1677df9fa365SBarry Smith }
1678df9fa365SBarry Smith 
16794a2ae208SSatish Balay #undef __FUNCT__
1680a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy"
1681a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGDestroy(PetscDrawLG draw)
1682df9fa365SBarry Smith {
1683dfbe8321SBarry Smith   PetscErrorCode ierr;
1684df9fa365SBarry Smith 
1685df9fa365SBarry Smith   PetscFunctionBegin;
1686a6570f20SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
1687df9fa365SBarry Smith   PetscFunctionReturn(0);
1688df9fa365SBarry Smith }
1689df9fa365SBarry Smith 
1690b271bb04SBarry Smith extern PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorRange_Private(SNES,PetscInt,PetscReal*);
1691b271bb04SBarry Smith #undef __FUNCT__
1692b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRange"
1693b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRange(SNES snes,PetscInt n,PetscReal rnorm,void *monctx)
1694b271bb04SBarry Smith {
1695b271bb04SBarry Smith   PetscDrawLG      lg;
1696b271bb04SBarry Smith   PetscErrorCode   ierr;
1697b271bb04SBarry Smith   PetscReal        x,y,per;
1698b271bb04SBarry Smith   PetscViewer      v = (PetscViewer)monctx;
1699b271bb04SBarry Smith   static PetscReal prev; /* should be in the context */
1700b271bb04SBarry Smith   PetscDraw        draw;
1701b271bb04SBarry Smith   PetscFunctionBegin;
1702b271bb04SBarry Smith   if (!monctx) {
1703b271bb04SBarry Smith     MPI_Comm    comm;
1704b271bb04SBarry Smith 
1705b271bb04SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr);
1706b271bb04SBarry Smith     v      = PETSC_VIEWER_DRAW_(comm);
1707b271bb04SBarry Smith   }
1708b271bb04SBarry Smith   ierr   = PetscViewerDrawGetDrawLG(v,0,&lg);CHKERRQ(ierr);
1709b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1710b271bb04SBarry Smith   ierr   = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1711b271bb04SBarry Smith   ierr   = PetscDrawSetTitle(draw,"Residual norm");CHKERRQ(ierr);
1712b271bb04SBarry Smith   x = (PetscReal) n;
1713b271bb04SBarry Smith   if (rnorm > 0.0) y = log10(rnorm); else y = -15.0;
1714b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1715b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1716b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1717b271bb04SBarry Smith   }
1718b271bb04SBarry Smith 
1719b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,1,&lg);CHKERRQ(ierr);
1720b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1721b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1722b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"% elemts > .2*max elemt");CHKERRQ(ierr);
1723b271bb04SBarry Smith   ierr =  SNESMonitorRange_Private(snes,n,&per);CHKERRQ(ierr);
1724b271bb04SBarry Smith   x = (PetscReal) n;
1725b271bb04SBarry Smith   y = 100.0*per;
1726b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1727b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1728b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1729b271bb04SBarry Smith   }
1730b271bb04SBarry Smith 
1731b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,2,&lg);CHKERRQ(ierr);
1732b271bb04SBarry Smith   if (!n) {prev = rnorm;ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1733b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1734b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm");CHKERRQ(ierr);
1735b271bb04SBarry Smith   x = (PetscReal) n;
1736b271bb04SBarry Smith   y = (prev - rnorm)/prev;
1737b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1738b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1739b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1740b271bb04SBarry Smith   }
1741b271bb04SBarry Smith 
1742b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,3,&lg);CHKERRQ(ierr);
1743b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1744b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1745b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");CHKERRQ(ierr);
1746b271bb04SBarry Smith   x = (PetscReal) n;
1747b271bb04SBarry Smith   y = (prev - rnorm)/(prev*per);
1748b271bb04SBarry Smith   if (n > 2) { /*skip initial crazy value */
1749b271bb04SBarry Smith     ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1750b271bb04SBarry Smith   }
1751b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1752b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1753b271bb04SBarry Smith   }
1754b271bb04SBarry Smith   prev = rnorm;
1755b271bb04SBarry Smith   PetscFunctionReturn(0);
1756b271bb04SBarry Smith }
1757b271bb04SBarry Smith 
1758b271bb04SBarry Smith #undef __FUNCT__
1759b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeCreate"
1760b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRangeCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1761b271bb04SBarry Smith {
1762b271bb04SBarry Smith   PetscErrorCode ierr;
1763b271bb04SBarry Smith 
1764b271bb04SBarry Smith   PetscFunctionBegin;
1765b271bb04SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1766b271bb04SBarry Smith   PetscFunctionReturn(0);
1767b271bb04SBarry Smith }
1768b271bb04SBarry Smith 
1769b271bb04SBarry Smith #undef __FUNCT__
1770b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeDestroy"
1771b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRangeDestroy(PetscDrawLG draw)
1772b271bb04SBarry Smith {
1773b271bb04SBarry Smith   PetscErrorCode ierr;
1774b271bb04SBarry Smith 
1775b271bb04SBarry Smith   PetscFunctionBegin;
1776b271bb04SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
1777b271bb04SBarry Smith   PetscFunctionReturn(0);
1778b271bb04SBarry Smith }
1779b271bb04SBarry Smith 
17809b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
17819b94acceSBarry Smith 
17824a2ae208SSatish Balay #undef __FUNCT__
1783a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet"
17849b94acceSBarry Smith /*@C
1785a6570f20SBarry Smith    SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every
17869b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
17879b94acceSBarry Smith    progress.
17889b94acceSBarry Smith 
1789fee21e36SBarry Smith    Collective on SNES
1790fee21e36SBarry Smith 
1791c7afd0dbSLois Curfman McInnes    Input Parameters:
1792c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1793c7afd0dbSLois Curfman McInnes .  func - monitoring routine
1794b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
1795e8105e01SRichard Katz           monitor routine (use PETSC_NULL if no context is desired)
1796b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1797b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
17989b94acceSBarry Smith 
1799c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1800a7cc72afSBarry Smith $     int func(SNES snes,PetscInt its, PetscReal norm,void *mctx)
1801c7afd0dbSLois Curfman McInnes 
1802c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1803c7afd0dbSLois Curfman McInnes .    its - iteration number
1804c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
180540a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
18069b94acceSBarry Smith 
18079665c990SLois Curfman McInnes    Options Database Keys:
1808a6570f20SBarry Smith +    -snes_monitor        - sets SNESMonitorDefault()
1809a6570f20SBarry Smith .    -snes_monitor_draw    - sets line graph monitor,
1810a6570f20SBarry Smith                             uses SNESMonitorLGCreate()
1811a6570f20SBarry Smith _    -snes_monitor_cancel - cancels all monitors that have
1812c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
1813a6570f20SBarry Smith                             calls to SNESMonitorSet(), but
1814c7afd0dbSLois Curfman McInnes                             does not cancel those set via
1815c7afd0dbSLois Curfman McInnes                             the options database.
18169665c990SLois Curfman McInnes 
1817639f9d9dSBarry Smith    Notes:
18186bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
1819a6570f20SBarry Smith    SNESMonitorSet() multiple times; all will be called in the
18206bc08f3fSLois Curfman McInnes    order in which they were set.
1821639f9d9dSBarry Smith 
1822025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each SNES object
1823025f1a04SBarry Smith 
182436851e7fSLois Curfman McInnes    Level: intermediate
182536851e7fSLois Curfman McInnes 
18269b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
18279b94acceSBarry Smith 
1828a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel()
18299b94acceSBarry Smith @*/
1830b90d0a6eSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void*))
18319b94acceSBarry Smith {
1832b90d0a6eSBarry Smith   PetscInt i;
1833b90d0a6eSBarry Smith 
18343a40ed3dSBarry Smith   PetscFunctionBegin;
18354482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1836639f9d9dSBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) {
183729bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1838639f9d9dSBarry Smith   }
1839b90d0a6eSBarry Smith   for (i=0; i<snes->numbermonitors;i++) {
1840b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) PetscFunctionReturn(0);
1841b90d0a6eSBarry Smith 
1842b90d0a6eSBarry Smith     /* check if both default monitors that share common ASCII viewer */
1843b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitor == SNESMonitorDefault) {
1844b90d0a6eSBarry Smith       if (mctx && snes->monitorcontext[i]) {
1845b90d0a6eSBarry Smith         PetscErrorCode          ierr;
1846b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer1 = (PetscViewerASCIIMonitor) mctx;
1847b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer2 = (PetscViewerASCIIMonitor) snes->monitorcontext[i];
1848b90d0a6eSBarry Smith         if (viewer1->viewer == viewer2->viewer) {
1849b90d0a6eSBarry Smith           ierr = (*monitordestroy)(mctx);CHKERRQ(ierr);
1850b90d0a6eSBarry Smith           PetscFunctionReturn(0);
1851b90d0a6eSBarry Smith         }
1852b90d0a6eSBarry Smith       }
1853b90d0a6eSBarry Smith     }
1854b90d0a6eSBarry Smith   }
1855b90d0a6eSBarry Smith   snes->monitor[snes->numbermonitors]           = monitor;
1856b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
1857639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
18583a40ed3dSBarry Smith   PetscFunctionReturn(0);
18599b94acceSBarry Smith }
18609b94acceSBarry Smith 
18614a2ae208SSatish Balay #undef __FUNCT__
1862a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel"
18635cd90555SBarry Smith /*@C
1864a6570f20SBarry Smith    SNESMonitorCancel - Clears all the monitor functions for a SNES object.
18655cd90555SBarry Smith 
1866c7afd0dbSLois Curfman McInnes    Collective on SNES
1867c7afd0dbSLois Curfman McInnes 
18685cd90555SBarry Smith    Input Parameters:
18695cd90555SBarry Smith .  snes - the SNES context
18705cd90555SBarry Smith 
18711a480d89SAdministrator    Options Database Key:
1872a6570f20SBarry Smith .  -snes_monitor_cancel - cancels all monitors that have been hardwired
1873a6570f20SBarry Smith     into a code by calls to SNESMonitorSet(), but does not cancel those
1874c7afd0dbSLois Curfman McInnes     set via the options database
18755cd90555SBarry Smith 
18765cd90555SBarry Smith    Notes:
18775cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
18785cd90555SBarry Smith 
187936851e7fSLois Curfman McInnes    Level: intermediate
188036851e7fSLois Curfman McInnes 
18815cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
18825cd90555SBarry Smith 
1883a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet()
18845cd90555SBarry Smith @*/
1885a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorCancel(SNES snes)
18865cd90555SBarry Smith {
1887d952e501SBarry Smith   PetscErrorCode ierr;
1888d952e501SBarry Smith   PetscInt       i;
1889d952e501SBarry Smith 
18905cd90555SBarry Smith   PetscFunctionBegin;
18914482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1892d952e501SBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
1893d952e501SBarry Smith     if (snes->monitordestroy[i]) {
1894d952e501SBarry Smith       ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr);
1895d952e501SBarry Smith     }
1896d952e501SBarry Smith   }
18975cd90555SBarry Smith   snes->numbermonitors = 0;
18985cd90555SBarry Smith   PetscFunctionReturn(0);
18995cd90555SBarry Smith }
19005cd90555SBarry Smith 
19014a2ae208SSatish Balay #undef __FUNCT__
19024a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
19039b94acceSBarry Smith /*@C
19049b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
19059b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
19069b94acceSBarry Smith 
1907fee21e36SBarry Smith    Collective on SNES
1908fee21e36SBarry Smith 
1909c7afd0dbSLois Curfman McInnes    Input Parameters:
1910c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1911c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
19127f7931b9SBarry Smith .  cctx - [optional] context for private data for the convergence routine  (may be PETSC_NULL)
19137f7931b9SBarry Smith -  destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran)
19149b94acceSBarry Smith 
1915c7afd0dbSLois Curfman McInnes    Calling sequence of func:
191606ee9f85SBarry Smith $     PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
1917c7afd0dbSLois Curfman McInnes 
1918c7afd0dbSLois Curfman McInnes +    snes - the SNES context
191906ee9f85SBarry Smith .    it - current iteration (0 is the first and is before any Newton step)
1920c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
1921184914b5SBarry Smith .    reason - reason for convergence/divergence
1922c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
19234b27c08aSLois Curfman McInnes .    gnorm - 2-norm of current step
19244b27c08aSLois Curfman McInnes -    f - 2-norm of function
19259b94acceSBarry Smith 
192636851e7fSLois Curfman McInnes    Level: advanced
192736851e7fSLois Curfman McInnes 
19289b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
19299b94acceSBarry Smith 
193085385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged()
19319b94acceSBarry Smith @*/
19327f7931b9SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*))
19339b94acceSBarry Smith {
19347f7931b9SBarry Smith   PetscErrorCode ierr;
19357f7931b9SBarry Smith 
19363a40ed3dSBarry Smith   PetscFunctionBegin;
19374482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
193885385478SLisandro Dalcin   if (!func) func = SNESSkipConverged;
19397f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {
19407f7931b9SBarry Smith     ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);
19417f7931b9SBarry Smith   }
194285385478SLisandro Dalcin   snes->ops->converged        = func;
19437f7931b9SBarry Smith   snes->ops->convergeddestroy = destroy;
194485385478SLisandro Dalcin   snes->cnvP                  = cctx;
19453a40ed3dSBarry Smith   PetscFunctionReturn(0);
19469b94acceSBarry Smith }
19479b94acceSBarry Smith 
19484a2ae208SSatish Balay #undef __FUNCT__
19494a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
195052baeb72SSatish Balay /*@
1951184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
1952184914b5SBarry Smith 
1953184914b5SBarry Smith    Not Collective
1954184914b5SBarry Smith 
1955184914b5SBarry Smith    Input Parameter:
1956184914b5SBarry Smith .  snes - the SNES context
1957184914b5SBarry Smith 
1958184914b5SBarry Smith    Output Parameter:
19594d0a8057SBarry Smith .  reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the
1960184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
1961184914b5SBarry Smith 
1962184914b5SBarry Smith    Level: intermediate
1963184914b5SBarry Smith 
1964184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
1965184914b5SBarry Smith 
1966184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
1967184914b5SBarry Smith 
196885385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason
1969184914b5SBarry Smith @*/
197063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
1971184914b5SBarry Smith {
1972184914b5SBarry Smith   PetscFunctionBegin;
19734482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
19744482741eSBarry Smith   PetscValidPointer(reason,2);
1975184914b5SBarry Smith   *reason = snes->reason;
1976184914b5SBarry Smith   PetscFunctionReturn(0);
1977184914b5SBarry Smith }
1978184914b5SBarry Smith 
19794a2ae208SSatish Balay #undef __FUNCT__
19804a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
1981c9005455SLois Curfman McInnes /*@
1982c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
1983c9005455SLois Curfman McInnes 
1984fee21e36SBarry Smith    Collective on SNES
1985fee21e36SBarry Smith 
1986c7afd0dbSLois Curfman McInnes    Input Parameters:
1987c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
1988c7afd0dbSLois Curfman McInnes .  a   - array to hold history
1989cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
1990758f92a0SBarry Smith .  na  - size of a and its
199164731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
1992758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
1993c7afd0dbSLois Curfman McInnes 
1994c9005455SLois Curfman McInnes    Notes:
19954b27c08aSLois Curfman McInnes    If set, this array will contain the function norms computed
1996c9005455SLois Curfman McInnes    at each step.
1997c9005455SLois Curfman McInnes 
1998c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
1999c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
2000c9005455SLois Curfman McInnes    during the section of code that is being timed.
2001c9005455SLois Curfman McInnes 
200236851e7fSLois Curfman McInnes    Level: intermediate
200336851e7fSLois Curfman McInnes 
2004c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
2005758f92a0SBarry Smith 
200608405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
2007758f92a0SBarry Smith 
2008c9005455SLois Curfman McInnes @*/
2009a562a398SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscTruth reset)
2010c9005455SLois Curfman McInnes {
20113a40ed3dSBarry Smith   PetscFunctionBegin;
20124482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
20134482741eSBarry Smith   if (na)  PetscValidScalarPointer(a,2);
2014a562a398SLisandro Dalcin   if (its) PetscValidIntPointer(its,3);
2015c9005455SLois Curfman McInnes   snes->conv_hist       = a;
2016758f92a0SBarry Smith   snes->conv_hist_its   = its;
2017758f92a0SBarry Smith   snes->conv_hist_max   = na;
2018a12bc48fSLisandro Dalcin   snes->conv_hist_len   = 0;
2019758f92a0SBarry Smith   snes->conv_hist_reset = reset;
2020758f92a0SBarry Smith   PetscFunctionReturn(0);
2021758f92a0SBarry Smith }
2022758f92a0SBarry Smith 
20234a2ae208SSatish Balay #undef __FUNCT__
20244a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
20250c4c9dddSBarry Smith /*@C
2026758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
2027758f92a0SBarry Smith 
2028758f92a0SBarry Smith    Collective on SNES
2029758f92a0SBarry Smith 
2030758f92a0SBarry Smith    Input Parameter:
2031758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
2032758f92a0SBarry Smith 
2033758f92a0SBarry Smith    Output Parameters:
2034758f92a0SBarry Smith .  a   - array to hold history
2035758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
2036758f92a0SBarry Smith          negative if not converged) for each solve.
2037758f92a0SBarry Smith -  na  - size of a and its
2038758f92a0SBarry Smith 
2039758f92a0SBarry Smith    Notes:
2040758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
2041758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
2042758f92a0SBarry Smith 
2043758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
2044758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
2045758f92a0SBarry Smith    during the section of code that is being timed.
2046758f92a0SBarry Smith 
2047758f92a0SBarry Smith    Level: intermediate
2048758f92a0SBarry Smith 
2049758f92a0SBarry Smith .keywords: SNES, get, convergence, history
2050758f92a0SBarry Smith 
2051758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
2052758f92a0SBarry Smith 
2053758f92a0SBarry Smith @*/
205463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na)
2055758f92a0SBarry Smith {
2056758f92a0SBarry Smith   PetscFunctionBegin;
20574482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2058758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
2059758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
2060758f92a0SBarry Smith   if (na)  *na  = snes->conv_hist_len;
20613a40ed3dSBarry Smith   PetscFunctionReturn(0);
2062c9005455SLois Curfman McInnes }
2063c9005455SLois Curfman McInnes 
2064e74ef692SMatthew Knepley #undef __FUNCT__
2065e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
2066ac226902SBarry Smith /*@C
206776b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
20687e4bb74cSBarry Smith   at the beginning o every iteration of the nonlinear solve. Specifically
20697e4bb74cSBarry Smith   it is called just before the Jacobian is "evaluated".
207076b2cf59SMatthew Knepley 
207176b2cf59SMatthew Knepley   Collective on SNES
207276b2cf59SMatthew Knepley 
207376b2cf59SMatthew Knepley   Input Parameters:
207476b2cf59SMatthew Knepley . snes - The nonlinear solver context
207576b2cf59SMatthew Knepley . func - The function
207676b2cf59SMatthew Knepley 
207776b2cf59SMatthew Knepley   Calling sequence of func:
2078b5d30489SBarry Smith . func (SNES snes, PetscInt step);
207976b2cf59SMatthew Knepley 
208076b2cf59SMatthew Knepley . step - The current step of the iteration
208176b2cf59SMatthew Knepley 
208276b2cf59SMatthew Knepley   Level: intermediate
208376b2cf59SMatthew Knepley 
208476b2cf59SMatthew Knepley .keywords: SNES, update
2085b5d30489SBarry Smith 
208685385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve()
208776b2cf59SMatthew Knepley @*/
208863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt))
208976b2cf59SMatthew Knepley {
209076b2cf59SMatthew Knepley   PetscFunctionBegin;
20914482741eSBarry Smith   PetscValidHeaderSpecific(snes, SNES_COOKIE,1);
2092e7788613SBarry Smith   snes->ops->update = func;
209376b2cf59SMatthew Knepley   PetscFunctionReturn(0);
209476b2cf59SMatthew Knepley }
209576b2cf59SMatthew Knepley 
2096e74ef692SMatthew Knepley #undef __FUNCT__
2097e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
209876b2cf59SMatthew Knepley /*@
209976b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
210076b2cf59SMatthew Knepley 
210176b2cf59SMatthew Knepley   Not collective
210276b2cf59SMatthew Knepley 
210376b2cf59SMatthew Knepley   Input Parameters:
210476b2cf59SMatthew Knepley . snes - The nonlinear solver context
210576b2cf59SMatthew Knepley . step - The current step of the iteration
210676b2cf59SMatthew Knepley 
2107205452f4SMatthew Knepley   Level: intermediate
2108205452f4SMatthew Knepley 
210976b2cf59SMatthew Knepley .keywords: SNES, update
2110a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC()
211176b2cf59SMatthew Knepley @*/
211263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultUpdate(SNES snes, PetscInt step)
211376b2cf59SMatthew Knepley {
211476b2cf59SMatthew Knepley   PetscFunctionBegin;
211576b2cf59SMatthew Knepley   PetscFunctionReturn(0);
211676b2cf59SMatthew Knepley }
211776b2cf59SMatthew Knepley 
21184a2ae208SSatish Balay #undef __FUNCT__
21194a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
21209b94acceSBarry Smith /*
21219b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
21229b94acceSBarry Smith    positive parameter delta.
21239b94acceSBarry Smith 
21249b94acceSBarry Smith     Input Parameters:
2125c7afd0dbSLois Curfman McInnes +   snes - the SNES context
21269b94acceSBarry Smith .   y - approximate solution of linear system
21279b94acceSBarry Smith .   fnorm - 2-norm of current function
2128c7afd0dbSLois Curfman McInnes -   delta - trust region size
21299b94acceSBarry Smith 
21309b94acceSBarry Smith     Output Parameters:
2131c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
21329b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
21339b94acceSBarry Smith     region, and exceeds zero otherwise.
2134c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
21359b94acceSBarry Smith 
21369b94acceSBarry Smith     Note:
21374b27c08aSLois Curfman McInnes     For non-trust region methods such as SNESLS, the parameter delta
21389b94acceSBarry Smith     is set to be the maximum allowable step size.
21399b94acceSBarry Smith 
21409b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
21419b94acceSBarry Smith */
2142dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
21439b94acceSBarry Smith {
2144064f8208SBarry Smith   PetscReal      nrm;
2145ea709b57SSatish Balay   PetscScalar    cnorm;
2146dfbe8321SBarry Smith   PetscErrorCode ierr;
21473a40ed3dSBarry Smith 
21483a40ed3dSBarry Smith   PetscFunctionBegin;
21494482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
21504482741eSBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE,2);
2151c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,2);
2152184914b5SBarry Smith 
2153064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
2154064f8208SBarry Smith   if (nrm > *delta) {
2155064f8208SBarry Smith      nrm = *delta/nrm;
2156064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
2157064f8208SBarry Smith      cnorm = nrm;
21582dcb1b2aSMatthew Knepley      ierr = VecScale(y,cnorm);CHKERRQ(ierr);
21599b94acceSBarry Smith      *ynorm = *delta;
21609b94acceSBarry Smith   } else {
21619b94acceSBarry Smith      *gpnorm = 0.0;
2162064f8208SBarry Smith      *ynorm = nrm;
21639b94acceSBarry Smith   }
21643a40ed3dSBarry Smith   PetscFunctionReturn(0);
21659b94acceSBarry Smith }
21669b94acceSBarry Smith 
21674a2ae208SSatish Balay #undef __FUNCT__
21684a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
21696ce558aeSBarry Smith /*@C
2170f69a0ea3SMatthew Knepley    SNESSolve - Solves a nonlinear system F(x) = b.
2171f69a0ea3SMatthew Knepley    Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX().
21729b94acceSBarry Smith 
2173c7afd0dbSLois Curfman McInnes    Collective on SNES
2174c7afd0dbSLois Curfman McInnes 
2175b2002411SLois Curfman McInnes    Input Parameters:
2176c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2177f69a0ea3SMatthew Knepley .  b - the constant part of the equation, or PETSC_NULL to use zero.
217885385478SLisandro Dalcin -  x - the solution vector.
21799b94acceSBarry Smith 
2180b2002411SLois Curfman McInnes    Notes:
21818ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
21828ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
21838ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
21848ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
21858ddd3da0SLois Curfman McInnes 
218636851e7fSLois Curfman McInnes    Level: beginner
218736851e7fSLois Curfman McInnes 
21889b94acceSBarry Smith .keywords: SNES, nonlinear, solve
21899b94acceSBarry Smith 
219085385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian()
21919b94acceSBarry Smith @*/
2192f69a0ea3SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSolve(SNES snes,Vec b,Vec x)
21939b94acceSBarry Smith {
2194dfbe8321SBarry Smith   PetscErrorCode ierr;
2195f1af5d2fSBarry Smith   PetscTruth     flg;
2196eabae89aSBarry Smith   char           filename[PETSC_MAX_PATH_LEN];
2197eabae89aSBarry Smith   PetscViewer    viewer;
2198052efed2SBarry Smith 
21993a40ed3dSBarry Smith   PetscFunctionBegin;
22004482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2201f69a0ea3SMatthew Knepley   PetscValidHeaderSpecific(x,VEC_COOKIE,3);
2202f69a0ea3SMatthew Knepley   PetscCheckSameComm(snes,1,x,3);
220385385478SLisandro Dalcin   if (b) PetscValidHeaderSpecific(b,VEC_COOKIE,2);
220485385478SLisandro Dalcin   if (b) PetscCheckSameComm(snes,1,b,2);
220585385478SLisandro Dalcin 
220685385478SLisandro Dalcin   /* set solution vector */
220785385478SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);
220885385478SLisandro Dalcin   if (snes->vec_sol) { ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr); }
220985385478SLisandro Dalcin   snes->vec_sol = x;
221085385478SLisandro Dalcin   /* set afine vector if provided */
221185385478SLisandro Dalcin   if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); }
221285385478SLisandro Dalcin   if (snes->vec_rhs) { ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr); }
221385385478SLisandro Dalcin   snes->vec_rhs = b;
221485385478SLisandro Dalcin 
221585385478SLisandro Dalcin   if (!snes->vec_func && snes->vec_rhs) {
221685385478SLisandro Dalcin     ierr = VecDuplicate(b, &snes->vec_func);CHKERRQ(ierr);
221770e92668SMatthew Knepley   }
22183f149594SLisandro Dalcin 
221970e92668SMatthew Knepley   ierr = SNESSetUp(snes);CHKERRQ(ierr);
22203f149594SLisandro Dalcin 
2221abc0a331SBarry Smith   if (snes->conv_hist_reset) snes->conv_hist_len = 0;
222250ffb88aSMatthew Knepley   snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
2223d5e45103SBarry Smith 
22243f149594SLisandro Dalcin   ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
22254936397dSBarry Smith   ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr);
222685385478SLisandro Dalcin   ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
22274936397dSBarry Smith   if (snes->domainerror){
22284936397dSBarry Smith     snes->reason      = SNES_DIVERGED_FUNCTION_DOMAIN;
22294936397dSBarry Smith     snes->domainerror = PETSC_FALSE;
22304936397dSBarry Smith   }
223185385478SLisandro Dalcin 
22323f149594SLisandro Dalcin   if (!snes->reason) {
22333f149594SLisandro Dalcin     SETERRQ(PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason");
22343f149594SLisandro Dalcin   }
22353f149594SLisandro Dalcin 
22367adad957SLisandro Dalcin   ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
2237eabae89aSBarry Smith   if (flg && !PetscPreLoadingOn) {
22387adad957SLisandro Dalcin     ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr);
2239eabae89aSBarry Smith     ierr = SNESView(snes,viewer);CHKERRQ(ierr);
2240eabae89aSBarry Smith     ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr);
2241eabae89aSBarry Smith   }
2242eabae89aSBarry Smith 
22437adad957SLisandro Dalcin   ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg);CHKERRQ(ierr);
2244da9b6338SBarry Smith   if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); }
22455968eb51SBarry Smith   if (snes->printreason) {
22465968eb51SBarry Smith     if (snes->reason > 0) {
22477adad957SLisandro Dalcin       ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
22485968eb51SBarry Smith     } else {
22497adad957SLisandro Dalcin       ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
22505968eb51SBarry Smith     }
22515968eb51SBarry Smith   }
22525968eb51SBarry Smith 
22533a40ed3dSBarry Smith   PetscFunctionReturn(0);
22549b94acceSBarry Smith }
22559b94acceSBarry Smith 
22569b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
22579b94acceSBarry Smith 
22584a2ae208SSatish Balay #undef __FUNCT__
22594a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
226082bf6240SBarry Smith /*@C
22614b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
22629b94acceSBarry Smith 
2263fee21e36SBarry Smith    Collective on SNES
2264fee21e36SBarry Smith 
2265c7afd0dbSLois Curfman McInnes    Input Parameters:
2266c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2267454a90a3SBarry Smith -  type - a known method
2268c7afd0dbSLois Curfman McInnes 
2269c7afd0dbSLois Curfman McInnes    Options Database Key:
2270454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
2271c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
2272ae12b187SLois Curfman McInnes 
22739b94acceSBarry Smith    Notes:
2274e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
22754b27c08aSLois Curfman McInnes +    SNESLS - Newton's method with line search
2276c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
22774b27c08aSLois Curfman McInnes .    SNESTR - Newton's method with trust region
2278c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
22799b94acceSBarry Smith 
2280ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
2281ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
2282ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
2283ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
2284ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
2285ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
2286ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
2287ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
2288ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
2289b0a32e0cSBarry Smith   appropriate method.
229036851e7fSLois Curfman McInnes 
229136851e7fSLois Curfman McInnes   Level: intermediate
2292a703fe33SLois Curfman McInnes 
2293454a90a3SBarry Smith .keywords: SNES, set, type
2294435da068SBarry Smith 
2295435da068SBarry Smith .seealso: SNESType, SNESCreate()
2296435da068SBarry Smith 
22979b94acceSBarry Smith @*/
2298a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetType(SNES snes,const SNESType type)
22999b94acceSBarry Smith {
2300dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(SNES);
23016831982aSBarry Smith   PetscTruth     match;
23023a40ed3dSBarry Smith 
23033a40ed3dSBarry Smith   PetscFunctionBegin;
23044482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
23054482741eSBarry Smith   PetscValidCharPointer(type,2);
230682bf6240SBarry Smith 
23076831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
23080f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
230992ff6ae8SBarry Smith 
23107adad957SLisandro Dalcin   ierr =  PetscFListFind(SNESList,((PetscObject)snes)->comm,type,(void (**)(void)) &r);CHKERRQ(ierr);
2311958c9bccSBarry Smith   if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type);
231275396ef9SLisandro Dalcin   /* Destroy the previous private SNES context */
231375396ef9SLisandro Dalcin   if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); }
231475396ef9SLisandro Dalcin   /* Reinitialize function pointers in SNESOps structure */
231575396ef9SLisandro Dalcin   snes->ops->setup          = 0;
231675396ef9SLisandro Dalcin   snes->ops->solve          = 0;
231775396ef9SLisandro Dalcin   snes->ops->view           = 0;
231875396ef9SLisandro Dalcin   snes->ops->setfromoptions = 0;
231975396ef9SLisandro Dalcin   snes->ops->destroy        = 0;
232075396ef9SLisandro Dalcin   /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */
232175396ef9SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
23223a40ed3dSBarry Smith   ierr = (*r)(snes);CHKERRQ(ierr);
2323454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
23243a40ed3dSBarry Smith   PetscFunctionReturn(0);
23259b94acceSBarry Smith }
23269b94acceSBarry Smith 
2327a847f771SSatish Balay 
23289b94acceSBarry Smith /* --------------------------------------------------------------------- */
23294a2ae208SSatish Balay #undef __FUNCT__
23304a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
233152baeb72SSatish Balay /*@
23329b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
2333f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
23349b94acceSBarry Smith 
2335fee21e36SBarry Smith    Not Collective
2336fee21e36SBarry Smith 
233736851e7fSLois Curfman McInnes    Level: advanced
233836851e7fSLois Curfman McInnes 
23399b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
23409b94acceSBarry Smith 
23419b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
23429b94acceSBarry Smith @*/
234363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegisterDestroy(void)
23449b94acceSBarry Smith {
2345dfbe8321SBarry Smith   PetscErrorCode ierr;
234682bf6240SBarry Smith 
23473a40ed3dSBarry Smith   PetscFunctionBegin;
23481441b1d3SBarry Smith   ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
23494c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
23503a40ed3dSBarry Smith   PetscFunctionReturn(0);
23519b94acceSBarry Smith }
23529b94acceSBarry Smith 
23534a2ae208SSatish Balay #undef __FUNCT__
23544a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
23559b94acceSBarry Smith /*@C
23569a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
23579b94acceSBarry Smith 
2358c7afd0dbSLois Curfman McInnes    Not Collective
2359c7afd0dbSLois Curfman McInnes 
23609b94acceSBarry Smith    Input Parameter:
23614b0e389bSBarry Smith .  snes - nonlinear solver context
23629b94acceSBarry Smith 
23639b94acceSBarry Smith    Output Parameter:
23643a7fca6bSBarry Smith .  type - SNES method (a character string)
23659b94acceSBarry Smith 
236636851e7fSLois Curfman McInnes    Level: intermediate
236736851e7fSLois Curfman McInnes 
2368454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
23699b94acceSBarry Smith @*/
2370a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetType(SNES snes,const SNESType *type)
23719b94acceSBarry Smith {
23723a40ed3dSBarry Smith   PetscFunctionBegin;
23734482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
23744482741eSBarry Smith   PetscValidPointer(type,2);
23757adad957SLisandro Dalcin   *type = ((PetscObject)snes)->type_name;
23763a40ed3dSBarry Smith   PetscFunctionReturn(0);
23779b94acceSBarry Smith }
23789b94acceSBarry Smith 
23794a2ae208SSatish Balay #undef __FUNCT__
23804a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
238152baeb72SSatish Balay /*@
23829b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
23839b94acceSBarry Smith    stored.
23849b94acceSBarry Smith 
2385c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2386c7afd0dbSLois Curfman McInnes 
23879b94acceSBarry Smith    Input Parameter:
23889b94acceSBarry Smith .  snes - the SNES context
23899b94acceSBarry Smith 
23909b94acceSBarry Smith    Output Parameter:
23919b94acceSBarry Smith .  x - the solution
23929b94acceSBarry Smith 
239370e92668SMatthew Knepley    Level: intermediate
239436851e7fSLois Curfman McInnes 
23959b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
23969b94acceSBarry Smith 
239785385478SLisandro Dalcin .seealso:  SNESGetSolutionUpdate(), SNESGetFunction()
23989b94acceSBarry Smith @*/
239963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolution(SNES snes,Vec *x)
24009b94acceSBarry Smith {
24013a40ed3dSBarry Smith   PetscFunctionBegin;
24024482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
24034482741eSBarry Smith   PetscValidPointer(x,2);
240485385478SLisandro Dalcin   *x = snes->vec_sol;
240570e92668SMatthew Knepley   PetscFunctionReturn(0);
240670e92668SMatthew Knepley }
240770e92668SMatthew Knepley 
240870e92668SMatthew Knepley #undef __FUNCT__
24094a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
241052baeb72SSatish Balay /*@
24119b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
24129b94acceSBarry Smith    stored.
24139b94acceSBarry Smith 
2414c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2415c7afd0dbSLois Curfman McInnes 
24169b94acceSBarry Smith    Input Parameter:
24179b94acceSBarry Smith .  snes - the SNES context
24189b94acceSBarry Smith 
24199b94acceSBarry Smith    Output Parameter:
24209b94acceSBarry Smith .  x - the solution update
24219b94acceSBarry Smith 
242236851e7fSLois Curfman McInnes    Level: advanced
242336851e7fSLois Curfman McInnes 
24249b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
24259b94acceSBarry Smith 
242685385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction()
24279b94acceSBarry Smith @*/
242863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolutionUpdate(SNES snes,Vec *x)
24299b94acceSBarry Smith {
24303a40ed3dSBarry Smith   PetscFunctionBegin;
24314482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
24324482741eSBarry Smith   PetscValidPointer(x,2);
243385385478SLisandro Dalcin   *x = snes->vec_sol_update;
24343a40ed3dSBarry Smith   PetscFunctionReturn(0);
24359b94acceSBarry Smith }
24369b94acceSBarry Smith 
24374a2ae208SSatish Balay #undef __FUNCT__
24384a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
24399b94acceSBarry Smith /*@C
24403638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
24419b94acceSBarry Smith 
2442c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2443c7afd0dbSLois Curfman McInnes 
24449b94acceSBarry Smith    Input Parameter:
24459b94acceSBarry Smith .  snes - the SNES context
24469b94acceSBarry Smith 
24479b94acceSBarry Smith    Output Parameter:
24487bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
244970e92668SMatthew Knepley .  func - the function (or PETSC_NULL)
245070e92668SMatthew Knepley -  ctx - the function context (or PETSC_NULL)
24519b94acceSBarry Smith 
245236851e7fSLois Curfman McInnes    Level: advanced
245336851e7fSLois Curfman McInnes 
2454a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
24559b94acceSBarry Smith 
24564b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution()
24579b94acceSBarry Smith @*/
245870e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx)
24599b94acceSBarry Smith {
24603a40ed3dSBarry Smith   PetscFunctionBegin;
24614482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
246285385478SLisandro Dalcin   if (r)    *r    = snes->vec_func;
2463e7788613SBarry Smith   if (func) *func = snes->ops->computefunction;
246470e92668SMatthew Knepley   if (ctx)  *ctx  = snes->funP;
24653a40ed3dSBarry Smith   PetscFunctionReturn(0);
24669b94acceSBarry Smith }
24679b94acceSBarry Smith 
24684a2ae208SSatish Balay #undef __FUNCT__
24694a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
24703c7409f5SSatish Balay /*@C
24713c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2472d850072dSLois Curfman McInnes    SNES options in the database.
24733c7409f5SSatish Balay 
2474fee21e36SBarry Smith    Collective on SNES
2475fee21e36SBarry Smith 
2476c7afd0dbSLois Curfman McInnes    Input Parameter:
2477c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2478c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2479c7afd0dbSLois Curfman McInnes 
2480d850072dSLois Curfman McInnes    Notes:
2481a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2482c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2483d850072dSLois Curfman McInnes 
248436851e7fSLois Curfman McInnes    Level: advanced
248536851e7fSLois Curfman McInnes 
24863c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2487a86d99e1SLois Curfman McInnes 
2488a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
24893c7409f5SSatish Balay @*/
249063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetOptionsPrefix(SNES snes,const char prefix[])
24913c7409f5SSatish Balay {
2492dfbe8321SBarry Smith   PetscErrorCode ierr;
24933c7409f5SSatish Balay 
24943a40ed3dSBarry Smith   PetscFunctionBegin;
24954482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2496639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
24971cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
249894b7f48cSBarry Smith   ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
24993a40ed3dSBarry Smith   PetscFunctionReturn(0);
25003c7409f5SSatish Balay }
25013c7409f5SSatish Balay 
25024a2ae208SSatish Balay #undef __FUNCT__
25034a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
25043c7409f5SSatish Balay /*@C
2505f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2506d850072dSLois Curfman McInnes    SNES options in the database.
25073c7409f5SSatish Balay 
2508fee21e36SBarry Smith    Collective on SNES
2509fee21e36SBarry Smith 
2510c7afd0dbSLois Curfman McInnes    Input Parameters:
2511c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2512c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2513c7afd0dbSLois Curfman McInnes 
2514d850072dSLois Curfman McInnes    Notes:
2515a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2516c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2517d850072dSLois Curfman McInnes 
251836851e7fSLois Curfman McInnes    Level: advanced
251936851e7fSLois Curfman McInnes 
25203c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2521a86d99e1SLois Curfman McInnes 
2522a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
25233c7409f5SSatish Balay @*/
252463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAppendOptionsPrefix(SNES snes,const char prefix[])
25253c7409f5SSatish Balay {
2526dfbe8321SBarry Smith   PetscErrorCode ierr;
25273c7409f5SSatish Balay 
25283a40ed3dSBarry Smith   PetscFunctionBegin;
25294482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2530639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
25311cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
253294b7f48cSBarry Smith   ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
25333a40ed3dSBarry Smith   PetscFunctionReturn(0);
25343c7409f5SSatish Balay }
25353c7409f5SSatish Balay 
25364a2ae208SSatish Balay #undef __FUNCT__
25374a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
25389ab63eb5SSatish Balay /*@C
25393c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
25403c7409f5SSatish Balay    SNES options in the database.
25413c7409f5SSatish Balay 
2542c7afd0dbSLois Curfman McInnes    Not Collective
2543c7afd0dbSLois Curfman McInnes 
25443c7409f5SSatish Balay    Input Parameter:
25453c7409f5SSatish Balay .  snes - the SNES context
25463c7409f5SSatish Balay 
25473c7409f5SSatish Balay    Output Parameter:
25483c7409f5SSatish Balay .  prefix - pointer to the prefix string used
25493c7409f5SSatish Balay 
25504ef407dbSRichard Tran Mills    Notes: On the fortran side, the user should pass in a string 'prefix' of
25519ab63eb5SSatish Balay    sufficient length to hold the prefix.
25529ab63eb5SSatish Balay 
255336851e7fSLois Curfman McInnes    Level: advanced
255436851e7fSLois Curfman McInnes 
25553c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2556a86d99e1SLois Curfman McInnes 
2557a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
25583c7409f5SSatish Balay @*/
2559e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetOptionsPrefix(SNES snes,const char *prefix[])
25603c7409f5SSatish Balay {
2561dfbe8321SBarry Smith   PetscErrorCode ierr;
25623c7409f5SSatish Balay 
25633a40ed3dSBarry Smith   PetscFunctionBegin;
25644482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2565639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
25663a40ed3dSBarry Smith   PetscFunctionReturn(0);
25673c7409f5SSatish Balay }
25683c7409f5SSatish Balay 
2569b2002411SLois Curfman McInnes 
25704a2ae208SSatish Balay #undef __FUNCT__
25714a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
25723cea93caSBarry Smith /*@C
25733cea93caSBarry Smith   SNESRegister - See SNESRegisterDynamic()
25743cea93caSBarry Smith 
25757f6c08e0SMatthew Knepley   Level: advanced
25763cea93caSBarry Smith @*/
257763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES))
2578b2002411SLois Curfman McInnes {
2579e2d1d2b7SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
2580dfbe8321SBarry Smith   PetscErrorCode ierr;
2581b2002411SLois Curfman McInnes 
2582b2002411SLois Curfman McInnes   PetscFunctionBegin;
2583b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2584c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
2585b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2586b2002411SLois Curfman McInnes }
2587da9b6338SBarry Smith 
2588da9b6338SBarry Smith #undef __FUNCT__
2589da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin"
259063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESTestLocalMin(SNES snes)
2591da9b6338SBarry Smith {
2592dfbe8321SBarry Smith   PetscErrorCode ierr;
259377431f27SBarry Smith   PetscInt       N,i,j;
2594da9b6338SBarry Smith   Vec            u,uh,fh;
2595da9b6338SBarry Smith   PetscScalar    value;
2596da9b6338SBarry Smith   PetscReal      norm;
2597da9b6338SBarry Smith 
2598da9b6338SBarry Smith   PetscFunctionBegin;
2599da9b6338SBarry Smith   ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr);
2600da9b6338SBarry Smith   ierr = VecDuplicate(u,&uh);CHKERRQ(ierr);
2601da9b6338SBarry Smith   ierr = VecDuplicate(u,&fh);CHKERRQ(ierr);
2602da9b6338SBarry Smith 
2603da9b6338SBarry Smith   /* currently only works for sequential */
2604da9b6338SBarry Smith   ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n");
2605da9b6338SBarry Smith   ierr = VecGetSize(u,&N);CHKERRQ(ierr);
2606da9b6338SBarry Smith   for (i=0; i<N; i++) {
2607da9b6338SBarry Smith     ierr = VecCopy(u,uh);CHKERRQ(ierr);
260877431f27SBarry Smith     ierr  = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr);
2609da9b6338SBarry Smith     for (j=-10; j<11; j++) {
2610ccae9161SBarry Smith       value = PetscSign(j)*exp(PetscAbs(j)-10.0);
2611da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
26123ab0aad5SBarry Smith       ierr  = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr);
2613da9b6338SBarry Smith       ierr  = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr);
261477431f27SBarry Smith       ierr  = PetscPrintf(PETSC_COMM_WORLD,"       j norm %D %18.16e\n",j,norm);CHKERRQ(ierr);
2615da9b6338SBarry Smith       value = -value;
2616da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
2617da9b6338SBarry Smith     }
2618da9b6338SBarry Smith   }
2619da9b6338SBarry Smith   ierr = VecDestroy(uh);CHKERRQ(ierr);
2620da9b6338SBarry Smith   ierr = VecDestroy(fh);CHKERRQ(ierr);
2621da9b6338SBarry Smith   PetscFunctionReturn(0);
2622da9b6338SBarry Smith }
262371f87433Sdalcinl 
262471f87433Sdalcinl #undef __FUNCT__
2625fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW"
262671f87433Sdalcinl /*@
2627fa9f3622SBarry Smith    SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for
262871f87433Sdalcinl    computing relative tolerance for linear solvers within an inexact
262971f87433Sdalcinl    Newton method.
263071f87433Sdalcinl 
263171f87433Sdalcinl    Collective on SNES
263271f87433Sdalcinl 
263371f87433Sdalcinl    Input Parameters:
263471f87433Sdalcinl +  snes - SNES context
263571f87433Sdalcinl -  flag - PETSC_TRUE or PETSC_FALSE
263671f87433Sdalcinl 
263771f87433Sdalcinl    Notes:
263871f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
263971f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
264071f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
264171f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
264271f87433Sdalcinl    solver.
264371f87433Sdalcinl 
264471f87433Sdalcinl    Level: advanced
264571f87433Sdalcinl 
264671f87433Sdalcinl    Reference:
264771f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
264871f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
264971f87433Sdalcinl 
265071f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
265171f87433Sdalcinl 
2652fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
265371f87433Sdalcinl @*/
2654fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetUseEW(SNES snes,PetscTruth flag)
265571f87433Sdalcinl {
265671f87433Sdalcinl   PetscFunctionBegin;
265771f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
265871f87433Sdalcinl   snes->ksp_ewconv = flag;
265971f87433Sdalcinl   PetscFunctionReturn(0);
266071f87433Sdalcinl }
266171f87433Sdalcinl 
266271f87433Sdalcinl #undef __FUNCT__
2663fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW"
266471f87433Sdalcinl /*@
2665fa9f3622SBarry Smith    SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method
266671f87433Sdalcinl    for computing relative tolerance for linear solvers within an
266771f87433Sdalcinl    inexact Newton method.
266871f87433Sdalcinl 
266971f87433Sdalcinl    Not Collective
267071f87433Sdalcinl 
267171f87433Sdalcinl    Input Parameter:
267271f87433Sdalcinl .  snes - SNES context
267371f87433Sdalcinl 
267471f87433Sdalcinl    Output Parameter:
267571f87433Sdalcinl .  flag - PETSC_TRUE or PETSC_FALSE
267671f87433Sdalcinl 
267771f87433Sdalcinl    Notes:
267871f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
267971f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
268071f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
268171f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
268271f87433Sdalcinl    solver.
268371f87433Sdalcinl 
268471f87433Sdalcinl    Level: advanced
268571f87433Sdalcinl 
268671f87433Sdalcinl    Reference:
268771f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
268871f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
268971f87433Sdalcinl 
269071f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
269171f87433Sdalcinl 
2692fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
269371f87433Sdalcinl @*/
2694fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetUseEW(SNES snes, PetscTruth *flag)
269571f87433Sdalcinl {
269671f87433Sdalcinl   PetscFunctionBegin;
269771f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
269871f87433Sdalcinl   PetscValidPointer(flag,2);
269971f87433Sdalcinl   *flag = snes->ksp_ewconv;
270071f87433Sdalcinl   PetscFunctionReturn(0);
270171f87433Sdalcinl }
270271f87433Sdalcinl 
270371f87433Sdalcinl #undef __FUNCT__
2704fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW"
270571f87433Sdalcinl /*@
2706fa9f3622SBarry Smith    SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker
270771f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
270871f87433Sdalcinl    Newton method.
270971f87433Sdalcinl 
271071f87433Sdalcinl    Collective on SNES
271171f87433Sdalcinl 
271271f87433Sdalcinl    Input Parameters:
271371f87433Sdalcinl +    snes - SNES context
271471f87433Sdalcinl .    version - version 1, 2 (default is 2) or 3
271571f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
271671f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
271771f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
271871f87433Sdalcinl              (0 <= gamma2 <= 1)
271971f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
272071f87433Sdalcinl .    alpha2 - power for safeguard
272171f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
272271f87433Sdalcinl 
272371f87433Sdalcinl    Note:
272471f87433Sdalcinl    Version 3 was contributed by Luis Chacon, June 2006.
272571f87433Sdalcinl 
272671f87433Sdalcinl    Use PETSC_DEFAULT to retain the default for any of the parameters.
272771f87433Sdalcinl 
272871f87433Sdalcinl    Level: advanced
272971f87433Sdalcinl 
273071f87433Sdalcinl    Reference:
273171f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
273271f87433Sdalcinl    inexact Newton method", Utah State University Math. Stat. Dept. Res.
273371f87433Sdalcinl    Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput.
273471f87433Sdalcinl 
273571f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters
273671f87433Sdalcinl 
2737fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW()
273871f87433Sdalcinl @*/
2739fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max,
274071f87433Sdalcinl 							    PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold)
274171f87433Sdalcinl {
2742fa9f3622SBarry Smith   SNESKSPEW *kctx;
274371f87433Sdalcinl   PetscFunctionBegin;
274471f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2745fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
274671f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
274771f87433Sdalcinl 
274871f87433Sdalcinl   if (version != PETSC_DEFAULT)   kctx->version   = version;
274971f87433Sdalcinl   if (rtol_0 != PETSC_DEFAULT)    kctx->rtol_0    = rtol_0;
275071f87433Sdalcinl   if (rtol_max != PETSC_DEFAULT)  kctx->rtol_max  = rtol_max;
275171f87433Sdalcinl   if (gamma != PETSC_DEFAULT)     kctx->gamma     = gamma;
275271f87433Sdalcinl   if (alpha != PETSC_DEFAULT)     kctx->alpha     = alpha;
275371f87433Sdalcinl   if (alpha2 != PETSC_DEFAULT)    kctx->alpha2    = alpha2;
275471f87433Sdalcinl   if (threshold != PETSC_DEFAULT) kctx->threshold = threshold;
275571f87433Sdalcinl 
275671f87433Sdalcinl   if (kctx->version < 1 || kctx->version > 3) {
275771f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version);
275871f87433Sdalcinl   }
275971f87433Sdalcinl   if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) {
276071f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0);
276171f87433Sdalcinl   }
276271f87433Sdalcinl   if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) {
276371f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max);
276471f87433Sdalcinl   }
276571f87433Sdalcinl   if (kctx->gamma < 0.0 || kctx->gamma > 1.0) {
276671f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma);
276771f87433Sdalcinl   }
276871f87433Sdalcinl   if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) {
276971f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha);
277071f87433Sdalcinl   }
277171f87433Sdalcinl   if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) {
277271f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold);
277371f87433Sdalcinl   }
277471f87433Sdalcinl   PetscFunctionReturn(0);
277571f87433Sdalcinl }
277671f87433Sdalcinl 
277771f87433Sdalcinl #undef __FUNCT__
2778fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW"
277971f87433Sdalcinl /*@
2780fa9f3622SBarry Smith    SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker
278171f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
278271f87433Sdalcinl    Newton method.
278371f87433Sdalcinl 
278471f87433Sdalcinl    Not Collective
278571f87433Sdalcinl 
278671f87433Sdalcinl    Input Parameters:
278771f87433Sdalcinl      snes - SNES context
278871f87433Sdalcinl 
278971f87433Sdalcinl    Output Parameters:
279071f87433Sdalcinl +    version - version 1, 2 (default is 2) or 3
279171f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
279271f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
279371f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
279471f87433Sdalcinl              (0 <= gamma2 <= 1)
279571f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
279671f87433Sdalcinl .    alpha2 - power for safeguard
279771f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
279871f87433Sdalcinl 
279971f87433Sdalcinl    Level: advanced
280071f87433Sdalcinl 
280171f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters
280271f87433Sdalcinl 
2803fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW()
280471f87433Sdalcinl @*/
2805fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max,
280671f87433Sdalcinl 							    PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold)
280771f87433Sdalcinl {
2808fa9f3622SBarry Smith   SNESKSPEW *kctx;
280971f87433Sdalcinl   PetscFunctionBegin;
281071f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2811fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
281271f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
281371f87433Sdalcinl   if(version)   *version   = kctx->version;
281471f87433Sdalcinl   if(rtol_0)    *rtol_0    = kctx->rtol_0;
281571f87433Sdalcinl   if(rtol_max)  *rtol_max  = kctx->rtol_max;
281671f87433Sdalcinl   if(gamma)     *gamma     = kctx->gamma;
281771f87433Sdalcinl   if(alpha)     *alpha     = kctx->alpha;
281871f87433Sdalcinl   if(alpha2)    *alpha2    = kctx->alpha2;
281971f87433Sdalcinl   if(threshold) *threshold = kctx->threshold;
282071f87433Sdalcinl   PetscFunctionReturn(0);
282171f87433Sdalcinl }
282271f87433Sdalcinl 
282371f87433Sdalcinl #undef __FUNCT__
2824fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve"
2825fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x)
282671f87433Sdalcinl {
282771f87433Sdalcinl   PetscErrorCode ierr;
2828fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
282971f87433Sdalcinl   PetscReal      rtol=PETSC_DEFAULT,stol;
283071f87433Sdalcinl 
283171f87433Sdalcinl   PetscFunctionBegin;
283271f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
283371f87433Sdalcinl   if (!snes->iter) { /* first time in, so use the original user rtol */
283471f87433Sdalcinl     rtol = kctx->rtol_0;
283571f87433Sdalcinl   } else {
283671f87433Sdalcinl     if (kctx->version == 1) {
283771f87433Sdalcinl       rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last;
283871f87433Sdalcinl       if (rtol < 0.0) rtol = -rtol;
283971f87433Sdalcinl       stol = pow(kctx->rtol_last,kctx->alpha2);
284071f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
284171f87433Sdalcinl     } else if (kctx->version == 2) {
284271f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
284371f87433Sdalcinl       stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha);
284471f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
284571f87433Sdalcinl     } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */
284671f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
284771f87433Sdalcinl       /* safeguard: avoid sharp decrease of rtol */
284871f87433Sdalcinl       stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha);
284971f87433Sdalcinl       stol = PetscMax(rtol,stol);
285071f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
285171f87433Sdalcinl       /* safeguard: avoid oversolving */
285271f87433Sdalcinl       stol = kctx->gamma*(snes->ttol)/snes->norm;
285371f87433Sdalcinl       stol = PetscMax(rtol,stol);
285471f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
285571f87433Sdalcinl     } else SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version);
285671f87433Sdalcinl   }
285771f87433Sdalcinl   /* safeguard: avoid rtol greater than one */
285871f87433Sdalcinl   rtol = PetscMin(rtol,kctx->rtol_max);
285971f87433Sdalcinl   ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
286071f87433Sdalcinl   ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr);
286171f87433Sdalcinl   PetscFunctionReturn(0);
286271f87433Sdalcinl }
286371f87433Sdalcinl 
286471f87433Sdalcinl #undef __FUNCT__
2865fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve"
2866fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x)
286771f87433Sdalcinl {
286871f87433Sdalcinl   PetscErrorCode ierr;
2869fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
287071f87433Sdalcinl   PCSide         pcside;
287171f87433Sdalcinl   Vec            lres;
287271f87433Sdalcinl 
287371f87433Sdalcinl   PetscFunctionBegin;
287471f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
287571f87433Sdalcinl   ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr);
287671f87433Sdalcinl   ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr);
287771f87433Sdalcinl   if (kctx->version == 1) {
287871f87433Sdalcinl     ierr = KSPGetPreconditionerSide(ksp,&pcside);CHKERRQ(ierr);
287971f87433Sdalcinl     if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */
288071f87433Sdalcinl       /* KSP residual is true linear residual */
288171f87433Sdalcinl       ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr);
288271f87433Sdalcinl     } else {
288371f87433Sdalcinl       /* KSP residual is preconditioned residual */
288471f87433Sdalcinl       /* compute true linear residual norm */
288571f87433Sdalcinl       ierr = VecDuplicate(b,&lres);CHKERRQ(ierr);
288671f87433Sdalcinl       ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr);
288771f87433Sdalcinl       ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr);
288871f87433Sdalcinl       ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr);
288971f87433Sdalcinl       ierr = VecDestroy(lres);CHKERRQ(ierr);
289071f87433Sdalcinl     }
289171f87433Sdalcinl   }
289271f87433Sdalcinl   PetscFunctionReturn(0);
289371f87433Sdalcinl }
289471f87433Sdalcinl 
289571f87433Sdalcinl #undef __FUNCT__
289671f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve"
289771f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x)
289871f87433Sdalcinl {
289971f87433Sdalcinl   PetscErrorCode ierr;
290071f87433Sdalcinl 
290171f87433Sdalcinl   PetscFunctionBegin;
2902fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr);  }
290371f87433Sdalcinl   ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);
2904fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); }
290571f87433Sdalcinl   PetscFunctionReturn(0);
290671f87433Sdalcinl }
2907