xref: /petsc/src/snes/interface/snes.c (revision e35cf81ddd3d80ce338419283d20072b93e92362)
163dd3a1aSKris Buschelman #define PETSCSNES_DLL
29b94acceSBarry Smith 
3b9147fbbSdalcinl #include "include/private/snesimpl.h"      /*I "petscsnes.h"  I*/
49b94acceSBarry Smith 
54c49b128SBarry Smith PetscTruth SNESRegisterAllCalled = PETSC_FALSE;
68ba1e511SMatthew Knepley PetscFList SNESList              = PETSC_NULL;
78ba1e511SMatthew Knepley 
88ba1e511SMatthew Knepley /* Logging support */
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)
180*e35cf81dSBarry Smith .  -snes_lag_jacobian <lag> - how often Jacobian is rebuilt (use -1 to never rebuild)
181b39c3a46SLois Curfman McInnes .  -snes_trtol <trtol> - trust region tolerance
1822492ecdbSBarry Smith .  -snes_no_convergence_test - skip convergence test in nonlinear
18382738288SBarry Smith                                solver; hence iterations will continue until max_it
1841fbbfb26SLois Curfman McInnes                                or some other criterion is reached. Saves expense
18582738288SBarry Smith                                of convergence test
186e8105e01SRichard Katz .  -snes_monitor <optional filename> - prints residual norm at each iteration. if no
187e8105e01SRichard Katz                                        filename given prints to stdout
188a6570f20SBarry Smith .  -snes_monitor_solution - plots solution at each iteration
189a6570f20SBarry Smith .  -snes_monitor_residual - plots residual (not its norm) at each iteration
190a6570f20SBarry Smith .  -snes_monitor_solution_update - plots update to solution at each iteration
191a6570f20SBarry Smith .  -snes_monitor_draw - plots residual norm at each iteration
192e24b481bSBarry Smith .  -snes_fd - use finite differences to compute Jacobian; very slow, only for testing
1935968eb51SBarry Smith .  -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration
194fee2055bSBarry Smith -  -snes_converged_reason - print the reason for convergence/divergence after each solve
19582738288SBarry Smith 
19682738288SBarry Smith     Options Database for Eisenstat-Walker method:
197fa9f3622SBarry Smith +  -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence
1984b27c08aSLois Curfman McInnes .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
19936851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
20036851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
20136851e7fSLois Curfman McInnes .  -snes_ksp_ew_gamma <gamma> - Sets gamma
20236851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha <alpha> - Sets alpha
20336851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
20436851e7fSLois Curfman McInnes -  -snes_ksp_ew_threshold <threshold> - Sets threshold
20582738288SBarry Smith 
20611ca99fdSLois Curfman McInnes    Notes:
20711ca99fdSLois Curfman McInnes    To see all options, run your program with the -help option or consult
20811ca99fdSLois Curfman McInnes    the users manual.
20983e2fdc7SBarry Smith 
21036851e7fSLois Curfman McInnes    Level: beginner
21136851e7fSLois Curfman McInnes 
2129b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database
2139b94acceSBarry Smith 
21469ed319cSSatish Balay .seealso: SNESSetOptionsPrefix()
2159b94acceSBarry Smith @*/
21663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFromOptions(SNES snes)
2179b94acceSBarry Smith {
218f1af5d2fSBarry Smith   PetscTruth              flg;
219a8054027SBarry Smith   PetscInt                i,indx,lag;
22085385478SLisandro Dalcin   const char              *deft = SNESLS;
22185385478SLisandro Dalcin   const char              *convtests[] = {"default","skip"};
22285385478SLisandro Dalcin   SNESKSPEW               *kctx = NULL;
223e8105e01SRichard Katz   char                    type[256], monfilename[PETSC_MAX_PATH_LEN];
22423d894e5SBarry Smith   PetscViewerASCIIMonitor monviewer;
22585385478SLisandro Dalcin   PetscErrorCode          ierr;
2269b94acceSBarry Smith 
2273a40ed3dSBarry Smith   PetscFunctionBegin;
2284482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
229ca161407SBarry Smith 
2307adad957SLisandro Dalcin   ierr = PetscOptionsBegin(((PetscObject)snes)->comm,((PetscObject)snes)->prefix,"Nonlinear solver (SNES) options","SNES");CHKERRQ(ierr);
231186905e3SBarry Smith     if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
2327adad957SLisandro Dalcin     if (((PetscObject)snes)->type_name) { deft = ((PetscObject)snes)->type_name; }
233b0a32e0cSBarry Smith     ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr);
234d64ed03dSBarry Smith     if (flg) {
235186905e3SBarry Smith       ierr = SNESSetType(snes,type);CHKERRQ(ierr);
2367adad957SLisandro Dalcin     } else if (!((PetscObject)snes)->type_name) {
237186905e3SBarry Smith       ierr = SNESSetType(snes,deft);CHKERRQ(ierr);
238d64ed03dSBarry Smith     }
239909c8a9fSBarry Smith     ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr);
24093c39befSBarry Smith 
24187828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_stol","Stop if step length less then","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr);
24270441072SBarry Smith     ierr = PetscOptionsReal("-snes_atol","Stop if function norm less then","SNESSetTolerances",snes->abstol,&snes->abstol,0);CHKERRQ(ierr);
243186905e3SBarry Smith 
24487828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less then","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr);
245b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr);
246b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr);
24750ffb88aSMatthew Knepley     ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr);
248ddf469c8SBarry Smith     ierr = PetscOptionsInt("-snes_max_linear_solve_fail","Maximum failures in linear solves allowed","SNESSetMaxLinearSolveFailures",snes->maxLinearSolveFailures,&snes->maxLinearSolveFailures,PETSC_NULL);CHKERRQ(ierr);
24985385478SLisandro Dalcin 
250a8054027SBarry Smith     ierr = PetscOptionsInt("-snes_lag_preconditioner","How often to rebuild preconditioner","SNESSetLagPreconditioner",snes->lagpreconditioner,&lag,&flg);CHKERRQ(ierr);
251a8054027SBarry Smith     if (flg) {
252a8054027SBarry Smith       ierr = SNESSetLagPreconditioner(snes,lag);CHKERRQ(ierr);
253a8054027SBarry Smith     }
254*e35cf81dSBarry Smith     ierr = PetscOptionsInt("-snes_lag_jacobian","How often to rebuild Jacobian","SNESSetLagJacobian",snes->lagjacobian,&lag,&flg);CHKERRQ(ierr);
255*e35cf81dSBarry Smith     if (flg) {
256*e35cf81dSBarry Smith       ierr = SNESSetLagJacobian(snes,lag);CHKERRQ(ierr);
257*e35cf81dSBarry 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 
293a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_ratiomonitor","Monitor ratios of norms of function","SNESMonitorSetRatio","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
294eabae89aSBarry Smith     if (flg) {
295050a712dSBarry Smith       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr);
296f1bef1bcSMatthew Knepley       ierr = SNESMonitorSetRatio(snes,monviewer);CHKERRQ(ierr);
297e8105e01SRichard Katz     }
298eabae89aSBarry Smith 
299a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor_short","Monitor norm of function (fewer digits)","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
300eabae89aSBarry Smith     if (flg) {
301050a712dSBarry Smith       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr);
30223d894e5SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefaultShort,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
303eabae89aSBarry Smith     }
304eabae89aSBarry Smith 
305a6570f20SBarry Smith     ierr = PetscOptionsName("-snes_monitor_solution","Plot solution at each iteration","SNESMonitorSolution",&flg);CHKERRQ(ierr);
306a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolution,0,0);CHKERRQ(ierr);}
307a6570f20SBarry Smith     ierr = PetscOptionsName("-snes_monitor_solution_update","Plot correction at each iteration","SNESMonitorSolutionUpdate",&flg);CHKERRQ(ierr);
308a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolutionUpdate,0,0);CHKERRQ(ierr);}
309a6570f20SBarry Smith     ierr = PetscOptionsName("-snes_monitor_residual","Plot residual at each iteration","SNESMonitorResidual",&flg);CHKERRQ(ierr);
310a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorResidual,0,0);CHKERRQ(ierr);}
311a6570f20SBarry Smith     ierr = PetscOptionsName("-snes_monitor_draw","Plot function norm at each iteration","SNESMonitorLG",&flg);CHKERRQ(ierr);
312a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
313e24b481bSBarry Smith 
314b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",&flg);CHKERRQ(ierr);
3154b27c08aSLois Curfman McInnes     if (flg) {
316186905e3SBarry Smith       ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr);
317ae15b995SBarry Smith       ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr);
3189b94acceSBarry Smith     }
319639f9d9dSBarry Smith 
32076b2cf59SMatthew Knepley     for(i = 0; i < numberofsetfromoptions; i++) {
32176b2cf59SMatthew Knepley       ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr);
32276b2cf59SMatthew Knepley     }
32376b2cf59SMatthew Knepley 
324e7788613SBarry Smith     if (snes->ops->setfromoptions) {
325e7788613SBarry Smith       ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr);
326639f9d9dSBarry Smith     }
327b0a32e0cSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
3284bbc92c1SBarry Smith 
3291cee3971SBarry Smith 
3301cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
33185385478SLisandro Dalcin   ierr = KSPSetFromOptions(snes->ksp);CHKERRQ(ierr);
33293993e2dSLois Curfman McInnes 
3333a40ed3dSBarry Smith   PetscFunctionReturn(0);
3349b94acceSBarry Smith }
3359b94acceSBarry Smith 
336a847f771SSatish Balay 
3374a2ae208SSatish Balay #undef __FUNCT__
3384a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext"
3399b94acceSBarry Smith /*@
3409b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
3419b94acceSBarry Smith    the nonlinear solvers.
3429b94acceSBarry Smith 
343fee21e36SBarry Smith    Collective on SNES
344fee21e36SBarry Smith 
345c7afd0dbSLois Curfman McInnes    Input Parameters:
346c7afd0dbSLois Curfman McInnes +  snes - the SNES context
347c7afd0dbSLois Curfman McInnes -  usrP - optional user context
348c7afd0dbSLois Curfman McInnes 
34936851e7fSLois Curfman McInnes    Level: intermediate
35036851e7fSLois Curfman McInnes 
3519b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
3529b94acceSBarry Smith 
3539b94acceSBarry Smith .seealso: SNESGetApplicationContext()
3549b94acceSBarry Smith @*/
35563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetApplicationContext(SNES snes,void *usrP)
3569b94acceSBarry Smith {
3573a40ed3dSBarry Smith   PetscFunctionBegin;
3584482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
3599b94acceSBarry Smith   snes->user		= usrP;
3603a40ed3dSBarry Smith   PetscFunctionReturn(0);
3619b94acceSBarry Smith }
36274679c65SBarry Smith 
3634a2ae208SSatish Balay #undef __FUNCT__
3644a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext"
3659b94acceSBarry Smith /*@C
3669b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
3679b94acceSBarry Smith    nonlinear solvers.
3689b94acceSBarry Smith 
369c7afd0dbSLois Curfman McInnes    Not Collective
370c7afd0dbSLois Curfman McInnes 
3719b94acceSBarry Smith    Input Parameter:
3729b94acceSBarry Smith .  snes - SNES context
3739b94acceSBarry Smith 
3749b94acceSBarry Smith    Output Parameter:
3759b94acceSBarry Smith .  usrP - user context
3769b94acceSBarry Smith 
37736851e7fSLois Curfman McInnes    Level: intermediate
37836851e7fSLois Curfman McInnes 
3799b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
3809b94acceSBarry Smith 
3819b94acceSBarry Smith .seealso: SNESSetApplicationContext()
3829b94acceSBarry Smith @*/
38363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetApplicationContext(SNES snes,void **usrP)
3849b94acceSBarry Smith {
3853a40ed3dSBarry Smith   PetscFunctionBegin;
3864482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
3879b94acceSBarry Smith   *usrP = snes->user;
3883a40ed3dSBarry Smith   PetscFunctionReturn(0);
3899b94acceSBarry Smith }
39074679c65SBarry Smith 
3914a2ae208SSatish Balay #undef __FUNCT__
3924a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber"
3939b94acceSBarry Smith /*@
394c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
395c8228a4eSBarry Smith    at this time.
3969b94acceSBarry Smith 
397c7afd0dbSLois Curfman McInnes    Not Collective
398c7afd0dbSLois Curfman McInnes 
3999b94acceSBarry Smith    Input Parameter:
4009b94acceSBarry Smith .  snes - SNES context
4019b94acceSBarry Smith 
4029b94acceSBarry Smith    Output Parameter:
4039b94acceSBarry Smith .  iter - iteration number
4049b94acceSBarry Smith 
405c8228a4eSBarry Smith    Notes:
406c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
407c8228a4eSBarry Smith 
408c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
40908405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
41008405cd6SLois Curfman McInnes .vb
41108405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
41208405cd6SLois Curfman McInnes       if (!(it % 2)) {
41308405cd6SLois Curfman McInnes         [compute Jacobian here]
41408405cd6SLois Curfman McInnes       }
41508405cd6SLois Curfman McInnes .ve
416c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
41708405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
418c8228a4eSBarry Smith 
41936851e7fSLois Curfman McInnes    Level: intermediate
42036851e7fSLois Curfman McInnes 
4212b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number,
4222b668275SBarry Smith 
423b850b91aSLisandro Dalcin .seealso:   SNESGetFunctionNorm(), SNESGetLinearSolveIterations()
4249b94acceSBarry Smith @*/
42563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetIterationNumber(SNES snes,PetscInt* iter)
4269b94acceSBarry Smith {
4273a40ed3dSBarry Smith   PetscFunctionBegin;
4284482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
4294482741eSBarry Smith   PetscValidIntPointer(iter,2);
4309b94acceSBarry Smith   *iter = snes->iter;
4313a40ed3dSBarry Smith   PetscFunctionReturn(0);
4329b94acceSBarry Smith }
43374679c65SBarry Smith 
4344a2ae208SSatish Balay #undef __FUNCT__
4354a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm"
4369b94acceSBarry Smith /*@
4379b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
4389b94acceSBarry Smith    with SNESSSetFunction().
4399b94acceSBarry Smith 
440c7afd0dbSLois Curfman McInnes    Collective on SNES
441c7afd0dbSLois Curfman McInnes 
4429b94acceSBarry Smith    Input Parameter:
4439b94acceSBarry Smith .  snes - SNES context
4449b94acceSBarry Smith 
4459b94acceSBarry Smith    Output Parameter:
4469b94acceSBarry Smith .  fnorm - 2-norm of function
4479b94acceSBarry Smith 
44836851e7fSLois Curfman McInnes    Level: intermediate
44936851e7fSLois Curfman McInnes 
4509b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
451a86d99e1SLois Curfman McInnes 
452b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations()
4539b94acceSBarry Smith @*/
45471f87433Sdalcinl PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunctionNorm(SNES snes,PetscReal *fnorm)
4559b94acceSBarry Smith {
4563a40ed3dSBarry Smith   PetscFunctionBegin;
4574482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
4584482741eSBarry Smith   PetscValidScalarPointer(fnorm,2);
4599b94acceSBarry Smith   *fnorm = snes->norm;
4603a40ed3dSBarry Smith   PetscFunctionReturn(0);
4619b94acceSBarry Smith }
46274679c65SBarry Smith 
4634a2ae208SSatish Balay #undef __FUNCT__
464b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures"
4659b94acceSBarry Smith /*@
466b850b91aSLisandro Dalcin    SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps
4679b94acceSBarry Smith    attempted by the nonlinear solver.
4689b94acceSBarry Smith 
469c7afd0dbSLois Curfman McInnes    Not Collective
470c7afd0dbSLois Curfman McInnes 
4719b94acceSBarry Smith    Input Parameter:
4729b94acceSBarry Smith .  snes - SNES context
4739b94acceSBarry Smith 
4749b94acceSBarry Smith    Output Parameter:
4759b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
4769b94acceSBarry Smith 
477c96a6f78SLois Curfman McInnes    Notes:
478c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
479c96a6f78SLois Curfman McInnes 
48036851e7fSLois Curfman McInnes    Level: intermediate
48136851e7fSLois Curfman McInnes 
4829b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
48358ebbce7SBarry Smith 
484e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
48558ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures()
4869b94acceSBarry Smith @*/
487b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails)
4889b94acceSBarry Smith {
4893a40ed3dSBarry Smith   PetscFunctionBegin;
4904482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
4914482741eSBarry Smith   PetscValidIntPointer(nfails,2);
49250ffb88aSMatthew Knepley   *nfails = snes->numFailures;
49350ffb88aSMatthew Knepley   PetscFunctionReturn(0);
49450ffb88aSMatthew Knepley }
49550ffb88aSMatthew Knepley 
49650ffb88aSMatthew Knepley #undef __FUNCT__
497b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures"
49850ffb88aSMatthew Knepley /*@
499b850b91aSLisandro Dalcin    SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps
50050ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
50150ffb88aSMatthew Knepley 
50250ffb88aSMatthew Knepley    Not Collective
50350ffb88aSMatthew Knepley 
50450ffb88aSMatthew Knepley    Input Parameters:
50550ffb88aSMatthew Knepley +  snes     - SNES context
50650ffb88aSMatthew Knepley -  maxFails - maximum of unsuccessful steps
50750ffb88aSMatthew Knepley 
50850ffb88aSMatthew Knepley    Level: intermediate
50950ffb88aSMatthew Knepley 
51050ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
51158ebbce7SBarry Smith 
512e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
51358ebbce7SBarry Smith           SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
51450ffb88aSMatthew Knepley @*/
515b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails)
51650ffb88aSMatthew Knepley {
51750ffb88aSMatthew Knepley   PetscFunctionBegin;
5184482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
51950ffb88aSMatthew Knepley   snes->maxFailures = maxFails;
52050ffb88aSMatthew Knepley   PetscFunctionReturn(0);
52150ffb88aSMatthew Knepley }
52250ffb88aSMatthew Knepley 
52350ffb88aSMatthew Knepley #undef __FUNCT__
524b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures"
52550ffb88aSMatthew Knepley /*@
526b850b91aSLisandro Dalcin    SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps
52750ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
52850ffb88aSMatthew Knepley 
52950ffb88aSMatthew Knepley    Not Collective
53050ffb88aSMatthew Knepley 
53150ffb88aSMatthew Knepley    Input Parameter:
53250ffb88aSMatthew Knepley .  snes     - SNES context
53350ffb88aSMatthew Knepley 
53450ffb88aSMatthew Knepley    Output Parameter:
53550ffb88aSMatthew Knepley .  maxFails - maximum of unsuccessful steps
53650ffb88aSMatthew Knepley 
53750ffb88aSMatthew Knepley    Level: intermediate
53850ffb88aSMatthew Knepley 
53950ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
54058ebbce7SBarry Smith 
541e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
54258ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
54358ebbce7SBarry Smith 
54450ffb88aSMatthew Knepley @*/
545b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails)
54650ffb88aSMatthew Knepley {
54750ffb88aSMatthew Knepley   PetscFunctionBegin;
5484482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
5494482741eSBarry Smith   PetscValidIntPointer(maxFails,2);
55050ffb88aSMatthew Knepley   *maxFails = snes->maxFailures;
5513a40ed3dSBarry Smith   PetscFunctionReturn(0);
5529b94acceSBarry Smith }
553a847f771SSatish Balay 
5544a2ae208SSatish Balay #undef __FUNCT__
5552541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals"
5562541af92SBarry Smith /*@
5572541af92SBarry Smith    SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations
5582541af92SBarry Smith      done by SNES.
5592541af92SBarry Smith 
5602541af92SBarry Smith    Not Collective
5612541af92SBarry Smith 
5622541af92SBarry Smith    Input Parameter:
5632541af92SBarry Smith .  snes     - SNES context
5642541af92SBarry Smith 
5652541af92SBarry Smith    Output Parameter:
5662541af92SBarry Smith .  nfuncs - number of evaluations
5672541af92SBarry Smith 
5682541af92SBarry Smith    Level: intermediate
5692541af92SBarry Smith 
5702541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
57158ebbce7SBarry Smith 
572e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures()
5732541af92SBarry Smith @*/
5742541af92SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs)
5752541af92SBarry Smith {
5762541af92SBarry Smith   PetscFunctionBegin;
5772541af92SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
5782541af92SBarry Smith   PetscValidIntPointer(nfuncs,2);
5792541af92SBarry Smith   *nfuncs = snes->nfuncs;
5802541af92SBarry Smith   PetscFunctionReturn(0);
5812541af92SBarry Smith }
5822541af92SBarry Smith 
5832541af92SBarry Smith #undef __FUNCT__
5843d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures"
5853d4c4710SBarry Smith /*@
5863d4c4710SBarry Smith    SNESGetLinearSolveFailures - Gets the number of failed (non-converged)
5873d4c4710SBarry Smith    linear solvers.
5883d4c4710SBarry Smith 
5893d4c4710SBarry Smith    Not Collective
5903d4c4710SBarry Smith 
5913d4c4710SBarry Smith    Input Parameter:
5923d4c4710SBarry Smith .  snes - SNES context
5933d4c4710SBarry Smith 
5943d4c4710SBarry Smith    Output Parameter:
5953d4c4710SBarry Smith .  nfails - number of failed solves
5963d4c4710SBarry Smith 
5973d4c4710SBarry Smith    Notes:
5983d4c4710SBarry Smith    This counter is reset to zero for each successive call to SNESSolve().
5993d4c4710SBarry Smith 
6003d4c4710SBarry Smith    Level: intermediate
6013d4c4710SBarry Smith 
6023d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
60358ebbce7SBarry Smith 
604e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures()
6053d4c4710SBarry Smith @*/
6063d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails)
6073d4c4710SBarry Smith {
6083d4c4710SBarry Smith   PetscFunctionBegin;
6093d4c4710SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
6103d4c4710SBarry Smith   PetscValidIntPointer(nfails,2);
6113d4c4710SBarry Smith   *nfails = snes->numLinearSolveFailures;
6123d4c4710SBarry Smith   PetscFunctionReturn(0);
6133d4c4710SBarry Smith }
6143d4c4710SBarry Smith 
6153d4c4710SBarry Smith #undef __FUNCT__
6163d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures"
6173d4c4710SBarry Smith /*@
6183d4c4710SBarry Smith    SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts
6193d4c4710SBarry Smith    allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE
6203d4c4710SBarry Smith 
6213d4c4710SBarry Smith    Collective on SNES
6223d4c4710SBarry Smith 
6233d4c4710SBarry Smith    Input Parameters:
6243d4c4710SBarry Smith +  snes     - SNES context
6253d4c4710SBarry Smith -  maxFails - maximum allowed linear solve failures
6263d4c4710SBarry Smith 
6273d4c4710SBarry Smith    Level: intermediate
6283d4c4710SBarry Smith 
629a6796414SBarry Smith    Notes: By default this is 0; that is SNES returns on the first failed linear solve
6303d4c4710SBarry Smith 
6313d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
6323d4c4710SBarry Smith 
63358ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations()
6343d4c4710SBarry Smith @*/
6353d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails)
6363d4c4710SBarry Smith {
6373d4c4710SBarry Smith   PetscFunctionBegin;
6383d4c4710SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
6393d4c4710SBarry Smith   snes->maxLinearSolveFailures = maxFails;
6403d4c4710SBarry Smith   PetscFunctionReturn(0);
6413d4c4710SBarry Smith }
6423d4c4710SBarry Smith 
6433d4c4710SBarry Smith #undef __FUNCT__
6443d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures"
6453d4c4710SBarry Smith /*@
6463d4c4710SBarry Smith    SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that
6473d4c4710SBarry Smith      are allowed before SNES terminates
6483d4c4710SBarry Smith 
6493d4c4710SBarry Smith    Not Collective
6503d4c4710SBarry Smith 
6513d4c4710SBarry Smith    Input Parameter:
6523d4c4710SBarry Smith .  snes     - SNES context
6533d4c4710SBarry Smith 
6543d4c4710SBarry Smith    Output Parameter:
6553d4c4710SBarry Smith .  maxFails - maximum of unsuccessful solves allowed
6563d4c4710SBarry Smith 
6573d4c4710SBarry Smith    Level: intermediate
6583d4c4710SBarry Smith 
6593d4c4710SBarry Smith    Notes: By default this is 1; that is SNES returns on the first failed linear solve
6603d4c4710SBarry Smith 
6613d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
6623d4c4710SBarry Smith 
663e1c61ce8SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(),
6643d4c4710SBarry Smith @*/
6653d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails)
6663d4c4710SBarry Smith {
6673d4c4710SBarry Smith   PetscFunctionBegin;
6683d4c4710SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
6693d4c4710SBarry Smith   PetscValidIntPointer(maxFails,2);
6703d4c4710SBarry Smith   *maxFails = snes->maxLinearSolveFailures;
6713d4c4710SBarry Smith   PetscFunctionReturn(0);
6723d4c4710SBarry Smith }
6733d4c4710SBarry Smith 
6743d4c4710SBarry Smith #undef __FUNCT__
675b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations"
676c96a6f78SLois Curfman McInnes /*@
677b850b91aSLisandro Dalcin    SNESGetLinearSolveIterations - Gets the total number of linear iterations
678c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
679c96a6f78SLois Curfman McInnes 
680c7afd0dbSLois Curfman McInnes    Not Collective
681c7afd0dbSLois Curfman McInnes 
682c96a6f78SLois Curfman McInnes    Input Parameter:
683c96a6f78SLois Curfman McInnes .  snes - SNES context
684c96a6f78SLois Curfman McInnes 
685c96a6f78SLois Curfman McInnes    Output Parameter:
686c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
687c96a6f78SLois Curfman McInnes 
688c96a6f78SLois Curfman McInnes    Notes:
689c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
690c96a6f78SLois Curfman McInnes 
69136851e7fSLois Curfman McInnes    Level: intermediate
69236851e7fSLois Curfman McInnes 
693c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
6942b668275SBarry Smith 
69558ebbce7SBarry Smith .seealso:  SNESGetIterationNumber(), SNESGetFunctionNorm()S, NESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures()
696c96a6f78SLois Curfman McInnes @*/
697b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveIterations(SNES snes,PetscInt* lits)
698c96a6f78SLois Curfman McInnes {
6993a40ed3dSBarry Smith   PetscFunctionBegin;
7004482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
7014482741eSBarry Smith   PetscValidIntPointer(lits,2);
702c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
7033a40ed3dSBarry Smith   PetscFunctionReturn(0);
704c96a6f78SLois Curfman McInnes }
705c96a6f78SLois Curfman McInnes 
7064a2ae208SSatish Balay #undef __FUNCT__
70794b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP"
70852baeb72SSatish Balay /*@
70994b7f48cSBarry Smith    SNESGetKSP - Returns the KSP context for a SNES solver.
7109b94acceSBarry Smith 
71194b7f48cSBarry Smith    Not Collective, but if SNES object is parallel, then KSP object is parallel
712c7afd0dbSLois Curfman McInnes 
7139b94acceSBarry Smith    Input Parameter:
7149b94acceSBarry Smith .  snes - the SNES context
7159b94acceSBarry Smith 
7169b94acceSBarry Smith    Output Parameter:
71794b7f48cSBarry Smith .  ksp - the KSP context
7189b94acceSBarry Smith 
7199b94acceSBarry Smith    Notes:
72094b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
7219b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
7222999313aSBarry Smith    PC contexts as well.
7239b94acceSBarry Smith 
72436851e7fSLois Curfman McInnes    Level: beginner
72536851e7fSLois Curfman McInnes 
72694b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
7279b94acceSBarry Smith 
7282999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
7299b94acceSBarry Smith @*/
73063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetKSP(SNES snes,KSP *ksp)
7319b94acceSBarry Smith {
7321cee3971SBarry Smith   PetscErrorCode ierr;
7331cee3971SBarry Smith 
7343a40ed3dSBarry Smith   PetscFunctionBegin;
7354482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
7364482741eSBarry Smith   PetscValidPointer(ksp,2);
7371cee3971SBarry Smith 
7381cee3971SBarry Smith   if (!snes->ksp) {
7391cee3971SBarry Smith     ierr = KSPCreate(((PetscObject)snes)->comm,&snes->ksp);CHKERRQ(ierr);
7401cee3971SBarry Smith     ierr = PetscObjectIncrementTabLevel((PetscObject)snes->ksp,(PetscObject)snes,1);CHKERRQ(ierr);
7411cee3971SBarry Smith     ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr);
7421cee3971SBarry Smith   }
74394b7f48cSBarry Smith   *ksp = snes->ksp;
7443a40ed3dSBarry Smith   PetscFunctionReturn(0);
7459b94acceSBarry Smith }
74682bf6240SBarry Smith 
7474a2ae208SSatish Balay #undef __FUNCT__
7482999313aSBarry Smith #define __FUNCT__ "SNESSetKSP"
7492999313aSBarry Smith /*@
7502999313aSBarry Smith    SNESSetKSP - Sets a KSP context for the SNES object to use
7512999313aSBarry Smith 
7522999313aSBarry Smith    Not Collective, but the SNES and KSP objects must live on the same MPI_Comm
7532999313aSBarry Smith 
7542999313aSBarry Smith    Input Parameters:
7552999313aSBarry Smith +  snes - the SNES context
7562999313aSBarry Smith -  ksp - the KSP context
7572999313aSBarry Smith 
7582999313aSBarry Smith    Notes:
7592999313aSBarry Smith    The SNES object already has its KSP object, you can obtain with SNESGetKSP()
7602999313aSBarry Smith    so this routine is rarely needed.
7612999313aSBarry Smith 
7622999313aSBarry Smith    The KSP object that is already in the SNES object has its reference count
7632999313aSBarry Smith    decreased by one.
7642999313aSBarry Smith 
7652999313aSBarry Smith    Level: developer
7662999313aSBarry Smith 
7672999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
7682999313aSBarry Smith 
7692999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
7702999313aSBarry Smith @*/
7712999313aSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetKSP(SNES snes,KSP ksp)
7722999313aSBarry Smith {
7732999313aSBarry Smith   PetscErrorCode ierr;
7742999313aSBarry Smith 
7752999313aSBarry Smith   PetscFunctionBegin;
7762999313aSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
7772999313aSBarry Smith   PetscValidHeaderSpecific(ksp,KSP_COOKIE,2);
7782999313aSBarry Smith   PetscCheckSameComm(snes,1,ksp,2);
7797dcf0eaaSdalcinl   ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr);
780906ed7ccSBarry Smith   if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);}
7812999313aSBarry Smith   snes->ksp = ksp;
7822999313aSBarry Smith   PetscFunctionReturn(0);
7832999313aSBarry Smith }
7842999313aSBarry Smith 
7857adad957SLisandro Dalcin #if 0
7862999313aSBarry Smith #undef __FUNCT__
7874a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc"
7886849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj)
789e24b481bSBarry Smith {
790e24b481bSBarry Smith   PetscFunctionBegin;
791e24b481bSBarry Smith   PetscFunctionReturn(0);
792e24b481bSBarry Smith }
7937adad957SLisandro Dalcin #endif
794e24b481bSBarry Smith 
7959b94acceSBarry Smith /* -----------------------------------------------------------*/
7964a2ae208SSatish Balay #undef __FUNCT__
7974a2ae208SSatish Balay #define __FUNCT__ "SNESCreate"
79852baeb72SSatish Balay /*@
7999b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
8009b94acceSBarry Smith 
801c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
802c7afd0dbSLois Curfman McInnes 
803c7afd0dbSLois Curfman McInnes    Input Parameters:
804906ed7ccSBarry Smith .  comm - MPI communicator
8059b94acceSBarry Smith 
8069b94acceSBarry Smith    Output Parameter:
8079b94acceSBarry Smith .  outsnes - the new SNES context
8089b94acceSBarry Smith 
809c7afd0dbSLois Curfman McInnes    Options Database Keys:
810c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
811c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
812c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
813c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
814c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
815c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
816c1f60f51SBarry Smith 
81736851e7fSLois Curfman McInnes    Level: beginner
81836851e7fSLois Curfman McInnes 
8199b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
8209b94acceSBarry Smith 
821a8054027SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNES, SNESSetLagPreconditioner()
822a8054027SBarry Smith 
8239b94acceSBarry Smith @*/
82463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESCreate(MPI_Comm comm,SNES *outsnes)
8259b94acceSBarry Smith {
826dfbe8321SBarry Smith   PetscErrorCode      ierr;
8279b94acceSBarry Smith   SNES                snes;
828fa9f3622SBarry Smith   SNESKSPEW           *kctx;
82937fcc0dbSBarry Smith 
8303a40ed3dSBarry Smith   PetscFunctionBegin;
831ed1caa07SMatthew Knepley   PetscValidPointer(outsnes,2);
8328ba1e511SMatthew Knepley   *outsnes = PETSC_NULL;
8338ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
8348ba1e511SMatthew Knepley   ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr);
8358ba1e511SMatthew Knepley #endif
8368ba1e511SMatthew Knepley 
837e7788613SBarry Smith   ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr);
8387adad957SLisandro Dalcin 
83985385478SLisandro Dalcin   snes->ops->converged    = SNESDefaultConverged;
8409b94acceSBarry Smith   snes->max_its           = 50;
8419750a799SBarry Smith   snes->max_funcs	  = 10000;
8429b94acceSBarry Smith   snes->norm		  = 0.0;
843b4874afaSBarry Smith   snes->rtol		  = 1.e-8;
844b4874afaSBarry Smith   snes->ttol              = 0.0;
84570441072SBarry Smith   snes->abstol		  = 1.e-50;
8469b94acceSBarry Smith   snes->xtol		  = 1.e-8;
8474b27c08aSLois Curfman McInnes   snes->deltatol	  = 1.e-12;
8489b94acceSBarry Smith   snes->nfuncs            = 0;
84950ffb88aSMatthew Knepley   snes->numFailures       = 0;
85050ffb88aSMatthew Knepley   snes->maxFailures       = 1;
8517a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
852*e35cf81dSBarry Smith   snes->lagjacobian       = 1;
853a8054027SBarry Smith   snes->lagpreconditioner = 1;
854639f9d9dSBarry Smith   snes->numbermonitors    = 0;
8559b94acceSBarry Smith   snes->data              = 0;
8564dc4c822SBarry Smith   snes->setupcalled       = PETSC_FALSE;
857186905e3SBarry Smith   snes->ksp_ewconv        = PETSC_FALSE;
8586f24a144SLois Curfman McInnes   snes->vwork             = 0;
8596f24a144SLois Curfman McInnes   snes->nwork             = 0;
860758f92a0SBarry Smith   snes->conv_hist_len     = 0;
861758f92a0SBarry Smith   snes->conv_hist_max     = 0;
862758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
863758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
864758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
865184914b5SBarry Smith   snes->reason            = SNES_CONVERGED_ITERATING;
8669b94acceSBarry Smith 
8673d4c4710SBarry Smith   snes->numLinearSolveFailures = 0;
8683d4c4710SBarry Smith   snes->maxLinearSolveFailures = 1;
8693d4c4710SBarry Smith 
8709b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
87138f2d2fdSLisandro Dalcin   ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr);
8729b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
8739b94acceSBarry Smith   kctx->version     = 2;
8749b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
8759b94acceSBarry Smith                              this was too large for some test cases */
8769b94acceSBarry Smith   kctx->rtol_last   = 0;
8779b94acceSBarry Smith   kctx->rtol_max    = .9;
8789b94acceSBarry Smith   kctx->gamma       = 1.0;
87971f87433Sdalcinl   kctx->alpha       = .5*(1.0 + sqrt(5.0));
88071f87433Sdalcinl   kctx->alpha2      = kctx->alpha;
8819b94acceSBarry Smith   kctx->threshold   = .1;
8829b94acceSBarry Smith   kctx->lresid_last = 0;
8839b94acceSBarry Smith   kctx->norm_last   = 0;
8849b94acceSBarry Smith 
8859b94acceSBarry Smith   *outsnes = snes;
88600036973SBarry Smith   ierr = PetscPublishAll(snes);CHKERRQ(ierr);
8873a40ed3dSBarry Smith   PetscFunctionReturn(0);
8889b94acceSBarry Smith }
8899b94acceSBarry Smith 
8904a2ae208SSatish Balay #undef __FUNCT__
8914a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction"
8929b94acceSBarry Smith /*@C
8939b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
8949b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
8959b94acceSBarry Smith    equations.
8969b94acceSBarry Smith 
897fee21e36SBarry Smith    Collective on SNES
898fee21e36SBarry Smith 
899c7afd0dbSLois Curfman McInnes    Input Parameters:
900c7afd0dbSLois Curfman McInnes +  snes - the SNES context
901c7afd0dbSLois Curfman McInnes .  r - vector to store function value
902de044059SHong Zhang .  func - function evaluation routine
903c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
904c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
9059b94acceSBarry Smith 
906c7afd0dbSLois Curfman McInnes    Calling sequence of func:
9078d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
908c7afd0dbSLois Curfman McInnes 
909313e4042SLois Curfman McInnes .  f - function vector
910c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
9119b94acceSBarry Smith 
9129b94acceSBarry Smith    Notes:
9139b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
9149b94acceSBarry Smith $      f'(x) x = -f(x),
915c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
9169b94acceSBarry Smith 
91736851e7fSLois Curfman McInnes    Level: beginner
91836851e7fSLois Curfman McInnes 
9199b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
9209b94acceSBarry Smith 
921a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
9229b94acceSBarry Smith @*/
92363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx)
9249b94acceSBarry Smith {
92585385478SLisandro Dalcin   PetscErrorCode ierr;
9263a40ed3dSBarry Smith   PetscFunctionBegin;
9274482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
9284482741eSBarry Smith   PetscValidHeaderSpecific(r,VEC_COOKIE,2);
929c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,r,2);
93085385478SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr);
93185385478SLisandro Dalcin   if (snes->vec_func) { ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr); }
932e7788613SBarry Smith   snes->ops->computefunction = func;
93385385478SLisandro Dalcin   snes->vec_func             = r;
9349b94acceSBarry Smith   snes->funP                 = ctx;
9353a40ed3dSBarry Smith   PetscFunctionReturn(0);
9369b94acceSBarry Smith }
9379b94acceSBarry Smith 
9383ab0aad5SBarry Smith /* --------------------------------------------------------------- */
9393ab0aad5SBarry Smith #undef __FUNCT__
9401096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs"
9411096aae1SMatthew Knepley /*@C
9421096aae1SMatthew Knepley    SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set
9431096aae1SMatthew Knepley    it assumes a zero right hand side.
9441096aae1SMatthew Knepley 
9451096aae1SMatthew Knepley    Collective on SNES
9461096aae1SMatthew Knepley 
9471096aae1SMatthew Knepley    Input Parameter:
9481096aae1SMatthew Knepley .  snes - the SNES context
9491096aae1SMatthew Knepley 
9501096aae1SMatthew Knepley    Output Parameter:
951bc08b0f1SBarry Smith .  rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null
9521096aae1SMatthew Knepley 
9531096aae1SMatthew Knepley    Level: intermediate
9541096aae1SMatthew Knepley 
9551096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side
9561096aae1SMatthew Knepley 
95785385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
9581096aae1SMatthew Knepley @*/
9591096aae1SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetRhs(SNES snes,Vec *rhs)
9601096aae1SMatthew Knepley {
9611096aae1SMatthew Knepley   PetscFunctionBegin;
9621096aae1SMatthew Knepley   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
9631096aae1SMatthew Knepley   PetscValidPointer(rhs,2);
96485385478SLisandro Dalcin   *rhs = snes->vec_rhs;
9651096aae1SMatthew Knepley   PetscFunctionReturn(0);
9661096aae1SMatthew Knepley }
9671096aae1SMatthew Knepley 
9681096aae1SMatthew Knepley #undef __FUNCT__
9694a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction"
9709b94acceSBarry Smith /*@
97136851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
9729b94acceSBarry Smith                          SNESSetFunction().
9739b94acceSBarry Smith 
974c7afd0dbSLois Curfman McInnes    Collective on SNES
975c7afd0dbSLois Curfman McInnes 
9769b94acceSBarry Smith    Input Parameters:
977c7afd0dbSLois Curfman McInnes +  snes - the SNES context
978c7afd0dbSLois Curfman McInnes -  x - input vector
9799b94acceSBarry Smith 
9809b94acceSBarry Smith    Output Parameter:
9813638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
9829b94acceSBarry Smith 
9831bffabb2SLois Curfman McInnes    Notes:
98436851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
98536851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
98636851e7fSLois Curfman McInnes    themselves.
98736851e7fSLois Curfman McInnes 
98836851e7fSLois Curfman McInnes    Level: developer
98936851e7fSLois Curfman McInnes 
9909b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
9919b94acceSBarry Smith 
992a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
9939b94acceSBarry Smith @*/
99463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeFunction(SNES snes,Vec x,Vec y)
9959b94acceSBarry Smith {
996dfbe8321SBarry Smith   PetscErrorCode ierr;
9979b94acceSBarry Smith 
9983a40ed3dSBarry Smith   PetscFunctionBegin;
9994482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
10004482741eSBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE,2);
10014482741eSBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE,3);
1002c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,x,2);
1003c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,3);
1004184914b5SBarry Smith 
1005d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
1006e7788613SBarry Smith   if (snes->ops->computefunction) {
1007d64ed03dSBarry Smith     PetscStackPush("SNES user function");
1008e9a2bbcdSBarry Smith     CHKMEMQ;
1009e7788613SBarry Smith     ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP);
1010e9a2bbcdSBarry Smith     CHKMEMQ;
1011d64ed03dSBarry Smith     PetscStackPop;
1012d5e45103SBarry Smith     if (PetscExceptionValue(ierr)) {
101319717074SBarry Smith       PetscErrorCode pierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(pierr);
101419717074SBarry Smith     }
1015d5e45103SBarry Smith     CHKERRQ(ierr);
101685385478SLisandro Dalcin   } else if (snes->vec_rhs) {
10171096aae1SMatthew Knepley     ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr);
10181096aae1SMatthew Knepley   } else {
10191096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() before SNESComputeFunction(), likely called from SNESSolve().");
10201096aae1SMatthew Knepley   }
102185385478SLisandro Dalcin   if (snes->vec_rhs) {
102285385478SLisandro Dalcin     ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr);
10233ab0aad5SBarry Smith   }
1024ae3c334cSLois Curfman McInnes   snes->nfuncs++;
1025d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
10263a40ed3dSBarry Smith   PetscFunctionReturn(0);
10279b94acceSBarry Smith }
10289b94acceSBarry Smith 
10294a2ae208SSatish Balay #undef __FUNCT__
10304a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian"
103162fef451SLois Curfman McInnes /*@
103262fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
103362fef451SLois Curfman McInnes    set with SNESSetJacobian().
103462fef451SLois Curfman McInnes 
1035c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1036c7afd0dbSLois Curfman McInnes 
103762fef451SLois Curfman McInnes    Input Parameters:
1038c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1039c7afd0dbSLois Curfman McInnes -  x - input vector
104062fef451SLois Curfman McInnes 
104162fef451SLois Curfman McInnes    Output Parameters:
1042c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
104362fef451SLois Curfman McInnes .  B - optional preconditioning matrix
10442b668275SBarry Smith -  flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER)
1045fee21e36SBarry Smith 
1046*e35cf81dSBarry Smith   Options Database Keys:
1047*e35cf81dSBarry Smith +    -snes_lag_preconditioner <lag>
1048*e35cf81dSBarry Smith -    -snes_lag_jacobian <lag>
1049*e35cf81dSBarry Smith 
105062fef451SLois Curfman McInnes    Notes:
105162fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
105262fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
105362fef451SLois Curfman McInnes 
105494b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
1055dc5a77f8SLois Curfman McInnes    flag parameter.
105662fef451SLois Curfman McInnes 
105736851e7fSLois Curfman McInnes    Level: developer
105836851e7fSLois Curfman McInnes 
105962fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
106062fef451SLois Curfman McInnes 
1061*e35cf81dSBarry Smith .seealso:  SNESSetJacobian(), KSPSetOperators(), MatStructure, SNESSetLagPreconditioner(), SNESSetLagJacobian()
106262fef451SLois Curfman McInnes @*/
106363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
10649b94acceSBarry Smith {
1065dfbe8321SBarry Smith   PetscErrorCode ierr;
10663a40ed3dSBarry Smith 
10673a40ed3dSBarry Smith   PetscFunctionBegin;
10684482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
10694482741eSBarry Smith   PetscValidHeaderSpecific(X,VEC_COOKIE,2);
10704482741eSBarry Smith   PetscValidPointer(flg,5);
1071c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,X,2);
1072e7788613SBarry Smith   if (!snes->ops->computejacobian) PetscFunctionReturn(0);
1073*e35cf81dSBarry Smith   if (snes->lagjacobian == -1) {
1074*e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1075*e35cf81dSBarry Smith     ierr = PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");CHKERRQ(ierr);
1076*e35cf81dSBarry Smith     PetscFunctionReturn(0);
1077*e35cf81dSBarry Smith   } else if (snes->lagjacobian > 1 && snes->iter % snes->lagjacobian) {
1078*e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1079*e35cf81dSBarry Smith     ierr = PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);CHKERRQ(ierr);
1080*e35cf81dSBarry Smith     PetscFunctionReturn(0);
1081*e35cf81dSBarry Smith   }
1082*e35cf81dSBarry Smith 
1083c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1084*e35cf81dSBarry Smith   ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1085d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
1086dc67937bSBarry Smith   CHKMEMQ;
1087e7788613SBarry Smith   ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
1088dc67937bSBarry Smith   CHKMEMQ;
1089d64ed03dSBarry Smith   PetscStackPop;
1090d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1091a8054027SBarry Smith 
1092a8054027SBarry Smith   if (snes->lagpreconditioner == -1) {
1093a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1094a8054027SBarry Smith     ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr);
1095a8054027SBarry Smith   } else if (snes->lagpreconditioner > 1 && snes->iter % snes->lagpreconditioner) {
1096a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1097a8054027SBarry Smith     ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr);
1098a8054027SBarry Smith   }
1099a8054027SBarry Smith 
11006d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
11016ce558aeSBarry Smith   /* PetscValidHeaderSpecific(*A,MAT_COOKIE,3);
11026ce558aeSBarry Smith     PetscValidHeaderSpecific(*B,MAT_COOKIE,4);   */
11033a40ed3dSBarry Smith   PetscFunctionReturn(0);
11049b94acceSBarry Smith }
11059b94acceSBarry Smith 
11064a2ae208SSatish Balay #undef __FUNCT__
11074a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian"
11089b94acceSBarry Smith /*@C
11099b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1110044dda88SLois Curfman McInnes    location to store the matrix.
11119b94acceSBarry Smith 
1112c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1113c7afd0dbSLois Curfman McInnes 
11149b94acceSBarry Smith    Input Parameters:
1115c7afd0dbSLois Curfman McInnes +  snes - the SNES context
11169b94acceSBarry Smith .  A - Jacobian matrix
11179b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
11189b94acceSBarry Smith .  func - Jacobian evaluation routine
1119c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
11202cd2dfdcSLois Curfman McInnes          Jacobian evaluation routine (may be PETSC_NULL)
11219b94acceSBarry Smith 
11229b94acceSBarry Smith    Calling sequence of func:
11238d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
11249b94acceSBarry Smith 
1125c7afd0dbSLois Curfman McInnes +  x - input vector
11269b94acceSBarry Smith .  A - Jacobian matrix
11279b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1128ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
11292b668275SBarry Smith    structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER
1130c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
11319b94acceSBarry Smith 
11329b94acceSBarry Smith    Notes:
113394b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
11342cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1135ac21db08SLois Curfman McInnes 
1136ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
11379b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
11389b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
11399b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
11409b94acceSBarry Smith    throughout the global iterations.
11419b94acceSBarry Smith 
114216913363SBarry Smith    If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on
114316913363SBarry Smith    each matrix.
114416913363SBarry Smith 
114536851e7fSLois Curfman McInnes    Level: beginner
114636851e7fSLois Curfman McInnes 
11479b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
11489b94acceSBarry Smith 
11493ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure
11509b94acceSBarry Smith @*/
115163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
11529b94acceSBarry Smith {
1153dfbe8321SBarry Smith   PetscErrorCode ierr;
11543a7fca6bSBarry Smith 
11553a40ed3dSBarry Smith   PetscFunctionBegin;
11564482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
11574482741eSBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_COOKIE,2);
11584482741eSBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_COOKIE,3);
1159c9780b6fSBarry Smith   if (A) PetscCheckSameComm(snes,1,A,2);
1160c9780b6fSBarry Smith   if (B) PetscCheckSameComm(snes,1,B,2);
1161e7788613SBarry Smith   if (func) snes->ops->computejacobian = func;
11623a7fca6bSBarry Smith   if (ctx)  snes->jacP                 = ctx;
11633a7fca6bSBarry Smith   if (A) {
11647dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
11653a7fca6bSBarry Smith     if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
11669b94acceSBarry Smith     snes->jacobian = A;
11673a7fca6bSBarry Smith   }
11683a7fca6bSBarry Smith   if (B) {
11697dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
11703a7fca6bSBarry Smith     if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
11719b94acceSBarry Smith     snes->jacobian_pre = B;
11723a7fca6bSBarry Smith   }
11733a40ed3dSBarry Smith   PetscFunctionReturn(0);
11749b94acceSBarry Smith }
117562fef451SLois Curfman McInnes 
11764a2ae208SSatish Balay #undef __FUNCT__
11774a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian"
1178c2aafc4cSSatish Balay /*@C
1179b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1180b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1181b4fd4287SBarry Smith 
1182c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1183c7afd0dbSLois Curfman McInnes 
1184b4fd4287SBarry Smith    Input Parameter:
1185b4fd4287SBarry Smith .  snes - the nonlinear solver context
1186b4fd4287SBarry Smith 
1187b4fd4287SBarry Smith    Output Parameters:
1188c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1189b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
119070e92668SMatthew Knepley .  func - location to put Jacobian function (or PETSC_NULL)
119170e92668SMatthew Knepley -  ctx - location to stash Jacobian ctx (or PETSC_NULL)
1192fee21e36SBarry Smith 
119336851e7fSLois Curfman McInnes    Level: advanced
119436851e7fSLois Curfman McInnes 
1195b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1196b4fd4287SBarry Smith @*/
119770e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx)
1198b4fd4287SBarry Smith {
11993a40ed3dSBarry Smith   PetscFunctionBegin;
12004482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1201b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
1202b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
1203e7788613SBarry Smith   if (func) *func = snes->ops->computejacobian;
120470e92668SMatthew Knepley   if (ctx)  *ctx  = snes->jacP;
12053a40ed3dSBarry Smith   PetscFunctionReturn(0);
1206b4fd4287SBarry Smith }
1207b4fd4287SBarry Smith 
12089b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
120963dd3a1aSKris Buschelman EXTERN PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*);
12109b94acceSBarry Smith 
12114a2ae208SSatish Balay #undef __FUNCT__
12124a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp"
12139b94acceSBarry Smith /*@
12149b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1215272ac6f2SLois Curfman McInnes    of a nonlinear solver.
12169b94acceSBarry Smith 
1217fee21e36SBarry Smith    Collective on SNES
1218fee21e36SBarry Smith 
1219c7afd0dbSLois Curfman McInnes    Input Parameters:
122070e92668SMatthew Knepley .  snes - the SNES context
1221c7afd0dbSLois Curfman McInnes 
1222272ac6f2SLois Curfman McInnes    Notes:
1223272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1224272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1225272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1226272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1227272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1228272ac6f2SLois Curfman McInnes 
122936851e7fSLois Curfman McInnes    Level: advanced
123036851e7fSLois Curfman McInnes 
12319b94acceSBarry Smith .keywords: SNES, nonlinear, setup
12329b94acceSBarry Smith 
12339b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
12349b94acceSBarry Smith @*/
123570e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUp(SNES snes)
12369b94acceSBarry Smith {
1237dfbe8321SBarry Smith   PetscErrorCode ierr;
123871f87433Sdalcinl   PetscTruth     flg;
12393a40ed3dSBarry Smith 
12403a40ed3dSBarry Smith   PetscFunctionBegin;
12414482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
12424dc4c822SBarry Smith   if (snes->setupcalled) PetscFunctionReturn(0);
12439b94acceSBarry Smith 
12447adad957SLisandro Dalcin   if (!((PetscObject)snes)->type_name) {
124585385478SLisandro Dalcin     ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr);
124685385478SLisandro Dalcin   }
124785385478SLisandro Dalcin 
12487adad957SLisandro Dalcin   ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf_operator",&flg);CHKERRQ(ierr);
1249c1f60f51SBarry Smith   /*
1250c1f60f51SBarry Smith       This version replaces the user provided Jacobian matrix with a
1251dfa02198SLois Curfman McInnes       matrix-free version but still employs the user-provided preconditioner matrix
1252c1f60f51SBarry Smith   */
1253c1f60f51SBarry Smith   if (flg) {
1254c1f60f51SBarry Smith     Mat J;
1255fef1beadSBarry Smith     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
12563ec795f1SBarry Smith     ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr);
1257ae15b995SBarry Smith     ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr);
12583a7fca6bSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
12593a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
1260c1f60f51SBarry Smith   }
126145fc7adcSBarry Smith 
126203c60df9SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_MAT_SINGLE) && !defined(PETSC_USE_LONG_DOUBLE) && !defined(PETSC_USE_INT)
12637adad957SLisandro Dalcin   ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf_operator2",&flg);CHKERRQ(ierr);
126445fc7adcSBarry Smith   if (flg) {
126545fc7adcSBarry Smith     Mat J;
126645fc7adcSBarry Smith     ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_sol,&J);CHKERRQ(ierr);
126775396ef9SLisandro Dalcin     ierr = PetscInfo(snes,"Setting default matrix-free operator routines (version 2)\n");CHKERRQ(ierr);
126845fc7adcSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
126945fc7adcSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
127045fc7adcSBarry Smith   }
127132a4b47aSMatthew Knepley #endif
127245fc7adcSBarry Smith 
12737adad957SLisandro Dalcin   ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf",&flg);CHKERRQ(ierr);
1274c1f60f51SBarry Smith   /*
1275dfa02198SLois Curfman McInnes       This version replaces both the user-provided Jacobian and the user-
1276c1f60f51SBarry Smith       provided preconditioner matrix with the default matrix free version.
1277c1f60f51SBarry Smith    */
127831615d04SLois Curfman McInnes   if (flg) {
1279272ac6f2SLois Curfman McInnes     Mat  J;
1280b5d62d44SBarry Smith     KSP ksp;
128194b7f48cSBarry Smith     PC   pc;
128275396ef9SLisandro Dalcin     /* create and set matrix-free operator */
1283fef1beadSBarry Smith     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
12843ec795f1SBarry Smith     ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr);
128575396ef9SLisandro Dalcin     ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr);
12863ec795f1SBarry Smith     ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr);
12873a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
1288f3ef73ceSBarry Smith     /* force no preconditioner */
128994b7f48cSBarry Smith     ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
1290b5d62d44SBarry Smith     ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
1291a9815358SBarry Smith     ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&flg);CHKERRQ(ierr);
1292a9815358SBarry Smith     if (!flg) {
129375396ef9SLisandro Dalcin       ierr = PetscInfo(snes,"Setting default matrix-free preconditioner routines;\nThat is no preconditioner is being used\n");CHKERRQ(ierr);
1294f3ef73ceSBarry Smith       ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
1295272ac6f2SLois Curfman McInnes     }
1296a9815358SBarry Smith   }
1297f3ef73ceSBarry Smith 
129885385478SLisandro Dalcin   if (!snes->vec_func && !snes->vec_rhs) {
12991096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
13001096aae1SMatthew Knepley   }
130185385478SLisandro Dalcin   if (!snes->ops->computefunction && !snes->vec_rhs) {
13021096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
13031096aae1SMatthew Knepley   }
130475396ef9SLisandro Dalcin   if (!snes->jacobian) {
130575396ef9SLisandro Dalcin     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetJacobian() first \n or use -snes_mf option");
130675396ef9SLisandro Dalcin   }
1307a8c6a408SBarry Smith   if (snes->vec_func == snes->vec_sol) {
130829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
1309a8c6a408SBarry Smith   }
1310a703fe33SLois Curfman McInnes 
1311410397dcSLisandro Dalcin   if (snes->ops->setup) {
1312410397dcSLisandro Dalcin     ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr);
1313410397dcSLisandro Dalcin   }
13147aaed0d8SBarry Smith   snes->setupcalled = PETSC_TRUE;
13153a40ed3dSBarry Smith   PetscFunctionReturn(0);
13169b94acceSBarry Smith }
13179b94acceSBarry Smith 
13184a2ae208SSatish Balay #undef __FUNCT__
13194a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
132052baeb72SSatish Balay /*@
13219b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
13229b94acceSBarry Smith    with SNESCreate().
13239b94acceSBarry Smith 
1324c7afd0dbSLois Curfman McInnes    Collective on SNES
1325c7afd0dbSLois Curfman McInnes 
13269b94acceSBarry Smith    Input Parameter:
13279b94acceSBarry Smith .  snes - the SNES context
13289b94acceSBarry Smith 
132936851e7fSLois Curfman McInnes    Level: beginner
133036851e7fSLois Curfman McInnes 
13319b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
13329b94acceSBarry Smith 
133363a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
13349b94acceSBarry Smith @*/
133563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDestroy(SNES snes)
13369b94acceSBarry Smith {
13376849ba73SBarry Smith   PetscErrorCode ierr;
13383a40ed3dSBarry Smith 
13393a40ed3dSBarry Smith   PetscFunctionBegin;
13404482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
13417adad957SLisandro Dalcin   if (--((PetscObject)snes)->refct > 0) PetscFunctionReturn(0);
1342d4bb536fSBarry Smith 
1343be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
13440f5bd95cSBarry Smith   ierr = PetscObjectDepublish(snes);CHKERRQ(ierr);
1345e7788613SBarry Smith   if (snes->ops->destroy) {ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr);}
134685385478SLisandro Dalcin 
134785385478SLisandro Dalcin   if (snes->vec_rhs) {ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr);}
134885385478SLisandro Dalcin   if (snes->vec_sol) {ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr);}
134985385478SLisandro Dalcin   if (snes->vec_func) {ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr);}
13503a7fca6bSBarry Smith   if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
13513a7fca6bSBarry Smith   if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
13521cee3971SBarry Smith   if (snes->ksp) {ierr = KSPDestroy(snes->ksp);CHKERRQ(ierr);}
135385385478SLisandro Dalcin   ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);
1354522c5e43SBarry Smith   if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);}
1355a6570f20SBarry Smith   ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);
13567f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);}
1357a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr);
13583a40ed3dSBarry Smith   PetscFunctionReturn(0);
13599b94acceSBarry Smith }
13609b94acceSBarry Smith 
13619b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
13629b94acceSBarry Smith 
13634a2ae208SSatish Balay #undef __FUNCT__
1364a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner"
1365a8054027SBarry Smith /*@
1366a8054027SBarry Smith    SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve.
1367a8054027SBarry Smith 
1368a8054027SBarry Smith    Collective on SNES
1369a8054027SBarry Smith 
1370a8054027SBarry Smith    Input Parameters:
1371a8054027SBarry Smith +  snes - the SNES context
1372a8054027SBarry 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
1373a8054027SBarry Smith          the Jacobian is built etc.
1374a8054027SBarry Smith 
1375a8054027SBarry Smith    Options Database Keys:
1376a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1377a8054027SBarry Smith 
1378a8054027SBarry Smith    Notes:
1379a8054027SBarry Smith    The default is 1
1380a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1381a8054027SBarry Smith    If  -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use
1382a8054027SBarry Smith 
1383a8054027SBarry Smith    Level: intermediate
1384a8054027SBarry Smith 
1385a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1386a8054027SBarry Smith 
1387*e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian()
1388a8054027SBarry Smith 
1389a8054027SBarry Smith @*/
1390a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagPreconditioner(SNES snes,PetscInt lag)
1391a8054027SBarry Smith {
1392a8054027SBarry Smith   PetscFunctionBegin;
1393a8054027SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1394a8054027SBarry Smith   if (lag < -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -1, 1 or greater");
1395a8054027SBarry Smith   if (!lag) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1396a8054027SBarry Smith   snes->lagpreconditioner = lag;
1397a8054027SBarry Smith   PetscFunctionReturn(0);
1398a8054027SBarry Smith }
1399a8054027SBarry Smith 
1400a8054027SBarry Smith #undef __FUNCT__
1401a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner"
1402a8054027SBarry Smith /*@
1403a8054027SBarry Smith    SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt
1404a8054027SBarry Smith 
1405a8054027SBarry Smith    Collective on SNES
1406a8054027SBarry Smith 
1407a8054027SBarry Smith    Input Parameter:
1408a8054027SBarry Smith .  snes - the SNES context
1409a8054027SBarry Smith 
1410a8054027SBarry Smith    Output Parameter:
1411a8054027SBarry 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
1412a8054027SBarry Smith          the Jacobian is built etc.
1413a8054027SBarry Smith 
1414a8054027SBarry Smith    Options Database Keys:
1415a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1416a8054027SBarry Smith 
1417a8054027SBarry Smith    Notes:
1418a8054027SBarry Smith    The default is 1
1419a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1420a8054027SBarry Smith 
1421a8054027SBarry Smith    Level: intermediate
1422a8054027SBarry Smith 
1423a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1424a8054027SBarry Smith 
1425a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner()
1426a8054027SBarry Smith 
1427a8054027SBarry Smith @*/
1428a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagPreconditioner(SNES snes,PetscInt *lag)
1429a8054027SBarry Smith {
1430a8054027SBarry Smith   PetscFunctionBegin;
1431a8054027SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1432a8054027SBarry Smith   *lag = snes->lagpreconditioner;
1433a8054027SBarry Smith   PetscFunctionReturn(0);
1434a8054027SBarry Smith }
1435a8054027SBarry Smith 
1436a8054027SBarry Smith #undef __FUNCT__
1437*e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian"
1438*e35cf81dSBarry Smith /*@
1439*e35cf81dSBarry Smith    SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how
1440*e35cf81dSBarry Smith      often the preconditioner is rebuilt.
1441*e35cf81dSBarry Smith 
1442*e35cf81dSBarry Smith    Collective on SNES
1443*e35cf81dSBarry Smith 
1444*e35cf81dSBarry Smith    Input Parameters:
1445*e35cf81dSBarry Smith +  snes - the SNES context
1446*e35cf81dSBarry 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
1447*e35cf81dSBarry Smith          the Jacobian is built etc.
1448*e35cf81dSBarry Smith 
1449*e35cf81dSBarry Smith    Options Database Keys:
1450*e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1451*e35cf81dSBarry Smith 
1452*e35cf81dSBarry Smith    Notes:
1453*e35cf81dSBarry Smith    The default is 1
1454*e35cf81dSBarry Smith    The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1455*e35cf81dSBarry Smith    If  -1 is used before the very first nonlinear solve the CODE WILL FAIL! because no Jacobian is used
1456*e35cf81dSBarry Smith 
1457*e35cf81dSBarry Smith    Level: intermediate
1458*e35cf81dSBarry Smith 
1459*e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1460*e35cf81dSBarry Smith 
1461*e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian()
1462*e35cf81dSBarry Smith 
1463*e35cf81dSBarry Smith @*/
1464*e35cf81dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagJacobian(SNES snes,PetscInt lag)
1465*e35cf81dSBarry Smith {
1466*e35cf81dSBarry Smith   PetscFunctionBegin;
1467*e35cf81dSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1468*e35cf81dSBarry Smith   if (lag < -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -1, 1 or greater");
1469*e35cf81dSBarry Smith   if (!lag) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1470*e35cf81dSBarry Smith   snes->lagjacobian = lag;
1471*e35cf81dSBarry Smith   PetscFunctionReturn(0);
1472*e35cf81dSBarry Smith }
1473*e35cf81dSBarry Smith 
1474*e35cf81dSBarry Smith #undef __FUNCT__
1475*e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian"
1476*e35cf81dSBarry Smith /*@
1477*e35cf81dSBarry Smith    SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt
1478*e35cf81dSBarry Smith 
1479*e35cf81dSBarry Smith    Collective on SNES
1480*e35cf81dSBarry Smith 
1481*e35cf81dSBarry Smith    Input Parameter:
1482*e35cf81dSBarry Smith .  snes - the SNES context
1483*e35cf81dSBarry Smith 
1484*e35cf81dSBarry Smith    Output Parameter:
1485*e35cf81dSBarry 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
1486*e35cf81dSBarry Smith          the Jacobian is built etc.
1487*e35cf81dSBarry Smith 
1488*e35cf81dSBarry Smith    Options Database Keys:
1489*e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1490*e35cf81dSBarry Smith 
1491*e35cf81dSBarry Smith    Notes:
1492*e35cf81dSBarry Smith    The default is 1
1493*e35cf81dSBarry Smith    The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1494*e35cf81dSBarry Smith 
1495*e35cf81dSBarry Smith    Level: intermediate
1496*e35cf81dSBarry Smith 
1497*e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1498*e35cf81dSBarry Smith 
1499*e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner()
1500*e35cf81dSBarry Smith 
1501*e35cf81dSBarry Smith @*/
1502*e35cf81dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagJacobian(SNES snes,PetscInt *lag)
1503*e35cf81dSBarry Smith {
1504*e35cf81dSBarry Smith   PetscFunctionBegin;
1505*e35cf81dSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1506*e35cf81dSBarry Smith   *lag = snes->lagjacobian;
1507*e35cf81dSBarry Smith   PetscFunctionReturn(0);
1508*e35cf81dSBarry Smith }
1509*e35cf81dSBarry Smith 
1510*e35cf81dSBarry Smith #undef __FUNCT__
15114a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
15129b94acceSBarry Smith /*@
1513d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
15149b94acceSBarry Smith 
1515c7afd0dbSLois Curfman McInnes    Collective on SNES
1516c7afd0dbSLois Curfman McInnes 
15179b94acceSBarry Smith    Input Parameters:
1518c7afd0dbSLois Curfman McInnes +  snes - the SNES context
151970441072SBarry Smith .  abstol - absolute convergence tolerance
152033174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
152133174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
152233174efeSLois Curfman McInnes            of the change in the solution between steps
152333174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1524c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1525fee21e36SBarry Smith 
152633174efeSLois Curfman McInnes    Options Database Keys:
152770441072SBarry Smith +    -snes_atol <abstol> - Sets abstol
1528c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1529c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1530c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1531c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
15329b94acceSBarry Smith 
1533d7a720efSLois Curfman McInnes    Notes:
15349b94acceSBarry Smith    The default maximum number of iterations is 50.
15359b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
15369b94acceSBarry Smith 
153736851e7fSLois Curfman McInnes    Level: intermediate
153836851e7fSLois Curfman McInnes 
153933174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
15409b94acceSBarry Smith 
15412492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance()
15429b94acceSBarry Smith @*/
154363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf)
15449b94acceSBarry Smith {
15453a40ed3dSBarry Smith   PetscFunctionBegin;
15464482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
154770441072SBarry Smith   if (abstol != PETSC_DEFAULT)  snes->abstol      = abstol;
1548d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1549d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1550d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1551d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
15523a40ed3dSBarry Smith   PetscFunctionReturn(0);
15539b94acceSBarry Smith }
15549b94acceSBarry Smith 
15554a2ae208SSatish Balay #undef __FUNCT__
15564a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
15579b94acceSBarry Smith /*@
155833174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
155933174efeSLois Curfman McInnes 
1560c7afd0dbSLois Curfman McInnes    Not Collective
1561c7afd0dbSLois Curfman McInnes 
156233174efeSLois Curfman McInnes    Input Parameters:
1563c7afd0dbSLois Curfman McInnes +  snes - the SNES context
156485385478SLisandro Dalcin .  atol - absolute convergence tolerance
156533174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
156633174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
156733174efeSLois Curfman McInnes            of the change in the solution between steps
156833174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1569c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1570fee21e36SBarry Smith 
157133174efeSLois Curfman McInnes    Notes:
157233174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
157333174efeSLois Curfman McInnes 
157436851e7fSLois Curfman McInnes    Level: intermediate
157536851e7fSLois Curfman McInnes 
157633174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
157733174efeSLois Curfman McInnes 
157833174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
157933174efeSLois Curfman McInnes @*/
158085385478SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf)
158133174efeSLois Curfman McInnes {
15823a40ed3dSBarry Smith   PetscFunctionBegin;
15834482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
158485385478SLisandro Dalcin   if (atol)  *atol  = snes->abstol;
158533174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
158633174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
158733174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
158833174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
15893a40ed3dSBarry Smith   PetscFunctionReturn(0);
159033174efeSLois Curfman McInnes }
159133174efeSLois Curfman McInnes 
15924a2ae208SSatish Balay #undef __FUNCT__
15934a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
159433174efeSLois Curfman McInnes /*@
15959b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
15969b94acceSBarry Smith 
1597fee21e36SBarry Smith    Collective on SNES
1598fee21e36SBarry Smith 
1599c7afd0dbSLois Curfman McInnes    Input Parameters:
1600c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1601c7afd0dbSLois Curfman McInnes -  tol - tolerance
1602c7afd0dbSLois Curfman McInnes 
16039b94acceSBarry Smith    Options Database Key:
1604c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
16059b94acceSBarry Smith 
160636851e7fSLois Curfman McInnes    Level: intermediate
160736851e7fSLois Curfman McInnes 
16089b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
16099b94acceSBarry Smith 
16102492ecdbSBarry Smith .seealso: SNESSetTolerances()
16119b94acceSBarry Smith @*/
161263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
16139b94acceSBarry Smith {
16143a40ed3dSBarry Smith   PetscFunctionBegin;
16154482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
16169b94acceSBarry Smith   snes->deltatol = tol;
16173a40ed3dSBarry Smith   PetscFunctionReturn(0);
16189b94acceSBarry Smith }
16199b94acceSBarry Smith 
1620df9fa365SBarry Smith /*
1621df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1622df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1623df9fa365SBarry Smith    macros instead of functions
1624df9fa365SBarry Smith */
16254a2ae208SSatish Balay #undef __FUNCT__
1626a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG"
1627a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx)
1628ce1608b8SBarry Smith {
1629dfbe8321SBarry Smith   PetscErrorCode ierr;
1630ce1608b8SBarry Smith 
1631ce1608b8SBarry Smith   PetscFunctionBegin;
16324482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1633a6570f20SBarry Smith   ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1634ce1608b8SBarry Smith   PetscFunctionReturn(0);
1635ce1608b8SBarry Smith }
1636ce1608b8SBarry Smith 
16374a2ae208SSatish Balay #undef __FUNCT__
1638a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate"
1639a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1640df9fa365SBarry Smith {
1641dfbe8321SBarry Smith   PetscErrorCode ierr;
1642df9fa365SBarry Smith 
1643df9fa365SBarry Smith   PetscFunctionBegin;
1644a6570f20SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1645df9fa365SBarry Smith   PetscFunctionReturn(0);
1646df9fa365SBarry Smith }
1647df9fa365SBarry Smith 
16484a2ae208SSatish Balay #undef __FUNCT__
1649a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy"
1650a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGDestroy(PetscDrawLG draw)
1651df9fa365SBarry Smith {
1652dfbe8321SBarry Smith   PetscErrorCode ierr;
1653df9fa365SBarry Smith 
1654df9fa365SBarry Smith   PetscFunctionBegin;
1655a6570f20SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
1656df9fa365SBarry Smith   PetscFunctionReturn(0);
1657df9fa365SBarry Smith }
1658df9fa365SBarry Smith 
16599b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
16609b94acceSBarry Smith 
16614a2ae208SSatish Balay #undef __FUNCT__
1662a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet"
16639b94acceSBarry Smith /*@C
1664a6570f20SBarry Smith    SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every
16659b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
16669b94acceSBarry Smith    progress.
16679b94acceSBarry Smith 
1668fee21e36SBarry Smith    Collective on SNES
1669fee21e36SBarry Smith 
1670c7afd0dbSLois Curfman McInnes    Input Parameters:
1671c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1672c7afd0dbSLois Curfman McInnes .  func - monitoring routine
1673b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
1674e8105e01SRichard Katz           monitor routine (use PETSC_NULL if no context is desired)
1675b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1676b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
16779b94acceSBarry Smith 
1678c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1679a7cc72afSBarry Smith $     int func(SNES snes,PetscInt its, PetscReal norm,void *mctx)
1680c7afd0dbSLois Curfman McInnes 
1681c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1682c7afd0dbSLois Curfman McInnes .    its - iteration number
1683c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
168440a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
16859b94acceSBarry Smith 
16869665c990SLois Curfman McInnes    Options Database Keys:
1687a6570f20SBarry Smith +    -snes_monitor        - sets SNESMonitorDefault()
1688a6570f20SBarry Smith .    -snes_monitor_draw    - sets line graph monitor,
1689a6570f20SBarry Smith                             uses SNESMonitorLGCreate()
1690a6570f20SBarry Smith _    -snes_monitor_cancel - cancels all monitors that have
1691c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
1692a6570f20SBarry Smith                             calls to SNESMonitorSet(), but
1693c7afd0dbSLois Curfman McInnes                             does not cancel those set via
1694c7afd0dbSLois Curfman McInnes                             the options database.
16959665c990SLois Curfman McInnes 
1696639f9d9dSBarry Smith    Notes:
16976bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
1698a6570f20SBarry Smith    SNESMonitorSet() multiple times; all will be called in the
16996bc08f3fSLois Curfman McInnes    order in which they were set.
1700639f9d9dSBarry Smith 
1701025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each SNES object
1702025f1a04SBarry Smith 
170336851e7fSLois Curfman McInnes    Level: intermediate
170436851e7fSLois Curfman McInnes 
17059b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
17069b94acceSBarry Smith 
1707a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel()
17089b94acceSBarry Smith @*/
1709b90d0a6eSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void*))
17109b94acceSBarry Smith {
1711b90d0a6eSBarry Smith   PetscInt i;
1712b90d0a6eSBarry Smith 
17133a40ed3dSBarry Smith   PetscFunctionBegin;
17144482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1715639f9d9dSBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) {
171629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1717639f9d9dSBarry Smith   }
1718b90d0a6eSBarry Smith   for (i=0; i<snes->numbermonitors;i++) {
1719b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) PetscFunctionReturn(0);
1720b90d0a6eSBarry Smith 
1721b90d0a6eSBarry Smith     /* check if both default monitors that share common ASCII viewer */
1722b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitor == SNESMonitorDefault) {
1723b90d0a6eSBarry Smith       if (mctx && snes->monitorcontext[i]) {
1724b90d0a6eSBarry Smith         PetscErrorCode          ierr;
1725b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer1 = (PetscViewerASCIIMonitor) mctx;
1726b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer2 = (PetscViewerASCIIMonitor) snes->monitorcontext[i];
1727b90d0a6eSBarry Smith         if (viewer1->viewer == viewer2->viewer) {
1728b90d0a6eSBarry Smith           ierr = (*monitordestroy)(mctx);CHKERRQ(ierr);
1729b90d0a6eSBarry Smith           PetscFunctionReturn(0);
1730b90d0a6eSBarry Smith         }
1731b90d0a6eSBarry Smith       }
1732b90d0a6eSBarry Smith     }
1733b90d0a6eSBarry Smith   }
1734b90d0a6eSBarry Smith   snes->monitor[snes->numbermonitors]           = monitor;
1735b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
1736639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
17373a40ed3dSBarry Smith   PetscFunctionReturn(0);
17389b94acceSBarry Smith }
17399b94acceSBarry Smith 
17404a2ae208SSatish Balay #undef __FUNCT__
1741a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel"
17425cd90555SBarry Smith /*@C
1743a6570f20SBarry Smith    SNESMonitorCancel - Clears all the monitor functions for a SNES object.
17445cd90555SBarry Smith 
1745c7afd0dbSLois Curfman McInnes    Collective on SNES
1746c7afd0dbSLois Curfman McInnes 
17475cd90555SBarry Smith    Input Parameters:
17485cd90555SBarry Smith .  snes - the SNES context
17495cd90555SBarry Smith 
17501a480d89SAdministrator    Options Database Key:
1751a6570f20SBarry Smith .  -snes_monitor_cancel - cancels all monitors that have been hardwired
1752a6570f20SBarry Smith     into a code by calls to SNESMonitorSet(), but does not cancel those
1753c7afd0dbSLois Curfman McInnes     set via the options database
17545cd90555SBarry Smith 
17555cd90555SBarry Smith    Notes:
17565cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
17575cd90555SBarry Smith 
175836851e7fSLois Curfman McInnes    Level: intermediate
175936851e7fSLois Curfman McInnes 
17605cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
17615cd90555SBarry Smith 
1762a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet()
17635cd90555SBarry Smith @*/
1764a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorCancel(SNES snes)
17655cd90555SBarry Smith {
1766d952e501SBarry Smith   PetscErrorCode ierr;
1767d952e501SBarry Smith   PetscInt       i;
1768d952e501SBarry Smith 
17695cd90555SBarry Smith   PetscFunctionBegin;
17704482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1771d952e501SBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
1772d952e501SBarry Smith     if (snes->monitordestroy[i]) {
1773d952e501SBarry Smith       ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr);
1774d952e501SBarry Smith     }
1775d952e501SBarry Smith   }
17765cd90555SBarry Smith   snes->numbermonitors = 0;
17775cd90555SBarry Smith   PetscFunctionReturn(0);
17785cd90555SBarry Smith }
17795cd90555SBarry Smith 
17804a2ae208SSatish Balay #undef __FUNCT__
17814a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
17829b94acceSBarry Smith /*@C
17839b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
17849b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
17859b94acceSBarry Smith 
1786fee21e36SBarry Smith    Collective on SNES
1787fee21e36SBarry Smith 
1788c7afd0dbSLois Curfman McInnes    Input Parameters:
1789c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1790c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
17917f7931b9SBarry Smith .  cctx - [optional] context for private data for the convergence routine  (may be PETSC_NULL)
17927f7931b9SBarry Smith -  destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran)
17939b94acceSBarry Smith 
1794c7afd0dbSLois Curfman McInnes    Calling sequence of func:
179506ee9f85SBarry Smith $     PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
1796c7afd0dbSLois Curfman McInnes 
1797c7afd0dbSLois Curfman McInnes +    snes - the SNES context
179806ee9f85SBarry Smith .    it - current iteration (0 is the first and is before any Newton step)
1799c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
1800184914b5SBarry Smith .    reason - reason for convergence/divergence
1801c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
18024b27c08aSLois Curfman McInnes .    gnorm - 2-norm of current step
18034b27c08aSLois Curfman McInnes -    f - 2-norm of function
18049b94acceSBarry Smith 
180536851e7fSLois Curfman McInnes    Level: advanced
180636851e7fSLois Curfman McInnes 
18079b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
18089b94acceSBarry Smith 
180985385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged()
18109b94acceSBarry Smith @*/
18117f7931b9SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*))
18129b94acceSBarry Smith {
18137f7931b9SBarry Smith   PetscErrorCode ierr;
18147f7931b9SBarry Smith 
18153a40ed3dSBarry Smith   PetscFunctionBegin;
18164482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
181785385478SLisandro Dalcin   if (!func) func = SNESSkipConverged;
18187f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {
18197f7931b9SBarry Smith     ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);
18207f7931b9SBarry Smith   }
182185385478SLisandro Dalcin   snes->ops->converged        = func;
18227f7931b9SBarry Smith   snes->ops->convergeddestroy = destroy;
182385385478SLisandro Dalcin   snes->cnvP                  = cctx;
18243a40ed3dSBarry Smith   PetscFunctionReturn(0);
18259b94acceSBarry Smith }
18269b94acceSBarry Smith 
18274a2ae208SSatish Balay #undef __FUNCT__
18284a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
182952baeb72SSatish Balay /*@
1830184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
1831184914b5SBarry Smith 
1832184914b5SBarry Smith    Not Collective
1833184914b5SBarry Smith 
1834184914b5SBarry Smith    Input Parameter:
1835184914b5SBarry Smith .  snes - the SNES context
1836184914b5SBarry Smith 
1837184914b5SBarry Smith    Output Parameter:
18384d0a8057SBarry Smith .  reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the
1839184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
1840184914b5SBarry Smith 
1841184914b5SBarry Smith    Level: intermediate
1842184914b5SBarry Smith 
1843184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
1844184914b5SBarry Smith 
1845184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
1846184914b5SBarry Smith 
184785385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason
1848184914b5SBarry Smith @*/
184963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
1850184914b5SBarry Smith {
1851184914b5SBarry Smith   PetscFunctionBegin;
18524482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
18534482741eSBarry Smith   PetscValidPointer(reason,2);
1854184914b5SBarry Smith   *reason = snes->reason;
1855184914b5SBarry Smith   PetscFunctionReturn(0);
1856184914b5SBarry Smith }
1857184914b5SBarry Smith 
18584a2ae208SSatish Balay #undef __FUNCT__
18594a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
1860c9005455SLois Curfman McInnes /*@
1861c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
1862c9005455SLois Curfman McInnes 
1863fee21e36SBarry Smith    Collective on SNES
1864fee21e36SBarry Smith 
1865c7afd0dbSLois Curfman McInnes    Input Parameters:
1866c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
1867c7afd0dbSLois Curfman McInnes .  a   - array to hold history
1868cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
1869758f92a0SBarry Smith .  na  - size of a and its
187064731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
1871758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
1872c7afd0dbSLois Curfman McInnes 
1873c9005455SLois Curfman McInnes    Notes:
18744b27c08aSLois Curfman McInnes    If set, this array will contain the function norms computed
1875c9005455SLois Curfman McInnes    at each step.
1876c9005455SLois Curfman McInnes 
1877c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
1878c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
1879c9005455SLois Curfman McInnes    during the section of code that is being timed.
1880c9005455SLois Curfman McInnes 
188136851e7fSLois Curfman McInnes    Level: intermediate
188236851e7fSLois Curfman McInnes 
1883c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
1884758f92a0SBarry Smith 
188508405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
1886758f92a0SBarry Smith 
1887c9005455SLois Curfman McInnes @*/
1888a562a398SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscTruth reset)
1889c9005455SLois Curfman McInnes {
18903a40ed3dSBarry Smith   PetscFunctionBegin;
18914482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
18924482741eSBarry Smith   if (na)  PetscValidScalarPointer(a,2);
1893a562a398SLisandro Dalcin   if (its) PetscValidIntPointer(its,3);
1894c9005455SLois Curfman McInnes   snes->conv_hist       = a;
1895758f92a0SBarry Smith   snes->conv_hist_its   = its;
1896758f92a0SBarry Smith   snes->conv_hist_max   = na;
1897a12bc48fSLisandro Dalcin   snes->conv_hist_len   = 0;
1898758f92a0SBarry Smith   snes->conv_hist_reset = reset;
1899758f92a0SBarry Smith   PetscFunctionReturn(0);
1900758f92a0SBarry Smith }
1901758f92a0SBarry Smith 
19024a2ae208SSatish Balay #undef __FUNCT__
19034a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
19040c4c9dddSBarry Smith /*@C
1905758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
1906758f92a0SBarry Smith 
1907758f92a0SBarry Smith    Collective on SNES
1908758f92a0SBarry Smith 
1909758f92a0SBarry Smith    Input Parameter:
1910758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
1911758f92a0SBarry Smith 
1912758f92a0SBarry Smith    Output Parameters:
1913758f92a0SBarry Smith .  a   - array to hold history
1914758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
1915758f92a0SBarry Smith          negative if not converged) for each solve.
1916758f92a0SBarry Smith -  na  - size of a and its
1917758f92a0SBarry Smith 
1918758f92a0SBarry Smith    Notes:
1919758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
1920758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
1921758f92a0SBarry Smith 
1922758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
1923758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
1924758f92a0SBarry Smith    during the section of code that is being timed.
1925758f92a0SBarry Smith 
1926758f92a0SBarry Smith    Level: intermediate
1927758f92a0SBarry Smith 
1928758f92a0SBarry Smith .keywords: SNES, get, convergence, history
1929758f92a0SBarry Smith 
1930758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
1931758f92a0SBarry Smith 
1932758f92a0SBarry Smith @*/
193363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na)
1934758f92a0SBarry Smith {
1935758f92a0SBarry Smith   PetscFunctionBegin;
19364482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1937758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
1938758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
1939758f92a0SBarry Smith   if (na)  *na  = snes->conv_hist_len;
19403a40ed3dSBarry Smith   PetscFunctionReturn(0);
1941c9005455SLois Curfman McInnes }
1942c9005455SLois Curfman McInnes 
1943e74ef692SMatthew Knepley #undef __FUNCT__
1944e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
1945ac226902SBarry Smith /*@C
194676b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
19477e4bb74cSBarry Smith   at the beginning o every iteration of the nonlinear solve. Specifically
19487e4bb74cSBarry Smith   it is called just before the Jacobian is "evaluated".
194976b2cf59SMatthew Knepley 
195076b2cf59SMatthew Knepley   Collective on SNES
195176b2cf59SMatthew Knepley 
195276b2cf59SMatthew Knepley   Input Parameters:
195376b2cf59SMatthew Knepley . snes - The nonlinear solver context
195476b2cf59SMatthew Knepley . func - The function
195576b2cf59SMatthew Knepley 
195676b2cf59SMatthew Knepley   Calling sequence of func:
1957b5d30489SBarry Smith . func (SNES snes, PetscInt step);
195876b2cf59SMatthew Knepley 
195976b2cf59SMatthew Knepley . step - The current step of the iteration
196076b2cf59SMatthew Knepley 
196176b2cf59SMatthew Knepley   Level: intermediate
196276b2cf59SMatthew Knepley 
196376b2cf59SMatthew Knepley .keywords: SNES, update
1964b5d30489SBarry Smith 
196585385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve()
196676b2cf59SMatthew Knepley @*/
196763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt))
196876b2cf59SMatthew Knepley {
196976b2cf59SMatthew Knepley   PetscFunctionBegin;
19704482741eSBarry Smith   PetscValidHeaderSpecific(snes, SNES_COOKIE,1);
1971e7788613SBarry Smith   snes->ops->update = func;
197276b2cf59SMatthew Knepley   PetscFunctionReturn(0);
197376b2cf59SMatthew Knepley }
197476b2cf59SMatthew Knepley 
1975e74ef692SMatthew Knepley #undef __FUNCT__
1976e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
197776b2cf59SMatthew Knepley /*@
197876b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
197976b2cf59SMatthew Knepley 
198076b2cf59SMatthew Knepley   Not collective
198176b2cf59SMatthew Knepley 
198276b2cf59SMatthew Knepley   Input Parameters:
198376b2cf59SMatthew Knepley . snes - The nonlinear solver context
198476b2cf59SMatthew Knepley . step - The current step of the iteration
198576b2cf59SMatthew Knepley 
1986205452f4SMatthew Knepley   Level: intermediate
1987205452f4SMatthew Knepley 
198876b2cf59SMatthew Knepley .keywords: SNES, update
1989a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC()
199076b2cf59SMatthew Knepley @*/
199163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultUpdate(SNES snes, PetscInt step)
199276b2cf59SMatthew Knepley {
199376b2cf59SMatthew Knepley   PetscFunctionBegin;
199476b2cf59SMatthew Knepley   PetscFunctionReturn(0);
199576b2cf59SMatthew Knepley }
199676b2cf59SMatthew Knepley 
19974a2ae208SSatish Balay #undef __FUNCT__
19984a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
19999b94acceSBarry Smith /*
20009b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
20019b94acceSBarry Smith    positive parameter delta.
20029b94acceSBarry Smith 
20039b94acceSBarry Smith     Input Parameters:
2004c7afd0dbSLois Curfman McInnes +   snes - the SNES context
20059b94acceSBarry Smith .   y - approximate solution of linear system
20069b94acceSBarry Smith .   fnorm - 2-norm of current function
2007c7afd0dbSLois Curfman McInnes -   delta - trust region size
20089b94acceSBarry Smith 
20099b94acceSBarry Smith     Output Parameters:
2010c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
20119b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
20129b94acceSBarry Smith     region, and exceeds zero otherwise.
2013c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
20149b94acceSBarry Smith 
20159b94acceSBarry Smith     Note:
20164b27c08aSLois Curfman McInnes     For non-trust region methods such as SNESLS, the parameter delta
20179b94acceSBarry Smith     is set to be the maximum allowable step size.
20189b94acceSBarry Smith 
20199b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
20209b94acceSBarry Smith */
2021dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
20229b94acceSBarry Smith {
2023064f8208SBarry Smith   PetscReal      nrm;
2024ea709b57SSatish Balay   PetscScalar    cnorm;
2025dfbe8321SBarry Smith   PetscErrorCode ierr;
20263a40ed3dSBarry Smith 
20273a40ed3dSBarry Smith   PetscFunctionBegin;
20284482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
20294482741eSBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE,2);
2030c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,2);
2031184914b5SBarry Smith 
2032064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
2033064f8208SBarry Smith   if (nrm > *delta) {
2034064f8208SBarry Smith      nrm = *delta/nrm;
2035064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
2036064f8208SBarry Smith      cnorm = nrm;
20372dcb1b2aSMatthew Knepley      ierr = VecScale(y,cnorm);CHKERRQ(ierr);
20389b94acceSBarry Smith      *ynorm = *delta;
20399b94acceSBarry Smith   } else {
20409b94acceSBarry Smith      *gpnorm = 0.0;
2041064f8208SBarry Smith      *ynorm = nrm;
20429b94acceSBarry Smith   }
20433a40ed3dSBarry Smith   PetscFunctionReturn(0);
20449b94acceSBarry Smith }
20459b94acceSBarry Smith 
20464a2ae208SSatish Balay #undef __FUNCT__
20474a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
20486ce558aeSBarry Smith /*@C
2049f69a0ea3SMatthew Knepley    SNESSolve - Solves a nonlinear system F(x) = b.
2050f69a0ea3SMatthew Knepley    Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX().
20519b94acceSBarry Smith 
2052c7afd0dbSLois Curfman McInnes    Collective on SNES
2053c7afd0dbSLois Curfman McInnes 
2054b2002411SLois Curfman McInnes    Input Parameters:
2055c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2056f69a0ea3SMatthew Knepley .  b - the constant part of the equation, or PETSC_NULL to use zero.
205785385478SLisandro Dalcin -  x - the solution vector.
20589b94acceSBarry Smith 
2059b2002411SLois Curfman McInnes    Notes:
20608ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
20618ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
20628ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
20638ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
20648ddd3da0SLois Curfman McInnes 
206536851e7fSLois Curfman McInnes    Level: beginner
206636851e7fSLois Curfman McInnes 
20679b94acceSBarry Smith .keywords: SNES, nonlinear, solve
20689b94acceSBarry Smith 
206985385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian()
20709b94acceSBarry Smith @*/
2071f69a0ea3SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSolve(SNES snes,Vec b,Vec x)
20729b94acceSBarry Smith {
2073dfbe8321SBarry Smith   PetscErrorCode ierr;
2074f1af5d2fSBarry Smith   PetscTruth     flg;
2075eabae89aSBarry Smith   char           filename[PETSC_MAX_PATH_LEN];
2076eabae89aSBarry Smith   PetscViewer    viewer;
2077052efed2SBarry Smith 
20783a40ed3dSBarry Smith   PetscFunctionBegin;
20794482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2080f69a0ea3SMatthew Knepley   PetscValidHeaderSpecific(x,VEC_COOKIE,3);
2081f69a0ea3SMatthew Knepley   PetscCheckSameComm(snes,1,x,3);
208285385478SLisandro Dalcin   if (b) PetscValidHeaderSpecific(b,VEC_COOKIE,2);
208385385478SLisandro Dalcin   if (b) PetscCheckSameComm(snes,1,b,2);
208485385478SLisandro Dalcin 
208585385478SLisandro Dalcin   /* set solution vector */
208685385478SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);
208785385478SLisandro Dalcin   if (snes->vec_sol) { ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr); }
208885385478SLisandro Dalcin   snes->vec_sol = x;
208985385478SLisandro Dalcin   /* set afine vector if provided */
209085385478SLisandro Dalcin   if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); }
209185385478SLisandro Dalcin   if (snes->vec_rhs) { ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr); }
209285385478SLisandro Dalcin   snes->vec_rhs = b;
209385385478SLisandro Dalcin 
209485385478SLisandro Dalcin   if (!snes->vec_func && snes->vec_rhs) {
209585385478SLisandro Dalcin     ierr = VecDuplicate(b, &snes->vec_func); CHKERRQ(ierr);
209670e92668SMatthew Knepley   }
20973f149594SLisandro Dalcin 
209870e92668SMatthew Knepley   ierr = SNESSetUp(snes);CHKERRQ(ierr);
20993f149594SLisandro Dalcin 
2100abc0a331SBarry Smith   if (snes->conv_hist_reset) snes->conv_hist_len = 0;
210150ffb88aSMatthew Knepley   snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
2102d5e45103SBarry Smith 
21033f149594SLisandro Dalcin   ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
21044936397dSBarry Smith   ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr);
210585385478SLisandro Dalcin   ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
21064936397dSBarry Smith   if (snes->domainerror){
21074936397dSBarry Smith     snes->reason      = SNES_DIVERGED_FUNCTION_DOMAIN;
21084936397dSBarry Smith     snes->domainerror = PETSC_FALSE;
21094936397dSBarry Smith   }
211085385478SLisandro Dalcin 
21113f149594SLisandro Dalcin   if (!snes->reason) {
21123f149594SLisandro Dalcin     SETERRQ(PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason");
21133f149594SLisandro Dalcin   }
21143f149594SLisandro Dalcin 
21157adad957SLisandro Dalcin   ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
2116eabae89aSBarry Smith   if (flg && !PetscPreLoadingOn) {
21177adad957SLisandro Dalcin     ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr);
2118eabae89aSBarry Smith     ierr = SNESView(snes,viewer);CHKERRQ(ierr);
2119eabae89aSBarry Smith     ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr);
2120eabae89aSBarry Smith   }
2121eabae89aSBarry Smith 
21227adad957SLisandro Dalcin   ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg);CHKERRQ(ierr);
2123da9b6338SBarry Smith   if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); }
21245968eb51SBarry Smith   if (snes->printreason) {
21255968eb51SBarry Smith     if (snes->reason > 0) {
21267adad957SLisandro Dalcin       ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
21275968eb51SBarry Smith     } else {
21287adad957SLisandro Dalcin       ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
21295968eb51SBarry Smith     }
21305968eb51SBarry Smith   }
21315968eb51SBarry Smith 
21323a40ed3dSBarry Smith   PetscFunctionReturn(0);
21339b94acceSBarry Smith }
21349b94acceSBarry Smith 
21359b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
21369b94acceSBarry Smith 
21374a2ae208SSatish Balay #undef __FUNCT__
21384a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
213982bf6240SBarry Smith /*@C
21404b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
21419b94acceSBarry Smith 
2142fee21e36SBarry Smith    Collective on SNES
2143fee21e36SBarry Smith 
2144c7afd0dbSLois Curfman McInnes    Input Parameters:
2145c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2146454a90a3SBarry Smith -  type - a known method
2147c7afd0dbSLois Curfman McInnes 
2148c7afd0dbSLois Curfman McInnes    Options Database Key:
2149454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
2150c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
2151ae12b187SLois Curfman McInnes 
21529b94acceSBarry Smith    Notes:
2153e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
21544b27c08aSLois Curfman McInnes +    SNESLS - Newton's method with line search
2155c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
21564b27c08aSLois Curfman McInnes .    SNESTR - Newton's method with trust region
2157c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
21589b94acceSBarry Smith 
2159ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
2160ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
2161ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
2162ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
2163ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
2164ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
2165ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
2166ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
2167ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
2168b0a32e0cSBarry Smith   appropriate method.
216936851e7fSLois Curfman McInnes 
217036851e7fSLois Curfman McInnes   Level: intermediate
2171a703fe33SLois Curfman McInnes 
2172454a90a3SBarry Smith .keywords: SNES, set, type
2173435da068SBarry Smith 
2174435da068SBarry Smith .seealso: SNESType, SNESCreate()
2175435da068SBarry Smith 
21769b94acceSBarry Smith @*/
2177a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetType(SNES snes,const SNESType type)
21789b94acceSBarry Smith {
2179dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(SNES);
21806831982aSBarry Smith   PetscTruth     match;
21813a40ed3dSBarry Smith 
21823a40ed3dSBarry Smith   PetscFunctionBegin;
21834482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
21844482741eSBarry Smith   PetscValidCharPointer(type,2);
218582bf6240SBarry Smith 
21866831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
21870f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
218892ff6ae8SBarry Smith 
21897adad957SLisandro Dalcin   ierr =  PetscFListFind(SNESList,((PetscObject)snes)->comm,type,(void (**)(void)) &r);CHKERRQ(ierr);
2190958c9bccSBarry Smith   if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type);
219175396ef9SLisandro Dalcin   /* Destroy the previous private SNES context */
219275396ef9SLisandro Dalcin   if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); }
219375396ef9SLisandro Dalcin   /* Reinitialize function pointers in SNESOps structure */
219475396ef9SLisandro Dalcin   snes->ops->setup          = 0;
219575396ef9SLisandro Dalcin   snes->ops->solve          = 0;
219675396ef9SLisandro Dalcin   snes->ops->view           = 0;
219775396ef9SLisandro Dalcin   snes->ops->setfromoptions = 0;
219875396ef9SLisandro Dalcin   snes->ops->destroy        = 0;
219975396ef9SLisandro Dalcin   /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */
220075396ef9SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
22013a40ed3dSBarry Smith   ierr = (*r)(snes);CHKERRQ(ierr);
2202454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
22033a40ed3dSBarry Smith   PetscFunctionReturn(0);
22049b94acceSBarry Smith }
22059b94acceSBarry Smith 
2206a847f771SSatish Balay 
22079b94acceSBarry Smith /* --------------------------------------------------------------------- */
22084a2ae208SSatish Balay #undef __FUNCT__
22094a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
221052baeb72SSatish Balay /*@
22119b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
2212f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
22139b94acceSBarry Smith 
2214fee21e36SBarry Smith    Not Collective
2215fee21e36SBarry Smith 
221636851e7fSLois Curfman McInnes    Level: advanced
221736851e7fSLois Curfman McInnes 
22189b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
22199b94acceSBarry Smith 
22209b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
22219b94acceSBarry Smith @*/
222263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegisterDestroy(void)
22239b94acceSBarry Smith {
2224dfbe8321SBarry Smith   PetscErrorCode ierr;
222582bf6240SBarry Smith 
22263a40ed3dSBarry Smith   PetscFunctionBegin;
22271441b1d3SBarry Smith   ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
22284c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
22293a40ed3dSBarry Smith   PetscFunctionReturn(0);
22309b94acceSBarry Smith }
22319b94acceSBarry Smith 
22324a2ae208SSatish Balay #undef __FUNCT__
22334a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
22349b94acceSBarry Smith /*@C
22359a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
22369b94acceSBarry Smith 
2237c7afd0dbSLois Curfman McInnes    Not Collective
2238c7afd0dbSLois Curfman McInnes 
22399b94acceSBarry Smith    Input Parameter:
22404b0e389bSBarry Smith .  snes - nonlinear solver context
22419b94acceSBarry Smith 
22429b94acceSBarry Smith    Output Parameter:
22433a7fca6bSBarry Smith .  type - SNES method (a character string)
22449b94acceSBarry Smith 
224536851e7fSLois Curfman McInnes    Level: intermediate
224636851e7fSLois Curfman McInnes 
2247454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
22489b94acceSBarry Smith @*/
2249a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetType(SNES snes,const SNESType *type)
22509b94acceSBarry Smith {
22513a40ed3dSBarry Smith   PetscFunctionBegin;
22524482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
22534482741eSBarry Smith   PetscValidPointer(type,2);
22547adad957SLisandro Dalcin   *type = ((PetscObject)snes)->type_name;
22553a40ed3dSBarry Smith   PetscFunctionReturn(0);
22569b94acceSBarry Smith }
22579b94acceSBarry Smith 
22584a2ae208SSatish Balay #undef __FUNCT__
22594a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
226052baeb72SSatish Balay /*@
22619b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
22629b94acceSBarry Smith    stored.
22639b94acceSBarry Smith 
2264c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2265c7afd0dbSLois Curfman McInnes 
22669b94acceSBarry Smith    Input Parameter:
22679b94acceSBarry Smith .  snes - the SNES context
22689b94acceSBarry Smith 
22699b94acceSBarry Smith    Output Parameter:
22709b94acceSBarry Smith .  x - the solution
22719b94acceSBarry Smith 
227270e92668SMatthew Knepley    Level: intermediate
227336851e7fSLois Curfman McInnes 
22749b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
22759b94acceSBarry Smith 
227685385478SLisandro Dalcin .seealso:  SNESGetSolutionUpdate(), SNESGetFunction()
22779b94acceSBarry Smith @*/
227863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolution(SNES snes,Vec *x)
22799b94acceSBarry Smith {
22803a40ed3dSBarry Smith   PetscFunctionBegin;
22814482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
22824482741eSBarry Smith   PetscValidPointer(x,2);
228385385478SLisandro Dalcin   *x = snes->vec_sol;
228470e92668SMatthew Knepley   PetscFunctionReturn(0);
228570e92668SMatthew Knepley }
228670e92668SMatthew Knepley 
228770e92668SMatthew Knepley #undef __FUNCT__
22884a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
228952baeb72SSatish Balay /*@
22909b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
22919b94acceSBarry Smith    stored.
22929b94acceSBarry Smith 
2293c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2294c7afd0dbSLois Curfman McInnes 
22959b94acceSBarry Smith    Input Parameter:
22969b94acceSBarry Smith .  snes - the SNES context
22979b94acceSBarry Smith 
22989b94acceSBarry Smith    Output Parameter:
22999b94acceSBarry Smith .  x - the solution update
23009b94acceSBarry Smith 
230136851e7fSLois Curfman McInnes    Level: advanced
230236851e7fSLois Curfman McInnes 
23039b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
23049b94acceSBarry Smith 
230585385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction()
23069b94acceSBarry Smith @*/
230763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolutionUpdate(SNES snes,Vec *x)
23089b94acceSBarry Smith {
23093a40ed3dSBarry Smith   PetscFunctionBegin;
23104482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
23114482741eSBarry Smith   PetscValidPointer(x,2);
231285385478SLisandro Dalcin   *x = snes->vec_sol_update;
23133a40ed3dSBarry Smith   PetscFunctionReturn(0);
23149b94acceSBarry Smith }
23159b94acceSBarry Smith 
23164a2ae208SSatish Balay #undef __FUNCT__
23174a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
23189b94acceSBarry Smith /*@C
23193638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
23209b94acceSBarry Smith 
2321c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2322c7afd0dbSLois Curfman McInnes 
23239b94acceSBarry Smith    Input Parameter:
23249b94acceSBarry Smith .  snes - the SNES context
23259b94acceSBarry Smith 
23269b94acceSBarry Smith    Output Parameter:
23277bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
232870e92668SMatthew Knepley .  func - the function (or PETSC_NULL)
232970e92668SMatthew Knepley -  ctx - the function context (or PETSC_NULL)
23309b94acceSBarry Smith 
233136851e7fSLois Curfman McInnes    Level: advanced
233236851e7fSLois Curfman McInnes 
2333a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
23349b94acceSBarry Smith 
23354b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution()
23369b94acceSBarry Smith @*/
233770e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx)
23389b94acceSBarry Smith {
23393a40ed3dSBarry Smith   PetscFunctionBegin;
23404482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
234185385478SLisandro Dalcin   if (r)    *r    = snes->vec_func;
2342e7788613SBarry Smith   if (func) *func = snes->ops->computefunction;
234370e92668SMatthew Knepley   if (ctx)  *ctx  = snes->funP;
23443a40ed3dSBarry Smith   PetscFunctionReturn(0);
23459b94acceSBarry Smith }
23469b94acceSBarry Smith 
23474a2ae208SSatish Balay #undef __FUNCT__
23484a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
23493c7409f5SSatish Balay /*@C
23503c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2351d850072dSLois Curfman McInnes    SNES options in the database.
23523c7409f5SSatish Balay 
2353fee21e36SBarry Smith    Collective on SNES
2354fee21e36SBarry Smith 
2355c7afd0dbSLois Curfman McInnes    Input Parameter:
2356c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2357c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2358c7afd0dbSLois Curfman McInnes 
2359d850072dSLois Curfman McInnes    Notes:
2360a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2361c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2362d850072dSLois Curfman McInnes 
236336851e7fSLois Curfman McInnes    Level: advanced
236436851e7fSLois Curfman McInnes 
23653c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2366a86d99e1SLois Curfman McInnes 
2367a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
23683c7409f5SSatish Balay @*/
236963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetOptionsPrefix(SNES snes,const char prefix[])
23703c7409f5SSatish Balay {
2371dfbe8321SBarry Smith   PetscErrorCode ierr;
23723c7409f5SSatish Balay 
23733a40ed3dSBarry Smith   PetscFunctionBegin;
23744482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2375639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
23761cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
237794b7f48cSBarry Smith   ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
23783a40ed3dSBarry Smith   PetscFunctionReturn(0);
23793c7409f5SSatish Balay }
23803c7409f5SSatish Balay 
23814a2ae208SSatish Balay #undef __FUNCT__
23824a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
23833c7409f5SSatish Balay /*@C
2384f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2385d850072dSLois Curfman McInnes    SNES options in the database.
23863c7409f5SSatish Balay 
2387fee21e36SBarry Smith    Collective on SNES
2388fee21e36SBarry Smith 
2389c7afd0dbSLois Curfman McInnes    Input Parameters:
2390c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2391c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2392c7afd0dbSLois Curfman McInnes 
2393d850072dSLois Curfman McInnes    Notes:
2394a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2395c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2396d850072dSLois Curfman McInnes 
239736851e7fSLois Curfman McInnes    Level: advanced
239836851e7fSLois Curfman McInnes 
23993c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2400a86d99e1SLois Curfman McInnes 
2401a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
24023c7409f5SSatish Balay @*/
240363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAppendOptionsPrefix(SNES snes,const char prefix[])
24043c7409f5SSatish Balay {
2405dfbe8321SBarry Smith   PetscErrorCode ierr;
24063c7409f5SSatish Balay 
24073a40ed3dSBarry Smith   PetscFunctionBegin;
24084482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2409639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
24101cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
241194b7f48cSBarry Smith   ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
24123a40ed3dSBarry Smith   PetscFunctionReturn(0);
24133c7409f5SSatish Balay }
24143c7409f5SSatish Balay 
24154a2ae208SSatish Balay #undef __FUNCT__
24164a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
24179ab63eb5SSatish Balay /*@C
24183c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
24193c7409f5SSatish Balay    SNES options in the database.
24203c7409f5SSatish Balay 
2421c7afd0dbSLois Curfman McInnes    Not Collective
2422c7afd0dbSLois Curfman McInnes 
24233c7409f5SSatish Balay    Input Parameter:
24243c7409f5SSatish Balay .  snes - the SNES context
24253c7409f5SSatish Balay 
24263c7409f5SSatish Balay    Output Parameter:
24273c7409f5SSatish Balay .  prefix - pointer to the prefix string used
24283c7409f5SSatish Balay 
24294ef407dbSRichard Tran Mills    Notes: On the fortran side, the user should pass in a string 'prefix' of
24309ab63eb5SSatish Balay    sufficient length to hold the prefix.
24319ab63eb5SSatish Balay 
243236851e7fSLois Curfman McInnes    Level: advanced
243336851e7fSLois Curfman McInnes 
24343c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2435a86d99e1SLois Curfman McInnes 
2436a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
24373c7409f5SSatish Balay @*/
2438e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetOptionsPrefix(SNES snes,const char *prefix[])
24393c7409f5SSatish Balay {
2440dfbe8321SBarry Smith   PetscErrorCode ierr;
24413c7409f5SSatish Balay 
24423a40ed3dSBarry Smith   PetscFunctionBegin;
24434482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2444639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
24453a40ed3dSBarry Smith   PetscFunctionReturn(0);
24463c7409f5SSatish Balay }
24473c7409f5SSatish Balay 
2448b2002411SLois Curfman McInnes 
24494a2ae208SSatish Balay #undef __FUNCT__
24504a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
24513cea93caSBarry Smith /*@C
24523cea93caSBarry Smith   SNESRegister - See SNESRegisterDynamic()
24533cea93caSBarry Smith 
24547f6c08e0SMatthew Knepley   Level: advanced
24553cea93caSBarry Smith @*/
245663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES))
2457b2002411SLois Curfman McInnes {
2458e2d1d2b7SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
2459dfbe8321SBarry Smith   PetscErrorCode ierr;
2460b2002411SLois Curfman McInnes 
2461b2002411SLois Curfman McInnes   PetscFunctionBegin;
2462b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2463c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
2464b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2465b2002411SLois Curfman McInnes }
2466da9b6338SBarry Smith 
2467da9b6338SBarry Smith #undef __FUNCT__
2468da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin"
246963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESTestLocalMin(SNES snes)
2470da9b6338SBarry Smith {
2471dfbe8321SBarry Smith   PetscErrorCode ierr;
247277431f27SBarry Smith   PetscInt       N,i,j;
2473da9b6338SBarry Smith   Vec            u,uh,fh;
2474da9b6338SBarry Smith   PetscScalar    value;
2475da9b6338SBarry Smith   PetscReal      norm;
2476da9b6338SBarry Smith 
2477da9b6338SBarry Smith   PetscFunctionBegin;
2478da9b6338SBarry Smith   ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr);
2479da9b6338SBarry Smith   ierr = VecDuplicate(u,&uh);CHKERRQ(ierr);
2480da9b6338SBarry Smith   ierr = VecDuplicate(u,&fh);CHKERRQ(ierr);
2481da9b6338SBarry Smith 
2482da9b6338SBarry Smith   /* currently only works for sequential */
2483da9b6338SBarry Smith   ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n");
2484da9b6338SBarry Smith   ierr = VecGetSize(u,&N);CHKERRQ(ierr);
2485da9b6338SBarry Smith   for (i=0; i<N; i++) {
2486da9b6338SBarry Smith     ierr = VecCopy(u,uh);CHKERRQ(ierr);
248777431f27SBarry Smith     ierr  = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr);
2488da9b6338SBarry Smith     for (j=-10; j<11; j++) {
2489ccae9161SBarry Smith       value = PetscSign(j)*exp(PetscAbs(j)-10.0);
2490da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
24913ab0aad5SBarry Smith       ierr  = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr);
2492da9b6338SBarry Smith       ierr  = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr);
249377431f27SBarry Smith       ierr  = PetscPrintf(PETSC_COMM_WORLD,"       j norm %D %18.16e\n",j,norm);CHKERRQ(ierr);
2494da9b6338SBarry Smith       value = -value;
2495da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
2496da9b6338SBarry Smith     }
2497da9b6338SBarry Smith   }
2498da9b6338SBarry Smith   ierr = VecDestroy(uh);CHKERRQ(ierr);
2499da9b6338SBarry Smith   ierr = VecDestroy(fh);CHKERRQ(ierr);
2500da9b6338SBarry Smith   PetscFunctionReturn(0);
2501da9b6338SBarry Smith }
250271f87433Sdalcinl 
250371f87433Sdalcinl #undef __FUNCT__
2504fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW"
250571f87433Sdalcinl /*@
2506fa9f3622SBarry Smith    SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for
250771f87433Sdalcinl    computing relative tolerance for linear solvers within an inexact
250871f87433Sdalcinl    Newton method.
250971f87433Sdalcinl 
251071f87433Sdalcinl    Collective on SNES
251171f87433Sdalcinl 
251271f87433Sdalcinl    Input Parameters:
251371f87433Sdalcinl +  snes - SNES context
251471f87433Sdalcinl -  flag - PETSC_TRUE or PETSC_FALSE
251571f87433Sdalcinl 
251671f87433Sdalcinl    Notes:
251771f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
251871f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
251971f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
252071f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
252171f87433Sdalcinl    solver.
252271f87433Sdalcinl 
252371f87433Sdalcinl    Level: advanced
252471f87433Sdalcinl 
252571f87433Sdalcinl    Reference:
252671f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
252771f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
252871f87433Sdalcinl 
252971f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
253071f87433Sdalcinl 
2531fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
253271f87433Sdalcinl @*/
2533fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetUseEW(SNES snes,PetscTruth flag)
253471f87433Sdalcinl {
253571f87433Sdalcinl   PetscFunctionBegin;
253671f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
253771f87433Sdalcinl   snes->ksp_ewconv = flag;
253871f87433Sdalcinl   PetscFunctionReturn(0);
253971f87433Sdalcinl }
254071f87433Sdalcinl 
254171f87433Sdalcinl #undef __FUNCT__
2542fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW"
254371f87433Sdalcinl /*@
2544fa9f3622SBarry Smith    SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method
254571f87433Sdalcinl    for computing relative tolerance for linear solvers within an
254671f87433Sdalcinl    inexact Newton method.
254771f87433Sdalcinl 
254871f87433Sdalcinl    Not Collective
254971f87433Sdalcinl 
255071f87433Sdalcinl    Input Parameter:
255171f87433Sdalcinl .  snes - SNES context
255271f87433Sdalcinl 
255371f87433Sdalcinl    Output Parameter:
255471f87433Sdalcinl .  flag - PETSC_TRUE or PETSC_FALSE
255571f87433Sdalcinl 
255671f87433Sdalcinl    Notes:
255771f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
255871f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
255971f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
256071f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
256171f87433Sdalcinl    solver.
256271f87433Sdalcinl 
256371f87433Sdalcinl    Level: advanced
256471f87433Sdalcinl 
256571f87433Sdalcinl    Reference:
256671f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
256771f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
256871f87433Sdalcinl 
256971f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
257071f87433Sdalcinl 
2571fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
257271f87433Sdalcinl @*/
2573fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetUseEW(SNES snes, PetscTruth *flag)
257471f87433Sdalcinl {
257571f87433Sdalcinl   PetscFunctionBegin;
257671f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
257771f87433Sdalcinl   PetscValidPointer(flag,2);
257871f87433Sdalcinl   *flag = snes->ksp_ewconv;
257971f87433Sdalcinl   PetscFunctionReturn(0);
258071f87433Sdalcinl }
258171f87433Sdalcinl 
258271f87433Sdalcinl #undef __FUNCT__
2583fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW"
258471f87433Sdalcinl /*@
2585fa9f3622SBarry Smith    SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker
258671f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
258771f87433Sdalcinl    Newton method.
258871f87433Sdalcinl 
258971f87433Sdalcinl    Collective on SNES
259071f87433Sdalcinl 
259171f87433Sdalcinl    Input Parameters:
259271f87433Sdalcinl +    snes - SNES context
259371f87433Sdalcinl .    version - version 1, 2 (default is 2) or 3
259471f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
259571f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
259671f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
259771f87433Sdalcinl              (0 <= gamma2 <= 1)
259871f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
259971f87433Sdalcinl .    alpha2 - power for safeguard
260071f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
260171f87433Sdalcinl 
260271f87433Sdalcinl    Note:
260371f87433Sdalcinl    Version 3 was contributed by Luis Chacon, June 2006.
260471f87433Sdalcinl 
260571f87433Sdalcinl    Use PETSC_DEFAULT to retain the default for any of the parameters.
260671f87433Sdalcinl 
260771f87433Sdalcinl    Level: advanced
260871f87433Sdalcinl 
260971f87433Sdalcinl    Reference:
261071f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
261171f87433Sdalcinl    inexact Newton method", Utah State University Math. Stat. Dept. Res.
261271f87433Sdalcinl    Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput.
261371f87433Sdalcinl 
261471f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters
261571f87433Sdalcinl 
2616fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW()
261771f87433Sdalcinl @*/
2618fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max,
261971f87433Sdalcinl 							    PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold)
262071f87433Sdalcinl {
2621fa9f3622SBarry Smith   SNESKSPEW *kctx;
262271f87433Sdalcinl   PetscFunctionBegin;
262371f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2624fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
262571f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
262671f87433Sdalcinl 
262771f87433Sdalcinl   if (version != PETSC_DEFAULT)   kctx->version   = version;
262871f87433Sdalcinl   if (rtol_0 != PETSC_DEFAULT)    kctx->rtol_0    = rtol_0;
262971f87433Sdalcinl   if (rtol_max != PETSC_DEFAULT)  kctx->rtol_max  = rtol_max;
263071f87433Sdalcinl   if (gamma != PETSC_DEFAULT)     kctx->gamma     = gamma;
263171f87433Sdalcinl   if (alpha != PETSC_DEFAULT)     kctx->alpha     = alpha;
263271f87433Sdalcinl   if (alpha2 != PETSC_DEFAULT)    kctx->alpha2    = alpha2;
263371f87433Sdalcinl   if (threshold != PETSC_DEFAULT) kctx->threshold = threshold;
263471f87433Sdalcinl 
263571f87433Sdalcinl   if (kctx->version < 1 || kctx->version > 3) {
263671f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version);
263771f87433Sdalcinl   }
263871f87433Sdalcinl   if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) {
263971f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0);
264071f87433Sdalcinl   }
264171f87433Sdalcinl   if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) {
264271f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max);
264371f87433Sdalcinl   }
264471f87433Sdalcinl   if (kctx->gamma < 0.0 || kctx->gamma > 1.0) {
264571f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma);
264671f87433Sdalcinl   }
264771f87433Sdalcinl   if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) {
264871f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha);
264971f87433Sdalcinl   }
265071f87433Sdalcinl   if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) {
265171f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold);
265271f87433Sdalcinl   }
265371f87433Sdalcinl   PetscFunctionReturn(0);
265471f87433Sdalcinl }
265571f87433Sdalcinl 
265671f87433Sdalcinl #undef __FUNCT__
2657fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW"
265871f87433Sdalcinl /*@
2659fa9f3622SBarry Smith    SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker
266071f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
266171f87433Sdalcinl    Newton method.
266271f87433Sdalcinl 
266371f87433Sdalcinl    Not Collective
266471f87433Sdalcinl 
266571f87433Sdalcinl    Input Parameters:
266671f87433Sdalcinl      snes - SNES context
266771f87433Sdalcinl 
266871f87433Sdalcinl    Output Parameters:
266971f87433Sdalcinl +    version - version 1, 2 (default is 2) or 3
267071f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
267171f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
267271f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
267371f87433Sdalcinl              (0 <= gamma2 <= 1)
267471f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
267571f87433Sdalcinl .    alpha2 - power for safeguard
267671f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
267771f87433Sdalcinl 
267871f87433Sdalcinl    Level: advanced
267971f87433Sdalcinl 
268071f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters
268171f87433Sdalcinl 
2682fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW()
268371f87433Sdalcinl @*/
2684fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max,
268571f87433Sdalcinl 							    PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold)
268671f87433Sdalcinl {
2687fa9f3622SBarry Smith   SNESKSPEW *kctx;
268871f87433Sdalcinl   PetscFunctionBegin;
268971f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2690fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
269171f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
269271f87433Sdalcinl   if(version)   *version   = kctx->version;
269371f87433Sdalcinl   if(rtol_0)    *rtol_0    = kctx->rtol_0;
269471f87433Sdalcinl   if(rtol_max)  *rtol_max  = kctx->rtol_max;
269571f87433Sdalcinl   if(gamma)     *gamma     = kctx->gamma;
269671f87433Sdalcinl   if(alpha)     *alpha     = kctx->alpha;
269771f87433Sdalcinl   if(alpha2)    *alpha2    = kctx->alpha2;
269871f87433Sdalcinl   if(threshold) *threshold = kctx->threshold;
269971f87433Sdalcinl   PetscFunctionReturn(0);
270071f87433Sdalcinl }
270171f87433Sdalcinl 
270271f87433Sdalcinl #undef __FUNCT__
2703fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve"
2704fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x)
270571f87433Sdalcinl {
270671f87433Sdalcinl   PetscErrorCode ierr;
2707fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
270871f87433Sdalcinl   PetscReal      rtol=PETSC_DEFAULT,stol;
270971f87433Sdalcinl 
271071f87433Sdalcinl   PetscFunctionBegin;
271171f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
271271f87433Sdalcinl   if (!snes->iter) { /* first time in, so use the original user rtol */
271371f87433Sdalcinl     rtol = kctx->rtol_0;
271471f87433Sdalcinl   } else {
271571f87433Sdalcinl     if (kctx->version == 1) {
271671f87433Sdalcinl       rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last;
271771f87433Sdalcinl       if (rtol < 0.0) rtol = -rtol;
271871f87433Sdalcinl       stol = pow(kctx->rtol_last,kctx->alpha2);
271971f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
272071f87433Sdalcinl     } else if (kctx->version == 2) {
272171f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
272271f87433Sdalcinl       stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha);
272371f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
272471f87433Sdalcinl     } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */
272571f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
272671f87433Sdalcinl       /* safeguard: avoid sharp decrease of rtol */
272771f87433Sdalcinl       stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha);
272871f87433Sdalcinl       stol = PetscMax(rtol,stol);
272971f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
273071f87433Sdalcinl       /* safeguard: avoid oversolving */
273171f87433Sdalcinl       stol = kctx->gamma*(snes->ttol)/snes->norm;
273271f87433Sdalcinl       stol = PetscMax(rtol,stol);
273371f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
273471f87433Sdalcinl     } else SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version);
273571f87433Sdalcinl   }
273671f87433Sdalcinl   /* safeguard: avoid rtol greater than one */
273771f87433Sdalcinl   rtol = PetscMin(rtol,kctx->rtol_max);
273871f87433Sdalcinl   ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
273971f87433Sdalcinl   ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr);
274071f87433Sdalcinl   PetscFunctionReturn(0);
274171f87433Sdalcinl }
274271f87433Sdalcinl 
274371f87433Sdalcinl #undef __FUNCT__
2744fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve"
2745fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x)
274671f87433Sdalcinl {
274771f87433Sdalcinl   PetscErrorCode ierr;
2748fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
274971f87433Sdalcinl   PCSide         pcside;
275071f87433Sdalcinl   Vec            lres;
275171f87433Sdalcinl 
275271f87433Sdalcinl   PetscFunctionBegin;
275371f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
275471f87433Sdalcinl   ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr);
275571f87433Sdalcinl   ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr);
275671f87433Sdalcinl   if (kctx->version == 1) {
275771f87433Sdalcinl     ierr = KSPGetPreconditionerSide(ksp,&pcside);CHKERRQ(ierr);
275871f87433Sdalcinl     if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */
275971f87433Sdalcinl       /* KSP residual is true linear residual */
276071f87433Sdalcinl       ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr);
276171f87433Sdalcinl     } else {
276271f87433Sdalcinl       /* KSP residual is preconditioned residual */
276371f87433Sdalcinl       /* compute true linear residual norm */
276471f87433Sdalcinl       ierr = VecDuplicate(b,&lres);CHKERRQ(ierr);
276571f87433Sdalcinl       ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr);
276671f87433Sdalcinl       ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr);
276771f87433Sdalcinl       ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr);
276871f87433Sdalcinl       ierr = VecDestroy(lres);CHKERRQ(ierr);
276971f87433Sdalcinl     }
277071f87433Sdalcinl   }
277171f87433Sdalcinl   PetscFunctionReturn(0);
277271f87433Sdalcinl }
277371f87433Sdalcinl 
277471f87433Sdalcinl #undef __FUNCT__
277571f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve"
277671f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x)
277771f87433Sdalcinl {
277871f87433Sdalcinl   PetscErrorCode ierr;
277971f87433Sdalcinl 
278071f87433Sdalcinl   PetscFunctionBegin;
2781fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr);  }
278271f87433Sdalcinl   ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);
2783fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); }
278471f87433Sdalcinl   PetscFunctionReturn(0);
278571f87433Sdalcinl }
2786