xref: /petsc/src/snes/interface/snes.c (revision 227d817a37c50a73d88aa8c031007e5b1fb2e4ec)
19b94acceSBarry Smith #ifndef lint
2*227d817aSBarry Smith static char vcid[] = "$Id: snes.c,v 1.44 1996/01/24 04:16:11 bsmith 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)) {
50*227d817aSBarry 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 
909b94acceSBarry Smith .seealso: SNESPrintHelp()
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);
1759b94acceSBarry Smith   }
1763c7409f5SSatish Balay   ierr = OptionsHasName(snes->prefix,"-snes_mf", &flg);  CHKERRQ(ierr);
1773c7409f5SSatish Balay   if( flg && snes->method_class == SNES_NONLINEAR_EQUATIONS) {
1789b94acceSBarry Smith     Mat J;
1799b94acceSBarry Smith     ierr = SNESDefaultMatrixFreeMatCreate(snes,snes->vec_sol,&J);CHKERRQ(ierr);
1809b94acceSBarry Smith     ierr = SNESSetJacobian(snes,J,J,0,snes->funP); CHKERRQ(ierr);
1819b94acceSBarry Smith     PLogObjectParent(snes,J);
1829b94acceSBarry Smith     snes->mfshell = J;
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);
2549b94acceSBarry Smith   MPIU_printf(snes->comm,"a particular method\n");
2556daaf66cSBarry Smith   if (snes->printhelp) (*snes->printhelp)(snes,p);
2569b94acceSBarry Smith   return 0;
2579b94acceSBarry Smith }
2589b94acceSBarry Smith /*@
2599b94acceSBarry Smith    SNESSetApplicationContext - Sets the optional user-defined context for
2609b94acceSBarry Smith    the nonlinear solvers.
2619b94acceSBarry Smith 
2629b94acceSBarry Smith    Input Parameters:
2639b94acceSBarry Smith .  snes - the SNES context
2649b94acceSBarry Smith .  usrP - optional user context
2659b94acceSBarry Smith 
2669b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context
2679b94acceSBarry Smith 
2689b94acceSBarry Smith .seealso: SNESGetApplicationContext()
2699b94acceSBarry Smith @*/
2709b94acceSBarry Smith int SNESSetApplicationContext(SNES snes,void *usrP)
2719b94acceSBarry Smith {
2729b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
2739b94acceSBarry Smith   snes->user		= usrP;
2749b94acceSBarry Smith   return 0;
2759b94acceSBarry Smith }
2769b94acceSBarry Smith /*@C
2779b94acceSBarry Smith    SNESGetApplicationContext - Gets the user-defined context for the
2789b94acceSBarry Smith    nonlinear solvers.
2799b94acceSBarry Smith 
2809b94acceSBarry Smith    Input Parameter:
2819b94acceSBarry Smith .  snes - SNES context
2829b94acceSBarry Smith 
2839b94acceSBarry Smith    Output Parameter:
2849b94acceSBarry Smith .  usrP - user context
2859b94acceSBarry Smith 
2869b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context
2879b94acceSBarry Smith 
2889b94acceSBarry Smith .seealso: SNESSetApplicationContext()
2899b94acceSBarry Smith @*/
2909b94acceSBarry Smith int SNESGetApplicationContext( SNES snes,  void **usrP )
2919b94acceSBarry Smith {
2929b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
2939b94acceSBarry Smith   *usrP = snes->user;
2949b94acceSBarry Smith   return 0;
2959b94acceSBarry Smith }
2969b94acceSBarry Smith /*@
2979b94acceSBarry Smith    SNESGetIterationNumber - Gets the current iteration number of the
2989b94acceSBarry Smith    nonlinear solver.
2999b94acceSBarry Smith 
3009b94acceSBarry Smith    Input Parameter:
3019b94acceSBarry Smith .  snes - SNES context
3029b94acceSBarry Smith 
3039b94acceSBarry Smith    Output Parameter:
3049b94acceSBarry Smith .  iter - iteration number
3059b94acceSBarry Smith 
3069b94acceSBarry Smith .keywords: SNES, nonlinear, get, iteration, number
3079b94acceSBarry Smith @*/
3089b94acceSBarry Smith int SNESGetIterationNumber(SNES snes,int* iter)
3099b94acceSBarry Smith {
3109b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
3119b94acceSBarry Smith   *iter = snes->iter;
3129b94acceSBarry Smith   return 0;
3139b94acceSBarry Smith }
3149b94acceSBarry Smith /*@
3159b94acceSBarry Smith    SNESGetFunctionNorm - Gets the norm of the current function that was set
3169b94acceSBarry Smith    with SNESSSetFunction().
3179b94acceSBarry Smith 
3189b94acceSBarry Smith    Input Parameter:
3199b94acceSBarry Smith .  snes - SNES context
3209b94acceSBarry Smith 
3219b94acceSBarry Smith    Output Parameter:
3229b94acceSBarry Smith .  fnorm - 2-norm of function
3239b94acceSBarry Smith 
3249b94acceSBarry Smith    Note:
3259b94acceSBarry Smith    SNESGetFunctionNorm() is valid for SNES_NONLINEAR_EQUATIONS methods only.
3269b94acceSBarry Smith 
3279b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm
3289b94acceSBarry Smith @*/
3299b94acceSBarry Smith int SNESGetFunctionNorm(SNES snes,Scalar *fnorm)
3309b94acceSBarry Smith {
3319b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
3329b94acceSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1,
33348d91487SBarry Smith     "SNESGetFunctionNorm:For SNES_NONLINEAR_EQUATIONS only");
3349b94acceSBarry Smith   *fnorm = snes->norm;
3359b94acceSBarry Smith   return 0;
3369b94acceSBarry Smith }
3379b94acceSBarry Smith /*@
3389b94acceSBarry Smith    SNESGetGradientNorm - Gets the norm of the current gradient that was set
3399b94acceSBarry Smith    with SNESSSetGradient().
3409b94acceSBarry Smith 
3419b94acceSBarry Smith    Input Parameter:
3429b94acceSBarry Smith .  snes - SNES context
3439b94acceSBarry Smith 
3449b94acceSBarry Smith    Output Parameter:
3459b94acceSBarry Smith .  fnorm - 2-norm of gradient
3469b94acceSBarry Smith 
3479b94acceSBarry Smith    Note:
3489b94acceSBarry Smith    SNESGetGradientNorm() is valid for SNES_UNCONSTRAINED_MINIMIZATION
3499b94acceSBarry Smith    methods only.
3509b94acceSBarry Smith 
3519b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient, norm
3529b94acceSBarry Smith @*/
3539b94acceSBarry Smith int SNESGetGradientNorm(SNES snes,Scalar *gnorm)
3549b94acceSBarry Smith {
3559b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
3569b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
35748d91487SBarry Smith     "SNESGetGradientNorm:For SNES_UNCONSTRAINED_MINIMIZATION only");
3589b94acceSBarry Smith   *gnorm = snes->norm;
3599b94acceSBarry Smith   return 0;
3609b94acceSBarry Smith }
3619b94acceSBarry Smith /*@
3629b94acceSBarry Smith    SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps
3639b94acceSBarry Smith    attempted by the nonlinear solver.
3649b94acceSBarry Smith 
3659b94acceSBarry Smith    Input Parameter:
3669b94acceSBarry Smith .  snes - SNES context
3679b94acceSBarry Smith 
3689b94acceSBarry Smith    Output Parameter:
3699b94acceSBarry Smith .  nfails - number of unsuccessful steps attempted
3709b94acceSBarry Smith 
3719b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps
3729b94acceSBarry Smith @*/
3739b94acceSBarry Smith int SNESGetNumberUnsuccessfulSteps(SNES snes,int* nfails)
3749b94acceSBarry Smith {
3759b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
3769b94acceSBarry Smith   *nfails = snes->nfailures;
3779b94acceSBarry Smith   return 0;
3789b94acceSBarry Smith }
3799b94acceSBarry Smith /*@C
3809b94acceSBarry Smith    SNESGetSLES - Returns the SLES context for a SNES solver.
3819b94acceSBarry Smith 
3829b94acceSBarry Smith    Input Parameter:
3839b94acceSBarry Smith .  snes - the SNES context
3849b94acceSBarry Smith 
3859b94acceSBarry Smith    Output Parameter:
3869b94acceSBarry Smith .  sles - the SLES context
3879b94acceSBarry Smith 
3889b94acceSBarry Smith    Notes:
3899b94acceSBarry Smith    The user can then directly manipulate the SLES context to set various
3909b94acceSBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
3919b94acceSBarry Smith    KSP and PC contexts as well.
3929b94acceSBarry Smith 
3939b94acceSBarry Smith .keywords: SNES, nonlinear, get, SLES, context
3949b94acceSBarry Smith 
3959b94acceSBarry Smith .seealso: SLESGetPC(), SLESGetKSP()
3969b94acceSBarry Smith @*/
3979b94acceSBarry Smith int SNESGetSLES(SNES snes,SLES *sles)
3989b94acceSBarry Smith {
3999b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
4009b94acceSBarry Smith   *sles = snes->sles;
4019b94acceSBarry Smith   return 0;
4029b94acceSBarry Smith }
4039b94acceSBarry Smith /* -----------------------------------------------------------*/
4049b94acceSBarry Smith /*@C
4059b94acceSBarry Smith    SNESCreate - Creates a nonlinear solver context.
4069b94acceSBarry Smith 
4079b94acceSBarry Smith    Input Parameter:
4089b94acceSBarry Smith .  comm - MPI communicator
4099b94acceSBarry Smith .  type - type of method, one of
4109b94acceSBarry Smith $    SNES_NONLINEAR_EQUATIONS
4119b94acceSBarry Smith $      (for systems of nonlinear equations)
4129b94acceSBarry Smith $    SNES_UNCONSTRAINED_MINIMIZATION
4139b94acceSBarry Smith $      (for unconstrained minimization)
4149b94acceSBarry Smith 
4159b94acceSBarry Smith    Output Parameter:
4169b94acceSBarry Smith .  outsnes - the new SNES context
4179b94acceSBarry Smith 
4189b94acceSBarry Smith .keywords: SNES, nonlinear, create, context
4199b94acceSBarry Smith 
4209b94acceSBarry Smith .seealso: SNESSetUp(), SNESSolve(), SNESDestroy()
4219b94acceSBarry Smith @*/
4224b0e389bSBarry Smith int SNESCreate(MPI_Comm comm,SNESProblemType type,SNES *outsnes)
4239b94acceSBarry Smith {
4249b94acceSBarry Smith   int                 ierr;
4259b94acceSBarry Smith   SNES                snes;
4269b94acceSBarry Smith   SNES_KSP_EW_ConvCtx *kctx;
42737fcc0dbSBarry Smith 
4289b94acceSBarry Smith   *outsnes = 0;
4292a0acf01SLois Curfman McInnes   if (type != SNES_UNCONSTRAINED_MINIMIZATION && type != SNES_NONLINEAR_EQUATIONS)
4302a0acf01SLois Curfman McInnes     SETERRQ(1,"SNESCreate:incorrect method type");
4310452661fSBarry Smith   PetscHeaderCreate(snes,_SNES,SNES_COOKIE,SNES_UNKNOWN_METHOD,comm);
4329b94acceSBarry Smith   PLogObjectCreate(snes);
4339b94acceSBarry Smith   snes->max_its           = 50;
4349b94acceSBarry Smith   snes->max_funcs	  = 1000;
4359b94acceSBarry Smith   snes->norm		  = 0.0;
4365a2d0531SBarry Smith   if (type == SNES_UNCONSTRAINED_MINIMIZATION) {
4372a0acf01SLois Curfman McInnes     snes->rtol		  = 1.e-08;
4389b94acceSBarry Smith     snes->atol		  = 1.e-10;
4395a2d0531SBarry Smith   }
440b4874afaSBarry Smith   else {
441b4874afaSBarry Smith     snes->rtol		  = 1.e-8;
442b4874afaSBarry Smith     snes->ttol            = 0.0;
443b4874afaSBarry Smith     snes->atol		  = 1.e-50;
444b4874afaSBarry Smith   }
4459b94acceSBarry Smith   snes->xtol		  = 1.e-8;
4469b94acceSBarry Smith   snes->trunctol	  = 1.e-12;
4479b94acceSBarry Smith   snes->nfuncs            = 0;
4489b94acceSBarry Smith   snes->nfailures         = 0;
4499b94acceSBarry Smith   snes->monitor           = 0;
4509b94acceSBarry Smith   snes->data              = 0;
4519b94acceSBarry Smith   snes->view              = 0;
4529b94acceSBarry Smith   snes->computeumfunction = 0;
4539b94acceSBarry Smith   snes->umfunP            = 0;
4549b94acceSBarry Smith   snes->fc                = 0;
4559b94acceSBarry Smith   snes->deltatol          = 1.e-12;
4569b94acceSBarry Smith   snes->fmin              = -1.e30;
4579b94acceSBarry Smith   snes->method_class      = type;
4589b94acceSBarry Smith   snes->set_method_called = 0;
459a703fe33SLois Curfman McInnes   snes->setup_called      = 0;
4609b94acceSBarry Smith   snes->ksp_ewconv        = 0;
4619b94acceSBarry Smith 
4629b94acceSBarry Smith   /* Create context to compute Eisenstat-Walker relative tolerance for KSP */
4630452661fSBarry Smith   kctx = PetscNew(SNES_KSP_EW_ConvCtx); CHKPTRQ(kctx);
4649b94acceSBarry Smith   snes->kspconvctx  = (void*)kctx;
4659b94acceSBarry Smith   kctx->version     = 2;
4669b94acceSBarry Smith   kctx->rtol_0      = .3; /* Eisenstat and Walker suggest rtol_0=.5, but
4679b94acceSBarry Smith                              this was too large for some test cases */
4689b94acceSBarry Smith   kctx->rtol_last   = 0;
4699b94acceSBarry Smith   kctx->rtol_max    = .9;
4709b94acceSBarry Smith   kctx->gamma       = 1.0;
4719b94acceSBarry Smith   kctx->alpha2      = .5*(1.0 + sqrt(5.0));
4729b94acceSBarry Smith   kctx->alpha       = kctx->alpha2;
4739b94acceSBarry Smith   kctx->threshold   = .1;
4749b94acceSBarry Smith   kctx->lresid_last = 0;
4759b94acceSBarry Smith   kctx->norm_last   = 0;
4769b94acceSBarry Smith 
4779b94acceSBarry Smith   ierr = SLESCreate(comm,&snes->sles); CHKERRQ(ierr);
4789b94acceSBarry Smith   PLogObjectParent(snes,snes->sles)
4795334005bSBarry Smith 
4809b94acceSBarry Smith   *outsnes = snes;
4819b94acceSBarry Smith   return 0;
4829b94acceSBarry Smith }
4839b94acceSBarry Smith 
4849b94acceSBarry Smith /* --------------------------------------------------------------- */
4859b94acceSBarry Smith /*@C
4869b94acceSBarry Smith    SNESSetFunction - Sets the function evaluation routine and function
4879b94acceSBarry Smith    vector for use by the SNES routines in solving systems of nonlinear
4889b94acceSBarry Smith    equations.
4899b94acceSBarry Smith 
4909b94acceSBarry Smith    Input Parameters:
4919b94acceSBarry Smith .  snes - the SNES context
4929b94acceSBarry Smith .  func - function evaluation routine
4939b94acceSBarry Smith .  ctx - optional user-defined function context
4949b94acceSBarry Smith .  r - vector to store function value
4959b94acceSBarry Smith 
4969b94acceSBarry Smith    Calling sequence of func:
4979b94acceSBarry Smith .  func (SNES, Vec x, Vec f, void *ctx);
4989b94acceSBarry Smith 
4999b94acceSBarry Smith .  x - input vector
5005334005bSBarry Smith .  f - vector function
5019b94acceSBarry Smith .  ctx - optional user-defined context for private data for the
5029b94acceSBarry Smith          function evaluation routine (may be null)
5039b94acceSBarry Smith 
5049b94acceSBarry Smith    Notes:
5059b94acceSBarry Smith    The Newton-like methods typically solve linear systems of the form
5069b94acceSBarry Smith $      f'(x) x = -f(x),
5079b94acceSBarry Smith $  where f'(x) denotes the Jacobian matrix and f(x) is the function.
5089b94acceSBarry Smith 
5099b94acceSBarry Smith    SNESSetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only.
5109b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
5119b94acceSBarry Smith    SNESSetMinimizationFunction() and SNESSetGradient();
5129b94acceSBarry Smith 
5139b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
5149b94acceSBarry Smith 
5159b94acceSBarry Smith .seealso: SNESGetFunction(), SNESSetJacobian(), SNESSetSolution()
5169b94acceSBarry Smith @*/
5175334005bSBarry Smith int SNESSetFunction( SNES snes, Vec r, int (*func)(SNES,Vec,Vec,void*),void *ctx)
5189b94acceSBarry Smith {
5199b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
5209b94acceSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1,
52148d91487SBarry Smith     "SNESSetFunction:For SNES_NONLINEAR_EQUATIONS only");
5229b94acceSBarry Smith   snes->computefunction     = func;
5239b94acceSBarry Smith   snes->vec_func            = snes->vec_func_always = r;
5249b94acceSBarry Smith   snes->funP                = ctx;
5259b94acceSBarry Smith   return 0;
5269b94acceSBarry Smith }
5279b94acceSBarry Smith 
5289b94acceSBarry Smith /*@
5299b94acceSBarry Smith    SNESComputeFunction - Computes the function that has been set with
5309b94acceSBarry Smith    SNESSetFunction().
5319b94acceSBarry Smith 
5329b94acceSBarry Smith    Input Parameters:
5339b94acceSBarry Smith .  snes - the SNES context
5349b94acceSBarry Smith .  x - input vector
5359b94acceSBarry Smith 
5369b94acceSBarry Smith    Output Parameter:
5379b94acceSBarry Smith .  y - function vector or its negative, as set by SNESSetFunction()
5389b94acceSBarry Smith 
5399b94acceSBarry Smith    Notes:
5409b94acceSBarry Smith    SNESComputeFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only.
5419b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
5429b94acceSBarry Smith    SNESComputeMinimizationFunction() and SNESComputeGradient();
5439b94acceSBarry Smith 
5449b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function
5459b94acceSBarry Smith 
5469b94acceSBarry Smith .seealso: SNESSetFunction()
5479b94acceSBarry Smith @*/
5489b94acceSBarry Smith int SNESComputeFunction(SNES snes,Vec x, Vec y)
5499b94acceSBarry Smith {
5509b94acceSBarry Smith   int    ierr;
5519b94acceSBarry Smith 
5529b94acceSBarry Smith   PLogEventBegin(SNES_FunctionEval,snes,x,y,0);
5539b94acceSBarry Smith   ierr = (*snes->computefunction)(snes,x,y,snes->funP); CHKERRQ(ierr);
5549b94acceSBarry Smith   PLogEventEnd(SNES_FunctionEval,snes,x,y,0);
5559b94acceSBarry Smith   return 0;
5569b94acceSBarry Smith }
5579b94acceSBarry Smith 
5589b94acceSBarry Smith /*@C
5599b94acceSBarry Smith    SNESSetMinimizationFunction - Sets the function evaluation routine for
5609b94acceSBarry Smith    unconstrained minimization.
5619b94acceSBarry Smith 
5629b94acceSBarry Smith    Input Parameters:
5639b94acceSBarry Smith .  snes - the SNES context
5649b94acceSBarry Smith .  func - function evaluation routine
5659b94acceSBarry Smith .  ctx - optional user-defined function context
5669b94acceSBarry Smith 
5679b94acceSBarry Smith    Calling sequence of func:
5689b94acceSBarry Smith .  func (SNES snes,Vec x,double *f,void *ctx);
5699b94acceSBarry Smith 
5709b94acceSBarry Smith .  x - input vector
5719b94acceSBarry Smith .  f - function
5729b94acceSBarry Smith .  ctx - optional user-defined context for private data for the
5739b94acceSBarry Smith          function evaluation routine (may be null)
5749b94acceSBarry Smith 
5759b94acceSBarry Smith    Notes:
5769b94acceSBarry Smith    SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
5779b94acceSBarry Smith    methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
5789b94acceSBarry Smith    SNESSetFunction().
5799b94acceSBarry Smith 
5809b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimization, function
5819b94acceSBarry Smith 
5829b94acceSBarry Smith .seealso:  SNESGetMinimizationFunction(), SNESSetHessian(), SNESSetGradient(),
5839b94acceSBarry Smith            SNESSetSolution()
5849b94acceSBarry Smith @*/
5859b94acceSBarry Smith int SNESSetMinimizationFunction(SNES snes,int (*func)(SNES,Vec,double*,void*),
5869b94acceSBarry Smith                       void *ctx)
5879b94acceSBarry Smith {
5889b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
5899b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
59048d91487SBarry Smith     "SNESSetMinimizationFunction:Only for SNES_UNCONSTRAINED_MINIMIZATION");
5919b94acceSBarry Smith   snes->computeumfunction   = func;
5929b94acceSBarry Smith   snes->umfunP              = ctx;
5939b94acceSBarry Smith   return 0;
5949b94acceSBarry Smith }
5959b94acceSBarry Smith 
5969b94acceSBarry Smith /*@
5979b94acceSBarry Smith    SNESComputeMinimizationFunction - Computes the function that has been
5989b94acceSBarry Smith    set with SNESSetMinimizationFunction().
5999b94acceSBarry Smith 
6009b94acceSBarry Smith    Input Parameters:
6019b94acceSBarry Smith .  snes - the SNES context
6029b94acceSBarry Smith .  x - input vector
6039b94acceSBarry Smith 
6049b94acceSBarry Smith    Output Parameter:
6059b94acceSBarry Smith .  y - function value
6069b94acceSBarry Smith 
6079b94acceSBarry Smith    Notes:
6089b94acceSBarry Smith    SNESComputeMinimizationFunction() is valid only for
6099b94acceSBarry Smith    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
6109b94acceSBarry Smith    SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction().
6119b94acceSBarry Smith @*/
6129b94acceSBarry Smith int SNESComputeMinimizationFunction(SNES snes,Vec x,double *y)
6139b94acceSBarry Smith {
6149b94acceSBarry Smith   int    ierr;
6159b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
61648d91487SBarry Smith     "SNESComputeMinimizationFunction:Only for SNES_UNCONSTRAINED_MINIMIZATION");
6179b94acceSBarry Smith   PLogEventBegin(SNES_MinimizationFunctionEval,snes,x,y,0);
6189b94acceSBarry Smith   ierr = (*snes->computeumfunction)(snes,x,y,snes->umfunP); CHKERRQ(ierr);
6199b94acceSBarry Smith   PLogEventEnd(SNES_MinimizationFunctionEval,snes,x,y,0);
6209b94acceSBarry Smith   return 0;
6219b94acceSBarry Smith }
6229b94acceSBarry Smith 
6239b94acceSBarry Smith /*@C
6249b94acceSBarry Smith    SNESSetGradient - Sets the gradient evaluation routine and gradient
6259b94acceSBarry Smith    vector for use by the SNES routines.
6269b94acceSBarry Smith 
6279b94acceSBarry Smith    Input Parameters:
6289b94acceSBarry Smith .  snes - the SNES context
6299b94acceSBarry Smith .  func - function evaluation routine
6309b94acceSBarry Smith .  ctx - optional user-defined function context
6319b94acceSBarry Smith .  r - vector to store gradient value
6329b94acceSBarry Smith 
6339b94acceSBarry Smith    Calling sequence of func:
6349b94acceSBarry Smith .  func (SNES, Vec x, Vec g, void *ctx);
6359b94acceSBarry Smith 
6369b94acceSBarry Smith .  x - input vector
6379b94acceSBarry Smith .  g - gradient vector
6389b94acceSBarry Smith .  ctx - optional user-defined context for private data for the
6399b94acceSBarry Smith          function evaluation routine (may be null)
6409b94acceSBarry Smith 
6419b94acceSBarry Smith    Notes:
6429b94acceSBarry Smith    SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
6439b94acceSBarry Smith    methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
6449b94acceSBarry Smith    SNESSetFunction().
6459b94acceSBarry Smith 
6469b94acceSBarry Smith .keywords: SNES, nonlinear, set, function
6479b94acceSBarry Smith 
6489b94acceSBarry Smith .seealso: SNESGetGradient(), SNESSetHessian(), SNESSetMinimizationFunction(),
6499b94acceSBarry Smith           SNESSetSolution()
6509b94acceSBarry Smith @*/
6519b94acceSBarry Smith int SNESSetGradient(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),
6529b94acceSBarry Smith                      void *ctx)
6539b94acceSBarry Smith {
6549b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
6559b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
65648d91487SBarry Smith     "SNESSetGradient:For SNES_UNCONSTRAINED_MINIMIZATION only");
6579b94acceSBarry Smith   snes->computefunction     = func;
6589b94acceSBarry Smith   snes->vec_func            = snes->vec_func_always = r;
6599b94acceSBarry Smith   snes->funP                = ctx;
6609b94acceSBarry Smith   return 0;
6619b94acceSBarry Smith }
6629b94acceSBarry Smith 
6639b94acceSBarry Smith /*@
6649b94acceSBarry Smith    SNESComputeGradient - Computes the gradient that has been
6659b94acceSBarry Smith    set with SNESSetGradient().
6669b94acceSBarry Smith 
6679b94acceSBarry Smith    Input Parameters:
6689b94acceSBarry Smith .  snes - the SNES context
6699b94acceSBarry Smith .  x - input vector
6709b94acceSBarry Smith 
6719b94acceSBarry Smith    Output Parameter:
6729b94acceSBarry Smith .  y - gradient vector
6739b94acceSBarry Smith 
6749b94acceSBarry Smith    Notes:
6759b94acceSBarry Smith    SNESComputeGradient() is valid only for
6769b94acceSBarry Smith    SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for
6779b94acceSBarry Smith    SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction().
6789b94acceSBarry Smith @*/
6799b94acceSBarry Smith int SNESComputeGradient(SNES snes,Vec x, Vec y)
6809b94acceSBarry Smith {
6819b94acceSBarry Smith   int    ierr;
6829b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
68348d91487SBarry Smith     "SNESComputeGradient:For SNES_UNCONSTRAINED_MINIMIZATION only");
6849b94acceSBarry Smith   PLogEventBegin(SNES_GradientEval,snes,x,y,0);
6859b94acceSBarry Smith   ierr = (*snes->computefunction)(snes,x,y,snes->funP); CHKERRQ(ierr);
6869b94acceSBarry Smith   PLogEventEnd(SNES_GradientEval,snes,x,y,0);
6879b94acceSBarry Smith   return 0;
6889b94acceSBarry Smith }
6899b94acceSBarry Smith 
6909b94acceSBarry Smith int SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
6919b94acceSBarry Smith {
6929b94acceSBarry Smith   int    ierr;
6939b94acceSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1,
69448d91487SBarry Smith     "SNESComputeJacobian: For SNES_NONLINEAR_EQUATIONS only");
6959b94acceSBarry Smith   if (!snes->computejacobian) return 0;
6969b94acceSBarry Smith   PLogEventBegin(SNES_JacobianEval,snes,X,*A,*B);
697b4fd4287SBarry Smith   *flg = ALLMAT_DIFFERENT_NONZERO_PATTERN;
6989b94acceSBarry Smith   ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP); CHKERRQ(ierr);
6999b94acceSBarry Smith   PLogEventEnd(SNES_JacobianEval,snes,X,*A,*B);
7009b94acceSBarry Smith   return 0;
7019b94acceSBarry Smith }
7029b94acceSBarry Smith 
7039b94acceSBarry Smith int SNESComputeHessian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg)
7049b94acceSBarry Smith {
7059b94acceSBarry Smith   int    ierr;
7069b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
70748d91487SBarry Smith     "SNESComputeHessian:For SNES_UNCONSTRAINED_MINIMIZATION only");
7089b94acceSBarry Smith   if (!snes->computejacobian) return 0;
7099b94acceSBarry Smith   PLogEventBegin(SNES_HessianEval,snes,X,*A,*B);
7109b94acceSBarry Smith   ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP); CHKERRQ(ierr);
7119b94acceSBarry Smith   PLogEventEnd(SNES_HessianEval,snes,X,*A,*B);
7129b94acceSBarry Smith   return 0;
7139b94acceSBarry Smith }
7149b94acceSBarry Smith 
7159b94acceSBarry Smith /*@C
7169b94acceSBarry Smith    SNESSetJacobian - Sets the function to compute Jacobian as well as the
7179b94acceSBarry Smith    location to store it.
7189b94acceSBarry Smith 
7199b94acceSBarry Smith    Input Parameters:
7209b94acceSBarry Smith .  snes - the SNES context
7219b94acceSBarry Smith .  A - Jacobian matrix
7229b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Jacobian)
7239b94acceSBarry Smith .  func - Jacobian evaluation routine
7249b94acceSBarry Smith .  ctx - optional user-defined context for private data for the
7259b94acceSBarry Smith          Jacobian evaluation routine (may be null)
7269b94acceSBarry Smith 
7279b94acceSBarry Smith    Calling sequence of func:
7289b94acceSBarry Smith .  func (SNES,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
7299b94acceSBarry Smith 
7309b94acceSBarry Smith .  x - input vector
7319b94acceSBarry Smith .  A - Jacobian matrix
7329b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
7339b94acceSBarry Smith .  flag - flag indicating information about matrix structure,
7349b94acceSBarry Smith    same as flag in SLESSetOperators()
7359b94acceSBarry Smith .  ctx - optional user-defined Jacobian context
7369b94acceSBarry Smith 
7379b94acceSBarry Smith    Notes:
7389b94acceSBarry Smith    The function func() takes Mat * as the matrix arguments rather than Mat.
7399b94acceSBarry Smith    This allows the Jacobian evaluation routine to replace A and/or B with a
7409b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
7419b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
7429b94acceSBarry Smith    throughout the global iterations.
7439b94acceSBarry Smith 
7449b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix
7459b94acceSBarry Smith 
7469b94acceSBarry Smith .seealso: SNESSetFunction(), SNESSetSolution()
7479b94acceSBarry Smith @*/
7489b94acceSBarry Smith int SNESSetJacobian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,
7499b94acceSBarry Smith                     MatStructure*,void*),void *ctx)
7509b94acceSBarry Smith {
7519b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
7529b94acceSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1,
75348d91487SBarry Smith     "SNESSetJacobian:For SNES_NONLINEAR_EQUATIONS only");
7549b94acceSBarry Smith   snes->computejacobian = func;
7559b94acceSBarry Smith   snes->jacP            = ctx;
7569b94acceSBarry Smith   snes->jacobian        = A;
7579b94acceSBarry Smith   snes->jacobian_pre    = B;
7589b94acceSBarry Smith   return 0;
7599b94acceSBarry Smith }
760b4fd4287SBarry Smith /*@
761b4fd4287SBarry Smith      SNESGetJacobian - Returns the Jacobian matrix and optionally the user
762b4fd4287SBarry Smith           provided context for evaluating the Jacobian.
763b4fd4287SBarry Smith 
764b4fd4287SBarry Smith   Input Parameter:
765b4fd4287SBarry Smith .  snes - the nonlinear solver context
766b4fd4287SBarry Smith 
767b4fd4287SBarry Smith   Output Parameters:
768b4fd4287SBarry Smith .  A - location to stash Jacobian matrix (or PETSC_NULL)
769b4fd4287SBarry Smith .  B - location to stash preconditioner matrix (or PETSC_NULL)
770b4fd4287SBarry Smith .  ctx - location to stash Jacobian ctx (or PETSC_NULL)
771b4fd4287SBarry Smith 
772b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian()
773b4fd4287SBarry Smith @*/
774b4fd4287SBarry Smith int SNESGetJacobian(SNES snes,Mat *A,Mat *B, void **ctx)
775b4fd4287SBarry Smith {
776b4fd4287SBarry Smith   if (A)   *A = snes->jacobian;
777b4fd4287SBarry Smith   if (B)   *B = snes->jacobian_pre;
778b4fd4287SBarry Smith   if (ctx) *ctx = snes->jacP;
779b4fd4287SBarry Smith   return 0;
780b4fd4287SBarry Smith }
781b4fd4287SBarry Smith 
7829b94acceSBarry Smith /*@C
7839b94acceSBarry Smith    SNESSetHessian - Sets the function to compute Hessian as well as the
7849b94acceSBarry Smith    location to store it.
7859b94acceSBarry Smith 
7869b94acceSBarry Smith    Input Parameters:
7879b94acceSBarry Smith .  snes - the SNES context
7889b94acceSBarry Smith .  A - Hessian matrix
7899b94acceSBarry Smith .  B - preconditioner matrix (usually same as the Hessian)
7909b94acceSBarry Smith .  func - Jacobian evaluation routine
7919b94acceSBarry Smith .  ctx - optional user-defined context for private data for the
7929b94acceSBarry Smith          Hessian evaluation routine (may be null)
7939b94acceSBarry Smith 
7949b94acceSBarry Smith    Calling sequence of func:
7959b94acceSBarry Smith .  func (SNES,Vec x,Mat *A,Mat *B,int *flag,void *ctx);
7969b94acceSBarry Smith 
7979b94acceSBarry Smith .  x - input vector
7989b94acceSBarry Smith .  A - Hessian matrix
7999b94acceSBarry Smith .  B - preconditioner matrix, usually the same as A
8009b94acceSBarry Smith .  flag - flag indicating information about matrix structure,
8019b94acceSBarry Smith    same as flag in SLESSetOperators()
8029b94acceSBarry Smith .  ctx - optional user-defined Hessian context
8039b94acceSBarry Smith 
8049b94acceSBarry Smith    Notes:
8059b94acceSBarry Smith    The function func() takes Mat * as the matrix arguments rather than Mat.
8069b94acceSBarry Smith    This allows the Hessian evaluation routine to replace A and/or B with a
8079b94acceSBarry Smith    completely new new matrix structure (not just different matrix elements)
8089b94acceSBarry Smith    when appropriate, for instance, if the nonzero structure is changing
8099b94acceSBarry Smith    throughout the global iterations.
8109b94acceSBarry Smith 
8119b94acceSBarry Smith .keywords: SNES, nonlinear, set, Hessian, matrix
8129b94acceSBarry Smith 
8139b94acceSBarry Smith .seealso: SNESSetMinimizationFunction(), SNESSetSolution(), SNESSetGradient()
8149b94acceSBarry Smith @*/
8159b94acceSBarry Smith int SNESSetHessian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*,
8169b94acceSBarry Smith                     MatStructure*,void*),void *ctx)
8179b94acceSBarry Smith {
8189b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
8199b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
82048d91487SBarry Smith     "SNESSetHessian:For SNES_UNCONSTRAINED_MINIMIZATION only");
8219b94acceSBarry Smith   snes->computejacobian = func;
8229b94acceSBarry Smith   snes->jacP            = ctx;
8239b94acceSBarry Smith   snes->jacobian        = A;
8249b94acceSBarry Smith   snes->jacobian_pre    = B;
8259b94acceSBarry Smith   return 0;
8269b94acceSBarry Smith }
8279b94acceSBarry Smith 
8289b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */
8299b94acceSBarry Smith 
8309b94acceSBarry Smith /*@
8319b94acceSBarry Smith    SNESSetUp - Sets up the internal data structures for the later use
8329b94acceSBarry Smith    of a nonlinear solver.  Call SNESSetUp() after calling SNESCreate()
8339b94acceSBarry Smith    and optional routines of the form SNESSetXXX(), but before calling
8349b94acceSBarry Smith    SNESSolve().
8359b94acceSBarry Smith 
8369b94acceSBarry Smith    Input Parameter:
8379b94acceSBarry Smith .  snes - the SNES context
8389b94acceSBarry Smith 
8399b94acceSBarry Smith .keywords: SNES, nonlinear, setup
8409b94acceSBarry Smith 
8419b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy()
8429b94acceSBarry Smith @*/
8439b94acceSBarry Smith int SNESSetUp(SNES snes)
8449b94acceSBarry Smith {
8459b94acceSBarry Smith   int ierr;
8469b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
84737fcc0dbSBarry Smith   if (!snes->vec_sol) SETERRQ(1,"SNESSetUp:Must call SNESSetSolution() first");
8489b94acceSBarry Smith 
8499b94acceSBarry Smith   if ((snes->method_class == SNES_NONLINEAR_EQUATIONS)) {
85037fcc0dbSBarry Smith     if (!snes->vec_func) SETERRQ(1,"SNESSetUp:Must call SNESSetFunction() first");
85137fcc0dbSBarry Smith     if (!snes->computefunction) SETERRQ(1,"SNESSetUp:Must call SNESSetFunction() first");
85237fcc0dbSBarry Smith     if (!snes->jacobian) SETERRQ(1,"SNESSetUp:Must call SNESSetJacobian() first");
853a703fe33SLois Curfman McInnes 
854a703fe33SLois Curfman McInnes     /* Set the KSP stopping criterion to use the Eisenstat-Walker method */
855a703fe33SLois Curfman McInnes     if (snes->ksp_ewconv && snes->type != SNES_EQ_NTR) {
856a703fe33SLois Curfman McInnes       SLES sles; KSP ksp;
857a703fe33SLois Curfman McInnes       ierr = SNESGetSLES(snes,&sles); CHKERRQ(ierr);
858a703fe33SLois Curfman McInnes       ierr = SLESGetKSP(sles,&ksp); CHKERRQ(ierr);
859a703fe33SLois Curfman McInnes       ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private,
860a703fe33SLois Curfman McInnes              (void *)snes); CHKERRQ(ierr);
861a703fe33SLois Curfman McInnes     }
8629b94acceSBarry Smith   } else if ((snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION)) {
86337fcc0dbSBarry Smith     if (!snes->vec_func) SETERRQ(1,"SNESSetUp:Must call SNESSetGradient() first");
86437fcc0dbSBarry Smith     if (!snes->computefunction) SETERRQ(1,"SNESSetUp:Must call SNESSetGradient() first");
86537fcc0dbSBarry Smith     if (!snes->computeumfunction)
86637fcc0dbSBarry Smith       SETERRQ(1,"SNESSetUp:Must call SNESSetMinimizationFunction() first");
86737fcc0dbSBarry Smith     if (!snes->jacobian) SETERRQ(1,"SNESSetUp:Must call SNESSetHessian() first");
8689b94acceSBarry Smith   } else SETERRQ(1,"SNESSetUp:Unknown method class");
869a703fe33SLois Curfman McInnes   if (snes->setup) {ierr = (*snes->setup)(snes); CHKERRQ(ierr);}
870a703fe33SLois Curfman McInnes   snes->setup_called = 1;
871a703fe33SLois Curfman McInnes   return 0;
8729b94acceSBarry Smith }
8739b94acceSBarry Smith 
8749b94acceSBarry Smith /*@C
8759b94acceSBarry Smith    SNESDestroy - Destroys the nonlinear solver context that was created
8769b94acceSBarry Smith    with SNESCreate().
8779b94acceSBarry Smith 
8789b94acceSBarry Smith    Input Parameter:
8799b94acceSBarry Smith .  snes - the SNES context
8809b94acceSBarry Smith 
8819b94acceSBarry Smith .keywords: SNES, nonlinear, destroy
8829b94acceSBarry Smith 
8839b94acceSBarry Smith .seealso: SNESCreate(), SNESSetUp(), SNESSolve()
8849b94acceSBarry Smith @*/
8859b94acceSBarry Smith int SNESDestroy(SNES snes)
8869b94acceSBarry Smith {
8879b94acceSBarry Smith   int ierr;
8889b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
8899b94acceSBarry Smith   ierr = (*(snes)->destroy)((PetscObject)snes); CHKERRQ(ierr);
8900452661fSBarry Smith   if (snes->kspconvctx) PetscFree(snes->kspconvctx);
8919b94acceSBarry Smith   if (snes->mfshell) MatDestroy(snes->mfshell);
8929b94acceSBarry Smith   ierr = SLESDestroy(snes->sles); CHKERRQ(ierr);
8939b94acceSBarry Smith   PLogObjectDestroy((PetscObject)snes);
8940452661fSBarry Smith   PetscHeaderDestroy((PetscObject)snes);
8959b94acceSBarry Smith   return 0;
8969b94acceSBarry Smith }
8979b94acceSBarry Smith 
8989b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */
8999b94acceSBarry Smith 
9009b94acceSBarry Smith /*@
9019b94acceSBarry Smith    SNESSetMaxIterations - Sets the maximum number of global iterations to use.
9029b94acceSBarry Smith 
9039b94acceSBarry Smith    Input Parameters:
9049b94acceSBarry Smith .  snes - the SNES context
9059b94acceSBarry Smith .  maxits - maximum number of iterations to use
9069b94acceSBarry Smith 
9079b94acceSBarry Smith    Options Database Key:
9089b94acceSBarry Smith $  -snes_max_it  maxits
9099b94acceSBarry Smith 
9109b94acceSBarry Smith    Note:
9119b94acceSBarry Smith    The default maximum number of iterations is 50.
9129b94acceSBarry Smith 
9139b94acceSBarry Smith .keywords: SNES, nonlinear, set, maximum, iterations
9149b94acceSBarry Smith 
9159b94acceSBarry Smith .seealso: SNESSetMaxFunctionEvaluations()
9169b94acceSBarry Smith @*/
9179b94acceSBarry Smith int SNESSetMaxIterations(SNES snes,int maxits)
9189b94acceSBarry Smith {
9199b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
9209b94acceSBarry Smith   snes->max_its = maxits;
9219b94acceSBarry Smith   return 0;
9229b94acceSBarry Smith }
9239b94acceSBarry Smith 
9249b94acceSBarry Smith /*@
9259b94acceSBarry Smith    SNESSetMaxFunctionEvaluations - Sets the maximum number of function
9269b94acceSBarry Smith    evaluations to use.
9279b94acceSBarry Smith 
9289b94acceSBarry Smith    Input Parameters:
9299b94acceSBarry Smith .  snes - the SNES context
9309b94acceSBarry Smith .  maxf - maximum number of function evaluations
9319b94acceSBarry Smith 
9329b94acceSBarry Smith    Options Database Key:
9339b94acceSBarry Smith $  -snes_max_funcs maxf
9349b94acceSBarry Smith 
9359b94acceSBarry Smith    Note:
9369b94acceSBarry Smith    The default maximum number of function evaluations is 1000.
9379b94acceSBarry Smith 
9389b94acceSBarry Smith .keywords: SNES, nonlinear, set, maximum, function, evaluations
9399b94acceSBarry Smith 
9409b94acceSBarry Smith .seealso: SNESSetMaxIterations()
9419b94acceSBarry Smith @*/
9429b94acceSBarry Smith int SNESSetMaxFunctionEvaluations(SNES snes,int maxf)
9439b94acceSBarry Smith {
9449b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
9459b94acceSBarry Smith   snes->max_funcs = maxf;
9469b94acceSBarry Smith   return 0;
9479b94acceSBarry Smith }
9489b94acceSBarry Smith 
9499b94acceSBarry Smith /*@
9509b94acceSBarry Smith    SNESSetRelativeTolerance - Sets the relative convergence tolerance.
9519b94acceSBarry Smith 
9529b94acceSBarry Smith    Input Parameters:
9539b94acceSBarry Smith .  snes - the SNES context
9549b94acceSBarry Smith .  rtol - tolerance
9559b94acceSBarry Smith 
9569b94acceSBarry Smith    Options Database Key:
9579b94acceSBarry Smith $    -snes_rtol tol
9589b94acceSBarry Smith 
9599b94acceSBarry Smith .keywords: SNES, nonlinear, set, relative, convergence, tolerance
9609b94acceSBarry Smith 
9619b94acceSBarry Smith .seealso: SNESSetAbsoluteTolerance(), SNESSetSolutionTolerance(),
9629b94acceSBarry Smith            SNESSetTruncationTolerance()
9639b94acceSBarry Smith @*/
9649b94acceSBarry Smith int SNESSetRelativeTolerance(SNES snes,double rtol)
9659b94acceSBarry Smith {
9669b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
9679b94acceSBarry Smith   snes->rtol = rtol;
9689b94acceSBarry Smith   return 0;
9699b94acceSBarry Smith }
9709b94acceSBarry Smith 
9719b94acceSBarry Smith /*@
9729b94acceSBarry Smith    SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance.
9739b94acceSBarry Smith 
9749b94acceSBarry Smith    Input Parameters:
9759b94acceSBarry Smith .  snes - the SNES context
9769b94acceSBarry Smith .  tol - tolerance
9779b94acceSBarry Smith 
9789b94acceSBarry Smith    Options Database Key:
9799b94acceSBarry Smith $    -snes_trtol tol
9809b94acceSBarry Smith 
9819b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance
9829b94acceSBarry Smith 
9839b94acceSBarry Smith .seealso: SNESSetAbsoluteTolerance(), SNESSetSolutionTolerance(),
9849b94acceSBarry Smith            SNESSetTruncationTolerance()
9859b94acceSBarry Smith @*/
9869b94acceSBarry Smith int SNESSetTrustRegionTolerance(SNES snes,double tol)
9879b94acceSBarry Smith {
9889b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
9899b94acceSBarry Smith   snes->deltatol = tol;
9909b94acceSBarry Smith   return 0;
9919b94acceSBarry Smith }
9929b94acceSBarry Smith 
9939b94acceSBarry Smith /*@
9949b94acceSBarry Smith    SNESSetAbsoluteTolerance - Sets the absolute convergence tolerance.
9959b94acceSBarry Smith 
9969b94acceSBarry Smith    Input Parameters:
9979b94acceSBarry Smith .  snes - the SNES context
9989b94acceSBarry Smith .  atol - tolerance
9999b94acceSBarry Smith 
10009b94acceSBarry Smith    Options Database Key:
10019b94acceSBarry Smith $    -snes_atol tol
10029b94acceSBarry Smith 
10039b94acceSBarry Smith .keywords: SNES, nonlinear, set, absolute, convergence, tolerance
10049b94acceSBarry Smith 
10059b94acceSBarry Smith .seealso: SNESSetRelativeTolerance(), SNESSetSolutionTolerance(),
10069b94acceSBarry Smith            SNESSetTruncationTolerance()
10079b94acceSBarry Smith @*/
10089b94acceSBarry Smith int SNESSetAbsoluteTolerance(SNES snes,double atol)
10099b94acceSBarry Smith {
10109b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
10119b94acceSBarry Smith   snes->atol = atol;
10129b94acceSBarry Smith   return 0;
10139b94acceSBarry Smith }
10149b94acceSBarry Smith 
10159b94acceSBarry Smith /*@
10169b94acceSBarry Smith    SNESSetTruncationTolerance - Sets the tolerance that may be used by the
10179b94acceSBarry Smith    step routines to control the accuracy of the step computation.
10189b94acceSBarry Smith 
10199b94acceSBarry Smith    Input Parameters:
10209b94acceSBarry Smith .  snes - the SNES context
10219b94acceSBarry Smith .  tol - tolerance
10229b94acceSBarry Smith 
10239b94acceSBarry Smith    Options Database Key:
10249b94acceSBarry Smith $    -snes_ttol tol
10259b94acceSBarry Smith 
10269b94acceSBarry Smith    Notes:
10279b94acceSBarry Smith    If the step computation involves an application of the inverse
10289b94acceSBarry Smith    Jacobian (or Hessian), this parameter may be used to control the
1029a703fe33SLois Curfman McInnes    accuracy of that application.
10309b94acceSBarry Smith 
10319b94acceSBarry Smith .keywords: SNES, nonlinear, set, truncation, tolerance
10329b94acceSBarry Smith 
10339b94acceSBarry Smith .seealso: SNESSetRelativeTolerance(), SNESSetSolutionTolerance(),
10349b94acceSBarry Smith           SNESSetAbsoluteTolerance()
10359b94acceSBarry Smith @*/
10369b94acceSBarry Smith int SNESSetTruncationTolerance(SNES snes,double tol)
10379b94acceSBarry Smith {
10389b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
10399b94acceSBarry Smith   snes->trunctol = tol;
10409b94acceSBarry Smith   return 0;
10419b94acceSBarry Smith }
10429b94acceSBarry Smith 
10439b94acceSBarry Smith /*@
10449b94acceSBarry Smith    SNESSetSolutionTolerance - Sets the convergence tolerance in terms of
10459b94acceSBarry Smith    the norm of the change in the solution between steps.
10469b94acceSBarry Smith 
10479b94acceSBarry Smith    Input Parameters:
10489b94acceSBarry Smith .  snes - the SNES context
10499b94acceSBarry Smith .  tol - tolerance
10509b94acceSBarry Smith 
10519b94acceSBarry Smith    Options Database Key:
10529b94acceSBarry Smith $    -snes_stol tol
10539b94acceSBarry Smith 
10549b94acceSBarry Smith .keywords: SNES, nonlinear, set, solution, tolerance
10559b94acceSBarry Smith 
10569b94acceSBarry Smith .seealso: SNESSetTruncationTolerance(), SNESSetRelativeTolerance(),
10579b94acceSBarry Smith           SNESSetAbsoluteTolerance()
10589b94acceSBarry Smith @*/
10599b94acceSBarry Smith int SNESSetSolutionTolerance( SNES snes, double tol )
10609b94acceSBarry Smith {
10619b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
10629b94acceSBarry Smith   snes->xtol = tol;
10639b94acceSBarry Smith   return 0;
10649b94acceSBarry Smith }
10659b94acceSBarry Smith 
10669b94acceSBarry Smith /*@
10679b94acceSBarry Smith    SNESSetMinFunctionTolerance - Sets the minimum allowable function tolerance
10689b94acceSBarry Smith    for unconstrained minimization solvers.
10699b94acceSBarry Smith 
10709b94acceSBarry Smith    Input Parameters:
10719b94acceSBarry Smith .  snes - the SNES context
10729b94acceSBarry Smith .  ftol - minimum function tolerance
10739b94acceSBarry Smith 
10749b94acceSBarry Smith    Options Database Key:
10759b94acceSBarry Smith $    -snes_fmin ftol
10769b94acceSBarry Smith 
10779b94acceSBarry Smith    Note:
10789b94acceSBarry Smith    SNESSetMinFunctionTolerance() is valid for SNES_UNCONSTRAINED_MINIMIZATION
10799b94acceSBarry Smith    methods only.
10809b94acceSBarry Smith 
10819b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimum, convergence, function, tolerance
10829b94acceSBarry Smith 
10839b94acceSBarry Smith .seealso: SNESSetRelativeTolerance(), SNESSetSolutionTolerance(),
10849b94acceSBarry Smith            SNESSetTruncationTolerance()
10859b94acceSBarry Smith @*/
10869b94acceSBarry Smith int SNESSetMinFunctionTolerance(SNES snes,double ftol)
10879b94acceSBarry Smith {
10889b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
10899b94acceSBarry Smith   snes->fmin = ftol;
10909b94acceSBarry Smith   return 0;
10919b94acceSBarry Smith }
10929b94acceSBarry Smith 
10939b94acceSBarry Smith 
10949b94acceSBarry Smith 
10959b94acceSBarry Smith /* ---------- Routines to set various aspects of nonlinear solver --------- */
10969b94acceSBarry Smith 
10979b94acceSBarry Smith /*@C
1098052efed2SBarry Smith    SNESSetSolution - Sets the solution vector for use by the SNES routines.
10999b94acceSBarry Smith 
11009b94acceSBarry Smith    Input Parameters:
11019b94acceSBarry Smith .  snes - the SNES context
11029b94acceSBarry Smith .  x - the solution vector
11039b94acceSBarry Smith 
11049b94acceSBarry Smith 
11059b94acceSBarry Smith .keywords: SNES, nonlinear, set, solution, initial guess
11069b94acceSBarry Smith 
11079b94acceSBarry Smith .seealso: SNESGetSolution(), SNESSetJacobian(), SNESSetFunction()
11089b94acceSBarry Smith @*/
1109052efed2SBarry Smith int SNESSetSolution(SNES snes,Vec x)
11109b94acceSBarry Smith {
11119b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
11129b94acceSBarry Smith   snes->vec_sol             = snes->vec_sol_always = x;
11139b94acceSBarry Smith   return 0;
11149b94acceSBarry Smith }
11159b94acceSBarry Smith 
11169b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
11179b94acceSBarry Smith 
11189b94acceSBarry Smith /*@C
11199b94acceSBarry Smith    SNESSetMonitor - Sets the function that is to be used at every
11209b94acceSBarry Smith    iteration of the nonlinear solver to display the iteration's
11219b94acceSBarry Smith    progress.
11229b94acceSBarry Smith 
11239b94acceSBarry Smith    Input Parameters:
11249b94acceSBarry Smith .  snes - the SNES context
11259b94acceSBarry Smith .  func - monitoring routine
11269b94acceSBarry Smith .  mctx - optional user-defined context for private data for the
11279b94acceSBarry Smith           monitor routine (may be null)
11289b94acceSBarry Smith 
11299b94acceSBarry Smith    Calling sequence of func:
1130682d7d0cSBarry Smith    int func(SNES snes,int its, Vec x,Vec f,double norm,void *mctx)
11319b94acceSBarry Smith 
11329b94acceSBarry Smith $    snes - the SNES context
11339b94acceSBarry Smith $    its - iteration number
11349b94acceSBarry Smith $    mctx - optional monitoring context
11359b94acceSBarry Smith $
11369b94acceSBarry Smith $ SNES_NONLINEAR_EQUATIONS methods:
11379b94acceSBarry Smith $    norm - 2-norm function value (may be estimated)
11389b94acceSBarry Smith $
11399b94acceSBarry Smith $ SNES_UNCONSTRAINED_MINIMIZATION methods:
11409b94acceSBarry Smith $    norm - 2-norm gradient value (may be estimated)
11419b94acceSBarry Smith 
11429b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor
11439b94acceSBarry Smith 
11449b94acceSBarry Smith .seealso: SNESDefaultMonitor()
11459b94acceSBarry Smith @*/
11469b94acceSBarry Smith int SNESSetMonitor( SNES snes, int (*func)(SNES,int,double,void*),
11479b94acceSBarry Smith                     void *mctx )
11489b94acceSBarry Smith {
11499b94acceSBarry Smith   snes->monitor = func;
11509b94acceSBarry Smith   snes->monP    = (void*)mctx;
11519b94acceSBarry Smith   return 0;
11529b94acceSBarry Smith }
11539b94acceSBarry Smith 
11549b94acceSBarry Smith /*@C
11559b94acceSBarry Smith    SNESSetConvergenceTest - Sets the function that is to be used
11569b94acceSBarry Smith    to test for convergence of the nonlinear iterative solution.
11579b94acceSBarry Smith 
11589b94acceSBarry Smith    Input Parameters:
11599b94acceSBarry Smith .  snes - the SNES context
11609b94acceSBarry Smith .  func - routine to test for convergence
11619b94acceSBarry Smith .  cctx - optional context for private data for the convergence routine
11629b94acceSBarry Smith           (may be null)
11639b94acceSBarry Smith 
11649b94acceSBarry Smith    Calling sequence of func:
11659b94acceSBarry Smith    int func (SNES snes,double xnorm,double gnorm,
11669b94acceSBarry Smith              double f,void *cctx)
11679b94acceSBarry Smith 
11689b94acceSBarry Smith $    snes - the SNES context
11699b94acceSBarry Smith $    cctx - optional convergence context
11709b94acceSBarry Smith $    xnorm - 2-norm of current iterate
11719b94acceSBarry Smith $
11729b94acceSBarry Smith $ SNES_NONLINEAR_EQUATIONS methods:
11739b94acceSBarry Smith $    gnorm - 2-norm of current step
11749b94acceSBarry Smith $    f - 2-norm of function
11759b94acceSBarry Smith $
11769b94acceSBarry Smith $ SNES_UNCONSTRAINED_MINIMIZATION methods:
11779b94acceSBarry Smith $    gnorm - 2-norm of current gradient
11789b94acceSBarry Smith $    f - function value
11799b94acceSBarry Smith 
11809b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test
11819b94acceSBarry Smith 
11829b94acceSBarry Smith .seealso: SNESDefaultConverged()
11839b94acceSBarry Smith @*/
11849b94acceSBarry Smith int SNESSetConvergenceTest(SNES snes,
11859b94acceSBarry Smith           int (*func)(SNES,double,double,double,void*),void *cctx)
11869b94acceSBarry Smith {
11879b94acceSBarry Smith   (snes)->converged = func;
11889b94acceSBarry Smith   (snes)->cnvP      = cctx;
11899b94acceSBarry Smith   return 0;
11909b94acceSBarry Smith }
11919b94acceSBarry Smith 
11929b94acceSBarry Smith /*
11939b94acceSBarry Smith    SNESScaleStep_Private - Scales a step so that its length is less than the
11949b94acceSBarry Smith    positive parameter delta.
11959b94acceSBarry Smith 
11969b94acceSBarry Smith     Input Parameters:
11979b94acceSBarry Smith .   snes - the SNES context
11989b94acceSBarry Smith .   y - approximate solution of linear system
11999b94acceSBarry Smith .   fnorm - 2-norm of current function
12009b94acceSBarry Smith .   delta - trust region size
12019b94acceSBarry Smith 
12029b94acceSBarry Smith     Output Parameters:
12039b94acceSBarry Smith .   gpnorm - predicted function norm at the new point, assuming local
12049b94acceSBarry Smith     linearization.  The value is zero if the step lies within the trust
12059b94acceSBarry Smith     region, and exceeds zero otherwise.
12069b94acceSBarry Smith .   ynorm - 2-norm of the step
12079b94acceSBarry Smith 
12089b94acceSBarry Smith     Note:
1209ddd12767SBarry Smith     For non-trust region methods such as SNES_EQ_NLS, the parameter delta
12109b94acceSBarry Smith     is set to be the maximum allowable step size.
12119b94acceSBarry Smith 
12129b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step
12139b94acceSBarry Smith */
12149b94acceSBarry Smith int SNESScaleStep_Private(SNES snes,Vec y,double *fnorm,double *delta,
12159b94acceSBarry Smith                   double *gpnorm,double *ynorm)
12169b94acceSBarry Smith {
12179b94acceSBarry Smith   double norm;
12189b94acceSBarry Smith   Scalar cnorm;
1219cddf8d76SBarry Smith   VecNorm(y,NORM_2, &norm );
12209b94acceSBarry Smith   if (norm > *delta) {
12219b94acceSBarry Smith      norm = *delta/norm;
12229b94acceSBarry Smith      *gpnorm = (1.0 - norm)*(*fnorm);
12239b94acceSBarry Smith      cnorm = norm;
12249b94acceSBarry Smith      VecScale( &cnorm, y );
12259b94acceSBarry Smith      *ynorm = *delta;
12269b94acceSBarry Smith   } else {
12279b94acceSBarry Smith      *gpnorm = 0.0;
12289b94acceSBarry Smith      *ynorm = norm;
12299b94acceSBarry Smith   }
12309b94acceSBarry Smith   return 0;
12319b94acceSBarry Smith }
12329b94acceSBarry Smith 
12339b94acceSBarry Smith /*@
12349b94acceSBarry Smith    SNESSolve - Solves a nonlinear system.  Call SNESSolve after calling
12359b94acceSBarry Smith    SNESCreate(), optional routines of the form SNESSetXXX(), and SNESSetUp().
12369b94acceSBarry Smith 
12379b94acceSBarry Smith    Input Parameter:
12389b94acceSBarry Smith .  snes - the SNES context
12399b94acceSBarry Smith 
12409b94acceSBarry Smith    Output Parameter:
12419b94acceSBarry Smith    its - number of iterations until termination
12429b94acceSBarry Smith 
12439b94acceSBarry Smith .keywords: SNES, nonlinear, solve
12449b94acceSBarry Smith 
12459b94acceSBarry Smith .seealso: SNESCreate(), SNESSetUp(), SNESDestroy()
12469b94acceSBarry Smith @*/
12479b94acceSBarry Smith int SNESSolve(SNES snes,int *its)
12489b94acceSBarry Smith {
12493c7409f5SSatish Balay   int ierr, flg;
1250052efed2SBarry Smith 
1251052efed2SBarry Smith   if (!snes->setup_called) {ierr = SNESSetUp(snes); CHKERRQ(ierr);}
125237fcc0dbSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
12539b94acceSBarry Smith   PLogEventBegin(SNES_Solve,snes,0,0,0);
12549b94acceSBarry Smith   ierr = (*(snes)->solve)(snes,its); CHKERRQ(ierr);
12559b94acceSBarry Smith   PLogEventEnd(SNES_Solve,snes,0,0,0);
12563c7409f5SSatish Balay   ierr = OptionsHasName(PETSC_NULL,"-snes_view", &flg); CHKERRQ(ierr);
12573c7409f5SSatish Balay   if (flg) { ierr = SNESView(snes,STDOUT_VIEWER_WORLD); CHKERRQ(ierr); }
12589b94acceSBarry Smith   return 0;
12599b94acceSBarry Smith }
12609b94acceSBarry Smith 
12619b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */
126237fcc0dbSBarry Smith static NRList *__SNESList = 0;
12639b94acceSBarry Smith 
12649b94acceSBarry Smith /*@
12654b0e389bSBarry Smith    SNESSetType - Sets the method for the nonlinear solver.
12669b94acceSBarry Smith 
12679b94acceSBarry Smith    Input Parameters:
12689b94acceSBarry Smith .  snes - the SNES context
12699b94acceSBarry Smith .  method - a known method
12709b94acceSBarry Smith 
12719b94acceSBarry Smith    Notes:
12729b94acceSBarry Smith    See "petsc/include/snes.h" for available methods (for instance)
12739b94acceSBarry Smith $  Systems of nonlinear equations:
1274ddd12767SBarry Smith $    SNES_EQ_NLS - Newton's method with line search
1275ddd12767SBarry Smith $    SNES_EQ_NTR - Newton's method with trust region
12769b94acceSBarry Smith $  Unconstrained minimization:
12779b94acceSBarry Smith $    SNES_UM_NTR - Newton's method with trust region
1278a703fe33SLois Curfman McInnes $    SNES_UM_NLS - Newton's method with line search
12799b94acceSBarry Smith 
12809b94acceSBarry Smith   Options Database Command:
12814b0e389bSBarry Smith $ -snes_type  <method>
12829b94acceSBarry Smith $    Use -help for a list of available methods
12839b94acceSBarry Smith $    (for instance, ls or tr)
1284a703fe33SLois Curfman McInnes 
1285a703fe33SLois Curfman McInnes .keysords: SNES, set, method
12869b94acceSBarry Smith @*/
12874b0e389bSBarry Smith int SNESSetType(SNES snes,SNESType method)
12889b94acceSBarry Smith {
12899b94acceSBarry Smith   int (*r)(SNES);
12909b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
12919b94acceSBarry Smith   /* Get the function pointers for the iterative method requested */
129237fcc0dbSBarry Smith   if (!__SNESList) {SNESRegisterAll();}
129337fcc0dbSBarry Smith   if (!__SNESList) {SETERRQ(1,"SNESSetType:Could not get methods");}
129437fcc0dbSBarry Smith   r =  (int (*)(SNES))NRFindRoutine( __SNESList, (int)method, (char *)0 );
12954b0e389bSBarry Smith   if (!r) {SETERRQ(1,"SNESSetType:Unknown method");}
12960452661fSBarry Smith   if (snes->data) PetscFree(snes->data);
12979b94acceSBarry Smith   snes->set_method_called = 1;
12989b94acceSBarry Smith   return (*r)(snes);
12999b94acceSBarry Smith }
13009b94acceSBarry Smith 
13019b94acceSBarry Smith /* --------------------------------------------------------------------- */
13029b94acceSBarry Smith /*@C
13039b94acceSBarry Smith    SNESRegister - Adds the method to the nonlinear solver package, given
13044b0e389bSBarry Smith    a function pointer and a nonlinear solver name of the type SNESType.
13059b94acceSBarry Smith 
13069b94acceSBarry Smith    Input Parameters:
1307ddd12767SBarry Smith .  name - for instance SNES_EQ_NLS, SNES_EQ_NTR, ...
13089b94acceSBarry Smith .  sname - corfunPonding string for name
13099b94acceSBarry Smith .  create - routine to create method context
13109b94acceSBarry Smith 
13119b94acceSBarry Smith .keywords: SNES, nonlinear, register
13129b94acceSBarry Smith 
13139b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterDestroy()
13149b94acceSBarry Smith @*/
13159b94acceSBarry Smith int SNESRegister(int name, char *sname, int (*create)(SNES))
13169b94acceSBarry Smith {
13179b94acceSBarry Smith   int ierr;
131837fcc0dbSBarry Smith   if (!__SNESList) {ierr = NRCreate(&__SNESList); CHKERRQ(ierr);}
131937fcc0dbSBarry Smith   NRRegister( __SNESList, name, sname, (int (*)(void*))create );
13209b94acceSBarry Smith   return 0;
13219b94acceSBarry Smith }
13229b94acceSBarry Smith /* --------------------------------------------------------------------- */
13239b94acceSBarry Smith /*@C
13249b94acceSBarry Smith    SNESRegisterDestroy - Frees the list of nonlinear solvers that were
13259b94acceSBarry Smith    registered by SNESRegister().
13269b94acceSBarry Smith 
13279b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy
13289b94acceSBarry Smith 
13299b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll()
13309b94acceSBarry Smith @*/
13319b94acceSBarry Smith int SNESRegisterDestroy()
13329b94acceSBarry Smith {
133337fcc0dbSBarry Smith   if (__SNESList) {
133437fcc0dbSBarry Smith     NRDestroy( __SNESList );
133537fcc0dbSBarry Smith     __SNESList = 0;
13369b94acceSBarry Smith   }
13379b94acceSBarry Smith   return 0;
13389b94acceSBarry Smith }
13399b94acceSBarry Smith 
13409b94acceSBarry Smith /*
13414b0e389bSBarry Smith    SNESGetTypeFromOptions_Private - Sets the selected method from the
13429b94acceSBarry Smith    options database.
13439b94acceSBarry Smith 
13449b94acceSBarry Smith    Input Parameter:
13459b94acceSBarry Smith .  ctx - the SNES context
13469b94acceSBarry Smith 
13479b94acceSBarry Smith    Output Parameter:
13489b94acceSBarry Smith .  method -  solver method
13499b94acceSBarry Smith 
13509b94acceSBarry Smith    Returns:
13519b94acceSBarry Smith    Returns 1 if the method is found; 0 otherwise.
13529b94acceSBarry Smith 
13539b94acceSBarry Smith    Options Database Key:
13544b0e389bSBarry Smith $  -snes_type  method
13559b94acceSBarry Smith */
1356052efed2SBarry Smith int SNESGetTypeFromOptions_Private(SNES ctx,SNESType *method,int *flg)
13579b94acceSBarry Smith {
1358052efed2SBarry Smith   int ierr;
13599b94acceSBarry Smith   char sbuf[50];
1360052efed2SBarry Smith   ierr = OptionsGetString(ctx->prefix,"-snes_type", sbuf, 50, flg); CHKERRQ(ierr);
1361052efed2SBarry Smith   if (*flg) {
1362052efed2SBarry Smith     if (!__SNESList) {ierr = SNESRegisterAll(); CHKERRQ(ierr);}
136337fcc0dbSBarry Smith     *method = (SNESType)NRFindID( __SNESList, sbuf );
13649b94acceSBarry Smith   }
13659b94acceSBarry Smith   return 0;
13669b94acceSBarry Smith }
13679b94acceSBarry Smith 
13689b94acceSBarry Smith /*@C
13699a28b0a6SLois Curfman McInnes    SNESGetType - Gets the SNES method type and name (as a string).
13709b94acceSBarry Smith 
13719b94acceSBarry Smith    Input Parameter:
13724b0e389bSBarry Smith .  snes - nonlinear solver context
13739b94acceSBarry Smith 
13749b94acceSBarry Smith    Output Parameter:
13759a28b0a6SLois Curfman McInnes .  method - SNES method (or use PETSC_NULL)
13769a28b0a6SLois Curfman McInnes .  name - name of SNES method (or use PETSC_NULL)
13779b94acceSBarry Smith 
13789b94acceSBarry Smith .keywords: SNES, nonlinear, get, method, name
13799b94acceSBarry Smith @*/
13804b0e389bSBarry Smith int SNESGetType(SNES snes, SNESType *method,char **name)
13819b94acceSBarry Smith {
138206a9b0adSLois Curfman McInnes   int ierr;
138337fcc0dbSBarry Smith   if (!__SNESList) {ierr = SNESRegisterAll(); CHKERRQ(ierr);}
13844b0e389bSBarry Smith   if (method) *method = (SNESType) snes->type;
138537fcc0dbSBarry Smith   if (name)  *name = NRFindName( __SNESList, (int) snes->type );
13869b94acceSBarry Smith   return 0;
13879b94acceSBarry Smith }
13889b94acceSBarry Smith 
13899b94acceSBarry Smith #include <stdio.h>
13909b94acceSBarry Smith /*
13914b0e389bSBarry Smith    SNESPrintTypes_Private - Prints the SNES methods available from the
13929b94acceSBarry Smith    options database.
13939b94acceSBarry Smith 
13949b94acceSBarry Smith    Input Parameters:
13959b94acceSBarry Smith .  prefix - prefix (usually "-")
13964b0e389bSBarry Smith .  name - the options database name (by default "snes_type")
13979b94acceSBarry Smith */
13984b0e389bSBarry Smith int SNESPrintTypes_Private(char* prefix,char *name)
13999b94acceSBarry Smith {
14009b94acceSBarry Smith   FuncList *entry;
140137fcc0dbSBarry Smith   if (!__SNESList) {SNESRegisterAll();}
140237fcc0dbSBarry Smith   entry = __SNESList->head;
14039b94acceSBarry Smith   fprintf(stderr," %s%s (one of)",prefix,name);
14049b94acceSBarry Smith   while (entry) {
14059b94acceSBarry Smith     fprintf(stderr," %s",entry->name);
14069b94acceSBarry Smith     entry = entry->next;
14079b94acceSBarry Smith   }
14089b94acceSBarry Smith   fprintf(stderr,"\n");
14099b94acceSBarry Smith   return 0;
14109b94acceSBarry Smith }
14119b94acceSBarry Smith 
14129b94acceSBarry Smith /*@C
14139b94acceSBarry Smith    SNESGetSolution - Returns the vector where the approximate solution is
14149b94acceSBarry Smith    stored.
14159b94acceSBarry Smith 
14169b94acceSBarry Smith    Input Parameter:
14179b94acceSBarry Smith .  snes - the SNES context
14189b94acceSBarry Smith 
14199b94acceSBarry Smith    Output Parameter:
14209b94acceSBarry Smith .  x - the solution
14219b94acceSBarry Smith 
14229b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution
14239b94acceSBarry Smith 
14249b94acceSBarry Smith .seealso: SNESSetSolution(), SNESGetFunction(), SNESGetSolutionUpdate()
14259b94acceSBarry Smith @*/
14269b94acceSBarry Smith int SNESGetSolution(SNES snes,Vec *x)
14279b94acceSBarry Smith {
14289b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
14299b94acceSBarry Smith   *x = snes->vec_sol_always;
14309b94acceSBarry Smith   return 0;
14319b94acceSBarry Smith }
14329b94acceSBarry Smith 
14339b94acceSBarry Smith /*@C
14349b94acceSBarry Smith    SNESGetSolutionUpdate - Returns the vector where the solution update is
14359b94acceSBarry Smith    stored.
14369b94acceSBarry Smith 
14379b94acceSBarry Smith    Input Parameter:
14389b94acceSBarry Smith .  snes - the SNES context
14399b94acceSBarry Smith 
14409b94acceSBarry Smith    Output Parameter:
14419b94acceSBarry Smith .  x - the solution update
14429b94acceSBarry Smith 
14439b94acceSBarry Smith    Notes:
14449b94acceSBarry Smith    This vector is implementation dependent.
14459b94acceSBarry Smith 
14469b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update
14479b94acceSBarry Smith 
14489b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction
14499b94acceSBarry Smith @*/
14509b94acceSBarry Smith int SNESGetSolutionUpdate(SNES snes,Vec *x)
14519b94acceSBarry Smith {
14529b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
14539b94acceSBarry Smith   *x = snes->vec_sol_update_always;
14549b94acceSBarry Smith   return 0;
14559b94acceSBarry Smith }
14569b94acceSBarry Smith 
14579b94acceSBarry Smith /*@C
14589b94acceSBarry Smith    SNESGetFunction - Returns the vector where the function is
14599b94acceSBarry Smith    stored.  Actually usually returns the vector where the negative of
14609b94acceSBarry Smith    the function is stored.
14619b94acceSBarry Smith 
14629b94acceSBarry Smith    Input Parameter:
14639b94acceSBarry Smith .  snes - the SNES context
14649b94acceSBarry Smith 
14659b94acceSBarry Smith    Output Parameter:
14669b94acceSBarry Smith .  r - the function (or its negative)
14679b94acceSBarry Smith 
14689b94acceSBarry Smith    Notes:
14699b94acceSBarry Smith    SNESGetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only
14709b94acceSBarry Smith    Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are
14719b94acceSBarry Smith    SNESGetMinimizationFunction() and SNESGetGradient();
14729b94acceSBarry Smith 
14739b94acceSBarry Smith .keywords: SNES, nonlinear, get function
14749b94acceSBarry Smith 
14759b94acceSBarry Smith .seealso: SNESSetFunction(), SNESGetSolution()
14769b94acceSBarry Smith @*/
14779b94acceSBarry Smith int SNESGetFunction(SNES snes,Vec *r)
14789b94acceSBarry Smith {
14799b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
14809b94acceSBarry Smith   if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1,
148148d91487SBarry Smith     "SNESGetFunction:For SNES_NONLINEAR_EQUATIONS only");
14829b94acceSBarry Smith   *r = snes->vec_func_always;
14839b94acceSBarry Smith   return 0;
14849b94acceSBarry Smith }
14859b94acceSBarry Smith 
14869b94acceSBarry Smith /*@C
14879b94acceSBarry Smith    SNESGetGradient - Returns the vector where the gradient is
14889b94acceSBarry Smith    stored.  Actually usually returns the vector where the negative of
14899b94acceSBarry Smith    the function is stored.
14909b94acceSBarry Smith 
14919b94acceSBarry Smith    Input Parameter:
14929b94acceSBarry Smith .  snes - the SNES context
14939b94acceSBarry Smith 
14949b94acceSBarry Smith    Output Parameter:
14959b94acceSBarry Smith .  r - the gradient
14969b94acceSBarry Smith 
14979b94acceSBarry Smith    Notes:
14989b94acceSBarry Smith    SNESGetGradient() is valid for SNES_UNCONSTRAINED_MINIMIZATION methods
14999b94acceSBarry Smith    only.  An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
15009b94acceSBarry Smith    SNESGetFunction().
15019b94acceSBarry Smith 
15029b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient
15039b94acceSBarry Smith 
15049b94acceSBarry Smith .seealso: SNESGetMinimizationFunction(), SNESGetSolution()
15059b94acceSBarry Smith @*/
15069b94acceSBarry Smith int SNESGetGradient(SNES snes,Vec *r)
15079b94acceSBarry Smith {
15089b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
15099b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
151048d91487SBarry Smith     "SNESGetGradient:For SNES_UNCONSTRAINED_MINIMIZATION only");
15119b94acceSBarry Smith   *r = snes->vec_func_always;
15129b94acceSBarry Smith   return 0;
15139b94acceSBarry Smith }
15149b94acceSBarry Smith 
15159b94acceSBarry Smith /*@
15169b94acceSBarry Smith    SNESGetMinimizationFunction - Returns the scalar function value for
15179b94acceSBarry Smith    unconstrained minimization problems.
15189b94acceSBarry Smith 
15199b94acceSBarry Smith    Input Parameter:
15209b94acceSBarry Smith .  snes - the SNES context
15219b94acceSBarry Smith 
15229b94acceSBarry Smith    Output Parameter:
15239b94acceSBarry Smith .  r - the function
15249b94acceSBarry Smith 
15259b94acceSBarry Smith    Notes:
15269b94acceSBarry Smith    SNESGetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION
15279b94acceSBarry Smith    methods only.  An analogous routine for SNES_NONLINEAR_EQUATIONS methods is
15289b94acceSBarry Smith    SNESGetFunction().
15299b94acceSBarry Smith 
15309b94acceSBarry Smith .keywords: SNES, nonlinear, get, function
15319b94acceSBarry Smith 
15329b94acceSBarry Smith .seealso: SNESGetGradient(), SNESGetSolution()
15339b94acceSBarry Smith @*/
15349b94acceSBarry Smith int SNESGetMinimizationFunction(SNES snes,double *r)
15359b94acceSBarry Smith {
15369b94acceSBarry Smith   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
15379b94acceSBarry Smith   if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,
153848d91487SBarry Smith     "SNESGetMinimizationFunction:For SNES_UNCONSTRAINED_MINIMIZATION only");
15399b94acceSBarry Smith   *r = snes->fc;
15409b94acceSBarry Smith   return 0;
15419b94acceSBarry Smith }
15429b94acceSBarry Smith 
15439b94acceSBarry Smith 
15443c7409f5SSatish Balay /*@C
15453c7409f5SSatish Balay    SNESSetOptionsPrefix - Sets the prefix used for searching for all
15463c7409f5SSatish Balay    SNES options in the database.
15473c7409f5SSatish Balay 
15483c7409f5SSatish Balay    Input Parameter:
15493c7409f5SSatish Balay .  snes - the SNES context
15503c7409f5SSatish Balay .  prefix - the prefix to prepend to all option names
15513c7409f5SSatish Balay 
15523c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database
15533c7409f5SSatish Balay @*/
15543c7409f5SSatish Balay int SNESSetOptionsPrefix(SNES snes,char *prefix)
15553c7409f5SSatish Balay {
15563c7409f5SSatish Balay   int ierr;
15573c7409f5SSatish Balay 
15583c7409f5SSatish Balay   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
15593c7409f5SSatish Balay   ierr = PetscObjectSetPrefix((PetscObject)snes, prefix); CHKERRQ(ierr);
15603c7409f5SSatish Balay   ierr = SLESSetOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr);
15613c7409f5SSatish Balay   return 0;
15623c7409f5SSatish Balay }
15633c7409f5SSatish Balay 
15643c7409f5SSatish Balay /*@C
15653c7409f5SSatish Balay    SNESAppendOptionsPrefix - Append to the prefix used for searching for all
15663c7409f5SSatish Balay    SNES options in the database.
15673c7409f5SSatish Balay 
15683c7409f5SSatish Balay    Input Parameter:
15693c7409f5SSatish Balay .  snes - the SNES context
15703c7409f5SSatish Balay .  prefix - the prefix to prepend to all option names
15713c7409f5SSatish Balay 
15723c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database
15733c7409f5SSatish Balay @*/
15743c7409f5SSatish Balay int SNESAppendOptionsPrefix(SNES snes,char *prefix)
15753c7409f5SSatish Balay {
15763c7409f5SSatish Balay   int ierr;
15773c7409f5SSatish Balay 
15783c7409f5SSatish Balay   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
15793c7409f5SSatish Balay   ierr = PetscObjectAppendPrefix((PetscObject)snes, prefix); CHKERRQ(ierr);
15803c7409f5SSatish Balay   ierr = SLESAppendOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr);
15813c7409f5SSatish Balay   return 0;
15823c7409f5SSatish Balay }
15833c7409f5SSatish Balay 
1584c83590e2SSatish Balay /*@
15853c7409f5SSatish Balay    SNESGetOptionsPrefix - Sets the prefix used for searching for all
15863c7409f5SSatish Balay    SNES options in the database.
15873c7409f5SSatish Balay 
15883c7409f5SSatish Balay    Input Parameter:
15893c7409f5SSatish Balay .  snes - the SNES context
15903c7409f5SSatish Balay 
15913c7409f5SSatish Balay    Output Parameter:
15923c7409f5SSatish Balay .  prefix - pointer to the prefix string used
15933c7409f5SSatish Balay 
15943c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database
15953c7409f5SSatish Balay @*/
15963c7409f5SSatish Balay int SNESGetOptionsPrefix(SNES snes,char **prefix)
15973c7409f5SSatish Balay {
15983c7409f5SSatish Balay   int ierr;
15993c7409f5SSatish Balay 
16003c7409f5SSatish Balay   PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE);
16013c7409f5SSatish Balay   ierr = PetscObjectGetPrefix((PetscObject)snes, prefix); CHKERRQ(ierr);
16023c7409f5SSatish Balay   return 0;
16033c7409f5SSatish Balay }
16043c7409f5SSatish Balay 
16053c7409f5SSatish Balay 
16069b94acceSBarry Smith 
16079b94acceSBarry Smith 
16089b94acceSBarry Smith 
1609