xref: /petsc/src/snes/interface/snes.c (revision 186905e3e415e69b907da891055a11b303d02b1e)
1*186905e3SBarry Smith /*$Id: snes.c,v 1.217 2000/08/17 04:52:40 bsmith Exp bsmith $*/
29b94acceSBarry Smith 
3e090d566SSatish Balay #include "src/snes/snesimpl.h"      /*I "petscsnes.h"  I*/
49b94acceSBarry Smith 
54c49b128SBarry Smith PetscTruth SNESRegisterAllCalled = PETSC_FALSE;
6488ecbafSBarry Smith FList      SNESList = 0;
782bf6240SBarry Smith 
85615d1e5SSatish Balay #undef __FUNC__
9b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESView"
107e2c5f70SBarry Smith /*@C
119b94acceSBarry Smith    SNESView - Prints the SNES data structure.
129b94acceSBarry Smith 
134c49b128SBarry Smith    Collective on SNES
14fee21e36SBarry Smith 
15c7afd0dbSLois Curfman McInnes    Input Parameters:
16c7afd0dbSLois Curfman McInnes +  SNES - the SNES context
17c7afd0dbSLois Curfman McInnes -  viewer - visualization context
18c7afd0dbSLois Curfman McInnes 
199b94acceSBarry Smith    Options Database Key:
20c8a8ba5cSLois Curfman McInnes .  -snes_view - Calls SNESView() at end of SNESSolve()
219b94acceSBarry Smith 
229b94acceSBarry Smith    Notes:
239b94acceSBarry Smith    The available visualization contexts include
24c7afd0dbSLois Curfman McInnes +     VIEWER_STDOUT_SELF - standard output (default)
25c7afd0dbSLois Curfman McInnes -     VIEWER_STDOUT_WORLD - synchronized standard
26c8a8ba5cSLois Curfman McInnes          output where only the first processor opens
27c8a8ba5cSLois Curfman McInnes          the file.  All other processors send their
28c8a8ba5cSLois Curfman McInnes          data to the first processor to print.
299b94acceSBarry Smith 
303e081fefSLois Curfman McInnes    The user can open an alternative visualization context with
3177ed5343SBarry Smith    ViewerASCIIOpen() - output to a specified file.
329b94acceSBarry Smith 
3336851e7fSLois Curfman McInnes    Level: beginner
3436851e7fSLois Curfman McInnes 
359b94acceSBarry Smith .keywords: SNES, view
369b94acceSBarry Smith 
3777ed5343SBarry Smith .seealso: ViewerASCIIOpen()
389b94acceSBarry Smith @*/
399b94acceSBarry Smith int SNESView(SNES snes,Viewer viewer)
409b94acceSBarry Smith {
419b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
429b94acceSBarry Smith   int                 ierr;
439b94acceSBarry Smith   SLES                sles;
44454a90a3SBarry Smith   char                *type;
456831982aSBarry Smith   PetscTruth          isascii,isstring;
469b94acceSBarry Smith 
473a40ed3dSBarry Smith   PetscFunctionBegin;
4874679c65SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
493eda8832SBarry Smith   if (!viewer) viewer = VIEWER_STDOUT_(snes->comm);
500f5bd95cSBarry Smith   PetscValidHeaderSpecific(viewer,VIEWER_COOKIE);
516831982aSBarry Smith   PetscCheckSameComm(snes,viewer);
5274679c65SBarry Smith 
536831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,ASCII_VIEWER,&isascii);CHKERRQ(ierr);
546831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,STRING_VIEWER,&isstring);CHKERRQ(ierr);
550f5bd95cSBarry Smith   if (isascii) {
56d132466eSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"SNES Object:\n");CHKERRQ(ierr);
57454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
58454a90a3SBarry Smith     if (type) {
59454a90a3SBarry Smith       ierr = ViewerASCIIPrintf(viewer,"  type: %s\n",type);CHKERRQ(ierr);
60184914b5SBarry Smith     } else {
61454a90a3SBarry Smith       ierr = ViewerASCIIPrintf(viewer,"  type: not set yet\n");CHKERRQ(ierr);
62184914b5SBarry Smith     }
630ef38995SBarry Smith     if (snes->view) {
640ef38995SBarry Smith       ierr = ViewerASCIIPushTab(viewer);CHKERRQ(ierr);
650ef38995SBarry Smith       ierr = (*snes->view)(snes,viewer);CHKERRQ(ierr);
660ef38995SBarry Smith       ierr = ViewerASCIIPopTab(viewer);CHKERRQ(ierr);
670ef38995SBarry Smith     }
68d132466eSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"  maximum iterations=%d, maximum function evaluations=%d\n",snes->max_its,snes->max_funcs);CHKERRQ(ierr);
69d132466eSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"  tolerances: relative=%g, absolute=%g, truncation=%g, solution=%g\n",
70d132466eSBarry Smith                  snes->rtol,snes->atol,snes->trunctol,snes->xtol);CHKERRQ(ierr);
71d132466eSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%d\n",snes->linear_its);CHKERRQ(ierr);
72d132466eSBarry Smith     ierr = ViewerASCIIPrintf(viewer,"  total number of function evaluations=%d\n",snes->nfuncs);CHKERRQ(ierr);
730ef38995SBarry Smith     if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
74d132466eSBarry Smith       ierr = ViewerASCIIPrintf(viewer,"  min function tolerance=%g\n",snes->fmin);CHKERRQ(ierr);
750ef38995SBarry Smith     }
769b94acceSBarry Smith     if (snes->ksp_ewconv) {
779b94acceSBarry Smith       kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
789b94acceSBarry Smith       if (kctx) {
79d132466eSBarry Smith         ierr = ViewerASCIIPrintf(viewer,"  Eisenstat-Walker computation of KSP relative tolerance (version %d)\n",kctx->version);CHKERRQ(ierr);
80d132466eSBarry Smith         ierr = ViewerASCIIPrintf(viewer,"    rtol_0=%g, rtol_max=%g, threshold=%g\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold);CHKERRQ(ierr);
81d132466eSBarry Smith         ierr = ViewerASCIIPrintf(viewer,"    gamma=%g, alpha=%g, alpha2=%g\n",kctx->gamma,kctx->alpha,kctx->alpha2);CHKERRQ(ierr);
829b94acceSBarry Smith       }
839b94acceSBarry Smith     }
840f5bd95cSBarry Smith   } else if (isstring) {
85454a90a3SBarry Smith     ierr = SNESGetType(snes,&type);CHKERRQ(ierr);
86454a90a3SBarry Smith     ierr = ViewerStringSPrintf(viewer," %-3.3s",type);CHKERRQ(ierr);
8719bcc07fSBarry Smith   }
8877ed5343SBarry Smith   ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr);
890ef38995SBarry Smith   ierr = ViewerASCIIPushTab(viewer);CHKERRQ(ierr);
909b94acceSBarry Smith   ierr = SLESView(sles,viewer);CHKERRQ(ierr);
910ef38995SBarry Smith   ierr = ViewerASCIIPopTab(viewer);CHKERRQ(ierr);
923a40ed3dSBarry Smith   PetscFunctionReturn(0);
939b94acceSBarry Smith }
949b94acceSBarry Smith 
9515091d37SBarry Smith #undef __FUNC__
96b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetFromOptions"
979b94acceSBarry Smith /*@
98682d7d0cSBarry Smith    SNESSetFromOptions - Sets various SNES and SLES parameters from user options.
999b94acceSBarry Smith 
100c7afd0dbSLois Curfman McInnes    Collective on SNES
101c7afd0dbSLois Curfman McInnes 
1029b94acceSBarry Smith    Input Parameter:
1039b94acceSBarry Smith .  snes - the SNES context
1049b94acceSBarry Smith 
10536851e7fSLois Curfman McInnes    Options Database Keys:
1066831982aSBarry Smith +  -snes_type <type> - ls, tr, umls, umtr, test
10782738288SBarry Smith .  -snes_stol - convergence tolerance in terms of the norm
10882738288SBarry Smith                 of the change in the solution between steps
109b39c3a46SLois Curfman McInnes .  -snes_atol <atol> - absolute tolerance of residual norm
110b39c3a46SLois Curfman McInnes .  -snes_rtol <rtol> - relative decrease in tolerance norm from initial
111b39c3a46SLois Curfman McInnes .  -snes_max_it <max_it> - maximum number of iterations
112b39c3a46SLois Curfman McInnes .  -snes_max_funcs <max_funcs> - maximum number of function evaluations
113b39c3a46SLois Curfman McInnes .  -snes_trtol <trtol> - trust region tolerance
11482738288SBarry Smith .  -snes_no_convergence_test - skip convergence test in nonlinear or minimization
11582738288SBarry Smith                                solver; hence iterations will continue until max_it
1161fbbfb26SLois Curfman McInnes                                or some other criterion is reached. Saves expense
11782738288SBarry Smith                                of convergence test
11882738288SBarry Smith .  -snes_monitor - prints residual norm at each iteration
119d132466eSBarry Smith .  -snes_vecmonitor - plots solution at each iteration
120d132466eSBarry Smith .  -snes_vecmonitor_update - plots update to solution at each iteration
12182738288SBarry Smith .  -snes_xmonitor - plots residual norm at each iteration
122e24b481bSBarry Smith .  -snes_fd - use finite differences to compute Jacobian; very slow, only for testing
12336851e7fSLois Curfman McInnes -  -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration
12482738288SBarry Smith 
12582738288SBarry Smith     Options Database for Eisenstat-Walker method:
12682738288SBarry Smith +  -snes_ksp_eq_conv - use Eisenstat-Walker method for determining linear system convergence
12782738288SBarry Smith .  -snes_ksp_eq_version ver - version of  Eisenstat-Walker method
12836851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0
12936851e7fSLois Curfman McInnes .  -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax
13036851e7fSLois Curfman McInnes .  -snes_ksp_ew_gamma <gamma> - Sets gamma
13136851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha <alpha> - Sets alpha
13236851e7fSLois Curfman McInnes .  -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2
13336851e7fSLois Curfman McInnes -  -snes_ksp_ew_threshold <threshold> - Sets threshold
13482738288SBarry Smith 
13511ca99fdSLois Curfman McInnes    Notes:
13611ca99fdSLois Curfman McInnes    To see all options, run your program with the -help option or consult
13711ca99fdSLois Curfman McInnes    the users manual.
13883e2fdc7SBarry Smith 
13936851e7fSLois Curfman McInnes    Level: beginner
14036851e7fSLois Curfman McInnes 
1419b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database
1429b94acceSBarry Smith 
143*186905e3SBarry Smith .seealso: SNESSetOptionsPrefix(), SNESSetTypeFromOptions()
1449b94acceSBarry Smith @*/
1459b94acceSBarry Smith int SNESSetFromOptions(SNES snes)
1469b94acceSBarry Smith {
1479b94acceSBarry Smith   SLES                sles;
148*186905e3SBarry Smith   SNES_KSP_EW_ConvCtx *kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
149f1af5d2fSBarry Smith   PetscTruth          flg;
150*186905e3SBarry Smith   int                 ierr;
151*186905e3SBarry Smith   char                *deft,type[256];
1529b94acceSBarry Smith 
1533a40ed3dSBarry Smith   PetscFunctionBegin;
15477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
155ca161407SBarry Smith 
156*186905e3SBarry Smith   ierr = OptionsBegin(snes->comm,snes->prefix,"Nonlinear solver (SNES) options");CHKERRQ(ierr);
157*186905e3SBarry Smith     if (snes->type_name) {
158*186905e3SBarry Smith       deft = snes->type_name;
159*186905e3SBarry Smith     } else {
160*186905e3SBarry Smith       if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
161*186905e3SBarry Smith         deft = SNESEQLS;
162*186905e3SBarry Smith       } else {
163*186905e3SBarry Smith         deft = SNESUMTR;
164d64ed03dSBarry Smith       }
165d64ed03dSBarry Smith     }
166*186905e3SBarry Smith     if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
167*186905e3SBarry Smith     ierr = OptionsList("-snes_type","Nonlinear solver method","SNESSetType",SNESList,deft,type,256,&flg);CHKERRQ(ierr);
168d64ed03dSBarry Smith     if (flg) {
169*186905e3SBarry Smith       ierr = SNESSetType(snes,type);CHKERRQ(ierr);
170*186905e3SBarry Smith     } else if (!snes->type_name) {
171*186905e3SBarry Smith       ierr = SNESSetType(snes,deft);CHKERRQ(ierr);
172d64ed03dSBarry Smith     }
17393c39befSBarry Smith 
174*186905e3SBarry Smith     ierr = OptionsDouble("-snes_stol","Stop if step length less then","SNESSetTolerances",snes->xtol,&snes->xtol,0);CHKERRQ(ierr);
175*186905e3SBarry Smith     ierr = OptionsDouble("-snes_atol","Stop if function norm less then","SNESSetTolerances",snes->atol,&snes->atol,0);CHKERRQ(ierr);
176*186905e3SBarry Smith 
177*186905e3SBarry Smith     ierr = OptionsDouble("-snes_rtol","Stop if decrease in function norm less then","SNESSetTolerances",snes->rtol,&snes->rtol,0);CHKERRQ(ierr);
178*186905e3SBarry Smith     ierr = OptionsInt("-snes_max_it","Maximum iterations","SNESSetTolerances",snes->max_its,&snes->max_its,PETSC_NULL);CHKERRQ(ierr);
179*186905e3SBarry Smith     ierr = OptionsInt("-snes_max_funcs","Maximum function evaluations","SNESSetTolerances",snes->max_funcs,&snes->max_funcs,PETSC_NULL);CHKERRQ(ierr);
180*186905e3SBarry Smith     ierr = OptionsDouble("-snes_fmin","Minimization function tolerance","SNESSetMinimizationFunctionTolerance",snes->fmin,&snes->fmin,0);CHKERRQ(ierr);
181*186905e3SBarry Smith 
182*186905e3SBarry Smith     ierr = OptionsName("-snes_ksp_ew_conv","Use Eisentat-Walker linear system convergence test","SNES_KSP_SetParametersEW",&snes->ksp_ewconv);CHKERRQ(ierr);
183*186905e3SBarry Smith 
184*186905e3SBarry Smith     ierr = OptionsInt("-snes_ksp_ew_version","Version 1 or 2","SNES_KSP_SetParametersEW",kctx->version,&kctx->version,0);CHKERRQ(ierr);
185*186905e3SBarry Smith     ierr = OptionsDouble("-snes_ksp_ew_rtol0","0 <= rtol0 < 1","SNES_KSP_SetParametersEW",kctx->rtol_0,&kctx->rtol_0,0);CHKERRQ(ierr);
186*186905e3SBarry Smith     ierr = OptionsDouble("-snes_ksp_ew_rtolmax","0 <= rtolmax < 1","SNES_KSP_SetParametersEW",kctx->rtol_max,&kctx->rtol_max,0);CHKERRQ(ierr);
187*186905e3SBarry Smith     ierr = OptionsDouble("-snes_ksp_ew_gamma","0 <= gamma <= 1","SNES_KSP_SetParametersEW",kctx->gamma,&kctx->gamma,0);CHKERRQ(ierr);
188*186905e3SBarry Smith     ierr = OptionsDouble("-snes_ksp_ew_alpha","1 < alpha <= 2","SNES_KSP_SetParametersEW",kctx->alpha,&kctx->alpha,0);CHKERRQ(ierr);
189*186905e3SBarry Smith     ierr = OptionsDouble("-snes_ksp_ew_alpha2","alpha2","SNES_KSP_SetParametersEW",kctx->alpha2,&kctx->alpha2,0);CHKERRQ(ierr);
190*186905e3SBarry Smith     ierr = OptionsDouble("-snes_ksp_ew_threshold","0 < threshold < 1","SNES_KSP_SetParametersEW",kctx->threshold,&kctx->threshold,0);CHKERRQ(ierr);
191*186905e3SBarry Smith 
192*186905e3SBarry Smith     ierr = OptionsName("-snes_no_convergence_test","Don't test for convergence","None",&flg);CHKERRQ(ierr);
19393c39befSBarry Smith     if (flg) {snes->converged = 0;}
194*186905e3SBarry Smith     ierr = OptionsName("-snes_cancelmonitors","Remove all monitors","SNESClearMonitor",&flg);CHKERRQ(ierr);
1955cd90555SBarry Smith     if (flg) {ierr = SNESClearMonitor(snes);CHKERRQ(ierr);}
196*186905e3SBarry Smith     ierr = OptionsName("-snes_monitor","Monitor norm of function","SNESDefaultMonitor",&flg);CHKERRQ(ierr);
197b8a78c4aSBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultMonitor,0,0);CHKERRQ(ierr);}
198*186905e3SBarry Smith     ierr = OptionsName("-snes_smonitor","Monitor norm of function (fewer digits)","SNESDefaultSMonitor",&flg);CHKERRQ(ierr);
199b8a78c4aSBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultSMonitor,0,0);CHKERRQ(ierr);}
200*186905e3SBarry Smith     ierr = OptionsName("-snes_vecmonitor","Plot solution at each iteration","SNESVecViewMonitor",&flg);CHKERRQ(ierr);
201b8a78c4aSBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewMonitor,0,0);CHKERRQ(ierr);}
202*186905e3SBarry Smith     ierr = OptionsName("-snes_vecmonitor_update","Plot correction at each iteration","SNESVecViewUpdateMonitor",&flg);CHKERRQ(ierr);
2037c922b88SBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewUpdateMonitor,0,0);CHKERRQ(ierr);}
204*186905e3SBarry Smith     ierr = OptionsName("-snes_xmonitor","Plot function norm at each iteration","SNESLGMonitor",&flg);CHKERRQ(ierr);
205*186905e3SBarry Smith     if (flg) {ierr = SNESSetMonitor(snes,SNESLGMonitor,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
206e24b481bSBarry Smith 
207*186905e3SBarry Smith     ierr = OptionsName("-snes_fd","Use finite differences (slow) to compute Jacobian","SNESDefaultComputeJacobian",&flg);CHKERRQ(ierr);
2083c7409f5SSatish Balay     if (flg && snes->method_class == SNES_NONLINEAR_EQUATIONS) {
209*186905e3SBarry Smith       ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeJacobian,snes->funP);CHKERRQ(ierr);
210981c4779SBarry Smith       PLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Jacobian matrix\n");
211981c4779SBarry Smith     } else if (flg && snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
212*186905e3SBarry Smith       ierr = SNESSetHessian(snes,snes->jacobian,snes->jacobian_pre,SNESDefaultComputeHessian,snes->funP);CHKERRQ(ierr);
213d64ed03dSBarry Smith       PLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Hessian matrix\n");
2149b94acceSBarry Smith     }
215639f9d9dSBarry Smith 
216*186905e3SBarry Smith   ierr = OptionsEnd();CHKERRQ(ierr);
217*186905e3SBarry Smith 
218*186905e3SBarry Smith   if (snes->setfromoptions) {
219*186905e3SBarry Smith     ierr = (*snes->setfromoptions)(snes);CHKERRQ(ierr);
220639f9d9dSBarry Smith   }
221639f9d9dSBarry Smith 
2229b94acceSBarry Smith   ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr);
2239b94acceSBarry Smith   ierr = SLESSetFromOptions(sles);CHKERRQ(ierr);
22493993e2dSLois Curfman McInnes 
2253a40ed3dSBarry Smith   PetscFunctionReturn(0);
2269b94acceSBarry Smith }
2279b94acceSBarry Smith 
228a847f771SSatish Balay 
2295615d1e5SSatish Balay #undef __FUNC__
230b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetApplicationContext"
2319b94acceSBarry Smith /*@
2329b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
2339b94acceSBarry Smith    the nonlinear solvers.
2349b94acceSBarry Smith 
235fee21e36SBarry Smith    Collective on SNES
236fee21e36SBarry Smith 
237c7afd0dbSLois Curfman McInnes    Input Parameters:
238c7afd0dbSLois Curfman McInnes +  snes - the SNES context
239c7afd0dbSLois Curfman McInnes -  usrP - optional user context
240c7afd0dbSLois Curfman McInnes 
24136851e7fSLois Curfman McInnes    Level: intermediate
24236851e7fSLois Curfman McInnes 
2439b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
2449b94acceSBarry Smith 
2459b94acceSBarry Smith .seealso: SNESGetApplicationContext()
2469b94acceSBarry Smith @*/
2479b94acceSBarry Smith int SNESSetApplicationContext(SNES snes,void *usrP)
2489b94acceSBarry Smith {
2493a40ed3dSBarry Smith   PetscFunctionBegin;
25077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2519b94acceSBarry Smith   snes->user		= usrP;
2523a40ed3dSBarry Smith   PetscFunctionReturn(0);
2539b94acceSBarry Smith }
25474679c65SBarry Smith 
2555615d1e5SSatish Balay #undef __FUNC__
256b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetApplicationContext"
2579b94acceSBarry Smith /*@C
2589b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
2599b94acceSBarry Smith    nonlinear solvers.
2609b94acceSBarry Smith 
261c7afd0dbSLois Curfman McInnes    Not Collective
262c7afd0dbSLois Curfman McInnes 
2639b94acceSBarry Smith    Input Parameter:
2649b94acceSBarry Smith .  snes - SNES context
2659b94acceSBarry Smith 
2669b94acceSBarry Smith    Output Parameter:
2679b94acceSBarry Smith .  usrP - user context
2689b94acceSBarry Smith 
26936851e7fSLois Curfman McInnes    Level: intermediate
27036851e7fSLois Curfman McInnes 
2719b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
2729b94acceSBarry Smith 
2739b94acceSBarry Smith .seealso: SNESSetApplicationContext()
2749b94acceSBarry Smith @*/
2759b94acceSBarry Smith int SNESGetApplicationContext(SNES snes,void **usrP)
2769b94acceSBarry Smith {
2773a40ed3dSBarry Smith   PetscFunctionBegin;
27877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2799b94acceSBarry Smith   *usrP = snes->user;
2803a40ed3dSBarry Smith   PetscFunctionReturn(0);
2819b94acceSBarry Smith }
28274679c65SBarry Smith 
2835615d1e5SSatish Balay #undef __FUNC__
284b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetIterationNumber"
2859b94acceSBarry Smith /*@
286c8228a4eSBarry Smith    SNESGetIterationNumber - Gets the number of nonlinear iterations completed
287c8228a4eSBarry Smith    at this time.
2889b94acceSBarry Smith 
289c7afd0dbSLois Curfman McInnes    Not Collective
290c7afd0dbSLois Curfman McInnes 
2919b94acceSBarry Smith    Input Parameter:
2929b94acceSBarry Smith .  snes - SNES context
2939b94acceSBarry Smith 
2949b94acceSBarry Smith    Output Parameter:
2959b94acceSBarry Smith .  iter - iteration number
2969b94acceSBarry Smith 
297c8228a4eSBarry Smith    Notes:
298c8228a4eSBarry Smith    For example, during the computation of iteration 2 this would return 1.
299c8228a4eSBarry Smith 
300c8228a4eSBarry Smith    This is useful for using lagged Jacobians (where one does not recompute the
30108405cd6SLois Curfman McInnes    Jacobian at each SNES iteration). For example, the code
30208405cd6SLois Curfman McInnes .vb
30308405cd6SLois Curfman McInnes       ierr = SNESGetIterationNumber(snes,&it);
30408405cd6SLois Curfman McInnes       if (!(it % 2)) {
30508405cd6SLois Curfman McInnes         [compute Jacobian here]
30608405cd6SLois Curfman McInnes       }
30708405cd6SLois Curfman McInnes .ve
308c8228a4eSBarry Smith    can be used in your ComputeJacobian() function to cause the Jacobian to be
30908405cd6SLois Curfman McInnes    recomputed every second SNES iteration.
310c8228a4eSBarry Smith 
31136851e7fSLois Curfman McInnes    Level: intermediate
31236851e7fSLois Curfman McInnes 
3139b94acceSBarry Smith .keywords: SNES, nonlinear, get, iteration, number
3149b94acceSBarry Smith @*/
3159b94acceSBarry Smith int SNESGetIterationNumber(SNES snes,int* iter)
3169b94acceSBarry Smith {
3173a40ed3dSBarry Smith   PetscFunctionBegin;
31877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
31974679c65SBarry Smith   PetscValidIntPointer(iter);
3209b94acceSBarry Smith   *iter = snes->iter;
3213a40ed3dSBarry Smith   PetscFunctionReturn(0);
3229b94acceSBarry Smith }
32374679c65SBarry Smith 
3245615d1e5SSatish Balay #undef __FUNC__
325b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetFunctionNorm"
3269b94acceSBarry Smith /*@
3279b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
3289b94acceSBarry Smith    with SNESSSetFunction().
3299b94acceSBarry Smith 
330c7afd0dbSLois Curfman McInnes    Collective on SNES
331c7afd0dbSLois Curfman McInnes 
3329b94acceSBarry Smith    Input Parameter:
3339b94acceSBarry Smith .  snes - SNES context
3349b94acceSBarry Smith 
3359b94acceSBarry Smith    Output Parameter:
3369b94acceSBarry Smith .  fnorm - 2-norm of function
3379b94acceSBarry Smith 
3389b94acceSBarry Smith    Note:
3399b94acceSBarry Smith    SNESGetFunctionNorm() is valid for SNES_NONLINEAR_EQUATIONS methods only.
340a86d99e1SLois Curfman McInnes    A related routine for SNES_UNCONSTRAINED_MINIMIZATION methods is
341a86d99e1SLois Curfman McInnes    SNESGetGradientNorm().
3429b94acceSBarry Smith 
34336851e7fSLois Curfman McInnes    Level: intermediate
34436851e7fSLois Curfman McInnes 
3459b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
346a86d99e1SLois Curfman McInnes 
34708405cd6SLois Curfman McInnes .seealso: SNESGetFunction()
3489b94acceSBarry Smith @*/
3499b94acceSBarry Smith int SNESGetFunctionNorm(SNES snes,Scalar *fnorm)
3509b94acceSBarry Smith {
3513a40ed3dSBarry Smith   PetscFunctionBegin;
35277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
35374679c65SBarry Smith   PetscValidScalarPointer(fnorm);
35474679c65SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
355d252947aSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"For SNES_NONLINEAR_EQUATIONS only");
35674679c65SBarry Smith   }
3579b94acceSBarry Smith   *fnorm = snes->norm;
3583a40ed3dSBarry Smith   PetscFunctionReturn(0);
3599b94acceSBarry Smith }
36074679c65SBarry Smith 
3615615d1e5SSatish Balay #undef __FUNC__
362b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetGradientNorm"
3639b94acceSBarry Smith /*@
3649b94acceSBarry Smith    SNESGetGradientNorm - Gets the norm of the current gradient that was set
3659b94acceSBarry Smith    with SNESSSetGradient().
3669b94acceSBarry Smith 
367c7afd0dbSLois Curfman McInnes    Collective on SNES
368c7afd0dbSLois Curfman McInnes 
3699b94acceSBarry Smith    Input Parameter:
3709b94acceSBarry Smith .  snes - SNES context
3719b94acceSBarry Smith 
3729b94acceSBarry Smith    Output Parameter:
3739b94acceSBarry Smith .  fnorm - 2-norm of gradient
3749b94acceSBarry Smith 
3759b94acceSBarry Smith    Note:
3769b94acceSBarry Smith    SNESGetGradientNorm() is valid for SNES_UNCONSTRAINED_MINIMIZATION
377a86d99e1SLois Curfman McInnes    methods only.  A related routine for SNES_NONLINEAR_EQUATIONS methods
378a86d99e1SLois Curfman McInnes    is SNESGetFunctionNorm().
3799b94acceSBarry Smith 
38036851e7fSLois Curfman McInnes    Level: intermediate
38136851e7fSLois Curfman McInnes 
3829b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient, norm
383a86d99e1SLois Curfman McInnes 
384a86d99e1SLois Curfman McInnes .seelso: SNESSetGradient()
3859b94acceSBarry Smith @*/
3869b94acceSBarry Smith int SNESGetGradientNorm(SNES snes,Scalar *gnorm)
3879b94acceSBarry Smith {
3883a40ed3dSBarry Smith   PetscFunctionBegin;
38977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
39074679c65SBarry Smith   PetscValidScalarPointer(gnorm);
39174679c65SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
392d252947aSBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
39374679c65SBarry Smith   }
3949b94acceSBarry Smith   *gnorm = snes->norm;
3953a40ed3dSBarry Smith   PetscFunctionReturn(0);
3969b94acceSBarry Smith }
39774679c65SBarry Smith 
3985615d1e5SSatish Balay #undef __FUNC__
399b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetNumberUnsuccessfulSteps"
4009b94acceSBarry Smith /*@
4019b94acceSBarry Smith    SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps
4029b94acceSBarry Smith    attempted by the nonlinear solver.
4039b94acceSBarry Smith 
404c7afd0dbSLois Curfman McInnes    Not Collective
405c7afd0dbSLois Curfman McInnes 
4069b94acceSBarry Smith    Input Parameter:
4079b94acceSBarry Smith .  snes - SNES context
4089b94acceSBarry Smith 
4099b94acceSBarry Smith    Output Parameter:
4109b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
4119b94acceSBarry Smith 
412c96a6f78SLois Curfman McInnes    Notes:
413c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
414c96a6f78SLois Curfman McInnes 
41536851e7fSLois Curfman McInnes    Level: intermediate
41636851e7fSLois Curfman McInnes 
4179b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
4189b94acceSBarry Smith @*/
4199b94acceSBarry Smith int SNESGetNumberUnsuccessfulSteps(SNES snes,int* nfails)
4209b94acceSBarry Smith {
4213a40ed3dSBarry Smith   PetscFunctionBegin;
42277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
42374679c65SBarry Smith   PetscValidIntPointer(nfails);
4249b94acceSBarry Smith   *nfails = snes->nfailures;
4253a40ed3dSBarry Smith   PetscFunctionReturn(0);
4269b94acceSBarry Smith }
427a847f771SSatish Balay 
4285615d1e5SSatish Balay #undef __FUNC__
429b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetNumberLinearIterations"
430c96a6f78SLois Curfman McInnes /*@
431c96a6f78SLois Curfman McInnes    SNESGetNumberLinearIterations - Gets the total number of linear iterations
432c96a6f78SLois Curfman McInnes    used by the nonlinear solver.
433c96a6f78SLois Curfman McInnes 
434c7afd0dbSLois Curfman McInnes    Not Collective
435c7afd0dbSLois Curfman McInnes 
436c96a6f78SLois Curfman McInnes    Input Parameter:
437c96a6f78SLois Curfman McInnes .  snes - SNES context
438c96a6f78SLois Curfman McInnes 
439c96a6f78SLois Curfman McInnes    Output Parameter:
440c96a6f78SLois Curfman McInnes .  lits - number of linear iterations
441c96a6f78SLois Curfman McInnes 
442c96a6f78SLois Curfman McInnes    Notes:
443c96a6f78SLois Curfman McInnes    This counter is reset to zero for each successive call to SNESSolve().
444c96a6f78SLois Curfman McInnes 
44536851e7fSLois Curfman McInnes    Level: intermediate
44636851e7fSLois Curfman McInnes 
447c96a6f78SLois Curfman McInnes .keywords: SNES, nonlinear, get, number, linear, iterations
448c96a6f78SLois Curfman McInnes @*/
44986bddb7dSBarry Smith int SNESGetNumberLinearIterations(SNES snes,int* lits)
450c96a6f78SLois Curfman McInnes {
4513a40ed3dSBarry Smith   PetscFunctionBegin;
452c96a6f78SLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
453c96a6f78SLois Curfman McInnes   PetscValidIntPointer(lits);
454c96a6f78SLois Curfman McInnes   *lits = snes->linear_its;
4553a40ed3dSBarry Smith   PetscFunctionReturn(0);
456c96a6f78SLois Curfman McInnes }
457c96a6f78SLois Curfman McInnes 
458c96a6f78SLois Curfman McInnes #undef __FUNC__
459b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetSLES"
4609b94acceSBarry Smith /*@C
4619b94acceSBarry Smith    SNESGetSLES - Returns the SLES context for a SNES solver.
4629b94acceSBarry Smith 
463c7afd0dbSLois Curfman McInnes    Not Collective, but if SNES object is parallel, then SLES object is parallel
464c7afd0dbSLois Curfman McInnes 
4659b94acceSBarry Smith    Input Parameter:
4669b94acceSBarry Smith .  snes - the SNES context
4679b94acceSBarry Smith 
4689b94acceSBarry Smith    Output Parameter:
4699b94acceSBarry Smith .  sles - the SLES context
4709b94acceSBarry Smith 
4719b94acceSBarry Smith    Notes:
4729b94acceSBarry Smith    The user can then directly manipulate the SLES context to set various
4739b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
4749b94acceSBarry Smith    KSP and PC contexts as well.
4759b94acceSBarry Smith 
47636851e7fSLois Curfman McInnes    Level: beginner
47736851e7fSLois Curfman McInnes 
4789b94acceSBarry Smith .keywords: SNES, nonlinear, get, SLES, context
4799b94acceSBarry Smith 
4809b94acceSBarry Smith .seealso: SLESGetPC(), SLESGetKSP()
4819b94acceSBarry Smith @*/
4829b94acceSBarry Smith int SNESGetSLES(SNES snes,SLES *sles)
4839b94acceSBarry Smith {
4843a40ed3dSBarry Smith   PetscFunctionBegin;
48577c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
4869b94acceSBarry Smith   *sles = snes->sles;
4873a40ed3dSBarry Smith   PetscFunctionReturn(0);
4889b94acceSBarry Smith }
48982bf6240SBarry Smith 
490e24b481bSBarry Smith #undef __FUNC__
491b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESPublish_Petsc"
492454a90a3SBarry Smith static int SNESPublish_Petsc(PetscObject obj)
493e24b481bSBarry Smith {
494aa482453SBarry Smith #if defined(PETSC_HAVE_AMS)
495454a90a3SBarry Smith   SNES          v = (SNES) obj;
496e24b481bSBarry Smith   int          ierr;
49743d6d2cbSBarry Smith #endif
498e24b481bSBarry Smith 
499e24b481bSBarry Smith   PetscFunctionBegin;
500e24b481bSBarry Smith 
50143d6d2cbSBarry Smith #if defined(PETSC_HAVE_AMS)
502e24b481bSBarry Smith   /* if it is already published then return */
503e24b481bSBarry Smith   if (v->amem >=0) PetscFunctionReturn(0);
504e24b481bSBarry Smith 
505454a90a3SBarry Smith   ierr = PetscObjectPublishBaseBegin(obj);CHKERRQ(ierr);
506e24b481bSBarry Smith   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Iteration",&v->iter,1,AMS_INT,AMS_READ,
507e24b481bSBarry Smith                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
508e24b481bSBarry Smith   ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Residual",&v->norm,1,AMS_DOUBLE,AMS_READ,
509e24b481bSBarry Smith                                 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr);
510454a90a3SBarry Smith   ierr = PetscObjectPublishBaseEnd(obj);CHKERRQ(ierr);
511433994e6SBarry Smith #endif
512e24b481bSBarry Smith   PetscFunctionReturn(0);
513e24b481bSBarry Smith }
514e24b481bSBarry Smith 
5159b94acceSBarry Smith /* -----------------------------------------------------------*/
5165615d1e5SSatish Balay #undef __FUNC__
517b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESCreate"
5189b94acceSBarry Smith /*@C
5199b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
5209b94acceSBarry Smith 
521c7afd0dbSLois Curfman McInnes    Collective on MPI_Comm
522c7afd0dbSLois Curfman McInnes 
523c7afd0dbSLois Curfman McInnes    Input Parameters:
524c7afd0dbSLois Curfman McInnes +  comm - MPI communicator
525c7afd0dbSLois Curfman McInnes -  type - type of method, either
526c7afd0dbSLois Curfman McInnes    SNES_NONLINEAR_EQUATIONS (for systems of nonlinear equations)
527c7afd0dbSLois Curfman McInnes    or SNES_UNCONSTRAINED_MINIMIZATION (for unconstrained minimization)
5289b94acceSBarry Smith 
5299b94acceSBarry Smith    Output Parameter:
5309b94acceSBarry Smith .  outsnes - the new SNES context
5319b94acceSBarry Smith 
532c7afd0dbSLois Curfman McInnes    Options Database Keys:
533c7afd0dbSLois Curfman McInnes +   -snes_mf - Activates default matrix-free Jacobian-vector products,
534c7afd0dbSLois Curfman McInnes                and no preconditioning matrix
535c7afd0dbSLois Curfman McInnes .   -snes_mf_operator - Activates default matrix-free Jacobian-vector
536c7afd0dbSLois Curfman McInnes                products, and a user-provided preconditioning matrix
537c7afd0dbSLois Curfman McInnes                as set by SNESSetJacobian()
538c7afd0dbSLois Curfman McInnes -   -snes_fd - Uses (slow!) finite differences to compute Jacobian
539c1f60f51SBarry Smith 
54036851e7fSLois Curfman McInnes    Level: beginner
54136851e7fSLois Curfman McInnes 
5429b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
5439b94acceSBarry Smith 
54463a78c88SLois Curfman McInnes .seealso: SNESSolve(), SNESDestroy()
5459b94acceSBarry Smith @*/
5464b0e389bSBarry Smith int SNESCreate(MPI_Comm comm,SNESProblemType type,SNES *outsnes)
5479b94acceSBarry Smith {
5489b94acceSBarry Smith   int                 ierr;
5499b94acceSBarry Smith   SNES                snes;
5509b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
55137fcc0dbSBarry Smith 
5523a40ed3dSBarry Smith   PetscFunctionBegin;
5539b94acceSBarry Smith   *outsnes = 0;
554d64ed03dSBarry Smith   if (type != SNES_UNCONSTRAINED_MINIMIZATION && type != SNES_NONLINEAR_EQUATIONS){
555d252947aSBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"incorrect method type");
556d64ed03dSBarry Smith   }
5573f1db9ecSBarry Smith   PetscHeaderCreate(snes,_p_SNES,int,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView);
5589b94acceSBarry Smith   PLogObjectCreate(snes);
559e24b481bSBarry Smith   snes->bops->publish     = SNESPublish_Petsc;
5609b94acceSBarry Smith   snes->max_its           = 50;
5619750a799SBarry Smith   snes->max_funcs	  = 10000;
5629b94acceSBarry Smith   snes->norm		  = 0.0;
5635a2d0531SBarry Smith   if (type == SNES_UNCONSTRAINED_MINIMIZATION) {
564b18e04deSLois Curfman McInnes     snes->rtol		  = 1.e-8;
565b18e04deSLois Curfman McInnes     snes->ttol            = 0.0;
5669b94acceSBarry Smith     snes->atol		  = 1.e-10;
567d64ed03dSBarry Smith   } else {
568b4874afaSBarry Smith     snes->rtol		  = 1.e-8;
569b4874afaSBarry Smith     snes->ttol            = 0.0;
570b4874afaSBarry Smith     snes->atol		  = 1.e-50;
571b4874afaSBarry Smith   }
5729b94acceSBarry Smith   snes->xtol		  = 1.e-8;
573b18e04deSLois Curfman McInnes   snes->trunctol	  = 1.e-12; /* no longer used */
5749b94acceSBarry Smith   snes->nfuncs            = 0;
5759b94acceSBarry Smith   snes->nfailures         = 0;
5767a00f4a9SLois Curfman McInnes   snes->linear_its        = 0;
577639f9d9dSBarry Smith   snes->numbermonitors    = 0;
5789b94acceSBarry Smith   snes->data              = 0;
5799b94acceSBarry Smith   snes->view              = 0;
5809b94acceSBarry Smith   snes->computeumfunction = 0;
5819b94acceSBarry Smith   snes->umfunP            = 0;
5829b94acceSBarry Smith   snes->fc                = 0;
5839b94acceSBarry Smith   snes->deltatol          = 1.e-12;
5849b94acceSBarry Smith   snes->fmin              = -1.e30;
5859b94acceSBarry Smith   snes->method_class      = type;
5869b94acceSBarry Smith   snes->set_method_called = 0;
58782bf6240SBarry Smith   snes->setupcalled       = 0;
588*186905e3SBarry Smith   snes->ksp_ewconv        = PETSC_FALSE;
5896f24a144SLois Curfman McInnes   snes->vwork             = 0;
5906f24a144SLois Curfman McInnes   snes->nwork             = 0;
591758f92a0SBarry Smith   snes->conv_hist_len     = 0;
592758f92a0SBarry Smith   snes->conv_hist_max     = 0;
593758f92a0SBarry Smith   snes->conv_hist         = PETSC_NULL;
594758f92a0SBarry Smith   snes->conv_hist_its     = PETSC_NULL;
595758f92a0SBarry Smith   snes->conv_hist_reset   = PETSC_TRUE;
596184914b5SBarry Smith   snes->reason            = SNES_CONVERGED_ITERATING;
5979b94acceSBarry Smith 
5989b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
5990452661fSBarry Smith   kctx = PetscNew(SNES_KSP_EW_ConvCtx);CHKPTRQ(kctx);
600eed86810SBarry Smith   PLogObjectMemory(snes,sizeof(SNES_KSP_EW_ConvCtx));
6019b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
6029b94acceSBarry Smith   kctx->version     = 2;
6039b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
6049b94acceSBarry Smith                              this was too large for some test cases */
6059b94acceSBarry Smith   kctx->rtol_last   = 0;
6069b94acceSBarry Smith   kctx->rtol_max    = .9;
6079b94acceSBarry Smith   kctx->gamma       = 1.0;
6089b94acceSBarry Smith   kctx->alpha2      = .5*(1.0 + sqrt(5.0));
6099b94acceSBarry Smith   kctx->alpha       = kctx->alpha2;
6109b94acceSBarry Smith   kctx->threshold   = .1;
6119b94acceSBarry Smith   kctx->lresid_last = 0;
6129b94acceSBarry Smith   kctx->norm_last   = 0;
6139b94acceSBarry Smith 
6149b94acceSBarry Smith   ierr = SLESCreate(comm,&snes->sles);CHKERRQ(ierr);
6159b94acceSBarry Smith   PLogObjectParent(snes,snes->sles)
6165334005bSBarry Smith 
6179b94acceSBarry Smith   *outsnes = snes;
61800036973SBarry Smith   ierr = PetscPublishAll(snes);CHKERRQ(ierr);
6193a40ed3dSBarry Smith   PetscFunctionReturn(0);
6209b94acceSBarry Smith }
6219b94acceSBarry Smith 
6229b94acceSBarry Smith /* --------------------------------------------------------------- */
6235615d1e5SSatish Balay #undef __FUNC__
624b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetFunction"
6259b94acceSBarry Smith /*@C
6269b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
6279b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
6289b94acceSBarry Smith    equations.
6299b94acceSBarry Smith 
630fee21e36SBarry Smith    Collective on SNES
631fee21e36SBarry Smith 
632c7afd0dbSLois Curfman McInnes    Input Parameters:
633c7afd0dbSLois Curfman McInnes +  snes - the SNES context
634c7afd0dbSLois Curfman McInnes .  func - function evaluation routine
635c7afd0dbSLois Curfman McInnes .  r - vector to store function value
636c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
637c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
6389b94acceSBarry Smith 
639c7afd0dbSLois Curfman McInnes    Calling sequence of func:
6408d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Vec f,void *ctx);
641c7afd0dbSLois Curfman McInnes 
642313e4042SLois Curfman McInnes .  f - function vector
643c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined function context
6449b94acceSBarry Smith 
6459b94acceSBarry Smith    Notes:
6469b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
6479b94acceSBarry Smith $      f'(x) x = -f(x),
648c7afd0dbSLois Curfman McInnes    where f'(x) denotes the Jacobian matrix and f(x) is the function.
6499b94acceSBarry Smith 
6509b94acceSBarry Smith    SNESSetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only.
6519b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
6529b94acceSBarry Smith    SNESSetMinimizationFunction() and SNESSetGradient();
6539b94acceSBarry Smith 
65436851e7fSLois Curfman McInnes    Level: beginner
65536851e7fSLois Curfman McInnes 
6569b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
6579b94acceSBarry Smith 
658a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
6599b94acceSBarry Smith @*/
6605334005bSBarry Smith int SNESSetFunction(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),void *ctx)
6619b94acceSBarry Smith {
6623a40ed3dSBarry Smith   PetscFunctionBegin;
66377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
664184914b5SBarry Smith   PetscValidHeaderSpecific(r,VEC_COOKIE);
665184914b5SBarry Smith   PetscCheckSameComm(snes,r);
666a8c6a408SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
667a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
668a8c6a408SBarry Smith   }
669184914b5SBarry Smith 
6709b94acceSBarry Smith   snes->computefunction     = func;
6719b94acceSBarry Smith   snes->vec_func            = snes->vec_func_always = r;
6729b94acceSBarry Smith   snes->funP                = ctx;
6733a40ed3dSBarry Smith   PetscFunctionReturn(0);
6749b94acceSBarry Smith }
6759b94acceSBarry Smith 
6765615d1e5SSatish Balay #undef __FUNC__
677b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESComputeFunction"
6789b94acceSBarry Smith /*@
67936851e7fSLois Curfman McInnes    SNESComputeFunction - Calls the function that has been set with
6809b94acceSBarry Smith                          SNESSetFunction().
6819b94acceSBarry Smith 
682c7afd0dbSLois Curfman McInnes    Collective on SNES
683c7afd0dbSLois Curfman McInnes 
6849b94acceSBarry Smith    Input Parameters:
685c7afd0dbSLois Curfman McInnes +  snes - the SNES context
686c7afd0dbSLois Curfman McInnes -  x - input vector
6879b94acceSBarry Smith 
6889b94acceSBarry Smith    Output Parameter:
6893638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
6909b94acceSBarry Smith 
6911bffabb2SLois Curfman McInnes    Notes:
6929b94acceSBarry Smith    SNESComputeFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only.
6939b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
6949b94acceSBarry Smith    SNESComputeMinimizationFunction() and SNESComputeGradient();
6959b94acceSBarry Smith 
69636851e7fSLois Curfman McInnes    SNESComputeFunction() is typically used within nonlinear solvers
69736851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
69836851e7fSLois Curfman McInnes    themselves.
69936851e7fSLois Curfman McInnes 
70036851e7fSLois Curfman McInnes    Level: developer
70136851e7fSLois Curfman McInnes 
7029b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
7039b94acceSBarry Smith 
704a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
7059b94acceSBarry Smith @*/
7069b94acceSBarry Smith int SNESComputeFunction(SNES snes,Vec x,Vec y)
7079b94acceSBarry Smith {
7089b94acceSBarry Smith   int    ierr;
7099b94acceSBarry Smith 
7103a40ed3dSBarry Smith   PetscFunctionBegin;
711184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
712184914b5SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
713184914b5SBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE);
714184914b5SBarry Smith   PetscCheckSameComm(snes,x);
715184914b5SBarry Smith   PetscCheckSameComm(snes,y);
716d4bb536fSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
717a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
718d4bb536fSBarry Smith   }
719184914b5SBarry Smith 
7209b94acceSBarry Smith   PLogEventBegin(SNES_FunctionEval,snes,x,y,0);
721d64ed03dSBarry Smith   PetscStackPush("SNES user function");
7229b94acceSBarry Smith   ierr = (*snes->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr);
723d64ed03dSBarry Smith   PetscStackPop;
724ae3c334cSLois Curfman McInnes   snes->nfuncs++;
7259b94acceSBarry Smith   PLogEventEnd(SNES_FunctionEval,snes,x,y,0);
7263a40ed3dSBarry Smith   PetscFunctionReturn(0);
7279b94acceSBarry Smith }
7289b94acceSBarry Smith 
7295615d1e5SSatish Balay #undef __FUNC__
730b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetMinimizationFunction"
7319b94acceSBarry Smith /*@C
7329b94acceSBarry Smith    SNESSetMinimizationFunction - Sets the function evaluation routine for
7339b94acceSBarry Smith    unconstrained minimization.
7349b94acceSBarry Smith 
735fee21e36SBarry Smith    Collective on SNES
736fee21e36SBarry Smith 
737c7afd0dbSLois Curfman McInnes    Input Parameters:
738c7afd0dbSLois Curfman McInnes +  snes - the SNES context
739c7afd0dbSLois Curfman McInnes .  func - function evaluation routine
740c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
741c7afd0dbSLois Curfman McInnes          function evaluation routine (may be PETSC_NULL)
7429b94acceSBarry Smith 
743c7afd0dbSLois Curfman McInnes    Calling sequence of func:
744329f5518SBarry Smith $     func (SNES snes,Vec x,PetscReal *f,void *ctx);
745c7afd0dbSLois Curfman McInnes 
746c7afd0dbSLois Curfman McInnes +  x - input vector
7479b94acceSBarry Smith .  f - function
748c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined function context
7499b94acceSBarry Smith 
75036851e7fSLois Curfman McInnes    Level: beginner
75136851e7fSLois Curfman McInnes 
7529b94acceSBarry Smith    Notes:
7539b94acceSBarry Smith    SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
7549b94acceSBarry Smith    methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
7559b94acceSBarry Smith    SNESSetFunction().
7569b94acceSBarry Smith 
7579b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimization, function
7589b94acceSBarry Smith 
759a86d99e1SLois Curfman McInnes .seealso:  SNESGetMinimizationFunction(), SNESComputeMinimizationFunction(),
760a86d99e1SLois Curfman McInnes            SNESSetHessian(), SNESSetGradient()
7619b94acceSBarry Smith @*/
762329f5518SBarry Smith int SNESSetMinimizationFunction(SNES snes,int (*func)(SNES,Vec,PetscReal*,void*),void *ctx)
7639b94acceSBarry Smith {
7643a40ed3dSBarry Smith   PetscFunctionBegin;
76577c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
766a8c6a408SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
767a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"Only for SNES_UNCONSTRAINED_MINIMIZATION");
768a8c6a408SBarry Smith   }
7699b94acceSBarry Smith   snes->computeumfunction   = func;
7709b94acceSBarry Smith   snes->umfunP              = ctx;
7713a40ed3dSBarry Smith   PetscFunctionReturn(0);
7729b94acceSBarry Smith }
7739b94acceSBarry Smith 
7745615d1e5SSatish Balay #undef __FUNC__
775b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESComputeMinimizationFunction"
7769b94acceSBarry Smith /*@
7779b94acceSBarry Smith    SNESComputeMinimizationFunction - Computes the function that has been
7789b94acceSBarry Smith    set with SNESSetMinimizationFunction().
7799b94acceSBarry Smith 
780c7afd0dbSLois Curfman McInnes    Collective on SNES
781c7afd0dbSLois Curfman McInnes 
7829b94acceSBarry Smith    Input Parameters:
783c7afd0dbSLois Curfman McInnes +  snes - the SNES context
784c7afd0dbSLois Curfman McInnes -  x - input vector
7859b94acceSBarry Smith 
7869b94acceSBarry Smith    Output Parameter:
7879b94acceSBarry Smith .  y - function value
7889b94acceSBarry Smith 
7899b94acceSBarry Smith    Notes:
7909b94acceSBarry Smith    SNESComputeMinimizationFunction() is valid only for
7919b94acceSBarry Smith    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
7929b94acceSBarry Smith    SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction().
793a86d99e1SLois Curfman McInnes 
79436851e7fSLois Curfman McInnes    SNESComputeMinimizationFunction() is typically used within minimization
79536851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
79636851e7fSLois Curfman McInnes    themselves.
79736851e7fSLois Curfman McInnes 
79836851e7fSLois Curfman McInnes    Level: developer
79936851e7fSLois Curfman McInnes 
800a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, minimization, function
801a86d99e1SLois Curfman McInnes 
802a86d99e1SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESGetMinimizationFunction(),
803a86d99e1SLois Curfman McInnes           SNESComputeGradient(), SNESComputeHessian()
8049b94acceSBarry Smith @*/
805329f5518SBarry Smith int SNESComputeMinimizationFunction(SNES snes,Vec x,PetscReal *y)
8069b94acceSBarry Smith {
8079b94acceSBarry Smith   int    ierr;
8083a40ed3dSBarry Smith 
8093a40ed3dSBarry Smith   PetscFunctionBegin;
810184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
811184914b5SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
812184914b5SBarry Smith   PetscCheckSameComm(snes,x);
813a8c6a408SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
814a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"Only for SNES_UNCONSTRAINED_MINIMIZATION");
815a8c6a408SBarry Smith   }
816184914b5SBarry Smith 
8179b94acceSBarry Smith   PLogEventBegin(SNES_MinimizationFunctionEval,snes,x,y,0);
818d64ed03dSBarry Smith   PetscStackPush("SNES user minimzation function");
8199b94acceSBarry Smith   ierr = (*snes->computeumfunction)(snes,x,y,snes->umfunP);CHKERRQ(ierr);
820d64ed03dSBarry Smith   PetscStackPop;
821ae3c334cSLois Curfman McInnes   snes->nfuncs++;
8229b94acceSBarry Smith   PLogEventEnd(SNES_MinimizationFunctionEval,snes,x,y,0);
8233a40ed3dSBarry Smith   PetscFunctionReturn(0);
8249b94acceSBarry Smith }
8259b94acceSBarry Smith 
8265615d1e5SSatish Balay #undef __FUNC__
827b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetGradient"
8289b94acceSBarry Smith /*@C
8299b94acceSBarry Smith    SNESSetGradient - Sets the gradient evaluation routine and gradient
8309b94acceSBarry Smith    vector for use by the SNES routines.
8319b94acceSBarry Smith 
832c7afd0dbSLois Curfman McInnes    Collective on SNES
833c7afd0dbSLois Curfman McInnes 
8349b94acceSBarry Smith    Input Parameters:
835c7afd0dbSLois Curfman McInnes +  snes - the SNES context
8369b94acceSBarry Smith .  func - function evaluation routine
837044dda88SLois Curfman McInnes .  ctx - optional user-defined context for private data for the
838044dda88SLois Curfman McInnes          gradient evaluation routine (may be PETSC_NULL)
839c7afd0dbSLois Curfman McInnes -  r - vector to store gradient value
840fee21e36SBarry Smith 
8419b94acceSBarry Smith    Calling sequence of func:
8428d76a1e5SLois Curfman McInnes $     func (SNES, Vec x, Vec g, void *ctx);
8439b94acceSBarry Smith 
844c7afd0dbSLois Curfman McInnes +  x - input vector
8459b94acceSBarry Smith .  g - gradient vector
846c7afd0dbSLois Curfman McInnes -  ctx - optional user-defined gradient context
8479b94acceSBarry Smith 
8489b94acceSBarry Smith    Notes:
8499b94acceSBarry Smith    SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
8509b94acceSBarry Smith    methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
8519b94acceSBarry Smith    SNESSetFunction().
8529b94acceSBarry Smith 
85336851e7fSLois Curfman McInnes    Level: beginner
85436851e7fSLois Curfman McInnes 
8559b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
8569b94acceSBarry Smith 
857a86d99e1SLois Curfman McInnes .seealso: SNESGetGradient(), SNESComputeGradient(), SNESSetHessian(),
858a86d99e1SLois Curfman McInnes           SNESSetMinimizationFunction(),
8599b94acceSBarry Smith @*/
86074679c65SBarry Smith int SNESSetGradient(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),void *ctx)
8619b94acceSBarry Smith {
8623a40ed3dSBarry Smith   PetscFunctionBegin;
86377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
864184914b5SBarry Smith   PetscValidHeaderSpecific(r,VEC_COOKIE);
865184914b5SBarry Smith   PetscCheckSameComm(snes,r);
866a8c6a408SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
867a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
868a8c6a408SBarry Smith   }
8699b94acceSBarry Smith   snes->computefunction     = func;
8709b94acceSBarry Smith   snes->vec_func            = snes->vec_func_always = r;
8719b94acceSBarry Smith   snes->funP                = ctx;
8723a40ed3dSBarry Smith   PetscFunctionReturn(0);
8739b94acceSBarry Smith }
8749b94acceSBarry Smith 
8755615d1e5SSatish Balay #undef __FUNC__
876b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESComputeGradient"
8779b94acceSBarry Smith /*@
878a86d99e1SLois Curfman McInnes    SNESComputeGradient - Computes the gradient that has been set with
879a86d99e1SLois Curfman McInnes    SNESSetGradient().
8809b94acceSBarry Smith 
881c7afd0dbSLois Curfman McInnes    Collective on SNES
882c7afd0dbSLois Curfman McInnes 
8839b94acceSBarry Smith    Input Parameters:
884c7afd0dbSLois Curfman McInnes +  snes - the SNES context
885c7afd0dbSLois Curfman McInnes -  x - input vector
8869b94acceSBarry Smith 
8879b94acceSBarry Smith    Output Parameter:
8889b94acceSBarry Smith .  y - gradient vector
8899b94acceSBarry Smith 
8909b94acceSBarry Smith    Notes:
8919b94acceSBarry Smith    SNESComputeGradient() is valid only for
8929b94acceSBarry Smith    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
8939b94acceSBarry Smith    SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction().
894a86d99e1SLois Curfman McInnes 
89536851e7fSLois Curfman McInnes    SNESComputeGradient() is typically used within minimization
89636851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
89736851e7fSLois Curfman McInnes    themselves.
89836851e7fSLois Curfman McInnes 
89936851e7fSLois Curfman McInnes    Level: developer
90036851e7fSLois Curfman McInnes 
901a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, gradient
902a86d99e1SLois Curfman McInnes 
903a86d99e1SLois Curfman McInnes .seealso:  SNESSetGradient(), SNESGetGradient(),
904a86d99e1SLois Curfman McInnes            SNESComputeMinimizationFunction(), SNESComputeHessian()
9059b94acceSBarry Smith @*/
9069b94acceSBarry Smith int SNESComputeGradient(SNES snes,Vec x,Vec y)
9079b94acceSBarry Smith {
9089b94acceSBarry Smith   int    ierr;
9093a40ed3dSBarry Smith 
9103a40ed3dSBarry Smith   PetscFunctionBegin;
911184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
912184914b5SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
913184914b5SBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE);
914184914b5SBarry Smith   PetscCheckSameComm(snes,x);
915184914b5SBarry Smith   PetscCheckSameComm(snes,y);
9163a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
917a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
9183a40ed3dSBarry Smith   }
9199b94acceSBarry Smith   PLogEventBegin(SNES_GradientEval,snes,x,y,0);
920d64ed03dSBarry Smith   PetscStackPush("SNES user gradient function");
9219b94acceSBarry Smith   ierr = (*snes->computefunction)(snes,x,y,snes->funP);CHKERRQ(ierr);
922d64ed03dSBarry Smith   PetscStackPop;
9239b94acceSBarry Smith   PLogEventEnd(SNES_GradientEval,snes,x,y,0);
9243a40ed3dSBarry Smith   PetscFunctionReturn(0);
9259b94acceSBarry Smith }
9269b94acceSBarry Smith 
9275615d1e5SSatish Balay #undef __FUNC__
928b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESComputeJacobian"
92962fef451SLois Curfman McInnes /*@
93062fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
93162fef451SLois Curfman McInnes    set with SNESSetJacobian().
93262fef451SLois Curfman McInnes 
933c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
934c7afd0dbSLois Curfman McInnes 
93562fef451SLois Curfman McInnes    Input Parameters:
936c7afd0dbSLois Curfman McInnes +  snes - the SNES context
937c7afd0dbSLois Curfman McInnes -  x - input vector
93862fef451SLois Curfman McInnes 
93962fef451SLois Curfman McInnes    Output Parameters:
940c7afd0dbSLois Curfman McInnes +  A - Jacobian matrix
94162fef451SLois Curfman McInnes .  B - optional preconditioning matrix
942c7afd0dbSLois Curfman McInnes -  flag - flag indicating matrix structure
943fee21e36SBarry Smith 
94462fef451SLois Curfman McInnes    Notes:
94562fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
94662fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
94762fef451SLois Curfman McInnes 
948dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the
949dc5a77f8SLois Curfman McInnes    flag parameter.
95062fef451SLois Curfman McInnes 
95162fef451SLois Curfman McInnes    SNESComputeJacobian() is valid only for SNES_NONLINEAR_EQUATIONS
95262fef451SLois Curfman McInnes    methods. An analogous routine for SNES_UNCONSTRAINED_MINIMIZATION
953005c665bSBarry Smith    methods is SNESComputeHessian().
95462fef451SLois Curfman McInnes 
95536851e7fSLois Curfman McInnes    Level: developer
95636851e7fSLois Curfman McInnes 
95762fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
95862fef451SLois Curfman McInnes 
95962fef451SLois Curfman McInnes .seealso:  SNESSetJacobian(), SLESSetOperators()
96062fef451SLois Curfman McInnes @*/
9619b94acceSBarry Smith int SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
9629b94acceSBarry Smith {
9639b94acceSBarry Smith   int    ierr;
9643a40ed3dSBarry Smith 
9653a40ed3dSBarry Smith   PetscFunctionBegin;
966184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
967184914b5SBarry Smith   PetscValidHeaderSpecific(X,VEC_COOKIE);
968184914b5SBarry Smith   PetscCheckSameComm(snes,X);
9693a40ed3dSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
970a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
9713a40ed3dSBarry Smith   }
9723a40ed3dSBarry Smith   if (!snes->computejacobian) PetscFunctionReturn(0);
9739b94acceSBarry Smith   PLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);
974c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
975d64ed03dSBarry Smith   PetscStackPush("SNES user Jacobian function");
9769b94acceSBarry Smith   ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP);CHKERRQ(ierr);
977d64ed03dSBarry Smith   PetscStackPop;
9789b94acceSBarry Smith   PLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);
9796d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
98077c4ece6SBarry Smith   PetscValidHeaderSpecific(*A,MAT_COOKIE);
98177c4ece6SBarry Smith   PetscValidHeaderSpecific(*B,MAT_COOKIE);
9823a40ed3dSBarry Smith   PetscFunctionReturn(0);
9839b94acceSBarry Smith }
9849b94acceSBarry Smith 
9855615d1e5SSatish Balay #undef __FUNC__
986b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESComputeHessian"
98762fef451SLois Curfman McInnes /*@
98862fef451SLois Curfman McInnes    SNESComputeHessian - Computes the Hessian matrix that has been
98962fef451SLois Curfman McInnes    set with SNESSetHessian().
99062fef451SLois Curfman McInnes 
991c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
992c7afd0dbSLois Curfman McInnes 
99362fef451SLois Curfman McInnes    Input Parameters:
994c7afd0dbSLois Curfman McInnes +  snes - the SNES context
995c7afd0dbSLois Curfman McInnes -  x - input vector
99662fef451SLois Curfman McInnes 
99762fef451SLois Curfman McInnes    Output Parameters:
998c7afd0dbSLois Curfman McInnes +  A - Hessian matrix
99962fef451SLois Curfman McInnes .  B - optional preconditioning matrix
1000c7afd0dbSLois Curfman McInnes -  flag - flag indicating matrix structure
1001fee21e36SBarry Smith 
100262fef451SLois Curfman McInnes    Notes:
100362fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
100462fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
100562fef451SLois Curfman McInnes 
1006dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the
1007dc5a77f8SLois Curfman McInnes    flag parameter.
100862fef451SLois Curfman McInnes 
100962fef451SLois Curfman McInnes    SNESComputeHessian() is valid only for
101062fef451SLois Curfman McInnes    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
101162fef451SLois Curfman McInnes    SNES_NONLINEAR_EQUATIONS methods is SNESComputeJacobian().
101262fef451SLois Curfman McInnes 
101336851e7fSLois Curfman McInnes    SNESComputeHessian() is typically used within minimization
101436851e7fSLois Curfman McInnes    implementations, so most users would not generally call this routine
101536851e7fSLois Curfman McInnes    themselves.
101636851e7fSLois Curfman McInnes 
101736851e7fSLois Curfman McInnes    Level: developer
101836851e7fSLois Curfman McInnes 
101962fef451SLois Curfman McInnes .keywords: SNES, compute, Hessian, matrix
102062fef451SLois Curfman McInnes 
1021a86d99e1SLois Curfman McInnes .seealso:  SNESSetHessian(), SLESSetOperators(), SNESComputeGradient(),
1022a86d99e1SLois Curfman McInnes            SNESComputeMinimizationFunction()
102362fef451SLois Curfman McInnes @*/
102462fef451SLois Curfman McInnes int SNESComputeHessian(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag)
10259b94acceSBarry Smith {
10269b94acceSBarry Smith   int    ierr;
10273a40ed3dSBarry Smith 
10283a40ed3dSBarry Smith   PetscFunctionBegin;
1029184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1030184914b5SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
1031184914b5SBarry Smith   PetscCheckSameComm(snes,x);
10323a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
1033a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
10343a40ed3dSBarry Smith   }
10353a40ed3dSBarry Smith   if (!snes->computejacobian) PetscFunctionReturn(0);
103662fef451SLois Curfman McInnes   PLogEventBegin(SNES_HessianEval,snes,x,*A,*B);
10370f4a323eSLois Curfman McInnes   *flag = DIFFERENT_NONZERO_PATTERN;
1038d64ed03dSBarry Smith   PetscStackPush("SNES user Hessian function");
103962fef451SLois Curfman McInnes   ierr = (*snes->computejacobian)(snes,x,A,B,flag,snes->jacP);CHKERRQ(ierr);
1040d64ed03dSBarry Smith   PetscStackPop;
104162fef451SLois Curfman McInnes   PLogEventEnd(SNES_HessianEval,snes,x,*A,*B);
10420f4a323eSLois Curfman McInnes   /* make sure user returned a correct Jacobian and preconditioner */
104377c4ece6SBarry Smith   PetscValidHeaderSpecific(*A,MAT_COOKIE);
104477c4ece6SBarry Smith   PetscValidHeaderSpecific(*B,MAT_COOKIE);
10453a40ed3dSBarry Smith   PetscFunctionReturn(0);
10469b94acceSBarry Smith }
10479b94acceSBarry Smith 
10485615d1e5SSatish Balay #undef __FUNC__
1049b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetJacobian"
10509b94acceSBarry Smith /*@C
10519b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
1052044dda88SLois Curfman McInnes    location to store the matrix.
10539b94acceSBarry Smith 
1054c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1055c7afd0dbSLois Curfman McInnes 
10569b94acceSBarry Smith    Input Parameters:
1057c7afd0dbSLois Curfman McInnes +  snes - the SNES context
10589b94acceSBarry Smith .  A - Jacobian matrix
10599b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
10609b94acceSBarry Smith .  func - Jacobian evaluation routine
1061c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
10622cd2dfdcSLois Curfman McInnes          Jacobian evaluation routine (may be PETSC_NULL)
10639b94acceSBarry Smith 
10649b94acceSBarry Smith    Calling sequence of func:
10658d76a1e5SLois Curfman McInnes $     func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
10669b94acceSBarry Smith 
1067c7afd0dbSLois Curfman McInnes +  x - input vector
10689b94acceSBarry Smith .  A - Jacobian matrix
10699b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1070ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
1071ac21db08SLois Curfman McInnes    structure (same as flag in SLESSetOperators())
1072c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Jacobian context
10739b94acceSBarry Smith 
10749b94acceSBarry Smith    Notes:
1075dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the flag
10762cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1077ac21db08SLois Curfman McInnes 
1078ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
10799b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
10809b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
10819b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
10829b94acceSBarry Smith    throughout the global iterations.
10839b94acceSBarry Smith 
108436851e7fSLois Curfman McInnes    Level: beginner
108536851e7fSLois Curfman McInnes 
10869b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
10879b94acceSBarry Smith 
1088ac21db08SLois Curfman McInnes .seealso: SLESSetOperators(), SNESSetFunction()
10899b94acceSBarry Smith @*/
1090454a90a3SBarry Smith int SNESSetJacobian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
10919b94acceSBarry Smith {
10923a40ed3dSBarry Smith   PetscFunctionBegin;
109377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
109400036973SBarry Smith   if (A) PetscValidHeaderSpecific(A,MAT_COOKIE);
109500036973SBarry Smith   if (B) PetscValidHeaderSpecific(B,MAT_COOKIE);
109600036973SBarry Smith   if (A) PetscCheckSameComm(snes,A);
109700036973SBarry Smith   if (B) PetscCheckSameComm(snes,B);
1098a8c6a408SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
1099a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
1100a8c6a408SBarry Smith   }
1101184914b5SBarry Smith 
11029b94acceSBarry Smith   snes->computejacobian = func;
11039b94acceSBarry Smith   snes->jacP            = ctx;
11049b94acceSBarry Smith   snes->jacobian        = A;
11059b94acceSBarry Smith   snes->jacobian_pre    = B;
11063a40ed3dSBarry Smith   PetscFunctionReturn(0);
11079b94acceSBarry Smith }
110862fef451SLois Curfman McInnes 
11095615d1e5SSatish Balay #undef __FUNC__
1110b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetJacobian"
1111c2aafc4cSSatish Balay /*@C
1112b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
1113b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
1114b4fd4287SBarry Smith 
1115c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object will be parallel if SNES object is
1116c7afd0dbSLois Curfman McInnes 
1117b4fd4287SBarry Smith    Input Parameter:
1118b4fd4287SBarry Smith .  snes - the nonlinear solver context
1119b4fd4287SBarry Smith 
1120b4fd4287SBarry Smith    Output Parameters:
1121c7afd0dbSLois Curfman McInnes +  A - location to stash Jacobian matrix (or PETSC_NULL)
1122b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
112300036973SBarry Smith .  ctx - location to stash Jacobian ctx (or PETSC_NULL)
112400036973SBarry Smith -  func - location to put Jacobian function (or PETSC_NULL)
1125fee21e36SBarry Smith 
112636851e7fSLois Curfman McInnes    Level: advanced
112736851e7fSLois Curfman McInnes 
1128b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
1129b4fd4287SBarry Smith @*/
113000036973SBarry Smith int SNESGetJacobian(SNES snes,Mat *A,Mat *B,void **ctx,int (**func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*))
1131b4fd4287SBarry Smith {
11323a40ed3dSBarry Smith   PetscFunctionBegin;
1133184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
11343a40ed3dSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
1135a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
11363a40ed3dSBarry Smith   }
1137b4fd4287SBarry Smith   if (A)    *A    = snes->jacobian;
1138b4fd4287SBarry Smith   if (B)    *B    = snes->jacobian_pre;
1139b4fd4287SBarry Smith   if (ctx)  *ctx  = snes->jacP;
114000036973SBarry Smith   if (func) *func = snes->computejacobian;
11413a40ed3dSBarry Smith   PetscFunctionReturn(0);
1142b4fd4287SBarry Smith }
1143b4fd4287SBarry Smith 
11445615d1e5SSatish Balay #undef __FUNC__
1145b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetHessian"
11469b94acceSBarry Smith /*@C
11479b94acceSBarry Smith    SNESSetHessian - Sets the function to compute Hessian as well as the
1148044dda88SLois Curfman McInnes    location to store the matrix.
11499b94acceSBarry Smith 
1150c7afd0dbSLois Curfman McInnes    Collective on SNES and Mat
1151c7afd0dbSLois Curfman McInnes 
11529b94acceSBarry Smith    Input Parameters:
1153c7afd0dbSLois Curfman McInnes +  snes - the SNES context
11549b94acceSBarry Smith .  A - Hessian matrix
11559b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Hessian)
11569b94acceSBarry Smith .  func - Jacobian evaluation routine
1157c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined context for private data for the
1158313e4042SLois Curfman McInnes          Hessian evaluation routine (may be PETSC_NULL)
11599b94acceSBarry Smith 
11609b94acceSBarry Smith    Calling sequence of func:
11618d76a1e5SLois Curfman McInnes $    func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
11629b94acceSBarry Smith 
1163c7afd0dbSLois Curfman McInnes +  x - input vector
11649b94acceSBarry Smith .  A - Hessian matrix
11659b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
1166ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
1167ac21db08SLois Curfman McInnes    structure (same as flag in SLESSetOperators())
1168c7afd0dbSLois Curfman McInnes -  ctx - [optional] user-defined Hessian context
11699b94acceSBarry Smith 
11709b94acceSBarry Smith    Notes:
1171dc5a77f8SLois Curfman McInnes    See SLESSetOperators() for important information about setting the flag
11722cd2dfdcSLois Curfman McInnes    output parameter in the routine func().  Be sure to read this information!
1173ac21db08SLois Curfman McInnes 
11749b94acceSBarry Smith    The function func() takes Mat * as the matrix arguments rather than Mat.
11759b94acceSBarry Smith    This allows the Hessian evaluation routine to replace A and/or B with a
11769b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
11779b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
11789b94acceSBarry Smith    throughout the global iterations.
11799b94acceSBarry Smith 
118036851e7fSLois Curfman McInnes    Level: beginner
118136851e7fSLois Curfman McInnes 
11829b94acceSBarry Smith .keywords: SNES, nonlinear, set, Hessian, matrix
11839b94acceSBarry Smith 
1184ac21db08SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESSetGradient(), SLESSetOperators()
11859b94acceSBarry Smith @*/
1186454a90a3SBarry Smith int SNESSetHessian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),void *ctx)
11879b94acceSBarry Smith {
11883a40ed3dSBarry Smith   PetscFunctionBegin;
118977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1190184914b5SBarry Smith   PetscValidHeaderSpecific(A,MAT_COOKIE);
1191184914b5SBarry Smith   PetscValidHeaderSpecific(B,MAT_COOKIE);
1192184914b5SBarry Smith   PetscCheckSameComm(snes,A);
1193184914b5SBarry Smith   PetscCheckSameComm(snes,B);
1194d4bb536fSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
1195a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
1196d4bb536fSBarry Smith   }
11979b94acceSBarry Smith   snes->computejacobian = func;
11989b94acceSBarry Smith   snes->jacP            = ctx;
11999b94acceSBarry Smith   snes->jacobian        = A;
12009b94acceSBarry Smith   snes->jacobian_pre    = B;
12013a40ed3dSBarry Smith   PetscFunctionReturn(0);
12029b94acceSBarry Smith }
12039b94acceSBarry Smith 
12045615d1e5SSatish Balay #undef __FUNC__
1205b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetHessian"
120662fef451SLois Curfman McInnes /*@
120762fef451SLois Curfman McInnes    SNESGetHessian - Returns the Hessian matrix and optionally the user
120862fef451SLois Curfman McInnes    provided context for evaluating the Hessian.
120962fef451SLois Curfman McInnes 
1210c7afd0dbSLois Curfman McInnes    Not Collective, but Mat object is parallel if SNES object is parallel
1211c7afd0dbSLois Curfman McInnes 
121262fef451SLois Curfman McInnes    Input Parameter:
121362fef451SLois Curfman McInnes .  snes - the nonlinear solver context
121462fef451SLois Curfman McInnes 
121562fef451SLois Curfman McInnes    Output Parameters:
1216c7afd0dbSLois Curfman McInnes +  A - location to stash Hessian matrix (or PETSC_NULL)
121762fef451SLois Curfman McInnes .  B - location to stash preconditioner matrix (or PETSC_NULL)
1218c7afd0dbSLois Curfman McInnes -  ctx - location to stash Hessian ctx (or PETSC_NULL)
1219fee21e36SBarry Smith 
122036851e7fSLois Curfman McInnes    Level: advanced
122136851e7fSLois Curfman McInnes 
122262fef451SLois Curfman McInnes .seealso: SNESSetHessian(), SNESComputeHessian()
1223c7afd0dbSLois Curfman McInnes 
1224c7afd0dbSLois Curfman McInnes .keywords: SNES, get, Hessian
122562fef451SLois Curfman McInnes @*/
122662fef451SLois Curfman McInnes int SNESGetHessian(SNES snes,Mat *A,Mat *B,void **ctx)
122762fef451SLois Curfman McInnes {
12283a40ed3dSBarry Smith   PetscFunctionBegin;
1229184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
12303a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION){
1231a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
12323a40ed3dSBarry Smith   }
123362fef451SLois Curfman McInnes   if (A)   *A = snes->jacobian;
123462fef451SLois Curfman McInnes   if (B)   *B = snes->jacobian_pre;
123562fef451SLois Curfman McInnes   if (ctx) *ctx = snes->jacP;
12363a40ed3dSBarry Smith   PetscFunctionReturn(0);
123762fef451SLois Curfman McInnes }
123862fef451SLois Curfman McInnes 
12399b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
12409b94acceSBarry Smith 
12415615d1e5SSatish Balay #undef __FUNC__
1242b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetUp"
12439b94acceSBarry Smith /*@
12449b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
1245272ac6f2SLois Curfman McInnes    of a nonlinear solver.
12469b94acceSBarry Smith 
1247fee21e36SBarry Smith    Collective on SNES
1248fee21e36SBarry Smith 
1249c7afd0dbSLois Curfman McInnes    Input Parameters:
1250c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1251c7afd0dbSLois Curfman McInnes -  x - the solution vector
1252c7afd0dbSLois Curfman McInnes 
1253272ac6f2SLois Curfman McInnes    Notes:
1254272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
1255272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
1256272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
1257272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
1258272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
1259272ac6f2SLois Curfman McInnes 
126036851e7fSLois Curfman McInnes    Level: advanced
126136851e7fSLois Curfman McInnes 
12629b94acceSBarry Smith .keywords: SNES, nonlinear, setup
12639b94acceSBarry Smith 
12649b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
12659b94acceSBarry Smith @*/
12668ddd3da0SLois Curfman McInnes int SNESSetUp(SNES snes,Vec x)
12679b94acceSBarry Smith {
1268f1af5d2fSBarry Smith   int        ierr;
1269f1af5d2fSBarry Smith   PetscTruth flg;
12703a40ed3dSBarry Smith 
12713a40ed3dSBarry Smith   PetscFunctionBegin;
127277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
127377c4ece6SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
1274184914b5SBarry Smith   PetscCheckSameComm(snes,x);
12758ddd3da0SLois Curfman McInnes   snes->vec_sol = snes->vec_sol_always = x;
12769b94acceSBarry Smith 
1277c1f60f51SBarry Smith   ierr = OptionsHasName(snes->prefix,"-snes_mf_operator",&flg);CHKERRQ(ierr);
1278c1f60f51SBarry Smith   /*
1279c1f60f51SBarry Smith       This version replaces the user provided Jacobian matrix with a
1280dfa02198SLois Curfman McInnes       matrix-free version but still employs the user-provided preconditioner matrix
1281c1f60f51SBarry Smith   */
1282c1f60f51SBarry Smith   if (flg) {
1283c1f60f51SBarry Smith     Mat J;
12845a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
1285c1f60f51SBarry Smith     PLogObjectParent(snes,J);
1286c1f60f51SBarry Smith     snes->mfshell = J;
1287c1f60f51SBarry Smith     snes->jacobian = J;
1288c2aafc4cSSatish Balay     if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
1289c1f60f51SBarry Smith       PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Jacobian routines\n");
1290d64ed03dSBarry Smith     } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
1291c1f60f51SBarry Smith       PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Hessian routines\n");
1292d4bb536fSBarry Smith     } else {
1293a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_SUP,0,"Method class doesn't support matrix-free operator option");
1294d4bb536fSBarry Smith     }
12955a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
1296c1f60f51SBarry Smith   }
1297272ac6f2SLois Curfman McInnes   ierr = OptionsHasName(snes->prefix,"-snes_mf",&flg);CHKERRQ(ierr);
1298c1f60f51SBarry Smith   /*
1299dfa02198SLois Curfman McInnes       This version replaces both the user-provided Jacobian and the user-
1300c1f60f51SBarry Smith       provided preconditioner matrix with the default matrix free version.
1301c1f60f51SBarry Smith    */
130231615d04SLois Curfman McInnes   if (flg) {
1303272ac6f2SLois Curfman McInnes     Mat J;
13045a655dc6SBarry Smith     ierr = MatCreateSNESMF(snes,snes->vec_sol,&J);CHKERRQ(ierr);
1305272ac6f2SLois Curfman McInnes     PLogObjectParent(snes,J);
1306272ac6f2SLois Curfman McInnes     snes->mfshell = J;
130783e56afdSLois Curfman McInnes     if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
13081d1367b7SBarry Smith       ierr = SNESSetJacobian(snes,J,J,MatSNESMFFormJacobian,snes->funP);CHKERRQ(ierr);
130994a424c1SBarry Smith       PLogInfo(snes,"SNESSetUp: Setting default matrix-free Jacobian routines\n");
1310d64ed03dSBarry Smith     } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
13111d1367b7SBarry Smith       ierr = SNESSetHessian(snes,J,J,MatSNESMFFormJacobian,snes->funP);CHKERRQ(ierr);
131294a424c1SBarry Smith       PLogInfo(snes,"SNESSetUp: Setting default matrix-free Hessian routines\n");
1313d4bb536fSBarry Smith     } else {
1314a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_SUP,0,"Method class doesn't support matrix-free option");
1315d4bb536fSBarry Smith     }
13165a655dc6SBarry Smith     ierr = MatSNESMFSetFromOptions(J);CHKERRQ(ierr);
1317272ac6f2SLois Curfman McInnes   }
13189b94acceSBarry Smith   if ((snes->method_class == SNES_NONLINEAR_EQUATIONS)) {
13196831982aSBarry Smith     PetscTruth iseqtr;
13206831982aSBarry Smith 
1321a8c6a408SBarry Smith     if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetFunction() first");
1322a8c6a408SBarry Smith     if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetFunction() first");
13235a655dc6SBarry Smith     if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetJacobian() first \n or use -snes_mf option");
1324a8c6a408SBarry Smith     if (snes->vec_func == snes->vec_sol) {
1325a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_ARG_IDN,0,"Solution vector cannot be function vector");
1326a8c6a408SBarry Smith     }
1327a703fe33SLois Curfman McInnes 
1328a703fe33SLois Curfman McInnes     /* Set the KSP stopping criterion to use the Eisenstat-Walker method */
13296831982aSBarry Smith     ierr = PetscTypeCompare((PetscObject)snes,SNESEQTR,&iseqtr);CHKERRQ(ierr);
13306831982aSBarry Smith     if (snes->ksp_ewconv && !iseqtr) {
1331a703fe33SLois Curfman McInnes       SLES sles; KSP ksp;
1332a703fe33SLois Curfman McInnes       ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr);
1333a703fe33SLois Curfman McInnes       ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr);
1334*186905e3SBarry Smith       ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private,snes);CHKERRQ(ierr);
1335a703fe33SLois Curfman McInnes     }
13369b94acceSBarry Smith   } else if ((snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION)) {
1337a8c6a408SBarry Smith     if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetGradient() first");
1338a8c6a408SBarry Smith     if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetGradient() first");
1339a8c6a408SBarry Smith     if (!snes->computeumfunction) {
1340a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetMinimizationFunction() first");
1341a8c6a408SBarry Smith     }
13425a655dc6SBarry Smith     if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetHessian()");
1343d4bb536fSBarry Smith   } else {
1344a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Unknown method class");
1345d4bb536fSBarry Smith   }
1346a703fe33SLois Curfman McInnes   if (snes->setup) {ierr = (*snes->setup)(snes);CHKERRQ(ierr);}
134782bf6240SBarry Smith   snes->setupcalled = 1;
13483a40ed3dSBarry Smith   PetscFunctionReturn(0);
13499b94acceSBarry Smith }
13509b94acceSBarry Smith 
13515615d1e5SSatish Balay #undef __FUNC__
1352b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESDestroy"
13539b94acceSBarry Smith /*@C
13549b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
13559b94acceSBarry Smith    with SNESCreate().
13569b94acceSBarry Smith 
1357c7afd0dbSLois Curfman McInnes    Collective on SNES
1358c7afd0dbSLois Curfman McInnes 
13599b94acceSBarry Smith    Input Parameter:
13609b94acceSBarry Smith .  snes - the SNES context
13619b94acceSBarry Smith 
136236851e7fSLois Curfman McInnes    Level: beginner
136336851e7fSLois Curfman McInnes 
13649b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
13659b94acceSBarry Smith 
136663a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
13679b94acceSBarry Smith @*/
13689b94acceSBarry Smith int SNESDestroy(SNES snes)
13699b94acceSBarry Smith {
1370b8a78c4aSBarry Smith   int i,ierr;
13713a40ed3dSBarry Smith 
13723a40ed3dSBarry Smith   PetscFunctionBegin;
137377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
13743a40ed3dSBarry Smith   if (--snes->refct > 0) PetscFunctionReturn(0);
1375d4bb536fSBarry Smith 
1376be0abb6dSBarry Smith   /* if memory was published with AMS then destroy it */
13770f5bd95cSBarry Smith   ierr = PetscObjectDepublish(snes);CHKERRQ(ierr);
1378be0abb6dSBarry Smith 
1379e1311b90SBarry Smith   if (snes->destroy) {ierr = (*(snes)->destroy)(snes);CHKERRQ(ierr);}
1380606d414cSSatish Balay   if (snes->kspconvctx) {ierr = PetscFree(snes->kspconvctx);CHKERRQ(ierr);}
1381522c5e43SBarry Smith   if (snes->mfshell) {ierr = MatDestroy(snes->mfshell);CHKERRQ(ierr);}
13829b94acceSBarry Smith   ierr = SLESDestroy(snes->sles);CHKERRQ(ierr);
1383522c5e43SBarry Smith   if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);}
1384b8a78c4aSBarry Smith   for (i=0; i<snes->numbermonitors; i++) {
1385b8a78c4aSBarry Smith     if (snes->monitordestroy[i]) {
1386b8a78c4aSBarry Smith       ierr = (*snes->monitordestroy[i])(snes->monitorcontext[i]);CHKERRQ(ierr);
1387b8a78c4aSBarry Smith     }
1388b8a78c4aSBarry Smith   }
13899b94acceSBarry Smith   PLogObjectDestroy((PetscObject)snes);
13900452661fSBarry Smith   PetscHeaderDestroy((PetscObject)snes);
13913a40ed3dSBarry Smith   PetscFunctionReturn(0);
13929b94acceSBarry Smith }
13939b94acceSBarry Smith 
13949b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
13959b94acceSBarry Smith 
13965615d1e5SSatish Balay #undef __FUNC__
1397b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetTolerances"
13989b94acceSBarry Smith /*@
1399d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
14009b94acceSBarry Smith 
1401c7afd0dbSLois Curfman McInnes    Collective on SNES
1402c7afd0dbSLois Curfman McInnes 
14039b94acceSBarry Smith    Input Parameters:
1404c7afd0dbSLois Curfman McInnes +  snes - the SNES context
140533174efeSLois Curfman McInnes .  atol - absolute convergence tolerance
140633174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
140733174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
140833174efeSLois Curfman McInnes            of the change in the solution between steps
140933174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1410c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1411fee21e36SBarry Smith 
141233174efeSLois Curfman McInnes    Options Database Keys:
1413c7afd0dbSLois Curfman McInnes +    -snes_atol <atol> - Sets atol
1414c7afd0dbSLois Curfman McInnes .    -snes_rtol <rtol> - Sets rtol
1415c7afd0dbSLois Curfman McInnes .    -snes_stol <stol> - Sets stol
1416c7afd0dbSLois Curfman McInnes .    -snes_max_it <maxit> - Sets maxit
1417c7afd0dbSLois Curfman McInnes -    -snes_max_funcs <maxf> - Sets maxf
14189b94acceSBarry Smith 
1419d7a720efSLois Curfman McInnes    Notes:
14209b94acceSBarry Smith    The default maximum number of iterations is 50.
14219b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
14229b94acceSBarry Smith 
142336851e7fSLois Curfman McInnes    Level: intermediate
142436851e7fSLois Curfman McInnes 
142533174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
14269b94acceSBarry Smith 
1427d7a720efSLois Curfman McInnes .seealso: SNESSetTrustRegionTolerance(), SNESSetMinimizationFunctionTolerance()
14289b94acceSBarry Smith @*/
1429329f5518SBarry Smith int SNESSetTolerances(SNES snes,PetscReal atol,PetscReal rtol,PetscReal stol,int maxit,int maxf)
14309b94acceSBarry Smith {
14313a40ed3dSBarry Smith   PetscFunctionBegin;
143277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1433d7a720efSLois Curfman McInnes   if (atol != PETSC_DEFAULT)  snes->atol      = atol;
1434d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1435d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1436d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1437d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
14383a40ed3dSBarry Smith   PetscFunctionReturn(0);
14399b94acceSBarry Smith }
14409b94acceSBarry Smith 
14415615d1e5SSatish Balay #undef __FUNC__
1442b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetTolerances"
14439b94acceSBarry Smith /*@
144433174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
144533174efeSLois Curfman McInnes 
1446c7afd0dbSLois Curfman McInnes    Not Collective
1447c7afd0dbSLois Curfman McInnes 
144833174efeSLois Curfman McInnes    Input Parameters:
1449c7afd0dbSLois Curfman McInnes +  snes - the SNES context
145033174efeSLois Curfman McInnes .  atol - absolute convergence tolerance
145133174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
145233174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
145333174efeSLois Curfman McInnes            of the change in the solution between steps
145433174efeSLois Curfman McInnes .  maxit - maximum number of iterations
1455c7afd0dbSLois Curfman McInnes -  maxf - maximum number of function evaluations
1456fee21e36SBarry Smith 
145733174efeSLois Curfman McInnes    Notes:
145833174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
145933174efeSLois Curfman McInnes 
146036851e7fSLois Curfman McInnes    Level: intermediate
146136851e7fSLois Curfman McInnes 
146233174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
146333174efeSLois Curfman McInnes 
146433174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
146533174efeSLois Curfman McInnes @*/
1466329f5518SBarry Smith int SNESGetTolerances(SNES snes,PetscReal *atol,PetscReal *rtol,PetscReal *stol,int *maxit,int *maxf)
146733174efeSLois Curfman McInnes {
14683a40ed3dSBarry Smith   PetscFunctionBegin;
146933174efeSLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
147033174efeSLois Curfman McInnes   if (atol)  *atol  = snes->atol;
147133174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
147233174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
147333174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
147433174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
14753a40ed3dSBarry Smith   PetscFunctionReturn(0);
147633174efeSLois Curfman McInnes }
147733174efeSLois Curfman McInnes 
14785615d1e5SSatish Balay #undef __FUNC__
1479b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetTrustRegionTolerance"
148033174efeSLois Curfman McInnes /*@
14819b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
14829b94acceSBarry Smith 
1483fee21e36SBarry Smith    Collective on SNES
1484fee21e36SBarry Smith 
1485c7afd0dbSLois Curfman McInnes    Input Parameters:
1486c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1487c7afd0dbSLois Curfman McInnes -  tol - tolerance
1488c7afd0dbSLois Curfman McInnes 
14899b94acceSBarry Smith    Options Database Key:
1490c7afd0dbSLois Curfman McInnes .  -snes_trtol <tol> - Sets tol
14919b94acceSBarry Smith 
149236851e7fSLois Curfman McInnes    Level: intermediate
149336851e7fSLois Curfman McInnes 
14949b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
14959b94acceSBarry Smith 
1496d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetMinimizationFunctionTolerance()
14979b94acceSBarry Smith @*/
1498329f5518SBarry Smith int SNESSetTrustRegionTolerance(SNES snes,PetscReal tol)
14999b94acceSBarry Smith {
15003a40ed3dSBarry Smith   PetscFunctionBegin;
150177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
15029b94acceSBarry Smith   snes->deltatol = tol;
15033a40ed3dSBarry Smith   PetscFunctionReturn(0);
15049b94acceSBarry Smith }
15059b94acceSBarry Smith 
15065615d1e5SSatish Balay #undef __FUNC__
1507b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetMinimizationFunctionTolerance"
15089b94acceSBarry Smith /*@
150977c4ece6SBarry Smith    SNESSetMinimizationFunctionTolerance - Sets the minimum allowable function tolerance
15109b94acceSBarry Smith    for unconstrained minimization solvers.
15119b94acceSBarry Smith 
1512fee21e36SBarry Smith    Collective on SNES
1513fee21e36SBarry Smith 
1514c7afd0dbSLois Curfman McInnes    Input Parameters:
1515c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1516c7afd0dbSLois Curfman McInnes -  ftol - minimum function tolerance
1517c7afd0dbSLois Curfman McInnes 
15189b94acceSBarry Smith    Options Database Key:
1519c7afd0dbSLois Curfman McInnes .  -snes_fmin <ftol> - Sets ftol
15209b94acceSBarry Smith 
15219b94acceSBarry Smith    Note:
152277c4ece6SBarry Smith    SNESSetMinimizationFunctionTolerance() is valid for SNES_UNCONSTRAINED_MINIMIZATION
15239b94acceSBarry Smith    methods only.
15249b94acceSBarry Smith 
152536851e7fSLois Curfman McInnes    Level: intermediate
152636851e7fSLois Curfman McInnes 
15279b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimum, convergence, function, tolerance
15289b94acceSBarry Smith 
1529d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetTrustRegionTolerance()
15309b94acceSBarry Smith @*/
1531329f5518SBarry Smith int SNESSetMinimizationFunctionTolerance(SNES snes,PetscReal ftol)
15329b94acceSBarry Smith {
15333a40ed3dSBarry Smith   PetscFunctionBegin;
153477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
15359b94acceSBarry Smith   snes->fmin = ftol;
15363a40ed3dSBarry Smith   PetscFunctionReturn(0);
15379b94acceSBarry Smith }
1538df9fa365SBarry Smith /*
1539df9fa365SBarry Smith    Duplicate the lg monitors for SNES from KSP; for some reason with
1540df9fa365SBarry Smith    dynamic libraries things don't work under Sun4 if we just use
1541df9fa365SBarry Smith    macros instead of functions
1542df9fa365SBarry Smith */
1543ce1608b8SBarry Smith #undef __FUNC__
1544b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESLGMonitor"
1545329f5518SBarry Smith int SNESLGMonitor(SNES snes,int it,PetscReal norm,void *ctx)
1546ce1608b8SBarry Smith {
1547ce1608b8SBarry Smith   int ierr;
1548ce1608b8SBarry Smith 
1549ce1608b8SBarry Smith   PetscFunctionBegin;
1550184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1551ce1608b8SBarry Smith   ierr = KSPLGMonitor((KSP)snes,it,norm,ctx);CHKERRQ(ierr);
1552ce1608b8SBarry Smith   PetscFunctionReturn(0);
1553ce1608b8SBarry Smith }
1554ce1608b8SBarry Smith 
1555df9fa365SBarry Smith #undef __FUNC__
1556b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESLGMonitorCreate"
1557df9fa365SBarry Smith int SNESLGMonitorCreate(char *host,char *label,int x,int y,int m,int n,DrawLG *draw)
1558df9fa365SBarry Smith {
1559df9fa365SBarry Smith   int ierr;
1560df9fa365SBarry Smith 
1561df9fa365SBarry Smith   PetscFunctionBegin;
1562df9fa365SBarry Smith   ierr = KSPLGMonitorCreate(host,label,x,y,m,n,draw);CHKERRQ(ierr);
1563df9fa365SBarry Smith   PetscFunctionReturn(0);
1564df9fa365SBarry Smith }
1565df9fa365SBarry Smith 
1566df9fa365SBarry Smith #undef __FUNC__
1567b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESLGMonitorDestroy"
1568df9fa365SBarry Smith int SNESLGMonitorDestroy(DrawLG draw)
1569df9fa365SBarry Smith {
1570df9fa365SBarry Smith   int ierr;
1571df9fa365SBarry Smith 
1572df9fa365SBarry Smith   PetscFunctionBegin;
1573df9fa365SBarry Smith   ierr = KSPLGMonitorDestroy(draw);CHKERRQ(ierr);
1574df9fa365SBarry Smith   PetscFunctionReturn(0);
1575df9fa365SBarry Smith }
1576df9fa365SBarry Smith 
15779b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
15789b94acceSBarry Smith 
15795615d1e5SSatish Balay #undef __FUNC__
1580b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetMonitor"
15819b94acceSBarry Smith /*@C
15825cd90555SBarry Smith    SNESSetMonitor - Sets an ADDITIONAL function that is to be used at every
15839b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
15849b94acceSBarry Smith    progress.
15859b94acceSBarry Smith 
1586fee21e36SBarry Smith    Collective on SNES
1587fee21e36SBarry Smith 
1588c7afd0dbSLois Curfman McInnes    Input Parameters:
1589c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1590c7afd0dbSLois Curfman McInnes .  func - monitoring routine
1591b8a78c4aSBarry Smith .  mctx - [optional] user-defined context for private data for the
1592b3006f0bSLois Curfman McInnes           monitor routine (use PETSC_NULL if no context is desitre)
1593b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
1594b3006f0bSLois Curfman McInnes           (may be PETSC_NULL)
15959b94acceSBarry Smith 
1596c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1597329f5518SBarry Smith $     int func(SNES snes,int its, PetscReal norm,void *mctx)
1598c7afd0dbSLois Curfman McInnes 
1599c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1600c7afd0dbSLois Curfman McInnes .    its - iteration number
1601c7afd0dbSLois Curfman McInnes .    norm - 2-norm function value (may be estimated)
1602c7afd0dbSLois Curfman McInnes             for SNES_NONLINEAR_EQUATIONS methods
160340a0c1c6SLois Curfman McInnes .    norm - 2-norm gradient value (may be estimated)
1604c7afd0dbSLois Curfman McInnes             for SNES_UNCONSTRAINED_MINIMIZATION methods
160540a0c1c6SLois Curfman McInnes -    mctx - [optional] monitoring context
16069b94acceSBarry Smith 
16079665c990SLois Curfman McInnes    Options Database Keys:
1608c7afd0dbSLois Curfman McInnes +    -snes_monitor        - sets SNESDefaultMonitor()
1609c7afd0dbSLois Curfman McInnes .    -snes_xmonitor       - sets line graph monitor,
1610c7afd0dbSLois Curfman McInnes                             uses SNESLGMonitorCreate()
1611c7afd0dbSLois Curfman McInnes _    -snes_cancelmonitors - cancels all monitors that have
1612c7afd0dbSLois Curfman McInnes                             been hardwired into a code by
1613c7afd0dbSLois Curfman McInnes                             calls to SNESSetMonitor(), but
1614c7afd0dbSLois Curfman McInnes                             does not cancel those set via
1615c7afd0dbSLois Curfman McInnes                             the options database.
16169665c990SLois Curfman McInnes 
1617639f9d9dSBarry Smith    Notes:
16186bc08f3fSLois Curfman McInnes    Several different monitoring routines may be set by calling
16196bc08f3fSLois Curfman McInnes    SNESSetMonitor() multiple times; all will be called in the
16206bc08f3fSLois Curfman McInnes    order in which they were set.
1621639f9d9dSBarry Smith 
162236851e7fSLois Curfman McInnes    Level: intermediate
162336851e7fSLois Curfman McInnes 
16249b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
16259b94acceSBarry Smith 
16265cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESClearMonitor()
16279b94acceSBarry Smith @*/
1628329f5518SBarry Smith int SNESSetMonitor(SNES snes,int (*func)(SNES,int,PetscReal,void*),void *mctx,int (*monitordestroy)(void *))
16299b94acceSBarry Smith {
16303a40ed3dSBarry Smith   PetscFunctionBegin;
1631184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1632639f9d9dSBarry Smith   if (snes->numbermonitors >= MAXSNESMONITORS) {
1633a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Too many monitors set");
1634639f9d9dSBarry Smith   }
1635639f9d9dSBarry Smith 
1636639f9d9dSBarry Smith   snes->monitor[snes->numbermonitors]           = func;
1637b8a78c4aSBarry Smith   snes->monitordestroy[snes->numbermonitors]    = monitordestroy;
1638639f9d9dSBarry Smith   snes->monitorcontext[snes->numbermonitors++]  = (void*)mctx;
16393a40ed3dSBarry Smith   PetscFunctionReturn(0);
16409b94acceSBarry Smith }
16419b94acceSBarry Smith 
16425615d1e5SSatish Balay #undef __FUNC__
1643b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESClearMonitor"
16445cd90555SBarry Smith /*@C
16455cd90555SBarry Smith    SNESClearMonitor - Clears all the monitor functions for a SNES object.
16465cd90555SBarry Smith 
1647c7afd0dbSLois Curfman McInnes    Collective on SNES
1648c7afd0dbSLois Curfman McInnes 
16495cd90555SBarry Smith    Input Parameters:
16505cd90555SBarry Smith .  snes - the SNES context
16515cd90555SBarry Smith 
16525cd90555SBarry Smith    Options Database:
1653c7afd0dbSLois Curfman McInnes .  -snes_cancelmonitors - cancels all monitors that have been hardwired
1654c7afd0dbSLois Curfman McInnes     into a code by calls to SNESSetMonitor(), but does not cancel those
1655c7afd0dbSLois Curfman McInnes     set via the options database
16565cd90555SBarry Smith 
16575cd90555SBarry Smith    Notes:
16585cd90555SBarry Smith    There is no way to clear one specific monitor from a SNES object.
16595cd90555SBarry Smith 
166036851e7fSLois Curfman McInnes    Level: intermediate
166136851e7fSLois Curfman McInnes 
16625cd90555SBarry Smith .keywords: SNES, nonlinear, set, monitor
16635cd90555SBarry Smith 
16645cd90555SBarry Smith .seealso: SNESDefaultMonitor(), SNESSetMonitor()
16655cd90555SBarry Smith @*/
16665cd90555SBarry Smith int SNESClearMonitor(SNES snes)
16675cd90555SBarry Smith {
16685cd90555SBarry Smith   PetscFunctionBegin;
1669184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
16705cd90555SBarry Smith   snes->numbermonitors = 0;
16715cd90555SBarry Smith   PetscFunctionReturn(0);
16725cd90555SBarry Smith }
16735cd90555SBarry Smith 
16745cd90555SBarry Smith #undef __FUNC__
1675b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetConvergenceTest"
16769b94acceSBarry Smith /*@C
16779b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
16789b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
16799b94acceSBarry Smith 
1680fee21e36SBarry Smith    Collective on SNES
1681fee21e36SBarry Smith 
1682c7afd0dbSLois Curfman McInnes    Input Parameters:
1683c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1684c7afd0dbSLois Curfman McInnes .  func - routine to test for convergence
1685c7afd0dbSLois Curfman McInnes -  cctx - [optional] context for private data for the convergence routine
1686c7afd0dbSLois Curfman McInnes           (may be PETSC_NULL)
16879b94acceSBarry Smith 
1688c7afd0dbSLois Curfman McInnes    Calling sequence of func:
1689329f5518SBarry Smith $     int func (SNES snes,PetscReal xnorm,PetscReal gnorm,PetscReal f,SNESConvergedReason *reason,void *cctx)
1690c7afd0dbSLois Curfman McInnes 
1691c7afd0dbSLois Curfman McInnes +    snes - the SNES context
1692c7afd0dbSLois Curfman McInnes .    cctx - [optional] convergence context
1693184914b5SBarry Smith .    reason - reason for convergence/divergence
1694c7afd0dbSLois Curfman McInnes .    xnorm - 2-norm of current iterate
1695c7afd0dbSLois Curfman McInnes .    gnorm - 2-norm of current step (SNES_NONLINEAR_EQUATIONS methods)
1696c7afd0dbSLois Curfman McInnes .    f - 2-norm of function (SNES_NONLINEAR_EQUATIONS methods)
1697c7afd0dbSLois Curfman McInnes .    gnorm - 2-norm of current gradient (SNES_UNCONSTRAINED_MINIMIZATION methods)
1698c7afd0dbSLois Curfman McInnes -    f - function value (SNES_UNCONSTRAINED_MINIMIZATION methods)
16999b94acceSBarry Smith 
170036851e7fSLois Curfman McInnes    Level: advanced
170136851e7fSLois Curfman McInnes 
17029b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
17039b94acceSBarry Smith 
170440191667SLois Curfman McInnes .seealso: SNESConverged_EQ_LS(), SNESConverged_EQ_TR(),
170540191667SLois Curfman McInnes           SNESConverged_UM_LS(), SNESConverged_UM_TR()
17069b94acceSBarry Smith @*/
1707329f5518SBarry Smith int SNESSetConvergenceTest(SNES snes,int (*func)(SNES,PetscReal,PetscReal,PetscReal,SNESConvergedReason*,void*),void *cctx)
17089b94acceSBarry Smith {
17093a40ed3dSBarry Smith   PetscFunctionBegin;
1710184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
17119b94acceSBarry Smith   (snes)->converged = func;
17129b94acceSBarry Smith   (snes)->cnvP      = cctx;
17133a40ed3dSBarry Smith   PetscFunctionReturn(0);
17149b94acceSBarry Smith }
17159b94acceSBarry Smith 
17165615d1e5SSatish Balay #undef __FUNC__
1717b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetConvergedReason"
1718184914b5SBarry Smith /*@C
1719184914b5SBarry Smith    SNESGetConvergedReason - Gets the reason the SNES iteration was stopped.
1720184914b5SBarry Smith 
1721184914b5SBarry Smith    Not Collective
1722184914b5SBarry Smith 
1723184914b5SBarry Smith    Input Parameter:
1724184914b5SBarry Smith .  snes - the SNES context
1725184914b5SBarry Smith 
1726184914b5SBarry Smith    Output Parameter:
1727e090d566SSatish Balay .  reason - negative value indicates diverged, positive value converged, see petscsnes.h or the
1728184914b5SBarry Smith             manual pages for the individual convergence tests for complete lists
1729184914b5SBarry Smith 
1730184914b5SBarry Smith    Level: intermediate
1731184914b5SBarry Smith 
1732184914b5SBarry Smith    Notes: Can only be called after the call the SNESSolve() is complete.
1733184914b5SBarry Smith 
1734184914b5SBarry Smith .keywords: SNES, nonlinear, set, convergence, test
1735184914b5SBarry Smith 
1736184914b5SBarry Smith .seealso: SNESSetConvergenceTest(), SNESConverged_EQ_LS(), SNESConverged_EQ_TR(),
1737184914b5SBarry Smith           SNESConverged_UM_LS(), SNESConverged_UM_TR()
1738184914b5SBarry Smith @*/
1739184914b5SBarry Smith int SNESGetConvergedReason(SNES snes,SNESConvergedReason *reason)
1740184914b5SBarry Smith {
1741184914b5SBarry Smith   PetscFunctionBegin;
1742184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1743184914b5SBarry Smith   *reason = snes->reason;
1744184914b5SBarry Smith   PetscFunctionReturn(0);
1745184914b5SBarry Smith }
1746184914b5SBarry Smith 
1747184914b5SBarry Smith #undef __FUNC__
1748b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetConvergenceHistory"
1749c9005455SLois Curfman McInnes /*@
1750c9005455SLois Curfman McInnes    SNESSetConvergenceHistory - Sets the array used to hold the convergence history.
1751c9005455SLois Curfman McInnes 
1752fee21e36SBarry Smith    Collective on SNES
1753fee21e36SBarry Smith 
1754c7afd0dbSLois Curfman McInnes    Input Parameters:
1755c7afd0dbSLois Curfman McInnes +  snes - iterative context obtained from SNESCreate()
1756c7afd0dbSLois Curfman McInnes .  a   - array to hold history
1757758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
1758758f92a0SBarry Smith          negative if not converged) for each solve.
1759758f92a0SBarry Smith .  na  - size of a and its
1760758f92a0SBarry Smith -  reset - PETSC_TRUTH indicates each new nonlinear solve resets the history counter to zero,
1761758f92a0SBarry Smith            else it continues storing new values for new nonlinear solves after the old ones
1762c7afd0dbSLois Curfman McInnes 
1763c9005455SLois Curfman McInnes    Notes:
1764c9005455SLois Curfman McInnes    If set, this array will contain the function norms (for
1765c9005455SLois Curfman McInnes    SNES_NONLINEAR_EQUATIONS methods) or gradient norms
1766c9005455SLois Curfman McInnes    (for SNES_UNCONSTRAINED_MINIMIZATION methods) computed
1767c9005455SLois Curfman McInnes    at each step.
1768c9005455SLois Curfman McInnes 
1769c9005455SLois Curfman McInnes    This routine is useful, e.g., when running a code for purposes
1770c9005455SLois Curfman McInnes    of accurate performance monitoring, when no I/O should be done
1771c9005455SLois Curfman McInnes    during the section of code that is being timed.
1772c9005455SLois Curfman McInnes 
177336851e7fSLois Curfman McInnes    Level: intermediate
177436851e7fSLois Curfman McInnes 
1775c9005455SLois Curfman McInnes .keywords: SNES, set, convergence, history
1776758f92a0SBarry Smith 
177708405cd6SLois Curfman McInnes .seealso: SNESGetConvergenceHistory()
1778758f92a0SBarry Smith 
1779c9005455SLois Curfman McInnes @*/
1780329f5518SBarry Smith int SNESSetConvergenceHistory(SNES snes,PetscReal *a,int *its,int na,PetscTruth reset)
1781c9005455SLois Curfman McInnes {
17823a40ed3dSBarry Smith   PetscFunctionBegin;
1783c9005455SLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1784c9005455SLois Curfman McInnes   if (na) PetscValidScalarPointer(a);
1785c9005455SLois Curfman McInnes   snes->conv_hist       = a;
1786758f92a0SBarry Smith   snes->conv_hist_its   = its;
1787758f92a0SBarry Smith   snes->conv_hist_max   = na;
1788758f92a0SBarry Smith   snes->conv_hist_reset = reset;
1789758f92a0SBarry Smith   PetscFunctionReturn(0);
1790758f92a0SBarry Smith }
1791758f92a0SBarry Smith 
1792758f92a0SBarry Smith #undef __FUNC__
1793b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetConvergenceHistory"
17940c4c9dddSBarry Smith /*@C
1795758f92a0SBarry Smith    SNESGetConvergenceHistory - Gets the array used to hold the convergence history.
1796758f92a0SBarry Smith 
1797758f92a0SBarry Smith    Collective on SNES
1798758f92a0SBarry Smith 
1799758f92a0SBarry Smith    Input Parameter:
1800758f92a0SBarry Smith .  snes - iterative context obtained from SNESCreate()
1801758f92a0SBarry Smith 
1802758f92a0SBarry Smith    Output Parameters:
1803758f92a0SBarry Smith .  a   - array to hold history
1804758f92a0SBarry Smith .  its - integer array holds the number of linear iterations (or
1805758f92a0SBarry Smith          negative if not converged) for each solve.
1806758f92a0SBarry Smith -  na  - size of a and its
1807758f92a0SBarry Smith 
1808758f92a0SBarry Smith    Notes:
1809758f92a0SBarry Smith     The calling sequence for this routine in Fortran is
1810758f92a0SBarry Smith $   call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr)
1811758f92a0SBarry Smith 
1812758f92a0SBarry Smith    This routine is useful, e.g., when running a code for purposes
1813758f92a0SBarry Smith    of accurate performance monitoring, when no I/O should be done
1814758f92a0SBarry Smith    during the section of code that is being timed.
1815758f92a0SBarry Smith 
1816758f92a0SBarry Smith    Level: intermediate
1817758f92a0SBarry Smith 
1818758f92a0SBarry Smith .keywords: SNES, get, convergence, history
1819758f92a0SBarry Smith 
1820758f92a0SBarry Smith .seealso: SNESSetConvergencHistory()
1821758f92a0SBarry Smith 
1822758f92a0SBarry Smith @*/
1823329f5518SBarry Smith int SNESGetConvergenceHistory(SNES snes,PetscReal **a,int **its,int *na)
1824758f92a0SBarry Smith {
1825758f92a0SBarry Smith   PetscFunctionBegin;
1826758f92a0SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1827758f92a0SBarry Smith   if (a)   *a   = snes->conv_hist;
1828758f92a0SBarry Smith   if (its) *its = snes->conv_hist_its;
1829758f92a0SBarry Smith   if (na) *na   = snes->conv_hist_len;
18303a40ed3dSBarry Smith   PetscFunctionReturn(0);
1831c9005455SLois Curfman McInnes }
1832c9005455SLois Curfman McInnes 
1833c9005455SLois Curfman McInnes #undef __FUNC__
1834b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESScaleStep_Private"
18359b94acceSBarry Smith /*
18369b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
18379b94acceSBarry Smith    positive parameter delta.
18389b94acceSBarry Smith 
18399b94acceSBarry Smith     Input Parameters:
1840c7afd0dbSLois Curfman McInnes +   snes - the SNES context
18419b94acceSBarry Smith .   y - approximate solution of linear system
18429b94acceSBarry Smith .   fnorm - 2-norm of current function
1843c7afd0dbSLois Curfman McInnes -   delta - trust region size
18449b94acceSBarry Smith 
18459b94acceSBarry Smith     Output Parameters:
1846c7afd0dbSLois Curfman McInnes +   gpnorm - predicted function norm at the new point, assuming local
18479b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
18489b94acceSBarry Smith     region, and exceeds zero otherwise.
1849c7afd0dbSLois Curfman McInnes -   ynorm - 2-norm of the step
18509b94acceSBarry Smith 
18519b94acceSBarry Smith     Note:
18526831982aSBarry Smith     For non-trust region methods such as SNESEQLS, the parameter delta
18539b94acceSBarry Smith     is set to be the maximum allowable step size.
18549b94acceSBarry Smith 
18559b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
18569b94acceSBarry Smith */
1857329f5518SBarry Smith int SNESScaleStep_Private(SNES snes,Vec y,PetscReal *fnorm,PetscReal *delta,
1858329f5518SBarry Smith                           PetscReal *gpnorm,PetscReal *ynorm)
18599b94acceSBarry Smith {
1860329f5518SBarry Smith   PetscReal norm;
18619b94acceSBarry Smith   Scalar cnorm;
18623a40ed3dSBarry Smith   int    ierr;
18633a40ed3dSBarry Smith 
18643a40ed3dSBarry Smith   PetscFunctionBegin;
1865184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1866184914b5SBarry Smith   PetscValidHeaderSpecific(y,VEC_COOKIE);
1867184914b5SBarry Smith   PetscCheckSameComm(snes,y);
1868184914b5SBarry Smith 
18693a40ed3dSBarry Smith   ierr = VecNorm(y,NORM_2,&norm);CHKERRQ(ierr);
18709b94acceSBarry Smith   if (norm > *delta) {
18719b94acceSBarry Smith      norm = *delta/norm;
18729b94acceSBarry Smith      *gpnorm = (1.0 - norm)*(*fnorm);
18739b94acceSBarry Smith      cnorm = norm;
1874329f5518SBarry Smith      ierr = VecScale(&cnorm,y);CHKERRQ(ierr);
18759b94acceSBarry Smith      *ynorm = *delta;
18769b94acceSBarry Smith   } else {
18779b94acceSBarry Smith      *gpnorm = 0.0;
18789b94acceSBarry Smith      *ynorm = norm;
18799b94acceSBarry Smith   }
18803a40ed3dSBarry Smith   PetscFunctionReturn(0);
18819b94acceSBarry Smith }
18829b94acceSBarry Smith 
18835615d1e5SSatish Balay #undef __FUNC__
1884b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSolve"
18859b94acceSBarry Smith /*@
18869b94acceSBarry Smith    SNESSolve - Solves a nonlinear system.  Call SNESSolve after calling
188763a78c88SLois Curfman McInnes    SNESCreate() and optional routines of the form SNESSetXXX().
18889b94acceSBarry Smith 
1889c7afd0dbSLois Curfman McInnes    Collective on SNES
1890c7afd0dbSLois Curfman McInnes 
1891b2002411SLois Curfman McInnes    Input Parameters:
1892c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1893c7afd0dbSLois Curfman McInnes -  x - the solution vector
18949b94acceSBarry Smith 
18959b94acceSBarry Smith    Output Parameter:
1896b2002411SLois Curfman McInnes .  its - number of iterations until termination
18979b94acceSBarry Smith 
1898b2002411SLois Curfman McInnes    Notes:
18998ddd3da0SLois Curfman McInnes    The user should initialize the vector,x, with the initial guess
19008ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
19018ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
19028ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
19038ddd3da0SLois Curfman McInnes 
190436851e7fSLois Curfman McInnes    Level: beginner
190536851e7fSLois Curfman McInnes 
19069b94acceSBarry Smith .keywords: SNES, nonlinear, solve
19079b94acceSBarry Smith 
190863a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESDestroy()
19099b94acceSBarry Smith @*/
19108ddd3da0SLois Curfman McInnes int SNESSolve(SNES snes,Vec x,int *its)
19119b94acceSBarry Smith {
1912f1af5d2fSBarry Smith   int        ierr;
1913f1af5d2fSBarry Smith   PetscTruth flg;
1914052efed2SBarry Smith 
19153a40ed3dSBarry Smith   PetscFunctionBegin;
191677c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1917184914b5SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
1918184914b5SBarry Smith   PetscCheckSameComm(snes,x);
191974679c65SBarry Smith   PetscValidIntPointer(its);
192074637425SBarry Smith   if (!snes->solve) SETERRQ(1,1,"SNESSetType() or SNESSetFromOptions() must be called before SNESSolve()");
192174637425SBarry Smith 
192282bf6240SBarry Smith   if (!snes->setupcalled) {ierr = SNESSetUp(snes,x);CHKERRQ(ierr);}
1923c4fc05e7SBarry Smith   else {snes->vec_sol = snes->vec_sol_always = x;}
1924758f92a0SBarry Smith   if (snes->conv_hist_reset == PETSC_TRUE) snes->conv_hist_len = 0;
19259b94acceSBarry Smith   PLogEventBegin(SNES_Solve,snes,0,0,0);
1926c96a6f78SLois Curfman McInnes   snes->nfuncs = 0; snes->linear_its = 0; snes->nfailures = 0;
19279b94acceSBarry Smith   ierr = (*(snes)->solve)(snes,its);CHKERRQ(ierr);
19289b94acceSBarry Smith   PLogEventEnd(SNES_Solve,snes,0,0,0);
19290f5bd95cSBarry Smith   ierr = OptionsHasName(snes->prefix,"-snes_view",&flg);CHKERRQ(ierr);
19306d4a8577SBarry Smith   if (flg) { ierr = SNESView(snes,VIEWER_STDOUT_WORLD);CHKERRQ(ierr); }
19313a40ed3dSBarry Smith   PetscFunctionReturn(0);
19329b94acceSBarry Smith }
19339b94acceSBarry Smith 
19349b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
19359b94acceSBarry Smith 
19365615d1e5SSatish Balay #undef __FUNC__
1937b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetType"
193882bf6240SBarry Smith /*@C
19394b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
19409b94acceSBarry Smith 
1941fee21e36SBarry Smith    Collective on SNES
1942fee21e36SBarry Smith 
1943c7afd0dbSLois Curfman McInnes    Input Parameters:
1944c7afd0dbSLois Curfman McInnes +  snes - the SNES context
1945454a90a3SBarry Smith -  type - a known method
1946c7afd0dbSLois Curfman McInnes 
1947c7afd0dbSLois Curfman McInnes    Options Database Key:
1948454a90a3SBarry Smith .  -snes_type <type> - Sets the method; use -help for a list
1949c7afd0dbSLois Curfman McInnes    of available methods (for instance, ls or tr)
1950ae12b187SLois Curfman McInnes 
19519b94acceSBarry Smith    Notes:
1952e090d566SSatish Balay    See "petsc/include/petscsnes.h" for available methods (for instance)
19536831982aSBarry Smith +    SNESEQLS - Newton's method with line search
1954c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
19556831982aSBarry Smith .    SNESEQTR - Newton's method with trust region
1956c7afd0dbSLois Curfman McInnes      (systems of nonlinear equations)
19576831982aSBarry Smith .    SNESUMTR - Newton's method with trust region
1958c7afd0dbSLois Curfman McInnes      (unconstrained minimization)
19596831982aSBarry Smith -    SNESUMLS - Newton's method with line search
1960c7afd0dbSLois Curfman McInnes      (unconstrained minimization)
19619b94acceSBarry Smith 
1962ae12b187SLois Curfman McInnes   Normally, it is best to use the SNESSetFromOptions() command and then
1963ae12b187SLois Curfman McInnes   set the SNES solver type from the options database rather than by using
1964ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
1965ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many nonlinear solvers.
1966ae12b187SLois Curfman McInnes   The SNESSetType() routine is provided for those situations where it
1967ae12b187SLois Curfman McInnes   is necessary to set the nonlinear solver independently of the command
1968ae12b187SLois Curfman McInnes   line or options database.  This might be the case, for example, when
1969ae12b187SLois Curfman McInnes   the choice of solver changes during the execution of the program,
1970ae12b187SLois Curfman McInnes   and the user's application is taking responsibility for choosing the
197136851e7fSLois Curfman McInnes   appropriate method.  In other words, this routine is not for beginners.
197236851e7fSLois Curfman McInnes 
197336851e7fSLois Curfman McInnes   Level: intermediate
1974a703fe33SLois Curfman McInnes 
1975454a90a3SBarry Smith .keywords: SNES, set, type
19769b94acceSBarry Smith @*/
1977454a90a3SBarry Smith int SNESSetType(SNES snes,SNESType type)
19789b94acceSBarry Smith {
19790f5bd95cSBarry Smith   int        ierr,(*r)(SNES);
19806831982aSBarry Smith   PetscTruth match;
19813a40ed3dSBarry Smith 
19823a40ed3dSBarry Smith   PetscFunctionBegin;
198377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
19840f5bd95cSBarry Smith   PetscValidCharPointer(type);
198582bf6240SBarry Smith 
19866831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)snes,type,&match);CHKERRQ(ierr);
19870f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
198882bf6240SBarry Smith 
198982bf6240SBarry Smith   if (snes->setupcalled) {
1990e1311b90SBarry Smith     ierr       = (*(snes)->destroy)(snes);CHKERRQ(ierr);
199182bf6240SBarry Smith     snes->data = 0;
199282bf6240SBarry Smith   }
19937d1a2b2bSBarry Smith 
19949b94acceSBarry Smith   /* Get the function pointers for the iterative method requested */
199582bf6240SBarry Smith   if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
199682bf6240SBarry Smith 
1997454a90a3SBarry Smith   ierr =  FListFind(snes->comm,SNESList,type,(int (**)(void *)) &r);CHKERRQ(ierr);
199882bf6240SBarry Smith 
1999454a90a3SBarry Smith   if (!r) SETERRQ1(1,1,"Unable to find requested SNES type %s",type);
20001548b14aSBarry Smith 
2001606d414cSSatish Balay   if (snes->data) {ierr = PetscFree(snes->data);CHKERRQ(ierr);}
200282bf6240SBarry Smith   snes->data = 0;
20033a40ed3dSBarry Smith   ierr = (*r)(snes);CHKERRQ(ierr);
200482bf6240SBarry Smith 
2005454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)snes,type);CHKERRQ(ierr);
200682bf6240SBarry Smith   snes->set_method_called = 1;
200782bf6240SBarry Smith 
20083a40ed3dSBarry Smith   PetscFunctionReturn(0);
20099b94acceSBarry Smith }
20109b94acceSBarry Smith 
2011a847f771SSatish Balay 
20129b94acceSBarry Smith /* --------------------------------------------------------------------- */
20135615d1e5SSatish Balay #undef __FUNC__
2014b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESRegisterDestroy"
20159b94acceSBarry Smith /*@C
20169b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
2017f1af5d2fSBarry Smith    registered by SNESRegisterDynamic().
20189b94acceSBarry Smith 
2019fee21e36SBarry Smith    Not Collective
2020fee21e36SBarry Smith 
202136851e7fSLois Curfman McInnes    Level: advanced
202236851e7fSLois Curfman McInnes 
20239b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
20249b94acceSBarry Smith 
20259b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
20269b94acceSBarry Smith @*/
2027cf256101SBarry Smith int SNESRegisterDestroy(void)
20289b94acceSBarry Smith {
202982bf6240SBarry Smith   int ierr;
203082bf6240SBarry Smith 
20313a40ed3dSBarry Smith   PetscFunctionBegin;
203282bf6240SBarry Smith   if (SNESList) {
20331d1367b7SBarry Smith     ierr = FListDestroy(&SNESList);CHKERRQ(ierr);
203482bf6240SBarry Smith     SNESList = 0;
20359b94acceSBarry Smith   }
20364c49b128SBarry Smith   SNESRegisterAllCalled = PETSC_FALSE;
20373a40ed3dSBarry Smith   PetscFunctionReturn(0);
20389b94acceSBarry Smith }
20399b94acceSBarry Smith 
20405615d1e5SSatish Balay #undef __FUNC__
2041b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetType"
20429b94acceSBarry Smith /*@C
20439a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
20449b94acceSBarry Smith 
2045c7afd0dbSLois Curfman McInnes    Not Collective
2046c7afd0dbSLois Curfman McInnes 
20479b94acceSBarry Smith    Input Parameter:
20484b0e389bSBarry Smith .  snes - nonlinear solver context
20499b94acceSBarry Smith 
20509b94acceSBarry Smith    Output Parameter:
2051454a90a3SBarry Smith .  type - SNES method (a charactor string)
20529b94acceSBarry Smith 
205336851e7fSLois Curfman McInnes    Level: intermediate
205436851e7fSLois Curfman McInnes 
2055454a90a3SBarry Smith .keywords: SNES, nonlinear, get, type, name
20569b94acceSBarry Smith @*/
2057454a90a3SBarry Smith int SNESGetType(SNES snes,SNESType *type)
20589b94acceSBarry Smith {
20593a40ed3dSBarry Smith   PetscFunctionBegin;
2060184914b5SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2061454a90a3SBarry Smith   *type = snes->type_name;
20623a40ed3dSBarry Smith   PetscFunctionReturn(0);
20639b94acceSBarry Smith }
20649b94acceSBarry Smith 
20655615d1e5SSatish Balay #undef __FUNC__
2066b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetSolution"
20679b94acceSBarry Smith /*@C
20689b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
20699b94acceSBarry Smith    stored.
20709b94acceSBarry Smith 
2071c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2072c7afd0dbSLois Curfman McInnes 
20739b94acceSBarry Smith    Input Parameter:
20749b94acceSBarry Smith .  snes - the SNES context
20759b94acceSBarry Smith 
20769b94acceSBarry Smith    Output Parameter:
20779b94acceSBarry Smith .  x - the solution
20789b94acceSBarry Smith 
207936851e7fSLois Curfman McInnes    Level: advanced
208036851e7fSLois Curfman McInnes 
20819b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
20829b94acceSBarry Smith 
2083a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESGetGradient(), SNESGetSolutionUpdate()
20849b94acceSBarry Smith @*/
20859b94acceSBarry Smith int SNESGetSolution(SNES snes,Vec *x)
20869b94acceSBarry Smith {
20873a40ed3dSBarry Smith   PetscFunctionBegin;
208877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
20899b94acceSBarry Smith   *x = snes->vec_sol_always;
20903a40ed3dSBarry Smith   PetscFunctionReturn(0);
20919b94acceSBarry Smith }
20929b94acceSBarry Smith 
20935615d1e5SSatish Balay #undef __FUNC__
2094b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetSolutionUpdate"
20959b94acceSBarry Smith /*@C
20969b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
20979b94acceSBarry Smith    stored.
20989b94acceSBarry Smith 
2099c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2100c7afd0dbSLois Curfman McInnes 
21019b94acceSBarry Smith    Input Parameter:
21029b94acceSBarry Smith .  snes - the SNES context
21039b94acceSBarry Smith 
21049b94acceSBarry Smith    Output Parameter:
21059b94acceSBarry Smith .  x - the solution update
21069b94acceSBarry Smith 
210736851e7fSLois Curfman McInnes    Level: advanced
210836851e7fSLois Curfman McInnes 
21099b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
21109b94acceSBarry Smith 
21119b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction
21129b94acceSBarry Smith @*/
21139b94acceSBarry Smith int SNESGetSolutionUpdate(SNES snes,Vec *x)
21149b94acceSBarry Smith {
21153a40ed3dSBarry Smith   PetscFunctionBegin;
211677c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
21179b94acceSBarry Smith   *x = snes->vec_sol_update_always;
21183a40ed3dSBarry Smith   PetscFunctionReturn(0);
21199b94acceSBarry Smith }
21209b94acceSBarry Smith 
21215615d1e5SSatish Balay #undef __FUNC__
2122b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetFunction"
21239b94acceSBarry Smith /*@C
21243638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
21259b94acceSBarry Smith 
2126c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2127c7afd0dbSLois Curfman McInnes 
21289b94acceSBarry Smith    Input Parameter:
21299b94acceSBarry Smith .  snes - the SNES context
21309b94acceSBarry Smith 
21319b94acceSBarry Smith    Output Parameter:
21327bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
213300036973SBarry Smith .  ctx - the function context (or PETSC_NULL)
213400036973SBarry Smith -  func - the function (or PETSC_NULL)
21359b94acceSBarry Smith 
21369b94acceSBarry Smith    Notes:
21379b94acceSBarry Smith    SNESGetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only
21389b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
21399b94acceSBarry Smith    SNESGetMinimizationFunction() and SNESGetGradient();
21409b94acceSBarry Smith 
214136851e7fSLois Curfman McInnes    Level: advanced
214236851e7fSLois Curfman McInnes 
2143a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
21449b94acceSBarry Smith 
214531615d04SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution(), SNESGetMinimizationFunction(),
214631615d04SLois Curfman McInnes           SNESGetGradient()
21477bf4e008SBarry Smith 
21489b94acceSBarry Smith @*/
214900036973SBarry Smith int SNESGetFunction(SNES snes,Vec *r,void **ctx,int (**func)(SNES,Vec,Vec,void*))
21509b94acceSBarry Smith {
21513a40ed3dSBarry Smith   PetscFunctionBegin;
215277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2153a8c6a408SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
2154a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only");
2155a8c6a408SBarry Smith   }
21567bf4e008SBarry Smith   if (r)    *r    = snes->vec_func_always;
21577bf4e008SBarry Smith   if (ctx)  *ctx  = snes->funP;
215800036973SBarry Smith   if (func) *func = snes->computefunction;
21593a40ed3dSBarry Smith   PetscFunctionReturn(0);
21609b94acceSBarry Smith }
21619b94acceSBarry Smith 
21625615d1e5SSatish Balay #undef __FUNC__
2163b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetGradient"
21649b94acceSBarry Smith /*@C
21653638b69dSLois Curfman McInnes    SNESGetGradient - Returns the vector where the gradient is stored.
21669b94acceSBarry Smith 
2167c7afd0dbSLois Curfman McInnes    Not Collective, but Vec is parallel if SNES is parallel
2168c7afd0dbSLois Curfman McInnes 
21699b94acceSBarry Smith    Input Parameter:
21709b94acceSBarry Smith .  snes - the SNES context
21719b94acceSBarry Smith 
21729b94acceSBarry Smith    Output Parameter:
21737bf4e008SBarry Smith +  r - the gradient (or PETSC_NULL)
21747bf4e008SBarry Smith -  ctx - the gradient context (or PETSC_NULL)
21759b94acceSBarry Smith 
21769b94acceSBarry Smith    Notes:
21779b94acceSBarry Smith    SNESGetGradient() is valid for SNES_UNCONSTRAINED_MINIMIZATION methods
21789b94acceSBarry Smith    only.  An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
21799b94acceSBarry Smith    SNESGetFunction().
21809b94acceSBarry Smith 
218136851e7fSLois Curfman McInnes    Level: advanced
218236851e7fSLois Curfman McInnes 
21839b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient
21849b94acceSBarry Smith 
21857bf4e008SBarry Smith .seealso: SNESGetMinimizationFunction(), SNESGetSolution(), SNESGetFunction(),
21867bf4e008SBarry Smith           SNESSetGradient(), SNESSetFunction()
21877bf4e008SBarry Smith 
21889b94acceSBarry Smith @*/
21897bf4e008SBarry Smith int SNESGetGradient(SNES snes,Vec *r,void **ctx)
21909b94acceSBarry Smith {
21913a40ed3dSBarry Smith   PetscFunctionBegin;
219277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
21933a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
2194a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
21953a40ed3dSBarry Smith   }
21967bf4e008SBarry Smith   if (r)   *r = snes->vec_func_always;
21977bf4e008SBarry Smith   if (ctx) *ctx = snes->funP;
21983a40ed3dSBarry Smith   PetscFunctionReturn(0);
21999b94acceSBarry Smith }
22009b94acceSBarry Smith 
22015615d1e5SSatish Balay #undef __FUNC__
2202b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetMinimizationFunction"
22037bf4e008SBarry Smith /*@C
22049b94acceSBarry Smith    SNESGetMinimizationFunction - Returns the scalar function value for
22059b94acceSBarry Smith    unconstrained minimization problems.
22069b94acceSBarry Smith 
2207c7afd0dbSLois Curfman McInnes    Not Collective
2208c7afd0dbSLois Curfman McInnes 
22099b94acceSBarry Smith    Input Parameter:
22109b94acceSBarry Smith .  snes - the SNES context
22119b94acceSBarry Smith 
22129b94acceSBarry Smith    Output Parameter:
22137bf4e008SBarry Smith +  r - the function (or PETSC_NULL)
22147bf4e008SBarry Smith -  ctx - the function context (or PETSC_NULL)
22159b94acceSBarry Smith 
22169b94acceSBarry Smith    Notes:
22179b94acceSBarry Smith    SNESGetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
22189b94acceSBarry Smith    methods only.  An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
22199b94acceSBarry Smith    SNESGetFunction().
22209b94acceSBarry Smith 
222136851e7fSLois Curfman McInnes    Level: advanced
222236851e7fSLois Curfman McInnes 
22239b94acceSBarry Smith .keywords: SNES, nonlinear, get, function
22249b94acceSBarry Smith 
22257bf4e008SBarry Smith .seealso: SNESGetGradient(), SNESGetSolution(), SNESGetFunction(), SNESSetFunction()
22267bf4e008SBarry Smith 
22279b94acceSBarry Smith @*/
2228329f5518SBarry Smith int SNESGetMinimizationFunction(SNES snes,PetscReal *r,void **ctx)
22299b94acceSBarry Smith {
22303a40ed3dSBarry Smith   PetscFunctionBegin;
223177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
223274679c65SBarry Smith   PetscValidScalarPointer(r);
22333a40ed3dSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
2234a8c6a408SBarry Smith     SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only");
22353a40ed3dSBarry Smith   }
22367bf4e008SBarry Smith   if (r)   *r = snes->fc;
22377bf4e008SBarry Smith   if (ctx) *ctx = snes->umfunP;
22383a40ed3dSBarry Smith   PetscFunctionReturn(0);
22399b94acceSBarry Smith }
22409b94acceSBarry Smith 
22415615d1e5SSatish Balay #undef __FUNC__
2242b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESSetOptionsPrefix"
22433c7409f5SSatish Balay /*@C
22443c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
2245d850072dSLois Curfman McInnes    SNES options in the database.
22463c7409f5SSatish Balay 
2247fee21e36SBarry Smith    Collective on SNES
2248fee21e36SBarry Smith 
2249c7afd0dbSLois Curfman McInnes    Input Parameter:
2250c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2251c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2252c7afd0dbSLois Curfman McInnes 
2253d850072dSLois Curfman McInnes    Notes:
2254a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2255c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2256d850072dSLois Curfman McInnes 
225736851e7fSLois Curfman McInnes    Level: advanced
225836851e7fSLois Curfman McInnes 
22593c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
2260a86d99e1SLois Curfman McInnes 
2261a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
22623c7409f5SSatish Balay @*/
22633c7409f5SSatish Balay int SNESSetOptionsPrefix(SNES snes,char *prefix)
22643c7409f5SSatish Balay {
22653c7409f5SSatish Balay   int ierr;
22663c7409f5SSatish Balay 
22673a40ed3dSBarry Smith   PetscFunctionBegin;
226877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2269639f9d9dSBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
22703c7409f5SSatish Balay   ierr = SLESSetOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr);
22713a40ed3dSBarry Smith   PetscFunctionReturn(0);
22723c7409f5SSatish Balay }
22733c7409f5SSatish Balay 
22745615d1e5SSatish Balay #undef __FUNC__
2275b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESAppendOptionsPrefix"
22763c7409f5SSatish Balay /*@C
2277f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
2278d850072dSLois Curfman McInnes    SNES options in the database.
22793c7409f5SSatish Balay 
2280fee21e36SBarry Smith    Collective on SNES
2281fee21e36SBarry Smith 
2282c7afd0dbSLois Curfman McInnes    Input Parameters:
2283c7afd0dbSLois Curfman McInnes +  snes - the SNES context
2284c7afd0dbSLois Curfman McInnes -  prefix - the prefix to prepend to all option names
2285c7afd0dbSLois Curfman McInnes 
2286d850072dSLois Curfman McInnes    Notes:
2287a83b1b31SSatish Balay    A hyphen (-) must NOT be given at the beginning of the prefix name.
2288c7afd0dbSLois Curfman McInnes    The first character of all runtime options is AUTOMATICALLY the hyphen.
2289d850072dSLois Curfman McInnes 
229036851e7fSLois Curfman McInnes    Level: advanced
229136851e7fSLois Curfman McInnes 
22923c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
2293a86d99e1SLois Curfman McInnes 
2294a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
22953c7409f5SSatish Balay @*/
22963c7409f5SSatish Balay int SNESAppendOptionsPrefix(SNES snes,char *prefix)
22973c7409f5SSatish Balay {
22983c7409f5SSatish Balay   int ierr;
22993c7409f5SSatish Balay 
23003a40ed3dSBarry Smith   PetscFunctionBegin;
230177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2302639f9d9dSBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
23033c7409f5SSatish Balay   ierr = SLESAppendOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr);
23043a40ed3dSBarry Smith   PetscFunctionReturn(0);
23053c7409f5SSatish Balay }
23063c7409f5SSatish Balay 
23075615d1e5SSatish Balay #undef __FUNC__
2308b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESGetOptionsPrefix"
23099ab63eb5SSatish Balay /*@C
23103c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
23113c7409f5SSatish Balay    SNES options in the database.
23123c7409f5SSatish Balay 
2313c7afd0dbSLois Curfman McInnes    Not Collective
2314c7afd0dbSLois Curfman McInnes 
23153c7409f5SSatish Balay    Input Parameter:
23163c7409f5SSatish Balay .  snes - the SNES context
23173c7409f5SSatish Balay 
23183c7409f5SSatish Balay    Output Parameter:
23193c7409f5SSatish Balay .  prefix - pointer to the prefix string used
23203c7409f5SSatish Balay 
23219ab63eb5SSatish Balay    Notes: On the fortran side, the user should pass in a string 'prifix' of
23229ab63eb5SSatish Balay    sufficient length to hold the prefix.
23239ab63eb5SSatish Balay 
232436851e7fSLois Curfman McInnes    Level: advanced
232536851e7fSLois Curfman McInnes 
23263c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
2327a86d99e1SLois Curfman McInnes 
2328a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
23293c7409f5SSatish Balay @*/
23303c7409f5SSatish Balay int SNESGetOptionsPrefix(SNES snes,char **prefix)
23313c7409f5SSatish Balay {
23323c7409f5SSatish Balay   int ierr;
23333c7409f5SSatish Balay 
23343a40ed3dSBarry Smith   PetscFunctionBegin;
233577c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2336639f9d9dSBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)snes,prefix);CHKERRQ(ierr);
23373a40ed3dSBarry Smith   PetscFunctionReturn(0);
23383c7409f5SSatish Balay }
23393c7409f5SSatish Balay 
2340acb85ae6SSatish Balay /*MC
2341f1af5d2fSBarry Smith    SNESRegisterDynamic - Adds a method to the nonlinear solver package.
23429b94acceSBarry Smith 
2343b2002411SLois Curfman McInnes    Synopsis:
2344f1af5d2fSBarry Smith    SNESRegisterDynamic(char *name_solver,char *path,char *name_create,int (*routine_create)(SNES))
23459b94acceSBarry Smith 
23468d76a1e5SLois Curfman McInnes    Not collective
23478d76a1e5SLois Curfman McInnes 
2348b2002411SLois Curfman McInnes    Input Parameters:
2349c7afd0dbSLois Curfman McInnes +  name_solver - name of a new user-defined solver
2350b2002411SLois Curfman McInnes .  path - path (either absolute or relative) the library containing this solver
2351b2002411SLois Curfman McInnes .  name_create - name of routine to create method context
2352c7afd0dbSLois Curfman McInnes -  routine_create - routine to create method context
23539b94acceSBarry Smith 
2354b2002411SLois Curfman McInnes    Notes:
2355f1af5d2fSBarry Smith    SNESRegisterDynamic() may be called multiple times to add several user-defined solvers.
23563a40ed3dSBarry Smith 
2357b2002411SLois Curfman McInnes    If dynamic libraries are used, then the fourth input argument (routine_create)
2358b2002411SLois Curfman McInnes    is ignored.
2359b2002411SLois Curfman McInnes 
23605ba88a07SLois Curfman McInnes    Environmental variables such as ${PETSC_ARCH}, ${PETSC_DIR}, ${PETSC_LDIR}, ${BOPT},
23615ba88a07SLois Curfman McInnes    and others of the form ${any_environmental_variable} occuring in pathname will be
23625ba88a07SLois Curfman McInnes    replaced with appropriate values.
23635ba88a07SLois Curfman McInnes 
2364b2002411SLois Curfman McInnes    Sample usage:
236569225d78SLois Curfman McInnes .vb
2366f1af5d2fSBarry Smith    SNESRegisterDynamic("my_solver",/home/username/my_lib/lib/libg/solaris/mylib.a,
2367b2002411SLois Curfman McInnes                 "MySolverCreate",MySolverCreate);
236869225d78SLois Curfman McInnes .ve
2369b2002411SLois Curfman McInnes 
2370b2002411SLois Curfman McInnes    Then, your solver can be chosen with the procedural interface via
2371b2002411SLois Curfman McInnes $     SNESSetType(snes,"my_solver")
2372b2002411SLois Curfman McInnes    or at runtime via the option
2373b2002411SLois Curfman McInnes $     -snes_type my_solver
2374b2002411SLois Curfman McInnes 
237536851e7fSLois Curfman McInnes    Level: advanced
237636851e7fSLois Curfman McInnes 
2377b2002411SLois Curfman McInnes .keywords: SNES, nonlinear, register
2378b2002411SLois Curfman McInnes 
2379b2002411SLois Curfman McInnes .seealso: SNESRegisterAll(), SNESRegisterDestroy()
2380b2002411SLois Curfman McInnes M*/
2381b2002411SLois Curfman McInnes 
2382b2002411SLois Curfman McInnes #undef __FUNC__
2383b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"SNESRegister"
2384f1af5d2fSBarry Smith int SNESRegister(char *sname,char *path,char *name,int (*function)(SNES))
2385b2002411SLois Curfman McInnes {
2386b2002411SLois Curfman McInnes   char fullname[256];
2387b2002411SLois Curfman McInnes   int  ierr;
2388b2002411SLois Curfman McInnes 
2389b2002411SLois Curfman McInnes   PetscFunctionBegin;
2390f1af5d2fSBarry Smith   ierr = FListConcat(path,name,fullname);CHKERRQ(ierr);
2391f1af5d2fSBarry Smith   ierr = FListAdd(&SNESList,sname,fullname,(int (*)(void*))function);CHKERRQ(ierr);
2392b2002411SLois Curfman McInnes   PetscFunctionReturn(0);
2393b2002411SLois Curfman McInnes }
2394