xref: /petsc/src/snes/interface/snes.c (revision fe3ffe1e96ff7dfde75f366dcb8ec9400c9132ed)
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;
10663a40ed3dSBarry Smith 
10673a40ed3dSBarry Smith   PetscFunctionBegin;
10684482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
10694482741eSBarry Smith   PetscValidHeaderSpecific(X,VEC_COOKIE,2);
10704482741eSBarry Smith   PetscValidPointer(flg,5);
1071c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,X,2);
1072e7788613SBarry Smith   if (!snes->ops->computejacobian) PetscFunctionReturn(0);
1073*fe3ffe1eSBarry Smith   if (snes->lagjacobian == -2) {
1074*fe3ffe1eSBarry Smith     snes->lagjacobian = -1;
1075*fe3ffe1eSBarry Smith     ierr = PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n");CHKERRQ(ierr);
1076*fe3ffe1eSBarry Smith   } else if (snes->lagjacobian == -1) {
1077e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1078e35cf81dSBarry Smith     ierr = PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");CHKERRQ(ierr);
1079e35cf81dSBarry Smith     PetscFunctionReturn(0);
1080e35cf81dSBarry Smith   } else if (snes->lagjacobian > 1 && snes->iter % snes->lagjacobian) {
1081e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1082e35cf81dSBarry Smith     ierr = PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);CHKERRQ(ierr);
1083e35cf81dSBarry Smith     PetscFunctionReturn(0);
1084e35cf81dSBarry Smith   }
1085e35cf81dSBarry Smith 
1086c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1087e35cf81dSBarry Smith   ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1088d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
1089dc67937bSBarry Smith   CHKMEMQ;
1090e7788613SBarry Smith   ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
1091dc67937bSBarry Smith   CHKMEMQ;
1092d64ed03dSBarry Smith   PetscStackPop;
1093d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1094a8054027SBarry Smith 
1095a8054027SBarry Smith   if (snes->lagpreconditioner == -1) {
1096a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1097a8054027SBarry Smith     ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr);
1098a8054027SBarry Smith   } else if (snes->lagpreconditioner > 1 && snes->iter % snes->lagpreconditioner) {
1099a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1100a8054027SBarry Smith     ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr);
1101a8054027SBarry Smith   }
1102a8054027SBarry Smith 
11036d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
11046ce558aeSBarry Smith   /* PetscValidHeaderSpecific(*A,MAT_COOKIE,3);
11056ce558aeSBarry Smith     PetscValidHeaderSpecific(*B,MAT_COOKIE,4);   */
11063a40ed3dSBarry Smith   PetscFunctionReturn(0);
11079b94acceSBarry Smith }
11089b94acceSBarry Smith 
11094a2ae208SSatish Balay #undef __FUNCT__
11104a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian"
11119b94acceSBarry Smith /*@C
11129b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1113044dda88SLois Curfman McInnes    location to store the matrix.
11149b94acceSBarry Smith 
1115c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1116c7afd0dbSLois Curfman McInnes 
11179b94acceSBarry Smith    Input Parameters:
1118c7afd0dbSLois Curfman McInnes +  snes - the SNES context
11199b94acceSBarry Smith .  A - Jacobian matrix
11209b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
11219b94acceSBarry Smith .  func - Jacobian evaluation routine
1122c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
11232cd2dfdcSLois Curfman McInnes          Jacobian evaluation routine (may be PETSC_NULL)
11249b94acceSBarry Smith 
11259b94acceSBarry Smith    Calling sequence of func:
11268d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
11279b94acceSBarry Smith 
1128c7afd0dbSLois Curfman McInnes +  x - input vector
11299b94acceSBarry Smith .  A - Jacobian matrix
11309b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1131ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
11322b668275SBarry Smith    structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER
1133c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
11349b94acceSBarry Smith 
11359b94acceSBarry Smith    Notes:
113694b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
11372cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1138ac21db08SLois Curfman McInnes 
1139ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
11409b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
11419b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
11429b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
11439b94acceSBarry Smith    throughout the global iterations.
11449b94acceSBarry Smith 
114516913363SBarry Smith    If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on
114616913363SBarry Smith    each matrix.
114716913363SBarry Smith 
114836851e7fSLois Curfman McInnes    Level: beginner
114936851e7fSLois Curfman McInnes 
11509b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
11519b94acceSBarry Smith 
11523ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure
11539b94acceSBarry Smith @*/
115463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
11559b94acceSBarry Smith {
1156dfbe8321SBarry Smith   PetscErrorCode ierr;
11573a7fca6bSBarry Smith 
11583a40ed3dSBarry Smith   PetscFunctionBegin;
11594482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
11604482741eSBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_COOKIE,2);
11614482741eSBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_COOKIE,3);
1162c9780b6fSBarry Smith   if (A) PetscCheckSameComm(snes,1,A,2);
1163c9780b6fSBarry Smith   if (B) PetscCheckSameComm(snes,1,B,2);
1164e7788613SBarry Smith   if (func) snes->ops->computejacobian = func;
11653a7fca6bSBarry Smith   if (ctx)  snes->jacP                 = ctx;
11663a7fca6bSBarry Smith   if (A) {
11677dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
11683a7fca6bSBarry Smith     if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
11699b94acceSBarry Smith     snes->jacobian = A;
11703a7fca6bSBarry Smith   }
11713a7fca6bSBarry Smith   if (B) {
11727dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
11733a7fca6bSBarry Smith     if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
11749b94acceSBarry Smith     snes->jacobian_pre = B;
11753a7fca6bSBarry Smith   }
11763a40ed3dSBarry Smith   PetscFunctionReturn(0);
11779b94acceSBarry Smith }
117862fef451SLois Curfman McInnes 
11794a2ae208SSatish Balay #undef __FUNCT__
11804a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian"
1181c2aafc4cSSatish Balay /*@C
1182b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1183b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1184b4fd4287SBarry Smith 
1185c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1186c7afd0dbSLois Curfman McInnes 
1187b4fd4287SBarry Smith    Input Parameter:
1188b4fd4287SBarry Smith .  snes - the nonlinear solver context
1189b4fd4287SBarry Smith 
1190b4fd4287SBarry Smith    Output Parameters:
1191c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1192b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
119370e92668SMatthew Knepley .  func - location to put Jacobian function (or PETSC_NULL)
119470e92668SMatthew Knepley -  ctx - location to stash Jacobian ctx (or PETSC_NULL)
1195fee21e36SBarry Smith 
119636851e7fSLois Curfman McInnes    Level: advanced
119736851e7fSLois Curfman McInnes 
1198b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1199b4fd4287SBarry Smith @*/
120070e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx)
1201b4fd4287SBarry Smith {
12023a40ed3dSBarry Smith   PetscFunctionBegin;
12034482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1204b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
1205b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
1206e7788613SBarry Smith   if (func) *func = snes->ops->computejacobian;
120770e92668SMatthew Knepley   if (ctx)  *ctx  = snes->jacP;
12083a40ed3dSBarry Smith   PetscFunctionReturn(0);
1209b4fd4287SBarry Smith }
1210b4fd4287SBarry Smith 
12119b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
121263dd3a1aSKris Buschelman EXTERN PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*);
12139b94acceSBarry Smith 
12144a2ae208SSatish Balay #undef __FUNCT__
12154a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp"
12169b94acceSBarry Smith /*@
12179b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1218272ac6f2SLois Curfman McInnes    of a nonlinear solver.
12199b94acceSBarry Smith 
1220fee21e36SBarry Smith    Collective on SNES
1221fee21e36SBarry Smith 
1222c7afd0dbSLois Curfman McInnes    Input Parameters:
122370e92668SMatthew Knepley .  snes - the SNES context
1224c7afd0dbSLois Curfman McInnes 
1225272ac6f2SLois Curfman McInnes    Notes:
1226272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1227272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1228272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1229272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1230272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1231272ac6f2SLois Curfman McInnes 
123236851e7fSLois Curfman McInnes    Level: advanced
123336851e7fSLois Curfman McInnes 
12349b94acceSBarry Smith .keywords: SNES, nonlinear, setup
12359b94acceSBarry Smith 
12369b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
12379b94acceSBarry Smith @*/
123870e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUp(SNES snes)
12399b94acceSBarry Smith {
1240dfbe8321SBarry Smith   PetscErrorCode ierr;
124171f87433Sdalcinl   PetscTruth     flg;
12423a40ed3dSBarry Smith 
12433a40ed3dSBarry Smith   PetscFunctionBegin;
12444482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
12454dc4c822SBarry Smith   if (snes->setupcalled) PetscFunctionReturn(0);
12469b94acceSBarry Smith 
12477adad957SLisandro Dalcin   if (!((PetscObject)snes)->type_name) {
124885385478SLisandro Dalcin     ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr);
124985385478SLisandro Dalcin   }
125085385478SLisandro Dalcin 
12517adad957SLisandro Dalcin   ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf_operator",&flg);CHKERRQ(ierr);
1252c1f60f51SBarry Smith   /*
1253c1f60f51SBarry Smith       This version replaces the user provided Jacobian matrix with a
1254dfa02198SLois Curfman McInnes       matrix-free version but still employs the user-provided preconditioner matrix
1255c1f60f51SBarry Smith   */
1256c1f60f51SBarry Smith   if (flg) {
1257c1f60f51SBarry Smith     Mat J;
1258fef1beadSBarry Smith     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
12593ec795f1SBarry Smith     ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr);
1260ae15b995SBarry Smith     ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr);
12613a7fca6bSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
12623a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
1263c1f60f51SBarry Smith   }
126445fc7adcSBarry Smith 
126503c60df9SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_MAT_SINGLE) && !defined(PETSC_USE_LONG_DOUBLE) && !defined(PETSC_USE_INT)
12667adad957SLisandro Dalcin   ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf_operator2",&flg);CHKERRQ(ierr);
126745fc7adcSBarry Smith   if (flg) {
126845fc7adcSBarry Smith     Mat J;
126945fc7adcSBarry Smith     ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_sol,&J);CHKERRQ(ierr);
127075396ef9SLisandro Dalcin     ierr = PetscInfo(snes,"Setting default matrix-free operator routines (version 2)\n");CHKERRQ(ierr);
127145fc7adcSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
127245fc7adcSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
127345fc7adcSBarry Smith   }
127432a4b47aSMatthew Knepley #endif
127545fc7adcSBarry Smith 
12767adad957SLisandro Dalcin   ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_mf",&flg);CHKERRQ(ierr);
1277c1f60f51SBarry Smith   /*
1278dfa02198SLois Curfman McInnes       This version replaces both the user-provided Jacobian and the user-
1279c1f60f51SBarry Smith       provided preconditioner matrix with the default matrix free version.
1280c1f60f51SBarry Smith    */
128131615d04SLois Curfman McInnes   if (flg) {
1282272ac6f2SLois Curfman McInnes     Mat  J;
1283b5d62d44SBarry Smith     KSP ksp;
128494b7f48cSBarry Smith     PC   pc;
128575396ef9SLisandro Dalcin     /* create and set matrix-free operator */
1286fef1beadSBarry Smith     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
12873ec795f1SBarry Smith     ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr);
128875396ef9SLisandro Dalcin     ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr);
12893ec795f1SBarry Smith     ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr);
12903a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
1291f3ef73ceSBarry Smith     /* force no preconditioner */
129294b7f48cSBarry Smith     ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
1293b5d62d44SBarry Smith     ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
1294a9815358SBarry Smith     ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&flg);CHKERRQ(ierr);
1295a9815358SBarry Smith     if (!flg) {
129675396ef9SLisandro Dalcin       ierr = PetscInfo(snes,"Setting default matrix-free preconditioner routines;\nThat is no preconditioner is being used\n");CHKERRQ(ierr);
1297f3ef73ceSBarry Smith       ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
1298272ac6f2SLois Curfman McInnes     }
1299a9815358SBarry Smith   }
1300f3ef73ceSBarry Smith 
130185385478SLisandro Dalcin   if (!snes->vec_func && !snes->vec_rhs) {
13021096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
13031096aae1SMatthew Knepley   }
130485385478SLisandro Dalcin   if (!snes->ops->computefunction && !snes->vec_rhs) {
13051096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
13061096aae1SMatthew Knepley   }
130775396ef9SLisandro Dalcin   if (!snes->jacobian) {
130875396ef9SLisandro Dalcin     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetJacobian() first \n or use -snes_mf option");
130975396ef9SLisandro Dalcin   }
1310a8c6a408SBarry Smith   if (snes->vec_func == snes->vec_sol) {
131129bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
1312a8c6a408SBarry Smith   }
1313a703fe33SLois Curfman McInnes 
1314410397dcSLisandro Dalcin   if (snes->ops->setup) {
1315410397dcSLisandro Dalcin     ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr);
1316410397dcSLisandro Dalcin   }
13177aaed0d8SBarry Smith   snes->setupcalled = PETSC_TRUE;
13183a40ed3dSBarry Smith   PetscFunctionReturn(0);
13199b94acceSBarry Smith }
13209b94acceSBarry Smith 
13214a2ae208SSatish Balay #undef __FUNCT__
13224a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
132352baeb72SSatish Balay /*@
13249b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
13259b94acceSBarry Smith    with SNESCreate().
13269b94acceSBarry Smith 
1327c7afd0dbSLois Curfman McInnes    Collective on SNES
1328c7afd0dbSLois Curfman McInnes 
13299b94acceSBarry Smith    Input Parameter:
13309b94acceSBarry Smith .  snes - the SNES context
13319b94acceSBarry Smith 
133236851e7fSLois Curfman McInnes    Level: beginner
133336851e7fSLois Curfman McInnes 
13349b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
13359b94acceSBarry Smith 
133663a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
13379b94acceSBarry Smith @*/
133863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDestroy(SNES snes)
13399b94acceSBarry Smith {
13406849ba73SBarry Smith   PetscErrorCode ierr;
13413a40ed3dSBarry Smith 
13423a40ed3dSBarry Smith   PetscFunctionBegin;
13434482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
13447adad957SLisandro Dalcin   if (--((PetscObject)snes)->refct > 0) PetscFunctionReturn(0);
1345d4bb536fSBarry Smith 
1346be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
13470f5bd95cSBarry Smith   ierr = PetscObjectDepublish(snes);CHKERRQ(ierr);
1348e7788613SBarry Smith   if (snes->ops->destroy) {ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr);}
134985385478SLisandro Dalcin 
135085385478SLisandro Dalcin   if (snes->vec_rhs) {ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr);}
135185385478SLisandro Dalcin   if (snes->vec_sol) {ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr);}
135285385478SLisandro Dalcin   if (snes->vec_func) {ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr);}
13533a7fca6bSBarry Smith   if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
13543a7fca6bSBarry Smith   if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
13551cee3971SBarry Smith   if (snes->ksp) {ierr = KSPDestroy(snes->ksp);CHKERRQ(ierr);}
135685385478SLisandro Dalcin   ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);
1357522c5e43SBarry Smith   if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);}
1358a6570f20SBarry Smith   ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);
13597f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);}
1360a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr);
13613a40ed3dSBarry Smith   PetscFunctionReturn(0);
13629b94acceSBarry Smith }
13639b94acceSBarry Smith 
13649b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
13659b94acceSBarry Smith 
13664a2ae208SSatish Balay #undef __FUNCT__
1367a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner"
1368a8054027SBarry Smith /*@
1369a8054027SBarry Smith    SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve.
1370a8054027SBarry Smith 
1371a8054027SBarry Smith    Collective on SNES
1372a8054027SBarry Smith 
1373a8054027SBarry Smith    Input Parameters:
1374a8054027SBarry Smith +  snes - the SNES context
1375a8054027SBarry 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
1376a8054027SBarry Smith          the Jacobian is built etc.
1377a8054027SBarry Smith 
1378a8054027SBarry Smith    Options Database Keys:
1379a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1380a8054027SBarry Smith 
1381a8054027SBarry Smith    Notes:
1382a8054027SBarry Smith    The default is 1
1383a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1384a8054027SBarry Smith    If  -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use
1385a8054027SBarry Smith 
1386a8054027SBarry Smith    Level: intermediate
1387a8054027SBarry Smith 
1388a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1389a8054027SBarry Smith 
1390e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian()
1391a8054027SBarry Smith 
1392a8054027SBarry Smith @*/
1393a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagPreconditioner(SNES snes,PetscInt lag)
1394a8054027SBarry Smith {
1395a8054027SBarry Smith   PetscFunctionBegin;
1396a8054027SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1397a8054027SBarry Smith   if (lag < -1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -1, 1 or greater");
1398a8054027SBarry Smith   if (!lag) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1399a8054027SBarry Smith   snes->lagpreconditioner = lag;
1400a8054027SBarry Smith   PetscFunctionReturn(0);
1401a8054027SBarry Smith }
1402a8054027SBarry Smith 
1403a8054027SBarry Smith #undef __FUNCT__
1404a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner"
1405a8054027SBarry Smith /*@
1406a8054027SBarry Smith    SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt
1407a8054027SBarry Smith 
1408a8054027SBarry Smith    Collective on SNES
1409a8054027SBarry Smith 
1410a8054027SBarry Smith    Input Parameter:
1411a8054027SBarry Smith .  snes - the SNES context
1412a8054027SBarry Smith 
1413a8054027SBarry Smith    Output Parameter:
1414a8054027SBarry 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
1415a8054027SBarry Smith          the Jacobian is built etc.
1416a8054027SBarry Smith 
1417a8054027SBarry Smith    Options Database Keys:
1418a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1419a8054027SBarry Smith 
1420a8054027SBarry Smith    Notes:
1421a8054027SBarry Smith    The default is 1
1422a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1423a8054027SBarry Smith 
1424a8054027SBarry Smith    Level: intermediate
1425a8054027SBarry Smith 
1426a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1427a8054027SBarry Smith 
1428a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner()
1429a8054027SBarry Smith 
1430a8054027SBarry Smith @*/
1431a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagPreconditioner(SNES snes,PetscInt *lag)
1432a8054027SBarry Smith {
1433a8054027SBarry Smith   PetscFunctionBegin;
1434a8054027SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1435a8054027SBarry Smith   *lag = snes->lagpreconditioner;
1436a8054027SBarry Smith   PetscFunctionReturn(0);
1437a8054027SBarry Smith }
1438a8054027SBarry Smith 
1439a8054027SBarry Smith #undef __FUNCT__
1440e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian"
1441e35cf81dSBarry Smith /*@
1442e35cf81dSBarry Smith    SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how
1443e35cf81dSBarry Smith      often the preconditioner is rebuilt.
1444e35cf81dSBarry Smith 
1445e35cf81dSBarry Smith    Collective on SNES
1446e35cf81dSBarry Smith 
1447e35cf81dSBarry Smith    Input Parameters:
1448e35cf81dSBarry Smith +  snes - the SNES context
1449e35cf81dSBarry 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
1450*fe3ffe1eSBarry Smith          the Jacobian is built etc. -2 means rebuild at next chance but then never again
1451e35cf81dSBarry Smith 
1452e35cf81dSBarry Smith    Options Database Keys:
1453e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1454e35cf81dSBarry Smith 
1455e35cf81dSBarry Smith    Notes:
1456e35cf81dSBarry Smith    The default is 1
1457e35cf81dSBarry Smith    The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1458*fe3ffe1eSBarry 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
1459*fe3ffe1eSBarry Smith    at the next Newton step but never again (unless it is reset to another value)
1460e35cf81dSBarry Smith 
1461e35cf81dSBarry Smith    Level: intermediate
1462e35cf81dSBarry Smith 
1463e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1464e35cf81dSBarry Smith 
1465e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian()
1466e35cf81dSBarry Smith 
1467e35cf81dSBarry Smith @*/
1468e35cf81dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagJacobian(SNES snes,PetscInt lag)
1469e35cf81dSBarry Smith {
1470e35cf81dSBarry Smith   PetscFunctionBegin;
1471e35cf81dSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1472*fe3ffe1eSBarry Smith   if (lag < -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1473e35cf81dSBarry Smith   if (!lag) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1474e35cf81dSBarry Smith   snes->lagjacobian = lag;
1475e35cf81dSBarry Smith   PetscFunctionReturn(0);
1476e35cf81dSBarry Smith }
1477e35cf81dSBarry Smith 
1478e35cf81dSBarry Smith #undef __FUNCT__
1479e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian"
1480e35cf81dSBarry Smith /*@
1481e35cf81dSBarry Smith    SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt
1482e35cf81dSBarry Smith 
1483e35cf81dSBarry Smith    Collective on SNES
1484e35cf81dSBarry Smith 
1485e35cf81dSBarry Smith    Input Parameter:
1486e35cf81dSBarry Smith .  snes - the SNES context
1487e35cf81dSBarry Smith 
1488e35cf81dSBarry Smith    Output Parameter:
1489e35cf81dSBarry 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
1490e35cf81dSBarry Smith          the Jacobian is built etc.
1491e35cf81dSBarry Smith 
1492e35cf81dSBarry Smith    Options Database Keys:
1493e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1494e35cf81dSBarry Smith 
1495e35cf81dSBarry Smith    Notes:
1496e35cf81dSBarry Smith    The default is 1
1497e35cf81dSBarry Smith    The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1498e35cf81dSBarry Smith 
1499e35cf81dSBarry Smith    Level: intermediate
1500e35cf81dSBarry Smith 
1501e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1502e35cf81dSBarry Smith 
1503e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner()
1504e35cf81dSBarry Smith 
1505e35cf81dSBarry Smith @*/
1506e35cf81dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagJacobian(SNES snes,PetscInt *lag)
1507e35cf81dSBarry Smith {
1508e35cf81dSBarry Smith   PetscFunctionBegin;
1509e35cf81dSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1510e35cf81dSBarry Smith   *lag = snes->lagjacobian;
1511e35cf81dSBarry Smith   PetscFunctionReturn(0);
1512e35cf81dSBarry Smith }
1513e35cf81dSBarry Smith 
1514e35cf81dSBarry Smith #undef __FUNCT__
15154a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
15169b94acceSBarry Smith /*@
1517d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
15189b94acceSBarry Smith 
1519c7afd0dbSLois Curfman McInnes    Collective on SNES
1520c7afd0dbSLois Curfman McInnes 
15219b94acceSBarry Smith    Input Parameters:
1522c7afd0dbSLois Curfman McInnes +  snes - the SNES context
152370441072SBarry Smith .  abstol - absolute convergence tolerance
152433174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
152533174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
152633174efeSLois Curfman McInnes            of the change in the solution between steps
152733174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1528c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1529fee21e36SBarry Smith 
153033174efeSLois Curfman McInnes    Options Database Keys:
153170441072SBarry Smith +    -snes_atol <abstol> - Sets abstol
1532c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1533c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1534c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1535c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
15369b94acceSBarry Smith 
1537d7a720efSLois Curfman McInnes    Notes:
15389b94acceSBarry Smith    The default maximum number of iterations is 50.
15399b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
15409b94acceSBarry Smith 
154136851e7fSLois Curfman McInnes    Level: intermediate
154236851e7fSLois Curfman McInnes 
154333174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
15449b94acceSBarry Smith 
15452492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance()
15469b94acceSBarry Smith @*/
154763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf)
15489b94acceSBarry Smith {
15493a40ed3dSBarry Smith   PetscFunctionBegin;
15504482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
155170441072SBarry Smith   if (abstol != PETSC_DEFAULT)  snes->abstol      = abstol;
1552d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1553d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1554d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1555d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
15563a40ed3dSBarry Smith   PetscFunctionReturn(0);
15579b94acceSBarry Smith }
15589b94acceSBarry Smith 
15594a2ae208SSatish Balay #undef __FUNCT__
15604a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
15619b94acceSBarry Smith /*@
156233174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
156333174efeSLois Curfman McInnes 
1564c7afd0dbSLois Curfman McInnes    Not Collective
1565c7afd0dbSLois Curfman McInnes 
156633174efeSLois Curfman McInnes    Input Parameters:
1567c7afd0dbSLois Curfman McInnes +  snes - the SNES context
156885385478SLisandro Dalcin .  atol - absolute convergence tolerance
156933174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
157033174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
157133174efeSLois Curfman McInnes            of the change in the solution between steps
157233174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1573c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1574fee21e36SBarry Smith 
157533174efeSLois Curfman McInnes    Notes:
157633174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
157733174efeSLois Curfman McInnes 
157836851e7fSLois Curfman McInnes    Level: intermediate
157936851e7fSLois Curfman McInnes 
158033174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
158133174efeSLois Curfman McInnes 
158233174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
158333174efeSLois Curfman McInnes @*/
158485385478SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf)
158533174efeSLois Curfman McInnes {
15863a40ed3dSBarry Smith   PetscFunctionBegin;
15874482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
158885385478SLisandro Dalcin   if (atol)  *atol  = snes->abstol;
158933174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
159033174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
159133174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
159233174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
15933a40ed3dSBarry Smith   PetscFunctionReturn(0);
159433174efeSLois Curfman McInnes }
159533174efeSLois Curfman McInnes 
15964a2ae208SSatish Balay #undef __FUNCT__
15974a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
159833174efeSLois Curfman McInnes /*@
15999b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
16009b94acceSBarry Smith 
1601fee21e36SBarry Smith    Collective on SNES
1602fee21e36SBarry Smith 
1603c7afd0dbSLois Curfman McInnes    Input Parameters:
1604c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1605c7afd0dbSLois Curfman McInnes -  tol - tolerance
1606c7afd0dbSLois Curfman McInnes 
16079b94acceSBarry Smith    Options Database Key:
1608c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
16099b94acceSBarry Smith 
161036851e7fSLois Curfman McInnes    Level: intermediate
161136851e7fSLois Curfman McInnes 
16129b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
16139b94acceSBarry Smith 
16142492ecdbSBarry Smith .seealso: SNESSetTolerances()
16159b94acceSBarry Smith @*/
161663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
16179b94acceSBarry Smith {
16183a40ed3dSBarry Smith   PetscFunctionBegin;
16194482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
16209b94acceSBarry Smith   snes->deltatol = tol;
16213a40ed3dSBarry Smith   PetscFunctionReturn(0);
16229b94acceSBarry Smith }
16239b94acceSBarry Smith 
1624df9fa365SBarry Smith /*
1625df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1626df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1627df9fa365SBarry Smith    macros instead of functions
1628df9fa365SBarry Smith */
16294a2ae208SSatish Balay #undef __FUNCT__
1630a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG"
1631a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx)
1632ce1608b8SBarry Smith {
1633dfbe8321SBarry Smith   PetscErrorCode ierr;
1634ce1608b8SBarry Smith 
1635ce1608b8SBarry Smith   PetscFunctionBegin;
16364482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1637a6570f20SBarry Smith   ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1638ce1608b8SBarry Smith   PetscFunctionReturn(0);
1639ce1608b8SBarry Smith }
1640ce1608b8SBarry Smith 
16414a2ae208SSatish Balay #undef __FUNCT__
1642a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate"
1643a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1644df9fa365SBarry Smith {
1645dfbe8321SBarry Smith   PetscErrorCode ierr;
1646df9fa365SBarry Smith 
1647df9fa365SBarry Smith   PetscFunctionBegin;
1648a6570f20SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1649df9fa365SBarry Smith   PetscFunctionReturn(0);
1650df9fa365SBarry Smith }
1651df9fa365SBarry Smith 
16524a2ae208SSatish Balay #undef __FUNCT__
1653a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy"
1654a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGDestroy(PetscDrawLG draw)
1655df9fa365SBarry Smith {
1656dfbe8321SBarry Smith   PetscErrorCode ierr;
1657df9fa365SBarry Smith 
1658df9fa365SBarry Smith   PetscFunctionBegin;
1659a6570f20SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
1660df9fa365SBarry Smith   PetscFunctionReturn(0);
1661df9fa365SBarry Smith }
1662df9fa365SBarry Smith 
16639b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
16649b94acceSBarry Smith 
16654a2ae208SSatish Balay #undef __FUNCT__
1666a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet"
16679b94acceSBarry Smith /*@C
1668a6570f20SBarry Smith    SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every
16699b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
16709b94acceSBarry Smith    progress.
16719b94acceSBarry Smith 
1672fee21e36SBarry Smith    Collective on SNES
1673fee21e36SBarry Smith 
1674c7afd0dbSLois Curfman McInnes    Input Parameters:
1675c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1676c7afd0dbSLois Curfman McInnes .  func - monitoring routine
1677b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
1678e8105e01SRichard Katz           monitor routine (use PETSC_NULL if no context is desired)
1679b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1680b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
16819b94acceSBarry Smith 
1682c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1683a7cc72afSBarry Smith $     int func(SNES snes,PetscInt its, PetscReal norm,void *mctx)
1684c7afd0dbSLois Curfman McInnes 
1685c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1686c7afd0dbSLois Curfman McInnes .    its - iteration number
1687c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
168840a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
16899b94acceSBarry Smith 
16909665c990SLois Curfman McInnes    Options Database Keys:
1691a6570f20SBarry Smith +    -snes_monitor        - sets SNESMonitorDefault()
1692a6570f20SBarry Smith .    -snes_monitor_draw    - sets line graph monitor,
1693a6570f20SBarry Smith                             uses SNESMonitorLGCreate()
1694a6570f20SBarry Smith _    -snes_monitor_cancel - cancels all monitors that have
1695c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
1696a6570f20SBarry Smith                             calls to SNESMonitorSet(), but
1697c7afd0dbSLois Curfman McInnes                             does not cancel those set via
1698c7afd0dbSLois Curfman McInnes                             the options database.
16999665c990SLois Curfman McInnes 
1700639f9d9dSBarry Smith    Notes:
17016bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
1702a6570f20SBarry Smith    SNESMonitorSet() multiple times; all will be called in the
17036bc08f3fSLois Curfman McInnes    order in which they were set.
1704639f9d9dSBarry Smith 
1705025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each SNES object
1706025f1a04SBarry Smith 
170736851e7fSLois Curfman McInnes    Level: intermediate
170836851e7fSLois Curfman McInnes 
17099b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
17109b94acceSBarry Smith 
1711a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel()
17129b94acceSBarry Smith @*/
1713b90d0a6eSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void*))
17149b94acceSBarry Smith {
1715b90d0a6eSBarry Smith   PetscInt i;
1716b90d0a6eSBarry Smith 
17173a40ed3dSBarry Smith   PetscFunctionBegin;
17184482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1719639f9d9dSBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) {
172029bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1721639f9d9dSBarry Smith   }
1722b90d0a6eSBarry Smith   for (i=0; i<snes->numbermonitors;i++) {
1723b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) PetscFunctionReturn(0);
1724b90d0a6eSBarry Smith 
1725b90d0a6eSBarry Smith     /* check if both default monitors that share common ASCII viewer */
1726b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitor == SNESMonitorDefault) {
1727b90d0a6eSBarry Smith       if (mctx && snes->monitorcontext[i]) {
1728b90d0a6eSBarry Smith         PetscErrorCode          ierr;
1729b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer1 = (PetscViewerASCIIMonitor) mctx;
1730b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer2 = (PetscViewerASCIIMonitor) snes->monitorcontext[i];
1731b90d0a6eSBarry Smith         if (viewer1->viewer == viewer2->viewer) {
1732b90d0a6eSBarry Smith           ierr = (*monitordestroy)(mctx);CHKERRQ(ierr);
1733b90d0a6eSBarry Smith           PetscFunctionReturn(0);
1734b90d0a6eSBarry Smith         }
1735b90d0a6eSBarry Smith       }
1736b90d0a6eSBarry Smith     }
1737b90d0a6eSBarry Smith   }
1738b90d0a6eSBarry Smith   snes->monitor[snes->numbermonitors]           = monitor;
1739b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
1740639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
17413a40ed3dSBarry Smith   PetscFunctionReturn(0);
17429b94acceSBarry Smith }
17439b94acceSBarry Smith 
17444a2ae208SSatish Balay #undef __FUNCT__
1745a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel"
17465cd90555SBarry Smith /*@C
1747a6570f20SBarry Smith    SNESMonitorCancel - Clears all the monitor functions for a SNES object.
17485cd90555SBarry Smith 
1749c7afd0dbSLois Curfman McInnes    Collective on SNES
1750c7afd0dbSLois Curfman McInnes 
17515cd90555SBarry Smith    Input Parameters:
17525cd90555SBarry Smith .  snes - the SNES context
17535cd90555SBarry Smith 
17541a480d89SAdministrator    Options Database Key:
1755a6570f20SBarry Smith .  -snes_monitor_cancel - cancels all monitors that have been hardwired
1756a6570f20SBarry Smith     into a code by calls to SNESMonitorSet(), but does not cancel those
1757c7afd0dbSLois Curfman McInnes     set via the options database
17585cd90555SBarry Smith 
17595cd90555SBarry Smith    Notes:
17605cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
17615cd90555SBarry Smith 
176236851e7fSLois Curfman McInnes    Level: intermediate
176336851e7fSLois Curfman McInnes 
17645cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
17655cd90555SBarry Smith 
1766a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet()
17675cd90555SBarry Smith @*/
1768a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorCancel(SNES snes)
17695cd90555SBarry Smith {
1770d952e501SBarry Smith   PetscErrorCode ierr;
1771d952e501SBarry Smith   PetscInt       i;
1772d952e501SBarry Smith 
17735cd90555SBarry Smith   PetscFunctionBegin;
17744482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1775d952e501SBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
1776d952e501SBarry Smith     if (snes->monitordestroy[i]) {
1777d952e501SBarry Smith       ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr);
1778d952e501SBarry Smith     }
1779d952e501SBarry Smith   }
17805cd90555SBarry Smith   snes->numbermonitors = 0;
17815cd90555SBarry Smith   PetscFunctionReturn(0);
17825cd90555SBarry Smith }
17835cd90555SBarry Smith 
17844a2ae208SSatish Balay #undef __FUNCT__
17854a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
17869b94acceSBarry Smith /*@C
17879b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
17889b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
17899b94acceSBarry Smith 
1790fee21e36SBarry Smith    Collective on SNES
1791fee21e36SBarry Smith 
1792c7afd0dbSLois Curfman McInnes    Input Parameters:
1793c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1794c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
17957f7931b9SBarry Smith .  cctx - [optional] context for private data for the convergence routine  (may be PETSC_NULL)
17967f7931b9SBarry Smith -  destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran)
17979b94acceSBarry Smith 
1798c7afd0dbSLois Curfman McInnes    Calling sequence of func:
179906ee9f85SBarry Smith $     PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
1800c7afd0dbSLois Curfman McInnes 
1801c7afd0dbSLois Curfman McInnes +    snes - the SNES context
180206ee9f85SBarry Smith .    it - current iteration (0 is the first and is before any Newton step)
1803c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
1804184914b5SBarry Smith .    reason - reason for convergence/divergence
1805c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
18064b27c08aSLois Curfman McInnes .    gnorm - 2-norm of current step
18074b27c08aSLois Curfman McInnes -    f - 2-norm of function
18089b94acceSBarry Smith 
180936851e7fSLois Curfman McInnes    Level: advanced
181036851e7fSLois Curfman McInnes 
18119b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
18129b94acceSBarry Smith 
181385385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged()
18149b94acceSBarry Smith @*/
18157f7931b9SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*))
18169b94acceSBarry Smith {
18177f7931b9SBarry Smith   PetscErrorCode ierr;
18187f7931b9SBarry Smith 
18193a40ed3dSBarry Smith   PetscFunctionBegin;
18204482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
182185385478SLisandro Dalcin   if (!func) func = SNESSkipConverged;
18227f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {
18237f7931b9SBarry Smith     ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);
18247f7931b9SBarry Smith   }
182585385478SLisandro Dalcin   snes->ops->converged        = func;
18267f7931b9SBarry Smith   snes->ops->convergeddestroy = destroy;
182785385478SLisandro Dalcin   snes->cnvP                  = cctx;
18283a40ed3dSBarry Smith   PetscFunctionReturn(0);
18299b94acceSBarry Smith }
18309b94acceSBarry Smith 
18314a2ae208SSatish Balay #undef __FUNCT__
18324a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
183352baeb72SSatish Balay /*@
1834184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
1835184914b5SBarry Smith 
1836184914b5SBarry Smith    Not Collective
1837184914b5SBarry Smith 
1838184914b5SBarry Smith    Input Parameter:
1839184914b5SBarry Smith .  snes - the SNES context
1840184914b5SBarry Smith 
1841184914b5SBarry Smith    Output Parameter:
18424d0a8057SBarry Smith .  reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the
1843184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
1844184914b5SBarry Smith 
1845184914b5SBarry Smith    Level: intermediate
1846184914b5SBarry Smith 
1847184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
1848184914b5SBarry Smith 
1849184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
1850184914b5SBarry Smith 
185185385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason
1852184914b5SBarry Smith @*/
185363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
1854184914b5SBarry Smith {
1855184914b5SBarry Smith   PetscFunctionBegin;
18564482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
18574482741eSBarry Smith   PetscValidPointer(reason,2);
1858184914b5SBarry Smith   *reason = snes->reason;
1859184914b5SBarry Smith   PetscFunctionReturn(0);
1860184914b5SBarry Smith }
1861184914b5SBarry Smith 
18624a2ae208SSatish Balay #undef __FUNCT__
18634a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
1864c9005455SLois Curfman McInnes /*@
1865c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
1866c9005455SLois Curfman McInnes 
1867fee21e36SBarry Smith    Collective on SNES
1868fee21e36SBarry Smith 
1869c7afd0dbSLois Curfman McInnes    Input Parameters:
1870c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
1871c7afd0dbSLois Curfman McInnes .  a   - array to hold history
1872cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
1873758f92a0SBarry Smith .  na  - size of a and its
187464731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
1875758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
1876c7afd0dbSLois Curfman McInnes 
1877c9005455SLois Curfman McInnes    Notes:
18784b27c08aSLois Curfman McInnes    If set, this array will contain the function norms computed
1879c9005455SLois Curfman McInnes    at each step.
1880c9005455SLois Curfman McInnes 
1881c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
1882c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
1883c9005455SLois Curfman McInnes    during the section of code that is being timed.
1884c9005455SLois Curfman McInnes 
188536851e7fSLois Curfman McInnes    Level: intermediate
188636851e7fSLois Curfman McInnes 
1887c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
1888758f92a0SBarry Smith 
188908405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
1890758f92a0SBarry Smith 
1891c9005455SLois Curfman McInnes @*/
1892a562a398SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscTruth reset)
1893c9005455SLois Curfman McInnes {
18943a40ed3dSBarry Smith   PetscFunctionBegin;
18954482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
18964482741eSBarry Smith   if (na)  PetscValidScalarPointer(a,2);
1897a562a398SLisandro Dalcin   if (its) PetscValidIntPointer(its,3);
1898c9005455SLois Curfman McInnes   snes->conv_hist       = a;
1899758f92a0SBarry Smith   snes->conv_hist_its   = its;
1900758f92a0SBarry Smith   snes->conv_hist_max   = na;
1901a12bc48fSLisandro Dalcin   snes->conv_hist_len   = 0;
1902758f92a0SBarry Smith   snes->conv_hist_reset = reset;
1903758f92a0SBarry Smith   PetscFunctionReturn(0);
1904758f92a0SBarry Smith }
1905758f92a0SBarry Smith 
19064a2ae208SSatish Balay #undef __FUNCT__
19074a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
19080c4c9dddSBarry Smith /*@C
1909758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
1910758f92a0SBarry Smith 
1911758f92a0SBarry Smith    Collective on SNES
1912758f92a0SBarry Smith 
1913758f92a0SBarry Smith    Input Parameter:
1914758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
1915758f92a0SBarry Smith 
1916758f92a0SBarry Smith    Output Parameters:
1917758f92a0SBarry Smith .  a   - array to hold history
1918758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
1919758f92a0SBarry Smith          negative if not converged) for each solve.
1920758f92a0SBarry Smith -  na  - size of a and its
1921758f92a0SBarry Smith 
1922758f92a0SBarry Smith    Notes:
1923758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
1924758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
1925758f92a0SBarry Smith 
1926758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
1927758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
1928758f92a0SBarry Smith    during the section of code that is being timed.
1929758f92a0SBarry Smith 
1930758f92a0SBarry Smith    Level: intermediate
1931758f92a0SBarry Smith 
1932758f92a0SBarry Smith .keywords: SNES, get, convergence, history
1933758f92a0SBarry Smith 
1934758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
1935758f92a0SBarry Smith 
1936758f92a0SBarry Smith @*/
193763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na)
1938758f92a0SBarry Smith {
1939758f92a0SBarry Smith   PetscFunctionBegin;
19404482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1941758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
1942758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
1943758f92a0SBarry Smith   if (na)  *na  = snes->conv_hist_len;
19443a40ed3dSBarry Smith   PetscFunctionReturn(0);
1945c9005455SLois Curfman McInnes }
1946c9005455SLois Curfman McInnes 
1947e74ef692SMatthew Knepley #undef __FUNCT__
1948e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
1949ac226902SBarry Smith /*@C
195076b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
19517e4bb74cSBarry Smith   at the beginning o every iteration of the nonlinear solve. Specifically
19527e4bb74cSBarry Smith   it is called just before the Jacobian is "evaluated".
195376b2cf59SMatthew Knepley 
195476b2cf59SMatthew Knepley   Collective on SNES
195576b2cf59SMatthew Knepley 
195676b2cf59SMatthew Knepley   Input Parameters:
195776b2cf59SMatthew Knepley . snes - The nonlinear solver context
195876b2cf59SMatthew Knepley . func - The function
195976b2cf59SMatthew Knepley 
196076b2cf59SMatthew Knepley   Calling sequence of func:
1961b5d30489SBarry Smith . func (SNES snes, PetscInt step);
196276b2cf59SMatthew Knepley 
196376b2cf59SMatthew Knepley . step - The current step of the iteration
196476b2cf59SMatthew Knepley 
196576b2cf59SMatthew Knepley   Level: intermediate
196676b2cf59SMatthew Knepley 
196776b2cf59SMatthew Knepley .keywords: SNES, update
1968b5d30489SBarry Smith 
196985385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve()
197076b2cf59SMatthew Knepley @*/
197163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt))
197276b2cf59SMatthew Knepley {
197376b2cf59SMatthew Knepley   PetscFunctionBegin;
19744482741eSBarry Smith   PetscValidHeaderSpecific(snes, SNES_COOKIE,1);
1975e7788613SBarry Smith   snes->ops->update = func;
197676b2cf59SMatthew Knepley   PetscFunctionReturn(0);
197776b2cf59SMatthew Knepley }
197876b2cf59SMatthew Knepley 
1979e74ef692SMatthew Knepley #undef __FUNCT__
1980e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
198176b2cf59SMatthew Knepley /*@
198276b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
198376b2cf59SMatthew Knepley 
198476b2cf59SMatthew Knepley   Not collective
198576b2cf59SMatthew Knepley 
198676b2cf59SMatthew Knepley   Input Parameters:
198776b2cf59SMatthew Knepley . snes - The nonlinear solver context
198876b2cf59SMatthew Knepley . step - The current step of the iteration
198976b2cf59SMatthew Knepley 
1990205452f4SMatthew Knepley   Level: intermediate
1991205452f4SMatthew Knepley 
199276b2cf59SMatthew Knepley .keywords: SNES, update
1993a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC()
199476b2cf59SMatthew Knepley @*/
199563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultUpdate(SNES snes, PetscInt step)
199676b2cf59SMatthew Knepley {
199776b2cf59SMatthew Knepley   PetscFunctionBegin;
199876b2cf59SMatthew Knepley   PetscFunctionReturn(0);
199976b2cf59SMatthew Knepley }
200076b2cf59SMatthew Knepley 
20014a2ae208SSatish Balay #undef __FUNCT__
20024a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
20039b94acceSBarry Smith /*
20049b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
20059b94acceSBarry Smith    positive parameter delta.
20069b94acceSBarry Smith 
20079b94acceSBarry Smith     Input Parameters:
2008c7afd0dbSLois Curfman McInnes +   snes - the SNES context
20099b94acceSBarry Smith .   y - approximate solution of linear system
20109b94acceSBarry Smith .   fnorm - 2-norm of current function
2011c7afd0dbSLois Curfman McInnes -   delta - trust region size
20129b94acceSBarry Smith 
20139b94acceSBarry Smith     Output Parameters:
2014c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
20159b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
20169b94acceSBarry Smith     region, and exceeds zero otherwise.
2017c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
20189b94acceSBarry Smith 
20199b94acceSBarry Smith     Note:
20204b27c08aSLois Curfman McInnes     For non-trust region methods such as SNESLS, the parameter delta
20219b94acceSBarry Smith     is set to be the maximum allowable step size.
20229b94acceSBarry Smith 
20239b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
20249b94acceSBarry Smith */
2025dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
20269b94acceSBarry Smith {
2027064f8208SBarry Smith   PetscReal      nrm;
2028ea709b57SSatish Balay   PetscScalar    cnorm;
2029dfbe8321SBarry Smith   PetscErrorCode ierr;
20303a40ed3dSBarry Smith 
20313a40ed3dSBarry Smith   PetscFunctionBegin;
20324482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
20334482741eSBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE,2);
2034c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,2);
2035184914b5SBarry Smith 
2036064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
2037064f8208SBarry Smith   if (nrm > *delta) {
2038064f8208SBarry Smith      nrm = *delta/nrm;
2039064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
2040064f8208SBarry Smith      cnorm = nrm;
20412dcb1b2aSMatthew Knepley      ierr = VecScale(y,cnorm);CHKERRQ(ierr);
20429b94acceSBarry Smith      *ynorm = *delta;
20439b94acceSBarry Smith   } else {
20449b94acceSBarry Smith      *gpnorm = 0.0;
2045064f8208SBarry Smith      *ynorm = nrm;
20469b94acceSBarry Smith   }
20473a40ed3dSBarry Smith   PetscFunctionReturn(0);
20489b94acceSBarry Smith }
20499b94acceSBarry Smith 
20504a2ae208SSatish Balay #undef __FUNCT__
20514a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
20526ce558aeSBarry Smith /*@C
2053f69a0ea3SMatthew Knepley    SNESSolve - Solves a nonlinear system F(x) = b.
2054f69a0ea3SMatthew Knepley    Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX().
20559b94acceSBarry Smith 
2056c7afd0dbSLois Curfman McInnes    Collective on SNES
2057c7afd0dbSLois Curfman McInnes 
2058b2002411SLois Curfman McInnes    Input Parameters:
2059c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2060f69a0ea3SMatthew Knepley .  b - the constant part of the equation, or PETSC_NULL to use zero.
206185385478SLisandro Dalcin -  x - the solution vector.
20629b94acceSBarry Smith 
2063b2002411SLois Curfman McInnes    Notes:
20648ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
20658ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
20668ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
20678ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
20688ddd3da0SLois Curfman McInnes 
206936851e7fSLois Curfman McInnes    Level: beginner
207036851e7fSLois Curfman McInnes 
20719b94acceSBarry Smith .keywords: SNES, nonlinear, solve
20729b94acceSBarry Smith 
207385385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian()
20749b94acceSBarry Smith @*/
2075f69a0ea3SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSolve(SNES snes,Vec b,Vec x)
20769b94acceSBarry Smith {
2077dfbe8321SBarry Smith   PetscErrorCode ierr;
2078f1af5d2fSBarry Smith   PetscTruth     flg;
2079eabae89aSBarry Smith   char           filename[PETSC_MAX_PATH_LEN];
2080eabae89aSBarry Smith   PetscViewer    viewer;
2081052efed2SBarry Smith 
20823a40ed3dSBarry Smith   PetscFunctionBegin;
20834482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2084f69a0ea3SMatthew Knepley   PetscValidHeaderSpecific(x,VEC_COOKIE,3);
2085f69a0ea3SMatthew Knepley   PetscCheckSameComm(snes,1,x,3);
208685385478SLisandro Dalcin   if (b) PetscValidHeaderSpecific(b,VEC_COOKIE,2);
208785385478SLisandro Dalcin   if (b) PetscCheckSameComm(snes,1,b,2);
208885385478SLisandro Dalcin 
208985385478SLisandro Dalcin   /* set solution vector */
209085385478SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);
209185385478SLisandro Dalcin   if (snes->vec_sol) { ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr); }
209285385478SLisandro Dalcin   snes->vec_sol = x;
209385385478SLisandro Dalcin   /* set afine vector if provided */
209485385478SLisandro Dalcin   if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); }
209585385478SLisandro Dalcin   if (snes->vec_rhs) { ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr); }
209685385478SLisandro Dalcin   snes->vec_rhs = b;
209785385478SLisandro Dalcin 
209885385478SLisandro Dalcin   if (!snes->vec_func && snes->vec_rhs) {
209985385478SLisandro Dalcin     ierr = VecDuplicate(b, &snes->vec_func); CHKERRQ(ierr);
210070e92668SMatthew Knepley   }
21013f149594SLisandro Dalcin 
210270e92668SMatthew Knepley   ierr = SNESSetUp(snes);CHKERRQ(ierr);
21033f149594SLisandro Dalcin 
2104abc0a331SBarry Smith   if (snes->conv_hist_reset) snes->conv_hist_len = 0;
210550ffb88aSMatthew Knepley   snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
2106d5e45103SBarry Smith 
21073f149594SLisandro Dalcin   ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
21084936397dSBarry Smith   ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr);
210985385478SLisandro Dalcin   ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
21104936397dSBarry Smith   if (snes->domainerror){
21114936397dSBarry Smith     snes->reason      = SNES_DIVERGED_FUNCTION_DOMAIN;
21124936397dSBarry Smith     snes->domainerror = PETSC_FALSE;
21134936397dSBarry Smith   }
211485385478SLisandro Dalcin 
21153f149594SLisandro Dalcin   if (!snes->reason) {
21163f149594SLisandro Dalcin     SETERRQ(PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason");
21173f149594SLisandro Dalcin   }
21183f149594SLisandro Dalcin 
21197adad957SLisandro Dalcin   ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
2120eabae89aSBarry Smith   if (flg && !PetscPreLoadingOn) {
21217adad957SLisandro Dalcin     ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr);
2122eabae89aSBarry Smith     ierr = SNESView(snes,viewer);CHKERRQ(ierr);
2123eabae89aSBarry Smith     ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr);
2124eabae89aSBarry Smith   }
2125eabae89aSBarry Smith 
21267adad957SLisandro Dalcin   ierr = PetscOptionsHasName(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg);CHKERRQ(ierr);
2127da9b6338SBarry Smith   if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); }
21285968eb51SBarry Smith   if (snes->printreason) {
21295968eb51SBarry Smith     if (snes->reason > 0) {
21307adad957SLisandro Dalcin       ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
21315968eb51SBarry Smith     } else {
21327adad957SLisandro Dalcin       ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
21335968eb51SBarry Smith     }
21345968eb51SBarry Smith   }
21355968eb51SBarry Smith 
21363a40ed3dSBarry Smith   PetscFunctionReturn(0);
21379b94acceSBarry Smith }
21389b94acceSBarry Smith 
21399b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
21409b94acceSBarry Smith 
21414a2ae208SSatish Balay #undef __FUNCT__
21424a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
214382bf6240SBarry Smith /*@C
21444b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
21459b94acceSBarry Smith 
2146fee21e36SBarry Smith    Collective on SNES
2147fee21e36SBarry Smith 
2148c7afd0dbSLois Curfman McInnes    Input Parameters:
2149c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2150454a90a3SBarry Smith -  type - a known method
2151c7afd0dbSLois Curfman McInnes 
2152c7afd0dbSLois Curfman McInnes    Options Database Key:
2153454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
2154c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
2155ae12b187SLois Curfman McInnes 
21569b94acceSBarry Smith    Notes:
2157e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
21584b27c08aSLois Curfman McInnes +    SNESLS - Newton's method with line search
2159c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
21604b27c08aSLois Curfman McInnes .    SNESTR - Newton's method with trust region
2161c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
21629b94acceSBarry Smith 
2163ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
2164ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
2165ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
2166ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
2167ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
2168ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
2169ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
2170ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
2171ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
2172b0a32e0cSBarry Smith   appropriate method.
217336851e7fSLois Curfman McInnes 
217436851e7fSLois Curfman McInnes   Level: intermediate
2175a703fe33SLois Curfman McInnes 
2176454a90a3SBarry Smith .keywords: SNES, set, type
2177435da068SBarry Smith 
2178435da068SBarry Smith .seealso: SNESType, SNESCreate()
2179435da068SBarry Smith 
21809b94acceSBarry Smith @*/
2181a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetType(SNES snes,const SNESType type)
21829b94acceSBarry Smith {
2183dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(SNES);
21846831982aSBarry Smith   PetscTruth     match;
21853a40ed3dSBarry Smith 
21863a40ed3dSBarry Smith   PetscFunctionBegin;
21874482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
21884482741eSBarry Smith   PetscValidCharPointer(type,2);
218982bf6240SBarry Smith 
21906831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
21910f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
219292ff6ae8SBarry Smith 
21937adad957SLisandro Dalcin   ierr =  PetscFListFind(SNESList,((PetscObject)snes)->comm,type,(void (**)(void)) &r);CHKERRQ(ierr);
2194958c9bccSBarry Smith   if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type);
219575396ef9SLisandro Dalcin   /* Destroy the previous private SNES context */
219675396ef9SLisandro Dalcin   if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); }
219775396ef9SLisandro Dalcin   /* Reinitialize function pointers in SNESOps structure */
219875396ef9SLisandro Dalcin   snes->ops->setup          = 0;
219975396ef9SLisandro Dalcin   snes->ops->solve          = 0;
220075396ef9SLisandro Dalcin   snes->ops->view           = 0;
220175396ef9SLisandro Dalcin   snes->ops->setfromoptions = 0;
220275396ef9SLisandro Dalcin   snes->ops->destroy        = 0;
220375396ef9SLisandro Dalcin   /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */
220475396ef9SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
22053a40ed3dSBarry Smith   ierr = (*r)(snes);CHKERRQ(ierr);
2206454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
22073a40ed3dSBarry Smith   PetscFunctionReturn(0);
22089b94acceSBarry Smith }
22099b94acceSBarry Smith 
2210a847f771SSatish Balay 
22119b94acceSBarry Smith /* --------------------------------------------------------------------- */
22124a2ae208SSatish Balay #undef __FUNCT__
22134a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
221452baeb72SSatish Balay /*@
22159b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
2216f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
22179b94acceSBarry Smith 
2218fee21e36SBarry Smith    Not Collective
2219fee21e36SBarry Smith 
222036851e7fSLois Curfman McInnes    Level: advanced
222136851e7fSLois Curfman McInnes 
22229b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
22239b94acceSBarry Smith 
22249b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
22259b94acceSBarry Smith @*/
222663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegisterDestroy(void)
22279b94acceSBarry Smith {
2228dfbe8321SBarry Smith   PetscErrorCode ierr;
222982bf6240SBarry Smith 
22303a40ed3dSBarry Smith   PetscFunctionBegin;
22311441b1d3SBarry Smith   ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
22324c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
22333a40ed3dSBarry Smith   PetscFunctionReturn(0);
22349b94acceSBarry Smith }
22359b94acceSBarry Smith 
22364a2ae208SSatish Balay #undef __FUNCT__
22374a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
22389b94acceSBarry Smith /*@C
22399a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
22409b94acceSBarry Smith 
2241c7afd0dbSLois Curfman McInnes    Not Collective
2242c7afd0dbSLois Curfman McInnes 
22439b94acceSBarry Smith    Input Parameter:
22444b0e389bSBarry Smith .  snes - nonlinear solver context
22459b94acceSBarry Smith 
22469b94acceSBarry Smith    Output Parameter:
22473a7fca6bSBarry Smith .  type - SNES method (a character string)
22489b94acceSBarry Smith 
224936851e7fSLois Curfman McInnes    Level: intermediate
225036851e7fSLois Curfman McInnes 
2251454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
22529b94acceSBarry Smith @*/
2253a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetType(SNES snes,const SNESType *type)
22549b94acceSBarry Smith {
22553a40ed3dSBarry Smith   PetscFunctionBegin;
22564482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
22574482741eSBarry Smith   PetscValidPointer(type,2);
22587adad957SLisandro Dalcin   *type = ((PetscObject)snes)->type_name;
22593a40ed3dSBarry Smith   PetscFunctionReturn(0);
22609b94acceSBarry Smith }
22619b94acceSBarry Smith 
22624a2ae208SSatish Balay #undef __FUNCT__
22634a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
226452baeb72SSatish Balay /*@
22659b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
22669b94acceSBarry Smith    stored.
22679b94acceSBarry Smith 
2268c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2269c7afd0dbSLois Curfman McInnes 
22709b94acceSBarry Smith    Input Parameter:
22719b94acceSBarry Smith .  snes - the SNES context
22729b94acceSBarry Smith 
22739b94acceSBarry Smith    Output Parameter:
22749b94acceSBarry Smith .  x - the solution
22759b94acceSBarry Smith 
227670e92668SMatthew Knepley    Level: intermediate
227736851e7fSLois Curfman McInnes 
22789b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
22799b94acceSBarry Smith 
228085385478SLisandro Dalcin .seealso:  SNESGetSolutionUpdate(), SNESGetFunction()
22819b94acceSBarry Smith @*/
228263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolution(SNES snes,Vec *x)
22839b94acceSBarry Smith {
22843a40ed3dSBarry Smith   PetscFunctionBegin;
22854482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
22864482741eSBarry Smith   PetscValidPointer(x,2);
228785385478SLisandro Dalcin   *x = snes->vec_sol;
228870e92668SMatthew Knepley   PetscFunctionReturn(0);
228970e92668SMatthew Knepley }
229070e92668SMatthew Knepley 
229170e92668SMatthew Knepley #undef __FUNCT__
22924a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
229352baeb72SSatish Balay /*@
22949b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
22959b94acceSBarry Smith    stored.
22969b94acceSBarry Smith 
2297c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2298c7afd0dbSLois Curfman McInnes 
22999b94acceSBarry Smith    Input Parameter:
23009b94acceSBarry Smith .  snes - the SNES context
23019b94acceSBarry Smith 
23029b94acceSBarry Smith    Output Parameter:
23039b94acceSBarry Smith .  x - the solution update
23049b94acceSBarry Smith 
230536851e7fSLois Curfman McInnes    Level: advanced
230636851e7fSLois Curfman McInnes 
23079b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
23089b94acceSBarry Smith 
230985385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction()
23109b94acceSBarry Smith @*/
231163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolutionUpdate(SNES snes,Vec *x)
23129b94acceSBarry Smith {
23133a40ed3dSBarry Smith   PetscFunctionBegin;
23144482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
23154482741eSBarry Smith   PetscValidPointer(x,2);
231685385478SLisandro Dalcin   *x = snes->vec_sol_update;
23173a40ed3dSBarry Smith   PetscFunctionReturn(0);
23189b94acceSBarry Smith }
23199b94acceSBarry Smith 
23204a2ae208SSatish Balay #undef __FUNCT__
23214a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
23229b94acceSBarry Smith /*@C
23233638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
23249b94acceSBarry Smith 
2325c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2326c7afd0dbSLois Curfman McInnes 
23279b94acceSBarry Smith    Input Parameter:
23289b94acceSBarry Smith .  snes - the SNES context
23299b94acceSBarry Smith 
23309b94acceSBarry Smith    Output Parameter:
23317bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
233270e92668SMatthew Knepley .  func - the function (or PETSC_NULL)
233370e92668SMatthew Knepley -  ctx - the function context (or PETSC_NULL)
23349b94acceSBarry Smith 
233536851e7fSLois Curfman McInnes    Level: advanced
233636851e7fSLois Curfman McInnes 
2337a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
23389b94acceSBarry Smith 
23394b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution()
23409b94acceSBarry Smith @*/
234170e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx)
23429b94acceSBarry Smith {
23433a40ed3dSBarry Smith   PetscFunctionBegin;
23444482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
234585385478SLisandro Dalcin   if (r)    *r    = snes->vec_func;
2346e7788613SBarry Smith   if (func) *func = snes->ops->computefunction;
234770e92668SMatthew Knepley   if (ctx)  *ctx  = snes->funP;
23483a40ed3dSBarry Smith   PetscFunctionReturn(0);
23499b94acceSBarry Smith }
23509b94acceSBarry Smith 
23514a2ae208SSatish Balay #undef __FUNCT__
23524a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
23533c7409f5SSatish Balay /*@C
23543c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2355d850072dSLois Curfman McInnes    SNES options in the database.
23563c7409f5SSatish Balay 
2357fee21e36SBarry Smith    Collective on SNES
2358fee21e36SBarry Smith 
2359c7afd0dbSLois Curfman McInnes    Input Parameter:
2360c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2361c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2362c7afd0dbSLois Curfman McInnes 
2363d850072dSLois Curfman McInnes    Notes:
2364a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2365c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2366d850072dSLois Curfman McInnes 
236736851e7fSLois Curfman McInnes    Level: advanced
236836851e7fSLois Curfman McInnes 
23693c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2370a86d99e1SLois Curfman McInnes 
2371a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
23723c7409f5SSatish Balay @*/
237363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetOptionsPrefix(SNES snes,const char prefix[])
23743c7409f5SSatish Balay {
2375dfbe8321SBarry Smith   PetscErrorCode ierr;
23763c7409f5SSatish Balay 
23773a40ed3dSBarry Smith   PetscFunctionBegin;
23784482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2379639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
23801cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
238194b7f48cSBarry Smith   ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
23823a40ed3dSBarry Smith   PetscFunctionReturn(0);
23833c7409f5SSatish Balay }
23843c7409f5SSatish Balay 
23854a2ae208SSatish Balay #undef __FUNCT__
23864a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
23873c7409f5SSatish Balay /*@C
2388f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2389d850072dSLois Curfman McInnes    SNES options in the database.
23903c7409f5SSatish Balay 
2391fee21e36SBarry Smith    Collective on SNES
2392fee21e36SBarry Smith 
2393c7afd0dbSLois Curfman McInnes    Input Parameters:
2394c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2395c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2396c7afd0dbSLois Curfman McInnes 
2397d850072dSLois Curfman McInnes    Notes:
2398a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2399c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2400d850072dSLois Curfman McInnes 
240136851e7fSLois Curfman McInnes    Level: advanced
240236851e7fSLois Curfman McInnes 
24033c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2404a86d99e1SLois Curfman McInnes 
2405a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
24063c7409f5SSatish Balay @*/
240763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAppendOptionsPrefix(SNES snes,const char prefix[])
24083c7409f5SSatish Balay {
2409dfbe8321SBarry Smith   PetscErrorCode ierr;
24103c7409f5SSatish Balay 
24113a40ed3dSBarry Smith   PetscFunctionBegin;
24124482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2413639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
24141cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
241594b7f48cSBarry Smith   ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
24163a40ed3dSBarry Smith   PetscFunctionReturn(0);
24173c7409f5SSatish Balay }
24183c7409f5SSatish Balay 
24194a2ae208SSatish Balay #undef __FUNCT__
24204a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
24219ab63eb5SSatish Balay /*@C
24223c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
24233c7409f5SSatish Balay    SNES options in the database.
24243c7409f5SSatish Balay 
2425c7afd0dbSLois Curfman McInnes    Not Collective
2426c7afd0dbSLois Curfman McInnes 
24273c7409f5SSatish Balay    Input Parameter:
24283c7409f5SSatish Balay .  snes - the SNES context
24293c7409f5SSatish Balay 
24303c7409f5SSatish Balay    Output Parameter:
24313c7409f5SSatish Balay .  prefix - pointer to the prefix string used
24323c7409f5SSatish Balay 
24334ef407dbSRichard Tran Mills    Notes: On the fortran side, the user should pass in a string 'prefix' of
24349ab63eb5SSatish Balay    sufficient length to hold the prefix.
24359ab63eb5SSatish Balay 
243636851e7fSLois Curfman McInnes    Level: advanced
243736851e7fSLois Curfman McInnes 
24383c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2439a86d99e1SLois Curfman McInnes 
2440a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
24413c7409f5SSatish Balay @*/
2442e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetOptionsPrefix(SNES snes,const char *prefix[])
24433c7409f5SSatish Balay {
2444dfbe8321SBarry Smith   PetscErrorCode ierr;
24453c7409f5SSatish Balay 
24463a40ed3dSBarry Smith   PetscFunctionBegin;
24474482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2448639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
24493a40ed3dSBarry Smith   PetscFunctionReturn(0);
24503c7409f5SSatish Balay }
24513c7409f5SSatish Balay 
2452b2002411SLois Curfman McInnes 
24534a2ae208SSatish Balay #undef __FUNCT__
24544a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
24553cea93caSBarry Smith /*@C
24563cea93caSBarry Smith   SNESRegister - See SNESRegisterDynamic()
24573cea93caSBarry Smith 
24587f6c08e0SMatthew Knepley   Level: advanced
24593cea93caSBarry Smith @*/
246063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES))
2461b2002411SLois Curfman McInnes {
2462e2d1d2b7SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
2463dfbe8321SBarry Smith   PetscErrorCode ierr;
2464b2002411SLois Curfman McInnes 
2465b2002411SLois Curfman McInnes   PetscFunctionBegin;
2466b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2467c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
2468b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2469b2002411SLois Curfman McInnes }
2470da9b6338SBarry Smith 
2471da9b6338SBarry Smith #undef __FUNCT__
2472da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin"
247363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESTestLocalMin(SNES snes)
2474da9b6338SBarry Smith {
2475dfbe8321SBarry Smith   PetscErrorCode ierr;
247677431f27SBarry Smith   PetscInt       N,i,j;
2477da9b6338SBarry Smith   Vec            u,uh,fh;
2478da9b6338SBarry Smith   PetscScalar    value;
2479da9b6338SBarry Smith   PetscReal      norm;
2480da9b6338SBarry Smith 
2481da9b6338SBarry Smith   PetscFunctionBegin;
2482da9b6338SBarry Smith   ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr);
2483da9b6338SBarry Smith   ierr = VecDuplicate(u,&uh);CHKERRQ(ierr);
2484da9b6338SBarry Smith   ierr = VecDuplicate(u,&fh);CHKERRQ(ierr);
2485da9b6338SBarry Smith 
2486da9b6338SBarry Smith   /* currently only works for sequential */
2487da9b6338SBarry Smith   ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n");
2488da9b6338SBarry Smith   ierr = VecGetSize(u,&N);CHKERRQ(ierr);
2489da9b6338SBarry Smith   for (i=0; i<N; i++) {
2490da9b6338SBarry Smith     ierr = VecCopy(u,uh);CHKERRQ(ierr);
249177431f27SBarry Smith     ierr  = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr);
2492da9b6338SBarry Smith     for (j=-10; j<11; j++) {
2493ccae9161SBarry Smith       value = PetscSign(j)*exp(PetscAbs(j)-10.0);
2494da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
24953ab0aad5SBarry Smith       ierr  = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr);
2496da9b6338SBarry Smith       ierr  = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr);
249777431f27SBarry Smith       ierr  = PetscPrintf(PETSC_COMM_WORLD,"       j norm %D %18.16e\n",j,norm);CHKERRQ(ierr);
2498da9b6338SBarry Smith       value = -value;
2499da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
2500da9b6338SBarry Smith     }
2501da9b6338SBarry Smith   }
2502da9b6338SBarry Smith   ierr = VecDestroy(uh);CHKERRQ(ierr);
2503da9b6338SBarry Smith   ierr = VecDestroy(fh);CHKERRQ(ierr);
2504da9b6338SBarry Smith   PetscFunctionReturn(0);
2505da9b6338SBarry Smith }
250671f87433Sdalcinl 
250771f87433Sdalcinl #undef __FUNCT__
2508fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW"
250971f87433Sdalcinl /*@
2510fa9f3622SBarry Smith    SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for
251171f87433Sdalcinl    computing relative tolerance for linear solvers within an inexact
251271f87433Sdalcinl    Newton method.
251371f87433Sdalcinl 
251471f87433Sdalcinl    Collective on SNES
251571f87433Sdalcinl 
251671f87433Sdalcinl    Input Parameters:
251771f87433Sdalcinl +  snes - SNES context
251871f87433Sdalcinl -  flag - PETSC_TRUE or PETSC_FALSE
251971f87433Sdalcinl 
252071f87433Sdalcinl    Notes:
252171f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
252271f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
252371f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
252471f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
252571f87433Sdalcinl    solver.
252671f87433Sdalcinl 
252771f87433Sdalcinl    Level: advanced
252871f87433Sdalcinl 
252971f87433Sdalcinl    Reference:
253071f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
253171f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
253271f87433Sdalcinl 
253371f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
253471f87433Sdalcinl 
2535fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
253671f87433Sdalcinl @*/
2537fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetUseEW(SNES snes,PetscTruth flag)
253871f87433Sdalcinl {
253971f87433Sdalcinl   PetscFunctionBegin;
254071f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
254171f87433Sdalcinl   snes->ksp_ewconv = flag;
254271f87433Sdalcinl   PetscFunctionReturn(0);
254371f87433Sdalcinl }
254471f87433Sdalcinl 
254571f87433Sdalcinl #undef __FUNCT__
2546fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW"
254771f87433Sdalcinl /*@
2548fa9f3622SBarry Smith    SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method
254971f87433Sdalcinl    for computing relative tolerance for linear solvers within an
255071f87433Sdalcinl    inexact Newton method.
255171f87433Sdalcinl 
255271f87433Sdalcinl    Not Collective
255371f87433Sdalcinl 
255471f87433Sdalcinl    Input Parameter:
255571f87433Sdalcinl .  snes - SNES context
255671f87433Sdalcinl 
255771f87433Sdalcinl    Output Parameter:
255871f87433Sdalcinl .  flag - PETSC_TRUE or PETSC_FALSE
255971f87433Sdalcinl 
256071f87433Sdalcinl    Notes:
256171f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
256271f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
256371f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
256471f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
256571f87433Sdalcinl    solver.
256671f87433Sdalcinl 
256771f87433Sdalcinl    Level: advanced
256871f87433Sdalcinl 
256971f87433Sdalcinl    Reference:
257071f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
257171f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
257271f87433Sdalcinl 
257371f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
257471f87433Sdalcinl 
2575fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
257671f87433Sdalcinl @*/
2577fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetUseEW(SNES snes, PetscTruth *flag)
257871f87433Sdalcinl {
257971f87433Sdalcinl   PetscFunctionBegin;
258071f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
258171f87433Sdalcinl   PetscValidPointer(flag,2);
258271f87433Sdalcinl   *flag = snes->ksp_ewconv;
258371f87433Sdalcinl   PetscFunctionReturn(0);
258471f87433Sdalcinl }
258571f87433Sdalcinl 
258671f87433Sdalcinl #undef __FUNCT__
2587fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW"
258871f87433Sdalcinl /*@
2589fa9f3622SBarry Smith    SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker
259071f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
259171f87433Sdalcinl    Newton method.
259271f87433Sdalcinl 
259371f87433Sdalcinl    Collective on SNES
259471f87433Sdalcinl 
259571f87433Sdalcinl    Input Parameters:
259671f87433Sdalcinl +    snes - SNES context
259771f87433Sdalcinl .    version - version 1, 2 (default is 2) or 3
259871f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
259971f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
260071f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
260171f87433Sdalcinl              (0 <= gamma2 <= 1)
260271f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
260371f87433Sdalcinl .    alpha2 - power for safeguard
260471f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
260571f87433Sdalcinl 
260671f87433Sdalcinl    Note:
260771f87433Sdalcinl    Version 3 was contributed by Luis Chacon, June 2006.
260871f87433Sdalcinl 
260971f87433Sdalcinl    Use PETSC_DEFAULT to retain the default for any of the parameters.
261071f87433Sdalcinl 
261171f87433Sdalcinl    Level: advanced
261271f87433Sdalcinl 
261371f87433Sdalcinl    Reference:
261471f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
261571f87433Sdalcinl    inexact Newton method", Utah State University Math. Stat. Dept. Res.
261671f87433Sdalcinl    Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput.
261771f87433Sdalcinl 
261871f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters
261971f87433Sdalcinl 
2620fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW()
262171f87433Sdalcinl @*/
2622fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max,
262371f87433Sdalcinl 							    PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold)
262471f87433Sdalcinl {
2625fa9f3622SBarry Smith   SNESKSPEW *kctx;
262671f87433Sdalcinl   PetscFunctionBegin;
262771f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2628fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
262971f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
263071f87433Sdalcinl 
263171f87433Sdalcinl   if (version != PETSC_DEFAULT)   kctx->version   = version;
263271f87433Sdalcinl   if (rtol_0 != PETSC_DEFAULT)    kctx->rtol_0    = rtol_0;
263371f87433Sdalcinl   if (rtol_max != PETSC_DEFAULT)  kctx->rtol_max  = rtol_max;
263471f87433Sdalcinl   if (gamma != PETSC_DEFAULT)     kctx->gamma     = gamma;
263571f87433Sdalcinl   if (alpha != PETSC_DEFAULT)     kctx->alpha     = alpha;
263671f87433Sdalcinl   if (alpha2 != PETSC_DEFAULT)    kctx->alpha2    = alpha2;
263771f87433Sdalcinl   if (threshold != PETSC_DEFAULT) kctx->threshold = threshold;
263871f87433Sdalcinl 
263971f87433Sdalcinl   if (kctx->version < 1 || kctx->version > 3) {
264071f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version);
264171f87433Sdalcinl   }
264271f87433Sdalcinl   if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) {
264371f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0);
264471f87433Sdalcinl   }
264571f87433Sdalcinl   if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) {
264671f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max);
264771f87433Sdalcinl   }
264871f87433Sdalcinl   if (kctx->gamma < 0.0 || kctx->gamma > 1.0) {
264971f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma);
265071f87433Sdalcinl   }
265171f87433Sdalcinl   if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) {
265271f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha);
265371f87433Sdalcinl   }
265471f87433Sdalcinl   if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) {
265571f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold);
265671f87433Sdalcinl   }
265771f87433Sdalcinl   PetscFunctionReturn(0);
265871f87433Sdalcinl }
265971f87433Sdalcinl 
266071f87433Sdalcinl #undef __FUNCT__
2661fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW"
266271f87433Sdalcinl /*@
2663fa9f3622SBarry Smith    SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker
266471f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
266571f87433Sdalcinl    Newton method.
266671f87433Sdalcinl 
266771f87433Sdalcinl    Not Collective
266871f87433Sdalcinl 
266971f87433Sdalcinl    Input Parameters:
267071f87433Sdalcinl      snes - SNES context
267171f87433Sdalcinl 
267271f87433Sdalcinl    Output Parameters:
267371f87433Sdalcinl +    version - version 1, 2 (default is 2) or 3
267471f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
267571f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
267671f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
267771f87433Sdalcinl              (0 <= gamma2 <= 1)
267871f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
267971f87433Sdalcinl .    alpha2 - power for safeguard
268071f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
268171f87433Sdalcinl 
268271f87433Sdalcinl    Level: advanced
268371f87433Sdalcinl 
268471f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters
268571f87433Sdalcinl 
2686fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW()
268771f87433Sdalcinl @*/
2688fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max,
268971f87433Sdalcinl 							    PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold)
269071f87433Sdalcinl {
2691fa9f3622SBarry Smith   SNESKSPEW *kctx;
269271f87433Sdalcinl   PetscFunctionBegin;
269371f87433Sdalcinl   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2694fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
269571f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
269671f87433Sdalcinl   if(version)   *version   = kctx->version;
269771f87433Sdalcinl   if(rtol_0)    *rtol_0    = kctx->rtol_0;
269871f87433Sdalcinl   if(rtol_max)  *rtol_max  = kctx->rtol_max;
269971f87433Sdalcinl   if(gamma)     *gamma     = kctx->gamma;
270071f87433Sdalcinl   if(alpha)     *alpha     = kctx->alpha;
270171f87433Sdalcinl   if(alpha2)    *alpha2    = kctx->alpha2;
270271f87433Sdalcinl   if(threshold) *threshold = kctx->threshold;
270371f87433Sdalcinl   PetscFunctionReturn(0);
270471f87433Sdalcinl }
270571f87433Sdalcinl 
270671f87433Sdalcinl #undef __FUNCT__
2707fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve"
2708fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x)
270971f87433Sdalcinl {
271071f87433Sdalcinl   PetscErrorCode ierr;
2711fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
271271f87433Sdalcinl   PetscReal      rtol=PETSC_DEFAULT,stol;
271371f87433Sdalcinl 
271471f87433Sdalcinl   PetscFunctionBegin;
271571f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
271671f87433Sdalcinl   if (!snes->iter) { /* first time in, so use the original user rtol */
271771f87433Sdalcinl     rtol = kctx->rtol_0;
271871f87433Sdalcinl   } else {
271971f87433Sdalcinl     if (kctx->version == 1) {
272071f87433Sdalcinl       rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last;
272171f87433Sdalcinl       if (rtol < 0.0) rtol = -rtol;
272271f87433Sdalcinl       stol = pow(kctx->rtol_last,kctx->alpha2);
272371f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
272471f87433Sdalcinl     } else if (kctx->version == 2) {
272571f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
272671f87433Sdalcinl       stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha);
272771f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
272871f87433Sdalcinl     } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */
272971f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
273071f87433Sdalcinl       /* safeguard: avoid sharp decrease of rtol */
273171f87433Sdalcinl       stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha);
273271f87433Sdalcinl       stol = PetscMax(rtol,stol);
273371f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
273471f87433Sdalcinl       /* safeguard: avoid oversolving */
273571f87433Sdalcinl       stol = kctx->gamma*(snes->ttol)/snes->norm;
273671f87433Sdalcinl       stol = PetscMax(rtol,stol);
273771f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
273871f87433Sdalcinl     } else SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version);
273971f87433Sdalcinl   }
274071f87433Sdalcinl   /* safeguard: avoid rtol greater than one */
274171f87433Sdalcinl   rtol = PetscMin(rtol,kctx->rtol_max);
274271f87433Sdalcinl   ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
274371f87433Sdalcinl   ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr);
274471f87433Sdalcinl   PetscFunctionReturn(0);
274571f87433Sdalcinl }
274671f87433Sdalcinl 
274771f87433Sdalcinl #undef __FUNCT__
2748fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve"
2749fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x)
275071f87433Sdalcinl {
275171f87433Sdalcinl   PetscErrorCode ierr;
2752fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
275371f87433Sdalcinl   PCSide         pcside;
275471f87433Sdalcinl   Vec            lres;
275571f87433Sdalcinl 
275671f87433Sdalcinl   PetscFunctionBegin;
275771f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
275871f87433Sdalcinl   ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr);
275971f87433Sdalcinl   ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr);
276071f87433Sdalcinl   if (kctx->version == 1) {
276171f87433Sdalcinl     ierr = KSPGetPreconditionerSide(ksp,&pcside);CHKERRQ(ierr);
276271f87433Sdalcinl     if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */
276371f87433Sdalcinl       /* KSP residual is true linear residual */
276471f87433Sdalcinl       ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr);
276571f87433Sdalcinl     } else {
276671f87433Sdalcinl       /* KSP residual is preconditioned residual */
276771f87433Sdalcinl       /* compute true linear residual norm */
276871f87433Sdalcinl       ierr = VecDuplicate(b,&lres);CHKERRQ(ierr);
276971f87433Sdalcinl       ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr);
277071f87433Sdalcinl       ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr);
277171f87433Sdalcinl       ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr);
277271f87433Sdalcinl       ierr = VecDestroy(lres);CHKERRQ(ierr);
277371f87433Sdalcinl     }
277471f87433Sdalcinl   }
277571f87433Sdalcinl   PetscFunctionReturn(0);
277671f87433Sdalcinl }
277771f87433Sdalcinl 
277871f87433Sdalcinl #undef __FUNCT__
277971f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve"
278071f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x)
278171f87433Sdalcinl {
278271f87433Sdalcinl   PetscErrorCode ierr;
278371f87433Sdalcinl 
278471f87433Sdalcinl   PetscFunctionBegin;
2785fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr);  }
278671f87433Sdalcinl   ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);
2787fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); }
278871f87433Sdalcinl   PetscFunctionReturn(0);
278971f87433Sdalcinl }
2790