xref: /petsc/src/snes/interface/snes.c (revision ebd3b9af57854535fc709985e0eff1ba8ef1a7e3)
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)
180e35cf81dSBarry Smith .  -snes_lag_jacobian <lag> - how often Jacobian is rebuilt (use -1 to never rebuild)
181b39c3a46SLois Curfman McInnes .  -snes_trtol <trtol> - trust region tolerance
1822492ecdbSBarry Smith .  -snes_no_convergence_test - skip convergence test in nonlinear
18382738288SBarry Smith                                solver; hence iterations will continue until max_it
1841fbbfb26SLois Curfman McInnes                                or some other criterion is reached. Saves expense
18582738288SBarry Smith                                of convergence test
186e8105e01SRichard Katz .  -snes_monitor <optional filename> - prints residual norm at each iteration. if no
187e8105e01SRichard Katz                                        filename given prints to stdout
188a6570f20SBarry Smith .  -snes_monitor_solution - plots solution at each iteration
189a6570f20SBarry Smith .  -snes_monitor_residual - plots residual (not its norm) at each iteration
190a6570f20SBarry Smith .  -snes_monitor_solution_update - plots update to solution at each iteration
191a6570f20SBarry Smith .  -snes_monitor_draw - plots residual norm at each iteration
192e24b481bSBarry Smith .  -snes_fd - use finite differences to compute Jacobian; very slow, only for testing
1935968eb51SBarry Smith .  -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration
194fee2055bSBarry Smith -  -snes_converged_reason - print the reason for convergence/divergence after each solve
19582738288SBarry Smith 
19682738288SBarry Smith     Options Database for Eisenstat-Walker method:
197fa9f3622SBarry Smith +  -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence
1984b27c08aSLois Curfman McInnes .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
19936851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
20036851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
20136851e7fSLois Curfman McInnes .  -snes_ksp_ew_gamma <gamma> - Sets gamma
20236851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha <alpha> - Sets alpha
20336851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
20436851e7fSLois Curfman McInnes -  -snes_ksp_ew_threshold <threshold> - Sets threshold
20582738288SBarry Smith 
20611ca99fdSLois Curfman McInnes    Notes:
20711ca99fdSLois Curfman McInnes    To see all options, run your program with the -help option or consult
20811ca99fdSLois Curfman McInnes    the users manual.
20983e2fdc7SBarry Smith 
21036851e7fSLois Curfman McInnes    Level: beginner
21136851e7fSLois Curfman McInnes 
2129b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database
2139b94acceSBarry Smith 
21469ed319cSSatish Balay .seealso: SNESSetOptionsPrefix()
2159b94acceSBarry Smith @*/
21663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFromOptions(SNES snes)
2179b94acceSBarry Smith {
218f1af5d2fSBarry Smith   PetscTruth              flg;
219a8054027SBarry Smith   PetscInt                i,indx,lag;
22085385478SLisandro Dalcin   const char              *deft = SNESLS;
22185385478SLisandro Dalcin   const char              *convtests[] = {"default","skip"};
22285385478SLisandro Dalcin   SNESKSPEW               *kctx = NULL;
223e8105e01SRichard Katz   char                    type[256], monfilename[PETSC_MAX_PATH_LEN];
22423d894e5SBarry Smith   PetscViewerASCIIMonitor monviewer;
22585385478SLisandro Dalcin   PetscErrorCode          ierr;
2269b94acceSBarry Smith 
2273a40ed3dSBarry Smith   PetscFunctionBegin;
2284482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
229ca161407SBarry Smith 
2307adad957SLisandro Dalcin   ierr = PetscOptionsBegin(((PetscObject)snes)->comm,((PetscObject)snes)->prefix,"Nonlinear solver (SNES) options","SNES");CHKERRQ(ierr);
231186905e3SBarry Smith     if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
2327adad957SLisandro Dalcin     if (((PetscObject)snes)->type_name) { deft = ((PetscObject)snes)->type_name; }
233b0a32e0cSBarry Smith     ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr);
234d64ed03dSBarry Smith     if (flg) {
235186905e3SBarry Smith       ierr = SNESSetType(snes,type);CHKERRQ(ierr);
2367adad957SLisandro Dalcin     } else if (!((PetscObject)snes)->type_name) {
237186905e3SBarry Smith       ierr = SNESSetType(snes,deft);CHKERRQ(ierr);
238d64ed03dSBarry Smith     }
239909c8a9fSBarry Smith     ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr);
24093c39befSBarry Smith 
24187828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_stol","Stop if step length less then","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr);
24270441072SBarry Smith     ierr = PetscOptionsReal("-snes_atol","Stop if function norm less then","SNESSetTolerances",snes->abstol,&snes->abstol,0);CHKERRQ(ierr);
243186905e3SBarry Smith 
24487828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less then","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr);
245b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr);
246b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr);
24750ffb88aSMatthew Knepley     ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr);
248ddf469c8SBarry Smith     ierr = PetscOptionsInt("-snes_max_linear_solve_fail","Maximum failures in linear solves allowed","SNESSetMaxLinearSolveFailures",snes->maxLinearSolveFailures,&snes->maxLinearSolveFailures,PETSC_NULL);CHKERRQ(ierr);
24985385478SLisandro Dalcin 
250a8054027SBarry Smith     ierr = PetscOptionsInt("-snes_lag_preconditioner","How often to rebuild preconditioner","SNESSetLagPreconditioner",snes->lagpreconditioner,&lag,&flg);CHKERRQ(ierr);
251a8054027SBarry Smith     if (flg) {
252a8054027SBarry Smith       ierr = SNESSetLagPreconditioner(snes,lag);CHKERRQ(ierr);
253a8054027SBarry Smith     }
254e35cf81dSBarry Smith     ierr = PetscOptionsInt("-snes_lag_jacobian","How often to rebuild Jacobian","SNESSetLagJacobian",snes->lagjacobian,&lag,&flg);CHKERRQ(ierr);
255e35cf81dSBarry Smith     if (flg) {
256e35cf81dSBarry Smith       ierr = SNESSetLagJacobian(snes,lag);CHKERRQ(ierr);
257e35cf81dSBarry Smith     }
258a8054027SBarry Smith 
25985385478SLisandro Dalcin     ierr = PetscOptionsEList("-snes_convergence_test","Convergence test","SNESSetConvergenceTest",convtests,2,"default",&indx,&flg);CHKERRQ(ierr);
26085385478SLisandro Dalcin     if (flg) {
26185385478SLisandro Dalcin       switch (indx) {
2627f7931b9SBarry Smith       case 0: ierr = SNESSetConvergenceTest(snes,SNESDefaultConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break;
2637f7931b9SBarry Smith       case 1: ierr = SNESSetConvergenceTest(snes,SNESSkipConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);    break;
26485385478SLisandro Dalcin       }
26585385478SLisandro Dalcin     }
26685385478SLisandro Dalcin 
2675968eb51SBarry Smith     ierr = PetscOptionsName("-snes_converged_reason","Print reason for converged or diverged","SNESSolve",&flg);CHKERRQ(ierr);
2685968eb51SBarry Smith     if (flg) {
2695968eb51SBarry Smith       snes->printreason = PETSC_TRUE;
2705968eb51SBarry Smith     }
271186905e3SBarry Smith 
27285385478SLisandro Dalcin     kctx = (SNESKSPEW *)snes->kspconvctx;
27385385478SLisandro Dalcin 
274fa9f3622SBarry Smith     ierr = PetscOptionsTruth("-snes_ksp_ew","Use Eisentat-Walker linear system convergence test","SNESKSPSetUseEW",snes->ksp_ewconv,&snes->ksp_ewconv,PETSC_NULL);CHKERRQ(ierr);
275186905e3SBarry Smith 
276fa9f3622SBarry Smith     ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNESKSPSetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr);
277fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNESKSPSetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr);
278fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNESKSPSetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr);
279fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNESKSPSetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr);
280fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNESKSPSetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr);
281fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNESKSPSetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr);
282fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNESKSPSetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr);
283186905e3SBarry Smith 
284a6570f20SBarry Smith     ierr = PetscOptionsName("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",&flg);CHKERRQ(ierr);
285a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);}
286eabae89aSBarry Smith 
287a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor","Monitor norm of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
288e8105e01SRichard Katz     if (flg) {
289050a712dSBarry Smith       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr);
29023d894e5SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefault,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
291e8105e01SRichard Katz     }
292eabae89aSBarry Smith 
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;
852e35cf81dSBarry 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 
1046e35cf81dSBarry Smith   Options Database Keys:
1047e35cf81dSBarry Smith +    -snes_lag_preconditioner <lag>
1048e35cf81dSBarry Smith -    -snes_lag_jacobian <lag>
1049e35cf81dSBarry 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 
1061e35cf81dSBarry 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;
1066*ebd3b9afSBarry Smith   PetscTruth     flag;
10673a40ed3dSBarry Smith 
10683a40ed3dSBarry Smith   PetscFunctionBegin;
10694482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
10704482741eSBarry Smith   PetscValidHeaderSpecific(X,VEC_COOKIE,2);
10714482741eSBarry Smith   PetscValidPointer(flg,5);
1072c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,X,2);
1073e7788613SBarry Smith   if (!snes->ops->computejacobian) PetscFunctionReturn(0);
1074*ebd3b9afSBarry Smith 
1075*ebd3b9afSBarry Smith   /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */
1076*ebd3b9afSBarry Smith 
1077fe3ffe1eSBarry Smith   if (snes->lagjacobian == -2) {
1078fe3ffe1eSBarry Smith     snes->lagjacobian = -1;
1079fe3ffe1eSBarry Smith     ierr = PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n");CHKERRQ(ierr);
1080fe3ffe1eSBarry Smith   } else if (snes->lagjacobian == -1) {
1081e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1082e35cf81dSBarry Smith     ierr = PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");CHKERRQ(ierr);
1083*ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1084*ebd3b9afSBarry Smith     if (flag) {
1085*ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1086*ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1087*ebd3b9afSBarry Smith     }
1088e35cf81dSBarry Smith     PetscFunctionReturn(0);
1089e35cf81dSBarry Smith   } else if (snes->lagjacobian > 1 && snes->iter % snes->lagjacobian) {
1090e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1091e35cf81dSBarry Smith     ierr = PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);CHKERRQ(ierr);
1092*ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1093*ebd3b9afSBarry Smith     if (flag) {
1094*ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1095*ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1096*ebd3b9afSBarry Smith     }
1097e35cf81dSBarry Smith     PetscFunctionReturn(0);
1098e35cf81dSBarry Smith   }
1099e35cf81dSBarry Smith 
1100c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1101e35cf81dSBarry Smith   ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1102d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
1103dc67937bSBarry Smith   CHKMEMQ;
1104e7788613SBarry Smith   ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
1105dc67937bSBarry Smith   CHKMEMQ;
1106d64ed03dSBarry Smith   PetscStackPop;
1107d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1108a8054027SBarry Smith 
1109a8054027SBarry Smith   if (snes->lagpreconditioner == -1) {
1110a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1111a8054027SBarry Smith     ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr);
1112a8054027SBarry Smith   } else if (snes->lagpreconditioner > 1 && snes->iter % snes->lagpreconditioner) {
1113a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1114a8054027SBarry Smith     ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr);
1115a8054027SBarry Smith   }
1116a8054027SBarry Smith 
11176d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
11186ce558aeSBarry Smith   /* PetscValidHeaderSpecific(*A,MAT_COOKIE,3);
11196ce558aeSBarry Smith     PetscValidHeaderSpecific(*B,MAT_COOKIE,4);   */
11203a40ed3dSBarry Smith   PetscFunctionReturn(0);
11219b94acceSBarry Smith }
11229b94acceSBarry Smith 
11234a2ae208SSatish Balay #undef __FUNCT__
11244a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian"
11259b94acceSBarry Smith /*@C
11269b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1127044dda88SLois Curfman McInnes    location to store the matrix.
11289b94acceSBarry Smith 
1129c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1130c7afd0dbSLois Curfman McInnes 
11319b94acceSBarry Smith    Input Parameters:
1132c7afd0dbSLois Curfman McInnes +  snes - the SNES context
11339b94acceSBarry Smith .  A - Jacobian matrix
11349b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
11359b94acceSBarry Smith .  func - Jacobian evaluation routine
1136c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
11372cd2dfdcSLois Curfman McInnes          Jacobian evaluation routine (may be PETSC_NULL)
11389b94acceSBarry Smith 
11399b94acceSBarry Smith    Calling sequence of func:
11408d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
11419b94acceSBarry Smith 
1142c7afd0dbSLois Curfman McInnes +  x - input vector
11439b94acceSBarry Smith .  A - Jacobian matrix
11449b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1145ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
11462b668275SBarry Smith    structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER
1147c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
11489b94acceSBarry Smith 
11499b94acceSBarry Smith    Notes:
115094b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
11512cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1152ac21db08SLois Curfman McInnes 
1153ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
11549b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
11559b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
11569b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
11579b94acceSBarry Smith    throughout the global iterations.
11589b94acceSBarry Smith 
115916913363SBarry Smith    If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on
116016913363SBarry Smith    each matrix.
116116913363SBarry Smith 
116236851e7fSLois Curfman McInnes    Level: beginner
116336851e7fSLois Curfman McInnes 
11649b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
11659b94acceSBarry Smith 
11663ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure
11679b94acceSBarry Smith @*/
116863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
11699b94acceSBarry Smith {
1170dfbe8321SBarry Smith   PetscErrorCode ierr;
11713a7fca6bSBarry Smith 
11723a40ed3dSBarry Smith   PetscFunctionBegin;
11734482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
11744482741eSBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_COOKIE,2);
11754482741eSBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_COOKIE,3);
1176c9780b6fSBarry Smith   if (A) PetscCheckSameComm(snes,1,A,2);
1177c9780b6fSBarry Smith   if (B) PetscCheckSameComm(snes,1,B,2);
1178e7788613SBarry Smith   if (func) snes->ops->computejacobian = func;
11793a7fca6bSBarry Smith   if (ctx)  snes->jacP                 = ctx;
11803a7fca6bSBarry Smith   if (A) {
11817dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
11823a7fca6bSBarry Smith     if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
11839b94acceSBarry Smith     snes->jacobian = A;
11843a7fca6bSBarry Smith   }
11853a7fca6bSBarry Smith   if (B) {
11867dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
11873a7fca6bSBarry Smith     if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
11889b94acceSBarry Smith     snes->jacobian_pre = B;
11893a7fca6bSBarry Smith   }
11903a40ed3dSBarry Smith   PetscFunctionReturn(0);
11919b94acceSBarry Smith }
119262fef451SLois Curfman McInnes 
11934a2ae208SSatish Balay #undef __FUNCT__
11944a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian"
1195c2aafc4cSSatish Balay /*@C
1196b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1197b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1198b4fd4287SBarry Smith 
1199c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1200c7afd0dbSLois Curfman McInnes 
1201b4fd4287SBarry Smith    Input Parameter:
1202b4fd4287SBarry Smith .  snes - the nonlinear solver context
1203b4fd4287SBarry Smith 
1204b4fd4287SBarry Smith    Output Parameters:
1205c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1206b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
120770e92668SMatthew Knepley .  func - location to put Jacobian function (or PETSC_NULL)
120870e92668SMatthew Knepley -  ctx - location to stash Jacobian ctx (or PETSC_NULL)
1209fee21e36SBarry Smith 
121036851e7fSLois Curfman McInnes    Level: advanced
121136851e7fSLois Curfman McInnes 
1212b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1213b4fd4287SBarry Smith @*/
121470e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx)
1215b4fd4287SBarry Smith {
12163a40ed3dSBarry Smith   PetscFunctionBegin;
12174482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1218b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
1219b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
1220e7788613SBarry Smith   if (func) *func = snes->ops->computejacobian;
122170e92668SMatthew Knepley   if (ctx)  *ctx  = snes->jacP;
12223a40ed3dSBarry Smith   PetscFunctionReturn(0);
1223b4fd4287SBarry Smith }
1224b4fd4287SBarry Smith 
12259b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
122663dd3a1aSKris Buschelman EXTERN PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*);
12279b94acceSBarry Smith 
12284a2ae208SSatish Balay #undef __FUNCT__
12294a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp"
12309b94acceSBarry Smith /*@
12319b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1232272ac6f2SLois Curfman McInnes    of a nonlinear solver.
12339b94acceSBarry Smith 
1234fee21e36SBarry Smith    Collective on SNES
1235fee21e36SBarry Smith 
1236c7afd0dbSLois Curfman McInnes    Input Parameters:
123770e92668SMatthew Knepley .  snes - the SNES context
1238c7afd0dbSLois Curfman McInnes 
1239272ac6f2SLois Curfman McInnes    Notes:
1240272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1241272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1242272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1243272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1244272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1245272ac6f2SLois Curfman McInnes 
124636851e7fSLois Curfman McInnes    Level: advanced
124736851e7fSLois Curfman McInnes 
12489b94acceSBarry Smith .keywords: SNES, nonlinear, setup
12499b94acceSBarry Smith 
12509b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
12519b94acceSBarry Smith @*/
125270e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUp(SNES snes)
12539b94acceSBarry Smith {
1254dfbe8321SBarry Smith   PetscErrorCode ierr;
125571f87433Sdalcinl   PetscTruth     flg;
12563a40ed3dSBarry Smith 
12573a40ed3dSBarry Smith   PetscFunctionBegin;
12584482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
12594dc4c822SBarry Smith   if (snes->setupcalled) PetscFunctionReturn(0);
12609b94acceSBarry Smith 
12617adad957SLisandro Dalcin   if (!((PetscObject)snes)->type_name) {
126285385478SLisandro Dalcin     ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr);
126385385478SLisandro Dalcin   }
126485385478SLisandro Dalcin 
12657adad957SLisandro Dalcin   ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf_operator",&flg);CHKERRQ(ierr);
1266c1f60f51SBarry Smith   /*
1267c1f60f51SBarry Smith       This version replaces the user provided Jacobian matrix with a
1268dfa02198SLois Curfman McInnes       matrix-free version but still employs the user-provided preconditioner matrix
1269c1f60f51SBarry Smith   */
1270c1f60f51SBarry Smith   if (flg) {
1271c1f60f51SBarry Smith     Mat J;
1272fef1beadSBarry Smith     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
12733ec795f1SBarry Smith     ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr);
1274ae15b995SBarry Smith     ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr);
12753a7fca6bSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
12763a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
1277c1f60f51SBarry Smith   }
127845fc7adcSBarry Smith 
127903c60df9SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_MAT_SINGLE) && !defined(PETSC_USE_LONG_DOUBLE) && !defined(PETSC_USE_INT)
12807adad957SLisandro Dalcin   ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf_operator2",&flg);CHKERRQ(ierr);
128145fc7adcSBarry Smith   if (flg) {
128245fc7adcSBarry Smith     Mat J;
128345fc7adcSBarry Smith     ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_sol,&J);CHKERRQ(ierr);
128475396ef9SLisandro Dalcin     ierr = PetscInfo(snes,"Setting default matrix-free operator routines (version 2)\n");CHKERRQ(ierr);
128545fc7adcSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
128645fc7adcSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
128745fc7adcSBarry Smith   }
128832a4b47aSMatthew Knepley #endif
128945fc7adcSBarry Smith 
12907adad957SLisandro Dalcin   ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf",&flg);CHKERRQ(ierr);
1291c1f60f51SBarry Smith   /*
1292dfa02198SLois Curfman McInnes       This version replaces both the user-provided Jacobian and the user-
1293c1f60f51SBarry Smith       provided preconditioner matrix with the default matrix free version.
1294c1f60f51SBarry Smith    */
129531615d04SLois Curfman McInnes   if (flg) {
1296272ac6f2SLois Curfman McInnes     Mat  J;
1297b5d62d44SBarry Smith     KSP ksp;
129894b7f48cSBarry Smith     PC   pc;
129975396ef9SLisandro Dalcin     /* create and set matrix-free operator */
1300fef1beadSBarry Smith     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
13013ec795f1SBarry Smith     ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr);
130275396ef9SLisandro Dalcin     ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr);
13033ec795f1SBarry Smith     ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr);
13043a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
1305f3ef73ceSBarry Smith     /* force no preconditioner */
130694b7f48cSBarry Smith     ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
1307b5d62d44SBarry Smith     ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
1308a9815358SBarry Smith     ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&flg);CHKERRQ(ierr);
1309a9815358SBarry Smith     if (!flg) {
131075396ef9SLisandro Dalcin       ierr = PetscInfo(snes,"Setting default matrix-free preconditioner routines;\nThat is no preconditioner is being used\n");CHKERRQ(ierr);
1311f3ef73ceSBarry Smith       ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
1312272ac6f2SLois Curfman McInnes     }
1313a9815358SBarry Smith   }
1314f3ef73ceSBarry Smith 
131585385478SLisandro Dalcin   if (!snes->vec_func && !snes->vec_rhs) {
13161096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
13171096aae1SMatthew Knepley   }
131885385478SLisandro Dalcin   if (!snes->ops->computefunction && !snes->vec_rhs) {
13191096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
13201096aae1SMatthew Knepley   }
132175396ef9SLisandro Dalcin   if (!snes->jacobian) {
132275396ef9SLisandro Dalcin     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetJacobian() first \n or use -snes_mf option");
132375396ef9SLisandro Dalcin   }
1324a8c6a408SBarry Smith   if (snes->vec_func == snes->vec_sol) {
132529bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
1326a8c6a408SBarry Smith   }
1327a703fe33SLois Curfman McInnes 
1328410397dcSLisandro Dalcin   if (snes->ops->setup) {
1329410397dcSLisandro Dalcin     ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr);
1330410397dcSLisandro Dalcin   }
13317aaed0d8SBarry Smith   snes->setupcalled = PETSC_TRUE;
13323a40ed3dSBarry Smith   PetscFunctionReturn(0);
13339b94acceSBarry Smith }
13349b94acceSBarry Smith 
13354a2ae208SSatish Balay #undef __FUNCT__
13364a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
133752baeb72SSatish Balay /*@
13389b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
13399b94acceSBarry Smith    with SNESCreate().
13409b94acceSBarry Smith 
1341c7afd0dbSLois Curfman McInnes    Collective on SNES
1342c7afd0dbSLois Curfman McInnes 
13439b94acceSBarry Smith    Input Parameter:
13449b94acceSBarry Smith .  snes - the SNES context
13459b94acceSBarry Smith 
134636851e7fSLois Curfman McInnes    Level: beginner
134736851e7fSLois Curfman McInnes 
13489b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
13499b94acceSBarry Smith 
135063a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
13519b94acceSBarry Smith @*/
135263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDestroy(SNES snes)
13539b94acceSBarry Smith {
13546849ba73SBarry Smith   PetscErrorCode ierr;
13553a40ed3dSBarry Smith 
13563a40ed3dSBarry Smith   PetscFunctionBegin;
13574482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
13587adad957SLisandro Dalcin   if (--((PetscObject)snes)->refct > 0) PetscFunctionReturn(0);
1359d4bb536fSBarry Smith 
1360be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
13610f5bd95cSBarry Smith   ierr = PetscObjectDepublish(snes);CHKERRQ(ierr);
1362e7788613SBarry Smith   if (snes->ops->destroy) {ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr);}
136385385478SLisandro Dalcin 
136485385478SLisandro Dalcin   if (snes->vec_rhs) {ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr);}
136585385478SLisandro Dalcin   if (snes->vec_sol) {ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr);}
136685385478SLisandro Dalcin   if (snes->vec_func) {ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr);}
13673a7fca6bSBarry Smith   if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
13683a7fca6bSBarry Smith   if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
13691cee3971SBarry Smith   if (snes->ksp) {ierr = KSPDestroy(snes->ksp);CHKERRQ(ierr);}
137085385478SLisandro Dalcin   ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);
1371522c5e43SBarry Smith   if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);}
1372a6570f20SBarry Smith   ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);
13737f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);}
1374a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr);
13753a40ed3dSBarry Smith   PetscFunctionReturn(0);
13769b94acceSBarry Smith }
13779b94acceSBarry Smith 
13789b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
13799b94acceSBarry Smith 
13804a2ae208SSatish Balay #undef __FUNCT__
1381a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner"
1382a8054027SBarry Smith /*@
1383a8054027SBarry Smith    SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve.
1384a8054027SBarry Smith 
1385a8054027SBarry Smith    Collective on SNES
1386a8054027SBarry Smith 
1387a8054027SBarry Smith    Input Parameters:
1388a8054027SBarry Smith +  snes - the SNES context
1389a8054027SBarry 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
1390a8054027SBarry Smith          the Jacobian is built etc.
1391a8054027SBarry Smith 
1392a8054027SBarry Smith    Options Database Keys:
1393a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1394a8054027SBarry Smith 
1395a8054027SBarry Smith    Notes:
1396a8054027SBarry Smith    The default is 1
1397a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1398a8054027SBarry Smith    If  -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use
1399a8054027SBarry Smith 
1400a8054027SBarry Smith    Level: intermediate
1401a8054027SBarry Smith 
1402a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1403a8054027SBarry Smith 
1404e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian()
1405a8054027SBarry Smith 
1406a8054027SBarry Smith @*/
1407a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagPreconditioner(SNES snes,PetscInt lag)
1408a8054027SBarry Smith {
1409a8054027SBarry Smith   PetscFunctionBegin;
1410a8054027SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1411a8054027SBarry Smith   if (lag < -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -1, 1 or greater");
1412a8054027SBarry Smith   if (!lag) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1413a8054027SBarry Smith   snes->lagpreconditioner = lag;
1414a8054027SBarry Smith   PetscFunctionReturn(0);
1415a8054027SBarry Smith }
1416a8054027SBarry Smith 
1417a8054027SBarry Smith #undef __FUNCT__
1418a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner"
1419a8054027SBarry Smith /*@
1420a8054027SBarry Smith    SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt
1421a8054027SBarry Smith 
1422a8054027SBarry Smith    Collective on SNES
1423a8054027SBarry Smith 
1424a8054027SBarry Smith    Input Parameter:
1425a8054027SBarry Smith .  snes - the SNES context
1426a8054027SBarry Smith 
1427a8054027SBarry Smith    Output Parameter:
1428a8054027SBarry 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
1429a8054027SBarry Smith          the Jacobian is built etc.
1430a8054027SBarry Smith 
1431a8054027SBarry Smith    Options Database Keys:
1432a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1433a8054027SBarry Smith 
1434a8054027SBarry Smith    Notes:
1435a8054027SBarry Smith    The default is 1
1436a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1437a8054027SBarry Smith 
1438a8054027SBarry Smith    Level: intermediate
1439a8054027SBarry Smith 
1440a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1441a8054027SBarry Smith 
1442a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner()
1443a8054027SBarry Smith 
1444a8054027SBarry Smith @*/
1445a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagPreconditioner(SNES snes,PetscInt *lag)
1446a8054027SBarry Smith {
1447a8054027SBarry Smith   PetscFunctionBegin;
1448a8054027SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1449a8054027SBarry Smith   *lag = snes->lagpreconditioner;
1450a8054027SBarry Smith   PetscFunctionReturn(0);
1451a8054027SBarry Smith }
1452a8054027SBarry Smith 
1453a8054027SBarry Smith #undef __FUNCT__
1454e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian"
1455e35cf81dSBarry Smith /*@
1456e35cf81dSBarry Smith    SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how
1457e35cf81dSBarry Smith      often the preconditioner is rebuilt.
1458e35cf81dSBarry Smith 
1459e35cf81dSBarry Smith    Collective on SNES
1460e35cf81dSBarry Smith 
1461e35cf81dSBarry Smith    Input Parameters:
1462e35cf81dSBarry Smith +  snes - the SNES context
1463e35cf81dSBarry 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
1464fe3ffe1eSBarry Smith          the Jacobian is built etc. -2 means rebuild at next chance but then never again
1465e35cf81dSBarry Smith 
1466e35cf81dSBarry Smith    Options Database Keys:
1467e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1468e35cf81dSBarry Smith 
1469e35cf81dSBarry Smith    Notes:
1470e35cf81dSBarry Smith    The default is 1
1471e35cf81dSBarry Smith    The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1472fe3ffe1eSBarry Smith    If  -1 is used before the very first nonlinear solve the CODE WILL FAIL! because no Jacobian is used, use -2 to indicate you want it recomputed
1473fe3ffe1eSBarry Smith    at the next Newton step but never again (unless it is reset to another value)
1474e35cf81dSBarry Smith 
1475e35cf81dSBarry Smith    Level: intermediate
1476e35cf81dSBarry Smith 
1477e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1478e35cf81dSBarry Smith 
1479e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian()
1480e35cf81dSBarry Smith 
1481e35cf81dSBarry Smith @*/
1482e35cf81dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagJacobian(SNES snes,PetscInt lag)
1483e35cf81dSBarry Smith {
1484e35cf81dSBarry Smith   PetscFunctionBegin;
1485e35cf81dSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1486fe3ffe1eSBarry Smith   if (lag < -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1487e35cf81dSBarry Smith   if (!lag) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1488e35cf81dSBarry Smith   snes->lagjacobian = lag;
1489e35cf81dSBarry Smith   PetscFunctionReturn(0);
1490e35cf81dSBarry Smith }
1491e35cf81dSBarry Smith 
1492e35cf81dSBarry Smith #undef __FUNCT__
1493e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian"
1494e35cf81dSBarry Smith /*@
1495e35cf81dSBarry Smith    SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt
1496e35cf81dSBarry Smith 
1497e35cf81dSBarry Smith    Collective on SNES
1498e35cf81dSBarry Smith 
1499e35cf81dSBarry Smith    Input Parameter:
1500e35cf81dSBarry Smith .  snes - the SNES context
1501e35cf81dSBarry Smith 
1502e35cf81dSBarry Smith    Output Parameter:
1503e35cf81dSBarry 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
1504e35cf81dSBarry Smith          the Jacobian is built etc.
1505e35cf81dSBarry Smith 
1506e35cf81dSBarry Smith    Options Database Keys:
1507e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1508e35cf81dSBarry Smith 
1509e35cf81dSBarry Smith    Notes:
1510e35cf81dSBarry Smith    The default is 1
1511e35cf81dSBarry Smith    The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1512e35cf81dSBarry Smith 
1513e35cf81dSBarry Smith    Level: intermediate
1514e35cf81dSBarry Smith 
1515e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1516e35cf81dSBarry Smith 
1517e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner()
1518e35cf81dSBarry Smith 
1519e35cf81dSBarry Smith @*/
1520e35cf81dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagJacobian(SNES snes,PetscInt *lag)
1521e35cf81dSBarry Smith {
1522e35cf81dSBarry Smith   PetscFunctionBegin;
1523e35cf81dSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1524e35cf81dSBarry Smith   *lag = snes->lagjacobian;
1525e35cf81dSBarry Smith   PetscFunctionReturn(0);
1526e35cf81dSBarry Smith }
1527e35cf81dSBarry Smith 
1528e35cf81dSBarry Smith #undef __FUNCT__
15294a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
15309b94acceSBarry Smith /*@
1531d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
15329b94acceSBarry Smith 
1533c7afd0dbSLois Curfman McInnes    Collective on SNES
1534c7afd0dbSLois Curfman McInnes 
15359b94acceSBarry Smith    Input Parameters:
1536c7afd0dbSLois Curfman McInnes +  snes - the SNES context
153770441072SBarry Smith .  abstol - absolute convergence tolerance
153833174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
153933174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
154033174efeSLois Curfman McInnes            of the change in the solution between steps
154133174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1542c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1543fee21e36SBarry Smith 
154433174efeSLois Curfman McInnes    Options Database Keys:
154570441072SBarry Smith +    -snes_atol <abstol> - Sets abstol
1546c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1547c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1548c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1549c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
15509b94acceSBarry Smith 
1551d7a720efSLois Curfman McInnes    Notes:
15529b94acceSBarry Smith    The default maximum number of iterations is 50.
15539b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
15549b94acceSBarry Smith 
155536851e7fSLois Curfman McInnes    Level: intermediate
155636851e7fSLois Curfman McInnes 
155733174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
15589b94acceSBarry Smith 
15592492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance()
15609b94acceSBarry Smith @*/
156163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf)
15629b94acceSBarry Smith {
15633a40ed3dSBarry Smith   PetscFunctionBegin;
15644482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
156570441072SBarry Smith   if (abstol != PETSC_DEFAULT)  snes->abstol      = abstol;
1566d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1567d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1568d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1569d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
15703a40ed3dSBarry Smith   PetscFunctionReturn(0);
15719b94acceSBarry Smith }
15729b94acceSBarry Smith 
15734a2ae208SSatish Balay #undef __FUNCT__
15744a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
15759b94acceSBarry Smith /*@
157633174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
157733174efeSLois Curfman McInnes 
1578c7afd0dbSLois Curfman McInnes    Not Collective
1579c7afd0dbSLois Curfman McInnes 
158033174efeSLois Curfman McInnes    Input Parameters:
1581c7afd0dbSLois Curfman McInnes +  snes - the SNES context
158285385478SLisandro Dalcin .  atol - absolute convergence tolerance
158333174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
158433174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
158533174efeSLois Curfman McInnes            of the change in the solution between steps
158633174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1587c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1588fee21e36SBarry Smith 
158933174efeSLois Curfman McInnes    Notes:
159033174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
159133174efeSLois Curfman McInnes 
159236851e7fSLois Curfman McInnes    Level: intermediate
159336851e7fSLois Curfman McInnes 
159433174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
159533174efeSLois Curfman McInnes 
159633174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
159733174efeSLois Curfman McInnes @*/
159885385478SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf)
159933174efeSLois Curfman McInnes {
16003a40ed3dSBarry Smith   PetscFunctionBegin;
16014482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
160285385478SLisandro Dalcin   if (atol)  *atol  = snes->abstol;
160333174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
160433174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
160533174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
160633174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
16073a40ed3dSBarry Smith   PetscFunctionReturn(0);
160833174efeSLois Curfman McInnes }
160933174efeSLois Curfman McInnes 
16104a2ae208SSatish Balay #undef __FUNCT__
16114a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
161233174efeSLois Curfman McInnes /*@
16139b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
16149b94acceSBarry Smith 
1615fee21e36SBarry Smith    Collective on SNES
1616fee21e36SBarry Smith 
1617c7afd0dbSLois Curfman McInnes    Input Parameters:
1618c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1619c7afd0dbSLois Curfman McInnes -  tol - tolerance
1620c7afd0dbSLois Curfman McInnes 
16219b94acceSBarry Smith    Options Database Key:
1622c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
16239b94acceSBarry Smith 
162436851e7fSLois Curfman McInnes    Level: intermediate
162536851e7fSLois Curfman McInnes 
16269b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
16279b94acceSBarry Smith 
16282492ecdbSBarry Smith .seealso: SNESSetTolerances()
16299b94acceSBarry Smith @*/
163063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
16319b94acceSBarry Smith {
16323a40ed3dSBarry Smith   PetscFunctionBegin;
16334482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
16349b94acceSBarry Smith   snes->deltatol = tol;
16353a40ed3dSBarry Smith   PetscFunctionReturn(0);
16369b94acceSBarry Smith }
16379b94acceSBarry Smith 
1638df9fa365SBarry Smith /*
1639df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1640df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1641df9fa365SBarry Smith    macros instead of functions
1642df9fa365SBarry Smith */
16434a2ae208SSatish Balay #undef __FUNCT__
1644a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG"
1645a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx)
1646ce1608b8SBarry Smith {
1647dfbe8321SBarry Smith   PetscErrorCode ierr;
1648ce1608b8SBarry Smith 
1649ce1608b8SBarry Smith   PetscFunctionBegin;
16504482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1651a6570f20SBarry Smith   ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1652ce1608b8SBarry Smith   PetscFunctionReturn(0);
1653ce1608b8SBarry Smith }
1654ce1608b8SBarry Smith 
16554a2ae208SSatish Balay #undef __FUNCT__
1656a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate"
1657a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1658df9fa365SBarry Smith {
1659dfbe8321SBarry Smith   PetscErrorCode ierr;
1660df9fa365SBarry Smith 
1661df9fa365SBarry Smith   PetscFunctionBegin;
1662a6570f20SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1663df9fa365SBarry Smith   PetscFunctionReturn(0);
1664df9fa365SBarry Smith }
1665df9fa365SBarry Smith 
16664a2ae208SSatish Balay #undef __FUNCT__
1667a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy"
1668a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGDestroy(PetscDrawLG draw)
1669df9fa365SBarry Smith {
1670dfbe8321SBarry Smith   PetscErrorCode ierr;
1671df9fa365SBarry Smith 
1672df9fa365SBarry Smith   PetscFunctionBegin;
1673a6570f20SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
1674df9fa365SBarry Smith   PetscFunctionReturn(0);
1675df9fa365SBarry Smith }
1676df9fa365SBarry Smith 
16779b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
16789b94acceSBarry Smith 
16794a2ae208SSatish Balay #undef __FUNCT__
1680a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet"
16819b94acceSBarry Smith /*@C
1682a6570f20SBarry Smith    SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every
16839b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
16849b94acceSBarry Smith    progress.
16859b94acceSBarry Smith 
1686fee21e36SBarry Smith    Collective on SNES
1687fee21e36SBarry Smith 
1688c7afd0dbSLois Curfman McInnes    Input Parameters:
1689c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1690c7afd0dbSLois Curfman McInnes .  func - monitoring routine
1691b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
1692e8105e01SRichard Katz           monitor routine (use PETSC_NULL if no context is desired)
1693b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1694b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
16959b94acceSBarry Smith 
1696c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1697a7cc72afSBarry Smith $     int func(SNES snes,PetscInt its, PetscReal norm,void *mctx)
1698c7afd0dbSLois Curfman McInnes 
1699c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1700c7afd0dbSLois Curfman McInnes .    its - iteration number
1701c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
170240a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
17039b94acceSBarry Smith 
17049665c990SLois Curfman McInnes    Options Database Keys:
1705a6570f20SBarry Smith +    -snes_monitor        - sets SNESMonitorDefault()
1706a6570f20SBarry Smith .    -snes_monitor_draw    - sets line graph monitor,
1707a6570f20SBarry Smith                             uses SNESMonitorLGCreate()
1708a6570f20SBarry Smith _    -snes_monitor_cancel - cancels all monitors that have
1709c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
1710a6570f20SBarry Smith                             calls to SNESMonitorSet(), but
1711c7afd0dbSLois Curfman McInnes                             does not cancel those set via
1712c7afd0dbSLois Curfman McInnes                             the options database.
17139665c990SLois Curfman McInnes 
1714639f9d9dSBarry Smith    Notes:
17156bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
1716a6570f20SBarry Smith    SNESMonitorSet() multiple times; all will be called in the
17176bc08f3fSLois Curfman McInnes    order in which they were set.
1718639f9d9dSBarry Smith 
1719025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each SNES object
1720025f1a04SBarry Smith 
172136851e7fSLois Curfman McInnes    Level: intermediate
172236851e7fSLois Curfman McInnes 
17239b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
17249b94acceSBarry Smith 
1725a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel()
17269b94acceSBarry Smith @*/
1727b90d0a6eSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void*))
17289b94acceSBarry Smith {
1729b90d0a6eSBarry Smith   PetscInt i;
1730b90d0a6eSBarry Smith 
17313a40ed3dSBarry Smith   PetscFunctionBegin;
17324482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1733639f9d9dSBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) {
173429bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1735639f9d9dSBarry Smith   }
1736b90d0a6eSBarry Smith   for (i=0; i<snes->numbermonitors;i++) {
1737b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) PetscFunctionReturn(0);
1738b90d0a6eSBarry Smith 
1739b90d0a6eSBarry Smith     /* check if both default monitors that share common ASCII viewer */
1740b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitor == SNESMonitorDefault) {
1741b90d0a6eSBarry Smith       if (mctx && snes->monitorcontext[i]) {
1742b90d0a6eSBarry Smith         PetscErrorCode          ierr;
1743b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer1 = (PetscViewerASCIIMonitor) mctx;
1744b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer2 = (PetscViewerASCIIMonitor) snes->monitorcontext[i];
1745b90d0a6eSBarry Smith         if (viewer1->viewer == viewer2->viewer) {
1746b90d0a6eSBarry Smith           ierr = (*monitordestroy)(mctx);CHKERRQ(ierr);
1747b90d0a6eSBarry Smith           PetscFunctionReturn(0);
1748b90d0a6eSBarry Smith         }
1749b90d0a6eSBarry Smith       }
1750b90d0a6eSBarry Smith     }
1751b90d0a6eSBarry Smith   }
1752b90d0a6eSBarry Smith   snes->monitor[snes->numbermonitors]           = monitor;
1753b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
1754639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
17553a40ed3dSBarry Smith   PetscFunctionReturn(0);
17569b94acceSBarry Smith }
17579b94acceSBarry Smith 
17584a2ae208SSatish Balay #undef __FUNCT__
1759a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel"
17605cd90555SBarry Smith /*@C
1761a6570f20SBarry Smith    SNESMonitorCancel - Clears all the monitor functions for a SNES object.
17625cd90555SBarry Smith 
1763c7afd0dbSLois Curfman McInnes    Collective on SNES
1764c7afd0dbSLois Curfman McInnes 
17655cd90555SBarry Smith    Input Parameters:
17665cd90555SBarry Smith .  snes - the SNES context
17675cd90555SBarry Smith 
17681a480d89SAdministrator    Options Database Key:
1769a6570f20SBarry Smith .  -snes_monitor_cancel - cancels all monitors that have been hardwired
1770a6570f20SBarry Smith     into a code by calls to SNESMonitorSet(), but does not cancel those
1771c7afd0dbSLois Curfman McInnes     set via the options database
17725cd90555SBarry Smith 
17735cd90555SBarry Smith    Notes:
17745cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
17755cd90555SBarry Smith 
177636851e7fSLois Curfman McInnes    Level: intermediate
177736851e7fSLois Curfman McInnes 
17785cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
17795cd90555SBarry Smith 
1780a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet()
17815cd90555SBarry Smith @*/
1782a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorCancel(SNES snes)
17835cd90555SBarry Smith {
1784d952e501SBarry Smith   PetscErrorCode ierr;
1785d952e501SBarry Smith   PetscInt       i;
1786d952e501SBarry Smith 
17875cd90555SBarry Smith   PetscFunctionBegin;
17884482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1789d952e501SBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
1790d952e501SBarry Smith     if (snes->monitordestroy[i]) {
1791d952e501SBarry Smith       ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr);
1792d952e501SBarry Smith     }
1793d952e501SBarry Smith   }
17945cd90555SBarry Smith   snes->numbermonitors = 0;
17955cd90555SBarry Smith   PetscFunctionReturn(0);
17965cd90555SBarry Smith }
17975cd90555SBarry Smith 
17984a2ae208SSatish Balay #undef __FUNCT__
17994a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
18009b94acceSBarry Smith /*@C
18019b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
18029b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
18039b94acceSBarry Smith 
1804fee21e36SBarry Smith    Collective on SNES
1805fee21e36SBarry Smith 
1806c7afd0dbSLois Curfman McInnes    Input Parameters:
1807c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1808c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
18097f7931b9SBarry Smith .  cctx - [optional] context for private data for the convergence routine  (may be PETSC_NULL)
18107f7931b9SBarry Smith -  destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran)
18119b94acceSBarry Smith 
1812c7afd0dbSLois Curfman McInnes    Calling sequence of func:
181306ee9f85SBarry Smith $     PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
1814c7afd0dbSLois Curfman McInnes 
1815c7afd0dbSLois Curfman McInnes +    snes - the SNES context
181606ee9f85SBarry Smith .    it - current iteration (0 is the first and is before any Newton step)
1817c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
1818184914b5SBarry Smith .    reason - reason for convergence/divergence
1819c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
18204b27c08aSLois Curfman McInnes .    gnorm - 2-norm of current step
18214b27c08aSLois Curfman McInnes -    f - 2-norm of function
18229b94acceSBarry Smith 
182336851e7fSLois Curfman McInnes    Level: advanced
182436851e7fSLois Curfman McInnes 
18259b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
18269b94acceSBarry Smith 
182785385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged()
18289b94acceSBarry Smith @*/
18297f7931b9SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*))
18309b94acceSBarry Smith {
18317f7931b9SBarry Smith   PetscErrorCode ierr;
18327f7931b9SBarry Smith 
18333a40ed3dSBarry Smith   PetscFunctionBegin;
18344482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
183585385478SLisandro Dalcin   if (!func) func = SNESSkipConverged;
18367f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {
18377f7931b9SBarry Smith     ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);
18387f7931b9SBarry Smith   }
183985385478SLisandro Dalcin   snes->ops->converged        = func;
18407f7931b9SBarry Smith   snes->ops->convergeddestroy = destroy;
184185385478SLisandro Dalcin   snes->cnvP                  = cctx;
18423a40ed3dSBarry Smith   PetscFunctionReturn(0);
18439b94acceSBarry Smith }
18449b94acceSBarry Smith 
18454a2ae208SSatish Balay #undef __FUNCT__
18464a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
184752baeb72SSatish Balay /*@
1848184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
1849184914b5SBarry Smith 
1850184914b5SBarry Smith    Not Collective
1851184914b5SBarry Smith 
1852184914b5SBarry Smith    Input Parameter:
1853184914b5SBarry Smith .  snes - the SNES context
1854184914b5SBarry Smith 
1855184914b5SBarry Smith    Output Parameter:
18564d0a8057SBarry Smith .  reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the
1857184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
1858184914b5SBarry Smith 
1859184914b5SBarry Smith    Level: intermediate
1860184914b5SBarry Smith 
1861184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
1862184914b5SBarry Smith 
1863184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
1864184914b5SBarry Smith 
186585385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason
1866184914b5SBarry Smith @*/
186763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
1868184914b5SBarry Smith {
1869184914b5SBarry Smith   PetscFunctionBegin;
18704482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
18714482741eSBarry Smith   PetscValidPointer(reason,2);
1872184914b5SBarry Smith   *reason = snes->reason;
1873184914b5SBarry Smith   PetscFunctionReturn(0);
1874184914b5SBarry Smith }
1875184914b5SBarry Smith 
18764a2ae208SSatish Balay #undef __FUNCT__
18774a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
1878c9005455SLois Curfman McInnes /*@
1879c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
1880c9005455SLois Curfman McInnes 
1881fee21e36SBarry Smith    Collective on SNES
1882fee21e36SBarry Smith 
1883c7afd0dbSLois Curfman McInnes    Input Parameters:
1884c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
1885c7afd0dbSLois Curfman McInnes .  a   - array to hold history
1886cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
1887758f92a0SBarry Smith .  na  - size of a and its
188864731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
1889758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
1890c7afd0dbSLois Curfman McInnes 
1891c9005455SLois Curfman McInnes    Notes:
18924b27c08aSLois Curfman McInnes    If set, this array will contain the function norms computed
1893c9005455SLois Curfman McInnes    at each step.
1894c9005455SLois Curfman McInnes 
1895c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
1896c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
1897c9005455SLois Curfman McInnes    during the section of code that is being timed.
1898c9005455SLois Curfman McInnes 
189936851e7fSLois Curfman McInnes    Level: intermediate
190036851e7fSLois Curfman McInnes 
1901c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
1902758f92a0SBarry Smith 
190308405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
1904758f92a0SBarry Smith 
1905c9005455SLois Curfman McInnes @*/
1906a562a398SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscTruth reset)
1907c9005455SLois Curfman McInnes {
19083a40ed3dSBarry Smith   PetscFunctionBegin;
19094482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
19104482741eSBarry Smith   if (na)  PetscValidScalarPointer(a,2);
1911a562a398SLisandro Dalcin   if (its) PetscValidIntPointer(its,3);
1912c9005455SLois Curfman McInnes   snes->conv_hist       = a;
1913758f92a0SBarry Smith   snes->conv_hist_its   = its;
1914758f92a0SBarry Smith   snes->conv_hist_max   = na;
1915a12bc48fSLisandro Dalcin   snes->conv_hist_len   = 0;
1916758f92a0SBarry Smith   snes->conv_hist_reset = reset;
1917758f92a0SBarry Smith   PetscFunctionReturn(0);
1918758f92a0SBarry Smith }
1919758f92a0SBarry Smith 
19204a2ae208SSatish Balay #undef __FUNCT__
19214a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
19220c4c9dddSBarry Smith /*@C
1923758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
1924758f92a0SBarry Smith 
1925758f92a0SBarry Smith    Collective on SNES
1926758f92a0SBarry Smith 
1927758f92a0SBarry Smith    Input Parameter:
1928758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
1929758f92a0SBarry Smith 
1930758f92a0SBarry Smith    Output Parameters:
1931758f92a0SBarry Smith .  a   - array to hold history
1932758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
1933758f92a0SBarry Smith          negative if not converged) for each solve.
1934758f92a0SBarry Smith -  na  - size of a and its
1935758f92a0SBarry Smith 
1936758f92a0SBarry Smith    Notes:
1937758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
1938758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
1939758f92a0SBarry Smith 
1940758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
1941758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
1942758f92a0SBarry Smith    during the section of code that is being timed.
1943758f92a0SBarry Smith 
1944758f92a0SBarry Smith    Level: intermediate
1945758f92a0SBarry Smith 
1946758f92a0SBarry Smith .keywords: SNES, get, convergence, history
1947758f92a0SBarry Smith 
1948758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
1949758f92a0SBarry Smith 
1950758f92a0SBarry Smith @*/
195163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na)
1952758f92a0SBarry Smith {
1953758f92a0SBarry Smith   PetscFunctionBegin;
19544482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1955758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
1956758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
1957758f92a0SBarry Smith   if (na)  *na  = snes->conv_hist_len;
19583a40ed3dSBarry Smith   PetscFunctionReturn(0);
1959c9005455SLois Curfman McInnes }
1960c9005455SLois Curfman McInnes 
1961e74ef692SMatthew Knepley #undef __FUNCT__
1962e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
1963ac226902SBarry Smith /*@C
196476b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
19657e4bb74cSBarry Smith   at the beginning o every iteration of the nonlinear solve. Specifically
19667e4bb74cSBarry Smith   it is called just before the Jacobian is "evaluated".
196776b2cf59SMatthew Knepley 
196876b2cf59SMatthew Knepley   Collective on SNES
196976b2cf59SMatthew Knepley 
197076b2cf59SMatthew Knepley   Input Parameters:
197176b2cf59SMatthew Knepley . snes - The nonlinear solver context
197276b2cf59SMatthew Knepley . func - The function
197376b2cf59SMatthew Knepley 
197476b2cf59SMatthew Knepley   Calling sequence of func:
1975b5d30489SBarry Smith . func (SNES snes, PetscInt step);
197676b2cf59SMatthew Knepley 
197776b2cf59SMatthew Knepley . step - The current step of the iteration
197876b2cf59SMatthew Knepley 
197976b2cf59SMatthew Knepley   Level: intermediate
198076b2cf59SMatthew Knepley 
198176b2cf59SMatthew Knepley .keywords: SNES, update
1982b5d30489SBarry Smith 
198385385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve()
198476b2cf59SMatthew Knepley @*/
198563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt))
198676b2cf59SMatthew Knepley {
198776b2cf59SMatthew Knepley   PetscFunctionBegin;
19884482741eSBarry Smith   PetscValidHeaderSpecific(snes, SNES_COOKIE,1);
1989e7788613SBarry Smith   snes->ops->update = func;
199076b2cf59SMatthew Knepley   PetscFunctionReturn(0);
199176b2cf59SMatthew Knepley }
199276b2cf59SMatthew Knepley 
1993e74ef692SMatthew Knepley #undef __FUNCT__
1994e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
199576b2cf59SMatthew Knepley /*@
199676b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
199776b2cf59SMatthew Knepley 
199876b2cf59SMatthew Knepley   Not collective
199976b2cf59SMatthew Knepley 
200076b2cf59SMatthew Knepley   Input Parameters:
200176b2cf59SMatthew Knepley . snes - The nonlinear solver context
200276b2cf59SMatthew Knepley . step - The current step of the iteration
200376b2cf59SMatthew Knepley 
2004205452f4SMatthew Knepley   Level: intermediate
2005205452f4SMatthew Knepley 
200676b2cf59SMatthew Knepley .keywords: SNES, update
2007a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC()
200876b2cf59SMatthew Knepley @*/
200963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultUpdate(SNES snes, PetscInt step)
201076b2cf59SMatthew Knepley {
201176b2cf59SMatthew Knepley   PetscFunctionBegin;
201276b2cf59SMatthew Knepley   PetscFunctionReturn(0);
201376b2cf59SMatthew Knepley }
201476b2cf59SMatthew Knepley 
20154a2ae208SSatish Balay #undef __FUNCT__
20164a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
20179b94acceSBarry Smith /*
20189b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
20199b94acceSBarry Smith    positive parameter delta.
20209b94acceSBarry Smith 
20219b94acceSBarry Smith     Input Parameters:
2022c7afd0dbSLois Curfman McInnes +   snes - the SNES context
20239b94acceSBarry Smith .   y - approximate solution of linear system
20249b94acceSBarry Smith .   fnorm - 2-norm of current function
2025c7afd0dbSLois Curfman McInnes -   delta - trust region size
20269b94acceSBarry Smith 
20279b94acceSBarry Smith     Output Parameters:
2028c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
20299b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
20309b94acceSBarry Smith     region, and exceeds zero otherwise.
2031c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
20329b94acceSBarry Smith 
20339b94acceSBarry Smith     Note:
20344b27c08aSLois Curfman McInnes     For non-trust region methods such as SNESLS, the parameter delta
20359b94acceSBarry Smith     is set to be the maximum allowable step size.
20369b94acceSBarry Smith 
20379b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
20389b94acceSBarry Smith */
2039dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
20409b94acceSBarry Smith {
2041064f8208SBarry Smith   PetscReal      nrm;
2042ea709b57SSatish Balay   PetscScalar    cnorm;
2043dfbe8321SBarry Smith   PetscErrorCode ierr;
20443a40ed3dSBarry Smith 
20453a40ed3dSBarry Smith   PetscFunctionBegin;
20464482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
20474482741eSBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE,2);
2048c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,2);
2049184914b5SBarry Smith 
2050064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
2051064f8208SBarry Smith   if (nrm > *delta) {
2052064f8208SBarry Smith      nrm = *delta/nrm;
2053064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
2054064f8208SBarry Smith      cnorm = nrm;
20552dcb1b2aSMatthew Knepley      ierr = VecScale(y,cnorm);CHKERRQ(ierr);
20569b94acceSBarry Smith      *ynorm = *delta;
20579b94acceSBarry Smith   } else {
20589b94acceSBarry Smith      *gpnorm = 0.0;
2059064f8208SBarry Smith      *ynorm = nrm;
20609b94acceSBarry Smith   }
20613a40ed3dSBarry Smith   PetscFunctionReturn(0);
20629b94acceSBarry Smith }
20639b94acceSBarry Smith 
20644a2ae208SSatish Balay #undef __FUNCT__
20654a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
20666ce558aeSBarry Smith /*@C
2067f69a0ea3SMatthew Knepley    SNESSolve - Solves a nonlinear system F(x) = b.
2068f69a0ea3SMatthew Knepley    Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX().
20699b94acceSBarry Smith 
2070c7afd0dbSLois Curfman McInnes    Collective on SNES
2071c7afd0dbSLois Curfman McInnes 
2072b2002411SLois Curfman McInnes    Input Parameters:
2073c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2074f69a0ea3SMatthew Knepley .  b - the constant part of the equation, or PETSC_NULL to use zero.
207585385478SLisandro Dalcin -  x - the solution vector.
20769b94acceSBarry Smith 
2077b2002411SLois Curfman McInnes    Notes:
20788ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
20798ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
20808ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
20818ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
20828ddd3da0SLois Curfman McInnes 
208336851e7fSLois Curfman McInnes    Level: beginner
208436851e7fSLois Curfman McInnes 
20859b94acceSBarry Smith .keywords: SNES, nonlinear, solve
20869b94acceSBarry Smith 
208785385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian()
20889b94acceSBarry Smith @*/
2089f69a0ea3SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSolve(SNES snes,Vec b,Vec x)
20909b94acceSBarry Smith {
2091dfbe8321SBarry Smith   PetscErrorCode ierr;
2092f1af5d2fSBarry Smith   PetscTruth     flg;
2093eabae89aSBarry Smith   char           filename[PETSC_MAX_PATH_LEN];
2094eabae89aSBarry Smith   PetscViewer    viewer;
2095052efed2SBarry Smith 
20963a40ed3dSBarry Smith   PetscFunctionBegin;
20974482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2098f69a0ea3SMatthew Knepley   PetscValidHeaderSpecific(x,VEC_COOKIE,3);
2099f69a0ea3SMatthew Knepley   PetscCheckSameComm(snes,1,x,3);
210085385478SLisandro Dalcin   if (b) PetscValidHeaderSpecific(b,VEC_COOKIE,2);
210185385478SLisandro Dalcin   if (b) PetscCheckSameComm(snes,1,b,2);
210285385478SLisandro Dalcin 
210385385478SLisandro Dalcin   /* set solution vector */
210485385478SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);
210585385478SLisandro Dalcin   if (snes->vec_sol) { ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr); }
210685385478SLisandro Dalcin   snes->vec_sol = x;
210785385478SLisandro Dalcin   /* set afine vector if provided */
210885385478SLisandro Dalcin   if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); }
210985385478SLisandro Dalcin   if (snes->vec_rhs) { ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr); }
211085385478SLisandro Dalcin   snes->vec_rhs = b;
211185385478SLisandro Dalcin 
211285385478SLisandro Dalcin   if (!snes->vec_func && snes->vec_rhs) {
211385385478SLisandro Dalcin     ierr = VecDuplicate(b, &snes->vec_func); CHKERRQ(ierr);
211470e92668SMatthew Knepley   }
21153f149594SLisandro Dalcin 
211670e92668SMatthew Knepley   ierr = SNESSetUp(snes);CHKERRQ(ierr);
21173f149594SLisandro Dalcin 
2118abc0a331SBarry Smith   if (snes->conv_hist_reset) snes->conv_hist_len = 0;
211950ffb88aSMatthew Knepley   snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
2120d5e45103SBarry Smith 
21213f149594SLisandro Dalcin   ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
21224936397dSBarry Smith   ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr);
212385385478SLisandro Dalcin   ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
21244936397dSBarry Smith   if (snes->domainerror){
21254936397dSBarry Smith     snes->reason      = SNES_DIVERGED_FUNCTION_DOMAIN;
21264936397dSBarry Smith     snes->domainerror = PETSC_FALSE;
21274936397dSBarry Smith   }
212885385478SLisandro Dalcin 
21293f149594SLisandro Dalcin   if (!snes->reason) {
21303f149594SLisandro Dalcin     SETERRQ(PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason");
21313f149594SLisandro Dalcin   }
21323f149594SLisandro Dalcin 
21337adad957SLisandro Dalcin   ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
2134eabae89aSBarry Smith   if (flg && !PetscPreLoadingOn) {
21357adad957SLisandro Dalcin     ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr);
2136eabae89aSBarry Smith     ierr = SNESView(snes,viewer);CHKERRQ(ierr);
2137eabae89aSBarry Smith     ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr);
2138eabae89aSBarry Smith   }
2139eabae89aSBarry Smith 
21407adad957SLisandro Dalcin   ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg);CHKERRQ(ierr);
2141da9b6338SBarry Smith   if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); }
21425968eb51SBarry Smith   if (snes->printreason) {
21435968eb51SBarry Smith     if (snes->reason > 0) {
21447adad957SLisandro Dalcin       ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
21455968eb51SBarry Smith     } else {
21467adad957SLisandro Dalcin       ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
21475968eb51SBarry Smith     }
21485968eb51SBarry Smith   }
21495968eb51SBarry Smith 
21503a40ed3dSBarry Smith   PetscFunctionReturn(0);
21519b94acceSBarry Smith }
21529b94acceSBarry Smith 
21539b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
21549b94acceSBarry Smith 
21554a2ae208SSatish Balay #undef __FUNCT__
21564a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
215782bf6240SBarry Smith /*@C
21584b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
21599b94acceSBarry Smith 
2160fee21e36SBarry Smith    Collective on SNES
2161fee21e36SBarry Smith 
2162c7afd0dbSLois Curfman McInnes    Input Parameters:
2163c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2164454a90a3SBarry Smith -  type - a known method
2165c7afd0dbSLois Curfman McInnes 
2166c7afd0dbSLois Curfman McInnes    Options Database Key:
2167454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
2168c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
2169ae12b187SLois Curfman McInnes 
21709b94acceSBarry Smith    Notes:
2171e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
21724b27c08aSLois Curfman McInnes +    SNESLS - Newton's method with line search
2173c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
21744b27c08aSLois Curfman McInnes .    SNESTR - Newton's method with trust region
2175c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
21769b94acceSBarry Smith 
2177ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
2178ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
2179ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
2180ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
2181ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
2182ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
2183ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
2184ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
2185ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
2186b0a32e0cSBarry Smith   appropriate method.
218736851e7fSLois Curfman McInnes 
218836851e7fSLois Curfman McInnes   Level: intermediate
2189a703fe33SLois Curfman McInnes 
2190454a90a3SBarry Smith .keywords: SNES, set, type
2191435da068SBarry Smith 
2192435da068SBarry Smith .seealso: SNESType, SNESCreate()
2193435da068SBarry Smith 
21949b94acceSBarry Smith @*/
2195a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetType(SNES snes,const SNESType type)
21969b94acceSBarry Smith {
2197dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(SNES);
21986831982aSBarry Smith   PetscTruth     match;
21993a40ed3dSBarry Smith 
22003a40ed3dSBarry Smith   PetscFunctionBegin;
22014482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
22024482741eSBarry Smith   PetscValidCharPointer(type,2);
220382bf6240SBarry Smith 
22046831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
22050f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
220692ff6ae8SBarry Smith 
22077adad957SLisandro Dalcin   ierr =  PetscFListFind(SNESList,((PetscObject)snes)->comm,type,(void (**)(void)) &r);CHKERRQ(ierr);
2208958c9bccSBarry Smith   if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type);
220975396ef9SLisandro Dalcin   /* Destroy the previous private SNES context */
221075396ef9SLisandro Dalcin   if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); }
221175396ef9SLisandro Dalcin   /* Reinitialize function pointers in SNESOps structure */
221275396ef9SLisandro Dalcin   snes->ops->setup          = 0;
221375396ef9SLisandro Dalcin   snes->ops->solve          = 0;
221475396ef9SLisandro Dalcin   snes->ops->view           = 0;
221575396ef9SLisandro Dalcin   snes->ops->setfromoptions = 0;
221675396ef9SLisandro Dalcin   snes->ops->destroy        = 0;
221775396ef9SLisandro Dalcin   /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */
221875396ef9SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
22193a40ed3dSBarry Smith   ierr = (*r)(snes);CHKERRQ(ierr);
2220454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
22213a40ed3dSBarry Smith   PetscFunctionReturn(0);
22229b94acceSBarry Smith }
22239b94acceSBarry Smith 
2224a847f771SSatish Balay 
22259b94acceSBarry Smith /* --------------------------------------------------------------------- */
22264a2ae208SSatish Balay #undef __FUNCT__
22274a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
222852baeb72SSatish Balay /*@
22299b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
2230f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
22319b94acceSBarry Smith 
2232fee21e36SBarry Smith    Not Collective
2233fee21e36SBarry Smith 
223436851e7fSLois Curfman McInnes    Level: advanced
223536851e7fSLois Curfman McInnes 
22369b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
22379b94acceSBarry Smith 
22389b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
22399b94acceSBarry Smith @*/
224063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegisterDestroy(void)
22419b94acceSBarry Smith {
2242dfbe8321SBarry Smith   PetscErrorCode ierr;
224382bf6240SBarry Smith 
22443a40ed3dSBarry Smith   PetscFunctionBegin;
22451441b1d3SBarry Smith   ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
22464c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
22473a40ed3dSBarry Smith   PetscFunctionReturn(0);
22489b94acceSBarry Smith }
22499b94acceSBarry Smith 
22504a2ae208SSatish Balay #undef __FUNCT__
22514a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
22529b94acceSBarry Smith /*@C
22539a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
22549b94acceSBarry Smith 
2255c7afd0dbSLois Curfman McInnes    Not Collective
2256c7afd0dbSLois Curfman McInnes 
22579b94acceSBarry Smith    Input Parameter:
22584b0e389bSBarry Smith .  snes - nonlinear solver context
22599b94acceSBarry Smith 
22609b94acceSBarry Smith    Output Parameter:
22613a7fca6bSBarry Smith .  type - SNES method (a character string)
22629b94acceSBarry Smith 
226336851e7fSLois Curfman McInnes    Level: intermediate
226436851e7fSLois Curfman McInnes 
2265454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
22669b94acceSBarry Smith @*/
2267a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetType(SNES snes,const SNESType *type)
22689b94acceSBarry Smith {
22693a40ed3dSBarry Smith   PetscFunctionBegin;
22704482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
22714482741eSBarry Smith   PetscValidPointer(type,2);
22727adad957SLisandro Dalcin   *type = ((PetscObject)snes)->type_name;
22733a40ed3dSBarry Smith   PetscFunctionReturn(0);
22749b94acceSBarry Smith }
22759b94acceSBarry Smith 
22764a2ae208SSatish Balay #undef __FUNCT__
22774a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
227852baeb72SSatish Balay /*@
22799b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
22809b94acceSBarry Smith    stored.
22819b94acceSBarry Smith 
2282c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2283c7afd0dbSLois Curfman McInnes 
22849b94acceSBarry Smith    Input Parameter:
22859b94acceSBarry Smith .  snes - the SNES context
22869b94acceSBarry Smith 
22879b94acceSBarry Smith    Output Parameter:
22889b94acceSBarry Smith .  x - the solution
22899b94acceSBarry Smith 
229070e92668SMatthew Knepley    Level: intermediate
229136851e7fSLois Curfman McInnes 
22929b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
22939b94acceSBarry Smith 
229485385478SLisandro Dalcin .seealso:  SNESGetSolutionUpdate(), SNESGetFunction()
22959b94acceSBarry Smith @*/
229663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolution(SNES snes,Vec *x)
22979b94acceSBarry Smith {
22983a40ed3dSBarry Smith   PetscFunctionBegin;
22994482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
23004482741eSBarry Smith   PetscValidPointer(x,2);
230185385478SLisandro Dalcin   *x = snes->vec_sol;
230270e92668SMatthew Knepley   PetscFunctionReturn(0);
230370e92668SMatthew Knepley }
230470e92668SMatthew Knepley 
230570e92668SMatthew Knepley #undef __FUNCT__
23064a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
230752baeb72SSatish Balay /*@
23089b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
23099b94acceSBarry Smith    stored.
23109b94acceSBarry Smith 
2311c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2312c7afd0dbSLois Curfman McInnes 
23139b94acceSBarry Smith    Input Parameter:
23149b94acceSBarry Smith .  snes - the SNES context
23159b94acceSBarry Smith 
23169b94acceSBarry Smith    Output Parameter:
23179b94acceSBarry Smith .  x - the solution update
23189b94acceSBarry Smith 
231936851e7fSLois Curfman McInnes    Level: advanced
232036851e7fSLois Curfman McInnes 
23219b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
23229b94acceSBarry Smith 
232385385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction()
23249b94acceSBarry Smith @*/
232563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolutionUpdate(SNES snes,Vec *x)
23269b94acceSBarry Smith {
23273a40ed3dSBarry Smith   PetscFunctionBegin;
23284482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
23294482741eSBarry Smith   PetscValidPointer(x,2);
233085385478SLisandro Dalcin   *x = snes->vec_sol_update;
23313a40ed3dSBarry Smith   PetscFunctionReturn(0);
23329b94acceSBarry Smith }
23339b94acceSBarry Smith 
23344a2ae208SSatish Balay #undef __FUNCT__
23354a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
23369b94acceSBarry Smith /*@C
23373638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
23389b94acceSBarry Smith 
2339c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2340c7afd0dbSLois Curfman McInnes 
23419b94acceSBarry Smith    Input Parameter:
23429b94acceSBarry Smith .  snes - the SNES context
23439b94acceSBarry Smith 
23449b94acceSBarry Smith    Output Parameter:
23457bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
234670e92668SMatthew Knepley .  func - the function (or PETSC_NULL)
234770e92668SMatthew Knepley -  ctx - the function context (or PETSC_NULL)
23489b94acceSBarry Smith 
234936851e7fSLois Curfman McInnes    Level: advanced
235036851e7fSLois Curfman McInnes 
2351a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
23529b94acceSBarry Smith 
23534b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution()
23549b94acceSBarry Smith @*/
235570e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx)
23569b94acceSBarry Smith {
23573a40ed3dSBarry Smith   PetscFunctionBegin;
23584482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
235985385478SLisandro Dalcin   if (r)    *r    = snes->vec_func;
2360e7788613SBarry Smith   if (func) *func = snes->ops->computefunction;
236170e92668SMatthew Knepley   if (ctx)  *ctx  = snes->funP;
23623a40ed3dSBarry Smith   PetscFunctionReturn(0);
23639b94acceSBarry Smith }
23649b94acceSBarry Smith 
23654a2ae208SSatish Balay #undef __FUNCT__
23664a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
23673c7409f5SSatish Balay /*@C
23683c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2369d850072dSLois Curfman McInnes    SNES options in the database.
23703c7409f5SSatish Balay 
2371fee21e36SBarry Smith    Collective on SNES
2372fee21e36SBarry Smith 
2373c7afd0dbSLois Curfman McInnes    Input Parameter:
2374c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2375c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2376c7afd0dbSLois Curfman McInnes 
2377d850072dSLois Curfman McInnes    Notes:
2378a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2379c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2380d850072dSLois Curfman McInnes 
238136851e7fSLois Curfman McInnes    Level: advanced
238236851e7fSLois Curfman McInnes 
23833c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2384a86d99e1SLois Curfman McInnes 
2385a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
23863c7409f5SSatish Balay @*/
238763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetOptionsPrefix(SNES snes,const char prefix[])
23883c7409f5SSatish Balay {
2389dfbe8321SBarry Smith   PetscErrorCode ierr;
23903c7409f5SSatish Balay 
23913a40ed3dSBarry Smith   PetscFunctionBegin;
23924482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2393639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
23941cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
239594b7f48cSBarry Smith   ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
23963a40ed3dSBarry Smith   PetscFunctionReturn(0);
23973c7409f5SSatish Balay }
23983c7409f5SSatish Balay 
23994a2ae208SSatish Balay #undef __FUNCT__
24004a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
24013c7409f5SSatish Balay /*@C
2402f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2403d850072dSLois Curfman McInnes    SNES options in the database.
24043c7409f5SSatish Balay 
2405fee21e36SBarry Smith    Collective on SNES
2406fee21e36SBarry Smith 
2407c7afd0dbSLois Curfman McInnes    Input Parameters:
2408c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2409c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2410c7afd0dbSLois Curfman McInnes 
2411d850072dSLois Curfman McInnes    Notes:
2412a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2413c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2414d850072dSLois Curfman McInnes 
241536851e7fSLois Curfman McInnes    Level: advanced
241636851e7fSLois Curfman McInnes 
24173c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2418a86d99e1SLois Curfman McInnes 
2419a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
24203c7409f5SSatish Balay @*/
242163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAppendOptionsPrefix(SNES snes,const char prefix[])
24223c7409f5SSatish Balay {
2423dfbe8321SBarry Smith   PetscErrorCode ierr;
24243c7409f5SSatish Balay 
24253a40ed3dSBarry Smith   PetscFunctionBegin;
24264482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2427639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
24281cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
242994b7f48cSBarry Smith   ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
24303a40ed3dSBarry Smith   PetscFunctionReturn(0);
24313c7409f5SSatish Balay }
24323c7409f5SSatish Balay 
24334a2ae208SSatish Balay #undef __FUNCT__
24344a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
24359ab63eb5SSatish Balay /*@C
24363c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
24373c7409f5SSatish Balay    SNES options in the database.
24383c7409f5SSatish Balay 
2439c7afd0dbSLois Curfman McInnes    Not Collective
2440c7afd0dbSLois Curfman McInnes 
24413c7409f5SSatish Balay    Input Parameter:
24423c7409f5SSatish Balay .  snes - the SNES context
24433c7409f5SSatish Balay 
24443c7409f5SSatish Balay    Output Parameter:
24453c7409f5SSatish Balay .  prefix - pointer to the prefix string used
24463c7409f5SSatish Balay 
24474ef407dbSRichard Tran Mills    Notes: On the fortran side, the user should pass in a string 'prefix' of
24489ab63eb5SSatish Balay    sufficient length to hold the prefix.
24499ab63eb5SSatish Balay 
245036851e7fSLois Curfman McInnes    Level: advanced
245136851e7fSLois Curfman McInnes 
24523c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2453a86d99e1SLois Curfman McInnes 
2454a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
24553c7409f5SSatish Balay @*/
2456e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetOptionsPrefix(SNES snes,const char *prefix[])
24573c7409f5SSatish Balay {
2458dfbe8321SBarry Smith   PetscErrorCode ierr;
24593c7409f5SSatish Balay 
24603a40ed3dSBarry Smith   PetscFunctionBegin;
24614482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2462639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
24633a40ed3dSBarry Smith   PetscFunctionReturn(0);
24643c7409f5SSatish Balay }
24653c7409f5SSatish Balay 
2466b2002411SLois Curfman McInnes 
24674a2ae208SSatish Balay #undef __FUNCT__
24684a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
24693cea93caSBarry Smith /*@C
24703cea93caSBarry Smith   SNESRegister - See SNESRegisterDynamic()
24713cea93caSBarry Smith 
24727f6c08e0SMatthew Knepley   Level: advanced
24733cea93caSBarry Smith @*/
247463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES))
2475b2002411SLois Curfman McInnes {
2476e2d1d2b7SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
2477dfbe8321SBarry Smith   PetscErrorCode ierr;
2478b2002411SLois Curfman McInnes 
2479b2002411SLois Curfman McInnes   PetscFunctionBegin;
2480b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2481c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
2482b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2483b2002411SLois Curfman McInnes }
2484da9b6338SBarry Smith 
2485da9b6338SBarry Smith #undef __FUNCT__
2486da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin"
248763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESTestLocalMin(SNES snes)
2488da9b6338SBarry Smith {
2489dfbe8321SBarry Smith   PetscErrorCode ierr;
249077431f27SBarry Smith   PetscInt       N,i,j;
2491da9b6338SBarry Smith   Vec            u,uh,fh;
2492da9b6338SBarry Smith   PetscScalar    value;
2493da9b6338SBarry Smith   PetscReal      norm;
2494da9b6338SBarry Smith 
2495da9b6338SBarry Smith   PetscFunctionBegin;
2496da9b6338SBarry Smith   ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr);
2497da9b6338SBarry Smith   ierr = VecDuplicate(u,&uh);CHKERRQ(ierr);
2498da9b6338SBarry Smith   ierr = VecDuplicate(u,&fh);CHKERRQ(ierr);
2499da9b6338SBarry Smith 
2500da9b6338SBarry Smith   /* currently only works for sequential */
2501da9b6338SBarry Smith   ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n");
2502da9b6338SBarry Smith   ierr = VecGetSize(u,&N);CHKERRQ(ierr);
2503da9b6338SBarry Smith   for (i=0; i<N; i++) {
2504da9b6338SBarry Smith     ierr = VecCopy(u,uh);CHKERRQ(ierr);
250577431f27SBarry Smith     ierr  = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr);
2506da9b6338SBarry Smith     for (j=-10; j<11; j++) {
2507ccae9161SBarry Smith       value = PetscSign(j)*exp(PetscAbs(j)-10.0);
2508da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
25093ab0aad5SBarry Smith       ierr  = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr);
2510da9b6338SBarry Smith       ierr  = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr);
251177431f27SBarry Smith       ierr  = PetscPrintf(PETSC_COMM_WORLD,"       j norm %D %18.16e\n",j,norm);CHKERRQ(ierr);
2512da9b6338SBarry Smith       value = -value;
2513da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
2514da9b6338SBarry Smith     }
2515da9b6338SBarry Smith   }
2516da9b6338SBarry Smith   ierr = VecDestroy(uh);CHKERRQ(ierr);
2517da9b6338SBarry Smith   ierr = VecDestroy(fh);CHKERRQ(ierr);
2518da9b6338SBarry Smith   PetscFunctionReturn(0);
2519da9b6338SBarry Smith }
252071f87433Sdalcinl 
252171f87433Sdalcinl #undef __FUNCT__
2522fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW"
252371f87433Sdalcinl /*@
2524fa9f3622SBarry Smith    SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for
252571f87433Sdalcinl    computing relative tolerance for linear solvers within an inexact
252671f87433Sdalcinl    Newton method.
252771f87433Sdalcinl 
252871f87433Sdalcinl    Collective on SNES
252971f87433Sdalcinl 
253071f87433Sdalcinl    Input Parameters:
253171f87433Sdalcinl +  snes - SNES context
253271f87433Sdalcinl -  flag - PETSC_TRUE or PETSC_FALSE
253371f87433Sdalcinl 
253471f87433Sdalcinl    Notes:
253571f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
253671f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
253771f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
253871f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
253971f87433Sdalcinl    solver.
254071f87433Sdalcinl 
254171f87433Sdalcinl    Level: advanced
254271f87433Sdalcinl 
254371f87433Sdalcinl    Reference:
254471f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
254571f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
254671f87433Sdalcinl 
254771f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
254871f87433Sdalcinl 
2549fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
255071f87433Sdalcinl @*/
2551fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetUseEW(SNES snes,PetscTruth flag)
255271f87433Sdalcinl {
255371f87433Sdalcinl   PetscFunctionBegin;
255471f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
255571f87433Sdalcinl   snes->ksp_ewconv = flag;
255671f87433Sdalcinl   PetscFunctionReturn(0);
255771f87433Sdalcinl }
255871f87433Sdalcinl 
255971f87433Sdalcinl #undef __FUNCT__
2560fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW"
256171f87433Sdalcinl /*@
2562fa9f3622SBarry Smith    SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method
256371f87433Sdalcinl    for computing relative tolerance for linear solvers within an
256471f87433Sdalcinl    inexact Newton method.
256571f87433Sdalcinl 
256671f87433Sdalcinl    Not Collective
256771f87433Sdalcinl 
256871f87433Sdalcinl    Input Parameter:
256971f87433Sdalcinl .  snes - SNES context
257071f87433Sdalcinl 
257171f87433Sdalcinl    Output Parameter:
257271f87433Sdalcinl .  flag - PETSC_TRUE or PETSC_FALSE
257371f87433Sdalcinl 
257471f87433Sdalcinl    Notes:
257571f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
257671f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
257771f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
257871f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
257971f87433Sdalcinl    solver.
258071f87433Sdalcinl 
258171f87433Sdalcinl    Level: advanced
258271f87433Sdalcinl 
258371f87433Sdalcinl    Reference:
258471f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
258571f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
258671f87433Sdalcinl 
258771f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
258871f87433Sdalcinl 
2589fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
259071f87433Sdalcinl @*/
2591fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetUseEW(SNES snes, PetscTruth *flag)
259271f87433Sdalcinl {
259371f87433Sdalcinl   PetscFunctionBegin;
259471f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
259571f87433Sdalcinl   PetscValidPointer(flag,2);
259671f87433Sdalcinl   *flag = snes->ksp_ewconv;
259771f87433Sdalcinl   PetscFunctionReturn(0);
259871f87433Sdalcinl }
259971f87433Sdalcinl 
260071f87433Sdalcinl #undef __FUNCT__
2601fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW"
260271f87433Sdalcinl /*@
2603fa9f3622SBarry Smith    SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker
260471f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
260571f87433Sdalcinl    Newton method.
260671f87433Sdalcinl 
260771f87433Sdalcinl    Collective on SNES
260871f87433Sdalcinl 
260971f87433Sdalcinl    Input Parameters:
261071f87433Sdalcinl +    snes - SNES context
261171f87433Sdalcinl .    version - version 1, 2 (default is 2) or 3
261271f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
261371f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
261471f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
261571f87433Sdalcinl              (0 <= gamma2 <= 1)
261671f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
261771f87433Sdalcinl .    alpha2 - power for safeguard
261871f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
261971f87433Sdalcinl 
262071f87433Sdalcinl    Note:
262171f87433Sdalcinl    Version 3 was contributed by Luis Chacon, June 2006.
262271f87433Sdalcinl 
262371f87433Sdalcinl    Use PETSC_DEFAULT to retain the default for any of the parameters.
262471f87433Sdalcinl 
262571f87433Sdalcinl    Level: advanced
262671f87433Sdalcinl 
262771f87433Sdalcinl    Reference:
262871f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
262971f87433Sdalcinl    inexact Newton method", Utah State University Math. Stat. Dept. Res.
263071f87433Sdalcinl    Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput.
263171f87433Sdalcinl 
263271f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters
263371f87433Sdalcinl 
2634fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW()
263571f87433Sdalcinl @*/
2636fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max,
263771f87433Sdalcinl 							    PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold)
263871f87433Sdalcinl {
2639fa9f3622SBarry Smith   SNESKSPEW *kctx;
264071f87433Sdalcinl   PetscFunctionBegin;
264171f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2642fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
264371f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
264471f87433Sdalcinl 
264571f87433Sdalcinl   if (version != PETSC_DEFAULT)   kctx->version   = version;
264671f87433Sdalcinl   if (rtol_0 != PETSC_DEFAULT)    kctx->rtol_0    = rtol_0;
264771f87433Sdalcinl   if (rtol_max != PETSC_DEFAULT)  kctx->rtol_max  = rtol_max;
264871f87433Sdalcinl   if (gamma != PETSC_DEFAULT)     kctx->gamma     = gamma;
264971f87433Sdalcinl   if (alpha != PETSC_DEFAULT)     kctx->alpha     = alpha;
265071f87433Sdalcinl   if (alpha2 != PETSC_DEFAULT)    kctx->alpha2    = alpha2;
265171f87433Sdalcinl   if (threshold != PETSC_DEFAULT) kctx->threshold = threshold;
265271f87433Sdalcinl 
265371f87433Sdalcinl   if (kctx->version < 1 || kctx->version > 3) {
265471f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version);
265571f87433Sdalcinl   }
265671f87433Sdalcinl   if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) {
265771f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0);
265871f87433Sdalcinl   }
265971f87433Sdalcinl   if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) {
266071f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max);
266171f87433Sdalcinl   }
266271f87433Sdalcinl   if (kctx->gamma < 0.0 || kctx->gamma > 1.0) {
266371f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma);
266471f87433Sdalcinl   }
266571f87433Sdalcinl   if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) {
266671f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha);
266771f87433Sdalcinl   }
266871f87433Sdalcinl   if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) {
266971f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold);
267071f87433Sdalcinl   }
267171f87433Sdalcinl   PetscFunctionReturn(0);
267271f87433Sdalcinl }
267371f87433Sdalcinl 
267471f87433Sdalcinl #undef __FUNCT__
2675fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW"
267671f87433Sdalcinl /*@
2677fa9f3622SBarry Smith    SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker
267871f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
267971f87433Sdalcinl    Newton method.
268071f87433Sdalcinl 
268171f87433Sdalcinl    Not Collective
268271f87433Sdalcinl 
268371f87433Sdalcinl    Input Parameters:
268471f87433Sdalcinl      snes - SNES context
268571f87433Sdalcinl 
268671f87433Sdalcinl    Output Parameters:
268771f87433Sdalcinl +    version - version 1, 2 (default is 2) or 3
268871f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
268971f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
269071f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
269171f87433Sdalcinl              (0 <= gamma2 <= 1)
269271f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
269371f87433Sdalcinl .    alpha2 - power for safeguard
269471f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
269571f87433Sdalcinl 
269671f87433Sdalcinl    Level: advanced
269771f87433Sdalcinl 
269871f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters
269971f87433Sdalcinl 
2700fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW()
270171f87433Sdalcinl @*/
2702fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max,
270371f87433Sdalcinl 							    PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold)
270471f87433Sdalcinl {
2705fa9f3622SBarry Smith   SNESKSPEW *kctx;
270671f87433Sdalcinl   PetscFunctionBegin;
270771f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2708fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
270971f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
271071f87433Sdalcinl   if(version)   *version   = kctx->version;
271171f87433Sdalcinl   if(rtol_0)    *rtol_0    = kctx->rtol_0;
271271f87433Sdalcinl   if(rtol_max)  *rtol_max  = kctx->rtol_max;
271371f87433Sdalcinl   if(gamma)     *gamma     = kctx->gamma;
271471f87433Sdalcinl   if(alpha)     *alpha     = kctx->alpha;
271571f87433Sdalcinl   if(alpha2)    *alpha2    = kctx->alpha2;
271671f87433Sdalcinl   if(threshold) *threshold = kctx->threshold;
271771f87433Sdalcinl   PetscFunctionReturn(0);
271871f87433Sdalcinl }
271971f87433Sdalcinl 
272071f87433Sdalcinl #undef __FUNCT__
2721fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve"
2722fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x)
272371f87433Sdalcinl {
272471f87433Sdalcinl   PetscErrorCode ierr;
2725fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
272671f87433Sdalcinl   PetscReal      rtol=PETSC_DEFAULT,stol;
272771f87433Sdalcinl 
272871f87433Sdalcinl   PetscFunctionBegin;
272971f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
273071f87433Sdalcinl   if (!snes->iter) { /* first time in, so use the original user rtol */
273171f87433Sdalcinl     rtol = kctx->rtol_0;
273271f87433Sdalcinl   } else {
273371f87433Sdalcinl     if (kctx->version == 1) {
273471f87433Sdalcinl       rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last;
273571f87433Sdalcinl       if (rtol < 0.0) rtol = -rtol;
273671f87433Sdalcinl       stol = pow(kctx->rtol_last,kctx->alpha2);
273771f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
273871f87433Sdalcinl     } else if (kctx->version == 2) {
273971f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
274071f87433Sdalcinl       stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha);
274171f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
274271f87433Sdalcinl     } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */
274371f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
274471f87433Sdalcinl       /* safeguard: avoid sharp decrease of rtol */
274571f87433Sdalcinl       stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha);
274671f87433Sdalcinl       stol = PetscMax(rtol,stol);
274771f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
274871f87433Sdalcinl       /* safeguard: avoid oversolving */
274971f87433Sdalcinl       stol = kctx->gamma*(snes->ttol)/snes->norm;
275071f87433Sdalcinl       stol = PetscMax(rtol,stol);
275171f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
275271f87433Sdalcinl     } else SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version);
275371f87433Sdalcinl   }
275471f87433Sdalcinl   /* safeguard: avoid rtol greater than one */
275571f87433Sdalcinl   rtol = PetscMin(rtol,kctx->rtol_max);
275671f87433Sdalcinl   ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
275771f87433Sdalcinl   ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr);
275871f87433Sdalcinl   PetscFunctionReturn(0);
275971f87433Sdalcinl }
276071f87433Sdalcinl 
276171f87433Sdalcinl #undef __FUNCT__
2762fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve"
2763fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x)
276471f87433Sdalcinl {
276571f87433Sdalcinl   PetscErrorCode ierr;
2766fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
276771f87433Sdalcinl   PCSide         pcside;
276871f87433Sdalcinl   Vec            lres;
276971f87433Sdalcinl 
277071f87433Sdalcinl   PetscFunctionBegin;
277171f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
277271f87433Sdalcinl   ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr);
277371f87433Sdalcinl   ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr);
277471f87433Sdalcinl   if (kctx->version == 1) {
277571f87433Sdalcinl     ierr = KSPGetPreconditionerSide(ksp,&pcside);CHKERRQ(ierr);
277671f87433Sdalcinl     if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */
277771f87433Sdalcinl       /* KSP residual is true linear residual */
277871f87433Sdalcinl       ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr);
277971f87433Sdalcinl     } else {
278071f87433Sdalcinl       /* KSP residual is preconditioned residual */
278171f87433Sdalcinl       /* compute true linear residual norm */
278271f87433Sdalcinl       ierr = VecDuplicate(b,&lres);CHKERRQ(ierr);
278371f87433Sdalcinl       ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr);
278471f87433Sdalcinl       ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr);
278571f87433Sdalcinl       ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr);
278671f87433Sdalcinl       ierr = VecDestroy(lres);CHKERRQ(ierr);
278771f87433Sdalcinl     }
278871f87433Sdalcinl   }
278971f87433Sdalcinl   PetscFunctionReturn(0);
279071f87433Sdalcinl }
279171f87433Sdalcinl 
279271f87433Sdalcinl #undef __FUNCT__
279371f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve"
279471f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x)
279571f87433Sdalcinl {
279671f87433Sdalcinl   PetscErrorCode ierr;
279771f87433Sdalcinl 
279871f87433Sdalcinl   PetscFunctionBegin;
2799fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr);  }
280071f87433Sdalcinl   ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);
2801fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); }
280271f87433Sdalcinl   PetscFunctionReturn(0);
280371f87433Sdalcinl }
2804