xref: /petsc/src/snes/interface/snes.c (revision 70663e4a70994b7bf6a2e51d4030e794ec74df4c)
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) {
163e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,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) {
194e32f2f54SBarry Smith     if (!snes->vec_func) SETERRQ(PETSC_COMM_SELF,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
198e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP, "matrix-free operator rutines (version 2)");
199aa3661deSLisandro Dalcin #endif
200aa3661deSLisandro Dalcin   } else {
201e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,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);
111617186662SBarry Smith   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() before SNESComputeFunction(), likely called from SNESSolve().");
111785385478SLisandro Dalcin   if (snes->vec_rhs) {
111885385478SLisandro Dalcin     ierr = VecAXPY(y,-1.0,snes->vec_rhs);CHKERRQ(ierr);
11193ab0aad5SBarry Smith   }
1120ae3c334cSLois Curfman McInnes   snes->nfuncs++;
1121d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
11223a40ed3dSBarry Smith   PetscFunctionReturn(0);
11239b94acceSBarry Smith }
11249b94acceSBarry Smith 
11254a2ae208SSatish Balay #undef __FUNCT__
11264a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian"
112762fef451SLois Curfman McInnes /*@
112862fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
112962fef451SLois Curfman McInnes    set with SNESSetJacobian().
113062fef451SLois Curfman McInnes 
1131c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1132c7afd0dbSLois Curfman McInnes 
113362fef451SLois Curfman McInnes    Input Parameters:
1134c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1135c7afd0dbSLois Curfman McInnes -  x - input vector
113662fef451SLois Curfman McInnes 
113762fef451SLois Curfman McInnes    Output Parameters:
1138c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
113962fef451SLois Curfman McInnes .  B - optional preconditioning matrix
11402b668275SBarry Smith -  flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER)
1141fee21e36SBarry Smith 
1142e35cf81dSBarry Smith   Options Database Keys:
1143e35cf81dSBarry Smith +    -snes_lag_preconditioner <lag>
1144e35cf81dSBarry Smith -    -snes_lag_jacobian <lag>
1145e35cf81dSBarry Smith 
114662fef451SLois Curfman McInnes    Notes:
114762fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
114862fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
114962fef451SLois Curfman McInnes 
115094b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
1151dc5a77f8SLois Curfman McInnes    flag parameter.
115262fef451SLois Curfman McInnes 
115336851e7fSLois Curfman McInnes    Level: developer
115436851e7fSLois Curfman McInnes 
115562fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
115662fef451SLois Curfman McInnes 
1157e35cf81dSBarry Smith .seealso:  SNESSetJacobian(), KSPSetOperators(), MatStructure, SNESSetLagPreconditioner(), SNESSetLagJacobian()
115862fef451SLois Curfman McInnes @*/
115963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
11609b94acceSBarry Smith {
1161dfbe8321SBarry Smith   PetscErrorCode ierr;
1162ebd3b9afSBarry Smith   PetscTruth     flag;
11633a40ed3dSBarry Smith 
11643a40ed3dSBarry Smith   PetscFunctionBegin;
11650700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
11660700a824SBarry Smith   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
11674482741eSBarry Smith   PetscValidPointer(flg,5);
1168c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,X,2);
1169e7788613SBarry Smith   if (!snes->ops->computejacobian) PetscFunctionReturn(0);
1170ebd3b9afSBarry Smith 
1171ebd3b9afSBarry Smith   /* make sure that MatAssemblyBegin/End() is called on A matrix if it is matrix free */
1172ebd3b9afSBarry Smith 
1173fe3ffe1eSBarry Smith   if (snes->lagjacobian == -2) {
1174fe3ffe1eSBarry Smith     snes->lagjacobian = -1;
1175fe3ffe1eSBarry Smith     ierr = PetscInfo(snes,"Recomputing Jacobian/preconditioner because lag is -2 (means compute Jacobian, but then never again) \n");CHKERRQ(ierr);
1176fe3ffe1eSBarry Smith   } else if (snes->lagjacobian == -1) {
1177e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1178e35cf81dSBarry Smith     ierr = PetscInfo(snes,"Reusing Jacobian/preconditioner because lag is -1\n");CHKERRQ(ierr);
1179ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1180ebd3b9afSBarry Smith     if (flag) {
1181ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1182ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1183ebd3b9afSBarry Smith     }
1184e35cf81dSBarry Smith     PetscFunctionReturn(0);
1185e35cf81dSBarry Smith   } else if (snes->lagjacobian > 1 && snes->iter % snes->lagjacobian) {
1186e35cf81dSBarry Smith     *flg = SAME_PRECONDITIONER;
1187e35cf81dSBarry Smith     ierr = PetscInfo2(snes,"Reusing Jacobian/preconditioner because lag is %D and SNES iteration is %D\n",snes->lagjacobian,snes->iter);CHKERRQ(ierr);
1188ebd3b9afSBarry Smith     ierr = PetscTypeCompare((PetscObject)*A,MATMFFD,&flag);CHKERRQ(ierr);
1189ebd3b9afSBarry Smith     if (flag) {
1190ebd3b9afSBarry Smith       ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1191ebd3b9afSBarry Smith       ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
1192ebd3b9afSBarry Smith     }
1193e35cf81dSBarry Smith     PetscFunctionReturn(0);
1194e35cf81dSBarry Smith   }
1195e35cf81dSBarry Smith 
1196c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1197e35cf81dSBarry Smith   ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1198d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
1199dc67937bSBarry Smith   CHKMEMQ;
1200e7788613SBarry Smith   ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
1201dc67937bSBarry Smith   CHKMEMQ;
1202d64ed03dSBarry Smith   PetscStackPop;
1203d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1204a8054027SBarry Smith 
12053b4f5425SBarry Smith   if (snes->lagpreconditioner == -2) {
12063b4f5425SBarry Smith     ierr = PetscInfo(snes,"Rebuilding preconditioner exactly once since lag is -2\n");CHKERRQ(ierr);
12073b4f5425SBarry Smith     snes->lagpreconditioner = -1;
12083b4f5425SBarry Smith   } else if (snes->lagpreconditioner == -1) {
1209a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1210a8054027SBarry Smith     ierr = PetscInfo(snes,"Reusing preconditioner because lag is -1\n");CHKERRQ(ierr);
1211a8054027SBarry Smith   } else if (snes->lagpreconditioner > 1 && snes->iter % snes->lagpreconditioner) {
1212a8054027SBarry Smith     *flg = SAME_PRECONDITIONER;
1213a8054027SBarry Smith     ierr = PetscInfo2(snes,"Reusing preconditioner because lag is %D and SNES iteration is %D\n",snes->lagpreconditioner,snes->iter);CHKERRQ(ierr);
1214a8054027SBarry Smith   }
1215a8054027SBarry Smith 
12166d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
12170700a824SBarry Smith   /* PetscValidHeaderSpecific(*A,MAT_CLASSID,3);
12180700a824SBarry Smith     PetscValidHeaderSpecific(*B,MAT_CLASSID,4);   */
12193a40ed3dSBarry Smith   PetscFunctionReturn(0);
12209b94acceSBarry Smith }
12219b94acceSBarry Smith 
12224a2ae208SSatish Balay #undef __FUNCT__
12234a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian"
12249b94acceSBarry Smith /*@C
12259b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1226044dda88SLois Curfman McInnes    location to store the matrix.
12279b94acceSBarry Smith 
1228c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1229c7afd0dbSLois Curfman McInnes 
12309b94acceSBarry Smith    Input Parameters:
1231c7afd0dbSLois Curfman McInnes +  snes - the SNES context
12329b94acceSBarry Smith .  A - Jacobian matrix
12339b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
12349b94acceSBarry Smith .  func - Jacobian evaluation routine
1235c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
12362cd2dfdcSLois Curfman McInnes          Jacobian evaluation routine (may be PETSC_NULL)
12379b94acceSBarry Smith 
12389b94acceSBarry Smith    Calling sequence of func:
12398d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
12409b94acceSBarry Smith 
1241c7afd0dbSLois Curfman McInnes +  x - input vector
12429b94acceSBarry Smith .  A - Jacobian matrix
12439b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1244ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
12452b668275SBarry Smith    structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER
1246c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
12479b94acceSBarry Smith 
12489b94acceSBarry Smith    Notes:
124994b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
12502cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1251ac21db08SLois Curfman McInnes 
1252ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
12539b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
12549b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
12559b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
12569b94acceSBarry Smith    throughout the global iterations.
12579b94acceSBarry Smith 
125816913363SBarry Smith    If the A matrix and B matrix are different you must call MatAssemblyBegin/End() on
125916913363SBarry Smith    each matrix.
126016913363SBarry Smith 
126136851e7fSLois Curfman McInnes    Level: beginner
126236851e7fSLois Curfman McInnes 
12639b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
12649b94acceSBarry Smith 
12653ec795f1SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatMFFDComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure
12669b94acceSBarry Smith @*/
126763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
12689b94acceSBarry Smith {
1269dfbe8321SBarry Smith   PetscErrorCode ierr;
12703a7fca6bSBarry Smith 
12713a40ed3dSBarry Smith   PetscFunctionBegin;
12720700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
12730700a824SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_CLASSID,2);
12740700a824SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_CLASSID,3);
1275c9780b6fSBarry Smith   if (A) PetscCheckSameComm(snes,1,A,2);
127606975374SJed Brown   if (B) PetscCheckSameComm(snes,1,B,3);
1277e7788613SBarry Smith   if (func) snes->ops->computejacobian = func;
12783a7fca6bSBarry Smith   if (ctx)  snes->jacP                 = ctx;
12793a7fca6bSBarry Smith   if (A) {
12807dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
12813a7fca6bSBarry Smith     if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
12829b94acceSBarry Smith     snes->jacobian = A;
12833a7fca6bSBarry Smith   }
12843a7fca6bSBarry Smith   if (B) {
12857dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
12863a7fca6bSBarry Smith     if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
12879b94acceSBarry Smith     snes->jacobian_pre = B;
12883a7fca6bSBarry Smith   }
12893a40ed3dSBarry Smith   PetscFunctionReturn(0);
12909b94acceSBarry Smith }
129162fef451SLois Curfman McInnes 
12924a2ae208SSatish Balay #undef __FUNCT__
12934a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian"
1294c2aafc4cSSatish Balay /*@C
1295b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1296b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1297b4fd4287SBarry Smith 
1298c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1299c7afd0dbSLois Curfman McInnes 
1300b4fd4287SBarry Smith    Input Parameter:
1301b4fd4287SBarry Smith .  snes - the nonlinear solver context
1302b4fd4287SBarry Smith 
1303b4fd4287SBarry Smith    Output Parameters:
1304c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1305b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
130670e92668SMatthew Knepley .  func - location to put Jacobian function (or PETSC_NULL)
130770e92668SMatthew Knepley -  ctx - location to stash Jacobian ctx (or PETSC_NULL)
1308fee21e36SBarry Smith 
130936851e7fSLois Curfman McInnes    Level: advanced
131036851e7fSLois Curfman McInnes 
1311b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1312b4fd4287SBarry Smith @*/
131370e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx)
1314b4fd4287SBarry Smith {
13153a40ed3dSBarry Smith   PetscFunctionBegin;
13160700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1317b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
1318b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
1319e7788613SBarry Smith   if (func) *func = snes->ops->computejacobian;
132070e92668SMatthew Knepley   if (ctx)  *ctx  = snes->jacP;
13213a40ed3dSBarry Smith   PetscFunctionReturn(0);
1322b4fd4287SBarry Smith }
1323b4fd4287SBarry Smith 
13249b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
13259b94acceSBarry Smith 
13264a2ae208SSatish Balay #undef __FUNCT__
13274a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp"
13289b94acceSBarry Smith /*@
13299b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1330272ac6f2SLois Curfman McInnes    of a nonlinear solver.
13319b94acceSBarry Smith 
1332fee21e36SBarry Smith    Collective on SNES
1333fee21e36SBarry Smith 
1334c7afd0dbSLois Curfman McInnes    Input Parameters:
133570e92668SMatthew Knepley .  snes - the SNES context
1336c7afd0dbSLois Curfman McInnes 
1337272ac6f2SLois Curfman McInnes    Notes:
1338272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1339272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1340272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1341272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1342272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1343272ac6f2SLois Curfman McInnes 
134436851e7fSLois Curfman McInnes    Level: advanced
134536851e7fSLois Curfman McInnes 
13469b94acceSBarry Smith .keywords: SNES, nonlinear, setup
13479b94acceSBarry Smith 
13489b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
13499b94acceSBarry Smith @*/
135070e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUp(SNES snes)
13519b94acceSBarry Smith {
1352dfbe8321SBarry Smith   PetscErrorCode ierr;
13533a40ed3dSBarry Smith 
13543a40ed3dSBarry Smith   PetscFunctionBegin;
13550700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
13564dc4c822SBarry Smith   if (snes->setupcalled) PetscFunctionReturn(0);
13579b94acceSBarry Smith 
13587adad957SLisandro Dalcin   if (!((PetscObject)snes)->type_name) {
135985385478SLisandro Dalcin     ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr);
136085385478SLisandro Dalcin   }
136185385478SLisandro Dalcin 
136217186662SBarry Smith   if (!snes->vec_func && !snes->vec_rhs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
136317186662SBarry Smith   if (!snes->ops->computefunction && !snes->vec_rhs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
136417186662SBarry Smith   if (snes->vec_func == snes->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
1365ef8dffc7SBarry Smith   if (!snes->ops->computejacobian && snes->dm) {
1366ef8dffc7SBarry Smith     Mat           J;
1367ef8dffc7SBarry Smith     ISColoring    coloring;
1368ef8dffc7SBarry Smith     MatFDColoring fd;
1369a703fe33SLois Curfman McInnes 
1370ef8dffc7SBarry Smith     ierr = DMGetMatrix(snes->dm,MATAIJ,&J);CHKERRQ(ierr);
1371ef8dffc7SBarry Smith     ierr = DMGetColoring(snes->dm,IS_COLORING_GHOSTED,MATAIJ,&coloring);CHKERRQ(ierr);
1372ef8dffc7SBarry Smith     ierr = MatFDColoringCreate(J,coloring,&fd);CHKERRQ(ierr);
1373ef8dffc7SBarry Smith     ierr = MatFDColoringSetFunction(fd,(PetscErrorCode (*)(void))snes->ops->computefunction,snes->funP);CHKERRQ(ierr);
1374ef8dffc7SBarry Smith     ierr = SNESSetJacobian(snes,J,J,SNESDefaultComputeJacobianColor,fd);CHKERRQ(ierr);
1375ef8dffc7SBarry Smith     ierr = ISColoringDestroy(coloring);CHKERRQ(ierr);
1376ef8dffc7SBarry Smith   }
1377b710008aSBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes, &snes->ksp);CHKERRQ(ierr);}
1378b710008aSBarry Smith 
1379410397dcSLisandro Dalcin   if (snes->ops->setup) {
1380410397dcSLisandro Dalcin     ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr);
1381410397dcSLisandro Dalcin   }
13827aaed0d8SBarry Smith   snes->setupcalled = PETSC_TRUE;
13833a40ed3dSBarry Smith   PetscFunctionReturn(0);
13849b94acceSBarry Smith }
13859b94acceSBarry Smith 
13864a2ae208SSatish Balay #undef __FUNCT__
13874a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
138852baeb72SSatish Balay /*@
13899b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
13909b94acceSBarry Smith    with SNESCreate().
13919b94acceSBarry Smith 
1392c7afd0dbSLois Curfman McInnes    Collective on SNES
1393c7afd0dbSLois Curfman McInnes 
13949b94acceSBarry Smith    Input Parameter:
13959b94acceSBarry Smith .  snes - the SNES context
13969b94acceSBarry Smith 
139736851e7fSLois Curfman McInnes    Level: beginner
139836851e7fSLois Curfman McInnes 
13999b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
14009b94acceSBarry Smith 
140163a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
14029b94acceSBarry Smith @*/
140363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDestroy(SNES snes)
14049b94acceSBarry Smith {
14056849ba73SBarry Smith   PetscErrorCode ierr;
14063a40ed3dSBarry Smith 
14073a40ed3dSBarry Smith   PetscFunctionBegin;
14080700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
14097adad957SLisandro Dalcin   if (--((PetscObject)snes)->refct > 0) PetscFunctionReturn(0);
1410d4bb536fSBarry Smith 
1411be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
14120f5bd95cSBarry Smith   ierr = PetscObjectDepublish(snes);CHKERRQ(ierr);
14136c699258SBarry Smith 
14146c699258SBarry Smith   if (snes->dm) {ierr = DMDestroy(snes->dm);CHKERRQ(ierr);}
1415e7788613SBarry Smith   if (snes->ops->destroy) {ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr);}
141685385478SLisandro Dalcin 
141785385478SLisandro Dalcin   if (snes->vec_rhs) {ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr);}
141885385478SLisandro Dalcin   if (snes->vec_sol) {ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr);}
141985385478SLisandro Dalcin   if (snes->vec_func) {ierr = VecDestroy(snes->vec_func);CHKERRQ(ierr);}
14203a7fca6bSBarry Smith   if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
14213a7fca6bSBarry Smith   if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
14221cee3971SBarry Smith   if (snes->ksp) {ierr = KSPDestroy(snes->ksp);CHKERRQ(ierr);}
142385385478SLisandro Dalcin   ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);
1424522c5e43SBarry Smith   if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);}
1425a6570f20SBarry Smith   ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);
14267f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);}
1427a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr);
14283a40ed3dSBarry Smith   PetscFunctionReturn(0);
14299b94acceSBarry Smith }
14309b94acceSBarry Smith 
14319b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
14329b94acceSBarry Smith 
14334a2ae208SSatish Balay #undef __FUNCT__
1434a8054027SBarry Smith #define __FUNCT__ "SNESSetLagPreconditioner"
1435a8054027SBarry Smith /*@
1436a8054027SBarry Smith    SNESSetLagPreconditioner - Determines when the preconditioner is rebuilt in the nonlinear solve.
1437a8054027SBarry Smith 
1438a8054027SBarry Smith    Collective on SNES
1439a8054027SBarry Smith 
1440a8054027SBarry Smith    Input Parameters:
1441a8054027SBarry Smith +  snes - the SNES context
1442a8054027SBarry 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
14433b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1444a8054027SBarry Smith 
1445a8054027SBarry Smith    Options Database Keys:
1446a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1447a8054027SBarry Smith 
1448a8054027SBarry Smith    Notes:
1449a8054027SBarry Smith    The default is 1
1450a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1451a8054027SBarry Smith    If  -1 is used before the very first nonlinear solve the preconditioner is still built because there is no previous preconditioner to use
1452a8054027SBarry Smith 
1453a8054027SBarry Smith    Level: intermediate
1454a8054027SBarry Smith 
1455a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1456a8054027SBarry Smith 
1457e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagJacobian(), SNESGetLagJacobian()
1458a8054027SBarry Smith 
1459a8054027SBarry Smith @*/
1460a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagPreconditioner(SNES snes,PetscInt lag)
1461a8054027SBarry Smith {
1462a8054027SBarry Smith   PetscFunctionBegin;
14630700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1464e32f2f54SBarry Smith   if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1465e32f2f54SBarry Smith   if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1466a8054027SBarry Smith   snes->lagpreconditioner = lag;
1467a8054027SBarry Smith   PetscFunctionReturn(0);
1468a8054027SBarry Smith }
1469a8054027SBarry Smith 
1470a8054027SBarry Smith #undef __FUNCT__
1471a8054027SBarry Smith #define __FUNCT__ "SNESGetLagPreconditioner"
1472a8054027SBarry Smith /*@
1473a8054027SBarry Smith    SNESGetLagPreconditioner - Indicates how often the preconditioner is rebuilt
1474a8054027SBarry Smith 
1475a8054027SBarry Smith    Collective on SNES
1476a8054027SBarry Smith 
1477a8054027SBarry Smith    Input Parameter:
1478a8054027SBarry Smith .  snes - the SNES context
1479a8054027SBarry Smith 
1480a8054027SBarry Smith    Output Parameter:
1481a8054027SBarry 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
14823b4f5425SBarry Smith          the Jacobian is built etc. -2 indicates rebuild preconditioner at next chance but then never rebuild after that
1483a8054027SBarry Smith 
1484a8054027SBarry Smith    Options Database Keys:
1485a8054027SBarry Smith .    -snes_lag_preconditioner <lag>
1486a8054027SBarry Smith 
1487a8054027SBarry Smith    Notes:
1488a8054027SBarry Smith    The default is 1
1489a8054027SBarry Smith    The preconditioner is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1490a8054027SBarry Smith 
1491a8054027SBarry Smith    Level: intermediate
1492a8054027SBarry Smith 
1493a8054027SBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1494a8054027SBarry Smith 
1495a8054027SBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagPreconditioner()
1496a8054027SBarry Smith 
1497a8054027SBarry Smith @*/
1498a8054027SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagPreconditioner(SNES snes,PetscInt *lag)
1499a8054027SBarry Smith {
1500a8054027SBarry Smith   PetscFunctionBegin;
15010700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1502a8054027SBarry Smith   *lag = snes->lagpreconditioner;
1503a8054027SBarry Smith   PetscFunctionReturn(0);
1504a8054027SBarry Smith }
1505a8054027SBarry Smith 
1506a8054027SBarry Smith #undef __FUNCT__
1507e35cf81dSBarry Smith #define __FUNCT__ "SNESSetLagJacobian"
1508e35cf81dSBarry Smith /*@
1509e35cf81dSBarry Smith    SNESSetLagJacobian - Determines when the Jacobian is rebuilt in the nonlinear solve. See SNESSetLagPreconditioner() for determining how
1510e35cf81dSBarry Smith      often the preconditioner is rebuilt.
1511e35cf81dSBarry Smith 
1512e35cf81dSBarry Smith    Collective on SNES
1513e35cf81dSBarry Smith 
1514e35cf81dSBarry Smith    Input Parameters:
1515e35cf81dSBarry Smith +  snes - the SNES context
1516e35cf81dSBarry Smith -  lag - -1 indicates NEVER rebuild, 1 means rebuild every time the Jacobian is computed within a single nonlinear solve, 2 means every second time
1517fe3ffe1eSBarry Smith          the Jacobian is built etc. -2 means rebuild at next chance but then never again
1518e35cf81dSBarry Smith 
1519e35cf81dSBarry Smith    Options Database Keys:
1520e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1521e35cf81dSBarry Smith 
1522e35cf81dSBarry Smith    Notes:
1523e35cf81dSBarry Smith    The default is 1
1524e35cf81dSBarry Smith    The Jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1525fe3ffe1eSBarry 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
1526fe3ffe1eSBarry Smith    at the next Newton step but never again (unless it is reset to another value)
1527e35cf81dSBarry Smith 
1528e35cf81dSBarry Smith    Level: intermediate
1529e35cf81dSBarry Smith 
1530e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1531e35cf81dSBarry Smith 
1532e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESGetLagPreconditioner(), SNESSetLagPreconditioner(), SNESGetLagJacobian()
1533e35cf81dSBarry Smith 
1534e35cf81dSBarry Smith @*/
1535e35cf81dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetLagJacobian(SNES snes,PetscInt lag)
1536e35cf81dSBarry Smith {
1537e35cf81dSBarry Smith   PetscFunctionBegin;
15380700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1539e32f2f54SBarry Smith   if (lag < -2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag must be -2, -1, 1 or greater");
1540e32f2f54SBarry Smith   if (!lag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Lag cannot be 0");
1541e35cf81dSBarry Smith   snes->lagjacobian = lag;
1542e35cf81dSBarry Smith   PetscFunctionReturn(0);
1543e35cf81dSBarry Smith }
1544e35cf81dSBarry Smith 
1545e35cf81dSBarry Smith #undef __FUNCT__
1546e35cf81dSBarry Smith #define __FUNCT__ "SNESGetLagJacobian"
1547e35cf81dSBarry Smith /*@
1548e35cf81dSBarry Smith    SNESGetLagJacobian - Indicates how often the Jacobian is rebuilt. See SNESGetLagPreconditioner() to determine when the preconditioner is rebuilt
1549e35cf81dSBarry Smith 
1550e35cf81dSBarry Smith    Collective on SNES
1551e35cf81dSBarry Smith 
1552e35cf81dSBarry Smith    Input Parameter:
1553e35cf81dSBarry Smith .  snes - the SNES context
1554e35cf81dSBarry Smith 
1555e35cf81dSBarry Smith    Output Parameter:
1556e35cf81dSBarry 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
1557e35cf81dSBarry Smith          the Jacobian is built etc.
1558e35cf81dSBarry Smith 
1559e35cf81dSBarry Smith    Options Database Keys:
1560e35cf81dSBarry Smith .    -snes_lag_jacobian <lag>
1561e35cf81dSBarry Smith 
1562e35cf81dSBarry Smith    Notes:
1563e35cf81dSBarry Smith    The default is 1
1564e35cf81dSBarry Smith    The jacobian is ALWAYS built in the first iteration of a nonlinear solve unless lag is -1
1565e35cf81dSBarry Smith 
1566e35cf81dSBarry Smith    Level: intermediate
1567e35cf81dSBarry Smith 
1568e35cf81dSBarry Smith .keywords: SNES, nonlinear, set, convergence, tolerances
1569e35cf81dSBarry Smith 
1570e35cf81dSBarry Smith .seealso: SNESSetTrustRegionTolerance(), SNESSetLagJacobian(), SNESSetLagPreconditioner(), SNESGetLagPreconditioner()
1571e35cf81dSBarry Smith 
1572e35cf81dSBarry Smith @*/
1573e35cf81dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLagJacobian(SNES snes,PetscInt *lag)
1574e35cf81dSBarry Smith {
1575e35cf81dSBarry Smith   PetscFunctionBegin;
15760700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1577e35cf81dSBarry Smith   *lag = snes->lagjacobian;
1578e35cf81dSBarry Smith   PetscFunctionReturn(0);
1579e35cf81dSBarry Smith }
1580e35cf81dSBarry Smith 
1581e35cf81dSBarry Smith #undef __FUNCT__
15824a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
15839b94acceSBarry Smith /*@
1584d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
15859b94acceSBarry Smith 
1586c7afd0dbSLois Curfman McInnes    Collective on SNES
1587c7afd0dbSLois Curfman McInnes 
15889b94acceSBarry Smith    Input Parameters:
1589c7afd0dbSLois Curfman McInnes +  snes - the SNES context
159070441072SBarry Smith .  abstol - absolute convergence tolerance
159133174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
159233174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
159333174efeSLois Curfman McInnes            of the change in the solution between steps
159433174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1595c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1596fee21e36SBarry Smith 
159733174efeSLois Curfman McInnes    Options Database Keys:
159870441072SBarry Smith +    -snes_atol <abstol> - Sets abstol
1599c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1600c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1601c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1602c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
16039b94acceSBarry Smith 
1604d7a720efSLois Curfman McInnes    Notes:
16059b94acceSBarry Smith    The default maximum number of iterations is 50.
16069b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
16079b94acceSBarry Smith 
160836851e7fSLois Curfman McInnes    Level: intermediate
160936851e7fSLois Curfman McInnes 
161033174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
16119b94acceSBarry Smith 
16122492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance()
16139b94acceSBarry Smith @*/
161463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf)
16159b94acceSBarry Smith {
16163a40ed3dSBarry Smith   PetscFunctionBegin;
16170700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
161870441072SBarry Smith   if (abstol != PETSC_DEFAULT)  snes->abstol      = abstol;
1619d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1620d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1621d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1622d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
16233a40ed3dSBarry Smith   PetscFunctionReturn(0);
16249b94acceSBarry Smith }
16259b94acceSBarry Smith 
16264a2ae208SSatish Balay #undef __FUNCT__
16274a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
16289b94acceSBarry Smith /*@
162933174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
163033174efeSLois Curfman McInnes 
1631c7afd0dbSLois Curfman McInnes    Not Collective
1632c7afd0dbSLois Curfman McInnes 
163333174efeSLois Curfman McInnes    Input Parameters:
1634c7afd0dbSLois Curfman McInnes +  snes - the SNES context
163585385478SLisandro Dalcin .  atol - absolute convergence tolerance
163633174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
163733174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
163833174efeSLois Curfman McInnes            of the change in the solution between steps
163933174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1640c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1641fee21e36SBarry Smith 
164233174efeSLois Curfman McInnes    Notes:
164333174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
164433174efeSLois Curfman McInnes 
164536851e7fSLois Curfman McInnes    Level: intermediate
164636851e7fSLois Curfman McInnes 
164733174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
164833174efeSLois Curfman McInnes 
164933174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
165033174efeSLois Curfman McInnes @*/
165185385478SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf)
165233174efeSLois Curfman McInnes {
16533a40ed3dSBarry Smith   PetscFunctionBegin;
16540700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
165585385478SLisandro Dalcin   if (atol)  *atol  = snes->abstol;
165633174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
165733174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
165833174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
165933174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
16603a40ed3dSBarry Smith   PetscFunctionReturn(0);
166133174efeSLois Curfman McInnes }
166233174efeSLois Curfman McInnes 
16634a2ae208SSatish Balay #undef __FUNCT__
16644a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
166533174efeSLois Curfman McInnes /*@
16669b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
16679b94acceSBarry Smith 
1668fee21e36SBarry Smith    Collective on SNES
1669fee21e36SBarry Smith 
1670c7afd0dbSLois Curfman McInnes    Input Parameters:
1671c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1672c7afd0dbSLois Curfman McInnes -  tol - tolerance
1673c7afd0dbSLois Curfman McInnes 
16749b94acceSBarry Smith    Options Database Key:
1675c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
16769b94acceSBarry Smith 
167736851e7fSLois Curfman McInnes    Level: intermediate
167836851e7fSLois Curfman McInnes 
16799b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
16809b94acceSBarry Smith 
16812492ecdbSBarry Smith .seealso: SNESSetTolerances()
16829b94acceSBarry Smith @*/
168363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
16849b94acceSBarry Smith {
16853a40ed3dSBarry Smith   PetscFunctionBegin;
16860700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
16879b94acceSBarry Smith   snes->deltatol = tol;
16883a40ed3dSBarry Smith   PetscFunctionReturn(0);
16899b94acceSBarry Smith }
16909b94acceSBarry Smith 
1691df9fa365SBarry Smith /*
1692df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1693df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1694df9fa365SBarry Smith    macros instead of functions
1695df9fa365SBarry Smith */
16964a2ae208SSatish Balay #undef __FUNCT__
1697a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG"
1698a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx)
1699ce1608b8SBarry Smith {
1700dfbe8321SBarry Smith   PetscErrorCode ierr;
1701ce1608b8SBarry Smith 
1702ce1608b8SBarry Smith   PetscFunctionBegin;
17030700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1704a6570f20SBarry Smith   ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1705ce1608b8SBarry Smith   PetscFunctionReturn(0);
1706ce1608b8SBarry Smith }
1707ce1608b8SBarry Smith 
17084a2ae208SSatish Balay #undef __FUNCT__
1709a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate"
1710a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1711df9fa365SBarry Smith {
1712dfbe8321SBarry Smith   PetscErrorCode ierr;
1713df9fa365SBarry Smith 
1714df9fa365SBarry Smith   PetscFunctionBegin;
1715a6570f20SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1716df9fa365SBarry Smith   PetscFunctionReturn(0);
1717df9fa365SBarry Smith }
1718df9fa365SBarry Smith 
17194a2ae208SSatish Balay #undef __FUNCT__
1720a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy"
1721a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGDestroy(PetscDrawLG draw)
1722df9fa365SBarry Smith {
1723dfbe8321SBarry Smith   PetscErrorCode ierr;
1724df9fa365SBarry Smith 
1725df9fa365SBarry Smith   PetscFunctionBegin;
1726a6570f20SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
1727df9fa365SBarry Smith   PetscFunctionReturn(0);
1728df9fa365SBarry Smith }
1729df9fa365SBarry Smith 
1730b271bb04SBarry Smith extern PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorRange_Private(SNES,PetscInt,PetscReal*);
1731b271bb04SBarry Smith #undef __FUNCT__
1732b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRange"
1733b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRange(SNES snes,PetscInt n,PetscReal rnorm,void *monctx)
1734b271bb04SBarry Smith {
1735b271bb04SBarry Smith   PetscDrawLG      lg;
1736b271bb04SBarry Smith   PetscErrorCode   ierr;
1737b271bb04SBarry Smith   PetscReal        x,y,per;
1738b271bb04SBarry Smith   PetscViewer      v = (PetscViewer)monctx;
1739b271bb04SBarry Smith   static PetscReal prev; /* should be in the context */
1740b271bb04SBarry Smith   PetscDraw        draw;
1741b271bb04SBarry Smith   PetscFunctionBegin;
1742b271bb04SBarry Smith   if (!monctx) {
1743b271bb04SBarry Smith     MPI_Comm    comm;
1744b271bb04SBarry Smith 
1745b271bb04SBarry Smith     ierr   = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr);
1746b271bb04SBarry Smith     v      = PETSC_VIEWER_DRAW_(comm);
1747b271bb04SBarry Smith   }
1748b271bb04SBarry Smith   ierr   = PetscViewerDrawGetDrawLG(v,0,&lg);CHKERRQ(ierr);
1749b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1750b271bb04SBarry Smith   ierr   = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1751b271bb04SBarry Smith   ierr   = PetscDrawSetTitle(draw,"Residual norm");CHKERRQ(ierr);
1752b271bb04SBarry Smith   x = (PetscReal) n;
1753b271bb04SBarry Smith   if (rnorm > 0.0) y = log10(rnorm); else y = -15.0;
1754b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1755b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1756b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1757b271bb04SBarry Smith   }
1758b271bb04SBarry Smith 
1759b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,1,&lg);CHKERRQ(ierr);
1760b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1761b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1762b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"% elemts > .2*max elemt");CHKERRQ(ierr);
1763b271bb04SBarry Smith   ierr =  SNESMonitorRange_Private(snes,n,&per);CHKERRQ(ierr);
1764b271bb04SBarry Smith   x = (PetscReal) n;
1765b271bb04SBarry Smith   y = 100.0*per;
1766b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1767b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1768b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1769b271bb04SBarry Smith   }
1770b271bb04SBarry Smith 
1771b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,2,&lg);CHKERRQ(ierr);
1772b271bb04SBarry Smith   if (!n) {prev = rnorm;ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1773b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1774b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm");CHKERRQ(ierr);
1775b271bb04SBarry Smith   x = (PetscReal) n;
1776b271bb04SBarry Smith   y = (prev - rnorm)/prev;
1777b271bb04SBarry Smith   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1778b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1779b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1780b271bb04SBarry Smith   }
1781b271bb04SBarry Smith 
1782b271bb04SBarry Smith   ierr = PetscViewerDrawGetDrawLG(v,3,&lg);CHKERRQ(ierr);
1783b271bb04SBarry Smith   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
1784b271bb04SBarry Smith   ierr = PetscDrawLGGetDraw(lg,&draw);CHKERRQ(ierr);
1785b271bb04SBarry Smith   ierr = PetscDrawSetTitle(draw,"(norm -oldnorm)/oldnorm*(% > .2 max)");CHKERRQ(ierr);
1786b271bb04SBarry Smith   x = (PetscReal) n;
1787b271bb04SBarry Smith   y = (prev - rnorm)/(prev*per);
1788b271bb04SBarry Smith   if (n > 2) { /*skip initial crazy value */
1789b271bb04SBarry Smith     ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
1790b271bb04SBarry Smith   }
1791b271bb04SBarry Smith   if (n < 20 || !(n % 5)) {
1792b271bb04SBarry Smith     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
1793b271bb04SBarry Smith   }
1794b271bb04SBarry Smith   prev = rnorm;
1795b271bb04SBarry Smith   PetscFunctionReturn(0);
1796b271bb04SBarry Smith }
1797b271bb04SBarry Smith 
1798b271bb04SBarry Smith #undef __FUNCT__
1799b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeCreate"
1800b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRangeCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1801b271bb04SBarry Smith {
1802b271bb04SBarry Smith   PetscErrorCode ierr;
1803b271bb04SBarry Smith 
1804b271bb04SBarry Smith   PetscFunctionBegin;
1805b271bb04SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1806b271bb04SBarry Smith   PetscFunctionReturn(0);
1807b271bb04SBarry Smith }
1808b271bb04SBarry Smith 
1809b271bb04SBarry Smith #undef __FUNCT__
1810b271bb04SBarry Smith #define __FUNCT__ "SNESMonitorLGRangeDestroy"
1811b271bb04SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGRangeDestroy(PetscDrawLG draw)
1812b271bb04SBarry Smith {
1813b271bb04SBarry Smith   PetscErrorCode ierr;
1814b271bb04SBarry Smith 
1815b271bb04SBarry Smith   PetscFunctionBegin;
1816b271bb04SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
1817b271bb04SBarry Smith   PetscFunctionReturn(0);
1818b271bb04SBarry Smith }
1819b271bb04SBarry Smith 
18209b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
18219b94acceSBarry Smith 
18224a2ae208SSatish Balay #undef __FUNCT__
1823a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet"
18249b94acceSBarry Smith /*@C
1825a6570f20SBarry Smith    SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every
18269b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
18279b94acceSBarry Smith    progress.
18289b94acceSBarry Smith 
1829fee21e36SBarry Smith    Collective on SNES
1830fee21e36SBarry Smith 
1831c7afd0dbSLois Curfman McInnes    Input Parameters:
1832c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1833c7afd0dbSLois Curfman McInnes .  func - monitoring routine
1834b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
1835e8105e01SRichard Katz           monitor routine (use PETSC_NULL if no context is desired)
1836b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1837b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
18389b94acceSBarry Smith 
1839c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1840a7cc72afSBarry Smith $     int func(SNES snes,PetscInt its, PetscReal norm,void *mctx)
1841c7afd0dbSLois Curfman McInnes 
1842c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1843c7afd0dbSLois Curfman McInnes .    its - iteration number
1844c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
184540a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
18469b94acceSBarry Smith 
18479665c990SLois Curfman McInnes    Options Database Keys:
1848a6570f20SBarry Smith +    -snes_monitor        - sets SNESMonitorDefault()
1849a6570f20SBarry Smith .    -snes_monitor_draw    - sets line graph monitor,
1850a6570f20SBarry Smith                             uses SNESMonitorLGCreate()
1851a6570f20SBarry Smith _    -snes_monitor_cancel - cancels all monitors that have
1852c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
1853a6570f20SBarry Smith                             calls to SNESMonitorSet(), but
1854c7afd0dbSLois Curfman McInnes                             does not cancel those set via
1855c7afd0dbSLois Curfman McInnes                             the options database.
18569665c990SLois Curfman McInnes 
1857639f9d9dSBarry Smith    Notes:
18586bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
1859a6570f20SBarry Smith    SNESMonitorSet() multiple times; all will be called in the
18606bc08f3fSLois Curfman McInnes    order in which they were set.
1861639f9d9dSBarry Smith 
1862025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each SNES object
1863025f1a04SBarry Smith 
186436851e7fSLois Curfman McInnes    Level: intermediate
186536851e7fSLois Curfman McInnes 
18669b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
18679b94acceSBarry Smith 
1868a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel()
18699b94acceSBarry Smith @*/
1870b90d0a6eSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorSet(SNES snes,PetscErrorCode (*monitor)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void*))
18719b94acceSBarry Smith {
1872b90d0a6eSBarry Smith   PetscInt i;
1873b90d0a6eSBarry Smith 
18743a40ed3dSBarry Smith   PetscFunctionBegin;
18750700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
187617186662SBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1877b90d0a6eSBarry Smith   for (i=0; i<snes->numbermonitors;i++) {
1878b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitordestroy == snes->monitordestroy[i] && mctx == snes->monitorcontext[i]) PetscFunctionReturn(0);
1879b90d0a6eSBarry Smith 
1880b90d0a6eSBarry Smith     /* check if both default monitors that share common ASCII viewer */
1881b90d0a6eSBarry Smith     if (monitor == snes->monitor[i] && monitor == SNESMonitorDefault) {
1882b90d0a6eSBarry Smith       if (mctx && snes->monitorcontext[i]) {
1883b90d0a6eSBarry Smith         PetscErrorCode          ierr;
1884b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer1 = (PetscViewerASCIIMonitor) mctx;
1885b90d0a6eSBarry Smith         PetscViewerASCIIMonitor viewer2 = (PetscViewerASCIIMonitor) snes->monitorcontext[i];
1886b90d0a6eSBarry Smith         if (viewer1->viewer == viewer2->viewer) {
1887b90d0a6eSBarry Smith           ierr = (*monitordestroy)(mctx);CHKERRQ(ierr);
1888b90d0a6eSBarry Smith           PetscFunctionReturn(0);
1889b90d0a6eSBarry Smith         }
1890b90d0a6eSBarry Smith       }
1891b90d0a6eSBarry Smith     }
1892b90d0a6eSBarry Smith   }
1893b90d0a6eSBarry Smith   snes->monitor[snes->numbermonitors]           = monitor;
1894b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
1895639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
18963a40ed3dSBarry Smith   PetscFunctionReturn(0);
18979b94acceSBarry Smith }
18989b94acceSBarry Smith 
18994a2ae208SSatish Balay #undef __FUNCT__
1900a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel"
19015cd90555SBarry Smith /*@C
1902a6570f20SBarry Smith    SNESMonitorCancel - Clears all the monitor functions for a SNES object.
19035cd90555SBarry Smith 
1904c7afd0dbSLois Curfman McInnes    Collective on SNES
1905c7afd0dbSLois Curfman McInnes 
19065cd90555SBarry Smith    Input Parameters:
19075cd90555SBarry Smith .  snes - the SNES context
19085cd90555SBarry Smith 
19091a480d89SAdministrator    Options Database Key:
1910a6570f20SBarry Smith .  -snes_monitor_cancel - cancels all monitors that have been hardwired
1911a6570f20SBarry Smith     into a code by calls to SNESMonitorSet(), but does not cancel those
1912c7afd0dbSLois Curfman McInnes     set via the options database
19135cd90555SBarry Smith 
19145cd90555SBarry Smith    Notes:
19155cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
19165cd90555SBarry Smith 
191736851e7fSLois Curfman McInnes    Level: intermediate
191836851e7fSLois Curfman McInnes 
19195cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
19205cd90555SBarry Smith 
1921a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet()
19225cd90555SBarry Smith @*/
1923a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorCancel(SNES snes)
19245cd90555SBarry Smith {
1925d952e501SBarry Smith   PetscErrorCode ierr;
1926d952e501SBarry Smith   PetscInt       i;
1927d952e501SBarry Smith 
19285cd90555SBarry Smith   PetscFunctionBegin;
19290700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
1930d952e501SBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
1931d952e501SBarry Smith     if (snes->monitordestroy[i]) {
1932d952e501SBarry Smith       ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr);
1933d952e501SBarry Smith     }
1934d952e501SBarry Smith   }
19355cd90555SBarry Smith   snes->numbermonitors = 0;
19365cd90555SBarry Smith   PetscFunctionReturn(0);
19375cd90555SBarry Smith }
19385cd90555SBarry Smith 
19394a2ae208SSatish Balay #undef __FUNCT__
19404a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
19419b94acceSBarry Smith /*@C
19429b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
19439b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
19449b94acceSBarry Smith 
1945fee21e36SBarry Smith    Collective on SNES
1946fee21e36SBarry Smith 
1947c7afd0dbSLois Curfman McInnes    Input Parameters:
1948c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1949c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
19507f7931b9SBarry Smith .  cctx - [optional] context for private data for the convergence routine  (may be PETSC_NULL)
19517f7931b9SBarry Smith -  destroy - [optional] destructor for the context (may be PETSC_NULL; PETSC_NULL_FUNCTION in Fortran)
19529b94acceSBarry Smith 
1953c7afd0dbSLois Curfman McInnes    Calling sequence of func:
195406ee9f85SBarry Smith $     PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
1955c7afd0dbSLois Curfman McInnes 
1956c7afd0dbSLois Curfman McInnes +    snes - the SNES context
195706ee9f85SBarry Smith .    it - current iteration (0 is the first and is before any Newton step)
1958c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
1959184914b5SBarry Smith .    reason - reason for convergence/divergence
1960c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
19614b27c08aSLois Curfman McInnes .    gnorm - 2-norm of current step
19624b27c08aSLois Curfman McInnes -    f - 2-norm of function
19639b94acceSBarry Smith 
196436851e7fSLois Curfman McInnes    Level: advanced
196536851e7fSLois Curfman McInnes 
19669b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
19679b94acceSBarry Smith 
196885385478SLisandro Dalcin .seealso: SNESDefaultConverged(), SNESSkipConverged()
19699b94acceSBarry Smith @*/
19707f7931b9SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx,PetscErrorCode (*destroy)(void*))
19719b94acceSBarry Smith {
19727f7931b9SBarry Smith   PetscErrorCode ierr;
19737f7931b9SBarry Smith 
19743a40ed3dSBarry Smith   PetscFunctionBegin;
19750700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
197685385478SLisandro Dalcin   if (!func) func = SNESSkipConverged;
19777f7931b9SBarry Smith   if (snes->ops->convergeddestroy) {
19787f7931b9SBarry Smith     ierr = (*snes->ops->convergeddestroy)(snes->cnvP);CHKERRQ(ierr);
19797f7931b9SBarry Smith   }
198085385478SLisandro Dalcin   snes->ops->converged        = func;
19817f7931b9SBarry Smith   snes->ops->convergeddestroy = destroy;
198285385478SLisandro Dalcin   snes->cnvP                  = cctx;
19833a40ed3dSBarry Smith   PetscFunctionReturn(0);
19849b94acceSBarry Smith }
19859b94acceSBarry Smith 
19864a2ae208SSatish Balay #undef __FUNCT__
19874a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
198852baeb72SSatish Balay /*@
1989184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
1990184914b5SBarry Smith 
1991184914b5SBarry Smith    Not Collective
1992184914b5SBarry Smith 
1993184914b5SBarry Smith    Input Parameter:
1994184914b5SBarry Smith .  snes - the SNES context
1995184914b5SBarry Smith 
1996184914b5SBarry Smith    Output Parameter:
19974d0a8057SBarry Smith .  reason - negative value indicates diverged, positive value converged, see SNESConvergedReason or the
1998184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
1999184914b5SBarry Smith 
2000184914b5SBarry Smith    Level: intermediate
2001184914b5SBarry Smith 
2002184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
2003184914b5SBarry Smith 
2004184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
2005184914b5SBarry Smith 
200685385478SLisandro Dalcin .seealso: SNESSetConvergenceTest(), SNESConvergedReason
2007184914b5SBarry Smith @*/
200863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
2009184914b5SBarry Smith {
2010184914b5SBarry Smith   PetscFunctionBegin;
20110700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
20124482741eSBarry Smith   PetscValidPointer(reason,2);
2013184914b5SBarry Smith   *reason = snes->reason;
2014184914b5SBarry Smith   PetscFunctionReturn(0);
2015184914b5SBarry Smith }
2016184914b5SBarry Smith 
20174a2ae208SSatish Balay #undef __FUNCT__
20184a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
2019c9005455SLois Curfman McInnes /*@
2020c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
2021c9005455SLois Curfman McInnes 
2022fee21e36SBarry Smith    Collective on SNES
2023fee21e36SBarry Smith 
2024c7afd0dbSLois Curfman McInnes    Input Parameters:
2025c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
20268c7482ecSBarry Smith .  a   - array to hold history, this array will contain the function norms computed at each step
2027cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
2028758f92a0SBarry Smith .  na  - size of a and its
202964731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
2030758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
2031c7afd0dbSLois Curfman McInnes 
2032c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
2033c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
2034c9005455SLois Curfman McInnes    during the section of code that is being timed.
2035c9005455SLois Curfman McInnes 
203636851e7fSLois Curfman McInnes    Level: intermediate
203736851e7fSLois Curfman McInnes 
2038c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
2039758f92a0SBarry Smith 
204008405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
2041758f92a0SBarry Smith 
2042c9005455SLois Curfman McInnes @*/
2043a562a398SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscTruth reset)
2044c9005455SLois Curfman McInnes {
20453a40ed3dSBarry Smith   PetscFunctionBegin;
20460700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
20474482741eSBarry Smith   if (na)  PetscValidScalarPointer(a,2);
2048a562a398SLisandro Dalcin   if (its) PetscValidIntPointer(its,3);
2049c9005455SLois Curfman McInnes   snes->conv_hist       = a;
2050758f92a0SBarry Smith   snes->conv_hist_its   = its;
2051758f92a0SBarry Smith   snes->conv_hist_max   = na;
2052a12bc48fSLisandro Dalcin   snes->conv_hist_len   = 0;
2053758f92a0SBarry Smith   snes->conv_hist_reset = reset;
2054758f92a0SBarry Smith   PetscFunctionReturn(0);
2055758f92a0SBarry Smith }
2056758f92a0SBarry Smith 
20574a2ae208SSatish Balay #undef __FUNCT__
20584a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
20590c4c9dddSBarry Smith /*@C
2060758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
2061758f92a0SBarry Smith 
2062758f92a0SBarry Smith    Collective on SNES
2063758f92a0SBarry Smith 
2064758f92a0SBarry Smith    Input Parameter:
2065758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
2066758f92a0SBarry Smith 
2067758f92a0SBarry Smith    Output Parameters:
2068758f92a0SBarry Smith .  a   - array to hold history
2069758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
2070758f92a0SBarry Smith          negative if not converged) for each solve.
2071758f92a0SBarry Smith -  na  - size of a and its
2072758f92a0SBarry Smith 
2073758f92a0SBarry Smith    Notes:
2074758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
2075758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
2076758f92a0SBarry Smith 
2077758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
2078758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
2079758f92a0SBarry Smith    during the section of code that is being timed.
2080758f92a0SBarry Smith 
2081758f92a0SBarry Smith    Level: intermediate
2082758f92a0SBarry Smith 
2083758f92a0SBarry Smith .keywords: SNES, get, convergence, history
2084758f92a0SBarry Smith 
2085758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
2086758f92a0SBarry Smith 
2087758f92a0SBarry Smith @*/
208863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na)
2089758f92a0SBarry Smith {
2090758f92a0SBarry Smith   PetscFunctionBegin;
20910700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2092758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
2093758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
2094758f92a0SBarry Smith   if (na)  *na  = snes->conv_hist_len;
20953a40ed3dSBarry Smith   PetscFunctionReturn(0);
2096c9005455SLois Curfman McInnes }
2097c9005455SLois Curfman McInnes 
2098e74ef692SMatthew Knepley #undef __FUNCT__
2099e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
2100ac226902SBarry Smith /*@C
210176b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
21027e4bb74cSBarry Smith   at the beginning o every iteration of the nonlinear solve. Specifically
21037e4bb74cSBarry Smith   it is called just before the Jacobian is "evaluated".
210476b2cf59SMatthew Knepley 
210576b2cf59SMatthew Knepley   Collective on SNES
210676b2cf59SMatthew Knepley 
210776b2cf59SMatthew Knepley   Input Parameters:
210876b2cf59SMatthew Knepley . snes - The nonlinear solver context
210976b2cf59SMatthew Knepley . func - The function
211076b2cf59SMatthew Knepley 
211176b2cf59SMatthew Knepley   Calling sequence of func:
2112b5d30489SBarry Smith . func (SNES snes, PetscInt step);
211376b2cf59SMatthew Knepley 
211476b2cf59SMatthew Knepley . step - The current step of the iteration
211576b2cf59SMatthew Knepley 
2116fe97e370SBarry Smith   Level: advanced
2117fe97e370SBarry Smith 
2118fe97e370SBarry 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()
2119fe97e370SBarry Smith         This is not used by most users.
212076b2cf59SMatthew Knepley 
212176b2cf59SMatthew Knepley .keywords: SNES, update
2122b5d30489SBarry Smith 
212385385478SLisandro Dalcin .seealso SNESDefaultUpdate(), SNESSetJacobian(), SNESSolve()
212476b2cf59SMatthew Knepley @*/
212563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt))
212676b2cf59SMatthew Knepley {
212776b2cf59SMatthew Knepley   PetscFunctionBegin;
21280700a824SBarry Smith   PetscValidHeaderSpecific(snes, SNES_CLASSID,1);
2129e7788613SBarry Smith   snes->ops->update = func;
213076b2cf59SMatthew Knepley   PetscFunctionReturn(0);
213176b2cf59SMatthew Knepley }
213276b2cf59SMatthew Knepley 
2133e74ef692SMatthew Knepley #undef __FUNCT__
2134e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
213576b2cf59SMatthew Knepley /*@
213676b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
213776b2cf59SMatthew Knepley 
213876b2cf59SMatthew Knepley   Not collective
213976b2cf59SMatthew Knepley 
214076b2cf59SMatthew Knepley   Input Parameters:
214176b2cf59SMatthew Knepley . snes - The nonlinear solver context
214276b2cf59SMatthew Knepley . step - The current step of the iteration
214376b2cf59SMatthew Knepley 
2144205452f4SMatthew Knepley   Level: intermediate
2145205452f4SMatthew Knepley 
214676b2cf59SMatthew Knepley .keywords: SNES, update
2147a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC()
214876b2cf59SMatthew Knepley @*/
214963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultUpdate(SNES snes, PetscInt step)
215076b2cf59SMatthew Knepley {
215176b2cf59SMatthew Knepley   PetscFunctionBegin;
215276b2cf59SMatthew Knepley   PetscFunctionReturn(0);
215376b2cf59SMatthew Knepley }
215476b2cf59SMatthew Knepley 
21554a2ae208SSatish Balay #undef __FUNCT__
21564a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
21579b94acceSBarry Smith /*
21589b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
21599b94acceSBarry Smith    positive parameter delta.
21609b94acceSBarry Smith 
21619b94acceSBarry Smith     Input Parameters:
2162c7afd0dbSLois Curfman McInnes +   snes - the SNES context
21639b94acceSBarry Smith .   y - approximate solution of linear system
21649b94acceSBarry Smith .   fnorm - 2-norm of current function
2165c7afd0dbSLois Curfman McInnes -   delta - trust region size
21669b94acceSBarry Smith 
21679b94acceSBarry Smith     Output Parameters:
2168c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
21699b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
21709b94acceSBarry Smith     region, and exceeds zero otherwise.
2171c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
21729b94acceSBarry Smith 
21739b94acceSBarry Smith     Note:
21744b27c08aSLois Curfman McInnes     For non-trust region methods such as SNESLS, the parameter delta
21759b94acceSBarry Smith     is set to be the maximum allowable step size.
21769b94acceSBarry Smith 
21779b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
21789b94acceSBarry Smith */
2179dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
21809b94acceSBarry Smith {
2181064f8208SBarry Smith   PetscReal      nrm;
2182ea709b57SSatish Balay   PetscScalar    cnorm;
2183dfbe8321SBarry Smith   PetscErrorCode ierr;
21843a40ed3dSBarry Smith 
21853a40ed3dSBarry Smith   PetscFunctionBegin;
21860700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
21870700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,2);
2188c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,2);
2189184914b5SBarry Smith 
2190064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
2191064f8208SBarry Smith   if (nrm > *delta) {
2192064f8208SBarry Smith      nrm = *delta/nrm;
2193064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
2194064f8208SBarry Smith      cnorm = nrm;
21952dcb1b2aSMatthew Knepley      ierr = VecScale(y,cnorm);CHKERRQ(ierr);
21969b94acceSBarry Smith      *ynorm = *delta;
21979b94acceSBarry Smith   } else {
21989b94acceSBarry Smith      *gpnorm = 0.0;
2199064f8208SBarry Smith      *ynorm = nrm;
22009b94acceSBarry Smith   }
22013a40ed3dSBarry Smith   PetscFunctionReturn(0);
22029b94acceSBarry Smith }
22039b94acceSBarry Smith 
22044a2ae208SSatish Balay #undef __FUNCT__
22054a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
22066ce558aeSBarry Smith /*@C
2207f69a0ea3SMatthew Knepley    SNESSolve - Solves a nonlinear system F(x) = b.
2208f69a0ea3SMatthew Knepley    Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX().
22099b94acceSBarry Smith 
2210c7afd0dbSLois Curfman McInnes    Collective on SNES
2211c7afd0dbSLois Curfman McInnes 
2212b2002411SLois Curfman McInnes    Input Parameters:
2213c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2214f69a0ea3SMatthew Knepley .  b - the constant part of the equation, or PETSC_NULL to use zero.
221585385478SLisandro Dalcin -  x - the solution vector.
22169b94acceSBarry Smith 
2217b2002411SLois Curfman McInnes    Notes:
22188ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
22198ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
22208ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
22218ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
22228ddd3da0SLois Curfman McInnes 
222336851e7fSLois Curfman McInnes    Level: beginner
222436851e7fSLois Curfman McInnes 
22259b94acceSBarry Smith .keywords: SNES, nonlinear, solve
22269b94acceSBarry Smith 
222785385478SLisandro Dalcin .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian()
22289b94acceSBarry Smith @*/
2229f69a0ea3SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSolve(SNES snes,Vec b,Vec x)
22309b94acceSBarry Smith {
2231dfbe8321SBarry Smith   PetscErrorCode ierr;
2232f1af5d2fSBarry Smith   PetscTruth     flg;
2233eabae89aSBarry Smith   char           filename[PETSC_MAX_PATH_LEN];
2234eabae89aSBarry Smith   PetscViewer    viewer;
2235052efed2SBarry Smith 
22363a40ed3dSBarry Smith   PetscFunctionBegin;
22370700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
22380700a824SBarry Smith   PetscValidHeaderSpecific(x,VEC_CLASSID,3);
2239f69a0ea3SMatthew Knepley   PetscCheckSameComm(snes,1,x,3);
22400700a824SBarry Smith   if (b) PetscValidHeaderSpecific(b,VEC_CLASSID,2);
224185385478SLisandro Dalcin   if (b) PetscCheckSameComm(snes,1,b,2);
224285385478SLisandro Dalcin 
224385385478SLisandro Dalcin   /* set solution vector */
224485385478SLisandro Dalcin   ierr = PetscObjectReference((PetscObject)x);CHKERRQ(ierr);
224585385478SLisandro Dalcin   if (snes->vec_sol) { ierr = VecDestroy(snes->vec_sol);CHKERRQ(ierr); }
224685385478SLisandro Dalcin   snes->vec_sol = x;
224785385478SLisandro Dalcin   /* set afine vector if provided */
224885385478SLisandro Dalcin   if (b) { ierr = PetscObjectReference((PetscObject)b);CHKERRQ(ierr); }
224985385478SLisandro Dalcin   if (snes->vec_rhs) { ierr = VecDestroy(snes->vec_rhs);CHKERRQ(ierr); }
225085385478SLisandro Dalcin   snes->vec_rhs = b;
225185385478SLisandro Dalcin 
225285385478SLisandro Dalcin   if (!snes->vec_func && snes->vec_rhs) {
225385385478SLisandro Dalcin     ierr = VecDuplicate(b, &snes->vec_func);CHKERRQ(ierr);
225470e92668SMatthew Knepley   }
22553f149594SLisandro Dalcin 
225670e92668SMatthew Knepley   ierr = SNESSetUp(snes);CHKERRQ(ierr);
22573f149594SLisandro Dalcin 
2258abc0a331SBarry Smith   if (snes->conv_hist_reset) snes->conv_hist_len = 0;
225950ffb88aSMatthew Knepley   snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
2260d5e45103SBarry Smith 
22613f149594SLisandro Dalcin   ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
22624936397dSBarry Smith   ierr = (*snes->ops->solve)(snes);CHKERRQ(ierr);
226385385478SLisandro Dalcin   ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
22644936397dSBarry Smith   if (snes->domainerror){
22654936397dSBarry Smith     snes->reason      = SNES_DIVERGED_FUNCTION_DOMAIN;
22664936397dSBarry Smith     snes->domainerror = PETSC_FALSE;
22674936397dSBarry Smith   }
226817186662SBarry Smith   if (!snes->reason) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason");
22693f149594SLisandro Dalcin 
22707adad957SLisandro Dalcin   ierr = PetscOptionsGetString(((PetscObject)snes)->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
2271eabae89aSBarry Smith   if (flg && !PetscPreLoadingOn) {
22727adad957SLisandro Dalcin     ierr = PetscViewerASCIIOpen(((PetscObject)snes)->comm,filename,&viewer);CHKERRQ(ierr);
2273eabae89aSBarry Smith     ierr = SNESView(snes,viewer);CHKERRQ(ierr);
2274eabae89aSBarry Smith     ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr);
2275eabae89aSBarry Smith   }
2276eabae89aSBarry Smith 
227790d69ab7SBarry Smith   flg  = PETSC_FALSE;
227890d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(((PetscObject)snes)->prefix,"-snes_test_local_min",&flg,PETSC_NULL);CHKERRQ(ierr);
2279da9b6338SBarry Smith   if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); }
22805968eb51SBarry Smith   if (snes->printreason) {
22815968eb51SBarry Smith     if (snes->reason > 0) {
22827adad957SLisandro Dalcin       ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
22835968eb51SBarry Smith     } else {
22847adad957SLisandro Dalcin       ierr = PetscPrintf(((PetscObject)snes)->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
22855968eb51SBarry Smith     }
22865968eb51SBarry Smith   }
22875968eb51SBarry Smith 
22883a40ed3dSBarry Smith   PetscFunctionReturn(0);
22899b94acceSBarry Smith }
22909b94acceSBarry Smith 
22919b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
22929b94acceSBarry Smith 
22934a2ae208SSatish Balay #undef __FUNCT__
22944a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
229582bf6240SBarry Smith /*@C
22964b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
22979b94acceSBarry Smith 
2298fee21e36SBarry Smith    Collective on SNES
2299fee21e36SBarry Smith 
2300c7afd0dbSLois Curfman McInnes    Input Parameters:
2301c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2302454a90a3SBarry Smith -  type - a known method
2303c7afd0dbSLois Curfman McInnes 
2304c7afd0dbSLois Curfman McInnes    Options Database Key:
2305454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
2306c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
2307ae12b187SLois Curfman McInnes 
23089b94acceSBarry Smith    Notes:
2309e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
23104b27c08aSLois Curfman McInnes +    SNESLS - Newton's method with line search
2311c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
23124b27c08aSLois Curfman McInnes .    SNESTR - Newton's method with trust region
2313c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
23149b94acceSBarry Smith 
2315ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
2316ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
2317ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
2318ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
2319ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
2320ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
2321ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
2322ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
2323ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
2324b0a32e0cSBarry Smith   appropriate method.
232536851e7fSLois Curfman McInnes 
232636851e7fSLois Curfman McInnes   Level: intermediate
2327a703fe33SLois Curfman McInnes 
2328454a90a3SBarry Smith .keywords: SNES, set, type
2329435da068SBarry Smith 
2330435da068SBarry Smith .seealso: SNESType, SNESCreate()
2331435da068SBarry Smith 
23329b94acceSBarry Smith @*/
2333a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetType(SNES snes,const SNESType type)
23349b94acceSBarry Smith {
2335dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(SNES);
23366831982aSBarry Smith   PetscTruth     match;
23373a40ed3dSBarry Smith 
23383a40ed3dSBarry Smith   PetscFunctionBegin;
23390700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
23404482741eSBarry Smith   PetscValidCharPointer(type,2);
234182bf6240SBarry Smith 
23426831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
23430f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
234492ff6ae8SBarry Smith 
23457adad957SLisandro Dalcin   ierr =  PetscFListFind(SNESList,((PetscObject)snes)->comm,type,(void (**)(void)) &r);CHKERRQ(ierr);
2346e32f2f54SBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type);
234775396ef9SLisandro Dalcin   /* Destroy the previous private SNES context */
234875396ef9SLisandro Dalcin   if (snes->ops->destroy) { ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr); }
234975396ef9SLisandro Dalcin   /* Reinitialize function pointers in SNESOps structure */
235075396ef9SLisandro Dalcin   snes->ops->setup          = 0;
235175396ef9SLisandro Dalcin   snes->ops->solve          = 0;
235275396ef9SLisandro Dalcin   snes->ops->view           = 0;
235375396ef9SLisandro Dalcin   snes->ops->setfromoptions = 0;
235475396ef9SLisandro Dalcin   snes->ops->destroy        = 0;
235575396ef9SLisandro Dalcin   /* Call the SNESCreate_XXX routine for this particular Nonlinear solver */
235675396ef9SLisandro Dalcin   snes->setupcalled = PETSC_FALSE;
23573a40ed3dSBarry Smith   ierr = (*r)(snes);CHKERRQ(ierr);
2358454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
23593a40ed3dSBarry Smith   PetscFunctionReturn(0);
23609b94acceSBarry Smith }
23619b94acceSBarry Smith 
2362a847f771SSatish Balay 
23639b94acceSBarry Smith /* --------------------------------------------------------------------- */
23644a2ae208SSatish Balay #undef __FUNCT__
23654a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
236652baeb72SSatish Balay /*@
23679b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
2368f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
23699b94acceSBarry Smith 
2370fee21e36SBarry Smith    Not Collective
2371fee21e36SBarry Smith 
237236851e7fSLois Curfman McInnes    Level: advanced
237336851e7fSLois Curfman McInnes 
23749b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
23759b94acceSBarry Smith 
23769b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
23779b94acceSBarry Smith @*/
237863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegisterDestroy(void)
23799b94acceSBarry Smith {
2380dfbe8321SBarry Smith   PetscErrorCode ierr;
238182bf6240SBarry Smith 
23823a40ed3dSBarry Smith   PetscFunctionBegin;
23831441b1d3SBarry Smith   ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
23844c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
23853a40ed3dSBarry Smith   PetscFunctionReturn(0);
23869b94acceSBarry Smith }
23879b94acceSBarry Smith 
23884a2ae208SSatish Balay #undef __FUNCT__
23894a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
23909b94acceSBarry Smith /*@C
23919a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
23929b94acceSBarry Smith 
2393c7afd0dbSLois Curfman McInnes    Not Collective
2394c7afd0dbSLois Curfman McInnes 
23959b94acceSBarry Smith    Input Parameter:
23964b0e389bSBarry Smith .  snes - nonlinear solver context
23979b94acceSBarry Smith 
23989b94acceSBarry Smith    Output Parameter:
23993a7fca6bSBarry Smith .  type - SNES method (a character string)
24009b94acceSBarry Smith 
240136851e7fSLois Curfman McInnes    Level: intermediate
240236851e7fSLois Curfman McInnes 
2403454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
24049b94acceSBarry Smith @*/
2405a313700dSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetType(SNES snes,const SNESType *type)
24069b94acceSBarry Smith {
24073a40ed3dSBarry Smith   PetscFunctionBegin;
24080700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
24094482741eSBarry Smith   PetscValidPointer(type,2);
24107adad957SLisandro Dalcin   *type = ((PetscObject)snes)->type_name;
24113a40ed3dSBarry Smith   PetscFunctionReturn(0);
24129b94acceSBarry Smith }
24139b94acceSBarry Smith 
24144a2ae208SSatish Balay #undef __FUNCT__
24154a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
241652baeb72SSatish Balay /*@
24179b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
24189b94acceSBarry Smith    stored.
24199b94acceSBarry Smith 
2420c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2421c7afd0dbSLois Curfman McInnes 
24229b94acceSBarry Smith    Input Parameter:
24239b94acceSBarry Smith .  snes - the SNES context
24249b94acceSBarry Smith 
24259b94acceSBarry Smith    Output Parameter:
24269b94acceSBarry Smith .  x - the solution
24279b94acceSBarry Smith 
242870e92668SMatthew Knepley    Level: intermediate
242936851e7fSLois Curfman McInnes 
24309b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
24319b94acceSBarry Smith 
243285385478SLisandro Dalcin .seealso:  SNESGetSolutionUpdate(), SNESGetFunction()
24339b94acceSBarry Smith @*/
243463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolution(SNES snes,Vec *x)
24359b94acceSBarry Smith {
24363a40ed3dSBarry Smith   PetscFunctionBegin;
24370700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
24384482741eSBarry Smith   PetscValidPointer(x,2);
243985385478SLisandro Dalcin   *x = snes->vec_sol;
244070e92668SMatthew Knepley   PetscFunctionReturn(0);
244170e92668SMatthew Knepley }
244270e92668SMatthew Knepley 
244370e92668SMatthew Knepley #undef __FUNCT__
24444a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
244552baeb72SSatish Balay /*@
24469b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
24479b94acceSBarry Smith    stored.
24489b94acceSBarry Smith 
2449c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2450c7afd0dbSLois Curfman McInnes 
24519b94acceSBarry Smith    Input Parameter:
24529b94acceSBarry Smith .  snes - the SNES context
24539b94acceSBarry Smith 
24549b94acceSBarry Smith    Output Parameter:
24559b94acceSBarry Smith .  x - the solution update
24569b94acceSBarry Smith 
245736851e7fSLois Curfman McInnes    Level: advanced
245836851e7fSLois Curfman McInnes 
24599b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
24609b94acceSBarry Smith 
246185385478SLisandro Dalcin .seealso: SNESGetSolution(), SNESGetFunction()
24629b94acceSBarry Smith @*/
246363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolutionUpdate(SNES snes,Vec *x)
24649b94acceSBarry Smith {
24653a40ed3dSBarry Smith   PetscFunctionBegin;
24660700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
24674482741eSBarry Smith   PetscValidPointer(x,2);
246885385478SLisandro Dalcin   *x = snes->vec_sol_update;
24693a40ed3dSBarry Smith   PetscFunctionReturn(0);
24709b94acceSBarry Smith }
24719b94acceSBarry Smith 
24724a2ae208SSatish Balay #undef __FUNCT__
24734a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
24749b94acceSBarry Smith /*@C
24753638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
24769b94acceSBarry Smith 
2477c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2478c7afd0dbSLois Curfman McInnes 
24799b94acceSBarry Smith    Input Parameter:
24809b94acceSBarry Smith .  snes - the SNES context
24819b94acceSBarry Smith 
24829b94acceSBarry Smith    Output Parameter:
24837bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
248470e92668SMatthew Knepley .  func - the function (or PETSC_NULL)
248570e92668SMatthew Knepley -  ctx - the function context (or PETSC_NULL)
24869b94acceSBarry Smith 
248736851e7fSLois Curfman McInnes    Level: advanced
248836851e7fSLois Curfman McInnes 
2489a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
24909b94acceSBarry Smith 
24914b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution()
24929b94acceSBarry Smith @*/
249370e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx)
24949b94acceSBarry Smith {
24953a40ed3dSBarry Smith   PetscFunctionBegin;
24960700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
249785385478SLisandro Dalcin   if (r)    *r    = snes->vec_func;
2498e7788613SBarry Smith   if (func) *func = snes->ops->computefunction;
249970e92668SMatthew Knepley   if (ctx)  *ctx  = snes->funP;
25003a40ed3dSBarry Smith   PetscFunctionReturn(0);
25019b94acceSBarry Smith }
25029b94acceSBarry Smith 
25034a2ae208SSatish Balay #undef __FUNCT__
25044a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
25053c7409f5SSatish Balay /*@C
25063c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2507d850072dSLois Curfman McInnes    SNES options in the database.
25083c7409f5SSatish Balay 
2509fee21e36SBarry Smith    Collective on SNES
2510fee21e36SBarry Smith 
2511c7afd0dbSLois Curfman McInnes    Input Parameter:
2512c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2513c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2514c7afd0dbSLois Curfman McInnes 
2515d850072dSLois Curfman McInnes    Notes:
2516a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2517c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2518d850072dSLois Curfman McInnes 
251936851e7fSLois Curfman McInnes    Level: advanced
252036851e7fSLois Curfman McInnes 
25213c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2522a86d99e1SLois Curfman McInnes 
2523a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
25243c7409f5SSatish Balay @*/
252563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetOptionsPrefix(SNES snes,const char prefix[])
25263c7409f5SSatish Balay {
2527dfbe8321SBarry Smith   PetscErrorCode ierr;
25283c7409f5SSatish Balay 
25293a40ed3dSBarry Smith   PetscFunctionBegin;
25300700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2531639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
25321cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
253394b7f48cSBarry Smith   ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
25343a40ed3dSBarry Smith   PetscFunctionReturn(0);
25353c7409f5SSatish Balay }
25363c7409f5SSatish Balay 
25374a2ae208SSatish Balay #undef __FUNCT__
25384a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
25393c7409f5SSatish Balay /*@C
2540f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2541d850072dSLois Curfman McInnes    SNES options in the database.
25423c7409f5SSatish Balay 
2543fee21e36SBarry Smith    Collective on SNES
2544fee21e36SBarry Smith 
2545c7afd0dbSLois Curfman McInnes    Input Parameters:
2546c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2547c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2548c7afd0dbSLois Curfman McInnes 
2549d850072dSLois Curfman McInnes    Notes:
2550a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2551c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2552d850072dSLois Curfman McInnes 
255336851e7fSLois Curfman McInnes    Level: advanced
255436851e7fSLois Curfman McInnes 
25553c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2556a86d99e1SLois Curfman McInnes 
2557a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
25583c7409f5SSatish Balay @*/
255963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAppendOptionsPrefix(SNES snes,const char prefix[])
25603c7409f5SSatish Balay {
2561dfbe8321SBarry Smith   PetscErrorCode ierr;
25623c7409f5SSatish Balay 
25633a40ed3dSBarry Smith   PetscFunctionBegin;
25640700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2565639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
25661cee3971SBarry Smith   if (!snes->ksp) {ierr = SNESGetKSP(snes,&snes->ksp);CHKERRQ(ierr);}
256794b7f48cSBarry Smith   ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
25683a40ed3dSBarry Smith   PetscFunctionReturn(0);
25693c7409f5SSatish Balay }
25703c7409f5SSatish Balay 
25714a2ae208SSatish Balay #undef __FUNCT__
25724a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
25739ab63eb5SSatish Balay /*@C
25743c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
25753c7409f5SSatish Balay    SNES options in the database.
25763c7409f5SSatish Balay 
2577c7afd0dbSLois Curfman McInnes    Not Collective
2578c7afd0dbSLois Curfman McInnes 
25793c7409f5SSatish Balay    Input Parameter:
25803c7409f5SSatish Balay .  snes - the SNES context
25813c7409f5SSatish Balay 
25823c7409f5SSatish Balay    Output Parameter:
25833c7409f5SSatish Balay .  prefix - pointer to the prefix string used
25843c7409f5SSatish Balay 
25854ef407dbSRichard Tran Mills    Notes: On the fortran side, the user should pass in a string 'prefix' of
25869ab63eb5SSatish Balay    sufficient length to hold the prefix.
25879ab63eb5SSatish Balay 
258836851e7fSLois Curfman McInnes    Level: advanced
258936851e7fSLois Curfman McInnes 
25903c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2591a86d99e1SLois Curfman McInnes 
2592a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
25933c7409f5SSatish Balay @*/
2594e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetOptionsPrefix(SNES snes,const char *prefix[])
25953c7409f5SSatish Balay {
2596dfbe8321SBarry Smith   PetscErrorCode ierr;
25973c7409f5SSatish Balay 
25983a40ed3dSBarry Smith   PetscFunctionBegin;
25990700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2600639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
26013a40ed3dSBarry Smith   PetscFunctionReturn(0);
26023c7409f5SSatish Balay }
26033c7409f5SSatish Balay 
2604b2002411SLois Curfman McInnes 
26054a2ae208SSatish Balay #undef __FUNCT__
26064a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
26073cea93caSBarry Smith /*@C
26083cea93caSBarry Smith   SNESRegister - See SNESRegisterDynamic()
26093cea93caSBarry Smith 
26107f6c08e0SMatthew Knepley   Level: advanced
26113cea93caSBarry Smith @*/
261263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES))
2613b2002411SLois Curfman McInnes {
2614e2d1d2b7SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
2615dfbe8321SBarry Smith   PetscErrorCode ierr;
2616b2002411SLois Curfman McInnes 
2617b2002411SLois Curfman McInnes   PetscFunctionBegin;
2618b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2619c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
2620b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2621b2002411SLois Curfman McInnes }
2622da9b6338SBarry Smith 
2623da9b6338SBarry Smith #undef __FUNCT__
2624da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin"
262563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESTestLocalMin(SNES snes)
2626da9b6338SBarry Smith {
2627dfbe8321SBarry Smith   PetscErrorCode ierr;
262877431f27SBarry Smith   PetscInt       N,i,j;
2629da9b6338SBarry Smith   Vec            u,uh,fh;
2630da9b6338SBarry Smith   PetscScalar    value;
2631da9b6338SBarry Smith   PetscReal      norm;
2632da9b6338SBarry Smith 
2633da9b6338SBarry Smith   PetscFunctionBegin;
2634da9b6338SBarry Smith   ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr);
2635da9b6338SBarry Smith   ierr = VecDuplicate(u,&uh);CHKERRQ(ierr);
2636da9b6338SBarry Smith   ierr = VecDuplicate(u,&fh);CHKERRQ(ierr);
2637da9b6338SBarry Smith 
2638da9b6338SBarry Smith   /* currently only works for sequential */
2639da9b6338SBarry Smith   ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n");
2640da9b6338SBarry Smith   ierr = VecGetSize(u,&N);CHKERRQ(ierr);
2641da9b6338SBarry Smith   for (i=0; i<N; i++) {
2642da9b6338SBarry Smith     ierr = VecCopy(u,uh);CHKERRQ(ierr);
264377431f27SBarry Smith     ierr  = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr);
2644da9b6338SBarry Smith     for (j=-10; j<11; j++) {
2645ccae9161SBarry Smith       value = PetscSign(j)*exp(PetscAbs(j)-10.0);
2646da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
26473ab0aad5SBarry Smith       ierr  = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr);
2648da9b6338SBarry Smith       ierr  = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr);
264977431f27SBarry Smith       ierr  = PetscPrintf(PETSC_COMM_WORLD,"       j norm %D %18.16e\n",j,norm);CHKERRQ(ierr);
2650da9b6338SBarry Smith       value = -value;
2651da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
2652da9b6338SBarry Smith     }
2653da9b6338SBarry Smith   }
2654da9b6338SBarry Smith   ierr = VecDestroy(uh);CHKERRQ(ierr);
2655da9b6338SBarry Smith   ierr = VecDestroy(fh);CHKERRQ(ierr);
2656da9b6338SBarry Smith   PetscFunctionReturn(0);
2657da9b6338SBarry Smith }
265871f87433Sdalcinl 
265971f87433Sdalcinl #undef __FUNCT__
2660fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetUseEW"
266171f87433Sdalcinl /*@
2662fa9f3622SBarry Smith    SNESKSPSetUseEW - Sets SNES use Eisenstat-Walker method for
266371f87433Sdalcinl    computing relative tolerance for linear solvers within an inexact
266471f87433Sdalcinl    Newton method.
266571f87433Sdalcinl 
266671f87433Sdalcinl    Collective on SNES
266771f87433Sdalcinl 
266871f87433Sdalcinl    Input Parameters:
266971f87433Sdalcinl +  snes - SNES context
267071f87433Sdalcinl -  flag - PETSC_TRUE or PETSC_FALSE
267171f87433Sdalcinl 
267264ba62caSBarry Smith     Options Database:
267364ba62caSBarry Smith +  -snes_ksp_ew - use Eisenstat-Walker method for determining linear system convergence
267464ba62caSBarry Smith .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
267564ba62caSBarry Smith .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
267664ba62caSBarry Smith .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
267764ba62caSBarry Smith .  -snes_ksp_ew_gamma <gamma> - Sets gamma
267864ba62caSBarry Smith .  -snes_ksp_ew_alpha <alpha> - Sets alpha
267964ba62caSBarry Smith .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
268064ba62caSBarry Smith -  -snes_ksp_ew_threshold <threshold> - Sets threshold
268164ba62caSBarry Smith 
268271f87433Sdalcinl    Notes:
268371f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
268471f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
268571f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
268671f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
268771f87433Sdalcinl    solver.
268871f87433Sdalcinl 
268971f87433Sdalcinl    Level: advanced
269071f87433Sdalcinl 
269171f87433Sdalcinl    Reference:
269271f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
269371f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
269471f87433Sdalcinl 
269571f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
269671f87433Sdalcinl 
2697fa9f3622SBarry Smith .seealso: SNESKSPGetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
269871f87433Sdalcinl @*/
2699fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetUseEW(SNES snes,PetscTruth flag)
270071f87433Sdalcinl {
270171f87433Sdalcinl   PetscFunctionBegin;
27020700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
270371f87433Sdalcinl   snes->ksp_ewconv = flag;
270471f87433Sdalcinl   PetscFunctionReturn(0);
270571f87433Sdalcinl }
270671f87433Sdalcinl 
270771f87433Sdalcinl #undef __FUNCT__
2708fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetUseEW"
270971f87433Sdalcinl /*@
2710fa9f3622SBarry Smith    SNESKSPGetUseEW - Gets if SNES is using Eisenstat-Walker method
271171f87433Sdalcinl    for computing relative tolerance for linear solvers within an
271271f87433Sdalcinl    inexact Newton method.
271371f87433Sdalcinl 
271471f87433Sdalcinl    Not Collective
271571f87433Sdalcinl 
271671f87433Sdalcinl    Input Parameter:
271771f87433Sdalcinl .  snes - SNES context
271871f87433Sdalcinl 
271971f87433Sdalcinl    Output Parameter:
272071f87433Sdalcinl .  flag - PETSC_TRUE or PETSC_FALSE
272171f87433Sdalcinl 
272271f87433Sdalcinl    Notes:
272371f87433Sdalcinl    Currently, the default is to use a constant relative tolerance for
272471f87433Sdalcinl    the inner linear solvers.  Alternatively, one can use the
272571f87433Sdalcinl    Eisenstat-Walker method, where the relative convergence tolerance
272671f87433Sdalcinl    is reset at each Newton iteration according progress of the nonlinear
272771f87433Sdalcinl    solver.
272871f87433Sdalcinl 
272971f87433Sdalcinl    Level: advanced
273071f87433Sdalcinl 
273171f87433Sdalcinl    Reference:
273271f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
273371f87433Sdalcinl    inexact Newton method", SISC 17 (1), pp.16-32, 1996.
273471f87433Sdalcinl 
273571f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, convergence, test, inexact, Newton
273671f87433Sdalcinl 
2737fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetParametersEW(), SNESKSPSetParametersEW()
273871f87433Sdalcinl @*/
2739fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetUseEW(SNES snes, PetscTruth *flag)
274071f87433Sdalcinl {
274171f87433Sdalcinl   PetscFunctionBegin;
27420700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
274371f87433Sdalcinl   PetscValidPointer(flag,2);
274471f87433Sdalcinl   *flag = snes->ksp_ewconv;
274571f87433Sdalcinl   PetscFunctionReturn(0);
274671f87433Sdalcinl }
274771f87433Sdalcinl 
274871f87433Sdalcinl #undef __FUNCT__
2749fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPSetParametersEW"
275071f87433Sdalcinl /*@
2751fa9f3622SBarry Smith    SNESKSPSetParametersEW - Sets parameters for Eisenstat-Walker
275271f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
275371f87433Sdalcinl    Newton method.
275471f87433Sdalcinl 
275571f87433Sdalcinl    Collective on SNES
275671f87433Sdalcinl 
275771f87433Sdalcinl    Input Parameters:
275871f87433Sdalcinl +    snes - SNES context
275971f87433Sdalcinl .    version - version 1, 2 (default is 2) or 3
276071f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
276171f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
276271f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
276371f87433Sdalcinl              (0 <= gamma2 <= 1)
276471f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
276571f87433Sdalcinl .    alpha2 - power for safeguard
276671f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
276771f87433Sdalcinl 
276871f87433Sdalcinl    Note:
276971f87433Sdalcinl    Version 3 was contributed by Luis Chacon, June 2006.
277071f87433Sdalcinl 
277171f87433Sdalcinl    Use PETSC_DEFAULT to retain the default for any of the parameters.
277271f87433Sdalcinl 
277371f87433Sdalcinl    Level: advanced
277471f87433Sdalcinl 
277571f87433Sdalcinl    Reference:
277671f87433Sdalcinl    S. C. Eisenstat and H. F. Walker, "Choosing the forcing terms in an
277771f87433Sdalcinl    inexact Newton method", Utah State University Math. Stat. Dept. Res.
277871f87433Sdalcinl    Report 6/94/75, June, 1994, to appear in SIAM J. Sci. Comput.
277971f87433Sdalcinl 
278071f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, set, parameters
278171f87433Sdalcinl 
2782fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPGetParametersEW()
278371f87433Sdalcinl @*/
2784fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPSetParametersEW(SNES snes,PetscInt version,PetscReal rtol_0,PetscReal rtol_max,
278571f87433Sdalcinl 							    PetscReal gamma,PetscReal alpha,PetscReal alpha2,PetscReal threshold)
278671f87433Sdalcinl {
2787fa9f3622SBarry Smith   SNESKSPEW *kctx;
278871f87433Sdalcinl   PetscFunctionBegin;
27890700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2790fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
2791e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
279271f87433Sdalcinl 
279371f87433Sdalcinl   if (version != PETSC_DEFAULT)   kctx->version   = version;
279471f87433Sdalcinl   if (rtol_0 != PETSC_DEFAULT)    kctx->rtol_0    = rtol_0;
279571f87433Sdalcinl   if (rtol_max != PETSC_DEFAULT)  kctx->rtol_max  = rtol_max;
279671f87433Sdalcinl   if (gamma != PETSC_DEFAULT)     kctx->gamma     = gamma;
279771f87433Sdalcinl   if (alpha != PETSC_DEFAULT)     kctx->alpha     = alpha;
279871f87433Sdalcinl   if (alpha2 != PETSC_DEFAULT)    kctx->alpha2    = alpha2;
279971f87433Sdalcinl   if (threshold != PETSC_DEFAULT) kctx->threshold = threshold;
280071f87433Sdalcinl 
280171f87433Sdalcinl   if (kctx->version < 1 || kctx->version > 3) {
2802e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 and 3 are supported: %D",kctx->version);
280371f87433Sdalcinl   }
280471f87433Sdalcinl   if (kctx->rtol_0 < 0.0 || kctx->rtol_0 >= 1.0) {
2805e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_0 < 1.0: %G",kctx->rtol_0);
280671f87433Sdalcinl   }
280771f87433Sdalcinl   if (kctx->rtol_max < 0.0 || kctx->rtol_max >= 1.0) {
2808e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= rtol_max (%G) < 1.0\n",kctx->rtol_max);
280971f87433Sdalcinl   }
281071f87433Sdalcinl   if (kctx->gamma < 0.0 || kctx->gamma > 1.0) {
2811e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 <= gamma (%G) <= 1.0\n",kctx->gamma);
281271f87433Sdalcinl   }
281371f87433Sdalcinl   if (kctx->alpha <= 1.0 || kctx->alpha > 2.0) {
2814e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"1.0 < alpha (%G) <= 2.0\n",kctx->alpha);
281571f87433Sdalcinl   }
281671f87433Sdalcinl   if (kctx->threshold <= 0.0 || kctx->threshold >= 1.0) {
2817e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"0.0 < threshold (%G) < 1.0\n",kctx->threshold);
281871f87433Sdalcinl   }
281971f87433Sdalcinl   PetscFunctionReturn(0);
282071f87433Sdalcinl }
282171f87433Sdalcinl 
282271f87433Sdalcinl #undef __FUNCT__
2823fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPGetParametersEW"
282471f87433Sdalcinl /*@
2825fa9f3622SBarry Smith    SNESKSPGetParametersEW - Gets parameters for Eisenstat-Walker
282671f87433Sdalcinl    convergence criteria for the linear solvers within an inexact
282771f87433Sdalcinl    Newton method.
282871f87433Sdalcinl 
282971f87433Sdalcinl    Not Collective
283071f87433Sdalcinl 
283171f87433Sdalcinl    Input Parameters:
283271f87433Sdalcinl      snes - SNES context
283371f87433Sdalcinl 
283471f87433Sdalcinl    Output Parameters:
283571f87433Sdalcinl +    version - version 1, 2 (default is 2) or 3
283671f87433Sdalcinl .    rtol_0 - initial relative tolerance (0 <= rtol_0 < 1)
283771f87433Sdalcinl .    rtol_max - maximum relative tolerance (0 <= rtol_max < 1)
283871f87433Sdalcinl .    gamma - multiplicative factor for version 2 rtol computation
283971f87433Sdalcinl              (0 <= gamma2 <= 1)
284071f87433Sdalcinl .    alpha - power for version 2 rtol computation (1 < alpha <= 2)
284171f87433Sdalcinl .    alpha2 - power for safeguard
284271f87433Sdalcinl -    threshold - threshold for imposing safeguard (0 < threshold < 1)
284371f87433Sdalcinl 
284471f87433Sdalcinl    Level: advanced
284571f87433Sdalcinl 
284671f87433Sdalcinl .keywords: SNES, KSP, Eisenstat, Walker, get, parameters
284771f87433Sdalcinl 
2848fa9f3622SBarry Smith .seealso: SNESKSPSetUseEW(), SNESKSPGetUseEW(), SNESKSPSetParametersEW()
284971f87433Sdalcinl @*/
2850fa9f3622SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESKSPGetParametersEW(SNES snes,PetscInt *version,PetscReal *rtol_0,PetscReal *rtol_max,
285171f87433Sdalcinl 							    PetscReal *gamma,PetscReal *alpha,PetscReal *alpha2,PetscReal *threshold)
285271f87433Sdalcinl {
2853fa9f3622SBarry Smith   SNESKSPEW *kctx;
285471f87433Sdalcinl   PetscFunctionBegin;
28550700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2856fa9f3622SBarry Smith   kctx = (SNESKSPEW*)snes->kspconvctx;
2857e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context existing");
285871f87433Sdalcinl   if(version)   *version   = kctx->version;
285971f87433Sdalcinl   if(rtol_0)    *rtol_0    = kctx->rtol_0;
286071f87433Sdalcinl   if(rtol_max)  *rtol_max  = kctx->rtol_max;
286171f87433Sdalcinl   if(gamma)     *gamma     = kctx->gamma;
286271f87433Sdalcinl   if(alpha)     *alpha     = kctx->alpha;
286371f87433Sdalcinl   if(alpha2)    *alpha2    = kctx->alpha2;
286471f87433Sdalcinl   if(threshold) *threshold = kctx->threshold;
286571f87433Sdalcinl   PetscFunctionReturn(0);
286671f87433Sdalcinl }
286771f87433Sdalcinl 
286871f87433Sdalcinl #undef __FUNCT__
2869fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PreSolve"
2870fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PreSolve(SNES snes, KSP ksp, Vec b, Vec x)
287171f87433Sdalcinl {
287271f87433Sdalcinl   PetscErrorCode ierr;
2873fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
287471f87433Sdalcinl   PetscReal      rtol=PETSC_DEFAULT,stol;
287571f87433Sdalcinl 
287671f87433Sdalcinl   PetscFunctionBegin;
2877e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
287871f87433Sdalcinl   if (!snes->iter) { /* first time in, so use the original user rtol */
287971f87433Sdalcinl     rtol = kctx->rtol_0;
288071f87433Sdalcinl   } else {
288171f87433Sdalcinl     if (kctx->version == 1) {
288271f87433Sdalcinl       rtol = (snes->norm - kctx->lresid_last)/kctx->norm_last;
288371f87433Sdalcinl       if (rtol < 0.0) rtol = -rtol;
288471f87433Sdalcinl       stol = pow(kctx->rtol_last,kctx->alpha2);
288571f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
288671f87433Sdalcinl     } else if (kctx->version == 2) {
288771f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
288871f87433Sdalcinl       stol = kctx->gamma * pow(kctx->rtol_last,kctx->alpha);
288971f87433Sdalcinl       if (stol > kctx->threshold) rtol = PetscMax(rtol,stol);
289071f87433Sdalcinl     } else if (kctx->version == 3) {/* contributed by Luis Chacon, June 2006. */
289171f87433Sdalcinl       rtol = kctx->gamma * pow(snes->norm/kctx->norm_last,kctx->alpha);
289271f87433Sdalcinl       /* safeguard: avoid sharp decrease of rtol */
289371f87433Sdalcinl       stol = kctx->gamma*pow(kctx->rtol_last,kctx->alpha);
289471f87433Sdalcinl       stol = PetscMax(rtol,stol);
289571f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
289671f87433Sdalcinl       /* safeguard: avoid oversolving */
289771f87433Sdalcinl       stol = kctx->gamma*(snes->ttol)/snes->norm;
289871f87433Sdalcinl       stol = PetscMax(rtol,stol);
289971f87433Sdalcinl       rtol = PetscMin(kctx->rtol_0,stol);
2900e32f2f54SBarry Smith     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only versions 1, 2 or 3 are supported: %D",kctx->version);
290171f87433Sdalcinl   }
290271f87433Sdalcinl   /* safeguard: avoid rtol greater than one */
290371f87433Sdalcinl   rtol = PetscMin(rtol,kctx->rtol_max);
290471f87433Sdalcinl   ierr = KSPSetTolerances(ksp,rtol,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
290571f87433Sdalcinl   ierr = PetscInfo3(snes,"iter %D, Eisenstat-Walker (version %D) KSP rtol=%G\n",snes->iter,kctx->version,rtol);CHKERRQ(ierr);
290671f87433Sdalcinl   PetscFunctionReturn(0);
290771f87433Sdalcinl }
290871f87433Sdalcinl 
290971f87433Sdalcinl #undef __FUNCT__
2910fa9f3622SBarry Smith #define __FUNCT__ "SNESKSPEW_PostSolve"
2911fa9f3622SBarry Smith static PetscErrorCode SNESKSPEW_PostSolve(SNES snes, KSP ksp, Vec b, Vec x)
291271f87433Sdalcinl {
291371f87433Sdalcinl   PetscErrorCode ierr;
2914fa9f3622SBarry Smith   SNESKSPEW      *kctx = (SNESKSPEW*)snes->kspconvctx;
291571f87433Sdalcinl   PCSide         pcside;
291671f87433Sdalcinl   Vec            lres;
291771f87433Sdalcinl 
291871f87433Sdalcinl   PetscFunctionBegin;
2919e32f2f54SBarry Smith   if (!kctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"No Eisenstat-Walker context exists");
292071f87433Sdalcinl   ierr = KSPGetTolerances(ksp,&kctx->rtol_last,0,0,0);CHKERRQ(ierr);
292171f87433Sdalcinl   ierr = SNESGetFunctionNorm(snes,&kctx->norm_last);CHKERRQ(ierr);
292271f87433Sdalcinl   if (kctx->version == 1) {
2923b037da10SBarry Smith     ierr = KSPGetPCSide(ksp,&pcside);CHKERRQ(ierr);
292471f87433Sdalcinl     if (pcside == PC_RIGHT) { /* XXX Should we also test KSP_UNPRECONDITIONED_NORM ? */
292571f87433Sdalcinl       /* KSP residual is true linear residual */
292671f87433Sdalcinl       ierr = KSPGetResidualNorm(ksp,&kctx->lresid_last);CHKERRQ(ierr);
292771f87433Sdalcinl     } else {
292871f87433Sdalcinl       /* KSP residual is preconditioned residual */
292971f87433Sdalcinl       /* compute true linear residual norm */
293071f87433Sdalcinl       ierr = VecDuplicate(b,&lres);CHKERRQ(ierr);
293171f87433Sdalcinl       ierr = MatMult(snes->jacobian,x,lres);CHKERRQ(ierr);
293271f87433Sdalcinl       ierr = VecAYPX(lres,-1.0,b);CHKERRQ(ierr);
293371f87433Sdalcinl       ierr = VecNorm(lres,NORM_2,&kctx->lresid_last);CHKERRQ(ierr);
293471f87433Sdalcinl       ierr = VecDestroy(lres);CHKERRQ(ierr);
293571f87433Sdalcinl     }
293671f87433Sdalcinl   }
293771f87433Sdalcinl   PetscFunctionReturn(0);
293871f87433Sdalcinl }
293971f87433Sdalcinl 
294071f87433Sdalcinl #undef __FUNCT__
294171f87433Sdalcinl #define __FUNCT__ "SNES_KSPSolve"
294271f87433Sdalcinl PetscErrorCode SNES_KSPSolve(SNES snes, KSP ksp, Vec b, Vec x)
294371f87433Sdalcinl {
294471f87433Sdalcinl   PetscErrorCode ierr;
294571f87433Sdalcinl 
294671f87433Sdalcinl   PetscFunctionBegin;
2947fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PreSolve(snes,ksp,b,x);CHKERRQ(ierr);  }
294871f87433Sdalcinl   ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);
2949fa9f3622SBarry Smith   if (snes->ksp_ewconv) { ierr = SNESKSPEW_PostSolve(snes,ksp,b,x);CHKERRQ(ierr); }
295071f87433Sdalcinl   PetscFunctionReturn(0);
295171f87433Sdalcinl }
29526c699258SBarry Smith 
29536c699258SBarry Smith #undef __FUNCT__
29546c699258SBarry Smith #define __FUNCT__ "SNESSetDM"
29556c699258SBarry Smith /*@
29566c699258SBarry Smith    SNESSetDM - Sets the DM that may be used by some preconditioners
29576c699258SBarry Smith 
29586c699258SBarry Smith    Collective on SNES
29596c699258SBarry Smith 
29606c699258SBarry Smith    Input Parameters:
29616c699258SBarry Smith +  snes - the preconditioner context
29626c699258SBarry Smith -  dm - the dm
29636c699258SBarry Smith 
29646c699258SBarry Smith    Level: intermediate
29656c699258SBarry Smith 
29666c699258SBarry Smith 
29676c699258SBarry Smith .seealso: SNESGetDM(), KSPSetDM(), KSPGetDM()
29686c699258SBarry Smith @*/
29696c699258SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetDM(SNES snes,DM dm)
29706c699258SBarry Smith {
29716c699258SBarry Smith   PetscErrorCode ierr;
2972345fed2cSBarry Smith   KSP            ksp;
29736c699258SBarry Smith 
29746c699258SBarry Smith   PetscFunctionBegin;
29750700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
2976*70663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
29776c699258SBarry Smith   if (snes->dm) {ierr = DMDestroy(snes->dm);CHKERRQ(ierr);}
29786c699258SBarry Smith   snes->dm = dm;
2979345fed2cSBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
2980345fed2cSBarry Smith   ierr = KSPSetDM(ksp,dm);CHKERRQ(ierr);
2981f22f69f0SBarry Smith   ierr = KSPSetDMActive(ksp,PETSC_FALSE);CHKERRQ(ierr);
29826c699258SBarry Smith   PetscFunctionReturn(0);
29836c699258SBarry Smith }
29846c699258SBarry Smith 
29856c699258SBarry Smith #undef __FUNCT__
29866c699258SBarry Smith #define __FUNCT__ "SNESGetDM"
29876c699258SBarry Smith /*@
29886c699258SBarry Smith    SNESGetDM - Gets the DM that may be used by some preconditioners
29896c699258SBarry Smith 
29906c699258SBarry Smith    Collective on SNES
29916c699258SBarry Smith 
29926c699258SBarry Smith    Input Parameter:
29936c699258SBarry Smith . snes - the preconditioner context
29946c699258SBarry Smith 
29956c699258SBarry Smith    Output Parameter:
29966c699258SBarry Smith .  dm - the dm
29976c699258SBarry Smith 
29986c699258SBarry Smith    Level: intermediate
29996c699258SBarry Smith 
30006c699258SBarry Smith 
30016c699258SBarry Smith .seealso: SNESSetDM(), KSPSetDM(), KSPGetDM()
30026c699258SBarry Smith @*/
30036c699258SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetDM(SNES snes,DM *dm)
30046c699258SBarry Smith {
30056c699258SBarry Smith   PetscFunctionBegin;
30060700a824SBarry Smith   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
30076c699258SBarry Smith   *dm = snes->dm;
30086c699258SBarry Smith   PetscFunctionReturn(0);
30096c699258SBarry Smith }
3010