xref: /petsc/src/snes/interface/snes.c (revision 2999313a40390bfad2537131cdec66ecc8a3cf1d)
163dd3a1aSKris Buschelman #define PETSCSNES_DLL
29b94acceSBarry Smith 
3e090d566SSatish Balay #include "src/snes/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);
53b0a32e0cSBarry Smith   if (!viewer) viewer = PETSC_VIEWER_STDOUT_(snes->comm);
544482741eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE,2);
55c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,viewer,2);
5674679c65SBarry Smith 
5732077d6dSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
58b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);CHKERRQ(ierr);
5932077d6dSBarry Smith   if (iascii) {
603a7fca6bSBarry Smith     if (snes->prefix) {
613a7fca6bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:(%s)\n",snes->prefix);CHKERRQ(ierr);
623a7fca6bSBarry Smith     } else {
63b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:\n");CHKERRQ(ierr);
643a7fca6bSBarry Smith     }
65454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
66454a90a3SBarry Smith     if (type) {
67b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: %s\n",type);CHKERRQ(ierr);
68184914b5SBarry Smith     } else {
69b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: not set yet\n");CHKERRQ(ierr);
70184914b5SBarry Smith     }
71e7788613SBarry Smith     if (snes->ops->view) {
72b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
73e7788613SBarry Smith       ierr = (*snes->ops->view)(snes,viewer);CHKERRQ(ierr);
74b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
750ef38995SBarry Smith     }
7677431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum iterations=%D, maximum function evaluations=%D\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr);
77a83599f4SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  tolerances: relative=%G, absolute=%G, solution=%G\n",
7870441072SBarry Smith                  snes->rtol,snes->abstol,snes->xtol);CHKERRQ(ierr);
7977431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",snes->linear_its);CHKERRQ(ierr);
8077431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of function evaluations=%D\n",snes->nfuncs);CHKERRQ(ierr);
819b94acceSBarry Smith     if (snes->ksp_ewconv) {
829b94acceSBarry Smith       kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
839b94acceSBarry Smith       if (kctx) {
8477431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"  Eisenstat-Walker computation of KSP relative tolerance (version %D)\n",kctx->version);CHKERRQ(ierr);
85a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    rtol_0=%G, rtol_max=%G, threshold=%G\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr);
86a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    gamma=%G, alpha=%G, alpha2=%G\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr);
879b94acceSBarry Smith       }
889b94acceSBarry Smith     }
890f5bd95cSBarry Smith   } else if (isstring) {
90454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
91b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr);
9219bcc07fSBarry Smith   }
9394b7f48cSBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
94b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
9594b7f48cSBarry Smith   ierr = KSPView(ksp,viewer);CHKERRQ(ierr);
96b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
973a40ed3dSBarry Smith   PetscFunctionReturn(0);
989b94acceSBarry Smith }
999b94acceSBarry Smith 
10076b2cf59SMatthew Knepley /*
10176b2cf59SMatthew Knepley   We retain a list of functions that also take SNES command
10276b2cf59SMatthew Knepley   line options. These are called at the end SNESSetFromOptions()
10376b2cf59SMatthew Knepley */
10476b2cf59SMatthew Knepley #define MAXSETFROMOPTIONS 5
105a7cc72afSBarry Smith static PetscInt numberofsetfromoptions;
1066849ba73SBarry Smith static PetscErrorCode (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES);
10776b2cf59SMatthew Knepley 
108e74ef692SMatthew Knepley #undef __FUNCT__
109e74ef692SMatthew Knepley #define __FUNCT__ "SNESAddOptionsChecker"
110ac226902SBarry Smith /*@C
11176b2cf59SMatthew Knepley   SNESAddOptionsChecker - Adds an additional function to check for SNES options.
11276b2cf59SMatthew Knepley 
11376b2cf59SMatthew Knepley   Not Collective
11476b2cf59SMatthew Knepley 
11576b2cf59SMatthew Knepley   Input Parameter:
11676b2cf59SMatthew Knepley . snescheck - function that checks for options
11776b2cf59SMatthew Knepley 
11876b2cf59SMatthew Knepley   Level: developer
11976b2cf59SMatthew Knepley 
12076b2cf59SMatthew Knepley .seealso: SNESSetFromOptions()
12176b2cf59SMatthew Knepley @*/
12263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAddOptionsChecker(PetscErrorCode (*snescheck)(SNES))
12376b2cf59SMatthew Knepley {
12476b2cf59SMatthew Knepley   PetscFunctionBegin;
12576b2cf59SMatthew Knepley   if (numberofsetfromoptions >= MAXSETFROMOPTIONS) {
12677431f27SBarry Smith     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %D allowed", MAXSETFROMOPTIONS);
12776b2cf59SMatthew Knepley   }
12876b2cf59SMatthew Knepley   othersetfromoptions[numberofsetfromoptions++] = snescheck;
12976b2cf59SMatthew Knepley   PetscFunctionReturn(0);
13076b2cf59SMatthew Knepley }
13176b2cf59SMatthew Knepley 
1324a2ae208SSatish Balay #undef __FUNCT__
1334a2ae208SSatish Balay #define __FUNCT__ "SNESSetFromOptions"
1349b94acceSBarry Smith /*@
13594b7f48cSBarry Smith    SNESSetFromOptions - Sets various SNES and KSP parameters from user options.
1369b94acceSBarry Smith 
137c7afd0dbSLois Curfman McInnes    Collective on SNES
138c7afd0dbSLois Curfman McInnes 
1399b94acceSBarry Smith    Input Parameter:
1409b94acceSBarry Smith .  snes - the SNES context
1419b94acceSBarry Smith 
14236851e7fSLois Curfman McInnes    Options Database Keys:
1436831982aSBarry Smith +  -snes_type <type> - ls, tr, umls, umtr, test
14482738288SBarry Smith .  -snes_stol - convergence tolerance in terms of the norm
14582738288SBarry Smith                 of the change in the solution between steps
14670441072SBarry Smith .  -snes_atol <abstol> - absolute tolerance of residual norm
147b39c3a46SLois Curfman McInnes .  -snes_rtol <rtol> - relative decrease in tolerance norm from initial
148b39c3a46SLois Curfman McInnes .  -snes_max_it <max_it> - maximum number of iterations
149b39c3a46SLois Curfman McInnes .  -snes_max_funcs <max_funcs> - maximum number of function evaluations
15050ffb88aSMatthew Knepley .  -snes_max_fail <max_fail> - maximum number of failures
151b39c3a46SLois Curfman McInnes .  -snes_trtol <trtol> - trust region tolerance
1522492ecdbSBarry Smith .  -snes_no_convergence_test - skip convergence test in nonlinear
15382738288SBarry Smith                                solver; hence iterations will continue until max_it
1541fbbfb26SLois Curfman McInnes                                or some other criterion is reached. Saves expense
15582738288SBarry Smith                                of convergence test
156e8105e01SRichard Katz .  -snes_monitor <optional filename> - prints residual norm at each iteration. if no
157e8105e01SRichard Katz                                        filename given prints to stdout
158d132466eSBarry Smith .  -snes_vecmonitor - plots solution at each iteration
159d132466eSBarry Smith .  -snes_vecmonitor_update - plots update to solution at each iteration
16082738288SBarry Smith .  -snes_xmonitor - plots residual norm at each iteration
161e24b481bSBarry Smith .  -snes_fd - use finite differences to compute Jacobian; very slow, only for testing
1625968eb51SBarry Smith .  -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration
1635968eb51SBarry Smith -  -snes_print_converged_reason - print the reason for convergence/divergence after each solve
16482738288SBarry Smith 
16582738288SBarry Smith     Options Database for Eisenstat-Walker method:
1664b27c08aSLois Curfman McInnes +  -snes_ksp_ew_conv - use Eisenstat-Walker method for determining linear system convergence
1674b27c08aSLois Curfman McInnes .  -snes_ksp_ew_version ver - version of  Eisenstat-Walker method
16836851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
16936851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
17036851e7fSLois Curfman McInnes .  -snes_ksp_ew_gamma <gamma> - Sets gamma
17136851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha <alpha> - Sets alpha
17236851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
17336851e7fSLois Curfman McInnes -  -snes_ksp_ew_threshold <threshold> - Sets threshold
17482738288SBarry Smith 
17511ca99fdSLois Curfman McInnes    Notes:
17611ca99fdSLois Curfman McInnes    To see all options, run your program with the -help option or consult
17711ca99fdSLois Curfman McInnes    the users manual.
17883e2fdc7SBarry Smith 
17936851e7fSLois Curfman McInnes    Level: beginner
18036851e7fSLois Curfman McInnes 
1819b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database
1829b94acceSBarry Smith 
18369ed319cSSatish Balay .seealso: SNESSetOptionsPrefix()
1849b94acceSBarry Smith @*/
18563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFromOptions(SNES snes)
1869b94acceSBarry Smith {
18794b7f48cSBarry Smith   KSP                 ksp;
188186905e3SBarry Smith   SNES_KSP_EW_ConvCtx *kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
189f1af5d2fSBarry Smith   PetscTruth          flg;
190dfbe8321SBarry Smith   PetscErrorCode      ierr;
19177431f27SBarry Smith   PetscInt            i;
1922fc52814SBarry Smith   const char          *deft;
193e8105e01SRichard Katz   char                type[256], monfilename[PETSC_MAX_PATH_LEN];
194e8105e01SRichard Katz   PetscViewer         monviewer;
1959b94acceSBarry Smith 
1963a40ed3dSBarry Smith   PetscFunctionBegin;
1974482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
198ca161407SBarry Smith 
199b0a32e0cSBarry Smith   ierr = PetscOptionsBegin(snes->comm,snes->prefix,"Nonlinear solver (SNES) options","SNES");CHKERRQ(ierr);
200186905e3SBarry Smith     if (snes->type_name) {
201186905e3SBarry Smith       deft = snes->type_name;
202186905e3SBarry Smith     } else {
2034b27c08aSLois Curfman McInnes       deft = SNESLS;
204d64ed03dSBarry Smith     }
2054bbc92c1SBarry Smith 
206186905e3SBarry Smith     if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
207b0a32e0cSBarry Smith     ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr);
208d64ed03dSBarry Smith     if (flg) {
209186905e3SBarry Smith       ierr = SNESSetType(snes,type);CHKERRQ(ierr);
210186905e3SBarry Smith     } else if (!snes->type_name) {
211186905e3SBarry Smith       ierr = SNESSetType(snes,deft);CHKERRQ(ierr);
212d64ed03dSBarry Smith     }
213909c8a9fSBarry Smith     ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr);
21493c39befSBarry Smith 
21587828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_stol","Stop if step length less then","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr);
21670441072SBarry Smith     ierr = PetscOptionsReal("-snes_atol","Stop if function norm less then","SNESSetTolerances",snes->abstol,&snes->abstol,0);CHKERRQ(ierr);
217186905e3SBarry Smith 
21887828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less then","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr);
219b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr);
220b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr);
22150ffb88aSMatthew Knepley     ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr);
2225968eb51SBarry Smith     ierr = PetscOptionsName("-snes_converged_reason","Print reason for converged or diverged","SNESSolve",&flg);CHKERRQ(ierr);
2235968eb51SBarry Smith     if (flg) {
2245968eb51SBarry Smith       snes->printreason = PETSC_TRUE;
2255968eb51SBarry Smith     }
226186905e3SBarry Smith 
2277aaed0d8SBarry 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);
228186905e3SBarry Smith 
229cf502942SBarry Smith     ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1, 2 or 3","SNES_KSP_SetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr);
23087828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNES_KSP_SetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr);
23187828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNES_KSP_SetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr);
23287828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNES_KSP_SetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr);
23387828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNES_KSP_SetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr);
23487828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNES_KSP_SetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr);
23587828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNES_KSP_SetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr);
236186905e3SBarry Smith 
237b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_no_convergence_test","Don't test for convergence","None",&flg);CHKERRQ(ierr);
238e7788613SBarry Smith     if (flg) {snes->ops->converged = 0;}
239b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_cancelmonitors","Remove all monitors","SNESClearMonitor",&flg);CHKERRQ(ierr);
2405cd90555SBarry Smith     if (flg) {ierr = SNESClearMonitor(snes);CHKERRQ(ierr);}
241eabae89aSBarry Smith 
242eabae89aSBarry Smith     ierr = PetscOptionsString("-snes_monitor","Monitor norm of function","SNESSetMonitor","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
243e8105e01SRichard Katz     if (flg) {
244e8105e01SRichard Katz       ierr = PetscViewerASCIIOpen(snes->comm,monfilename,&monviewer);CHKERRQ(ierr);
245e8105e01SRichard Katz       ierr = SNESSetMonitor(snes,SNESDefaultMonitor,monviewer,(PetscErrorCode (*)(void*))PetscViewerDestroy);CHKERRQ(ierr);
246e8105e01SRichard Katz     }
247eabae89aSBarry Smith 
248eabae89aSBarry Smith     ierr = PetscOptionsString("-snes_ratiomonitor","Monitor ratios of norms of function","SNESSetRatioMonitor","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
249eabae89aSBarry Smith     if (flg) {
250eabae89aSBarry Smith       ierr = PetscViewerASCIIOpen(snes->comm,monfilename,&monviewer);CHKERRQ(ierr);
251eabae89aSBarry Smith       ierr = SNESSetRatioMonitor(snes,monviewer);CHKERRQ(ierr);
252e8105e01SRichard Katz     }
253eabae89aSBarry Smith 
254eabae89aSBarry Smith     ierr = PetscOptionsString("-snes_smonitor","Monitor norm of function (fewer digits)","SNESSetMonitor","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
255eabae89aSBarry Smith     if (flg) {
256eabae89aSBarry Smith       ierr = PetscViewerASCIIOpen(snes->comm,monfilename,&monviewer);CHKERRQ(ierr);
257eabae89aSBarry Smith       ierr = SNESSetMonitor(snes,SNESDefaultSMonitor,monviewer,(PetscErrorCode (*)(void*))PetscViewerDestroy);CHKERRQ(ierr);
258eabae89aSBarry Smith     }
259eabae89aSBarry Smith 
260b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_vecmonitor","Plot solution at each iteration","SNESVecViewMonitor",&flg);CHKERRQ(ierr);
261b8a78c4aSBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewMonitor,0,0);CHKERRQ(ierr);}
262b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_vecmonitor_update","Plot correction at each iteration","SNESVecViewUpdateMonitor",&flg);CHKERRQ(ierr);
2637c922b88SBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewUpdateMonitor,0,0);CHKERRQ(ierr);}
2645ed2d596SBarry Smith     ierr = PetscOptionsName("-snes_vecmonitor_residual","Plot residual at each iteration","SNESVecViewResidualMonitor",&flg);CHKERRQ(ierr);
2655ed2d596SBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewResidualMonitor,0,0);CHKERRQ(ierr);}
266b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_xmonitor","Plot function norm at each iteration","SNESLGMonitor",&flg);CHKERRQ(ierr);
267186905e3SBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESLGMonitor,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
268e24b481bSBarry Smith 
269b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",&flg);CHKERRQ(ierr);
2704b27c08aSLois Curfman McInnes     if (flg) {
271186905e3SBarry Smith       ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr);
272ae15b995SBarry Smith       ierr = PetscInfo(snes,"Setting default finite difference Jacobian matrix\n");CHKERRQ(ierr);
2739b94acceSBarry Smith     }
274639f9d9dSBarry Smith 
27576b2cf59SMatthew Knepley     for(i = 0; i < numberofsetfromoptions; i++) {
27676b2cf59SMatthew Knepley       ierr = (*othersetfromoptions[i])(snes);CHKERRQ(ierr);
27776b2cf59SMatthew Knepley     }
27876b2cf59SMatthew Knepley 
279e7788613SBarry Smith     if (snes->ops->setfromoptions) {
280e7788613SBarry Smith       ierr = (*snes->ops->setfromoptions)(snes);CHKERRQ(ierr);
281639f9d9dSBarry Smith     }
282b0a32e0cSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
2834bbc92c1SBarry Smith 
28494b7f48cSBarry Smith   ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
28594b7f48cSBarry Smith   ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);
28693993e2dSLois Curfman McInnes 
2873a40ed3dSBarry Smith   PetscFunctionReturn(0);
2889b94acceSBarry Smith }
2899b94acceSBarry Smith 
290a847f771SSatish Balay 
2914a2ae208SSatish Balay #undef __FUNCT__
2924a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext"
2939b94acceSBarry Smith /*@
2949b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
2959b94acceSBarry Smith    the nonlinear solvers.
2969b94acceSBarry Smith 
297fee21e36SBarry Smith    Collective on SNES
298fee21e36SBarry Smith 
299c7afd0dbSLois Curfman McInnes    Input Parameters:
300c7afd0dbSLois Curfman McInnes +  snes - the SNES context
301c7afd0dbSLois Curfman McInnes -  usrP - optional user context
302c7afd0dbSLois Curfman McInnes 
30336851e7fSLois Curfman McInnes    Level: intermediate
30436851e7fSLois Curfman McInnes 
3059b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
3069b94acceSBarry Smith 
3079b94acceSBarry Smith .seealso: SNESGetApplicationContext()
3089b94acceSBarry Smith @*/
30963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetApplicationContext(SNES snes,void *usrP)
3109b94acceSBarry Smith {
3113a40ed3dSBarry Smith   PetscFunctionBegin;
3124482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
3139b94acceSBarry Smith   snes->user		= usrP;
3143a40ed3dSBarry Smith   PetscFunctionReturn(0);
3159b94acceSBarry Smith }
31674679c65SBarry Smith 
3174a2ae208SSatish Balay #undef __FUNCT__
3184a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext"
3199b94acceSBarry Smith /*@C
3209b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
3219b94acceSBarry Smith    nonlinear solvers.
3229b94acceSBarry Smith 
323c7afd0dbSLois Curfman McInnes    Not Collective
324c7afd0dbSLois Curfman McInnes 
3259b94acceSBarry Smith    Input Parameter:
3269b94acceSBarry Smith .  snes - SNES context
3279b94acceSBarry Smith 
3289b94acceSBarry Smith    Output Parameter:
3299b94acceSBarry Smith .  usrP - user context
3309b94acceSBarry Smith 
33136851e7fSLois Curfman McInnes    Level: intermediate
33236851e7fSLois Curfman McInnes 
3339b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
3349b94acceSBarry Smith 
3359b94acceSBarry Smith .seealso: SNESSetApplicationContext()
3369b94acceSBarry Smith @*/
33763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetApplicationContext(SNES snes,void **usrP)
3389b94acceSBarry Smith {
3393a40ed3dSBarry Smith   PetscFunctionBegin;
3404482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
3419b94acceSBarry Smith   *usrP = snes->user;
3423a40ed3dSBarry Smith   PetscFunctionReturn(0);
3439b94acceSBarry Smith }
34474679c65SBarry Smith 
3454a2ae208SSatish Balay #undef __FUNCT__
3464a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber"
3479b94acceSBarry Smith /*@
348c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
349c8228a4eSBarry Smith    at this time.
3509b94acceSBarry Smith 
351c7afd0dbSLois Curfman McInnes    Not Collective
352c7afd0dbSLois Curfman McInnes 
3539b94acceSBarry Smith    Input Parameter:
3549b94acceSBarry Smith .  snes - SNES context
3559b94acceSBarry Smith 
3569b94acceSBarry Smith    Output Parameter:
3579b94acceSBarry Smith .  iter - iteration number
3589b94acceSBarry Smith 
359c8228a4eSBarry Smith    Notes:
360c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
361c8228a4eSBarry Smith 
362c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
36308405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
36408405cd6SLois Curfman McInnes .vb
36508405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
36608405cd6SLois Curfman McInnes       if (!(it % 2)) {
36708405cd6SLois Curfman McInnes         [compute Jacobian here]
36808405cd6SLois Curfman McInnes       }
36908405cd6SLois Curfman McInnes .ve
370c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
37108405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
372c8228a4eSBarry Smith 
37336851e7fSLois Curfman McInnes    Level: intermediate
37436851e7fSLois Curfman McInnes 
3752b668275SBarry Smith .keywords: SNES, nonlinear, get, iteration, number,
3762b668275SBarry Smith 
3772b668275SBarry Smith .seealso:   SNESGetFunctionNorm(), SNESGetNumberLinearIterations()
3789b94acceSBarry Smith @*/
37963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetIterationNumber(SNES snes,PetscInt* iter)
3809b94acceSBarry Smith {
3813a40ed3dSBarry Smith   PetscFunctionBegin;
3824482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
3834482741eSBarry Smith   PetscValidIntPointer(iter,2);
3849b94acceSBarry Smith   *iter = snes->iter;
3853a40ed3dSBarry Smith   PetscFunctionReturn(0);
3869b94acceSBarry Smith }
38774679c65SBarry Smith 
3884a2ae208SSatish Balay #undef __FUNCT__
3894a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm"
3909b94acceSBarry Smith /*@
3919b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
3929b94acceSBarry Smith    with SNESSSetFunction().
3939b94acceSBarry Smith 
394c7afd0dbSLois Curfman McInnes    Collective on SNES
395c7afd0dbSLois Curfman McInnes 
3969b94acceSBarry Smith    Input Parameter:
3979b94acceSBarry Smith .  snes - SNES context
3989b94acceSBarry Smith 
3999b94acceSBarry Smith    Output Parameter:
4009b94acceSBarry Smith .  fnorm - 2-norm of function
4019b94acceSBarry Smith 
40236851e7fSLois Curfman McInnes    Level: intermediate
40336851e7fSLois Curfman McInnes 
4049b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
405a86d99e1SLois Curfman McInnes 
4062b668275SBarry Smith .seealso: SNESGetFunction(), SNESGetIterationNumber(), SNESGetNumberLinearIterations()
4079b94acceSBarry Smith @*/
40863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunctionNorm(SNES snes,PetscScalar *fnorm)
4099b94acceSBarry Smith {
4103a40ed3dSBarry Smith   PetscFunctionBegin;
4114482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
4124482741eSBarry Smith   PetscValidScalarPointer(fnorm,2);
4139b94acceSBarry Smith   *fnorm = snes->norm;
4143a40ed3dSBarry Smith   PetscFunctionReturn(0);
4159b94acceSBarry Smith }
41674679c65SBarry Smith 
4174a2ae208SSatish Balay #undef __FUNCT__
4184a2ae208SSatish Balay #define __FUNCT__ "SNESGetNumberUnsuccessfulSteps"
4199b94acceSBarry Smith /*@
4209b94acceSBarry Smith    SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps
4219b94acceSBarry Smith    attempted by the nonlinear solver.
4229b94acceSBarry Smith 
423c7afd0dbSLois Curfman McInnes    Not Collective
424c7afd0dbSLois Curfman McInnes 
4259b94acceSBarry Smith    Input Parameter:
4269b94acceSBarry Smith .  snes - SNES context
4279b94acceSBarry Smith 
4289b94acceSBarry Smith    Output Parameter:
4299b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
4309b94acceSBarry Smith 
431c96a6f78SLois Curfman McInnes    Notes:
432c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
433c96a6f78SLois Curfman McInnes 
43436851e7fSLois Curfman McInnes    Level: intermediate
43536851e7fSLois Curfman McInnes 
4369b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
4379b94acceSBarry Smith @*/
43863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNumberUnsuccessfulSteps(SNES snes,PetscInt* nfails)
4399b94acceSBarry Smith {
4403a40ed3dSBarry Smith   PetscFunctionBegin;
4414482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
4424482741eSBarry Smith   PetscValidIntPointer(nfails,2);
44350ffb88aSMatthew Knepley   *nfails = snes->numFailures;
44450ffb88aSMatthew Knepley   PetscFunctionReturn(0);
44550ffb88aSMatthew Knepley }
44650ffb88aSMatthew Knepley 
44750ffb88aSMatthew Knepley #undef __FUNCT__
44850ffb88aSMatthew Knepley #define __FUNCT__ "SNESSetMaximumUnsuccessfulSteps"
44950ffb88aSMatthew Knepley /*@
45050ffb88aSMatthew Knepley    SNESSetMaximumUnsuccessfulSteps - Sets the maximum number of unsuccessful steps
45150ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
45250ffb88aSMatthew Knepley 
45350ffb88aSMatthew Knepley    Not Collective
45450ffb88aSMatthew Knepley 
45550ffb88aSMatthew Knepley    Input Parameters:
45650ffb88aSMatthew Knepley +  snes     - SNES context
45750ffb88aSMatthew Knepley -  maxFails - maximum of unsuccessful steps
45850ffb88aSMatthew Knepley 
45950ffb88aSMatthew Knepley    Level: intermediate
46050ffb88aSMatthew Knepley 
46150ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
46250ffb88aSMatthew Knepley @*/
46363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaximumUnsuccessfulSteps(SNES snes, PetscInt maxFails)
46450ffb88aSMatthew Knepley {
46550ffb88aSMatthew Knepley   PetscFunctionBegin;
4664482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
46750ffb88aSMatthew Knepley   snes->maxFailures = maxFails;
46850ffb88aSMatthew Knepley   PetscFunctionReturn(0);
46950ffb88aSMatthew Knepley }
47050ffb88aSMatthew Knepley 
47150ffb88aSMatthew Knepley #undef __FUNCT__
47250ffb88aSMatthew Knepley #define __FUNCT__ "SNESGetMaximumUnsuccessfulSteps"
47350ffb88aSMatthew Knepley /*@
47450ffb88aSMatthew Knepley    SNESGetMaximumUnsuccessfulSteps - Gets the maximum number of unsuccessful steps
47550ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
47650ffb88aSMatthew Knepley 
47750ffb88aSMatthew Knepley    Not Collective
47850ffb88aSMatthew Knepley 
47950ffb88aSMatthew Knepley    Input Parameter:
48050ffb88aSMatthew Knepley .  snes     - SNES context
48150ffb88aSMatthew Knepley 
48250ffb88aSMatthew Knepley    Output Parameter:
48350ffb88aSMatthew Knepley .  maxFails - maximum of unsuccessful steps
48450ffb88aSMatthew Knepley 
48550ffb88aSMatthew Knepley    Level: intermediate
48650ffb88aSMatthew Knepley 
48750ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
48850ffb88aSMatthew Knepley @*/
48963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaximumUnsuccessfulSteps(SNES snes, PetscInt *maxFails)
49050ffb88aSMatthew Knepley {
49150ffb88aSMatthew Knepley   PetscFunctionBegin;
4924482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
4934482741eSBarry Smith   PetscValidIntPointer(maxFails,2);
49450ffb88aSMatthew Knepley   *maxFails = snes->maxFailures;
4953a40ed3dSBarry Smith   PetscFunctionReturn(0);
4969b94acceSBarry Smith }
497a847f771SSatish Balay 
4984a2ae208SSatish Balay #undef __FUNCT__
4993d4c4710SBarry Smith #define __FUNCT__ "SNESGetLinearSolveFailures"
5003d4c4710SBarry Smith /*@
5013d4c4710SBarry Smith    SNESGetLinearSolveFailures - Gets the number of failed (non-converged)
5023d4c4710SBarry Smith    linear solvers.
5033d4c4710SBarry Smith 
5043d4c4710SBarry Smith    Not Collective
5053d4c4710SBarry Smith 
5063d4c4710SBarry Smith    Input Parameter:
5073d4c4710SBarry Smith .  snes - SNES context
5083d4c4710SBarry Smith 
5093d4c4710SBarry Smith    Output Parameter:
5103d4c4710SBarry Smith .  nfails - number of failed solves
5113d4c4710SBarry Smith 
5123d4c4710SBarry Smith    Notes:
5133d4c4710SBarry Smith    This counter is reset to zero for each successive call to SNESSolve().
5143d4c4710SBarry Smith 
5153d4c4710SBarry Smith    Level: intermediate
5163d4c4710SBarry Smith 
5173d4c4710SBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
5183d4c4710SBarry Smith @*/
5193d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetLinearSolveFailures(SNES snes,PetscInt* nfails)
5203d4c4710SBarry Smith {
5213d4c4710SBarry Smith   PetscFunctionBegin;
5223d4c4710SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
5233d4c4710SBarry Smith   PetscValidIntPointer(nfails,2);
5243d4c4710SBarry Smith   *nfails = snes->numLinearSolveFailures;
5253d4c4710SBarry Smith   PetscFunctionReturn(0);
5263d4c4710SBarry Smith }
5273d4c4710SBarry Smith 
5283d4c4710SBarry Smith #undef __FUNCT__
5293d4c4710SBarry Smith #define __FUNCT__ "SNESSetMaxLinearSolveFailures"
5303d4c4710SBarry Smith /*@
5313d4c4710SBarry Smith    SNESSetMaxLinearSolveFailures - the number of failed linear solve attempts
5323d4c4710SBarry Smith    allowed before SNES returns with a diverged reason of SNES_DIVERGED_LINEAR_SOLVE
5333d4c4710SBarry Smith 
5343d4c4710SBarry Smith    Collective on SNES
5353d4c4710SBarry Smith 
5363d4c4710SBarry Smith    Input Parameters:
5373d4c4710SBarry Smith +  snes     - SNES context
5383d4c4710SBarry Smith -  maxFails - maximum allowed linear solve failures
5393d4c4710SBarry Smith 
5403d4c4710SBarry Smith    Level: intermediate
5413d4c4710SBarry Smith 
5423d4c4710SBarry Smith    Notes: By default this is 1; that is SNES returns on the first failed linear solve
5433d4c4710SBarry Smith 
5443d4c4710SBarry Smith .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
5453d4c4710SBarry Smith 
5463d4c4710SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures()
5473d4c4710SBarry Smith @*/
5483d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMaxLinearSolveFailures(SNES snes, PetscInt maxFails)
5493d4c4710SBarry Smith {
5503d4c4710SBarry Smith   PetscFunctionBegin;
5513d4c4710SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
5523d4c4710SBarry Smith   snes->maxLinearSolveFailures = maxFails;
5533d4c4710SBarry Smith   PetscFunctionReturn(0);
5543d4c4710SBarry Smith }
5553d4c4710SBarry Smith 
5563d4c4710SBarry Smith #undef __FUNCT__
5573d4c4710SBarry Smith #define __FUNCT__ "SNESGetMaxLinearSolveFailures"
5583d4c4710SBarry Smith /*@
5593d4c4710SBarry Smith    SNESGetMaxLinearSolveFailures - gets the maximum number of linear solve failures that
5603d4c4710SBarry Smith      are allowed before SNES terminates
5613d4c4710SBarry Smith 
5623d4c4710SBarry Smith    Not Collective
5633d4c4710SBarry Smith 
5643d4c4710SBarry Smith    Input Parameter:
5653d4c4710SBarry Smith .  snes     - SNES context
5663d4c4710SBarry Smith 
5673d4c4710SBarry Smith    Output Parameter:
5683d4c4710SBarry Smith .  maxFails - maximum of unsuccessful solves allowed
5693d4c4710SBarry Smith 
5703d4c4710SBarry Smith    Level: intermediate
5713d4c4710SBarry Smith 
5723d4c4710SBarry Smith    Notes: By default this is 1; that is SNES returns on the first failed linear solve
5733d4c4710SBarry Smith 
5743d4c4710SBarry Smith .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
5753d4c4710SBarry Smith 
5763d4c4710SBarry Smith .seealso: SNESGetLinearSolveFailures(), SNESGetMaxLinearSolveFailures()
5773d4c4710SBarry Smith @*/
5783d4c4710SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetMaxLinearSolveFailures(SNES snes, PetscInt *maxFails)
5793d4c4710SBarry Smith {
5803d4c4710SBarry Smith   PetscFunctionBegin;
5813d4c4710SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
5823d4c4710SBarry Smith   PetscValidIntPointer(maxFails,2);
5833d4c4710SBarry Smith   *maxFails = snes->maxLinearSolveFailures;
5843d4c4710SBarry Smith   PetscFunctionReturn(0);
5853d4c4710SBarry Smith }
5863d4c4710SBarry Smith 
5873d4c4710SBarry Smith #undef __FUNCT__
5884a2ae208SSatish Balay #define __FUNCT__ "SNESGetNumberLinearIterations"
589c96a6f78SLois Curfman McInnes /*@
590c96a6f78SLois Curfman McInnes    SNESGetNumberLinearIterations - Gets the total number of linear iterations
591c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
592c96a6f78SLois Curfman McInnes 
593c7afd0dbSLois Curfman McInnes    Not Collective
594c7afd0dbSLois Curfman McInnes 
595c96a6f78SLois Curfman McInnes    Input Parameter:
596c96a6f78SLois Curfman McInnes .  snes - SNES context
597c96a6f78SLois Curfman McInnes 
598c96a6f78SLois Curfman McInnes    Output Parameter:
599c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
600c96a6f78SLois Curfman McInnes 
601c96a6f78SLois Curfman McInnes    Notes:
602c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
603c96a6f78SLois Curfman McInnes 
60436851e7fSLois Curfman McInnes    Level: intermediate
60536851e7fSLois Curfman McInnes 
606c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
6072b668275SBarry Smith 
6082b668275SBarry Smith .seealso:  SNESGetIterationNumber(), SNESGetFunctionNorm()
609c96a6f78SLois Curfman McInnes @*/
61063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetNumberLinearIterations(SNES snes,PetscInt* lits)
611c96a6f78SLois Curfman McInnes {
6123a40ed3dSBarry Smith   PetscFunctionBegin;
6134482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
6144482741eSBarry Smith   PetscValidIntPointer(lits,2);
615c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
6163a40ed3dSBarry Smith   PetscFunctionReturn(0);
617c96a6f78SLois Curfman McInnes }
618c96a6f78SLois Curfman McInnes 
6194a2ae208SSatish Balay #undef __FUNCT__
62094b7f48cSBarry Smith #define __FUNCT__ "SNESGetKSP"
62152baeb72SSatish Balay /*@
62294b7f48cSBarry Smith    SNESGetKSP - Returns the KSP context for a SNES solver.
6239b94acceSBarry Smith 
62494b7f48cSBarry Smith    Not Collective, but if SNES object is parallel, then KSP object is parallel
625c7afd0dbSLois Curfman McInnes 
6269b94acceSBarry Smith    Input Parameter:
6279b94acceSBarry Smith .  snes - the SNES context
6289b94acceSBarry Smith 
6299b94acceSBarry Smith    Output Parameter:
63094b7f48cSBarry Smith .  ksp - the KSP context
6319b94acceSBarry Smith 
6329b94acceSBarry Smith    Notes:
63394b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
6349b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
635*2999313aSBarry Smith    PC contexts as well.
6369b94acceSBarry Smith 
63736851e7fSLois Curfman McInnes    Level: beginner
63836851e7fSLois Curfman McInnes 
63994b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
6409b94acceSBarry Smith 
641*2999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
6429b94acceSBarry Smith @*/
64363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetKSP(SNES snes,KSP *ksp)
6449b94acceSBarry Smith {
6453a40ed3dSBarry Smith   PetscFunctionBegin;
6464482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
6474482741eSBarry Smith   PetscValidPointer(ksp,2);
64894b7f48cSBarry Smith   *ksp = snes->ksp;
6493a40ed3dSBarry Smith   PetscFunctionReturn(0);
6509b94acceSBarry Smith }
65182bf6240SBarry Smith 
6524a2ae208SSatish Balay #undef __FUNCT__
653*2999313aSBarry Smith #define __FUNCT__ "SNESSetKSP"
654*2999313aSBarry Smith /*@
655*2999313aSBarry Smith    SNESSetKSP - Sets a KSP context for the SNES object to use
656*2999313aSBarry Smith 
657*2999313aSBarry Smith    Not Collective, but the SNES and KSP objects must live on the same MPI_Comm
658*2999313aSBarry Smith 
659*2999313aSBarry Smith    Input Parameters:
660*2999313aSBarry Smith +  snes - the SNES context
661*2999313aSBarry Smith -  ksp - the KSP context
662*2999313aSBarry Smith 
663*2999313aSBarry Smith    Notes:
664*2999313aSBarry Smith    The SNES object already has its KSP object, you can obtain with SNESGetKSP()
665*2999313aSBarry Smith    so this routine is rarely needed.
666*2999313aSBarry Smith 
667*2999313aSBarry Smith    The KSP object that is already in the SNES object has its reference count
668*2999313aSBarry Smith    decreased by one.
669*2999313aSBarry Smith 
670*2999313aSBarry Smith    Level: developer
671*2999313aSBarry Smith 
672*2999313aSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
673*2999313aSBarry Smith 
674*2999313aSBarry Smith .seealso: KSPGetPC(), SNESCreate(), KSPCreate(), SNESSetKSP()
675*2999313aSBarry Smith @*/
676*2999313aSBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetKSP(SNES snes,KSP ksp)
677*2999313aSBarry Smith {
678*2999313aSBarry Smith   PetscErrorCode ierr;
679*2999313aSBarry Smith 
680*2999313aSBarry Smith   PetscFunctionBegin;
681*2999313aSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
682*2999313aSBarry Smith   PetscValidHeaderSpecific(ksp,KSP_COOKIE,2);
683*2999313aSBarry Smith   PetscCheckSameComm(snes,1,ksp,2);
684*2999313aSBarry Smith   if (snes->ksp) {
685*2999313aSBarry Smith     ierr = KSPDestroy(snes->ksp);CHKERRQ(ierr);
686*2999313aSBarry Smith   }
687*2999313aSBarry Smith   snes->ksp = ksp;
688*2999313aSBarry Smith   PetscFunctionReturn(0);
689*2999313aSBarry Smith }
690*2999313aSBarry Smith 
691*2999313aSBarry Smith #undef __FUNCT__
6924a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc"
6936849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj)
694e24b481bSBarry Smith {
695e24b481bSBarry Smith   PetscFunctionBegin;
696e24b481bSBarry Smith   PetscFunctionReturn(0);
697e24b481bSBarry Smith }
698e24b481bSBarry Smith 
6999b94acceSBarry Smith /* -----------------------------------------------------------*/
7004a2ae208SSatish Balay #undef __FUNCT__
7014a2ae208SSatish Balay #define __FUNCT__ "SNESCreate"
70252baeb72SSatish Balay /*@
7039b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
7049b94acceSBarry Smith 
705c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
706c7afd0dbSLois Curfman McInnes 
707c7afd0dbSLois Curfman McInnes    Input Parameters:
708c7afd0dbSLois Curfman McInnes +  comm - MPI communicator
7099b94acceSBarry Smith 
7109b94acceSBarry Smith    Output Parameter:
7119b94acceSBarry Smith .  outsnes - the new SNES context
7129b94acceSBarry Smith 
713c7afd0dbSLois Curfman McInnes    Options Database Keys:
714c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
715c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
716c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
717c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
718c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
719c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
720c1f60f51SBarry Smith 
72136851e7fSLois Curfman McInnes    Level: beginner
72236851e7fSLois Curfman McInnes 
7239b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
7249b94acceSBarry Smith 
7254b27c08aSLois Curfman McInnes .seealso: SNESSolve(), SNESDestroy(), SNES
7269b94acceSBarry Smith @*/
72763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESCreate(MPI_Comm comm,SNES *outsnes)
7289b94acceSBarry Smith {
729dfbe8321SBarry Smith   PetscErrorCode      ierr;
7309b94acceSBarry Smith   SNES                snes;
7319b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
73237fcc0dbSBarry Smith 
7333a40ed3dSBarry Smith   PetscFunctionBegin;
734ed1caa07SMatthew Knepley   PetscValidPointer(outsnes,2);
7358ba1e511SMatthew Knepley   *outsnes = PETSC_NULL;
7368ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
7378ba1e511SMatthew Knepley   ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr);
7388ba1e511SMatthew Knepley #endif
7398ba1e511SMatthew Knepley 
740e7788613SBarry Smith   ierr = PetscHeaderCreate(snes,_p_SNES,struct _SNESOps,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr);
741e24b481bSBarry Smith   snes->bops->publish     = SNESPublish_Petsc;
7429b94acceSBarry Smith   snes->max_its           = 50;
7439750a799SBarry Smith   snes->max_funcs	  = 10000;
7449b94acceSBarry Smith   snes->norm		  = 0.0;
745b4874afaSBarry Smith   snes->rtol		  = 1.e-8;
746b4874afaSBarry Smith   snes->ttol              = 0.0;
74770441072SBarry Smith   snes->abstol		  = 1.e-50;
7489b94acceSBarry Smith   snes->xtol		  = 1.e-8;
7494b27c08aSLois Curfman McInnes   snes->deltatol	  = 1.e-12;
7509b94acceSBarry Smith   snes->nfuncs            = 0;
75150ffb88aSMatthew Knepley   snes->numFailures       = 0;
75250ffb88aSMatthew Knepley   snes->maxFailures       = 1;
7537a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
754639f9d9dSBarry Smith   snes->numbermonitors    = 0;
7559b94acceSBarry Smith   snes->data              = 0;
756e7788613SBarry Smith   snes->ops->view         = 0;
7574dc4c822SBarry Smith   snes->setupcalled       = PETSC_FALSE;
758186905e3SBarry Smith   snes->ksp_ewconv        = PETSC_FALSE;
7596f24a144SLois Curfman McInnes   snes->vwork             = 0;
7606f24a144SLois Curfman McInnes   snes->nwork             = 0;
761758f92a0SBarry Smith   snes->conv_hist_len     = 0;
762758f92a0SBarry Smith   snes->conv_hist_max     = 0;
763758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
764758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
765758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
766184914b5SBarry Smith   snes->reason            = SNES_CONVERGED_ITERATING;
7679b94acceSBarry Smith 
7683d4c4710SBarry Smith   snes->numLinearSolveFailures = 0;
7693d4c4710SBarry Smith   snes->maxLinearSolveFailures = 1;
7703d4c4710SBarry Smith 
7719b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
772b0a32e0cSBarry Smith   ierr = PetscNew(SNES_KSP_EW_ConvCtx,&kctx);CHKERRQ(ierr);
77352e6d16bSBarry Smith   ierr = PetscLogObjectMemory(snes,sizeof(SNES_KSP_EW_ConvCtx));CHKERRQ(ierr);
7749b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
7759b94acceSBarry Smith   kctx->version     = 2;
7769b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
7779b94acceSBarry Smith                              this was too large for some test cases */
7789b94acceSBarry Smith   kctx->rtol_last   = 0;
7799b94acceSBarry Smith   kctx->rtol_max    = .9;
7809b94acceSBarry Smith   kctx->gamma       = 1.0;
7819b94acceSBarry Smith   kctx->alpha2      = .5*(1.0 + sqrt(5.0));
7829b94acceSBarry Smith   kctx->alpha       = kctx->alpha2;
7839b94acceSBarry Smith   kctx->threshold   = .1;
7849b94acceSBarry Smith   kctx->lresid_last = 0;
7859b94acceSBarry Smith   kctx->norm_last   = 0;
7869b94acceSBarry Smith 
78794b7f48cSBarry Smith   ierr = KSPCreate(comm,&snes->ksp);CHKERRQ(ierr);
78852e6d16bSBarry Smith   ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr);
7895334005bSBarry Smith 
7909b94acceSBarry Smith   *outsnes = snes;
79100036973SBarry Smith   ierr = PetscPublishAll(snes);CHKERRQ(ierr);
7923a40ed3dSBarry Smith   PetscFunctionReturn(0);
7939b94acceSBarry Smith }
7949b94acceSBarry Smith 
7954a2ae208SSatish Balay #undef __FUNCT__
7964a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction"
7979b94acceSBarry Smith /*@C
7989b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
7999b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
8009b94acceSBarry Smith    equations.
8019b94acceSBarry Smith 
802fee21e36SBarry Smith    Collective on SNES
803fee21e36SBarry Smith 
804c7afd0dbSLois Curfman McInnes    Input Parameters:
805c7afd0dbSLois Curfman McInnes +  snes - the SNES context
806c7afd0dbSLois Curfman McInnes .  func - function evaluation routine
807c7afd0dbSLois Curfman McInnes .  r - vector to store function value
808c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
809c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
8109b94acceSBarry Smith 
811c7afd0dbSLois Curfman McInnes    Calling sequence of func:
8128d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
813c7afd0dbSLois Curfman McInnes 
814313e4042SLois Curfman McInnes .  f - function vector
815c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
8169b94acceSBarry Smith 
8179b94acceSBarry Smith    Notes:
8189b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
8199b94acceSBarry Smith $      f'(x) x = -f(x),
820c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
8219b94acceSBarry Smith 
82236851e7fSLois Curfman McInnes    Level: beginner
82336851e7fSLois Curfman McInnes 
8249b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
8259b94acceSBarry Smith 
826a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
8279b94acceSBarry Smith @*/
82863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx)
8299b94acceSBarry Smith {
8303a40ed3dSBarry Smith   PetscFunctionBegin;
8314482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
8324482741eSBarry Smith   PetscValidHeaderSpecific(r,VEC_COOKIE,2);
833c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,r,2);
834184914b5SBarry Smith 
835e7788613SBarry Smith   snes->ops->computefunction = func;
8369b94acceSBarry Smith   snes->vec_func             = snes->vec_func_always = r;
8379b94acceSBarry Smith   snes->funP                 = ctx;
8383a40ed3dSBarry Smith   PetscFunctionReturn(0);
8399b94acceSBarry Smith }
8409b94acceSBarry Smith 
8413ab0aad5SBarry Smith /* --------------------------------------------------------------- */
8423ab0aad5SBarry Smith #undef __FUNCT__
8433ab0aad5SBarry Smith #define __FUNCT__ "SNESSetRhs"
8443ab0aad5SBarry Smith /*@C
8453ab0aad5SBarry Smith    SNESSetRhs - Sets the vector for solving F(x) = rhs. If rhs is not set
8463ab0aad5SBarry Smith    it assumes a zero right hand side.
8473ab0aad5SBarry Smith 
8483ab0aad5SBarry Smith    Collective on SNES
8493ab0aad5SBarry Smith 
8503ab0aad5SBarry Smith    Input Parameters:
8513ab0aad5SBarry Smith +  snes - the SNES context
8523ab0aad5SBarry Smith -  rhs - the right hand side vector or PETSC_NULL for a zero right hand side
8533ab0aad5SBarry Smith 
8543ab0aad5SBarry Smith    Level: intermediate
8553ab0aad5SBarry Smith 
8563ab0aad5SBarry Smith .keywords: SNES, nonlinear, set, function, right hand side
8573ab0aad5SBarry Smith 
8581096aae1SMatthew Knepley .seealso: SNESGetRhs(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
8593ab0aad5SBarry Smith @*/
86063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetRhs(SNES snes,Vec rhs)
8613ab0aad5SBarry Smith {
862dfbe8321SBarry Smith   PetscErrorCode ierr;
8633ab0aad5SBarry Smith 
8643ab0aad5SBarry Smith   PetscFunctionBegin;
8653ab0aad5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
8663ab0aad5SBarry Smith   if (rhs) {
8673ab0aad5SBarry Smith     PetscValidHeaderSpecific(rhs,VEC_COOKIE,2);
8683ab0aad5SBarry Smith     PetscCheckSameComm(snes,1,rhs,2);
8693ab0aad5SBarry Smith     ierr = PetscObjectReference((PetscObject)rhs);CHKERRQ(ierr);
8703ab0aad5SBarry Smith   }
8713ab0aad5SBarry Smith   if (snes->afine) {
8723ab0aad5SBarry Smith     ierr = VecDestroy(snes->afine);CHKERRQ(ierr);
8733ab0aad5SBarry Smith   }
8743ab0aad5SBarry Smith   snes->afine = rhs;
8753ab0aad5SBarry Smith   PetscFunctionReturn(0);
8763ab0aad5SBarry Smith }
8773ab0aad5SBarry Smith 
8784a2ae208SSatish Balay #undef __FUNCT__
8791096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs"
8801096aae1SMatthew Knepley /*@C
8811096aae1SMatthew Knepley    SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set
8821096aae1SMatthew Knepley    it assumes a zero right hand side.
8831096aae1SMatthew Knepley 
8841096aae1SMatthew Knepley    Collective on SNES
8851096aae1SMatthew Knepley 
8861096aae1SMatthew Knepley    Input Parameter:
8871096aae1SMatthew Knepley .  snes - the SNES context
8881096aae1SMatthew Knepley 
8891096aae1SMatthew Knepley    Output Parameter:
8901096aae1SMatthew Knepley .  rhs - the right hand side vector or PETSC_NULL for a zero right hand side
8911096aae1SMatthew Knepley 
8921096aae1SMatthew Knepley    Level: intermediate
8931096aae1SMatthew Knepley 
8941096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side
8951096aae1SMatthew Knepley 
8961096aae1SMatthew Knepley .seealso: SNESSetRhs(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
8971096aae1SMatthew Knepley @*/
8981096aae1SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetRhs(SNES snes,Vec *rhs)
8991096aae1SMatthew Knepley {
9001096aae1SMatthew Knepley   PetscFunctionBegin;
9011096aae1SMatthew Knepley   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
9021096aae1SMatthew Knepley   PetscValidPointer(rhs,2);
9031096aae1SMatthew Knepley   *rhs = snes->afine;
9041096aae1SMatthew Knepley   PetscFunctionReturn(0);
9051096aae1SMatthew Knepley }
9061096aae1SMatthew Knepley 
9071096aae1SMatthew Knepley #undef __FUNCT__
9084a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction"
9099b94acceSBarry Smith /*@
91036851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
9119b94acceSBarry Smith                          SNESSetFunction().
9129b94acceSBarry Smith 
913c7afd0dbSLois Curfman McInnes    Collective on SNES
914c7afd0dbSLois Curfman McInnes 
9159b94acceSBarry Smith    Input Parameters:
916c7afd0dbSLois Curfman McInnes +  snes - the SNES context
917c7afd0dbSLois Curfman McInnes -  x - input vector
9189b94acceSBarry Smith 
9199b94acceSBarry Smith    Output Parameter:
9203638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
9219b94acceSBarry Smith 
9221bffabb2SLois Curfman McInnes    Notes:
92336851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
92436851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
92536851e7fSLois Curfman McInnes    themselves.
92636851e7fSLois Curfman McInnes 
92736851e7fSLois Curfman McInnes    Level: developer
92836851e7fSLois Curfman McInnes 
9299b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
9309b94acceSBarry Smith 
931a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
9329b94acceSBarry Smith @*/
93363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeFunction(SNES snes,Vec x,Vec y)
9349b94acceSBarry Smith {
935dfbe8321SBarry Smith   PetscErrorCode ierr;
9369b94acceSBarry Smith 
9373a40ed3dSBarry Smith   PetscFunctionBegin;
9384482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
9394482741eSBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE,2);
9404482741eSBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE,3);
941c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,x,2);
942c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,3);
943184914b5SBarry Smith 
944d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
945e7788613SBarry Smith   if (snes->ops->computefunction) {
946d64ed03dSBarry Smith     PetscStackPush("SNES user function");
947e9a2bbcdSBarry Smith     CHKMEMQ;
948e7788613SBarry Smith     ierr = (*snes->ops->computefunction)(snes,x,y,snes->funP);
949e9a2bbcdSBarry Smith     CHKMEMQ;
950d64ed03dSBarry Smith     PetscStackPop;
951d5e45103SBarry Smith     if (PetscExceptionValue(ierr)) {
95219717074SBarry Smith       PetscErrorCode pierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(pierr);
95319717074SBarry Smith     }
954d5e45103SBarry Smith     CHKERRQ(ierr);
9551096aae1SMatthew Knepley   } else if (snes->afine) {
9561096aae1SMatthew Knepley     ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr);
9571096aae1SMatthew Knepley   } else {
9581096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() before SNESComputeFunction(), likely called from SNESSolve().");
9591096aae1SMatthew Knepley   }
9603ab0aad5SBarry Smith   if (snes->afine) {
961016dedfbSBarry Smith     ierr = VecAXPY(y,-1.0,snes->afine);CHKERRQ(ierr);
9623ab0aad5SBarry Smith   }
963ae3c334cSLois Curfman McInnes   snes->nfuncs++;
964d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
9653a40ed3dSBarry Smith   PetscFunctionReturn(0);
9669b94acceSBarry Smith }
9679b94acceSBarry Smith 
9684a2ae208SSatish Balay #undef __FUNCT__
9694a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian"
97062fef451SLois Curfman McInnes /*@
97162fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
97262fef451SLois Curfman McInnes    set with SNESSetJacobian().
97362fef451SLois Curfman McInnes 
974c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
975c7afd0dbSLois Curfman McInnes 
97662fef451SLois Curfman McInnes    Input Parameters:
977c7afd0dbSLois Curfman McInnes +  snes - the SNES context
978c7afd0dbSLois Curfman McInnes -  x - input vector
97962fef451SLois Curfman McInnes 
98062fef451SLois Curfman McInnes    Output Parameters:
981c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
98262fef451SLois Curfman McInnes .  B - optional preconditioning matrix
9832b668275SBarry Smith -  flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER)
984fee21e36SBarry Smith 
98562fef451SLois Curfman McInnes    Notes:
98662fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
98762fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
98862fef451SLois Curfman McInnes 
98994b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
990dc5a77f8SLois Curfman McInnes    flag parameter.
99162fef451SLois Curfman McInnes 
99236851e7fSLois Curfman McInnes    Level: developer
99336851e7fSLois Curfman McInnes 
99462fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
99562fef451SLois Curfman McInnes 
9962b668275SBarry Smith .seealso:  SNESSetJacobian(), KSPSetOperators(), MatStructure
99762fef451SLois Curfman McInnes @*/
99863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
9999b94acceSBarry Smith {
1000dfbe8321SBarry Smith   PetscErrorCode ierr;
10013a40ed3dSBarry Smith 
10023a40ed3dSBarry Smith   PetscFunctionBegin;
10034482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
10044482741eSBarry Smith   PetscValidHeaderSpecific(X,VEC_COOKIE,2);
10054482741eSBarry Smith   PetscValidPointer(flg,5);
1006c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,X,2);
1007e7788613SBarry Smith   if (!snes->ops->computejacobian) PetscFunctionReturn(0);
1008d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
1009c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
1010d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
1011dc67937bSBarry Smith   CHKMEMQ;
1012e7788613SBarry Smith   ierr = (*snes->ops->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
1013dc67937bSBarry Smith   CHKMEMQ;
1014d64ed03dSBarry Smith   PetscStackPop;
1015d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
10166d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
10176ce558aeSBarry Smith   /* PetscValidHeaderSpecific(*A,MAT_COOKIE,3);
10186ce558aeSBarry Smith     PetscValidHeaderSpecific(*B,MAT_COOKIE,4);   */
10193a40ed3dSBarry Smith   PetscFunctionReturn(0);
10209b94acceSBarry Smith }
10219b94acceSBarry Smith 
10224a2ae208SSatish Balay #undef __FUNCT__
10234a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian"
10249b94acceSBarry Smith /*@C
10259b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1026044dda88SLois Curfman McInnes    location to store the matrix.
10279b94acceSBarry Smith 
1028c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1029c7afd0dbSLois Curfman McInnes 
10309b94acceSBarry Smith    Input Parameters:
1031c7afd0dbSLois Curfman McInnes +  snes - the SNES context
10329b94acceSBarry Smith .  A - Jacobian matrix
10339b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
10349b94acceSBarry Smith .  func - Jacobian evaluation routine
1035c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
10362cd2dfdcSLois Curfman McInnes          Jacobian evaluation routine (may be PETSC_NULL)
10379b94acceSBarry Smith 
10389b94acceSBarry Smith    Calling sequence of func:
10398d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
10409b94acceSBarry Smith 
1041c7afd0dbSLois Curfman McInnes +  x - input vector
10429b94acceSBarry Smith .  A - Jacobian matrix
10439b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1044ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
10452b668275SBarry Smith    structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER
1046c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
10479b94acceSBarry Smith 
10489b94acceSBarry Smith    Notes:
104994b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
10502cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1051ac21db08SLois Curfman McInnes 
1052ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
10539b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
10549b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
10559b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
10569b94acceSBarry Smith    throughout the global iterations.
10579b94acceSBarry Smith 
105836851e7fSLois Curfman McInnes    Level: beginner
105936851e7fSLois Curfman McInnes 
10609b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
10619b94acceSBarry Smith 
10622b668275SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatSNESMFComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure
10639b94acceSBarry Smith @*/
106463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
10659b94acceSBarry Smith {
1066dfbe8321SBarry Smith   PetscErrorCode ierr;
10673a7fca6bSBarry Smith 
10683a40ed3dSBarry Smith   PetscFunctionBegin;
10694482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
10704482741eSBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_COOKIE,2);
10714482741eSBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_COOKIE,3);
1072c9780b6fSBarry Smith   if (A) PetscCheckSameComm(snes,1,A,2);
1073c9780b6fSBarry Smith   if (B) PetscCheckSameComm(snes,1,B,2);
1074e7788613SBarry Smith    if (func) snes->ops->computejacobian = func;
10753a7fca6bSBarry Smith    if (ctx)  snes->jacP                 = ctx;
10763a7fca6bSBarry Smith   if (A) {
10773a7fca6bSBarry Smith     if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
10789b94acceSBarry Smith      snes->jacobian = A;
10793a7fca6bSBarry Smith     ierr           = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
10803a7fca6bSBarry Smith   }
10813a7fca6bSBarry Smith   if (B) {
10823a7fca6bSBarry Smith      if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
10839b94acceSBarry Smith      snes->jacobian_pre = B;
10843a7fca6bSBarry Smith     ierr               = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
10853a7fca6bSBarry Smith   }
108669a612a9SBarry Smith    ierr = KSPSetOperators(snes->ksp,A,B,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
10873a40ed3dSBarry Smith    PetscFunctionReturn(0);
10889b94acceSBarry Smith }
108962fef451SLois Curfman McInnes 
10904a2ae208SSatish Balay #undef __FUNCT__
10914a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian"
1092c2aafc4cSSatish Balay /*@C
1093b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1094b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1095b4fd4287SBarry Smith 
1096c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1097c7afd0dbSLois Curfman McInnes 
1098b4fd4287SBarry Smith    Input Parameter:
1099b4fd4287SBarry Smith .  snes - the nonlinear solver context
1100b4fd4287SBarry Smith 
1101b4fd4287SBarry Smith    Output Parameters:
1102c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1103b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
110470e92668SMatthew Knepley .  func - location to put Jacobian function (or PETSC_NULL)
110570e92668SMatthew Knepley -  ctx - location to stash Jacobian ctx (or PETSC_NULL)
1106fee21e36SBarry Smith 
110736851e7fSLois Curfman McInnes    Level: advanced
110836851e7fSLois Curfman McInnes 
1109b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1110b4fd4287SBarry Smith @*/
111170e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx)
1112b4fd4287SBarry Smith {
11133a40ed3dSBarry Smith   PetscFunctionBegin;
11144482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1115b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
1116b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
1117e7788613SBarry Smith   if (func) *func = snes->ops->computejacobian;
111870e92668SMatthew Knepley   if (ctx)  *ctx  = snes->jacP;
11193a40ed3dSBarry Smith   PetscFunctionReturn(0);
1120b4fd4287SBarry Smith }
1121b4fd4287SBarry Smith 
11229b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
112363dd3a1aSKris Buschelman EXTERN PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*);
11249b94acceSBarry Smith 
11254a2ae208SSatish Balay #undef __FUNCT__
11264a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp"
11279b94acceSBarry Smith /*@
11289b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1129272ac6f2SLois Curfman McInnes    of a nonlinear solver.
11309b94acceSBarry Smith 
1131fee21e36SBarry Smith    Collective on SNES
1132fee21e36SBarry Smith 
1133c7afd0dbSLois Curfman McInnes    Input Parameters:
113470e92668SMatthew Knepley .  snes - the SNES context
1135c7afd0dbSLois Curfman McInnes 
1136272ac6f2SLois Curfman McInnes    Notes:
1137272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1138272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1139272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1140272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1141272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1142272ac6f2SLois Curfman McInnes 
114336851e7fSLois Curfman McInnes    Level: advanced
114436851e7fSLois Curfman McInnes 
11459b94acceSBarry Smith .keywords: SNES, nonlinear, setup
11469b94acceSBarry Smith 
11479b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
11489b94acceSBarry Smith @*/
114970e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUp(SNES snes)
11509b94acceSBarry Smith {
1151dfbe8321SBarry Smith   PetscErrorCode ierr;
11524b27c08aSLois Curfman McInnes   PetscTruth     flg, iseqtr;
11533a40ed3dSBarry Smith 
11543a40ed3dSBarry Smith   PetscFunctionBegin;
11554482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
11564dc4c822SBarry Smith   if (snes->setupcalled) PetscFunctionReturn(0);
11579b94acceSBarry Smith 
1158b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_mf_operator",&flg);CHKERRQ(ierr);
1159c1f60f51SBarry Smith   /*
1160c1f60f51SBarry Smith       This version replaces the user provided Jacobian matrix with a
1161dfa02198SLois Curfman McInnes       matrix-free version but still employs the user-provided preconditioner matrix
1162c1f60f51SBarry Smith   */
1163c1f60f51SBarry Smith   if (flg) {
1164c1f60f51SBarry Smith     Mat J;
11655a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
11665a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
1167ae15b995SBarry Smith     ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr);
11683a7fca6bSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
11693a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
1170c1f60f51SBarry Smith   }
117145fc7adcSBarry Smith 
117203c60df9SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_MAT_SINGLE) && !defined(PETSC_USE_LONG_DOUBLE) && !defined(PETSC_USE_INT)
117345fc7adcSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_mf_operator2",&flg);CHKERRQ(ierr);
117445fc7adcSBarry Smith   if (flg) {
117545fc7adcSBarry Smith     Mat J;
117645fc7adcSBarry Smith     ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_sol,&J);CHKERRQ(ierr);
117745fc7adcSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
117845fc7adcSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
117945fc7adcSBarry Smith   }
118032a4b47aSMatthew Knepley #endif
118145fc7adcSBarry Smith 
1182b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_mf",&flg);CHKERRQ(ierr);
1183c1f60f51SBarry Smith   /*
1184dfa02198SLois Curfman McInnes       This version replaces both the user-provided Jacobian and the user-
1185c1f60f51SBarry Smith       provided preconditioner matrix with the default matrix free version.
1186c1f60f51SBarry Smith    */
118731615d04SLois Curfman McInnes   if (flg) {
1188272ac6f2SLois Curfman McInnes     Mat  J;
1189b5d62d44SBarry Smith     KSP ksp;
119094b7f48cSBarry Smith     PC   pc;
1191f3ef73ceSBarry Smith 
11925a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
11933a7fca6bSBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
1194ae15b995SBarry Smith     ierr = PetscInfo(snes,"Setting default matrix-free operator and preconditioner routines;\nThat is no preconditioner is being used.\n");CHKERRQ(ierr);
11953a7fca6bSBarry Smith     ierr = SNESSetJacobian(snes,J,J,MatSNESMFComputeJacobian,snes->funP);CHKERRQ(ierr);
11963a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
11973a7fca6bSBarry Smith 
1198f3ef73ceSBarry Smith     /* force no preconditioner */
119994b7f48cSBarry Smith     ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
1200b5d62d44SBarry Smith     ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
1201a9815358SBarry Smith     ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&flg);CHKERRQ(ierr);
1202a9815358SBarry Smith     if (!flg) {
1203f3ef73ceSBarry Smith       ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
1204272ac6f2SLois Curfman McInnes     }
1205a9815358SBarry Smith   }
1206f3ef73ceSBarry Smith 
12071096aae1SMatthew Knepley   if (!snes->vec_func && !snes->afine) {
12081096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
12091096aae1SMatthew Knepley   }
1210e7788613SBarry Smith   if (!snes->ops->computefunction && !snes->afine) {
12111096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
12121096aae1SMatthew Knepley   }
121329bbc08cSBarry Smith   if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetJacobian() first \n or use -snes_mf option");
1214a8c6a408SBarry Smith   if (snes->vec_func == snes->vec_sol) {
121529bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
1216a8c6a408SBarry Smith   }
1217a703fe33SLois Curfman McInnes 
1218a703fe33SLois Curfman McInnes   /* Set the KSP stopping criterion to use the Eisenstat-Walker method */
12194b27c08aSLois Curfman McInnes   ierr = PetscTypeCompare((PetscObject)snes,SNESTR,&iseqtr);CHKERRQ(ierr);
12206831982aSBarry Smith   if (snes->ksp_ewconv && !iseqtr) {
122194b7f48cSBarry Smith     KSP ksp;
122294b7f48cSBarry Smith     ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
1223186905e3SBarry Smith     ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private,snes);CHKERRQ(ierr);
1224a703fe33SLois Curfman McInnes   }
12254b27c08aSLois Curfman McInnes 
1226e7788613SBarry Smith   if (snes->ops->setup) {ierr = (*snes->ops->setup)(snes);CHKERRQ(ierr);}
12277aaed0d8SBarry Smith   snes->setupcalled = PETSC_TRUE;
12283a40ed3dSBarry Smith   PetscFunctionReturn(0);
12299b94acceSBarry Smith }
12309b94acceSBarry Smith 
12314a2ae208SSatish Balay #undef __FUNCT__
12324a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
123352baeb72SSatish Balay /*@
12349b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
12359b94acceSBarry Smith    with SNESCreate().
12369b94acceSBarry Smith 
1237c7afd0dbSLois Curfman McInnes    Collective on SNES
1238c7afd0dbSLois Curfman McInnes 
12399b94acceSBarry Smith    Input Parameter:
12409b94acceSBarry Smith .  snes - the SNES context
12419b94acceSBarry Smith 
124236851e7fSLois Curfman McInnes    Level: beginner
124336851e7fSLois Curfman McInnes 
12449b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
12459b94acceSBarry Smith 
124663a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
12479b94acceSBarry Smith @*/
124863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDestroy(SNES snes)
12499b94acceSBarry Smith {
12506849ba73SBarry Smith   PetscErrorCode ierr;
12513a40ed3dSBarry Smith 
12523a40ed3dSBarry Smith   PetscFunctionBegin;
12534482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
12543a40ed3dSBarry Smith   if (--snes->refct > 0) PetscFunctionReturn(0);
1255d4bb536fSBarry Smith 
1256be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
12570f5bd95cSBarry Smith   ierr = PetscObjectDepublish(snes);CHKERRQ(ierr);
1258be0abb6dSBarry Smith 
1259e7788613SBarry Smith   if (snes->ops->destroy) {ierr = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr);}
126005b42c5fSBarry Smith   ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);
12613a7fca6bSBarry Smith   if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
12623a7fca6bSBarry Smith   if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
12633ab0aad5SBarry Smith   if (snes->afine) {ierr = VecDestroy(snes->afine);CHKERRQ(ierr);}
126494b7f48cSBarry Smith   ierr = KSPDestroy(snes->ksp);CHKERRQ(ierr);
1265522c5e43SBarry Smith   if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);}
1266d952e501SBarry Smith   ierr = SNESClearMonitor(snes);CHKERRQ(ierr);
1267a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr);
12683a40ed3dSBarry Smith   PetscFunctionReturn(0);
12699b94acceSBarry Smith }
12709b94acceSBarry Smith 
12719b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
12729b94acceSBarry Smith 
12734a2ae208SSatish Balay #undef __FUNCT__
12744a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
12759b94acceSBarry Smith /*@
1276d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
12779b94acceSBarry Smith 
1278c7afd0dbSLois Curfman McInnes    Collective on SNES
1279c7afd0dbSLois Curfman McInnes 
12809b94acceSBarry Smith    Input Parameters:
1281c7afd0dbSLois Curfman McInnes +  snes - the SNES context
128270441072SBarry Smith .  abstol - absolute convergence tolerance
128333174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
128433174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
128533174efeSLois Curfman McInnes            of the change in the solution between steps
128633174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1287c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1288fee21e36SBarry Smith 
128933174efeSLois Curfman McInnes    Options Database Keys:
129070441072SBarry Smith +    -snes_atol <abstol> - Sets abstol
1291c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1292c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1293c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1294c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
12959b94acceSBarry Smith 
1296d7a720efSLois Curfman McInnes    Notes:
12979b94acceSBarry Smith    The default maximum number of iterations is 50.
12989b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
12999b94acceSBarry Smith 
130036851e7fSLois Curfman McInnes    Level: intermediate
130136851e7fSLois Curfman McInnes 
130233174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
13039b94acceSBarry Smith 
13042492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance()
13059b94acceSBarry Smith @*/
130663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf)
13079b94acceSBarry Smith {
13083a40ed3dSBarry Smith   PetscFunctionBegin;
13094482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
131070441072SBarry Smith   if (abstol != PETSC_DEFAULT)  snes->abstol      = abstol;
1311d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1312d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1313d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1314d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
13153a40ed3dSBarry Smith   PetscFunctionReturn(0);
13169b94acceSBarry Smith }
13179b94acceSBarry Smith 
13184a2ae208SSatish Balay #undef __FUNCT__
13194a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
13209b94acceSBarry Smith /*@
132133174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
132233174efeSLois Curfman McInnes 
1323c7afd0dbSLois Curfman McInnes    Not Collective
1324c7afd0dbSLois Curfman McInnes 
132533174efeSLois Curfman McInnes    Input Parameters:
1326c7afd0dbSLois Curfman McInnes +  snes - the SNES context
132770441072SBarry Smith .  abstol - absolute convergence tolerance
132833174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
132933174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
133033174efeSLois Curfman McInnes            of the change in the solution between steps
133133174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1332c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1333fee21e36SBarry Smith 
133433174efeSLois Curfman McInnes    Notes:
133533174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
133633174efeSLois Curfman McInnes 
133736851e7fSLois Curfman McInnes    Level: intermediate
133836851e7fSLois Curfman McInnes 
133933174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
134033174efeSLois Curfman McInnes 
134133174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
134233174efeSLois Curfman McInnes @*/
134363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetTolerances(SNES snes,PetscReal *abstol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf)
134433174efeSLois Curfman McInnes {
13453a40ed3dSBarry Smith   PetscFunctionBegin;
13464482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
134770441072SBarry Smith   if (abstol)  *abstol  = snes->abstol;
134833174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
134933174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
135033174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
135133174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
13523a40ed3dSBarry Smith   PetscFunctionReturn(0);
135333174efeSLois Curfman McInnes }
135433174efeSLois Curfman McInnes 
13554a2ae208SSatish Balay #undef __FUNCT__
13564a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
135733174efeSLois Curfman McInnes /*@
13589b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
13599b94acceSBarry Smith 
1360fee21e36SBarry Smith    Collective on SNES
1361fee21e36SBarry Smith 
1362c7afd0dbSLois Curfman McInnes    Input Parameters:
1363c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1364c7afd0dbSLois Curfman McInnes -  tol - tolerance
1365c7afd0dbSLois Curfman McInnes 
13669b94acceSBarry Smith    Options Database Key:
1367c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
13689b94acceSBarry Smith 
136936851e7fSLois Curfman McInnes    Level: intermediate
137036851e7fSLois Curfman McInnes 
13719b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
13729b94acceSBarry Smith 
13732492ecdbSBarry Smith .seealso: SNESSetTolerances()
13749b94acceSBarry Smith @*/
137563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
13769b94acceSBarry Smith {
13773a40ed3dSBarry Smith   PetscFunctionBegin;
13784482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
13799b94acceSBarry Smith   snes->deltatol = tol;
13803a40ed3dSBarry Smith   PetscFunctionReturn(0);
13819b94acceSBarry Smith }
13829b94acceSBarry Smith 
1383df9fa365SBarry Smith /*
1384df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1385df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1386df9fa365SBarry Smith    macros instead of functions
1387df9fa365SBarry Smith */
13884a2ae208SSatish Balay #undef __FUNCT__
13894a2ae208SSatish Balay #define __FUNCT__ "SNESLGMonitor"
139063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESLGMonitor(SNES snes,PetscInt it,PetscReal norm,void *ctx)
1391ce1608b8SBarry Smith {
1392dfbe8321SBarry Smith   PetscErrorCode ierr;
1393ce1608b8SBarry Smith 
1394ce1608b8SBarry Smith   PetscFunctionBegin;
13954482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1396ce1608b8SBarry Smith   ierr = KSPLGMonitor((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1397ce1608b8SBarry Smith   PetscFunctionReturn(0);
1398ce1608b8SBarry Smith }
1399ce1608b8SBarry Smith 
14004a2ae208SSatish Balay #undef __FUNCT__
14014a2ae208SSatish Balay #define __FUNCT__ "SNESLGMonitorCreate"
140263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESLGMonitorCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1403df9fa365SBarry Smith {
1404dfbe8321SBarry Smith   PetscErrorCode ierr;
1405df9fa365SBarry Smith 
1406df9fa365SBarry Smith   PetscFunctionBegin;
1407df9fa365SBarry Smith   ierr = KSPLGMonitorCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1408df9fa365SBarry Smith   PetscFunctionReturn(0);
1409df9fa365SBarry Smith }
1410df9fa365SBarry Smith 
14114a2ae208SSatish Balay #undef __FUNCT__
14124a2ae208SSatish Balay #define __FUNCT__ "SNESLGMonitorDestroy"
141363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESLGMonitorDestroy(PetscDrawLG draw)
1414df9fa365SBarry Smith {
1415dfbe8321SBarry Smith   PetscErrorCode ierr;
1416df9fa365SBarry Smith 
1417df9fa365SBarry Smith   PetscFunctionBegin;
1418df9fa365SBarry Smith   ierr = KSPLGMonitorDestroy(draw);CHKERRQ(ierr);
1419df9fa365SBarry Smith   PetscFunctionReturn(0);
1420df9fa365SBarry Smith }
1421df9fa365SBarry Smith 
14229b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
14239b94acceSBarry Smith 
14244a2ae208SSatish Balay #undef __FUNCT__
14254a2ae208SSatish Balay #define __FUNCT__ "SNESSetMonitor"
14269b94acceSBarry Smith /*@C
14275cd90555SBarry Smith    SNESSetMonitor - Sets an ADDITIONAL function that is to be used at every
14289b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
14299b94acceSBarry Smith    progress.
14309b94acceSBarry Smith 
1431fee21e36SBarry Smith    Collective on SNES
1432fee21e36SBarry Smith 
1433c7afd0dbSLois Curfman McInnes    Input Parameters:
1434c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1435c7afd0dbSLois Curfman McInnes .  func - monitoring routine
1436b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
1437e8105e01SRichard Katz           monitor routine (use PETSC_NULL if no context is desired)
1438b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1439b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
14409b94acceSBarry Smith 
1441c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1442a7cc72afSBarry Smith $     int func(SNES snes,PetscInt its, PetscReal norm,void *mctx)
1443c7afd0dbSLois Curfman McInnes 
1444c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1445c7afd0dbSLois Curfman McInnes .    its - iteration number
1446c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
144740a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
14489b94acceSBarry Smith 
14499665c990SLois Curfman McInnes    Options Database Keys:
1450c7afd0dbSLois Curfman McInnes +    -snes_monitor        - sets SNESDefaultMonitor()
1451c7afd0dbSLois Curfman McInnes .    -snes_xmonitor       - sets line graph monitor,
1452c7afd0dbSLois Curfman McInnes                             uses SNESLGMonitorCreate()
1453c7afd0dbSLois Curfman McInnes _    -snes_cancelmonitors - cancels all monitors that have
1454c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
1455c7afd0dbSLois Curfman McInnes                             calls to SNESSetMonitor(), but
1456c7afd0dbSLois Curfman McInnes                             does not cancel those set via
1457c7afd0dbSLois Curfman McInnes                             the options database.
14589665c990SLois Curfman McInnes 
1459639f9d9dSBarry Smith    Notes:
14606bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
14616bc08f3fSLois Curfman McInnes    SNESSetMonitor() multiple times; all will be called in the
14626bc08f3fSLois Curfman McInnes    order in which they were set.
1463639f9d9dSBarry Smith 
146436851e7fSLois Curfman McInnes    Level: intermediate
146536851e7fSLois Curfman McInnes 
14669b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
14679b94acceSBarry Smith 
14685cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESClearMonitor()
14699b94acceSBarry Smith @*/
147063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMonitor(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void*))
14719b94acceSBarry Smith {
14723a40ed3dSBarry Smith   PetscFunctionBegin;
14734482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1474639f9d9dSBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) {
147529bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1476639f9d9dSBarry Smith   }
1477639f9d9dSBarry Smith   snes->monitor[snes->numbermonitors]           = func;
1478b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
1479639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
14803a40ed3dSBarry Smith   PetscFunctionReturn(0);
14819b94acceSBarry Smith }
14829b94acceSBarry Smith 
14834a2ae208SSatish Balay #undef __FUNCT__
14844a2ae208SSatish Balay #define __FUNCT__ "SNESClearMonitor"
14855cd90555SBarry Smith /*@C
14865cd90555SBarry Smith    SNESClearMonitor - Clears all the monitor functions for a SNES object.
14875cd90555SBarry Smith 
1488c7afd0dbSLois Curfman McInnes    Collective on SNES
1489c7afd0dbSLois Curfman McInnes 
14905cd90555SBarry Smith    Input Parameters:
14915cd90555SBarry Smith .  snes - the SNES context
14925cd90555SBarry Smith 
14931a480d89SAdministrator    Options Database Key:
1494c7afd0dbSLois Curfman McInnes .  -snes_cancelmonitors - cancels all monitors that have been hardwired
1495c7afd0dbSLois Curfman McInnes     into a code by calls to SNESSetMonitor(), but does not cancel those
1496c7afd0dbSLois Curfman McInnes     set via the options database
14975cd90555SBarry Smith 
14985cd90555SBarry Smith    Notes:
14995cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
15005cd90555SBarry Smith 
150136851e7fSLois Curfman McInnes    Level: intermediate
150236851e7fSLois Curfman McInnes 
15035cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
15045cd90555SBarry Smith 
15055cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESSetMonitor()
15065cd90555SBarry Smith @*/
150763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESClearMonitor(SNES snes)
15085cd90555SBarry Smith {
1509d952e501SBarry Smith   PetscErrorCode ierr;
1510d952e501SBarry Smith   PetscInt       i;
1511d952e501SBarry Smith 
15125cd90555SBarry Smith   PetscFunctionBegin;
15134482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1514d952e501SBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
1515d952e501SBarry Smith     if (snes->monitordestroy[i]) {
1516d952e501SBarry Smith       ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr);
1517d952e501SBarry Smith     }
1518d952e501SBarry Smith   }
15195cd90555SBarry Smith   snes->numbermonitors = 0;
15205cd90555SBarry Smith   PetscFunctionReturn(0);
15215cd90555SBarry Smith }
15225cd90555SBarry Smith 
15234a2ae208SSatish Balay #undef __FUNCT__
15244a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
15259b94acceSBarry Smith /*@C
15269b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
15279b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
15289b94acceSBarry Smith 
1529fee21e36SBarry Smith    Collective on SNES
1530fee21e36SBarry Smith 
1531c7afd0dbSLois Curfman McInnes    Input Parameters:
1532c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1533c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
1534c7afd0dbSLois Curfman McInnes -  cctx - [optional] context for private data for the convergence routine
1535c7afd0dbSLois Curfman McInnes           (may be PETSC_NULL)
15369b94acceSBarry Smith 
1537c7afd0dbSLois Curfman McInnes    Calling sequence of func:
153806ee9f85SBarry Smith $     PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
1539c7afd0dbSLois Curfman McInnes 
1540c7afd0dbSLois Curfman McInnes +    snes - the SNES context
154106ee9f85SBarry Smith .    it - current iteration (0 is the first and is before any Newton step)
1542c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
1543184914b5SBarry Smith .    reason - reason for convergence/divergence
1544c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
15454b27c08aSLois Curfman McInnes .    gnorm - 2-norm of current step
15464b27c08aSLois Curfman McInnes -    f - 2-norm of function
15479b94acceSBarry Smith 
154836851e7fSLois Curfman McInnes    Level: advanced
154936851e7fSLois Curfman McInnes 
15509b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
15519b94acceSBarry Smith 
15524b27c08aSLois Curfman McInnes .seealso: SNESConverged_LS(), SNESConverged_TR()
15539b94acceSBarry Smith @*/
155406ee9f85SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx)
15559b94acceSBarry Smith {
15563a40ed3dSBarry Smith   PetscFunctionBegin;
15574482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1558e7788613SBarry Smith   (snes)->ops->converged = func;
15599b94acceSBarry Smith   (snes)->cnvP           = cctx;
15603a40ed3dSBarry Smith   PetscFunctionReturn(0);
15619b94acceSBarry Smith }
15629b94acceSBarry Smith 
15634a2ae208SSatish Balay #undef __FUNCT__
15644a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
156552baeb72SSatish Balay /*@
1566184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
1567184914b5SBarry Smith 
1568184914b5SBarry Smith    Not Collective
1569184914b5SBarry Smith 
1570184914b5SBarry Smith    Input Parameter:
1571184914b5SBarry Smith .  snes - the SNES context
1572184914b5SBarry Smith 
1573184914b5SBarry Smith    Output Parameter:
1574e090d566SSatish Balay .  reason - negative value indicates diverged, positive value converged, see petscsnes.h or the
1575184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
1576184914b5SBarry Smith 
1577184914b5SBarry Smith    Level: intermediate
1578184914b5SBarry Smith 
1579184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
1580184914b5SBarry Smith 
1581184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
1582184914b5SBarry Smith 
15834b27c08aSLois Curfman McInnes .seealso: SNESSetConvergenceTest(), SNESConverged_LS(), SNESConverged_TR(), SNESConvergedReason
1584184914b5SBarry Smith @*/
158563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
1586184914b5SBarry Smith {
1587184914b5SBarry Smith   PetscFunctionBegin;
15884482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
15894482741eSBarry Smith   PetscValidPointer(reason,2);
1590184914b5SBarry Smith   *reason = snes->reason;
1591184914b5SBarry Smith   PetscFunctionReturn(0);
1592184914b5SBarry Smith }
1593184914b5SBarry Smith 
15944a2ae208SSatish Balay #undef __FUNCT__
15954a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
1596c9005455SLois Curfman McInnes /*@
1597c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
1598c9005455SLois Curfman McInnes 
1599fee21e36SBarry Smith    Collective on SNES
1600fee21e36SBarry Smith 
1601c7afd0dbSLois Curfman McInnes    Input Parameters:
1602c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
1603c7afd0dbSLois Curfman McInnes .  a   - array to hold history
1604cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
1605758f92a0SBarry Smith .  na  - size of a and its
160664731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
1607758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
1608c7afd0dbSLois Curfman McInnes 
1609c9005455SLois Curfman McInnes    Notes:
16104b27c08aSLois Curfman McInnes    If set, this array will contain the function norms computed
1611c9005455SLois Curfman McInnes    at each step.
1612c9005455SLois Curfman McInnes 
1613c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
1614c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
1615c9005455SLois Curfman McInnes    during the section of code that is being timed.
1616c9005455SLois Curfman McInnes 
161736851e7fSLois Curfman McInnes    Level: intermediate
161836851e7fSLois Curfman McInnes 
1619c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
1620758f92a0SBarry Smith 
162108405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
1622758f92a0SBarry Smith 
1623c9005455SLois Curfman McInnes @*/
162463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt *its,PetscInt na,PetscTruth reset)
1625c9005455SLois Curfman McInnes {
16263a40ed3dSBarry Smith   PetscFunctionBegin;
16274482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
16284482741eSBarry Smith   if (na) PetscValidScalarPointer(a,2);
1629c9005455SLois Curfman McInnes   snes->conv_hist       = a;
1630758f92a0SBarry Smith   snes->conv_hist_its   = its;
1631758f92a0SBarry Smith   snes->conv_hist_max   = na;
1632758f92a0SBarry Smith   snes->conv_hist_reset = reset;
1633758f92a0SBarry Smith   PetscFunctionReturn(0);
1634758f92a0SBarry Smith }
1635758f92a0SBarry Smith 
16364a2ae208SSatish Balay #undef __FUNCT__
16374a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
16380c4c9dddSBarry Smith /*@C
1639758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
1640758f92a0SBarry Smith 
1641758f92a0SBarry Smith    Collective on SNES
1642758f92a0SBarry Smith 
1643758f92a0SBarry Smith    Input Parameter:
1644758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
1645758f92a0SBarry Smith 
1646758f92a0SBarry Smith    Output Parameters:
1647758f92a0SBarry Smith .  a   - array to hold history
1648758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
1649758f92a0SBarry Smith          negative if not converged) for each solve.
1650758f92a0SBarry Smith -  na  - size of a and its
1651758f92a0SBarry Smith 
1652758f92a0SBarry Smith    Notes:
1653758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
1654758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
1655758f92a0SBarry Smith 
1656758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
1657758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
1658758f92a0SBarry Smith    during the section of code that is being timed.
1659758f92a0SBarry Smith 
1660758f92a0SBarry Smith    Level: intermediate
1661758f92a0SBarry Smith 
1662758f92a0SBarry Smith .keywords: SNES, get, convergence, history
1663758f92a0SBarry Smith 
1664758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
1665758f92a0SBarry Smith 
1666758f92a0SBarry Smith @*/
166763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na)
1668758f92a0SBarry Smith {
1669758f92a0SBarry Smith   PetscFunctionBegin;
16704482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1671758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
1672758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
1673758f92a0SBarry Smith   if (na) *na   = snes->conv_hist_len;
16743a40ed3dSBarry Smith   PetscFunctionReturn(0);
1675c9005455SLois Curfman McInnes }
1676c9005455SLois Curfman McInnes 
1677e74ef692SMatthew Knepley #undef __FUNCT__
1678e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
1679ac226902SBarry Smith /*@C
168076b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
16817e4bb74cSBarry Smith   at the beginning o every iteration of the nonlinear solve. Specifically
16827e4bb74cSBarry Smith   it is called just before the Jacobian is "evaluated".
168376b2cf59SMatthew Knepley 
168476b2cf59SMatthew Knepley   Collective on SNES
168576b2cf59SMatthew Knepley 
168676b2cf59SMatthew Knepley   Input Parameters:
168776b2cf59SMatthew Knepley . snes - The nonlinear solver context
168876b2cf59SMatthew Knepley . func - The function
168976b2cf59SMatthew Knepley 
169076b2cf59SMatthew Knepley   Calling sequence of func:
1691b5d30489SBarry Smith . func (SNES snes, PetscInt step);
169276b2cf59SMatthew Knepley 
169376b2cf59SMatthew Knepley . step - The current step of the iteration
169476b2cf59SMatthew Knepley 
169576b2cf59SMatthew Knepley   Level: intermediate
169676b2cf59SMatthew Knepley 
169776b2cf59SMatthew Knepley .keywords: SNES, update
1698b5d30489SBarry Smith 
169976b2cf59SMatthew Knepley .seealso SNESDefaultUpdate(), SNESSetRhsBC(), SNESSetSolutionBC()
170076b2cf59SMatthew Knepley @*/
170163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt))
170276b2cf59SMatthew Knepley {
170376b2cf59SMatthew Knepley   PetscFunctionBegin;
17044482741eSBarry Smith   PetscValidHeaderSpecific(snes, SNES_COOKIE,1);
1705e7788613SBarry Smith   snes->ops->update = func;
170676b2cf59SMatthew Knepley   PetscFunctionReturn(0);
170776b2cf59SMatthew Knepley }
170876b2cf59SMatthew Knepley 
1709e74ef692SMatthew Knepley #undef __FUNCT__
1710e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
171176b2cf59SMatthew Knepley /*@
171276b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
171376b2cf59SMatthew Knepley 
171476b2cf59SMatthew Knepley   Not collective
171576b2cf59SMatthew Knepley 
171676b2cf59SMatthew Knepley   Input Parameters:
171776b2cf59SMatthew Knepley . snes - The nonlinear solver context
171876b2cf59SMatthew Knepley . step - The current step of the iteration
171976b2cf59SMatthew Knepley 
1720205452f4SMatthew Knepley   Level: intermediate
1721205452f4SMatthew Knepley 
172276b2cf59SMatthew Knepley .keywords: SNES, update
172376b2cf59SMatthew Knepley .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultSolutionBC()
172476b2cf59SMatthew Knepley @*/
172563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultUpdate(SNES snes, PetscInt step)
172676b2cf59SMatthew Knepley {
172776b2cf59SMatthew Knepley   PetscFunctionBegin;
172876b2cf59SMatthew Knepley   PetscFunctionReturn(0);
172976b2cf59SMatthew Knepley }
173076b2cf59SMatthew Knepley 
17314a2ae208SSatish Balay #undef __FUNCT__
17324a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
17339b94acceSBarry Smith /*
17349b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
17359b94acceSBarry Smith    positive parameter delta.
17369b94acceSBarry Smith 
17379b94acceSBarry Smith     Input Parameters:
1738c7afd0dbSLois Curfman McInnes +   snes - the SNES context
17399b94acceSBarry Smith .   y - approximate solution of linear system
17409b94acceSBarry Smith .   fnorm - 2-norm of current function
1741c7afd0dbSLois Curfman McInnes -   delta - trust region size
17429b94acceSBarry Smith 
17439b94acceSBarry Smith     Output Parameters:
1744c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
17459b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
17469b94acceSBarry Smith     region, and exceeds zero otherwise.
1747c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
17489b94acceSBarry Smith 
17499b94acceSBarry Smith     Note:
17504b27c08aSLois Curfman McInnes     For non-trust region methods such as SNESLS, the parameter delta
17519b94acceSBarry Smith     is set to be the maximum allowable step size.
17529b94acceSBarry Smith 
17539b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
17549b94acceSBarry Smith */
1755dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
17569b94acceSBarry Smith {
1757064f8208SBarry Smith   PetscReal      nrm;
1758ea709b57SSatish Balay   PetscScalar    cnorm;
1759dfbe8321SBarry Smith   PetscErrorCode ierr;
17603a40ed3dSBarry Smith 
17613a40ed3dSBarry Smith   PetscFunctionBegin;
17624482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
17634482741eSBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE,2);
1764c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,2);
1765184914b5SBarry Smith 
1766064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
1767064f8208SBarry Smith   if (nrm > *delta) {
1768064f8208SBarry Smith      nrm = *delta/nrm;
1769064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
1770064f8208SBarry Smith      cnorm = nrm;
17712dcb1b2aSMatthew Knepley      ierr = VecScale(y,cnorm);CHKERRQ(ierr);
17729b94acceSBarry Smith      *ynorm = *delta;
17739b94acceSBarry Smith   } else {
17749b94acceSBarry Smith      *gpnorm = 0.0;
1775064f8208SBarry Smith      *ynorm = nrm;
17769b94acceSBarry Smith   }
17773a40ed3dSBarry Smith   PetscFunctionReturn(0);
17789b94acceSBarry Smith }
17799b94acceSBarry Smith 
17804a2ae208SSatish Balay #undef __FUNCT__
17814a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
17826ce558aeSBarry Smith /*@C
1783f69a0ea3SMatthew Knepley    SNESSolve - Solves a nonlinear system F(x) = b.
1784f69a0ea3SMatthew Knepley    Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX().
17859b94acceSBarry Smith 
1786c7afd0dbSLois Curfman McInnes    Collective on SNES
1787c7afd0dbSLois Curfman McInnes 
1788b2002411SLois Curfman McInnes    Input Parameters:
1789c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1790f69a0ea3SMatthew Knepley .  b - the constant part of the equation, or PETSC_NULL to use zero.
179170e92668SMatthew Knepley -  x - the solution vector, or PETSC_NULL if it was set with SNESSetSolution()
17929b94acceSBarry Smith 
1793b2002411SLois Curfman McInnes    Notes:
17948ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
17958ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
17968ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
17978ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
17988ddd3da0SLois Curfman McInnes 
179936851e7fSLois Curfman McInnes    Level: beginner
180036851e7fSLois Curfman McInnes 
18019b94acceSBarry Smith .keywords: SNES, nonlinear, solve
18029b94acceSBarry Smith 
180370e92668SMatthew Knepley .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian(), SNESSetRhs(), SNESSetSolution()
18049b94acceSBarry Smith @*/
1805f69a0ea3SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSolve(SNES snes,Vec b,Vec x)
18069b94acceSBarry Smith {
1807dfbe8321SBarry Smith   PetscErrorCode ierr;
1808f1af5d2fSBarry Smith   PetscTruth     flg;
1809eabae89aSBarry Smith   char           filename[PETSC_MAX_PATH_LEN];
1810eabae89aSBarry Smith   PetscViewer    viewer;
1811052efed2SBarry Smith 
18123a40ed3dSBarry Smith   PetscFunctionBegin;
18134482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1814e7788613SBarry Smith   if (!snes->ops->solve) SETERRQ(PETSC_ERR_ORDER,"SNESSetType() or SNESSetFromOptions() must be called before SNESSolve()");
181574637425SBarry Smith 
1816f69a0ea3SMatthew Knepley   if (b) {
1817f69a0ea3SMatthew Knepley     ierr = SNESSetRhs(snes, b); CHKERRQ(ierr);
18181096aae1SMatthew Knepley     if (!snes->vec_func) {
18191096aae1SMatthew Knepley       Vec r;
18201096aae1SMatthew Knepley 
18211096aae1SMatthew Knepley       ierr = VecDuplicate(b, &r); CHKERRQ(ierr);
18221096aae1SMatthew Knepley       ierr = SNESSetFunction(snes, r, PETSC_NULL, PETSC_NULL); CHKERRQ(ierr);
18231096aae1SMatthew Knepley     }
1824f69a0ea3SMatthew Knepley   }
182570e92668SMatthew Knepley   if (x) {
1826f69a0ea3SMatthew Knepley     PetscValidHeaderSpecific(x,VEC_COOKIE,3);
1827f69a0ea3SMatthew Knepley     PetscCheckSameComm(snes,1,x,3);
182870e92668SMatthew Knepley   } else {
182970e92668SMatthew Knepley     ierr = SNESGetSolution(snes, &x); CHKERRQ(ierr);
183070e92668SMatthew Knepley     if (!x) {
183170e92668SMatthew Knepley       ierr = VecDuplicate(snes->vec_func_always, &x); CHKERRQ(ierr);
183270e92668SMatthew Knepley     }
183370e92668SMatthew Knepley   }
183470e92668SMatthew Knepley   snes->vec_sol = snes->vec_sol_always = x;
183570e92668SMatthew Knepley   if (!snes->setupcalled) {
183670e92668SMatthew Knepley     ierr = SNESSetUp(snes);CHKERRQ(ierr);
183770e92668SMatthew Knepley   }
1838abc0a331SBarry Smith   if (snes->conv_hist_reset) snes->conv_hist_len = 0;
1839d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
184050ffb88aSMatthew Knepley   snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
1841d5e45103SBarry Smith 
1842e7788613SBarry Smith   ierr = PetscExceptionTry1((*(snes)->ops->solve)(snes),PETSC_ERR_ARG_DOMAIN);
1843d5e45103SBarry Smith   if (PetscExceptionValue(ierr)) {
184438f152feSBarry Smith     /* this means that a caller above me has also tryed this exception so I don't handle it here, pass it up */
184519717074SBarry Smith     PetscErrorCode pierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(pierr);
1846d5e45103SBarry Smith   } else if (PetscExceptionCaught(ierr,PETSC_ERR_ARG_DOMAIN)) {
1847d5e45103SBarry Smith     /* translate exception into SNES not converged reason */
1848d5e45103SBarry Smith     snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN;
184938f152feSBarry Smith     ierr = 0;
185038f152feSBarry Smith   }
185138f152feSBarry Smith   CHKERRQ(ierr);
1852d5e45103SBarry Smith 
1853d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
1854eabae89aSBarry Smith   ierr = PetscOptionsGetString(snes->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1855eabae89aSBarry Smith   if (flg && !PetscPreLoadingOn) {
1856eabae89aSBarry Smith     ierr = PetscViewerASCIIOpen(snes->comm,filename,&viewer);CHKERRQ(ierr);
1857eabae89aSBarry Smith     ierr = SNESView(snes,viewer);CHKERRQ(ierr);
1858eabae89aSBarry Smith     ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr);
1859eabae89aSBarry Smith   }
1860eabae89aSBarry Smith 
1861da9b6338SBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_test_local_min",&flg);CHKERRQ(ierr);
1862da9b6338SBarry Smith   if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); }
18635968eb51SBarry Smith   if (snes->printreason) {
18645968eb51SBarry Smith     if (snes->reason > 0) {
18659dcbbd2bSBarry Smith       ierr = PetscPrintf(snes->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
18665968eb51SBarry Smith     } else {
18679dcbbd2bSBarry Smith       ierr = PetscPrintf(snes->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
18685968eb51SBarry Smith     }
18695968eb51SBarry Smith   }
18705968eb51SBarry Smith 
18713a40ed3dSBarry Smith   PetscFunctionReturn(0);
18729b94acceSBarry Smith }
18739b94acceSBarry Smith 
18749b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
18759b94acceSBarry Smith 
18764a2ae208SSatish Balay #undef __FUNCT__
18774a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
187882bf6240SBarry Smith /*@C
18794b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
18809b94acceSBarry Smith 
1881fee21e36SBarry Smith    Collective on SNES
1882fee21e36SBarry Smith 
1883c7afd0dbSLois Curfman McInnes    Input Parameters:
1884c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1885454a90a3SBarry Smith -  type - a known method
1886c7afd0dbSLois Curfman McInnes 
1887c7afd0dbSLois Curfman McInnes    Options Database Key:
1888454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
1889c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
1890ae12b187SLois Curfman McInnes 
18919b94acceSBarry Smith    Notes:
1892e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
18934b27c08aSLois Curfman McInnes +    SNESLS - Newton's method with line search
1894c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
18954b27c08aSLois Curfman McInnes .    SNESTR - Newton's method with trust region
1896c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
18979b94acceSBarry Smith 
1898ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
1899ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
1900ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
1901ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
1902ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
1903ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
1904ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
1905ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
1906ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
1907b0a32e0cSBarry Smith   appropriate method.
190836851e7fSLois Curfman McInnes 
190936851e7fSLois Curfman McInnes   Level: intermediate
1910a703fe33SLois Curfman McInnes 
1911454a90a3SBarry Smith .keywords: SNES, set, type
1912435da068SBarry Smith 
1913435da068SBarry Smith .seealso: SNESType, SNESCreate()
1914435da068SBarry Smith 
19159b94acceSBarry Smith @*/
1916e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetType(SNES snes,SNESType type)
19179b94acceSBarry Smith {
1918dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(SNES);
19196831982aSBarry Smith   PetscTruth     match;
19203a40ed3dSBarry Smith 
19213a40ed3dSBarry Smith   PetscFunctionBegin;
19224482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
19234482741eSBarry Smith   PetscValidCharPointer(type,2);
192482bf6240SBarry Smith 
19256831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
19260f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
192782bf6240SBarry Smith 
192882bf6240SBarry Smith   if (snes->setupcalled) {
19294dc4c822SBarry Smith     snes->setupcalled = PETSC_FALSE;
1930e7788613SBarry Smith     ierr              = (*(snes)->ops->destroy)(snes);CHKERRQ(ierr);
193182bf6240SBarry Smith     snes->data        = 0;
193282bf6240SBarry Smith   }
19337d1a2b2bSBarry Smith 
19349b94acceSBarry Smith   /* Get the function pointers for the iterative method requested */
193582bf6240SBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
1936b9617806SBarry Smith   ierr =  PetscFListFind(snes->comm,SNESList,type,(void (**)(void)) &r);CHKERRQ(ierr);
1937958c9bccSBarry Smith   if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type);
193805b42c5fSBarry Smith   ierr = PetscFree(snes->data);CHKERRQ(ierr);
193982bf6240SBarry Smith   snes->data = 0;
19403a40ed3dSBarry Smith   ierr = (*r)(snes);CHKERRQ(ierr);
1941454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
19423a40ed3dSBarry Smith   PetscFunctionReturn(0);
19439b94acceSBarry Smith }
19449b94acceSBarry Smith 
1945a847f771SSatish Balay 
19469b94acceSBarry Smith /* --------------------------------------------------------------------- */
19474a2ae208SSatish Balay #undef __FUNCT__
19484a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
194952baeb72SSatish Balay /*@
19509b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
1951f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
19529b94acceSBarry Smith 
1953fee21e36SBarry Smith    Not Collective
1954fee21e36SBarry Smith 
195536851e7fSLois Curfman McInnes    Level: advanced
195636851e7fSLois Curfman McInnes 
19579b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
19589b94acceSBarry Smith 
19599b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
19609b94acceSBarry Smith @*/
196163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegisterDestroy(void)
19629b94acceSBarry Smith {
1963dfbe8321SBarry Smith   PetscErrorCode ierr;
196482bf6240SBarry Smith 
19653a40ed3dSBarry Smith   PetscFunctionBegin;
196682bf6240SBarry Smith   if (SNESList) {
1967b0a32e0cSBarry Smith     ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
196882bf6240SBarry Smith     SNESList = 0;
19699b94acceSBarry Smith   }
19704c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
19713a40ed3dSBarry Smith   PetscFunctionReturn(0);
19729b94acceSBarry Smith }
19739b94acceSBarry Smith 
19744a2ae208SSatish Balay #undef __FUNCT__
19754a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
19769b94acceSBarry Smith /*@C
19779a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
19789b94acceSBarry Smith 
1979c7afd0dbSLois Curfman McInnes    Not Collective
1980c7afd0dbSLois Curfman McInnes 
19819b94acceSBarry Smith    Input Parameter:
19824b0e389bSBarry Smith .  snes - nonlinear solver context
19839b94acceSBarry Smith 
19849b94acceSBarry Smith    Output Parameter:
19853a7fca6bSBarry Smith .  type - SNES method (a character string)
19869b94acceSBarry Smith 
198736851e7fSLois Curfman McInnes    Level: intermediate
198836851e7fSLois Curfman McInnes 
1989454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
19909b94acceSBarry Smith @*/
199163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetType(SNES snes,SNESType *type)
19929b94acceSBarry Smith {
19933a40ed3dSBarry Smith   PetscFunctionBegin;
19944482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
19954482741eSBarry Smith   PetscValidPointer(type,2);
1996454a90a3SBarry Smith   *type = snes->type_name;
19973a40ed3dSBarry Smith   PetscFunctionReturn(0);
19989b94acceSBarry Smith }
19999b94acceSBarry Smith 
20004a2ae208SSatish Balay #undef __FUNCT__
20014a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
200252baeb72SSatish Balay /*@
20039b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
20049b94acceSBarry Smith    stored.
20059b94acceSBarry Smith 
2006c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2007c7afd0dbSLois Curfman McInnes 
20089b94acceSBarry Smith    Input Parameter:
20099b94acceSBarry Smith .  snes - the SNES context
20109b94acceSBarry Smith 
20119b94acceSBarry Smith    Output Parameter:
20129b94acceSBarry Smith .  x - the solution
20139b94acceSBarry Smith 
201470e92668SMatthew Knepley    Level: intermediate
201536851e7fSLois Curfman McInnes 
20169b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
20179b94acceSBarry Smith 
201870e92668SMatthew Knepley .seealso: SNESSetSolution(), SNESGetFunction(), SNESGetSolutionUpdate()
20199b94acceSBarry Smith @*/
202063dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolution(SNES snes,Vec *x)
20219b94acceSBarry Smith {
20223a40ed3dSBarry Smith   PetscFunctionBegin;
20234482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
20244482741eSBarry Smith   PetscValidPointer(x,2);
20259b94acceSBarry Smith   *x = snes->vec_sol_always;
20263a40ed3dSBarry Smith   PetscFunctionReturn(0);
20279b94acceSBarry Smith }
20289b94acceSBarry Smith 
20294a2ae208SSatish Balay #undef __FUNCT__
203070e92668SMatthew Knepley #define __FUNCT__ "SNESSetSolution"
20317e4bb74cSBarry Smith /*@
203270e92668SMatthew Knepley    SNESSetSolution - Sets the vector where the approximate solution is stored.
203370e92668SMatthew Knepley 
203470e92668SMatthew Knepley    Not Collective, but Vec is parallel if SNES is parallel
203570e92668SMatthew Knepley 
203670e92668SMatthew Knepley    Input Parameters:
203770e92668SMatthew Knepley +  snes - the SNES context
203870e92668SMatthew Knepley -  x - the solution
203970e92668SMatthew Knepley 
204070e92668SMatthew Knepley    Output Parameter:
204170e92668SMatthew Knepley 
204270e92668SMatthew Knepley    Level: intermediate
204370e92668SMatthew Knepley 
204442219521SBarry Smith    Notes: this is not normally used, rather one simply calls SNESSolve() with
204542219521SBarry Smith           the appropriate solution vector.
204642219521SBarry Smith 
204770e92668SMatthew Knepley .keywords: SNES, nonlinear, set, solution
204870e92668SMatthew Knepley 
204970e92668SMatthew Knepley .seealso: SNESGetSolution(), SNESGetFunction(), SNESGetSolutionUpdate()
205070e92668SMatthew Knepley @*/
205170e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSetSolution(SNES snes,Vec x)
205270e92668SMatthew Knepley {
205370e92668SMatthew Knepley   PetscFunctionBegin;
205470e92668SMatthew Knepley   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
205570e92668SMatthew Knepley   PetscValidHeaderSpecific(x,VEC_COOKIE,2);
205670e92668SMatthew Knepley   PetscCheckSameComm(snes,1,x,2);
205770e92668SMatthew Knepley   snes->vec_sol_always = x;
205870e92668SMatthew Knepley   PetscFunctionReturn(0);
205970e92668SMatthew Knepley }
206070e92668SMatthew Knepley 
206170e92668SMatthew Knepley #undef __FUNCT__
20624a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
206352baeb72SSatish Balay /*@
20649b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
20659b94acceSBarry Smith    stored.
20669b94acceSBarry Smith 
2067c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2068c7afd0dbSLois Curfman McInnes 
20699b94acceSBarry Smith    Input Parameter:
20709b94acceSBarry Smith .  snes - the SNES context
20719b94acceSBarry Smith 
20729b94acceSBarry Smith    Output Parameter:
20739b94acceSBarry Smith .  x - the solution update
20749b94acceSBarry Smith 
207536851e7fSLois Curfman McInnes    Level: advanced
207636851e7fSLois Curfman McInnes 
20779b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
20789b94acceSBarry Smith 
20799b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction
20809b94acceSBarry Smith @*/
208163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolutionUpdate(SNES snes,Vec *x)
20829b94acceSBarry Smith {
20833a40ed3dSBarry Smith   PetscFunctionBegin;
20844482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
20854482741eSBarry Smith   PetscValidPointer(x,2);
20869b94acceSBarry Smith   *x = snes->vec_sol_update_always;
20873a40ed3dSBarry Smith   PetscFunctionReturn(0);
20889b94acceSBarry Smith }
20899b94acceSBarry Smith 
20904a2ae208SSatish Balay #undef __FUNCT__
20914a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
20929b94acceSBarry Smith /*@C
20933638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
20949b94acceSBarry Smith 
2095c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2096c7afd0dbSLois Curfman McInnes 
20979b94acceSBarry Smith    Input Parameter:
20989b94acceSBarry Smith .  snes - the SNES context
20999b94acceSBarry Smith 
21009b94acceSBarry Smith    Output Parameter:
21017bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
210270e92668SMatthew Knepley .  func - the function (or PETSC_NULL)
210370e92668SMatthew Knepley -  ctx - the function context (or PETSC_NULL)
21049b94acceSBarry Smith 
210536851e7fSLois Curfman McInnes    Level: advanced
210636851e7fSLois Curfman McInnes 
2107a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
21089b94acceSBarry Smith 
21094b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution()
21109b94acceSBarry Smith @*/
211170e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx)
21129b94acceSBarry Smith {
21133a40ed3dSBarry Smith   PetscFunctionBegin;
21144482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
21157bf4e008SBarry Smith   if (r)    *r    = snes->vec_func_always;
2116e7788613SBarry Smith   if (func) *func = snes->ops->computefunction;
211770e92668SMatthew Knepley   if (ctx)  *ctx  = snes->funP;
21183a40ed3dSBarry Smith   PetscFunctionReturn(0);
21199b94acceSBarry Smith }
21209b94acceSBarry Smith 
21214a2ae208SSatish Balay #undef __FUNCT__
21224a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
21233c7409f5SSatish Balay /*@C
21243c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2125d850072dSLois Curfman McInnes    SNES options in the database.
21263c7409f5SSatish Balay 
2127fee21e36SBarry Smith    Collective on SNES
2128fee21e36SBarry Smith 
2129c7afd0dbSLois Curfman McInnes    Input Parameter:
2130c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2131c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2132c7afd0dbSLois Curfman McInnes 
2133d850072dSLois Curfman McInnes    Notes:
2134a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2135c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2136d850072dSLois Curfman McInnes 
213736851e7fSLois Curfman McInnes    Level: advanced
213836851e7fSLois Curfman McInnes 
21393c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2140a86d99e1SLois Curfman McInnes 
2141a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
21423c7409f5SSatish Balay @*/
214363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetOptionsPrefix(SNES snes,const char prefix[])
21443c7409f5SSatish Balay {
2145dfbe8321SBarry Smith   PetscErrorCode ierr;
21463c7409f5SSatish Balay 
21473a40ed3dSBarry Smith   PetscFunctionBegin;
21484482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2149639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
215094b7f48cSBarry Smith   ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
21513a40ed3dSBarry Smith   PetscFunctionReturn(0);
21523c7409f5SSatish Balay }
21533c7409f5SSatish Balay 
21544a2ae208SSatish Balay #undef __FUNCT__
21554a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
21563c7409f5SSatish Balay /*@C
2157f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2158d850072dSLois Curfman McInnes    SNES options in the database.
21593c7409f5SSatish Balay 
2160fee21e36SBarry Smith    Collective on SNES
2161fee21e36SBarry Smith 
2162c7afd0dbSLois Curfman McInnes    Input Parameters:
2163c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2164c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2165c7afd0dbSLois Curfman McInnes 
2166d850072dSLois Curfman McInnes    Notes:
2167a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2168c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2169d850072dSLois Curfman McInnes 
217036851e7fSLois Curfman McInnes    Level: advanced
217136851e7fSLois Curfman McInnes 
21723c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2173a86d99e1SLois Curfman McInnes 
2174a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
21753c7409f5SSatish Balay @*/
217663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAppendOptionsPrefix(SNES snes,const char prefix[])
21773c7409f5SSatish Balay {
2178dfbe8321SBarry Smith   PetscErrorCode ierr;
21793c7409f5SSatish Balay 
21803a40ed3dSBarry Smith   PetscFunctionBegin;
21814482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2182639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
218394b7f48cSBarry Smith   ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
21843a40ed3dSBarry Smith   PetscFunctionReturn(0);
21853c7409f5SSatish Balay }
21863c7409f5SSatish Balay 
21874a2ae208SSatish Balay #undef __FUNCT__
21884a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
21899ab63eb5SSatish Balay /*@C
21903c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
21913c7409f5SSatish Balay    SNES options in the database.
21923c7409f5SSatish Balay 
2193c7afd0dbSLois Curfman McInnes    Not Collective
2194c7afd0dbSLois Curfman McInnes 
21953c7409f5SSatish Balay    Input Parameter:
21963c7409f5SSatish Balay .  snes - the SNES context
21973c7409f5SSatish Balay 
21983c7409f5SSatish Balay    Output Parameter:
21993c7409f5SSatish Balay .  prefix - pointer to the prefix string used
22003c7409f5SSatish Balay 
22019ab63eb5SSatish Balay    Notes: On the fortran side, the user should pass in a string 'prifix' of
22029ab63eb5SSatish Balay    sufficient length to hold the prefix.
22039ab63eb5SSatish Balay 
220436851e7fSLois Curfman McInnes    Level: advanced
220536851e7fSLois Curfman McInnes 
22063c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2207a86d99e1SLois Curfman McInnes 
2208a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
22093c7409f5SSatish Balay @*/
2210e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetOptionsPrefix(SNES snes,const char *prefix[])
22113c7409f5SSatish Balay {
2212dfbe8321SBarry Smith   PetscErrorCode ierr;
22133c7409f5SSatish Balay 
22143a40ed3dSBarry Smith   PetscFunctionBegin;
22154482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2216639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
22173a40ed3dSBarry Smith   PetscFunctionReturn(0);
22183c7409f5SSatish Balay }
22193c7409f5SSatish Balay 
2220b2002411SLois Curfman McInnes 
22214a2ae208SSatish Balay #undef __FUNCT__
22224a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
22233cea93caSBarry Smith /*@C
22243cea93caSBarry Smith   SNESRegister - See SNESRegisterDynamic()
22253cea93caSBarry Smith 
22267f6c08e0SMatthew Knepley   Level: advanced
22273cea93caSBarry Smith @*/
222863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES))
2229b2002411SLois Curfman McInnes {
2230e2d1d2b7SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
2231dfbe8321SBarry Smith   PetscErrorCode ierr;
2232b2002411SLois Curfman McInnes 
2233b2002411SLois Curfman McInnes   PetscFunctionBegin;
2234b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2235c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
2236b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2237b2002411SLois Curfman McInnes }
2238da9b6338SBarry Smith 
2239da9b6338SBarry Smith #undef __FUNCT__
2240da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin"
224163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESTestLocalMin(SNES snes)
2242da9b6338SBarry Smith {
2243dfbe8321SBarry Smith   PetscErrorCode ierr;
224477431f27SBarry Smith   PetscInt       N,i,j;
2245da9b6338SBarry Smith   Vec            u,uh,fh;
2246da9b6338SBarry Smith   PetscScalar    value;
2247da9b6338SBarry Smith   PetscReal      norm;
2248da9b6338SBarry Smith 
2249da9b6338SBarry Smith   PetscFunctionBegin;
2250da9b6338SBarry Smith   ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr);
2251da9b6338SBarry Smith   ierr = VecDuplicate(u,&uh);CHKERRQ(ierr);
2252da9b6338SBarry Smith   ierr = VecDuplicate(u,&fh);CHKERRQ(ierr);
2253da9b6338SBarry Smith 
2254da9b6338SBarry Smith   /* currently only works for sequential */
2255da9b6338SBarry Smith   ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n");
2256da9b6338SBarry Smith   ierr = VecGetSize(u,&N);CHKERRQ(ierr);
2257da9b6338SBarry Smith   for (i=0; i<N; i++) {
2258da9b6338SBarry Smith     ierr = VecCopy(u,uh);CHKERRQ(ierr);
225977431f27SBarry Smith     ierr  = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr);
2260da9b6338SBarry Smith     for (j=-10; j<11; j++) {
2261ccae9161SBarry Smith       value = PetscSign(j)*exp(PetscAbs(j)-10.0);
2262da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
22633ab0aad5SBarry Smith       ierr  = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr);
2264da9b6338SBarry Smith       ierr  = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr);
226577431f27SBarry Smith       ierr  = PetscPrintf(PETSC_COMM_WORLD,"       j norm %D %18.16e\n",j,norm);CHKERRQ(ierr);
2266da9b6338SBarry Smith       value = -value;
2267da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
2268da9b6338SBarry Smith     }
2269da9b6338SBarry Smith   }
2270da9b6338SBarry Smith   ierr = VecDestroy(uh);CHKERRQ(ierr);
2271da9b6338SBarry Smith   ierr = VecDestroy(fh);CHKERRQ(ierr);
2272da9b6338SBarry Smith   PetscFunctionReturn(0);
2273da9b6338SBarry Smith }
2274