xref: /petsc/src/snes/interface/snes.c (revision c30f285ea0b4cb365f39f2624898a0511eebc13b)
19b94acceSBarry Smith #ifndef lint
2*c30f285eSLois Curfman McInnes static char vcid[] = "$Id: snes.c,v 1.63 1996/03/20 06:06:35 curfman Exp bsmith $";
39b94acceSBarry Smith #endif
49b94acceSBarry Smith 
59b94acceSBarry Smith #include "draw.h"          /*I "draw.h"  I*/
69b94acceSBarry Smith #include "snesimpl.h"      /*I "snes.h"  I*/
7682d7d0cSBarry Smith #include "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
269b94acceSBarry Smith $     STDOUT_VIEWER_SELF - standard output (default)
279b94acceSBarry Smith $     STDOUT_VIEWER_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 
4819bcc07fSBarry Smith   ierr = ViewerGetType(viewer,&vtype); CHKERRQ(ierr);
4919bcc07fSBarry Smith   if (vtype == ASCII_FILE_VIEWER || vtype == ASCII_FILES_VIEWER) {
5090ace30eSBarry Smith     ierr = ViewerASCIIGetPointer(viewer,&fd); CHKERRQ(ierr);
5177c4ece6SBarry Smith     PetscFPrintf(snes->comm,fd,"SNES Object:\n");
524b0e389bSBarry Smith     SNESGetType(snes,PETSC_NULL,&method);
5377c4ece6SBarry Smith     PetscFPrintf(snes->comm,fd,"  method: %s\n",method);
549b94acceSBarry Smith     if (snes->view) (*snes->view)((PetscObject)snes,viewer);
5577c4ece6SBarry Smith     PetscFPrintf(snes->comm,fd,
569b94acceSBarry Smith       "  maximum iterations=%d, maximum function evaluations=%d\n",
579b94acceSBarry Smith       snes->max_its,snes->max_funcs);
5877c4ece6SBarry Smith     PetscFPrintf(snes->comm,fd,
599b94acceSBarry Smith     "  tolerances: relative=%g, absolute=%g, truncation=%g, solution=%g\n",
609b94acceSBarry Smith       snes->rtol, snes->atol, snes->trunctol, snes->xtol);
619b94acceSBarry Smith     if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION)
6277c4ece6SBarry Smith       PetscFPrintf(snes->comm,fd,"  min function tolerance=%g\n",snes->fmin);
639b94acceSBarry Smith     if (snes->ksp_ewconv) {
649b94acceSBarry Smith       kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
659b94acceSBarry Smith       if (kctx) {
6677c4ece6SBarry Smith         PetscFPrintf(snes->comm,fd,
679b94acceSBarry Smith      "  Eisenstat-Walker computation of KSP relative tolerance (version %d)\n",
689b94acceSBarry Smith         kctx->version);
6977c4ece6SBarry Smith         PetscFPrintf(snes->comm,fd,
709b94acceSBarry Smith           "    rtol_0=%g, rtol_max=%g, threshold=%g\n",kctx->rtol_0,
719b94acceSBarry Smith           kctx->rtol_max,kctx->threshold);
7277c4ece6SBarry Smith         PetscFPrintf(snes->comm,fd,"    gamma=%g, alpha=%g, alpha2=%g\n",
739b94acceSBarry Smith           kctx->gamma,kctx->alpha,kctx->alpha2);
749b94acceSBarry Smith       }
759b94acceSBarry Smith     }
76*c30f285eSLois Curfman McInnes   } else if (vtype == STRING_VIEWER) {
77*c30f285eSLois Curfman McInnes     SNESGetType(snes,PETSC_NULL,&method);
78*c30f285eSLois Curfman McInnes     ViewerStringSPrintf(viewer," %-3.3s",method);
7919bcc07fSBarry Smith   }
809b94acceSBarry Smith   SNESGetSLES(snes,&sles);
819b94acceSBarry Smith   ierr = SLESView(sles,viewer); CHKERRQ(ierr);
829b94acceSBarry Smith   return 0;
839b94acceSBarry Smith }
849b94acceSBarry Smith 
859b94acceSBarry Smith /*@
86682d7d0cSBarry Smith    SNESSetFromOptions - Sets various SNES and SLES parameters from user options.
879b94acceSBarry Smith 
889b94acceSBarry Smith    Input Parameter:
899b94acceSBarry Smith .  snes - the SNES context
909b94acceSBarry Smith 
919b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database
929b94acceSBarry Smith 
93a86d99e1SLois Curfman McInnes .seealso: SNESPrintHelp(), SNESSetOptionsPrefix()
949b94acceSBarry Smith @*/
959b94acceSBarry Smith int SNESSetFromOptions(SNES snes)
969b94acceSBarry Smith {
974b0e389bSBarry Smith   SNESType method;
989b94acceSBarry Smith   double   tmp;
999b94acceSBarry Smith   SLES     sles;
1003c7409f5SSatish Balay   int      ierr, flg;
1013c7409f5SSatish Balay   int      version   = PETSC_DEFAULT;
1029b94acceSBarry Smith   double   rtol_0    = PETSC_DEFAULT;
1039b94acceSBarry Smith   double   rtol_max  = PETSC_DEFAULT;
1049b94acceSBarry Smith   double   gamma2    = PETSC_DEFAULT;
1059b94acceSBarry Smith   double   alpha     = PETSC_DEFAULT;
1069b94acceSBarry Smith   double   alpha2    = PETSC_DEFAULT;
1079b94acceSBarry Smith   double   threshold = PETSC_DEFAULT;
1089b94acceSBarry Smith 
10977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1104b0e389bSBarry Smith   if (snes->setup_called)SETERRQ(1,"SNESSetFromOptions:Must call prior to SNESSetUp!");
111052efed2SBarry Smith   ierr = SNESGetTypeFromOptions_Private(snes,&method,&flg); CHKERRQ(ierr);
112052efed2SBarry Smith   if (flg) {
113052efed2SBarry Smith     ierr = SNESSetType(snes,method); CHKERRQ(ierr);
1149b94acceSBarry Smith   }
1155334005bSBarry Smith   else if (!snes->set_method_called) {
1165334005bSBarry Smith     if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
1175334005bSBarry Smith       ierr = SNESSetType(snes,SNES_EQ_NLS); CHKERRQ(ierr);
1185334005bSBarry Smith     }
1195334005bSBarry Smith     else {
1205334005bSBarry Smith       ierr = SNESSetType(snes,SNES_UM_NTR); CHKERRQ(ierr);
1215334005bSBarry Smith     }
1225334005bSBarry Smith   }
1235334005bSBarry Smith 
1243c7409f5SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-help", &flg); CHKERRQ(ierr);
1253c7409f5SSatish Balay   if (flg) { SNESPrintHelp(snes); }
1263c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_stol",&tmp, &flg); CHKERRQ(ierr);
1273c7409f5SSatish Balay   if (flg) { SNESSetSolutionTolerance(snes,tmp); }
1283c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_atol",&tmp, &flg); CHKERRQ(ierr);
1293c7409f5SSatish Balay   if (flg) { SNESSetAbsoluteTolerance(snes,tmp); }
1303c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_trtol",&tmp, &flg);  CHKERRQ(ierr);
1313c7409f5SSatish Balay   if (flg) { SNESSetTrustRegionTolerance(snes,tmp); }
1323c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_rtol",&tmp, &flg);  CHKERRQ(ierr);
1333c7409f5SSatish Balay   if (flg) { SNESSetRelativeTolerance(snes,tmp); }
1343c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_fmin",&tmp, &flg);  CHKERRQ(ierr);
13577c4ece6SBarry Smith   if (flg) { SNESSetMinimizationFunctionTolerance(snes,tmp); }
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);
1383c7409f5SSatish Balay   ierr = OptionsHasName(snes->prefix,"-snes_ksp_ew_conv", &flg);  CHKERRQ(ierr);
1393c7409f5SSatish Balay   if (flg) { snes->ksp_ewconv = 1; }
1403c7409f5SSatish Balay   ierr = OptionsGetInt(snes->prefix,"-snes_ksp_ew_version",&version, \
1413c7409f5SSatish Balay                        &flg); CHKERRQ(ierr);
1423c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtol0",&rtol_0, \
1433c7409f5SSatish Balay                           &flg); CHKERRQ(ierr);
1443c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtolmax",&rtol_max, \
1453c7409f5SSatish Balay                           &flg);  CHKERRQ(ierr);
1463c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_gamma",&gamma2, \
1473c7409f5SSatish Balay                           &flg);  CHKERRQ(ierr);
1483c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha",&alpha, \
1493c7409f5SSatish Balay                           &flg);  CHKERRQ(ierr);
1503c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha2",&alpha2, \
1513c7409f5SSatish Balay                           &flg);  CHKERRQ(ierr);
1523c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_threshold",&threshold, \
1533c7409f5SSatish Balay                           &flg);  CHKERRQ(ierr);
1549b94acceSBarry Smith   ierr = SNES_KSP_SetParametersEW(snes,version,rtol_0,rtol_max,gamma2,alpha,
1559b94acceSBarry Smith                                   alpha2,threshold); CHKERRQ(ierr);
1563c7409f5SSatish Balay   ierr = OptionsHasName(snes->prefix,"-snes_monitor", &flg);  CHKERRQ(ierr);
1573c7409f5SSatish Balay   if (flg) { SNESSetMonitor(snes,SNESDefaultMonitor,0); }
1583c7409f5SSatish Balay   ierr = OptionsHasName(snes->prefix,"-snes_smonitor", &flg);  CHKERRQ(ierr);
1593c7409f5SSatish Balay    if (flg) { SNESSetMonitor(snes,SNESDefaultSMonitor,0); }
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;
16417699dbbSLois Curfman McInnes     MPI_Initialized(&rank);
16517699dbbSLois Curfman McInnes     if (rank) MPI_Comm_rank(snes->comm,&rank);
16617699dbbSLois Curfman McInnes     if (!rank) {
1679b94acceSBarry Smith       ierr = SNESLGMonitorCreate(0,0,0,0,300,300,&lg); CHKERRQ(ierr);
1689b94acceSBarry Smith       ierr = SNESSetMonitor(snes,SNESLGMonitor,(void *)lg); CHKERRQ(ierr);
1699b94acceSBarry Smith       PLogObjectParent(snes,lg);
1709b94acceSBarry Smith     }
1719b94acceSBarry Smith   }
1723c7409f5SSatish Balay   ierr = OptionsHasName(snes->prefix,"-snes_fd", &flg);  CHKERRQ(ierr);
1733c7409f5SSatish Balay   if (flg && snes->method_class == SNES_NONLINEAR_EQUATIONS) {
1749b94acceSBarry Smith     ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,
1759b94acceSBarry Smith                  SNESDefaultComputeJacobian,snes->funP); CHKERRQ(ierr);
17631615d04SLois Curfman McInnes     PLogInfo((PetscObject)snes,
17731615d04SLois Curfman McInnes       "SNESSetFromOptions: Setting default finite difference Jacobian matrix\n");
17831615d04SLois Curfman McInnes   }
17931615d04SLois Curfman McInnes   else if (flg && snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
18031615d04SLois Curfman McInnes     ierr = SNESSetHessian(snes,snes->jacobian,snes->jacobian_pre,
18131615d04SLois Curfman McInnes                  SNESDefaultComputeHessian,snes->funP); CHKERRQ(ierr);
18231615d04SLois Curfman McInnes     PLogInfo((PetscObject)snes,
18331615d04SLois Curfman McInnes       "SNESSetFromOptions: Setting default finite difference Hessian matrix\n");
1849b94acceSBarry Smith   }
1859b94acceSBarry Smith   ierr = SNESGetSLES(snes,&sles); CHKERRQ(ierr);
1869b94acceSBarry Smith   ierr = SLESSetFromOptions(sles); CHKERRQ(ierr);
1879b94acceSBarry Smith   if (!snes->setfromoptions) return 0;
1889b94acceSBarry Smith   return (*snes->setfromoptions)(snes);
1899b94acceSBarry Smith }
1909b94acceSBarry Smith 
1919b94acceSBarry Smith /*@
1929b94acceSBarry Smith    SNESPrintHelp - Prints all options for the SNES component.
1939b94acceSBarry Smith 
1949b94acceSBarry Smith    Input Parameter:
1959b94acceSBarry Smith .  snes - the SNES context
1969b94acceSBarry Smith 
197a703fe33SLois Curfman McInnes    Options Database Keys:
198a703fe33SLois Curfman McInnes $  -help, -h
199a703fe33SLois Curfman McInnes 
2009b94acceSBarry Smith .keywords: SNES, nonlinear, help
2019b94acceSBarry Smith 
202682d7d0cSBarry Smith .seealso: SNESSetFromOptions()
2039b94acceSBarry Smith @*/
2049b94acceSBarry Smith int SNESPrintHelp(SNES snes)
2059b94acceSBarry Smith {
2066daaf66cSBarry Smith   char                p[64];
2076daaf66cSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
2086daaf66cSBarry Smith 
20977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2106daaf66cSBarry Smith 
2116daaf66cSBarry Smith   PetscStrcpy(p,"-");
2126daaf66cSBarry Smith   if (snes->prefix) PetscStrcat(p, snes->prefix);
2136daaf66cSBarry Smith 
2146daaf66cSBarry Smith   kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
2156daaf66cSBarry Smith 
21677c4ece6SBarry Smith   PetscPrintf(snes->comm,"SNES options ----------------------------\n");
21733455573SSatish Balay   SNESPrintTypes_Private(snes->comm,p,"snes_type");
21877c4ece6SBarry Smith   PetscPrintf(snes->comm," %ssnes_monitor: use default SNES monitor\n",p);
21977c4ece6SBarry Smith   PetscPrintf(snes->comm," %ssnes_view: view SNES info after each nonlinear solve\n",p);
22077c4ece6SBarry Smith   PetscPrintf(snes->comm," %ssnes_max_it its (default %d)\n",p,snes->max_its);
22177c4ece6SBarry Smith   PetscPrintf(snes->comm," %ssnes_stol tol (default %g)\n",p,snes->xtol);
22277c4ece6SBarry Smith   PetscPrintf(snes->comm," %ssnes_atol tol (default %g)\n",p,snes->atol);
22377c4ece6SBarry Smith   PetscPrintf(snes->comm," %ssnes_rtol tol (default %g)\n",p,snes->rtol);
22477c4ece6SBarry Smith   PetscPrintf(snes->comm," %ssnes_ttol tol (default %g)\n",p,snes->trunctol);
22577c4ece6SBarry Smith   PetscPrintf(snes->comm,
2269b94acceSBarry Smith    " options for solving systems of nonlinear equations only:\n");
22777c4ece6SBarry Smith   PetscPrintf(snes->comm,"   %ssnes_fd: use finite differences for Jacobian\n",p);
22877c4ece6SBarry Smith   PetscPrintf(snes->comm,"   %ssnes_mf: use matrix-free Jacobian\n",p);
22977c4ece6SBarry Smith   PetscPrintf(snes->comm,"   %ssnes_ksp_ew_conv: use Eisenstat-Walker computation of KSP rtol. Params are:\n",p);
23077c4ece6SBarry Smith   PetscPrintf(snes->comm,
2319b94acceSBarry Smith    "     %ssnes_ksp_ew_version version (1 or 2, default is %d)\n",
2326daaf66cSBarry Smith    p,kctx->version);
23377c4ece6SBarry Smith   PetscPrintf(snes->comm,
2349b94acceSBarry Smith    "     %ssnes_ksp_ew_rtol0 rtol0 (0 <= rtol0 < 1, default %g)\n",
2356daaf66cSBarry Smith    p,kctx->rtol_0);
23677c4ece6SBarry Smith   PetscPrintf(snes->comm,
2379b94acceSBarry Smith    "     %ssnes_ksp_ew_rtolmax rtolmax (0 <= rtolmax < 1, default %g)\n",
2386daaf66cSBarry Smith    p,kctx->rtol_max);
23977c4ece6SBarry Smith   PetscPrintf(snes->comm,
2409b94acceSBarry Smith    "     %ssnes_ksp_ew_gamma gamma (0 <= gamma <= 1, default %g)\n",
2416daaf66cSBarry Smith    p,kctx->gamma);
24277c4ece6SBarry Smith   PetscPrintf(snes->comm,
2439b94acceSBarry Smith    "     %ssnes_ksp_ew_alpha alpha (1 < alpha <= 2, default %g)\n",
2446daaf66cSBarry Smith    p,kctx->alpha);
24577c4ece6SBarry Smith   PetscPrintf(snes->comm,
2469b94acceSBarry Smith    "     %ssnes_ksp_ew_alpha2 alpha2 (default %g)\n",
2476daaf66cSBarry Smith    p,kctx->alpha2);
24877c4ece6SBarry Smith   PetscPrintf(snes->comm,
2499b94acceSBarry Smith    "     %ssnes_ksp_ew_threshold threshold (0 < threshold < 1, default %g)\n",
2506daaf66cSBarry Smith    p,kctx->threshold);
25177c4ece6SBarry Smith   PetscPrintf(snes->comm,
2529b94acceSBarry Smith    " options for solving unconstrained minimization problems only:\n");
25377c4ece6SBarry Smith   PetscPrintf(snes->comm,"   %ssnes_fmin tol (default %g)\n",p,snes->fmin);
25477c4ece6SBarry Smith   PetscPrintf(snes->comm," Run program with %ssnes_type method -help for help on ",p);
25577c4ece6SBarry Smith   PetscPrintf(snes->comm,"   %ssnes_fd: use finite differences for Hessian\n",p);
25677c4ece6SBarry Smith   PetscPrintf(snes->comm,"   %ssnes_mf: use matrix-free Hessian\n",p);
25777c4ece6SBarry Smith   PetscPrintf(snes->comm,"a particular method\n");
2586daaf66cSBarry Smith   if (snes->printhelp) (*snes->printhelp)(snes,p);
2599b94acceSBarry Smith   return 0;
2609b94acceSBarry Smith }
2619b94acceSBarry Smith /*@
2629b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
2639b94acceSBarry Smith    the nonlinear solvers.
2649b94acceSBarry Smith 
2659b94acceSBarry Smith    Input Parameters:
2669b94acceSBarry Smith .  snes - the SNES context
2679b94acceSBarry Smith .  usrP - optional user context
2689b94acceSBarry Smith 
2699b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
2709b94acceSBarry Smith 
2719b94acceSBarry Smith .seealso: SNESGetApplicationContext()
2729b94acceSBarry Smith @*/
2739b94acceSBarry Smith int SNESSetApplicationContext(SNES snes,void *usrP)
2749b94acceSBarry Smith {
27577c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2769b94acceSBarry Smith   snes->user		= usrP;
2779b94acceSBarry Smith   return 0;
2789b94acceSBarry Smith }
2799b94acceSBarry Smith /*@C
2809b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
2819b94acceSBarry Smith    nonlinear solvers.
2829b94acceSBarry Smith 
2839b94acceSBarry Smith    Input Parameter:
2849b94acceSBarry Smith .  snes - SNES context
2859b94acceSBarry Smith 
2869b94acceSBarry Smith    Output Parameter:
2879b94acceSBarry Smith .  usrP - user context
2889b94acceSBarry Smith 
2899b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
2909b94acceSBarry Smith 
2919b94acceSBarry Smith .seealso: SNESSetApplicationContext()
2929b94acceSBarry Smith @*/
2939b94acceSBarry Smith int SNESGetApplicationContext( SNES snes,  void **usrP )
2949b94acceSBarry Smith {
29577c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
2969b94acceSBarry Smith   *usrP = snes->user;
2979b94acceSBarry Smith   return 0;
2989b94acceSBarry Smith }
2999b94acceSBarry Smith /*@
3009b94acceSBarry Smith    SNESGetIterationNumber - Gets the current iteration number of the
3019b94acceSBarry Smith    nonlinear solver.
3029b94acceSBarry Smith 
3039b94acceSBarry Smith    Input Parameter:
3049b94acceSBarry Smith .  snes - SNES context
3059b94acceSBarry Smith 
3069b94acceSBarry Smith    Output Parameter:
3079b94acceSBarry Smith .  iter - iteration number
3089b94acceSBarry Smith 
3099b94acceSBarry Smith .keywords: SNES, nonlinear, get, iteration, number
3109b94acceSBarry Smith @*/
3119b94acceSBarry Smith int SNESGetIterationNumber(SNES snes,int* iter)
3129b94acceSBarry Smith {
31377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
3149b94acceSBarry Smith   *iter = snes->iter;
3159b94acceSBarry Smith   return 0;
3169b94acceSBarry Smith }
3179b94acceSBarry Smith /*@
3189b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
3199b94acceSBarry Smith    with SNESSSetFunction().
3209b94acceSBarry Smith 
3219b94acceSBarry Smith    Input Parameter:
3229b94acceSBarry Smith .  snes - SNES context
3239b94acceSBarry Smith 
3249b94acceSBarry Smith    Output Parameter:
3259b94acceSBarry Smith .  fnorm - 2-norm of function
3269b94acceSBarry Smith 
3279b94acceSBarry Smith    Note:
3289b94acceSBarry Smith    SNESGetFunctionNorm() is valid for SNES_NONLINEAR_EQUATIONS methods only.
329a86d99e1SLois Curfman McInnes    A related routine for SNES_UNCONSTRAINED_MINIMIZATION methods is
330a86d99e1SLois Curfman McInnes    SNESGetGradientNorm().
3319b94acceSBarry Smith 
3329b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
333a86d99e1SLois Curfman McInnes 
334a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction()
3359b94acceSBarry Smith @*/
3369b94acceSBarry Smith int SNESGetFunctionNorm(SNES snes,Scalar *fnorm)
3379b94acceSBarry Smith {
33877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
3399b94acceSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1,
34048d91487SBarry Smith     "SNESGetFunctionNorm:For SNES_NONLINEAR_EQUATIONS only");
3419b94acceSBarry Smith   *fnorm = snes->norm;
3429b94acceSBarry Smith   return 0;
3439b94acceSBarry Smith }
3449b94acceSBarry Smith /*@
3459b94acceSBarry Smith    SNESGetGradientNorm - Gets the norm of the current gradient that was set
3469b94acceSBarry Smith    with SNESSSetGradient().
3479b94acceSBarry Smith 
3489b94acceSBarry Smith    Input Parameter:
3499b94acceSBarry Smith .  snes - SNES context
3509b94acceSBarry Smith 
3519b94acceSBarry Smith    Output Parameter:
3529b94acceSBarry Smith .  fnorm - 2-norm of gradient
3539b94acceSBarry Smith 
3549b94acceSBarry Smith    Note:
3559b94acceSBarry Smith    SNESGetGradientNorm() is valid for SNES_UNCONSTRAINED_MINIMIZATION
356a86d99e1SLois Curfman McInnes    methods only.  A related routine for SNES_NONLINEAR_EQUATIONS methods
357a86d99e1SLois Curfman McInnes    is SNESGetFunctionNorm().
3589b94acceSBarry Smith 
3599b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient, norm
360a86d99e1SLois Curfman McInnes 
361a86d99e1SLois Curfman McInnes .seelso: SNESSetGradient()
3629b94acceSBarry Smith @*/
3639b94acceSBarry Smith int SNESGetGradientNorm(SNES snes,Scalar *gnorm)
3649b94acceSBarry Smith {
36577c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
3669b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
36748d91487SBarry Smith     "SNESGetGradientNorm:For SNES_UNCONSTRAINED_MINIMIZATION only");
3689b94acceSBarry Smith   *gnorm = snes->norm;
3699b94acceSBarry Smith   return 0;
3709b94acceSBarry Smith }
3719b94acceSBarry Smith /*@
3729b94acceSBarry Smith    SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps
3739b94acceSBarry Smith    attempted by the nonlinear solver.
3749b94acceSBarry Smith 
3759b94acceSBarry Smith    Input Parameter:
3769b94acceSBarry Smith .  snes - SNES context
3779b94acceSBarry Smith 
3789b94acceSBarry Smith    Output Parameter:
3799b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
3809b94acceSBarry Smith 
3819b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
3829b94acceSBarry Smith @*/
3839b94acceSBarry Smith int SNESGetNumberUnsuccessfulSteps(SNES snes,int* nfails)
3849b94acceSBarry Smith {
38577c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
3869b94acceSBarry Smith   *nfails = snes->nfailures;
3879b94acceSBarry Smith   return 0;
3889b94acceSBarry Smith }
3899b94acceSBarry Smith /*@C
3909b94acceSBarry Smith    SNESGetSLES - Returns the SLES context for a SNES solver.
3919b94acceSBarry Smith 
3929b94acceSBarry Smith    Input Parameter:
3939b94acceSBarry Smith .  snes - the SNES context
3949b94acceSBarry Smith 
3959b94acceSBarry Smith    Output Parameter:
3969b94acceSBarry Smith .  sles - the SLES context
3979b94acceSBarry Smith 
3989b94acceSBarry Smith    Notes:
3999b94acceSBarry Smith    The user can then directly manipulate the SLES context to set various
4009b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
4019b94acceSBarry Smith    KSP and PC contexts as well.
4029b94acceSBarry Smith 
4039b94acceSBarry Smith .keywords: SNES, nonlinear, get, SLES, context
4049b94acceSBarry Smith 
4059b94acceSBarry Smith .seealso: SLESGetPC(), SLESGetKSP()
4069b94acceSBarry Smith @*/
4079b94acceSBarry Smith int SNESGetSLES(SNES snes,SLES *sles)
4089b94acceSBarry Smith {
40977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
4109b94acceSBarry Smith   *sles = snes->sles;
4119b94acceSBarry Smith   return 0;
4129b94acceSBarry Smith }
4139b94acceSBarry Smith /* -----------------------------------------------------------*/
4149b94acceSBarry Smith /*@C
4159b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
4169b94acceSBarry Smith 
4179b94acceSBarry Smith    Input Parameter:
4189b94acceSBarry Smith .  comm - MPI communicator
4199b94acceSBarry Smith .  type - type of method, one of
4209b94acceSBarry Smith $    SNES_NONLINEAR_EQUATIONS
4219b94acceSBarry Smith $      (for systems of nonlinear equations)
4229b94acceSBarry Smith $    SNES_UNCONSTRAINED_MINIMIZATION
4239b94acceSBarry Smith $      (for unconstrained minimization)
4249b94acceSBarry Smith 
4259b94acceSBarry Smith    Output Parameter:
4269b94acceSBarry Smith .  outsnes - the new SNES context
4279b94acceSBarry Smith 
4289b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
4299b94acceSBarry Smith 
43063a78c88SLois Curfman McInnes .seealso: SNESSolve(), SNESDestroy()
4319b94acceSBarry Smith @*/
4324b0e389bSBarry Smith int SNESCreate(MPI_Comm comm,SNESProblemType type,SNES *outsnes)
4339b94acceSBarry Smith {
4349b94acceSBarry Smith   int                 ierr;
4359b94acceSBarry Smith   SNES                snes;
4369b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
43737fcc0dbSBarry Smith 
4389b94acceSBarry Smith   *outsnes = 0;
4392a0acf01SLois Curfman McInnes   if (type != SNES_UNCONSTRAINED_MINIMIZATION && type != SNES_NONLINEAR_EQUATIONS)
4402a0acf01SLois Curfman McInnes     SETERRQ(1,"SNESCreate:incorrect method type");
4410452661fSBarry Smith   PetscHeaderCreate(snes,_SNES,SNES_COOKIE,SNES_UNKNOWN_METHOD,comm);
4429b94acceSBarry Smith   PLogObjectCreate(snes);
4439b94acceSBarry Smith   snes->max_its           = 50;
4449b94acceSBarry Smith   snes->max_funcs	  = 1000;
4459b94acceSBarry Smith   snes->norm		  = 0.0;
4465a2d0531SBarry Smith   if (type == SNES_UNCONSTRAINED_MINIMIZATION) {
4472a0acf01SLois Curfman McInnes     snes->rtol		  = 1.e-08;
4489b94acceSBarry Smith     snes->atol		  = 1.e-10;
4495a2d0531SBarry Smith   }
450b4874afaSBarry Smith   else {
451b4874afaSBarry Smith     snes->rtol		  = 1.e-8;
452b4874afaSBarry Smith     snes->ttol            = 0.0;
453b4874afaSBarry Smith     snes->atol		  = 1.e-50;
454b4874afaSBarry Smith   }
4559b94acceSBarry Smith   snes->xtol		  = 1.e-8;
4569b94acceSBarry Smith   snes->trunctol	  = 1.e-12;
4579b94acceSBarry Smith   snes->nfuncs            = 0;
4589b94acceSBarry Smith   snes->nfailures         = 0;
4599b94acceSBarry Smith   snes->monitor           = 0;
4609b94acceSBarry Smith   snes->data              = 0;
4619b94acceSBarry Smith   snes->view              = 0;
4629b94acceSBarry Smith   snes->computeumfunction = 0;
4639b94acceSBarry Smith   snes->umfunP            = 0;
4649b94acceSBarry Smith   snes->fc                = 0;
4659b94acceSBarry Smith   snes->deltatol          = 1.e-12;
4669b94acceSBarry Smith   snes->fmin              = -1.e30;
4679b94acceSBarry Smith   snes->method_class      = type;
4689b94acceSBarry Smith   snes->set_method_called = 0;
469a703fe33SLois Curfman McInnes   snes->setup_called      = 0;
4709b94acceSBarry Smith   snes->ksp_ewconv        = 0;
4719b94acceSBarry Smith 
4729b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
4730452661fSBarry Smith   kctx = PetscNew(SNES_KSP_EW_ConvCtx); CHKPTRQ(kctx);
4749b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
4759b94acceSBarry Smith   kctx->version     = 2;
4769b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
4779b94acceSBarry Smith                              this was too large for some test cases */
4789b94acceSBarry Smith   kctx->rtol_last   = 0;
4799b94acceSBarry Smith   kctx->rtol_max    = .9;
4809b94acceSBarry Smith   kctx->gamma       = 1.0;
4819b94acceSBarry Smith   kctx->alpha2      = .5*(1.0 + sqrt(5.0));
4829b94acceSBarry Smith   kctx->alpha       = kctx->alpha2;
4839b94acceSBarry Smith   kctx->threshold   = .1;
4849b94acceSBarry Smith   kctx->lresid_last = 0;
4859b94acceSBarry Smith   kctx->norm_last   = 0;
4869b94acceSBarry Smith 
4879b94acceSBarry Smith   ierr = SLESCreate(comm,&snes->sles); CHKERRQ(ierr);
4889b94acceSBarry Smith   PLogObjectParent(snes,snes->sles)
4895334005bSBarry Smith 
4909b94acceSBarry Smith   *outsnes = snes;
4919b94acceSBarry Smith   return 0;
4929b94acceSBarry Smith }
4939b94acceSBarry Smith 
4949b94acceSBarry Smith /* --------------------------------------------------------------- */
4959b94acceSBarry Smith /*@C
4969b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
4979b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
4989b94acceSBarry Smith    equations.
4999b94acceSBarry Smith 
5009b94acceSBarry Smith    Input Parameters:
5019b94acceSBarry Smith .  snes - the SNES context
5029b94acceSBarry Smith .  func - function evaluation routine
5039b94acceSBarry Smith .  ctx - optional user-defined function context
5049b94acceSBarry Smith .  r - vector to store function value
5059b94acceSBarry Smith 
5069b94acceSBarry Smith    Calling sequence of func:
5079b94acceSBarry Smith .  func (SNES, Vec x, Vec f, void *ctx);
5089b94acceSBarry Smith 
5099b94acceSBarry Smith .  x - input vector
5105334005bSBarry Smith .  f - vector function
5119b94acceSBarry Smith .  ctx - optional user-defined context for private data for the
5129b94acceSBarry Smith          function evaluation routine (may be null)
5139b94acceSBarry Smith 
5149b94acceSBarry Smith    Notes:
5159b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
5169b94acceSBarry Smith $      f'(x) x = -f(x),
5179b94acceSBarry Smith $  where f'(x) denotes the Jacobian matrix and f(x) is the function.
5189b94acceSBarry Smith 
5199b94acceSBarry Smith    SNESSetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only.
5209b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
5219b94acceSBarry Smith    SNESSetMinimizationFunction() and SNESSetGradient();
5229b94acceSBarry Smith 
5239b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
5249b94acceSBarry Smith 
525a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
5269b94acceSBarry Smith @*/
5275334005bSBarry Smith int SNESSetFunction( SNES snes, Vec r, int (*func)(SNES,Vec,Vec,void*),void *ctx)
5289b94acceSBarry Smith {
52977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
5309b94acceSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1,
53148d91487SBarry Smith     "SNESSetFunction:For SNES_NONLINEAR_EQUATIONS only");
5329b94acceSBarry Smith   snes->computefunction     = func;
5339b94acceSBarry Smith   snes->vec_func            = snes->vec_func_always = r;
5349b94acceSBarry Smith   snes->funP                = ctx;
5359b94acceSBarry Smith   return 0;
5369b94acceSBarry Smith }
5379b94acceSBarry Smith 
5389b94acceSBarry Smith /*@
5399b94acceSBarry Smith    SNESComputeFunction - Computes the function that has been set with
5409b94acceSBarry Smith    SNESSetFunction().
5419b94acceSBarry Smith 
5429b94acceSBarry Smith    Input Parameters:
5439b94acceSBarry Smith .  snes - the SNES context
5449b94acceSBarry Smith .  x - input vector
5459b94acceSBarry Smith 
5469b94acceSBarry Smith    Output Parameter:
5473638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
5489b94acceSBarry Smith 
549a86d99e1SLois Curfman McInnes n   Notes:
5509b94acceSBarry Smith    SNESComputeFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only.
5519b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
5529b94acceSBarry Smith    SNESComputeMinimizationFunction() and SNESComputeGradient();
5539b94acceSBarry Smith 
5549b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
5559b94acceSBarry Smith 
556a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
5579b94acceSBarry Smith @*/
5589b94acceSBarry Smith int SNESComputeFunction(SNES snes,Vec x, Vec y)
5599b94acceSBarry Smith {
5609b94acceSBarry Smith   int    ierr;
5619b94acceSBarry Smith 
562a86d99e1SLois Curfman McInnes   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1,
563a86d99e1SLois Curfman McInnes     "SNESComputeFunction: For SNES_NONLINEAR_EQUATIONS only");
5649b94acceSBarry Smith   PLogEventBegin(SNES_FunctionEval,snes,x,y,0);
5659b94acceSBarry Smith   ierr = (*snes->computefunction)(snes,x,y,snes->funP); CHKERRQ(ierr);
5669b94acceSBarry Smith   PLogEventEnd(SNES_FunctionEval,snes,x,y,0);
5679b94acceSBarry Smith   return 0;
5689b94acceSBarry Smith }
5699b94acceSBarry Smith 
5709b94acceSBarry Smith /*@C
5719b94acceSBarry Smith    SNESSetMinimizationFunction - Sets the function evaluation routine for
5729b94acceSBarry Smith    unconstrained minimization.
5739b94acceSBarry Smith 
5749b94acceSBarry Smith    Input Parameters:
5759b94acceSBarry Smith .  snes - the SNES context
5769b94acceSBarry Smith .  func - function evaluation routine
5779b94acceSBarry Smith .  ctx - optional user-defined function context
5789b94acceSBarry Smith 
5799b94acceSBarry Smith    Calling sequence of func:
5809b94acceSBarry Smith .  func (SNES snes,Vec x,double *f,void *ctx);
5819b94acceSBarry Smith 
5829b94acceSBarry Smith .  x - input vector
5839b94acceSBarry Smith .  f - function
5849b94acceSBarry Smith .  ctx - optional user-defined context for private data for the
5859b94acceSBarry Smith          function evaluation routine (may be null)
5869b94acceSBarry Smith 
5879b94acceSBarry Smith    Notes:
5889b94acceSBarry Smith    SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
5899b94acceSBarry Smith    methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
5909b94acceSBarry Smith    SNESSetFunction().
5919b94acceSBarry Smith 
5929b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimization, function
5939b94acceSBarry Smith 
594a86d99e1SLois Curfman McInnes .seealso:  SNESGetMinimizationFunction(), SNESComputeMinimizationFunction(),
595a86d99e1SLois Curfman McInnes            SNESSetHessian(), SNESSetGradient()
5969b94acceSBarry Smith @*/
5979b94acceSBarry Smith int SNESSetMinimizationFunction(SNES snes,int (*func)(SNES,Vec,double*,void*),
5989b94acceSBarry Smith                       void *ctx)
5999b94acceSBarry Smith {
60077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
6019b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
60248d91487SBarry Smith     "SNESSetMinimizationFunction:Only for SNES_UNCONSTRAINED_MINIMIZATION");
6039b94acceSBarry Smith   snes->computeumfunction   = func;
6049b94acceSBarry Smith   snes->umfunP              = ctx;
6059b94acceSBarry Smith   return 0;
6069b94acceSBarry Smith }
6079b94acceSBarry Smith 
6089b94acceSBarry Smith /*@
6099b94acceSBarry Smith    SNESComputeMinimizationFunction - Computes the function that has been
6109b94acceSBarry Smith    set with SNESSetMinimizationFunction().
6119b94acceSBarry Smith 
6129b94acceSBarry Smith    Input Parameters:
6139b94acceSBarry Smith .  snes - the SNES context
6149b94acceSBarry Smith .  x - input vector
6159b94acceSBarry Smith 
6169b94acceSBarry Smith    Output Parameter:
6179b94acceSBarry Smith .  y - function value
6189b94acceSBarry Smith 
6199b94acceSBarry Smith    Notes:
6209b94acceSBarry Smith    SNESComputeMinimizationFunction() is valid only for
6219b94acceSBarry Smith    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
6229b94acceSBarry Smith    SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction().
623a86d99e1SLois Curfman McInnes 
624a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, minimization, function
625a86d99e1SLois Curfman McInnes 
626a86d99e1SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESGetMinimizationFunction(),
627a86d99e1SLois Curfman McInnes           SNESComputeGradient(), SNESComputeHessian()
6289b94acceSBarry Smith @*/
6299b94acceSBarry Smith int SNESComputeMinimizationFunction(SNES snes,Vec x,double *y)
6309b94acceSBarry Smith {
6319b94acceSBarry Smith   int    ierr;
6329b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
63348d91487SBarry Smith     "SNESComputeMinimizationFunction:Only for SNES_UNCONSTRAINED_MINIMIZATION");
6349b94acceSBarry Smith   PLogEventBegin(SNES_MinimizationFunctionEval,snes,x,y,0);
6359b94acceSBarry Smith   ierr = (*snes->computeumfunction)(snes,x,y,snes->umfunP); CHKERRQ(ierr);
6369b94acceSBarry Smith   PLogEventEnd(SNES_MinimizationFunctionEval,snes,x,y,0);
6379b94acceSBarry Smith   return 0;
6389b94acceSBarry Smith }
6399b94acceSBarry Smith 
6409b94acceSBarry Smith /*@C
6419b94acceSBarry Smith    SNESSetGradient - Sets the gradient evaluation routine and gradient
6429b94acceSBarry Smith    vector for use by the SNES routines.
6439b94acceSBarry Smith 
6449b94acceSBarry Smith    Input Parameters:
6459b94acceSBarry Smith .  snes - the SNES context
6469b94acceSBarry Smith .  func - function evaluation routine
6479b94acceSBarry Smith .  ctx - optional user-defined function context
6489b94acceSBarry Smith .  r - vector to store gradient value
6499b94acceSBarry Smith 
6509b94acceSBarry Smith    Calling sequence of func:
6519b94acceSBarry Smith .  func (SNES, Vec x, Vec g, void *ctx);
6529b94acceSBarry Smith 
6539b94acceSBarry Smith .  x - input vector
6549b94acceSBarry Smith .  g - gradient vector
6559b94acceSBarry Smith .  ctx - optional user-defined context for private data for the
6569b94acceSBarry Smith          function evaluation routine (may be null)
6579b94acceSBarry Smith 
6589b94acceSBarry Smith    Notes:
6599b94acceSBarry Smith    SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
6609b94acceSBarry Smith    methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
6619b94acceSBarry Smith    SNESSetFunction().
6629b94acceSBarry Smith 
6639b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
6649b94acceSBarry Smith 
665a86d99e1SLois Curfman McInnes .seealso: SNESGetGradient(), SNESComputeGradient(), SNESSetHessian(),
666a86d99e1SLois Curfman McInnes           SNESSetMinimizationFunction(),
6679b94acceSBarry Smith @*/
6689b94acceSBarry Smith int SNESSetGradient(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),
6699b94acceSBarry Smith                      void *ctx)
6709b94acceSBarry Smith {
67177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
6729b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
67348d91487SBarry Smith     "SNESSetGradient:For SNES_UNCONSTRAINED_MINIMIZATION only");
6749b94acceSBarry Smith   snes->computefunction     = func;
6759b94acceSBarry Smith   snes->vec_func            = snes->vec_func_always = r;
6769b94acceSBarry Smith   snes->funP                = ctx;
6779b94acceSBarry Smith   return 0;
6789b94acceSBarry Smith }
6799b94acceSBarry Smith 
6809b94acceSBarry Smith /*@
681a86d99e1SLois Curfman McInnes    SNESComputeGradient - Computes the gradient that has been set with
682a86d99e1SLois Curfman McInnes    SNESSetGradient().
6839b94acceSBarry Smith 
6849b94acceSBarry Smith    Input Parameters:
6859b94acceSBarry Smith .  snes - the SNES context
6869b94acceSBarry Smith .  x - input vector
6879b94acceSBarry Smith 
6889b94acceSBarry Smith    Output Parameter:
6899b94acceSBarry Smith .  y - gradient vector
6909b94acceSBarry Smith 
6919b94acceSBarry Smith    Notes:
6929b94acceSBarry Smith    SNESComputeGradient() is valid only for
6939b94acceSBarry Smith    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
6949b94acceSBarry Smith    SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction().
695a86d99e1SLois Curfman McInnes 
696a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, gradient
697a86d99e1SLois Curfman McInnes 
698a86d99e1SLois Curfman McInnes .seealso:  SNESSetGradient(), SNESGetGradient(),
699a86d99e1SLois Curfman McInnes            SNESComputeMinimizationFunction(), SNESComputeHessian()
7009b94acceSBarry Smith @*/
7019b94acceSBarry Smith int SNESComputeGradient(SNES snes,Vec x, Vec y)
7029b94acceSBarry Smith {
7039b94acceSBarry Smith   int    ierr;
7049b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
70548d91487SBarry Smith     "SNESComputeGradient:For SNES_UNCONSTRAINED_MINIMIZATION only");
7069b94acceSBarry Smith   PLogEventBegin(SNES_GradientEval,snes,x,y,0);
7079b94acceSBarry Smith   ierr = (*snes->computefunction)(snes,x,y,snes->funP); CHKERRQ(ierr);
7089b94acceSBarry Smith   PLogEventEnd(SNES_GradientEval,snes,x,y,0);
7099b94acceSBarry Smith   return 0;
7109b94acceSBarry Smith }
7119b94acceSBarry Smith 
71262fef451SLois Curfman McInnes /*@
71362fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
71462fef451SLois Curfman McInnes    set with SNESSetJacobian().
71562fef451SLois Curfman McInnes 
71662fef451SLois Curfman McInnes    Input Parameters:
71762fef451SLois Curfman McInnes .  snes - the SNES context
71862fef451SLois Curfman McInnes .  x - input vector
71962fef451SLois Curfman McInnes 
72062fef451SLois Curfman McInnes    Output Parameters:
72162fef451SLois Curfman McInnes .  A - Jacobian matrix
72262fef451SLois Curfman McInnes .  B - optional preconditioning matrix
72362fef451SLois Curfman McInnes .  flag - flag indicating matrix structure
72462fef451SLois Curfman McInnes 
72562fef451SLois Curfman McInnes    Notes:
72662fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
72762fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
72862fef451SLois Curfman McInnes 
72962fef451SLois Curfman McInnes    See SLESSetOperators() for information about setting the flag parameter.
73062fef451SLois Curfman McInnes 
73162fef451SLois Curfman McInnes    SNESComputeJacobian() is valid only for SNES_NONLINEAR_EQUATIONS
73262fef451SLois Curfman McInnes    methods. An analogous routine for SNES_UNCONSTRAINED_MINIMIZATION
73362fef451SLois Curfman McInnes    methods is SNESComputeJacobian().
73462fef451SLois Curfman McInnes 
73562fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
73662fef451SLois Curfman McInnes 
73762fef451SLois Curfman McInnes .seealso:  SNESSetJacobian(), SLESSetOperators()
73862fef451SLois Curfman McInnes @*/
7399b94acceSBarry Smith int SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
7409b94acceSBarry Smith {
7419b94acceSBarry Smith   int    ierr;
7429b94acceSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1,
74348d91487SBarry Smith     "SNESComputeJacobian: For SNES_NONLINEAR_EQUATIONS only");
7449b94acceSBarry Smith   if (!snes->computejacobian) return 0;
7459b94acceSBarry Smith   PLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);
746c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
7479b94acceSBarry Smith   ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP); CHKERRQ(ierr);
7489b94acceSBarry Smith   PLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);
7496d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
75077c4ece6SBarry Smith   PetscValidHeaderSpecific(*A,MAT_COOKIE);
75177c4ece6SBarry Smith   PetscValidHeaderSpecific(*B,MAT_COOKIE);
7529b94acceSBarry Smith   return 0;
7539b94acceSBarry Smith }
7549b94acceSBarry Smith 
75562fef451SLois Curfman McInnes /*@
75662fef451SLois Curfman McInnes    SNESComputeHessian - Computes the Hessian matrix that has been
75762fef451SLois Curfman McInnes    set with SNESSetHessian().
75862fef451SLois Curfman McInnes 
75962fef451SLois Curfman McInnes    Input Parameters:
76062fef451SLois Curfman McInnes .  snes - the SNES context
76162fef451SLois Curfman McInnes .  x - input vector
76262fef451SLois Curfman McInnes 
76362fef451SLois Curfman McInnes    Output Parameters:
76462fef451SLois Curfman McInnes .  A - Hessian matrix
76562fef451SLois Curfman McInnes .  B - optional preconditioning matrix
76662fef451SLois Curfman McInnes .  flag - flag indicating matrix structure
76762fef451SLois Curfman McInnes 
76862fef451SLois Curfman McInnes    Notes:
76962fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
77062fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
77162fef451SLois Curfman McInnes 
77262fef451SLois Curfman McInnes    See SLESSetOperators() for information about setting the flag parameter.
77362fef451SLois Curfman McInnes 
77462fef451SLois Curfman McInnes    SNESComputeHessian() is valid only for
77562fef451SLois Curfman McInnes    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
77662fef451SLois Curfman McInnes    SNES_NONLINEAR_EQUATIONS methods is SNESComputeJacobian().
77762fef451SLois Curfman McInnes 
77862fef451SLois Curfman McInnes .keywords: SNES, compute, Hessian, matrix
77962fef451SLois Curfman McInnes 
780a86d99e1SLois Curfman McInnes .seealso:  SNESSetHessian(), SLESSetOperators(), SNESComputeGradient(),
781a86d99e1SLois Curfman McInnes            SNESComputeMinimizationFunction()
78262fef451SLois Curfman McInnes @*/
78362fef451SLois Curfman McInnes int SNESComputeHessian(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag)
7849b94acceSBarry Smith {
7859b94acceSBarry Smith   int    ierr;
7869b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
78748d91487SBarry Smith     "SNESComputeHessian:For SNES_UNCONSTRAINED_MINIMIZATION only");
7889b94acceSBarry Smith   if (!snes->computejacobian) return 0;
78962fef451SLois Curfman McInnes   PLogEventBegin(SNES_HessianEval,snes,x,*A,*B);
7900f4a323eSLois Curfman McInnes   *flag = DIFFERENT_NONZERO_PATTERN;
79162fef451SLois Curfman McInnes   ierr = (*snes->computejacobian)(snes,x,A,B,flag,snes->jacP); CHKERRQ(ierr);
79262fef451SLois Curfman McInnes   PLogEventEnd(SNES_HessianEval,snes,x,*A,*B);
7930f4a323eSLois Curfman McInnes   /* make sure user returned a correct Jacobian and preconditioner */
79477c4ece6SBarry Smith   PetscValidHeaderSpecific(*A,MAT_COOKIE);
79577c4ece6SBarry Smith   PetscValidHeaderSpecific(*B,MAT_COOKIE);
7969b94acceSBarry Smith   return 0;
7979b94acceSBarry Smith }
7989b94acceSBarry Smith 
7999b94acceSBarry Smith /*@C
8009b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
8019b94acceSBarry Smith    location to store it.
8029b94acceSBarry Smith 
8039b94acceSBarry Smith    Input Parameters:
8049b94acceSBarry Smith .  snes - the SNES context
8059b94acceSBarry Smith .  A - Jacobian matrix
8069b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
8079b94acceSBarry Smith .  func - Jacobian evaluation routine
8089b94acceSBarry Smith .  ctx - optional user-defined context for private data for the
8099b94acceSBarry Smith          Jacobian evaluation routine (may be null)
8109b94acceSBarry Smith 
8119b94acceSBarry Smith    Calling sequence of func:
8129b94acceSBarry Smith .  func (SNES,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
8139b94acceSBarry Smith 
8149b94acceSBarry Smith .  x - input vector
8159b94acceSBarry Smith .  A - Jacobian matrix
8169b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
817ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
818ac21db08SLois Curfman McInnes    structure (same as flag in SLESSetOperators())
8199b94acceSBarry Smith .  ctx - optional user-defined Jacobian context
8209b94acceSBarry Smith 
8219b94acceSBarry Smith    Notes:
822ac21db08SLois Curfman McInnes    See SLESSetOperators() for information about setting the flag input
823ac21db08SLois Curfman McInnes    parameter in the routine func().  Be sure to read this information!
824ac21db08SLois Curfman McInnes 
825ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
8269b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
8279b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
8289b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
8299b94acceSBarry Smith    throughout the global iterations.
8309b94acceSBarry Smith 
8319b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
8329b94acceSBarry Smith 
833ac21db08SLois Curfman McInnes .seealso: SLESSetOperators(), SNESSetFunction()
8349b94acceSBarry Smith @*/
8359b94acceSBarry Smith int SNESSetJacobian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,
8369b94acceSBarry Smith                     MatStructure*,void*),void *ctx)
8379b94acceSBarry Smith {
83877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
8399b94acceSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1,
84048d91487SBarry Smith     "SNESSetJacobian:For SNES_NONLINEAR_EQUATIONS only");
8419b94acceSBarry Smith   snes->computejacobian = func;
8429b94acceSBarry Smith   snes->jacP            = ctx;
8439b94acceSBarry Smith   snes->jacobian        = A;
8449b94acceSBarry Smith   snes->jacobian_pre    = B;
8459b94acceSBarry Smith   return 0;
8469b94acceSBarry Smith }
84762fef451SLois Curfman McInnes 
848b4fd4287SBarry Smith /*@
849b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
850b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
851b4fd4287SBarry Smith 
852b4fd4287SBarry Smith    Input Parameter:
853b4fd4287SBarry Smith .  snes - the nonlinear solver context
854b4fd4287SBarry Smith 
855b4fd4287SBarry Smith    Output Parameters:
856b4fd4287SBarry Smith .  A - location to stash Jacobian matrix (or PETSC_NULL)
857b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
858b4fd4287SBarry Smith .  ctx - location to stash Jacobian ctx (or PETSC_NULL)
859b4fd4287SBarry Smith 
860b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
861b4fd4287SBarry Smith @*/
862b4fd4287SBarry Smith int SNESGetJacobian(SNES snes,Mat *A,Mat *B, void **ctx)
863b4fd4287SBarry Smith {
86462fef451SLois Curfman McInnes   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1,
86562fef451SLois Curfman McInnes     "SNESSetJacobian:For SNES_NONLINEAR_EQUATIONS only");
866b4fd4287SBarry Smith   if (A)   *A = snes->jacobian;
867b4fd4287SBarry Smith   if (B)   *B = snes->jacobian_pre;
868b4fd4287SBarry Smith   if (ctx) *ctx = snes->jacP;
869b4fd4287SBarry Smith   return 0;
870b4fd4287SBarry Smith }
871b4fd4287SBarry Smith 
8729b94acceSBarry Smith /*@C
8739b94acceSBarry Smith    SNESSetHessian - Sets the function to compute Hessian as well as the
8749b94acceSBarry Smith    location to store it.
8759b94acceSBarry Smith 
8769b94acceSBarry Smith    Input Parameters:
8779b94acceSBarry Smith .  snes - the SNES context
8789b94acceSBarry Smith .  A - Hessian matrix
8799b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Hessian)
8809b94acceSBarry Smith .  func - Jacobian evaluation routine
8819b94acceSBarry Smith .  ctx - optional user-defined context for private data for the
8829b94acceSBarry Smith          Hessian evaluation routine (may be null)
8839b94acceSBarry Smith 
8849b94acceSBarry Smith    Calling sequence of func:
8859b94acceSBarry Smith .  func (SNES,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
8869b94acceSBarry Smith 
8879b94acceSBarry Smith .  x - input vector
8889b94acceSBarry Smith .  A - Hessian matrix
8899b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
890ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
891ac21db08SLois Curfman McInnes    structure (same as flag in SLESSetOperators())
8929b94acceSBarry Smith .  ctx - optional user-defined Hessian context
8939b94acceSBarry Smith 
8949b94acceSBarry Smith    Notes:
895ac21db08SLois Curfman McInnes    See SLESSetOperators() for information about setting the flag input
896ac21db08SLois Curfman McInnes    parameter in the routine func().  Be sure to read this information!
897ac21db08SLois Curfman McInnes 
8989b94acceSBarry Smith    The function func() takes Mat * as the matrix arguments rather than Mat.
8999b94acceSBarry Smith    This allows the Hessian evaluation routine to replace A and/or B with a
9009b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
9019b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
9029b94acceSBarry Smith    throughout the global iterations.
9039b94acceSBarry Smith 
9049b94acceSBarry Smith .keywords: SNES, nonlinear, set, Hessian, matrix
9059b94acceSBarry Smith 
906ac21db08SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESSetGradient(), SLESSetOperators()
9079b94acceSBarry Smith @*/
9089b94acceSBarry Smith int SNESSetHessian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,
9099b94acceSBarry Smith                     MatStructure*,void*),void *ctx)
9109b94acceSBarry Smith {
91177c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
9129b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
91348d91487SBarry Smith     "SNESSetHessian:For SNES_UNCONSTRAINED_MINIMIZATION only");
9149b94acceSBarry Smith   snes->computejacobian = func;
9159b94acceSBarry Smith   snes->jacP            = ctx;
9169b94acceSBarry Smith   snes->jacobian        = A;
9179b94acceSBarry Smith   snes->jacobian_pre    = B;
9189b94acceSBarry Smith   return 0;
9199b94acceSBarry Smith }
9209b94acceSBarry Smith 
92162fef451SLois Curfman McInnes /*@
92262fef451SLois Curfman McInnes    SNESGetHessian - Returns the Hessian matrix and optionally the user
92362fef451SLois Curfman McInnes    provided context for evaluating the Hessian.
92462fef451SLois Curfman McInnes 
92562fef451SLois Curfman McInnes    Input Parameter:
92662fef451SLois Curfman McInnes .  snes - the nonlinear solver context
92762fef451SLois Curfman McInnes 
92862fef451SLois Curfman McInnes    Output Parameters:
92962fef451SLois Curfman McInnes .  A - location to stash Hessian matrix (or PETSC_NULL)
93062fef451SLois Curfman McInnes .  B - location to stash preconditioner matrix (or PETSC_NULL)
93162fef451SLois Curfman McInnes .  ctx - location to stash Hessian ctx (or PETSC_NULL)
93262fef451SLois Curfman McInnes 
93362fef451SLois Curfman McInnes .seealso: SNESSetHessian(), SNESComputeHessian()
93462fef451SLois Curfman McInnes @*/
93562fef451SLois Curfman McInnes int SNESGetHessian(SNES snes,Mat *A,Mat *B, void **ctx)
93662fef451SLois Curfman McInnes {
93762fef451SLois Curfman McInnes   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
93862fef451SLois Curfman McInnes     "SNESSetHessian:For SNES_UNCONSTRAINED_MINIMIZATION only");
93962fef451SLois Curfman McInnes   if (A)   *A = snes->jacobian;
94062fef451SLois Curfman McInnes   if (B)   *B = snes->jacobian_pre;
94162fef451SLois Curfman McInnes   if (ctx) *ctx = snes->jacP;
94262fef451SLois Curfman McInnes   return 0;
94362fef451SLois Curfman McInnes }
94462fef451SLois Curfman McInnes 
9459b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
9469b94acceSBarry Smith 
9479b94acceSBarry Smith /*@
9489b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
949272ac6f2SLois Curfman McInnes    of a nonlinear solver.
9509b94acceSBarry Smith 
9519b94acceSBarry Smith    Input Parameter:
9529b94acceSBarry Smith .  snes - the SNES context
9538ddd3da0SLois Curfman McInnes .  x - the solution vector
9549b94acceSBarry Smith 
955272ac6f2SLois Curfman McInnes    Notes:
956272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
957272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
958272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
959272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
960272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
961272ac6f2SLois Curfman McInnes 
9629b94acceSBarry Smith .keywords: SNES, nonlinear, setup
9639b94acceSBarry Smith 
9649b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
9659b94acceSBarry Smith @*/
9668ddd3da0SLois Curfman McInnes int SNESSetUp(SNES snes,Vec x)
9679b94acceSBarry Smith {
968272ac6f2SLois Curfman McInnes   int ierr, flg;
96977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
97077c4ece6SBarry Smith   PetscValidHeaderSpecific(x,VEC_COOKIE);
9718ddd3da0SLois Curfman McInnes   snes->vec_sol = snes->vec_sol_always = x;
9729b94acceSBarry Smith 
973272ac6f2SLois Curfman McInnes   ierr = OptionsHasName(snes->prefix,"-snes_mf", &flg);  CHKERRQ(ierr);
97431615d04SLois Curfman McInnes   if (flg) {
975272ac6f2SLois Curfman McInnes     Mat J;
976272ac6f2SLois Curfman McInnes     ierr = SNESDefaultMatrixFreeMatCreate(snes,snes->vec_sol,&J);CHKERRQ(ierr);
977272ac6f2SLois Curfman McInnes     PLogObjectParent(snes,J);
978272ac6f2SLois Curfman McInnes     snes->mfshell = J;
97983e56afdSLois Curfman McInnes     if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
98083e56afdSLois Curfman McInnes       ierr = SNESSetJacobian(snes,J,J,0,snes->funP); CHKERRQ(ierr);
98183e56afdSLois Curfman McInnes       PLogInfo((PetscObject)snes,"SNESSetUp: Setting default matrix-free Jacobian routines\n");
98283e56afdSLois Curfman McInnes     }
98383e56afdSLois Curfman McInnes     else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
98483e56afdSLois Curfman McInnes       ierr = SNESSetHessian(snes,J,J,0,snes->funP); CHKERRQ(ierr);
98583e56afdSLois Curfman McInnes       PLogInfo((PetscObject)snes,"SNESSetUp: Setting default matrix-free Hessian routines\n");
98683e56afdSLois Curfman McInnes     } else SETERRQ(1,"SNESSetUp:Method class doesn't support matrix-free option");
987272ac6f2SLois Curfman McInnes   }
9889b94acceSBarry Smith   if ((snes->method_class == SNES_NONLINEAR_EQUATIONS)) {
98937fcc0dbSBarry Smith     if (!snes->vec_func) SETERRQ(1,"SNESSetUp:Must call SNESSetFunction() first");
99037fcc0dbSBarry Smith     if (!snes->computefunction) SETERRQ(1,"SNESSetUp:Must call SNESSetFunction() first");
99137fcc0dbSBarry Smith     if (!snes->jacobian) SETERRQ(1,"SNESSetUp:Must call SNESSetJacobian() first");
992d804570eSBarry Smith     if (snes->vec_func == snes->vec_sol) SETERRQ(1,"SNESSetUp:Solution vector cannot be function vector");
993a703fe33SLois Curfman McInnes 
994a703fe33SLois Curfman McInnes     /* Set the KSP stopping criterion to use the Eisenstat-Walker method */
995a703fe33SLois Curfman McInnes     if (snes->ksp_ewconv && snes->type != SNES_EQ_NTR) {
996a703fe33SLois Curfman McInnes       SLES sles; KSP ksp;
997a703fe33SLois Curfman McInnes       ierr = SNESGetSLES(snes,&sles); CHKERRQ(ierr);
998a703fe33SLois Curfman McInnes       ierr = SLESGetKSP(sles,&ksp); CHKERRQ(ierr);
999a703fe33SLois Curfman McInnes       ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private,
1000a703fe33SLois Curfman McInnes              (void *)snes); CHKERRQ(ierr);
1001a703fe33SLois Curfman McInnes     }
10029b94acceSBarry Smith   } else if ((snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION)) {
100337fcc0dbSBarry Smith     if (!snes->vec_func) SETERRQ(1,"SNESSetUp:Must call SNESSetGradient() first");
100437fcc0dbSBarry Smith     if (!snes->computefunction) SETERRQ(1,"SNESSetUp:Must call SNESSetGradient() first");
100537fcc0dbSBarry Smith     if (!snes->computeumfunction)
100637fcc0dbSBarry Smith       SETERRQ(1,"SNESSetUp:Must call SNESSetMinimizationFunction() first");
100737fcc0dbSBarry Smith     if (!snes->jacobian) SETERRQ(1,"SNESSetUp:Must call SNESSetHessian() first");
10089b94acceSBarry Smith   } else SETERRQ(1,"SNESSetUp:Unknown method class");
1009a703fe33SLois Curfman McInnes   if (snes->setup) {ierr = (*snes->setup)(snes); CHKERRQ(ierr);}
1010a703fe33SLois Curfman McInnes   snes->setup_called = 1;
1011a703fe33SLois Curfman McInnes   return 0;
10129b94acceSBarry Smith }
10139b94acceSBarry Smith 
10149b94acceSBarry Smith /*@C
10159b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
10169b94acceSBarry Smith    with SNESCreate().
10179b94acceSBarry Smith 
10189b94acceSBarry Smith    Input Parameter:
10199b94acceSBarry Smith .  snes - the SNES context
10209b94acceSBarry Smith 
10219b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
10229b94acceSBarry Smith 
102363a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
10249b94acceSBarry Smith @*/
10259b94acceSBarry Smith int SNESDestroy(SNES snes)
10269b94acceSBarry Smith {
10279b94acceSBarry Smith   int ierr;
102877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
10299b94acceSBarry Smith   ierr = (*(snes)->destroy)((PetscObject)snes); CHKERRQ(ierr);
10300452661fSBarry Smith   if (snes->kspconvctx) PetscFree(snes->kspconvctx);
10319b94acceSBarry Smith   if (snes->mfshell) MatDestroy(snes->mfshell);
10329b94acceSBarry Smith   ierr = SLESDestroy(snes->sles); CHKERRQ(ierr);
10339b94acceSBarry Smith   PLogObjectDestroy((PetscObject)snes);
10340452661fSBarry Smith   PetscHeaderDestroy((PetscObject)snes);
10359b94acceSBarry Smith   return 0;
10369b94acceSBarry Smith }
10379b94acceSBarry Smith 
10389b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
10399b94acceSBarry Smith 
10409b94acceSBarry Smith /*@
10419b94acceSBarry Smith    SNESSetMaxIterations - Sets the maximum number of global iterations to use.
10429b94acceSBarry Smith 
10439b94acceSBarry Smith    Input Parameters:
10449b94acceSBarry Smith .  snes - the SNES context
10459b94acceSBarry Smith .  maxits - maximum number of iterations to use
10469b94acceSBarry Smith 
10479b94acceSBarry Smith    Options Database Key:
10489b94acceSBarry Smith $  -snes_max_it  maxits
10499b94acceSBarry Smith 
10509b94acceSBarry Smith    Note:
10519b94acceSBarry Smith    The default maximum number of iterations is 50.
10529b94acceSBarry Smith 
10539b94acceSBarry Smith .keywords: SNES, nonlinear, set, maximum, iterations
10549b94acceSBarry Smith 
10559b94acceSBarry Smith .seealso: SNESSetMaxFunctionEvaluations()
10569b94acceSBarry Smith @*/
10579b94acceSBarry Smith int SNESSetMaxIterations(SNES snes,int maxits)
10589b94acceSBarry Smith {
105977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
10609b94acceSBarry Smith   snes->max_its = maxits;
10619b94acceSBarry Smith   return 0;
10629b94acceSBarry Smith }
10639b94acceSBarry Smith 
10649b94acceSBarry Smith /*@
10659b94acceSBarry Smith    SNESSetMaxFunctionEvaluations - Sets the maximum number of function
10669b94acceSBarry Smith    evaluations to use.
10679b94acceSBarry Smith 
10689b94acceSBarry Smith    Input Parameters:
10699b94acceSBarry Smith .  snes - the SNES context
10709b94acceSBarry Smith .  maxf - maximum number of function evaluations
10719b94acceSBarry Smith 
10729b94acceSBarry Smith    Options Database Key:
10739b94acceSBarry Smith $  -snes_max_funcs maxf
10749b94acceSBarry Smith 
10759b94acceSBarry Smith    Note:
10769b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
10779b94acceSBarry Smith 
10789b94acceSBarry Smith .keywords: SNES, nonlinear, set, maximum, function, evaluations
10799b94acceSBarry Smith 
10809b94acceSBarry Smith .seealso: SNESSetMaxIterations()
10819b94acceSBarry Smith @*/
10829b94acceSBarry Smith int SNESSetMaxFunctionEvaluations(SNES snes,int maxf)
10839b94acceSBarry Smith {
108477c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
10859b94acceSBarry Smith   snes->max_funcs = maxf;
10869b94acceSBarry Smith   return 0;
10879b94acceSBarry Smith }
10889b94acceSBarry Smith 
10899b94acceSBarry Smith /*@
10909b94acceSBarry Smith    SNESSetRelativeTolerance - Sets the relative convergence tolerance.
10919b94acceSBarry Smith 
10929b94acceSBarry Smith    Input Parameters:
10939b94acceSBarry Smith .  snes - the SNES context
10949b94acceSBarry Smith .  rtol - tolerance
10959b94acceSBarry Smith 
10969b94acceSBarry Smith    Options Database Key:
10979b94acceSBarry Smith $    -snes_rtol tol
10989b94acceSBarry Smith 
10999b94acceSBarry Smith .keywords: SNES, nonlinear, set, relative, convergence, tolerance
11009b94acceSBarry Smith 
110177c4ece6SBarry Smith .seealso: SNESSetAbsoluteTolerance(), SNESSetSolutionTolerance()
110277c4ece6SBarry Smith 
11039b94acceSBarry Smith @*/
11049b94acceSBarry Smith int SNESSetRelativeTolerance(SNES snes,double rtol)
11059b94acceSBarry Smith {
110677c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
11079b94acceSBarry Smith   snes->rtol = rtol;
11089b94acceSBarry Smith   return 0;
11099b94acceSBarry Smith }
11109b94acceSBarry Smith 
11119b94acceSBarry Smith /*@
11129b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
11139b94acceSBarry Smith 
11149b94acceSBarry Smith    Input Parameters:
11159b94acceSBarry Smith .  snes - the SNES context
11169b94acceSBarry Smith .  tol - tolerance
11179b94acceSBarry Smith 
11189b94acceSBarry Smith    Options Database Key:
11199b94acceSBarry Smith $    -snes_trtol tol
11209b94acceSBarry Smith 
11219b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
11229b94acceSBarry Smith 
112377c4ece6SBarry Smith .seealso: SNESSetAbsoluteTolerance(), SNESSetSolutionTolerance()
11249b94acceSBarry Smith @*/
11259b94acceSBarry Smith int SNESSetTrustRegionTolerance(SNES snes,double tol)
11269b94acceSBarry Smith {
112777c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
11289b94acceSBarry Smith   snes->deltatol = tol;
11299b94acceSBarry Smith   return 0;
11309b94acceSBarry Smith }
11319b94acceSBarry Smith 
11329b94acceSBarry Smith /*@
11339b94acceSBarry Smith    SNESSetAbsoluteTolerance - Sets the absolute convergence tolerance.
11349b94acceSBarry Smith 
11359b94acceSBarry Smith    Input Parameters:
11369b94acceSBarry Smith .  snes - the SNES context
11379b94acceSBarry Smith .  atol - tolerance
11389b94acceSBarry Smith 
11399b94acceSBarry Smith    Options Database Key:
11409b94acceSBarry Smith $    -snes_atol tol
11419b94acceSBarry Smith 
11429b94acceSBarry Smith .keywords: SNES, nonlinear, set, absolute, convergence, tolerance
11439b94acceSBarry Smith 
114477c4ece6SBarry Smith .seealso: SNESSetRelativeTolerance(), SNESSetSolutionTolerance()
11459b94acceSBarry Smith @*/
11469b94acceSBarry Smith int SNESSetAbsoluteTolerance(SNES snes,double atol)
11479b94acceSBarry Smith {
114877c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
11499b94acceSBarry Smith   snes->atol = atol;
11509b94acceSBarry Smith   return 0;
11519b94acceSBarry Smith }
11529b94acceSBarry Smith 
11539b94acceSBarry Smith /*@
11549b94acceSBarry Smith    SNESSetSolutionTolerance - Sets the convergence tolerance in terms of
11559b94acceSBarry Smith    the norm of the change in the solution between steps.
11569b94acceSBarry Smith 
11579b94acceSBarry Smith    Input Parameters:
11589b94acceSBarry Smith .  snes - the SNES context
11599b94acceSBarry Smith .  tol - tolerance
11609b94acceSBarry Smith 
11619b94acceSBarry Smith    Options Database Key:
11629b94acceSBarry Smith $    -snes_stol tol
11639b94acceSBarry Smith 
11649b94acceSBarry Smith .keywords: SNES, nonlinear, set, solution, tolerance
11659b94acceSBarry Smith 
116677c4ece6SBarry Smith .seealso: SNESSetTruncationTolerance(), SNESSetRelativeTolerance()
11679b94acceSBarry Smith @*/
11689b94acceSBarry Smith int SNESSetSolutionTolerance( SNES snes, double tol )
11699b94acceSBarry Smith {
117077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
11719b94acceSBarry Smith   snes->xtol = tol;
11729b94acceSBarry Smith   return 0;
11739b94acceSBarry Smith }
11749b94acceSBarry Smith 
11759b94acceSBarry Smith /*@
117677c4ece6SBarry Smith    SNESSetMinimizationFunctionTolerance - Sets the minimum allowable function tolerance
11779b94acceSBarry Smith    for unconstrained minimization solvers.
11789b94acceSBarry Smith 
11799b94acceSBarry Smith    Input Parameters:
11809b94acceSBarry Smith .  snes - the SNES context
11819b94acceSBarry Smith .  ftol - minimum function tolerance
11829b94acceSBarry Smith 
11839b94acceSBarry Smith    Options Database Key:
11849b94acceSBarry Smith $    -snes_fmin ftol
11859b94acceSBarry Smith 
11869b94acceSBarry Smith    Note:
118777c4ece6SBarry Smith    SNESSetMinimizationFunctionTolerance() is valid for SNES_UNCONSTRAINED_MINIMIZATION
11889b94acceSBarry Smith    methods only.
11899b94acceSBarry Smith 
11909b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimum, convergence, function, tolerance
11919b94acceSBarry Smith 
119277c4ece6SBarry Smith .seealso: SNESSetRelativeTolerance(), SNESSetSolutionTolerance()
11939b94acceSBarry Smith @*/
119477c4ece6SBarry Smith int SNESSetMinimizationFunctionTolerance(SNES snes,double ftol)
11959b94acceSBarry Smith {
119677c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
11979b94acceSBarry Smith   snes->fmin = ftol;
11989b94acceSBarry Smith   return 0;
11999b94acceSBarry Smith }
12009b94acceSBarry Smith 
12019b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
12029b94acceSBarry Smith 
12039b94acceSBarry Smith /*@C
12049b94acceSBarry Smith    SNESSetMonitor - Sets the function that is to be used at every
12059b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
12069b94acceSBarry Smith    progress.
12079b94acceSBarry Smith 
12089b94acceSBarry Smith    Input Parameters:
12099b94acceSBarry Smith .  snes - the SNES context
12109b94acceSBarry Smith .  func - monitoring routine
12119b94acceSBarry Smith .  mctx - optional user-defined context for private data for the
12129b94acceSBarry Smith           monitor routine (may be null)
12139b94acceSBarry Smith 
12149b94acceSBarry Smith    Calling sequence of func:
1215682d7d0cSBarry Smith    int func(SNES snes,int its, Vec x,Vec f,double norm,void *mctx)
12169b94acceSBarry Smith 
12179b94acceSBarry Smith $    snes - the SNES context
12189b94acceSBarry Smith $    its - iteration number
12199b94acceSBarry Smith $    mctx - optional monitoring context
12209b94acceSBarry Smith $
12219b94acceSBarry Smith $ SNES_NONLINEAR_EQUATIONS methods:
12229b94acceSBarry Smith $    norm - 2-norm function value (may be estimated)
12239b94acceSBarry Smith $
12249b94acceSBarry Smith $ SNES_UNCONSTRAINED_MINIMIZATION methods:
12259b94acceSBarry Smith $    norm - 2-norm gradient value (may be estimated)
12269b94acceSBarry Smith 
12279b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
12289b94acceSBarry Smith 
12299b94acceSBarry Smith .seealso: SNESDefaultMonitor()
12309b94acceSBarry Smith @*/
12319b94acceSBarry Smith int SNESSetMonitor( SNES snes, int (*func)(SNES,int,double,void*),
12329b94acceSBarry Smith                     void *mctx )
12339b94acceSBarry Smith {
12349b94acceSBarry Smith   snes->monitor = func;
12359b94acceSBarry Smith   snes->monP    = (void*)mctx;
12369b94acceSBarry Smith   return 0;
12379b94acceSBarry Smith }
12389b94acceSBarry Smith 
12399b94acceSBarry Smith /*@C
12409b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
12419b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
12429b94acceSBarry Smith 
12439b94acceSBarry Smith    Input Parameters:
12449b94acceSBarry Smith .  snes - the SNES context
12459b94acceSBarry Smith .  func - routine to test for convergence
12469b94acceSBarry Smith .  cctx - optional context for private data for the convergence routine
12479b94acceSBarry Smith           (may be null)
12489b94acceSBarry Smith 
12499b94acceSBarry Smith    Calling sequence of func:
12509b94acceSBarry Smith    int func (SNES snes,double xnorm,double gnorm,
12519b94acceSBarry Smith              double f,void *cctx)
12529b94acceSBarry Smith 
12539b94acceSBarry Smith $    snes - the SNES context
12549b94acceSBarry Smith $    cctx - optional convergence context
12559b94acceSBarry Smith $    xnorm - 2-norm of current iterate
12569b94acceSBarry Smith $
12579b94acceSBarry Smith $ SNES_NONLINEAR_EQUATIONS methods:
12589b94acceSBarry Smith $    gnorm - 2-norm of current step
12599b94acceSBarry Smith $    f - 2-norm of function
12609b94acceSBarry Smith $
12619b94acceSBarry Smith $ SNES_UNCONSTRAINED_MINIMIZATION methods:
12629b94acceSBarry Smith $    gnorm - 2-norm of current gradient
12639b94acceSBarry Smith $    f - function value
12649b94acceSBarry Smith 
12659b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
12669b94acceSBarry Smith 
12679b94acceSBarry Smith .seealso: SNESDefaultConverged()
12689b94acceSBarry Smith @*/
12699b94acceSBarry Smith int SNESSetConvergenceTest(SNES snes,
12709b94acceSBarry Smith           int (*func)(SNES,double,double,double,void*),void *cctx)
12719b94acceSBarry Smith {
12729b94acceSBarry Smith   (snes)->converged = func;
12739b94acceSBarry Smith   (snes)->cnvP      = cctx;
12749b94acceSBarry Smith   return 0;
12759b94acceSBarry Smith }
12769b94acceSBarry Smith 
12779b94acceSBarry Smith /*
12789b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
12799b94acceSBarry Smith    positive parameter delta.
12809b94acceSBarry Smith 
12819b94acceSBarry Smith     Input Parameters:
12829b94acceSBarry Smith .   snes - the SNES context
12839b94acceSBarry Smith .   y - approximate solution of linear system
12849b94acceSBarry Smith .   fnorm - 2-norm of current function
12859b94acceSBarry Smith .   delta - trust region size
12869b94acceSBarry Smith 
12879b94acceSBarry Smith     Output Parameters:
12889b94acceSBarry Smith .   gpnorm - predicted function norm at the new point, assuming local
12899b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
12909b94acceSBarry Smith     region, and exceeds zero otherwise.
12919b94acceSBarry Smith .   ynorm - 2-norm of the step
12929b94acceSBarry Smith 
12939b94acceSBarry Smith     Note:
1294ddd12767SBarry Smith     For non-trust region methods such as SNES_EQ_NLS, the parameter delta
12959b94acceSBarry Smith     is set to be the maximum allowable step size.
12969b94acceSBarry Smith 
12979b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
12989b94acceSBarry Smith */
12999b94acceSBarry Smith int SNESScaleStep_Private(SNES snes,Vec y,double *fnorm,double *delta,
13009b94acceSBarry Smith                   double *gpnorm,double *ynorm)
13019b94acceSBarry Smith {
13029b94acceSBarry Smith   double norm;
13039b94acceSBarry Smith   Scalar cnorm;
1304cddf8d76SBarry Smith   VecNorm(y,NORM_2, &norm );
13059b94acceSBarry Smith   if (norm > *delta) {
13069b94acceSBarry Smith      norm = *delta/norm;
13079b94acceSBarry Smith      *gpnorm = (1.0 - norm)*(*fnorm);
13089b94acceSBarry Smith      cnorm = norm;
13099b94acceSBarry Smith      VecScale( &cnorm, y );
13109b94acceSBarry Smith      *ynorm = *delta;
13119b94acceSBarry Smith   } else {
13129b94acceSBarry Smith      *gpnorm = 0.0;
13139b94acceSBarry Smith      *ynorm = norm;
13149b94acceSBarry Smith   }
13159b94acceSBarry Smith   return 0;
13169b94acceSBarry Smith }
13179b94acceSBarry Smith 
13189b94acceSBarry Smith /*@
13199b94acceSBarry Smith    SNESSolve - Solves a nonlinear system.  Call SNESSolve after calling
132063a78c88SLois Curfman McInnes    SNESCreate() and optional routines of the form SNESSetXXX().
13219b94acceSBarry Smith 
13229b94acceSBarry Smith    Input Parameter:
13239b94acceSBarry Smith .  snes - the SNES context
13248ddd3da0SLois Curfman McInnes .  x - the solution vector
13259b94acceSBarry Smith 
13269b94acceSBarry Smith    Output Parameter:
13279b94acceSBarry Smith    its - number of iterations until termination
13289b94acceSBarry Smith 
13298ddd3da0SLois Curfman McInnes    Note:
13308ddd3da0SLois Curfman McInnes    The user should initialize the vector, x, with the initial guess
13318ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
13328ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
13338ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
13348ddd3da0SLois Curfman McInnes 
13359b94acceSBarry Smith .keywords: SNES, nonlinear, solve
13369b94acceSBarry Smith 
133763a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESDestroy()
13389b94acceSBarry Smith @*/
13398ddd3da0SLois Curfman McInnes int SNESSolve(SNES snes,Vec x,int *its)
13409b94acceSBarry Smith {
13413c7409f5SSatish Balay   int ierr, flg;
1342052efed2SBarry Smith 
134377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
1344c4fc05e7SBarry Smith   if (!snes->setup_called) {ierr = SNESSetUp(snes,x); CHKERRQ(ierr);}
1345c4fc05e7SBarry Smith   else {snes->vec_sol = snes->vec_sol_always = x;}
13469b94acceSBarry Smith   PLogEventBegin(SNES_Solve,snes,0,0,0);
13479b94acceSBarry Smith   ierr = (*(snes)->solve)(snes,its); CHKERRQ(ierr);
13489b94acceSBarry Smith   PLogEventEnd(SNES_Solve,snes,0,0,0);
13493c7409f5SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-snes_view", &flg); CHKERRQ(ierr);
13503c7409f5SSatish Balay   if (flg) { ierr = SNESView(snes,STDOUT_VIEWER_WORLD); CHKERRQ(ierr); }
13519b94acceSBarry Smith   return 0;
13529b94acceSBarry Smith }
13539b94acceSBarry Smith 
13549b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
135537fcc0dbSBarry Smith static NRList *__SNESList = 0;
13569b94acceSBarry Smith 
13579b94acceSBarry Smith /*@
13584b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
13599b94acceSBarry Smith 
13609b94acceSBarry Smith    Input Parameters:
13619b94acceSBarry Smith .  snes - the SNES context
13629b94acceSBarry Smith .  method - a known method
13639b94acceSBarry Smith 
13649b94acceSBarry Smith    Notes:
13659b94acceSBarry Smith    See "petsc/include/snes.h" for available methods (for instance)
13669b94acceSBarry Smith $  Systems of nonlinear equations:
1367ddd12767SBarry Smith $    SNES_EQ_NLS - Newton's method with line search
1368ddd12767SBarry Smith $    SNES_EQ_NTR - Newton's method with trust region
13699b94acceSBarry Smith $  Unconstrained minimization:
13709b94acceSBarry Smith $    SNES_UM_NTR - Newton's method with trust region
1371a703fe33SLois Curfman McInnes $    SNES_UM_NLS - Newton's method with line search
13729b94acceSBarry Smith 
13739b94acceSBarry Smith   Options Database Command:
13744b0e389bSBarry Smith $ -snes_type  <method>
13759b94acceSBarry Smith $    Use -help for a list of available methods
13769b94acceSBarry Smith $    (for instance, ls or tr)
1377a703fe33SLois Curfman McInnes 
1378a703fe33SLois Curfman McInnes .keysords: SNES, set, method
13799b94acceSBarry Smith @*/
13804b0e389bSBarry Smith int SNESSetType(SNES snes,SNESType method)
13819b94acceSBarry Smith {
13829b94acceSBarry Smith   int (*r)(SNES);
138377c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
13849b94acceSBarry Smith   /* Get the function pointers for the iterative method requested */
138537fcc0dbSBarry Smith   if (!__SNESList) {SNESRegisterAll();}
138637fcc0dbSBarry Smith   if (!__SNESList) {SETERRQ(1,"SNESSetType:Could not get methods");}
138737fcc0dbSBarry Smith   r =  (int (*)(SNES))NRFindRoutine( __SNESList, (int)method, (char *)0 );
13884b0e389bSBarry Smith   if (!r) {SETERRQ(1,"SNESSetType:Unknown method");}
13890452661fSBarry Smith   if (snes->data) PetscFree(snes->data);
13909b94acceSBarry Smith   snes->set_method_called = 1;
13919b94acceSBarry Smith   return (*r)(snes);
13929b94acceSBarry Smith }
13939b94acceSBarry Smith 
13949b94acceSBarry Smith /* --------------------------------------------------------------------- */
13959b94acceSBarry Smith /*@C
13969b94acceSBarry Smith    SNESRegister - Adds the method to the nonlinear solver package, given
13974b0e389bSBarry Smith    a function pointer and a nonlinear solver name of the type SNESType.
13989b94acceSBarry Smith 
13999b94acceSBarry Smith    Input Parameters:
1400ddd12767SBarry Smith .  name - for instance SNES_EQ_NLS, SNES_EQ_NTR, ...
14019b94acceSBarry Smith .  sname - corfunPonding string for name
14029b94acceSBarry Smith .  create - routine to create method context
14039b94acceSBarry Smith 
14049b94acceSBarry Smith .keywords: SNES, nonlinear, register
14059b94acceSBarry Smith 
14069b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterDestroy()
14079b94acceSBarry Smith @*/
14089b94acceSBarry Smith int SNESRegister(int name, char *sname, int (*create)(SNES))
14099b94acceSBarry Smith {
14109b94acceSBarry Smith   int ierr;
141137fcc0dbSBarry Smith   if (!__SNESList) {ierr = NRCreate(&__SNESList); CHKERRQ(ierr);}
141237fcc0dbSBarry Smith   NRRegister( __SNESList, name, sname, (int (*)(void*))create );
14139b94acceSBarry Smith   return 0;
14149b94acceSBarry Smith }
14159b94acceSBarry Smith /* --------------------------------------------------------------------- */
14169b94acceSBarry Smith /*@C
14179b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
14189b94acceSBarry Smith    registered by SNESRegister().
14199b94acceSBarry Smith 
14209b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
14219b94acceSBarry Smith 
14229b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
14239b94acceSBarry Smith @*/
14249b94acceSBarry Smith int SNESRegisterDestroy()
14259b94acceSBarry Smith {
142637fcc0dbSBarry Smith   if (__SNESList) {
142737fcc0dbSBarry Smith     NRDestroy( __SNESList );
142837fcc0dbSBarry Smith     __SNESList = 0;
14299b94acceSBarry Smith   }
14309b94acceSBarry Smith   return 0;
14319b94acceSBarry Smith }
14329b94acceSBarry Smith 
14339b94acceSBarry Smith /*
14344b0e389bSBarry Smith    SNESGetTypeFromOptions_Private - Sets the selected method from the
14359b94acceSBarry Smith    options database.
14369b94acceSBarry Smith 
14379b94acceSBarry Smith    Input Parameter:
14389b94acceSBarry Smith .  ctx - the SNES context
14399b94acceSBarry Smith 
14409b94acceSBarry Smith    Output Parameter:
14419b94acceSBarry Smith .  method -  solver method
14429b94acceSBarry Smith 
14439b94acceSBarry Smith    Returns:
14449b94acceSBarry Smith    Returns 1 if the method is found; 0 otherwise.
14459b94acceSBarry Smith 
14469b94acceSBarry Smith    Options Database Key:
14474b0e389bSBarry Smith $  -snes_type  method
14489b94acceSBarry Smith */
1449052efed2SBarry Smith int SNESGetTypeFromOptions_Private(SNES ctx,SNESType *method,int *flg)
14509b94acceSBarry Smith {
1451052efed2SBarry Smith   int ierr;
14529b94acceSBarry Smith   char sbuf[50];
1453052efed2SBarry Smith   ierr = OptionsGetString(ctx->prefix,"-snes_type", sbuf, 50, flg); CHKERRQ(ierr);
1454052efed2SBarry Smith   if (*flg) {
1455052efed2SBarry Smith     if (!__SNESList) {ierr = SNESRegisterAll(); CHKERRQ(ierr);}
145637fcc0dbSBarry Smith     *method = (SNESType)NRFindID( __SNESList, sbuf );
14579b94acceSBarry Smith   }
14589b94acceSBarry Smith   return 0;
14599b94acceSBarry Smith }
14609b94acceSBarry Smith 
14619b94acceSBarry Smith /*@C
14629a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
14639b94acceSBarry Smith 
14649b94acceSBarry Smith    Input Parameter:
14654b0e389bSBarry Smith .  snes - nonlinear solver context
14669b94acceSBarry Smith 
14679b94acceSBarry Smith    Output Parameter:
14689a28b0a6SLois Curfman McInnes .  method - SNES method (or use PETSC_NULL)
14699a28b0a6SLois Curfman McInnes .  name - name of SNES method (or use PETSC_NULL)
14709b94acceSBarry Smith 
14719b94acceSBarry Smith .keywords: SNES, nonlinear, get, method, name
14729b94acceSBarry Smith @*/
14734b0e389bSBarry Smith int SNESGetType(SNES snes, SNESType *method,char **name)
14749b94acceSBarry Smith {
147506a9b0adSLois Curfman McInnes   int ierr;
147637fcc0dbSBarry Smith   if (!__SNESList) {ierr = SNESRegisterAll(); CHKERRQ(ierr);}
14774b0e389bSBarry Smith   if (method) *method = (SNESType) snes->type;
147837fcc0dbSBarry Smith   if (name)  *name = NRFindName( __SNESList, (int) snes->type );
14799b94acceSBarry Smith   return 0;
14809b94acceSBarry Smith }
14819b94acceSBarry Smith 
14829b94acceSBarry Smith #include <stdio.h>
14839b94acceSBarry Smith /*
14844b0e389bSBarry Smith    SNESPrintTypes_Private - Prints the SNES methods available from the
14859b94acceSBarry Smith    options database.
14869b94acceSBarry Smith 
14879b94acceSBarry Smith    Input Parameters:
148833455573SSatish Balay .  comm   - communicator (usually MPI_COMM_WORLD)
14899b94acceSBarry Smith .  prefix - prefix (usually "-")
14904b0e389bSBarry Smith .  name   - the options database name (by default "snes_type")
14919b94acceSBarry Smith */
149233455573SSatish Balay int SNESPrintTypes_Private(MPI_Comm comm,char* prefix,char *name)
14939b94acceSBarry Smith {
14949b94acceSBarry Smith   FuncList *entry;
149537fcc0dbSBarry Smith   if (!__SNESList) {SNESRegisterAll();}
149637fcc0dbSBarry Smith   entry = __SNESList->head;
149777c4ece6SBarry Smith   PetscPrintf(comm," %s%s (one of)",prefix,name);
14989b94acceSBarry Smith   while (entry) {
149977c4ece6SBarry Smith     PetscPrintf(comm," %s",entry->name);
15009b94acceSBarry Smith     entry = entry->next;
15019b94acceSBarry Smith   }
150277c4ece6SBarry Smith   PetscPrintf(comm,"\n");
15039b94acceSBarry Smith   return 0;
15049b94acceSBarry Smith }
15059b94acceSBarry Smith 
15069b94acceSBarry Smith /*@C
15079b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
15089b94acceSBarry Smith    stored.
15099b94acceSBarry Smith 
15109b94acceSBarry Smith    Input Parameter:
15119b94acceSBarry Smith .  snes - the SNES context
15129b94acceSBarry Smith 
15139b94acceSBarry Smith    Output Parameter:
15149b94acceSBarry Smith .  x - the solution
15159b94acceSBarry Smith 
15169b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
15179b94acceSBarry Smith 
1518a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESGetGradient(), SNESGetSolutionUpdate()
15199b94acceSBarry Smith @*/
15209b94acceSBarry Smith int SNESGetSolution(SNES snes,Vec *x)
15219b94acceSBarry Smith {
152277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
15239b94acceSBarry Smith   *x = snes->vec_sol_always;
15249b94acceSBarry Smith   return 0;
15259b94acceSBarry Smith }
15269b94acceSBarry Smith 
15279b94acceSBarry Smith /*@C
15289b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
15299b94acceSBarry Smith    stored.
15309b94acceSBarry Smith 
15319b94acceSBarry Smith    Input Parameter:
15329b94acceSBarry Smith .  snes - the SNES context
15339b94acceSBarry Smith 
15349b94acceSBarry Smith    Output Parameter:
15359b94acceSBarry Smith .  x - the solution update
15369b94acceSBarry Smith 
15379b94acceSBarry Smith    Notes:
15389b94acceSBarry Smith    This vector is implementation dependent.
15399b94acceSBarry Smith 
15409b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
15419b94acceSBarry Smith 
15429b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction
15439b94acceSBarry Smith @*/
15449b94acceSBarry Smith int SNESGetSolutionUpdate(SNES snes,Vec *x)
15459b94acceSBarry Smith {
154677c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
15479b94acceSBarry Smith   *x = snes->vec_sol_update_always;
15489b94acceSBarry Smith   return 0;
15499b94acceSBarry Smith }
15509b94acceSBarry Smith 
15519b94acceSBarry Smith /*@C
15523638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
15539b94acceSBarry Smith 
15549b94acceSBarry Smith    Input Parameter:
15559b94acceSBarry Smith .  snes - the SNES context
15569b94acceSBarry Smith 
15579b94acceSBarry Smith    Output Parameter:
15583638b69dSLois Curfman McInnes .  r - the function
15599b94acceSBarry Smith 
15609b94acceSBarry Smith    Notes:
15619b94acceSBarry Smith    SNESGetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only
15629b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
15639b94acceSBarry Smith    SNESGetMinimizationFunction() and SNESGetGradient();
15649b94acceSBarry Smith 
1565a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
15669b94acceSBarry Smith 
156731615d04SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution(), SNESGetMinimizationFunction(),
156831615d04SLois Curfman McInnes           SNESGetGradient()
15699b94acceSBarry Smith @*/
15709b94acceSBarry Smith int SNESGetFunction(SNES snes,Vec *r)
15719b94acceSBarry Smith {
157277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
15739b94acceSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1,
157448d91487SBarry Smith     "SNESGetFunction:For SNES_NONLINEAR_EQUATIONS only");
15759b94acceSBarry Smith   *r = snes->vec_func_always;
15769b94acceSBarry Smith   return 0;
15779b94acceSBarry Smith }
15789b94acceSBarry Smith 
15799b94acceSBarry Smith /*@C
15803638b69dSLois Curfman McInnes    SNESGetGradient - Returns the vector where the gradient is stored.
15819b94acceSBarry Smith 
15829b94acceSBarry Smith    Input Parameter:
15839b94acceSBarry Smith .  snes - the SNES context
15849b94acceSBarry Smith 
15859b94acceSBarry Smith    Output Parameter:
15869b94acceSBarry Smith .  r - the gradient
15879b94acceSBarry Smith 
15889b94acceSBarry Smith    Notes:
15899b94acceSBarry Smith    SNESGetGradient() is valid for SNES_UNCONSTRAINED_MINIMIZATION methods
15909b94acceSBarry Smith    only.  An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
15919b94acceSBarry Smith    SNESGetFunction().
15929b94acceSBarry Smith 
15939b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient
15949b94acceSBarry Smith 
159531615d04SLois Curfman McInnes .seealso: SNESGetMinimizationFunction(), SNESGetSolution(), SNESGetFunction()
15969b94acceSBarry Smith @*/
15979b94acceSBarry Smith int SNESGetGradient(SNES snes,Vec *r)
15989b94acceSBarry Smith {
159977c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
16009b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
160148d91487SBarry Smith     "SNESGetGradient:For SNES_UNCONSTRAINED_MINIMIZATION only");
16029b94acceSBarry Smith   *r = snes->vec_func_always;
16039b94acceSBarry Smith   return 0;
16049b94acceSBarry Smith }
16059b94acceSBarry Smith 
16069b94acceSBarry Smith /*@
16079b94acceSBarry Smith    SNESGetMinimizationFunction - Returns the scalar function value for
16089b94acceSBarry Smith    unconstrained minimization problems.
16099b94acceSBarry Smith 
16109b94acceSBarry Smith    Input Parameter:
16119b94acceSBarry Smith .  snes - the SNES context
16129b94acceSBarry Smith 
16139b94acceSBarry Smith    Output Parameter:
16149b94acceSBarry Smith .  r - the function
16159b94acceSBarry Smith 
16169b94acceSBarry Smith    Notes:
16179b94acceSBarry Smith    SNESGetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
16189b94acceSBarry Smith    methods only.  An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
16199b94acceSBarry Smith    SNESGetFunction().
16209b94acceSBarry Smith 
16219b94acceSBarry Smith .keywords: SNES, nonlinear, get, function
16229b94acceSBarry Smith 
162331615d04SLois Curfman McInnes .seealso: SNESGetGradient(), SNESGetSolution(), SNESGetFunction()
16249b94acceSBarry Smith @*/
16259b94acceSBarry Smith int SNESGetMinimizationFunction(SNES snes,double *r)
16269b94acceSBarry Smith {
162777c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
16289b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
162948d91487SBarry Smith     "SNESGetMinimizationFunction:For SNES_UNCONSTRAINED_MINIMIZATION only");
16309b94acceSBarry Smith   *r = snes->fc;
16319b94acceSBarry Smith   return 0;
16329b94acceSBarry Smith }
16339b94acceSBarry Smith 
16343c7409f5SSatish Balay /*@C
16353c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
16363c7409f5SSatish Balay    SNES options in the database.
16373c7409f5SSatish Balay 
16383c7409f5SSatish Balay    Input Parameter:
16393c7409f5SSatish Balay .  snes - the SNES context
16403c7409f5SSatish Balay .  prefix - the prefix to prepend to all option names
16413c7409f5SSatish Balay 
16423c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
1643a86d99e1SLois Curfman McInnes 
1644a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
16453c7409f5SSatish Balay @*/
16463c7409f5SSatish Balay int SNESSetOptionsPrefix(SNES snes,char *prefix)
16473c7409f5SSatish Balay {
16483c7409f5SSatish Balay   int ierr;
16493c7409f5SSatish Balay 
165077c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
16513c7409f5SSatish Balay   ierr = PetscObjectSetPrefix((PetscObject)snes, prefix); CHKERRQ(ierr);
16523c7409f5SSatish Balay   ierr = SLESSetOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr);
16533c7409f5SSatish Balay   return 0;
16543c7409f5SSatish Balay }
16553c7409f5SSatish Balay 
16563c7409f5SSatish Balay /*@C
16573c7409f5SSatish Balay    SNESAppendOptionsPrefix - Append to the prefix used for searching for all
16583c7409f5SSatish Balay    SNES options in the database.
16593c7409f5SSatish Balay 
16603c7409f5SSatish Balay    Input Parameter:
16613c7409f5SSatish Balay .  snes - the SNES context
16623c7409f5SSatish Balay .  prefix - the prefix to prepend to all option names
16633c7409f5SSatish Balay 
16643c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
1665a86d99e1SLois Curfman McInnes 
1666a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
16673c7409f5SSatish Balay @*/
16683c7409f5SSatish Balay int SNESAppendOptionsPrefix(SNES snes,char *prefix)
16693c7409f5SSatish Balay {
16703c7409f5SSatish Balay   int ierr;
16713c7409f5SSatish Balay 
167277c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
16733c7409f5SSatish Balay   ierr = PetscObjectAppendPrefix((PetscObject)snes, prefix); CHKERRQ(ierr);
16743c7409f5SSatish Balay   ierr = SLESAppendOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr);
16753c7409f5SSatish Balay   return 0;
16763c7409f5SSatish Balay }
16773c7409f5SSatish Balay 
1678c83590e2SSatish Balay /*@
16793c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
16803c7409f5SSatish Balay    SNES options in the database.
16813c7409f5SSatish Balay 
16823c7409f5SSatish Balay    Input Parameter:
16833c7409f5SSatish Balay .  snes - the SNES context
16843c7409f5SSatish Balay 
16853c7409f5SSatish Balay    Output Parameter:
16863c7409f5SSatish Balay .  prefix - pointer to the prefix string used
16873c7409f5SSatish Balay 
16883c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
1689a86d99e1SLois Curfman McInnes 
1690a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
16913c7409f5SSatish Balay @*/
16923c7409f5SSatish Balay int SNESGetOptionsPrefix(SNES snes,char **prefix)
16933c7409f5SSatish Balay {
16943c7409f5SSatish Balay   int ierr;
16953c7409f5SSatish Balay 
169677c4ece6SBarry Smith   PetscValidHeaderSpecific(snes,SNES_COOKIE);
16973c7409f5SSatish Balay   ierr = PetscObjectGetPrefix((PetscObject)snes, prefix); CHKERRQ(ierr);
16983c7409f5SSatish Balay   return 0;
16993c7409f5SSatish Balay }
17003c7409f5SSatish Balay 
17013c7409f5SSatish Balay 
17029b94acceSBarry Smith 
17039b94acceSBarry Smith 
17049b94acceSBarry Smith 
1705