xref: /petsc/src/snes/interface/snes.c (revision 6d84be18fbb99ba69be7b8bdde5411a66955b7ea)
19b94acceSBarry Smith #ifndef lint
2*6d84be18SBarry Smith static char vcid[] = "$Id: snes.c,v 1.55 1996/02/15 02:05:56 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*);
124b0e389bSBarry Smith extern int SNESPrintTypes_Private(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   PetscObject         vobj = (PetscObject) viewer;
429b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
439b94acceSBarry Smith   FILE                *fd;
449b94acceSBarry Smith   int                 ierr;
459b94acceSBarry Smith   SLES                sles;
469b94acceSBarry Smith   char                *method;
479b94acceSBarry Smith 
489b94acceSBarry Smith   if (vobj->cookie == VIEWER_COOKIE && (vobj->type == ASCII_FILE_VIEWER ||
499b94acceSBarry Smith                                         vobj->type == ASCII_FILES_VIEWER)) {
50227d817aSBarry Smith     ierr = ViewerFileGetPointer(viewer,&fd); CHKERRQ(ierr);
519b94acceSBarry Smith     MPIU_fprintf(snes->comm,fd,"SNES Object:\n");
524b0e389bSBarry Smith     SNESGetType(snes,PETSC_NULL,&method);
539b94acceSBarry Smith     MPIU_fprintf(snes->comm,fd,"  method: %s\n",method);
549b94acceSBarry Smith     if (snes->view) (*snes->view)((PetscObject)snes,viewer);
559b94acceSBarry Smith     MPIU_fprintf(snes->comm,fd,
569b94acceSBarry Smith       "  maximum iterations=%d, maximum function evaluations=%d\n",
579b94acceSBarry Smith       snes->max_its,snes->max_funcs);
589b94acceSBarry Smith     MPIU_fprintf(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)
629b94acceSBarry Smith       MPIU_fprintf(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) {
669b94acceSBarry Smith         MPIU_fprintf(snes->comm,fd,
679b94acceSBarry Smith      "  Eisenstat-Walker computation of KSP relative tolerance (version %d)\n",
689b94acceSBarry Smith         kctx->version);
699b94acceSBarry Smith         MPIU_fprintf(snes->comm,fd,
709b94acceSBarry Smith           "    rtol_0=%g, rtol_max=%g, threshold=%g\n",kctx->rtol_0,
719b94acceSBarry Smith           kctx->rtol_max,kctx->threshold);
729b94acceSBarry Smith         MPIU_fprintf(snes->comm,fd,"    gamma=%g, alpha=%g, alpha2=%g\n",
739b94acceSBarry Smith           kctx->gamma,kctx->alpha,kctx->alpha2);
749b94acceSBarry Smith       }
759b94acceSBarry Smith     }
769b94acceSBarry Smith     SNESGetSLES(snes,&sles);
779b94acceSBarry Smith     ierr = SLESView(sles,viewer); CHKERRQ(ierr);
789b94acceSBarry Smith   }
799b94acceSBarry Smith   return 0;
809b94acceSBarry Smith }
819b94acceSBarry Smith 
829b94acceSBarry Smith /*@
83682d7d0cSBarry Smith    SNESSetFromOptions - Sets various SNES and SLES parameters from user options.
849b94acceSBarry Smith 
859b94acceSBarry Smith    Input Parameter:
869b94acceSBarry Smith .  snes - the SNES context
879b94acceSBarry Smith 
889b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database
899b94acceSBarry Smith 
90a86d99e1SLois Curfman McInnes .seealso: SNESPrintHelp(), SNESSetOptionsPrefix()
919b94acceSBarry Smith @*/
929b94acceSBarry Smith int SNESSetFromOptions(SNES snes)
939b94acceSBarry Smith {
944b0e389bSBarry Smith   SNESType method;
959b94acceSBarry Smith   double   tmp;
969b94acceSBarry Smith   SLES     sles;
973c7409f5SSatish Balay   int      ierr, flg;
983c7409f5SSatish Balay   int      version   = PETSC_DEFAULT;
999b94acceSBarry Smith   double   rtol_0    = PETSC_DEFAULT;
1009b94acceSBarry Smith   double   rtol_max  = PETSC_DEFAULT;
1019b94acceSBarry Smith   double   gamma2    = PETSC_DEFAULT;
1029b94acceSBarry Smith   double   alpha     = PETSC_DEFAULT;
1039b94acceSBarry Smith   double   alpha2    = PETSC_DEFAULT;
1049b94acceSBarry Smith   double   threshold = PETSC_DEFAULT;
1059b94acceSBarry Smith 
1069b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
1074b0e389bSBarry Smith   if (snes->setup_called)SETERRQ(1,"SNESSetFromOptions:Must call prior to SNESSetUp!");
108052efed2SBarry Smith   ierr = SNESGetTypeFromOptions_Private(snes,&method,&flg); CHKERRQ(ierr);
109052efed2SBarry Smith   if (flg) {
110052efed2SBarry Smith     ierr = SNESSetType(snes,method); CHKERRQ(ierr);
1119b94acceSBarry Smith   }
1125334005bSBarry Smith   else if (!snes->set_method_called) {
1135334005bSBarry Smith     if (snes->method_class == SNES_NONLINEAR_EQUATIONS) {
1145334005bSBarry Smith       ierr = SNESSetType(snes,SNES_EQ_NLS); CHKERRQ(ierr);
1155334005bSBarry Smith     }
1165334005bSBarry Smith     else {
1175334005bSBarry Smith       ierr = SNESSetType(snes,SNES_UM_NTR); CHKERRQ(ierr);
1185334005bSBarry Smith     }
1195334005bSBarry Smith   }
1205334005bSBarry Smith 
1213c7409f5SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-help", &flg); CHKERRQ(ierr);
1223c7409f5SSatish Balay   if (flg) { SNESPrintHelp(snes); }
1233c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_stol",&tmp, &flg); CHKERRQ(ierr);
1243c7409f5SSatish Balay   if (flg) { SNESSetSolutionTolerance(snes,tmp); }
1253c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_ttol",&tmp, &flg); CHKERRQ(ierr);
1263c7409f5SSatish Balay   if (flg) { SNESSetTruncationTolerance(snes,tmp); }
1273c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_atol",&tmp, &flg); CHKERRQ(ierr);
1283c7409f5SSatish Balay   if (flg) { SNESSetAbsoluteTolerance(snes,tmp); }
1293c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_trtol",&tmp, &flg);  CHKERRQ(ierr);
1303c7409f5SSatish Balay   if (flg) { SNESSetTrustRegionTolerance(snes,tmp); }
1313c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_rtol",&tmp, &flg);  CHKERRQ(ierr);
1323c7409f5SSatish Balay   if (flg) { SNESSetRelativeTolerance(snes,tmp); }
1333c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_fmin",&tmp, &flg);  CHKERRQ(ierr);
1343c7409f5SSatish Balay   if (flg) { SNESSetMinFunctionTolerance(snes,tmp); }
1353c7409f5SSatish Balay   ierr = OptionsGetInt(snes->prefix,"-snes_max_it",&snes->max_its, &flg); CHKERRQ(ierr);
1363c7409f5SSatish Balay   ierr = OptionsGetInt(snes->prefix,"-snes_max_funcs",&snes->max_funcs, &flg);CHKERRQ(ierr);
1373c7409f5SSatish Balay   ierr = OptionsHasName(snes->prefix,"-snes_ksp_ew_conv", &flg);  CHKERRQ(ierr);
1383c7409f5SSatish Balay   if (flg) { snes->ksp_ewconv = 1; }
1393c7409f5SSatish Balay   ierr = OptionsGetInt(snes->prefix,"-snes_ksp_ew_version",&version, \
1403c7409f5SSatish Balay                        &flg); CHKERRQ(ierr);
1413c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtol0",&rtol_0, \
1423c7409f5SSatish Balay                           &flg); CHKERRQ(ierr);
1433c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtolmax",&rtol_max, \
1443c7409f5SSatish Balay                           &flg);  CHKERRQ(ierr);
1453c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_gamma",&gamma2, \
1463c7409f5SSatish Balay                           &flg);  CHKERRQ(ierr);
1473c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha",&alpha, \
1483c7409f5SSatish Balay                           &flg);  CHKERRQ(ierr);
1493c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha2",&alpha2, \
1503c7409f5SSatish Balay                           &flg);  CHKERRQ(ierr);
1513c7409f5SSatish Balay   ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_threshold",&threshold, \
1523c7409f5SSatish Balay                           &flg);  CHKERRQ(ierr);
1539b94acceSBarry Smith   ierr = SNES_KSP_SetParametersEW(snes,version,rtol_0,rtol_max,gamma2,alpha,
1549b94acceSBarry Smith                                   alpha2,threshold); CHKERRQ(ierr);
1553c7409f5SSatish Balay   ierr = OptionsHasName(snes->prefix,"-snes_monitor", &flg);  CHKERRQ(ierr);
1563c7409f5SSatish Balay   if (flg) { SNESSetMonitor(snes,SNESDefaultMonitor,0); }
1573c7409f5SSatish Balay   ierr = OptionsHasName(snes->prefix,"-snes_smonitor", &flg);  CHKERRQ(ierr);
1583c7409f5SSatish Balay    if (flg) { SNESSetMonitor(snes,SNESDefaultSMonitor,0); }
1593c7409f5SSatish Balay   ierr = OptionsHasName(snes->prefix,"-snes_xmonitor", &flg);  CHKERRQ(ierr);
1603c7409f5SSatish Balay   if (flg) {
16117699dbbSLois Curfman McInnes     int       rank = 0;
162d7e8b826SBarry Smith     DrawLG lg;
16317699dbbSLois Curfman McInnes     MPI_Initialized(&rank);
16417699dbbSLois Curfman McInnes     if (rank) MPI_Comm_rank(snes->comm,&rank);
16517699dbbSLois Curfman McInnes     if (!rank) {
1669b94acceSBarry Smith       ierr = SNESLGMonitorCreate(0,0,0,0,300,300,&lg); CHKERRQ(ierr);
1679b94acceSBarry Smith       ierr = SNESSetMonitor(snes,SNESLGMonitor,(void *)lg); CHKERRQ(ierr);
1689b94acceSBarry Smith       PLogObjectParent(snes,lg);
1699b94acceSBarry Smith     }
1709b94acceSBarry Smith   }
1713c7409f5SSatish Balay   ierr = OptionsHasName(snes->prefix,"-snes_fd", &flg);  CHKERRQ(ierr);
1723c7409f5SSatish Balay   if (flg && snes->method_class == SNES_NONLINEAR_EQUATIONS) {
1739b94acceSBarry Smith     ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre,
1749b94acceSBarry Smith                  SNESDefaultComputeJacobian,snes->funP); CHKERRQ(ierr);
17531615d04SLois Curfman McInnes     PLogInfo((PetscObject)snes,
17631615d04SLois Curfman McInnes       "SNESSetFromOptions: Setting default finite difference Jacobian matrix\n");
17731615d04SLois Curfman McInnes   }
17831615d04SLois Curfman McInnes   else if (flg && snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) {
17931615d04SLois Curfman McInnes     ierr = SNESSetHessian(snes,snes->jacobian,snes->jacobian_pre,
18031615d04SLois Curfman McInnes                  SNESDefaultComputeHessian,snes->funP); CHKERRQ(ierr);
18131615d04SLois Curfman McInnes     PLogInfo((PetscObject)snes,
18231615d04SLois Curfman McInnes       "SNESSetFromOptions: Setting default finite difference Hessian matrix\n");
1839b94acceSBarry Smith   }
1849b94acceSBarry Smith   ierr = SNESGetSLES(snes,&sles); CHKERRQ(ierr);
1859b94acceSBarry Smith   ierr = SLESSetFromOptions(sles); CHKERRQ(ierr);
1869b94acceSBarry Smith   if (!snes->setfromoptions) return 0;
1879b94acceSBarry Smith   return (*snes->setfromoptions)(snes);
1889b94acceSBarry Smith }
1899b94acceSBarry Smith 
1909b94acceSBarry Smith /*@
1919b94acceSBarry Smith    SNESPrintHelp - Prints all options for the SNES component.
1929b94acceSBarry Smith 
1939b94acceSBarry Smith    Input Parameter:
1949b94acceSBarry Smith .  snes - the SNES context
1959b94acceSBarry Smith 
196a703fe33SLois Curfman McInnes    Options Database Keys:
197a703fe33SLois Curfman McInnes $  -help, -h
198a703fe33SLois Curfman McInnes 
1999b94acceSBarry Smith .keywords: SNES, nonlinear, help
2009b94acceSBarry Smith 
201682d7d0cSBarry Smith .seealso: SNESSetFromOptions()
2029b94acceSBarry Smith @*/
2039b94acceSBarry Smith int SNESPrintHelp(SNES snes)
2049b94acceSBarry Smith {
2056daaf66cSBarry Smith   char                p[64];
2066daaf66cSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
2076daaf66cSBarry Smith 
2089b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
2096daaf66cSBarry Smith 
2106daaf66cSBarry Smith   PetscStrcpy(p,"-");
2116daaf66cSBarry Smith   if (snes->prefix) PetscStrcat(p, snes->prefix);
2126daaf66cSBarry Smith 
2136daaf66cSBarry Smith   kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx;
2146daaf66cSBarry Smith 
2159b94acceSBarry Smith   MPIU_printf(snes->comm,"SNES options ----------------------------\n");
2166daaf66cSBarry Smith   SNESPrintTypes_Private(p,"snes_type");
2176daaf66cSBarry Smith   MPIU_printf(snes->comm," %ssnes_monitor: use default SNES monitor\n",p);
2186daaf66cSBarry Smith   MPIU_printf(snes->comm," %ssnes_view: view SNES info after each nonlinear solve\n",p);
2196daaf66cSBarry Smith   MPIU_printf(snes->comm," %ssnes_max_it its (default %d)\n",p,snes->max_its);
2206daaf66cSBarry Smith   MPIU_printf(snes->comm," %ssnes_stol tol (default %g)\n",p,snes->xtol);
2216daaf66cSBarry Smith   MPIU_printf(snes->comm," %ssnes_atol tol (default %g)\n",p,snes->atol);
2226daaf66cSBarry Smith   MPIU_printf(snes->comm," %ssnes_rtol tol (default %g)\n",p,snes->rtol);
2236daaf66cSBarry Smith   MPIU_printf(snes->comm," %ssnes_ttol tol (default %g)\n",p,snes->trunctol);
2249b94acceSBarry Smith   MPIU_printf(snes->comm,
2259b94acceSBarry Smith    " options for solving systems of nonlinear equations only:\n");
2266daaf66cSBarry Smith   MPIU_printf(snes->comm,"   %ssnes_fd: use finite differences for Jacobian\n",p);
2276daaf66cSBarry Smith   MPIU_printf(snes->comm,"   %ssnes_mf: use matrix-free Jacobian\n",p);
2286daaf66cSBarry Smith   MPIU_printf(snes->comm,"   %ssnes_ksp_ew_conv: use Eisenstat-Walker computation of KSP rtol. Params are:\n",p);
2299b94acceSBarry Smith   MPIU_printf(snes->comm,
2309b94acceSBarry Smith    "     %ssnes_ksp_ew_version version (1 or 2, default is %d)\n",
2316daaf66cSBarry Smith    p,kctx->version);
2329b94acceSBarry Smith   MPIU_printf(snes->comm,
2339b94acceSBarry Smith    "     %ssnes_ksp_ew_rtol0 rtol0 (0 <= rtol0 < 1, default %g)\n",
2346daaf66cSBarry Smith    p,kctx->rtol_0);
2359b94acceSBarry Smith   MPIU_printf(snes->comm,
2369b94acceSBarry Smith    "     %ssnes_ksp_ew_rtolmax rtolmax (0 <= rtolmax < 1, default %g)\n",
2376daaf66cSBarry Smith    p,kctx->rtol_max);
2389b94acceSBarry Smith   MPIU_printf(snes->comm,
2399b94acceSBarry Smith    "     %ssnes_ksp_ew_gamma gamma (0 <= gamma <= 1, default %g)\n",
2406daaf66cSBarry Smith    p,kctx->gamma);
2419b94acceSBarry Smith   MPIU_printf(snes->comm,
2429b94acceSBarry Smith    "     %ssnes_ksp_ew_alpha alpha (1 < alpha <= 2, default %g)\n",
2436daaf66cSBarry Smith    p,kctx->alpha);
2449b94acceSBarry Smith   MPIU_printf(snes->comm,
2459b94acceSBarry Smith    "     %ssnes_ksp_ew_alpha2 alpha2 (default %g)\n",
2466daaf66cSBarry Smith    p,kctx->alpha2);
2479b94acceSBarry Smith   MPIU_printf(snes->comm,
2489b94acceSBarry Smith    "     %ssnes_ksp_ew_threshold threshold (0 < threshold < 1, default %g)\n",
2496daaf66cSBarry Smith    p,kctx->threshold);
2509b94acceSBarry Smith   MPIU_printf(snes->comm,
2519b94acceSBarry Smith    " options for solving unconstrained minimization problems only:\n");
2526daaf66cSBarry Smith   MPIU_printf(snes->comm,"   %ssnes_fmin tol (default %g)\n",p,snes->fmin);
2536daaf66cSBarry Smith   MPIU_printf(snes->comm," Run program with %ssnes_type method -help for help on ",p);
25431615d04SLois Curfman McInnes   MPIU_printf(snes->comm,"   %ssnes_fd: use finite differences for Hessian\n",p);
25531615d04SLois Curfman McInnes   MPIU_printf(snes->comm,"   %ssnes_mf: use matrix-free Hessian\n",p);
2569b94acceSBarry Smith   MPIU_printf(snes->comm,"a particular method\n");
2576daaf66cSBarry Smith   if (snes->printhelp) (*snes->printhelp)(snes,p);
2589b94acceSBarry Smith   return 0;
2599b94acceSBarry Smith }
2609b94acceSBarry Smith /*@
2619b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
2629b94acceSBarry Smith    the nonlinear solvers.
2639b94acceSBarry Smith 
2649b94acceSBarry Smith    Input Parameters:
2659b94acceSBarry Smith .  snes - the SNES context
2669b94acceSBarry Smith .  usrP - optional user context
2679b94acceSBarry Smith 
2689b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
2699b94acceSBarry Smith 
2709b94acceSBarry Smith .seealso: SNESGetApplicationContext()
2719b94acceSBarry Smith @*/
2729b94acceSBarry Smith int SNESSetApplicationContext(SNES snes,void *usrP)
2739b94acceSBarry Smith {
2749b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
2759b94acceSBarry Smith   snes->user		= usrP;
2769b94acceSBarry Smith   return 0;
2779b94acceSBarry Smith }
2789b94acceSBarry Smith /*@C
2799b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
2809b94acceSBarry Smith    nonlinear solvers.
2819b94acceSBarry Smith 
2829b94acceSBarry Smith    Input Parameter:
2839b94acceSBarry Smith .  snes - SNES context
2849b94acceSBarry Smith 
2859b94acceSBarry Smith    Output Parameter:
2869b94acceSBarry Smith .  usrP - user context
2879b94acceSBarry Smith 
2889b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
2899b94acceSBarry Smith 
2909b94acceSBarry Smith .seealso: SNESSetApplicationContext()
2919b94acceSBarry Smith @*/
2929b94acceSBarry Smith int SNESGetApplicationContext( SNES snes,  void **usrP )
2939b94acceSBarry Smith {
2949b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
2959b94acceSBarry Smith   *usrP = snes->user;
2969b94acceSBarry Smith   return 0;
2979b94acceSBarry Smith }
2989b94acceSBarry Smith /*@
2999b94acceSBarry Smith    SNESGetIterationNumber - Gets the current iteration number of the
3009b94acceSBarry Smith    nonlinear solver.
3019b94acceSBarry Smith 
3029b94acceSBarry Smith    Input Parameter:
3039b94acceSBarry Smith .  snes - SNES context
3049b94acceSBarry Smith 
3059b94acceSBarry Smith    Output Parameter:
3069b94acceSBarry Smith .  iter - iteration number
3079b94acceSBarry Smith 
3089b94acceSBarry Smith .keywords: SNES, nonlinear, get, iteration, number
3099b94acceSBarry Smith @*/
3109b94acceSBarry Smith int SNESGetIterationNumber(SNES snes,int* iter)
3119b94acceSBarry Smith {
3129b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
3139b94acceSBarry Smith   *iter = snes->iter;
3149b94acceSBarry Smith   return 0;
3159b94acceSBarry Smith }
3169b94acceSBarry Smith /*@
3179b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
3189b94acceSBarry Smith    with SNESSSetFunction().
3199b94acceSBarry Smith 
3209b94acceSBarry Smith    Input Parameter:
3219b94acceSBarry Smith .  snes - SNES context
3229b94acceSBarry Smith 
3239b94acceSBarry Smith    Output Parameter:
3249b94acceSBarry Smith .  fnorm - 2-norm of function
3259b94acceSBarry Smith 
3269b94acceSBarry Smith    Note:
3279b94acceSBarry Smith    SNESGetFunctionNorm() is valid for SNES_NONLINEAR_EQUATIONS methods only.
328a86d99e1SLois Curfman McInnes    A related routine for SNES_UNCONSTRAINED_MINIMIZATION methods is
329a86d99e1SLois Curfman McInnes    SNESGetGradientNorm().
3309b94acceSBarry Smith 
3319b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
332a86d99e1SLois Curfman McInnes 
333a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction()
3349b94acceSBarry Smith @*/
3359b94acceSBarry Smith int SNESGetFunctionNorm(SNES snes,Scalar *fnorm)
3369b94acceSBarry Smith {
3379b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
3389b94acceSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1,
33948d91487SBarry Smith     "SNESGetFunctionNorm:For SNES_NONLINEAR_EQUATIONS only");
3409b94acceSBarry Smith   *fnorm = snes->norm;
3419b94acceSBarry Smith   return 0;
3429b94acceSBarry Smith }
3439b94acceSBarry Smith /*@
3449b94acceSBarry Smith    SNESGetGradientNorm - Gets the norm of the current gradient that was set
3459b94acceSBarry Smith    with SNESSSetGradient().
3469b94acceSBarry Smith 
3479b94acceSBarry Smith    Input Parameter:
3489b94acceSBarry Smith .  snes - SNES context
3499b94acceSBarry Smith 
3509b94acceSBarry Smith    Output Parameter:
3519b94acceSBarry Smith .  fnorm - 2-norm of gradient
3529b94acceSBarry Smith 
3539b94acceSBarry Smith    Note:
3549b94acceSBarry Smith    SNESGetGradientNorm() is valid for SNES_UNCONSTRAINED_MINIMIZATION
355a86d99e1SLois Curfman McInnes    methods only.  A related routine for SNES_NONLINEAR_EQUATIONS methods
356a86d99e1SLois Curfman McInnes    is SNESGetFunctionNorm().
3579b94acceSBarry Smith 
3589b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient, norm
359a86d99e1SLois Curfman McInnes 
360a86d99e1SLois Curfman McInnes .seelso: SNESSetGradient()
3619b94acceSBarry Smith @*/
3629b94acceSBarry Smith int SNESGetGradientNorm(SNES snes,Scalar *gnorm)
3639b94acceSBarry Smith {
3649b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
3659b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
36648d91487SBarry Smith     "SNESGetGradientNorm:For SNES_UNCONSTRAINED_MINIMIZATION only");
3679b94acceSBarry Smith   *gnorm = snes->norm;
3689b94acceSBarry Smith   return 0;
3699b94acceSBarry Smith }
3709b94acceSBarry Smith /*@
3719b94acceSBarry Smith    SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps
3729b94acceSBarry Smith    attempted by the nonlinear solver.
3739b94acceSBarry Smith 
3749b94acceSBarry Smith    Input Parameter:
3759b94acceSBarry Smith .  snes - SNES context
3769b94acceSBarry Smith 
3779b94acceSBarry Smith    Output Parameter:
3789b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
3799b94acceSBarry Smith 
3809b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
3819b94acceSBarry Smith @*/
3829b94acceSBarry Smith int SNESGetNumberUnsuccessfulSteps(SNES snes,int* nfails)
3839b94acceSBarry Smith {
3849b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
3859b94acceSBarry Smith   *nfails = snes->nfailures;
3869b94acceSBarry Smith   return 0;
3879b94acceSBarry Smith }
3889b94acceSBarry Smith /*@C
3899b94acceSBarry Smith    SNESGetSLES - Returns the SLES context for a SNES solver.
3909b94acceSBarry Smith 
3919b94acceSBarry Smith    Input Parameter:
3929b94acceSBarry Smith .  snes - the SNES context
3939b94acceSBarry Smith 
3949b94acceSBarry Smith    Output Parameter:
3959b94acceSBarry Smith .  sles - the SLES context
3969b94acceSBarry Smith 
3979b94acceSBarry Smith    Notes:
3989b94acceSBarry Smith    The user can then directly manipulate the SLES context to set various
3999b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
4009b94acceSBarry Smith    KSP and PC contexts as well.
4019b94acceSBarry Smith 
4029b94acceSBarry Smith .keywords: SNES, nonlinear, get, SLES, context
4039b94acceSBarry Smith 
4049b94acceSBarry Smith .seealso: SLESGetPC(), SLESGetKSP()
4059b94acceSBarry Smith @*/
4069b94acceSBarry Smith int SNESGetSLES(SNES snes,SLES *sles)
4079b94acceSBarry Smith {
4089b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
4099b94acceSBarry Smith   *sles = snes->sles;
4109b94acceSBarry Smith   return 0;
4119b94acceSBarry Smith }
4129b94acceSBarry Smith /* -----------------------------------------------------------*/
4139b94acceSBarry Smith /*@C
4149b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
4159b94acceSBarry Smith 
4169b94acceSBarry Smith    Input Parameter:
4179b94acceSBarry Smith .  comm - MPI communicator
4189b94acceSBarry Smith .  type - type of method, one of
4199b94acceSBarry Smith $    SNES_NONLINEAR_EQUATIONS
4209b94acceSBarry Smith $      (for systems of nonlinear equations)
4219b94acceSBarry Smith $    SNES_UNCONSTRAINED_MINIMIZATION
4229b94acceSBarry Smith $      (for unconstrained minimization)
4239b94acceSBarry Smith 
4249b94acceSBarry Smith    Output Parameter:
4259b94acceSBarry Smith .  outsnes - the new SNES context
4269b94acceSBarry Smith 
4279b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
4289b94acceSBarry Smith 
42963a78c88SLois Curfman McInnes .seealso: SNESSolve(), SNESDestroy()
4309b94acceSBarry Smith @*/
4314b0e389bSBarry Smith int SNESCreate(MPI_Comm comm,SNESProblemType type,SNES *outsnes)
4329b94acceSBarry Smith {
4339b94acceSBarry Smith   int                 ierr;
4349b94acceSBarry Smith   SNES                snes;
4359b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
43637fcc0dbSBarry Smith 
4379b94acceSBarry Smith   *outsnes = 0;
4382a0acf01SLois Curfman McInnes   if (type != SNES_UNCONSTRAINED_MINIMIZATION && type != SNES_NONLINEAR_EQUATIONS)
4392a0acf01SLois Curfman McInnes     SETERRQ(1,"SNESCreate:incorrect method type");
4400452661fSBarry Smith   PetscHeaderCreate(snes,_SNES,SNES_COOKIE,SNES_UNKNOWN_METHOD,comm);
4419b94acceSBarry Smith   PLogObjectCreate(snes);
4429b94acceSBarry Smith   snes->max_its           = 50;
4439b94acceSBarry Smith   snes->max_funcs	  = 1000;
4449b94acceSBarry Smith   snes->norm		  = 0.0;
4455a2d0531SBarry Smith   if (type == SNES_UNCONSTRAINED_MINIMIZATION) {
4462a0acf01SLois Curfman McInnes     snes->rtol		  = 1.e-08;
4479b94acceSBarry Smith     snes->atol		  = 1.e-10;
4485a2d0531SBarry Smith   }
449b4874afaSBarry Smith   else {
450b4874afaSBarry Smith     snes->rtol		  = 1.e-8;
451b4874afaSBarry Smith     snes->ttol            = 0.0;
452b4874afaSBarry Smith     snes->atol		  = 1.e-50;
453b4874afaSBarry Smith   }
4549b94acceSBarry Smith   snes->xtol		  = 1.e-8;
4559b94acceSBarry Smith   snes->trunctol	  = 1.e-12;
4569b94acceSBarry Smith   snes->nfuncs            = 0;
4579b94acceSBarry Smith   snes->nfailures         = 0;
4589b94acceSBarry Smith   snes->monitor           = 0;
4599b94acceSBarry Smith   snes->data              = 0;
4609b94acceSBarry Smith   snes->view              = 0;
4619b94acceSBarry Smith   snes->computeumfunction = 0;
4629b94acceSBarry Smith   snes->umfunP            = 0;
4639b94acceSBarry Smith   snes->fc                = 0;
4649b94acceSBarry Smith   snes->deltatol          = 1.e-12;
4659b94acceSBarry Smith   snes->fmin              = -1.e30;
4669b94acceSBarry Smith   snes->method_class      = type;
4679b94acceSBarry Smith   snes->set_method_called = 0;
468a703fe33SLois Curfman McInnes   snes->setup_called      = 0;
4699b94acceSBarry Smith   snes->ksp_ewconv        = 0;
4709b94acceSBarry Smith 
4719b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
4720452661fSBarry Smith   kctx = PetscNew(SNES_KSP_EW_ConvCtx); CHKPTRQ(kctx);
4739b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
4749b94acceSBarry Smith   kctx->version     = 2;
4759b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
4769b94acceSBarry Smith                              this was too large for some test cases */
4779b94acceSBarry Smith   kctx->rtol_last   = 0;
4789b94acceSBarry Smith   kctx->rtol_max    = .9;
4799b94acceSBarry Smith   kctx->gamma       = 1.0;
4809b94acceSBarry Smith   kctx->alpha2      = .5*(1.0 + sqrt(5.0));
4819b94acceSBarry Smith   kctx->alpha       = kctx->alpha2;
4829b94acceSBarry Smith   kctx->threshold   = .1;
4839b94acceSBarry Smith   kctx->lresid_last = 0;
4849b94acceSBarry Smith   kctx->norm_last   = 0;
4859b94acceSBarry Smith 
4869b94acceSBarry Smith   ierr = SLESCreate(comm,&snes->sles); CHKERRQ(ierr);
4879b94acceSBarry Smith   PLogObjectParent(snes,snes->sles)
4885334005bSBarry Smith 
4899b94acceSBarry Smith   *outsnes = snes;
4909b94acceSBarry Smith   return 0;
4919b94acceSBarry Smith }
4929b94acceSBarry Smith 
4939b94acceSBarry Smith /* --------------------------------------------------------------- */
4949b94acceSBarry Smith /*@C
4959b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
4969b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
4979b94acceSBarry Smith    equations.
4989b94acceSBarry Smith 
4999b94acceSBarry Smith    Input Parameters:
5009b94acceSBarry Smith .  snes - the SNES context
5019b94acceSBarry Smith .  func - function evaluation routine
5029b94acceSBarry Smith .  ctx - optional user-defined function context
5039b94acceSBarry Smith .  r - vector to store function value
5049b94acceSBarry Smith 
5059b94acceSBarry Smith    Calling sequence of func:
5069b94acceSBarry Smith .  func (SNES, Vec x, Vec f, void *ctx);
5079b94acceSBarry Smith 
5089b94acceSBarry Smith .  x - input vector
5095334005bSBarry Smith .  f - vector function
5109b94acceSBarry Smith .  ctx - optional user-defined context for private data for the
5119b94acceSBarry Smith          function evaluation routine (may be null)
5129b94acceSBarry Smith 
5139b94acceSBarry Smith    Notes:
5149b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
5159b94acceSBarry Smith $      f'(x) x = -f(x),
5169b94acceSBarry Smith $  where f'(x) denotes the Jacobian matrix and f(x) is the function.
5179b94acceSBarry Smith 
5189b94acceSBarry Smith    SNESSetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only.
5199b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
5209b94acceSBarry Smith    SNESSetMinimizationFunction() and SNESSetGradient();
5219b94acceSBarry Smith 
5229b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
5239b94acceSBarry Smith 
524a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian()
5259b94acceSBarry Smith @*/
5265334005bSBarry Smith int SNESSetFunction( SNES snes, Vec r, int (*func)(SNES,Vec,Vec,void*),void *ctx)
5279b94acceSBarry Smith {
5289b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
5299b94acceSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1,
53048d91487SBarry Smith     "SNESSetFunction:For SNES_NONLINEAR_EQUATIONS only");
5319b94acceSBarry Smith   snes->computefunction     = func;
5329b94acceSBarry Smith   snes->vec_func            = snes->vec_func_always = r;
5339b94acceSBarry Smith   snes->funP                = ctx;
5349b94acceSBarry Smith   return 0;
5359b94acceSBarry Smith }
5369b94acceSBarry Smith 
5379b94acceSBarry Smith /*@
5389b94acceSBarry Smith    SNESComputeFunction - Computes the function that has been set with
5399b94acceSBarry Smith    SNESSetFunction().
5409b94acceSBarry Smith 
5419b94acceSBarry Smith    Input Parameters:
5429b94acceSBarry Smith .  snes - the SNES context
5439b94acceSBarry Smith .  x - input vector
5449b94acceSBarry Smith 
5459b94acceSBarry Smith    Output Parameter:
5463638b69dSLois Curfman McInnes .  y - function vector, as set by SNESSetFunction()
5479b94acceSBarry Smith 
548a86d99e1SLois Curfman McInnes n   Notes:
5499b94acceSBarry Smith    SNESComputeFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only.
5509b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
5519b94acceSBarry Smith    SNESComputeMinimizationFunction() and SNESComputeGradient();
5529b94acceSBarry Smith 
5539b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
5549b94acceSBarry Smith 
555a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction()
5569b94acceSBarry Smith @*/
5579b94acceSBarry Smith int SNESComputeFunction(SNES snes,Vec x, Vec y)
5589b94acceSBarry Smith {
5599b94acceSBarry Smith   int    ierr;
5609b94acceSBarry Smith 
561a86d99e1SLois Curfman McInnes   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1,
562a86d99e1SLois Curfman McInnes     "SNESComputeFunction: For SNES_NONLINEAR_EQUATIONS only");
5639b94acceSBarry Smith   PLogEventBegin(SNES_FunctionEval,snes,x,y,0);
5649b94acceSBarry Smith   ierr = (*snes->computefunction)(snes,x,y,snes->funP); CHKERRQ(ierr);
5659b94acceSBarry Smith   PLogEventEnd(SNES_FunctionEval,snes,x,y,0);
5669b94acceSBarry Smith   return 0;
5679b94acceSBarry Smith }
5689b94acceSBarry Smith 
5699b94acceSBarry Smith /*@C
5709b94acceSBarry Smith    SNESSetMinimizationFunction - Sets the function evaluation routine for
5719b94acceSBarry Smith    unconstrained minimization.
5729b94acceSBarry Smith 
5739b94acceSBarry Smith    Input Parameters:
5749b94acceSBarry Smith .  snes - the SNES context
5759b94acceSBarry Smith .  func - function evaluation routine
5769b94acceSBarry Smith .  ctx - optional user-defined function context
5779b94acceSBarry Smith 
5789b94acceSBarry Smith    Calling sequence of func:
5799b94acceSBarry Smith .  func (SNES snes,Vec x,double *f,void *ctx);
5809b94acceSBarry Smith 
5819b94acceSBarry Smith .  x - input vector
5829b94acceSBarry Smith .  f - function
5839b94acceSBarry Smith .  ctx - optional user-defined context for private data for the
5849b94acceSBarry Smith          function evaluation routine (may be null)
5859b94acceSBarry Smith 
5869b94acceSBarry Smith    Notes:
5879b94acceSBarry Smith    SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
5889b94acceSBarry Smith    methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
5899b94acceSBarry Smith    SNESSetFunction().
5909b94acceSBarry Smith 
5919b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimization, function
5929b94acceSBarry Smith 
593a86d99e1SLois Curfman McInnes .seealso:  SNESGetMinimizationFunction(), SNESComputeMinimizationFunction(),
594a86d99e1SLois Curfman McInnes            SNESSetHessian(), SNESSetGradient()
5959b94acceSBarry Smith @*/
5969b94acceSBarry Smith int SNESSetMinimizationFunction(SNES snes,int (*func)(SNES,Vec,double*,void*),
5979b94acceSBarry Smith                       void *ctx)
5989b94acceSBarry Smith {
5999b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
6009b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
60148d91487SBarry Smith     "SNESSetMinimizationFunction:Only for SNES_UNCONSTRAINED_MINIMIZATION");
6029b94acceSBarry Smith   snes->computeumfunction   = func;
6039b94acceSBarry Smith   snes->umfunP              = ctx;
6049b94acceSBarry Smith   return 0;
6059b94acceSBarry Smith }
6069b94acceSBarry Smith 
6079b94acceSBarry Smith /*@
6089b94acceSBarry Smith    SNESComputeMinimizationFunction - Computes the function that has been
6099b94acceSBarry Smith    set with SNESSetMinimizationFunction().
6109b94acceSBarry Smith 
6119b94acceSBarry Smith    Input Parameters:
6129b94acceSBarry Smith .  snes - the SNES context
6139b94acceSBarry Smith .  x - input vector
6149b94acceSBarry Smith 
6159b94acceSBarry Smith    Output Parameter:
6169b94acceSBarry Smith .  y - function value
6179b94acceSBarry Smith 
6189b94acceSBarry Smith    Notes:
6199b94acceSBarry Smith    SNESComputeMinimizationFunction() is valid only for
6209b94acceSBarry Smith    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
6219b94acceSBarry Smith    SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction().
622a86d99e1SLois Curfman McInnes 
623a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, minimization, function
624a86d99e1SLois Curfman McInnes 
625a86d99e1SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESGetMinimizationFunction(),
626a86d99e1SLois Curfman McInnes           SNESComputeGradient(), SNESComputeHessian()
6279b94acceSBarry Smith @*/
6289b94acceSBarry Smith int SNESComputeMinimizationFunction(SNES snes,Vec x,double *y)
6299b94acceSBarry Smith {
6309b94acceSBarry Smith   int    ierr;
6319b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
63248d91487SBarry Smith     "SNESComputeMinimizationFunction:Only for SNES_UNCONSTRAINED_MINIMIZATION");
6339b94acceSBarry Smith   PLogEventBegin(SNES_MinimizationFunctionEval,snes,x,y,0);
6349b94acceSBarry Smith   ierr = (*snes->computeumfunction)(snes,x,y,snes->umfunP); CHKERRQ(ierr);
6359b94acceSBarry Smith   PLogEventEnd(SNES_MinimizationFunctionEval,snes,x,y,0);
6369b94acceSBarry Smith   return 0;
6379b94acceSBarry Smith }
6389b94acceSBarry Smith 
6399b94acceSBarry Smith /*@C
6409b94acceSBarry Smith    SNESSetGradient - Sets the gradient evaluation routine and gradient
6419b94acceSBarry Smith    vector for use by the SNES routines.
6429b94acceSBarry Smith 
6439b94acceSBarry Smith    Input Parameters:
6449b94acceSBarry Smith .  snes - the SNES context
6459b94acceSBarry Smith .  func - function evaluation routine
6469b94acceSBarry Smith .  ctx - optional user-defined function context
6479b94acceSBarry Smith .  r - vector to store gradient value
6489b94acceSBarry Smith 
6499b94acceSBarry Smith    Calling sequence of func:
6509b94acceSBarry Smith .  func (SNES, Vec x, Vec g, void *ctx);
6519b94acceSBarry Smith 
6529b94acceSBarry Smith .  x - input vector
6539b94acceSBarry Smith .  g - gradient vector
6549b94acceSBarry Smith .  ctx - optional user-defined context for private data for the
6559b94acceSBarry Smith          function evaluation routine (may be null)
6569b94acceSBarry Smith 
6579b94acceSBarry Smith    Notes:
6589b94acceSBarry Smith    SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
6599b94acceSBarry Smith    methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
6609b94acceSBarry Smith    SNESSetFunction().
6619b94acceSBarry Smith 
6629b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
6639b94acceSBarry Smith 
664a86d99e1SLois Curfman McInnes .seealso: SNESGetGradient(), SNESComputeGradient(), SNESSetHessian(),
665a86d99e1SLois Curfman McInnes           SNESSetMinimizationFunction(),
6669b94acceSBarry Smith @*/
6679b94acceSBarry Smith int SNESSetGradient(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),
6689b94acceSBarry Smith                      void *ctx)
6699b94acceSBarry Smith {
6709b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
6719b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
67248d91487SBarry Smith     "SNESSetGradient:For SNES_UNCONSTRAINED_MINIMIZATION only");
6739b94acceSBarry Smith   snes->computefunction     = func;
6749b94acceSBarry Smith   snes->vec_func            = snes->vec_func_always = r;
6759b94acceSBarry Smith   snes->funP                = ctx;
6769b94acceSBarry Smith   return 0;
6779b94acceSBarry Smith }
6789b94acceSBarry Smith 
6799b94acceSBarry Smith /*@
680a86d99e1SLois Curfman McInnes    SNESComputeGradient - Computes the gradient that has been set with
681a86d99e1SLois Curfman McInnes    SNESSetGradient().
6829b94acceSBarry Smith 
6839b94acceSBarry Smith    Input Parameters:
6849b94acceSBarry Smith .  snes - the SNES context
6859b94acceSBarry Smith .  x - input vector
6869b94acceSBarry Smith 
6879b94acceSBarry Smith    Output Parameter:
6889b94acceSBarry Smith .  y - gradient vector
6899b94acceSBarry Smith 
6909b94acceSBarry Smith    Notes:
6919b94acceSBarry Smith    SNESComputeGradient() is valid only for
6929b94acceSBarry Smith    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
6939b94acceSBarry Smith    SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction().
694a86d99e1SLois Curfman McInnes 
695a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, gradient
696a86d99e1SLois Curfman McInnes 
697a86d99e1SLois Curfman McInnes .seealso:  SNESSetGradient(), SNESGetGradient(),
698a86d99e1SLois Curfman McInnes            SNESComputeMinimizationFunction(), SNESComputeHessian()
6999b94acceSBarry Smith @*/
7009b94acceSBarry Smith int SNESComputeGradient(SNES snes,Vec x, Vec y)
7019b94acceSBarry Smith {
7029b94acceSBarry Smith   int    ierr;
7039b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
70448d91487SBarry Smith     "SNESComputeGradient:For SNES_UNCONSTRAINED_MINIMIZATION only");
7059b94acceSBarry Smith   PLogEventBegin(SNES_GradientEval,snes,x,y,0);
7069b94acceSBarry Smith   ierr = (*snes->computefunction)(snes,x,y,snes->funP); CHKERRQ(ierr);
7079b94acceSBarry Smith   PLogEventEnd(SNES_GradientEval,snes,x,y,0);
7089b94acceSBarry Smith   return 0;
7099b94acceSBarry Smith }
7109b94acceSBarry Smith 
71162fef451SLois Curfman McInnes /*@
71262fef451SLois Curfman McInnes    SNESComputeJacobian - Computes the Jacobian matrix that has been
71362fef451SLois Curfman McInnes    set with SNESSetJacobian().
71462fef451SLois Curfman McInnes 
71562fef451SLois Curfman McInnes    Input Parameters:
71662fef451SLois Curfman McInnes .  snes - the SNES context
71762fef451SLois Curfman McInnes .  x - input vector
71862fef451SLois Curfman McInnes 
71962fef451SLois Curfman McInnes    Output Parameters:
72062fef451SLois Curfman McInnes .  A - Jacobian matrix
72162fef451SLois Curfman McInnes .  B - optional preconditioning matrix
72262fef451SLois Curfman McInnes .  flag - flag indicating matrix structure
72362fef451SLois Curfman McInnes 
72462fef451SLois Curfman McInnes    Notes:
72562fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
72662fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
72762fef451SLois Curfman McInnes 
72862fef451SLois Curfman McInnes    See SLESSetOperators() for information about setting the flag parameter.
72962fef451SLois Curfman McInnes 
73062fef451SLois Curfman McInnes    SNESComputeJacobian() is valid only for SNES_NONLINEAR_EQUATIONS
73162fef451SLois Curfman McInnes    methods. An analogous routine for SNES_UNCONSTRAINED_MINIMIZATION
73262fef451SLois Curfman McInnes    methods is SNESComputeJacobian().
73362fef451SLois Curfman McInnes 
73462fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix
73562fef451SLois Curfman McInnes 
73662fef451SLois Curfman McInnes .seealso:  SNESSetJacobian(), SLESSetOperators()
73762fef451SLois Curfman McInnes @*/
7389b94acceSBarry Smith int SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
7399b94acceSBarry Smith {
7409b94acceSBarry Smith   int    ierr;
7419b94acceSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1,
74248d91487SBarry Smith     "SNESComputeJacobian: For SNES_NONLINEAR_EQUATIONS only");
7439b94acceSBarry Smith   if (!snes->computejacobian) return 0;
7449b94acceSBarry Smith   PLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);
745c4fc05e7SBarry Smith   *flg = DIFFERENT_NONZERO_PATTERN;
7469b94acceSBarry Smith   ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP); CHKERRQ(ierr);
7479b94acceSBarry Smith   PLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);
748*6d84be18SBarry Smith   /* make sure user returned a correct Jacobian and preconditioner */
749*6d84be18SBarry Smith   PETSCVALIDHEADERSPECIFIC(*A,MAT_COOKIE);
750*6d84be18SBarry Smith   PETSCVALIDHEADERSPECIFIC(*B,MAT_COOKIE);
7519b94acceSBarry Smith   return 0;
7529b94acceSBarry Smith }
7539b94acceSBarry Smith 
75462fef451SLois Curfman McInnes /*@
75562fef451SLois Curfman McInnes    SNESComputeHessian - Computes the Hessian matrix that has been
75662fef451SLois Curfman McInnes    set with SNESSetHessian().
75762fef451SLois Curfman McInnes 
75862fef451SLois Curfman McInnes    Input Parameters:
75962fef451SLois Curfman McInnes .  snes - the SNES context
76062fef451SLois Curfman McInnes .  x - input vector
76162fef451SLois Curfman McInnes 
76262fef451SLois Curfman McInnes    Output Parameters:
76362fef451SLois Curfman McInnes .  A - Hessian matrix
76462fef451SLois Curfman McInnes .  B - optional preconditioning matrix
76562fef451SLois Curfman McInnes .  flag - flag indicating matrix structure
76662fef451SLois Curfman McInnes 
76762fef451SLois Curfman McInnes    Notes:
76862fef451SLois Curfman McInnes    Most users should not need to explicitly call this routine, as it
76962fef451SLois Curfman McInnes    is used internally within the nonlinear solvers.
77062fef451SLois Curfman McInnes 
77162fef451SLois Curfman McInnes    See SLESSetOperators() for information about setting the flag parameter.
77262fef451SLois Curfman McInnes 
77362fef451SLois Curfman McInnes    SNESComputeHessian() is valid only for
77462fef451SLois Curfman McInnes    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
77562fef451SLois Curfman McInnes    SNES_NONLINEAR_EQUATIONS methods is SNESComputeJacobian().
77662fef451SLois Curfman McInnes 
77762fef451SLois Curfman McInnes .keywords: SNES, compute, Hessian, matrix
77862fef451SLois Curfman McInnes 
779a86d99e1SLois Curfman McInnes .seealso:  SNESSetHessian(), SLESSetOperators(), SNESComputeGradient(),
780a86d99e1SLois Curfman McInnes            SNESComputeMinimizationFunction()
78162fef451SLois Curfman McInnes @*/
78262fef451SLois Curfman McInnes int SNESComputeHessian(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag)
7839b94acceSBarry Smith {
7849b94acceSBarry Smith   int    ierr;
7859b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
78648d91487SBarry Smith     "SNESComputeHessian:For SNES_UNCONSTRAINED_MINIMIZATION only");
7879b94acceSBarry Smith   if (!snes->computejacobian) return 0;
78862fef451SLois Curfman McInnes   PLogEventBegin(SNES_HessianEval,snes,x,*A,*B);
78962fef451SLois Curfman McInnes   ierr = (*snes->computejacobian)(snes,x,A,B,flag,snes->jacP); CHKERRQ(ierr);
79062fef451SLois Curfman McInnes   PLogEventEnd(SNES_HessianEval,snes,x,*A,*B);
7919b94acceSBarry Smith   return 0;
7929b94acceSBarry Smith }
7939b94acceSBarry Smith 
7949b94acceSBarry Smith /*@C
7959b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
7969b94acceSBarry Smith    location to store it.
7979b94acceSBarry Smith 
7989b94acceSBarry Smith    Input Parameters:
7999b94acceSBarry Smith .  snes - the SNES context
8009b94acceSBarry Smith .  A - Jacobian matrix
8019b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
8029b94acceSBarry Smith .  func - Jacobian evaluation routine
8039b94acceSBarry Smith .  ctx - optional user-defined context for private data for the
8049b94acceSBarry Smith          Jacobian evaluation routine (may be null)
8059b94acceSBarry Smith 
8069b94acceSBarry Smith    Calling sequence of func:
8079b94acceSBarry Smith .  func (SNES,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
8089b94acceSBarry Smith 
8099b94acceSBarry Smith .  x - input vector
8109b94acceSBarry Smith .  A - Jacobian matrix
8119b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
812ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
813ac21db08SLois Curfman McInnes    structure (same as flag in SLESSetOperators())
8149b94acceSBarry Smith .  ctx - optional user-defined Jacobian context
8159b94acceSBarry Smith 
8169b94acceSBarry Smith    Notes:
817ac21db08SLois Curfman McInnes    See SLESSetOperators() for information about setting the flag input
818ac21db08SLois Curfman McInnes    parameter in the routine func().  Be sure to read this information!
819ac21db08SLois Curfman McInnes 
820ac21db08SLois Curfman McInnes    The routine func() takes Mat * as the matrix arguments rather than Mat.
8219b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
8229b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
8239b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
8249b94acceSBarry Smith    throughout the global iterations.
8259b94acceSBarry Smith 
8269b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
8279b94acceSBarry Smith 
828ac21db08SLois Curfman McInnes .seealso: SLESSetOperators(), SNESSetFunction()
8299b94acceSBarry Smith @*/
8309b94acceSBarry Smith int SNESSetJacobian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,
8319b94acceSBarry Smith                     MatStructure*,void*),void *ctx)
8329b94acceSBarry Smith {
8339b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
8349b94acceSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1,
83548d91487SBarry Smith     "SNESSetJacobian:For SNES_NONLINEAR_EQUATIONS only");
8369b94acceSBarry Smith   snes->computejacobian = func;
8379b94acceSBarry Smith   snes->jacP            = ctx;
8389b94acceSBarry Smith   snes->jacobian        = A;
8399b94acceSBarry Smith   snes->jacobian_pre    = B;
8409b94acceSBarry Smith   return 0;
8419b94acceSBarry Smith }
84262fef451SLois Curfman McInnes 
843b4fd4287SBarry Smith /*@
844b4fd4287SBarry Smith    SNESGetJacobian - Returns the Jacobian matrix and optionally the user
845b4fd4287SBarry Smith    provided context for evaluating the Jacobian.
846b4fd4287SBarry Smith 
847b4fd4287SBarry Smith    Input Parameter:
848b4fd4287SBarry Smith .  snes - the nonlinear solver context
849b4fd4287SBarry Smith 
850b4fd4287SBarry Smith    Output Parameters:
851b4fd4287SBarry Smith .  A - location to stash Jacobian matrix (or PETSC_NULL)
852b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
853b4fd4287SBarry Smith .  ctx - location to stash Jacobian ctx (or PETSC_NULL)
854b4fd4287SBarry Smith 
855b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
856b4fd4287SBarry Smith @*/
857b4fd4287SBarry Smith int SNESGetJacobian(SNES snes,Mat *A,Mat *B, void **ctx)
858b4fd4287SBarry Smith {
85962fef451SLois Curfman McInnes   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1,
86062fef451SLois Curfman McInnes     "SNESSetJacobian:For SNES_NONLINEAR_EQUATIONS only");
861b4fd4287SBarry Smith   if (A)   *A = snes->jacobian;
862b4fd4287SBarry Smith   if (B)   *B = snes->jacobian_pre;
863b4fd4287SBarry Smith   if (ctx) *ctx = snes->jacP;
864b4fd4287SBarry Smith   return 0;
865b4fd4287SBarry Smith }
866b4fd4287SBarry Smith 
8679b94acceSBarry Smith /*@C
8689b94acceSBarry Smith    SNESSetHessian - Sets the function to compute Hessian as well as the
8699b94acceSBarry Smith    location to store it.
8709b94acceSBarry Smith 
8719b94acceSBarry Smith    Input Parameters:
8729b94acceSBarry Smith .  snes - the SNES context
8739b94acceSBarry Smith .  A - Hessian matrix
8749b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Hessian)
8759b94acceSBarry Smith .  func - Jacobian evaluation routine
8769b94acceSBarry Smith .  ctx - optional user-defined context for private data for the
8779b94acceSBarry Smith          Hessian evaluation routine (may be null)
8789b94acceSBarry Smith 
8799b94acceSBarry Smith    Calling sequence of func:
8809b94acceSBarry Smith .  func (SNES,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
8819b94acceSBarry Smith 
8829b94acceSBarry Smith .  x - input vector
8839b94acceSBarry Smith .  A - Hessian matrix
8849b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
885ac21db08SLois Curfman McInnes .  flag - flag indicating information about the preconditioner matrix
886ac21db08SLois Curfman McInnes    structure (same as flag in SLESSetOperators())
8879b94acceSBarry Smith .  ctx - optional user-defined Hessian context
8889b94acceSBarry Smith 
8899b94acceSBarry Smith    Notes:
890ac21db08SLois Curfman McInnes    See SLESSetOperators() for information about setting the flag input
891ac21db08SLois Curfman McInnes    parameter in the routine func().  Be sure to read this information!
892ac21db08SLois Curfman McInnes 
8939b94acceSBarry Smith    The function func() takes Mat * as the matrix arguments rather than Mat.
8949b94acceSBarry Smith    This allows the Hessian evaluation routine to replace A and/or B with a
8959b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
8969b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
8979b94acceSBarry Smith    throughout the global iterations.
8989b94acceSBarry Smith 
8999b94acceSBarry Smith .keywords: SNES, nonlinear, set, Hessian, matrix
9009b94acceSBarry Smith 
901ac21db08SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESSetGradient(), SLESSetOperators()
9029b94acceSBarry Smith @*/
9039b94acceSBarry Smith int SNESSetHessian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,
9049b94acceSBarry Smith                     MatStructure*,void*),void *ctx)
9059b94acceSBarry Smith {
9069b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
9079b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
90848d91487SBarry Smith     "SNESSetHessian:For SNES_UNCONSTRAINED_MINIMIZATION only");
9099b94acceSBarry Smith   snes->computejacobian = func;
9109b94acceSBarry Smith   snes->jacP            = ctx;
9119b94acceSBarry Smith   snes->jacobian        = A;
9129b94acceSBarry Smith   snes->jacobian_pre    = B;
9139b94acceSBarry Smith   return 0;
9149b94acceSBarry Smith }
9159b94acceSBarry Smith 
91662fef451SLois Curfman McInnes /*@
91762fef451SLois Curfman McInnes    SNESGetHessian - Returns the Hessian matrix and optionally the user
91862fef451SLois Curfman McInnes    provided context for evaluating the Hessian.
91962fef451SLois Curfman McInnes 
92062fef451SLois Curfman McInnes    Input Parameter:
92162fef451SLois Curfman McInnes .  snes - the nonlinear solver context
92262fef451SLois Curfman McInnes 
92362fef451SLois Curfman McInnes    Output Parameters:
92462fef451SLois Curfman McInnes .  A - location to stash Hessian matrix (or PETSC_NULL)
92562fef451SLois Curfman McInnes .  B - location to stash preconditioner matrix (or PETSC_NULL)
92662fef451SLois Curfman McInnes .  ctx - location to stash Hessian ctx (or PETSC_NULL)
92762fef451SLois Curfman McInnes 
92862fef451SLois Curfman McInnes .seealso: SNESSetHessian(), SNESComputeHessian()
92962fef451SLois Curfman McInnes @*/
93062fef451SLois Curfman McInnes int SNESGetHessian(SNES snes,Mat *A,Mat *B, void **ctx)
93162fef451SLois Curfman McInnes {
93262fef451SLois Curfman McInnes   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
93362fef451SLois Curfman McInnes     "SNESSetHessian:For SNES_UNCONSTRAINED_MINIMIZATION only");
93462fef451SLois Curfman McInnes   if (A)   *A = snes->jacobian;
93562fef451SLois Curfman McInnes   if (B)   *B = snes->jacobian_pre;
93662fef451SLois Curfman McInnes   if (ctx) *ctx = snes->jacP;
93762fef451SLois Curfman McInnes   return 0;
93862fef451SLois Curfman McInnes }
93962fef451SLois Curfman McInnes 
9409b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
9419b94acceSBarry Smith 
9429b94acceSBarry Smith /*@
9439b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
944272ac6f2SLois Curfman McInnes    of a nonlinear solver.
9459b94acceSBarry Smith 
9469b94acceSBarry Smith    Input Parameter:
9479b94acceSBarry Smith .  snes - the SNES context
9488ddd3da0SLois Curfman McInnes .  x - the solution vector
9499b94acceSBarry Smith 
950272ac6f2SLois Curfman McInnes    Notes:
951272ac6f2SLois Curfman McInnes    For basic use of the SNES solvers the user need not explicitly call
952272ac6f2SLois Curfman McInnes    SNESSetUp(), since these actions will automatically occur during
953272ac6f2SLois Curfman McInnes    the call to SNESSolve().  However, if one wishes to control this
954272ac6f2SLois Curfman McInnes    phase separately, SNESSetUp() should be called after SNESCreate()
955272ac6f2SLois Curfman McInnes    and optional routines of the form SNESSetXXX(), but before SNESSolve().
956272ac6f2SLois Curfman McInnes 
9579b94acceSBarry Smith .keywords: SNES, nonlinear, setup
9589b94acceSBarry Smith 
9599b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
9609b94acceSBarry Smith @*/
9618ddd3da0SLois Curfman McInnes int SNESSetUp(SNES snes,Vec x)
9629b94acceSBarry Smith {
963272ac6f2SLois Curfman McInnes   int ierr, flg;
9649b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
9658ddd3da0SLois Curfman McInnes   PETSCVALIDHEADERSPECIFIC(x,VEC_COOKIE);
9668ddd3da0SLois Curfman McInnes   snes->vec_sol = snes->vec_sol_always = x;
9679b94acceSBarry Smith 
968272ac6f2SLois Curfman McInnes   ierr = OptionsHasName(snes->prefix,"-snes_mf", &flg);  CHKERRQ(ierr);
96931615d04SLois Curfman McInnes   if (flg) {
970272ac6f2SLois Curfman McInnes     Mat J;
971272ac6f2SLois Curfman McInnes     ierr = SNESDefaultMatrixFreeMatCreate(snes,snes->vec_sol,&J);CHKERRQ(ierr);
972272ac6f2SLois Curfman McInnes     ierr = SNESSetJacobian(snes,J,J,0,snes->funP); CHKERRQ(ierr);
973272ac6f2SLois Curfman McInnes     PLogObjectParent(snes,J);
974272ac6f2SLois Curfman McInnes     snes->mfshell = J;
97531615d04SLois Curfman McInnes     if (snes->method_class == SNES_NONLINEAR_EQUATIONS) PLogInfo((PetscObject)snes,
97631615d04SLois Curfman McInnes         "SNESSetUp: Setting default matrix-free Jacobian routines\n");
97731615d04SLois Curfman McInnes     else if (snes->method_class == SNES_NONLINEAR_EQUATIONS) PLogInfo((PetscObject)snes,
97831615d04SLois Curfman McInnes         "SNESSetUp: Setting default matrix-free Hessian routines\n");
979272ac6f2SLois Curfman McInnes   }
9809b94acceSBarry Smith   if ((snes->method_class == SNES_NONLINEAR_EQUATIONS)) {
98137fcc0dbSBarry Smith     if (!snes->vec_func) SETERRQ(1,"SNESSetUp:Must call SNESSetFunction() first");
98237fcc0dbSBarry Smith     if (!snes->computefunction) SETERRQ(1,"SNESSetUp:Must call SNESSetFunction() first");
98337fcc0dbSBarry Smith     if (!snes->jacobian) SETERRQ(1,"SNESSetUp:Must call SNESSetJacobian() first");
984d804570eSBarry Smith     if (snes->vec_func == snes->vec_sol) SETERRQ(1,"SNESSetUp:Solution vector cannot be function vector");
985a703fe33SLois Curfman McInnes 
986a703fe33SLois Curfman McInnes     /* Set the KSP stopping criterion to use the Eisenstat-Walker method */
987a703fe33SLois Curfman McInnes     if (snes->ksp_ewconv && snes->type != SNES_EQ_NTR) {
988a703fe33SLois Curfman McInnes       SLES sles; KSP ksp;
989a703fe33SLois Curfman McInnes       ierr = SNESGetSLES(snes,&sles); CHKERRQ(ierr);
990a703fe33SLois Curfman McInnes       ierr = SLESGetKSP(sles,&ksp); CHKERRQ(ierr);
991a703fe33SLois Curfman McInnes       ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private,
992a703fe33SLois Curfman McInnes              (void *)snes); CHKERRQ(ierr);
993a703fe33SLois Curfman McInnes     }
9949b94acceSBarry Smith   } else if ((snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION)) {
99537fcc0dbSBarry Smith     if (!snes->vec_func) SETERRQ(1,"SNESSetUp:Must call SNESSetGradient() first");
99637fcc0dbSBarry Smith     if (!snes->computefunction) SETERRQ(1,"SNESSetUp:Must call SNESSetGradient() first");
99737fcc0dbSBarry Smith     if (!snes->computeumfunction)
99837fcc0dbSBarry Smith       SETERRQ(1,"SNESSetUp:Must call SNESSetMinimizationFunction() first");
99937fcc0dbSBarry Smith     if (!snes->jacobian) SETERRQ(1,"SNESSetUp:Must call SNESSetHessian() first");
10009b94acceSBarry Smith   } else SETERRQ(1,"SNESSetUp:Unknown method class");
1001a703fe33SLois Curfman McInnes   if (snes->setup) {ierr = (*snes->setup)(snes); CHKERRQ(ierr);}
1002a703fe33SLois Curfman McInnes   snes->setup_called = 1;
1003a703fe33SLois Curfman McInnes   return 0;
10049b94acceSBarry Smith }
10059b94acceSBarry Smith 
10069b94acceSBarry Smith /*@C
10079b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
10089b94acceSBarry Smith    with SNESCreate().
10099b94acceSBarry Smith 
10109b94acceSBarry Smith    Input Parameter:
10119b94acceSBarry Smith .  snes - the SNES context
10129b94acceSBarry Smith 
10139b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
10149b94acceSBarry Smith 
101563a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve()
10169b94acceSBarry Smith @*/
10179b94acceSBarry Smith int SNESDestroy(SNES snes)
10189b94acceSBarry Smith {
10199b94acceSBarry Smith   int ierr;
10209b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
10219b94acceSBarry Smith   ierr = (*(snes)->destroy)((PetscObject)snes); CHKERRQ(ierr);
10220452661fSBarry Smith   if (snes->kspconvctx) PetscFree(snes->kspconvctx);
10239b94acceSBarry Smith   if (snes->mfshell) MatDestroy(snes->mfshell);
10249b94acceSBarry Smith   ierr = SLESDestroy(snes->sles); CHKERRQ(ierr);
10259b94acceSBarry Smith   PLogObjectDestroy((PetscObject)snes);
10260452661fSBarry Smith   PetscHeaderDestroy((PetscObject)snes);
10279b94acceSBarry Smith   return 0;
10289b94acceSBarry Smith }
10299b94acceSBarry Smith 
10309b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
10319b94acceSBarry Smith 
10329b94acceSBarry Smith /*@
10339b94acceSBarry Smith    SNESSetMaxIterations - Sets the maximum number of global iterations to use.
10349b94acceSBarry Smith 
10359b94acceSBarry Smith    Input Parameters:
10369b94acceSBarry Smith .  snes - the SNES context
10379b94acceSBarry Smith .  maxits - maximum number of iterations to use
10389b94acceSBarry Smith 
10399b94acceSBarry Smith    Options Database Key:
10409b94acceSBarry Smith $  -snes_max_it  maxits
10419b94acceSBarry Smith 
10429b94acceSBarry Smith    Note:
10439b94acceSBarry Smith    The default maximum number of iterations is 50.
10449b94acceSBarry Smith 
10459b94acceSBarry Smith .keywords: SNES, nonlinear, set, maximum, iterations
10469b94acceSBarry Smith 
10479b94acceSBarry Smith .seealso: SNESSetMaxFunctionEvaluations()
10489b94acceSBarry Smith @*/
10499b94acceSBarry Smith int SNESSetMaxIterations(SNES snes,int maxits)
10509b94acceSBarry Smith {
10519b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
10529b94acceSBarry Smith   snes->max_its = maxits;
10539b94acceSBarry Smith   return 0;
10549b94acceSBarry Smith }
10559b94acceSBarry Smith 
10569b94acceSBarry Smith /*@
10579b94acceSBarry Smith    SNESSetMaxFunctionEvaluations - Sets the maximum number of function
10589b94acceSBarry Smith    evaluations to use.
10599b94acceSBarry Smith 
10609b94acceSBarry Smith    Input Parameters:
10619b94acceSBarry Smith .  snes - the SNES context
10629b94acceSBarry Smith .  maxf - maximum number of function evaluations
10639b94acceSBarry Smith 
10649b94acceSBarry Smith    Options Database Key:
10659b94acceSBarry Smith $  -snes_max_funcs maxf
10669b94acceSBarry Smith 
10679b94acceSBarry Smith    Note:
10689b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
10699b94acceSBarry Smith 
10709b94acceSBarry Smith .keywords: SNES, nonlinear, set, maximum, function, evaluations
10719b94acceSBarry Smith 
10729b94acceSBarry Smith .seealso: SNESSetMaxIterations()
10739b94acceSBarry Smith @*/
10749b94acceSBarry Smith int SNESSetMaxFunctionEvaluations(SNES snes,int maxf)
10759b94acceSBarry Smith {
10769b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
10779b94acceSBarry Smith   snes->max_funcs = maxf;
10789b94acceSBarry Smith   return 0;
10799b94acceSBarry Smith }
10809b94acceSBarry Smith 
10819b94acceSBarry Smith /*@
10829b94acceSBarry Smith    SNESSetRelativeTolerance - Sets the relative convergence tolerance.
10839b94acceSBarry Smith 
10849b94acceSBarry Smith    Input Parameters:
10859b94acceSBarry Smith .  snes - the SNES context
10869b94acceSBarry Smith .  rtol - tolerance
10879b94acceSBarry Smith 
10889b94acceSBarry Smith    Options Database Key:
10899b94acceSBarry Smith $    -snes_rtol tol
10909b94acceSBarry Smith 
10919b94acceSBarry Smith .keywords: SNES, nonlinear, set, relative, convergence, tolerance
10929b94acceSBarry Smith 
10939b94acceSBarry Smith .seealso: SNESSetAbsoluteTolerance(), SNESSetSolutionTolerance(),
10949b94acceSBarry Smith            SNESSetTruncationTolerance()
10959b94acceSBarry Smith @*/
10969b94acceSBarry Smith int SNESSetRelativeTolerance(SNES snes,double rtol)
10979b94acceSBarry Smith {
10989b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
10999b94acceSBarry Smith   snes->rtol = rtol;
11009b94acceSBarry Smith   return 0;
11019b94acceSBarry Smith }
11029b94acceSBarry Smith 
11039b94acceSBarry Smith /*@
11049b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
11059b94acceSBarry Smith 
11069b94acceSBarry Smith    Input Parameters:
11079b94acceSBarry Smith .  snes - the SNES context
11089b94acceSBarry Smith .  tol - tolerance
11099b94acceSBarry Smith 
11109b94acceSBarry Smith    Options Database Key:
11119b94acceSBarry Smith $    -snes_trtol tol
11129b94acceSBarry Smith 
11139b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
11149b94acceSBarry Smith 
11159b94acceSBarry Smith .seealso: SNESSetAbsoluteTolerance(), SNESSetSolutionTolerance(),
11169b94acceSBarry Smith            SNESSetTruncationTolerance()
11179b94acceSBarry Smith @*/
11189b94acceSBarry Smith int SNESSetTrustRegionTolerance(SNES snes,double tol)
11199b94acceSBarry Smith {
11209b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
11219b94acceSBarry Smith   snes->deltatol = tol;
11229b94acceSBarry Smith   return 0;
11239b94acceSBarry Smith }
11249b94acceSBarry Smith 
11259b94acceSBarry Smith /*@
11269b94acceSBarry Smith    SNESSetAbsoluteTolerance - Sets the absolute convergence tolerance.
11279b94acceSBarry Smith 
11289b94acceSBarry Smith    Input Parameters:
11299b94acceSBarry Smith .  snes - the SNES context
11309b94acceSBarry Smith .  atol - tolerance
11319b94acceSBarry Smith 
11329b94acceSBarry Smith    Options Database Key:
11339b94acceSBarry Smith $    -snes_atol tol
11349b94acceSBarry Smith 
11359b94acceSBarry Smith .keywords: SNES, nonlinear, set, absolute, convergence, tolerance
11369b94acceSBarry Smith 
11379b94acceSBarry Smith .seealso: SNESSetRelativeTolerance(), SNESSetSolutionTolerance(),
11389b94acceSBarry Smith            SNESSetTruncationTolerance()
11399b94acceSBarry Smith @*/
11409b94acceSBarry Smith int SNESSetAbsoluteTolerance(SNES snes,double atol)
11419b94acceSBarry Smith {
11429b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
11439b94acceSBarry Smith   snes->atol = atol;
11449b94acceSBarry Smith   return 0;
11459b94acceSBarry Smith }
11469b94acceSBarry Smith 
11479b94acceSBarry Smith /*@
11489b94acceSBarry Smith    SNESSetTruncationTolerance - Sets the tolerance that may be used by the
11499b94acceSBarry Smith    step routines to control the accuracy of the step computation.
11509b94acceSBarry Smith 
11519b94acceSBarry Smith    Input Parameters:
11529b94acceSBarry Smith .  snes - the SNES context
11539b94acceSBarry Smith .  tol - tolerance
11549b94acceSBarry Smith 
11559b94acceSBarry Smith    Options Database Key:
11569b94acceSBarry Smith $    -snes_ttol tol
11579b94acceSBarry Smith 
11589b94acceSBarry Smith    Notes:
11599b94acceSBarry Smith    If the step computation involves an application of the inverse
11609b94acceSBarry Smith    Jacobian (or Hessian), this parameter may be used to control the
1161a703fe33SLois Curfman McInnes    accuracy of that application.
11629b94acceSBarry Smith 
11639b94acceSBarry Smith .keywords: SNES, nonlinear, set, truncation, tolerance
11649b94acceSBarry Smith 
11659b94acceSBarry Smith .seealso: SNESSetRelativeTolerance(), SNESSetSolutionTolerance(),
11669b94acceSBarry Smith           SNESSetAbsoluteTolerance()
11679b94acceSBarry Smith @*/
11689b94acceSBarry Smith int SNESSetTruncationTolerance(SNES snes,double tol)
11699b94acceSBarry Smith {
11709b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
11719b94acceSBarry Smith   snes->trunctol = tol;
11729b94acceSBarry Smith   return 0;
11739b94acceSBarry Smith }
11749b94acceSBarry Smith 
11759b94acceSBarry Smith /*@
11769b94acceSBarry Smith    SNESSetSolutionTolerance - Sets the convergence tolerance in terms of
11779b94acceSBarry Smith    the norm of the change in the solution between steps.
11789b94acceSBarry Smith 
11799b94acceSBarry Smith    Input Parameters:
11809b94acceSBarry Smith .  snes - the SNES context
11819b94acceSBarry Smith .  tol - tolerance
11829b94acceSBarry Smith 
11839b94acceSBarry Smith    Options Database Key:
11849b94acceSBarry Smith $    -snes_stol tol
11859b94acceSBarry Smith 
11869b94acceSBarry Smith .keywords: SNES, nonlinear, set, solution, tolerance
11879b94acceSBarry Smith 
11889b94acceSBarry Smith .seealso: SNESSetTruncationTolerance(), SNESSetRelativeTolerance(),
11899b94acceSBarry Smith           SNESSetAbsoluteTolerance()
11909b94acceSBarry Smith @*/
11919b94acceSBarry Smith int SNESSetSolutionTolerance( SNES snes, double tol )
11929b94acceSBarry Smith {
11939b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
11949b94acceSBarry Smith   snes->xtol = tol;
11959b94acceSBarry Smith   return 0;
11969b94acceSBarry Smith }
11979b94acceSBarry Smith 
11989b94acceSBarry Smith /*@
11999b94acceSBarry Smith    SNESSetMinFunctionTolerance - Sets the minimum allowable function tolerance
12009b94acceSBarry Smith    for unconstrained minimization solvers.
12019b94acceSBarry Smith 
12029b94acceSBarry Smith    Input Parameters:
12039b94acceSBarry Smith .  snes - the SNES context
12049b94acceSBarry Smith .  ftol - minimum function tolerance
12059b94acceSBarry Smith 
12069b94acceSBarry Smith    Options Database Key:
12079b94acceSBarry Smith $    -snes_fmin ftol
12089b94acceSBarry Smith 
12099b94acceSBarry Smith    Note:
12109b94acceSBarry Smith    SNESSetMinFunctionTolerance() is valid for SNES_UNCONSTRAINED_MINIMIZATION
12119b94acceSBarry Smith    methods only.
12129b94acceSBarry Smith 
12139b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimum, convergence, function, tolerance
12149b94acceSBarry Smith 
12159b94acceSBarry Smith .seealso: SNESSetRelativeTolerance(), SNESSetSolutionTolerance(),
12169b94acceSBarry Smith            SNESSetTruncationTolerance()
12179b94acceSBarry Smith @*/
12189b94acceSBarry Smith int SNESSetMinFunctionTolerance(SNES snes,double ftol)
12199b94acceSBarry Smith {
12209b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
12219b94acceSBarry Smith   snes->fmin = ftol;
12229b94acceSBarry Smith   return 0;
12239b94acceSBarry Smith }
12249b94acceSBarry Smith 
12259b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
12269b94acceSBarry Smith 
12279b94acceSBarry Smith /*@C
12289b94acceSBarry Smith    SNESSetMonitor - Sets the function that is to be used at every
12299b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
12309b94acceSBarry Smith    progress.
12319b94acceSBarry Smith 
12329b94acceSBarry Smith    Input Parameters:
12339b94acceSBarry Smith .  snes - the SNES context
12349b94acceSBarry Smith .  func - monitoring routine
12359b94acceSBarry Smith .  mctx - optional user-defined context for private data for the
12369b94acceSBarry Smith           monitor routine (may be null)
12379b94acceSBarry Smith 
12389b94acceSBarry Smith    Calling sequence of func:
1239682d7d0cSBarry Smith    int func(SNES snes,int its, Vec x,Vec f,double norm,void *mctx)
12409b94acceSBarry Smith 
12419b94acceSBarry Smith $    snes - the SNES context
12429b94acceSBarry Smith $    its - iteration number
12439b94acceSBarry Smith $    mctx - optional monitoring context
12449b94acceSBarry Smith $
12459b94acceSBarry Smith $ SNES_NONLINEAR_EQUATIONS methods:
12469b94acceSBarry Smith $    norm - 2-norm function value (may be estimated)
12479b94acceSBarry Smith $
12489b94acceSBarry Smith $ SNES_UNCONSTRAINED_MINIMIZATION methods:
12499b94acceSBarry Smith $    norm - 2-norm gradient value (may be estimated)
12509b94acceSBarry Smith 
12519b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
12529b94acceSBarry Smith 
12539b94acceSBarry Smith .seealso: SNESDefaultMonitor()
12549b94acceSBarry Smith @*/
12559b94acceSBarry Smith int SNESSetMonitor( SNES snes, int (*func)(SNES,int,double,void*),
12569b94acceSBarry Smith                     void *mctx )
12579b94acceSBarry Smith {
12589b94acceSBarry Smith   snes->monitor = func;
12599b94acceSBarry Smith   snes->monP    = (void*)mctx;
12609b94acceSBarry Smith   return 0;
12619b94acceSBarry Smith }
12629b94acceSBarry Smith 
12639b94acceSBarry Smith /*@C
12649b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
12659b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
12669b94acceSBarry Smith 
12679b94acceSBarry Smith    Input Parameters:
12689b94acceSBarry Smith .  snes - the SNES context
12699b94acceSBarry Smith .  func - routine to test for convergence
12709b94acceSBarry Smith .  cctx - optional context for private data for the convergence routine
12719b94acceSBarry Smith           (may be null)
12729b94acceSBarry Smith 
12739b94acceSBarry Smith    Calling sequence of func:
12749b94acceSBarry Smith    int func (SNES snes,double xnorm,double gnorm,
12759b94acceSBarry Smith              double f,void *cctx)
12769b94acceSBarry Smith 
12779b94acceSBarry Smith $    snes - the SNES context
12789b94acceSBarry Smith $    cctx - optional convergence context
12799b94acceSBarry Smith $    xnorm - 2-norm of current iterate
12809b94acceSBarry Smith $
12819b94acceSBarry Smith $ SNES_NONLINEAR_EQUATIONS methods:
12829b94acceSBarry Smith $    gnorm - 2-norm of current step
12839b94acceSBarry Smith $    f - 2-norm of function
12849b94acceSBarry Smith $
12859b94acceSBarry Smith $ SNES_UNCONSTRAINED_MINIMIZATION methods:
12869b94acceSBarry Smith $    gnorm - 2-norm of current gradient
12879b94acceSBarry Smith $    f - function value
12889b94acceSBarry Smith 
12899b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
12909b94acceSBarry Smith 
12919b94acceSBarry Smith .seealso: SNESDefaultConverged()
12929b94acceSBarry Smith @*/
12939b94acceSBarry Smith int SNESSetConvergenceTest(SNES snes,
12949b94acceSBarry Smith           int (*func)(SNES,double,double,double,void*),void *cctx)
12959b94acceSBarry Smith {
12969b94acceSBarry Smith   (snes)->converged = func;
12979b94acceSBarry Smith   (snes)->cnvP      = cctx;
12989b94acceSBarry Smith   return 0;
12999b94acceSBarry Smith }
13009b94acceSBarry Smith 
13019b94acceSBarry Smith /*
13029b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
13039b94acceSBarry Smith    positive parameter delta.
13049b94acceSBarry Smith 
13059b94acceSBarry Smith     Input Parameters:
13069b94acceSBarry Smith .   snes - the SNES context
13079b94acceSBarry Smith .   y - approximate solution of linear system
13089b94acceSBarry Smith .   fnorm - 2-norm of current function
13099b94acceSBarry Smith .   delta - trust region size
13109b94acceSBarry Smith 
13119b94acceSBarry Smith     Output Parameters:
13129b94acceSBarry Smith .   gpnorm - predicted function norm at the new point, assuming local
13139b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
13149b94acceSBarry Smith     region, and exceeds zero otherwise.
13159b94acceSBarry Smith .   ynorm - 2-norm of the step
13169b94acceSBarry Smith 
13179b94acceSBarry Smith     Note:
1318ddd12767SBarry Smith     For non-trust region methods such as SNES_EQ_NLS, the parameter delta
13199b94acceSBarry Smith     is set to be the maximum allowable step size.
13209b94acceSBarry Smith 
13219b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
13229b94acceSBarry Smith */
13239b94acceSBarry Smith int SNESScaleStep_Private(SNES snes,Vec y,double *fnorm,double *delta,
13249b94acceSBarry Smith                   double *gpnorm,double *ynorm)
13259b94acceSBarry Smith {
13269b94acceSBarry Smith   double norm;
13279b94acceSBarry Smith   Scalar cnorm;
1328cddf8d76SBarry Smith   VecNorm(y,NORM_2, &norm );
13299b94acceSBarry Smith   if (norm > *delta) {
13309b94acceSBarry Smith      norm = *delta/norm;
13319b94acceSBarry Smith      *gpnorm = (1.0 - norm)*(*fnorm);
13329b94acceSBarry Smith      cnorm = norm;
13339b94acceSBarry Smith      VecScale( &cnorm, y );
13349b94acceSBarry Smith      *ynorm = *delta;
13359b94acceSBarry Smith   } else {
13369b94acceSBarry Smith      *gpnorm = 0.0;
13379b94acceSBarry Smith      *ynorm = norm;
13389b94acceSBarry Smith   }
13399b94acceSBarry Smith   return 0;
13409b94acceSBarry Smith }
13419b94acceSBarry Smith 
13429b94acceSBarry Smith /*@
13439b94acceSBarry Smith    SNESSolve - Solves a nonlinear system.  Call SNESSolve after calling
134463a78c88SLois Curfman McInnes    SNESCreate() and optional routines of the form SNESSetXXX().
13459b94acceSBarry Smith 
13469b94acceSBarry Smith    Input Parameter:
13479b94acceSBarry Smith .  snes - the SNES context
13488ddd3da0SLois Curfman McInnes .  x - the solution vector
13499b94acceSBarry Smith 
13509b94acceSBarry Smith    Output Parameter:
13519b94acceSBarry Smith    its - number of iterations until termination
13529b94acceSBarry Smith 
13538ddd3da0SLois Curfman McInnes    Note:
13548ddd3da0SLois Curfman McInnes    The user should initialize the vector, x, with the initial guess
13558ddd3da0SLois Curfman McInnes    for the nonlinear solve prior to calling SNESSolve.  In particular,
13568ddd3da0SLois Curfman McInnes    to employ an initial guess of zero, the user should explicitly set
13578ddd3da0SLois Curfman McInnes    this vector to zero by calling VecSet().
13588ddd3da0SLois Curfman McInnes 
13599b94acceSBarry Smith .keywords: SNES, nonlinear, solve
13609b94acceSBarry Smith 
136163a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESDestroy()
13629b94acceSBarry Smith @*/
13638ddd3da0SLois Curfman McInnes int SNESSolve(SNES snes,Vec x,int *its)
13649b94acceSBarry Smith {
13653c7409f5SSatish Balay   int ierr, flg;
1366052efed2SBarry Smith 
136737fcc0dbSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
1368c4fc05e7SBarry Smith   if (!snes->setup_called) {ierr = SNESSetUp(snes,x); CHKERRQ(ierr);}
1369c4fc05e7SBarry Smith   else {snes->vec_sol = snes->vec_sol_always = x;}
13709b94acceSBarry Smith   PLogEventBegin(SNES_Solve,snes,0,0,0);
13719b94acceSBarry Smith   ierr = (*(snes)->solve)(snes,its); CHKERRQ(ierr);
13729b94acceSBarry Smith   PLogEventEnd(SNES_Solve,snes,0,0,0);
13733c7409f5SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-snes_view", &flg); CHKERRQ(ierr);
13743c7409f5SSatish Balay   if (flg) { ierr = SNESView(snes,STDOUT_VIEWER_WORLD); CHKERRQ(ierr); }
13759b94acceSBarry Smith   return 0;
13769b94acceSBarry Smith }
13779b94acceSBarry Smith 
13789b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
137937fcc0dbSBarry Smith static NRList *__SNESList = 0;
13809b94acceSBarry Smith 
13819b94acceSBarry Smith /*@
13824b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
13839b94acceSBarry Smith 
13849b94acceSBarry Smith    Input Parameters:
13859b94acceSBarry Smith .  snes - the SNES context
13869b94acceSBarry Smith .  method - a known method
13879b94acceSBarry Smith 
13889b94acceSBarry Smith    Notes:
13899b94acceSBarry Smith    See "petsc/include/snes.h" for available methods (for instance)
13909b94acceSBarry Smith $  Systems of nonlinear equations:
1391ddd12767SBarry Smith $    SNES_EQ_NLS - Newton's method with line search
1392ddd12767SBarry Smith $    SNES_EQ_NTR - Newton's method with trust region
13939b94acceSBarry Smith $  Unconstrained minimization:
13949b94acceSBarry Smith $    SNES_UM_NTR - Newton's method with trust region
1395a703fe33SLois Curfman McInnes $    SNES_UM_NLS - Newton's method with line search
13969b94acceSBarry Smith 
13979b94acceSBarry Smith   Options Database Command:
13984b0e389bSBarry Smith $ -snes_type  <method>
13999b94acceSBarry Smith $    Use -help for a list of available methods
14009b94acceSBarry Smith $    (for instance, ls or tr)
1401a703fe33SLois Curfman McInnes 
1402a703fe33SLois Curfman McInnes .keysords: SNES, set, method
14039b94acceSBarry Smith @*/
14044b0e389bSBarry Smith int SNESSetType(SNES snes,SNESType method)
14059b94acceSBarry Smith {
14069b94acceSBarry Smith   int (*r)(SNES);
14079b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
14089b94acceSBarry Smith   /* Get the function pointers for the iterative method requested */
140937fcc0dbSBarry Smith   if (!__SNESList) {SNESRegisterAll();}
141037fcc0dbSBarry Smith   if (!__SNESList) {SETERRQ(1,"SNESSetType:Could not get methods");}
141137fcc0dbSBarry Smith   r =  (int (*)(SNES))NRFindRoutine( __SNESList, (int)method, (char *)0 );
14124b0e389bSBarry Smith   if (!r) {SETERRQ(1,"SNESSetType:Unknown method");}
14130452661fSBarry Smith   if (snes->data) PetscFree(snes->data);
14149b94acceSBarry Smith   snes->set_method_called = 1;
14159b94acceSBarry Smith   return (*r)(snes);
14169b94acceSBarry Smith }
14179b94acceSBarry Smith 
14189b94acceSBarry Smith /* --------------------------------------------------------------------- */
14199b94acceSBarry Smith /*@C
14209b94acceSBarry Smith    SNESRegister - Adds the method to the nonlinear solver package, given
14214b0e389bSBarry Smith    a function pointer and a nonlinear solver name of the type SNESType.
14229b94acceSBarry Smith 
14239b94acceSBarry Smith    Input Parameters:
1424ddd12767SBarry Smith .  name - for instance SNES_EQ_NLS, SNES_EQ_NTR, ...
14259b94acceSBarry Smith .  sname - corfunPonding string for name
14269b94acceSBarry Smith .  create - routine to create method context
14279b94acceSBarry Smith 
14289b94acceSBarry Smith .keywords: SNES, nonlinear, register
14299b94acceSBarry Smith 
14309b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterDestroy()
14319b94acceSBarry Smith @*/
14329b94acceSBarry Smith int SNESRegister(int name, char *sname, int (*create)(SNES))
14339b94acceSBarry Smith {
14349b94acceSBarry Smith   int ierr;
143537fcc0dbSBarry Smith   if (!__SNESList) {ierr = NRCreate(&__SNESList); CHKERRQ(ierr);}
143637fcc0dbSBarry Smith   NRRegister( __SNESList, name, sname, (int (*)(void*))create );
14379b94acceSBarry Smith   return 0;
14389b94acceSBarry Smith }
14399b94acceSBarry Smith /* --------------------------------------------------------------------- */
14409b94acceSBarry Smith /*@C
14419b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
14429b94acceSBarry Smith    registered by SNESRegister().
14439b94acceSBarry Smith 
14449b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
14459b94acceSBarry Smith 
14469b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
14479b94acceSBarry Smith @*/
14489b94acceSBarry Smith int SNESRegisterDestroy()
14499b94acceSBarry Smith {
145037fcc0dbSBarry Smith   if (__SNESList) {
145137fcc0dbSBarry Smith     NRDestroy( __SNESList );
145237fcc0dbSBarry Smith     __SNESList = 0;
14539b94acceSBarry Smith   }
14549b94acceSBarry Smith   return 0;
14559b94acceSBarry Smith }
14569b94acceSBarry Smith 
14579b94acceSBarry Smith /*
14584b0e389bSBarry Smith    SNESGetTypeFromOptions_Private - Sets the selected method from the
14599b94acceSBarry Smith    options database.
14609b94acceSBarry Smith 
14619b94acceSBarry Smith    Input Parameter:
14629b94acceSBarry Smith .  ctx - the SNES context
14639b94acceSBarry Smith 
14649b94acceSBarry Smith    Output Parameter:
14659b94acceSBarry Smith .  method -  solver method
14669b94acceSBarry Smith 
14679b94acceSBarry Smith    Returns:
14689b94acceSBarry Smith    Returns 1 if the method is found; 0 otherwise.
14699b94acceSBarry Smith 
14709b94acceSBarry Smith    Options Database Key:
14714b0e389bSBarry Smith $  -snes_type  method
14729b94acceSBarry Smith */
1473052efed2SBarry Smith int SNESGetTypeFromOptions_Private(SNES ctx,SNESType *method,int *flg)
14749b94acceSBarry Smith {
1475052efed2SBarry Smith   int ierr;
14769b94acceSBarry Smith   char sbuf[50];
1477052efed2SBarry Smith   ierr = OptionsGetString(ctx->prefix,"-snes_type", sbuf, 50, flg); CHKERRQ(ierr);
1478052efed2SBarry Smith   if (*flg) {
1479052efed2SBarry Smith     if (!__SNESList) {ierr = SNESRegisterAll(); CHKERRQ(ierr);}
148037fcc0dbSBarry Smith     *method = (SNESType)NRFindID( __SNESList, sbuf );
14819b94acceSBarry Smith   }
14829b94acceSBarry Smith   return 0;
14839b94acceSBarry Smith }
14849b94acceSBarry Smith 
14859b94acceSBarry Smith /*@C
14869a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
14879b94acceSBarry Smith 
14889b94acceSBarry Smith    Input Parameter:
14894b0e389bSBarry Smith .  snes - nonlinear solver context
14909b94acceSBarry Smith 
14919b94acceSBarry Smith    Output Parameter:
14929a28b0a6SLois Curfman McInnes .  method - SNES method (or use PETSC_NULL)
14939a28b0a6SLois Curfman McInnes .  name - name of SNES method (or use PETSC_NULL)
14949b94acceSBarry Smith 
14959b94acceSBarry Smith .keywords: SNES, nonlinear, get, method, name
14969b94acceSBarry Smith @*/
14974b0e389bSBarry Smith int SNESGetType(SNES snes, SNESType *method,char **name)
14989b94acceSBarry Smith {
149906a9b0adSLois Curfman McInnes   int ierr;
150037fcc0dbSBarry Smith   if (!__SNESList) {ierr = SNESRegisterAll(); CHKERRQ(ierr);}
15014b0e389bSBarry Smith   if (method) *method = (SNESType) snes->type;
150237fcc0dbSBarry Smith   if (name)  *name = NRFindName( __SNESList, (int) snes->type );
15039b94acceSBarry Smith   return 0;
15049b94acceSBarry Smith }
15059b94acceSBarry Smith 
15069b94acceSBarry Smith #include <stdio.h>
15079b94acceSBarry Smith /*
15084b0e389bSBarry Smith    SNESPrintTypes_Private - Prints the SNES methods available from the
15099b94acceSBarry Smith    options database.
15109b94acceSBarry Smith 
15119b94acceSBarry Smith    Input Parameters:
15129b94acceSBarry Smith .  prefix - prefix (usually "-")
15134b0e389bSBarry Smith .  name - the options database name (by default "snes_type")
15149b94acceSBarry Smith */
15154b0e389bSBarry Smith int SNESPrintTypes_Private(char* prefix,char *name)
15169b94acceSBarry Smith {
15179b94acceSBarry Smith   FuncList *entry;
151837fcc0dbSBarry Smith   if (!__SNESList) {SNESRegisterAll();}
151937fcc0dbSBarry Smith   entry = __SNESList->head;
15209b94acceSBarry Smith   fprintf(stderr," %s%s (one of)",prefix,name);
15219b94acceSBarry Smith   while (entry) {
15229b94acceSBarry Smith     fprintf(stderr," %s",entry->name);
15239b94acceSBarry Smith     entry = entry->next;
15249b94acceSBarry Smith   }
15259b94acceSBarry Smith   fprintf(stderr,"\n");
15269b94acceSBarry Smith   return 0;
15279b94acceSBarry Smith }
15289b94acceSBarry Smith 
15299b94acceSBarry Smith /*@C
15309b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
15319b94acceSBarry Smith    stored.
15329b94acceSBarry Smith 
15339b94acceSBarry Smith    Input Parameter:
15349b94acceSBarry Smith .  snes - the SNES context
15359b94acceSBarry Smith 
15369b94acceSBarry Smith    Output Parameter:
15379b94acceSBarry Smith .  x - the solution
15389b94acceSBarry Smith 
15399b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
15409b94acceSBarry Smith 
1541a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESGetGradient(), SNESGetSolutionUpdate()
15429b94acceSBarry Smith @*/
15439b94acceSBarry Smith int SNESGetSolution(SNES snes,Vec *x)
15449b94acceSBarry Smith {
15459b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
15469b94acceSBarry Smith   *x = snes->vec_sol_always;
15479b94acceSBarry Smith   return 0;
15489b94acceSBarry Smith }
15499b94acceSBarry Smith 
15509b94acceSBarry Smith /*@C
15519b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
15529b94acceSBarry Smith    stored.
15539b94acceSBarry Smith 
15549b94acceSBarry Smith    Input Parameter:
15559b94acceSBarry Smith .  snes - the SNES context
15569b94acceSBarry Smith 
15579b94acceSBarry Smith    Output Parameter:
15589b94acceSBarry Smith .  x - the solution update
15599b94acceSBarry Smith 
15609b94acceSBarry Smith    Notes:
15619b94acceSBarry Smith    This vector is implementation dependent.
15629b94acceSBarry Smith 
15639b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
15649b94acceSBarry Smith 
15659b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction
15669b94acceSBarry Smith @*/
15679b94acceSBarry Smith int SNESGetSolutionUpdate(SNES snes,Vec *x)
15689b94acceSBarry Smith {
15699b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
15709b94acceSBarry Smith   *x = snes->vec_sol_update_always;
15719b94acceSBarry Smith   return 0;
15729b94acceSBarry Smith }
15739b94acceSBarry Smith 
15749b94acceSBarry Smith /*@C
15753638b69dSLois Curfman McInnes    SNESGetFunction - Returns the vector where the function is stored.
15769b94acceSBarry Smith 
15779b94acceSBarry Smith    Input Parameter:
15789b94acceSBarry Smith .  snes - the SNES context
15799b94acceSBarry Smith 
15809b94acceSBarry Smith    Output Parameter:
15813638b69dSLois Curfman McInnes .  r - the function
15829b94acceSBarry Smith 
15839b94acceSBarry Smith    Notes:
15849b94acceSBarry Smith    SNESGetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only
15859b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
15869b94acceSBarry Smith    SNESGetMinimizationFunction() and SNESGetGradient();
15879b94acceSBarry Smith 
1588a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function
15899b94acceSBarry Smith 
159031615d04SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution(), SNESGetMinimizationFunction(),
159131615d04SLois Curfman McInnes           SNESGetGradient()
15929b94acceSBarry Smith @*/
15939b94acceSBarry Smith int SNESGetFunction(SNES snes,Vec *r)
15949b94acceSBarry Smith {
15959b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
15969b94acceSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1,
159748d91487SBarry Smith     "SNESGetFunction:For SNES_NONLINEAR_EQUATIONS only");
15989b94acceSBarry Smith   *r = snes->vec_func_always;
15999b94acceSBarry Smith   return 0;
16009b94acceSBarry Smith }
16019b94acceSBarry Smith 
16029b94acceSBarry Smith /*@C
16033638b69dSLois Curfman McInnes    SNESGetGradient - Returns the vector where the gradient is stored.
16049b94acceSBarry Smith 
16059b94acceSBarry Smith    Input Parameter:
16069b94acceSBarry Smith .  snes - the SNES context
16079b94acceSBarry Smith 
16089b94acceSBarry Smith    Output Parameter:
16099b94acceSBarry Smith .  r - the gradient
16109b94acceSBarry Smith 
16119b94acceSBarry Smith    Notes:
16129b94acceSBarry Smith    SNESGetGradient() is valid for SNES_UNCONSTRAINED_MINIMIZATION methods
16139b94acceSBarry Smith    only.  An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
16149b94acceSBarry Smith    SNESGetFunction().
16159b94acceSBarry Smith 
16169b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient
16179b94acceSBarry Smith 
161831615d04SLois Curfman McInnes .seealso: SNESGetMinimizationFunction(), SNESGetSolution(), SNESGetFunction()
16199b94acceSBarry Smith @*/
16209b94acceSBarry Smith int SNESGetGradient(SNES snes,Vec *r)
16219b94acceSBarry Smith {
16229b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
16239b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
162448d91487SBarry Smith     "SNESGetGradient:For SNES_UNCONSTRAINED_MINIMIZATION only");
16259b94acceSBarry Smith   *r = snes->vec_func_always;
16269b94acceSBarry Smith   return 0;
16279b94acceSBarry Smith }
16289b94acceSBarry Smith 
16299b94acceSBarry Smith /*@
16309b94acceSBarry Smith    SNESGetMinimizationFunction - Returns the scalar function value for
16319b94acceSBarry Smith    unconstrained minimization problems.
16329b94acceSBarry Smith 
16339b94acceSBarry Smith    Input Parameter:
16349b94acceSBarry Smith .  snes - the SNES context
16359b94acceSBarry Smith 
16369b94acceSBarry Smith    Output Parameter:
16379b94acceSBarry Smith .  r - the function
16389b94acceSBarry Smith 
16399b94acceSBarry Smith    Notes:
16409b94acceSBarry Smith    SNESGetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
16419b94acceSBarry Smith    methods only.  An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
16429b94acceSBarry Smith    SNESGetFunction().
16439b94acceSBarry Smith 
16449b94acceSBarry Smith .keywords: SNES, nonlinear, get, function
16459b94acceSBarry Smith 
164631615d04SLois Curfman McInnes .seealso: SNESGetGradient(), SNESGetSolution(), SNESGetFunction()
16479b94acceSBarry Smith @*/
16489b94acceSBarry Smith int SNESGetMinimizationFunction(SNES snes,double *r)
16499b94acceSBarry Smith {
16509b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
16519b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
165248d91487SBarry Smith     "SNESGetMinimizationFunction:For SNES_UNCONSTRAINED_MINIMIZATION only");
16539b94acceSBarry Smith   *r = snes->fc;
16549b94acceSBarry Smith   return 0;
16559b94acceSBarry Smith }
16569b94acceSBarry Smith 
16573c7409f5SSatish Balay /*@C
16583c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
16593c7409f5SSatish Balay    SNES options in the database.
16603c7409f5SSatish Balay 
16613c7409f5SSatish Balay    Input Parameter:
16623c7409f5SSatish Balay .  snes - the SNES context
16633c7409f5SSatish Balay .  prefix - the prefix to prepend to all option names
16643c7409f5SSatish Balay 
16653c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
1666a86d99e1SLois Curfman McInnes 
1667a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions()
16683c7409f5SSatish Balay @*/
16693c7409f5SSatish Balay int SNESSetOptionsPrefix(SNES snes,char *prefix)
16703c7409f5SSatish Balay {
16713c7409f5SSatish Balay   int ierr;
16723c7409f5SSatish Balay 
16733c7409f5SSatish Balay   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
16743c7409f5SSatish Balay   ierr = PetscObjectSetPrefix((PetscObject)snes, prefix); CHKERRQ(ierr);
16753c7409f5SSatish Balay   ierr = SLESSetOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr);
16763c7409f5SSatish Balay   return 0;
16773c7409f5SSatish Balay }
16783c7409f5SSatish Balay 
16793c7409f5SSatish Balay /*@C
16803c7409f5SSatish Balay    SNESAppendOptionsPrefix - Append to the prefix used for searching for all
16813c7409f5SSatish Balay    SNES options in the database.
16823c7409f5SSatish Balay 
16833c7409f5SSatish Balay    Input Parameter:
16843c7409f5SSatish Balay .  snes - the SNES context
16853c7409f5SSatish Balay .  prefix - the prefix to prepend to all option names
16863c7409f5SSatish Balay 
16873c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
1688a86d99e1SLois Curfman McInnes 
1689a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix()
16903c7409f5SSatish Balay @*/
16913c7409f5SSatish Balay int SNESAppendOptionsPrefix(SNES snes,char *prefix)
16923c7409f5SSatish Balay {
16933c7409f5SSatish Balay   int ierr;
16943c7409f5SSatish Balay 
16953c7409f5SSatish Balay   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
16963c7409f5SSatish Balay   ierr = PetscObjectAppendPrefix((PetscObject)snes, prefix); CHKERRQ(ierr);
16973c7409f5SSatish Balay   ierr = SLESAppendOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr);
16983c7409f5SSatish Balay   return 0;
16993c7409f5SSatish Balay }
17003c7409f5SSatish Balay 
1701c83590e2SSatish Balay /*@
17023c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
17033c7409f5SSatish Balay    SNES options in the database.
17043c7409f5SSatish Balay 
17053c7409f5SSatish Balay    Input Parameter:
17063c7409f5SSatish Balay .  snes - the SNES context
17073c7409f5SSatish Balay 
17083c7409f5SSatish Balay    Output Parameter:
17093c7409f5SSatish Balay .  prefix - pointer to the prefix string used
17103c7409f5SSatish Balay 
17113c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
1712a86d99e1SLois Curfman McInnes 
1713a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix()
17143c7409f5SSatish Balay @*/
17153c7409f5SSatish Balay int SNESGetOptionsPrefix(SNES snes,char **prefix)
17163c7409f5SSatish Balay {
17173c7409f5SSatish Balay   int ierr;
17183c7409f5SSatish Balay 
17193c7409f5SSatish Balay   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
17203c7409f5SSatish Balay   ierr = PetscObjectGetPrefix((PetscObject)snes, prefix); CHKERRQ(ierr);
17213c7409f5SSatish Balay   return 0;
17223c7409f5SSatish Balay }
17233c7409f5SSatish Balay 
17243c7409f5SSatish Balay 
17259b94acceSBarry Smith 
17269b94acceSBarry Smith 
17279b94acceSBarry Smith 
1728