xref: /petsc/src/snes/interface/snes.c (revision f525115efdda27879a8ad77c9779b8a83a787982)
19b94acceSBarry Smith #ifndef lint
2*f525115eSLois Curfman McInnes static char vcid[] = "$Id: snes.c,v 1.87 1996/09/08 22:53:38 curfman Exp curfman $";
39b94acceSBarry Smith #endif
49b94acceSBarry Smith 
59b94acceSBarry Smith #include "draw.h"          /*I "draw.h"  I*/
670f55243SBarry Smith #include "src/snes/snesimpl.h"      /*I "snes.h"  I*/
7f5eb4b81SSatish Balay #include "src/sys/nreg.h"
89b94acceSBarry Smith #include "pinclude/pviewer.h"
99b94acceSBarry Smith #include <math.h>
109b94acceSBarry Smith 
11052efed2SBarry Smith extern int SNESGetTypeFromOptions_Private(SNES,SNESType*,int*);
1233455573SSatish Balay extern int SNESPrintTypes_Private(MPI_Comm,char*,char*);
139b94acceSBarry Smith 
149b94acceSBarry Smith /*@
159b94acceSBarry Smith    SNESView - Prints the SNES data structure.
169b94acceSBarry Smith 
179b94acceSBarry Smith    Input Parameters:
189b94acceSBarry Smith .  SNES - the SNES context
199b94acceSBarry Smith .  viewer - visualization context
209b94acceSBarry Smith 
219b94acceSBarry Smith    Options Database Key:
229b94acceSBarry Smith $  -snes_view : calls SNESView() at end of SNESSolve()
239b94acceSBarry Smith 
249b94acceSBarry Smith    Notes:
259b94acceSBarry Smith    The available visualization contexts include
266d4a8577SBarry Smith $     VIEWER_STDOUT_SELF - standard output (default)
276d4a8577SBarry Smith $     VIEWER_STDOUT_WORLD - synchronized standard
289b94acceSBarry Smith $       output where only the first processor opens
299b94acceSBarry Smith $       the file.  All other processors send their
309b94acceSBarry Smith $       data to the first processor to print.
319b94acceSBarry Smith 
329b94acceSBarry Smith    The user can open alternative vistualization contexts with
33dbb450caSBarry Smith $    ViewerFileOpenASCII() - output to a specified file
349b94acceSBarry Smith 
359b94acceSBarry Smith .keywords: SNES, view
369b94acceSBarry Smith 
37dbb450caSBarry Smith .seealso: ViewerFileOpenASCII()
389b94acceSBarry Smith @*/
399b94acceSBarry Smith int SNESView(SNES snes,Viewer viewer)
409b94acceSBarry Smith {
419b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
429b94acceSBarry Smith   FILE                *fd;
439b94acceSBarry Smith   int                 ierr;
449b94acceSBarry Smith   SLES                sles;
459b94acceSBarry Smith   char                *method;
4619bcc07fSBarry Smith   ViewerType          vtype;
479b94acceSBarry Smith 
4874679c65SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
4974679c65SBarry Smith   if (viewer) {PetscValidHeader(viewer);}
5074679c65SBarry Smith   else { viewer = VIEWER_STDOUT_SELF; }
5174679c65SBarry Smith 
5219bcc07fSBarry Smith   ierr = ViewerGetType(viewer,&vtype); CHKERRQ(ierr);
5319bcc07fSBarry Smith   if (vtype == ASCII_FILE_VIEWER || vtype == ASCII_FILES_VIEWER) {
5490ace30eSBarry Smith     ierr = ViewerASCIIGetPointer(viewer,&fd); CHKERRQ(ierr);
5577c4ece6SBarry Smith     PetscFPrintf(snes->comm,fd,"SNES Object:\n");
564b0e389bSBarry Smith     SNESGetType(snes,PETSC_NULL,&method);
5777c4ece6SBarry Smith     PetscFPrintf(snes->comm,fd,"  method: %s\n",method);
589b94acceSBarry Smith     if (snes->view) (*snes->view)((PetscObject)snes,viewer);
5977c4ece6SBarry Smith     PetscFPrintf(snes->comm,fd,
609b94acceSBarry Smith       "  maximum iterations=%d, maximum function evaluations=%d\n",
619b94acceSBarry Smith       snes->max_its,snes->max_funcs);
6277c4ece6SBarry Smith     PetscFPrintf(snes->comm,fd,
639b94acceSBarry Smith     "  tolerances: relative=%g, absolute=%g, truncation=%g, solution=%g\n",
649b94acceSBarry Smith       snes->rtol, snes->atol, snes->trunctol, snes->xtol);
659b94acceSBarry Smith     if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION)
6677c4ece6SBarry Smith       PetscFPrintf(snes->comm,fd,"  min function tolerance=%g\n",snes->fmin);
679b94acceSBarry Smith     if (snes->ksp_ewconv) {
689b94acceSBarry Smith       kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
699b94acceSBarry Smith       if (kctx) {
7077c4ece6SBarry Smith         PetscFPrintf(snes->comm,fd,
719b94acceSBarry Smith      "  Eisenstat-Walker computation of KSP relative tolerance (version %d)\n",
729b94acceSBarry Smith         kctx->version);
7377c4ece6SBarry Smith         PetscFPrintf(snes->comm,fd,
749b94acceSBarry Smith           "    rtol_0=%g, rtol_max=%g, threshold=%g\n",kctx->rtol_0,
759b94acceSBarry Smith           kctx->rtol_max,kctx->threshold);
7677c4ece6SBarry Smith         PetscFPrintf(snes->comm,fd,"    gamma=%g, alpha=%g, alpha2=%g\n",
779b94acceSBarry Smith           kctx->gamma,kctx->alpha,kctx->alpha2);
789b94acceSBarry Smith       }
799b94acceSBarry Smith     }
80c30f285eSLois Curfman McInnes   } else if (vtype == STRING_VIEWER) {
81c30f285eSLois Curfman McInnes     SNESGetType(snes,PETSC_NULL,&method);
82c30f285eSLois Curfman McInnes     ViewerStringSPrintf(viewer," %-3.3s",method);
8319bcc07fSBarry Smith   }
849b94acceSBarry Smith   SNESGetSLES(snes,&sles);
859b94acceSBarry Smith   ierr = SLESView(sles,viewer); CHKERRQ(ierr);
869b94acceSBarry Smith   return 0;
879b94acceSBarry Smith }
889b94acceSBarry Smith 
899b94acceSBarry Smith /*@
90682d7d0cSBarry Smith    SNESSetFromOptions - Sets various SNES and SLES parameters from user options.
919b94acceSBarry Smith 
929b94acceSBarry Smith    Input Parameter:
939b94acceSBarry Smith .  snes - the SNES context
949b94acceSBarry Smith 
959b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database
969b94acceSBarry Smith 
97a86d99e1SLois Curfman McInnes .seealso: SNESPrintHelp(), SNESSetOptionsPrefix()
989b94acceSBarry Smith @*/
999b94acceSBarry Smith int SNESSetFromOptions(SNES snes)
1009b94acceSBarry Smith {
1014b0e389bSBarry Smith   SNESType method;
1029b94acceSBarry Smith   double   tmp;
1039b94acceSBarry Smith   SLES     sles;
104d31a3109SLois Curfman McInnes   int      ierr, flg, mset = 0;
1053c7409f5SSatish Balay   int      version   = PETSC_DEFAULT;
1069b94acceSBarry Smith   double   rtol_0    = PETSC_DEFAULT;
1079b94acceSBarry Smith   double   rtol_max  = PETSC_DEFAULT;
1089b94acceSBarry Smith   double   gamma2    = PETSC_DEFAULT;
1099b94acceSBarry Smith   double   alpha     = PETSC_DEFAULT;
1109b94acceSBarry Smith   double   alpha2    = PETSC_DEFAULT;
1119b94acceSBarry Smith   double   threshold = PETSC_DEFAULT;
1129b94acceSBarry Smith 
11377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
11474679c65SBarry Smith   if (snes->setup_called) SETERRQ(1,"SNESSetFromOptions:Must call prior to SNESSetUp");
115052efed2SBarry Smith   ierr = SNESGetTypeFromOptions_Private(snes,&method,&flg); CHKERRQ(ierr);
116052efed2SBarry Smith   if (flg) {
117052efed2SBarry Smith     ierr = SNESSetType(snes,method); CHKERRQ(ierr);
1189b94acceSBarry Smith   }
1195334005bSBarry Smith   else if (!snes->set_method_called) {
1205334005bSBarry Smith     if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
12140191667SLois Curfman McInnes       ierr = SNESSetType(snes,SNES_EQ_LS); CHKERRQ(ierr);
1225334005bSBarry Smith     }
1235334005bSBarry Smith     else {
12440191667SLois Curfman McInnes       ierr = SNESSetType(snes,SNES_UM_TR); CHKERRQ(ierr);
1255334005bSBarry Smith     }
1265334005bSBarry Smith   }
1275334005bSBarry Smith 
1283c7409f5SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-help", &flg); CHKERRQ(ierr);
1293c7409f5SSatish Balay   if (flg) { SNESPrintHelp(snes); }
1303c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_stol",&tmp, &flg); CHKERRQ(ierr);
131d7a720efSLois Curfman McInnes   if (flg) {SNESSetTolerances(snes,PETSC_DEFAULT,PETSC_DEFAULT,tmp,PETSC_DEFAULT,PETSC_DEFAULT);}
1323c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_atol",&tmp, &flg); CHKERRQ(ierr);
133d7a720efSLois Curfman McInnes   if (flg) {SNESSetTolerances(snes,tmp,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);}
1343c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_rtol",&tmp, &flg);  CHKERRQ(ierr);
135d7a720efSLois Curfman McInnes   if (flg) {SNESSetTolerances(snes,PETSC_DEFAULT,tmp,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT); }
1363c7409f5SSatish Balay   ierr = OptionsGetInt(snes->prefix,"-snes_max_it",&snes->max_its, &flg); CHKERRQ(ierr);
1373c7409f5SSatish Balay   ierr = OptionsGetInt(snes->prefix,"-snes_max_funcs",&snes->max_funcs, &flg);CHKERRQ(ierr);
138d7a720efSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_trtol",&tmp, &flg); CHKERRQ(ierr);
139d7a720efSLois Curfman McInnes   if (flg) { SNESSetTrustRegionTolerance(snes,tmp); }
140d7a720efSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_fmin",&tmp, &flg); CHKERRQ(ierr);
141d7a720efSLois Curfman McInnes   if (flg) { SNESSetMinimizationFunctionTolerance(snes,tmp); }
1423c7409f5SSatish Balay   ierr = OptionsHasName(snes->prefix,"-snes_ksp_ew_conv", &flg); CHKERRQ(ierr);
1433c7409f5SSatish Balay   if (flg) { snes->ksp_ewconv = 1; }
144b18e04deSLois Curfman McInnes   ierr = OptionsGetInt(snes->prefix,"-snes_ksp_ew_version",&version,&flg); CHKERRQ(ierr);
145b18e04deSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtol0",&rtol_0,&flg); CHKERRQ(ierr);
146b18e04deSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtolmax",&rtol_max,&flg);CHKERRQ(ierr);
147b18e04deSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_gamma",&gamma2,&flg); CHKERRQ(ierr);
148b18e04deSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha",&alpha,&flg); CHKERRQ(ierr);
149b18e04deSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha2",&alpha2,&flg); CHKERRQ(ierr);
150b18e04deSLois Curfman McInnes   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_threshold",&threshold,&flg);CHKERRQ(ierr);
1519b94acceSBarry Smith   ierr = SNES_KSP_SetParametersEW(snes,version,rtol_0,rtol_max,gamma2,alpha,
1529b94acceSBarry Smith                                   alpha2,threshold); CHKERRQ(ierr);
1533c7409f5SSatish Balay   ierr = OptionsHasName(snes->prefix,"-snes_monitor",&flg); CHKERRQ(ierr);
154d31a3109SLois Curfman McInnes   if (flg) {SNESSetMonitor(snes,SNESDefaultMonitor,0); mset = 1;}
1553c7409f5SSatish Balay   ierr = OptionsHasName(snes->prefix,"-snes_smonitor",&flg); CHKERRQ(ierr);
156d31a3109SLois Curfman McInnes   if (flg) {
157d31a3109SLois Curfman McInnes    if (mset) SETERRQ(1,"SNESSetFromOptions:Monitor for SNES is already set");
158d31a3109SLois Curfman McInnes    SNESSetMonitor(snes,SNESDefaultSMonitor,0); mset = 1;
159d31a3109SLois Curfman McInnes   }
1603c7409f5SSatish Balay   ierr = OptionsHasName(snes->prefix,"-snes_xmonitor",&flg); CHKERRQ(ierr);
1613c7409f5SSatish Balay   if (flg) {
16217699dbbSLois Curfman McInnes     int    rank = 0;
163d7e8b826SBarry Smith     DrawLG lg;
164d31a3109SLois Curfman McInnes     if (mset) SETERRQ(1,"SNESSetFromOptions:Monitor for SNES is already set");
16517699dbbSLois Curfman McInnes     MPI_Initialized(&rank);
16617699dbbSLois Curfman McInnes     if (rank) MPI_Comm_rank(snes->comm,&rank);
16717699dbbSLois Curfman McInnes     if (!rank) {
1689b94acceSBarry Smith       ierr = SNESLGMonitorCreate(0,0,0,0,300,300,&lg); CHKERRQ(ierr);
1699b94acceSBarry Smith       ierr = SNESSetMonitor(snes,SNESLGMonitor,(void *)lg); CHKERRQ(ierr);
170f8c826e1SBarry Smith       snes->xmonitor = lg;
1719b94acceSBarry Smith       PLogObjectParent(snes,lg);
1729b94acceSBarry Smith     }
173d31a3109SLois Curfman McInnes     mset = 1;
1749b94acceSBarry Smith   }
1753c7409f5SSatish Balay   ierr = OptionsHasName(snes->prefix,"-snes_fd", &flg);  CHKERRQ(ierr);
1763c7409f5SSatish Balay   if (flg && snes->method_class == SNES_NONLINEAR_EQUATIONS) {
1779b94acceSBarry Smith     ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,
1789b94acceSBarry Smith                  SNESDefaultComputeJacobian,snes->funP); CHKERRQ(ierr);
17994a424c1SBarry Smith     PLogInfo(snes,
18031615d04SLois Curfman McInnes       "SNESSetFromOptions: Setting default finite difference Jacobian matrix\n");
18131615d04SLois Curfman McInnes   }
18231615d04SLois Curfman McInnes   else if (flg && snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
18331615d04SLois Curfman McInnes     ierr = SNESSetHessian(snes,snes->jacobian,snes->jacobian_pre,
18431615d04SLois Curfman McInnes                  SNESDefaultComputeHessian,snes->funP); CHKERRQ(ierr);
18594a424c1SBarry Smith     PLogInfo(snes,
18631615d04SLois Curfman McInnes       "SNESSetFromOptions: Setting default finite difference Hessian matrix\n");
1879b94acceSBarry Smith   }
1889b94acceSBarry Smith   ierr = SNESGetSLES(snes,&sles); CHKERRQ(ierr);
1899b94acceSBarry Smith   ierr = SLESSetFromOptions(sles); CHKERRQ(ierr);
1909b94acceSBarry Smith   if (!snes->setfromoptions) return 0;
1919b94acceSBarry Smith   return (*snes->setfromoptions)(snes);
1929b94acceSBarry Smith }
1939b94acceSBarry Smith 
1949b94acceSBarry Smith /*@
1959b94acceSBarry Smith    SNESPrintHelp - Prints all options for the SNES component.
1969b94acceSBarry Smith 
1979b94acceSBarry Smith    Input Parameter:
1989b94acceSBarry Smith .  snes - the SNES context
1999b94acceSBarry Smith 
200a703fe33SLois Curfman McInnes    Options Database Keys:
201a703fe33SLois Curfman McInnes $  -help, -h
202a703fe33SLois Curfman McInnes 
2039b94acceSBarry Smith .keywords: SNES, nonlinear, help
2049b94acceSBarry Smith 
205682d7d0cSBarry Smith .seealso: SNESSetFromOptions()
2069b94acceSBarry Smith @*/
2079b94acceSBarry Smith int SNESPrintHelp(SNES snes)
2089b94acceSBarry Smith {
2096daaf66cSBarry Smith   char                p[64];
2106daaf66cSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
2116daaf66cSBarry Smith 
21277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2136daaf66cSBarry Smith 
2146daaf66cSBarry Smith   PetscStrcpy(p,"-");
2156daaf66cSBarry Smith   if (snes->prefix) PetscStrcat(p, snes->prefix);
2166daaf66cSBarry Smith 
2176daaf66cSBarry Smith   kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
2186daaf66cSBarry Smith 
219d31a3109SLois Curfman McInnes   PetscPrintf(snes->comm,"SNES options ------------------------------------------------\n");
22033455573SSatish Balay   SNESPrintTypes_Private(snes->comm,p,"snes_type");
22177c4ece6SBarry Smith   PetscPrintf(snes->comm," %ssnes_view: view SNES info after each nonlinear solve\n",p);
222b18e04deSLois Curfman McInnes   PetscPrintf(snes->comm," %ssnes_max_it <its>: max iterations (default %d)\n",p,snes->max_its);
223b18e04deSLois Curfman McInnes   PetscPrintf(snes->comm," %ssnes_max_funcs <maxf>: max function evals (default %d)\n",p,snes->max_funcs);
224b18e04deSLois Curfman McInnes   PetscPrintf(snes->comm," %ssnes_stol <stol>: successive step tolerance (default %g)\n",p,snes->xtol);
225b18e04deSLois Curfman McInnes   PetscPrintf(snes->comm," %ssnes_atol <atol>: absolute tolerance (default %g)\n",p,snes->atol);
226b18e04deSLois Curfman McInnes   PetscPrintf(snes->comm," %ssnes_rtol <rtol>: relative tolerance (default %g)\n",p,snes->rtol);
227b18e04deSLois Curfman McInnes   PetscPrintf(snes->comm," %ssnes_trtol <trtol>: trust region parameter tolerance (default %g)\n",p,snes->deltatol);
228d31a3109SLois Curfman McInnes   PetscPrintf(snes->comm," SNES Monitoring Options: Choose 1 of the following\n");
229d31a3109SLois Curfman McInnes   PetscPrintf(snes->comm,"   %ssnes_monitor: use default SNES convergence monitor\n",p);
230d31a3109SLois Curfman McInnes   PetscPrintf(snes->comm,"   %ssnes_xmonitor [x,y,w,h]: use X graphics convergence monitor\n",p);
231b18e04deSLois Curfman McInnes   if (snes->type == SNES_NONLINEAR_EQUATIONS) {
23277c4ece6SBarry Smith     PetscPrintf(snes->comm,
233d31a3109SLois Curfman McInnes      " Options for solving systems of nonlinear equations only:\n");
23477c4ece6SBarry Smith     PetscPrintf(snes->comm,"   %ssnes_fd: use finite differences for Jacobian\n",p);
23577c4ece6SBarry Smith     PetscPrintf(snes->comm,"   %ssnes_mf: use matrix-free Jacobian\n",p);
236ef1dfb11SLois Curfman McInnes     PetscPrintf(snes->comm,"   %ssnes_mf_operator:use matrix-free Jacobian and user-provided preconditioning matrix\n",p);
2371650c7b0SLois Curfman McInnes     PetscPrintf(snes->comm,"   %ssnes_mf_err: relative error in function evaluation for matrix-free Jacobian\n",p);
2381650c7b0SLois Curfman McInnes     PetscPrintf(snes->comm,"   %ssnes_mf_umin: minimum iterate parameter for matrix-free Jacobian\n",p);
23977c4ece6SBarry Smith     PetscPrintf(snes->comm,"   %ssnes_ksp_ew_conv: use Eisenstat-Walker computation of KSP rtol. Params are:\n",p);
24077c4ece6SBarry Smith     PetscPrintf(snes->comm,
241b18e04deSLois Curfman McInnes      "     %ssnes_ksp_ew_version <version> (1 or 2, default is %d)\n",p,kctx->version);
24277c4ece6SBarry Smith     PetscPrintf(snes->comm,
243b18e04deSLois Curfman McInnes      "     %ssnes_ksp_ew_rtol0 <rtol0> (0 <= rtol0 < 1, default %g)\n",p,kctx->rtol_0);
24477c4ece6SBarry Smith     PetscPrintf(snes->comm,
245b18e04deSLois Curfman McInnes      "     %ssnes_ksp_ew_rtolmax <rtolmax> (0 <= rtolmax < 1, default %g)\n",p,kctx->rtol_max);
24677c4ece6SBarry Smith     PetscPrintf(snes->comm,
247b18e04deSLois Curfman McInnes      "     %ssnes_ksp_ew_gamma <gamma> (0 <= gamma <= 1, default %g)\n",p,kctx->gamma);
24877c4ece6SBarry Smith     PetscPrintf(snes->comm,
249b18e04deSLois Curfman McInnes      "     %ssnes_ksp_ew_alpha <alpha> (1 < alpha <= 2, default %g)\n",p,kctx->alpha);
25077c4ece6SBarry Smith     PetscPrintf(snes->comm,
251b18e04deSLois Curfman McInnes      "     %ssnes_ksp_ew_alpha2 <alpha2> (default %g)\n",p,kctx->alpha2);
25277c4ece6SBarry Smith     PetscPrintf(snes->comm,
253b18e04deSLois Curfman McInnes      "     %ssnes_ksp_ew_threshold <threshold> (0 < threshold < 1, default %g)\n",p,kctx->threshold);
254b18e04deSLois Curfman McInnes   }
255b18e04deSLois Curfman McInnes   else if (snes->type == SNES_UNCONSTRAINED_MINIMIZATION) {
25677c4ece6SBarry Smith     PetscPrintf(snes->comm,
257d31a3109SLois Curfman McInnes      " Options for solving unconstrained minimization problems only:\n");
258b18e04deSLois Curfman McInnes     PetscPrintf(snes->comm,"   %ssnes_fmin <ftol>: minimum function tolerance (default %g)\n",p,snes->fmin);
25977c4ece6SBarry Smith     PetscPrintf(snes->comm,"   %ssnes_fd: use finite differences for Hessian\n",p);
26077c4ece6SBarry Smith     PetscPrintf(snes->comm,"   %ssnes_mf: use matrix-free Hessian\n",p);
261d31a3109SLois Curfman McInnes     PetscPrintf(snes->comm,"   %ssnes_mf_err: relative error in gradient evaluation for matrix-free Hessian\n",p);
262d31a3109SLois Curfman McInnes      PetscPrintf(snes->comm,"   %ssnes_mf_umin: minimum iterate parameter for matrix-free Hessian\n",p);
263b18e04deSLois Curfman McInnes   }
2644537a946SLois Curfman McInnes   PetscPrintf(snes->comm," Run program with -help %ssnes_type <method> for help on ",p);
26577c4ece6SBarry Smith   PetscPrintf(snes->comm,"a particular method\n");
2666daaf66cSBarry Smith   if (snes->printhelp) (*snes->printhelp)(snes,p);
2679b94acceSBarry Smith   return 0;
2689b94acceSBarry Smith }
2699b94acceSBarry Smith /*@
2709b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
2719b94acceSBarry Smith    the nonlinear solvers.
2729b94acceSBarry Smith 
2739b94acceSBarry Smith    Input Parameters:
2749b94acceSBarry Smith .  snes - the SNES context
2759b94acceSBarry Smith .  usrP - optional user context
2769b94acceSBarry Smith 
2779b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
2789b94acceSBarry Smith 
2799b94acceSBarry Smith .seealso: SNESGetApplicationContext()
2809b94acceSBarry Smith @*/
2819b94acceSBarry Smith int SNESSetApplicationContext(SNES snes,void *usrP)
2829b94acceSBarry Smith {
28377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2849b94acceSBarry Smith   snes->user		= usrP;
2859b94acceSBarry Smith   return 0;
2869b94acceSBarry Smith }
28774679c65SBarry Smith 
2889b94acceSBarry Smith /*@C
2899b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
2909b94acceSBarry Smith    nonlinear solvers.
2919b94acceSBarry Smith 
2929b94acceSBarry Smith    Input Parameter:
2939b94acceSBarry Smith .  snes - SNES context
2949b94acceSBarry Smith 
2959b94acceSBarry Smith    Output Parameter:
2969b94acceSBarry Smith .  usrP - user context
2979b94acceSBarry Smith 
2989b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
2999b94acceSBarry Smith 
3009b94acceSBarry Smith .seealso: SNESSetApplicationContext()
3019b94acceSBarry Smith @*/
3029b94acceSBarry Smith int SNESGetApplicationContext( SNES snes,  void **usrP )
3039b94acceSBarry Smith {
30477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
3059b94acceSBarry Smith   *usrP = snes->user;
3069b94acceSBarry Smith   return 0;
3079b94acceSBarry Smith }
30874679c65SBarry Smith 
3099b94acceSBarry Smith /*@
3109b94acceSBarry Smith    SNESGetIterationNumber - Gets the current iteration number of the
3119b94acceSBarry Smith    nonlinear solver.
3129b94acceSBarry Smith 
3139b94acceSBarry Smith    Input Parameter:
3149b94acceSBarry Smith .  snes - SNES context
3159b94acceSBarry Smith 
3169b94acceSBarry Smith    Output Parameter:
3179b94acceSBarry Smith .  iter - iteration number
3189b94acceSBarry Smith 
3199b94acceSBarry Smith .keywords: SNES, nonlinear, get, iteration, number
3209b94acceSBarry Smith @*/
3219b94acceSBarry Smith int SNESGetIterationNumber(SNES snes,int* iter)
3229b94acceSBarry Smith {
32377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
32474679c65SBarry Smith   PetscValidIntPointer(iter);
3259b94acceSBarry Smith   *iter = snes->iter;
3269b94acceSBarry Smith   return 0;
3279b94acceSBarry Smith }
32874679c65SBarry Smith 
3299b94acceSBarry Smith /*@
3309b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
3319b94acceSBarry Smith    with SNESSSetFunction().
3329b94acceSBarry Smith 
3339b94acceSBarry Smith    Input Parameter:
3349b94acceSBarry Smith .  snes - SNES context
3359b94acceSBarry Smith 
3369b94acceSBarry Smith    Output Parameter:
3379b94acceSBarry Smith .  fnorm - 2-norm of function
3389b94acceSBarry Smith 
3399b94acceSBarry Smith    Note:
3409b94acceSBarry Smith    SNESGetFunctionNorm() is valid for SNES_NONLINEAR_EQUATIONS methods only.
341a86d99e1SLois Curfman McInnes    A related routine for SNES_UNCONSTRAINED_MINIMIZATION methods is
342a86d99e1SLois Curfman McInnes    SNESGetGradientNorm().
3439b94acceSBarry Smith 
3449b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
345a86d99e1SLois Curfman McInnes 
346a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction()
3479b94acceSBarry Smith @*/
3489b94acceSBarry Smith int SNESGetFunctionNorm(SNES snes,Scalar *fnorm)
3499b94acceSBarry Smith {
35077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
35174679c65SBarry Smith   PetscValidScalarPointer(fnorm);
35274679c65SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) {
35374679c65SBarry Smith     SETERRQ(1,"SNESGetFunctionNorm:For SNES_NONLINEAR_EQUATIONS only");
35474679c65SBarry Smith   }
3559b94acceSBarry Smith   *fnorm = snes->norm;
3569b94acceSBarry Smith   return 0;
3579b94acceSBarry Smith }
35874679c65SBarry Smith 
3599b94acceSBarry Smith /*@
3609b94acceSBarry Smith    SNESGetGradientNorm - Gets the norm of the current gradient that was set
3619b94acceSBarry Smith    with SNESSSetGradient().
3629b94acceSBarry Smith 
3639b94acceSBarry Smith    Input Parameter:
3649b94acceSBarry Smith .  snes - SNES context
3659b94acceSBarry Smith 
3669b94acceSBarry Smith    Output Parameter:
3679b94acceSBarry Smith .  fnorm - 2-norm of gradient
3689b94acceSBarry Smith 
3699b94acceSBarry Smith    Note:
3709b94acceSBarry Smith    SNESGetGradientNorm() is valid for SNES_UNCONSTRAINED_MINIMIZATION
371a86d99e1SLois Curfman McInnes    methods only.  A related routine for SNES_NONLINEAR_EQUATIONS methods
372a86d99e1SLois Curfman McInnes    is SNESGetFunctionNorm().
3739b94acceSBarry Smith 
3749b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient, norm
375a86d99e1SLois Curfman McInnes 
376a86d99e1SLois Curfman McInnes .seelso: SNESSetGradient()
3779b94acceSBarry Smith @*/
3789b94acceSBarry Smith int SNESGetGradientNorm(SNES snes,Scalar *gnorm)
3799b94acceSBarry Smith {
38077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
38174679c65SBarry Smith   PetscValidScalarPointer(gnorm);
38274679c65SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) {
38374679c65SBarry Smith     SETERRQ(1,"SNESGetGradientNorm:For SNES_UNCONSTRAINED_MINIMIZATION only");
38474679c65SBarry Smith   }
3859b94acceSBarry Smith   *gnorm = snes->norm;
3869b94acceSBarry Smith   return 0;
3879b94acceSBarry Smith }
38874679c65SBarry Smith 
3899b94acceSBarry Smith /*@
3909b94acceSBarry Smith    SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps
3919b94acceSBarry Smith    attempted by the nonlinear solver.
3929b94acceSBarry Smith 
3939b94acceSBarry Smith    Input Parameter:
3949b94acceSBarry Smith .  snes - SNES context
3959b94acceSBarry Smith 
3969b94acceSBarry Smith    Output Parameter:
3979b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
3989b94acceSBarry Smith 
3999b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
4009b94acceSBarry Smith @*/
4019b94acceSBarry Smith int SNESGetNumberUnsuccessfulSteps(SNES snes,int* nfails)
4029b94acceSBarry Smith {
40377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
40474679c65SBarry Smith   PetscValidIntPointer(nfails);
4059b94acceSBarry Smith   *nfails = snes->nfailures;
4069b94acceSBarry Smith   return 0;
4079b94acceSBarry Smith }
4089b94acceSBarry Smith /*@C
4099b94acceSBarry Smith    SNESGetSLES - Returns the SLES context for a SNES solver.
4109b94acceSBarry Smith 
4119b94acceSBarry Smith    Input Parameter:
4129b94acceSBarry Smith .  snes - the SNES context
4139b94acceSBarry Smith 
4149b94acceSBarry Smith    Output Parameter:
4159b94acceSBarry Smith .  sles - the SLES context
4169b94acceSBarry Smith 
4179b94acceSBarry Smith    Notes:
4189b94acceSBarry Smith    The user can then directly manipulate the SLES context to set various
4199b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
4209b94acceSBarry Smith    KSP and PC contexts as well.
4219b94acceSBarry Smith 
4229b94acceSBarry Smith .keywords: SNES, nonlinear, get, SLES, context
4239b94acceSBarry Smith 
4249b94acceSBarry Smith .seealso: SLESGetPC(), SLESGetKSP()
4259b94acceSBarry Smith @*/
4269b94acceSBarry Smith int SNESGetSLES(SNES snes,SLES *sles)
4279b94acceSBarry Smith {
42877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
4299b94acceSBarry Smith   *sles = snes->sles;
4309b94acceSBarry Smith   return 0;
4319b94acceSBarry Smith }
4329b94acceSBarry Smith /* -----------------------------------------------------------*/
4339b94acceSBarry Smith /*@C
4349b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
4359b94acceSBarry Smith 
4369b94acceSBarry Smith    Input Parameter:
4379b94acceSBarry Smith .  comm - MPI communicator
4389b94acceSBarry Smith .  type - type of method, one of
4399b94acceSBarry Smith $    SNES_NONLINEAR_EQUATIONS
4409b94acceSBarry Smith $      (for systems of nonlinear equations)
4419b94acceSBarry Smith $    SNES_UNCONSTRAINED_MINIMIZATION
4429b94acceSBarry Smith $      (for unconstrained minimization)
4439b94acceSBarry Smith 
4449b94acceSBarry Smith    Output Parameter:
4459b94acceSBarry Smith .  outsnes - the new SNES context
4469b94acceSBarry Smith 
447c1f60f51SBarry Smith    Options Database Key:
44819bd6747SLois Curfman McInnes $   -snes_mf - use default matrix-free Jacobian-vector products,
44919bd6747SLois Curfman McInnes $              and no preconditioning matrix
45019bd6747SLois Curfman McInnes $   -snes_mf_operator - use default matrix-free Jacobian-vector
45119bd6747SLois Curfman McInnes $             products, and a user-provided preconditioning matrix
45219bd6747SLois Curfman McInnes $             as set by SNESSetJacobian()
45319bd6747SLois Curfman McInnes $   -snes_fd - use (slow!) finite differences to compute Jacobian
454c1f60f51SBarry Smith 
4559b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
4569b94acceSBarry Smith 
45763a78c88SLois Curfman McInnes .seealso: SNESSolve(), SNESDestroy()
4589b94acceSBarry Smith @*/
4594b0e389bSBarry Smith int SNESCreate(MPI_Comm comm,SNESProblemType type,SNES *outsnes)
4609b94acceSBarry Smith {
4619b94acceSBarry Smith   int                 ierr;
4629b94acceSBarry Smith   SNES                snes;
4639b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
46437fcc0dbSBarry Smith 
4659b94acceSBarry Smith   *outsnes = 0;
4662a0acf01SLois Curfman McInnes   if (type != SNES_UNCONSTRAINED_MINIMIZATION && type != SNES_NONLINEAR_EQUATIONS)
4672a0acf01SLois Curfman McInnes     SETERRQ(1,"SNESCreate:incorrect method type");
4680452661fSBarry Smith   PetscHeaderCreate(snes,_SNES,SNES_COOKIE,SNES_UNKNOWN_METHOD,comm);
4699b94acceSBarry Smith   PLogObjectCreate(snes);
4709b94acceSBarry Smith   snes->max_its           = 50;
4719b94acceSBarry Smith   snes->max_funcs	  = 1000;
4729b94acceSBarry Smith   snes->norm		  = 0.0;
4735a2d0531SBarry Smith   if (type == SNES_UNCONSTRAINED_MINIMIZATION) {
474b18e04deSLois Curfman McInnes     snes->rtol		  = 1.e-8;
475b18e04deSLois Curfman McInnes     snes->ttol            = 0.0;
4769b94acceSBarry Smith     snes->atol		  = 1.e-10;
4775a2d0531SBarry Smith   }
478b4874afaSBarry Smith   else {
479b4874afaSBarry Smith     snes->rtol		  = 1.e-8;
480b4874afaSBarry Smith     snes->ttol            = 0.0;
481b4874afaSBarry Smith     snes->atol		  = 1.e-50;
482b4874afaSBarry Smith   }
4839b94acceSBarry Smith   snes->xtol		  = 1.e-8;
484b18e04deSLois Curfman McInnes   snes->trunctol	  = 1.e-12; /* no longer used */
4859b94acceSBarry Smith   snes->nfuncs            = 0;
4869b94acceSBarry Smith   snes->nfailures         = 0;
4879b94acceSBarry Smith   snes->monitor           = 0;
4889b94acceSBarry Smith   snes->data              = 0;
4899b94acceSBarry Smith   snes->view              = 0;
4909b94acceSBarry Smith   snes->computeumfunction = 0;
4919b94acceSBarry Smith   snes->umfunP            = 0;
4929b94acceSBarry Smith   snes->fc                = 0;
4939b94acceSBarry Smith   snes->deltatol          = 1.e-12;
4949b94acceSBarry Smith   snes->fmin              = -1.e30;
4959b94acceSBarry Smith   snes->method_class      = type;
4969b94acceSBarry Smith   snes->set_method_called = 0;
497a703fe33SLois Curfman McInnes   snes->setup_called      = 0;
4989b94acceSBarry Smith   snes->ksp_ewconv        = 0;
4997d1a2b2bSBarry Smith   snes->type              = -1;
5006f24a144SLois Curfman McInnes   snes->xmonitor          = 0;
5016f24a144SLois Curfman McInnes   snes->vwork             = 0;
5026f24a144SLois Curfman McInnes   snes->nwork             = 0;
5039b94acceSBarry Smith 
5049b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
5050452661fSBarry Smith   kctx = PetscNew(SNES_KSP_EW_ConvCtx); CHKPTRQ(kctx);
5069b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
5079b94acceSBarry Smith   kctx->version     = 2;
5089b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
5099b94acceSBarry Smith                              this was too large for some test cases */
5109b94acceSBarry Smith   kctx->rtol_last   = 0;
5119b94acceSBarry Smith   kctx->rtol_max    = .9;
5129b94acceSBarry Smith   kctx->gamma       = 1.0;
5139b94acceSBarry Smith   kctx->alpha2      = .5*(1.0 + sqrt(5.0));
5149b94acceSBarry Smith   kctx->alpha       = kctx->alpha2;
5159b94acceSBarry Smith   kctx->threshold   = .1;
5169b94acceSBarry Smith   kctx->lresid_last = 0;
5179b94acceSBarry Smith   kctx->norm_last   = 0;
5189b94acceSBarry Smith 
5199b94acceSBarry Smith   ierr = SLESCreate(comm,&snes->sles); CHKERRQ(ierr);
5209b94acceSBarry Smith   PLogObjectParent(snes,snes->sles)
5215334005bSBarry Smith 
5229b94acceSBarry Smith   *outsnes = snes;
5239b94acceSBarry Smith   return 0;
5249b94acceSBarry Smith }
5259b94acceSBarry Smith 
5269b94acceSBarry Smith /* --------------------------------------------------------------- */
5279b94acceSBarry Smith /*@C
5289b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
5299b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
5309b94acceSBarry Smith    equations.
5319b94acceSBarry Smith 
5329b94acceSBarry Smith    Input Parameters:
5339b94acceSBarry Smith .  snes - the SNES context
5349b94acceSBarry Smith .  func - function evaluation routine
5359b94acceSBarry Smith .  ctx - optional user-defined function context
5369b94acceSBarry Smith .  r - vector to store function value
5379b94acceSBarry Smith 
5389b94acceSBarry Smith    Calling sequence of func:
5399b94acceSBarry Smith .  func (SNES, Vec x, Vec f, void *ctx);
5409b94acceSBarry Smith 
5419b94acceSBarry Smith .  x - input vector
5425334005bSBarry Smith .  f - vector function
5439b94acceSBarry Smith .  ctx - optional user-defined context for private data for the
5449b94acceSBarry Smith          function evaluation routine (may be null)
5459b94acceSBarry Smith 
5469b94acceSBarry Smith    Notes:
5479b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
5489b94acceSBarry Smith $      f'(x) x = -f(x),
5499b94acceSBarry Smith $  where f'(x) denotes the Jacobian matrix and f(x) is the function.
5509b94acceSBarry Smith 
5519b94acceSBarry Smith    SNESSetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only.
5529b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
5539b94acceSBarry Smith    SNESSetMinimizationFunction() and SNESSetGradient();
5549b94acceSBarry Smith 
5559b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
5569b94acceSBarry Smith 
557a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
5589b94acceSBarry Smith @*/
5595334005bSBarry Smith int SNESSetFunction( SNES snes, Vec r, int (*func)(SNES,Vec,Vec,void*),void *ctx)
5609b94acceSBarry Smith {
56177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
5629b94acceSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1,
56348d91487SBarry Smith     "SNESSetFunction:For SNES_NONLINEAR_EQUATIONS only");
5649b94acceSBarry Smith   snes->computefunction     = func;
5659b94acceSBarry Smith   snes->vec_func            = snes->vec_func_always = r;
5669b94acceSBarry Smith   snes->funP                = ctx;
5679b94acceSBarry Smith   return 0;
5689b94acceSBarry Smith }
5699b94acceSBarry Smith 
5709b94acceSBarry Smith /*@
5719b94acceSBarry Smith    SNESComputeFunction - Computes the function that has been set with
5729b94acceSBarry Smith    SNESSetFunction().
5739b94acceSBarry Smith 
5749b94acceSBarry Smith    Input Parameters:
5759b94acceSBarry Smith .  snes - the SNES context
5769b94acceSBarry Smith .  x - input vector
5779b94acceSBarry Smith 
5789b94acceSBarry Smith    Output Parameter:
5793638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
5809b94acceSBarry Smith 
5811bffabb2SLois Curfman McInnes    Notes:
5829b94acceSBarry Smith    SNESComputeFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only.
5839b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
5849b94acceSBarry Smith    SNESComputeMinimizationFunction() and SNESComputeGradient();
5859b94acceSBarry Smith 
5869b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
5879b94acceSBarry Smith 
588a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
5899b94acceSBarry Smith @*/
5909b94acceSBarry Smith int SNESComputeFunction(SNES snes,Vec x, Vec y)
5919b94acceSBarry Smith {
5929b94acceSBarry Smith   int    ierr;
5939b94acceSBarry Smith 
59474679c65SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS)
59574679c65SBarry Smith     SETERRQ(1,"SNESComputeFunction: For SNES_NONLINEAR_EQUATIONS only");
5969b94acceSBarry Smith   PLogEventBegin(SNES_FunctionEval,snes,x,y,0);
5979b94acceSBarry Smith   ierr = (*snes->computefunction)(snes,x,y,snes->funP); CHKERRQ(ierr);
5989b94acceSBarry Smith   PLogEventEnd(SNES_FunctionEval,snes,x,y,0);
5999b94acceSBarry Smith   return 0;
6009b94acceSBarry Smith }
6019b94acceSBarry Smith 
6029b94acceSBarry Smith /*@C
6039b94acceSBarry Smith    SNESSetMinimizationFunction - Sets the function evaluation routine for
6049b94acceSBarry Smith    unconstrained minimization.
6059b94acceSBarry Smith 
6069b94acceSBarry Smith    Input Parameters:
6079b94acceSBarry Smith .  snes - the SNES context
6089b94acceSBarry Smith .  func - function evaluation routine
6099b94acceSBarry Smith .  ctx - optional user-defined function context
6109b94acceSBarry Smith 
6119b94acceSBarry Smith    Calling sequence of func:
6129b94acceSBarry Smith .  func (SNES snes,Vec x,double *f,void *ctx);
6139b94acceSBarry Smith 
6149b94acceSBarry Smith .  x - input vector
6159b94acceSBarry Smith .  f - function
6169b94acceSBarry Smith .  ctx - optional user-defined context for private data for the
6179b94acceSBarry Smith          function evaluation routine (may be null)
6189b94acceSBarry Smith 
6199b94acceSBarry Smith    Notes:
6209b94acceSBarry Smith    SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
6219b94acceSBarry Smith    methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
6229b94acceSBarry Smith    SNESSetFunction().
6239b94acceSBarry Smith 
6249b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimization, function
6259b94acceSBarry Smith 
626a86d99e1SLois Curfman McInnes .seealso:  SNESGetMinimizationFunction(), SNESComputeMinimizationFunction(),
627a86d99e1SLois Curfman McInnes            SNESSetHessian(), SNESSetGradient()
6289b94acceSBarry Smith @*/
6299b94acceSBarry Smith int SNESSetMinimizationFunction(SNES snes,int (*func)(SNES,Vec,double*,void*),
6309b94acceSBarry Smith                       void *ctx)
6319b94acceSBarry Smith {
63277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
6339b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
63448d91487SBarry Smith     "SNESSetMinimizationFunction:Only for SNES_UNCONSTRAINED_MINIMIZATION");
6359b94acceSBarry Smith   snes->computeumfunction   = func;
6369b94acceSBarry Smith   snes->umfunP              = ctx;
6379b94acceSBarry Smith   return 0;
6389b94acceSBarry Smith }
6399b94acceSBarry Smith 
6409b94acceSBarry Smith /*@
6419b94acceSBarry Smith    SNESComputeMinimizationFunction - Computes the function that has been
6429b94acceSBarry Smith    set with SNESSetMinimizationFunction().
6439b94acceSBarry Smith 
6449b94acceSBarry Smith    Input Parameters:
6459b94acceSBarry Smith .  snes - the SNES context
6469b94acceSBarry Smith .  x - input vector
6479b94acceSBarry Smith 
6489b94acceSBarry Smith    Output Parameter:
6499b94acceSBarry Smith .  y - function value
6509b94acceSBarry Smith 
6519b94acceSBarry Smith    Notes:
6529b94acceSBarry Smith    SNESComputeMinimizationFunction() is valid only for
6539b94acceSBarry Smith    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
6549b94acceSBarry Smith    SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction().
655a86d99e1SLois Curfman McInnes 
656a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, minimization, function
657a86d99e1SLois Curfman McInnes 
658a86d99e1SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESGetMinimizationFunction(),
659a86d99e1SLois Curfman McInnes           SNESComputeGradient(), SNESComputeHessian()
6609b94acceSBarry Smith @*/
6619b94acceSBarry Smith int SNESComputeMinimizationFunction(SNES snes,Vec x,double *y)
6629b94acceSBarry Smith {
6639b94acceSBarry Smith   int    ierr;
6649b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
66548d91487SBarry Smith     "SNESComputeMinimizationFunction:Only for SNES_UNCONSTRAINED_MINIMIZATION");
6669b94acceSBarry Smith   PLogEventBegin(SNES_MinimizationFunctionEval,snes,x,y,0);
6679b94acceSBarry Smith   ierr = (*snes->computeumfunction)(snes,x,y,snes->umfunP); CHKERRQ(ierr);
6689b94acceSBarry Smith   PLogEventEnd(SNES_MinimizationFunctionEval,snes,x,y,0);
6699b94acceSBarry Smith   return 0;
6709b94acceSBarry Smith }
6719b94acceSBarry Smith 
6729b94acceSBarry Smith /*@C
6739b94acceSBarry Smith    SNESSetGradient - Sets the gradient evaluation routine and gradient
6749b94acceSBarry Smith    vector for use by the SNES routines.
6759b94acceSBarry Smith 
6769b94acceSBarry Smith    Input Parameters:
6779b94acceSBarry Smith .  snes - the SNES context
6789b94acceSBarry Smith .  func - function evaluation routine
6799b94acceSBarry Smith .  ctx - optional user-defined function context
6809b94acceSBarry Smith .  r - vector to store gradient value
6819b94acceSBarry Smith 
6829b94acceSBarry Smith    Calling sequence of func:
6839b94acceSBarry Smith .  func (SNES, Vec x, Vec g, void *ctx);
6849b94acceSBarry Smith 
6859b94acceSBarry Smith .  x - input vector
6869b94acceSBarry Smith .  g - gradient vector
6879b94acceSBarry Smith .  ctx - optional user-defined context for private data for the
6889b94acceSBarry Smith          function evaluation routine (may be null)
6899b94acceSBarry Smith 
6909b94acceSBarry Smith    Notes:
6919b94acceSBarry Smith    SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
6929b94acceSBarry Smith    methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
6939b94acceSBarry Smith    SNESSetFunction().
6949b94acceSBarry Smith 
6959b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
6969b94acceSBarry Smith 
697a86d99e1SLois Curfman McInnes .seealso: SNESGetGradient(), SNESComputeGradient(), SNESSetHessian(),
698a86d99e1SLois Curfman McInnes           SNESSetMinimizationFunction(),
6999b94acceSBarry Smith @*/
70074679c65SBarry Smith int SNESSetGradient(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),void *ctx)
7019b94acceSBarry Smith {
70277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
7039b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
70448d91487SBarry Smith     "SNESSetGradient:For SNES_UNCONSTRAINED_MINIMIZATION only");
7059b94acceSBarry Smith   snes->computefunction     = func;
7069b94acceSBarry Smith   snes->vec_func            = snes->vec_func_always = r;
7079b94acceSBarry Smith   snes->funP                = ctx;
7089b94acceSBarry Smith   return 0;
7099b94acceSBarry Smith }
7109b94acceSBarry Smith 
7119b94acceSBarry Smith /*@
712a86d99e1SLois Curfman McInnes    SNESComputeGradient - Computes the gradient that has been set with
713a86d99e1SLois Curfman McInnes    SNESSetGradient().
7149b94acceSBarry Smith 
7159b94acceSBarry Smith    Input Parameters:
7169b94acceSBarry Smith .  snes - the SNES context
7179b94acceSBarry Smith .  x - input vector
7189b94acceSBarry Smith 
7199b94acceSBarry Smith    Output Parameter:
7209b94acceSBarry Smith .  y - gradient vector
7219b94acceSBarry Smith 
7229b94acceSBarry Smith    Notes:
7239b94acceSBarry Smith    SNESComputeGradient() is valid only for
7249b94acceSBarry Smith    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
7259b94acceSBarry Smith    SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction().
726a86d99e1SLois Curfman McInnes 
727a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, gradient
728a86d99e1SLois Curfman McInnes 
729a86d99e1SLois Curfman McInnes .seealso:  SNESSetGradient(), SNESGetGradient(),
730a86d99e1SLois Curfman McInnes            SNESComputeMinimizationFunction(), SNESComputeHessian()
7319b94acceSBarry Smith @*/
7329b94acceSBarry Smith int SNESComputeGradient(SNES snes,Vec x, Vec y)
7339b94acceSBarry Smith {
7349b94acceSBarry Smith   int    ierr;
73574679c65SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION)
73674679c65SBarry Smith     SETERRQ(1,"SNESComputeGradient:For SNES_UNCONSTRAINED_MINIMIZATION only");
7379b94acceSBarry Smith   PLogEventBegin(SNES_GradientEval,snes,x,y,0);
7389b94acceSBarry Smith   ierr = (*snes->computefunction)(snes,x,y,snes->funP); CHKERRQ(ierr);
7399b94acceSBarry Smith   PLogEventEnd(SNES_GradientEval,snes,x,y,0);
7409b94acceSBarry Smith   return 0;
7419b94acceSBarry Smith }
7429b94acceSBarry Smith 
74362fef451SLois Curfman McInnes /*@
74462fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
74562fef451SLois Curfman McInnes    set with SNESSetJacobian().
74662fef451SLois Curfman McInnes 
74762fef451SLois Curfman McInnes    Input Parameters:
74862fef451SLois Curfman McInnes .  snes - the SNES context
74962fef451SLois Curfman McInnes .  x - input vector
75062fef451SLois Curfman McInnes 
75162fef451SLois Curfman McInnes    Output Parameters:
75262fef451SLois Curfman McInnes .  A - Jacobian matrix
75362fef451SLois Curfman McInnes .  B - optional preconditioning matrix
75462fef451SLois Curfman McInnes .  flag - flag indicating matrix structure
75562fef451SLois Curfman McInnes 
75662fef451SLois Curfman McInnes    Notes:
75762fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
75862fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
75962fef451SLois Curfman McInnes 
76062fef451SLois Curfman McInnes    See SLESSetOperators() for information about setting the flag parameter.
76162fef451SLois Curfman McInnes 
76262fef451SLois Curfman McInnes    SNESComputeJacobian() is valid only for SNES_NONLINEAR_EQUATIONS
76362fef451SLois Curfman McInnes    methods. An analogous routine for SNES_UNCONSTRAINED_MINIMIZATION
76462fef451SLois Curfman McInnes    methods is SNESComputeJacobian().
76562fef451SLois Curfman McInnes 
76662fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
76762fef451SLois Curfman McInnes 
76862fef451SLois Curfman McInnes .seealso:  SNESSetJacobian(), SLESSetOperators()
76962fef451SLois Curfman McInnes @*/
7709b94acceSBarry Smith int SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
7719b94acceSBarry Smith {
7729b94acceSBarry Smith   int    ierr;
77374679c65SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS)
77474679c65SBarry Smith     SETERRQ(1,"SNESComputeJacobian: For SNES_NONLINEAR_EQUATIONS only");
7759b94acceSBarry Smith   if (!snes->computejacobian) return 0;
7769b94acceSBarry Smith   PLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);
777c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
7789b94acceSBarry Smith   ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP); CHKERRQ(ierr);
7799b94acceSBarry Smith   PLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);
7806d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
78177c4ece6SBarry Smith   PetscValidHeaderSpecific(*A,MAT_COOKIE);
78277c4ece6SBarry Smith   PetscValidHeaderSpecific(*B,MAT_COOKIE);
7839b94acceSBarry Smith   return 0;
7849b94acceSBarry Smith }
7859b94acceSBarry Smith 
78662fef451SLois Curfman McInnes /*@
78762fef451SLois Curfman McInnes    SNESComputeHessian - Computes the Hessian matrix that has been
78862fef451SLois Curfman McInnes    set with SNESSetHessian().
78962fef451SLois Curfman McInnes 
79062fef451SLois Curfman McInnes    Input Parameters:
79162fef451SLois Curfman McInnes .  snes - the SNES context
79262fef451SLois Curfman McInnes .  x - input vector
79362fef451SLois Curfman McInnes 
79462fef451SLois Curfman McInnes    Output Parameters:
79562fef451SLois Curfman McInnes .  A - Hessian matrix
79662fef451SLois Curfman McInnes .  B - optional preconditioning matrix
79762fef451SLois Curfman McInnes .  flag - flag indicating matrix structure
79862fef451SLois Curfman McInnes 
79962fef451SLois Curfman McInnes    Notes:
80062fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
80162fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
80262fef451SLois Curfman McInnes 
80362fef451SLois Curfman McInnes    See SLESSetOperators() for information about setting the flag parameter.
80462fef451SLois Curfman McInnes 
80562fef451SLois Curfman McInnes    SNESComputeHessian() is valid only for
80662fef451SLois Curfman McInnes    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
80762fef451SLois Curfman McInnes    SNES_NONLINEAR_EQUATIONS methods is SNESComputeJacobian().
80862fef451SLois Curfman McInnes 
80962fef451SLois Curfman McInnes .keywords: SNES, compute, Hessian, matrix
81062fef451SLois Curfman McInnes 
811a86d99e1SLois Curfman McInnes .seealso:  SNESSetHessian(), SLESSetOperators(), SNESComputeGradient(),
812a86d99e1SLois Curfman McInnes            SNESComputeMinimizationFunction()
81362fef451SLois Curfman McInnes @*/
81462fef451SLois Curfman McInnes int SNESComputeHessian(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag)
8159b94acceSBarry Smith {
8169b94acceSBarry Smith   int    ierr;
81774679c65SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION)
81874679c65SBarry Smith     SETERRQ(1,"SNESComputeHessian:For SNES_UNCONSTRAINED_MINIMIZATION only");
8199b94acceSBarry Smith   if (!snes->computejacobian) return 0;
82062fef451SLois Curfman McInnes   PLogEventBegin(SNES_HessianEval,snes,x,*A,*B);
8210f4a323eSLois Curfman McInnes   *flag = DIFFERENT_NONZERO_PATTERN;
82262fef451SLois Curfman McInnes   ierr = (*snes->computejacobian)(snes,x,A,B,flag,snes->jacP); CHKERRQ(ierr);
82362fef451SLois Curfman McInnes   PLogEventEnd(SNES_HessianEval,snes,x,*A,*B);
8240f4a323eSLois Curfman McInnes   /* make sure user returned a correct Jacobian and preconditioner */
82577c4ece6SBarry Smith   PetscValidHeaderSpecific(*A,MAT_COOKIE);
82677c4ece6SBarry Smith   PetscValidHeaderSpecific(*B,MAT_COOKIE);
8279b94acceSBarry Smith   return 0;
8289b94acceSBarry Smith }
8299b94acceSBarry Smith 
8309b94acceSBarry Smith /*@C
8319b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
8329b94acceSBarry Smith    location to store it.
8339b94acceSBarry Smith 
8349b94acceSBarry Smith    Input Parameters:
8359b94acceSBarry Smith .  snes - the SNES context
8369b94acceSBarry Smith .  A - Jacobian matrix
8379b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
8389b94acceSBarry Smith .  func - Jacobian evaluation routine
8399b94acceSBarry Smith .  ctx - optional user-defined context for private data for the
8409b94acceSBarry Smith          Jacobian evaluation routine (may be null)
8419b94acceSBarry Smith 
8429b94acceSBarry Smith    Calling sequence of func:
8439b94acceSBarry Smith .  func (SNES,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
8449b94acceSBarry Smith 
8459b94acceSBarry Smith .  x - input vector
8469b94acceSBarry Smith .  A - Jacobian matrix
8479b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
848ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
849ac21db08SLois Curfman McInnes    structure (same as flag in SLESSetOperators())
8509b94acceSBarry Smith .  ctx - optional user-defined Jacobian context
8519b94acceSBarry Smith 
8529b94acceSBarry Smith    Notes:
853ac21db08SLois Curfman McInnes    See SLESSetOperators() for information about setting the flag input
854ac21db08SLois Curfman McInnes    parameter in the routine func().  Be sure to read this information!
855ac21db08SLois Curfman McInnes 
856ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
8579b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
8589b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
8599b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
8609b94acceSBarry Smith    throughout the global iterations.
8619b94acceSBarry Smith 
8629b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
8639b94acceSBarry Smith 
864ac21db08SLois Curfman McInnes .seealso: SLESSetOperators(), SNESSetFunction()
8659b94acceSBarry Smith @*/
8669b94acceSBarry Smith int SNESSetJacobian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,
8679b94acceSBarry Smith                     MatStructure*,void*),void *ctx)
8689b94acceSBarry Smith {
86977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
87074679c65SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS)
87174679c65SBarry Smith     SETERRQ(1,"SNESSetJacobian:For SNES_NONLINEAR_EQUATIONS only");
8729b94acceSBarry Smith   snes->computejacobian = func;
8739b94acceSBarry Smith   snes->jacP            = ctx;
8749b94acceSBarry Smith   snes->jacobian        = A;
8759b94acceSBarry Smith   snes->jacobian_pre    = B;
8769b94acceSBarry Smith   return 0;
8779b94acceSBarry Smith }
87862fef451SLois Curfman McInnes 
879b4fd4287SBarry Smith /*@
880b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
881b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
882b4fd4287SBarry Smith 
883b4fd4287SBarry Smith    Input Parameter:
884b4fd4287SBarry Smith .  snes - the nonlinear solver context
885b4fd4287SBarry Smith 
886b4fd4287SBarry Smith    Output Parameters:
887b4fd4287SBarry Smith .  A - location to stash Jacobian matrix (or PETSC_NULL)
888b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
889b4fd4287SBarry Smith .  ctx - location to stash Jacobian ctx (or PETSC_NULL)
890b4fd4287SBarry Smith 
891b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
892b4fd4287SBarry Smith @*/
893b4fd4287SBarry Smith int SNESGetJacobian(SNES snes,Mat *A,Mat *B, void **ctx)
894b4fd4287SBarry Smith {
89574679c65SBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS)
89674679c65SBarry Smith     SETERRQ(1,"SNESSetJacobian:For SNES_NONLINEAR_EQUATIONS only");
897b4fd4287SBarry Smith   if (A)   *A = snes->jacobian;
898b4fd4287SBarry Smith   if (B)   *B = snes->jacobian_pre;
899b4fd4287SBarry Smith   if (ctx) *ctx = snes->jacP;
900b4fd4287SBarry Smith   return 0;
901b4fd4287SBarry Smith }
902b4fd4287SBarry Smith 
9039b94acceSBarry Smith /*@C
9049b94acceSBarry Smith    SNESSetHessian - Sets the function to compute Hessian as well as the
9059b94acceSBarry Smith    location to store it.
9069b94acceSBarry Smith 
9079b94acceSBarry Smith    Input Parameters:
9089b94acceSBarry Smith .  snes - the SNES context
9099b94acceSBarry Smith .  A - Hessian matrix
9109b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Hessian)
9119b94acceSBarry Smith .  func - Jacobian evaluation routine
9129b94acceSBarry Smith .  ctx - optional user-defined context for private data for the
9139b94acceSBarry Smith          Hessian evaluation routine (may be null)
9149b94acceSBarry Smith 
9159b94acceSBarry Smith    Calling sequence of func:
9169b94acceSBarry Smith .  func (SNES,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
9179b94acceSBarry Smith 
9189b94acceSBarry Smith .  x - input vector
9199b94acceSBarry Smith .  A - Hessian matrix
9209b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
921ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
922ac21db08SLois Curfman McInnes    structure (same as flag in SLESSetOperators())
9239b94acceSBarry Smith .  ctx - optional user-defined Hessian context
9249b94acceSBarry Smith 
9259b94acceSBarry Smith    Notes:
926ac21db08SLois Curfman McInnes    See SLESSetOperators() for information about setting the flag input
927ac21db08SLois Curfman McInnes    parameter in the routine func().  Be sure to read this information!
928ac21db08SLois Curfman McInnes 
9299b94acceSBarry Smith    The function func() takes Mat * as the matrix arguments rather than Mat.
9309b94acceSBarry Smith    This allows the Hessian evaluation routine to replace A and/or B with a
9319b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
9329b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
9339b94acceSBarry Smith    throughout the global iterations.
9349b94acceSBarry Smith 
9359b94acceSBarry Smith .keywords: SNES, nonlinear, set, Hessian, matrix
9369b94acceSBarry Smith 
937ac21db08SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESSetGradient(), SLESSetOperators()
9389b94acceSBarry Smith @*/
9399b94acceSBarry Smith int SNESSetHessian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,
9409b94acceSBarry Smith                     MatStructure*,void*),void *ctx)
9419b94acceSBarry Smith {
94277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
94374679c65SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION)
94474679c65SBarry Smith     SETERRQ(1,"SNESSetHessian:For SNES_UNCONSTRAINED_MINIMIZATION only");
9459b94acceSBarry Smith   snes->computejacobian = func;
9469b94acceSBarry Smith   snes->jacP            = ctx;
9479b94acceSBarry Smith   snes->jacobian        = A;
9489b94acceSBarry Smith   snes->jacobian_pre    = B;
9499b94acceSBarry Smith   return 0;
9509b94acceSBarry Smith }
9519b94acceSBarry Smith 
95262fef451SLois Curfman McInnes /*@
95362fef451SLois Curfman McInnes    SNESGetHessian - Returns the Hessian matrix and optionally the user
95462fef451SLois Curfman McInnes    provided context for evaluating the Hessian.
95562fef451SLois Curfman McInnes 
95662fef451SLois Curfman McInnes    Input Parameter:
95762fef451SLois Curfman McInnes .  snes - the nonlinear solver context
95862fef451SLois Curfman McInnes 
95962fef451SLois Curfman McInnes    Output Parameters:
96062fef451SLois Curfman McInnes .  A - location to stash Hessian matrix (or PETSC_NULL)
96162fef451SLois Curfman McInnes .  B - location to stash preconditioner matrix (or PETSC_NULL)
96262fef451SLois Curfman McInnes .  ctx - location to stash Hessian ctx (or PETSC_NULL)
96362fef451SLois Curfman McInnes 
96462fef451SLois Curfman McInnes .seealso: SNESSetHessian(), SNESComputeHessian()
96562fef451SLois Curfman McInnes @*/
96662fef451SLois Curfman McInnes int SNESGetHessian(SNES snes,Mat *A,Mat *B, void **ctx)
96762fef451SLois Curfman McInnes {
96874679c65SBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION)
96974679c65SBarry Smith     SETERRQ(1,"SNESSetHessian:For SNES_UNCONSTRAINED_MINIMIZATION only");
97062fef451SLois Curfman McInnes   if (A)   *A = snes->jacobian;
97162fef451SLois Curfman McInnes   if (B)   *B = snes->jacobian_pre;
97262fef451SLois Curfman McInnes   if (ctx) *ctx = snes->jacP;
97362fef451SLois Curfman McInnes   return 0;
97462fef451SLois Curfman McInnes }
97562fef451SLois Curfman McInnes 
9769b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
9779b94acceSBarry Smith 
9789b94acceSBarry Smith /*@
9799b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
980272ac6f2SLois Curfman McInnes    of a nonlinear solver.
9819b94acceSBarry Smith 
9829b94acceSBarry Smith    Input Parameter:
9839b94acceSBarry Smith .  snes - the SNES context
9848ddd3da0SLois Curfman McInnes .  x - the solution vector
9859b94acceSBarry Smith 
986272ac6f2SLois Curfman McInnes    Notes:
987272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
988272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
989272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
990272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
991272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
992272ac6f2SLois Curfman McInnes 
9939b94acceSBarry Smith .keywords: SNES, nonlinear, setup
9949b94acceSBarry Smith 
9959b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
9969b94acceSBarry Smith @*/
9978ddd3da0SLois Curfman McInnes int SNESSetUp(SNES snes,Vec x)
9989b94acceSBarry Smith {
999272ac6f2SLois Curfman McInnes   int ierr, flg;
100077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
100177c4ece6SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
10028ddd3da0SLois Curfman McInnes   snes->vec_sol = snes->vec_sol_always = x;
10039b94acceSBarry Smith 
1004c1f60f51SBarry Smith   ierr = OptionsHasName(snes->prefix,"-snes_mf_operator", &flg);  CHKERRQ(ierr);
1005c1f60f51SBarry Smith   /*
1006c1f60f51SBarry Smith       This version replaces the user provided Jacobian matrix with a
1007dfa02198SLois Curfman McInnes       matrix-free version but still employs the user-provided preconditioner matrix
1008c1f60f51SBarry Smith   */
1009c1f60f51SBarry Smith   if (flg) {
1010c1f60f51SBarry Smith     Mat J;
1011c1f60f51SBarry Smith     ierr = SNESDefaultMatrixFreeMatCreate(snes,snes->vec_sol,&J);CHKERRQ(ierr);
1012c1f60f51SBarry Smith     PLogObjectParent(snes,J);
1013c1f60f51SBarry Smith     snes->mfshell = J;
1014c1f60f51SBarry Smith     if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
1015c1f60f51SBarry Smith       snes->jacobian = J;
1016c1f60f51SBarry Smith       PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Jacobian routines\n");
1017c1f60f51SBarry Smith     }
1018c1f60f51SBarry Smith     else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
1019c1f60f51SBarry Smith       snes->jacobian = J;
1020c1f60f51SBarry Smith       PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Hessian routines\n");
1021c1f60f51SBarry Smith     } else SETERRQ(1,"SNESSetUp:Method class doesn't support matrix-free operator option");
1022c1f60f51SBarry Smith   }
1023272ac6f2SLois Curfman McInnes   ierr = OptionsHasName(snes->prefix,"-snes_mf", &flg);  CHKERRQ(ierr);
1024c1f60f51SBarry Smith   /*
1025dfa02198SLois Curfman McInnes       This version replaces both the user-provided Jacobian and the user-
1026c1f60f51SBarry Smith       provided preconditioner matrix with the default matrix free version.
1027c1f60f51SBarry Smith    */
102831615d04SLois Curfman McInnes   if (flg) {
1029272ac6f2SLois Curfman McInnes     Mat J;
1030272ac6f2SLois Curfman McInnes     ierr = SNESDefaultMatrixFreeMatCreate(snes,snes->vec_sol,&J);CHKERRQ(ierr);
1031272ac6f2SLois Curfman McInnes     PLogObjectParent(snes,J);
1032272ac6f2SLois Curfman McInnes     snes->mfshell = J;
103383e56afdSLois Curfman McInnes     if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
103483e56afdSLois Curfman McInnes       ierr = SNESSetJacobian(snes,J,J,0,snes->funP); CHKERRQ(ierr);
103594a424c1SBarry Smith       PLogInfo(snes,"SNESSetUp: Setting default matrix-free Jacobian routines\n");
103683e56afdSLois Curfman McInnes     }
103783e56afdSLois Curfman McInnes     else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
103883e56afdSLois Curfman McInnes       ierr = SNESSetHessian(snes,J,J,0,snes->funP); CHKERRQ(ierr);
103994a424c1SBarry Smith       PLogInfo(snes,"SNESSetUp: Setting default matrix-free Hessian routines\n");
104083e56afdSLois Curfman McInnes     } else SETERRQ(1,"SNESSetUp:Method class doesn't support matrix-free option");
1041272ac6f2SLois Curfman McInnes   }
10429b94acceSBarry Smith   if ((snes->method_class == SNES_NONLINEAR_EQUATIONS)) {
104337fcc0dbSBarry Smith     if (!snes->vec_func) SETERRQ(1,"SNESSetUp:Must call SNESSetFunction() first");
104437fcc0dbSBarry Smith     if (!snes->computefunction) SETERRQ(1,"SNESSetUp:Must call SNESSetFunction() first");
104537fcc0dbSBarry Smith     if (!snes->jacobian) SETERRQ(1,"SNESSetUp:Must call SNESSetJacobian() first");
1046d804570eSBarry Smith     if (snes->vec_func == snes->vec_sol) SETERRQ(1,"SNESSetUp:Solution vector cannot be function vector");
1047a703fe33SLois Curfman McInnes 
1048a703fe33SLois Curfman McInnes     /* Set the KSP stopping criterion to use the Eisenstat-Walker method */
104940191667SLois Curfman McInnes     if (snes->ksp_ewconv && snes->type != SNES_EQ_TR) {
1050a703fe33SLois Curfman McInnes       SLES sles; KSP ksp;
1051a703fe33SLois Curfman McInnes       ierr = SNESGetSLES(snes,&sles); CHKERRQ(ierr);
1052a703fe33SLois Curfman McInnes       ierr = SLESGetKSP(sles,&ksp); CHKERRQ(ierr);
1053a703fe33SLois Curfman McInnes       ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private,
1054a703fe33SLois Curfman McInnes              (void *)snes); CHKERRQ(ierr);
1055a703fe33SLois Curfman McInnes     }
10569b94acceSBarry Smith   } else if ((snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION)) {
105737fcc0dbSBarry Smith     if (!snes->vec_func) SETERRQ(1,"SNESSetUp:Must call SNESSetGradient() first");
105837fcc0dbSBarry Smith     if (!snes->computefunction) SETERRQ(1,"SNESSetUp:Must call SNESSetGradient() first");
105937fcc0dbSBarry Smith     if (!snes->computeumfunction)
106037fcc0dbSBarry Smith       SETERRQ(1,"SNESSetUp:Must call SNESSetMinimizationFunction() first");
106137fcc0dbSBarry Smith     if (!snes->jacobian) SETERRQ(1,"SNESSetUp:Must call SNESSetHessian() first");
10629b94acceSBarry Smith   } else SETERRQ(1,"SNESSetUp:Unknown method class");
1063a703fe33SLois Curfman McInnes   if (snes->setup) {ierr = (*snes->setup)(snes); CHKERRQ(ierr);}
1064a703fe33SLois Curfman McInnes   snes->setup_called = 1;
1065a703fe33SLois Curfman McInnes   return 0;
10669b94acceSBarry Smith }
10679b94acceSBarry Smith 
10689b94acceSBarry Smith /*@C
10699b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
10709b94acceSBarry Smith    with SNESCreate().
10719b94acceSBarry Smith 
10729b94acceSBarry Smith    Input Parameter:
10739b94acceSBarry Smith .  snes - the SNES context
10749b94acceSBarry Smith 
10759b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
10769b94acceSBarry Smith 
107763a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
10789b94acceSBarry Smith @*/
10799b94acceSBarry Smith int SNESDestroy(SNES snes)
10809b94acceSBarry Smith {
10819b94acceSBarry Smith   int ierr;
108277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
10839b94acceSBarry Smith   ierr = (*(snes)->destroy)((PetscObject)snes); CHKERRQ(ierr);
10840452661fSBarry Smith   if (snes->kspconvctx) PetscFree(snes->kspconvctx);
10859b94acceSBarry Smith   if (snes->mfshell) MatDestroy(snes->mfshell);
10869b94acceSBarry Smith   ierr = SLESDestroy(snes->sles); CHKERRQ(ierr);
10873e2e452bSBarry Smith   if (snes->xmonitor) SNESLGMonitorDestroy(snes->xmonitor);
10886f24a144SLois Curfman McInnes   if (snes->vwork) VecDestroyVecs(snes->vwork,snes->nvwork);
10899b94acceSBarry Smith   PLogObjectDestroy((PetscObject)snes);
10900452661fSBarry Smith   PetscHeaderDestroy((PetscObject)snes);
10919b94acceSBarry Smith   return 0;
10929b94acceSBarry Smith }
10939b94acceSBarry Smith 
10949b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
10959b94acceSBarry Smith 
10969b94acceSBarry Smith /*@
1097d7a720efSLois Curfman McInnes    SNESSetTolerances - Sets various parameters used in convergence tests.
10989b94acceSBarry Smith 
10999b94acceSBarry Smith    Input Parameters:
11009b94acceSBarry Smith .  snes - the SNES context
110133174efeSLois Curfman McInnes .  atol - absolute convergence tolerance
110233174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
110333174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
110433174efeSLois Curfman McInnes            of the change in the solution between steps
110533174efeSLois Curfman McInnes .  maxit - maximum number of iterations
110633174efeSLois Curfman McInnes .  maxf - maximum number of function evaluations
11079b94acceSBarry Smith 
110833174efeSLois Curfman McInnes    Options Database Keys:
110933174efeSLois Curfman McInnes $    -snes_atol <atol>
111033174efeSLois Curfman McInnes $    -snes_rtol <rtol>
111133174efeSLois Curfman McInnes $    -snes_stol <stol>
111233174efeSLois Curfman McInnes $    -snes_max_it <maxit>
111333174efeSLois Curfman McInnes $    -snes_max_funcs <maxf>
11149b94acceSBarry Smith 
1115d7a720efSLois Curfman McInnes    Notes:
11169b94acceSBarry Smith    The default maximum number of iterations is 50.
11179b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
11189b94acceSBarry Smith 
111933174efeSLois Curfman McInnes .keywords: SNES, nonlinear, set, convergence, tolerances
11209b94acceSBarry Smith 
1121d7a720efSLois Curfman McInnes .seealso: SNESSetTrustRegionTolerance(), SNESSetMinimizationFunctionTolerance()
11229b94acceSBarry Smith @*/
1123d7a720efSLois Curfman McInnes int SNESSetTolerances(SNES snes,double atol,double rtol,double stol,int maxit,int maxf)
11249b94acceSBarry Smith {
112577c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1126d7a720efSLois Curfman McInnes   if (atol != PETSC_DEFAULT)  snes->atol      = atol;
1127d7a720efSLois Curfman McInnes   if (rtol != PETSC_DEFAULT)  snes->rtol      = rtol;
1128d7a720efSLois Curfman McInnes   if (stol != PETSC_DEFAULT)  snes->xtol      = stol;
1129d7a720efSLois Curfman McInnes   if (maxit != PETSC_DEFAULT) snes->max_its   = maxit;
1130d7a720efSLois Curfman McInnes   if (maxf != PETSC_DEFAULT)  snes->max_funcs = maxf;
11319b94acceSBarry Smith   return 0;
11329b94acceSBarry Smith }
11339b94acceSBarry Smith 
11349b94acceSBarry Smith /*@
113533174efeSLois Curfman McInnes    SNESGetTolerances - Gets various parameters used in convergence tests.
113633174efeSLois Curfman McInnes 
113733174efeSLois Curfman McInnes    Input Parameters:
113833174efeSLois Curfman McInnes .  snes - the SNES context
113933174efeSLois Curfman McInnes .  atol - absolute convergence tolerance
114033174efeSLois Curfman McInnes .  rtol - relative convergence tolerance
114133174efeSLois Curfman McInnes .  stol -  convergence tolerance in terms of the norm
114233174efeSLois Curfman McInnes            of the change in the solution between steps
114333174efeSLois Curfman McInnes .  maxit - maximum number of iterations
114433174efeSLois Curfman McInnes .  maxf - maximum number of function evaluations
114533174efeSLois Curfman McInnes 
114633174efeSLois Curfman McInnes    Notes:
114733174efeSLois Curfman McInnes    The user can specify PETSC_NULL for any parameter that is not needed.
114833174efeSLois Curfman McInnes 
114933174efeSLois Curfman McInnes .keywords: SNES, nonlinear, get, convergence, tolerances
115033174efeSLois Curfman McInnes 
115133174efeSLois Curfman McInnes .seealso: SNESSetTolerances()
115233174efeSLois Curfman McInnes @*/
115333174efeSLois Curfman McInnes int SNESGetTolerances(SNES snes,double *atol,double *rtol,double *stol,int *maxit,int *maxf)
115433174efeSLois Curfman McInnes {
115533174efeSLois Curfman McInnes   PetscValidHeaderSpecific(snes,SNES_COOKIE);
115633174efeSLois Curfman McInnes   if (atol)  *atol  = snes->atol;
115733174efeSLois Curfman McInnes   if (rtol)  *rtol  = snes->rtol;
115833174efeSLois Curfman McInnes   if (stol)  *stol  = snes->xtol;
115933174efeSLois Curfman McInnes   if (maxit) *maxit = snes->max_its;
116033174efeSLois Curfman McInnes   if (maxf)  *maxf  = snes->max_funcs;
116133174efeSLois Curfman McInnes   return 0;
116233174efeSLois Curfman McInnes }
116333174efeSLois Curfman McInnes 
116433174efeSLois Curfman McInnes /*@
11659b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
11669b94acceSBarry Smith 
11679b94acceSBarry Smith    Input Parameters:
11689b94acceSBarry Smith .  snes - the SNES context
11699b94acceSBarry Smith .  tol - tolerance
11709b94acceSBarry Smith 
11719b94acceSBarry Smith    Options Database Key:
1172d7a720efSLois Curfman McInnes $    -snes_trtol <tol>
11739b94acceSBarry Smith 
11749b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
11759b94acceSBarry Smith 
1176d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetMinimizationFunctionTolerance()
11779b94acceSBarry Smith @*/
11789b94acceSBarry Smith int SNESSetTrustRegionTolerance(SNES snes,double tol)
11799b94acceSBarry Smith {
118077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
11819b94acceSBarry Smith   snes->deltatol = tol;
11829b94acceSBarry Smith   return 0;
11839b94acceSBarry Smith }
11849b94acceSBarry Smith 
11859b94acceSBarry Smith /*@
118677c4ece6SBarry Smith    SNESSetMinimizationFunctionTolerance - Sets the minimum allowable function tolerance
11879b94acceSBarry Smith    for unconstrained minimization solvers.
11889b94acceSBarry Smith 
11899b94acceSBarry Smith    Input Parameters:
11909b94acceSBarry Smith .  snes - the SNES context
11919b94acceSBarry Smith .  ftol - minimum function tolerance
11929b94acceSBarry Smith 
11939b94acceSBarry Smith    Options Database Key:
1194d7a720efSLois Curfman McInnes $    -snes_fmin <ftol>
11959b94acceSBarry Smith 
11969b94acceSBarry Smith    Note:
119777c4ece6SBarry Smith    SNESSetMinimizationFunctionTolerance() is valid for SNES_UNCONSTRAINED_MINIMIZATION
11989b94acceSBarry Smith    methods only.
11999b94acceSBarry Smith 
12009b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimum, convergence, function, tolerance
12019b94acceSBarry Smith 
1202d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetTrustRegionTolerance()
12039b94acceSBarry Smith @*/
120477c4ece6SBarry Smith int SNESSetMinimizationFunctionTolerance(SNES snes,double ftol)
12059b94acceSBarry Smith {
120677c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
12079b94acceSBarry Smith   snes->fmin = ftol;
12089b94acceSBarry Smith   return 0;
12099b94acceSBarry Smith }
12109b94acceSBarry Smith 
12119b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
12129b94acceSBarry Smith 
12139b94acceSBarry Smith /*@C
12149b94acceSBarry Smith    SNESSetMonitor - Sets the function that is to be used at every
12159b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
12169b94acceSBarry Smith    progress.
12179b94acceSBarry Smith 
12189b94acceSBarry Smith    Input Parameters:
12199b94acceSBarry Smith .  snes - the SNES context
12209b94acceSBarry Smith .  func - monitoring routine
12219b94acceSBarry Smith .  mctx - optional user-defined context for private data for the
12229b94acceSBarry Smith           monitor routine (may be null)
12239b94acceSBarry Smith 
12249b94acceSBarry Smith    Calling sequence of func:
1225682d7d0cSBarry Smith    int func(SNES snes,int its, Vec x,Vec f,double norm,void *mctx)
12269b94acceSBarry Smith 
12279b94acceSBarry Smith $    snes - the SNES context
12289b94acceSBarry Smith $    its - iteration number
12299b94acceSBarry Smith $    mctx - optional monitoring context
12309b94acceSBarry Smith $
12319b94acceSBarry Smith $ SNES_NONLINEAR_EQUATIONS methods:
12329b94acceSBarry Smith $    norm - 2-norm function value (may be estimated)
12339b94acceSBarry Smith $
12349b94acceSBarry Smith $ SNES_UNCONSTRAINED_MINIMIZATION methods:
12359b94acceSBarry Smith $    norm - 2-norm gradient value (may be estimated)
12369b94acceSBarry Smith 
12379b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
12389b94acceSBarry Smith 
12399b94acceSBarry Smith .seealso: SNESDefaultMonitor()
12409b94acceSBarry Smith @*/
124174679c65SBarry Smith int SNESSetMonitor( SNES snes, int (*func)(SNES,int,double,void*),void *mctx )
12429b94acceSBarry Smith {
12439b94acceSBarry Smith   snes->monitor = func;
124474679c65SBarry Smith   snes->monP    = mctx;
12459b94acceSBarry Smith   return 0;
12469b94acceSBarry Smith }
12479b94acceSBarry Smith 
12489b94acceSBarry Smith /*@C
12499b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
12509b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
12519b94acceSBarry Smith 
12529b94acceSBarry Smith    Input Parameters:
12539b94acceSBarry Smith .  snes - the SNES context
12549b94acceSBarry Smith .  func - routine to test for convergence
12559b94acceSBarry Smith .  cctx - optional context for private data for the convergence routine
12569b94acceSBarry Smith           (may be null)
12579b94acceSBarry Smith 
12589b94acceSBarry Smith    Calling sequence of func:
12599b94acceSBarry Smith    int func (SNES snes,double xnorm,double gnorm,
12609b94acceSBarry Smith              double f,void *cctx)
12619b94acceSBarry Smith 
12629b94acceSBarry Smith $    snes - the SNES context
12639b94acceSBarry Smith $    cctx - optional convergence context
12649b94acceSBarry Smith $    xnorm - 2-norm of current iterate
12659b94acceSBarry Smith $
12669b94acceSBarry Smith $ SNES_NONLINEAR_EQUATIONS methods:
12679b94acceSBarry Smith $    gnorm - 2-norm of current step
12689b94acceSBarry Smith $    f - 2-norm of function
12699b94acceSBarry Smith $
12709b94acceSBarry Smith $ SNES_UNCONSTRAINED_MINIMIZATION methods:
12719b94acceSBarry Smith $    gnorm - 2-norm of current gradient
12729b94acceSBarry Smith $    f - function value
12739b94acceSBarry Smith 
12749b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
12759b94acceSBarry Smith 
127640191667SLois Curfman McInnes .seealso: SNESConverged_EQ_LS(), SNESConverged_EQ_TR(),
127740191667SLois Curfman McInnes           SNESConverged_UM_LS(), SNESConverged_UM_TR()
12789b94acceSBarry Smith @*/
127974679c65SBarry Smith int SNESSetConvergenceTest(SNES snes,int (*func)(SNES,double,double,double,void*),void *cctx)
12809b94acceSBarry Smith {
12819b94acceSBarry Smith   (snes)->converged = func;
12829b94acceSBarry Smith   (snes)->cnvP      = cctx;
12839b94acceSBarry Smith   return 0;
12849b94acceSBarry Smith }
12859b94acceSBarry Smith 
12869b94acceSBarry Smith /*
12879b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
12889b94acceSBarry Smith    positive parameter delta.
12899b94acceSBarry Smith 
12909b94acceSBarry Smith     Input Parameters:
12919b94acceSBarry Smith .   snes - the SNES context
12929b94acceSBarry Smith .   y - approximate solution of linear system
12939b94acceSBarry Smith .   fnorm - 2-norm of current function
12949b94acceSBarry Smith .   delta - trust region size
12959b94acceSBarry Smith 
12969b94acceSBarry Smith     Output Parameters:
12979b94acceSBarry Smith .   gpnorm - predicted function norm at the new point, assuming local
12989b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
12999b94acceSBarry Smith     region, and exceeds zero otherwise.
13009b94acceSBarry Smith .   ynorm - 2-norm of the step
13019b94acceSBarry Smith 
13029b94acceSBarry Smith     Note:
130340191667SLois Curfman McInnes     For non-trust region methods such as SNES_EQ_LS, the parameter delta
13049b94acceSBarry Smith     is set to be the maximum allowable step size.
13059b94acceSBarry Smith 
13069b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
13079b94acceSBarry Smith */
13089b94acceSBarry Smith int SNESScaleStep_Private(SNES snes,Vec y,double *fnorm,double *delta,
13099b94acceSBarry Smith                           double *gpnorm,double *ynorm)
13109b94acceSBarry Smith {
13119b94acceSBarry Smith   double norm;
13129b94acceSBarry Smith   Scalar cnorm;
1313cddf8d76SBarry Smith   VecNorm(y,NORM_2, &norm );
13149b94acceSBarry Smith   if (norm > *delta) {
13159b94acceSBarry Smith      norm = *delta/norm;
13169b94acceSBarry Smith      *gpnorm = (1.0 - norm)*(*fnorm);
13179b94acceSBarry Smith      cnorm = norm;
13189b94acceSBarry Smith      VecScale( &cnorm, y );
13199b94acceSBarry Smith      *ynorm = *delta;
13209b94acceSBarry Smith   } else {
13219b94acceSBarry Smith      *gpnorm = 0.0;
13229b94acceSBarry Smith      *ynorm = norm;
13239b94acceSBarry Smith   }
13249b94acceSBarry Smith   return 0;
13259b94acceSBarry Smith }
13269b94acceSBarry Smith 
13279b94acceSBarry Smith /*@
13289b94acceSBarry Smith    SNESSolve - Solves a nonlinear system.  Call SNESSolve after calling
132963a78c88SLois Curfman McInnes    SNESCreate() and optional routines of the form SNESSetXXX().
13309b94acceSBarry Smith 
13319b94acceSBarry Smith    Input Parameter:
13329b94acceSBarry Smith .  snes - the SNES context
13338ddd3da0SLois Curfman McInnes .  x - the solution vector
13349b94acceSBarry Smith 
13359b94acceSBarry Smith    Output Parameter:
13369b94acceSBarry Smith    its - number of iterations until termination
13379b94acceSBarry Smith 
13388ddd3da0SLois Curfman McInnes    Note:
13398ddd3da0SLois Curfman McInnes    The user should initialize the vector, x, with the initial guess
13408ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
13418ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
13428ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
13438ddd3da0SLois Curfman McInnes 
13449b94acceSBarry Smith .keywords: SNES, nonlinear, solve
13459b94acceSBarry Smith 
134663a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESDestroy()
13479b94acceSBarry Smith @*/
13488ddd3da0SLois Curfman McInnes int SNESSolve(SNES snes,Vec x,int *its)
13499b94acceSBarry Smith {
13503c7409f5SSatish Balay   int ierr, flg;
1351052efed2SBarry Smith 
135277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
135374679c65SBarry Smith   PetscValidIntPointer(its);
1354c4fc05e7SBarry Smith   if (!snes->setup_called) {ierr = SNESSetUp(snes,x); CHKERRQ(ierr);}
1355c4fc05e7SBarry Smith   else {snes->vec_sol = snes->vec_sol_always = x;}
13569b94acceSBarry Smith   PLogEventBegin(SNES_Solve,snes,0,0,0);
13579b94acceSBarry Smith   ierr = (*(snes)->solve)(snes,its); CHKERRQ(ierr);
13589b94acceSBarry Smith   PLogEventEnd(SNES_Solve,snes,0,0,0);
13593c7409f5SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-snes_view", &flg); CHKERRQ(ierr);
13606d4a8577SBarry Smith   if (flg) { ierr = SNESView(snes,VIEWER_STDOUT_WORLD); CHKERRQ(ierr); }
13619b94acceSBarry Smith   return 0;
13629b94acceSBarry Smith }
13639b94acceSBarry Smith 
13649b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
136537fcc0dbSBarry Smith static NRList *__SNESList = 0;
13669b94acceSBarry Smith 
13679b94acceSBarry Smith /*@
13684b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
13699b94acceSBarry Smith 
13709b94acceSBarry Smith    Input Parameters:
13719b94acceSBarry Smith .  snes - the SNES context
13729b94acceSBarry Smith .  method - a known method
13739b94acceSBarry Smith 
13749b94acceSBarry Smith    Notes:
13759b94acceSBarry Smith    See "petsc/include/snes.h" for available methods (for instance)
13769b94acceSBarry Smith $  Systems of nonlinear equations:
137740191667SLois Curfman McInnes $    SNES_EQ_LS - Newton's method with line search
137840191667SLois Curfman McInnes $    SNES_EQ_TR - Newton's method with trust region
13799b94acceSBarry Smith $  Unconstrained minimization:
138040191667SLois Curfman McInnes $    SNES_UM_TR - Newton's method with trust region
138140191667SLois Curfman McInnes $    SNES_UM_LS - Newton's method with line search
13829b94acceSBarry Smith 
13839b94acceSBarry Smith   Options Database Command:
13844b0e389bSBarry Smith $ -snes_type  <method>
13859b94acceSBarry Smith $    Use -help for a list of available methods
13869b94acceSBarry Smith $    (for instance, ls or tr)
1387a703fe33SLois Curfman McInnes 
1388a703fe33SLois Curfman McInnes .keysords: SNES, set, method
13899b94acceSBarry Smith @*/
13904b0e389bSBarry Smith int SNESSetType(SNES snes,SNESType method)
13919b94acceSBarry Smith {
13929b94acceSBarry Smith   int (*r)(SNES);
139377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
13947d1a2b2bSBarry Smith   if (snes->type == (int) method) return 0;
13957d1a2b2bSBarry Smith 
13969b94acceSBarry Smith   /* Get the function pointers for the iterative method requested */
139737fcc0dbSBarry Smith   if (!__SNESList) {SNESRegisterAll();}
139837fcc0dbSBarry Smith   if (!__SNESList) {SETERRQ(1,"SNESSetType:Could not get methods");}
139937fcc0dbSBarry Smith   r =  (int (*)(SNES))NRFindRoutine( __SNESList, (int)method, (char *)0 );
14004b0e389bSBarry Smith   if (!r) {SETERRQ(1,"SNESSetType:Unknown method");}
14010452661fSBarry Smith   if (snes->data) PetscFree(snes->data);
14029b94acceSBarry Smith   snes->set_method_called = 1;
14039b94acceSBarry Smith   return (*r)(snes);
14049b94acceSBarry Smith }
14059b94acceSBarry Smith 
14069b94acceSBarry Smith /* --------------------------------------------------------------------- */
14079b94acceSBarry Smith /*@C
14089b94acceSBarry Smith    SNESRegister - Adds the method to the nonlinear solver package, given
14094b0e389bSBarry Smith    a function pointer and a nonlinear solver name of the type SNESType.
14109b94acceSBarry Smith 
14119b94acceSBarry Smith    Input Parameters:
141240191667SLois Curfman McInnes .  name - for instance SNES_EQ_LS, SNES_EQ_TR, ...
141340191667SLois Curfman McInnes .  sname - corresponding string for name
14149b94acceSBarry Smith .  create - routine to create method context
14159b94acceSBarry Smith 
14169b94acceSBarry Smith .keywords: SNES, nonlinear, register
14179b94acceSBarry Smith 
14189b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterDestroy()
14199b94acceSBarry Smith @*/
14209b94acceSBarry Smith int SNESRegister(int name, char *sname, int (*create)(SNES))
14219b94acceSBarry Smith {
14229b94acceSBarry Smith   int ierr;
142337fcc0dbSBarry Smith   if (!__SNESList) {ierr = NRCreate(&__SNESList); CHKERRQ(ierr);}
142437fcc0dbSBarry Smith   NRRegister( __SNESList, name, sname, (int (*)(void*))create );
14259b94acceSBarry Smith   return 0;
14269b94acceSBarry Smith }
14279b94acceSBarry Smith /* --------------------------------------------------------------------- */
14289b94acceSBarry Smith /*@C
14299b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
14309b94acceSBarry Smith    registered by SNESRegister().
14319b94acceSBarry Smith 
14329b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
14339b94acceSBarry Smith 
14349b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
14359b94acceSBarry Smith @*/
14369b94acceSBarry Smith int SNESRegisterDestroy()
14379b94acceSBarry Smith {
143837fcc0dbSBarry Smith   if (__SNESList) {
143937fcc0dbSBarry Smith     NRDestroy( __SNESList );
144037fcc0dbSBarry Smith     __SNESList = 0;
14419b94acceSBarry Smith   }
14429b94acceSBarry Smith   return 0;
14439b94acceSBarry Smith }
14449b94acceSBarry Smith 
14459b94acceSBarry Smith /*
14464b0e389bSBarry Smith    SNESGetTypeFromOptions_Private - Sets the selected method from the
14479b94acceSBarry Smith    options database.
14489b94acceSBarry Smith 
14499b94acceSBarry Smith    Input Parameter:
14509b94acceSBarry Smith .  ctx - the SNES context
14519b94acceSBarry Smith 
14529b94acceSBarry Smith    Output Parameter:
14539b94acceSBarry Smith .  method -  solver method
14549b94acceSBarry Smith 
14559b94acceSBarry Smith    Returns:
14569b94acceSBarry Smith    Returns 1 if the method is found; 0 otherwise.
14579b94acceSBarry Smith 
14589b94acceSBarry Smith    Options Database Key:
14594b0e389bSBarry Smith $  -snes_type  method
14609b94acceSBarry Smith */
1461052efed2SBarry Smith int SNESGetTypeFromOptions_Private(SNES ctx,SNESType *method,int *flg)
14629b94acceSBarry Smith {
1463052efed2SBarry Smith   int  ierr;
14649b94acceSBarry Smith   char sbuf[50];
14655baf8537SBarry Smith 
1466052efed2SBarry Smith   ierr = OptionsGetString(ctx->prefix,"-snes_type",sbuf,50,flg);CHKERRQ(ierr);
1467052efed2SBarry Smith   if (*flg) {
1468052efed2SBarry Smith     if (!__SNESList) {ierr = SNESRegisterAll(); CHKERRQ(ierr);}
146937fcc0dbSBarry Smith     *method = (SNESType)NRFindID( __SNESList, sbuf );
14709b94acceSBarry Smith   }
14719b94acceSBarry Smith   return 0;
14729b94acceSBarry Smith }
14739b94acceSBarry Smith 
14749b94acceSBarry Smith /*@C
14759a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
14769b94acceSBarry Smith 
14779b94acceSBarry Smith    Input Parameter:
14784b0e389bSBarry Smith .  snes - nonlinear solver context
14799b94acceSBarry Smith 
14809b94acceSBarry Smith    Output Parameter:
14819a28b0a6SLois Curfman McInnes .  method - SNES method (or use PETSC_NULL)
14829a28b0a6SLois Curfman McInnes .  name - name of SNES method (or use PETSC_NULL)
14839b94acceSBarry Smith 
14849b94acceSBarry Smith .keywords: SNES, nonlinear, get, method, name
14859b94acceSBarry Smith @*/
14864b0e389bSBarry Smith int SNESGetType(SNES snes, SNESType *method,char **name)
14879b94acceSBarry Smith {
148806a9b0adSLois Curfman McInnes   int ierr;
148937fcc0dbSBarry Smith   if (!__SNESList) {ierr = SNESRegisterAll(); CHKERRQ(ierr);}
14904b0e389bSBarry Smith   if (method) *method = (SNESType) snes->type;
149137fcc0dbSBarry Smith   if (name)  *name = NRFindName( __SNESList, (int) snes->type );
14929b94acceSBarry Smith   return 0;
14939b94acceSBarry Smith }
14949b94acceSBarry Smith 
14959b94acceSBarry Smith #include <stdio.h>
14969b94acceSBarry Smith /*
14974b0e389bSBarry Smith    SNESPrintTypes_Private - Prints the SNES methods available from the
14989b94acceSBarry Smith    options database.
14999b94acceSBarry Smith 
15009b94acceSBarry Smith    Input Parameters:
150133455573SSatish Balay .  comm   - communicator (usually MPI_COMM_WORLD)
15029b94acceSBarry Smith .  prefix - prefix (usually "-")
15034b0e389bSBarry Smith .  name   - the options database name (by default "snes_type")
15049b94acceSBarry Smith */
150533455573SSatish Balay int SNESPrintTypes_Private(MPI_Comm comm,char* prefix,char *name)
15069b94acceSBarry Smith {
15079b94acceSBarry Smith   FuncList *entry;
150837fcc0dbSBarry Smith   if (!__SNESList) {SNESRegisterAll();}
150937fcc0dbSBarry Smith   entry = __SNESList->head;
151077c4ece6SBarry Smith   PetscPrintf(comm," %s%s (one of)",prefix,name);
15119b94acceSBarry Smith   while (entry) {
151277c4ece6SBarry Smith     PetscPrintf(comm," %s",entry->name);
15139b94acceSBarry Smith     entry = entry->next;
15149b94acceSBarry Smith   }
151577c4ece6SBarry Smith   PetscPrintf(comm,"\n");
15169b94acceSBarry Smith   return 0;
15179b94acceSBarry Smith }
15189b94acceSBarry Smith 
15199b94acceSBarry Smith /*@C
15209b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
15219b94acceSBarry Smith    stored.
15229b94acceSBarry Smith 
15239b94acceSBarry Smith    Input Parameter:
15249b94acceSBarry Smith .  snes - the SNES context
15259b94acceSBarry Smith 
15269b94acceSBarry Smith    Output Parameter:
15279b94acceSBarry Smith .  x - the solution
15289b94acceSBarry Smith 
15299b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
15309b94acceSBarry Smith 
1531a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESGetGradient(), SNESGetSolutionUpdate()
15329b94acceSBarry Smith @*/
15339b94acceSBarry Smith int SNESGetSolution(SNES snes,Vec *x)
15349b94acceSBarry Smith {
153577c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
15369b94acceSBarry Smith   *x = snes->vec_sol_always;
15379b94acceSBarry Smith   return 0;
15389b94acceSBarry Smith }
15399b94acceSBarry Smith 
15409b94acceSBarry Smith /*@C
15419b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
15429b94acceSBarry Smith    stored.
15439b94acceSBarry Smith 
15449b94acceSBarry Smith    Input Parameter:
15459b94acceSBarry Smith .  snes - the SNES context
15469b94acceSBarry Smith 
15479b94acceSBarry Smith    Output Parameter:
15489b94acceSBarry Smith .  x - the solution update
15499b94acceSBarry Smith 
15509b94acceSBarry Smith    Notes:
15519b94acceSBarry Smith    This vector is implementation dependent.
15529b94acceSBarry Smith 
15539b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
15549b94acceSBarry Smith 
15559b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction
15569b94acceSBarry Smith @*/
15579b94acceSBarry Smith int SNESGetSolutionUpdate(SNES snes,Vec *x)
15589b94acceSBarry Smith {
155977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
15609b94acceSBarry Smith   *x = snes->vec_sol_update_always;
15619b94acceSBarry Smith   return 0;
15629b94acceSBarry Smith }
15639b94acceSBarry Smith 
15649b94acceSBarry Smith /*@C
15653638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
15669b94acceSBarry Smith 
15679b94acceSBarry Smith    Input Parameter:
15689b94acceSBarry Smith .  snes - the SNES context
15699b94acceSBarry Smith 
15709b94acceSBarry Smith    Output Parameter:
15713638b69dSLois Curfman McInnes .  r - the function
15729b94acceSBarry Smith 
15739b94acceSBarry Smith    Notes:
15749b94acceSBarry Smith    SNESGetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only
15759b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
15769b94acceSBarry Smith    SNESGetMinimizationFunction() and SNESGetGradient();
15779b94acceSBarry Smith 
1578a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
15799b94acceSBarry Smith 
158031615d04SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution(), SNESGetMinimizationFunction(),
158131615d04SLois Curfman McInnes           SNESGetGradient()
15829b94acceSBarry Smith @*/
15839b94acceSBarry Smith int SNESGetFunction(SNES snes,Vec *r)
15849b94acceSBarry Smith {
158577c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
15869b94acceSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1,
158748d91487SBarry Smith     "SNESGetFunction:For SNES_NONLINEAR_EQUATIONS only");
15889b94acceSBarry Smith   *r = snes->vec_func_always;
15899b94acceSBarry Smith   return 0;
15909b94acceSBarry Smith }
15919b94acceSBarry Smith 
15929b94acceSBarry Smith /*@C
15933638b69dSLois Curfman McInnes    SNESGetGradient - Returns the vector where the gradient is stored.
15949b94acceSBarry Smith 
15959b94acceSBarry Smith    Input Parameter:
15969b94acceSBarry Smith .  snes - the SNES context
15979b94acceSBarry Smith 
15989b94acceSBarry Smith    Output Parameter:
15999b94acceSBarry Smith .  r - the gradient
16009b94acceSBarry Smith 
16019b94acceSBarry Smith    Notes:
16029b94acceSBarry Smith    SNESGetGradient() is valid for SNES_UNCONSTRAINED_MINIMIZATION methods
16039b94acceSBarry Smith    only.  An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
16049b94acceSBarry Smith    SNESGetFunction().
16059b94acceSBarry Smith 
16069b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient
16079b94acceSBarry Smith 
160831615d04SLois Curfman McInnes .seealso: SNESGetMinimizationFunction(), SNESGetSolution(), SNESGetFunction()
16099b94acceSBarry Smith @*/
16109b94acceSBarry Smith int SNESGetGradient(SNES snes,Vec *r)
16119b94acceSBarry Smith {
161277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
16139b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
161448d91487SBarry Smith     "SNESGetGradient:For SNES_UNCONSTRAINED_MINIMIZATION only");
16159b94acceSBarry Smith   *r = snes->vec_func_always;
16169b94acceSBarry Smith   return 0;
16179b94acceSBarry Smith }
16189b94acceSBarry Smith 
16199b94acceSBarry Smith /*@
16209b94acceSBarry Smith    SNESGetMinimizationFunction - Returns the scalar function value for
16219b94acceSBarry Smith    unconstrained minimization problems.
16229b94acceSBarry Smith 
16239b94acceSBarry Smith    Input Parameter:
16249b94acceSBarry Smith .  snes - the SNES context
16259b94acceSBarry Smith 
16269b94acceSBarry Smith    Output Parameter:
16279b94acceSBarry Smith .  r - the function
16289b94acceSBarry Smith 
16299b94acceSBarry Smith    Notes:
16309b94acceSBarry Smith    SNESGetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
16319b94acceSBarry Smith    methods only.  An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
16329b94acceSBarry Smith    SNESGetFunction().
16339b94acceSBarry Smith 
16349b94acceSBarry Smith .keywords: SNES, nonlinear, get, function
16359b94acceSBarry Smith 
163631615d04SLois Curfman McInnes .seealso: SNESGetGradient(), SNESGetSolution(), SNESGetFunction()
16379b94acceSBarry Smith @*/
16389b94acceSBarry Smith int SNESGetMinimizationFunction(SNES snes,double *r)
16399b94acceSBarry Smith {
164077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
164174679c65SBarry Smith   PetscValidScalarPointer(r);
16429b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
164348d91487SBarry Smith     "SNESGetMinimizationFunction:For SNES_UNCONSTRAINED_MINIMIZATION only");
16449b94acceSBarry Smith   *r = snes->fc;
16459b94acceSBarry Smith   return 0;
16469b94acceSBarry Smith }
16479b94acceSBarry Smith 
16483c7409f5SSatish Balay /*@C
16493c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
16503c7409f5SSatish Balay    SNES options in the database.
16513c7409f5SSatish Balay 
16523c7409f5SSatish Balay    Input Parameter:
16533c7409f5SSatish Balay .  snes - the SNES context
16543c7409f5SSatish Balay .  prefix - the prefix to prepend to all option names
16553c7409f5SSatish Balay 
16563c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
1657a86d99e1SLois Curfman McInnes 
1658a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
16593c7409f5SSatish Balay @*/
16603c7409f5SSatish Balay int SNESSetOptionsPrefix(SNES snes,char *prefix)
16613c7409f5SSatish Balay {
16623c7409f5SSatish Balay   int ierr;
16633c7409f5SSatish Balay 
166477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
16653c7409f5SSatish Balay   ierr = PetscObjectSetPrefix((PetscObject)snes, prefix); CHKERRQ(ierr);
16663c7409f5SSatish Balay   ierr = SLESSetOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr);
16673c7409f5SSatish Balay   return 0;
16683c7409f5SSatish Balay }
16693c7409f5SSatish Balay 
16703c7409f5SSatish Balay /*@C
1671*f525115eSLois Curfman McInnes    SNESAppendOptionsPrefix - Appends to the prefix used for searching for all
16723c7409f5SSatish Balay    SNES options in the database.
16733c7409f5SSatish Balay 
16743c7409f5SSatish Balay    Input Parameter:
16753c7409f5SSatish Balay .  snes - the SNES context
16763c7409f5SSatish Balay .  prefix - the prefix to prepend to all option names
16773c7409f5SSatish Balay 
16783c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
1679a86d99e1SLois Curfman McInnes 
1680a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
16813c7409f5SSatish Balay @*/
16823c7409f5SSatish Balay int SNESAppendOptionsPrefix(SNES snes,char *prefix)
16833c7409f5SSatish Balay {
16843c7409f5SSatish Balay   int ierr;
16853c7409f5SSatish Balay 
168677c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
16873c7409f5SSatish Balay   ierr = PetscObjectAppendPrefix((PetscObject)snes, prefix); CHKERRQ(ierr);
16883c7409f5SSatish Balay   ierr = SLESAppendOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr);
16893c7409f5SSatish Balay   return 0;
16903c7409f5SSatish Balay }
16913c7409f5SSatish Balay 
1692c83590e2SSatish Balay /*@
16933c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
16943c7409f5SSatish Balay    SNES options in the database.
16953c7409f5SSatish Balay 
16963c7409f5SSatish Balay    Input Parameter:
16973c7409f5SSatish Balay .  snes - the SNES context
16983c7409f5SSatish Balay 
16993c7409f5SSatish Balay    Output Parameter:
17003c7409f5SSatish Balay .  prefix - pointer to the prefix string used
17013c7409f5SSatish Balay 
17023c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
1703a86d99e1SLois Curfman McInnes 
1704a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
17053c7409f5SSatish Balay @*/
17063c7409f5SSatish Balay int SNESGetOptionsPrefix(SNES snes,char **prefix)
17073c7409f5SSatish Balay {
17083c7409f5SSatish Balay   int ierr;
17093c7409f5SSatish Balay 
171077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
17113c7409f5SSatish Balay   ierr = PetscObjectGetPrefix((PetscObject)snes, prefix); CHKERRQ(ierr);
17123c7409f5SSatish Balay   return 0;
17133c7409f5SSatish Balay }
17143c7409f5SSatish Balay 
17153c7409f5SSatish Balay 
17169b94acceSBarry Smith 
17179b94acceSBarry Smith 
17189b94acceSBarry Smith 
1719