xref: /petsc/src/snes/interface/snes.c (revision a562a398f118b4e146a8b5b88b4f6476bf00d42a)
163dd3a1aSKris Buschelman #define PETSCSNES_DLL
29b94acceSBarry Smith 
3b9147fbbSdalcinl #include "include/private/snesimpl.h"      /*I "petscsnes.h"  I*/
49b94acceSBarry Smith 
54c49b128SBarry Smith PetscTruth SNESRegisterAllCalled = PETSC_FALSE;
68ba1e511SMatthew Knepley PetscFList SNESList              = PETSC_NULL;
78ba1e511SMatthew Knepley 
88ba1e511SMatthew Knepley /* Logging support */
963dd3a1aSKris Buschelman PetscCookie PETSCSNES_DLLEXPORT SNES_COOKIE = 0;
106849ba73SBarry Smith PetscEvent  SNES_Solve = 0, SNES_LineSearch = 0, SNES_FunctionEval = 0, SNES_JacobianEval = 0;
11a09944afSBarry Smith 
12a09944afSBarry Smith #undef __FUNCT__
134a2ae208SSatish Balay #define __FUNCT__ "SNESView"
147e2c5f70SBarry Smith /*@C
159b94acceSBarry Smith    SNESView - Prints the SNES data structure.
169b94acceSBarry Smith 
174c49b128SBarry Smith    Collective on SNES
18fee21e36SBarry Smith 
19c7afd0dbSLois Curfman McInnes    Input Parameters:
20c7afd0dbSLois Curfman McInnes +  SNES - the SNES context
21c7afd0dbSLois Curfman McInnes -  viewer - visualization context
22c7afd0dbSLois Curfman McInnes 
239b94acceSBarry Smith    Options Database Key:
24c8a8ba5cSLois Curfman McInnes .  -snes_view - Calls SNESView() at end of SNESSolve()
259b94acceSBarry Smith 
269b94acceSBarry Smith    Notes:
279b94acceSBarry Smith    The available visualization contexts include
28b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
29b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
30c8a8ba5cSLois Curfman McInnes          output where only the first processor opens
31c8a8ba5cSLois Curfman McInnes          the file.  All other processors send their
32c8a8ba5cSLois Curfman McInnes          data to the first processor to print.
339b94acceSBarry Smith 
343e081fefSLois Curfman McInnes    The user can open an alternative visualization context with
35b0a32e0cSBarry Smith    PetscViewerASCIIOpen() - output to a specified file.
369b94acceSBarry Smith 
3736851e7fSLois Curfman McInnes    Level: beginner
3836851e7fSLois Curfman McInnes 
399b94acceSBarry Smith .keywords: SNES, view
409b94acceSBarry Smith 
41b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
429b94acceSBarry Smith @*/
4363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESView(SNES snes,PetscViewer viewer)
449b94acceSBarry Smith {
459b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
46dfbe8321SBarry Smith   PetscErrorCode      ierr;
4794b7f48cSBarry Smith   KSP                 ksp;
48e060cb09SBarry Smith   SNESType            type;
4932077d6dSBarry Smith   PetscTruth          iascii,isstring;
509b94acceSBarry Smith 
513a40ed3dSBarry Smith   PetscFunctionBegin;
524482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
533050cee2SBarry Smith   if (!viewer) {
543050cee2SBarry Smith     ierr = PetscViewerASCIIGetStdout(snes->comm,&viewer);CHKERRQ(ierr);
553050cee2SBarry Smith   }
564482741eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE,2);
57c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,viewer,2);
5874679c65SBarry Smith 
5932077d6dSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
60b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);CHKERRQ(ierr);
6132077d6dSBarry Smith   if (iascii) {
623a7fca6bSBarry Smith     if (snes->prefix) {
633a7fca6bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:(%s)\n",snes->prefix);CHKERRQ(ierr);
643a7fca6bSBarry Smith     } else {
65b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:\n");CHKERRQ(ierr);
663a7fca6bSBarry Smith     }
67454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
68454a90a3SBarry Smith     if (type) {
69b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: %s\n",type);CHKERRQ(ierr);
70184914b5SBarry Smith     } else {
71b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: not set yet\n");CHKERRQ(ierr);
72184914b5SBarry Smith     }
73e7788613SBarry Smith     if (snes->ops->view) {
74b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
75e7788613SBarry Smith       ierr = (*snes->ops->view)(snes,viewer);CHKERRQ(ierr);
76b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
770ef38995SBarry Smith     }
7877431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum iterations=%D, maximum function evaluations=%D\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr);
79a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  tolerances: relative=%G, absolute=%G, solution=%G\n",
8070441072SBarry Smith                  snes->rtol,snes->abstol,snes->xtol);CHKERRQ(ierr);
8177431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",snes->linear_its);CHKERRQ(ierr);
8277431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of function evaluations=%D\n",snes->nfuncs);CHKERRQ(ierr);
839b94acceSBarry Smith     if (snes->ksp_ewconv) {
849b94acceSBarry Smith       kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
859b94acceSBarry Smith       if (kctx) {
8677431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"  Eisenstat-Walker computation of KSP relative tolerance (version %D)\n",kctx->version);CHKERRQ(ierr);
87a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    rtol_0=%G, rtol_max=%G, threshold=%G\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr);
88a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    gamma=%G, alpha=%G, alpha2=%G\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr);
899b94acceSBarry Smith       }
909b94acceSBarry Smith     }
910f5bd95cSBarry Smith   } else if (isstring) {
92454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
93b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr);
9419bcc07fSBarry Smith   }
9594b7f48cSBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
96b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
9794b7f48cSBarry Smith   ierr = KSPView(ksp,viewer);CHKERRQ(ierr);
98b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
993a40ed3dSBarry Smith   PetscFunctionReturn(0);
1009b94acceSBarry Smith }
1019b94acceSBarry Smith 
10276b2cf59SMatthew Knepley /*
10376b2cf59SMatthew Knepley   We retain a list of functions that also take SNES command
10476b2cf59SMatthew Knepley   line options. These are called at the end SNESSetFromOptions()
10576b2cf59SMatthew Knepley */
10676b2cf59SMatthew Knepley #define MAXSETFROMOPTIONS 5
107a7cc72afSBarry Smith static PetscInt numberofsetfromoptions;
1086849ba73SBarry Smith static PetscErrorCode (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES);
10976b2cf59SMatthew Knepley 
110e74ef692SMatthew Knepley #undef __FUNCT__
111e74ef692SMatthew Knepley #define __FUNCT__ "SNESAddOptionsChecker"
112ac226902SBarry Smith /*@C
11376b2cf59SMatthew Knepley   SNESAddOptionsChecker - Adds an additional function to check for SNES options.
11476b2cf59SMatthew Knepley 
11576b2cf59SMatthew Knepley   Not Collective
11676b2cf59SMatthew Knepley 
11776b2cf59SMatthew Knepley   Input Parameter:
11876b2cf59SMatthew Knepley . snescheck - function that checks for options
11976b2cf59SMatthew Knepley 
12076b2cf59SMatthew Knepley   Level: developer
12176b2cf59SMatthew Knepley 
12276b2cf59SMatthew Knepley .seealso: SNESSetFromOptions()
12376b2cf59SMatthew Knepley @*/
12463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAddOptionsChecker(PetscErrorCode (*snescheck)(SNES))
12576b2cf59SMatthew Knepley {
12676b2cf59SMatthew Knepley   PetscFunctionBegin;
12776b2cf59SMatthew Knepley   if (numberofsetfromoptions >= MAXSETFROMOPTIONS) {
12877431f27SBarry Smith     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %D allowed", MAXSETFROMOPTIONS);
12976b2cf59SMatthew Knepley   }
13076b2cf59SMatthew Knepley   othersetfromoptions[numberofsetfromoptions++] = snescheck;
13176b2cf59SMatthew Knepley   PetscFunctionReturn(0);
13276b2cf59SMatthew Knepley }
13376b2cf59SMatthew Knepley 
1344a2ae208SSatish Balay #undef __FUNCT__
1354a2ae208SSatish Balay #define __FUNCT__ "SNESSetFromOptions"
1369b94acceSBarry Smith /*@
13794b7f48cSBarry Smith    SNESSetFromOptions - Sets various SNES and KSP parameters from user options.
1389b94acceSBarry Smith 
139c7afd0dbSLois Curfman McInnes    Collective on SNES
140c7afd0dbSLois Curfman McInnes 
1419b94acceSBarry Smith    Input Parameter:
1429b94acceSBarry Smith .  snes - the SNES context
1439b94acceSBarry Smith 
14436851e7fSLois Curfman McInnes    Options Database Keys:
1456831982aSBarry Smith +  -snes_type <type> - ls, tr, umls, umtr, test
14682738288SBarry Smith .  -snes_stol - convergence tolerance in terms of the norm
14782738288SBarry Smith                 of the change in the solution between steps
14870441072SBarry Smith .  -snes_atol <abstol> - absolute tolerance of residual norm
149b39c3a46SLois Curfman McInnes .  -snes_rtol <rtol> - relative decrease in tolerance norm from initial
150b39c3a46SLois Curfman McInnes .  -snes_max_it <max_it> - maximum number of iterations
151b39c3a46SLois Curfman McInnes .  -snes_max_funcs <max_funcs> - maximum number of function evaluations
15250ffb88aSMatthew Knepley .  -snes_max_fail <max_fail> - maximum number of failures
153b39c3a46SLois Curfman McInnes .  -snes_trtol <trtol> - trust region tolerance
1542492ecdbSBarry Smith .  -snes_no_convergence_test - skip convergence test in nonlinear
15582738288SBarry Smith                                solver; hence iterations will continue until max_it
1561fbbfb26SLois Curfman McInnes                                or some other criterion is reached. Saves expense
15782738288SBarry Smith                                of convergence test
158e8105e01SRichard Katz .  -snes_monitor <optional filename> - prints residual norm at each iteration. if no
159e8105e01SRichard Katz                                        filename given prints to stdout
160a6570f20SBarry Smith .  -snes_monitor_solution - plots solution at each iteration
161a6570f20SBarry Smith .  -snes_monitor_residual - plots residual (not its norm) at each iteration
162a6570f20SBarry Smith .  -snes_monitor_solution_update - plots update to solution at each iteration
163a6570f20SBarry Smith .  -snes_monitor_draw - plots residual norm at each iteration
164e24b481bSBarry Smith .  -snes_fd - use finite differences to compute Jacobian; very slow, only for testing
1655968eb51SBarry Smith .  -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration
166fee2055bSBarry Smith -  -snes_converged_reason - print the reason for convergence/divergence after each solve
16782738288SBarry Smith 
16882738288SBarry Smith     Options Database for Eisenstat-Walker method:
1694b27c08aSLois Curfman McInnes +  -snes_ksp_ew_conv - use Eisenstat-Walker method for determining linear system convergence
1704b27c08aSLois Curfman McInnes .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
17136851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
17236851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
17336851e7fSLois Curfman McInnes .  -snes_ksp_ew_gamma <gamma> - Sets gamma
17436851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha <alpha> - Sets alpha
17536851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
17636851e7fSLois Curfman McInnes -  -snes_ksp_ew_threshold <threshold> - Sets threshold
17782738288SBarry Smith 
17811ca99fdSLois Curfman McInnes    Notes:
17911ca99fdSLois Curfman McInnes    To see all options, run your program with the -help option or consult
18011ca99fdSLois Curfman McInnes    the users manual.
18183e2fdc7SBarry Smith 
18236851e7fSLois Curfman McInnes    Level: beginner
18336851e7fSLois Curfman McInnes 
1849b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database
1859b94acceSBarry Smith 
18669ed319cSSatish Balay .seealso: SNESSetOptionsPrefix()
1879b94acceSBarry Smith @*/
18863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFromOptions(SNES snes)
1899b94acceSBarry Smith {
19094b7f48cSBarry Smith   KSP                     ksp;
191186905e3SBarry Smith   SNES_KSP_EW_ConvCtx     *kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
192f1af5d2fSBarry Smith   PetscTruth              flg;
193dfbe8321SBarry Smith   PetscErrorCode          ierr;
19477431f27SBarry Smith   PetscInt                i;
1952fc52814SBarry Smith   const char              *deft;
196e8105e01SRichard Katz   char                    type[256], monfilename[PETSC_MAX_PATH_LEN];
19723d894e5SBarry Smith   PetscViewerASCIIMonitor monviewer;
1989b94acceSBarry Smith 
1993a40ed3dSBarry Smith   PetscFunctionBegin;
2004482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
201ca161407SBarry Smith 
202b0a32e0cSBarry Smith   ierr = PetscOptionsBegin(snes->comm,snes->prefix,"Nonlinear solver (SNES) options","SNES");CHKERRQ(ierr);
203186905e3SBarry Smith     if (snes->type_name) {
204186905e3SBarry Smith       deft = snes->type_name;
205186905e3SBarry Smith     } else {
2064b27c08aSLois Curfman McInnes       deft = SNESLS;
207d64ed03dSBarry Smith     }
2084bbc92c1SBarry Smith 
209186905e3SBarry Smith     if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
210b0a32e0cSBarry Smith     ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr);
211d64ed03dSBarry Smith     if (flg) {
212186905e3SBarry Smith       ierr = SNESSetType(snes,type);CHKERRQ(ierr);
213186905e3SBarry Smith     } else if (!snes->type_name) {
214186905e3SBarry Smith       ierr = SNESSetType(snes,deft);CHKERRQ(ierr);
215d64ed03dSBarry Smith     }
216909c8a9fSBarry Smith     ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr);
21793c39befSBarry Smith 
21887828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_stol","Stop if step length less then","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr);
21970441072SBarry Smith     ierr = PetscOptionsReal("-snes_atol","Stop if function norm less then","SNESSetTolerances",snes->abstol,&snes->abstol,0);CHKERRQ(ierr);
220186905e3SBarry Smith 
22187828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less then","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr);
222b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr);
223b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr);
22450ffb88aSMatthew Knepley     ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr);
2255968eb51SBarry Smith     ierr = PetscOptionsName("-snes_converged_reason","Print reason for converged or diverged","SNESSolve",&flg);CHKERRQ(ierr);
2265968eb51SBarry Smith     if (flg) {
2275968eb51SBarry Smith       snes->printreason = PETSC_TRUE;
2285968eb51SBarry Smith     }
229186905e3SBarry Smith 
2307aaed0d8SBarry Smith     ierr = PetscOptionsTruth("-snes_ksp_ew_conv","Use Eisentat-Walker linear system convergence test","SNES_KSP_SetParametersEW",snes->ksp_ewconv,&snes->ksp_ewconv,PETSC_NULL);CHKERRQ(ierr);
231186905e3SBarry Smith 
232cf502942SBarry Smith     ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNES_KSP_SetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr);
23387828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNES_KSP_SetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr);
23487828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNES_KSP_SetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr);
23587828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNES_KSP_SetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr);
23687828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNES_KSP_SetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr);
23787828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNES_KSP_SetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr);
23887828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNES_KSP_SetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr);
239186905e3SBarry Smith 
240b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_no_convergence_test","Don't test for convergence","None",&flg);CHKERRQ(ierr);
241e7788613SBarry Smith     if (flg) {snes->ops->converged = 0;}
242a6570f20SBarry Smith     ierr = PetscOptionsName("-snes_monitor_cancel","Remove all monitors","SNESMonitorCancel",&flg);CHKERRQ(ierr);
243a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);}
244eabae89aSBarry Smith 
245a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor","Monitor norm of function","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
246e8105e01SRichard Katz     if (flg) {
24723d894e5SBarry Smith       ierr = PetscViewerASCIIMonitorCreate(snes->comm,monfilename,0,&monviewer);CHKERRQ(ierr);
24823d894e5SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefault,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
249e8105e01SRichard Katz     }
250eabae89aSBarry Smith 
251a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_ratiomonitor","Monitor ratios of norms of function","SNESMonitorSetRatio","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
252eabae89aSBarry Smith     if (flg) {
25323d894e5SBarry Smith       ierr = PetscViewerASCIIMonitorCreate(snes->comm,monfilename,0,&monviewer);CHKERRQ(ierr);
254f1bef1bcSMatthew Knepley       ierr = SNESMonitorSetRatio(snes,monviewer);CHKERRQ(ierr);
255e8105e01SRichard Katz     }
256eabae89aSBarry Smith 
257a6570f20SBarry Smith     ierr = PetscOptionsString("-snes_monitor_short","Monitor norm of function (fewer digits)","SNESMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
258eabae89aSBarry Smith     if (flg) {
25923d894e5SBarry Smith       ierr = PetscViewerASCIIMonitorCreate(snes->comm,monfilename,0,&monviewer);CHKERRQ(ierr);
26023d894e5SBarry Smith       ierr = SNESMonitorSet(snes,SNESMonitorDefaultShort,monviewer,(PetscErrorCode (*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
261eabae89aSBarry Smith     }
262eabae89aSBarry Smith 
263a6570f20SBarry Smith     ierr = PetscOptionsName("-snes_monitor_solution","Plot solution at each iteration","SNESMonitorSolution",&flg);CHKERRQ(ierr);
264a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolution,0,0);CHKERRQ(ierr);}
265a6570f20SBarry Smith     ierr = PetscOptionsName("-snes_monitor_solution_update","Plot correction at each iteration","SNESMonitorSolutionUpdate",&flg);CHKERRQ(ierr);
266a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorSolutionUpdate,0,0);CHKERRQ(ierr);}
267a6570f20SBarry Smith     ierr = PetscOptionsName("-snes_monitor_residual","Plot residual at each iteration","SNESMonitorResidual",&flg);CHKERRQ(ierr);
268a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorResidual,0,0);CHKERRQ(ierr);}
269a6570f20SBarry Smith     ierr = PetscOptionsName("-snes_monitor_draw","Plot function norm at each iteration","SNESMonitorLG",&flg);CHKERRQ(ierr);
270a6570f20SBarry Smith     if (flg) {ierr = SNESMonitorSet(snes,SNESMonitorLG,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
271e24b481bSBarry Smith 
272b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",&flg);CHKERRQ(ierr);
2734b27c08aSLois Curfman McInnes     if (flg) {
274186905e3SBarry Smith       ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr);
275ae15b995SBarry Smith       ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr);
2769b94acceSBarry Smith     }
277639f9d9dSBarry Smith 
27876b2cf59SMatthew Knepley     for(i = 0; i < numberofsetfromoptions; i++) {
27976b2cf59SMatthew Knepley       ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr);
28076b2cf59SMatthew Knepley     }
28176b2cf59SMatthew Knepley 
282e7788613SBarry Smith     if (snes->ops->setfromoptions) {
283e7788613SBarry Smith       ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr);
284639f9d9dSBarry Smith     }
285b0a32e0cSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
2864bbc92c1SBarry Smith 
28794b7f48cSBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
28894b7f48cSBarry Smith   ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);
28993993e2dSLois Curfman McInnes 
2903a40ed3dSBarry Smith   PetscFunctionReturn(0);
2919b94acceSBarry Smith }
2929b94acceSBarry Smith 
293a847f771SSatish Balay 
2944a2ae208SSatish Balay #undef __FUNCT__
2954a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext"
2969b94acceSBarry Smith /*@
2979b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
2989b94acceSBarry Smith    the nonlinear solvers.
2999b94acceSBarry Smith 
300fee21e36SBarry Smith    Collective on SNES
301fee21e36SBarry Smith 
302c7afd0dbSLois Curfman McInnes    Input Parameters:
303c7afd0dbSLois Curfman McInnes +  snes - the SNES context
304c7afd0dbSLois Curfman McInnes -  usrP - optional user context
305c7afd0dbSLois Curfman McInnes 
30636851e7fSLois Curfman McInnes    Level: intermediate
30736851e7fSLois Curfman McInnes 
3089b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
3099b94acceSBarry Smith 
3109b94acceSBarry Smith .seealso: SNESGetApplicationContext()
3119b94acceSBarry Smith @*/
31263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetApplicationContext(SNES snes,void *usrP)
3139b94acceSBarry Smith {
3143a40ed3dSBarry Smith   PetscFunctionBegin;
3154482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
3169b94acceSBarry Smith   snes->user		= usrP;
3173a40ed3dSBarry Smith   PetscFunctionReturn(0);
3189b94acceSBarry Smith }
31974679c65SBarry Smith 
3204a2ae208SSatish Balay #undef __FUNCT__
3214a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext"
3229b94acceSBarry Smith /*@C
3239b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
3249b94acceSBarry Smith    nonlinear solvers.
3259b94acceSBarry Smith 
326c7afd0dbSLois Curfman McInnes    Not Collective
327c7afd0dbSLois Curfman McInnes 
3289b94acceSBarry Smith    Input Parameter:
3299b94acceSBarry Smith .  snes - SNES context
3309b94acceSBarry Smith 
3319b94acceSBarry Smith    Output Parameter:
3329b94acceSBarry Smith .  usrP - user context
3339b94acceSBarry Smith 
33436851e7fSLois Curfman McInnes    Level: intermediate
33536851e7fSLois Curfman McInnes 
3369b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
3379b94acceSBarry Smith 
3389b94acceSBarry Smith .seealso: SNESSetApplicationContext()
3399b94acceSBarry Smith @*/
34063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetApplicationContext(SNES snes,void **usrP)
3419b94acceSBarry Smith {
3423a40ed3dSBarry Smith   PetscFunctionBegin;
3434482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
3449b94acceSBarry Smith   *usrP = snes->user;
3453a40ed3dSBarry Smith   PetscFunctionReturn(0);
3469b94acceSBarry Smith }
34774679c65SBarry Smith 
3484a2ae208SSatish Balay #undef __FUNCT__
3494a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber"
3509b94acceSBarry Smith /*@
351c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
352c8228a4eSBarry Smith    at this time.
3539b94acceSBarry Smith 
354c7afd0dbSLois Curfman McInnes    Not Collective
355c7afd0dbSLois Curfman McInnes 
3569b94acceSBarry Smith    Input Parameter:
3579b94acceSBarry Smith .  snes - SNES context
3589b94acceSBarry Smith 
3599b94acceSBarry Smith    Output Parameter:
3609b94acceSBarry Smith .  iter - iteration number
3619b94acceSBarry Smith 
362c8228a4eSBarry Smith    Notes:
363c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
364c8228a4eSBarry Smith 
365c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
36608405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
36708405cd6SLois Curfman McInnes .vb
36808405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
36908405cd6SLois Curfman McInnes       if (!(it % 2)) {
37008405cd6SLois Curfman McInnes         [compute Jacobian here]
37108405cd6SLois Curfman McInnes       }
37208405cd6SLois Curfman McInnes .ve
373c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
37408405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
375c8228a4eSBarry Smith 
37636851e7fSLois Curfman McInnes    Level: intermediate
37736851e7fSLois Curfman McInnes 
3782b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number,
3792b668275SBarry Smith 
3802b668275SBarry Smith .seealso:   SNESGetFunctionNorm(), SNESGetNumberLinearIterations()
3819b94acceSBarry Smith @*/
38263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetIterationNumber(SNES snes,PetscInt* iter)
3839b94acceSBarry Smith {
3843a40ed3dSBarry Smith   PetscFunctionBegin;
3854482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
3864482741eSBarry Smith   PetscValidIntPointer(iter,2);
3879b94acceSBarry Smith   *iter = snes->iter;
3883a40ed3dSBarry Smith   PetscFunctionReturn(0);
3899b94acceSBarry Smith }
39074679c65SBarry Smith 
3914a2ae208SSatish Balay #undef __FUNCT__
3924a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm"
3939b94acceSBarry Smith /*@
3949b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
3959b94acceSBarry Smith    with SNESSSetFunction().
3969b94acceSBarry Smith 
397c7afd0dbSLois Curfman McInnes    Collective on SNES
398c7afd0dbSLois Curfman McInnes 
3999b94acceSBarry Smith    Input Parameter:
4009b94acceSBarry Smith .  snes - SNES context
4019b94acceSBarry Smith 
4029b94acceSBarry Smith    Output Parameter:
4039b94acceSBarry Smith .  fnorm - 2-norm of function
4049b94acceSBarry Smith 
40536851e7fSLois Curfman McInnes    Level: intermediate
40636851e7fSLois Curfman McInnes 
4079b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
408a86d99e1SLois Curfman McInnes 
4092b668275SBarry Smith .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetNumberLinearIterations()
4109b94acceSBarry Smith @*/
41163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunctionNorm(SNES snes,PetscScalar *fnorm)
4129b94acceSBarry Smith {
4133a40ed3dSBarry Smith   PetscFunctionBegin;
4144482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
4154482741eSBarry Smith   PetscValidScalarPointer(fnorm,2);
4169b94acceSBarry Smith   *fnorm = snes->norm;
4173a40ed3dSBarry Smith   PetscFunctionReturn(0);
4189b94acceSBarry Smith }
41974679c65SBarry Smith 
4204a2ae208SSatish Balay #undef __FUNCT__
4214a2ae208SSatish Balay #define __FUNCT__ "SNESGetNumberUnsuccessfulSteps"
4229b94acceSBarry Smith /*@
4239b94acceSBarry Smith    SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps
4249b94acceSBarry Smith    attempted by the nonlinear solver.
4259b94acceSBarry Smith 
426c7afd0dbSLois Curfman McInnes    Not Collective
427c7afd0dbSLois Curfman McInnes 
4289b94acceSBarry Smith    Input Parameter:
4299b94acceSBarry Smith .  snes - SNES context
4309b94acceSBarry Smith 
4319b94acceSBarry Smith    Output Parameter:
4329b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
4339b94acceSBarry Smith 
434c96a6f78SLois Curfman McInnes    Notes:
435c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
436c96a6f78SLois Curfman McInnes 
43736851e7fSLois Curfman McInnes    Level: intermediate
43836851e7fSLois Curfman McInnes 
4399b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
4409b94acceSBarry Smith @*/
44163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNumberUnsuccessfulSteps(SNES snes,PetscInt* nfails)
4429b94acceSBarry Smith {
4433a40ed3dSBarry Smith   PetscFunctionBegin;
4444482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
4454482741eSBarry Smith   PetscValidIntPointer(nfails,2);
44650ffb88aSMatthew Knepley   *nfails = snes->numFailures;
44750ffb88aSMatthew Knepley   PetscFunctionReturn(0);
44850ffb88aSMatthew Knepley }
44950ffb88aSMatthew Knepley 
45050ffb88aSMatthew Knepley #undef __FUNCT__
45150ffb88aSMatthew Knepley #define __FUNCT__ "SNESSetMaximumUnsuccessfulSteps"
45250ffb88aSMatthew Knepley /*@
45350ffb88aSMatthew Knepley    SNESSetMaximumUnsuccessfulSteps - Sets the maximum number of unsuccessful steps
45450ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
45550ffb88aSMatthew Knepley 
45650ffb88aSMatthew Knepley    Not Collective
45750ffb88aSMatthew Knepley 
45850ffb88aSMatthew Knepley    Input Parameters:
45950ffb88aSMatthew Knepley +  snes     - SNES context
46050ffb88aSMatthew Knepley -  maxFails - maximum of unsuccessful steps
46150ffb88aSMatthew Knepley 
46250ffb88aSMatthew Knepley    Level: intermediate
46350ffb88aSMatthew Knepley 
46450ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
46550ffb88aSMatthew Knepley @*/
46663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaximumUnsuccessfulSteps(SNES snes, PetscInt maxFails)
46750ffb88aSMatthew Knepley {
46850ffb88aSMatthew Knepley   PetscFunctionBegin;
4694482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
47050ffb88aSMatthew Knepley   snes->maxFailures = maxFails;
47150ffb88aSMatthew Knepley   PetscFunctionReturn(0);
47250ffb88aSMatthew Knepley }
47350ffb88aSMatthew Knepley 
47450ffb88aSMatthew Knepley #undef __FUNCT__
47550ffb88aSMatthew Knepley #define __FUNCT__ "SNESGetMaximumUnsuccessfulSteps"
47650ffb88aSMatthew Knepley /*@
47750ffb88aSMatthew Knepley    SNESGetMaximumUnsuccessfulSteps - Gets the maximum number of unsuccessful steps
47850ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
47950ffb88aSMatthew Knepley 
48050ffb88aSMatthew Knepley    Not Collective
48150ffb88aSMatthew Knepley 
48250ffb88aSMatthew Knepley    Input Parameter:
48350ffb88aSMatthew Knepley .  snes     - SNES context
48450ffb88aSMatthew Knepley 
48550ffb88aSMatthew Knepley    Output Parameter:
48650ffb88aSMatthew Knepley .  maxFails - maximum of unsuccessful steps
48750ffb88aSMatthew Knepley 
48850ffb88aSMatthew Knepley    Level: intermediate
48950ffb88aSMatthew Knepley 
49050ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
49150ffb88aSMatthew Knepley @*/
49263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaximumUnsuccessfulSteps(SNES snes, PetscInt *maxFails)
49350ffb88aSMatthew Knepley {
49450ffb88aSMatthew Knepley   PetscFunctionBegin;
4954482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
4964482741eSBarry Smith   PetscValidIntPointer(maxFails,2);
49750ffb88aSMatthew Knepley   *maxFails = snes->maxFailures;
4983a40ed3dSBarry Smith   PetscFunctionReturn(0);
4999b94acceSBarry Smith }
500a847f771SSatish Balay 
5014a2ae208SSatish Balay #undef __FUNCT__
5023d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures"
5033d4c4710SBarry Smith /*@
5043d4c4710SBarry Smith    SNESGetLinearSolveFailures - Gets the number of failed (non-converged)
5053d4c4710SBarry Smith    linear solvers.
5063d4c4710SBarry Smith 
5073d4c4710SBarry Smith    Not Collective
5083d4c4710SBarry Smith 
5093d4c4710SBarry Smith    Input Parameter:
5103d4c4710SBarry Smith .  snes - SNES context
5113d4c4710SBarry Smith 
5123d4c4710SBarry Smith    Output Parameter:
5133d4c4710SBarry Smith .  nfails - number of failed solves
5143d4c4710SBarry Smith 
5153d4c4710SBarry Smith    Notes:
5163d4c4710SBarry Smith    This counter is reset to zero for each successive call to SNESSolve().
5173d4c4710SBarry Smith 
5183d4c4710SBarry Smith    Level: intermediate
5193d4c4710SBarry Smith 
5203d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
5213d4c4710SBarry Smith @*/
5223d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails)
5233d4c4710SBarry Smith {
5243d4c4710SBarry Smith   PetscFunctionBegin;
5253d4c4710SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
5263d4c4710SBarry Smith   PetscValidIntPointer(nfails,2);
5273d4c4710SBarry Smith   *nfails = snes->numLinearSolveFailures;
5283d4c4710SBarry Smith   PetscFunctionReturn(0);
5293d4c4710SBarry Smith }
5303d4c4710SBarry Smith 
5313d4c4710SBarry Smith #undef __FUNCT__
5323d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures"
5333d4c4710SBarry Smith /*@
5343d4c4710SBarry Smith    SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts
5353d4c4710SBarry Smith    allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE
5363d4c4710SBarry Smith 
5373d4c4710SBarry Smith    Collective on SNES
5383d4c4710SBarry Smith 
5393d4c4710SBarry Smith    Input Parameters:
5403d4c4710SBarry Smith +  snes     - SNES context
5413d4c4710SBarry Smith -  maxFails - maximum allowed linear solve failures
5423d4c4710SBarry Smith 
5433d4c4710SBarry Smith    Level: intermediate
5443d4c4710SBarry Smith 
5453d4c4710SBarry Smith    Notes: By default this is 1; that is SNES returns on the first failed linear solve
5463d4c4710SBarry Smith 
5473d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
5483d4c4710SBarry Smith 
5493d4c4710SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures()
5503d4c4710SBarry Smith @*/
5513d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails)
5523d4c4710SBarry Smith {
5533d4c4710SBarry Smith   PetscFunctionBegin;
5543d4c4710SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
5553d4c4710SBarry Smith   snes->maxLinearSolveFailures = maxFails;
5563d4c4710SBarry Smith   PetscFunctionReturn(0);
5573d4c4710SBarry Smith }
5583d4c4710SBarry Smith 
5593d4c4710SBarry Smith #undef __FUNCT__
5603d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures"
5613d4c4710SBarry Smith /*@
5623d4c4710SBarry Smith    SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that
5633d4c4710SBarry Smith      are allowed before SNES terminates
5643d4c4710SBarry Smith 
5653d4c4710SBarry Smith    Not Collective
5663d4c4710SBarry Smith 
5673d4c4710SBarry Smith    Input Parameter:
5683d4c4710SBarry Smith .  snes     - SNES context
5693d4c4710SBarry Smith 
5703d4c4710SBarry Smith    Output Parameter:
5713d4c4710SBarry Smith .  maxFails - maximum of unsuccessful solves allowed
5723d4c4710SBarry Smith 
5733d4c4710SBarry Smith    Level: intermediate
5743d4c4710SBarry Smith 
5753d4c4710SBarry Smith    Notes: By default this is 1; that is SNES returns on the first failed linear solve
5763d4c4710SBarry Smith 
5773d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
5783d4c4710SBarry Smith 
5793d4c4710SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures()
5803d4c4710SBarry Smith @*/
5813d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails)
5823d4c4710SBarry Smith {
5833d4c4710SBarry Smith   PetscFunctionBegin;
5843d4c4710SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
5853d4c4710SBarry Smith   PetscValidIntPointer(maxFails,2);
5863d4c4710SBarry Smith   *maxFails = snes->maxLinearSolveFailures;
5873d4c4710SBarry Smith   PetscFunctionReturn(0);
5883d4c4710SBarry Smith }
5893d4c4710SBarry Smith 
5903d4c4710SBarry Smith #undef __FUNCT__
5914a2ae208SSatish Balay #define __FUNCT__ "SNESGetNumberLinearIterations"
592c96a6f78SLois Curfman McInnes /*@
593c96a6f78SLois Curfman McInnes    SNESGetNumberLinearIterations - Gets the total number of linear iterations
594c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
595c96a6f78SLois Curfman McInnes 
596c7afd0dbSLois Curfman McInnes    Not Collective
597c7afd0dbSLois Curfman McInnes 
598c96a6f78SLois Curfman McInnes    Input Parameter:
599c96a6f78SLois Curfman McInnes .  snes - SNES context
600c96a6f78SLois Curfman McInnes 
601c96a6f78SLois Curfman McInnes    Output Parameter:
602c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
603c96a6f78SLois Curfman McInnes 
604c96a6f78SLois Curfman McInnes    Notes:
605c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
606c96a6f78SLois Curfman McInnes 
60736851e7fSLois Curfman McInnes    Level: intermediate
60836851e7fSLois Curfman McInnes 
609c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
6102b668275SBarry Smith 
6112b668275SBarry Smith .seealso:  SNESGetIterationNumber(), SNESGetFunctionNorm()
612c96a6f78SLois Curfman McInnes @*/
61363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNumberLinearIterations(SNES snes,PetscInt* lits)
614c96a6f78SLois Curfman McInnes {
6153a40ed3dSBarry Smith   PetscFunctionBegin;
6164482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
6174482741eSBarry Smith   PetscValidIntPointer(lits,2);
618c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
6193a40ed3dSBarry Smith   PetscFunctionReturn(0);
620c96a6f78SLois Curfman McInnes }
621c96a6f78SLois Curfman McInnes 
6224a2ae208SSatish Balay #undef __FUNCT__
62394b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP"
62452baeb72SSatish Balay /*@
62594b7f48cSBarry Smith    SNESGetKSP - Returns the KSP context for a SNES solver.
6269b94acceSBarry Smith 
62794b7f48cSBarry Smith    Not Collective, but if SNES object is parallel, then KSP object is parallel
628c7afd0dbSLois Curfman McInnes 
6299b94acceSBarry Smith    Input Parameter:
6309b94acceSBarry Smith .  snes - the SNES context
6319b94acceSBarry Smith 
6329b94acceSBarry Smith    Output Parameter:
63394b7f48cSBarry Smith .  ksp - the KSP context
6349b94acceSBarry Smith 
6359b94acceSBarry Smith    Notes:
63694b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
6379b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
6382999313aSBarry Smith    PC contexts as well.
6399b94acceSBarry Smith 
64036851e7fSLois Curfman McInnes    Level: beginner
64136851e7fSLois Curfman McInnes 
64294b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
6439b94acceSBarry Smith 
6442999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
6459b94acceSBarry Smith @*/
64663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetKSP(SNES snes,KSP *ksp)
6479b94acceSBarry Smith {
6483a40ed3dSBarry Smith   PetscFunctionBegin;
6494482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
6504482741eSBarry Smith   PetscValidPointer(ksp,2);
65194b7f48cSBarry Smith   *ksp = snes->ksp;
6523a40ed3dSBarry Smith   PetscFunctionReturn(0);
6539b94acceSBarry Smith }
65482bf6240SBarry Smith 
6554a2ae208SSatish Balay #undef __FUNCT__
6562999313aSBarry Smith #define __FUNCT__ "SNESSetKSP"
6572999313aSBarry Smith /*@
6582999313aSBarry Smith    SNESSetKSP - Sets a KSP context for the SNES object to use
6592999313aSBarry Smith 
6602999313aSBarry Smith    Not Collective, but the SNES and KSP objects must live on the same MPI_Comm
6612999313aSBarry Smith 
6622999313aSBarry Smith    Input Parameters:
6632999313aSBarry Smith +  snes - the SNES context
6642999313aSBarry Smith -  ksp - the KSP context
6652999313aSBarry Smith 
6662999313aSBarry Smith    Notes:
6672999313aSBarry Smith    The SNES object already has its KSP object, you can obtain with SNESGetKSP()
6682999313aSBarry Smith    so this routine is rarely needed.
6692999313aSBarry Smith 
6702999313aSBarry Smith    The KSP object that is already in the SNES object has its reference count
6712999313aSBarry Smith    decreased by one.
6722999313aSBarry Smith 
6732999313aSBarry Smith    Level: developer
6742999313aSBarry Smith 
6752999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
6762999313aSBarry Smith 
6772999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
6782999313aSBarry Smith @*/
6792999313aSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetKSP(SNES snes,KSP ksp)
6802999313aSBarry Smith {
6812999313aSBarry Smith   PetscErrorCode ierr;
6822999313aSBarry Smith 
6832999313aSBarry Smith   PetscFunctionBegin;
6842999313aSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
6852999313aSBarry Smith   PetscValidHeaderSpecific(ksp,KSP_COOKIE,2);
6862999313aSBarry Smith   PetscCheckSameComm(snes,1,ksp,2);
6877dcf0eaaSdalcinl   ierr = PetscObjectReference((PetscObject)ksp);CHKERRQ(ierr);
688906ed7ccSBarry Smith   if (snes->ksp) {ierr = PetscObjectDereference((PetscObject)snes->ksp);CHKERRQ(ierr);}
6892999313aSBarry Smith   snes->ksp = ksp;
6902999313aSBarry Smith   PetscFunctionReturn(0);
6912999313aSBarry Smith }
6922999313aSBarry Smith 
6932999313aSBarry Smith #undef __FUNCT__
6944a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc"
6956849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj)
696e24b481bSBarry Smith {
697e24b481bSBarry Smith   PetscFunctionBegin;
698e24b481bSBarry Smith   PetscFunctionReturn(0);
699e24b481bSBarry Smith }
700e24b481bSBarry Smith 
7019b94acceSBarry Smith /* -----------------------------------------------------------*/
7024a2ae208SSatish Balay #undef __FUNCT__
7034a2ae208SSatish Balay #define __FUNCT__ "SNESCreate"
70452baeb72SSatish Balay /*@
7059b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
7069b94acceSBarry Smith 
707c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
708c7afd0dbSLois Curfman McInnes 
709c7afd0dbSLois Curfman McInnes    Input Parameters:
710906ed7ccSBarry Smith .  comm - MPI communicator
7119b94acceSBarry Smith 
7129b94acceSBarry Smith    Output Parameter:
7139b94acceSBarry Smith .  outsnes - the new SNES context
7149b94acceSBarry Smith 
715c7afd0dbSLois Curfman McInnes    Options Database Keys:
716c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
717c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
718c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
719c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
720c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
721c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
722c1f60f51SBarry Smith 
72336851e7fSLois Curfman McInnes    Level: beginner
72436851e7fSLois Curfman McInnes 
7259b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
7269b94acceSBarry Smith 
7274b27c08aSLois Curfman McInnes .seealso: SNESSolve(), SNESDestroy(), SNES
7289b94acceSBarry Smith @*/
72963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESCreate(MPI_Comm comm,SNES *outsnes)
7309b94acceSBarry Smith {
731dfbe8321SBarry Smith   PetscErrorCode      ierr;
7329b94acceSBarry Smith   SNES                snes;
7339b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
73437fcc0dbSBarry Smith 
7353a40ed3dSBarry Smith   PetscFunctionBegin;
736ed1caa07SMatthew Knepley   PetscValidPointer(outsnes,2);
7378ba1e511SMatthew Knepley   *outsnes = PETSC_NULL;
7388ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
7398ba1e511SMatthew Knepley   ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr);
7408ba1e511SMatthew Knepley #endif
7418ba1e511SMatthew Knepley 
742e7788613SBarry Smith   ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr);
743e24b481bSBarry Smith   snes->bops->publish     = SNESPublish_Petsc;
7449b94acceSBarry Smith   snes->max_its           = 50;
7459750a799SBarry Smith   snes->max_funcs	  = 10000;
7469b94acceSBarry Smith   snes->norm		  = 0.0;
747b4874afaSBarry Smith   snes->rtol		  = 1.e-8;
748b4874afaSBarry Smith   snes->ttol              = 0.0;
74970441072SBarry Smith   snes->abstol		  = 1.e-50;
7509b94acceSBarry Smith   snes->xtol		  = 1.e-8;
7514b27c08aSLois Curfman McInnes   snes->deltatol	  = 1.e-12;
7529b94acceSBarry Smith   snes->nfuncs            = 0;
75350ffb88aSMatthew Knepley   snes->numFailures       = 0;
75450ffb88aSMatthew Knepley   snes->maxFailures       = 1;
7557a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
756639f9d9dSBarry Smith   snes->numbermonitors    = 0;
7579b94acceSBarry Smith   snes->data              = 0;
758e7788613SBarry Smith   snes->ops->view         = 0;
7594dc4c822SBarry Smith   snes->setupcalled       = PETSC_FALSE;
760186905e3SBarry Smith   snes->ksp_ewconv        = PETSC_FALSE;
7616f24a144SLois Curfman McInnes   snes->vwork             = 0;
7626f24a144SLois Curfman McInnes   snes->nwork             = 0;
763758f92a0SBarry Smith   snes->conv_hist_len     = 0;
764758f92a0SBarry Smith   snes->conv_hist_max     = 0;
765758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
766758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
767758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
768184914b5SBarry Smith   snes->reason            = SNES_CONVERGED_ITERATING;
7699b94acceSBarry Smith 
7703d4c4710SBarry Smith   snes->numLinearSolveFailures = 0;
7713d4c4710SBarry Smith   snes->maxLinearSolveFailures = 1;
7723d4c4710SBarry Smith 
7739b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
774b0a32e0cSBarry Smith   ierr = PetscNew(SNES_KSP_EW_ConvCtx,&kctx);CHKERRQ(ierr);
77552e6d16bSBarry Smith   ierr = PetscLogObjectMemory(snes,sizeof(SNES_KSP_EW_ConvCtx));CHKERRQ(ierr);
7769b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
7779b94acceSBarry Smith   kctx->version     = 2;
7789b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
7799b94acceSBarry Smith                              this was too large for some test cases */
7809b94acceSBarry Smith   kctx->rtol_last   = 0;
7819b94acceSBarry Smith   kctx->rtol_max    = .9;
7829b94acceSBarry Smith   kctx->gamma       = 1.0;
7839b94acceSBarry Smith   kctx->alpha2      = .5*(1.0 + sqrt(5.0));
7849b94acceSBarry Smith   kctx->alpha       = kctx->alpha2;
7859b94acceSBarry Smith   kctx->threshold   = .1;
7869b94acceSBarry Smith   kctx->lresid_last = 0;
7879b94acceSBarry Smith   kctx->norm_last   = 0;
7889b94acceSBarry Smith 
78994b7f48cSBarry Smith   ierr = KSPCreate(comm,&snes->ksp);CHKERRQ(ierr);
79052e6d16bSBarry Smith   ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr);
7915334005bSBarry Smith 
7929b94acceSBarry Smith   *outsnes = snes;
79300036973SBarry Smith   ierr = PetscPublishAll(snes);CHKERRQ(ierr);
7943a40ed3dSBarry Smith   PetscFunctionReturn(0);
7959b94acceSBarry Smith }
7969b94acceSBarry Smith 
7974a2ae208SSatish Balay #undef __FUNCT__
7984a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction"
7999b94acceSBarry Smith /*@C
8009b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
8019b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
8029b94acceSBarry Smith    equations.
8039b94acceSBarry Smith 
804fee21e36SBarry Smith    Collective on SNES
805fee21e36SBarry Smith 
806c7afd0dbSLois Curfman McInnes    Input Parameters:
807c7afd0dbSLois Curfman McInnes +  snes - the SNES context
808c7afd0dbSLois Curfman McInnes .  r - vector to store function value
809de044059SHong Zhang .  func - function evaluation routine
810c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
811c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
8129b94acceSBarry Smith 
813c7afd0dbSLois Curfman McInnes    Calling sequence of func:
8148d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
815c7afd0dbSLois Curfman McInnes 
816313e4042SLois Curfman McInnes .  f - function vector
817c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
8189b94acceSBarry Smith 
8199b94acceSBarry Smith    Notes:
8209b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
8219b94acceSBarry Smith $      f'(x) x = -f(x),
822c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
8239b94acceSBarry Smith 
82436851e7fSLois Curfman McInnes    Level: beginner
82536851e7fSLois Curfman McInnes 
8269b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
8279b94acceSBarry Smith 
828a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
8299b94acceSBarry Smith @*/
83063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx)
8319b94acceSBarry Smith {
8323a40ed3dSBarry Smith   PetscFunctionBegin;
8334482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
8344482741eSBarry Smith   PetscValidHeaderSpecific(r,VEC_COOKIE,2);
835c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,r,2);
836184914b5SBarry Smith 
837e7788613SBarry Smith   snes->ops->computefunction = func;
8389b94acceSBarry Smith   snes->vec_func             = snes->vec_func_always = r;
8399b94acceSBarry Smith   snes->funP                 = ctx;
8403a40ed3dSBarry Smith   PetscFunctionReturn(0);
8419b94acceSBarry Smith }
8429b94acceSBarry Smith 
8433ab0aad5SBarry Smith /* --------------------------------------------------------------- */
8443ab0aad5SBarry Smith #undef __FUNCT__
8453ab0aad5SBarry Smith #define __FUNCT__ "SNESSetRhs"
8463ab0aad5SBarry Smith /*@C
8473ab0aad5SBarry Smith    SNESSetRhs - Sets the vector for solving F(x) = rhs. If rhs is not set
8483ab0aad5SBarry Smith    it assumes a zero right hand side.
8493ab0aad5SBarry Smith 
8503ab0aad5SBarry Smith    Collective on SNES
8513ab0aad5SBarry Smith 
8523ab0aad5SBarry Smith    Input Parameters:
8533ab0aad5SBarry Smith +  snes - the SNES context
8543ab0aad5SBarry Smith -  rhs - the right hand side vector or PETSC_NULL for a zero right hand side
8553ab0aad5SBarry Smith 
8563ab0aad5SBarry Smith    Level: intermediate
8573ab0aad5SBarry Smith 
8583ab0aad5SBarry Smith .keywords: SNES, nonlinear, set, function, right hand side
8593ab0aad5SBarry Smith 
8601096aae1SMatthew Knepley .seealso: SNESGetRhs(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
8613ab0aad5SBarry Smith @*/
86263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetRhs(SNES snes,Vec rhs)
8633ab0aad5SBarry Smith {
864dfbe8321SBarry Smith   PetscErrorCode ierr;
8653ab0aad5SBarry Smith 
8663ab0aad5SBarry Smith   PetscFunctionBegin;
8673ab0aad5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
8683ab0aad5SBarry Smith   if (rhs) {
8693ab0aad5SBarry Smith     PetscValidHeaderSpecific(rhs,VEC_COOKIE,2);
8703ab0aad5SBarry Smith     PetscCheckSameComm(snes,1,rhs,2);
8713ab0aad5SBarry Smith     ierr = PetscObjectReference((PetscObject)rhs);CHKERRQ(ierr);
8723ab0aad5SBarry Smith   }
8733ab0aad5SBarry Smith   if (snes->afine) {
8743ab0aad5SBarry Smith     ierr = VecDestroy(snes->afine);CHKERRQ(ierr);
8753ab0aad5SBarry Smith   }
8763ab0aad5SBarry Smith   snes->afine = rhs;
8773ab0aad5SBarry Smith   PetscFunctionReturn(0);
8783ab0aad5SBarry Smith }
8793ab0aad5SBarry Smith 
8804a2ae208SSatish Balay #undef __FUNCT__
8811096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs"
8821096aae1SMatthew Knepley /*@C
8831096aae1SMatthew Knepley    SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set
8841096aae1SMatthew Knepley    it assumes a zero right hand side.
8851096aae1SMatthew Knepley 
8861096aae1SMatthew Knepley    Collective on SNES
8871096aae1SMatthew Knepley 
8881096aae1SMatthew Knepley    Input Parameter:
8891096aae1SMatthew Knepley .  snes - the SNES context
8901096aae1SMatthew Knepley 
8911096aae1SMatthew Knepley    Output Parameter:
8921096aae1SMatthew Knepley .  rhs - the right hand side vector or PETSC_NULL for a zero right hand side
8931096aae1SMatthew Knepley 
8941096aae1SMatthew Knepley    Level: intermediate
8951096aae1SMatthew Knepley 
8961096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side
8971096aae1SMatthew Knepley 
8981096aae1SMatthew Knepley .seealso: SNESSetRhs(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
8991096aae1SMatthew Knepley @*/
9001096aae1SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetRhs(SNES snes,Vec *rhs)
9011096aae1SMatthew Knepley {
9021096aae1SMatthew Knepley   PetscFunctionBegin;
9031096aae1SMatthew Knepley   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
9041096aae1SMatthew Knepley   PetscValidPointer(rhs,2);
9051096aae1SMatthew Knepley   *rhs = snes->afine;
9061096aae1SMatthew Knepley   PetscFunctionReturn(0);
9071096aae1SMatthew Knepley }
9081096aae1SMatthew Knepley 
9091096aae1SMatthew Knepley #undef __FUNCT__
9104a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction"
9119b94acceSBarry Smith /*@
91236851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
9139b94acceSBarry Smith                          SNESSetFunction().
9149b94acceSBarry Smith 
915c7afd0dbSLois Curfman McInnes    Collective on SNES
916c7afd0dbSLois Curfman McInnes 
9179b94acceSBarry Smith    Input Parameters:
918c7afd0dbSLois Curfman McInnes +  snes - the SNES context
919c7afd0dbSLois Curfman McInnes -  x - input vector
9209b94acceSBarry Smith 
9219b94acceSBarry Smith    Output Parameter:
9223638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
9239b94acceSBarry Smith 
9241bffabb2SLois Curfman McInnes    Notes:
92536851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
92636851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
92736851e7fSLois Curfman McInnes    themselves.
92836851e7fSLois Curfman McInnes 
92936851e7fSLois Curfman McInnes    Level: developer
93036851e7fSLois Curfman McInnes 
9319b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
9329b94acceSBarry Smith 
933a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
9349b94acceSBarry Smith @*/
93563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeFunction(SNES snes,Vec x,Vec y)
9369b94acceSBarry Smith {
937dfbe8321SBarry Smith   PetscErrorCode ierr;
9389b94acceSBarry Smith 
9393a40ed3dSBarry Smith   PetscFunctionBegin;
9404482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
9414482741eSBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE,2);
9424482741eSBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE,3);
943c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,x,2);
944c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,3);
945184914b5SBarry Smith 
946d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
947e7788613SBarry Smith   if (snes->ops->computefunction) {
948d64ed03dSBarry Smith     PetscStackPush("SNES user function");
949e9a2bbcdSBarry Smith     CHKMEMQ;
950e7788613SBarry Smith     ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP);
951e9a2bbcdSBarry Smith     CHKMEMQ;
952d64ed03dSBarry Smith     PetscStackPop;
953d5e45103SBarry Smith     if (PetscExceptionValue(ierr)) {
95419717074SBarry Smith       PetscErrorCode pierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(pierr);
95519717074SBarry Smith     }
956d5e45103SBarry Smith     CHKERRQ(ierr);
9571096aae1SMatthew Knepley   } else if (snes->afine) {
9581096aae1SMatthew Knepley     ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr);
9591096aae1SMatthew Knepley   } else {
9601096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() before SNESComputeFunction(), likely called from SNESSolve().");
9611096aae1SMatthew Knepley   }
9623ab0aad5SBarry Smith   if (snes->afine) {
963016dedfbSBarry Smith     ierr = VecAXPY(y,-1.0,snes->afine);CHKERRQ(ierr);
9643ab0aad5SBarry Smith   }
965ae3c334cSLois Curfman McInnes   snes->nfuncs++;
966d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
9673a40ed3dSBarry Smith   PetscFunctionReturn(0);
9689b94acceSBarry Smith }
9699b94acceSBarry Smith 
9704a2ae208SSatish Balay #undef __FUNCT__
9714a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian"
97262fef451SLois Curfman McInnes /*@
97362fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
97462fef451SLois Curfman McInnes    set with SNESSetJacobian().
97562fef451SLois Curfman McInnes 
976c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
977c7afd0dbSLois Curfman McInnes 
97862fef451SLois Curfman McInnes    Input Parameters:
979c7afd0dbSLois Curfman McInnes +  snes - the SNES context
980c7afd0dbSLois Curfman McInnes -  x - input vector
98162fef451SLois Curfman McInnes 
98262fef451SLois Curfman McInnes    Output Parameters:
983c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
98462fef451SLois Curfman McInnes .  B - optional preconditioning matrix
9852b668275SBarry Smith -  flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER)
986fee21e36SBarry Smith 
98762fef451SLois Curfman McInnes    Notes:
98862fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
98962fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
99062fef451SLois Curfman McInnes 
99194b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
992dc5a77f8SLois Curfman McInnes    flag parameter.
99362fef451SLois Curfman McInnes 
99436851e7fSLois Curfman McInnes    Level: developer
99536851e7fSLois Curfman McInnes 
99662fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
99762fef451SLois Curfman McInnes 
9982b668275SBarry Smith .seealso:  SNESSetJacobian(), KSPSetOperators(), MatStructure
99962fef451SLois Curfman McInnes @*/
100063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
10019b94acceSBarry Smith {
1002dfbe8321SBarry Smith   PetscErrorCode ierr;
10033a40ed3dSBarry Smith 
10043a40ed3dSBarry Smith   PetscFunctionBegin;
10054482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
10064482741eSBarry Smith   PetscValidHeaderSpecific(X,VEC_COOKIE,2);
10074482741eSBarry Smith   PetscValidPointer(flg,5);
1008c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,X,2);
1009e7788613SBarry Smith   if (!snes->ops->computejacobian) PetscFunctionReturn(0);
1010d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1011c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1012d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
1013dc67937bSBarry Smith   CHKMEMQ;
1014e7788613SBarry Smith   ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
1015dc67937bSBarry Smith   CHKMEMQ;
1016d64ed03dSBarry Smith   PetscStackPop;
1017d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
10186d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
10196ce558aeSBarry Smith   /* PetscValidHeaderSpecific(*A,MAT_COOKIE,3);
10206ce558aeSBarry Smith     PetscValidHeaderSpecific(*B,MAT_COOKIE,4);   */
10213a40ed3dSBarry Smith   PetscFunctionReturn(0);
10229b94acceSBarry Smith }
10239b94acceSBarry Smith 
10244a2ae208SSatish Balay #undef __FUNCT__
10254a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian"
10269b94acceSBarry Smith /*@C
10279b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1028044dda88SLois Curfman McInnes    location to store the matrix.
10299b94acceSBarry Smith 
1030c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1031c7afd0dbSLois Curfman McInnes 
10329b94acceSBarry Smith    Input Parameters:
1033c7afd0dbSLois Curfman McInnes +  snes - the SNES context
10349b94acceSBarry Smith .  A - Jacobian matrix
10359b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
10369b94acceSBarry Smith .  func - Jacobian evaluation routine
1037c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
10382cd2dfdcSLois Curfman McInnes          Jacobian evaluation routine (may be PETSC_NULL)
10399b94acceSBarry Smith 
10409b94acceSBarry Smith    Calling sequence of func:
10418d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
10429b94acceSBarry Smith 
1043c7afd0dbSLois Curfman McInnes +  x - input vector
10449b94acceSBarry Smith .  A - Jacobian matrix
10459b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1046ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
10472b668275SBarry Smith    structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER
1048c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
10499b94acceSBarry Smith 
10509b94acceSBarry Smith    Notes:
105194b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
10522cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1053ac21db08SLois Curfman McInnes 
1054ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
10559b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
10569b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
10579b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
10589b94acceSBarry Smith    throughout the global iterations.
10599b94acceSBarry Smith 
106036851e7fSLois Curfman McInnes    Level: beginner
106136851e7fSLois Curfman McInnes 
10629b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
10639b94acceSBarry Smith 
10642b668275SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatSNESMFComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure
10659b94acceSBarry Smith @*/
106663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
10679b94acceSBarry Smith {
1068dfbe8321SBarry Smith   PetscErrorCode ierr;
10693a7fca6bSBarry Smith 
10703a40ed3dSBarry Smith   PetscFunctionBegin;
10714482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
10724482741eSBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_COOKIE,2);
10734482741eSBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_COOKIE,3);
1074c9780b6fSBarry Smith   if (A) PetscCheckSameComm(snes,1,A,2);
1075c9780b6fSBarry Smith   if (B) PetscCheckSameComm(snes,1,B,2);
1076e7788613SBarry Smith    if (func) snes->ops->computejacobian = func;
10773a7fca6bSBarry Smith    if (ctx)  snes->jacP                 = ctx;
10783a7fca6bSBarry Smith   if (A) {
10797dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
10803a7fca6bSBarry Smith     if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
10819b94acceSBarry Smith     snes->jacobian = A;
10823a7fca6bSBarry Smith   }
10833a7fca6bSBarry Smith   if (B) {
10847dcf0eaaSdalcinl     ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
10853a7fca6bSBarry Smith     if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
10869b94acceSBarry Smith     snes->jacobian_pre = B;
10873a7fca6bSBarry Smith   }
10883a40ed3dSBarry Smith   PetscFunctionReturn(0);
10899b94acceSBarry Smith }
109062fef451SLois Curfman McInnes 
10914a2ae208SSatish Balay #undef __FUNCT__
10924a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian"
1093c2aafc4cSSatish Balay /*@C
1094b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1095b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1096b4fd4287SBarry Smith 
1097c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1098c7afd0dbSLois Curfman McInnes 
1099b4fd4287SBarry Smith    Input Parameter:
1100b4fd4287SBarry Smith .  snes - the nonlinear solver context
1101b4fd4287SBarry Smith 
1102b4fd4287SBarry Smith    Output Parameters:
1103c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1104b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
110570e92668SMatthew Knepley .  func - location to put Jacobian function (or PETSC_NULL)
110670e92668SMatthew Knepley -  ctx - location to stash Jacobian ctx (or PETSC_NULL)
1107fee21e36SBarry Smith 
110836851e7fSLois Curfman McInnes    Level: advanced
110936851e7fSLois Curfman McInnes 
1110b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1111b4fd4287SBarry Smith @*/
111270e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx)
1113b4fd4287SBarry Smith {
11143a40ed3dSBarry Smith   PetscFunctionBegin;
11154482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1116b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
1117b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
1118e7788613SBarry Smith   if (func) *func = snes->ops->computejacobian;
111970e92668SMatthew Knepley   if (ctx)  *ctx  = snes->jacP;
11203a40ed3dSBarry Smith   PetscFunctionReturn(0);
1121b4fd4287SBarry Smith }
1122b4fd4287SBarry Smith 
11239b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
112463dd3a1aSKris Buschelman EXTERN PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*);
11259b94acceSBarry Smith 
11264a2ae208SSatish Balay #undef __FUNCT__
11274a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp"
11289b94acceSBarry Smith /*@
11299b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1130272ac6f2SLois Curfman McInnes    of a nonlinear solver.
11319b94acceSBarry Smith 
1132fee21e36SBarry Smith    Collective on SNES
1133fee21e36SBarry Smith 
1134c7afd0dbSLois Curfman McInnes    Input Parameters:
113570e92668SMatthew Knepley .  snes - the SNES context
1136c7afd0dbSLois Curfman McInnes 
1137272ac6f2SLois Curfman McInnes    Notes:
1138272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1139272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1140272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1141272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1142272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1143272ac6f2SLois Curfman McInnes 
114436851e7fSLois Curfman McInnes    Level: advanced
114536851e7fSLois Curfman McInnes 
11469b94acceSBarry Smith .keywords: SNES, nonlinear, setup
11479b94acceSBarry Smith 
11489b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
11499b94acceSBarry Smith @*/
115070e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUp(SNES snes)
11519b94acceSBarry Smith {
1152dfbe8321SBarry Smith   PetscErrorCode ierr;
11534b27c08aSLois Curfman McInnes   PetscTruth     flg, iseqtr;
11543a40ed3dSBarry Smith 
11553a40ed3dSBarry Smith   PetscFunctionBegin;
11564482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
11574dc4c822SBarry Smith   if (snes->setupcalled) PetscFunctionReturn(0);
11589b94acceSBarry Smith 
1159b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_mf_operator",&flg);CHKERRQ(ierr);
1160c1f60f51SBarry Smith   /*
1161c1f60f51SBarry Smith       This version replaces the user provided Jacobian matrix with a
1162dfa02198SLois Curfman McInnes       matrix-free version but still employs the user-provided preconditioner matrix
1163c1f60f51SBarry Smith   */
1164c1f60f51SBarry Smith   if (flg) {
1165c1f60f51SBarry Smith     Mat J;
11665a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
11675a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
1168ae15b995SBarry Smith     ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr);
11693a7fca6bSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
11703a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
1171c1f60f51SBarry Smith   }
117245fc7adcSBarry Smith 
117303c60df9SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_MAT_SINGLE) && !defined(PETSC_USE_LONG_DOUBLE) && !defined(PETSC_USE_INT)
117445fc7adcSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_mf_operator2",&flg);CHKERRQ(ierr);
117545fc7adcSBarry Smith   if (flg) {
117645fc7adcSBarry Smith     Mat J;
117745fc7adcSBarry Smith     ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_sol,&J);CHKERRQ(ierr);
117845fc7adcSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
117945fc7adcSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
118045fc7adcSBarry Smith   }
118132a4b47aSMatthew Knepley #endif
118245fc7adcSBarry Smith 
1183b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_mf",&flg);CHKERRQ(ierr);
1184c1f60f51SBarry Smith   /*
1185dfa02198SLois Curfman McInnes       This version replaces both the user-provided Jacobian and the user-
1186c1f60f51SBarry Smith       provided preconditioner matrix with the default matrix free version.
1187c1f60f51SBarry Smith    */
118831615d04SLois Curfman McInnes   if (flg) {
1189272ac6f2SLois Curfman McInnes     Mat  J;
1190b5d62d44SBarry Smith     KSP ksp;
119194b7f48cSBarry Smith     PC   pc;
1192f3ef73ceSBarry Smith 
11935a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
11943a7fca6bSBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
1195ae15b995SBarry Smith     ierr = PetscInfo(snes,"Setting default matrix-free operator and preconditioner routines;\nThat is no preconditioner is being used.\n");CHKERRQ(ierr);
11963a7fca6bSBarry Smith     ierr = SNESSetJacobian(snes,J,J,MatSNESMFComputeJacobian,snes->funP);CHKERRQ(ierr);
11973a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
11983a7fca6bSBarry Smith 
1199f3ef73ceSBarry Smith     /* force no preconditioner */
120094b7f48cSBarry Smith     ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
1201b5d62d44SBarry Smith     ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
1202a9815358SBarry Smith     ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&flg);CHKERRQ(ierr);
1203a9815358SBarry Smith     if (!flg) {
1204f3ef73ceSBarry Smith       ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
1205272ac6f2SLois Curfman McInnes     }
1206a9815358SBarry Smith   }
1207f3ef73ceSBarry Smith 
12081096aae1SMatthew Knepley   if (!snes->vec_func && !snes->afine) {
12091096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
12101096aae1SMatthew Knepley   }
1211e7788613SBarry Smith   if (!snes->ops->computefunction && !snes->afine) {
12121096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
12131096aae1SMatthew Knepley   }
121429bbc08cSBarry Smith   if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetJacobian() first \n or use -snes_mf option");
1215a8c6a408SBarry Smith   if (snes->vec_func == snes->vec_sol) {
121629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
1217a8c6a408SBarry Smith   }
1218a703fe33SLois Curfman McInnes 
1219a703fe33SLois Curfman McInnes   /* Set the KSP stopping criterion to use the Eisenstat-Walker method */
12204b27c08aSLois Curfman McInnes   ierr = PetscTypeCompare((PetscObject)snes,SNESTR,&iseqtr);CHKERRQ(ierr);
12216831982aSBarry Smith   if (snes->ksp_ewconv && !iseqtr) {
122294b7f48cSBarry Smith     KSP ksp;
122394b7f48cSBarry Smith     ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
1224186905e3SBarry Smith     ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private,snes);CHKERRQ(ierr);
1225a703fe33SLois Curfman McInnes   }
12264b27c08aSLois Curfman McInnes 
1227410397dcSLisandro Dalcin   if (!snes->type_name) {
1228410397dcSLisandro Dalcin     ierr = SNESSetType(snes,SNESLS);CHKERRQ(ierr);
1229410397dcSLisandro Dalcin   }
1230410397dcSLisandro Dalcin   if (snes->ops->setup) {
1231410397dcSLisandro Dalcin     ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr);
1232410397dcSLisandro Dalcin   }
12337aaed0d8SBarry Smith   snes->setupcalled = PETSC_TRUE;
12343a40ed3dSBarry Smith   PetscFunctionReturn(0);
12359b94acceSBarry Smith }
12369b94acceSBarry Smith 
12374a2ae208SSatish Balay #undef __FUNCT__
12384a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
123952baeb72SSatish Balay /*@
12409b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
12419b94acceSBarry Smith    with SNESCreate().
12429b94acceSBarry Smith 
1243c7afd0dbSLois Curfman McInnes    Collective on SNES
1244c7afd0dbSLois Curfman McInnes 
12459b94acceSBarry Smith    Input Parameter:
12469b94acceSBarry Smith .  snes - the SNES context
12479b94acceSBarry Smith 
124836851e7fSLois Curfman McInnes    Level: beginner
124936851e7fSLois Curfman McInnes 
12509b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
12519b94acceSBarry Smith 
125263a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
12539b94acceSBarry Smith @*/
125463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDestroy(SNES snes)
12559b94acceSBarry Smith {
12566849ba73SBarry Smith   PetscErrorCode ierr;
12573a40ed3dSBarry Smith 
12583a40ed3dSBarry Smith   PetscFunctionBegin;
12594482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
12603a40ed3dSBarry Smith   if (--snes->refct > 0) PetscFunctionReturn(0);
1261d4bb536fSBarry Smith 
1262be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
12630f5bd95cSBarry Smith   ierr = PetscObjectDepublish(snes);CHKERRQ(ierr);
1264be0abb6dSBarry Smith 
1265e7788613SBarry Smith   if (snes->ops->destroy) {ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr);}
126605b42c5fSBarry Smith   ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);
12673a7fca6bSBarry Smith   if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
12683a7fca6bSBarry Smith   if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
12693ab0aad5SBarry Smith   if (snes->afine) {ierr = VecDestroy(snes->afine);CHKERRQ(ierr);}
127094b7f48cSBarry Smith   ierr = KSPDestroy(snes->ksp);CHKERRQ(ierr);
1271522c5e43SBarry Smith   if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);}
1272a6570f20SBarry Smith   ierr = SNESMonitorCancel(snes);CHKERRQ(ierr);
1273a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr);
12743a40ed3dSBarry Smith   PetscFunctionReturn(0);
12759b94acceSBarry Smith }
12769b94acceSBarry Smith 
12779b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
12789b94acceSBarry Smith 
12794a2ae208SSatish Balay #undef __FUNCT__
12804a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
12819b94acceSBarry Smith /*@
1282d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
12839b94acceSBarry Smith 
1284c7afd0dbSLois Curfman McInnes    Collective on SNES
1285c7afd0dbSLois Curfman McInnes 
12869b94acceSBarry Smith    Input Parameters:
1287c7afd0dbSLois Curfman McInnes +  snes - the SNES context
128870441072SBarry Smith .  abstol - absolute convergence tolerance
128933174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
129033174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
129133174efeSLois Curfman McInnes            of the change in the solution between steps
129233174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1293c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1294fee21e36SBarry Smith 
129533174efeSLois Curfman McInnes    Options Database Keys:
129670441072SBarry Smith +    -snes_atol <abstol> - Sets abstol
1297c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1298c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1299c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1300c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
13019b94acceSBarry Smith 
1302d7a720efSLois Curfman McInnes    Notes:
13039b94acceSBarry Smith    The default maximum number of iterations is 50.
13049b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
13059b94acceSBarry Smith 
130636851e7fSLois Curfman McInnes    Level: intermediate
130736851e7fSLois Curfman McInnes 
130833174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
13099b94acceSBarry Smith 
13102492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance()
13119b94acceSBarry Smith @*/
131263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf)
13139b94acceSBarry Smith {
13143a40ed3dSBarry Smith   PetscFunctionBegin;
13154482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
131670441072SBarry Smith   if (abstol != PETSC_DEFAULT)  snes->abstol      = abstol;
1317d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1318d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1319d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1320d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
13213a40ed3dSBarry Smith   PetscFunctionReturn(0);
13229b94acceSBarry Smith }
13239b94acceSBarry Smith 
13244a2ae208SSatish Balay #undef __FUNCT__
13254a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
13269b94acceSBarry Smith /*@
132733174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
132833174efeSLois Curfman McInnes 
1329c7afd0dbSLois Curfman McInnes    Not Collective
1330c7afd0dbSLois Curfman McInnes 
133133174efeSLois Curfman McInnes    Input Parameters:
1332c7afd0dbSLois Curfman McInnes +  snes - the SNES context
133370441072SBarry Smith .  abstol - absolute convergence tolerance
133433174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
133533174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
133633174efeSLois Curfman McInnes            of the change in the solution between steps
133733174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1338c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1339fee21e36SBarry Smith 
134033174efeSLois Curfman McInnes    Notes:
134133174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
134233174efeSLois Curfman McInnes 
134336851e7fSLois Curfman McInnes    Level: intermediate
134436851e7fSLois Curfman McInnes 
134533174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
134633174efeSLois Curfman McInnes 
134733174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
134833174efeSLois Curfman McInnes @*/
134963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetTolerances(SNES snes,PetscReal *abstol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf)
135033174efeSLois Curfman McInnes {
13513a40ed3dSBarry Smith   PetscFunctionBegin;
13524482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
135370441072SBarry Smith   if (abstol)  *abstol  = snes->abstol;
135433174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
135533174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
135633174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
135733174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
13583a40ed3dSBarry Smith   PetscFunctionReturn(0);
135933174efeSLois Curfman McInnes }
136033174efeSLois Curfman McInnes 
13614a2ae208SSatish Balay #undef __FUNCT__
13624a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
136333174efeSLois Curfman McInnes /*@
13649b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
13659b94acceSBarry Smith 
1366fee21e36SBarry Smith    Collective on SNES
1367fee21e36SBarry Smith 
1368c7afd0dbSLois Curfman McInnes    Input Parameters:
1369c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1370c7afd0dbSLois Curfman McInnes -  tol - tolerance
1371c7afd0dbSLois Curfman McInnes 
13729b94acceSBarry Smith    Options Database Key:
1373c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
13749b94acceSBarry Smith 
137536851e7fSLois Curfman McInnes    Level: intermediate
137636851e7fSLois Curfman McInnes 
13779b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
13789b94acceSBarry Smith 
13792492ecdbSBarry Smith .seealso: SNESSetTolerances()
13809b94acceSBarry Smith @*/
138163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
13829b94acceSBarry Smith {
13833a40ed3dSBarry Smith   PetscFunctionBegin;
13844482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
13859b94acceSBarry Smith   snes->deltatol = tol;
13863a40ed3dSBarry Smith   PetscFunctionReturn(0);
13879b94acceSBarry Smith }
13889b94acceSBarry Smith 
1389df9fa365SBarry Smith /*
1390df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1391df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1392df9fa365SBarry Smith    macros instead of functions
1393df9fa365SBarry Smith */
13944a2ae208SSatish Balay #undef __FUNCT__
1395a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLG"
1396a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLG(SNES snes,PetscInt it,PetscReal norm,void *ctx)
1397ce1608b8SBarry Smith {
1398dfbe8321SBarry Smith   PetscErrorCode ierr;
1399ce1608b8SBarry Smith 
1400ce1608b8SBarry Smith   PetscFunctionBegin;
14014482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1402a6570f20SBarry Smith   ierr = KSPMonitorLG((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1403ce1608b8SBarry Smith   PetscFunctionReturn(0);
1404ce1608b8SBarry Smith }
1405ce1608b8SBarry Smith 
14064a2ae208SSatish Balay #undef __FUNCT__
1407a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGCreate"
1408a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1409df9fa365SBarry Smith {
1410dfbe8321SBarry Smith   PetscErrorCode ierr;
1411df9fa365SBarry Smith 
1412df9fa365SBarry Smith   PetscFunctionBegin;
1413a6570f20SBarry Smith   ierr = KSPMonitorLGCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1414df9fa365SBarry Smith   PetscFunctionReturn(0);
1415df9fa365SBarry Smith }
1416df9fa365SBarry Smith 
14174a2ae208SSatish Balay #undef __FUNCT__
1418a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorLGDestroy"
1419a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorLGDestroy(PetscDrawLG draw)
1420df9fa365SBarry Smith {
1421dfbe8321SBarry Smith   PetscErrorCode ierr;
1422df9fa365SBarry Smith 
1423df9fa365SBarry Smith   PetscFunctionBegin;
1424a6570f20SBarry Smith   ierr = KSPMonitorLGDestroy(draw);CHKERRQ(ierr);
1425df9fa365SBarry Smith   PetscFunctionReturn(0);
1426df9fa365SBarry Smith }
1427df9fa365SBarry Smith 
14289b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
14299b94acceSBarry Smith 
14304a2ae208SSatish Balay #undef __FUNCT__
1431a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorSet"
14329b94acceSBarry Smith /*@C
1433a6570f20SBarry Smith    SNESMonitorSet - Sets an ADDITIONAL function that is to be used at every
14349b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
14359b94acceSBarry Smith    progress.
14369b94acceSBarry Smith 
1437fee21e36SBarry Smith    Collective on SNES
1438fee21e36SBarry Smith 
1439c7afd0dbSLois Curfman McInnes    Input Parameters:
1440c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1441c7afd0dbSLois Curfman McInnes .  func - monitoring routine
1442b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
1443e8105e01SRichard Katz           monitor routine (use PETSC_NULL if no context is desired)
1444b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1445b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
14469b94acceSBarry Smith 
1447c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1448a7cc72afSBarry Smith $     int func(SNES snes,PetscInt its, PetscReal norm,void *mctx)
1449c7afd0dbSLois Curfman McInnes 
1450c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1451c7afd0dbSLois Curfman McInnes .    its - iteration number
1452c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
145340a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
14549b94acceSBarry Smith 
14559665c990SLois Curfman McInnes    Options Database Keys:
1456a6570f20SBarry Smith +    -snes_monitor        - sets SNESMonitorDefault()
1457a6570f20SBarry Smith .    -snes_monitor_draw    - sets line graph monitor,
1458a6570f20SBarry Smith                             uses SNESMonitorLGCreate()
1459a6570f20SBarry Smith _    -snes_monitor_cancel - cancels all monitors that have
1460c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
1461a6570f20SBarry Smith                             calls to SNESMonitorSet(), but
1462c7afd0dbSLois Curfman McInnes                             does not cancel those set via
1463c7afd0dbSLois Curfman McInnes                             the options database.
14649665c990SLois Curfman McInnes 
1465639f9d9dSBarry Smith    Notes:
14666bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
1467a6570f20SBarry Smith    SNESMonitorSet() multiple times; all will be called in the
14686bc08f3fSLois Curfman McInnes    order in which they were set.
1469639f9d9dSBarry Smith 
147036851e7fSLois Curfman McInnes    Level: intermediate
147136851e7fSLois Curfman McInnes 
14729b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
14739b94acceSBarry Smith 
1474a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorCancel()
14759b94acceSBarry Smith @*/
1476a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorSet(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void*))
14779b94acceSBarry Smith {
14783a40ed3dSBarry Smith   PetscFunctionBegin;
14794482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1480639f9d9dSBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) {
148129bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1482639f9d9dSBarry Smith   }
1483639f9d9dSBarry Smith   snes->monitor[snes->numbermonitors]           = func;
1484b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
1485639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
14863a40ed3dSBarry Smith   PetscFunctionReturn(0);
14879b94acceSBarry Smith }
14889b94acceSBarry Smith 
14894a2ae208SSatish Balay #undef __FUNCT__
1490a6570f20SBarry Smith #define __FUNCT__ "SNESMonitorCancel"
14915cd90555SBarry Smith /*@C
1492a6570f20SBarry Smith    SNESMonitorCancel - Clears all the monitor functions for a SNES object.
14935cd90555SBarry Smith 
1494c7afd0dbSLois Curfman McInnes    Collective on SNES
1495c7afd0dbSLois Curfman McInnes 
14965cd90555SBarry Smith    Input Parameters:
14975cd90555SBarry Smith .  snes - the SNES context
14985cd90555SBarry Smith 
14991a480d89SAdministrator    Options Database Key:
1500a6570f20SBarry Smith .  -snes_monitor_cancel - cancels all monitors that have been hardwired
1501a6570f20SBarry Smith     into a code by calls to SNESMonitorSet(), but does not cancel those
1502c7afd0dbSLois Curfman McInnes     set via the options database
15035cd90555SBarry Smith 
15045cd90555SBarry Smith    Notes:
15055cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
15065cd90555SBarry Smith 
150736851e7fSLois Curfman McInnes    Level: intermediate
150836851e7fSLois Curfman McInnes 
15095cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
15105cd90555SBarry Smith 
1511a6570f20SBarry Smith .seealso: SNESMonitorDefault(), SNESMonitorSet()
15125cd90555SBarry Smith @*/
1513a6570f20SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESMonitorCancel(SNES snes)
15145cd90555SBarry Smith {
1515d952e501SBarry Smith   PetscErrorCode ierr;
1516d952e501SBarry Smith   PetscInt       i;
1517d952e501SBarry Smith 
15185cd90555SBarry Smith   PetscFunctionBegin;
15194482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1520d952e501SBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
1521d952e501SBarry Smith     if (snes->monitordestroy[i]) {
1522d952e501SBarry Smith       ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr);
1523d952e501SBarry Smith     }
1524d952e501SBarry Smith   }
15255cd90555SBarry Smith   snes->numbermonitors = 0;
15265cd90555SBarry Smith   PetscFunctionReturn(0);
15275cd90555SBarry Smith }
15285cd90555SBarry Smith 
15294a2ae208SSatish Balay #undef __FUNCT__
15304a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
15319b94acceSBarry Smith /*@C
15329b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
15339b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
15349b94acceSBarry Smith 
1535fee21e36SBarry Smith    Collective on SNES
1536fee21e36SBarry Smith 
1537c7afd0dbSLois Curfman McInnes    Input Parameters:
1538c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1539c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
1540c7afd0dbSLois Curfman McInnes -  cctx - [optional] context for private data for the convergence routine
1541c7afd0dbSLois Curfman McInnes           (may be PETSC_NULL)
15429b94acceSBarry Smith 
1543c7afd0dbSLois Curfman McInnes    Calling sequence of func:
154406ee9f85SBarry Smith $     PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
1545c7afd0dbSLois Curfman McInnes 
1546c7afd0dbSLois Curfman McInnes +    snes - the SNES context
154706ee9f85SBarry Smith .    it - current iteration (0 is the first and is before any Newton step)
1548c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
1549184914b5SBarry Smith .    reason - reason for convergence/divergence
1550c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
15514b27c08aSLois Curfman McInnes .    gnorm - 2-norm of current step
15524b27c08aSLois Curfman McInnes -    f - 2-norm of function
15539b94acceSBarry Smith 
155436851e7fSLois Curfman McInnes    Level: advanced
155536851e7fSLois Curfman McInnes 
15569b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
15579b94acceSBarry Smith 
15584b27c08aSLois Curfman McInnes .seealso: SNESConverged_LS(), SNESConverged_TR()
15599b94acceSBarry Smith @*/
156006ee9f85SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx)
15619b94acceSBarry Smith {
15623a40ed3dSBarry Smith   PetscFunctionBegin;
15634482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1564e7788613SBarry Smith   (snes)->ops->converged = func;
15659b94acceSBarry Smith   (snes)->cnvP           = cctx;
15663a40ed3dSBarry Smith   PetscFunctionReturn(0);
15679b94acceSBarry Smith }
15689b94acceSBarry Smith 
15694a2ae208SSatish Balay #undef __FUNCT__
15704a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
157152baeb72SSatish Balay /*@
1572184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
1573184914b5SBarry Smith 
1574184914b5SBarry Smith    Not Collective
1575184914b5SBarry Smith 
1576184914b5SBarry Smith    Input Parameter:
1577184914b5SBarry Smith .  snes - the SNES context
1578184914b5SBarry Smith 
1579184914b5SBarry Smith    Output Parameter:
1580e090d566SSatish Balay .  reason - negative value indicates diverged, positive value converged, see petscsnes.h or the
1581184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
1582184914b5SBarry Smith 
1583184914b5SBarry Smith    Level: intermediate
1584184914b5SBarry Smith 
1585184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
1586184914b5SBarry Smith 
1587184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
1588184914b5SBarry Smith 
15894b27c08aSLois Curfman McInnes .seealso: SNESSetConvergenceTest(), SNESConverged_LS(), SNESConverged_TR(), SNESConvergedReason
1590184914b5SBarry Smith @*/
159163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
1592184914b5SBarry Smith {
1593184914b5SBarry Smith   PetscFunctionBegin;
15944482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
15954482741eSBarry Smith   PetscValidPointer(reason,2);
1596184914b5SBarry Smith   *reason = snes->reason;
1597184914b5SBarry Smith   PetscFunctionReturn(0);
1598184914b5SBarry Smith }
1599184914b5SBarry Smith 
16004a2ae208SSatish Balay #undef __FUNCT__
16014a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
1602c9005455SLois Curfman McInnes /*@
1603c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
1604c9005455SLois Curfman McInnes 
1605fee21e36SBarry Smith    Collective on SNES
1606fee21e36SBarry Smith 
1607c7afd0dbSLois Curfman McInnes    Input Parameters:
1608c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
1609c7afd0dbSLois Curfman McInnes .  a   - array to hold history
1610cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
1611758f92a0SBarry Smith .  na  - size of a and its
161264731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
1613758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
1614c7afd0dbSLois Curfman McInnes 
1615c9005455SLois Curfman McInnes    Notes:
16164b27c08aSLois Curfman McInnes    If set, this array will contain the function norms computed
1617c9005455SLois Curfman McInnes    at each step.
1618c9005455SLois Curfman McInnes 
1619c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
1620c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
1621c9005455SLois Curfman McInnes    during the section of code that is being timed.
1622c9005455SLois Curfman McInnes 
162336851e7fSLois Curfman McInnes    Level: intermediate
162436851e7fSLois Curfman McInnes 
1625c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
1626758f92a0SBarry Smith 
162708405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
1628758f92a0SBarry Smith 
1629c9005455SLois Curfman McInnes @*/
1630*a562a398SLisandro Dalcin PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt its[],PetscInt na,PetscTruth reset)
1631c9005455SLois Curfman McInnes {
16323a40ed3dSBarry Smith   PetscFunctionBegin;
16334482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
16344482741eSBarry Smith   if (na)  PetscValidScalarPointer(a,2);
1635*a562a398SLisandro Dalcin   if (its) PetscValidIntPointer(its,3);
1636c9005455SLois Curfman McInnes   snes->conv_hist       = a;
1637758f92a0SBarry Smith   snes->conv_hist_its   = its;
1638758f92a0SBarry Smith   snes->conv_hist_max   = na;
1639758f92a0SBarry Smith   snes->conv_hist_reset = reset;
1640758f92a0SBarry Smith   PetscFunctionReturn(0);
1641758f92a0SBarry Smith }
1642758f92a0SBarry Smith 
16434a2ae208SSatish Balay #undef __FUNCT__
16444a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
16450c4c9dddSBarry Smith /*@C
1646758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
1647758f92a0SBarry Smith 
1648758f92a0SBarry Smith    Collective on SNES
1649758f92a0SBarry Smith 
1650758f92a0SBarry Smith    Input Parameter:
1651758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
1652758f92a0SBarry Smith 
1653758f92a0SBarry Smith    Output Parameters:
1654758f92a0SBarry Smith .  a   - array to hold history
1655758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
1656758f92a0SBarry Smith          negative if not converged) for each solve.
1657758f92a0SBarry Smith -  na  - size of a and its
1658758f92a0SBarry Smith 
1659758f92a0SBarry Smith    Notes:
1660758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
1661758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
1662758f92a0SBarry Smith 
1663758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
1664758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
1665758f92a0SBarry Smith    during the section of code that is being timed.
1666758f92a0SBarry Smith 
1667758f92a0SBarry Smith    Level: intermediate
1668758f92a0SBarry Smith 
1669758f92a0SBarry Smith .keywords: SNES, get, convergence, history
1670758f92a0SBarry Smith 
1671758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
1672758f92a0SBarry Smith 
1673758f92a0SBarry Smith @*/
167463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na)
1675758f92a0SBarry Smith {
1676758f92a0SBarry Smith   PetscFunctionBegin;
16774482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1678758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
1679758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
1680758f92a0SBarry Smith   if (na)  *na  = snes->conv_hist_len;
16813a40ed3dSBarry Smith   PetscFunctionReturn(0);
1682c9005455SLois Curfman McInnes }
1683c9005455SLois Curfman McInnes 
1684e74ef692SMatthew Knepley #undef __FUNCT__
1685e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
1686ac226902SBarry Smith /*@C
168776b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
16887e4bb74cSBarry Smith   at the beginning o every iteration of the nonlinear solve. Specifically
16897e4bb74cSBarry Smith   it is called just before the Jacobian is "evaluated".
169076b2cf59SMatthew Knepley 
169176b2cf59SMatthew Knepley   Collective on SNES
169276b2cf59SMatthew Knepley 
169376b2cf59SMatthew Knepley   Input Parameters:
169476b2cf59SMatthew Knepley . snes - The nonlinear solver context
169576b2cf59SMatthew Knepley . func - The function
169676b2cf59SMatthew Knepley 
169776b2cf59SMatthew Knepley   Calling sequence of func:
1698b5d30489SBarry Smith . func (SNES snes, PetscInt step);
169976b2cf59SMatthew Knepley 
170076b2cf59SMatthew Knepley . step - The current step of the iteration
170176b2cf59SMatthew Knepley 
170276b2cf59SMatthew Knepley   Level: intermediate
170376b2cf59SMatthew Knepley 
170476b2cf59SMatthew Knepley .keywords: SNES, update
1705b5d30489SBarry Smith 
170676b2cf59SMatthew Knepley .seealso SNESDefaultUpdate(), SNESSetRhsBC(), SNESSetSolutionBC()
170776b2cf59SMatthew Knepley @*/
170863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt))
170976b2cf59SMatthew Knepley {
171076b2cf59SMatthew Knepley   PetscFunctionBegin;
17114482741eSBarry Smith   PetscValidHeaderSpecific(snes, SNES_COOKIE,1);
1712e7788613SBarry Smith   snes->ops->update = func;
171376b2cf59SMatthew Knepley   PetscFunctionReturn(0);
171476b2cf59SMatthew Knepley }
171576b2cf59SMatthew Knepley 
1716e74ef692SMatthew Knepley #undef __FUNCT__
1717e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
171876b2cf59SMatthew Knepley /*@
171976b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
172076b2cf59SMatthew Knepley 
172176b2cf59SMatthew Knepley   Not collective
172276b2cf59SMatthew Knepley 
172376b2cf59SMatthew Knepley   Input Parameters:
172476b2cf59SMatthew Knepley . snes - The nonlinear solver context
172576b2cf59SMatthew Knepley . step - The current step of the iteration
172676b2cf59SMatthew Knepley 
1727205452f4SMatthew Knepley   Level: intermediate
1728205452f4SMatthew Knepley 
172976b2cf59SMatthew Knepley .keywords: SNES, update
1730a6570f20SBarry Smith .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultShortolutionBC()
173176b2cf59SMatthew Knepley @*/
173263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultUpdate(SNES snes, PetscInt step)
173376b2cf59SMatthew Knepley {
173476b2cf59SMatthew Knepley   PetscFunctionBegin;
173576b2cf59SMatthew Knepley   PetscFunctionReturn(0);
173676b2cf59SMatthew Knepley }
173776b2cf59SMatthew Knepley 
17384a2ae208SSatish Balay #undef __FUNCT__
17394a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
17409b94acceSBarry Smith /*
17419b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
17429b94acceSBarry Smith    positive parameter delta.
17439b94acceSBarry Smith 
17449b94acceSBarry Smith     Input Parameters:
1745c7afd0dbSLois Curfman McInnes +   snes - the SNES context
17469b94acceSBarry Smith .   y - approximate solution of linear system
17479b94acceSBarry Smith .   fnorm - 2-norm of current function
1748c7afd0dbSLois Curfman McInnes -   delta - trust region size
17499b94acceSBarry Smith 
17509b94acceSBarry Smith     Output Parameters:
1751c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
17529b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
17539b94acceSBarry Smith     region, and exceeds zero otherwise.
1754c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
17559b94acceSBarry Smith 
17569b94acceSBarry Smith     Note:
17574b27c08aSLois Curfman McInnes     For non-trust region methods such as SNESLS, the parameter delta
17589b94acceSBarry Smith     is set to be the maximum allowable step size.
17599b94acceSBarry Smith 
17609b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
17619b94acceSBarry Smith */
1762dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
17639b94acceSBarry Smith {
1764064f8208SBarry Smith   PetscReal      nrm;
1765ea709b57SSatish Balay   PetscScalar    cnorm;
1766dfbe8321SBarry Smith   PetscErrorCode ierr;
17673a40ed3dSBarry Smith 
17683a40ed3dSBarry Smith   PetscFunctionBegin;
17694482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
17704482741eSBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE,2);
1771c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,2);
1772184914b5SBarry Smith 
1773064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
1774064f8208SBarry Smith   if (nrm > *delta) {
1775064f8208SBarry Smith      nrm = *delta/nrm;
1776064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
1777064f8208SBarry Smith      cnorm = nrm;
17782dcb1b2aSMatthew Knepley      ierr = VecScale(y,cnorm);CHKERRQ(ierr);
17799b94acceSBarry Smith      *ynorm = *delta;
17809b94acceSBarry Smith   } else {
17819b94acceSBarry Smith      *gpnorm = 0.0;
1782064f8208SBarry Smith      *ynorm = nrm;
17839b94acceSBarry Smith   }
17843a40ed3dSBarry Smith   PetscFunctionReturn(0);
17859b94acceSBarry Smith }
17869b94acceSBarry Smith 
17874a2ae208SSatish Balay #undef __FUNCT__
17884a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
17896ce558aeSBarry Smith /*@C
1790f69a0ea3SMatthew Knepley    SNESSolve - Solves a nonlinear system F(x) = b.
1791f69a0ea3SMatthew Knepley    Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX().
17929b94acceSBarry Smith 
1793c7afd0dbSLois Curfman McInnes    Collective on SNES
1794c7afd0dbSLois Curfman McInnes 
1795b2002411SLois Curfman McInnes    Input Parameters:
1796c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1797f69a0ea3SMatthew Knepley .  b - the constant part of the equation, or PETSC_NULL to use zero.
179870e92668SMatthew Knepley -  x - the solution vector, or PETSC_NULL if it was set with SNESSetSolution()
17999b94acceSBarry Smith 
1800b2002411SLois Curfman McInnes    Notes:
18018ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
18028ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
18038ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
18048ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
18058ddd3da0SLois Curfman McInnes 
180636851e7fSLois Curfman McInnes    Level: beginner
180736851e7fSLois Curfman McInnes 
18089b94acceSBarry Smith .keywords: SNES, nonlinear, solve
18099b94acceSBarry Smith 
181070e92668SMatthew Knepley .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian(), SNESSetRhs(), SNESSetSolution()
18119b94acceSBarry Smith @*/
1812f69a0ea3SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSolve(SNES snes,Vec b,Vec x)
18139b94acceSBarry Smith {
1814dfbe8321SBarry Smith   PetscErrorCode ierr;
1815f1af5d2fSBarry Smith   PetscTruth     flg;
1816eabae89aSBarry Smith   char           filename[PETSC_MAX_PATH_LEN];
1817eabae89aSBarry Smith   PetscViewer    viewer;
1818052efed2SBarry Smith 
18193a40ed3dSBarry Smith   PetscFunctionBegin;
18204482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1821e7788613SBarry Smith   if (!snes->ops->solve) SETERRQ(PETSC_ERR_ORDER,"SNESSetType() or SNESSetFromOptions() must be called before SNESSolve()");
182274637425SBarry Smith 
1823f69a0ea3SMatthew Knepley   if (b) {
1824f69a0ea3SMatthew Knepley     ierr = SNESSetRhs(snes, b); CHKERRQ(ierr);
18251096aae1SMatthew Knepley     if (!snes->vec_func) {
18261096aae1SMatthew Knepley       Vec r;
18271096aae1SMatthew Knepley 
18281096aae1SMatthew Knepley       ierr = VecDuplicate(b, &r); CHKERRQ(ierr);
18291096aae1SMatthew Knepley       ierr = SNESSetFunction(snes, r, PETSC_NULL, PETSC_NULL); CHKERRQ(ierr);
18301096aae1SMatthew Knepley     }
1831f69a0ea3SMatthew Knepley   }
183270e92668SMatthew Knepley   if (x) {
1833f69a0ea3SMatthew Knepley     PetscValidHeaderSpecific(x,VEC_COOKIE,3);
1834f69a0ea3SMatthew Knepley     PetscCheckSameComm(snes,1,x,3);
183570e92668SMatthew Knepley   } else {
183670e92668SMatthew Knepley     ierr = SNESGetSolution(snes, &x); CHKERRQ(ierr);
183770e92668SMatthew Knepley     if (!x) {
183870e92668SMatthew Knepley       ierr = VecDuplicate(snes->vec_func_always, &x); CHKERRQ(ierr);
183970e92668SMatthew Knepley     }
184070e92668SMatthew Knepley   }
184170e92668SMatthew Knepley   snes->vec_sol = snes->vec_sol_always = x;
184270e92668SMatthew Knepley   if (!snes->setupcalled) {
184370e92668SMatthew Knepley     ierr = SNESSetUp(snes);CHKERRQ(ierr);
184470e92668SMatthew Knepley   }
1845abc0a331SBarry Smith   if (snes->conv_hist_reset) snes->conv_hist_len = 0;
1846d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
184750ffb88aSMatthew Knepley   snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
1848d5e45103SBarry Smith 
1849e7788613SBarry Smith   ierr = PetscExceptionTry1((*(snes)->ops->solve)(snes),PETSC_ERR_ARG_DOMAIN);
1850d5e45103SBarry Smith   if (PetscExceptionValue(ierr)) {
185138f152feSBarry Smith     /* this means that a caller above me has also tryed this exception so I don't handle it here, pass it up */
185219717074SBarry Smith     PetscErrorCode pierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(pierr);
1853c671dbe7SSatish Balay   } else if (PetscExceptionCaught(ierr,PETSC_ERR_ARG_DOMAIN)) {
1854d5e45103SBarry Smith     /* translate exception into SNES not converged reason */
1855d5e45103SBarry Smith     snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN;
185638f152feSBarry Smith     ierr = 0;
185738f152feSBarry Smith   }
185838f152feSBarry Smith   CHKERRQ(ierr);
1859d5e45103SBarry Smith 
1860d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
1861eabae89aSBarry Smith   ierr = PetscOptionsGetString(snes->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1862eabae89aSBarry Smith   if (flg && !PetscPreLoadingOn) {
1863eabae89aSBarry Smith     ierr = PetscViewerASCIIOpen(snes->comm,filename,&viewer);CHKERRQ(ierr);
1864eabae89aSBarry Smith     ierr = SNESView(snes,viewer);CHKERRQ(ierr);
1865eabae89aSBarry Smith     ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr);
1866eabae89aSBarry Smith   }
1867eabae89aSBarry Smith 
1868da9b6338SBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_test_local_min",&flg);CHKERRQ(ierr);
1869da9b6338SBarry Smith   if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); }
18705968eb51SBarry Smith   if (snes->printreason) {
18715968eb51SBarry Smith     if (snes->reason > 0) {
18729dcbbd2bSBarry Smith       ierr = PetscPrintf(snes->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
18735968eb51SBarry Smith     } else {
18749dcbbd2bSBarry Smith       ierr = PetscPrintf(snes->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
18755968eb51SBarry Smith     }
18765968eb51SBarry Smith   }
18775968eb51SBarry Smith 
18783a40ed3dSBarry Smith   PetscFunctionReturn(0);
18799b94acceSBarry Smith }
18809b94acceSBarry Smith 
18819b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
18829b94acceSBarry Smith 
18834a2ae208SSatish Balay #undef __FUNCT__
18844a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
188582bf6240SBarry Smith /*@C
18864b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
18879b94acceSBarry Smith 
1888fee21e36SBarry Smith    Collective on SNES
1889fee21e36SBarry Smith 
1890c7afd0dbSLois Curfman McInnes    Input Parameters:
1891c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1892454a90a3SBarry Smith -  type - a known method
1893c7afd0dbSLois Curfman McInnes 
1894c7afd0dbSLois Curfman McInnes    Options Database Key:
1895454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
1896c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
1897ae12b187SLois Curfman McInnes 
18989b94acceSBarry Smith    Notes:
1899e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
19004b27c08aSLois Curfman McInnes +    SNESLS - Newton's method with line search
1901c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
19024b27c08aSLois Curfman McInnes .    SNESTR - Newton's method with trust region
1903c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
19049b94acceSBarry Smith 
1905ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
1906ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
1907ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
1908ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
1909ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
1910ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
1911ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
1912ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
1913ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
1914b0a32e0cSBarry Smith   appropriate method.
191536851e7fSLois Curfman McInnes 
191636851e7fSLois Curfman McInnes   Level: intermediate
1917a703fe33SLois Curfman McInnes 
1918454a90a3SBarry Smith .keywords: SNES, set, type
1919435da068SBarry Smith 
1920435da068SBarry Smith .seealso: SNESType, SNESCreate()
1921435da068SBarry Smith 
19229b94acceSBarry Smith @*/
1923e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetType(SNES snes,SNESType type)
19249b94acceSBarry Smith {
1925dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(SNES);
19266831982aSBarry Smith   PetscTruth     match;
19273a40ed3dSBarry Smith 
19283a40ed3dSBarry Smith   PetscFunctionBegin;
19294482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
19304482741eSBarry Smith   PetscValidCharPointer(type,2);
193182bf6240SBarry Smith 
19326831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
19330f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
193482bf6240SBarry Smith 
193582bf6240SBarry Smith   if (snes->setupcalled) {
19364dc4c822SBarry Smith     snes->setupcalled = PETSC_FALSE;
1937e7788613SBarry Smith     ierr              = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr);
193882bf6240SBarry Smith     snes->data        = 0;
193982bf6240SBarry Smith   }
19407d1a2b2bSBarry Smith 
19419b94acceSBarry Smith   /* Get the function pointers for the iterative method requested */
194282bf6240SBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
1943b9617806SBarry Smith   ierr =  PetscFListFind(snes->comm,SNESList,type,(void (**)(void)) &r);CHKERRQ(ierr);
1944958c9bccSBarry Smith   if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type);
194505b42c5fSBarry Smith   ierr = PetscFree(snes->data);CHKERRQ(ierr);
194682bf6240SBarry Smith   snes->data = 0;
19473a40ed3dSBarry Smith   ierr = (*r)(snes);CHKERRQ(ierr);
1948454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
19493a40ed3dSBarry Smith   PetscFunctionReturn(0);
19509b94acceSBarry Smith }
19519b94acceSBarry Smith 
1952a847f771SSatish Balay 
19539b94acceSBarry Smith /* --------------------------------------------------------------------- */
19544a2ae208SSatish Balay #undef __FUNCT__
19554a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
195652baeb72SSatish Balay /*@
19579b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
1958f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
19599b94acceSBarry Smith 
1960fee21e36SBarry Smith    Not Collective
1961fee21e36SBarry Smith 
196236851e7fSLois Curfman McInnes    Level: advanced
196336851e7fSLois Curfman McInnes 
19649b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
19659b94acceSBarry Smith 
19669b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
19679b94acceSBarry Smith @*/
196863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegisterDestroy(void)
19699b94acceSBarry Smith {
1970dfbe8321SBarry Smith   PetscErrorCode ierr;
197182bf6240SBarry Smith 
19723a40ed3dSBarry Smith   PetscFunctionBegin;
197382bf6240SBarry Smith   if (SNESList) {
1974b0a32e0cSBarry Smith     ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
197582bf6240SBarry Smith     SNESList = 0;
19769b94acceSBarry Smith   }
19774c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
19783a40ed3dSBarry Smith   PetscFunctionReturn(0);
19799b94acceSBarry Smith }
19809b94acceSBarry Smith 
19814a2ae208SSatish Balay #undef __FUNCT__
19824a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
19839b94acceSBarry Smith /*@C
19849a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
19859b94acceSBarry Smith 
1986c7afd0dbSLois Curfman McInnes    Not Collective
1987c7afd0dbSLois Curfman McInnes 
19889b94acceSBarry Smith    Input Parameter:
19894b0e389bSBarry Smith .  snes - nonlinear solver context
19909b94acceSBarry Smith 
19919b94acceSBarry Smith    Output Parameter:
19923a7fca6bSBarry Smith .  type - SNES method (a character string)
19939b94acceSBarry Smith 
199436851e7fSLois Curfman McInnes    Level: intermediate
199536851e7fSLois Curfman McInnes 
1996454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
19979b94acceSBarry Smith @*/
199863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetType(SNES snes,SNESType *type)
19999b94acceSBarry Smith {
20003a40ed3dSBarry Smith   PetscFunctionBegin;
20014482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
20024482741eSBarry Smith   PetscValidPointer(type,2);
2003454a90a3SBarry Smith   *type = snes->type_name;
20043a40ed3dSBarry Smith   PetscFunctionReturn(0);
20059b94acceSBarry Smith }
20069b94acceSBarry Smith 
20074a2ae208SSatish Balay #undef __FUNCT__
20084a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
200952baeb72SSatish Balay /*@
20109b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
20119b94acceSBarry Smith    stored.
20129b94acceSBarry Smith 
2013c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2014c7afd0dbSLois Curfman McInnes 
20159b94acceSBarry Smith    Input Parameter:
20169b94acceSBarry Smith .  snes - the SNES context
20179b94acceSBarry Smith 
20189b94acceSBarry Smith    Output Parameter:
20199b94acceSBarry Smith .  x - the solution
20209b94acceSBarry Smith 
202170e92668SMatthew Knepley    Level: intermediate
202236851e7fSLois Curfman McInnes 
20239b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
20249b94acceSBarry Smith 
202570e92668SMatthew Knepley .seealso: SNESSetSolution(), SNESGetFunction(), SNESGetSolutionUpdate()
20269b94acceSBarry Smith @*/
202763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolution(SNES snes,Vec *x)
20289b94acceSBarry Smith {
20293a40ed3dSBarry Smith   PetscFunctionBegin;
20304482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
20314482741eSBarry Smith   PetscValidPointer(x,2);
20329b94acceSBarry Smith   *x = snes->vec_sol_always;
20333a40ed3dSBarry Smith   PetscFunctionReturn(0);
20349b94acceSBarry Smith }
20359b94acceSBarry Smith 
20364a2ae208SSatish Balay #undef __FUNCT__
203770e92668SMatthew Knepley #define __FUNCT__ "SNESSetSolution"
20387e4bb74cSBarry Smith /*@
203970e92668SMatthew Knepley    SNESSetSolution - Sets the vector where the approximate solution is stored.
204070e92668SMatthew Knepley 
204170e92668SMatthew Knepley    Not Collective, but Vec is parallel if SNES is parallel
204270e92668SMatthew Knepley 
204370e92668SMatthew Knepley    Input Parameters:
204470e92668SMatthew Knepley +  snes - the SNES context
204570e92668SMatthew Knepley -  x - the solution
204670e92668SMatthew Knepley 
204770e92668SMatthew Knepley    Output Parameter:
204870e92668SMatthew Knepley 
204970e92668SMatthew Knepley    Level: intermediate
205070e92668SMatthew Knepley 
205142219521SBarry Smith    Notes: this is not normally used, rather one simply calls SNESSolve() with
205242219521SBarry Smith           the appropriate solution vector.
205342219521SBarry Smith 
205470e92668SMatthew Knepley .keywords: SNES, nonlinear, set, solution
205570e92668SMatthew Knepley 
205670e92668SMatthew Knepley .seealso: SNESGetSolution(), SNESGetFunction(), SNESGetSolutionUpdate()
205770e92668SMatthew Knepley @*/
205870e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSetSolution(SNES snes,Vec x)
205970e92668SMatthew Knepley {
206070e92668SMatthew Knepley   PetscFunctionBegin;
206170e92668SMatthew Knepley   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
206270e92668SMatthew Knepley   PetscValidHeaderSpecific(x,VEC_COOKIE,2);
206370e92668SMatthew Knepley   PetscCheckSameComm(snes,1,x,2);
206470e92668SMatthew Knepley   snes->vec_sol_always = x;
206570e92668SMatthew Knepley   PetscFunctionReturn(0);
206670e92668SMatthew Knepley }
206770e92668SMatthew Knepley 
206870e92668SMatthew Knepley #undef __FUNCT__
20694a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
207052baeb72SSatish Balay /*@
20719b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
20729b94acceSBarry Smith    stored.
20739b94acceSBarry Smith 
2074c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2075c7afd0dbSLois Curfman McInnes 
20769b94acceSBarry Smith    Input Parameter:
20779b94acceSBarry Smith .  snes - the SNES context
20789b94acceSBarry Smith 
20799b94acceSBarry Smith    Output Parameter:
20809b94acceSBarry Smith .  x - the solution update
20819b94acceSBarry Smith 
208236851e7fSLois Curfman McInnes    Level: advanced
208336851e7fSLois Curfman McInnes 
20849b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
20859b94acceSBarry Smith 
20869b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction
20879b94acceSBarry Smith @*/
208863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolutionUpdate(SNES snes,Vec *x)
20899b94acceSBarry Smith {
20903a40ed3dSBarry Smith   PetscFunctionBegin;
20914482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
20924482741eSBarry Smith   PetscValidPointer(x,2);
20939b94acceSBarry Smith   *x = snes->vec_sol_update_always;
20943a40ed3dSBarry Smith   PetscFunctionReturn(0);
20959b94acceSBarry Smith }
20969b94acceSBarry Smith 
20974a2ae208SSatish Balay #undef __FUNCT__
20984a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
20999b94acceSBarry Smith /*@C
21003638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
21019b94acceSBarry Smith 
2102c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2103c7afd0dbSLois Curfman McInnes 
21049b94acceSBarry Smith    Input Parameter:
21059b94acceSBarry Smith .  snes - the SNES context
21069b94acceSBarry Smith 
21079b94acceSBarry Smith    Output Parameter:
21087bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
210970e92668SMatthew Knepley .  func - the function (or PETSC_NULL)
211070e92668SMatthew Knepley -  ctx - the function context (or PETSC_NULL)
21119b94acceSBarry Smith 
211236851e7fSLois Curfman McInnes    Level: advanced
211336851e7fSLois Curfman McInnes 
2114a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
21159b94acceSBarry Smith 
21164b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution()
21179b94acceSBarry Smith @*/
211870e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx)
21199b94acceSBarry Smith {
21203a40ed3dSBarry Smith   PetscFunctionBegin;
21214482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
21227bf4e008SBarry Smith   if (r)    *r    = snes->vec_func_always;
2123e7788613SBarry Smith   if (func) *func = snes->ops->computefunction;
212470e92668SMatthew Knepley   if (ctx)  *ctx  = snes->funP;
21253a40ed3dSBarry Smith   PetscFunctionReturn(0);
21269b94acceSBarry Smith }
21279b94acceSBarry Smith 
21284a2ae208SSatish Balay #undef __FUNCT__
21294a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
21303c7409f5SSatish Balay /*@C
21313c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2132d850072dSLois Curfman McInnes    SNES options in the database.
21333c7409f5SSatish Balay 
2134fee21e36SBarry Smith    Collective on SNES
2135fee21e36SBarry Smith 
2136c7afd0dbSLois Curfman McInnes    Input Parameter:
2137c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2138c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2139c7afd0dbSLois Curfman McInnes 
2140d850072dSLois Curfman McInnes    Notes:
2141a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2142c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2143d850072dSLois Curfman McInnes 
214436851e7fSLois Curfman McInnes    Level: advanced
214536851e7fSLois Curfman McInnes 
21463c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2147a86d99e1SLois Curfman McInnes 
2148a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
21493c7409f5SSatish Balay @*/
215063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetOptionsPrefix(SNES snes,const char prefix[])
21513c7409f5SSatish Balay {
2152dfbe8321SBarry Smith   PetscErrorCode ierr;
21533c7409f5SSatish Balay 
21543a40ed3dSBarry Smith   PetscFunctionBegin;
21554482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2156639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
215794b7f48cSBarry Smith   ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
21583a40ed3dSBarry Smith   PetscFunctionReturn(0);
21593c7409f5SSatish Balay }
21603c7409f5SSatish Balay 
21614a2ae208SSatish Balay #undef __FUNCT__
21624a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
21633c7409f5SSatish Balay /*@C
2164f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2165d850072dSLois Curfman McInnes    SNES options in the database.
21663c7409f5SSatish Balay 
2167fee21e36SBarry Smith    Collective on SNES
2168fee21e36SBarry Smith 
2169c7afd0dbSLois Curfman McInnes    Input Parameters:
2170c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2171c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2172c7afd0dbSLois Curfman McInnes 
2173d850072dSLois Curfman McInnes    Notes:
2174a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2175c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2176d850072dSLois Curfman McInnes 
217736851e7fSLois Curfman McInnes    Level: advanced
217836851e7fSLois Curfman McInnes 
21793c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2180a86d99e1SLois Curfman McInnes 
2181a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
21823c7409f5SSatish Balay @*/
218363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAppendOptionsPrefix(SNES snes,const char prefix[])
21843c7409f5SSatish Balay {
2185dfbe8321SBarry Smith   PetscErrorCode ierr;
21863c7409f5SSatish Balay 
21873a40ed3dSBarry Smith   PetscFunctionBegin;
21884482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2189639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
219094b7f48cSBarry Smith   ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
21913a40ed3dSBarry Smith   PetscFunctionReturn(0);
21923c7409f5SSatish Balay }
21933c7409f5SSatish Balay 
21944a2ae208SSatish Balay #undef __FUNCT__
21954a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
21969ab63eb5SSatish Balay /*@C
21973c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
21983c7409f5SSatish Balay    SNES options in the database.
21993c7409f5SSatish Balay 
2200c7afd0dbSLois Curfman McInnes    Not Collective
2201c7afd0dbSLois Curfman McInnes 
22023c7409f5SSatish Balay    Input Parameter:
22033c7409f5SSatish Balay .  snes - the SNES context
22043c7409f5SSatish Balay 
22053c7409f5SSatish Balay    Output Parameter:
22063c7409f5SSatish Balay .  prefix - pointer to the prefix string used
22073c7409f5SSatish Balay 
22089ab63eb5SSatish Balay    Notes: On the fortran side, the user should pass in a string 'prifix' of
22099ab63eb5SSatish Balay    sufficient length to hold the prefix.
22109ab63eb5SSatish Balay 
221136851e7fSLois Curfman McInnes    Level: advanced
221236851e7fSLois Curfman McInnes 
22133c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2214a86d99e1SLois Curfman McInnes 
2215a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
22163c7409f5SSatish Balay @*/
2217e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetOptionsPrefix(SNES snes,const char *prefix[])
22183c7409f5SSatish Balay {
2219dfbe8321SBarry Smith   PetscErrorCode ierr;
22203c7409f5SSatish Balay 
22213a40ed3dSBarry Smith   PetscFunctionBegin;
22224482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2223639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
22243a40ed3dSBarry Smith   PetscFunctionReturn(0);
22253c7409f5SSatish Balay }
22263c7409f5SSatish Balay 
2227b2002411SLois Curfman McInnes 
22284a2ae208SSatish Balay #undef __FUNCT__
22294a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
22303cea93caSBarry Smith /*@C
22313cea93caSBarry Smith   SNESRegister - See SNESRegisterDynamic()
22323cea93caSBarry Smith 
22337f6c08e0SMatthew Knepley   Level: advanced
22343cea93caSBarry Smith @*/
223563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES))
2236b2002411SLois Curfman McInnes {
2237e2d1d2b7SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
2238dfbe8321SBarry Smith   PetscErrorCode ierr;
2239b2002411SLois Curfman McInnes 
2240b2002411SLois Curfman McInnes   PetscFunctionBegin;
2241b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2242c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
2243b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2244b2002411SLois Curfman McInnes }
2245da9b6338SBarry Smith 
2246da9b6338SBarry Smith #undef __FUNCT__
2247da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin"
224863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESTestLocalMin(SNES snes)
2249da9b6338SBarry Smith {
2250dfbe8321SBarry Smith   PetscErrorCode ierr;
225177431f27SBarry Smith   PetscInt       N,i,j;
2252da9b6338SBarry Smith   Vec            u,uh,fh;
2253da9b6338SBarry Smith   PetscScalar    value;
2254da9b6338SBarry Smith   PetscReal      norm;
2255da9b6338SBarry Smith 
2256da9b6338SBarry Smith   PetscFunctionBegin;
2257da9b6338SBarry Smith   ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr);
2258da9b6338SBarry Smith   ierr = VecDuplicate(u,&uh);CHKERRQ(ierr);
2259da9b6338SBarry Smith   ierr = VecDuplicate(u,&fh);CHKERRQ(ierr);
2260da9b6338SBarry Smith 
2261da9b6338SBarry Smith   /* currently only works for sequential */
2262da9b6338SBarry Smith   ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n");
2263da9b6338SBarry Smith   ierr = VecGetSize(u,&N);CHKERRQ(ierr);
2264da9b6338SBarry Smith   for (i=0; i<N; i++) {
2265da9b6338SBarry Smith     ierr = VecCopy(u,uh);CHKERRQ(ierr);
226677431f27SBarry Smith     ierr  = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr);
2267da9b6338SBarry Smith     for (j=-10; j<11; j++) {
2268ccae9161SBarry Smith       value = PetscSign(j)*exp(PetscAbs(j)-10.0);
2269da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
22703ab0aad5SBarry Smith       ierr  = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr);
2271da9b6338SBarry Smith       ierr  = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr);
227277431f27SBarry Smith       ierr  = PetscPrintf(PETSC_COMM_WORLD,"       j norm %D %18.16e\n",j,norm);CHKERRQ(ierr);
2273da9b6338SBarry Smith       value = -value;
2274da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
2275da9b6338SBarry Smith     }
2276da9b6338SBarry Smith   }
2277da9b6338SBarry Smith   ierr = VecDestroy(uh);CHKERRQ(ierr);
2278da9b6338SBarry Smith   ierr = VecDestroy(fh);CHKERRQ(ierr);
2279da9b6338SBarry Smith   PetscFunctionReturn(0);
2280da9b6338SBarry Smith }
2281