xref: /petsc/src/snes/interface/snes.c (revision 06ee9f85ecda6adaae627a28ca41c24bdb6deb84)
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     }
710ef38995SBarry Smith     if (snes->view) {
72b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
730ef38995SBarry Smith       ierr = (*snes->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);
23893c39befSBarry Smith     if (flg) {snes->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 
279186905e3SBarry Smith     if (snes->setfromoptions) {
280186905e3SBarry Smith       ierr = (*snes->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
6359b94acceSBarry Smith    KSP and PC contexts as well.
6369b94acceSBarry Smith 
63736851e7fSLois Curfman McInnes    Level: beginner
63836851e7fSLois Curfman McInnes 
63994b7f48cSBarry Smith .keywords: SNES, nonlinear, get, KSP, context
6409b94acceSBarry Smith 
64194b7f48cSBarry Smith .seealso: KSPGetPC()
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__
6534a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc"
6546849ba73SBarry Smith static PetscErrorCode SNESPublish_Petsc(PetscObject obj)
655e24b481bSBarry Smith {
656e24b481bSBarry Smith   PetscFunctionBegin;
657e24b481bSBarry Smith   PetscFunctionReturn(0);
658e24b481bSBarry Smith }
659e24b481bSBarry Smith 
6609b94acceSBarry Smith /* -----------------------------------------------------------*/
6614a2ae208SSatish Balay #undef __FUNCT__
6624a2ae208SSatish Balay #define __FUNCT__ "SNESCreate"
66352baeb72SSatish Balay /*@
6649b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
6659b94acceSBarry Smith 
666c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
667c7afd0dbSLois Curfman McInnes 
668c7afd0dbSLois Curfman McInnes    Input Parameters:
669c7afd0dbSLois Curfman McInnes +  comm - MPI communicator
6709b94acceSBarry Smith 
6719b94acceSBarry Smith    Output Parameter:
6729b94acceSBarry Smith .  outsnes - the new SNES context
6739b94acceSBarry Smith 
674c7afd0dbSLois Curfman McInnes    Options Database Keys:
675c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
676c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
677c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
678c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
679c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
680c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
681c1f60f51SBarry Smith 
68236851e7fSLois Curfman McInnes    Level: beginner
68336851e7fSLois Curfman McInnes 
6849b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
6859b94acceSBarry Smith 
6864b27c08aSLois Curfman McInnes .seealso: SNESSolve(), SNESDestroy(), SNES
6879b94acceSBarry Smith @*/
68863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESCreate(MPI_Comm comm,SNES *outsnes)
6899b94acceSBarry Smith {
690dfbe8321SBarry Smith   PetscErrorCode      ierr;
6919b94acceSBarry Smith   SNES                snes;
6929b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
69337fcc0dbSBarry Smith 
6943a40ed3dSBarry Smith   PetscFunctionBegin;
695ed1caa07SMatthew Knepley   PetscValidPointer(outsnes,2);
6968ba1e511SMatthew Knepley   *outsnes = PETSC_NULL;
6978ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
6988ba1e511SMatthew Knepley   ierr = SNESInitializePackage(PETSC_NULL);CHKERRQ(ierr);
6998ba1e511SMatthew Knepley #endif
7008ba1e511SMatthew Knepley 
70152e6d16bSBarry Smith   ierr = PetscHeaderCreate(snes,_p_SNES,PetscInt,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView);CHKERRQ(ierr);
702e24b481bSBarry Smith   snes->bops->publish     = SNESPublish_Petsc;
7039b94acceSBarry Smith   snes->max_its           = 50;
7049750a799SBarry Smith   snes->max_funcs	  = 10000;
7059b94acceSBarry Smith   snes->norm		  = 0.0;
706b4874afaSBarry Smith   snes->rtol		  = 1.e-8;
707b4874afaSBarry Smith   snes->ttol              = 0.0;
70870441072SBarry Smith   snes->abstol		  = 1.e-50;
7099b94acceSBarry Smith   snes->xtol		  = 1.e-8;
7104b27c08aSLois Curfman McInnes   snes->deltatol	  = 1.e-12;
7119b94acceSBarry Smith   snes->nfuncs            = 0;
71250ffb88aSMatthew Knepley   snes->numFailures       = 0;
71350ffb88aSMatthew Knepley   snes->maxFailures       = 1;
7147a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
715639f9d9dSBarry Smith   snes->numbermonitors    = 0;
7169b94acceSBarry Smith   snes->data              = 0;
7179b94acceSBarry Smith   snes->view              = 0;
7184dc4c822SBarry Smith   snes->setupcalled       = PETSC_FALSE;
719186905e3SBarry Smith   snes->ksp_ewconv        = PETSC_FALSE;
7206f24a144SLois Curfman McInnes   snes->vwork             = 0;
7216f24a144SLois Curfman McInnes   snes->nwork             = 0;
722758f92a0SBarry Smith   snes->conv_hist_len     = 0;
723758f92a0SBarry Smith   snes->conv_hist_max     = 0;
724758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
725758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
726758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
727184914b5SBarry Smith   snes->reason            = SNES_CONVERGED_ITERATING;
7289b94acceSBarry Smith 
7293d4c4710SBarry Smith   snes->numLinearSolveFailures = 0;
7303d4c4710SBarry Smith   snes->maxLinearSolveFailures = 1;
7313d4c4710SBarry Smith 
7329b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
733b0a32e0cSBarry Smith   ierr = PetscNew(SNES_KSP_EW_ConvCtx,&kctx);CHKERRQ(ierr);
73452e6d16bSBarry Smith   ierr = PetscLogObjectMemory(snes,sizeof(SNES_KSP_EW_ConvCtx));CHKERRQ(ierr);
7359b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
7369b94acceSBarry Smith   kctx->version     = 2;
7379b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
7389b94acceSBarry Smith                              this was too large for some test cases */
7399b94acceSBarry Smith   kctx->rtol_last   = 0;
7409b94acceSBarry Smith   kctx->rtol_max    = .9;
7419b94acceSBarry Smith   kctx->gamma       = 1.0;
7429b94acceSBarry Smith   kctx->alpha2      = .5*(1.0 + sqrt(5.0));
7439b94acceSBarry Smith   kctx->alpha       = kctx->alpha2;
7449b94acceSBarry Smith   kctx->threshold   = .1;
7459b94acceSBarry Smith   kctx->lresid_last = 0;
7469b94acceSBarry Smith   kctx->norm_last   = 0;
7479b94acceSBarry Smith 
74894b7f48cSBarry Smith   ierr = KSPCreate(comm,&snes->ksp);CHKERRQ(ierr);
74952e6d16bSBarry Smith   ierr = PetscLogObjectParent(snes,snes->ksp);CHKERRQ(ierr);
7505334005bSBarry Smith 
7519b94acceSBarry Smith   *outsnes = snes;
75200036973SBarry Smith   ierr = PetscPublishAll(snes);CHKERRQ(ierr);
7533a40ed3dSBarry Smith   PetscFunctionReturn(0);
7549b94acceSBarry Smith }
7559b94acceSBarry Smith 
7564a2ae208SSatish Balay #undef __FUNCT__
7574a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction"
7589b94acceSBarry Smith /*@C
7599b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
7609b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
7619b94acceSBarry Smith    equations.
7629b94acceSBarry Smith 
763fee21e36SBarry Smith    Collective on SNES
764fee21e36SBarry Smith 
765c7afd0dbSLois Curfman McInnes    Input Parameters:
766c7afd0dbSLois Curfman McInnes +  snes - the SNES context
767c7afd0dbSLois Curfman McInnes .  func - function evaluation routine
768c7afd0dbSLois Curfman McInnes .  r - vector to store function value
769c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
770c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
7719b94acceSBarry Smith 
772c7afd0dbSLois Curfman McInnes    Calling sequence of func:
7738d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
774c7afd0dbSLois Curfman McInnes 
775313e4042SLois Curfman McInnes .  f - function vector
776c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
7779b94acceSBarry Smith 
7789b94acceSBarry Smith    Notes:
7799b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
7809b94acceSBarry Smith $      f'(x) x = -f(x),
781c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
7829b94acceSBarry Smith 
78336851e7fSLois Curfman McInnes    Level: beginner
78436851e7fSLois Curfman McInnes 
7859b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
7869b94acceSBarry Smith 
787a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
7889b94acceSBarry Smith @*/
78963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetFunction(SNES snes,Vec r,PetscErrorCode (*func)(SNES,Vec,Vec,void*),void *ctx)
7909b94acceSBarry Smith {
7913a40ed3dSBarry Smith   PetscFunctionBegin;
7924482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
7934482741eSBarry Smith   PetscValidHeaderSpecific(r,VEC_COOKIE,2);
794c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,r,2);
795184914b5SBarry Smith 
7969b94acceSBarry Smith   snes->computefunction     = func;
7979b94acceSBarry Smith   snes->vec_func            = snes->vec_func_always = r;
7989b94acceSBarry Smith   snes->funP                = ctx;
7993a40ed3dSBarry Smith   PetscFunctionReturn(0);
8009b94acceSBarry Smith }
8019b94acceSBarry Smith 
8023ab0aad5SBarry Smith /* --------------------------------------------------------------- */
8033ab0aad5SBarry Smith #undef __FUNCT__
8043ab0aad5SBarry Smith #define __FUNCT__ "SNESSetRhs"
8053ab0aad5SBarry Smith /*@C
8063ab0aad5SBarry Smith    SNESSetRhs - Sets the vector for solving F(x) = rhs. If rhs is not set
8073ab0aad5SBarry Smith    it assumes a zero right hand side.
8083ab0aad5SBarry Smith 
8093ab0aad5SBarry Smith    Collective on SNES
8103ab0aad5SBarry Smith 
8113ab0aad5SBarry Smith    Input Parameters:
8123ab0aad5SBarry Smith +  snes - the SNES context
8133ab0aad5SBarry Smith -  rhs - the right hand side vector or PETSC_NULL for a zero right hand side
8143ab0aad5SBarry Smith 
8153ab0aad5SBarry Smith    Level: intermediate
8163ab0aad5SBarry Smith 
8173ab0aad5SBarry Smith .keywords: SNES, nonlinear, set, function, right hand side
8183ab0aad5SBarry Smith 
8191096aae1SMatthew Knepley .seealso: SNESGetRhs(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
8203ab0aad5SBarry Smith @*/
82163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetRhs(SNES snes,Vec rhs)
8223ab0aad5SBarry Smith {
823dfbe8321SBarry Smith   PetscErrorCode ierr;
8243ab0aad5SBarry Smith 
8253ab0aad5SBarry Smith   PetscFunctionBegin;
8263ab0aad5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
8273ab0aad5SBarry Smith   if (rhs) {
8283ab0aad5SBarry Smith     PetscValidHeaderSpecific(rhs,VEC_COOKIE,2);
8293ab0aad5SBarry Smith     PetscCheckSameComm(snes,1,rhs,2);
8303ab0aad5SBarry Smith     ierr = PetscObjectReference((PetscObject)rhs);CHKERRQ(ierr);
8313ab0aad5SBarry Smith   }
8323ab0aad5SBarry Smith   if (snes->afine) {
8333ab0aad5SBarry Smith     ierr = VecDestroy(snes->afine);CHKERRQ(ierr);
8343ab0aad5SBarry Smith   }
8353ab0aad5SBarry Smith   snes->afine = rhs;
8363ab0aad5SBarry Smith   PetscFunctionReturn(0);
8373ab0aad5SBarry Smith }
8383ab0aad5SBarry Smith 
8394a2ae208SSatish Balay #undef __FUNCT__
8401096aae1SMatthew Knepley #define __FUNCT__ "SNESGetRhs"
8411096aae1SMatthew Knepley /*@C
8421096aae1SMatthew Knepley    SNESGetRhs - Gets the vector for solving F(x) = rhs. If rhs is not set
8431096aae1SMatthew Knepley    it assumes a zero right hand side.
8441096aae1SMatthew Knepley 
8451096aae1SMatthew Knepley    Collective on SNES
8461096aae1SMatthew Knepley 
8471096aae1SMatthew Knepley    Input Parameter:
8481096aae1SMatthew Knepley .  snes - the SNES context
8491096aae1SMatthew Knepley 
8501096aae1SMatthew Knepley    Output Parameter:
8511096aae1SMatthew Knepley .  rhs - the right hand side vector or PETSC_NULL for a zero right hand side
8521096aae1SMatthew Knepley 
8531096aae1SMatthew Knepley    Level: intermediate
8541096aae1SMatthew Knepley 
8551096aae1SMatthew Knepley .keywords: SNES, nonlinear, get, function, right hand side
8561096aae1SMatthew Knepley 
8571096aae1SMatthew Knepley .seealso: SNESSetRhs(), SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian(), SNESSetFunction()
8581096aae1SMatthew Knepley @*/
8591096aae1SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetRhs(SNES snes,Vec *rhs)
8601096aae1SMatthew Knepley {
8611096aae1SMatthew Knepley   PetscFunctionBegin;
8621096aae1SMatthew Knepley   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
8631096aae1SMatthew Knepley   PetscValidPointer(rhs,2);
8641096aae1SMatthew Knepley   *rhs = snes->afine;
8651096aae1SMatthew Knepley   PetscFunctionReturn(0);
8661096aae1SMatthew Knepley }
8671096aae1SMatthew Knepley 
8681096aae1SMatthew Knepley #undef __FUNCT__
8694a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction"
8709b94acceSBarry Smith /*@
87136851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
8729b94acceSBarry Smith                          SNESSetFunction().
8739b94acceSBarry Smith 
874c7afd0dbSLois Curfman McInnes    Collective on SNES
875c7afd0dbSLois Curfman McInnes 
8769b94acceSBarry Smith    Input Parameters:
877c7afd0dbSLois Curfman McInnes +  snes - the SNES context
878c7afd0dbSLois Curfman McInnes -  x - input vector
8799b94acceSBarry Smith 
8809b94acceSBarry Smith    Output Parameter:
8813638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
8829b94acceSBarry Smith 
8831bffabb2SLois Curfman McInnes    Notes:
88436851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
88536851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
88636851e7fSLois Curfman McInnes    themselves.
88736851e7fSLois Curfman McInnes 
88836851e7fSLois Curfman McInnes    Level: developer
88936851e7fSLois Curfman McInnes 
8909b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
8919b94acceSBarry Smith 
892a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
8939b94acceSBarry Smith @*/
89463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeFunction(SNES snes,Vec x,Vec y)
8959b94acceSBarry Smith {
896dfbe8321SBarry Smith   PetscErrorCode ierr;
8979b94acceSBarry Smith 
8983a40ed3dSBarry Smith   PetscFunctionBegin;
8994482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
9004482741eSBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE,2);
9014482741eSBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE,3);
902c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,x,2);
903c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,3);
904184914b5SBarry Smith 
905d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
9061096aae1SMatthew Knepley   if (snes->computefunction) {
907d64ed03dSBarry Smith     PetscStackPush("SNES user function");
908e9a2bbcdSBarry Smith     CHKMEMQ;
90919717074SBarry Smith     ierr = (*snes->computefunction)(snes,x,y,snes->funP);
910e9a2bbcdSBarry Smith     CHKMEMQ;
911d64ed03dSBarry Smith     PetscStackPop;
912d5e45103SBarry Smith     if (PetscExceptionValue(ierr)) {
91319717074SBarry Smith       PetscErrorCode pierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(pierr);
91419717074SBarry Smith     }
915d5e45103SBarry Smith     CHKERRQ(ierr);
9161096aae1SMatthew Knepley   } else if (snes->afine) {
9171096aae1SMatthew Knepley     ierr = MatMult(snes->jacobian, x, y);CHKERRQ(ierr);
9181096aae1SMatthew Knepley   } else {
9191096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Must call SNESSetFunction() before SNESComputeFunction(), likely called from SNESSolve().");
9201096aae1SMatthew Knepley   }
9213ab0aad5SBarry Smith   if (snes->afine) {
922016dedfbSBarry Smith     ierr = VecAXPY(y,-1.0,snes->afine);CHKERRQ(ierr);
9233ab0aad5SBarry Smith   }
924ae3c334cSLois Curfman McInnes   snes->nfuncs++;
925d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
9263a40ed3dSBarry Smith   PetscFunctionReturn(0);
9279b94acceSBarry Smith }
9289b94acceSBarry Smith 
9294a2ae208SSatish Balay #undef __FUNCT__
9304a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian"
93162fef451SLois Curfman McInnes /*@
93262fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
93362fef451SLois Curfman McInnes    set with SNESSetJacobian().
93462fef451SLois Curfman McInnes 
935c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
936c7afd0dbSLois Curfman McInnes 
93762fef451SLois Curfman McInnes    Input Parameters:
938c7afd0dbSLois Curfman McInnes +  snes - the SNES context
939c7afd0dbSLois Curfman McInnes -  x - input vector
94062fef451SLois Curfman McInnes 
94162fef451SLois Curfman McInnes    Output Parameters:
942c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
94362fef451SLois Curfman McInnes .  B - optional preconditioning matrix
9442b668275SBarry Smith -  flag - flag indicating matrix structure (one of, SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER)
945fee21e36SBarry Smith 
94662fef451SLois Curfman McInnes    Notes:
94762fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
94862fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
94962fef451SLois Curfman McInnes 
95094b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
951dc5a77f8SLois Curfman McInnes    flag parameter.
95262fef451SLois Curfman McInnes 
95336851e7fSLois Curfman McInnes    Level: developer
95436851e7fSLois Curfman McInnes 
95562fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
95662fef451SLois Curfman McInnes 
9572b668275SBarry Smith .seealso:  SNESSetJacobian(), KSPSetOperators(), MatStructure
95862fef451SLois Curfman McInnes @*/
95963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
9609b94acceSBarry Smith {
961dfbe8321SBarry Smith   PetscErrorCode ierr;
9623a40ed3dSBarry Smith 
9633a40ed3dSBarry Smith   PetscFunctionBegin;
9644482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
9654482741eSBarry Smith   PetscValidHeaderSpecific(X,VEC_COOKIE,2);
9664482741eSBarry Smith   PetscValidPointer(flg,5);
967c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,X,2);
9683a40ed3dSBarry Smith   if (!snes->computejacobian) PetscFunctionReturn(0);
969d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
970c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
971d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
972dc67937bSBarry Smith   CHKMEMQ;
9739b94acceSBarry Smith   ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
974dc67937bSBarry Smith   CHKMEMQ;
975d64ed03dSBarry Smith   PetscStackPop;
976d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
9776d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
9786ce558aeSBarry Smith   /* PetscValidHeaderSpecific(*A,MAT_COOKIE,3);
9796ce558aeSBarry Smith     PetscValidHeaderSpecific(*B,MAT_COOKIE,4);   */
9803a40ed3dSBarry Smith   PetscFunctionReturn(0);
9819b94acceSBarry Smith }
9829b94acceSBarry Smith 
9834a2ae208SSatish Balay #undef __FUNCT__
9844a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian"
9859b94acceSBarry Smith /*@C
9869b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
987044dda88SLois Curfman McInnes    location to store the matrix.
9889b94acceSBarry Smith 
989c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
990c7afd0dbSLois Curfman McInnes 
9919b94acceSBarry Smith    Input Parameters:
992c7afd0dbSLois Curfman McInnes +  snes - the SNES context
9939b94acceSBarry Smith .  A - Jacobian matrix
9949b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
9959b94acceSBarry Smith .  func - Jacobian evaluation routine
996c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
9972cd2dfdcSLois Curfman McInnes          Jacobian evaluation routine (may be PETSC_NULL)
9989b94acceSBarry Smith 
9999b94acceSBarry Smith    Calling sequence of func:
10008d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
10019b94acceSBarry Smith 
1002c7afd0dbSLois Curfman McInnes +  x - input vector
10039b94acceSBarry Smith .  A - Jacobian matrix
10049b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1005ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
10062b668275SBarry Smith    structure (same as flag in KSPSetOperators()), one of SAME_NONZERO_PATTERN,DIFFERENT_NONZERO_PATTERN,SAME_PRECONDITIONER
1007c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
10089b94acceSBarry Smith 
10099b94acceSBarry Smith    Notes:
101094b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the flag
10112cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1012ac21db08SLois Curfman McInnes 
1013ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
10149b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
10159b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
10169b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
10179b94acceSBarry Smith    throughout the global iterations.
10189b94acceSBarry Smith 
101936851e7fSLois Curfman McInnes    Level: beginner
102036851e7fSLois Curfman McInnes 
10219b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
10229b94acceSBarry Smith 
10232b668275SBarry Smith .seealso: KSPSetOperators(), SNESSetFunction(), MatSNESMFComputeJacobian(), SNESDefaultComputeJacobianColor(), MatStructure
10249b94acceSBarry Smith @*/
102563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetJacobian(SNES snes,Mat A,Mat B,PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
10269b94acceSBarry Smith {
1027dfbe8321SBarry Smith   PetscErrorCode ierr;
10283a7fca6bSBarry Smith 
10293a40ed3dSBarry Smith   PetscFunctionBegin;
10304482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
10314482741eSBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_COOKIE,2);
10324482741eSBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_COOKIE,3);
1033c9780b6fSBarry Smith   if (A) PetscCheckSameComm(snes,1,A,2);
1034c9780b6fSBarry Smith   if (B) PetscCheckSameComm(snes,1,B,2);
10353a7fca6bSBarry Smith   if (func) snes->computejacobian = func;
10363a7fca6bSBarry Smith   if (ctx)  snes->jacP            = ctx;
10373a7fca6bSBarry Smith   if (A) {
10383a7fca6bSBarry Smith     if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
10399b94acceSBarry Smith     snes->jacobian = A;
10403a7fca6bSBarry Smith     ierr           = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
10413a7fca6bSBarry Smith   }
10423a7fca6bSBarry Smith   if (B) {
10433a7fca6bSBarry Smith     if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
10449b94acceSBarry Smith     snes->jacobian_pre = B;
10453a7fca6bSBarry Smith     ierr               = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
10463a7fca6bSBarry Smith   }
104769a612a9SBarry Smith   ierr = KSPSetOperators(snes->ksp,A,B,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
10483a40ed3dSBarry Smith   PetscFunctionReturn(0);
10499b94acceSBarry Smith }
105062fef451SLois Curfman McInnes 
10514a2ae208SSatish Balay #undef __FUNCT__
10524a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian"
1053c2aafc4cSSatish Balay /*@C
1054b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1055b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1056b4fd4287SBarry Smith 
1057c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1058c7afd0dbSLois Curfman McInnes 
1059b4fd4287SBarry Smith    Input Parameter:
1060b4fd4287SBarry Smith .  snes - the nonlinear solver context
1061b4fd4287SBarry Smith 
1062b4fd4287SBarry Smith    Output Parameters:
1063c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1064b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
106570e92668SMatthew Knepley .  func - location to put Jacobian function (or PETSC_NULL)
106670e92668SMatthew Knepley -  ctx - location to stash Jacobian ctx (or PETSC_NULL)
1067fee21e36SBarry Smith 
106836851e7fSLois Curfman McInnes    Level: advanced
106936851e7fSLois Curfman McInnes 
1070b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1071b4fd4287SBarry Smith @*/
107270e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetJacobian(SNES snes,Mat *A,Mat *B,PetscErrorCode (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void **ctx)
1073b4fd4287SBarry Smith {
10743a40ed3dSBarry Smith   PetscFunctionBegin;
10754482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1076b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
1077b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
107800036973SBarry Smith   if (func) *func = snes->computejacobian;
107970e92668SMatthew Knepley   if (ctx)  *ctx  = snes->jacP;
10803a40ed3dSBarry Smith   PetscFunctionReturn(0);
1081b4fd4287SBarry Smith }
1082b4fd4287SBarry Smith 
10839b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
108463dd3a1aSKris Buschelman EXTERN PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*);
10859b94acceSBarry Smith 
10864a2ae208SSatish Balay #undef __FUNCT__
10874a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp"
10889b94acceSBarry Smith /*@
10899b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1090272ac6f2SLois Curfman McInnes    of a nonlinear solver.
10919b94acceSBarry Smith 
1092fee21e36SBarry Smith    Collective on SNES
1093fee21e36SBarry Smith 
1094c7afd0dbSLois Curfman McInnes    Input Parameters:
109570e92668SMatthew Knepley .  snes - the SNES context
1096c7afd0dbSLois Curfman McInnes 
1097272ac6f2SLois Curfman McInnes    Notes:
1098272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1099272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1100272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1101272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1102272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1103272ac6f2SLois Curfman McInnes 
110436851e7fSLois Curfman McInnes    Level: advanced
110536851e7fSLois Curfman McInnes 
11069b94acceSBarry Smith .keywords: SNES, nonlinear, setup
11079b94acceSBarry Smith 
11089b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
11099b94acceSBarry Smith @*/
111070e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUp(SNES snes)
11119b94acceSBarry Smith {
1112dfbe8321SBarry Smith   PetscErrorCode ierr;
11134b27c08aSLois Curfman McInnes   PetscTruth     flg, iseqtr;
11143a40ed3dSBarry Smith 
11153a40ed3dSBarry Smith   PetscFunctionBegin;
11164482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
11174dc4c822SBarry Smith   if (snes->setupcalled) PetscFunctionReturn(0);
11189b94acceSBarry Smith 
1119b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_mf_operator",&flg);CHKERRQ(ierr);
1120c1f60f51SBarry Smith   /*
1121c1f60f51SBarry Smith       This version replaces the user provided Jacobian matrix with a
1122dfa02198SLois Curfman McInnes       matrix-free version but still employs the user-provided preconditioner matrix
1123c1f60f51SBarry Smith   */
1124c1f60f51SBarry Smith   if (flg) {
1125c1f60f51SBarry Smith     Mat J;
11265a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
11275a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
1128ae15b995SBarry Smith     ierr = PetscInfo(snes,"Setting default matrix-free operator routines\n");CHKERRQ(ierr);
11293a7fca6bSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
11303a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
1131c1f60f51SBarry Smith   }
113245fc7adcSBarry Smith 
113303c60df9SBarry Smith #if !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SINGLE) && !defined(PETSC_USE_MAT_SINGLE) && !defined(PETSC_USE_LONG_DOUBLE) && !defined(PETSC_USE_INT)
113445fc7adcSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_mf_operator2",&flg);CHKERRQ(ierr);
113545fc7adcSBarry Smith   if (flg) {
113645fc7adcSBarry Smith     Mat J;
113745fc7adcSBarry Smith     ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_sol,&J);CHKERRQ(ierr);
113845fc7adcSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
113945fc7adcSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
114045fc7adcSBarry Smith   }
114132a4b47aSMatthew Knepley #endif
114245fc7adcSBarry Smith 
1143b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_mf",&flg);CHKERRQ(ierr);
1144c1f60f51SBarry Smith   /*
1145dfa02198SLois Curfman McInnes       This version replaces both the user-provided Jacobian and the user-
1146c1f60f51SBarry Smith       provided preconditioner matrix with the default matrix free version.
1147c1f60f51SBarry Smith    */
114831615d04SLois Curfman McInnes   if (flg) {
1149272ac6f2SLois Curfman McInnes     Mat  J;
1150b5d62d44SBarry Smith     KSP ksp;
115194b7f48cSBarry Smith     PC   pc;
1152f3ef73ceSBarry Smith 
11535a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
11543a7fca6bSBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
1155ae15b995SBarry Smith     ierr = PetscInfo(snes,"Setting default matrix-free operator and preconditioner routines;\nThat is no preconditioner is being used.\n");CHKERRQ(ierr);
11563a7fca6bSBarry Smith     ierr = SNESSetJacobian(snes,J,J,MatSNESMFComputeJacobian,snes->funP);CHKERRQ(ierr);
11573a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
11583a7fca6bSBarry Smith 
1159f3ef73ceSBarry Smith     /* force no preconditioner */
116094b7f48cSBarry Smith     ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
1161b5d62d44SBarry Smith     ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
1162a9815358SBarry Smith     ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&flg);CHKERRQ(ierr);
1163a9815358SBarry Smith     if (!flg) {
1164f3ef73ceSBarry Smith       ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
1165272ac6f2SLois Curfman McInnes     }
1166a9815358SBarry Smith   }
1167f3ef73ceSBarry Smith 
11681096aae1SMatthew Knepley   if (!snes->vec_func && !snes->afine) {
11691096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
11701096aae1SMatthew Knepley   }
11711096aae1SMatthew Knepley   if (!snes->computefunction && !snes->afine) {
11721096aae1SMatthew Knepley     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
11731096aae1SMatthew Knepley   }
117429bbc08cSBarry Smith   if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetJacobian() first \n or use -snes_mf option");
1175a8c6a408SBarry Smith   if (snes->vec_func == snes->vec_sol) {
117629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
1177a8c6a408SBarry Smith   }
1178a703fe33SLois Curfman McInnes 
1179a703fe33SLois Curfman McInnes   /* Set the KSP stopping criterion to use the Eisenstat-Walker method */
11804b27c08aSLois Curfman McInnes   ierr = PetscTypeCompare((PetscObject)snes,SNESTR,&iseqtr);CHKERRQ(ierr);
11816831982aSBarry Smith   if (snes->ksp_ewconv && !iseqtr) {
118294b7f48cSBarry Smith     KSP ksp;
118394b7f48cSBarry Smith     ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
1184186905e3SBarry Smith     ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private,snes);CHKERRQ(ierr);
1185a703fe33SLois Curfman McInnes   }
11864b27c08aSLois Curfman McInnes 
1187a703fe33SLois Curfman McInnes   if (snes->setup) {ierr = (*snes->setup)(snes);CHKERRQ(ierr);}
11887aaed0d8SBarry Smith   snes->setupcalled = PETSC_TRUE;
11893a40ed3dSBarry Smith   PetscFunctionReturn(0);
11909b94acceSBarry Smith }
11919b94acceSBarry Smith 
11924a2ae208SSatish Balay #undef __FUNCT__
11934a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
119452baeb72SSatish Balay /*@
11959b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
11969b94acceSBarry Smith    with SNESCreate().
11979b94acceSBarry Smith 
1198c7afd0dbSLois Curfman McInnes    Collective on SNES
1199c7afd0dbSLois Curfman McInnes 
12009b94acceSBarry Smith    Input Parameter:
12019b94acceSBarry Smith .  snes - the SNES context
12029b94acceSBarry Smith 
120336851e7fSLois Curfman McInnes    Level: beginner
120436851e7fSLois Curfman McInnes 
12059b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
12069b94acceSBarry Smith 
120763a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
12089b94acceSBarry Smith @*/
120963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDestroy(SNES snes)
12109b94acceSBarry Smith {
12116849ba73SBarry Smith   PetscErrorCode ierr;
12123a40ed3dSBarry Smith 
12133a40ed3dSBarry Smith   PetscFunctionBegin;
12144482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
12153a40ed3dSBarry Smith   if (--snes->refct > 0) PetscFunctionReturn(0);
1216d4bb536fSBarry Smith 
1217be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
12180f5bd95cSBarry Smith   ierr = PetscObjectDepublish(snes);CHKERRQ(ierr);
1219be0abb6dSBarry Smith 
1220e1311b90SBarry Smith   if (snes->destroy) {ierr = (*(snes)->destroy)(snes);CHKERRQ(ierr);}
122105b42c5fSBarry Smith   ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);
12223a7fca6bSBarry Smith   if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
12233a7fca6bSBarry Smith   if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
12243ab0aad5SBarry Smith   if (snes->afine) {ierr = VecDestroy(snes->afine);CHKERRQ(ierr);}
122594b7f48cSBarry Smith   ierr = KSPDestroy(snes->ksp);CHKERRQ(ierr);
1226522c5e43SBarry Smith   if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);}
1227d952e501SBarry Smith   ierr = SNESClearMonitor(snes);CHKERRQ(ierr);
1228a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(snes);CHKERRQ(ierr);
12293a40ed3dSBarry Smith   PetscFunctionReturn(0);
12309b94acceSBarry Smith }
12319b94acceSBarry Smith 
12329b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
12339b94acceSBarry Smith 
12344a2ae208SSatish Balay #undef __FUNCT__
12354a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
12369b94acceSBarry Smith /*@
1237d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
12389b94acceSBarry Smith 
1239c7afd0dbSLois Curfman McInnes    Collective on SNES
1240c7afd0dbSLois Curfman McInnes 
12419b94acceSBarry Smith    Input Parameters:
1242c7afd0dbSLois Curfman McInnes +  snes - the SNES context
124370441072SBarry Smith .  abstol - absolute convergence tolerance
124433174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
124533174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
124633174efeSLois Curfman McInnes            of the change in the solution between steps
124733174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1248c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1249fee21e36SBarry Smith 
125033174efeSLois Curfman McInnes    Options Database Keys:
125170441072SBarry Smith +    -snes_atol <abstol> - Sets abstol
1252c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1253c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1254c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1255c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
12569b94acceSBarry Smith 
1257d7a720efSLois Curfman McInnes    Notes:
12589b94acceSBarry Smith    The default maximum number of iterations is 50.
12599b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
12609b94acceSBarry Smith 
126136851e7fSLois Curfman McInnes    Level: intermediate
126236851e7fSLois Curfman McInnes 
126333174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
12649b94acceSBarry Smith 
12652492ecdbSBarry Smith .seealso: SNESSetTrustRegionTolerance()
12669b94acceSBarry Smith @*/
126763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTolerances(SNES snes,PetscReal abstol,PetscReal rtol,PetscReal stol,PetscInt maxit,PetscInt maxf)
12689b94acceSBarry Smith {
12693a40ed3dSBarry Smith   PetscFunctionBegin;
12704482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
127170441072SBarry Smith   if (abstol != PETSC_DEFAULT)  snes->abstol      = abstol;
1272d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1273d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1274d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1275d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
12763a40ed3dSBarry Smith   PetscFunctionReturn(0);
12779b94acceSBarry Smith }
12789b94acceSBarry Smith 
12794a2ae208SSatish Balay #undef __FUNCT__
12804a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
12819b94acceSBarry Smith /*@
128233174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
128333174efeSLois Curfman McInnes 
1284c7afd0dbSLois Curfman McInnes    Not Collective
1285c7afd0dbSLois Curfman McInnes 
128633174efeSLois Curfman McInnes    Input Parameters:
1287c7afd0dbSLois Curfman McInnes +  snes - the SNES context
128870441072SBarry Smith .  abstol - absolute convergence tolerance
128933174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
129033174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
129133174efeSLois Curfman McInnes            of the change in the solution between steps
129233174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1293c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1294fee21e36SBarry Smith 
129533174efeSLois Curfman McInnes    Notes:
129633174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
129733174efeSLois Curfman McInnes 
129836851e7fSLois Curfman McInnes    Level: intermediate
129936851e7fSLois Curfman McInnes 
130033174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
130133174efeSLois Curfman McInnes 
130233174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
130333174efeSLois Curfman McInnes @*/
130463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetTolerances(SNES snes,PetscReal *abstol,PetscReal *rtol,PetscReal *stol,PetscInt *maxit,PetscInt *maxf)
130533174efeSLois Curfman McInnes {
13063a40ed3dSBarry Smith   PetscFunctionBegin;
13074482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
130870441072SBarry Smith   if (abstol)  *abstol  = snes->abstol;
130933174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
131033174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
131133174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
131233174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
13133a40ed3dSBarry Smith   PetscFunctionReturn(0);
131433174efeSLois Curfman McInnes }
131533174efeSLois Curfman McInnes 
13164a2ae208SSatish Balay #undef __FUNCT__
13174a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
131833174efeSLois Curfman McInnes /*@
13199b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
13209b94acceSBarry Smith 
1321fee21e36SBarry Smith    Collective on SNES
1322fee21e36SBarry Smith 
1323c7afd0dbSLois Curfman McInnes    Input Parameters:
1324c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1325c7afd0dbSLois Curfman McInnes -  tol - tolerance
1326c7afd0dbSLois Curfman McInnes 
13279b94acceSBarry Smith    Options Database Key:
1328c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
13299b94acceSBarry Smith 
133036851e7fSLois Curfman McInnes    Level: intermediate
133136851e7fSLois Curfman McInnes 
13329b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
13339b94acceSBarry Smith 
13342492ecdbSBarry Smith .seealso: SNESSetTolerances()
13359b94acceSBarry Smith @*/
133663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
13379b94acceSBarry Smith {
13383a40ed3dSBarry Smith   PetscFunctionBegin;
13394482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
13409b94acceSBarry Smith   snes->deltatol = tol;
13413a40ed3dSBarry Smith   PetscFunctionReturn(0);
13429b94acceSBarry Smith }
13439b94acceSBarry Smith 
1344df9fa365SBarry Smith /*
1345df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1346df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1347df9fa365SBarry Smith    macros instead of functions
1348df9fa365SBarry Smith */
13494a2ae208SSatish Balay #undef __FUNCT__
13504a2ae208SSatish Balay #define __FUNCT__ "SNESLGMonitor"
135163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESLGMonitor(SNES snes,PetscInt it,PetscReal norm,void *ctx)
1352ce1608b8SBarry Smith {
1353dfbe8321SBarry Smith   PetscErrorCode ierr;
1354ce1608b8SBarry Smith 
1355ce1608b8SBarry Smith   PetscFunctionBegin;
13564482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1357ce1608b8SBarry Smith   ierr = KSPLGMonitor((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1358ce1608b8SBarry Smith   PetscFunctionReturn(0);
1359ce1608b8SBarry Smith }
1360ce1608b8SBarry Smith 
13614a2ae208SSatish Balay #undef __FUNCT__
13624a2ae208SSatish Balay #define __FUNCT__ "SNESLGMonitorCreate"
136363dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESLGMonitorCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
1364df9fa365SBarry Smith {
1365dfbe8321SBarry Smith   PetscErrorCode ierr;
1366df9fa365SBarry Smith 
1367df9fa365SBarry Smith   PetscFunctionBegin;
1368df9fa365SBarry Smith   ierr = KSPLGMonitorCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1369df9fa365SBarry Smith   PetscFunctionReturn(0);
1370df9fa365SBarry Smith }
1371df9fa365SBarry Smith 
13724a2ae208SSatish Balay #undef __FUNCT__
13734a2ae208SSatish Balay #define __FUNCT__ "SNESLGMonitorDestroy"
137463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESLGMonitorDestroy(PetscDrawLG draw)
1375df9fa365SBarry Smith {
1376dfbe8321SBarry Smith   PetscErrorCode ierr;
1377df9fa365SBarry Smith 
1378df9fa365SBarry Smith   PetscFunctionBegin;
1379df9fa365SBarry Smith   ierr = KSPLGMonitorDestroy(draw);CHKERRQ(ierr);
1380df9fa365SBarry Smith   PetscFunctionReturn(0);
1381df9fa365SBarry Smith }
1382df9fa365SBarry Smith 
13839b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
13849b94acceSBarry Smith 
13854a2ae208SSatish Balay #undef __FUNCT__
13864a2ae208SSatish Balay #define __FUNCT__ "SNESSetMonitor"
13879b94acceSBarry Smith /*@C
13885cd90555SBarry Smith    SNESSetMonitor - Sets an ADDITIONAL function that is to be used at every
13899b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
13909b94acceSBarry Smith    progress.
13919b94acceSBarry Smith 
1392fee21e36SBarry Smith    Collective on SNES
1393fee21e36SBarry Smith 
1394c7afd0dbSLois Curfman McInnes    Input Parameters:
1395c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1396c7afd0dbSLois Curfman McInnes .  func - monitoring routine
1397b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
1398e8105e01SRichard Katz           monitor routine (use PETSC_NULL if no context is desired)
1399b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1400b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
14019b94acceSBarry Smith 
1402c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1403a7cc72afSBarry Smith $     int func(SNES snes,PetscInt its, PetscReal norm,void *mctx)
1404c7afd0dbSLois Curfman McInnes 
1405c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1406c7afd0dbSLois Curfman McInnes .    its - iteration number
1407c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
140840a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
14099b94acceSBarry Smith 
14109665c990SLois Curfman McInnes    Options Database Keys:
1411c7afd0dbSLois Curfman McInnes +    -snes_monitor        - sets SNESDefaultMonitor()
1412c7afd0dbSLois Curfman McInnes .    -snes_xmonitor       - sets line graph monitor,
1413c7afd0dbSLois Curfman McInnes                             uses SNESLGMonitorCreate()
1414c7afd0dbSLois Curfman McInnes _    -snes_cancelmonitors - cancels all monitors that have
1415c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
1416c7afd0dbSLois Curfman McInnes                             calls to SNESSetMonitor(), but
1417c7afd0dbSLois Curfman McInnes                             does not cancel those set via
1418c7afd0dbSLois Curfman McInnes                             the options database.
14199665c990SLois Curfman McInnes 
1420639f9d9dSBarry Smith    Notes:
14216bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
14226bc08f3fSLois Curfman McInnes    SNESSetMonitor() multiple times; all will be called in the
14236bc08f3fSLois Curfman McInnes    order in which they were set.
1424639f9d9dSBarry Smith 
142536851e7fSLois Curfman McInnes    Level: intermediate
142636851e7fSLois Curfman McInnes 
14279b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
14289b94acceSBarry Smith 
14295cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESClearMonitor()
14309b94acceSBarry Smith @*/
143163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetMonitor(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void*))
14329b94acceSBarry Smith {
14333a40ed3dSBarry Smith   PetscFunctionBegin;
14344482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1435639f9d9dSBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) {
143629bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1437639f9d9dSBarry Smith   }
1438639f9d9dSBarry Smith   snes->monitor[snes->numbermonitors]           = func;
1439b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
1440639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
14413a40ed3dSBarry Smith   PetscFunctionReturn(0);
14429b94acceSBarry Smith }
14439b94acceSBarry Smith 
14444a2ae208SSatish Balay #undef __FUNCT__
14454a2ae208SSatish Balay #define __FUNCT__ "SNESClearMonitor"
14465cd90555SBarry Smith /*@C
14475cd90555SBarry Smith    SNESClearMonitor - Clears all the monitor functions for a SNES object.
14485cd90555SBarry Smith 
1449c7afd0dbSLois Curfman McInnes    Collective on SNES
1450c7afd0dbSLois Curfman McInnes 
14515cd90555SBarry Smith    Input Parameters:
14525cd90555SBarry Smith .  snes - the SNES context
14535cd90555SBarry Smith 
14541a480d89SAdministrator    Options Database Key:
1455c7afd0dbSLois Curfman McInnes .  -snes_cancelmonitors - cancels all monitors that have been hardwired
1456c7afd0dbSLois Curfman McInnes     into a code by calls to SNESSetMonitor(), but does not cancel those
1457c7afd0dbSLois Curfman McInnes     set via the options database
14585cd90555SBarry Smith 
14595cd90555SBarry Smith    Notes:
14605cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
14615cd90555SBarry Smith 
146236851e7fSLois Curfman McInnes    Level: intermediate
146336851e7fSLois Curfman McInnes 
14645cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
14655cd90555SBarry Smith 
14665cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESSetMonitor()
14675cd90555SBarry Smith @*/
146863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESClearMonitor(SNES snes)
14695cd90555SBarry Smith {
1470d952e501SBarry Smith   PetscErrorCode ierr;
1471d952e501SBarry Smith   PetscInt       i;
1472d952e501SBarry Smith 
14735cd90555SBarry Smith   PetscFunctionBegin;
14744482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1475d952e501SBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
1476d952e501SBarry Smith     if (snes->monitordestroy[i]) {
1477d952e501SBarry Smith       ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr);
1478d952e501SBarry Smith     }
1479d952e501SBarry Smith   }
14805cd90555SBarry Smith   snes->numbermonitors = 0;
14815cd90555SBarry Smith   PetscFunctionReturn(0);
14825cd90555SBarry Smith }
14835cd90555SBarry Smith 
14844a2ae208SSatish Balay #undef __FUNCT__
14854a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
14869b94acceSBarry Smith /*@C
14879b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
14889b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
14899b94acceSBarry Smith 
1490fee21e36SBarry Smith    Collective on SNES
1491fee21e36SBarry Smith 
1492c7afd0dbSLois Curfman McInnes    Input Parameters:
1493c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1494c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
1495c7afd0dbSLois Curfman McInnes -  cctx - [optional] context for private data for the convergence routine
1496c7afd0dbSLois Curfman McInnes           (may be PETSC_NULL)
14979b94acceSBarry Smith 
1498c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1499*06ee9f85SBarry Smith $     PetscErrorCode func (SNES snes,PetscInt it,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
1500c7afd0dbSLois Curfman McInnes 
1501c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1502*06ee9f85SBarry Smith .    it - current iteration (0 is the first and is before any Newton step)
1503c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
1504184914b5SBarry Smith .    reason - reason for convergence/divergence
1505c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
15064b27c08aSLois Curfman McInnes .    gnorm - 2-norm of current step
15074b27c08aSLois Curfman McInnes -    f - 2-norm of function
15089b94acceSBarry Smith 
150936851e7fSLois Curfman McInnes    Level: advanced
151036851e7fSLois Curfman McInnes 
15119b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
15129b94acceSBarry Smith 
15134b27c08aSLois Curfman McInnes .seealso: SNESConverged_LS(), SNESConverged_TR()
15149b94acceSBarry Smith @*/
1515*06ee9f85SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceTest(SNES snes,PetscErrorCode (*func)(SNES,PetscInt,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx)
15169b94acceSBarry Smith {
15173a40ed3dSBarry Smith   PetscFunctionBegin;
15184482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
15199b94acceSBarry Smith   (snes)->converged = func;
15209b94acceSBarry Smith   (snes)->cnvP      = cctx;
15213a40ed3dSBarry Smith   PetscFunctionReturn(0);
15229b94acceSBarry Smith }
15239b94acceSBarry Smith 
15244a2ae208SSatish Balay #undef __FUNCT__
15254a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
152652baeb72SSatish Balay /*@
1527184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
1528184914b5SBarry Smith 
1529184914b5SBarry Smith    Not Collective
1530184914b5SBarry Smith 
1531184914b5SBarry Smith    Input Parameter:
1532184914b5SBarry Smith .  snes - the SNES context
1533184914b5SBarry Smith 
1534184914b5SBarry Smith    Output Parameter:
1535e090d566SSatish Balay .  reason - negative value indicates diverged, positive value converged, see petscsnes.h or the
1536184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
1537184914b5SBarry Smith 
1538184914b5SBarry Smith    Level: intermediate
1539184914b5SBarry Smith 
1540184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
1541184914b5SBarry Smith 
1542184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
1543184914b5SBarry Smith 
15444b27c08aSLois Curfman McInnes .seealso: SNESSetConvergenceTest(), SNESConverged_LS(), SNESConverged_TR(), SNESConvergedReason
1545184914b5SBarry Smith @*/
154663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
1547184914b5SBarry Smith {
1548184914b5SBarry Smith   PetscFunctionBegin;
15494482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
15504482741eSBarry Smith   PetscValidPointer(reason,2);
1551184914b5SBarry Smith   *reason = snes->reason;
1552184914b5SBarry Smith   PetscFunctionReturn(0);
1553184914b5SBarry Smith }
1554184914b5SBarry Smith 
15554a2ae208SSatish Balay #undef __FUNCT__
15564a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
1557c9005455SLois Curfman McInnes /*@
1558c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
1559c9005455SLois Curfman McInnes 
1560fee21e36SBarry Smith    Collective on SNES
1561fee21e36SBarry Smith 
1562c7afd0dbSLois Curfman McInnes    Input Parameters:
1563c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
1564c7afd0dbSLois Curfman McInnes .  a   - array to hold history
1565cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
1566758f92a0SBarry Smith .  na  - size of a and its
156764731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
1568758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
1569c7afd0dbSLois Curfman McInnes 
1570c9005455SLois Curfman McInnes    Notes:
15714b27c08aSLois Curfman McInnes    If set, this array will contain the function norms computed
1572c9005455SLois Curfman McInnes    at each step.
1573c9005455SLois Curfman McInnes 
1574c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
1575c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
1576c9005455SLois Curfman McInnes    during the section of code that is being timed.
1577c9005455SLois Curfman McInnes 
157836851e7fSLois Curfman McInnes    Level: intermediate
157936851e7fSLois Curfman McInnes 
1580c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
1581758f92a0SBarry Smith 
158208405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
1583758f92a0SBarry Smith 
1584c9005455SLois Curfman McInnes @*/
158563dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetConvergenceHistory(SNES snes,PetscReal a[],PetscInt *its,PetscInt na,PetscTruth reset)
1586c9005455SLois Curfman McInnes {
15873a40ed3dSBarry Smith   PetscFunctionBegin;
15884482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
15894482741eSBarry Smith   if (na) PetscValidScalarPointer(a,2);
1590c9005455SLois Curfman McInnes   snes->conv_hist       = a;
1591758f92a0SBarry Smith   snes->conv_hist_its   = its;
1592758f92a0SBarry Smith   snes->conv_hist_max   = na;
1593758f92a0SBarry Smith   snes->conv_hist_reset = reset;
1594758f92a0SBarry Smith   PetscFunctionReturn(0);
1595758f92a0SBarry Smith }
1596758f92a0SBarry Smith 
15974a2ae208SSatish Balay #undef __FUNCT__
15984a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
15990c4c9dddSBarry Smith /*@C
1600758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
1601758f92a0SBarry Smith 
1602758f92a0SBarry Smith    Collective on SNES
1603758f92a0SBarry Smith 
1604758f92a0SBarry Smith    Input Parameter:
1605758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
1606758f92a0SBarry Smith 
1607758f92a0SBarry Smith    Output Parameters:
1608758f92a0SBarry Smith .  a   - array to hold history
1609758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
1610758f92a0SBarry Smith          negative if not converged) for each solve.
1611758f92a0SBarry Smith -  na  - size of a and its
1612758f92a0SBarry Smith 
1613758f92a0SBarry Smith    Notes:
1614758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
1615758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
1616758f92a0SBarry Smith 
1617758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
1618758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
1619758f92a0SBarry Smith    during the section of code that is being timed.
1620758f92a0SBarry Smith 
1621758f92a0SBarry Smith    Level: intermediate
1622758f92a0SBarry Smith 
1623758f92a0SBarry Smith .keywords: SNES, get, convergence, history
1624758f92a0SBarry Smith 
1625758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
1626758f92a0SBarry Smith 
1627758f92a0SBarry Smith @*/
162863dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetConvergenceHistory(SNES snes,PetscReal *a[],PetscInt *its[],PetscInt *na)
1629758f92a0SBarry Smith {
1630758f92a0SBarry Smith   PetscFunctionBegin;
16314482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
1632758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
1633758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
1634758f92a0SBarry Smith   if (na) *na   = snes->conv_hist_len;
16353a40ed3dSBarry Smith   PetscFunctionReturn(0);
1636c9005455SLois Curfman McInnes }
1637c9005455SLois Curfman McInnes 
1638e74ef692SMatthew Knepley #undef __FUNCT__
1639e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
1640ac226902SBarry Smith /*@C
164176b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
16427e4bb74cSBarry Smith   at the beginning o every iteration of the nonlinear solve. Specifically
16437e4bb74cSBarry Smith   it is called just before the Jacobian is "evaluated".
164476b2cf59SMatthew Knepley 
164576b2cf59SMatthew Knepley   Collective on SNES
164676b2cf59SMatthew Knepley 
164776b2cf59SMatthew Knepley   Input Parameters:
164876b2cf59SMatthew Knepley . snes - The nonlinear solver context
164976b2cf59SMatthew Knepley . func - The function
165076b2cf59SMatthew Knepley 
165176b2cf59SMatthew Knepley   Calling sequence of func:
1652b5d30489SBarry Smith . func (SNES snes, PetscInt step);
165376b2cf59SMatthew Knepley 
165476b2cf59SMatthew Knepley . step - The current step of the iteration
165576b2cf59SMatthew Knepley 
165676b2cf59SMatthew Knepley   Level: intermediate
165776b2cf59SMatthew Knepley 
165876b2cf59SMatthew Knepley .keywords: SNES, update
1659b5d30489SBarry Smith 
166076b2cf59SMatthew Knepley .seealso SNESDefaultUpdate(), SNESSetRhsBC(), SNESSetSolutionBC()
166176b2cf59SMatthew Knepley @*/
166263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetUpdate(SNES snes, PetscErrorCode (*func)(SNES, PetscInt))
166376b2cf59SMatthew Knepley {
166476b2cf59SMatthew Knepley   PetscFunctionBegin;
16654482741eSBarry Smith   PetscValidHeaderSpecific(snes, SNES_COOKIE,1);
166676b2cf59SMatthew Knepley   snes->update = func;
166776b2cf59SMatthew Knepley   PetscFunctionReturn(0);
166876b2cf59SMatthew Knepley }
166976b2cf59SMatthew Knepley 
1670e74ef692SMatthew Knepley #undef __FUNCT__
1671e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
167276b2cf59SMatthew Knepley /*@
167376b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
167476b2cf59SMatthew Knepley 
167576b2cf59SMatthew Knepley   Not collective
167676b2cf59SMatthew Knepley 
167776b2cf59SMatthew Knepley   Input Parameters:
167876b2cf59SMatthew Knepley . snes - The nonlinear solver context
167976b2cf59SMatthew Knepley . step - The current step of the iteration
168076b2cf59SMatthew Knepley 
1681205452f4SMatthew Knepley   Level: intermediate
1682205452f4SMatthew Knepley 
168376b2cf59SMatthew Knepley .keywords: SNES, update
168476b2cf59SMatthew Knepley .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultSolutionBC()
168576b2cf59SMatthew Knepley @*/
168663dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESDefaultUpdate(SNES snes, PetscInt step)
168776b2cf59SMatthew Knepley {
168876b2cf59SMatthew Knepley   PetscFunctionBegin;
168976b2cf59SMatthew Knepley   PetscFunctionReturn(0);
169076b2cf59SMatthew Knepley }
169176b2cf59SMatthew Knepley 
16924a2ae208SSatish Balay #undef __FUNCT__
16934a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
16949b94acceSBarry Smith /*
16959b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
16969b94acceSBarry Smith    positive parameter delta.
16979b94acceSBarry Smith 
16989b94acceSBarry Smith     Input Parameters:
1699c7afd0dbSLois Curfman McInnes +   snes - the SNES context
17009b94acceSBarry Smith .   y - approximate solution of linear system
17019b94acceSBarry Smith .   fnorm - 2-norm of current function
1702c7afd0dbSLois Curfman McInnes -   delta - trust region size
17039b94acceSBarry Smith 
17049b94acceSBarry Smith     Output Parameters:
1705c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
17069b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
17079b94acceSBarry Smith     region, and exceeds zero otherwise.
1708c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
17099b94acceSBarry Smith 
17109b94acceSBarry Smith     Note:
17114b27c08aSLois Curfman McInnes     For non-trust region methods such as SNESLS, the parameter delta
17129b94acceSBarry Smith     is set to be the maximum allowable step size.
17139b94acceSBarry Smith 
17149b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
17159b94acceSBarry Smith */
1716dfbe8321SBarry Smith PetscErrorCode SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
17179b94acceSBarry Smith {
1718064f8208SBarry Smith   PetscReal      nrm;
1719ea709b57SSatish Balay   PetscScalar    cnorm;
1720dfbe8321SBarry Smith   PetscErrorCode ierr;
17213a40ed3dSBarry Smith 
17223a40ed3dSBarry Smith   PetscFunctionBegin;
17234482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
17244482741eSBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE,2);
1725c9780b6fSBarry Smith   PetscCheckSameComm(snes,1,y,2);
1726184914b5SBarry Smith 
1727064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
1728064f8208SBarry Smith   if (nrm > *delta) {
1729064f8208SBarry Smith      nrm = *delta/nrm;
1730064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
1731064f8208SBarry Smith      cnorm = nrm;
17322dcb1b2aSMatthew Knepley      ierr = VecScale(y,cnorm);CHKERRQ(ierr);
17339b94acceSBarry Smith      *ynorm = *delta;
17349b94acceSBarry Smith   } else {
17359b94acceSBarry Smith      *gpnorm = 0.0;
1736064f8208SBarry Smith      *ynorm = nrm;
17379b94acceSBarry Smith   }
17383a40ed3dSBarry Smith   PetscFunctionReturn(0);
17399b94acceSBarry Smith }
17409b94acceSBarry Smith 
17414a2ae208SSatish Balay #undef __FUNCT__
17424a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
17436ce558aeSBarry Smith /*@C
1744f69a0ea3SMatthew Knepley    SNESSolve - Solves a nonlinear system F(x) = b.
1745f69a0ea3SMatthew Knepley    Call SNESSolve() after calling SNESCreate() and optional routines of the form SNESSetXXX().
17469b94acceSBarry Smith 
1747c7afd0dbSLois Curfman McInnes    Collective on SNES
1748c7afd0dbSLois Curfman McInnes 
1749b2002411SLois Curfman McInnes    Input Parameters:
1750c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1751f69a0ea3SMatthew Knepley .  b - the constant part of the equation, or PETSC_NULL to use zero.
175270e92668SMatthew Knepley -  x - the solution vector, or PETSC_NULL if it was set with SNESSetSolution()
17539b94acceSBarry Smith 
1754b2002411SLois Curfman McInnes    Notes:
17558ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
17568ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
17578ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
17588ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
17598ddd3da0SLois Curfman McInnes 
176036851e7fSLois Curfman McInnes    Level: beginner
176136851e7fSLois Curfman McInnes 
17629b94acceSBarry Smith .keywords: SNES, nonlinear, solve
17639b94acceSBarry Smith 
176470e92668SMatthew Knepley .seealso: SNESCreate(), SNESDestroy(), SNESSetFunction(), SNESSetJacobian(), SNESSetRhs(), SNESSetSolution()
17659b94acceSBarry Smith @*/
1766f69a0ea3SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSolve(SNES snes,Vec b,Vec x)
17679b94acceSBarry Smith {
1768dfbe8321SBarry Smith   PetscErrorCode ierr;
1769f1af5d2fSBarry Smith   PetscTruth     flg;
1770eabae89aSBarry Smith   char           filename[PETSC_MAX_PATH_LEN];
1771eabae89aSBarry Smith   PetscViewer    viewer;
1772052efed2SBarry Smith 
17733a40ed3dSBarry Smith   PetscFunctionBegin;
17744482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
17751302d50aSBarry Smith   if (!snes->solve) SETERRQ(PETSC_ERR_ORDER,"SNESSetType() or SNESSetFromOptions() must be called before SNESSolve()");
177674637425SBarry Smith 
1777f69a0ea3SMatthew Knepley   if (b) {
1778f69a0ea3SMatthew Knepley     ierr = SNESSetRhs(snes, b); CHKERRQ(ierr);
17791096aae1SMatthew Knepley     if (!snes->vec_func) {
17801096aae1SMatthew Knepley       Vec r;
17811096aae1SMatthew Knepley 
17821096aae1SMatthew Knepley       ierr = VecDuplicate(b, &r); CHKERRQ(ierr);
17831096aae1SMatthew Knepley       ierr = SNESSetFunction(snes, r, PETSC_NULL, PETSC_NULL); CHKERRQ(ierr);
17841096aae1SMatthew Knepley     }
1785f69a0ea3SMatthew Knepley   }
178670e92668SMatthew Knepley   if (x) {
1787f69a0ea3SMatthew Knepley     PetscValidHeaderSpecific(x,VEC_COOKIE,3);
1788f69a0ea3SMatthew Knepley     PetscCheckSameComm(snes,1,x,3);
178970e92668SMatthew Knepley   } else {
179070e92668SMatthew Knepley     ierr = SNESGetSolution(snes, &x); CHKERRQ(ierr);
179170e92668SMatthew Knepley     if (!x) {
179270e92668SMatthew Knepley       ierr = VecDuplicate(snes->vec_func_always, &x); CHKERRQ(ierr);
179370e92668SMatthew Knepley     }
179470e92668SMatthew Knepley   }
179570e92668SMatthew Knepley   snes->vec_sol = snes->vec_sol_always = x;
179670e92668SMatthew Knepley   if (!snes->setupcalled) {
179770e92668SMatthew Knepley     ierr = SNESSetUp(snes);CHKERRQ(ierr);
179870e92668SMatthew Knepley   }
1799abc0a331SBarry Smith   if (snes->conv_hist_reset) snes->conv_hist_len = 0;
1800d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
180150ffb88aSMatthew Knepley   snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
1802d5e45103SBarry Smith 
1803d5e45103SBarry Smith   ierr = PetscExceptionTry1((*(snes)->solve)(snes),PETSC_ERR_ARG_DOMAIN);
1804d5e45103SBarry Smith   if (PetscExceptionValue(ierr)) {
180538f152feSBarry Smith     /* this means that a caller above me has also tryed this exception so I don't handle it here, pass it up */
180619717074SBarry Smith     PetscErrorCode pierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(pierr);
1807d5e45103SBarry Smith   } else if (PetscExceptionCaught(ierr,PETSC_ERR_ARG_DOMAIN)) {
1808d5e45103SBarry Smith     /* translate exception into SNES not converged reason */
1809d5e45103SBarry Smith     snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN;
181038f152feSBarry Smith     ierr = 0;
181138f152feSBarry Smith   }
181238f152feSBarry Smith   CHKERRQ(ierr);
1813d5e45103SBarry Smith 
1814d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
1815eabae89aSBarry Smith   ierr = PetscOptionsGetString(snes->prefix,"-snes_view",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1816eabae89aSBarry Smith   if (flg && !PetscPreLoadingOn) {
1817eabae89aSBarry Smith     ierr = PetscViewerASCIIOpen(snes->comm,filename,&viewer);CHKERRQ(ierr);
1818eabae89aSBarry Smith     ierr = SNESView(snes,viewer);CHKERRQ(ierr);
1819eabae89aSBarry Smith     ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr);
1820eabae89aSBarry Smith   }
1821eabae89aSBarry Smith 
1822da9b6338SBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_test_local_min",&flg);CHKERRQ(ierr);
1823da9b6338SBarry Smith   if (flg && !PetscPreLoadingOn) { ierr = SNESTestLocalMin(snes);CHKERRQ(ierr); }
18245968eb51SBarry Smith   if (snes->printreason) {
18255968eb51SBarry Smith     if (snes->reason > 0) {
18269dcbbd2bSBarry Smith       ierr = PetscPrintf(snes->comm,"Nonlinear solve converged due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
18275968eb51SBarry Smith     } else {
18289dcbbd2bSBarry Smith       ierr = PetscPrintf(snes->comm,"Nonlinear solve did not converge due to %s\n",SNESConvergedReasons[snes->reason]);CHKERRQ(ierr);
18295968eb51SBarry Smith     }
18305968eb51SBarry Smith   }
18315968eb51SBarry Smith 
18323a40ed3dSBarry Smith   PetscFunctionReturn(0);
18339b94acceSBarry Smith }
18349b94acceSBarry Smith 
18359b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
18369b94acceSBarry Smith 
18374a2ae208SSatish Balay #undef __FUNCT__
18384a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
183982bf6240SBarry Smith /*@C
18404b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
18419b94acceSBarry Smith 
1842fee21e36SBarry Smith    Collective on SNES
1843fee21e36SBarry Smith 
1844c7afd0dbSLois Curfman McInnes    Input Parameters:
1845c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1846454a90a3SBarry Smith -  type - a known method
1847c7afd0dbSLois Curfman McInnes 
1848c7afd0dbSLois Curfman McInnes    Options Database Key:
1849454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
1850c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
1851ae12b187SLois Curfman McInnes 
18529b94acceSBarry Smith    Notes:
1853e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
18544b27c08aSLois Curfman McInnes +    SNESLS - Newton's method with line search
1855c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
18564b27c08aSLois Curfman McInnes .    SNESTR - Newton's method with trust region
1857c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
18589b94acceSBarry Smith 
1859ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
1860ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
1861ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
1862ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
1863ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
1864ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
1865ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
1866ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
1867ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
1868b0a32e0cSBarry Smith   appropriate method.
186936851e7fSLois Curfman McInnes 
187036851e7fSLois Curfman McInnes   Level: intermediate
1871a703fe33SLois Curfman McInnes 
1872454a90a3SBarry Smith .keywords: SNES, set, type
1873435da068SBarry Smith 
1874435da068SBarry Smith .seealso: SNESType, SNESCreate()
1875435da068SBarry Smith 
18769b94acceSBarry Smith @*/
1877e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESSetType(SNES snes,SNESType type)
18789b94acceSBarry Smith {
1879dfbe8321SBarry Smith   PetscErrorCode ierr,(*r)(SNES);
18806831982aSBarry Smith   PetscTruth     match;
18813a40ed3dSBarry Smith 
18823a40ed3dSBarry Smith   PetscFunctionBegin;
18834482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
18844482741eSBarry Smith   PetscValidCharPointer(type,2);
188582bf6240SBarry Smith 
18866831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
18870f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
188882bf6240SBarry Smith 
188982bf6240SBarry Smith   if (snes->setupcalled) {
18904dc4c822SBarry Smith     snes->setupcalled = PETSC_FALSE;
1891e1311b90SBarry Smith     ierr              = (*(snes)->destroy)(snes);CHKERRQ(ierr);
189282bf6240SBarry Smith     snes->data        = 0;
189382bf6240SBarry Smith   }
18947d1a2b2bSBarry Smith 
18959b94acceSBarry Smith   /* Get the function pointers for the iterative method requested */
189682bf6240SBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
1897b9617806SBarry Smith   ierr =  PetscFListFind(snes->comm,SNESList,type,(void (**)(void)) &r);CHKERRQ(ierr);
1898958c9bccSBarry Smith   if (!r) SETERRQ1(PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested SNES type %s",type);
189905b42c5fSBarry Smith   ierr = PetscFree(snes->data);CHKERRQ(ierr);
190082bf6240SBarry Smith   snes->data = 0;
19013a40ed3dSBarry Smith   ierr = (*r)(snes);CHKERRQ(ierr);
1902454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
19033a40ed3dSBarry Smith   PetscFunctionReturn(0);
19049b94acceSBarry Smith }
19059b94acceSBarry Smith 
1906a847f771SSatish Balay 
19079b94acceSBarry Smith /* --------------------------------------------------------------------- */
19084a2ae208SSatish Balay #undef __FUNCT__
19094a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
191052baeb72SSatish Balay /*@
19119b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
1912f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
19139b94acceSBarry Smith 
1914fee21e36SBarry Smith    Not Collective
1915fee21e36SBarry Smith 
191636851e7fSLois Curfman McInnes    Level: advanced
191736851e7fSLois Curfman McInnes 
19189b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
19199b94acceSBarry Smith 
19209b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
19219b94acceSBarry Smith @*/
192263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegisterDestroy(void)
19239b94acceSBarry Smith {
1924dfbe8321SBarry Smith   PetscErrorCode ierr;
192582bf6240SBarry Smith 
19263a40ed3dSBarry Smith   PetscFunctionBegin;
192782bf6240SBarry Smith   if (SNESList) {
1928b0a32e0cSBarry Smith     ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
192982bf6240SBarry Smith     SNESList = 0;
19309b94acceSBarry Smith   }
19314c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
19323a40ed3dSBarry Smith   PetscFunctionReturn(0);
19339b94acceSBarry Smith }
19349b94acceSBarry Smith 
19354a2ae208SSatish Balay #undef __FUNCT__
19364a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
19379b94acceSBarry Smith /*@C
19389a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
19399b94acceSBarry Smith 
1940c7afd0dbSLois Curfman McInnes    Not Collective
1941c7afd0dbSLois Curfman McInnes 
19429b94acceSBarry Smith    Input Parameter:
19434b0e389bSBarry Smith .  snes - nonlinear solver context
19449b94acceSBarry Smith 
19459b94acceSBarry Smith    Output Parameter:
19463a7fca6bSBarry Smith .  type - SNES method (a character string)
19479b94acceSBarry Smith 
194836851e7fSLois Curfman McInnes    Level: intermediate
194936851e7fSLois Curfman McInnes 
1950454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
19519b94acceSBarry Smith @*/
195263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetType(SNES snes,SNESType *type)
19539b94acceSBarry Smith {
19543a40ed3dSBarry Smith   PetscFunctionBegin;
19554482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
19564482741eSBarry Smith   PetscValidPointer(type,2);
1957454a90a3SBarry Smith   *type = snes->type_name;
19583a40ed3dSBarry Smith   PetscFunctionReturn(0);
19599b94acceSBarry Smith }
19609b94acceSBarry Smith 
19614a2ae208SSatish Balay #undef __FUNCT__
19624a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
196352baeb72SSatish Balay /*@
19649b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
19659b94acceSBarry Smith    stored.
19669b94acceSBarry Smith 
1967c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
1968c7afd0dbSLois Curfman McInnes 
19699b94acceSBarry Smith    Input Parameter:
19709b94acceSBarry Smith .  snes - the SNES context
19719b94acceSBarry Smith 
19729b94acceSBarry Smith    Output Parameter:
19739b94acceSBarry Smith .  x - the solution
19749b94acceSBarry Smith 
197570e92668SMatthew Knepley    Level: intermediate
197636851e7fSLois Curfman McInnes 
19779b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
19789b94acceSBarry Smith 
197970e92668SMatthew Knepley .seealso: SNESSetSolution(), SNESGetFunction(), SNESGetSolutionUpdate()
19809b94acceSBarry Smith @*/
198163dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolution(SNES snes,Vec *x)
19829b94acceSBarry Smith {
19833a40ed3dSBarry Smith   PetscFunctionBegin;
19844482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
19854482741eSBarry Smith   PetscValidPointer(x,2);
19869b94acceSBarry Smith   *x = snes->vec_sol_always;
19873a40ed3dSBarry Smith   PetscFunctionReturn(0);
19889b94acceSBarry Smith }
19899b94acceSBarry Smith 
19904a2ae208SSatish Balay #undef __FUNCT__
199170e92668SMatthew Knepley #define __FUNCT__ "SNESSetSolution"
19927e4bb74cSBarry Smith /*@
199370e92668SMatthew Knepley    SNESSetSolution - Sets the vector where the approximate solution is stored.
199470e92668SMatthew Knepley 
199570e92668SMatthew Knepley    Not Collective, but Vec is parallel if SNES is parallel
199670e92668SMatthew Knepley 
199770e92668SMatthew Knepley    Input Parameters:
199870e92668SMatthew Knepley +  snes - the SNES context
199970e92668SMatthew Knepley -  x - the solution
200070e92668SMatthew Knepley 
200170e92668SMatthew Knepley    Output Parameter:
200270e92668SMatthew Knepley 
200370e92668SMatthew Knepley    Level: intermediate
200470e92668SMatthew Knepley 
200542219521SBarry Smith    Notes: this is not normally used, rather one simply calls SNESSolve() with
200642219521SBarry Smith           the appropriate solution vector.
200742219521SBarry Smith 
200870e92668SMatthew Knepley .keywords: SNES, nonlinear, set, solution
200970e92668SMatthew Knepley 
201070e92668SMatthew Knepley .seealso: SNESGetSolution(), SNESGetFunction(), SNESGetSolutionUpdate()
201170e92668SMatthew Knepley @*/
201270e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESSetSolution(SNES snes,Vec x)
201370e92668SMatthew Knepley {
201470e92668SMatthew Knepley   PetscFunctionBegin;
201570e92668SMatthew Knepley   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
201670e92668SMatthew Knepley   PetscValidHeaderSpecific(x,VEC_COOKIE,2);
201770e92668SMatthew Knepley   PetscCheckSameComm(snes,1,x,2);
201870e92668SMatthew Knepley   snes->vec_sol_always = x;
201970e92668SMatthew Knepley   PetscFunctionReturn(0);
202070e92668SMatthew Knepley }
202170e92668SMatthew Knepley 
202270e92668SMatthew Knepley #undef __FUNCT__
20234a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
202452baeb72SSatish Balay /*@
20259b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
20269b94acceSBarry Smith    stored.
20279b94acceSBarry Smith 
2028c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2029c7afd0dbSLois Curfman McInnes 
20309b94acceSBarry Smith    Input Parameter:
20319b94acceSBarry Smith .  snes - the SNES context
20329b94acceSBarry Smith 
20339b94acceSBarry Smith    Output Parameter:
20349b94acceSBarry Smith .  x - the solution update
20359b94acceSBarry Smith 
203636851e7fSLois Curfman McInnes    Level: advanced
203736851e7fSLois Curfman McInnes 
20389b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
20399b94acceSBarry Smith 
20409b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction
20419b94acceSBarry Smith @*/
204263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESGetSolutionUpdate(SNES snes,Vec *x)
20439b94acceSBarry Smith {
20443a40ed3dSBarry Smith   PetscFunctionBegin;
20454482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
20464482741eSBarry Smith   PetscValidPointer(x,2);
20479b94acceSBarry Smith   *x = snes->vec_sol_update_always;
20483a40ed3dSBarry Smith   PetscFunctionReturn(0);
20499b94acceSBarry Smith }
20509b94acceSBarry Smith 
20514a2ae208SSatish Balay #undef __FUNCT__
20524a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
20539b94acceSBarry Smith /*@C
20543638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
20559b94acceSBarry Smith 
2056c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2057c7afd0dbSLois Curfman McInnes 
20589b94acceSBarry Smith    Input Parameter:
20599b94acceSBarry Smith .  snes - the SNES context
20609b94acceSBarry Smith 
20619b94acceSBarry Smith    Output Parameter:
20627bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
206370e92668SMatthew Knepley .  func - the function (or PETSC_NULL)
206470e92668SMatthew Knepley -  ctx - the function context (or PETSC_NULL)
20659b94acceSBarry Smith 
206636851e7fSLois Curfman McInnes    Level: advanced
206736851e7fSLois Curfman McInnes 
2068a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
20699b94acceSBarry Smith 
20704b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution()
20719b94acceSBarry Smith @*/
207270e92668SMatthew Knepley PetscErrorCode PETSCSNES_DLLEXPORT SNESGetFunction(SNES snes,Vec *r,PetscErrorCode (**func)(SNES,Vec,Vec,void*),void **ctx)
20739b94acceSBarry Smith {
20743a40ed3dSBarry Smith   PetscFunctionBegin;
20754482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
20767bf4e008SBarry Smith   if (r)    *r    = snes->vec_func_always;
207700036973SBarry Smith   if (func) *func = snes->computefunction;
207870e92668SMatthew Knepley   if (ctx)  *ctx  = snes->funP;
20793a40ed3dSBarry Smith   PetscFunctionReturn(0);
20809b94acceSBarry Smith }
20819b94acceSBarry Smith 
20824a2ae208SSatish Balay #undef __FUNCT__
20834a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
20843c7409f5SSatish Balay /*@C
20853c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2086d850072dSLois Curfman McInnes    SNES options in the database.
20873c7409f5SSatish Balay 
2088fee21e36SBarry Smith    Collective on SNES
2089fee21e36SBarry Smith 
2090c7afd0dbSLois Curfman McInnes    Input Parameter:
2091c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2092c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2093c7afd0dbSLois Curfman McInnes 
2094d850072dSLois Curfman McInnes    Notes:
2095a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2096c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2097d850072dSLois Curfman McInnes 
209836851e7fSLois Curfman McInnes    Level: advanced
209936851e7fSLois Curfman McInnes 
21003c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2101a86d99e1SLois Curfman McInnes 
2102a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
21033c7409f5SSatish Balay @*/
210463dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESSetOptionsPrefix(SNES snes,const char prefix[])
21053c7409f5SSatish Balay {
2106dfbe8321SBarry Smith   PetscErrorCode ierr;
21073c7409f5SSatish Balay 
21083a40ed3dSBarry Smith   PetscFunctionBegin;
21094482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2110639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
211194b7f48cSBarry Smith   ierr = KSPSetOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
21123a40ed3dSBarry Smith   PetscFunctionReturn(0);
21133c7409f5SSatish Balay }
21143c7409f5SSatish Balay 
21154a2ae208SSatish Balay #undef __FUNCT__
21164a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
21173c7409f5SSatish Balay /*@C
2118f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2119d850072dSLois Curfman McInnes    SNES options in the database.
21203c7409f5SSatish Balay 
2121fee21e36SBarry Smith    Collective on SNES
2122fee21e36SBarry Smith 
2123c7afd0dbSLois Curfman McInnes    Input Parameters:
2124c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2125c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2126c7afd0dbSLois Curfman McInnes 
2127d850072dSLois Curfman McInnes    Notes:
2128a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2129c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2130d850072dSLois Curfman McInnes 
213136851e7fSLois Curfman McInnes    Level: advanced
213236851e7fSLois Curfman McInnes 
21333c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2134a86d99e1SLois Curfman McInnes 
2135a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
21363c7409f5SSatish Balay @*/
213763dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESAppendOptionsPrefix(SNES snes,const char prefix[])
21383c7409f5SSatish Balay {
2139dfbe8321SBarry Smith   PetscErrorCode ierr;
21403c7409f5SSatish Balay 
21413a40ed3dSBarry Smith   PetscFunctionBegin;
21424482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2143639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
214494b7f48cSBarry Smith   ierr = KSPAppendOptionsPrefix(snes->ksp,prefix);CHKERRQ(ierr);
21453a40ed3dSBarry Smith   PetscFunctionReturn(0);
21463c7409f5SSatish Balay }
21473c7409f5SSatish Balay 
21484a2ae208SSatish Balay #undef __FUNCT__
21494a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
21509ab63eb5SSatish Balay /*@C
21513c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
21523c7409f5SSatish Balay    SNES options in the database.
21533c7409f5SSatish Balay 
2154c7afd0dbSLois Curfman McInnes    Not Collective
2155c7afd0dbSLois Curfman McInnes 
21563c7409f5SSatish Balay    Input Parameter:
21573c7409f5SSatish Balay .  snes - the SNES context
21583c7409f5SSatish Balay 
21593c7409f5SSatish Balay    Output Parameter:
21603c7409f5SSatish Balay .  prefix - pointer to the prefix string used
21613c7409f5SSatish Balay 
21629ab63eb5SSatish Balay    Notes: On the fortran side, the user should pass in a string 'prifix' of
21639ab63eb5SSatish Balay    sufficient length to hold the prefix.
21649ab63eb5SSatish Balay 
216536851e7fSLois Curfman McInnes    Level: advanced
216636851e7fSLois Curfman McInnes 
21673c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2168a86d99e1SLois Curfman McInnes 
2169a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
21703c7409f5SSatish Balay @*/
2171e060cb09SBarry Smith PetscErrorCode PETSCSNES_DLLEXPORT SNESGetOptionsPrefix(SNES snes,const char *prefix[])
21723c7409f5SSatish Balay {
2173dfbe8321SBarry Smith   PetscErrorCode ierr;
21743c7409f5SSatish Balay 
21753a40ed3dSBarry Smith   PetscFunctionBegin;
21764482741eSBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE,1);
2177639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
21783a40ed3dSBarry Smith   PetscFunctionReturn(0);
21793c7409f5SSatish Balay }
21803c7409f5SSatish Balay 
2181b2002411SLois Curfman McInnes 
21824a2ae208SSatish Balay #undef __FUNCT__
21834a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
21843cea93caSBarry Smith /*@C
21853cea93caSBarry Smith   SNESRegister - See SNESRegisterDynamic()
21863cea93caSBarry Smith 
21877f6c08e0SMatthew Knepley   Level: advanced
21883cea93caSBarry Smith @*/
218963dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(SNES))
2190b2002411SLois Curfman McInnes {
2191e2d1d2b7SBarry Smith   char           fullname[PETSC_MAX_PATH_LEN];
2192dfbe8321SBarry Smith   PetscErrorCode ierr;
2193b2002411SLois Curfman McInnes 
2194b2002411SLois Curfman McInnes   PetscFunctionBegin;
2195b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2196c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
2197b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2198b2002411SLois Curfman McInnes }
2199da9b6338SBarry Smith 
2200da9b6338SBarry Smith #undef __FUNCT__
2201da9b6338SBarry Smith #define __FUNCT__ "SNESTestLocalMin"
220263dd3a1aSKris Buschelman PetscErrorCode PETSCSNES_DLLEXPORT SNESTestLocalMin(SNES snes)
2203da9b6338SBarry Smith {
2204dfbe8321SBarry Smith   PetscErrorCode ierr;
220577431f27SBarry Smith   PetscInt       N,i,j;
2206da9b6338SBarry Smith   Vec            u,uh,fh;
2207da9b6338SBarry Smith   PetscScalar    value;
2208da9b6338SBarry Smith   PetscReal      norm;
2209da9b6338SBarry Smith 
2210da9b6338SBarry Smith   PetscFunctionBegin;
2211da9b6338SBarry Smith   ierr = SNESGetSolution(snes,&u);CHKERRQ(ierr);
2212da9b6338SBarry Smith   ierr = VecDuplicate(u,&uh);CHKERRQ(ierr);
2213da9b6338SBarry Smith   ierr = VecDuplicate(u,&fh);CHKERRQ(ierr);
2214da9b6338SBarry Smith 
2215da9b6338SBarry Smith   /* currently only works for sequential */
2216da9b6338SBarry Smith   ierr = PetscPrintf(PETSC_COMM_WORLD,"Testing FormFunction() for local min\n");
2217da9b6338SBarry Smith   ierr = VecGetSize(u,&N);CHKERRQ(ierr);
2218da9b6338SBarry Smith   for (i=0; i<N; i++) {
2219da9b6338SBarry Smith     ierr = VecCopy(u,uh);CHKERRQ(ierr);
222077431f27SBarry Smith     ierr  = PetscPrintf(PETSC_COMM_WORLD,"i = %D\n",i);CHKERRQ(ierr);
2221da9b6338SBarry Smith     for (j=-10; j<11; j++) {
2222ccae9161SBarry Smith       value = PetscSign(j)*exp(PetscAbs(j)-10.0);
2223da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
22243ab0aad5SBarry Smith       ierr  = SNESComputeFunction(snes,uh,fh);CHKERRQ(ierr);
2225da9b6338SBarry Smith       ierr  = VecNorm(fh,NORM_2,&norm);CHKERRQ(ierr);
222677431f27SBarry Smith       ierr  = PetscPrintf(PETSC_COMM_WORLD,"       j norm %D %18.16e\n",j,norm);CHKERRQ(ierr);
2227da9b6338SBarry Smith       value = -value;
2228da9b6338SBarry Smith       ierr  = VecSetValue(uh,i,value,ADD_VALUES);CHKERRQ(ierr);
2229da9b6338SBarry Smith     }
2230da9b6338SBarry Smith   }
2231da9b6338SBarry Smith   ierr = VecDestroy(uh);CHKERRQ(ierr);
2232da9b6338SBarry Smith   ierr = VecDestroy(fh);CHKERRQ(ierr);
2233da9b6338SBarry Smith   PetscFunctionReturn(0);
2234da9b6338SBarry Smith }
2235