xref: /petsc/src/snes/interface/snes.c (revision f22f69f01ec1ca0bb29797ff3752b65f1e1c47db)
163dd3a1aSKris Buschelman #define PETSCSNES_DLL
29b94acceSBarry Smith 
37c4f633dSBarry Smith #include "private/snesimpl.h"      /*I "petscsnes.h"  I*/
49b94acceSBarry Smith 
54c49b128SBarry Smith PetscTruth SNESRegisterAllCalled = PETSC_FALSE;
68ba1e511SMatthew Knepley PetscFList SNESList              = PETSC_NULL;
78ba1e511SMatthew Knepley 
88ba1e511SMatthew Knepley /* Logging support */
90700a824SBarry Smith PetscClassId PETSCSNES_DLLEXPORT SNES_CLASSID;
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;
320700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,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;
770700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
783050cee2SBarry Smith   if (!viewer) {
797adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)snes)->comm,&viewer);CHKERRQ(ierr);
803050cee2SBarry Smith   }
810700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,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     }
116eb1f6c34SBarry Smith     if (snes->lagpreconditioner == -1) {
117eb1f6c34SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioned is never rebuilt\n");CHKERRQ(ierr);
118eb1f6c34SBarry Smith     } else if (snes->lagpreconditioner > 1) {
119eb1f6c34SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Preconditioned is rebuilt every %D new Jacobians\n",snes->lagpreconditioner);CHKERRQ(ierr);
120eb1f6c34SBarry Smith     }
121eb1f6c34SBarry Smith     if (snes->lagjacobian == -1) {
122eb1f6c34SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Jacobian is never rebuilt\n");CHKERRQ(ierr);
123eb1f6c34SBarry Smith     } else if (snes->lagjacobian > 1) {
124eb1f6c34SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  Jacobian is rebuilt every %D Newton iterations\n",snes->lagjacobian);CHKERRQ(ierr);
125eb1f6c34SBarry Smith     }
1260f5bd95cSBarry Smith   } else if (isstring) {
127454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
128b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr);
12919bcc07fSBarry Smith   }
13094b7f48cSBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
131b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
13294b7f48cSBarry Smith   ierr = KSPView(ksp,viewer);CHKERRQ(ierr);
133b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1343a40ed3dSBarry Smith   PetscFunctionReturn(0);
1359b94acceSBarry Smith }
1369b94acceSBarry Smith 
13776b2cf59SMatthew Knepley /*
13876b2cf59SMatthew Knepley   We retain a list of functions that also take SNES command
13976b2cf59SMatthew Knepley   line options. These are called at the end SNESSetFromOptions()
14076b2cf59SMatthew Knepley */
14176b2cf59SMatthew Knepley #define MAXSETFROMOPTIONS 5
142a7cc72afSBarry Smith static PetscInt numberofsetfromoptions;
1436849ba73SBarry Smith static PetscErrorCode (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES);
14476b2cf59SMatthew Knepley 
145e74ef692SMatthew Knepley #undef __FUNCT__
146e74ef692SMatthew Knepley #define __FUNCT__ "SNESAddOptionsChecker"
147ac226902SBarry Smith /*@C
14876b2cf59SMatthew Knepley   SNESAddOptionsChecker - Adds an additional function to check for SNES options.
14976b2cf59SMatthew Knepley 
15076b2cf59SMatthew Knepley   Not Collective
15176b2cf59SMatthew Knepley 
15276b2cf59SMatthew Knepley   Input Parameter:
15376b2cf59SMatthew Knepley . snescheck - function that checks for options
15476b2cf59SMatthew Knepley 
15576b2cf59SMatthew Knepley   Level: developer
15676b2cf59SMatthew Knepley 
15776b2cf59SMatthew Knepley .seealso: SNESSetFromOptions()
15876b2cf59SMatthew Knepley @*/
15963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAddOptionsChecker(PetscErrorCode (*snescheck)(SNES))
16076b2cf59SMatthew Knepley {
16176b2cf59SMatthew Knepley   PetscFunctionBegin;
16276b2cf59SMatthew Knepley   if (numberofsetfromoptions >= MAXSETFROMOPTIONS) {
16377431f27SBarry Smith     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %D allowed", MAXSETFROMOPTIONS);
16476b2cf59SMatthew Knepley   }
16576b2cf59SMatthew Knepley   othersetfromoptions[numberofsetfromoptions++] = snescheck;
16676b2cf59SMatthew Knepley   PetscFunctionReturn(0);
16776b2cf59SMatthew Knepley }
16876b2cf59SMatthew Knepley 
169aa3661deSLisandro Dalcin EXTERN PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*);
170aa3661deSLisandro Dalcin 
171aa3661deSLisandro Dalcin #undef __FUNCT__
172aa3661deSLisandro Dalcin #define __FUNCT__ "SNESSetUpMatrixFree_Private"
173d3462f78SMatthew Knepley static PetscErrorCode SNESSetUpMatrixFree_Private(SNES snes, PetscTruth hasOperator, PetscInt version)
174aa3661deSLisandro Dalcin {
175aa3661deSLisandro Dalcin   Mat            J;
176aa3661deSLisandro Dalcin   KSP            ksp;
177aa3661deSLisandro Dalcin   PC             pc;
178aa3661deSLisandro Dalcin   PetscTruth     match;
179aa3661deSLisandro Dalcin   PetscErrorCode ierr;
180aa3661deSLisandro Dalcin 
181aa3661deSLisandro Dalcin   PetscFunctionBegin;
1820700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
183aa3661deSLisandro Dalcin 
18498613b67SLisandro Dalcin   if(!snes->vec_func && (snes->jacobian || snes->jacobian_pre)) {
18598613b67SLisandro Dalcin     Mat A = snes->jacobian, B = snes->jacobian_pre;
18698613b67SLisandro Dalcin     ierr = MatGetVecs(A ? A : B, PETSC_NULL,&snes->vec_func);CHKERRQ(ierr);
18798613b67SLisandro Dalcin   }
18898613b67SLisandro Dalcin 
189aa3661deSLisandro Dalcin   if (version == 1) {
190aa3661deSLisandro Dalcin     ierr = MatCreateSNESMF(snes,&J);CHKERRQ(ierr);
19198613b67SLisandro Dalcin     ierr = MatMFFDSetOptionsPrefix(J,((PetscObject)snes)->prefix);CHKERRQ(ierr);
192aa3661deSLisandro Dalcin     ierr = MatMFFDSetFromOptions(J);CHKERRQ(ierr);
193aa3661deSLisandro Dalcin   } else if (version == 2) {
194aa3661deSLisandro Dalcin     if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"SNESSetFunction() must be called first");
19565460251SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SCALAR_SINGLE) && !defined(PETSC_USE_SCALAR_MAT_SINGLE) && !defined(PETSC_USE_SCALAR_LONG_DOUBLE) && !defined(PETSC_USE_SCALAR_INT)
196aa3661deSLisandro Dalcin     ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_func,&J);CHKERRQ(ierr);
197aa3661deSLisandro Dalcin #else
198aa3661deSLisandro Dalcin     SETERRQ(PETSC_ERR_SUP, "matrix-free operator rutines (version 2)");
199aa3661deSLisandro Dalcin #endif
200aa3661deSLisandro Dalcin   } else {
201aa3661deSLisandro Dalcin     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE, "matrix-free operator rutines, only version 1 and 2");
202aa3661deSLisandro Dalcin   }
203aa3661deSLisandro Dalcin 
204aa3661deSLisandro Dalcin   ierr = PetscInfo1(snes,"Setting default matrix-free operator routines (version %D)\n", version);CHKERRQ(ierr);
205d3462f78SMatthew Knepley   if (hasOperator) {
206aa3661deSLisandro Dalcin     /* This version replaces the user provided Jacobian matrix with a
207aa3661deSLisandro Dalcin        matrix-free version but still employs the user-provided preconditioner matrix. */
208aa3661deSLisandro Dalcin     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
209aa3661deSLisandro Dalcin   } else {
210aa3661deSLisandro Dalcin     /* This version replaces both the user-provided Jacobian and the user-
211aa3661deSLisandro Dalcin        provided preconditioner matrix with the default matrix free version. */
212aa3661deSLisandro Dalcin     ierr = SNESSetJacobian(snes,J,J,MatMFFDComputeJacobian,snes->funP);CHKERRQ(ierr);
213aa3661deSLisandro Dalcin     /* Force no preconditioner */
214aa3661deSLisandro Dalcin     ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
215aa3661deSLisandro Dalcin     ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
216aa3661deSLisandro Dalcin     ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&match);CHKERRQ(ierr);
217aa3661deSLisandro Dalcin     if (!match) {
218aa3661deSLisandro Dalcin       ierr = PetscInfo(snes,"Setting default matrix-free preconditioner routines\nThat is no preconditioner is being used\n");CHKERRQ(ierr);
219aa3661deSLisandro Dalcin       ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
220aa3661deSLisandro Dalcin     }
221aa3661deSLisandro Dalcin   }
222aa3661deSLisandro Dalcin   ierr = MatDestroy(J);CHKERRQ(ierr);
223aa3661deSLisandro Dalcin 
224aa3661deSLisandro Dalcin   PetscFunctionReturn(0);
225aa3661deSLisandro Dalcin }
226aa3661deSLisandro Dalcin 
2274a2ae208SSatish Balay #undef __FUNCT__
2284a2ae208SSatish Balay #define __FUNCT__ "SNESSetFromOptions"
2299b94acceSBarry Smith /*@
23094b7f48cSBarry Smith    SNESSetFromOptions - Sets various SNES and KSP parameters from user options.
2319b94acceSBarry Smith 
232c7afd0dbSLois Curfman McInnes    Collective on SNES
233c7afd0dbSLois Curfman McInnes 
2349b94acceSBarry Smith    Input Parameter:
2359b94acceSBarry Smith .  snes - the SNES context
2369b94acceSBarry Smith 
23736851e7fSLois Curfman McInnes    Options Database Keys:
2386831982aSBarry Smith +  -snes_type <type> - ls, tr, umls, umtr, test
23982738288SBarry Smith .  -snes_stol - convergence tolerance in terms of the norm
24082738288SBarry Smith                 of the change in the solution between steps
24170441072SBarry Smith .  -snes_atol <abstol> - absolute tolerance of residual norm
242b39c3a46SLois Curfman McInnes .  -snes_rtol <rtol> - relative decrease in tolerance norm from initial
243b39c3a46SLois Curfman McInnes .  -snes_max_it <max_it> - maximum number of iterations
244b39c3a46SLois Curfman McInnes .  -snes_max_funcs <max_funcs> - maximum number of function evaluations
24550ffb88aSMatthew Knepley .  -snes_max_fail <max_fail> - maximum number of failures
246ddf469c8SBarry Smith .  -snes_max_linear_solve_fail - number of linear solver failures before SNESSolve() stops
247a8054027SBarry Smith .  -snes_lag_preconditioner <lag> - how often preconditioner is rebuilt (use -1 to never rebuild)
248e35cf81dSBarry Smith .  -snes_lag_jacobian <lag> - how often Jacobian is rebuilt (use -1 to never rebuild)
249b39c3a46SLois Curfman McInnes .  -snes_trtol <trtol> - trust region tolerance
2502492ecdbSBarry Smith .  -snes_no_convergence_test - skip convergence test in nonlinear
25182738288SBarry Smith                                solver; hence iterations will continue until max_it
2521fbbfb26SLois Curfman McInnes                                or some other criterion is reached. Saves expense
25382738288SBarry Smith                                of convergence test
254e8105e01SRichard Katz .  -snes_monitor <optional filename> - prints residual norm at each iteration. if no
255e8105e01SRichard Katz                                        filename given prints to stdout
256a6570f20SBarry Smith .  -snes_monitor_solution - plots solution at each iteration
257a6570f20SBarry Smith .  -snes_monitor_residual - plots residual (not its norm) at each iteration
258a6570f20SBarry Smith .  -snes_monitor_solution_update - plots update to solution at each iteration
259a6570f20SBarry Smith .  -snes_monitor_draw - plots residual norm at each iteration
260e24b481bSBarry Smith .  -snes_fd - use finite differences to compute Jacobian; very slow, only for testing
2615968eb51SBarry Smith .  -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration
262fee2055bSBarry Smith -  -snes_converged_reason - print the reason for convergence/divergence after each solve
26382738288SBarry Smith 
26482738288SBarry Smith     Options Database for Eisenstat-Walker method:
265fa9f3622SBarry Smith +  -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence
2664b27c08aSLois Curfman McInnes .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
26736851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
26836851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
26936851e7fSLois Curfman McInnes .  -snes_ksp_ew_gamma <gamma> - Sets gamma
27036851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha <alpha> - Sets alpha
27136851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
27236851e7fSLois Curfman McInnes -  -snes_ksp_ew_threshold <threshold> - Sets threshold
27382738288SBarry Smith 
27411ca99fdSLois Curfman McInnes    Notes:
27511ca99fdSLois Curfman McInnes    To see all options, run your program with the -help option or consult
27611ca99fdSLois Curfman McInnes    the users manual.
27783e2fdc7SBarry Smith 
27836851e7fSLois Curfman McInnes    Level: beginner
27936851e7fSLois Curfman McInnes 
2809b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database
2819b94acceSBarry Smith 
28269ed319cSSatish Balay .seealso: SNESSetOptionsPrefix()
2839b94acceSBarry Smith @*/
28463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFromOptions(SNES snes)
2859b94acceSBarry Smith {
286aa3661deSLisandro Dalcin   PetscTruth              flg,mf,mf_operator;
287aa3661deSLisandro Dalcin   PetscInt                i,indx,lag,mf_version;
288aa3661deSLisandro Dalcin   MatStructure            matflag;
28985385478SLisandro Dalcin   const char              *deft = SNESLS;
29085385478SLisandro Dalcin   const char              *convtests[] = {"default","skip"};
29185385478SLisandro Dalcin   SNESKSPEW               *kctx = NULL;
292e8105e01SRichard Katz   char                    type[256], monfilename[PETSC_MAX_PATH_LEN];
29323d894e5SBarry Smith   PetscViewerASCIIMonitor monviewer;
29485385478SLisandro Dalcin   PetscErrorCode          ierr;
2959b94acceSBarry Smith 
2963a40ed3dSBarry Smith   PetscFunctionBegin;
2970700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
298ca161407SBarry Smith 
299186905e3SBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
300cce0b1b2SLisandro Dalcin   ierr = PetscOptionsBegin(((PetscObject)snes)->comm,((PetscObject)snes)->prefix,"Nonlinear solver (SNES) options","SNES");CHKERRQ(ierr);
3017adad957SLisandro Dalcin     if (((PetscObject)snes)->type_name) { deft = ((PetscObject)snes)->type_name; }
302b0a32e0cSBarry Smith     ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr);
303d64ed03dSBarry Smith     if (flg) {
304186905e3SBarry Smith       ierr = SNESSetType(snes,type);CHKERRQ(ierr);
3057adad957SLisandro Dalcin     } else if (!((PetscObject)snes)->type_name) {
306186905e3SBarry Smith       ierr = SNESSetType(snes,deft);CHKERRQ(ierr);
307d64ed03dSBarry Smith     }
30890d69ab7SBarry Smith     /* not used here, but called so will go into help messaage */
309909c8a9fSBarry Smith     ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr);
31093c39befSBarry Smith 
31157034d6fSHong Zhang     ierr = PetscOptionsReal("-snes_stol","Stop if step length less than","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr);
31257034d6fSHong Zhang     ierr = PetscOptionsReal("-snes_atol","Stop if function norm less than","SNESSetTolerances",snes->abstol,&snes->abstol,0);CHKERRQ(ierr);
313186905e3SBarry Smith 
31457034d6fSHong Zhang     ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less than","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr);
315b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr);
316b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr);
31750ffb88aSMatthew Knepley     ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr);
318ddf469c8SBarry Smith     ierr = PetscOptionsInt("-snes_max_linear_solve_fail","Maximum failures in linear solves allowed","SNESSetMaxLinearSolveFailures",snes->maxLinearSolveFailures,&snes->maxLinearSolveFailures,PETSC_NULL);CHKERRQ(ierr);
31985385478SLisandro Dalcin 
320a8054027SBarry Smith     ierr = PetscOptionsInt("-snes_lag_preconditioner","How often to rebuild preconditioner","SNESSetLagPreconditioner",snes->lagpreconditioner,&lag,&flg);CHKERRQ(ierr);
321a8054027SBarry Smith     if (flg) {
322a8054027SBarry Smith       ierr = SNESSetLagPreconditioner(snes,lag);CHKERRQ(ierr);
323a8054027SBarry Smith     }
324e35cf81dSBarry Smith     ierr = PetscOptionsInt("-snes_lag_jacobian","How often to rebuild Jacobian","SNESSetLagJacobian",snes->lagjacobian,&lag,&flg);CHKERRQ(ierr);
325e35cf81dSBarry Smith     if (flg) {
326e35cf81dSBarry Smith       ierr = SNESSetLagJacobian(snes,lag);CHKERRQ(ierr);
327e35cf81dSBarry Smith     }
328a8054027SBarry Smith 
32985385478SLisandro Dalcin     ierr = PetscOptionsEList("-snes_convergence_test","Convergence test","SNESSetConvergenceTest",convtests,2,"default",&indx,&flg);CHKERRQ(ierr);
33085385478SLisandro Dalcin     if (flg) {
33185385478SLisandro Dalcin       switch (indx) {
3327f7931b9SBarry Smith       case 0: ierr = SNESSetConvergenceTest(snes,SNESDefaultConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); break;
3337f7931b9SBarry Smith       case 1: ierr = SNESSetConvergenceTest(snes,SNESSkipConverged,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);    break;
33485385478SLisandro Dalcin       }
33585385478SLisandro Dalcin     }
33685385478SLisandro Dalcin 
33790d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_converged_reason","Print reason for converged or diverged","SNESSolve",snes->printreason,&snes->printreason,PETSC_NULL);CHKERRQ(ierr);
338186905e3SBarry Smith 
33985385478SLisandro Dalcin     kctx = (SNESKSPEW *)snes->kspconvctx;
34085385478SLisandro Dalcin 
341fa9f3622SBarry Smith     ierr = PetscOptionsTruth("-snes_ksp_ew","Use Eisentat-Walker linear system convergence test","SNESKSPSetUseEW",snes->ksp_ewconv,&snes->ksp_ewconv,PETSC_NULL);CHKERRQ(ierr);
342186905e3SBarry Smith 
343fa9f3622SBarry Smith     ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNESKSPSetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr);
344fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNESKSPSetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr);
345fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNESKSPSetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr);
346fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNESKSPSetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr);
347fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNESKSPSetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr);
348fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNESKSPSetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr);
349fa9f3622SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNESKSPSetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr);
350186905e3SBarry Smith 
35190d69ab7SBarry Smith     flg  = PETSC_FALSE;
35290d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
353a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);}
354eabae89aSBarry Smith 
355a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor","Monitor norm of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
356e8105e01SRichard Katz     if (flg) {
357050a712dSBarry Smith       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr);
35823d894e5SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefault,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
359e8105e01SRichard Katz     }
360eabae89aSBarry Smith 
361b271bb04SBarry Smith     ierr = PetscOptionsString("-snes_monitor_range","Monitor range of elements of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
362b271bb04SBarry Smith     if (flg) {
363b271bb04SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorRange,0,0);CHKERRQ(ierr);
364b271bb04SBarry Smith     }
365b271bb04SBarry Smith 
366a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_ratiomonitor","Monitor ratios of norms of function","SNESMonitorSetRatio","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
367eabae89aSBarry Smith     if (flg) {
368050a712dSBarry Smith       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr);
369f1bef1bcSMatthew Knepley       ierr = SNESMonitorSetRatio(snes,monviewer);CHKERRQ(ierr);
370e8105e01SRichard Katz     }
371eabae89aSBarry Smith 
372a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor_short","Monitor norm of function (fewer digits)","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
373eabae89aSBarry Smith     if (flg) {
374050a712dSBarry Smith       ierr = PetscViewerASCIIMonitorCreate(((PetscObject)snes)->comm,monfilename,((PetscObject)snes)->tablevel,&monviewer);CHKERRQ(ierr);
37523d894e5SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefaultShort,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
376eabae89aSBarry Smith     }
377eabae89aSBarry Smith 
37890d69ab7SBarry Smith     flg  = PETSC_FALSE;
37990d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_monitor_solution","Plot solution at each iteration","SNESMonitorSolution",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
380a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolution,0,0);CHKERRQ(ierr);}
38190d69ab7SBarry Smith     flg  = PETSC_FALSE;
38290d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_monitor_solution_update","Plot correction at each iteration","SNESMonitorSolutionUpdate",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
383a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolutionUpdate,0,0);CHKERRQ(ierr);}
38490d69ab7SBarry Smith     flg  = PETSC_FALSE;
38590d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_monitor_residual","Plot residual at each iteration","SNESMonitorResidual",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
386a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorResidual,0,0);CHKERRQ(ierr);}
38790d69ab7SBarry Smith     flg  = PETSC_FALSE;
38890d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_monitor_draw","Plot function norm at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
389a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
39090d69ab7SBarry Smith     flg  = PETSC_FALSE;
39190d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_monitor_range_draw","Plot function range at each iteration","SNESMonitorLG",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
392b271bb04SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLGRange,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
393e24b481bSBarry Smith 
39490d69ab7SBarry Smith     flg  = PETSC_FALSE;
39590d69ab7SBarry Smith     ierr = PetscOptionsTruth("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
3964b27c08aSLois Curfman McInnes     if (flg) {
397186905e3SBarry Smith       ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr);
398ae15b995SBarry Smith       ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr);
3999b94acceSBarry Smith     }
400639f9d9dSBarry Smith 
401aa3661deSLisandro Dalcin     mf = mf_operator = PETSC_FALSE;
402aa3661deSLisandro Dalcin     flg = PETSC_FALSE;
403aa3661deSLisandro Dalcin     ierr = PetscOptionsTruth("-snes_mf_operator","Use a Matrix-Free Jacobian with user-provided preconditioner matrix","MatCreateSNESMF",PETSC_FALSE,&mf_operator,&flg);CHKERRQ(ierr);
404aa3661deSLisandro Dalcin     if (flg && mf_operator) mf = PETSC_TRUE;
405aa3661deSLisandro Dalcin     flg = PETSC_FALSE;
406aa3661deSLisandro Dalcin     ierr = PetscOptionsTruth("-snes_mf","Use a Matrix-Free Jacobian with no preconditioner matrix","MatCreateSNESMF",PETSC_FALSE,&mf,&flg);CHKERRQ(ierr);
407aa3661deSLisandro Dalcin     if (!flg && mf_operator) mf = PETSC_TRUE;
408aa3661deSLisandro Dalcin     mf_version = 1;
409aa3661deSLisandro Dalcin     ierr = PetscOptionsInt("-snes_mf_version","Matrix-Free routines version 1 or 2","None",mf_version,&mf_version,0);CHKERRQ(ierr);
410aa3661deSLisandro Dalcin 
41176b2cf59SMatthew Knepley     for(i = 0; i < numberofsetfromoptions; i++) {
41276b2cf59SMatthew Knepley       ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr);
41376b2cf59SMatthew Knepley     }
41476b2cf59SMatthew Knepley 
415e7788613SBarry Smith     if (snes->ops->setfromoptions) {
416e7788613SBarry Smith       ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr);
417639f9d9dSBarry Smith     }
418b0a32e0cSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
4194bbc92c1SBarry Smith 
420aa3661deSLisandro Dalcin   if (mf) { ierr = SNESSetUpMatrixFree_Private(snes, mf_operator, mf_version);CHKERRQ(ierr); }
4211cee3971SBarry Smith 
4221cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
423aa3661deSLisandro Dalcin   ierr = KSPGetOperators(snes->ksp,PETSC_NULL,PETSC_NULL,&matflag);
424aa3661deSLisandro Dalcin   ierr = KSPSetOperators(snes->ksp,snes->jacobian,snes->jacobian_pre,matflag);CHKERRQ(ierr);
42585385478SLisandro Dalcin   ierr = KSPSetFromOptions(snes->ksp);CHKERRQ(ierr);
42693993e2dSLois Curfman McInnes 
4273a40ed3dSBarry Smith   PetscFunctionReturn(0);
4289b94acceSBarry Smith }
4299b94acceSBarry Smith 
430a847f771SSatish Balay 
4314a2ae208SSatish Balay #undef __FUNCT__
4324a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext"
4339b94acceSBarry Smith /*@
4349b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
4359b94acceSBarry Smith    the nonlinear solvers.
4369b94acceSBarry Smith 
437fee21e36SBarry Smith    Collective on SNES
438fee21e36SBarry Smith 
439c7afd0dbSLois Curfman McInnes    Input Parameters:
440c7afd0dbSLois Curfman McInnes +  snes - the SNES context
441c7afd0dbSLois Curfman McInnes -  usrP - optional user context
442c7afd0dbSLois Curfman McInnes 
44336851e7fSLois Curfman McInnes    Level: intermediate
44436851e7fSLois Curfman McInnes 
4459b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
4469b94acceSBarry Smith 
4479b94acceSBarry Smith .seealso: SNESGetApplicationContext()
4489b94acceSBarry Smith @*/
44963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetApplicationContext(SNES snes,void *usrP)
4509b94acceSBarry Smith {
4513a40ed3dSBarry Smith   PetscFunctionBegin;
4520700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
4539b94acceSBarry Smith   snes->user		= usrP;
4543a40ed3dSBarry Smith   PetscFunctionReturn(0);
4559b94acceSBarry Smith }
45674679c65SBarry Smith 
4574a2ae208SSatish Balay #undef __FUNCT__
4584a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext"
4599b94acceSBarry Smith /*@C
4609b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
4619b94acceSBarry Smith    nonlinear solvers.
4629b94acceSBarry Smith 
463c7afd0dbSLois Curfman McInnes    Not Collective
464c7afd0dbSLois Curfman McInnes 
4659b94acceSBarry Smith    Input Parameter:
4669b94acceSBarry Smith .  snes - SNES context
4679b94acceSBarry Smith 
4689b94acceSBarry Smith    Output Parameter:
4699b94acceSBarry Smith .  usrP - user context
4709b94acceSBarry Smith 
47136851e7fSLois Curfman McInnes    Level: intermediate
47236851e7fSLois Curfman McInnes 
4739b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
4749b94acceSBarry Smith 
4759b94acceSBarry Smith .seealso: SNESSetApplicationContext()
4769b94acceSBarry Smith @*/
47763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetApplicationContext(SNES snes,void **usrP)
4789b94acceSBarry Smith {
4793a40ed3dSBarry Smith   PetscFunctionBegin;
4800700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
4819b94acceSBarry Smith   *usrP = snes->user;
4823a40ed3dSBarry Smith   PetscFunctionReturn(0);
4839b94acceSBarry Smith }
48474679c65SBarry Smith 
4854a2ae208SSatish Balay #undef __FUNCT__
4864a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber"
4879b94acceSBarry Smith /*@
488c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
489c8228a4eSBarry Smith    at this time.
4909b94acceSBarry Smith 
491c7afd0dbSLois Curfman McInnes    Not Collective
492c7afd0dbSLois Curfman McInnes 
4939b94acceSBarry Smith    Input Parameter:
4949b94acceSBarry Smith .  snes - SNES context
4959b94acceSBarry Smith 
4969b94acceSBarry Smith    Output Parameter:
4979b94acceSBarry Smith .  iter - iteration number
4989b94acceSBarry Smith 
499c8228a4eSBarry Smith    Notes:
500c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
501c8228a4eSBarry Smith 
502c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
50308405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
50408405cd6SLois Curfman McInnes .vb
50508405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
50608405cd6SLois Curfman McInnes       if (!(it % 2)) {
50708405cd6SLois Curfman McInnes         [compute Jacobian here]
50808405cd6SLois Curfman McInnes       }
50908405cd6SLois Curfman McInnes .ve
510c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
51108405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
512c8228a4eSBarry Smith 
51336851e7fSLois Curfman McInnes    Level: intermediate
51436851e7fSLois Curfman McInnes 
5152b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number,
5162b668275SBarry Smith 
517b850b91aSLisandro Dalcin .seealso:   SNESGetFunctionNorm(), SNESGetLinearSolveIterations()
5189b94acceSBarry Smith @*/
51963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetIterationNumber(SNES snes,PetscInt* iter)
5209b94acceSBarry Smith {
5213a40ed3dSBarry Smith   PetscFunctionBegin;
5220700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
5234482741eSBarry Smith   PetscValidIntPointer(iter,2);
5249b94acceSBarry Smith   *iter = snes->iter;
5253a40ed3dSBarry Smith   PetscFunctionReturn(0);
5269b94acceSBarry Smith }
52774679c65SBarry Smith 
5284a2ae208SSatish Balay #undef __FUNCT__
5294a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm"
5309b94acceSBarry Smith /*@
5319b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
5329b94acceSBarry Smith    with SNESSSetFunction().
5339b94acceSBarry Smith 
534c7afd0dbSLois Curfman McInnes    Collective on SNES
535c7afd0dbSLois Curfman McInnes 
5369b94acceSBarry Smith    Input Parameter:
5379b94acceSBarry Smith .  snes - SNES context
5389b94acceSBarry Smith 
5399b94acceSBarry Smith    Output Parameter:
5409b94acceSBarry Smith .  fnorm - 2-norm of function
5419b94acceSBarry Smith 
54236851e7fSLois Curfman McInnes    Level: intermediate
54336851e7fSLois Curfman McInnes 
5449b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
545a86d99e1SLois Curfman McInnes 
546b850b91aSLisandro Dalcin .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetLinearSolveIterations()
5479b94acceSBarry Smith @*/
54871f87433Sdalcinl PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunctionNorm(SNES snes,PetscReal *fnorm)
5499b94acceSBarry Smith {
5503a40ed3dSBarry Smith   PetscFunctionBegin;
5510700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
5524482741eSBarry Smith   PetscValidScalarPointer(fnorm,2);
5539b94acceSBarry Smith   *fnorm = snes->norm;
5543a40ed3dSBarry Smith   PetscFunctionReturn(0);
5559b94acceSBarry Smith }
55674679c65SBarry Smith 
5574a2ae208SSatish Balay #undef __FUNCT__
558b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetNonlinearStepFailures"
5599b94acceSBarry Smith /*@
560b850b91aSLisandro Dalcin    SNESGetNonlinearStepFailures - Gets the number of unsuccessful steps
5619b94acceSBarry Smith    attempted by the nonlinear solver.
5629b94acceSBarry Smith 
563c7afd0dbSLois Curfman McInnes    Not Collective
564c7afd0dbSLois Curfman McInnes 
5659b94acceSBarry Smith    Input Parameter:
5669b94acceSBarry Smith .  snes - SNES context
5679b94acceSBarry Smith 
5689b94acceSBarry Smith    Output Parameter:
5699b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
5709b94acceSBarry Smith 
571c96a6f78SLois Curfman McInnes    Notes:
572c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
573c96a6f78SLois Curfman McInnes 
57436851e7fSLois Curfman McInnes    Level: intermediate
57536851e7fSLois Curfman McInnes 
5769b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
57758ebbce7SBarry Smith 
578e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
57958ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetMaxNonlinearStepFailures()
5809b94acceSBarry Smith @*/
581b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNonlinearStepFailures(SNES snes,PetscInt* nfails)
5829b94acceSBarry Smith {
5833a40ed3dSBarry Smith   PetscFunctionBegin;
5840700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
5854482741eSBarry Smith   PetscValidIntPointer(nfails,2);
58650ffb88aSMatthew Knepley   *nfails = snes->numFailures;
58750ffb88aSMatthew Knepley   PetscFunctionReturn(0);
58850ffb88aSMatthew Knepley }
58950ffb88aSMatthew Knepley 
59050ffb88aSMatthew Knepley #undef __FUNCT__
591b850b91aSLisandro Dalcin #define __FUNCT__ "SNESSetMaxNonlinearStepFailures"
59250ffb88aSMatthew Knepley /*@
593b850b91aSLisandro Dalcin    SNESSetMaxNonlinearStepFailures - Sets the maximum number of unsuccessful steps
59450ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
59550ffb88aSMatthew Knepley 
59650ffb88aSMatthew Knepley    Not Collective
59750ffb88aSMatthew Knepley 
59850ffb88aSMatthew Knepley    Input Parameters:
59950ffb88aSMatthew Knepley +  snes     - SNES context
60050ffb88aSMatthew Knepley -  maxFails - maximum of unsuccessful steps
60150ffb88aSMatthew Knepley 
60250ffb88aSMatthew Knepley    Level: intermediate
60350ffb88aSMatthew Knepley 
60450ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
60558ebbce7SBarry Smith 
606e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
60758ebbce7SBarry Smith           SNESGetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
60850ffb88aSMatthew Knepley @*/
609b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxNonlinearStepFailures(SNES snes, PetscInt maxFails)
61050ffb88aSMatthew Knepley {
61150ffb88aSMatthew Knepley   PetscFunctionBegin;
6120700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
61350ffb88aSMatthew Knepley   snes->maxFailures = maxFails;
61450ffb88aSMatthew Knepley   PetscFunctionReturn(0);
61550ffb88aSMatthew Knepley }
61650ffb88aSMatthew Knepley 
61750ffb88aSMatthew Knepley #undef __FUNCT__
618b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetMaxNonlinearStepFailures"
61950ffb88aSMatthew Knepley /*@
620b850b91aSLisandro Dalcin    SNESGetMaxNonlinearStepFailures - Gets the maximum number of unsuccessful steps
62150ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
62250ffb88aSMatthew Knepley 
62350ffb88aSMatthew Knepley    Not Collective
62450ffb88aSMatthew Knepley 
62550ffb88aSMatthew Knepley    Input Parameter:
62650ffb88aSMatthew Knepley .  snes     - SNES context
62750ffb88aSMatthew Knepley 
62850ffb88aSMatthew Knepley    Output Parameter:
62950ffb88aSMatthew Knepley .  maxFails - maximum of unsuccessful steps
63050ffb88aSMatthew Knepley 
63150ffb88aSMatthew Knepley    Level: intermediate
63250ffb88aSMatthew Knepley 
63350ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
63458ebbce7SBarry Smith 
635e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures(),
63658ebbce7SBarry Smith           SNESSetMaxNonlinearStepFailures(), SNESGetNonlinearStepFailures()
63758ebbce7SBarry Smith 
63850ffb88aSMatthew Knepley @*/
639b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxNonlinearStepFailures(SNES snes, PetscInt *maxFails)
64050ffb88aSMatthew Knepley {
64150ffb88aSMatthew Knepley   PetscFunctionBegin;
6420700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
6434482741eSBarry Smith   PetscValidIntPointer(maxFails,2);
64450ffb88aSMatthew Knepley   *maxFails = snes->maxFailures;
6453a40ed3dSBarry Smith   PetscFunctionReturn(0);
6469b94acceSBarry Smith }
647a847f771SSatish Balay 
6484a2ae208SSatish Balay #undef __FUNCT__
6492541af92SBarry Smith #define __FUNCT__ "SNESGetNumberFunctionEvals"
6502541af92SBarry Smith /*@
6512541af92SBarry Smith    SNESGetNumberFunctionEvals - Gets the number of user provided function evaluations
6522541af92SBarry Smith      done by SNES.
6532541af92SBarry Smith 
6542541af92SBarry Smith    Not Collective
6552541af92SBarry Smith 
6562541af92SBarry Smith    Input Parameter:
6572541af92SBarry Smith .  snes     - SNES context
6582541af92SBarry Smith 
6592541af92SBarry Smith    Output Parameter:
6602541af92SBarry Smith .  nfuncs - number of evaluations
6612541af92SBarry Smith 
6622541af92SBarry Smith    Level: intermediate
6632541af92SBarry Smith 
6642541af92SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
66558ebbce7SBarry Smith 
666e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(), SNESGetLinearSolveFailures()
6672541af92SBarry Smith @*/
6682541af92SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNumberFunctionEvals(SNES snes, PetscInt *nfuncs)
6692541af92SBarry Smith {
6702541af92SBarry Smith   PetscFunctionBegin;
6710700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
6722541af92SBarry Smith   PetscValidIntPointer(nfuncs,2);
6732541af92SBarry Smith   *nfuncs = snes->nfuncs;
6742541af92SBarry Smith   PetscFunctionReturn(0);
6752541af92SBarry Smith }
6762541af92SBarry Smith 
6772541af92SBarry Smith #undef __FUNCT__
6783d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures"
6793d4c4710SBarry Smith /*@
6803d4c4710SBarry Smith    SNESGetLinearSolveFailures - Gets the number of failed (non-converged)
6813d4c4710SBarry Smith    linear solvers.
6823d4c4710SBarry Smith 
6833d4c4710SBarry Smith    Not Collective
6843d4c4710SBarry Smith 
6853d4c4710SBarry Smith    Input Parameter:
6863d4c4710SBarry Smith .  snes - SNES context
6873d4c4710SBarry Smith 
6883d4c4710SBarry Smith    Output Parameter:
6893d4c4710SBarry Smith .  nfails - number of failed solves
6903d4c4710SBarry Smith 
6913d4c4710SBarry Smith    Notes:
6923d4c4710SBarry Smith    This counter is reset to zero for each successive call to SNESSolve().
6933d4c4710SBarry Smith 
6943d4c4710SBarry Smith    Level: intermediate
6953d4c4710SBarry Smith 
6963d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
69758ebbce7SBarry Smith 
698e1c61ce8SBarry Smith .seealso: SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures()
6993d4c4710SBarry Smith @*/
7003d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails)
7013d4c4710SBarry Smith {
7023d4c4710SBarry Smith   PetscFunctionBegin;
7030700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7043d4c4710SBarry Smith   PetscValidIntPointer(nfails,2);
7053d4c4710SBarry Smith   *nfails = snes->numLinearSolveFailures;
7063d4c4710SBarry Smith   PetscFunctionReturn(0);
7073d4c4710SBarry Smith }
7083d4c4710SBarry Smith 
7093d4c4710SBarry Smith #undef __FUNCT__
7103d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures"
7113d4c4710SBarry Smith /*@
7123d4c4710SBarry Smith    SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts
7133d4c4710SBarry Smith    allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE
7143d4c4710SBarry Smith 
7153d4c4710SBarry Smith    Collective on SNES
7163d4c4710SBarry Smith 
7173d4c4710SBarry Smith    Input Parameters:
7183d4c4710SBarry Smith +  snes     - SNES context
7193d4c4710SBarry Smith -  maxFails - maximum allowed linear solve failures
7203d4c4710SBarry Smith 
7213d4c4710SBarry Smith    Level: intermediate
7223d4c4710SBarry Smith 
723a6796414SBarry Smith    Notes: By default this is 0; that is SNES returns on the first failed linear solve
7243d4c4710SBarry Smith 
7253d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
7263d4c4710SBarry Smith 
72758ebbce7SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures(), SNESGetLinearSolveIterations()
7283d4c4710SBarry Smith @*/
7293d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails)
7303d4c4710SBarry Smith {
7313d4c4710SBarry Smith   PetscFunctionBegin;
7320700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7333d4c4710SBarry Smith   snes->maxLinearSolveFailures = maxFails;
7343d4c4710SBarry Smith   PetscFunctionReturn(0);
7353d4c4710SBarry Smith }
7363d4c4710SBarry Smith 
7373d4c4710SBarry Smith #undef __FUNCT__
7383d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures"
7393d4c4710SBarry Smith /*@
7403d4c4710SBarry Smith    SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that
7413d4c4710SBarry Smith      are allowed before SNES terminates
7423d4c4710SBarry Smith 
7433d4c4710SBarry Smith    Not Collective
7443d4c4710SBarry Smith 
7453d4c4710SBarry Smith    Input Parameter:
7463d4c4710SBarry Smith .  snes     - SNES context
7473d4c4710SBarry Smith 
7483d4c4710SBarry Smith    Output Parameter:
7493d4c4710SBarry Smith .  maxFails - maximum of unsuccessful solves allowed
7503d4c4710SBarry Smith 
7513d4c4710SBarry Smith    Level: intermediate
7523d4c4710SBarry Smith 
7533d4c4710SBarry Smith    Notes: By default this is 1; that is SNES returns on the first failed linear solve
7543d4c4710SBarry Smith 
7553d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
7563d4c4710SBarry Smith 
757e1c61ce8SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetLinearSolveIterations(), SNESSetMaxLinearSolveFailures(),
7583d4c4710SBarry Smith @*/
7593d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails)
7603d4c4710SBarry Smith {
7613d4c4710SBarry Smith   PetscFunctionBegin;
7620700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7633d4c4710SBarry Smith   PetscValidIntPointer(maxFails,2);
7643d4c4710SBarry Smith   *maxFails = snes->maxLinearSolveFailures;
7653d4c4710SBarry Smith   PetscFunctionReturn(0);
7663d4c4710SBarry Smith }
7673d4c4710SBarry Smith 
7683d4c4710SBarry Smith #undef __FUNCT__
769b850b91aSLisandro Dalcin #define __FUNCT__ "SNESGetLinearSolveIterations"
770c96a6f78SLois Curfman McInnes /*@
771b850b91aSLisandro Dalcin    SNESGetLinearSolveIterations - Gets the total number of linear iterations
772c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
773c96a6f78SLois Curfman McInnes 
774c7afd0dbSLois Curfman McInnes    Not Collective
775c7afd0dbSLois Curfman McInnes 
776c96a6f78SLois Curfman McInnes    Input Parameter:
777c96a6f78SLois Curfman McInnes .  snes - SNES context
778c96a6f78SLois Curfman McInnes 
779c96a6f78SLois Curfman McInnes    Output Parameter:
780c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
781c96a6f78SLois Curfman McInnes 
782c96a6f78SLois Curfman McInnes    Notes:
783c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
784c96a6f78SLois Curfman McInnes 
78536851e7fSLois Curfman McInnes    Level: intermediate
78636851e7fSLois Curfman McInnes 
787c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
7882b668275SBarry Smith 
7898c7482ecSBarry Smith .seealso:  SNESGetIterationNumber(), SNESGetFunctionNorm(), SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures()
790c96a6f78SLois Curfman McInnes @*/
791b850b91aSLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveIterations(SNES snes,PetscInt* lits)
792c96a6f78SLois Curfman McInnes {
7933a40ed3dSBarry Smith   PetscFunctionBegin;
7940700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
7954482741eSBarry Smith   PetscValidIntPointer(lits,2);
796c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
7973a40ed3dSBarry Smith   PetscFunctionReturn(0);
798c96a6f78SLois Curfman McInnes }
799c96a6f78SLois Curfman McInnes 
8004a2ae208SSatish Balay #undef __FUNCT__
80194b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP"
80252baeb72SSatish Balay /*@
80394b7f48cSBarry Smith    SNESGetKSP - Returns the KSP context for a SNES solver.
8049b94acceSBarry Smith 
80594b7f48cSBarry Smith    Not Collective, but if SNES object is parallel, then KSP object is parallel
806c7afd0dbSLois Curfman McInnes 
8079b94acceSBarry Smith    Input Parameter:
8089b94acceSBarry Smith .  snes - the SNES context
8099b94acceSBarry Smith 
8109b94acceSBarry Smith    Output Parameter:
81194b7f48cSBarry Smith .  ksp - the KSP context
8129b94acceSBarry Smith 
8139b94acceSBarry Smith    Notes:
81494b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
8159b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
8162999313aSBarry Smith    PC contexts as well.
8179b94acceSBarry Smith 
81836851e7fSLois Curfman McInnes    Level: beginner
81936851e7fSLois Curfman McInnes 
82094b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
8219b94acceSBarry Smith 
8222999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
8239b94acceSBarry Smith @*/
82463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetKSP(SNES snes,KSP *ksp)
8259b94acceSBarry Smith {
8261cee3971SBarry Smith   PetscErrorCode ierr;
8271cee3971SBarry Smith 
8283a40ed3dSBarry Smith   PetscFunctionBegin;
8290700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8304482741eSBarry Smith   PetscValidPointer(ksp,2);
8311cee3971SBarry Smith 
8321cee3971SBarry Smith   if (!snes->ksp) {
8331cee3971SBarry Smith     ierr = KSPCreate(((PetscObject)snes)->comm,&snes->ksp);CHKERRQ(ierr);
8341cee3971SBarry Smith     ierr = PetscObjectIncrementTabLevel((PetscObject)snes->ksp,(PetscObject)snes,1);CHKERRQ(ierr);
8351cee3971SBarry Smith     ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr);
8361cee3971SBarry Smith   }
83794b7f48cSBarry Smith   *ksp = snes->ksp;
8383a40ed3dSBarry Smith   PetscFunctionReturn(0);
8399b94acceSBarry Smith }
84082bf6240SBarry Smith 
8414a2ae208SSatish Balay #undef __FUNCT__
8422999313aSBarry Smith #define __FUNCT__ "SNESSetKSP"
8432999313aSBarry Smith /*@
8442999313aSBarry Smith    SNESSetKSP - Sets a KSP context for the SNES object to use
8452999313aSBarry Smith 
8462999313aSBarry Smith    Not Collective, but the SNES and KSP objects must live on the same MPI_Comm
8472999313aSBarry Smith 
8482999313aSBarry Smith    Input Parameters:
8492999313aSBarry Smith +  snes - the SNES context
8502999313aSBarry Smith -  ksp - the KSP context
8512999313aSBarry Smith 
8522999313aSBarry Smith    Notes:
8532999313aSBarry Smith    The SNES object already has its KSP object, you can obtain with SNESGetKSP()
8542999313aSBarry Smith    so this routine is rarely needed.
8552999313aSBarry Smith 
8562999313aSBarry Smith    The KSP object that is already in the SNES object has its reference count
8572999313aSBarry Smith    decreased by one.
8582999313aSBarry Smith 
8592999313aSBarry Smith    Level: developer
8602999313aSBarry Smith 
8612999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
8622999313aSBarry Smith 
8632999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
8642999313aSBarry Smith @*/
8652999313aSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetKSP(SNES snes,KSP ksp)
8662999313aSBarry Smith {
8672999313aSBarry Smith   PetscErrorCode ierr;
8682999313aSBarry Smith 
8692999313aSBarry Smith   PetscFunctionBegin;
8700700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
8710700a824SBarry Smith   PetscValidHeaderSpecific(ksp,KSP_CLASSID,2);
8722999313aSBarry Smith   PetscCheckSameComm(snes,1,ksp,2);
8737dcf0eaaSdalcinl   ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr);
874906ed7ccSBarry Smith   if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);}
8752999313aSBarry Smith   snes->ksp = ksp;
8762999313aSBarry Smith   PetscFunctionReturn(0);
8772999313aSBarry Smith }
8782999313aSBarry Smith 
8797adad957SLisandro Dalcin #if 0
8802999313aSBarry Smith #undef __FUNCT__
8814a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc"
8826849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj)
883e24b481bSBarry Smith {
884e24b481bSBarry Smith   PetscFunctionBegin;
885e24b481bSBarry Smith   PetscFunctionReturn(0);
886e24b481bSBarry Smith }
8877adad957SLisandro Dalcin #endif
888e24b481bSBarry Smith 
8899b94acceSBarry Smith /* -----------------------------------------------------------*/
8904a2ae208SSatish Balay #undef __FUNCT__
8914a2ae208SSatish Balay #define __FUNCT__ "SNESCreate"
89252baeb72SSatish Balay /*@
8939b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
8949b94acceSBarry Smith 
895c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
896c7afd0dbSLois Curfman McInnes 
897c7afd0dbSLois Curfman McInnes    Input Parameters:
898906ed7ccSBarry Smith .  comm - MPI communicator
8999b94acceSBarry Smith 
9009b94acceSBarry Smith    Output Parameter:
9019b94acceSBarry Smith .  outsnes - the new SNES context
9029b94acceSBarry Smith 
903c7afd0dbSLois Curfman McInnes    Options Database Keys:
904c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
905c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
906c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
907c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
908c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
909c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
910c1f60f51SBarry Smith 
91136851e7fSLois Curfman McInnes    Level: beginner
91236851e7fSLois Curfman McInnes 
9139b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
9149b94acceSBarry Smith 
915a8054027SBarry Smith .seealso: SNESSolve(), SNESDestroy(), SNES, SNESSetLagPreconditioner()
916a8054027SBarry Smith 
9179b94acceSBarry Smith @*/
91863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESCreate(MPI_Comm comm,SNES *outsnes)
9199b94acceSBarry Smith {
920dfbe8321SBarry Smith   PetscErrorCode      ierr;
9219b94acceSBarry Smith   SNES                snes;
922fa9f3622SBarry Smith   SNESKSPEW           *kctx;
92337fcc0dbSBarry Smith 
9243a40ed3dSBarry Smith   PetscFunctionBegin;
925ed1caa07SMatthew Knepley   PetscValidPointer(outsnes,2);
9268ba1e511SMatthew Knepley   *outsnes = PETSC_NULL;
9278ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
9288ba1e511SMatthew Knepley   ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr);
9298ba1e511SMatthew Knepley #endif
9308ba1e511SMatthew Knepley 
9310700a824SBarry Smith   ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_CLASSID,0,"SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr);
9327adad957SLisandro Dalcin 
93385385478SLisandro Dalcin   snes->ops->converged    = SNESDefaultConverged;
9349b94acceSBarry Smith   snes->max_its           = 50;
9359750a799SBarry Smith   snes->max_funcs	  = 10000;
9369b94acceSBarry Smith   snes->norm		  = 0.0;
937b4874afaSBarry Smith   snes->rtol		  = 1.e-8;
938b4874afaSBarry Smith   snes->ttol              = 0.0;
93970441072SBarry Smith   snes->abstol		  = 1.e-50;
9409b94acceSBarry Smith   snes->xtol		  = 1.e-8;
9414b27c08aSLois Curfman McInnes   snes->deltatol	  = 1.e-12;
9429b94acceSBarry Smith   snes->nfuncs            = 0;
94350ffb88aSMatthew Knepley   snes->numFailures       = 0;
94450ffb88aSMatthew Knepley   snes->maxFailures       = 1;
9457a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
946e35cf81dSBarry Smith   snes->lagjacobian       = 1;
947a8054027SBarry Smith   snes->lagpreconditioner = 1;
948639f9d9dSBarry Smith   snes->numbermonitors    = 0;
9499b94acceSBarry Smith   snes->data              = 0;
9504dc4c822SBarry Smith   snes->setupcalled       = PETSC_FALSE;
951186905e3SBarry Smith   snes->ksp_ewconv        = PETSC_FALSE;
9526f24a144SLois Curfman McInnes   snes->vwork             = 0;
9536f24a144SLois Curfman McInnes   snes->nwork             = 0;
954758f92a0SBarry Smith   snes->conv_hist_len     = 0;
955758f92a0SBarry Smith   snes->conv_hist_max     = 0;
956758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
957758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
958758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
959184914b5SBarry Smith   snes->reason            = SNES_CONVERGED_ITERATING;
9609b94acceSBarry Smith 
9613d4c4710SBarry Smith   snes->numLinearSolveFailures = 0;
9623d4c4710SBarry Smith   snes->maxLinearSolveFailures = 1;
9633d4c4710SBarry Smith 
9649b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
96538f2d2fdSLisandro Dalcin   ierr = PetscNewLog(snes,SNESKSPEW,&kctx);CHKERRQ(ierr);
9669b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
9679b94acceSBarry Smith   kctx->version     = 2;
9689b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
9699b94acceSBarry Smith                              this was too large for some test cases */
97075567043SBarry Smith   kctx->rtol_last   = 0.0;
9719b94acceSBarry Smith   kctx->rtol_max    = .9;
9729b94acceSBarry Smith   kctx->gamma       = 1.0;
97371f87433Sdalcinl   kctx->alpha       = .5*(1.0 + sqrt(5.0));
97471f87433Sdalcinl   kctx->alpha2      = kctx->alpha;
9759b94acceSBarry Smith   kctx->threshold   = .1;
97675567043SBarry Smith   kctx->lresid_last = 0.0;
97775567043SBarry Smith   kctx->norm_last   = 0.0;
9789b94acceSBarry Smith 
9799b94acceSBarry Smith   *outsnes = snes;
98000036973SBarry Smith   ierr = PetscPublishAll(snes);CHKERRQ(ierr);
9813a40ed3dSBarry Smith   PetscFunctionReturn(0);
9829b94acceSBarry Smith }
9839b94acceSBarry Smith 
9844a2ae208SSatish Balay #undef __FUNCT__
9854a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction"
9869b94acceSBarry Smith /*@C
9879b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
9889b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
9899b94acceSBarry Smith    equations.
9909b94acceSBarry Smith 
991fee21e36SBarry Smith    Collective on SNES
992fee21e36SBarry Smith 
993c7afd0dbSLois Curfman McInnes    Input Parameters:
994c7afd0dbSLois Curfman McInnes +  snes - the SNES context
995c7afd0dbSLois Curfman McInnes .  r - vector to store function value
996de044059SHong Zhang .  func - function evaluation routine
997c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
998c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
9999b94acceSBarry Smith 
1000c7afd0dbSLois Curfman McInnes    Calling sequence of func:
10018d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
1002c7afd0dbSLois Curfman McInnes 
1003313e4042SLois Curfman McInnes .  f - function vector
1004c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
10059b94acceSBarry Smith 
10069b94acceSBarry Smith    Notes:
10079b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
10089b94acceSBarry Smith $      f'(x) x = -f(x),
1009c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
10109b94acceSBarry Smith 
101136851e7fSLois Curfman McInnes    Level: beginner
101236851e7fSLois Curfman McInnes 
10139b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
10149b94acceSBarry Smith 
1015a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
10169b94acceSBarry Smith @*/
101763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx)
10189b94acceSBarry Smith {
101985385478SLisandro Dalcin   PetscErrorCode ierr;
10203a40ed3dSBarry Smith   PetscFunctionBegin;
10210700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1022ef8dffc7SBarry Smith   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
1023ef8dffc7SBarry Smith   if (r) PetscCheckSameComm(snes,1,r,2);
1024ef8dffc7SBarry Smith   if (!r && snes->dm) {
1025ef8dffc7SBarry Smith     ierr = DMCreateGlobalVector(snes->dm,&r);CHKERRQ(ierr);
1026ef8dffc7SBarry Smith   } else {
102785385478SLisandro Dalcin     ierr = PetscObjectReference((PetscObject)r);CHKERRQ(ierr);
1028ef8dffc7SBarry Smith   }
102985385478SLisandro Dalcin   if (snes->vec_func) { ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr); }
1030e7788613SBarry Smith   snes->ops->computefunction = func;
103185385478SLisandro Dalcin   snes->vec_func             = r;
10329b94acceSBarry Smith   snes->funP                 = ctx;
10333a40ed3dSBarry Smith   PetscFunctionReturn(0);
10349b94acceSBarry Smith }
10359b94acceSBarry Smith 
10363ab0aad5SBarry Smith /* --------------------------------------------------------------- */
10373ab0aad5SBarry Smith #undef __FUNCT__
10381096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs"
10391096aae1SMatthew Knepley /*@C
10401096aae1SMatthew Knepley    SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set
10411096aae1SMatthew Knepley    it assumes a zero right hand side.
10421096aae1SMatthew Knepley 
10431096aae1SMatthew Knepley    Collective on SNES
10441096aae1SMatthew Knepley 
10451096aae1SMatthew Knepley    Input Parameter:
10461096aae1SMatthew Knepley .  snes - the SNES context
10471096aae1SMatthew Knepley 
10481096aae1SMatthew Knepley    Output Parameter:
1049bc08b0f1SBarry Smith .  rhs - the right hand side vector or PETSC_NULL if the right hand side vector is null
10501096aae1SMatthew Knepley 
10511096aae1SMatthew Knepley    Level: intermediate
10521096aae1SMatthew Knepley 
10531096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side
10541096aae1SMatthew Knepley 
105585385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
10561096aae1SMatthew Knepley @*/
10571096aae1SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetRhs(SNES snes,Vec *rhs)
10581096aae1SMatthew Knepley {
10591096aae1SMatthew Knepley   PetscFunctionBegin;
10600700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
10611096aae1SMatthew Knepley   PetscValidPointer(rhs,2);
106285385478SLisandro Dalcin   *rhs = snes->vec_rhs;
10631096aae1SMatthew Knepley   PetscFunctionReturn(0);
10641096aae1SMatthew Knepley }
10651096aae1SMatthew Knepley 
10661096aae1SMatthew Knepley #undef __FUNCT__
10674a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction"
10689b94acceSBarry Smith /*@
106936851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
10709b94acceSBarry Smith                          SNESSetFunction().
10719b94acceSBarry Smith 
1072c7afd0dbSLois Curfman McInnes    Collective on SNES
1073c7afd0dbSLois Curfman McInnes 
10749b94acceSBarry Smith    Input Parameters:
1075c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1076c7afd0dbSLois Curfman McInnes -  x - input vector
10779b94acceSBarry Smith 
10789b94acceSBarry Smith    Output Parameter:
10793638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
10809b94acceSBarry Smith 
10811bffabb2SLois Curfman McInnes    Notes:
108236851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
108336851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
108436851e7fSLois Curfman McInnes    themselves.
108536851e7fSLois Curfman McInnes 
108636851e7fSLois Curfman McInnes    Level: developer
108736851e7fSLois Curfman McInnes 
10889b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
10899b94acceSBarry Smith 
1090a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
10919b94acceSBarry Smith @*/
109263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeFunction(SNES snes,Vec x,Vec y)
10939b94acceSBarry Smith {
1094dfbe8321SBarry Smith   PetscErrorCode ierr;
10959b94acceSBarry Smith 
10963a40ed3dSBarry Smith   PetscFunctionBegin;
10970700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
10980700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,2);
10990700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
1100c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,x,2);
1101c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,3);
1102184914b5SBarry Smith 
1103d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
1104e7788613SBarry Smith   if (snes->ops->computefunction) {
1105d64ed03dSBarry Smith     PetscStackPush("SNES user function");
1106e9a2bbcdSBarry Smith     CHKMEMQ;
1107e7788613SBarry Smith     ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP);
1108e9a2bbcdSBarry Smith     CHKMEMQ;
1109d64ed03dSBarry Smith     PetscStackPop;
1110d5e45103SBarry Smith     if (PetscExceptionValue(ierr)) {
111119717074SBarry Smith       PetscErrorCode pierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(pierr);
111219717074SBarry Smith     }
1113d5e45103SBarry Smith     CHKERRQ(ierr);
111485385478SLisandro Dalcin   } else if (snes->vec_rhs) {
11151096aae1SMatthew Knepley     ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr);
11161096aae1SMatthew Knepley   } else {
11171096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() before SNESComputeFunction(), likely called from SNESSolve().");
11181096aae1SMatthew Knepley   }
111985385478SLisandro Dalcin   if (snes->vec_rhs) {
112085385478SLisandro Dalcin     ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr);
11213ab0aad5SBarry Smith   }
1122ae3c334cSLois Curfman McInnes   snes->nfuncs++;
1123d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
11243a40ed3dSBarry Smith   PetscFunctionReturn(0);
11259b94acceSBarry Smith }
11269b94acceSBarry Smith 
11274a2ae208SSatish Balay #undef __FUNCT__
11284a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian"
112962fef451SLois Curfman McInnes /*@
113062fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
113162fef451SLois Curfman McInnes    set with SNESSetJacobian().
113262fef451SLois Curfman McInnes 
1133c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1134c7afd0dbSLois Curfman McInnes 
113562fef451SLois Curfman McInnes    Input Parameters:
1136c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1137c7afd0dbSLois Curfman McInnes -  x - input vector
113862fef451SLois Curfman McInnes 
113962fef451SLois Curfman McInnes    Output Parameters:
1140c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
114162fef451SLois Curfman McInnes .  B - optional preconditioning matrix
11422b668275SBarry Smith -  flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER)
1143fee21e36SBarry Smith 
1144e35cf81dSBarry Smith   Options Database Keys:
1145e35cf81dSBarry Smith +    -snes_lag_preconditioner <lag>
1146e35cf81dSBarry Smith -    -snes_lag_jacobian <lag>
1147e35cf81dSBarry Smith 
114862fef451SLois Curfman McInnes    Notes:
114962fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
115062fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
115162fef451SLois Curfman McInnes 
115294b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
1153dc5a77f8SLois Curfman McInnes    flag parameter.
115462fef451SLois Curfman McInnes 
115536851e7fSLois Curfman McInnes    Level: developer
115636851e7fSLois Curfman McInnes 
115762fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
115862fef451SLois Curfman McInnes 
1159e35cf81dSBarry Smith .seealso:  SNESSetJacobian(), KSPSetOperators(), MatStructure, SNESSetLagPreconditioner(), SNESSetLagJacobian()
116062fef451SLois Curfman McInnes @*/
116163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
11629b94acceSBarry Smith {
1163dfbe8321SBarry Smith   PetscErrorCode ierr;
1164ebd3b9afSBarry Smith   PetscTruth     flag;
11653a40ed3dSBarry Smith 
11663a40ed3dSBarry Smith   PetscFunctionBegin;
11670700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
11680700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
11694482741eSBarry Smith   PetscValidPointer(flg,5);
1170c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,X,2);
1171e7788613SBarry Smith   if (!snes->ops->computejacobian) PetscFunctionReturn(0);
1172ebd3b9afSBarry Smith 
1173ebd3b9afSBarry Smith   /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */
1174ebd3b9afSBarry Smith 
1175fe3ffe1eSBarry Smith   if (snes->lagjacobian == -2) {
1176fe3ffe1eSBarry Smith     snes->lagjacobian = -1;
1177fe3ffe1eSBarry Smith     ierr = PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n");CHKERRQ(ierr);
1178fe3ffe1eSBarry Smith   } else if (snes->lagjacobian == -1) {
1179e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1180e35cf81dSBarry Smith     ierr = PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");CHKERRQ(ierr);
1181ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1182ebd3b9afSBarry Smith     if (flag) {
1183ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1184ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1185ebd3b9afSBarry Smith     }
1186e35cf81dSBarry Smith     PetscFunctionReturn(0);
1187e35cf81dSBarry Smith   } else if (snes->lagjacobian > 1 && snes->iter % snes->lagjacobian) {
1188e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1189e35cf81dSBarry Smith     ierr = PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);CHKERRQ(ierr);
1190ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1191ebd3b9afSBarry Smith     if (flag) {
1192ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1193ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1194ebd3b9afSBarry Smith     }
1195e35cf81dSBarry Smith     PetscFunctionReturn(0);
1196e35cf81dSBarry Smith   }
1197e35cf81dSBarry Smith 
1198c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1199e35cf81dSBarry Smith   ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1200d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
1201dc67937bSBarry Smith   CHKMEMQ;
1202e7788613SBarry Smith   ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
1203dc67937bSBarry Smith   CHKMEMQ;
1204d64ed03dSBarry Smith   PetscStackPop;
1205d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1206a8054027SBarry Smith 
12073b4f5425SBarry Smith   if (snes->lagpreconditioner == -2) {
12083b4f5425SBarry Smith     ierr = PetscInfo(snes,"Rebuilding preconditioner exactly once since lag is -2\n");CHKERRQ(ierr);
12093b4f5425SBarry Smith     snes->lagpreconditioner = -1;
12103b4f5425SBarry Smith   } else if (snes->lagpreconditioner == -1) {
1211a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1212a8054027SBarry Smith     ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr);
1213a8054027SBarry Smith   } else if (snes->lagpreconditioner > 1 && snes->iter % snes->lagpreconditioner) {
1214a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1215a8054027SBarry Smith     ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr);
1216a8054027SBarry Smith   }
1217a8054027SBarry Smith 
12186d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
12190700a824SBarry Smith   /* PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
12200700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,4);   */
12213a40ed3dSBarry Smith   PetscFunctionReturn(0);
12229b94acceSBarry Smith }
12239b94acceSBarry Smith 
12244a2ae208SSatish Balay #undef __FUNCT__
12254a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian"
12269b94acceSBarry Smith /*@C
12279b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1228044dda88SLois Curfman McInnes    location to store the matrix.
12299b94acceSBarry Smith 
1230c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1231c7afd0dbSLois Curfman McInnes 
12329b94acceSBarry Smith    Input Parameters:
1233c7afd0dbSLois Curfman McInnes +  snes - the SNES context
12349b94acceSBarry Smith .  A - Jacobian matrix
12359b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
12369b94acceSBarry Smith .  func - Jacobian evaluation routine
1237c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
12382cd2dfdcSLois Curfman McInnes          Jacobian evaluation routine (may be PETSC_NULL)
12399b94acceSBarry Smith 
12409b94acceSBarry Smith    Calling sequence of func:
12418d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
12429b94acceSBarry Smith 
1243c7afd0dbSLois Curfman McInnes +  x - input vector
12449b94acceSBarry Smith .  A - Jacobian matrix
12459b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1246ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
12472b668275SBarry Smith    structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER
1248c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
12499b94acceSBarry Smith 
12509b94acceSBarry Smith    Notes:
125194b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
12522cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1253ac21db08SLois Curfman McInnes 
1254ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
12559b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
12569b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
12579b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
12589b94acceSBarry Smith    throughout the global iterations.
12599b94acceSBarry Smith 
126016913363SBarry Smith    If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on
126116913363SBarry Smith    each matrix.
126216913363SBarry Smith 
126336851e7fSLois Curfman McInnes    Level: beginner
126436851e7fSLois Curfman McInnes 
12659b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
12669b94acceSBarry Smith 
12673ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure
12689b94acceSBarry Smith @*/
126963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
12709b94acceSBarry Smith {
1271dfbe8321SBarry Smith   PetscErrorCode ierr;
12723a7fca6bSBarry Smith 
12733a40ed3dSBarry Smith   PetscFunctionBegin;
12740700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
12750700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
12760700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
1277c9780b6fSBarry Smith   if (A) PetscCheckSameComm(snes,1,A,2);
127806975374SJed Brown   if (B) PetscCheckSameComm(snes,1,B,3);
1279e7788613SBarry Smith   if (func) snes->ops->computejacobian = func;
12803a7fca6bSBarry Smith   if (ctx)  snes->jacP                 = ctx;
12813a7fca6bSBarry Smith   if (A) {
12827dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
12833a7fca6bSBarry Smith     if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
12849b94acceSBarry Smith     snes->jacobian = A;
12853a7fca6bSBarry Smith   }
12863a7fca6bSBarry Smith   if (B) {
12877dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
12883a7fca6bSBarry Smith     if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
12899b94acceSBarry Smith     snes->jacobian_pre = B;
12903a7fca6bSBarry Smith   }
12913a40ed3dSBarry Smith   PetscFunctionReturn(0);
12929b94acceSBarry Smith }
129362fef451SLois Curfman McInnes 
12944a2ae208SSatish Balay #undef __FUNCT__
12954a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian"
1296c2aafc4cSSatish Balay /*@C
1297b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1298b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1299b4fd4287SBarry Smith 
1300c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1301c7afd0dbSLois Curfman McInnes 
1302b4fd4287SBarry Smith    Input Parameter:
1303b4fd4287SBarry Smith .  snes - the nonlinear solver context
1304b4fd4287SBarry Smith 
1305b4fd4287SBarry Smith    Output Parameters:
1306c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1307b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
130870e92668SMatthew Knepley .  func - location to put Jacobian function (or PETSC_NULL)
130970e92668SMatthew Knepley -  ctx - location to stash Jacobian ctx (or PETSC_NULL)
1310fee21e36SBarry Smith 
131136851e7fSLois Curfman McInnes    Level: advanced
131236851e7fSLois Curfman McInnes 
1313b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1314b4fd4287SBarry Smith @*/
131570e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx)
1316b4fd4287SBarry Smith {
13173a40ed3dSBarry Smith   PetscFunctionBegin;
13180700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1319b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
1320b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
1321e7788613SBarry Smith   if (func) *func = snes->ops->computejacobian;
132270e92668SMatthew Knepley   if (ctx)  *ctx  = snes->jacP;
13233a40ed3dSBarry Smith   PetscFunctionReturn(0);
1324b4fd4287SBarry Smith }
1325b4fd4287SBarry Smith 
13269b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
13279b94acceSBarry Smith 
13284a2ae208SSatish Balay #undef __FUNCT__
13294a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp"
13309b94acceSBarry Smith /*@
13319b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1332272ac6f2SLois Curfman McInnes    of a nonlinear solver.
13339b94acceSBarry Smith 
1334fee21e36SBarry Smith    Collective on SNES
1335fee21e36SBarry Smith 
1336c7afd0dbSLois Curfman McInnes    Input Parameters:
133770e92668SMatthew Knepley .  snes - the SNES context
1338c7afd0dbSLois Curfman McInnes 
1339272ac6f2SLois Curfman McInnes    Notes:
1340272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1341272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1342272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1343272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1344272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1345272ac6f2SLois Curfman McInnes 
134636851e7fSLois Curfman McInnes    Level: advanced
134736851e7fSLois Curfman McInnes 
13489b94acceSBarry Smith .keywords: SNES, nonlinear, setup
13499b94acceSBarry Smith 
13509b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
13519b94acceSBarry Smith @*/
135270e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUp(SNES snes)
13539b94acceSBarry Smith {
1354dfbe8321SBarry Smith   PetscErrorCode ierr;
13553a40ed3dSBarry Smith 
13563a40ed3dSBarry Smith   PetscFunctionBegin;
13570700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
13584dc4c822SBarry Smith   if (snes->setupcalled) PetscFunctionReturn(0);
13599b94acceSBarry Smith 
13607adad957SLisandro Dalcin   if (!((PetscObject)snes)->type_name) {
136185385478SLisandro Dalcin     ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr);
136285385478SLisandro Dalcin   }
136385385478SLisandro Dalcin 
136485385478SLisandro Dalcin   if (!snes->vec_func && !snes->vec_rhs) {
13651096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
13661096aae1SMatthew Knepley   }
136785385478SLisandro Dalcin   if (!snes->ops->computefunction && !snes->vec_rhs) {
13681096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
13691096aae1SMatthew Knepley   }
1370a8c6a408SBarry Smith   if (snes->vec_func == snes->vec_sol) {
137129bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
1372a8c6a408SBarry Smith   }
1373ef8dffc7SBarry Smith   if (!snes->ops->computejacobian && snes->dm) {
1374ef8dffc7SBarry Smith     Mat           J;
1375ef8dffc7SBarry Smith     ISColoring    coloring;
1376ef8dffc7SBarry Smith     MatFDColoring fd;
1377a703fe33SLois Curfman McInnes 
1378ef8dffc7SBarry Smith     ierr = DMGetMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr);
1379ef8dffc7SBarry Smith     ierr = DMGetColoring(snes->dm,IS_COLORING_GHOSTED,MATAIJ,&coloring);CHKERRQ(ierr);
1380ef8dffc7SBarry Smith     ierr = MatFDColoringCreate(J,coloring,&fd);CHKERRQ(ierr);
1381ef8dffc7SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))snes->ops->computefunction,snes->funP);CHKERRQ(ierr);
1382ef8dffc7SBarry Smith     ierr = SNESSetJacobian(snes,J,J,SNESDefaultComputeJacobianColor,fd);CHKERRQ(ierr);
1383ef8dffc7SBarry Smith     ierr = ISColoringDestroy(coloring);CHKERRQ(ierr);
1384ef8dffc7SBarry Smith   }
1385b710008aSBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes, &snes->ksp);CHKERRQ(ierr);}
1386b710008aSBarry Smith 
1387410397dcSLisandro Dalcin   if (snes->ops->setup) {
1388410397dcSLisandro Dalcin     ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr);
1389410397dcSLisandro Dalcin   }
13907aaed0d8SBarry Smith   snes->setupcalled = PETSC_TRUE;
13913a40ed3dSBarry Smith   PetscFunctionReturn(0);
13929b94acceSBarry Smith }
13939b94acceSBarry Smith 
13944a2ae208SSatish Balay #undef __FUNCT__
13954a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
139652baeb72SSatish Balay /*@
13979b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
13989b94acceSBarry Smith    with SNESCreate().
13999b94acceSBarry Smith 
1400c7afd0dbSLois Curfman McInnes    Collective on SNES
1401c7afd0dbSLois Curfman McInnes 
14029b94acceSBarry Smith    Input Parameter:
14039b94acceSBarry Smith .  snes - the SNES context
14049b94acceSBarry Smith 
140536851e7fSLois Curfman McInnes    Level: beginner
140636851e7fSLois Curfman McInnes 
14079b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
14089b94acceSBarry Smith 
140963a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
14109b94acceSBarry Smith @*/
141163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDestroy(SNES snes)
14129b94acceSBarry Smith {
14136849ba73SBarry Smith   PetscErrorCode ierr;
14143a40ed3dSBarry Smith 
14153a40ed3dSBarry Smith   PetscFunctionBegin;
14160700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
14177adad957SLisandro Dalcin   if (--((PetscObject)snes)->refct > 0) PetscFunctionReturn(0);
1418d4bb536fSBarry Smith 
1419be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
14200f5bd95cSBarry Smith   ierr = PetscObjectDepublish(snes);CHKERRQ(ierr);
14216c699258SBarry Smith 
14226c699258SBarry Smith   if (snes->dm) {ierr = DMDestroy(snes->dm);CHKERRQ(ierr);}
1423e7788613SBarry Smith   if (snes->ops->destroy) {ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr);}
142485385478SLisandro Dalcin 
142585385478SLisandro Dalcin   if (snes->vec_rhs) {ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr);}
142685385478SLisandro Dalcin   if (snes->vec_sol) {ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr);}
142785385478SLisandro Dalcin   if (snes->vec_func) {ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr);}
14283a7fca6bSBarry Smith   if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
14293a7fca6bSBarry Smith   if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
14301cee3971SBarry Smith   if (snes->ksp) {ierr = KSPDestroy(snes->ksp);CHKERRQ(ierr);}
143185385478SLisandro Dalcin   ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);
1432522c5e43SBarry Smith   if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);}
1433a6570f20SBarry Smith   ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);
14347f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);}
1435a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr);
14363a40ed3dSBarry Smith   PetscFunctionReturn(0);
14379b94acceSBarry Smith }
14389b94acceSBarry Smith 
14399b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
14409b94acceSBarry Smith 
14414a2ae208SSatish Balay #undef __FUNCT__
1442a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner"
1443a8054027SBarry Smith /*@
1444a8054027SBarry Smith    SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve.
1445a8054027SBarry Smith 
1446a8054027SBarry Smith    Collective on SNES
1447a8054027SBarry Smith 
1448a8054027SBarry Smith    Input Parameters:
1449a8054027SBarry Smith +  snes - the SNES context
1450a8054027SBarry 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
14513b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1452a8054027SBarry Smith 
1453a8054027SBarry Smith    Options Database Keys:
1454a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1455a8054027SBarry Smith 
1456a8054027SBarry Smith    Notes:
1457a8054027SBarry Smith    The default is 1
1458a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1459a8054027SBarry Smith    If  -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use
1460a8054027SBarry Smith 
1461a8054027SBarry Smith    Level: intermediate
1462a8054027SBarry Smith 
1463a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1464a8054027SBarry Smith 
1465e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian()
1466a8054027SBarry Smith 
1467a8054027SBarry Smith @*/
1468a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagPreconditioner(SNES snes,PetscInt lag)
1469a8054027SBarry Smith {
1470a8054027SBarry Smith   PetscFunctionBegin;
14710700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
14723b4f5425SBarry Smith   if (lag < -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1473a8054027SBarry Smith   if (!lag) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1474a8054027SBarry Smith   snes->lagpreconditioner = lag;
1475a8054027SBarry Smith   PetscFunctionReturn(0);
1476a8054027SBarry Smith }
1477a8054027SBarry Smith 
1478a8054027SBarry Smith #undef __FUNCT__
1479a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner"
1480a8054027SBarry Smith /*@
1481a8054027SBarry Smith    SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt
1482a8054027SBarry Smith 
1483a8054027SBarry Smith    Collective on SNES
1484a8054027SBarry Smith 
1485a8054027SBarry Smith    Input Parameter:
1486a8054027SBarry Smith .  snes - the SNES context
1487a8054027SBarry Smith 
1488a8054027SBarry Smith    Output Parameter:
1489a8054027SBarry 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
14903b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1491a8054027SBarry Smith 
1492a8054027SBarry Smith    Options Database Keys:
1493a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1494a8054027SBarry Smith 
1495a8054027SBarry Smith    Notes:
1496a8054027SBarry Smith    The default is 1
1497a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1498a8054027SBarry Smith 
1499a8054027SBarry Smith    Level: intermediate
1500a8054027SBarry Smith 
1501a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1502a8054027SBarry Smith 
1503a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner()
1504a8054027SBarry Smith 
1505a8054027SBarry Smith @*/
1506a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagPreconditioner(SNES snes,PetscInt *lag)
1507a8054027SBarry Smith {
1508a8054027SBarry Smith   PetscFunctionBegin;
15090700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1510a8054027SBarry Smith   *lag = snes->lagpreconditioner;
1511a8054027SBarry Smith   PetscFunctionReturn(0);
1512a8054027SBarry Smith }
1513a8054027SBarry Smith 
1514a8054027SBarry Smith #undef __FUNCT__
1515e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian"
1516e35cf81dSBarry Smith /*@
1517e35cf81dSBarry Smith    SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how
1518e35cf81dSBarry Smith      often the preconditioner is rebuilt.
1519e35cf81dSBarry Smith 
1520e35cf81dSBarry Smith    Collective on SNES
1521e35cf81dSBarry Smith 
1522e35cf81dSBarry Smith    Input Parameters:
1523e35cf81dSBarry Smith +  snes - the SNES context
1524e35cf81dSBarry 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
1525fe3ffe1eSBarry Smith          the Jacobian is built etc. -2 means rebuild at next chance but then never again
1526e35cf81dSBarry Smith 
1527e35cf81dSBarry Smith    Options Database Keys:
1528e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1529e35cf81dSBarry Smith 
1530e35cf81dSBarry Smith    Notes:
1531e35cf81dSBarry Smith    The default is 1
1532e35cf81dSBarry Smith    The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1533fe3ffe1eSBarry 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
1534fe3ffe1eSBarry Smith    at the next Newton step but never again (unless it is reset to another value)
1535e35cf81dSBarry Smith 
1536e35cf81dSBarry Smith    Level: intermediate
1537e35cf81dSBarry Smith 
1538e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1539e35cf81dSBarry Smith 
1540e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian()
1541e35cf81dSBarry Smith 
1542e35cf81dSBarry Smith @*/
1543e35cf81dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagJacobian(SNES snes,PetscInt lag)
1544e35cf81dSBarry Smith {
1545e35cf81dSBarry Smith   PetscFunctionBegin;
15460700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1547fe3ffe1eSBarry Smith   if (lag < -2) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1548e35cf81dSBarry Smith   if (!lag) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1549e35cf81dSBarry Smith   snes->lagjacobian = lag;
1550e35cf81dSBarry Smith   PetscFunctionReturn(0);
1551e35cf81dSBarry Smith }
1552e35cf81dSBarry Smith 
1553e35cf81dSBarry Smith #undef __FUNCT__
1554e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian"
1555e35cf81dSBarry Smith /*@
1556e35cf81dSBarry Smith    SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt
1557e35cf81dSBarry Smith 
1558e35cf81dSBarry Smith    Collective on SNES
1559e35cf81dSBarry Smith 
1560e35cf81dSBarry Smith    Input Parameter:
1561e35cf81dSBarry Smith .  snes - the SNES context
1562e35cf81dSBarry Smith 
1563e35cf81dSBarry Smith    Output Parameter:
1564e35cf81dSBarry 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
1565e35cf81dSBarry Smith          the Jacobian is built etc.
1566e35cf81dSBarry Smith 
1567e35cf81dSBarry Smith    Options Database Keys:
1568e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1569e35cf81dSBarry Smith 
1570e35cf81dSBarry Smith    Notes:
1571e35cf81dSBarry Smith    The default is 1
1572e35cf81dSBarry Smith    The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1573e35cf81dSBarry Smith 
1574e35cf81dSBarry Smith    Level: intermediate
1575e35cf81dSBarry Smith 
1576e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1577e35cf81dSBarry Smith 
1578e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner()
1579e35cf81dSBarry Smith 
1580e35cf81dSBarry Smith @*/
1581e35cf81dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagJacobian(SNES snes,PetscInt *lag)
1582e35cf81dSBarry Smith {
1583e35cf81dSBarry Smith   PetscFunctionBegin;
15840700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1585e35cf81dSBarry Smith   *lag = snes->lagjacobian;
1586e35cf81dSBarry Smith   PetscFunctionReturn(0);
1587e35cf81dSBarry Smith }
1588e35cf81dSBarry Smith 
1589e35cf81dSBarry Smith #undef __FUNCT__
15904a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
15919b94acceSBarry Smith /*@
1592d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
15939b94acceSBarry Smith 
1594c7afd0dbSLois Curfman McInnes    Collective on SNES
1595c7afd0dbSLois Curfman McInnes 
15969b94acceSBarry Smith    Input Parameters:
1597c7afd0dbSLois Curfman McInnes +  snes - the SNES context
159870441072SBarry Smith .  abstol - absolute convergence tolerance
159933174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
160033174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
160133174efeSLois Curfman McInnes            of the change in the solution between steps
160233174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1603c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1604fee21e36SBarry Smith 
160533174efeSLois Curfman McInnes    Options Database Keys:
160670441072SBarry Smith +    -snes_atol <abstol> - Sets abstol
1607c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1608c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1609c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1610c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
16119b94acceSBarry Smith 
1612d7a720efSLois Curfman McInnes    Notes:
16139b94acceSBarry Smith    The default maximum number of iterations is 50.
16149b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
16159b94acceSBarry Smith 
161636851e7fSLois Curfman McInnes    Level: intermediate
161736851e7fSLois Curfman McInnes 
161833174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
16199b94acceSBarry Smith 
16202492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance()
16219b94acceSBarry Smith @*/
162263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf)
16239b94acceSBarry Smith {
16243a40ed3dSBarry Smith   PetscFunctionBegin;
16250700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
162670441072SBarry Smith   if (abstol != PETSC_DEFAULT)  snes->abstol      = abstol;
1627d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1628d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1629d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1630d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
16313a40ed3dSBarry Smith   PetscFunctionReturn(0);
16329b94acceSBarry Smith }
16339b94acceSBarry Smith 
16344a2ae208SSatish Balay #undef __FUNCT__
16354a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
16369b94acceSBarry Smith /*@
163733174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
163833174efeSLois Curfman McInnes 
1639c7afd0dbSLois Curfman McInnes    Not Collective
1640c7afd0dbSLois Curfman McInnes 
164133174efeSLois Curfman McInnes    Input Parameters:
1642c7afd0dbSLois Curfman McInnes +  snes - the SNES context
164385385478SLisandro Dalcin .  atol - absolute convergence tolerance
164433174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
164533174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
164633174efeSLois Curfman McInnes            of the change in the solution between steps
164733174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1648c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1649fee21e36SBarry Smith 
165033174efeSLois Curfman McInnes    Notes:
165133174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
165233174efeSLois Curfman McInnes 
165336851e7fSLois Curfman McInnes    Level: intermediate
165436851e7fSLois Curfman McInnes 
165533174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
165633174efeSLois Curfman McInnes 
165733174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
165833174efeSLois Curfman McInnes @*/
165985385478SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf)
166033174efeSLois Curfman McInnes {
16613a40ed3dSBarry Smith   PetscFunctionBegin;
16620700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
166385385478SLisandro Dalcin   if (atol)  *atol  = snes->abstol;
166433174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
166533174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
166633174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
166733174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
16683a40ed3dSBarry Smith   PetscFunctionReturn(0);
166933174efeSLois Curfman McInnes }
167033174efeSLois Curfman McInnes 
16714a2ae208SSatish Balay #undef __FUNCT__
16724a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
167333174efeSLois Curfman McInnes /*@
16749b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
16759b94acceSBarry Smith 
1676fee21e36SBarry Smith    Collective on SNES
1677fee21e36SBarry Smith 
1678c7afd0dbSLois Curfman McInnes    Input Parameters:
1679c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1680c7afd0dbSLois Curfman McInnes -  tol - tolerance
1681c7afd0dbSLois Curfman McInnes 
16829b94acceSBarry Smith    Options Database Key:
1683c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
16849b94acceSBarry Smith 
168536851e7fSLois Curfman McInnes    Level: intermediate
168636851e7fSLois Curfman McInnes 
16879b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
16889b94acceSBarry Smith 
16892492ecdbSBarry Smith .seealso: SNESSetTolerances()
16909b94acceSBarry Smith @*/
169163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
16929b94acceSBarry Smith {
16933a40ed3dSBarry Smith   PetscFunctionBegin;
16940700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
16959b94acceSBarry Smith   snes->deltatol = tol;
16963a40ed3dSBarry Smith   PetscFunctionReturn(0);
16979b94acceSBarry Smith }
16989b94acceSBarry Smith 
1699df9fa365SBarry Smith /*
1700df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1701df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1702df9fa365SBarry Smith    macros instead of functions
1703df9fa365SBarry Smith */
17044a2ae208SSatish Balay #undef __FUNCT__
1705a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG"
1706a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx)
1707ce1608b8SBarry Smith {
1708dfbe8321SBarry Smith   PetscErrorCode ierr;
1709ce1608b8SBarry Smith 
1710ce1608b8SBarry Smith   PetscFunctionBegin;
17110700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1712a6570f20SBarry Smith   ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1713ce1608b8SBarry Smith   PetscFunctionReturn(0);
1714ce1608b8SBarry Smith }
1715ce1608b8SBarry Smith 
17164a2ae208SSatish Balay #undef __FUNCT__
1717a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate"
1718a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1719df9fa365SBarry Smith {
1720dfbe8321SBarry Smith   PetscErrorCode ierr;
1721df9fa365SBarry Smith 
1722df9fa365SBarry Smith   PetscFunctionBegin;
1723a6570f20SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1724df9fa365SBarry Smith   PetscFunctionReturn(0);
1725df9fa365SBarry Smith }
1726df9fa365SBarry Smith 
17274a2ae208SSatish Balay #undef __FUNCT__
1728a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy"
1729a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGDestroy(PetscDrawLG draw)
1730df9fa365SBarry Smith {
1731dfbe8321SBarry Smith   PetscErrorCode ierr;
1732df9fa365SBarry Smith 
1733df9fa365SBarry Smith   PetscFunctionBegin;
1734a6570f20SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
1735df9fa365SBarry Smith   PetscFunctionReturn(0);
1736df9fa365SBarry Smith }
1737df9fa365SBarry Smith 
1738b271bb04SBarry Smith extern PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorRange_Private(SNES,PetscInt,PetscReal*);
1739b271bb04SBarry Smith #undef __FUNCT__
1740b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRange"
1741b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRange(SNES snes,PetscInt n,PetscReal rnorm,void *monctx)
1742b271bb04SBarry Smith {
1743b271bb04SBarry Smith   PetscDrawLG      lg;
1744b271bb04SBarry Smith   PetscErrorCode   ierr;
1745b271bb04SBarry Smith   PetscReal        x,y,per;
1746b271bb04SBarry Smith   PetscViewer      v = (PetscViewer)monctx;
1747b271bb04SBarry Smith   static PetscReal prev; /* should be in the context */
1748b271bb04SBarry Smith   PetscDraw        draw;
1749b271bb04SBarry Smith   PetscFunctionBegin;
1750b271bb04SBarry Smith   if (!monctx) {
1751b271bb04SBarry Smith     MPI_Comm    comm;
1752b271bb04SBarry Smith 
1753b271bb04SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr);
1754b271bb04SBarry Smith     v      = PETSC_VIEWER_DRAW_(comm);
1755b271bb04SBarry Smith   }
1756b271bb04SBarry Smith   ierr   = PetscViewerDrawGetDrawLG(v,0,&lg);CHKERRQ(ierr);
1757b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1758b271bb04SBarry Smith   ierr   = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1759b271bb04SBarry Smith   ierr   = PetscDrawSetTitle(draw,"Residual norm");CHKERRQ(ierr);
1760b271bb04SBarry Smith   x = (PetscReal) n;
1761b271bb04SBarry Smith   if (rnorm > 0.0) y = log10(rnorm); else y = -15.0;
1762b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1763b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1764b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1765b271bb04SBarry Smith   }
1766b271bb04SBarry Smith 
1767b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,1,&lg);CHKERRQ(ierr);
1768b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1769b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1770b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"% elemts > .2*max elemt");CHKERRQ(ierr);
1771b271bb04SBarry Smith   ierr =  SNESMonitorRange_Private(snes,n,&per);CHKERRQ(ierr);
1772b271bb04SBarry Smith   x = (PetscReal) n;
1773b271bb04SBarry Smith   y = 100.0*per;
1774b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1775b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1776b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1777b271bb04SBarry Smith   }
1778b271bb04SBarry Smith 
1779b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,2,&lg);CHKERRQ(ierr);
1780b271bb04SBarry Smith   if (!n) {prev = rnorm;ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1781b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1782b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm");CHKERRQ(ierr);
1783b271bb04SBarry Smith   x = (PetscReal) n;
1784b271bb04SBarry Smith   y = (prev - rnorm)/prev;
1785b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1786b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1787b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1788b271bb04SBarry Smith   }
1789b271bb04SBarry Smith 
1790b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,3,&lg);CHKERRQ(ierr);
1791b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1792b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1793b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");CHKERRQ(ierr);
1794b271bb04SBarry Smith   x = (PetscReal) n;
1795b271bb04SBarry Smith   y = (prev - rnorm)/(prev*per);
1796b271bb04SBarry Smith   if (n > 2) { /*skip initial crazy value */
1797b271bb04SBarry Smith     ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1798b271bb04SBarry Smith   }
1799b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1800b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1801b271bb04SBarry Smith   }
1802b271bb04SBarry Smith   prev = rnorm;
1803b271bb04SBarry Smith   PetscFunctionReturn(0);
1804b271bb04SBarry Smith }
1805b271bb04SBarry Smith 
1806b271bb04SBarry Smith #undef __FUNCT__
1807b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeCreate"
1808b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRangeCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1809b271bb04SBarry Smith {
1810b271bb04SBarry Smith   PetscErrorCode ierr;
1811b271bb04SBarry Smith 
1812b271bb04SBarry Smith   PetscFunctionBegin;
1813b271bb04SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1814b271bb04SBarry Smith   PetscFunctionReturn(0);
1815b271bb04SBarry Smith }
1816b271bb04SBarry Smith 
1817b271bb04SBarry Smith #undef __FUNCT__
1818b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeDestroy"
1819b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRangeDestroy(PetscDrawLG draw)
1820b271bb04SBarry Smith {
1821b271bb04SBarry Smith   PetscErrorCode ierr;
1822b271bb04SBarry Smith 
1823b271bb04SBarry Smith   PetscFunctionBegin;
1824b271bb04SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
1825b271bb04SBarry Smith   PetscFunctionReturn(0);
1826b271bb04SBarry Smith }
1827b271bb04SBarry Smith 
18289b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
18299b94acceSBarry Smith 
18304a2ae208SSatish Balay #undef __FUNCT__
1831a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet"
18329b94acceSBarry Smith /*@C
1833a6570f20SBarry Smith    SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every
18349b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
18359b94acceSBarry Smith    progress.
18369b94acceSBarry Smith 
1837fee21e36SBarry Smith    Collective on SNES
1838fee21e36SBarry Smith 
1839c7afd0dbSLois Curfman McInnes    Input Parameters:
1840c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1841c7afd0dbSLois Curfman McInnes .  func - monitoring routine
1842b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
1843e8105e01SRichard Katz           monitor routine (use PETSC_NULL if no context is desired)
1844b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1845b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
18469b94acceSBarry Smith 
1847c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1848a7cc72afSBarry Smith $     int func(SNES snes,PetscInt its, PetscReal norm,void *mctx)
1849c7afd0dbSLois Curfman McInnes 
1850c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1851c7afd0dbSLois Curfman McInnes .    its - iteration number
1852c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
185340a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
18549b94acceSBarry Smith 
18559665c990SLois Curfman McInnes    Options Database Keys:
1856a6570f20SBarry Smith +    -snes_monitor        - sets SNESMonitorDefault()
1857a6570f20SBarry Smith .    -snes_monitor_draw    - sets line graph monitor,
1858a6570f20SBarry Smith                             uses SNESMonitorLGCreate()
1859a6570f20SBarry Smith _    -snes_monitor_cancel - cancels all monitors that have
1860c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
1861a6570f20SBarry Smith                             calls to SNESMonitorSet(), but
1862c7afd0dbSLois Curfman McInnes                             does not cancel those set via
1863c7afd0dbSLois Curfman McInnes                             the options database.
18649665c990SLois Curfman McInnes 
1865639f9d9dSBarry Smith    Notes:
18666bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
1867a6570f20SBarry Smith    SNESMonitorSet() multiple times; all will be called in the
18686bc08f3fSLois Curfman McInnes    order in which they were set.
1869639f9d9dSBarry Smith 
1870025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each SNES object
1871025f1a04SBarry Smith 
187236851e7fSLois Curfman McInnes    Level: intermediate
187336851e7fSLois Curfman McInnes 
18749b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
18759b94acceSBarry Smith 
1876a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel()
18779b94acceSBarry Smith @*/
1878b90d0a6eSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void*))
18799b94acceSBarry Smith {
1880b90d0a6eSBarry Smith   PetscInt i;
1881b90d0a6eSBarry Smith 
18823a40ed3dSBarry Smith   PetscFunctionBegin;
18830700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1884639f9d9dSBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) {
188529bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1886639f9d9dSBarry Smith   }
1887b90d0a6eSBarry Smith   for (i=0; i<snes->numbermonitors;i++) {
1888b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) PetscFunctionReturn(0);
1889b90d0a6eSBarry Smith 
1890b90d0a6eSBarry Smith     /* check if both default monitors that share common ASCII viewer */
1891b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitor == SNESMonitorDefault) {
1892b90d0a6eSBarry Smith       if (mctx && snes->monitorcontext[i]) {
1893b90d0a6eSBarry Smith         PetscErrorCode          ierr;
1894b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer1 = (PetscViewerASCIIMonitor) mctx;
1895b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer2 = (PetscViewerASCIIMonitor) snes->monitorcontext[i];
1896b90d0a6eSBarry Smith         if (viewer1->viewer == viewer2->viewer) {
1897b90d0a6eSBarry Smith           ierr = (*monitordestroy)(mctx);CHKERRQ(ierr);
1898b90d0a6eSBarry Smith           PetscFunctionReturn(0);
1899b90d0a6eSBarry Smith         }
1900b90d0a6eSBarry Smith       }
1901b90d0a6eSBarry Smith     }
1902b90d0a6eSBarry Smith   }
1903b90d0a6eSBarry Smith   snes->monitor[snes->numbermonitors]           = monitor;
1904b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
1905639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
19063a40ed3dSBarry Smith   PetscFunctionReturn(0);
19079b94acceSBarry Smith }
19089b94acceSBarry Smith 
19094a2ae208SSatish Balay #undef __FUNCT__
1910a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel"
19115cd90555SBarry Smith /*@C
1912a6570f20SBarry Smith    SNESMonitorCancel - Clears all the monitor functions for a SNES object.
19135cd90555SBarry Smith 
1914c7afd0dbSLois Curfman McInnes    Collective on SNES
1915c7afd0dbSLois Curfman McInnes 
19165cd90555SBarry Smith    Input Parameters:
19175cd90555SBarry Smith .  snes - the SNES context
19185cd90555SBarry Smith 
19191a480d89SAdministrator    Options Database Key:
1920a6570f20SBarry Smith .  -snes_monitor_cancel - cancels all monitors that have been hardwired
1921a6570f20SBarry Smith     into a code by calls to SNESMonitorSet(), but does not cancel those
1922c7afd0dbSLois Curfman McInnes     set via the options database
19235cd90555SBarry Smith 
19245cd90555SBarry Smith    Notes:
19255cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
19265cd90555SBarry Smith 
192736851e7fSLois Curfman McInnes    Level: intermediate
192836851e7fSLois Curfman McInnes 
19295cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
19305cd90555SBarry Smith 
1931a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet()
19325cd90555SBarry Smith @*/
1933a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorCancel(SNES snes)
19345cd90555SBarry Smith {
1935d952e501SBarry Smith   PetscErrorCode ierr;
1936d952e501SBarry Smith   PetscInt       i;
1937d952e501SBarry Smith 
19385cd90555SBarry Smith   PetscFunctionBegin;
19390700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1940d952e501SBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
1941d952e501SBarry Smith     if (snes->monitordestroy[i]) {
1942d952e501SBarry Smith       ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr);
1943d952e501SBarry Smith     }
1944d952e501SBarry Smith   }
19455cd90555SBarry Smith   snes->numbermonitors = 0;
19465cd90555SBarry Smith   PetscFunctionReturn(0);
19475cd90555SBarry Smith }
19485cd90555SBarry Smith 
19494a2ae208SSatish Balay #undef __FUNCT__
19504a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
19519b94acceSBarry Smith /*@C
19529b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
19539b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
19549b94acceSBarry Smith 
1955fee21e36SBarry Smith    Collective on SNES
1956fee21e36SBarry Smith 
1957c7afd0dbSLois Curfman McInnes    Input Parameters:
1958c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1959c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
19607f7931b9SBarry Smith .  cctx - [optional] context for private data for the convergence routine  (may be PETSC_NULL)
19617f7931b9SBarry Smith -  destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran)
19629b94acceSBarry Smith 
1963c7afd0dbSLois Curfman McInnes    Calling sequence of func:
196406ee9f85SBarry Smith $     PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
1965c7afd0dbSLois Curfman McInnes 
1966c7afd0dbSLois Curfman McInnes +    snes - the SNES context
196706ee9f85SBarry Smith .    it - current iteration (0 is the first and is before any Newton step)
1968c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
1969184914b5SBarry Smith .    reason - reason for convergence/divergence
1970c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
19714b27c08aSLois Curfman McInnes .    gnorm - 2-norm of current step
19724b27c08aSLois Curfman McInnes -    f - 2-norm of function
19739b94acceSBarry Smith 
197436851e7fSLois Curfman McInnes    Level: advanced
197536851e7fSLois Curfman McInnes 
19769b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
19779b94acceSBarry Smith 
197885385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged()
19799b94acceSBarry Smith @*/
19807f7931b9SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*))
19819b94acceSBarry Smith {
19827f7931b9SBarry Smith   PetscErrorCode ierr;
19837f7931b9SBarry Smith 
19843a40ed3dSBarry Smith   PetscFunctionBegin;
19850700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
198685385478SLisandro Dalcin   if (!func) func = SNESSkipConverged;
19877f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {
19887f7931b9SBarry Smith     ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);
19897f7931b9SBarry Smith   }
199085385478SLisandro Dalcin   snes->ops->converged        = func;
19917f7931b9SBarry Smith   snes->ops->convergeddestroy = destroy;
199285385478SLisandro Dalcin   snes->cnvP                  = cctx;
19933a40ed3dSBarry Smith   PetscFunctionReturn(0);
19949b94acceSBarry Smith }
19959b94acceSBarry Smith 
19964a2ae208SSatish Balay #undef __FUNCT__
19974a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
199852baeb72SSatish Balay /*@
1999184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
2000184914b5SBarry Smith 
2001184914b5SBarry Smith    Not Collective
2002184914b5SBarry Smith 
2003184914b5SBarry Smith    Input Parameter:
2004184914b5SBarry Smith .  snes - the SNES context
2005184914b5SBarry Smith 
2006184914b5SBarry Smith    Output Parameter:
20074d0a8057SBarry Smith .  reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the
2008184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
2009184914b5SBarry Smith 
2010184914b5SBarry Smith    Level: intermediate
2011184914b5SBarry Smith 
2012184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
2013184914b5SBarry Smith 
2014184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
2015184914b5SBarry Smith 
201685385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason
2017184914b5SBarry Smith @*/
201863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
2019184914b5SBarry Smith {
2020184914b5SBarry Smith   PetscFunctionBegin;
20210700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
20224482741eSBarry Smith   PetscValidPointer(reason,2);
2023184914b5SBarry Smith   *reason = snes->reason;
2024184914b5SBarry Smith   PetscFunctionReturn(0);
2025184914b5SBarry Smith }
2026184914b5SBarry Smith 
20274a2ae208SSatish Balay #undef __FUNCT__
20284a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
2029c9005455SLois Curfman McInnes /*@
2030c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
2031c9005455SLois Curfman McInnes 
2032fee21e36SBarry Smith    Collective on SNES
2033fee21e36SBarry Smith 
2034c7afd0dbSLois Curfman McInnes    Input Parameters:
2035c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
20368c7482ecSBarry Smith .  a   - array to hold history, this array will contain the function norms computed at each step
2037cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
2038758f92a0SBarry Smith .  na  - size of a and its
203964731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
2040758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
2041c7afd0dbSLois Curfman McInnes 
2042c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
2043c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
2044c9005455SLois Curfman McInnes    during the section of code that is being timed.
2045c9005455SLois Curfman McInnes 
204636851e7fSLois Curfman McInnes    Level: intermediate
204736851e7fSLois Curfman McInnes 
2048c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
2049758f92a0SBarry Smith 
205008405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
2051758f92a0SBarry Smith 
2052c9005455SLois Curfman McInnes @*/
2053a562a398SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscTruth reset)
2054c9005455SLois Curfman McInnes {
20553a40ed3dSBarry Smith   PetscFunctionBegin;
20560700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
20574482741eSBarry Smith   if (na)  PetscValidScalarPointer(a,2);
2058a562a398SLisandro Dalcin   if (its) PetscValidIntPointer(its,3);
2059c9005455SLois Curfman McInnes   snes->conv_hist       = a;
2060758f92a0SBarry Smith   snes->conv_hist_its   = its;
2061758f92a0SBarry Smith   snes->conv_hist_max   = na;
2062a12bc48fSLisandro Dalcin   snes->conv_hist_len   = 0;
2063758f92a0SBarry Smith   snes->conv_hist_reset = reset;
2064758f92a0SBarry Smith   PetscFunctionReturn(0);
2065758f92a0SBarry Smith }
2066758f92a0SBarry Smith 
20674a2ae208SSatish Balay #undef __FUNCT__
20684a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
20690c4c9dddSBarry Smith /*@C
2070758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
2071758f92a0SBarry Smith 
2072758f92a0SBarry Smith    Collective on SNES
2073758f92a0SBarry Smith 
2074758f92a0SBarry Smith    Input Parameter:
2075758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
2076758f92a0SBarry Smith 
2077758f92a0SBarry Smith    Output Parameters:
2078758f92a0SBarry Smith .  a   - array to hold history
2079758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
2080758f92a0SBarry Smith          negative if not converged) for each solve.
2081758f92a0SBarry Smith -  na  - size of a and its
2082758f92a0SBarry Smith 
2083758f92a0SBarry Smith    Notes:
2084758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
2085758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
2086758f92a0SBarry Smith 
2087758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
2088758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
2089758f92a0SBarry Smith    during the section of code that is being timed.
2090758f92a0SBarry Smith 
2091758f92a0SBarry Smith    Level: intermediate
2092758f92a0SBarry Smith 
2093758f92a0SBarry Smith .keywords: SNES, get, convergence, history
2094758f92a0SBarry Smith 
2095758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
2096758f92a0SBarry Smith 
2097758f92a0SBarry Smith @*/
209863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na)
2099758f92a0SBarry Smith {
2100758f92a0SBarry Smith   PetscFunctionBegin;
21010700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2102758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
2103758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
2104758f92a0SBarry Smith   if (na)  *na  = snes->conv_hist_len;
21053a40ed3dSBarry Smith   PetscFunctionReturn(0);
2106c9005455SLois Curfman McInnes }
2107c9005455SLois Curfman McInnes 
2108e74ef692SMatthew Knepley #undef __FUNCT__
2109e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
2110ac226902SBarry Smith /*@C
211176b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
21127e4bb74cSBarry Smith   at the beginning o every iteration of the nonlinear solve. Specifically
21137e4bb74cSBarry Smith   it is called just before the Jacobian is "evaluated".
211476b2cf59SMatthew Knepley 
211576b2cf59SMatthew Knepley   Collective on SNES
211676b2cf59SMatthew Knepley 
211776b2cf59SMatthew Knepley   Input Parameters:
211876b2cf59SMatthew Knepley . snes - The nonlinear solver context
211976b2cf59SMatthew Knepley . func - The function
212076b2cf59SMatthew Knepley 
212176b2cf59SMatthew Knepley   Calling sequence of func:
2122b5d30489SBarry Smith . func (SNES snes, PetscInt step);
212376b2cf59SMatthew Knepley 
212476b2cf59SMatthew Knepley . step - The current step of the iteration
212576b2cf59SMatthew Knepley 
2126fe97e370SBarry Smith   Level: advanced
2127fe97e370SBarry Smith 
2128fe97e370SBarry Smith   Note: This is NOT what one uses to update the ghost points before a function evaluation, that should be done at the beginning of your FormFunction()
2129fe97e370SBarry Smith         This is not used by most users.
213076b2cf59SMatthew Knepley 
213176b2cf59SMatthew Knepley .keywords: SNES, update
2132b5d30489SBarry Smith 
213385385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve()
213476b2cf59SMatthew Knepley @*/
213563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt))
213676b2cf59SMatthew Knepley {
213776b2cf59SMatthew Knepley   PetscFunctionBegin;
21380700a824SBarry Smith   PetscValidHeaderSpecific(snes, SNES_CLASSID,1);
2139e7788613SBarry Smith   snes->ops->update = func;
214076b2cf59SMatthew Knepley   PetscFunctionReturn(0);
214176b2cf59SMatthew Knepley }
214276b2cf59SMatthew Knepley 
2143e74ef692SMatthew Knepley #undef __FUNCT__
2144e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
214576b2cf59SMatthew Knepley /*@
214676b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
214776b2cf59SMatthew Knepley 
214876b2cf59SMatthew Knepley   Not collective
214976b2cf59SMatthew Knepley 
215076b2cf59SMatthew Knepley   Input Parameters:
215176b2cf59SMatthew Knepley . snes - The nonlinear solver context
215276b2cf59SMatthew Knepley . step - The current step of the iteration
215376b2cf59SMatthew Knepley 
2154205452f4SMatthew Knepley   Level: intermediate
2155205452f4SMatthew Knepley 
215676b2cf59SMatthew Knepley .keywords: SNES, update
2157a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC()
215876b2cf59SMatthew Knepley @*/
215963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultUpdate(SNES snes, PetscInt step)
216076b2cf59SMatthew Knepley {
216176b2cf59SMatthew Knepley   PetscFunctionBegin;
216276b2cf59SMatthew Knepley   PetscFunctionReturn(0);
216376b2cf59SMatthew Knepley }
216476b2cf59SMatthew Knepley 
21654a2ae208SSatish Balay #undef __FUNCT__
21664a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
21679b94acceSBarry Smith /*
21689b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
21699b94acceSBarry Smith    positive parameter delta.
21709b94acceSBarry Smith 
21719b94acceSBarry Smith     Input Parameters:
2172c7afd0dbSLois Curfman McInnes +   snes - the SNES context
21739b94acceSBarry Smith .   y - approximate solution of linear system
21749b94acceSBarry Smith .   fnorm - 2-norm of current function
2175c7afd0dbSLois Curfman McInnes -   delta - trust region size
21769b94acceSBarry Smith 
21779b94acceSBarry Smith     Output Parameters:
2178c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
21799b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
21809b94acceSBarry Smith     region, and exceeds zero otherwise.
2181c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
21829b94acceSBarry Smith 
21839b94acceSBarry Smith     Note:
21844b27c08aSLois Curfman McInnes     For non-trust region methods such as SNESLS, the parameter delta
21859b94acceSBarry Smith     is set to be the maximum allowable step size.
21869b94acceSBarry Smith 
21879b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
21889b94acceSBarry Smith */
2189dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
21909b94acceSBarry Smith {
2191064f8208SBarry Smith   PetscReal      nrm;
2192ea709b57SSatish Balay   PetscScalar    cnorm;
2193dfbe8321SBarry Smith   PetscErrorCode ierr;
21943a40ed3dSBarry Smith 
21953a40ed3dSBarry Smith   PetscFunctionBegin;
21960700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
21970700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,2);
2198c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,2);
2199184914b5SBarry Smith 
2200064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
2201064f8208SBarry Smith   if (nrm > *delta) {
2202064f8208SBarry Smith      nrm = *delta/nrm;
2203064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
2204064f8208SBarry Smith      cnorm = nrm;
22052dcb1b2aSMatthew Knepley      ierr = VecScale(y,cnorm);CHKERRQ(ierr);
22069b94acceSBarry Smith      *ynorm = *delta;
22079b94acceSBarry Smith   } else {
22089b94acceSBarry Smith      *gpnorm = 0.0;
2209064f8208SBarry Smith      *ynorm = nrm;
22109b94acceSBarry Smith   }
22113a40ed3dSBarry Smith   PetscFunctionReturn(0);
22129b94acceSBarry Smith }
22139b94acceSBarry Smith 
22144a2ae208SSatish Balay #undef __FUNCT__
22154a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
22166ce558aeSBarry Smith /*@C
2217f69a0ea3SMatthew Knepley    SNESSolve - Solves a nonlinear system F(x) = b.
2218f69a0ea3SMatthew Knepley    Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX().
22199b94acceSBarry Smith 
2220c7afd0dbSLois Curfman McInnes    Collective on SNES
2221c7afd0dbSLois Curfman McInnes 
2222b2002411SLois Curfman McInnes    Input Parameters:
2223c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2224f69a0ea3SMatthew Knepley .  b - the constant part of the equation, or PETSC_NULL to use zero.
222585385478SLisandro Dalcin -  x - the solution vector.
22269b94acceSBarry Smith 
2227b2002411SLois Curfman McInnes    Notes:
22288ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
22298ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
22308ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
22318ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
22328ddd3da0SLois Curfman McInnes 
223336851e7fSLois Curfman McInnes    Level: beginner
223436851e7fSLois Curfman McInnes 
22359b94acceSBarry Smith .keywords: SNES, nonlinear, solve
22369b94acceSBarry Smith 
223785385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian()
22389b94acceSBarry Smith @*/
2239f69a0ea3SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSolve(SNES snes,Vec b,Vec x)
22409b94acceSBarry Smith {
2241dfbe8321SBarry Smith   PetscErrorCode ierr;
2242f1af5d2fSBarry Smith   PetscTruth     flg;
2243eabae89aSBarry Smith   char           filename[PETSC_MAX_PATH_LEN];
2244eabae89aSBarry Smith   PetscViewer    viewer;
2245052efed2SBarry Smith 
22463a40ed3dSBarry Smith   PetscFunctionBegin;
22470700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
22480700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2249f69a0ea3SMatthew Knepley   PetscCheckSameComm(snes,1,x,3);
22500700a824SBarry Smith   if (b) PetscValidHeaderSpecific(b,VEC_CLASSID,2);
225185385478SLisandro Dalcin   if (b) PetscCheckSameComm(snes,1,b,2);
225285385478SLisandro Dalcin 
225385385478SLisandro Dalcin   /* set solution vector */
225485385478SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);
225585385478SLisandro Dalcin   if (snes->vec_sol) { ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr); }
225685385478SLisandro Dalcin   snes->vec_sol = x;
225785385478SLisandro Dalcin   /* set afine vector if provided */
225885385478SLisandro Dalcin   if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); }
225985385478SLisandro Dalcin   if (snes->vec_rhs) { ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr); }
226085385478SLisandro Dalcin   snes->vec_rhs = b;
226185385478SLisandro Dalcin 
226285385478SLisandro Dalcin   if (!snes->vec_func && snes->vec_rhs) {
226385385478SLisandro Dalcin     ierr = VecDuplicate(b, &snes->vec_func);CHKERRQ(ierr);
226470e92668SMatthew Knepley   }
22653f149594SLisandro Dalcin 
226670e92668SMatthew Knepley   ierr = SNESSetUp(snes);CHKERRQ(ierr);
22673f149594SLisandro Dalcin 
2268abc0a331SBarry Smith   if (snes->conv_hist_reset) snes->conv_hist_len = 0;
226950ffb88aSMatthew Knepley   snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
2270d5e45103SBarry Smith 
22713f149594SLisandro Dalcin   ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
22724936397dSBarry Smith   ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr);
227385385478SLisandro Dalcin   ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
22744936397dSBarry Smith   if (snes->domainerror){
22754936397dSBarry Smith     snes->reason      = SNES_DIVERGED_FUNCTION_DOMAIN;
22764936397dSBarry Smith     snes->domainerror = PETSC_FALSE;
22774936397dSBarry Smith   }
227885385478SLisandro Dalcin 
22793f149594SLisandro Dalcin   if (!snes->reason) {
22803f149594SLisandro Dalcin     SETERRQ(PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason");
22813f149594SLisandro Dalcin   }
22823f149594SLisandro Dalcin 
22837adad957SLisandro Dalcin   ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
2284eabae89aSBarry Smith   if (flg && !PetscPreLoadingOn) {
22857adad957SLisandro Dalcin     ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr);
2286eabae89aSBarry Smith     ierr = SNESView(snes,viewer);CHKERRQ(ierr);
2287eabae89aSBarry Smith     ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr);
2288eabae89aSBarry Smith   }
2289eabae89aSBarry Smith 
229090d69ab7SBarry Smith   flg  = PETSC_FALSE;
229190d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg,PETSC_NULL);CHKERRQ(ierr);
2292da9b6338SBarry Smith   if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); }
22935968eb51SBarry Smith   if (snes->printreason) {
22945968eb51SBarry Smith     if (snes->reason > 0) {
22957adad957SLisandro Dalcin       ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
22965968eb51SBarry Smith     } else {
22977adad957SLisandro Dalcin       ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
22985968eb51SBarry Smith     }
22995968eb51SBarry Smith   }
23005968eb51SBarry Smith 
23013a40ed3dSBarry Smith   PetscFunctionReturn(0);
23029b94acceSBarry Smith }
23039b94acceSBarry Smith 
23049b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
23059b94acceSBarry Smith 
23064a2ae208SSatish Balay #undef __FUNCT__
23074a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
230882bf6240SBarry Smith /*@C
23094b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
23109b94acceSBarry Smith 
2311fee21e36SBarry Smith    Collective on SNES
2312fee21e36SBarry Smith 
2313c7afd0dbSLois Curfman McInnes    Input Parameters:
2314c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2315454a90a3SBarry Smith -  type - a known method
2316c7afd0dbSLois Curfman McInnes 
2317c7afd0dbSLois Curfman McInnes    Options Database Key:
2318454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
2319c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
2320ae12b187SLois Curfman McInnes 
23219b94acceSBarry Smith    Notes:
2322e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
23234b27c08aSLois Curfman McInnes +    SNESLS - Newton's method with line search
2324c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
23254b27c08aSLois Curfman McInnes .    SNESTR - Newton's method with trust region
2326c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
23279b94acceSBarry Smith 
2328ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
2329ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
2330ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
2331ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
2332ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
2333ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
2334ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
2335ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
2336ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
2337b0a32e0cSBarry Smith   appropriate method.
233836851e7fSLois Curfman McInnes 
233936851e7fSLois Curfman McInnes   Level: intermediate
2340a703fe33SLois Curfman McInnes 
2341454a90a3SBarry Smith .keywords: SNES, set, type
2342435da068SBarry Smith 
2343435da068SBarry Smith .seealso: SNESType, SNESCreate()
2344435da068SBarry Smith 
23459b94acceSBarry Smith @*/
2346a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetType(SNES snes,const SNESType type)
23479b94acceSBarry Smith {
2348dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(SNES);
23496831982aSBarry Smith   PetscTruth     match;
23503a40ed3dSBarry Smith 
23513a40ed3dSBarry Smith   PetscFunctionBegin;
23520700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
23534482741eSBarry Smith   PetscValidCharPointer(type,2);
235482bf6240SBarry Smith 
23556831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
23560f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
235792ff6ae8SBarry Smith 
23587adad957SLisandro Dalcin   ierr =  PetscFListFind(SNESList,((PetscObject)snes)->comm,type,(void (**)(void)) &r);CHKERRQ(ierr);
2359958c9bccSBarry Smith   if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type);
236075396ef9SLisandro Dalcin   /* Destroy the previous private SNES context */
236175396ef9SLisandro Dalcin   if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); }
236275396ef9SLisandro Dalcin   /* Reinitialize function pointers in SNESOps structure */
236375396ef9SLisandro Dalcin   snes->ops->setup          = 0;
236475396ef9SLisandro Dalcin   snes->ops->solve          = 0;
236575396ef9SLisandro Dalcin   snes->ops->view           = 0;
236675396ef9SLisandro Dalcin   snes->ops->setfromoptions = 0;
236775396ef9SLisandro Dalcin   snes->ops->destroy        = 0;
236875396ef9SLisandro Dalcin   /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */
236975396ef9SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
23703a40ed3dSBarry Smith   ierr = (*r)(snes);CHKERRQ(ierr);
2371454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
23723a40ed3dSBarry Smith   PetscFunctionReturn(0);
23739b94acceSBarry Smith }
23749b94acceSBarry Smith 
2375a847f771SSatish Balay 
23769b94acceSBarry Smith /* --------------------------------------------------------------------- */
23774a2ae208SSatish Balay #undef __FUNCT__
23784a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
237952baeb72SSatish Balay /*@
23809b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
2381f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
23829b94acceSBarry Smith 
2383fee21e36SBarry Smith    Not Collective
2384fee21e36SBarry Smith 
238536851e7fSLois Curfman McInnes    Level: advanced
238636851e7fSLois Curfman McInnes 
23879b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
23889b94acceSBarry Smith 
23899b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
23909b94acceSBarry Smith @*/
239163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegisterDestroy(void)
23929b94acceSBarry Smith {
2393dfbe8321SBarry Smith   PetscErrorCode ierr;
239482bf6240SBarry Smith 
23953a40ed3dSBarry Smith   PetscFunctionBegin;
23961441b1d3SBarry Smith   ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
23974c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
23983a40ed3dSBarry Smith   PetscFunctionReturn(0);
23999b94acceSBarry Smith }
24009b94acceSBarry Smith 
24014a2ae208SSatish Balay #undef __FUNCT__
24024a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
24039b94acceSBarry Smith /*@C
24049a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
24059b94acceSBarry Smith 
2406c7afd0dbSLois Curfman McInnes    Not Collective
2407c7afd0dbSLois Curfman McInnes 
24089b94acceSBarry Smith    Input Parameter:
24094b0e389bSBarry Smith .  snes - nonlinear solver context
24109b94acceSBarry Smith 
24119b94acceSBarry Smith    Output Parameter:
24123a7fca6bSBarry Smith .  type - SNES method (a character string)
24139b94acceSBarry Smith 
241436851e7fSLois Curfman McInnes    Level: intermediate
241536851e7fSLois Curfman McInnes 
2416454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
24179b94acceSBarry Smith @*/
2418a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetType(SNES snes,const SNESType *type)
24199b94acceSBarry Smith {
24203a40ed3dSBarry Smith   PetscFunctionBegin;
24210700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
24224482741eSBarry Smith   PetscValidPointer(type,2);
24237adad957SLisandro Dalcin   *type = ((PetscObject)snes)->type_name;
24243a40ed3dSBarry Smith   PetscFunctionReturn(0);
24259b94acceSBarry Smith }
24269b94acceSBarry Smith 
24274a2ae208SSatish Balay #undef __FUNCT__
24284a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
242952baeb72SSatish Balay /*@
24309b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
24319b94acceSBarry Smith    stored.
24329b94acceSBarry Smith 
2433c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2434c7afd0dbSLois Curfman McInnes 
24359b94acceSBarry Smith    Input Parameter:
24369b94acceSBarry Smith .  snes - the SNES context
24379b94acceSBarry Smith 
24389b94acceSBarry Smith    Output Parameter:
24399b94acceSBarry Smith .  x - the solution
24409b94acceSBarry Smith 
244170e92668SMatthew Knepley    Level: intermediate
244236851e7fSLois Curfman McInnes 
24439b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
24449b94acceSBarry Smith 
244585385478SLisandro Dalcin .seealso:  SNESGetSolutionUpdate(), SNESGetFunction()
24469b94acceSBarry Smith @*/
244763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolution(SNES snes,Vec *x)
24489b94acceSBarry Smith {
24493a40ed3dSBarry Smith   PetscFunctionBegin;
24500700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
24514482741eSBarry Smith   PetscValidPointer(x,2);
245285385478SLisandro Dalcin   *x = snes->vec_sol;
245370e92668SMatthew Knepley   PetscFunctionReturn(0);
245470e92668SMatthew Knepley }
245570e92668SMatthew Knepley 
245670e92668SMatthew Knepley #undef __FUNCT__
24574a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
245852baeb72SSatish Balay /*@
24599b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
24609b94acceSBarry Smith    stored.
24619b94acceSBarry Smith 
2462c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2463c7afd0dbSLois Curfman McInnes 
24649b94acceSBarry Smith    Input Parameter:
24659b94acceSBarry Smith .  snes - the SNES context
24669b94acceSBarry Smith 
24679b94acceSBarry Smith    Output Parameter:
24689b94acceSBarry Smith .  x - the solution update
24699b94acceSBarry Smith 
247036851e7fSLois Curfman McInnes    Level: advanced
247136851e7fSLois Curfman McInnes 
24729b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
24739b94acceSBarry Smith 
247485385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction()
24759b94acceSBarry Smith @*/
247663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolutionUpdate(SNES snes,Vec *x)
24779b94acceSBarry Smith {
24783a40ed3dSBarry Smith   PetscFunctionBegin;
24790700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
24804482741eSBarry Smith   PetscValidPointer(x,2);
248185385478SLisandro Dalcin   *x = snes->vec_sol_update;
24823a40ed3dSBarry Smith   PetscFunctionReturn(0);
24839b94acceSBarry Smith }
24849b94acceSBarry Smith 
24854a2ae208SSatish Balay #undef __FUNCT__
24864a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
24879b94acceSBarry Smith /*@C
24883638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
24899b94acceSBarry Smith 
2490c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2491c7afd0dbSLois Curfman McInnes 
24929b94acceSBarry Smith    Input Parameter:
24939b94acceSBarry Smith .  snes - the SNES context
24949b94acceSBarry Smith 
24959b94acceSBarry Smith    Output Parameter:
24967bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
249770e92668SMatthew Knepley .  func - the function (or PETSC_NULL)
249870e92668SMatthew Knepley -  ctx - the function context (or PETSC_NULL)
24999b94acceSBarry Smith 
250036851e7fSLois Curfman McInnes    Level: advanced
250136851e7fSLois Curfman McInnes 
2502a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
25039b94acceSBarry Smith 
25044b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution()
25059b94acceSBarry Smith @*/
250670e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx)
25079b94acceSBarry Smith {
25083a40ed3dSBarry Smith   PetscFunctionBegin;
25090700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
251085385478SLisandro Dalcin   if (r)    *r    = snes->vec_func;
2511e7788613SBarry Smith   if (func) *func = snes->ops->computefunction;
251270e92668SMatthew Knepley   if (ctx)  *ctx  = snes->funP;
25133a40ed3dSBarry Smith   PetscFunctionReturn(0);
25149b94acceSBarry Smith }
25159b94acceSBarry Smith 
25164a2ae208SSatish Balay #undef __FUNCT__
25174a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
25183c7409f5SSatish Balay /*@C
25193c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2520d850072dSLois Curfman McInnes    SNES options in the database.
25213c7409f5SSatish Balay 
2522fee21e36SBarry Smith    Collective on SNES
2523fee21e36SBarry Smith 
2524c7afd0dbSLois Curfman McInnes    Input Parameter:
2525c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2526c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2527c7afd0dbSLois Curfman McInnes 
2528d850072dSLois Curfman McInnes    Notes:
2529a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2530c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2531d850072dSLois Curfman McInnes 
253236851e7fSLois Curfman McInnes    Level: advanced
253336851e7fSLois Curfman McInnes 
25343c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2535a86d99e1SLois Curfman McInnes 
2536a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
25373c7409f5SSatish Balay @*/
253863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetOptionsPrefix(SNES snes,const char prefix[])
25393c7409f5SSatish Balay {
2540dfbe8321SBarry Smith   PetscErrorCode ierr;
25413c7409f5SSatish Balay 
25423a40ed3dSBarry Smith   PetscFunctionBegin;
25430700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2544639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
25451cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
254694b7f48cSBarry Smith   ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
25473a40ed3dSBarry Smith   PetscFunctionReturn(0);
25483c7409f5SSatish Balay }
25493c7409f5SSatish Balay 
25504a2ae208SSatish Balay #undef __FUNCT__
25514a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
25523c7409f5SSatish Balay /*@C
2553f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2554d850072dSLois Curfman McInnes    SNES options in the database.
25553c7409f5SSatish Balay 
2556fee21e36SBarry Smith    Collective on SNES
2557fee21e36SBarry Smith 
2558c7afd0dbSLois Curfman McInnes    Input Parameters:
2559c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2560c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2561c7afd0dbSLois Curfman McInnes 
2562d850072dSLois Curfman McInnes    Notes:
2563a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2564c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2565d850072dSLois Curfman McInnes 
256636851e7fSLois Curfman McInnes    Level: advanced
256736851e7fSLois Curfman McInnes 
25683c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2569a86d99e1SLois Curfman McInnes 
2570a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
25713c7409f5SSatish Balay @*/
257263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAppendOptionsPrefix(SNES snes,const char prefix[])
25733c7409f5SSatish Balay {
2574dfbe8321SBarry Smith   PetscErrorCode ierr;
25753c7409f5SSatish Balay 
25763a40ed3dSBarry Smith   PetscFunctionBegin;
25770700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2578639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
25791cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
258094b7f48cSBarry Smith   ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
25813a40ed3dSBarry Smith   PetscFunctionReturn(0);
25823c7409f5SSatish Balay }
25833c7409f5SSatish Balay 
25844a2ae208SSatish Balay #undef __FUNCT__
25854a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
25869ab63eb5SSatish Balay /*@C
25873c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
25883c7409f5SSatish Balay    SNES options in the database.
25893c7409f5SSatish Balay 
2590c7afd0dbSLois Curfman McInnes    Not Collective
2591c7afd0dbSLois Curfman McInnes 
25923c7409f5SSatish Balay    Input Parameter:
25933c7409f5SSatish Balay .  snes - the SNES context
25943c7409f5SSatish Balay 
25953c7409f5SSatish Balay    Output Parameter:
25963c7409f5SSatish Balay .  prefix - pointer to the prefix string used
25973c7409f5SSatish Balay 
25984ef407dbSRichard Tran Mills    Notes: On the fortran side, the user should pass in a string 'prefix' of
25999ab63eb5SSatish Balay    sufficient length to hold the prefix.
26009ab63eb5SSatish Balay 
260136851e7fSLois Curfman McInnes    Level: advanced
260236851e7fSLois Curfman McInnes 
26033c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2604a86d99e1SLois Curfman McInnes 
2605a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
26063c7409f5SSatish Balay @*/
2607e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetOptionsPrefix(SNES snes,const char *prefix[])
26083c7409f5SSatish Balay {
2609dfbe8321SBarry Smith   PetscErrorCode ierr;
26103c7409f5SSatish Balay 
26113a40ed3dSBarry Smith   PetscFunctionBegin;
26120700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2613639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
26143a40ed3dSBarry Smith   PetscFunctionReturn(0);
26153c7409f5SSatish Balay }
26163c7409f5SSatish Balay 
2617b2002411SLois Curfman McInnes 
26184a2ae208SSatish Balay #undef __FUNCT__
26194a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
26203cea93caSBarry Smith /*@C
26213cea93caSBarry Smith   SNESRegister - See SNESRegisterDynamic()
26223cea93caSBarry Smith 
26237f6c08e0SMatthew Knepley   Level: advanced
26243cea93caSBarry Smith @*/
262563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES))
2626b2002411SLois Curfman McInnes {
2627e2d1d2b7SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
2628dfbe8321SBarry Smith   PetscErrorCode ierr;
2629b2002411SLois Curfman McInnes 
2630b2002411SLois Curfman McInnes   PetscFunctionBegin;
2631b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2632c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
2633b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2634b2002411SLois Curfman McInnes }
2635da9b6338SBarry Smith 
2636da9b6338SBarry Smith #undef __FUNCT__
2637da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin"
263863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESTestLocalMin(SNES snes)
2639da9b6338SBarry Smith {
2640dfbe8321SBarry Smith   PetscErrorCode ierr;
264177431f27SBarry Smith   PetscInt       N,i,j;
2642da9b6338SBarry Smith   Vec            u,uh,fh;
2643da9b6338SBarry Smith   PetscScalar    value;
2644da9b6338SBarry Smith   PetscReal      norm;
2645da9b6338SBarry Smith 
2646da9b6338SBarry Smith   PetscFunctionBegin;
2647da9b6338SBarry Smith   ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr);
2648da9b6338SBarry Smith   ierr = VecDuplicate(u,&uh);CHKERRQ(ierr);
2649da9b6338SBarry Smith   ierr = VecDuplicate(u,&fh);CHKERRQ(ierr);
2650da9b6338SBarry Smith 
2651da9b6338SBarry Smith   /* currently only works for sequential */
2652da9b6338SBarry Smith   ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n");
2653da9b6338SBarry Smith   ierr = VecGetSize(u,&N);CHKERRQ(ierr);
2654da9b6338SBarry Smith   for (i=0; i<N; i++) {
2655da9b6338SBarry Smith     ierr = VecCopy(u,uh);CHKERRQ(ierr);
265677431f27SBarry Smith     ierr  = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr);
2657da9b6338SBarry Smith     for (j=-10; j<11; j++) {
2658ccae9161SBarry Smith       value = PetscSign(j)*exp(PetscAbs(j)-10.0);
2659da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
26603ab0aad5SBarry Smith       ierr  = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr);
2661da9b6338SBarry Smith       ierr  = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr);
266277431f27SBarry Smith       ierr  = PetscPrintf(PETSC_COMM_WORLD,"       j norm %D %18.16e\n",j,norm);CHKERRQ(ierr);
2663da9b6338SBarry Smith       value = -value;
2664da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
2665da9b6338SBarry Smith     }
2666da9b6338SBarry Smith   }
2667da9b6338SBarry Smith   ierr = VecDestroy(uh);CHKERRQ(ierr);
2668da9b6338SBarry Smith   ierr = VecDestroy(fh);CHKERRQ(ierr);
2669da9b6338SBarry Smith   PetscFunctionReturn(0);
2670da9b6338SBarry Smith }
267171f87433Sdalcinl 
267271f87433Sdalcinl #undef __FUNCT__
2673fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW"
267471f87433Sdalcinl /*@
2675fa9f3622SBarry Smith    SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for
267671f87433Sdalcinl    computing relative tolerance for linear solvers within an inexact
267771f87433Sdalcinl    Newton method.
267871f87433Sdalcinl 
267971f87433Sdalcinl    Collective on SNES
268071f87433Sdalcinl 
268171f87433Sdalcinl    Input Parameters:
268271f87433Sdalcinl +  snes - SNES context
268371f87433Sdalcinl -  flag - PETSC_TRUE or PETSC_FALSE
268471f87433Sdalcinl 
268564ba62caSBarry Smith     Options Database:
268664ba62caSBarry Smith +  -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence
268764ba62caSBarry Smith .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
268864ba62caSBarry Smith .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
268964ba62caSBarry Smith .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
269064ba62caSBarry Smith .  -snes_ksp_ew_gamma <gamma> - Sets gamma
269164ba62caSBarry Smith .  -snes_ksp_ew_alpha <alpha> - Sets alpha
269264ba62caSBarry Smith .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
269364ba62caSBarry Smith -  -snes_ksp_ew_threshold <threshold> - Sets threshold
269464ba62caSBarry Smith 
269571f87433Sdalcinl    Notes:
269671f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
269771f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
269871f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
269971f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
270071f87433Sdalcinl    solver.
270171f87433Sdalcinl 
270271f87433Sdalcinl    Level: advanced
270371f87433Sdalcinl 
270471f87433Sdalcinl    Reference:
270571f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
270671f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
270771f87433Sdalcinl 
270871f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
270971f87433Sdalcinl 
2710fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
271171f87433Sdalcinl @*/
2712fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetUseEW(SNES snes,PetscTruth flag)
271371f87433Sdalcinl {
271471f87433Sdalcinl   PetscFunctionBegin;
27150700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
271671f87433Sdalcinl   snes->ksp_ewconv = flag;
271771f87433Sdalcinl   PetscFunctionReturn(0);
271871f87433Sdalcinl }
271971f87433Sdalcinl 
272071f87433Sdalcinl #undef __FUNCT__
2721fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW"
272271f87433Sdalcinl /*@
2723fa9f3622SBarry Smith    SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method
272471f87433Sdalcinl    for computing relative tolerance for linear solvers within an
272571f87433Sdalcinl    inexact Newton method.
272671f87433Sdalcinl 
272771f87433Sdalcinl    Not Collective
272871f87433Sdalcinl 
272971f87433Sdalcinl    Input Parameter:
273071f87433Sdalcinl .  snes - SNES context
273171f87433Sdalcinl 
273271f87433Sdalcinl    Output Parameter:
273371f87433Sdalcinl .  flag - PETSC_TRUE or PETSC_FALSE
273471f87433Sdalcinl 
273571f87433Sdalcinl    Notes:
273671f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
273771f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
273871f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
273971f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
274071f87433Sdalcinl    solver.
274171f87433Sdalcinl 
274271f87433Sdalcinl    Level: advanced
274371f87433Sdalcinl 
274471f87433Sdalcinl    Reference:
274571f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
274671f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
274771f87433Sdalcinl 
274871f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
274971f87433Sdalcinl 
2750fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
275171f87433Sdalcinl @*/
2752fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetUseEW(SNES snes, PetscTruth *flag)
275371f87433Sdalcinl {
275471f87433Sdalcinl   PetscFunctionBegin;
27550700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
275671f87433Sdalcinl   PetscValidPointer(flag,2);
275771f87433Sdalcinl   *flag = snes->ksp_ewconv;
275871f87433Sdalcinl   PetscFunctionReturn(0);
275971f87433Sdalcinl }
276071f87433Sdalcinl 
276171f87433Sdalcinl #undef __FUNCT__
2762fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW"
276371f87433Sdalcinl /*@
2764fa9f3622SBarry Smith    SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker
276571f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
276671f87433Sdalcinl    Newton method.
276771f87433Sdalcinl 
276871f87433Sdalcinl    Collective on SNES
276971f87433Sdalcinl 
277071f87433Sdalcinl    Input Parameters:
277171f87433Sdalcinl +    snes - SNES context
277271f87433Sdalcinl .    version - version 1, 2 (default is 2) or 3
277371f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
277471f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
277571f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
277671f87433Sdalcinl              (0 <= gamma2 <= 1)
277771f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
277871f87433Sdalcinl .    alpha2 - power for safeguard
277971f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
278071f87433Sdalcinl 
278171f87433Sdalcinl    Note:
278271f87433Sdalcinl    Version 3 was contributed by Luis Chacon, June 2006.
278371f87433Sdalcinl 
278471f87433Sdalcinl    Use PETSC_DEFAULT to retain the default for any of the parameters.
278571f87433Sdalcinl 
278671f87433Sdalcinl    Level: advanced
278771f87433Sdalcinl 
278871f87433Sdalcinl    Reference:
278971f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
279071f87433Sdalcinl    inexact Newton method", Utah State University Math. Stat. Dept. Res.
279171f87433Sdalcinl    Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput.
279271f87433Sdalcinl 
279371f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters
279471f87433Sdalcinl 
2795fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW()
279671f87433Sdalcinl @*/
2797fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max,
279871f87433Sdalcinl 							    PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold)
279971f87433Sdalcinl {
2800fa9f3622SBarry Smith   SNESKSPEW *kctx;
280171f87433Sdalcinl   PetscFunctionBegin;
28020700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2803fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
280471f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
280571f87433Sdalcinl 
280671f87433Sdalcinl   if (version != PETSC_DEFAULT)   kctx->version   = version;
280771f87433Sdalcinl   if (rtol_0 != PETSC_DEFAULT)    kctx->rtol_0    = rtol_0;
280871f87433Sdalcinl   if (rtol_max != PETSC_DEFAULT)  kctx->rtol_max  = rtol_max;
280971f87433Sdalcinl   if (gamma != PETSC_DEFAULT)     kctx->gamma     = gamma;
281071f87433Sdalcinl   if (alpha != PETSC_DEFAULT)     kctx->alpha     = alpha;
281171f87433Sdalcinl   if (alpha2 != PETSC_DEFAULT)    kctx->alpha2    = alpha2;
281271f87433Sdalcinl   if (threshold != PETSC_DEFAULT) kctx->threshold = threshold;
281371f87433Sdalcinl 
281471f87433Sdalcinl   if (kctx->version < 1 || kctx->version > 3) {
281571f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version);
281671f87433Sdalcinl   }
281771f87433Sdalcinl   if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) {
281871f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0);
281971f87433Sdalcinl   }
282071f87433Sdalcinl   if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) {
282171f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max);
282271f87433Sdalcinl   }
282371f87433Sdalcinl   if (kctx->gamma < 0.0 || kctx->gamma > 1.0) {
282471f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma);
282571f87433Sdalcinl   }
282671f87433Sdalcinl   if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) {
282771f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha);
282871f87433Sdalcinl   }
282971f87433Sdalcinl   if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) {
283071f87433Sdalcinl     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold);
283171f87433Sdalcinl   }
283271f87433Sdalcinl   PetscFunctionReturn(0);
283371f87433Sdalcinl }
283471f87433Sdalcinl 
283571f87433Sdalcinl #undef __FUNCT__
2836fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW"
283771f87433Sdalcinl /*@
2838fa9f3622SBarry Smith    SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker
283971f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
284071f87433Sdalcinl    Newton method.
284171f87433Sdalcinl 
284271f87433Sdalcinl    Not Collective
284371f87433Sdalcinl 
284471f87433Sdalcinl    Input Parameters:
284571f87433Sdalcinl      snes - SNES context
284671f87433Sdalcinl 
284771f87433Sdalcinl    Output Parameters:
284871f87433Sdalcinl +    version - version 1, 2 (default is 2) or 3
284971f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
285071f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
285171f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
285271f87433Sdalcinl              (0 <= gamma2 <= 1)
285371f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
285471f87433Sdalcinl .    alpha2 - power for safeguard
285571f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
285671f87433Sdalcinl 
285771f87433Sdalcinl    Level: advanced
285871f87433Sdalcinl 
285971f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters
286071f87433Sdalcinl 
2861fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW()
286271f87433Sdalcinl @*/
2863fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max,
286471f87433Sdalcinl 							    PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold)
286571f87433Sdalcinl {
2866fa9f3622SBarry Smith   SNESKSPEW *kctx;
286771f87433Sdalcinl   PetscFunctionBegin;
28680700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2869fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
287071f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
287171f87433Sdalcinl   if(version)   *version   = kctx->version;
287271f87433Sdalcinl   if(rtol_0)    *rtol_0    = kctx->rtol_0;
287371f87433Sdalcinl   if(rtol_max)  *rtol_max  = kctx->rtol_max;
287471f87433Sdalcinl   if(gamma)     *gamma     = kctx->gamma;
287571f87433Sdalcinl   if(alpha)     *alpha     = kctx->alpha;
287671f87433Sdalcinl   if(alpha2)    *alpha2    = kctx->alpha2;
287771f87433Sdalcinl   if(threshold) *threshold = kctx->threshold;
287871f87433Sdalcinl   PetscFunctionReturn(0);
287971f87433Sdalcinl }
288071f87433Sdalcinl 
288171f87433Sdalcinl #undef __FUNCT__
2882fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve"
2883fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x)
288471f87433Sdalcinl {
288571f87433Sdalcinl   PetscErrorCode ierr;
2886fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
288771f87433Sdalcinl   PetscReal      rtol=PETSC_DEFAULT,stol;
288871f87433Sdalcinl 
288971f87433Sdalcinl   PetscFunctionBegin;
289071f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
289171f87433Sdalcinl   if (!snes->iter) { /* first time in, so use the original user rtol */
289271f87433Sdalcinl     rtol = kctx->rtol_0;
289371f87433Sdalcinl   } else {
289471f87433Sdalcinl     if (kctx->version == 1) {
289571f87433Sdalcinl       rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last;
289671f87433Sdalcinl       if (rtol < 0.0) rtol = -rtol;
289771f87433Sdalcinl       stol = pow(kctx->rtol_last,kctx->alpha2);
289871f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
289971f87433Sdalcinl     } else if (kctx->version == 2) {
290071f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
290171f87433Sdalcinl       stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha);
290271f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
290371f87433Sdalcinl     } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */
290471f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
290571f87433Sdalcinl       /* safeguard: avoid sharp decrease of rtol */
290671f87433Sdalcinl       stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha);
290771f87433Sdalcinl       stol = PetscMax(rtol,stol);
290871f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
290971f87433Sdalcinl       /* safeguard: avoid oversolving */
291071f87433Sdalcinl       stol = kctx->gamma*(snes->ttol)/snes->norm;
291171f87433Sdalcinl       stol = PetscMax(rtol,stol);
291271f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
291371f87433Sdalcinl     } else SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version);
291471f87433Sdalcinl   }
291571f87433Sdalcinl   /* safeguard: avoid rtol greater than one */
291671f87433Sdalcinl   rtol = PetscMin(rtol,kctx->rtol_max);
291771f87433Sdalcinl   ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
291871f87433Sdalcinl   ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr);
291971f87433Sdalcinl   PetscFunctionReturn(0);
292071f87433Sdalcinl }
292171f87433Sdalcinl 
292271f87433Sdalcinl #undef __FUNCT__
2923fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve"
2924fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x)
292571f87433Sdalcinl {
292671f87433Sdalcinl   PetscErrorCode ierr;
2927fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
292871f87433Sdalcinl   PCSide         pcside;
292971f87433Sdalcinl   Vec            lres;
293071f87433Sdalcinl 
293171f87433Sdalcinl   PetscFunctionBegin;
293271f87433Sdalcinl   if (!kctx) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
293371f87433Sdalcinl   ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr);
293471f87433Sdalcinl   ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr);
293571f87433Sdalcinl   if (kctx->version == 1) {
2936b037da10SBarry Smith     ierr = KSPGetPCSide(ksp,&pcside);CHKERRQ(ierr);
293771f87433Sdalcinl     if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */
293871f87433Sdalcinl       /* KSP residual is true linear residual */
293971f87433Sdalcinl       ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr);
294071f87433Sdalcinl     } else {
294171f87433Sdalcinl       /* KSP residual is preconditioned residual */
294271f87433Sdalcinl       /* compute true linear residual norm */
294371f87433Sdalcinl       ierr = VecDuplicate(b,&lres);CHKERRQ(ierr);
294471f87433Sdalcinl       ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr);
294571f87433Sdalcinl       ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr);
294671f87433Sdalcinl       ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr);
294771f87433Sdalcinl       ierr = VecDestroy(lres);CHKERRQ(ierr);
294871f87433Sdalcinl     }
294971f87433Sdalcinl   }
295071f87433Sdalcinl   PetscFunctionReturn(0);
295171f87433Sdalcinl }
295271f87433Sdalcinl 
295371f87433Sdalcinl #undef __FUNCT__
295471f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve"
295571f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x)
295671f87433Sdalcinl {
295771f87433Sdalcinl   PetscErrorCode ierr;
295871f87433Sdalcinl 
295971f87433Sdalcinl   PetscFunctionBegin;
2960fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr);  }
296171f87433Sdalcinl   ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);
2962fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); }
296371f87433Sdalcinl   PetscFunctionReturn(0);
296471f87433Sdalcinl }
29656c699258SBarry Smith 
29666c699258SBarry Smith #undef __FUNCT__
29676c699258SBarry Smith #define __FUNCT__ "SNESSetDM"
29686c699258SBarry Smith /*@
29696c699258SBarry Smith    SNESSetDM - Sets the DM that may be used by some preconditioners
29706c699258SBarry Smith 
29716c699258SBarry Smith    Collective on SNES
29726c699258SBarry Smith 
29736c699258SBarry Smith    Input Parameters:
29746c699258SBarry Smith +  snes - the preconditioner context
29756c699258SBarry Smith -  dm - the dm
29766c699258SBarry Smith 
29776c699258SBarry Smith    Level: intermediate
29786c699258SBarry Smith 
29796c699258SBarry Smith 
29806c699258SBarry Smith .seealso: SNESGetDM(), KSPSetDM(), KSPGetDM()
29816c699258SBarry Smith @*/
29826c699258SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetDM(SNES snes,DM dm)
29836c699258SBarry Smith {
29846c699258SBarry Smith   PetscErrorCode ierr;
2985345fed2cSBarry Smith   KSP            ksp;
29866c699258SBarry Smith 
29876c699258SBarry Smith   PetscFunctionBegin;
29880700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
29896c699258SBarry Smith   if (snes->dm) {ierr = DMDestroy(snes->dm);CHKERRQ(ierr);}
29906c699258SBarry Smith   snes->dm = dm;
29916c699258SBarry Smith   ierr = PetscObjectReference((PetscObject)snes->dm);CHKERRQ(ierr);
2992345fed2cSBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
2993345fed2cSBarry Smith   ierr = KSPSetDM(ksp,dm);CHKERRQ(ierr);
2994*f22f69f0SBarry Smith   ierr = KSPSetDMActive(ksp,PETSC_FALSE);CHKERRQ(ierr);
29956c699258SBarry Smith   PetscFunctionReturn(0);
29966c699258SBarry Smith }
29976c699258SBarry Smith 
29986c699258SBarry Smith #undef __FUNCT__
29996c699258SBarry Smith #define __FUNCT__ "SNESGetDM"
30006c699258SBarry Smith /*@
30016c699258SBarry Smith    SNESGetDM - Gets the DM that may be used by some preconditioners
30026c699258SBarry Smith 
30036c699258SBarry Smith    Collective on SNES
30046c699258SBarry Smith 
30056c699258SBarry Smith    Input Parameter:
30066c699258SBarry Smith . snes - the preconditioner context
30076c699258SBarry Smith 
30086c699258SBarry Smith    Output Parameter:
30096c699258SBarry Smith .  dm - the dm
30106c699258SBarry Smith 
30116c699258SBarry Smith    Level: intermediate
30126c699258SBarry Smith 
30136c699258SBarry Smith 
30146c699258SBarry Smith .seealso: SNESSetDM(), KSPSetDM(), KSPGetDM()
30156c699258SBarry Smith @*/
30166c699258SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetDM(SNES snes,DM *dm)
30176c699258SBarry Smith {
30186c699258SBarry Smith   PetscFunctionBegin;
30190700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
30206c699258SBarry Smith   *dm = snes->dm;
30216c699258SBarry Smith   PetscFunctionReturn(0);
30226c699258SBarry Smith }
3023