xref: /petsc/src/snes/interface/snes.c (revision 4b27c08ac4c7fe27d83aaa07ba0dc5abca4ca6c8)
173f4d377SMatthew Knepley /*$Id: snes.c,v 1.235 2001/08/21 21:03:49 bsmith Exp $*/
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 */
98ba1e511SMatthew Knepley int SNES_COOKIE;
108ba1e511SMatthew Knepley int MATSNESMFCTX_COOKIE;
11*4b27c08aSLois Curfman McInnes int SNES_Solve, SNES_LineSearch, SNES_FunctionEval, SNES_JacobianEval;
12a09944afSBarry Smith 
13a09944afSBarry Smith #undef __FUNCT__
144a2ae208SSatish Balay #define __FUNCT__ "SNESView"
157e2c5f70SBarry Smith /*@C
169b94acceSBarry Smith    SNESView - Prints the SNES data structure.
179b94acceSBarry Smith 
184c49b128SBarry Smith    Collective on SNES
19fee21e36SBarry Smith 
20c7afd0dbSLois Curfman McInnes    Input Parameters:
21c7afd0dbSLois Curfman McInnes +  SNES - the SNES context
22c7afd0dbSLois Curfman McInnes -  viewer - visualization context
23c7afd0dbSLois Curfman McInnes 
249b94acceSBarry Smith    Options Database Key:
25c8a8ba5cSLois Curfman McInnes .  -snes_view - Calls SNESView() at end of SNESSolve()
269b94acceSBarry Smith 
279b94acceSBarry Smith    Notes:
289b94acceSBarry Smith    The available visualization contexts include
29b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
30b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
31c8a8ba5cSLois Curfman McInnes          output where only the first processor opens
32c8a8ba5cSLois Curfman McInnes          the file.  All other processors send their
33c8a8ba5cSLois Curfman McInnes          data to the first processor to print.
349b94acceSBarry Smith 
353e081fefSLois Curfman McInnes    The user can open an alternative visualization context with
36b0a32e0cSBarry Smith    PetscViewerASCIIOpen() - output to a specified file.
379b94acceSBarry Smith 
3836851e7fSLois Curfman McInnes    Level: beginner
3936851e7fSLois Curfman McInnes 
409b94acceSBarry Smith .keywords: SNES, view
419b94acceSBarry Smith 
42b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
439b94acceSBarry Smith @*/
44b0a32e0cSBarry Smith int SNESView(SNES snes,PetscViewer viewer)
459b94acceSBarry Smith {
469b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
479b94acceSBarry Smith   int                 ierr;
489b94acceSBarry Smith   SLES                sles;
49454a90a3SBarry Smith   char                *type;
506831982aSBarry Smith   PetscTruth          isascii,isstring;
519b94acceSBarry Smith 
523a40ed3dSBarry Smith   PetscFunctionBegin;
5374679c65SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
54b0a32e0cSBarry Smith   if (!viewer) viewer = PETSC_VIEWER_STDOUT_(snes->comm);
55b0a32e0cSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE);
566831982aSBarry Smith   PetscCheckSameComm(snes,viewer);
5774679c65SBarry Smith 
58b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
59b0a32e0cSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);CHKERRQ(ierr);
600f5bd95cSBarry Smith   if (isascii) {
613a7fca6bSBarry Smith     if (snes->prefix) {
623a7fca6bSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:(%s)\n",snes->prefix);CHKERRQ(ierr);
633a7fca6bSBarry Smith     } else {
64b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"SNES Object:\n");CHKERRQ(ierr);
653a7fca6bSBarry Smith     }
66454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
67454a90a3SBarry Smith     if (type) {
68b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: %s\n",type);CHKERRQ(ierr);
69184914b5SBarry Smith     } else {
70b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"  type: not set yet\n");CHKERRQ(ierr);
71184914b5SBarry Smith     }
720ef38995SBarry Smith     if (snes->view) {
73b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
740ef38995SBarry Smith       ierr = (*snes->view)(snes,viewer);CHKERRQ(ierr);
75b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
760ef38995SBarry Smith     }
77b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum iterations=%d, maximum function evaluations=%d\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr);
7877d8c4bbSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  tolerances: relative=%g, absolute=%g, solution=%g\n",
7977d8c4bbSBarry Smith                  snes->rtol,snes->atol,snes->xtol);CHKERRQ(ierr);
80b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%d\n",snes->linear_its);CHKERRQ(ierr);
81b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  total number of function evaluations=%d\n",snes->nfuncs);CHKERRQ(ierr);
829b94acceSBarry Smith     if (snes->ksp_ewconv) {
839b94acceSBarry Smith       kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
849b94acceSBarry Smith       if (kctx) {
85b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"  Eisenstat-Walker computation of KSP relative tolerance (version %d)\n",kctx->version);CHKERRQ(ierr);
86b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    rtol_0=%g, rtol_max=%g, threshold=%g\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr);
87b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"    gamma=%g, alpha=%g, alpha2=%g\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr);
889b94acceSBarry Smith       }
899b94acceSBarry Smith     }
900f5bd95cSBarry Smith   } else if (isstring) {
91454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
92b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr);
9319bcc07fSBarry Smith   }
9477ed5343SBarry Smith   ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr);
95b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
969b94acceSBarry Smith   ierr = SLESView(sles,viewer);CHKERRQ(ierr);
97b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
983a40ed3dSBarry Smith   PetscFunctionReturn(0);
999b94acceSBarry Smith }
1009b94acceSBarry Smith 
10176b2cf59SMatthew Knepley /*
10276b2cf59SMatthew Knepley   We retain a list of functions that also take SNES command
10376b2cf59SMatthew Knepley   line options. These are called at the end SNESSetFromOptions()
10476b2cf59SMatthew Knepley */
10576b2cf59SMatthew Knepley #define MAXSETFROMOPTIONS 5
10676b2cf59SMatthew Knepley static int numberofsetfromoptions;
10776b2cf59SMatthew Knepley static int (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES);
10876b2cf59SMatthew Knepley 
109e74ef692SMatthew Knepley #undef __FUNCT__
110e74ef692SMatthew Knepley #define __FUNCT__ "SNESAddOptionsChecker"
11176b2cf59SMatthew Knepley /*@
11276b2cf59SMatthew Knepley   SNESAddOptionsChecker - Adds an additional function to check for SNES options.
11376b2cf59SMatthew Knepley 
11476b2cf59SMatthew Knepley   Not Collective
11576b2cf59SMatthew Knepley 
11676b2cf59SMatthew Knepley   Input Parameter:
11776b2cf59SMatthew Knepley . snescheck - function that checks for options
11876b2cf59SMatthew Knepley 
11976b2cf59SMatthew Knepley   Level: developer
12076b2cf59SMatthew Knepley 
12176b2cf59SMatthew Knepley .seealso: SNESSetFromOptions()
12276b2cf59SMatthew Knepley @*/
12376b2cf59SMatthew Knepley int SNESAddOptionsChecker(int (*snescheck)(SNES))
12476b2cf59SMatthew Knepley {
12576b2cf59SMatthew Knepley   PetscFunctionBegin;
12676b2cf59SMatthew Knepley   if (numberofsetfromoptions >= MAXSETFROMOPTIONS) {
12776b2cf59SMatthew Knepley     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Too many options checkers, only %d allowed", MAXSETFROMOPTIONS);
12876b2cf59SMatthew Knepley   }
12976b2cf59SMatthew Knepley 
13076b2cf59SMatthew Knepley   othersetfromoptions[numberofsetfromoptions++] = snescheck;
13176b2cf59SMatthew Knepley   PetscFunctionReturn(0);
13276b2cf59SMatthew Knepley }
13376b2cf59SMatthew Knepley 
1344a2ae208SSatish Balay #undef __FUNCT__
1354a2ae208SSatish Balay #define __FUNCT__ "SNESSetFromOptions"
1369b94acceSBarry Smith /*@
137682d7d0cSBarry Smith    SNESSetFromOptions - Sets various SNES and SLES parameters from user options.
1389b94acceSBarry Smith 
139c7afd0dbSLois Curfman McInnes    Collective on SNES
140c7afd0dbSLois Curfman McInnes 
1419b94acceSBarry Smith    Input Parameter:
1429b94acceSBarry Smith .  snes - the SNES context
1439b94acceSBarry Smith 
14436851e7fSLois Curfman McInnes    Options Database Keys:
1456831982aSBarry Smith +  -snes_type <type> - ls, tr, umls, umtr, test
14682738288SBarry Smith .  -snes_stol - convergence tolerance in terms of the norm
14782738288SBarry Smith                 of the change in the solution between steps
148b39c3a46SLois Curfman McInnes .  -snes_atol <atol> - absolute tolerance of residual norm
149b39c3a46SLois Curfman McInnes .  -snes_rtol <rtol> - relative decrease in tolerance norm from initial
150b39c3a46SLois Curfman McInnes .  -snes_max_it <max_it> - maximum number of iterations
151b39c3a46SLois Curfman McInnes .  -snes_max_funcs <max_funcs> - maximum number of function evaluations
15250ffb88aSMatthew Knepley .  -snes_max_fail <max_fail> - maximum number of failures
153b39c3a46SLois Curfman McInnes .  -snes_trtol <trtol> - trust region tolerance
15482738288SBarry Smith .  -snes_no_convergence_test - skip convergence test in nonlinear or minimization
15582738288SBarry Smith                                solver; hence iterations will continue until max_it
1561fbbfb26SLois Curfman McInnes                                or some other criterion is reached. Saves expense
15782738288SBarry Smith                                of convergence test
15882738288SBarry Smith .  -snes_monitor - prints residual norm at each iteration
159d132466eSBarry Smith .  -snes_vecmonitor - plots solution at each iteration
160d132466eSBarry Smith .  -snes_vecmonitor_update - plots update to solution at each iteration
16182738288SBarry Smith .  -snes_xmonitor - plots residual norm at each iteration
162e24b481bSBarry Smith .  -snes_fd - use finite differences to compute Jacobian; very slow, only for testing
16336851e7fSLois Curfman McInnes -  -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration
16482738288SBarry Smith 
16582738288SBarry Smith     Options Database for Eisenstat-Walker method:
166*4b27c08aSLois Curfman McInnes +  -snes_ksp_ew_conv - use Eisenstat-Walker method for determining linear system convergence
167*4b27c08aSLois 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 @*/
1859b94acceSBarry Smith int SNESSetFromOptions(SNES snes)
1869b94acceSBarry Smith {
1879b94acceSBarry Smith   SLES                sles;
188186905e3SBarry Smith   SNES_KSP_EW_ConvCtx *kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
189f1af5d2fSBarry Smith   PetscTruth          flg;
19076b2cf59SMatthew Knepley   int                 ierr, i;
191186905e3SBarry Smith   char                *deft,type[256];
1929b94acceSBarry Smith 
1933a40ed3dSBarry Smith   PetscFunctionBegin;
19477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
195ca161407SBarry Smith 
196b0a32e0cSBarry Smith   ierr = PetscOptionsBegin(snes->comm,snes->prefix,"Nonlinear solver (SNES) options","SNES");CHKERRQ(ierr);
197186905e3SBarry Smith     if (snes->type_name) {
198186905e3SBarry Smith       deft = snes->type_name;
199186905e3SBarry Smith     } else {
200*4b27c08aSLois Curfman McInnes       deft = SNESLS;
201d64ed03dSBarry Smith     }
2024bbc92c1SBarry Smith 
203186905e3SBarry Smith     if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
204b0a32e0cSBarry Smith     ierr = PetscOptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr);
205d64ed03dSBarry Smith     if (flg) {
206186905e3SBarry Smith       ierr = SNESSetType(snes,type);CHKERRQ(ierr);
207186905e3SBarry Smith     } else if (!snes->type_name) {
208186905e3SBarry Smith       ierr = SNESSetType(snes,deft);CHKERRQ(ierr);
209d64ed03dSBarry Smith     }
210909c8a9fSBarry Smith     ierr = PetscOptionsName("-snes_view","Print detailed information on solver used","SNESView",0);CHKERRQ(ierr);
21193c39befSBarry Smith 
21287828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_stol","Stop if step length less then","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr);
21387828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_atol","Stop if function norm less then","SNESSetTolerances",snes->atol,&snes->atol,0);CHKERRQ(ierr);
214186905e3SBarry Smith 
21587828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_rtol","Stop if decrease in function norm less then","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr);
216b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr);
217b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr);
21850ffb88aSMatthew Knepley     ierr = PetscOptionsInt("-snes_max_fail","Maximum failures","SNESSetTolerances",snes->maxFailures,&snes->maxFailures,PETSC_NULL);CHKERRQ(ierr);
219186905e3SBarry Smith 
220b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_ksp_ew_conv","Use Eisentat-Walker linear system convergence test","SNES_KSP_SetParametersEW",&snes->ksp_ewconv);CHKERRQ(ierr);
221186905e3SBarry Smith 
222b0a32e0cSBarry Smith     ierr = PetscOptionsInt("-snes_ksp_ew_version","Version 1 or 2","SNES_KSP_SetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr);
22387828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNES_KSP_SetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr);
22487828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNES_KSP_SetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr);
22587828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNES_KSP_SetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr);
22687828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha","1 < alpha <= 2","SNES_KSP_SetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr);
22787828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_alpha2","alpha2","SNES_KSP_SetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr);
22887828ca2SBarry Smith     ierr = PetscOptionsReal("-snes_ksp_ew_threshold","0 < threshold < 1","SNES_KSP_SetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr);
229186905e3SBarry Smith 
230b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_no_convergence_test","Don't test for convergence","None",&flg);CHKERRQ(ierr);
23193c39befSBarry Smith     if (flg) {snes->converged = 0;}
232b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_cancelmonitors","Remove all monitors","SNESClearMonitor",&flg);CHKERRQ(ierr);
2335cd90555SBarry Smith     if (flg) {ierr = SNESClearMonitor(snes);CHKERRQ(ierr);}
234b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_monitor","Monitor norm of function","SNESDefaultMonitor",&flg);CHKERRQ(ierr);
235b8a78c4aSBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultMonitor,0,0);CHKERRQ(ierr);}
2363a7fca6bSBarry Smith     ierr = PetscOptionsName("-snes_ratiomonitor","Monitor norm of function","SNESSetRatioMonitor",&flg);CHKERRQ(ierr);
2373a7fca6bSBarry Smith     if (flg) {ierr = SNESSetRatioMonitor(snes);CHKERRQ(ierr);}
238b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_smonitor","Monitor norm of function (fewer digits)","SNESDefaultSMonitor",&flg);CHKERRQ(ierr);
239b8a78c4aSBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultSMonitor,0,0);CHKERRQ(ierr);}
240b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_vecmonitor","Plot solution at each iteration","SNESVecViewMonitor",&flg);CHKERRQ(ierr);
241b8a78c4aSBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewMonitor,0,0);CHKERRQ(ierr);}
242b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_vecmonitor_update","Plot correction at each iteration","SNESVecViewUpdateMonitor",&flg);CHKERRQ(ierr);
2437c922b88SBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewUpdateMonitor,0,0);CHKERRQ(ierr);}
2445ed2d596SBarry Smith     ierr = PetscOptionsName("-snes_vecmonitor_residual","Plot residual at each iteration","SNESVecViewResidualMonitor",&flg);CHKERRQ(ierr);
2455ed2d596SBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewResidualMonitor,0,0);CHKERRQ(ierr);}
246b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_xmonitor","Plot function norm at each iteration","SNESLGMonitor",&flg);CHKERRQ(ierr);
247186905e3SBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESLGMonitor,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
248e24b481bSBarry Smith 
249b0a32e0cSBarry Smith     ierr = PetscOptionsName("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",&flg);CHKERRQ(ierr);
250*4b27c08aSLois Curfman McInnes     if (flg) {
251186905e3SBarry Smith       ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr);
252b0a32e0cSBarry Smith       PetscLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Jacobian matrix\n");
2539b94acceSBarry Smith     }
254639f9d9dSBarry Smith 
25576b2cf59SMatthew Knepley     for(i = 0; i < numberofsetfromoptions; i++) {
25676b2cf59SMatthew Knepley       ierr = (*othersetfromoptions[i])(snes);                                                             CHKERRQ(ierr);
25776b2cf59SMatthew Knepley     }
25876b2cf59SMatthew Knepley 
259186905e3SBarry Smith     if (snes->setfromoptions) {
260186905e3SBarry Smith       ierr = (*snes->setfromoptions)(snes);CHKERRQ(ierr);
261639f9d9dSBarry Smith     }
262639f9d9dSBarry Smith 
263b0a32e0cSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
2644bbc92c1SBarry Smith 
2659b94acceSBarry Smith   ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr);
2669b94acceSBarry Smith   ierr = SLESSetFromOptions(sles);CHKERRQ(ierr);
26793993e2dSLois Curfman McInnes 
2683a40ed3dSBarry Smith   PetscFunctionReturn(0);
2699b94acceSBarry Smith }
2709b94acceSBarry Smith 
271a847f771SSatish Balay 
2724a2ae208SSatish Balay #undef __FUNCT__
2734a2ae208SSatish Balay #define __FUNCT__ "SNESSetApplicationContext"
2749b94acceSBarry Smith /*@
2759b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
2769b94acceSBarry Smith    the nonlinear solvers.
2779b94acceSBarry Smith 
278fee21e36SBarry Smith    Collective on SNES
279fee21e36SBarry Smith 
280c7afd0dbSLois Curfman McInnes    Input Parameters:
281c7afd0dbSLois Curfman McInnes +  snes - the SNES context
282c7afd0dbSLois Curfman McInnes -  usrP - optional user context
283c7afd0dbSLois Curfman McInnes 
28436851e7fSLois Curfman McInnes    Level: intermediate
28536851e7fSLois Curfman McInnes 
2869b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
2879b94acceSBarry Smith 
2889b94acceSBarry Smith .seealso: SNESGetApplicationContext()
2899b94acceSBarry Smith @*/
2909b94acceSBarry Smith int SNESSetApplicationContext(SNES snes,void *usrP)
2919b94acceSBarry Smith {
2923a40ed3dSBarry Smith   PetscFunctionBegin;
29377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2949b94acceSBarry Smith   snes->user		= usrP;
2953a40ed3dSBarry Smith   PetscFunctionReturn(0);
2969b94acceSBarry Smith }
29774679c65SBarry Smith 
2984a2ae208SSatish Balay #undef __FUNCT__
2994a2ae208SSatish Balay #define __FUNCT__ "SNESGetApplicationContext"
3009b94acceSBarry Smith /*@C
3019b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
3029b94acceSBarry Smith    nonlinear solvers.
3039b94acceSBarry Smith 
304c7afd0dbSLois Curfman McInnes    Not Collective
305c7afd0dbSLois Curfman McInnes 
3069b94acceSBarry Smith    Input Parameter:
3079b94acceSBarry Smith .  snes - SNES context
3089b94acceSBarry Smith 
3099b94acceSBarry Smith    Output Parameter:
3109b94acceSBarry Smith .  usrP - user context
3119b94acceSBarry Smith 
31236851e7fSLois Curfman McInnes    Level: intermediate
31336851e7fSLois Curfman McInnes 
3149b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
3159b94acceSBarry Smith 
3169b94acceSBarry Smith .seealso: SNESSetApplicationContext()
3179b94acceSBarry Smith @*/
3189b94acceSBarry Smith int SNESGetApplicationContext(SNES snes,void **usrP)
3199b94acceSBarry Smith {
3203a40ed3dSBarry Smith   PetscFunctionBegin;
32177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
3229b94acceSBarry Smith   *usrP = snes->user;
3233a40ed3dSBarry Smith   PetscFunctionReturn(0);
3249b94acceSBarry Smith }
32574679c65SBarry Smith 
3264a2ae208SSatish Balay #undef __FUNCT__
3274a2ae208SSatish Balay #define __FUNCT__ "SNESGetIterationNumber"
3289b94acceSBarry Smith /*@
329c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
330c8228a4eSBarry Smith    at this time.
3319b94acceSBarry Smith 
332c7afd0dbSLois Curfman McInnes    Not Collective
333c7afd0dbSLois Curfman McInnes 
3349b94acceSBarry Smith    Input Parameter:
3359b94acceSBarry Smith .  snes - SNES context
3369b94acceSBarry Smith 
3379b94acceSBarry Smith    Output Parameter:
3389b94acceSBarry Smith .  iter - iteration number
3399b94acceSBarry Smith 
340c8228a4eSBarry Smith    Notes:
341c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
342c8228a4eSBarry Smith 
343c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
34408405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
34508405cd6SLois Curfman McInnes .vb
34608405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
34708405cd6SLois Curfman McInnes       if (!(it % 2)) {
34808405cd6SLois Curfman McInnes         [compute Jacobian here]
34908405cd6SLois Curfman McInnes       }
35008405cd6SLois Curfman McInnes .ve
351c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
35208405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
353c8228a4eSBarry Smith 
35436851e7fSLois Curfman McInnes    Level: intermediate
35536851e7fSLois Curfman McInnes 
3569b94acceSBarry Smith .keywords: SNES, nonlinear, get, iteration, number
3579b94acceSBarry Smith @*/
3589b94acceSBarry Smith int SNESGetIterationNumber(SNES snes,int* iter)
3599b94acceSBarry Smith {
3603a40ed3dSBarry Smith   PetscFunctionBegin;
36177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
36274679c65SBarry Smith   PetscValidIntPointer(iter);
3639b94acceSBarry Smith   *iter = snes->iter;
3643a40ed3dSBarry Smith   PetscFunctionReturn(0);
3659b94acceSBarry Smith }
36674679c65SBarry Smith 
3674a2ae208SSatish Balay #undef __FUNCT__
3684a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunctionNorm"
3699b94acceSBarry Smith /*@
3709b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
3719b94acceSBarry Smith    with SNESSSetFunction().
3729b94acceSBarry Smith 
373c7afd0dbSLois Curfman McInnes    Collective on SNES
374c7afd0dbSLois Curfman McInnes 
3759b94acceSBarry Smith    Input Parameter:
3769b94acceSBarry Smith .  snes - SNES context
3779b94acceSBarry Smith 
3789b94acceSBarry Smith    Output Parameter:
3799b94acceSBarry Smith .  fnorm - 2-norm of function
3809b94acceSBarry Smith 
38136851e7fSLois Curfman McInnes    Level: intermediate
38236851e7fSLois Curfman McInnes 
3839b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
384a86d99e1SLois Curfman McInnes 
38508405cd6SLois Curfman McInnes .seealso: SNESGetFunction()
3869b94acceSBarry Smith @*/
38787828ca2SBarry Smith int SNESGetFunctionNorm(SNES snes,PetscScalar *fnorm)
3889b94acceSBarry Smith {
3893a40ed3dSBarry Smith   PetscFunctionBegin;
39077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
39174679c65SBarry Smith   PetscValidScalarPointer(fnorm);
3929b94acceSBarry Smith   *fnorm = snes->norm;
3933a40ed3dSBarry Smith   PetscFunctionReturn(0);
3949b94acceSBarry Smith }
39574679c65SBarry Smith 
3964a2ae208SSatish Balay #undef __FUNCT__
3974a2ae208SSatish Balay #define __FUNCT__ "SNESGetNumberUnsuccessfulSteps"
3989b94acceSBarry Smith /*@
3999b94acceSBarry Smith    SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps
4009b94acceSBarry Smith    attempted by the nonlinear solver.
4019b94acceSBarry Smith 
402c7afd0dbSLois Curfman McInnes    Not Collective
403c7afd0dbSLois Curfman McInnes 
4049b94acceSBarry Smith    Input Parameter:
4059b94acceSBarry Smith .  snes - SNES context
4069b94acceSBarry Smith 
4079b94acceSBarry Smith    Output Parameter:
4089b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
4099b94acceSBarry Smith 
410c96a6f78SLois Curfman McInnes    Notes:
411c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
412c96a6f78SLois Curfman McInnes 
41336851e7fSLois Curfman McInnes    Level: intermediate
41436851e7fSLois Curfman McInnes 
4159b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
4169b94acceSBarry Smith @*/
4179b94acceSBarry Smith int SNESGetNumberUnsuccessfulSteps(SNES snes,int* nfails)
4189b94acceSBarry Smith {
4193a40ed3dSBarry Smith   PetscFunctionBegin;
42077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
42174679c65SBarry Smith   PetscValidIntPointer(nfails);
42250ffb88aSMatthew Knepley   *nfails = snes->numFailures;
42350ffb88aSMatthew Knepley   PetscFunctionReturn(0);
42450ffb88aSMatthew Knepley }
42550ffb88aSMatthew Knepley 
42650ffb88aSMatthew Knepley #undef __FUNCT__
42750ffb88aSMatthew Knepley #define __FUNCT__ "SNESSetMaximumUnsuccessfulSteps"
42850ffb88aSMatthew Knepley /*@
42950ffb88aSMatthew Knepley    SNESSetMaximumUnsuccessfulSteps - Sets the maximum number of unsuccessful steps
43050ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
43150ffb88aSMatthew Knepley 
43250ffb88aSMatthew Knepley    Not Collective
43350ffb88aSMatthew Knepley 
43450ffb88aSMatthew Knepley    Input Parameters:
43550ffb88aSMatthew Knepley +  snes     - SNES context
43650ffb88aSMatthew Knepley -  maxFails - maximum of unsuccessful steps
43750ffb88aSMatthew Knepley 
43850ffb88aSMatthew Knepley    Level: intermediate
43950ffb88aSMatthew Knepley 
44050ffb88aSMatthew Knepley .keywords: SNES, nonlinear, set, maximum, unsuccessful, steps
44150ffb88aSMatthew Knepley @*/
44250ffb88aSMatthew Knepley int SNESSetMaximumUnsuccessfulSteps(SNES snes, int maxFails)
44350ffb88aSMatthew Knepley {
44450ffb88aSMatthew Knepley   PetscFunctionBegin;
44550ffb88aSMatthew Knepley   PetscValidHeaderSpecific(snes,SNES_COOKIE);
44650ffb88aSMatthew Knepley   snes->maxFailures = maxFails;
44750ffb88aSMatthew Knepley   PetscFunctionReturn(0);
44850ffb88aSMatthew Knepley }
44950ffb88aSMatthew Knepley 
45050ffb88aSMatthew Knepley #undef __FUNCT__
45150ffb88aSMatthew Knepley #define __FUNCT__ "SNESGetMaximumUnsuccessfulSteps"
45250ffb88aSMatthew Knepley /*@
45350ffb88aSMatthew Knepley    SNESGetMaximumUnsuccessfulSteps - Gets the maximum number of unsuccessful steps
45450ffb88aSMatthew Knepley    attempted by the nonlinear solver before it gives up.
45550ffb88aSMatthew Knepley 
45650ffb88aSMatthew Knepley    Not Collective
45750ffb88aSMatthew Knepley 
45850ffb88aSMatthew Knepley    Input Parameter:
45950ffb88aSMatthew Knepley .  snes     - SNES context
46050ffb88aSMatthew Knepley 
46150ffb88aSMatthew Knepley    Output Parameter:
46250ffb88aSMatthew Knepley .  maxFails - maximum of unsuccessful steps
46350ffb88aSMatthew Knepley 
46450ffb88aSMatthew Knepley    Level: intermediate
46550ffb88aSMatthew Knepley 
46650ffb88aSMatthew Knepley .keywords: SNES, nonlinear, get, maximum, unsuccessful, steps
46750ffb88aSMatthew Knepley @*/
46850ffb88aSMatthew Knepley int SNESGetMaximumUnsuccessfulSteps(SNES snes, int *maxFails)
46950ffb88aSMatthew Knepley {
47050ffb88aSMatthew Knepley   PetscFunctionBegin;
47150ffb88aSMatthew Knepley   PetscValidHeaderSpecific(snes,SNES_COOKIE);
47250ffb88aSMatthew Knepley   PetscValidIntPointer(maxFails);
47350ffb88aSMatthew Knepley   *maxFails = snes->maxFailures;
4743a40ed3dSBarry Smith   PetscFunctionReturn(0);
4759b94acceSBarry Smith }
476a847f771SSatish Balay 
4774a2ae208SSatish Balay #undef __FUNCT__
4784a2ae208SSatish Balay #define __FUNCT__ "SNESGetNumberLinearIterations"
479c96a6f78SLois Curfman McInnes /*@
480c96a6f78SLois Curfman McInnes    SNESGetNumberLinearIterations - Gets the total number of linear iterations
481c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
482c96a6f78SLois Curfman McInnes 
483c7afd0dbSLois Curfman McInnes    Not Collective
484c7afd0dbSLois Curfman McInnes 
485c96a6f78SLois Curfman McInnes    Input Parameter:
486c96a6f78SLois Curfman McInnes .  snes - SNES context
487c96a6f78SLois Curfman McInnes 
488c96a6f78SLois Curfman McInnes    Output Parameter:
489c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
490c96a6f78SLois Curfman McInnes 
491c96a6f78SLois Curfman McInnes    Notes:
492c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
493c96a6f78SLois Curfman McInnes 
49436851e7fSLois Curfman McInnes    Level: intermediate
49536851e7fSLois Curfman McInnes 
496c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
497c96a6f78SLois Curfman McInnes @*/
49886bddb7dSBarry Smith int SNESGetNumberLinearIterations(SNES snes,int* lits)
499c96a6f78SLois Curfman McInnes {
5003a40ed3dSBarry Smith   PetscFunctionBegin;
501c96a6f78SLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
502c96a6f78SLois Curfman McInnes   PetscValidIntPointer(lits);
503c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
5043a40ed3dSBarry Smith   PetscFunctionReturn(0);
505c96a6f78SLois Curfman McInnes }
506c96a6f78SLois Curfman McInnes 
5074a2ae208SSatish Balay #undef __FUNCT__
5084a2ae208SSatish Balay #define __FUNCT__ "SNESGetSLES"
5099b94acceSBarry Smith /*@C
5109b94acceSBarry Smith    SNESGetSLES - Returns the SLES context for a SNES solver.
5119b94acceSBarry Smith 
512c7afd0dbSLois Curfman McInnes    Not Collective, but if SNES object is parallel, then SLES object is parallel
513c7afd0dbSLois Curfman McInnes 
5149b94acceSBarry Smith    Input Parameter:
5159b94acceSBarry Smith .  snes - the SNES context
5169b94acceSBarry Smith 
5179b94acceSBarry Smith    Output Parameter:
5189b94acceSBarry Smith .  sles - the SLES context
5199b94acceSBarry Smith 
5209b94acceSBarry Smith    Notes:
5219b94acceSBarry Smith    The user can then directly manipulate the SLES context to set various
5229b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
5239b94acceSBarry Smith    KSP and PC contexts as well.
5249b94acceSBarry Smith 
52536851e7fSLois Curfman McInnes    Level: beginner
52636851e7fSLois Curfman McInnes 
5279b94acceSBarry Smith .keywords: SNES, nonlinear, get, SLES, context
5289b94acceSBarry Smith 
5299b94acceSBarry Smith .seealso: SLESGetPC(), SLESGetKSP()
5309b94acceSBarry Smith @*/
5319b94acceSBarry Smith int SNESGetSLES(SNES snes,SLES *sles)
5329b94acceSBarry Smith {
5333a40ed3dSBarry Smith   PetscFunctionBegin;
53477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
5359b94acceSBarry Smith   *sles = snes->sles;
5363a40ed3dSBarry Smith   PetscFunctionReturn(0);
5379b94acceSBarry Smith }
53882bf6240SBarry Smith 
5394a2ae208SSatish Balay #undef __FUNCT__
5404a2ae208SSatish Balay #define __FUNCT__ "SNESPublish_Petsc"
541454a90a3SBarry Smith static int SNESPublish_Petsc(PetscObject obj)
542e24b481bSBarry Smith {
543aa482453SBarry Smith #if defined(PETSC_HAVE_AMS)
544454a90a3SBarry Smith   SNES          v = (SNES) obj;
545e24b481bSBarry Smith   int          ierr;
54643d6d2cbSBarry Smith #endif
547e24b481bSBarry Smith 
548e24b481bSBarry Smith   PetscFunctionBegin;
549e24b481bSBarry Smith 
55043d6d2cbSBarry Smith #if defined(PETSC_HAVE_AMS)
551e24b481bSBarry Smith   /* if it is already published then return */
552e24b481bSBarry Smith   if (v->amem >=0) PetscFunctionReturn(0);
553e24b481bSBarry Smith 
554454a90a3SBarry Smith   ierr = PetscObjectPublishBaseBegin(obj);CHKERRQ(ierr);
555e24b481bSBarry Smith   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Iteration",&v->iter,1,AMS_INT,AMS_READ,
556e24b481bSBarry Smith                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
557e24b481bSBarry Smith   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Residual",&v->norm,1,AMS_DOUBLE,AMS_READ,
558e24b481bSBarry Smith                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
559454a90a3SBarry Smith   ierr = PetscObjectPublishBaseEnd(obj);CHKERRQ(ierr);
560433994e6SBarry Smith #endif
561e24b481bSBarry Smith   PetscFunctionReturn(0);
562e24b481bSBarry Smith }
563e24b481bSBarry Smith 
5649b94acceSBarry Smith /* -----------------------------------------------------------*/
5654a2ae208SSatish Balay #undef __FUNCT__
5664a2ae208SSatish Balay #define __FUNCT__ "SNESCreate"
5679b94acceSBarry Smith /*@C
5689b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
5699b94acceSBarry Smith 
570c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
571c7afd0dbSLois Curfman McInnes 
572c7afd0dbSLois Curfman McInnes    Input Parameters:
573c7afd0dbSLois Curfman McInnes +  comm - MPI communicator
5749b94acceSBarry Smith 
5759b94acceSBarry Smith    Output Parameter:
5769b94acceSBarry Smith .  outsnes - the new SNES context
5779b94acceSBarry Smith 
578c7afd0dbSLois Curfman McInnes    Options Database Keys:
579c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
580c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
581c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
582c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
583c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
584c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
585c1f60f51SBarry Smith 
58636851e7fSLois Curfman McInnes    Level: beginner
58736851e7fSLois Curfman McInnes 
5889b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
5899b94acceSBarry Smith 
590*4b27c08aSLois Curfman McInnes .seealso: SNESSolve(), SNESDestroy(), SNES
5919b94acceSBarry Smith @*/
592*4b27c08aSLois Curfman McInnes int SNESCreate(MPI_Comm comm,SNES *outsnes)
5939b94acceSBarry Smith {
5949b94acceSBarry Smith   int                 ierr;
5959b94acceSBarry Smith   SNES                snes;
5969b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
59737fcc0dbSBarry Smith 
5983a40ed3dSBarry Smith   PetscFunctionBegin;
5998ba1e511SMatthew Knepley   PetscValidPointer(outsnes);
6008ba1e511SMatthew Knepley   *outsnes = PETSC_NULL;
6018ba1e511SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
6028ba1e511SMatthew Knepley   ierr = SNESInitializePackage(PETSC_NULL);                                                               CHKERRQ(ierr);
6038ba1e511SMatthew Knepley #endif
6048ba1e511SMatthew Knepley 
6053f1db9ecSBarry Smith   PetscHeaderCreate(snes,_p_SNES,int,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView);
606b0a32e0cSBarry Smith   PetscLogObjectCreate(snes);
607e24b481bSBarry Smith   snes->bops->publish     = SNESPublish_Petsc;
6089b94acceSBarry Smith   snes->max_its           = 50;
6099750a799SBarry Smith   snes->max_funcs	  = 10000;
6109b94acceSBarry Smith   snes->norm		  = 0.0;
611b4874afaSBarry Smith   snes->rtol		  = 1.e-8;
612b4874afaSBarry Smith   snes->ttol              = 0.0;
613b4874afaSBarry Smith   snes->atol		  = 1.e-50;
6149b94acceSBarry Smith   snes->xtol		  = 1.e-8;
615*4b27c08aSLois Curfman McInnes   snes->deltatol	  = 1.e-12;
6169b94acceSBarry Smith   snes->nfuncs            = 0;
61750ffb88aSMatthew Knepley   snes->numFailures       = 0;
61850ffb88aSMatthew Knepley   snes->maxFailures       = 1;
6197a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
620639f9d9dSBarry Smith   snes->numbermonitors    = 0;
6219b94acceSBarry Smith   snes->data              = 0;
6229b94acceSBarry Smith   snes->view              = 0;
62382bf6240SBarry Smith   snes->setupcalled       = 0;
624186905e3SBarry Smith   snes->ksp_ewconv        = PETSC_FALSE;
6256f24a144SLois Curfman McInnes   snes->vwork             = 0;
6266f24a144SLois Curfman McInnes   snes->nwork             = 0;
627758f92a0SBarry Smith   snes->conv_hist_len     = 0;
628758f92a0SBarry Smith   snes->conv_hist_max     = 0;
629758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
630758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
631758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
632184914b5SBarry Smith   snes->reason            = SNES_CONVERGED_ITERATING;
6339b94acceSBarry Smith 
6349b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
635b0a32e0cSBarry Smith   ierr = PetscNew(SNES_KSP_EW_ConvCtx,&kctx);CHKERRQ(ierr);
636b0a32e0cSBarry Smith   PetscLogObjectMemory(snes,sizeof(SNES_KSP_EW_ConvCtx));
6379b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
6389b94acceSBarry Smith   kctx->version     = 2;
6399b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
6409b94acceSBarry Smith                              this was too large for some test cases */
6419b94acceSBarry Smith   kctx->rtol_last   = 0;
6429b94acceSBarry Smith   kctx->rtol_max    = .9;
6439b94acceSBarry Smith   kctx->gamma       = 1.0;
6449b94acceSBarry Smith   kctx->alpha2      = .5*(1.0 + sqrt(5.0));
6459b94acceSBarry Smith   kctx->alpha       = kctx->alpha2;
6469b94acceSBarry Smith   kctx->threshold   = .1;
6479b94acceSBarry Smith   kctx->lresid_last = 0;
6489b94acceSBarry Smith   kctx->norm_last   = 0;
6499b94acceSBarry Smith 
6509b94acceSBarry Smith   ierr = SLESCreate(comm,&snes->sles);CHKERRQ(ierr);
651b0a32e0cSBarry Smith   PetscLogObjectParent(snes,snes->sles)
6525334005bSBarry Smith 
6539b94acceSBarry Smith   *outsnes = snes;
65400036973SBarry Smith   ierr = PetscPublishAll(snes);CHKERRQ(ierr);
6553a40ed3dSBarry Smith   PetscFunctionReturn(0);
6569b94acceSBarry Smith }
6579b94acceSBarry Smith 
6589b94acceSBarry Smith /* --------------------------------------------------------------- */
6594a2ae208SSatish Balay #undef __FUNCT__
6604a2ae208SSatish Balay #define __FUNCT__ "SNESSetFunction"
6619b94acceSBarry Smith /*@C
6629b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
6639b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
6649b94acceSBarry Smith    equations.
6659b94acceSBarry Smith 
666fee21e36SBarry Smith    Collective on SNES
667fee21e36SBarry Smith 
668c7afd0dbSLois Curfman McInnes    Input Parameters:
669c7afd0dbSLois Curfman McInnes +  snes - the SNES context
670c7afd0dbSLois Curfman McInnes .  func - function evaluation routine
671c7afd0dbSLois Curfman McInnes .  r - vector to store function value
672c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
673c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
6749b94acceSBarry Smith 
675c7afd0dbSLois Curfman McInnes    Calling sequence of func:
6768d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
677c7afd0dbSLois Curfman McInnes 
678313e4042SLois Curfman McInnes .  f - function vector
679c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
6809b94acceSBarry Smith 
6819b94acceSBarry Smith    Notes:
6829b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
6839b94acceSBarry Smith $      f'(x) x = -f(x),
684c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
6859b94acceSBarry Smith 
68636851e7fSLois Curfman McInnes    Level: beginner
68736851e7fSLois Curfman McInnes 
6889b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
6899b94acceSBarry Smith 
690a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
6919b94acceSBarry Smith @*/
6925334005bSBarry Smith int SNESSetFunction(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),void *ctx)
6939b94acceSBarry Smith {
6943a40ed3dSBarry Smith   PetscFunctionBegin;
69577c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
696184914b5SBarry Smith   PetscValidHeaderSpecific(r,VEC_COOKIE);
697184914b5SBarry Smith   PetscCheckSameComm(snes,r);
698184914b5SBarry Smith 
6999b94acceSBarry Smith   snes->computefunction     = func;
7009b94acceSBarry Smith   snes->vec_func            = snes->vec_func_always = r;
7019b94acceSBarry Smith   snes->funP                = ctx;
7023a40ed3dSBarry Smith   PetscFunctionReturn(0);
7039b94acceSBarry Smith }
7049b94acceSBarry Smith 
7054a2ae208SSatish Balay #undef __FUNCT__
7064a2ae208SSatish Balay #define __FUNCT__ "SNESComputeFunction"
7079b94acceSBarry Smith /*@
70836851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
7099b94acceSBarry Smith                          SNESSetFunction().
7109b94acceSBarry Smith 
711c7afd0dbSLois Curfman McInnes    Collective on SNES
712c7afd0dbSLois Curfman McInnes 
7139b94acceSBarry Smith    Input Parameters:
714c7afd0dbSLois Curfman McInnes +  snes - the SNES context
715c7afd0dbSLois Curfman McInnes -  x - input vector
7169b94acceSBarry Smith 
7179b94acceSBarry Smith    Output Parameter:
7183638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
7199b94acceSBarry Smith 
7201bffabb2SLois Curfman McInnes    Notes:
72136851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
72236851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
72336851e7fSLois Curfman McInnes    themselves.
72436851e7fSLois Curfman McInnes 
72536851e7fSLois Curfman McInnes    Level: developer
72636851e7fSLois Curfman McInnes 
7279b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
7289b94acceSBarry Smith 
729a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
7309b94acceSBarry Smith @*/
7319b94acceSBarry Smith int SNESComputeFunction(SNES snes,Vec x,Vec y)
7329b94acceSBarry Smith {
7339b94acceSBarry Smith   int    ierr;
7349b94acceSBarry Smith 
7353a40ed3dSBarry Smith   PetscFunctionBegin;
736184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
737184914b5SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
738184914b5SBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE);
739184914b5SBarry Smith   PetscCheckSameComm(snes,x);
740184914b5SBarry Smith   PetscCheckSameComm(snes,y);
741184914b5SBarry Smith 
742d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
743d64ed03dSBarry Smith   PetscStackPush("SNES user function");
7449b94acceSBarry Smith   ierr = (*snes->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr);
745d64ed03dSBarry Smith   PetscStackPop;
746ae3c334cSLois Curfman McInnes   snes->nfuncs++;
747d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_FunctionEval,snes,x,y,0);CHKERRQ(ierr);
7483a40ed3dSBarry Smith   PetscFunctionReturn(0);
7499b94acceSBarry Smith }
7509b94acceSBarry Smith 
7514a2ae208SSatish Balay #undef __FUNCT__
7524a2ae208SSatish Balay #define __FUNCT__ "SNESComputeJacobian"
75362fef451SLois Curfman McInnes /*@
75462fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
75562fef451SLois Curfman McInnes    set with SNESSetJacobian().
75662fef451SLois Curfman McInnes 
757c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
758c7afd0dbSLois Curfman McInnes 
75962fef451SLois Curfman McInnes    Input Parameters:
760c7afd0dbSLois Curfman McInnes +  snes - the SNES context
761c7afd0dbSLois Curfman McInnes -  x - input vector
76262fef451SLois Curfman McInnes 
76362fef451SLois Curfman McInnes    Output Parameters:
764c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
76562fef451SLois Curfman McInnes .  B - optional preconditioning matrix
766c7afd0dbSLois Curfman McInnes -  flag - flag indicating matrix structure
767fee21e36SBarry Smith 
76862fef451SLois Curfman McInnes    Notes:
76962fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
77062fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
77162fef451SLois Curfman McInnes 
772dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the
773dc5a77f8SLois Curfman McInnes    flag parameter.
77462fef451SLois Curfman McInnes 
77536851e7fSLois Curfman McInnes    Level: developer
77636851e7fSLois Curfman McInnes 
77762fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
77862fef451SLois Curfman McInnes 
77962fef451SLois Curfman McInnes .seealso:  SNESSetJacobian(), SLESSetOperators()
78062fef451SLois Curfman McInnes @*/
7819b94acceSBarry Smith int SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
7829b94acceSBarry Smith {
7839b94acceSBarry Smith   int    ierr;
7843a40ed3dSBarry Smith 
7853a40ed3dSBarry Smith   PetscFunctionBegin;
786184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
787184914b5SBarry Smith   PetscValidHeaderSpecific(X,VEC_COOKIE);
788184914b5SBarry Smith   PetscCheckSameComm(snes,X);
7893a40ed3dSBarry Smith   if (!snes->computejacobian) PetscFunctionReturn(0);
790d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
791c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
792d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
7939b94acceSBarry Smith   ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
794d64ed03dSBarry Smith   PetscStackPop;
795d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);CHKERRQ(ierr);
7966d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
79777c4ece6SBarry Smith   PetscValidHeaderSpecific(*A,MAT_COOKIE);
79877c4ece6SBarry Smith   PetscValidHeaderSpecific(*B,MAT_COOKIE);
7993a40ed3dSBarry Smith   PetscFunctionReturn(0);
8009b94acceSBarry Smith }
8019b94acceSBarry Smith 
8024a2ae208SSatish Balay #undef __FUNCT__
8034a2ae208SSatish Balay #define __FUNCT__ "SNESSetJacobian"
8049b94acceSBarry Smith /*@C
8059b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
806044dda88SLois Curfman McInnes    location to store the matrix.
8079b94acceSBarry Smith 
808c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
809c7afd0dbSLois Curfman McInnes 
8109b94acceSBarry Smith    Input Parameters:
811c7afd0dbSLois Curfman McInnes +  snes - the SNES context
8129b94acceSBarry Smith .  A - Jacobian matrix
8139b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
8149b94acceSBarry Smith .  func - Jacobian evaluation routine
815c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
8162cd2dfdcSLois Curfman McInnes          Jacobian evaluation routine (may be PETSC_NULL)
8179b94acceSBarry Smith 
8189b94acceSBarry Smith    Calling sequence of func:
8198d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
8209b94acceSBarry Smith 
821c7afd0dbSLois Curfman McInnes +  x - input vector
8229b94acceSBarry Smith .  A - Jacobian matrix
8239b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
824ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
825ac21db08SLois Curfman McInnes    structure (same as flag in SLESSetOperators())
826c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
8279b94acceSBarry Smith 
8289b94acceSBarry Smith    Notes:
829dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the flag
8302cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
831ac21db08SLois Curfman McInnes 
832ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
8339b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
8349b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
8359b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
8369b94acceSBarry Smith    throughout the global iterations.
8379b94acceSBarry Smith 
83836851e7fSLois Curfman McInnes    Level: beginner
83936851e7fSLois Curfman McInnes 
8409b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
8419b94acceSBarry Smith 
842ac21db08SLois Curfman McInnes .seealso: SLESSetOperators(), SNESSetFunction()
8439b94acceSBarry Smith @*/
844454a90a3SBarry Smith int SNESSetJacobian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
8459b94acceSBarry Smith {
8463a7fca6bSBarry Smith   int ierr;
8473a7fca6bSBarry Smith 
8483a40ed3dSBarry Smith   PetscFunctionBegin;
84977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
85000036973SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_COOKIE);
85100036973SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_COOKIE);
85200036973SBarry Smith   if (A) PetscCheckSameComm(snes,A);
85300036973SBarry Smith   if (B) PetscCheckSameComm(snes,B);
8543a7fca6bSBarry Smith   if (func) snes->computejacobian = func;
8553a7fca6bSBarry Smith   if (ctx)  snes->jacP            = ctx;
8563a7fca6bSBarry Smith   if (A) {
8573a7fca6bSBarry Smith     if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
8589b94acceSBarry Smith     snes->jacobian = A;
8593a7fca6bSBarry Smith     ierr           = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
8603a7fca6bSBarry Smith   }
8613a7fca6bSBarry Smith   if (B) {
8623a7fca6bSBarry Smith     if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
8639b94acceSBarry Smith     snes->jacobian_pre = B;
8643a7fca6bSBarry Smith     ierr               = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
8653a7fca6bSBarry Smith   }
8663a40ed3dSBarry Smith   PetscFunctionReturn(0);
8679b94acceSBarry Smith }
86862fef451SLois Curfman McInnes 
8694a2ae208SSatish Balay #undef __FUNCT__
8704a2ae208SSatish Balay #define __FUNCT__ "SNESGetJacobian"
871c2aafc4cSSatish Balay /*@C
872b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
873b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
874b4fd4287SBarry Smith 
875c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
876c7afd0dbSLois Curfman McInnes 
877b4fd4287SBarry Smith    Input Parameter:
878b4fd4287SBarry Smith .  snes - the nonlinear solver context
879b4fd4287SBarry Smith 
880b4fd4287SBarry Smith    Output Parameters:
881c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
882b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
88300036973SBarry Smith .  ctx - location to stash Jacobian ctx (or PETSC_NULL)
88400036973SBarry Smith -  func - location to put Jacobian function (or PETSC_NULL)
885fee21e36SBarry Smith 
88636851e7fSLois Curfman McInnes    Level: advanced
88736851e7fSLois Curfman McInnes 
888b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
889b4fd4287SBarry Smith @*/
89000036973SBarry Smith int SNESGetJacobian(SNES snes,Mat *A,Mat *B,void **ctx,int (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*))
891b4fd4287SBarry Smith {
8923a40ed3dSBarry Smith   PetscFunctionBegin;
893184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
894b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
895b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
896b4fd4287SBarry Smith   if (ctx)  *ctx  = snes->jacP;
89700036973SBarry Smith   if (func) *func = snes->computejacobian;
8983a40ed3dSBarry Smith   PetscFunctionReturn(0);
899b4fd4287SBarry Smith }
900b4fd4287SBarry Smith 
9019b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
90245fc7adcSBarry Smith extern int SNESDefaultMatrixFreeCreate2(SNES,Vec,Mat*);
9039b94acceSBarry Smith 
9044a2ae208SSatish Balay #undef __FUNCT__
9054a2ae208SSatish Balay #define __FUNCT__ "SNESSetUp"
9069b94acceSBarry Smith /*@
9079b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
908272ac6f2SLois Curfman McInnes    of a nonlinear solver.
9099b94acceSBarry Smith 
910fee21e36SBarry Smith    Collective on SNES
911fee21e36SBarry Smith 
912c7afd0dbSLois Curfman McInnes    Input Parameters:
913c7afd0dbSLois Curfman McInnes +  snes - the SNES context
914c7afd0dbSLois Curfman McInnes -  x - the solution vector
915c7afd0dbSLois Curfman McInnes 
916272ac6f2SLois Curfman McInnes    Notes:
917272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
918272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
919272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
920272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
921272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
922272ac6f2SLois Curfman McInnes 
92336851e7fSLois Curfman McInnes    Level: advanced
92436851e7fSLois Curfman McInnes 
9259b94acceSBarry Smith .keywords: SNES, nonlinear, setup
9269b94acceSBarry Smith 
9279b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
9289b94acceSBarry Smith @*/
9298ddd3da0SLois Curfman McInnes int SNESSetUp(SNES snes,Vec x)
9309b94acceSBarry Smith {
931f1af5d2fSBarry Smith   int        ierr;
932*4b27c08aSLois Curfman McInnes   PetscTruth flg, iseqtr;
9333a40ed3dSBarry Smith 
9343a40ed3dSBarry Smith   PetscFunctionBegin;
93577c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
93677c4ece6SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
937184914b5SBarry Smith   PetscCheckSameComm(snes,x);
9388ddd3da0SLois Curfman McInnes   snes->vec_sol = snes->vec_sol_always = x;
9399b94acceSBarry Smith 
940b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_mf_operator",&flg);CHKERRQ(ierr);
941c1f60f51SBarry Smith   /*
942c1f60f51SBarry Smith       This version replaces the user provided Jacobian matrix with a
943dfa02198SLois Curfman McInnes       matrix-free version but still employs the user-provided preconditioner matrix
944c1f60f51SBarry Smith   */
945c1f60f51SBarry Smith   if (flg) {
946c1f60f51SBarry Smith     Mat J;
9475a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
9485a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
9493a7fca6bSBarry Smith     PetscLogInfo(snes,"SNESSetUp: Setting default matrix-free operator routines\n");
9503a7fca6bSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
9513a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
952c1f60f51SBarry Smith   }
95345fc7adcSBarry Smith 
95445fc7adcSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_mf_operator2",&flg);CHKERRQ(ierr);
95545fc7adcSBarry Smith   if (flg) {
95645fc7adcSBarry Smith     Mat J;
95745fc7adcSBarry Smith     ierr = SNESDefaultMatrixFreeCreate2(snes,snes->vec_sol,&J);CHKERRQ(ierr);
95845fc7adcSBarry Smith     ierr = SNESSetJacobian(snes,J,0,0,0);CHKERRQ(ierr);
95945fc7adcSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
96045fc7adcSBarry Smith   }
96145fc7adcSBarry Smith 
962b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_mf",&flg);CHKERRQ(ierr);
963c1f60f51SBarry Smith   /*
964dfa02198SLois Curfman McInnes       This version replaces both the user-provided Jacobian and the user-
965c1f60f51SBarry Smith       provided preconditioner matrix with the default matrix free version.
966c1f60f51SBarry Smith    */
96731615d04SLois Curfman McInnes   if (flg) {
968272ac6f2SLois Curfman McInnes     Mat  J;
969f3ef73ceSBarry Smith     SLES sles;
970f3ef73ceSBarry Smith     PC   pc;
971f3ef73ceSBarry Smith 
9725a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
9733a7fca6bSBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
97493b98531SBarry Smith     PetscLogInfo(snes,"SNESSetUp: Setting default matrix-free operator and preconditioner routines\n");
9753a7fca6bSBarry Smith     ierr = SNESSetJacobian(snes,J,J,MatSNESMFComputeJacobian,snes->funP);CHKERRQ(ierr);
9763a7fca6bSBarry Smith     ierr = MatDestroy(J);CHKERRQ(ierr);
9773a7fca6bSBarry Smith 
978f3ef73ceSBarry Smith     /* force no preconditioner */
979f3ef73ceSBarry Smith     ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr);
980f3ef73ceSBarry Smith     ierr = SLESGetPC(sles,&pc);CHKERRQ(ierr);
981f3ef73ceSBarry Smith     ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
982272ac6f2SLois Curfman McInnes   }
983f3ef73ceSBarry Smith 
98429bbc08cSBarry Smith   if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
98529bbc08cSBarry Smith   if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() first");
98629bbc08cSBarry Smith   if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetJacobian() first \n or use -snes_mf option");
987a8c6a408SBarry Smith   if (snes->vec_func == snes->vec_sol) {
98829bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_IDN,"Solution vector cannot be function vector");
989a8c6a408SBarry Smith   }
990a703fe33SLois Curfman McInnes 
991a703fe33SLois Curfman McInnes   /* Set the KSP stopping criterion to use the Eisenstat-Walker method */
992*4b27c08aSLois Curfman McInnes   ierr = PetscTypeCompare((PetscObject)snes,SNESTR,&iseqtr);CHKERRQ(ierr);
9936831982aSBarry Smith   if (snes->ksp_ewconv && !iseqtr) {
994a703fe33SLois Curfman McInnes     SLES sles; KSP ksp;
995a703fe33SLois Curfman McInnes     ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr);
996a703fe33SLois Curfman McInnes     ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr);
997186905e3SBarry Smith     ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private,snes);CHKERRQ(ierr);
998a703fe33SLois Curfman McInnes   }
999*4b27c08aSLois Curfman McInnes 
1000a703fe33SLois Curfman McInnes   if (snes->setup) {ierr = (*snes->setup)(snes);CHKERRQ(ierr);}
100182bf6240SBarry Smith   snes->setupcalled = 1;
10023a40ed3dSBarry Smith   PetscFunctionReturn(0);
10039b94acceSBarry Smith }
10049b94acceSBarry Smith 
10054a2ae208SSatish Balay #undef __FUNCT__
10064a2ae208SSatish Balay #define __FUNCT__ "SNESDestroy"
10079b94acceSBarry Smith /*@C
10089b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
10099b94acceSBarry Smith    with SNESCreate().
10109b94acceSBarry Smith 
1011c7afd0dbSLois Curfman McInnes    Collective on SNES
1012c7afd0dbSLois Curfman McInnes 
10139b94acceSBarry Smith    Input Parameter:
10149b94acceSBarry Smith .  snes - the SNES context
10159b94acceSBarry Smith 
101636851e7fSLois Curfman McInnes    Level: beginner
101736851e7fSLois Curfman McInnes 
10189b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
10199b94acceSBarry Smith 
102063a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
10219b94acceSBarry Smith @*/
10229b94acceSBarry Smith int SNESDestroy(SNES snes)
10239b94acceSBarry Smith {
1024b8a78c4aSBarry Smith   int i,ierr;
10253a40ed3dSBarry Smith 
10263a40ed3dSBarry Smith   PetscFunctionBegin;
102777c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
10283a40ed3dSBarry Smith   if (--snes->refct > 0) PetscFunctionReturn(0);
1029d4bb536fSBarry Smith 
1030be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
10310f5bd95cSBarry Smith   ierr = PetscObjectDepublish(snes);CHKERRQ(ierr);
1032be0abb6dSBarry Smith 
1033e1311b90SBarry Smith   if (snes->destroy) {ierr = (*(snes)->destroy)(snes);CHKERRQ(ierr);}
1034606d414cSSatish Balay   if (snes->kspconvctx) {ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);}
10353a7fca6bSBarry Smith   if (snes->jacobian) {ierr = MatDestroy(snes->jacobian);CHKERRQ(ierr);}
10363a7fca6bSBarry Smith   if (snes->jacobian_pre) {ierr = MatDestroy(snes->jacobian_pre);CHKERRQ(ierr);}
10379b94acceSBarry Smith   ierr = SLESDestroy(snes->sles);CHKERRQ(ierr);
1038522c5e43SBarry Smith   if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);}
1039b8a78c4aSBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
1040b8a78c4aSBarry Smith     if (snes->monitordestroy[i]) {
1041b8a78c4aSBarry Smith       ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr);
1042b8a78c4aSBarry Smith     }
1043b8a78c4aSBarry Smith   }
1044b0a32e0cSBarry Smith   PetscLogObjectDestroy((PetscObject)snes);
10450452661fSBarry Smith   PetscHeaderDestroy((PetscObject)snes);
10463a40ed3dSBarry Smith   PetscFunctionReturn(0);
10479b94acceSBarry Smith }
10489b94acceSBarry Smith 
10499b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
10509b94acceSBarry Smith 
10514a2ae208SSatish Balay #undef __FUNCT__
10524a2ae208SSatish Balay #define __FUNCT__ "SNESSetTolerances"
10539b94acceSBarry Smith /*@
1054d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
10559b94acceSBarry Smith 
1056c7afd0dbSLois Curfman McInnes    Collective on SNES
1057c7afd0dbSLois Curfman McInnes 
10589b94acceSBarry Smith    Input Parameters:
1059c7afd0dbSLois Curfman McInnes +  snes - the SNES context
106033174efeSLois Curfman McInnes .  atol - absolute convergence tolerance
106133174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
106233174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
106333174efeSLois Curfman McInnes            of the change in the solution between steps
106433174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1065c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1066fee21e36SBarry Smith 
106733174efeSLois Curfman McInnes    Options Database Keys:
1068c7afd0dbSLois Curfman McInnes +    -snes_atol <atol> - Sets atol
1069c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1070c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1071c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1072c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
10739b94acceSBarry Smith 
1074d7a720efSLois Curfman McInnes    Notes:
10759b94acceSBarry Smith    The default maximum number of iterations is 50.
10769b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
10779b94acceSBarry Smith 
107836851e7fSLois Curfman McInnes    Level: intermediate
107936851e7fSLois Curfman McInnes 
108033174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
10819b94acceSBarry Smith 
1082d7a720efSLois Curfman McInnes .seealso: SNESSetTrustRegionTolerance(), SNESSetMinimizationFunctionTolerance()
10839b94acceSBarry Smith @*/
1084329f5518SBarry Smith int SNESSetTolerances(SNES snes,PetscReal atol,PetscReal rtol,PetscReal stol,int maxit,int maxf)
10859b94acceSBarry Smith {
10863a40ed3dSBarry Smith   PetscFunctionBegin;
108777c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1088d7a720efSLois Curfman McInnes   if (atol != PETSC_DEFAULT)  snes->atol      = atol;
1089d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1090d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1091d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1092d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
10933a40ed3dSBarry Smith   PetscFunctionReturn(0);
10949b94acceSBarry Smith }
10959b94acceSBarry Smith 
10964a2ae208SSatish Balay #undef __FUNCT__
10974a2ae208SSatish Balay #define __FUNCT__ "SNESGetTolerances"
10989b94acceSBarry Smith /*@
109933174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
110033174efeSLois Curfman McInnes 
1101c7afd0dbSLois Curfman McInnes    Not Collective
1102c7afd0dbSLois Curfman McInnes 
110333174efeSLois Curfman McInnes    Input Parameters:
1104c7afd0dbSLois Curfman McInnes +  snes - the SNES context
110533174efeSLois Curfman McInnes .  atol - absolute convergence tolerance
110633174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
110733174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
110833174efeSLois Curfman McInnes            of the change in the solution between steps
110933174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1110c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1111fee21e36SBarry Smith 
111233174efeSLois Curfman McInnes    Notes:
111333174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
111433174efeSLois Curfman McInnes 
111536851e7fSLois Curfman McInnes    Level: intermediate
111636851e7fSLois Curfman McInnes 
111733174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
111833174efeSLois Curfman McInnes 
111933174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
112033174efeSLois Curfman McInnes @*/
1121329f5518SBarry Smith int SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,int *maxit,int *maxf)
112233174efeSLois Curfman McInnes {
11233a40ed3dSBarry Smith   PetscFunctionBegin;
112433174efeSLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
112533174efeSLois Curfman McInnes   if (atol)  *atol  = snes->atol;
112633174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
112733174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
112833174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
112933174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
11303a40ed3dSBarry Smith   PetscFunctionReturn(0);
113133174efeSLois Curfman McInnes }
113233174efeSLois Curfman McInnes 
11334a2ae208SSatish Balay #undef __FUNCT__
11344a2ae208SSatish Balay #define __FUNCT__ "SNESSetTrustRegionTolerance"
113533174efeSLois Curfman McInnes /*@
11369b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
11379b94acceSBarry Smith 
1138fee21e36SBarry Smith    Collective on SNES
1139fee21e36SBarry Smith 
1140c7afd0dbSLois Curfman McInnes    Input Parameters:
1141c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1142c7afd0dbSLois Curfman McInnes -  tol - tolerance
1143c7afd0dbSLois Curfman McInnes 
11449b94acceSBarry Smith    Options Database Key:
1145c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
11469b94acceSBarry Smith 
114736851e7fSLois Curfman McInnes    Level: intermediate
114836851e7fSLois Curfman McInnes 
11499b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
11509b94acceSBarry Smith 
1151d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetMinimizationFunctionTolerance()
11529b94acceSBarry Smith @*/
1153329f5518SBarry Smith int SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
11549b94acceSBarry Smith {
11553a40ed3dSBarry Smith   PetscFunctionBegin;
115677c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
11579b94acceSBarry Smith   snes->deltatol = tol;
11583a40ed3dSBarry Smith   PetscFunctionReturn(0);
11599b94acceSBarry Smith }
11609b94acceSBarry Smith 
1161df9fa365SBarry Smith /*
1162df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1163df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1164df9fa365SBarry Smith    macros instead of functions
1165df9fa365SBarry Smith */
11664a2ae208SSatish Balay #undef __FUNCT__
11674a2ae208SSatish Balay #define __FUNCT__ "SNESLGMonitor"
1168329f5518SBarry Smith int SNESLGMonitor(SNES snes,int it,PetscReal norm,void *ctx)
1169ce1608b8SBarry Smith {
1170ce1608b8SBarry Smith   int ierr;
1171ce1608b8SBarry Smith 
1172ce1608b8SBarry Smith   PetscFunctionBegin;
1173184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1174ce1608b8SBarry Smith   ierr = KSPLGMonitor((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1175ce1608b8SBarry Smith   PetscFunctionReturn(0);
1176ce1608b8SBarry Smith }
1177ce1608b8SBarry Smith 
11784a2ae208SSatish Balay #undef __FUNCT__
11794a2ae208SSatish Balay #define __FUNCT__ "SNESLGMonitorCreate"
1180b0a32e0cSBarry Smith int SNESLGMonitorCreate(char *host,char *label,int x,int y,int m,int n,PetscDrawLG *draw)
1181df9fa365SBarry Smith {
1182df9fa365SBarry Smith   int ierr;
1183df9fa365SBarry Smith 
1184df9fa365SBarry Smith   PetscFunctionBegin;
1185df9fa365SBarry Smith   ierr = KSPLGMonitorCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1186df9fa365SBarry Smith   PetscFunctionReturn(0);
1187df9fa365SBarry Smith }
1188df9fa365SBarry Smith 
11894a2ae208SSatish Balay #undef __FUNCT__
11904a2ae208SSatish Balay #define __FUNCT__ "SNESLGMonitorDestroy"
1191b0a32e0cSBarry Smith int SNESLGMonitorDestroy(PetscDrawLG draw)
1192df9fa365SBarry Smith {
1193df9fa365SBarry Smith   int ierr;
1194df9fa365SBarry Smith 
1195df9fa365SBarry Smith   PetscFunctionBegin;
1196df9fa365SBarry Smith   ierr = KSPLGMonitorDestroy(draw);CHKERRQ(ierr);
1197df9fa365SBarry Smith   PetscFunctionReturn(0);
1198df9fa365SBarry Smith }
1199df9fa365SBarry Smith 
12009b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
12019b94acceSBarry Smith 
12024a2ae208SSatish Balay #undef __FUNCT__
12034a2ae208SSatish Balay #define __FUNCT__ "SNESSetMonitor"
12049b94acceSBarry Smith /*@C
12055cd90555SBarry Smith    SNESSetMonitor - Sets an ADDITIONAL function that is to be used at every
12069b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
12079b94acceSBarry Smith    progress.
12089b94acceSBarry Smith 
1209fee21e36SBarry Smith    Collective on SNES
1210fee21e36SBarry Smith 
1211c7afd0dbSLois Curfman McInnes    Input Parameters:
1212c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1213c7afd0dbSLois Curfman McInnes .  func - monitoring routine
1214b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
1215b3006f0bSLois Curfman McInnes           monitor routine (use PETSC_NULL if no context is desitre)
1216b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1217b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
12189b94acceSBarry Smith 
1219c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1220329f5518SBarry Smith $     int func(SNES snes,int its, PetscReal norm,void *mctx)
1221c7afd0dbSLois Curfman McInnes 
1222c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1223c7afd0dbSLois Curfman McInnes .    its - iteration number
1224c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
122540a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
12269b94acceSBarry Smith 
12279665c990SLois Curfman McInnes    Options Database Keys:
1228c7afd0dbSLois Curfman McInnes +    -snes_monitor        - sets SNESDefaultMonitor()
1229c7afd0dbSLois Curfman McInnes .    -snes_xmonitor       - sets line graph monitor,
1230c7afd0dbSLois Curfman McInnes                             uses SNESLGMonitorCreate()
1231c7afd0dbSLois Curfman McInnes _    -snes_cancelmonitors - cancels all monitors that have
1232c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
1233c7afd0dbSLois Curfman McInnes                             calls to SNESSetMonitor(), but
1234c7afd0dbSLois Curfman McInnes                             does not cancel those set via
1235c7afd0dbSLois Curfman McInnes                             the options database.
12369665c990SLois Curfman McInnes 
1237639f9d9dSBarry Smith    Notes:
12386bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
12396bc08f3fSLois Curfman McInnes    SNESSetMonitor() multiple times; all will be called in the
12406bc08f3fSLois Curfman McInnes    order in which they were set.
1241639f9d9dSBarry Smith 
124236851e7fSLois Curfman McInnes    Level: intermediate
124336851e7fSLois Curfman McInnes 
12449b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
12459b94acceSBarry Smith 
12465cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESClearMonitor()
12479b94acceSBarry Smith @*/
1248329f5518SBarry Smith int SNESSetMonitor(SNES snes,int (*func)(SNES,int,PetscReal,void*),void *mctx,int (*monitordestroy)(void *))
12499b94acceSBarry Smith {
12503a40ed3dSBarry Smith   PetscFunctionBegin;
1251184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1252639f9d9dSBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) {
125329bbc08cSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
1254639f9d9dSBarry Smith   }
1255639f9d9dSBarry Smith 
1256639f9d9dSBarry Smith   snes->monitor[snes->numbermonitors]           = func;
1257b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
1258639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
12593a40ed3dSBarry Smith   PetscFunctionReturn(0);
12609b94acceSBarry Smith }
12619b94acceSBarry Smith 
12624a2ae208SSatish Balay #undef __FUNCT__
12634a2ae208SSatish Balay #define __FUNCT__ "SNESClearMonitor"
12645cd90555SBarry Smith /*@C
12655cd90555SBarry Smith    SNESClearMonitor - Clears all the monitor functions for a SNES object.
12665cd90555SBarry Smith 
1267c7afd0dbSLois Curfman McInnes    Collective on SNES
1268c7afd0dbSLois Curfman McInnes 
12695cd90555SBarry Smith    Input Parameters:
12705cd90555SBarry Smith .  snes - the SNES context
12715cd90555SBarry Smith 
12725cd90555SBarry Smith    Options Database:
1273c7afd0dbSLois Curfman McInnes .  -snes_cancelmonitors - cancels all monitors that have been hardwired
1274c7afd0dbSLois Curfman McInnes     into a code by calls to SNESSetMonitor(), but does not cancel those
1275c7afd0dbSLois Curfman McInnes     set via the options database
12765cd90555SBarry Smith 
12775cd90555SBarry Smith    Notes:
12785cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
12795cd90555SBarry Smith 
128036851e7fSLois Curfman McInnes    Level: intermediate
128136851e7fSLois Curfman McInnes 
12825cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
12835cd90555SBarry Smith 
12845cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESSetMonitor()
12855cd90555SBarry Smith @*/
12865cd90555SBarry Smith int SNESClearMonitor(SNES snes)
12875cd90555SBarry Smith {
12885cd90555SBarry Smith   PetscFunctionBegin;
1289184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
12905cd90555SBarry Smith   snes->numbermonitors = 0;
12915cd90555SBarry Smith   PetscFunctionReturn(0);
12925cd90555SBarry Smith }
12935cd90555SBarry Smith 
12944a2ae208SSatish Balay #undef __FUNCT__
12954a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceTest"
12969b94acceSBarry Smith /*@C
12979b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
12989b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
12999b94acceSBarry Smith 
1300fee21e36SBarry Smith    Collective on SNES
1301fee21e36SBarry Smith 
1302c7afd0dbSLois Curfman McInnes    Input Parameters:
1303c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1304c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
1305c7afd0dbSLois Curfman McInnes -  cctx - [optional] context for private data for the convergence routine
1306c7afd0dbSLois Curfman McInnes           (may be PETSC_NULL)
13079b94acceSBarry Smith 
1308c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1309329f5518SBarry Smith $     int func (SNES snes,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
1310c7afd0dbSLois Curfman McInnes 
1311c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1312c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
1313184914b5SBarry Smith .    reason - reason for convergence/divergence
1314c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
1315*4b27c08aSLois Curfman McInnes .    gnorm - 2-norm of current step
1316*4b27c08aSLois Curfman McInnes -    f - 2-norm of function
13179b94acceSBarry Smith 
131836851e7fSLois Curfman McInnes    Level: advanced
131936851e7fSLois Curfman McInnes 
13209b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
13219b94acceSBarry Smith 
1322*4b27c08aSLois Curfman McInnes .seealso: SNESConverged_LS(), SNESConverged_TR()
13239b94acceSBarry Smith @*/
1324329f5518SBarry Smith int SNESSetConvergenceTest(SNES snes,int (*func)(SNES,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx)
13259b94acceSBarry Smith {
13263a40ed3dSBarry Smith   PetscFunctionBegin;
1327184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
13289b94acceSBarry Smith   (snes)->converged = func;
13299b94acceSBarry Smith   (snes)->cnvP      = cctx;
13303a40ed3dSBarry Smith   PetscFunctionReturn(0);
13319b94acceSBarry Smith }
13329b94acceSBarry Smith 
13334a2ae208SSatish Balay #undef __FUNCT__
13344a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergedReason"
1335184914b5SBarry Smith /*@C
1336184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
1337184914b5SBarry Smith 
1338184914b5SBarry Smith    Not Collective
1339184914b5SBarry Smith 
1340184914b5SBarry Smith    Input Parameter:
1341184914b5SBarry Smith .  snes - the SNES context
1342184914b5SBarry Smith 
1343184914b5SBarry Smith    Output Parameter:
1344e090d566SSatish Balay .  reason - negative value indicates diverged, positive value converged, see petscsnes.h or the
1345184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
1346184914b5SBarry Smith 
1347184914b5SBarry Smith    Level: intermediate
1348184914b5SBarry Smith 
1349184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
1350184914b5SBarry Smith 
1351184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
1352184914b5SBarry Smith 
1353*4b27c08aSLois Curfman McInnes .seealso: SNESSetConvergenceTest(), SNESConverged_LS(), SNESConverged_TR(), SNESConvergedReason
1354184914b5SBarry Smith @*/
1355184914b5SBarry Smith int SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
1356184914b5SBarry Smith {
1357184914b5SBarry Smith   PetscFunctionBegin;
1358184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1359184914b5SBarry Smith   *reason = snes->reason;
1360184914b5SBarry Smith   PetscFunctionReturn(0);
1361184914b5SBarry Smith }
1362184914b5SBarry Smith 
13634a2ae208SSatish Balay #undef __FUNCT__
13644a2ae208SSatish Balay #define __FUNCT__ "SNESSetConvergenceHistory"
1365c9005455SLois Curfman McInnes /*@
1366c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
1367c9005455SLois Curfman McInnes 
1368fee21e36SBarry Smith    Collective on SNES
1369fee21e36SBarry Smith 
1370c7afd0dbSLois Curfman McInnes    Input Parameters:
1371c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
1372c7afd0dbSLois Curfman McInnes .  a   - array to hold history
1373cd5578b5SBarry Smith .  its - integer array holds the number of linear iterations for each solve.
1374758f92a0SBarry Smith .  na  - size of a and its
137564731454SLois Curfman McInnes -  reset - PETSC_TRUE indicates each new nonlinear solve resets the history counter to zero,
1376758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
1377c7afd0dbSLois Curfman McInnes 
1378c9005455SLois Curfman McInnes    Notes:
1379*4b27c08aSLois Curfman McInnes    If set, this array will contain the function norms computed
1380c9005455SLois Curfman McInnes    at each step.
1381c9005455SLois Curfman McInnes 
1382c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
1383c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
1384c9005455SLois Curfman McInnes    during the section of code that is being timed.
1385c9005455SLois Curfman McInnes 
138636851e7fSLois Curfman McInnes    Level: intermediate
138736851e7fSLois Curfman McInnes 
1388c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
1389758f92a0SBarry Smith 
139008405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
1391758f92a0SBarry Smith 
1392c9005455SLois Curfman McInnes @*/
1393329f5518SBarry Smith int SNESSetConvergenceHistory(SNES snes,PetscReal *a,int *its,int na,PetscTruth reset)
1394c9005455SLois Curfman McInnes {
13953a40ed3dSBarry Smith   PetscFunctionBegin;
1396c9005455SLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1397c9005455SLois Curfman McInnes   if (na) PetscValidScalarPointer(a);
1398c9005455SLois Curfman McInnes   snes->conv_hist       = a;
1399758f92a0SBarry Smith   snes->conv_hist_its   = its;
1400758f92a0SBarry Smith   snes->conv_hist_max   = na;
1401758f92a0SBarry Smith   snes->conv_hist_reset = reset;
1402758f92a0SBarry Smith   PetscFunctionReturn(0);
1403758f92a0SBarry Smith }
1404758f92a0SBarry Smith 
14054a2ae208SSatish Balay #undef __FUNCT__
14064a2ae208SSatish Balay #define __FUNCT__ "SNESGetConvergenceHistory"
14070c4c9dddSBarry Smith /*@C
1408758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
1409758f92a0SBarry Smith 
1410758f92a0SBarry Smith    Collective on SNES
1411758f92a0SBarry Smith 
1412758f92a0SBarry Smith    Input Parameter:
1413758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
1414758f92a0SBarry Smith 
1415758f92a0SBarry Smith    Output Parameters:
1416758f92a0SBarry Smith .  a   - array to hold history
1417758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
1418758f92a0SBarry Smith          negative if not converged) for each solve.
1419758f92a0SBarry Smith -  na  - size of a and its
1420758f92a0SBarry Smith 
1421758f92a0SBarry Smith    Notes:
1422758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
1423758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
1424758f92a0SBarry Smith 
1425758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
1426758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
1427758f92a0SBarry Smith    during the section of code that is being timed.
1428758f92a0SBarry Smith 
1429758f92a0SBarry Smith    Level: intermediate
1430758f92a0SBarry Smith 
1431758f92a0SBarry Smith .keywords: SNES, get, convergence, history
1432758f92a0SBarry Smith 
1433758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
1434758f92a0SBarry Smith 
1435758f92a0SBarry Smith @*/
1436329f5518SBarry Smith int SNESGetConvergenceHistory(SNES snes,PetscReal **a,int **its,int *na)
1437758f92a0SBarry Smith {
1438758f92a0SBarry Smith   PetscFunctionBegin;
1439758f92a0SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1440758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
1441758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
1442758f92a0SBarry Smith   if (na) *na   = snes->conv_hist_len;
14433a40ed3dSBarry Smith   PetscFunctionReturn(0);
1444c9005455SLois Curfman McInnes }
1445c9005455SLois Curfman McInnes 
1446e74ef692SMatthew Knepley #undef __FUNCT__
1447e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetRhsBC"
144876b2cf59SMatthew Knepley /*@
144976b2cf59SMatthew Knepley   SNESSetRhsBC - Sets the function which applies boundary conditions
145076b2cf59SMatthew Knepley   to the Rhs of each system.
145176b2cf59SMatthew Knepley 
145276b2cf59SMatthew Knepley   Collective on SNES
145376b2cf59SMatthew Knepley 
145476b2cf59SMatthew Knepley   Input Parameters:
145576b2cf59SMatthew Knepley . snes - The nonlinear solver context
145676b2cf59SMatthew Knepley . func - The function
145776b2cf59SMatthew Knepley 
145876b2cf59SMatthew Knepley   Calling sequence of func:
145976b2cf59SMatthew Knepley . func (SNES snes, Vec rhs, void *ctx);
146076b2cf59SMatthew Knepley 
146176b2cf59SMatthew Knepley . rhs - The current rhs vector
146276b2cf59SMatthew Knepley . ctx - The user-context
146376b2cf59SMatthew Knepley 
146476b2cf59SMatthew Knepley   Level: intermediate
146576b2cf59SMatthew Knepley 
146676b2cf59SMatthew Knepley .keywords: SNES, Rhs, boundary conditions
146776b2cf59SMatthew Knepley .seealso SNESDefaultRhsBC(), SNESSetSolutionBC(), SNESSetUpdate()
146876b2cf59SMatthew Knepley @*/
146976b2cf59SMatthew Knepley int SNESSetRhsBC(SNES snes, int (*func)(SNES, Vec, void *))
147076b2cf59SMatthew Knepley {
147176b2cf59SMatthew Knepley   PetscFunctionBegin;
147276b2cf59SMatthew Knepley   PetscValidHeaderSpecific(snes, SNES_COOKIE);
147376b2cf59SMatthew Knepley   snes->applyrhsbc = func;
147476b2cf59SMatthew Knepley   PetscFunctionReturn(0);
147576b2cf59SMatthew Knepley }
147676b2cf59SMatthew Knepley 
1477e74ef692SMatthew Knepley #undef __FUNCT__
1478e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultRhsBC"
147976b2cf59SMatthew Knepley /*@
148076b2cf59SMatthew Knepley   SNESDefaultRhsBC - The default boundary condition function which does nothing.
148176b2cf59SMatthew Knepley 
148276b2cf59SMatthew Knepley   Not collective
148376b2cf59SMatthew Knepley 
148476b2cf59SMatthew Knepley   Input Parameters:
148576b2cf59SMatthew Knepley . snes - The nonlinear solver context
148676b2cf59SMatthew Knepley . rhs  - The Rhs
148776b2cf59SMatthew Knepley . ctx  - The user-context
148876b2cf59SMatthew Knepley 
148976b2cf59SMatthew Knepley   Level: beginner
149076b2cf59SMatthew Knepley 
149176b2cf59SMatthew Knepley .keywords: SNES, Rhs, boundary conditions
149276b2cf59SMatthew Knepley .seealso SNESSetRhsBC(), SNESDefaultSolutionBC(), SNESDefaultUpdate()
149376b2cf59SMatthew Knepley @*/
149476b2cf59SMatthew Knepley int SNESDefaultRhsBC(SNES snes, Vec rhs, void *ctx)
149576b2cf59SMatthew Knepley {
149676b2cf59SMatthew Knepley   PetscFunctionBegin;
149776b2cf59SMatthew Knepley   PetscFunctionReturn(0);
149876b2cf59SMatthew Knepley }
149976b2cf59SMatthew Knepley 
1500e74ef692SMatthew Knepley #undef __FUNCT__
1501e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetSolutionBC"
150276b2cf59SMatthew Knepley /*@
150376b2cf59SMatthew Knepley   SNESSetSolutionBC - Sets the function which applies boundary conditions
150476b2cf59SMatthew Knepley   to the solution of each system.
150576b2cf59SMatthew Knepley 
150676b2cf59SMatthew Knepley   Collective on SNES
150776b2cf59SMatthew Knepley 
150876b2cf59SMatthew Knepley   Input Parameters:
150976b2cf59SMatthew Knepley . snes - The nonlinear solver context
151076b2cf59SMatthew Knepley . func - The function
151176b2cf59SMatthew Knepley 
151276b2cf59SMatthew Knepley   Calling sequence of func:
151376b2cf59SMatthew Knepley . func (TS ts, Vec rsol, void *ctx);
151476b2cf59SMatthew Knepley 
151576b2cf59SMatthew Knepley . sol - The current solution vector
151676b2cf59SMatthew Knepley . ctx - The user-context
151776b2cf59SMatthew Knepley 
151876b2cf59SMatthew Knepley   Level: intermediate
151976b2cf59SMatthew Knepley 
152076b2cf59SMatthew Knepley .keywords: SNES, solution, boundary conditions
152176b2cf59SMatthew Knepley .seealso SNESDefaultSolutionBC(), SNESSetRhsBC(), SNESSetUpdate()
152276b2cf59SMatthew Knepley @*/
152376b2cf59SMatthew Knepley int SNESSetSolutionBC(SNES snes, int (*func)(SNES, Vec, void *))
152476b2cf59SMatthew Knepley {
152576b2cf59SMatthew Knepley   PetscFunctionBegin;
152676b2cf59SMatthew Knepley   PetscValidHeaderSpecific(snes, SNES_COOKIE);
152776b2cf59SMatthew Knepley   snes->applysolbc = func;
152876b2cf59SMatthew Knepley   PetscFunctionReturn(0);
152976b2cf59SMatthew Knepley }
153076b2cf59SMatthew Knepley 
1531e74ef692SMatthew Knepley #undef __FUNCT__
1532e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultSolutionBC"
153376b2cf59SMatthew Knepley /*@
153476b2cf59SMatthew Knepley   SNESDefaultSolutionBC - The default boundary condition function which does nothing.
153576b2cf59SMatthew Knepley 
153676b2cf59SMatthew Knepley   Not collective
153776b2cf59SMatthew Knepley 
153876b2cf59SMatthew Knepley   Input Parameters:
153976b2cf59SMatthew Knepley . snes - The nonlinear solver context
154076b2cf59SMatthew Knepley . sol  - The solution
154176b2cf59SMatthew Knepley . ctx  - The user-context
154276b2cf59SMatthew Knepley 
154376b2cf59SMatthew Knepley   Level: beginner
154476b2cf59SMatthew Knepley 
154576b2cf59SMatthew Knepley .keywords: SNES, solution, boundary conditions
154676b2cf59SMatthew Knepley .seealso SNESSetSolutionBC(), SNESDefaultRhsBC(), SNESDefaultUpdate()
154776b2cf59SMatthew Knepley @*/
154876b2cf59SMatthew Knepley int SNESDefaultSolutionBC(SNES snes, Vec sol, void *ctx)
154976b2cf59SMatthew Knepley {
155076b2cf59SMatthew Knepley   PetscFunctionBegin;
155176b2cf59SMatthew Knepley   PetscFunctionReturn(0);
155276b2cf59SMatthew Knepley }
155376b2cf59SMatthew Knepley 
1554e74ef692SMatthew Knepley #undef __FUNCT__
1555e74ef692SMatthew Knepley #define __FUNCT__ "SNESSetUpdate"
155676b2cf59SMatthew Knepley /*@
155776b2cf59SMatthew Knepley   SNESSetUpdate - Sets the general-purpose update function called
155876b2cf59SMatthew Knepley   at the beginning of every step of the iteration.
155976b2cf59SMatthew Knepley 
156076b2cf59SMatthew Knepley   Collective on SNES
156176b2cf59SMatthew Knepley 
156276b2cf59SMatthew Knepley   Input Parameters:
156376b2cf59SMatthew Knepley . snes - The nonlinear solver context
156476b2cf59SMatthew Knepley . func - The function
156576b2cf59SMatthew Knepley 
156676b2cf59SMatthew Knepley   Calling sequence of func:
156776b2cf59SMatthew Knepley . func (TS ts, int step);
156876b2cf59SMatthew Knepley 
156976b2cf59SMatthew Knepley . step - The current step of the iteration
157076b2cf59SMatthew Knepley 
157176b2cf59SMatthew Knepley   Level: intermediate
157276b2cf59SMatthew Knepley 
157376b2cf59SMatthew Knepley .keywords: SNES, update
157476b2cf59SMatthew Knepley .seealso SNESDefaultUpdate(), SNESSetRhsBC(), SNESSetSolutionBC()
157576b2cf59SMatthew Knepley @*/
157676b2cf59SMatthew Knepley int SNESSetUpdate(SNES snes, int (*func)(SNES, int))
157776b2cf59SMatthew Knepley {
157876b2cf59SMatthew Knepley   PetscFunctionBegin;
157976b2cf59SMatthew Knepley   PetscValidHeaderSpecific(snes, SNES_COOKIE);
158076b2cf59SMatthew Knepley   snes->update = func;
158176b2cf59SMatthew Knepley   PetscFunctionReturn(0);
158276b2cf59SMatthew Knepley }
158376b2cf59SMatthew Knepley 
1584e74ef692SMatthew Knepley #undef __FUNCT__
1585e74ef692SMatthew Knepley #define __FUNCT__ "SNESDefaultUpdate"
158676b2cf59SMatthew Knepley /*@
158776b2cf59SMatthew Knepley   SNESDefaultUpdate - The default update function which does nothing.
158876b2cf59SMatthew Knepley 
158976b2cf59SMatthew Knepley   Not collective
159076b2cf59SMatthew Knepley 
159176b2cf59SMatthew Knepley   Input Parameters:
159276b2cf59SMatthew Knepley . snes - The nonlinear solver context
159376b2cf59SMatthew Knepley . step - The current step of the iteration
159476b2cf59SMatthew Knepley 
1595205452f4SMatthew Knepley   Level: intermediate
1596205452f4SMatthew Knepley 
159776b2cf59SMatthew Knepley .keywords: SNES, update
159876b2cf59SMatthew Knepley .seealso SNESSetUpdate(), SNESDefaultRhsBC(), SNESDefaultSolutionBC()
159976b2cf59SMatthew Knepley @*/
160076b2cf59SMatthew Knepley int SNESDefaultUpdate(SNES snes, int step)
160176b2cf59SMatthew Knepley {
160276b2cf59SMatthew Knepley   PetscFunctionBegin;
160376b2cf59SMatthew Knepley   PetscFunctionReturn(0);
160476b2cf59SMatthew Knepley }
160576b2cf59SMatthew Knepley 
16064a2ae208SSatish Balay #undef __FUNCT__
16074a2ae208SSatish Balay #define __FUNCT__ "SNESScaleStep_Private"
16089b94acceSBarry Smith /*
16099b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
16109b94acceSBarry Smith    positive parameter delta.
16119b94acceSBarry Smith 
16129b94acceSBarry Smith     Input Parameters:
1613c7afd0dbSLois Curfman McInnes +   snes - the SNES context
16149b94acceSBarry Smith .   y - approximate solution of linear system
16159b94acceSBarry Smith .   fnorm - 2-norm of current function
1616c7afd0dbSLois Curfman McInnes -   delta - trust region size
16179b94acceSBarry Smith 
16189b94acceSBarry Smith     Output Parameters:
1619c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
16209b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
16219b94acceSBarry Smith     region, and exceeds zero otherwise.
1622c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
16239b94acceSBarry Smith 
16249b94acceSBarry Smith     Note:
1625*4b27c08aSLois Curfman McInnes     For non-trust region methods such as SNESLS, the parameter delta
16269b94acceSBarry Smith     is set to be the maximum allowable step size.
16279b94acceSBarry Smith 
16289b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
16299b94acceSBarry Smith */
1630064f8208SBarry Smith int SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,PetscReal *gpnorm,PetscReal *ynorm)
16319b94acceSBarry Smith {
1632064f8208SBarry Smith   PetscReal   nrm;
1633ea709b57SSatish Balay   PetscScalar cnorm;
16343a40ed3dSBarry Smith   int         ierr;
16353a40ed3dSBarry Smith 
16363a40ed3dSBarry Smith   PetscFunctionBegin;
1637184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1638184914b5SBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE);
1639184914b5SBarry Smith   PetscCheckSameComm(snes,y);
1640184914b5SBarry Smith 
1641064f8208SBarry Smith   ierr = VecNorm(y,NORM_2,&nrm);CHKERRQ(ierr);
1642064f8208SBarry Smith   if (nrm > *delta) {
1643064f8208SBarry Smith      nrm = *delta/nrm;
1644064f8208SBarry Smith      *gpnorm = (1.0 - nrm)*(*fnorm);
1645064f8208SBarry Smith      cnorm = nrm;
1646329f5518SBarry Smith      ierr = VecScale(&cnorm,y);CHKERRQ(ierr);
16479b94acceSBarry Smith      *ynorm = *delta;
16489b94acceSBarry Smith   } else {
16499b94acceSBarry Smith      *gpnorm = 0.0;
1650064f8208SBarry Smith      *ynorm = nrm;
16519b94acceSBarry Smith   }
16523a40ed3dSBarry Smith   PetscFunctionReturn(0);
16539b94acceSBarry Smith }
16549b94acceSBarry Smith 
16554a2ae208SSatish Balay #undef __FUNCT__
16564a2ae208SSatish Balay #define __FUNCT__ "SNESSolve"
16579b94acceSBarry Smith /*@
16589b94acceSBarry Smith    SNESSolve - Solves a nonlinear system.  Call SNESSolve after calling
165963a78c88SLois Curfman McInnes    SNESCreate() and optional routines of the form SNESSetXXX().
16609b94acceSBarry Smith 
1661c7afd0dbSLois Curfman McInnes    Collective on SNES
1662c7afd0dbSLois Curfman McInnes 
1663b2002411SLois Curfman McInnes    Input Parameters:
1664c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1665c7afd0dbSLois Curfman McInnes -  x - the solution vector
16669b94acceSBarry Smith 
16679b94acceSBarry Smith    Output Parameter:
1668b2002411SLois Curfman McInnes .  its - number of iterations until termination
16699b94acceSBarry Smith 
1670b2002411SLois Curfman McInnes    Notes:
16718ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
16728ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
16738ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
16748ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
16758ddd3da0SLois Curfman McInnes 
167636851e7fSLois Curfman McInnes    Level: beginner
167736851e7fSLois Curfman McInnes 
16789b94acceSBarry Smith .keywords: SNES, nonlinear, solve
16799b94acceSBarry Smith 
168063a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESDestroy()
16819b94acceSBarry Smith @*/
16828ddd3da0SLois Curfman McInnes int SNESSolve(SNES snes,Vec x,int *its)
16839b94acceSBarry Smith {
1684f1af5d2fSBarry Smith   int        ierr;
1685f1af5d2fSBarry Smith   PetscTruth flg;
1686052efed2SBarry Smith 
16873a40ed3dSBarry Smith   PetscFunctionBegin;
168877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1689184914b5SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
1690184914b5SBarry Smith   PetscCheckSameComm(snes,x);
169174679c65SBarry Smith   PetscValidIntPointer(its);
169229bbc08cSBarry Smith   if (!snes->solve) SETERRQ(1,"SNESSetType() or SNESSetFromOptions() must be called before SNESSolve()");
169374637425SBarry Smith 
169482bf6240SBarry Smith   if (!snes->setupcalled) {ierr = SNESSetUp(snes,x);CHKERRQ(ierr);}
1695c4fc05e7SBarry Smith   else {snes->vec_sol = snes->vec_sol_always = x;}
1696758f92a0SBarry Smith   if (snes->conv_hist_reset == PETSC_TRUE) snes->conv_hist_len = 0;
1697d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
169850ffb88aSMatthew Knepley   snes->nfuncs = 0; snes->linear_its = 0; snes->numFailures = 0;
16999b94acceSBarry Smith   ierr = (*(snes)->solve)(snes,its);CHKERRQ(ierr);
1700d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(SNES_Solve,snes,0,0,0);CHKERRQ(ierr);
1701b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(snes->prefix,"-snes_view",&flg);CHKERRQ(ierr);
170293b98531SBarry Smith   if (flg && !PetscPreLoadingOn) { ierr = SNESView(snes,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); }
17033a40ed3dSBarry Smith   PetscFunctionReturn(0);
17049b94acceSBarry Smith }
17059b94acceSBarry Smith 
17069b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
17079b94acceSBarry Smith 
17084a2ae208SSatish Balay #undef __FUNCT__
17094a2ae208SSatish Balay #define __FUNCT__ "SNESSetType"
171082bf6240SBarry Smith /*@C
17114b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
17129b94acceSBarry Smith 
1713fee21e36SBarry Smith    Collective on SNES
1714fee21e36SBarry Smith 
1715c7afd0dbSLois Curfman McInnes    Input Parameters:
1716c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1717454a90a3SBarry Smith -  type - a known method
1718c7afd0dbSLois Curfman McInnes 
1719c7afd0dbSLois Curfman McInnes    Options Database Key:
1720454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
1721c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
1722ae12b187SLois Curfman McInnes 
17239b94acceSBarry Smith    Notes:
1724e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
1725*4b27c08aSLois Curfman McInnes +    SNESLS - Newton's method with line search
1726c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
1727*4b27c08aSLois Curfman McInnes .    SNESTR - Newton's method with trust region
1728c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
17299b94acceSBarry Smith 
1730ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
1731ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
1732ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
1733ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
1734ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
1735ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
1736ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
1737ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
1738ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
1739b0a32e0cSBarry Smith   appropriate method.
174036851e7fSLois Curfman McInnes 
174136851e7fSLois Curfman McInnes   Level: intermediate
1742a703fe33SLois Curfman McInnes 
1743454a90a3SBarry Smith .keywords: SNES, set, type
1744435da068SBarry Smith 
1745435da068SBarry Smith .seealso: SNESType, SNESCreate()
1746435da068SBarry Smith 
17479b94acceSBarry Smith @*/
1748454a90a3SBarry Smith int SNESSetType(SNES snes,SNESType type)
17499b94acceSBarry Smith {
17500f5bd95cSBarry Smith   int        ierr,(*r)(SNES);
17516831982aSBarry Smith   PetscTruth match;
17523a40ed3dSBarry Smith 
17533a40ed3dSBarry Smith   PetscFunctionBegin;
175477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
17550f5bd95cSBarry Smith   PetscValidCharPointer(type);
175682bf6240SBarry Smith 
17576831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
17580f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
175982bf6240SBarry Smith 
176082bf6240SBarry Smith   if (snes->setupcalled) {
1761e1311b90SBarry Smith     ierr       = (*(snes)->destroy)(snes);CHKERRQ(ierr);
176282bf6240SBarry Smith     snes->data = 0;
176382bf6240SBarry Smith   }
17647d1a2b2bSBarry Smith 
17659b94acceSBarry Smith   /* Get the function pointers for the iterative method requested */
176682bf6240SBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
176782bf6240SBarry Smith 
1768b9617806SBarry Smith   ierr =  PetscFListFind(snes->comm,SNESList,type,(void (**)(void)) &r);CHKERRQ(ierr);
176982bf6240SBarry Smith 
177029bbc08cSBarry Smith   if (!r) SETERRQ1(1,"Unable to find requested SNES type %s",type);
17711548b14aSBarry Smith 
1772606d414cSSatish Balay   if (snes->data) {ierr = PetscFree(snes->data);CHKERRQ(ierr);}
177382bf6240SBarry Smith   snes->data = 0;
17743a40ed3dSBarry Smith   ierr = (*r)(snes);CHKERRQ(ierr);
1775454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
177682bf6240SBarry Smith 
17773a40ed3dSBarry Smith   PetscFunctionReturn(0);
17789b94acceSBarry Smith }
17799b94acceSBarry Smith 
1780a847f771SSatish Balay 
17819b94acceSBarry Smith /* --------------------------------------------------------------------- */
17824a2ae208SSatish Balay #undef __FUNCT__
17834a2ae208SSatish Balay #define __FUNCT__ "SNESRegisterDestroy"
17849b94acceSBarry Smith /*@C
17859b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
1786f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
17879b94acceSBarry Smith 
1788fee21e36SBarry Smith    Not Collective
1789fee21e36SBarry Smith 
179036851e7fSLois Curfman McInnes    Level: advanced
179136851e7fSLois Curfman McInnes 
17929b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
17939b94acceSBarry Smith 
17949b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
17959b94acceSBarry Smith @*/
1796cf256101SBarry Smith int SNESRegisterDestroy(void)
17979b94acceSBarry Smith {
179882bf6240SBarry Smith   int ierr;
179982bf6240SBarry Smith 
18003a40ed3dSBarry Smith   PetscFunctionBegin;
180182bf6240SBarry Smith   if (SNESList) {
1802b0a32e0cSBarry Smith     ierr = PetscFListDestroy(&SNESList);CHKERRQ(ierr);
180382bf6240SBarry Smith     SNESList = 0;
18049b94acceSBarry Smith   }
18054c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
18063a40ed3dSBarry Smith   PetscFunctionReturn(0);
18079b94acceSBarry Smith }
18089b94acceSBarry Smith 
18094a2ae208SSatish Balay #undef __FUNCT__
18104a2ae208SSatish Balay #define __FUNCT__ "SNESGetType"
18119b94acceSBarry Smith /*@C
18129a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
18139b94acceSBarry Smith 
1814c7afd0dbSLois Curfman McInnes    Not Collective
1815c7afd0dbSLois Curfman McInnes 
18169b94acceSBarry Smith    Input Parameter:
18174b0e389bSBarry Smith .  snes - nonlinear solver context
18189b94acceSBarry Smith 
18199b94acceSBarry Smith    Output Parameter:
18203a7fca6bSBarry Smith .  type - SNES method (a character string)
18219b94acceSBarry Smith 
182236851e7fSLois Curfman McInnes    Level: intermediate
182336851e7fSLois Curfman McInnes 
1824454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
18259b94acceSBarry Smith @*/
1826454a90a3SBarry Smith int SNESGetType(SNES snes,SNESType *type)
18279b94acceSBarry Smith {
18283a40ed3dSBarry Smith   PetscFunctionBegin;
1829184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1830454a90a3SBarry Smith   *type = snes->type_name;
18313a40ed3dSBarry Smith   PetscFunctionReturn(0);
18329b94acceSBarry Smith }
18339b94acceSBarry Smith 
18344a2ae208SSatish Balay #undef __FUNCT__
18354a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolution"
18369b94acceSBarry Smith /*@C
18379b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
18389b94acceSBarry Smith    stored.
18399b94acceSBarry Smith 
1840c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
1841c7afd0dbSLois Curfman McInnes 
18429b94acceSBarry Smith    Input Parameter:
18439b94acceSBarry Smith .  snes - the SNES context
18449b94acceSBarry Smith 
18459b94acceSBarry Smith    Output Parameter:
18469b94acceSBarry Smith .  x - the solution
18479b94acceSBarry Smith 
184836851e7fSLois Curfman McInnes    Level: advanced
184936851e7fSLois Curfman McInnes 
18509b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
18519b94acceSBarry Smith 
1852*4b27c08aSLois Curfman McInnes .seealso: SNESGetFunction(), SNESGetSolutionUpdate()
18539b94acceSBarry Smith @*/
18549b94acceSBarry Smith int SNESGetSolution(SNES snes,Vec *x)
18559b94acceSBarry Smith {
18563a40ed3dSBarry Smith   PetscFunctionBegin;
185777c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
18589b94acceSBarry Smith   *x = snes->vec_sol_always;
18593a40ed3dSBarry Smith   PetscFunctionReturn(0);
18609b94acceSBarry Smith }
18619b94acceSBarry Smith 
18624a2ae208SSatish Balay #undef __FUNCT__
18634a2ae208SSatish Balay #define __FUNCT__ "SNESGetSolutionUpdate"
18649b94acceSBarry Smith /*@C
18659b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
18669b94acceSBarry Smith    stored.
18679b94acceSBarry Smith 
1868c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
1869c7afd0dbSLois Curfman McInnes 
18709b94acceSBarry Smith    Input Parameter:
18719b94acceSBarry Smith .  snes - the SNES context
18729b94acceSBarry Smith 
18739b94acceSBarry Smith    Output Parameter:
18749b94acceSBarry Smith .  x - the solution update
18759b94acceSBarry Smith 
187636851e7fSLois Curfman McInnes    Level: advanced
187736851e7fSLois Curfman McInnes 
18789b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
18799b94acceSBarry Smith 
18809b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction
18819b94acceSBarry Smith @*/
18829b94acceSBarry Smith int SNESGetSolutionUpdate(SNES snes,Vec *x)
18839b94acceSBarry Smith {
18843a40ed3dSBarry Smith   PetscFunctionBegin;
188577c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
18869b94acceSBarry Smith   *x = snes->vec_sol_update_always;
18873a40ed3dSBarry Smith   PetscFunctionReturn(0);
18889b94acceSBarry Smith }
18899b94acceSBarry Smith 
18904a2ae208SSatish Balay #undef __FUNCT__
18914a2ae208SSatish Balay #define __FUNCT__ "SNESGetFunction"
18929b94acceSBarry Smith /*@C
18933638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
18949b94acceSBarry Smith 
1895c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
1896c7afd0dbSLois Curfman McInnes 
18979b94acceSBarry Smith    Input Parameter:
18989b94acceSBarry Smith .  snes - the SNES context
18999b94acceSBarry Smith 
19009b94acceSBarry Smith    Output Parameter:
19017bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
190200036973SBarry Smith .  ctx - the function context (or PETSC_NULL)
190300036973SBarry Smith -  func - the function (or PETSC_NULL)
19049b94acceSBarry Smith 
190536851e7fSLois Curfman McInnes    Level: advanced
190636851e7fSLois Curfman McInnes 
1907a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
19089b94acceSBarry Smith 
1909*4b27c08aSLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution()
19109b94acceSBarry Smith @*/
191100036973SBarry Smith int SNESGetFunction(SNES snes,Vec *r,void **ctx,int (**func)(SNES,Vec,Vec,void*))
19129b94acceSBarry Smith {
19133a40ed3dSBarry Smith   PetscFunctionBegin;
191477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
19157bf4e008SBarry Smith   if (r)    *r    = snes->vec_func_always;
19167bf4e008SBarry Smith   if (ctx)  *ctx  = snes->funP;
191700036973SBarry Smith   if (func) *func = snes->computefunction;
19183a40ed3dSBarry Smith   PetscFunctionReturn(0);
19199b94acceSBarry Smith }
19209b94acceSBarry Smith 
19214a2ae208SSatish Balay #undef __FUNCT__
19224a2ae208SSatish Balay #define __FUNCT__ "SNESSetOptionsPrefix"
19233c7409f5SSatish Balay /*@C
19243c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
1925d850072dSLois Curfman McInnes    SNES options in the database.
19263c7409f5SSatish Balay 
1927fee21e36SBarry Smith    Collective on SNES
1928fee21e36SBarry Smith 
1929c7afd0dbSLois Curfman McInnes    Input Parameter:
1930c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1931c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
1932c7afd0dbSLois Curfman McInnes 
1933d850072dSLois Curfman McInnes    Notes:
1934a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
1935c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
1936d850072dSLois Curfman McInnes 
193736851e7fSLois Curfman McInnes    Level: advanced
193836851e7fSLois Curfman McInnes 
19393c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
1940a86d99e1SLois Curfman McInnes 
1941a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
19423c7409f5SSatish Balay @*/
19433c7409f5SSatish Balay int SNESSetOptionsPrefix(SNES snes,char *prefix)
19443c7409f5SSatish Balay {
19453c7409f5SSatish Balay   int ierr;
19463c7409f5SSatish Balay 
19473a40ed3dSBarry Smith   PetscFunctionBegin;
194877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1949639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
19503c7409f5SSatish Balay   ierr = SLESSetOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr);
19513a40ed3dSBarry Smith   PetscFunctionReturn(0);
19523c7409f5SSatish Balay }
19533c7409f5SSatish Balay 
19544a2ae208SSatish Balay #undef __FUNCT__
19554a2ae208SSatish Balay #define __FUNCT__ "SNESAppendOptionsPrefix"
19563c7409f5SSatish Balay /*@C
1957f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
1958d850072dSLois Curfman McInnes    SNES options in the database.
19593c7409f5SSatish Balay 
1960fee21e36SBarry Smith    Collective on SNES
1961fee21e36SBarry Smith 
1962c7afd0dbSLois Curfman McInnes    Input Parameters:
1963c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1964c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
1965c7afd0dbSLois Curfman McInnes 
1966d850072dSLois Curfman McInnes    Notes:
1967a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
1968c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
1969d850072dSLois Curfman McInnes 
197036851e7fSLois Curfman McInnes    Level: advanced
197136851e7fSLois Curfman McInnes 
19723c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
1973a86d99e1SLois Curfman McInnes 
1974a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
19753c7409f5SSatish Balay @*/
19763c7409f5SSatish Balay int SNESAppendOptionsPrefix(SNES snes,char *prefix)
19773c7409f5SSatish Balay {
19783c7409f5SSatish Balay   int ierr;
19793c7409f5SSatish Balay 
19803a40ed3dSBarry Smith   PetscFunctionBegin;
198177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1982639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
19833c7409f5SSatish Balay   ierr = SLESAppendOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr);
19843a40ed3dSBarry Smith   PetscFunctionReturn(0);
19853c7409f5SSatish Balay }
19863c7409f5SSatish Balay 
19874a2ae208SSatish Balay #undef __FUNCT__
19884a2ae208SSatish Balay #define __FUNCT__ "SNESGetOptionsPrefix"
19899ab63eb5SSatish Balay /*@C
19903c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
19913c7409f5SSatish Balay    SNES options in the database.
19923c7409f5SSatish Balay 
1993c7afd0dbSLois Curfman McInnes    Not Collective
1994c7afd0dbSLois Curfman McInnes 
19953c7409f5SSatish Balay    Input Parameter:
19963c7409f5SSatish Balay .  snes - the SNES context
19973c7409f5SSatish Balay 
19983c7409f5SSatish Balay    Output Parameter:
19993c7409f5SSatish Balay .  prefix - pointer to the prefix string used
20003c7409f5SSatish Balay 
20019ab63eb5SSatish Balay    Notes: On the fortran side, the user should pass in a string 'prifix' of
20029ab63eb5SSatish Balay    sufficient length to hold the prefix.
20039ab63eb5SSatish Balay 
200436851e7fSLois Curfman McInnes    Level: advanced
200536851e7fSLois Curfman McInnes 
20063c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2007a86d99e1SLois Curfman McInnes 
2008a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
20093c7409f5SSatish Balay @*/
20103c7409f5SSatish Balay int SNESGetOptionsPrefix(SNES snes,char **prefix)
20113c7409f5SSatish Balay {
20123c7409f5SSatish Balay   int ierr;
20133c7409f5SSatish Balay 
20143a40ed3dSBarry Smith   PetscFunctionBegin;
201577c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2016639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
20173a40ed3dSBarry Smith   PetscFunctionReturn(0);
20183c7409f5SSatish Balay }
20193c7409f5SSatish Balay 
2020acb85ae6SSatish Balay /*MC
2021f1af5d2fSBarry Smith    SNESRegisterDynamic - Adds a method to the nonlinear solver package.
20229b94acceSBarry Smith 
2023b2002411SLois Curfman McInnes    Synopsis:
20243a7fca6bSBarry Smith    int SNESRegisterDynamic(char *name_solver,char *path,char *name_create,int (*routine_create)(SNES))
20259b94acceSBarry Smith 
20268d76a1e5SLois Curfman McInnes    Not collective
20278d76a1e5SLois Curfman McInnes 
2028b2002411SLois Curfman McInnes    Input Parameters:
2029c7afd0dbSLois Curfman McInnes +  name_solver - name of a new user-defined solver
2030b2002411SLois Curfman McInnes .  path - path (either absolute or relative) the library containing this solver
2031b2002411SLois Curfman McInnes .  name_create - name of routine to create method context
2032c7afd0dbSLois Curfman McInnes -  routine_create - routine to create method context
20339b94acceSBarry Smith 
2034b2002411SLois Curfman McInnes    Notes:
2035f1af5d2fSBarry Smith    SNESRegisterDynamic() may be called multiple times to add several user-defined solvers.
20363a40ed3dSBarry Smith 
2037b2002411SLois Curfman McInnes    If dynamic libraries are used, then the fourth input argument (routine_create)
2038b2002411SLois Curfman McInnes    is ignored.
2039b2002411SLois Curfman McInnes 
2040b9617806SBarry Smith    Environmental variables such as ${PETSC_ARCH}, ${PETSC_DIR}, ${PETSC_LIB_DIR}, ${BOPT},
20415ba88a07SLois Curfman McInnes    and others of the form ${any_environmental_variable} occuring in pathname will be
20425ba88a07SLois Curfman McInnes    replaced with appropriate values.
20435ba88a07SLois Curfman McInnes 
2044b2002411SLois Curfman McInnes    Sample usage:
204569225d78SLois Curfman McInnes .vb
2046f1af5d2fSBarry Smith    SNESRegisterDynamic("my_solver",/home/username/my_lib/lib/libg/solaris/mylib.a,
2047b2002411SLois Curfman McInnes                 "MySolverCreate",MySolverCreate);
204869225d78SLois Curfman McInnes .ve
2049b2002411SLois Curfman McInnes 
2050b2002411SLois Curfman McInnes    Then, your solver can be chosen with the procedural interface via
2051b2002411SLois Curfman McInnes $     SNESSetType(snes,"my_solver")
2052b2002411SLois Curfman McInnes    or at runtime via the option
2053b2002411SLois Curfman McInnes $     -snes_type my_solver
2054b2002411SLois Curfman McInnes 
205536851e7fSLois Curfman McInnes    Level: advanced
205636851e7fSLois Curfman McInnes 
2057b2002411SLois Curfman McInnes .keywords: SNES, nonlinear, register
2058b2002411SLois Curfman McInnes 
2059b2002411SLois Curfman McInnes .seealso: SNESRegisterAll(), SNESRegisterDestroy()
2060b2002411SLois Curfman McInnes M*/
2061b2002411SLois Curfman McInnes 
20624a2ae208SSatish Balay #undef __FUNCT__
20634a2ae208SSatish Balay #define __FUNCT__ "SNESRegister"
2064f1af5d2fSBarry Smith int SNESRegister(char *sname,char *path,char *name,int (*function)(SNES))
2065b2002411SLois Curfman McInnes {
2066b2002411SLois Curfman McInnes   char fullname[256];
2067b2002411SLois Curfman McInnes   int  ierr;
2068b2002411SLois Curfman McInnes 
2069b2002411SLois Curfman McInnes   PetscFunctionBegin;
2070b0a32e0cSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2071c134de8dSSatish Balay   ierr = PetscFListAdd(&SNESList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
2072b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2073b2002411SLois Curfman McInnes }
2074