19b94acceSBarry Smith #ifndef lint 2*b18e04deSLois Curfman McInnes static char vcid[] = "$Id: snes.c,v 1.68 1996/03/23 20:43:51 bsmith Exp curfman $"; 39b94acceSBarry Smith #endif 49b94acceSBarry Smith 59b94acceSBarry Smith #include "draw.h" /*I "draw.h" I*/ 69b94acceSBarry Smith #include "snesimpl.h" /*I "snes.h" I*/ 7682d7d0cSBarry Smith #include "sys/nreg.h" 89b94acceSBarry Smith #include "pinclude/pviewer.h" 99b94acceSBarry Smith #include <math.h> 109b94acceSBarry Smith 11052efed2SBarry Smith extern int SNESGetTypeFromOptions_Private(SNES,SNESType*,int*); 1233455573SSatish Balay extern int SNESPrintTypes_Private(MPI_Comm,char*,char*); 139b94acceSBarry Smith 149b94acceSBarry Smith /*@ 159b94acceSBarry Smith SNESView - Prints the SNES data structure. 169b94acceSBarry Smith 179b94acceSBarry Smith Input Parameters: 189b94acceSBarry Smith . SNES - the SNES context 199b94acceSBarry Smith . viewer - visualization context 209b94acceSBarry Smith 219b94acceSBarry Smith Options Database Key: 229b94acceSBarry Smith $ -snes_view : calls SNESView() at end of SNESSolve() 239b94acceSBarry Smith 249b94acceSBarry Smith Notes: 259b94acceSBarry Smith The available visualization contexts include 269b94acceSBarry Smith $ STDOUT_VIEWER_SELF - standard output (default) 279b94acceSBarry Smith $ STDOUT_VIEWER_WORLD - synchronized standard 289b94acceSBarry Smith $ output where only the first processor opens 299b94acceSBarry Smith $ the file. All other processors send their 309b94acceSBarry Smith $ data to the first processor to print. 319b94acceSBarry Smith 329b94acceSBarry Smith The user can open alternative vistualization contexts with 33dbb450caSBarry Smith $ ViewerFileOpenASCII() - output to a specified file 349b94acceSBarry Smith 359b94acceSBarry Smith .keywords: SNES, view 369b94acceSBarry Smith 37dbb450caSBarry Smith .seealso: ViewerFileOpenASCII() 389b94acceSBarry Smith @*/ 399b94acceSBarry Smith int SNESView(SNES snes,Viewer viewer) 409b94acceSBarry Smith { 419b94acceSBarry Smith SNES_KSP_EW_ConvCtx *kctx; 429b94acceSBarry Smith FILE *fd; 439b94acceSBarry Smith int ierr; 449b94acceSBarry Smith SLES sles; 459b94acceSBarry Smith char *method; 4619bcc07fSBarry Smith ViewerType vtype; 479b94acceSBarry Smith 4819bcc07fSBarry Smith ierr = ViewerGetType(viewer,&vtype); CHKERRQ(ierr); 4919bcc07fSBarry Smith if (vtype == ASCII_FILE_VIEWER || vtype == ASCII_FILES_VIEWER) { 5090ace30eSBarry Smith ierr = ViewerASCIIGetPointer(viewer,&fd); CHKERRQ(ierr); 5177c4ece6SBarry Smith PetscFPrintf(snes->comm,fd,"SNES Object:\n"); 524b0e389bSBarry Smith SNESGetType(snes,PETSC_NULL,&method); 5377c4ece6SBarry Smith PetscFPrintf(snes->comm,fd," method: %s\n",method); 549b94acceSBarry Smith if (snes->view) (*snes->view)((PetscObject)snes,viewer); 5577c4ece6SBarry Smith PetscFPrintf(snes->comm,fd, 569b94acceSBarry Smith " maximum iterations=%d, maximum function evaluations=%d\n", 579b94acceSBarry Smith snes->max_its,snes->max_funcs); 5877c4ece6SBarry Smith PetscFPrintf(snes->comm,fd, 599b94acceSBarry Smith " tolerances: relative=%g, absolute=%g, truncation=%g, solution=%g\n", 609b94acceSBarry Smith snes->rtol, snes->atol, snes->trunctol, snes->xtol); 619b94acceSBarry Smith if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) 6277c4ece6SBarry Smith PetscFPrintf(snes->comm,fd," min function tolerance=%g\n",snes->fmin); 639b94acceSBarry Smith if (snes->ksp_ewconv) { 649b94acceSBarry Smith kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx; 659b94acceSBarry Smith if (kctx) { 6677c4ece6SBarry Smith PetscFPrintf(snes->comm,fd, 679b94acceSBarry Smith " Eisenstat-Walker computation of KSP relative tolerance (version %d)\n", 689b94acceSBarry Smith kctx->version); 6977c4ece6SBarry Smith PetscFPrintf(snes->comm,fd, 709b94acceSBarry Smith " rtol_0=%g, rtol_max=%g, threshold=%g\n",kctx->rtol_0, 719b94acceSBarry Smith kctx->rtol_max,kctx->threshold); 7277c4ece6SBarry Smith PetscFPrintf(snes->comm,fd," gamma=%g, alpha=%g, alpha2=%g\n", 739b94acceSBarry Smith kctx->gamma,kctx->alpha,kctx->alpha2); 749b94acceSBarry Smith } 759b94acceSBarry Smith } 76c30f285eSLois Curfman McInnes } else if (vtype == STRING_VIEWER) { 77c30f285eSLois Curfman McInnes SNESGetType(snes,PETSC_NULL,&method); 78c30f285eSLois Curfman McInnes ViewerStringSPrintf(viewer," %-3.3s",method); 7919bcc07fSBarry Smith } 809b94acceSBarry Smith SNESGetSLES(snes,&sles); 819b94acceSBarry Smith ierr = SLESView(sles,viewer); CHKERRQ(ierr); 829b94acceSBarry Smith return 0; 839b94acceSBarry Smith } 849b94acceSBarry Smith 859b94acceSBarry Smith /*@ 86682d7d0cSBarry Smith SNESSetFromOptions - Sets various SNES and SLES parameters from user options. 879b94acceSBarry Smith 889b94acceSBarry Smith Input Parameter: 899b94acceSBarry Smith . snes - the SNES context 909b94acceSBarry Smith 919b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database 929b94acceSBarry Smith 93a86d99e1SLois Curfman McInnes .seealso: SNESPrintHelp(), SNESSetOptionsPrefix() 949b94acceSBarry Smith @*/ 959b94acceSBarry Smith int SNESSetFromOptions(SNES snes) 969b94acceSBarry Smith { 974b0e389bSBarry Smith SNESType method; 989b94acceSBarry Smith double tmp; 999b94acceSBarry Smith SLES sles; 1003c7409f5SSatish Balay int ierr, flg; 1013c7409f5SSatish Balay int version = PETSC_DEFAULT; 1029b94acceSBarry Smith double rtol_0 = PETSC_DEFAULT; 1039b94acceSBarry Smith double rtol_max = PETSC_DEFAULT; 1049b94acceSBarry Smith double gamma2 = PETSC_DEFAULT; 1059b94acceSBarry Smith double alpha = PETSC_DEFAULT; 1069b94acceSBarry Smith double alpha2 = PETSC_DEFAULT; 1079b94acceSBarry Smith double threshold = PETSC_DEFAULT; 1089b94acceSBarry Smith 10977c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1104b0e389bSBarry Smith if (snes->setup_called)SETERRQ(1,"SNESSetFromOptions:Must call prior to SNESSetUp!"); 111052efed2SBarry Smith ierr = SNESGetTypeFromOptions_Private(snes,&method,&flg); CHKERRQ(ierr); 112052efed2SBarry Smith if (flg) { 113052efed2SBarry Smith ierr = SNESSetType(snes,method); CHKERRQ(ierr); 1149b94acceSBarry Smith } 1155334005bSBarry Smith else if (!snes->set_method_called) { 1165334005bSBarry Smith if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 1175334005bSBarry Smith ierr = SNESSetType(snes,SNES_EQ_NLS); CHKERRQ(ierr); 1185334005bSBarry Smith } 1195334005bSBarry Smith else { 1205334005bSBarry Smith ierr = SNESSetType(snes,SNES_UM_NTR); CHKERRQ(ierr); 1215334005bSBarry Smith } 1225334005bSBarry Smith } 1235334005bSBarry Smith 1243c7409f5SSatish Balay ierr = OptionsHasName(PETSC_NULL,"-help", &flg); CHKERRQ(ierr); 1253c7409f5SSatish Balay if (flg) { SNESPrintHelp(snes); } 1263c7409f5SSatish Balay ierr = OptionsGetDouble(snes->prefix,"-snes_stol",&tmp, &flg); CHKERRQ(ierr); 127d7a720efSLois Curfman McInnes if (flg) { SNESSetTolerances(snes,PETSC_DEFAULT,PETSC_DEFAULT,tmp,PETSC_DEFAULT,PETSC_DEFAULT); } 1283c7409f5SSatish Balay ierr = OptionsGetDouble(snes->prefix,"-snes_atol",&tmp, &flg); CHKERRQ(ierr); 129d7a720efSLois Curfman McInnes if (flg) { SNESSetTolerances(snes,tmp,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT); } 1303c7409f5SSatish Balay ierr = OptionsGetDouble(snes->prefix,"-snes_rtol",&tmp, &flg); CHKERRQ(ierr); 131d7a720efSLois Curfman McInnes if (flg) { SNESSetTolerances(snes,PETSC_DEFAULT,tmp,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT); } 1323c7409f5SSatish Balay ierr = OptionsGetInt(snes->prefix,"-snes_max_it",&snes->max_its, &flg); CHKERRQ(ierr); 1333c7409f5SSatish Balay ierr = OptionsGetInt(snes->prefix,"-snes_max_funcs",&snes->max_funcs, &flg);CHKERRQ(ierr); 134d7a720efSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_trtol",&tmp, &flg); CHKERRQ(ierr); 135d7a720efSLois Curfman McInnes if (flg) { SNESSetTrustRegionTolerance(snes,tmp); } 136d7a720efSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_fmin",&tmp, &flg); CHKERRQ(ierr); 137d7a720efSLois Curfman McInnes if (flg) { SNESSetMinimizationFunctionTolerance(snes,tmp); } 1383c7409f5SSatish Balay ierr = OptionsHasName(snes->prefix,"-snes_ksp_ew_conv", &flg); CHKERRQ(ierr); 1393c7409f5SSatish Balay if (flg) { snes->ksp_ewconv = 1; } 140*b18e04deSLois Curfman McInnes ierr = OptionsGetInt(snes->prefix,"-snes_ksp_ew_version",&version,&flg); CHKERRQ(ierr); 141*b18e04deSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtol0",&rtol_0,&flg); CHKERRQ(ierr); 142*b18e04deSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtolmax",&rtol_max,&flg); CHKERRQ(ierr); 143*b18e04deSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_gamma",&gamma2,&flg); CHKERRQ(ierr); 144*b18e04deSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha",&alpha,&flg); CHKERRQ(ierr); 145*b18e04deSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha2",&alpha2,&flg); CHKERRQ(ierr); 146*b18e04deSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_threshold",&threshold,&flg); CHKERRQ(ierr); 1479b94acceSBarry Smith ierr = SNES_KSP_SetParametersEW(snes,version,rtol_0,rtol_max,gamma2,alpha, 1489b94acceSBarry Smith alpha2,threshold); CHKERRQ(ierr); 1493c7409f5SSatish Balay ierr = OptionsHasName(snes->prefix,"-snes_monitor", &flg); CHKERRQ(ierr); 1503c7409f5SSatish Balay if (flg) { SNESSetMonitor(snes,SNESDefaultMonitor,0); } 1513c7409f5SSatish Balay ierr = OptionsHasName(snes->prefix,"-snes_smonitor", &flg); CHKERRQ(ierr); 1523c7409f5SSatish Balay if (flg) { SNESSetMonitor(snes,SNESDefaultSMonitor,0); } 1533c7409f5SSatish Balay ierr = OptionsHasName(snes->prefix,"-snes_xmonitor", &flg); CHKERRQ(ierr); 1543c7409f5SSatish Balay if (flg) { 15517699dbbSLois Curfman McInnes int rank = 0; 156d7e8b826SBarry Smith DrawLG lg; 15717699dbbSLois Curfman McInnes MPI_Initialized(&rank); 15817699dbbSLois Curfman McInnes if (rank) MPI_Comm_rank(snes->comm,&rank); 15917699dbbSLois Curfman McInnes if (!rank) { 1609b94acceSBarry Smith ierr = SNESLGMonitorCreate(0,0,0,0,300,300,&lg); CHKERRQ(ierr); 1619b94acceSBarry Smith ierr = SNESSetMonitor(snes,SNESLGMonitor,(void *)lg); CHKERRQ(ierr); 162f8c826e1SBarry Smith snes->xmonitor = lg; 1639b94acceSBarry Smith PLogObjectParent(snes,lg); 1649b94acceSBarry Smith } 1659b94acceSBarry Smith } 1663c7409f5SSatish Balay ierr = OptionsHasName(snes->prefix,"-snes_fd", &flg); CHKERRQ(ierr); 1673c7409f5SSatish Balay if (flg && snes->method_class == SNES_NONLINEAR_EQUATIONS) { 1689b94acceSBarry Smith ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre, 1699b94acceSBarry Smith SNESDefaultComputeJacobian,snes->funP); CHKERRQ(ierr); 17094a424c1SBarry Smith PLogInfo(snes, 17131615d04SLois Curfman McInnes "SNESSetFromOptions: Setting default finite difference Jacobian matrix\n"); 17231615d04SLois Curfman McInnes } 17331615d04SLois Curfman McInnes else if (flg && snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 17431615d04SLois Curfman McInnes ierr = SNESSetHessian(snes,snes->jacobian,snes->jacobian_pre, 17531615d04SLois Curfman McInnes SNESDefaultComputeHessian,snes->funP); CHKERRQ(ierr); 17694a424c1SBarry Smith PLogInfo(snes, 17731615d04SLois Curfman McInnes "SNESSetFromOptions: Setting default finite difference Hessian matrix\n"); 1789b94acceSBarry Smith } 1799b94acceSBarry Smith ierr = SNESGetSLES(snes,&sles); CHKERRQ(ierr); 1809b94acceSBarry Smith ierr = SLESSetFromOptions(sles); CHKERRQ(ierr); 1819b94acceSBarry Smith if (!snes->setfromoptions) return 0; 1829b94acceSBarry Smith return (*snes->setfromoptions)(snes); 1839b94acceSBarry Smith } 1849b94acceSBarry Smith 1859b94acceSBarry Smith /*@ 1869b94acceSBarry Smith SNESPrintHelp - Prints all options for the SNES component. 1879b94acceSBarry Smith 1889b94acceSBarry Smith Input Parameter: 1899b94acceSBarry Smith . snes - the SNES context 1909b94acceSBarry Smith 191a703fe33SLois Curfman McInnes Options Database Keys: 192a703fe33SLois Curfman McInnes $ -help, -h 193a703fe33SLois Curfman McInnes 1949b94acceSBarry Smith .keywords: SNES, nonlinear, help 1959b94acceSBarry Smith 196682d7d0cSBarry Smith .seealso: SNESSetFromOptions() 1979b94acceSBarry Smith @*/ 1989b94acceSBarry Smith int SNESPrintHelp(SNES snes) 1999b94acceSBarry Smith { 2006daaf66cSBarry Smith char p[64]; 2016daaf66cSBarry Smith SNES_KSP_EW_ConvCtx *kctx; 2026daaf66cSBarry Smith 20377c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2046daaf66cSBarry Smith 2056daaf66cSBarry Smith PetscStrcpy(p,"-"); 2066daaf66cSBarry Smith if (snes->prefix) PetscStrcat(p, snes->prefix); 2076daaf66cSBarry Smith 2086daaf66cSBarry Smith kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx; 2096daaf66cSBarry Smith 21077c4ece6SBarry Smith PetscPrintf(snes->comm,"SNES options ----------------------------\n"); 21133455573SSatish Balay SNESPrintTypes_Private(snes->comm,p,"snes_type"); 21277c4ece6SBarry Smith PetscPrintf(snes->comm," %ssnes_monitor: use default SNES monitor\n",p); 21377c4ece6SBarry Smith PetscPrintf(snes->comm," %ssnes_view: view SNES info after each nonlinear solve\n",p); 214*b18e04deSLois Curfman McInnes PetscPrintf(snes->comm," %ssnes_max_it <its>: max iterations (default %d)\n",p,snes->max_its); 215*b18e04deSLois Curfman McInnes PetscPrintf(snes->comm," %ssnes_max_funcs <maxf>: max function evals (default %d)\n",p,snes->max_funcs); 216*b18e04deSLois Curfman McInnes PetscPrintf(snes->comm," %ssnes_stol <stol>: successive step tolerance (default %g)\n",p,snes->xtol); 217*b18e04deSLois Curfman McInnes PetscPrintf(snes->comm," %ssnes_atol <atol>: absolute tolerance (default %g)\n",p,snes->atol); 218*b18e04deSLois Curfman McInnes PetscPrintf(snes->comm," %ssnes_rtol <rtol>: relative tolerance (default %g)\n",p,snes->rtol); 219*b18e04deSLois Curfman McInnes PetscPrintf(snes->comm," %ssnes_trtol <trtol>: trust region parameter tolerance (default %g)\n",p,snes->deltatol); 220*b18e04deSLois Curfman McInnes if (snes->type == SNES_NONLINEAR_EQUATIONS) { 22177c4ece6SBarry Smith PetscPrintf(snes->comm, 2229b94acceSBarry Smith " options for solving systems of nonlinear equations only:\n"); 22377c4ece6SBarry Smith PetscPrintf(snes->comm," %ssnes_fd: use finite differences for Jacobian\n",p); 22477c4ece6SBarry Smith PetscPrintf(snes->comm," %ssnes_mf: use matrix-free Jacobian\n",p); 22577c4ece6SBarry Smith PetscPrintf(snes->comm," %ssnes_ksp_ew_conv: use Eisenstat-Walker computation of KSP rtol. Params are:\n",p); 22677c4ece6SBarry Smith PetscPrintf(snes->comm, 227*b18e04deSLois Curfman McInnes " %ssnes_ksp_ew_version <version> (1 or 2, default is %d)\n",p,kctx->version); 22877c4ece6SBarry Smith PetscPrintf(snes->comm, 229*b18e04deSLois Curfman McInnes " %ssnes_ksp_ew_rtol0 <rtol0> (0 <= rtol0 < 1, default %g)\n",p,kctx->rtol_0); 23077c4ece6SBarry Smith PetscPrintf(snes->comm, 231*b18e04deSLois Curfman McInnes " %ssnes_ksp_ew_rtolmax <rtolmax> (0 <= rtolmax < 1, default %g)\n",p,kctx->rtol_max); 23277c4ece6SBarry Smith PetscPrintf(snes->comm, 233*b18e04deSLois Curfman McInnes " %ssnes_ksp_ew_gamma <gamma> (0 <= gamma <= 1, default %g)\n",p,kctx->gamma); 23477c4ece6SBarry Smith PetscPrintf(snes->comm, 235*b18e04deSLois Curfman McInnes " %ssnes_ksp_ew_alpha <alpha> (1 < alpha <= 2, default %g)\n",p,kctx->alpha); 23677c4ece6SBarry Smith PetscPrintf(snes->comm, 237*b18e04deSLois Curfman McInnes " %ssnes_ksp_ew_alpha2 <alpha2> (default %g)\n",p,kctx->alpha2); 23877c4ece6SBarry Smith PetscPrintf(snes->comm, 239*b18e04deSLois Curfman McInnes " %ssnes_ksp_ew_threshold <threshold> (0 < threshold < 1, default %g)\n",p,kctx->threshold); 240*b18e04deSLois Curfman McInnes } 241*b18e04deSLois Curfman McInnes else if (snes->type == SNES_UNCONSTRAINED_MINIMIZATION) { 24277c4ece6SBarry Smith PetscPrintf(snes->comm, 2439b94acceSBarry Smith " options for solving unconstrained minimization problems only:\n"); 244*b18e04deSLois Curfman McInnes PetscPrintf(snes->comm," %ssnes_fmin <ftol>: minimum function tolerance (default %g)\n",p,snes->fmin); 24577c4ece6SBarry Smith PetscPrintf(snes->comm," %ssnes_fd: use finite differences for Hessian\n",p); 24677c4ece6SBarry Smith PetscPrintf(snes->comm," %ssnes_mf: use matrix-free Hessian\n",p); 247*b18e04deSLois Curfman McInnes } 248*b18e04deSLois Curfman McInnes PetscPrintf(snes->comm," Run program with %ssnes_type method -help for help on ",p); 24977c4ece6SBarry Smith PetscPrintf(snes->comm,"a particular method\n"); 2506daaf66cSBarry Smith if (snes->printhelp) (*snes->printhelp)(snes,p); 2519b94acceSBarry Smith return 0; 2529b94acceSBarry Smith } 2539b94acceSBarry Smith /*@ 2549b94acceSBarry Smith SNESSetApplicationContext - Sets the optional user-defined context for 2559b94acceSBarry Smith the nonlinear solvers. 2569b94acceSBarry Smith 2579b94acceSBarry Smith Input Parameters: 2589b94acceSBarry Smith . snes - the SNES context 2599b94acceSBarry Smith . usrP - optional user context 2609b94acceSBarry Smith 2619b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context 2629b94acceSBarry Smith 2639b94acceSBarry Smith .seealso: SNESGetApplicationContext() 2649b94acceSBarry Smith @*/ 2659b94acceSBarry Smith int SNESSetApplicationContext(SNES snes,void *usrP) 2669b94acceSBarry Smith { 26777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2689b94acceSBarry Smith snes->user = usrP; 2699b94acceSBarry Smith return 0; 2709b94acceSBarry Smith } 2719b94acceSBarry Smith /*@C 2729b94acceSBarry Smith SNESGetApplicationContext - Gets the user-defined context for the 2739b94acceSBarry Smith nonlinear solvers. 2749b94acceSBarry Smith 2759b94acceSBarry Smith Input Parameter: 2769b94acceSBarry Smith . snes - SNES context 2779b94acceSBarry Smith 2789b94acceSBarry Smith Output Parameter: 2799b94acceSBarry Smith . usrP - user context 2809b94acceSBarry Smith 2819b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context 2829b94acceSBarry Smith 2839b94acceSBarry Smith .seealso: SNESSetApplicationContext() 2849b94acceSBarry Smith @*/ 2859b94acceSBarry Smith int SNESGetApplicationContext( SNES snes, void **usrP ) 2869b94acceSBarry Smith { 28777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2889b94acceSBarry Smith *usrP = snes->user; 2899b94acceSBarry Smith return 0; 2909b94acceSBarry Smith } 2919b94acceSBarry Smith /*@ 2929b94acceSBarry Smith SNESGetIterationNumber - Gets the current iteration number of the 2939b94acceSBarry Smith nonlinear solver. 2949b94acceSBarry Smith 2959b94acceSBarry Smith Input Parameter: 2969b94acceSBarry Smith . snes - SNES context 2979b94acceSBarry Smith 2989b94acceSBarry Smith Output Parameter: 2999b94acceSBarry Smith . iter - iteration number 3009b94acceSBarry Smith 3019b94acceSBarry Smith .keywords: SNES, nonlinear, get, iteration, number 3029b94acceSBarry Smith @*/ 3039b94acceSBarry Smith int SNESGetIterationNumber(SNES snes,int* iter) 3049b94acceSBarry Smith { 30577c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 3069b94acceSBarry Smith *iter = snes->iter; 3079b94acceSBarry Smith return 0; 3089b94acceSBarry Smith } 3099b94acceSBarry Smith /*@ 3109b94acceSBarry Smith SNESGetFunctionNorm - Gets the norm of the current function that was set 3119b94acceSBarry Smith with SNESSSetFunction(). 3129b94acceSBarry Smith 3139b94acceSBarry Smith Input Parameter: 3149b94acceSBarry Smith . snes - SNES context 3159b94acceSBarry Smith 3169b94acceSBarry Smith Output Parameter: 3179b94acceSBarry Smith . fnorm - 2-norm of function 3189b94acceSBarry Smith 3199b94acceSBarry Smith Note: 3209b94acceSBarry Smith SNESGetFunctionNorm() is valid for SNES_NONLINEAR_EQUATIONS methods only. 321a86d99e1SLois Curfman McInnes A related routine for SNES_UNCONSTRAINED_MINIMIZATION methods is 322a86d99e1SLois Curfman McInnes SNESGetGradientNorm(). 3239b94acceSBarry Smith 3249b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm 325a86d99e1SLois Curfman McInnes 326a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction() 3279b94acceSBarry Smith @*/ 3289b94acceSBarry Smith int SNESGetFunctionNorm(SNES snes,Scalar *fnorm) 3299b94acceSBarry Smith { 33077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 3319b94acceSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1, 33248d91487SBarry Smith "SNESGetFunctionNorm:For SNES_NONLINEAR_EQUATIONS only"); 3339b94acceSBarry Smith *fnorm = snes->norm; 3349b94acceSBarry Smith return 0; 3359b94acceSBarry Smith } 3369b94acceSBarry Smith /*@ 3379b94acceSBarry Smith SNESGetGradientNorm - Gets the norm of the current gradient that was set 3389b94acceSBarry Smith with SNESSSetGradient(). 3399b94acceSBarry Smith 3409b94acceSBarry Smith Input Parameter: 3419b94acceSBarry Smith . snes - SNES context 3429b94acceSBarry Smith 3439b94acceSBarry Smith Output Parameter: 3449b94acceSBarry Smith . fnorm - 2-norm of gradient 3459b94acceSBarry Smith 3469b94acceSBarry Smith Note: 3479b94acceSBarry Smith SNESGetGradientNorm() is valid for SNES_UNCONSTRAINED_MINIMIZATION 348a86d99e1SLois Curfman McInnes methods only. A related routine for SNES_NONLINEAR_EQUATIONS methods 349a86d99e1SLois Curfman McInnes is SNESGetFunctionNorm(). 3509b94acceSBarry Smith 3519b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient, norm 352a86d99e1SLois Curfman McInnes 353a86d99e1SLois Curfman McInnes .seelso: SNESSetGradient() 3549b94acceSBarry Smith @*/ 3559b94acceSBarry Smith int SNESGetGradientNorm(SNES snes,Scalar *gnorm) 3569b94acceSBarry Smith { 35777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 3589b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 35948d91487SBarry Smith "SNESGetGradientNorm:For SNES_UNCONSTRAINED_MINIMIZATION only"); 3609b94acceSBarry Smith *gnorm = snes->norm; 3619b94acceSBarry Smith return 0; 3629b94acceSBarry Smith } 3639b94acceSBarry Smith /*@ 3649b94acceSBarry Smith SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps 3659b94acceSBarry Smith attempted by the nonlinear solver. 3669b94acceSBarry Smith 3679b94acceSBarry Smith Input Parameter: 3689b94acceSBarry Smith . snes - SNES context 3699b94acceSBarry Smith 3709b94acceSBarry Smith Output Parameter: 3719b94acceSBarry Smith . nfails - number of unsuccessful steps attempted 3729b94acceSBarry Smith 3739b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 3749b94acceSBarry Smith @*/ 3759b94acceSBarry Smith int SNESGetNumberUnsuccessfulSteps(SNES snes,int* nfails) 3769b94acceSBarry Smith { 37777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 3789b94acceSBarry Smith *nfails = snes->nfailures; 3799b94acceSBarry Smith return 0; 3809b94acceSBarry Smith } 3819b94acceSBarry Smith /*@C 3829b94acceSBarry Smith SNESGetSLES - Returns the SLES context for a SNES solver. 3839b94acceSBarry Smith 3849b94acceSBarry Smith Input Parameter: 3859b94acceSBarry Smith . snes - the SNES context 3869b94acceSBarry Smith 3879b94acceSBarry Smith Output Parameter: 3889b94acceSBarry Smith . sles - the SLES context 3899b94acceSBarry Smith 3909b94acceSBarry Smith Notes: 3919b94acceSBarry Smith The user can then directly manipulate the SLES context to set various 3929b94acceSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 3939b94acceSBarry Smith KSP and PC contexts as well. 3949b94acceSBarry Smith 3959b94acceSBarry Smith .keywords: SNES, nonlinear, get, SLES, context 3969b94acceSBarry Smith 3979b94acceSBarry Smith .seealso: SLESGetPC(), SLESGetKSP() 3989b94acceSBarry Smith @*/ 3999b94acceSBarry Smith int SNESGetSLES(SNES snes,SLES *sles) 4009b94acceSBarry Smith { 40177c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 4029b94acceSBarry Smith *sles = snes->sles; 4039b94acceSBarry Smith return 0; 4049b94acceSBarry Smith } 4059b94acceSBarry Smith /* -----------------------------------------------------------*/ 4069b94acceSBarry Smith /*@C 4079b94acceSBarry Smith SNESCreate - Creates a nonlinear solver context. 4089b94acceSBarry Smith 4099b94acceSBarry Smith Input Parameter: 4109b94acceSBarry Smith . comm - MPI communicator 4119b94acceSBarry Smith . type - type of method, one of 4129b94acceSBarry Smith $ SNES_NONLINEAR_EQUATIONS 4139b94acceSBarry Smith $ (for systems of nonlinear equations) 4149b94acceSBarry Smith $ SNES_UNCONSTRAINED_MINIMIZATION 4159b94acceSBarry Smith $ (for unconstrained minimization) 4169b94acceSBarry Smith 4179b94acceSBarry Smith Output Parameter: 4189b94acceSBarry Smith . outsnes - the new SNES context 4199b94acceSBarry Smith 4209b94acceSBarry Smith .keywords: SNES, nonlinear, create, context 4219b94acceSBarry Smith 42263a78c88SLois Curfman McInnes .seealso: SNESSolve(), SNESDestroy() 4239b94acceSBarry Smith @*/ 4244b0e389bSBarry Smith int SNESCreate(MPI_Comm comm,SNESProblemType type,SNES *outsnes) 4259b94acceSBarry Smith { 4269b94acceSBarry Smith int ierr; 4279b94acceSBarry Smith SNES snes; 4289b94acceSBarry Smith SNES_KSP_EW_ConvCtx *kctx; 42937fcc0dbSBarry Smith 4309b94acceSBarry Smith *outsnes = 0; 4312a0acf01SLois Curfman McInnes if (type != SNES_UNCONSTRAINED_MINIMIZATION && type != SNES_NONLINEAR_EQUATIONS) 4322a0acf01SLois Curfman McInnes SETERRQ(1,"SNESCreate:incorrect method type"); 4330452661fSBarry Smith PetscHeaderCreate(snes,_SNES,SNES_COOKIE,SNES_UNKNOWN_METHOD,comm); 4349b94acceSBarry Smith PLogObjectCreate(snes); 4359b94acceSBarry Smith snes->max_its = 50; 4369b94acceSBarry Smith snes->max_funcs = 1000; 4379b94acceSBarry Smith snes->norm = 0.0; 4385a2d0531SBarry Smith if (type == SNES_UNCONSTRAINED_MINIMIZATION) { 439*b18e04deSLois Curfman McInnes snes->rtol = 1.e-8; 440*b18e04deSLois Curfman McInnes snes->ttol = 0.0; 4419b94acceSBarry Smith snes->atol = 1.e-10; 4425a2d0531SBarry Smith } 443b4874afaSBarry Smith else { 444b4874afaSBarry Smith snes->rtol = 1.e-8; 445b4874afaSBarry Smith snes->ttol = 0.0; 446b4874afaSBarry Smith snes->atol = 1.e-50; 447b4874afaSBarry Smith } 4489b94acceSBarry Smith snes->xtol = 1.e-8; 449*b18e04deSLois Curfman McInnes snes->trunctol = 1.e-12; /* no longer used */ 4509b94acceSBarry Smith snes->nfuncs = 0; 4519b94acceSBarry Smith snes->nfailures = 0; 4529b94acceSBarry Smith snes->monitor = 0; 4539b94acceSBarry Smith snes->data = 0; 4549b94acceSBarry Smith snes->view = 0; 4559b94acceSBarry Smith snes->computeumfunction = 0; 4569b94acceSBarry Smith snes->umfunP = 0; 4579b94acceSBarry Smith snes->fc = 0; 4589b94acceSBarry Smith snes->deltatol = 1.e-12; 4599b94acceSBarry Smith snes->fmin = -1.e30; 4609b94acceSBarry Smith snes->method_class = type; 4619b94acceSBarry Smith snes->set_method_called = 0; 462a703fe33SLois Curfman McInnes snes->setup_called = 0; 4639b94acceSBarry Smith snes->ksp_ewconv = 0; 4649b94acceSBarry Smith 4659b94acceSBarry Smith /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 4660452661fSBarry Smith kctx = PetscNew(SNES_KSP_EW_ConvCtx); CHKPTRQ(kctx); 4679b94acceSBarry Smith snes->kspconvctx = (void*)kctx; 4689b94acceSBarry Smith kctx->version = 2; 4699b94acceSBarry Smith kctx->rtol_0 = .3; /* Eisenstat and Walker suggest rtol_0=.5, but 4709b94acceSBarry Smith this was too large for some test cases */ 4719b94acceSBarry Smith kctx->rtol_last = 0; 4729b94acceSBarry Smith kctx->rtol_max = .9; 4739b94acceSBarry Smith kctx->gamma = 1.0; 4749b94acceSBarry Smith kctx->alpha2 = .5*(1.0 + sqrt(5.0)); 4759b94acceSBarry Smith kctx->alpha = kctx->alpha2; 4769b94acceSBarry Smith kctx->threshold = .1; 4779b94acceSBarry Smith kctx->lresid_last = 0; 4789b94acceSBarry Smith kctx->norm_last = 0; 4799b94acceSBarry Smith 4809b94acceSBarry Smith ierr = SLESCreate(comm,&snes->sles); CHKERRQ(ierr); 4819b94acceSBarry Smith PLogObjectParent(snes,snes->sles) 4825334005bSBarry Smith 4839b94acceSBarry Smith *outsnes = snes; 4849b94acceSBarry Smith return 0; 4859b94acceSBarry Smith } 4869b94acceSBarry Smith 4879b94acceSBarry Smith /* --------------------------------------------------------------- */ 4889b94acceSBarry Smith /*@C 4899b94acceSBarry Smith SNESSetFunction - Sets the function evaluation routine and function 4909b94acceSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 4919b94acceSBarry Smith equations. 4929b94acceSBarry Smith 4939b94acceSBarry Smith Input Parameters: 4949b94acceSBarry Smith . snes - the SNES context 4959b94acceSBarry Smith . func - function evaluation routine 4969b94acceSBarry Smith . ctx - optional user-defined function context 4979b94acceSBarry Smith . r - vector to store function value 4989b94acceSBarry Smith 4999b94acceSBarry Smith Calling sequence of func: 5009b94acceSBarry Smith . func (SNES, Vec x, Vec f, void *ctx); 5019b94acceSBarry Smith 5029b94acceSBarry Smith . x - input vector 5035334005bSBarry Smith . f - vector function 5049b94acceSBarry Smith . ctx - optional user-defined context for private data for the 5059b94acceSBarry Smith function evaluation routine (may be null) 5069b94acceSBarry Smith 5079b94acceSBarry Smith Notes: 5089b94acceSBarry Smith The Newton-like methods typically solve linear systems of the form 5099b94acceSBarry Smith $ f'(x) x = -f(x), 5109b94acceSBarry Smith $ where f'(x) denotes the Jacobian matrix and f(x) is the function. 5119b94acceSBarry Smith 5129b94acceSBarry Smith SNESSetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only. 5139b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 5149b94acceSBarry Smith SNESSetMinimizationFunction() and SNESSetGradient(); 5159b94acceSBarry Smith 5169b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 5179b94acceSBarry Smith 518a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 5199b94acceSBarry Smith @*/ 5205334005bSBarry Smith int SNESSetFunction( SNES snes, Vec r, int (*func)(SNES,Vec,Vec,void*),void *ctx) 5219b94acceSBarry Smith { 52277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 5239b94acceSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1, 52448d91487SBarry Smith "SNESSetFunction:For SNES_NONLINEAR_EQUATIONS only"); 5259b94acceSBarry Smith snes->computefunction = func; 5269b94acceSBarry Smith snes->vec_func = snes->vec_func_always = r; 5279b94acceSBarry Smith snes->funP = ctx; 5289b94acceSBarry Smith return 0; 5299b94acceSBarry Smith } 5309b94acceSBarry Smith 5319b94acceSBarry Smith /*@ 5329b94acceSBarry Smith SNESComputeFunction - Computes the function that has been set with 5339b94acceSBarry Smith SNESSetFunction(). 5349b94acceSBarry Smith 5359b94acceSBarry Smith Input Parameters: 5369b94acceSBarry Smith . snes - the SNES context 5379b94acceSBarry Smith . x - input vector 5389b94acceSBarry Smith 5399b94acceSBarry Smith Output Parameter: 5403638b69dSLois Curfman McInnes . y - function vector, as set by SNESSetFunction() 5419b94acceSBarry Smith 542a86d99e1SLois Curfman McInnes n Notes: 5439b94acceSBarry Smith SNESComputeFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only. 5449b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 5459b94acceSBarry Smith SNESComputeMinimizationFunction() and SNESComputeGradient(); 5469b94acceSBarry Smith 5479b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function 5489b94acceSBarry Smith 549a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction() 5509b94acceSBarry Smith @*/ 5519b94acceSBarry Smith int SNESComputeFunction(SNES snes,Vec x, Vec y) 5529b94acceSBarry Smith { 5539b94acceSBarry Smith int ierr; 5549b94acceSBarry Smith 555a86d99e1SLois Curfman McInnes if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1, 556a86d99e1SLois Curfman McInnes "SNESComputeFunction: For SNES_NONLINEAR_EQUATIONS only"); 5579b94acceSBarry Smith PLogEventBegin(SNES_FunctionEval,snes,x,y,0); 5589b94acceSBarry Smith ierr = (*snes->computefunction)(snes,x,y,snes->funP); CHKERRQ(ierr); 5599b94acceSBarry Smith PLogEventEnd(SNES_FunctionEval,snes,x,y,0); 5609b94acceSBarry Smith return 0; 5619b94acceSBarry Smith } 5629b94acceSBarry Smith 5639b94acceSBarry Smith /*@C 5649b94acceSBarry Smith SNESSetMinimizationFunction - Sets the function evaluation routine for 5659b94acceSBarry Smith unconstrained minimization. 5669b94acceSBarry Smith 5679b94acceSBarry Smith Input Parameters: 5689b94acceSBarry Smith . snes - the SNES context 5699b94acceSBarry Smith . func - function evaluation routine 5709b94acceSBarry Smith . ctx - optional user-defined function context 5719b94acceSBarry Smith 5729b94acceSBarry Smith Calling sequence of func: 5739b94acceSBarry Smith . func (SNES snes,Vec x,double *f,void *ctx); 5749b94acceSBarry Smith 5759b94acceSBarry Smith . x - input vector 5769b94acceSBarry Smith . f - function 5779b94acceSBarry Smith . ctx - optional user-defined context for private data for the 5789b94acceSBarry Smith function evaluation routine (may be null) 5799b94acceSBarry Smith 5809b94acceSBarry Smith Notes: 5819b94acceSBarry Smith SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 5829b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 5839b94acceSBarry Smith SNESSetFunction(). 5849b94acceSBarry Smith 5859b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimization, function 5869b94acceSBarry Smith 587a86d99e1SLois Curfman McInnes .seealso: SNESGetMinimizationFunction(), SNESComputeMinimizationFunction(), 588a86d99e1SLois Curfman McInnes SNESSetHessian(), SNESSetGradient() 5899b94acceSBarry Smith @*/ 5909b94acceSBarry Smith int SNESSetMinimizationFunction(SNES snes,int (*func)(SNES,Vec,double*,void*), 5919b94acceSBarry Smith void *ctx) 5929b94acceSBarry Smith { 59377c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 5949b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 59548d91487SBarry Smith "SNESSetMinimizationFunction:Only for SNES_UNCONSTRAINED_MINIMIZATION"); 5969b94acceSBarry Smith snes->computeumfunction = func; 5979b94acceSBarry Smith snes->umfunP = ctx; 5989b94acceSBarry Smith return 0; 5999b94acceSBarry Smith } 6009b94acceSBarry Smith 6019b94acceSBarry Smith /*@ 6029b94acceSBarry Smith SNESComputeMinimizationFunction - Computes the function that has been 6039b94acceSBarry Smith set with SNESSetMinimizationFunction(). 6049b94acceSBarry Smith 6059b94acceSBarry Smith Input Parameters: 6069b94acceSBarry Smith . snes - the SNES context 6079b94acceSBarry Smith . x - input vector 6089b94acceSBarry Smith 6099b94acceSBarry Smith Output Parameter: 6109b94acceSBarry Smith . y - function value 6119b94acceSBarry Smith 6129b94acceSBarry Smith Notes: 6139b94acceSBarry Smith SNESComputeMinimizationFunction() is valid only for 6149b94acceSBarry Smith SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 6159b94acceSBarry Smith SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction(). 616a86d99e1SLois Curfman McInnes 617a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, minimization, function 618a86d99e1SLois Curfman McInnes 619a86d99e1SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESGetMinimizationFunction(), 620a86d99e1SLois Curfman McInnes SNESComputeGradient(), SNESComputeHessian() 6219b94acceSBarry Smith @*/ 6229b94acceSBarry Smith int SNESComputeMinimizationFunction(SNES snes,Vec x,double *y) 6239b94acceSBarry Smith { 6249b94acceSBarry Smith int ierr; 6259b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 62648d91487SBarry Smith "SNESComputeMinimizationFunction:Only for SNES_UNCONSTRAINED_MINIMIZATION"); 6279b94acceSBarry Smith PLogEventBegin(SNES_MinimizationFunctionEval,snes,x,y,0); 6289b94acceSBarry Smith ierr = (*snes->computeumfunction)(snes,x,y,snes->umfunP); CHKERRQ(ierr); 6299b94acceSBarry Smith PLogEventEnd(SNES_MinimizationFunctionEval,snes,x,y,0); 6309b94acceSBarry Smith return 0; 6319b94acceSBarry Smith } 6329b94acceSBarry Smith 6339b94acceSBarry Smith /*@C 6349b94acceSBarry Smith SNESSetGradient - Sets the gradient evaluation routine and gradient 6359b94acceSBarry Smith vector for use by the SNES routines. 6369b94acceSBarry Smith 6379b94acceSBarry Smith Input Parameters: 6389b94acceSBarry Smith . snes - the SNES context 6399b94acceSBarry Smith . func - function evaluation routine 6409b94acceSBarry Smith . ctx - optional user-defined function context 6419b94acceSBarry Smith . r - vector to store gradient value 6429b94acceSBarry Smith 6439b94acceSBarry Smith Calling sequence of func: 6449b94acceSBarry Smith . func (SNES, Vec x, Vec g, void *ctx); 6459b94acceSBarry Smith 6469b94acceSBarry Smith . x - input vector 6479b94acceSBarry Smith . g - gradient vector 6489b94acceSBarry Smith . ctx - optional user-defined context for private data for the 6499b94acceSBarry Smith function evaluation routine (may be null) 6509b94acceSBarry Smith 6519b94acceSBarry Smith Notes: 6529b94acceSBarry Smith SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 6539b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 6549b94acceSBarry Smith SNESSetFunction(). 6559b94acceSBarry Smith 6569b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 6579b94acceSBarry Smith 658a86d99e1SLois Curfman McInnes .seealso: SNESGetGradient(), SNESComputeGradient(), SNESSetHessian(), 659a86d99e1SLois Curfman McInnes SNESSetMinimizationFunction(), 6609b94acceSBarry Smith @*/ 6619b94acceSBarry Smith int SNESSetGradient(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*), 6629b94acceSBarry Smith void *ctx) 6639b94acceSBarry Smith { 66477c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 6659b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 66648d91487SBarry Smith "SNESSetGradient:For SNES_UNCONSTRAINED_MINIMIZATION only"); 6679b94acceSBarry Smith snes->computefunction = func; 6689b94acceSBarry Smith snes->vec_func = snes->vec_func_always = r; 6699b94acceSBarry Smith snes->funP = ctx; 6709b94acceSBarry Smith return 0; 6719b94acceSBarry Smith } 6729b94acceSBarry Smith 6739b94acceSBarry Smith /*@ 674a86d99e1SLois Curfman McInnes SNESComputeGradient - Computes the gradient that has been set with 675a86d99e1SLois Curfman McInnes SNESSetGradient(). 6769b94acceSBarry Smith 6779b94acceSBarry Smith Input Parameters: 6789b94acceSBarry Smith . snes - the SNES context 6799b94acceSBarry Smith . x - input vector 6809b94acceSBarry Smith 6819b94acceSBarry Smith Output Parameter: 6829b94acceSBarry Smith . y - gradient vector 6839b94acceSBarry Smith 6849b94acceSBarry Smith Notes: 6859b94acceSBarry Smith SNESComputeGradient() is valid only for 6869b94acceSBarry Smith SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 6879b94acceSBarry Smith SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction(). 688a86d99e1SLois Curfman McInnes 689a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, gradient 690a86d99e1SLois Curfman McInnes 691a86d99e1SLois Curfman McInnes .seealso: SNESSetGradient(), SNESGetGradient(), 692a86d99e1SLois Curfman McInnes SNESComputeMinimizationFunction(), SNESComputeHessian() 6939b94acceSBarry Smith @*/ 6949b94acceSBarry Smith int SNESComputeGradient(SNES snes,Vec x, Vec y) 6959b94acceSBarry Smith { 6969b94acceSBarry Smith int ierr; 6979b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 69848d91487SBarry Smith "SNESComputeGradient:For SNES_UNCONSTRAINED_MINIMIZATION only"); 6999b94acceSBarry Smith PLogEventBegin(SNES_GradientEval,snes,x,y,0); 7009b94acceSBarry Smith ierr = (*snes->computefunction)(snes,x,y,snes->funP); CHKERRQ(ierr); 7019b94acceSBarry Smith PLogEventEnd(SNES_GradientEval,snes,x,y,0); 7029b94acceSBarry Smith return 0; 7039b94acceSBarry Smith } 7049b94acceSBarry Smith 70562fef451SLois Curfman McInnes /*@ 70662fef451SLois Curfman McInnes SNESComputeJacobian - Computes the Jacobian matrix that has been 70762fef451SLois Curfman McInnes set with SNESSetJacobian(). 70862fef451SLois Curfman McInnes 70962fef451SLois Curfman McInnes Input Parameters: 71062fef451SLois Curfman McInnes . snes - the SNES context 71162fef451SLois Curfman McInnes . x - input vector 71262fef451SLois Curfman McInnes 71362fef451SLois Curfman McInnes Output Parameters: 71462fef451SLois Curfman McInnes . A - Jacobian matrix 71562fef451SLois Curfman McInnes . B - optional preconditioning matrix 71662fef451SLois Curfman McInnes . flag - flag indicating matrix structure 71762fef451SLois Curfman McInnes 71862fef451SLois Curfman McInnes Notes: 71962fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 72062fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 72162fef451SLois Curfman McInnes 72262fef451SLois Curfman McInnes See SLESSetOperators() for information about setting the flag parameter. 72362fef451SLois Curfman McInnes 72462fef451SLois Curfman McInnes SNESComputeJacobian() is valid only for SNES_NONLINEAR_EQUATIONS 72562fef451SLois Curfman McInnes methods. An analogous routine for SNES_UNCONSTRAINED_MINIMIZATION 72662fef451SLois Curfman McInnes methods is SNESComputeJacobian(). 72762fef451SLois Curfman McInnes 72862fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix 72962fef451SLois Curfman McInnes 73062fef451SLois Curfman McInnes .seealso: SNESSetJacobian(), SLESSetOperators() 73162fef451SLois Curfman McInnes @*/ 7329b94acceSBarry Smith int SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 7339b94acceSBarry Smith { 7349b94acceSBarry Smith int ierr; 7359b94acceSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1, 73648d91487SBarry Smith "SNESComputeJacobian: For SNES_NONLINEAR_EQUATIONS only"); 7379b94acceSBarry Smith if (!snes->computejacobian) return 0; 7389b94acceSBarry Smith PLogEventBegin(SNES_JacobianEval,snes,X,*A,*B); 739c4fc05e7SBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 7409b94acceSBarry Smith ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP); CHKERRQ(ierr); 7419b94acceSBarry Smith PLogEventEnd(SNES_JacobianEval,snes,X,*A,*B); 7426d84be18SBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 74377c4ece6SBarry Smith PetscValidHeaderSpecific(*A,MAT_COOKIE); 74477c4ece6SBarry Smith PetscValidHeaderSpecific(*B,MAT_COOKIE); 7459b94acceSBarry Smith return 0; 7469b94acceSBarry Smith } 7479b94acceSBarry Smith 74862fef451SLois Curfman McInnes /*@ 74962fef451SLois Curfman McInnes SNESComputeHessian - Computes the Hessian matrix that has been 75062fef451SLois Curfman McInnes set with SNESSetHessian(). 75162fef451SLois Curfman McInnes 75262fef451SLois Curfman McInnes Input Parameters: 75362fef451SLois Curfman McInnes . snes - the SNES context 75462fef451SLois Curfman McInnes . x - input vector 75562fef451SLois Curfman McInnes 75662fef451SLois Curfman McInnes Output Parameters: 75762fef451SLois Curfman McInnes . A - Hessian matrix 75862fef451SLois Curfman McInnes . B - optional preconditioning matrix 75962fef451SLois Curfman McInnes . flag - flag indicating matrix structure 76062fef451SLois Curfman McInnes 76162fef451SLois Curfman McInnes Notes: 76262fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 76362fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 76462fef451SLois Curfman McInnes 76562fef451SLois Curfman McInnes See SLESSetOperators() for information about setting the flag parameter. 76662fef451SLois Curfman McInnes 76762fef451SLois Curfman McInnes SNESComputeHessian() is valid only for 76862fef451SLois Curfman McInnes SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 76962fef451SLois Curfman McInnes SNES_NONLINEAR_EQUATIONS methods is SNESComputeJacobian(). 77062fef451SLois Curfman McInnes 77162fef451SLois Curfman McInnes .keywords: SNES, compute, Hessian, matrix 77262fef451SLois Curfman McInnes 773a86d99e1SLois Curfman McInnes .seealso: SNESSetHessian(), SLESSetOperators(), SNESComputeGradient(), 774a86d99e1SLois Curfman McInnes SNESComputeMinimizationFunction() 77562fef451SLois Curfman McInnes @*/ 77662fef451SLois Curfman McInnes int SNESComputeHessian(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag) 7779b94acceSBarry Smith { 7789b94acceSBarry Smith int ierr; 7799b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 78048d91487SBarry Smith "SNESComputeHessian:For SNES_UNCONSTRAINED_MINIMIZATION only"); 7819b94acceSBarry Smith if (!snes->computejacobian) return 0; 78262fef451SLois Curfman McInnes PLogEventBegin(SNES_HessianEval,snes,x,*A,*B); 7830f4a323eSLois Curfman McInnes *flag = DIFFERENT_NONZERO_PATTERN; 78462fef451SLois Curfman McInnes ierr = (*snes->computejacobian)(snes,x,A,B,flag,snes->jacP); CHKERRQ(ierr); 78562fef451SLois Curfman McInnes PLogEventEnd(SNES_HessianEval,snes,x,*A,*B); 7860f4a323eSLois Curfman McInnes /* make sure user returned a correct Jacobian and preconditioner */ 78777c4ece6SBarry Smith PetscValidHeaderSpecific(*A,MAT_COOKIE); 78877c4ece6SBarry Smith PetscValidHeaderSpecific(*B,MAT_COOKIE); 7899b94acceSBarry Smith return 0; 7909b94acceSBarry Smith } 7919b94acceSBarry Smith 7929b94acceSBarry Smith /*@C 7939b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 7949b94acceSBarry Smith location to store it. 7959b94acceSBarry Smith 7969b94acceSBarry Smith Input Parameters: 7979b94acceSBarry Smith . snes - the SNES context 7989b94acceSBarry Smith . A - Jacobian matrix 7999b94acceSBarry Smith . B - preconditioner matrix (usually same as the Jacobian) 8009b94acceSBarry Smith . func - Jacobian evaluation routine 8019b94acceSBarry Smith . ctx - optional user-defined context for private data for the 8029b94acceSBarry Smith Jacobian evaluation routine (may be null) 8039b94acceSBarry Smith 8049b94acceSBarry Smith Calling sequence of func: 8059b94acceSBarry Smith . func (SNES,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 8069b94acceSBarry Smith 8079b94acceSBarry Smith . x - input vector 8089b94acceSBarry Smith . A - Jacobian matrix 8099b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 810ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 811ac21db08SLois Curfman McInnes structure (same as flag in SLESSetOperators()) 8129b94acceSBarry Smith . ctx - optional user-defined Jacobian context 8139b94acceSBarry Smith 8149b94acceSBarry Smith Notes: 815ac21db08SLois Curfman McInnes See SLESSetOperators() for information about setting the flag input 816ac21db08SLois Curfman McInnes parameter in the routine func(). Be sure to read this information! 817ac21db08SLois Curfman McInnes 818ac21db08SLois Curfman McInnes The routine func() takes Mat * as the matrix arguments rather than Mat. 8199b94acceSBarry Smith This allows the Jacobian evaluation routine to replace A and/or B with a 8209b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 8219b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 8229b94acceSBarry Smith throughout the global iterations. 8239b94acceSBarry Smith 8249b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix 8259b94acceSBarry Smith 826ac21db08SLois Curfman McInnes .seealso: SLESSetOperators(), SNESSetFunction() 8279b94acceSBarry Smith @*/ 8289b94acceSBarry Smith int SNESSetJacobian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*, 8299b94acceSBarry Smith MatStructure*,void*),void *ctx) 8309b94acceSBarry Smith { 83177c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 8329b94acceSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1, 83348d91487SBarry Smith "SNESSetJacobian:For SNES_NONLINEAR_EQUATIONS only"); 8349b94acceSBarry Smith snes->computejacobian = func; 8359b94acceSBarry Smith snes->jacP = ctx; 8369b94acceSBarry Smith snes->jacobian = A; 8379b94acceSBarry Smith snes->jacobian_pre = B; 8389b94acceSBarry Smith return 0; 8399b94acceSBarry Smith } 84062fef451SLois Curfman McInnes 841b4fd4287SBarry Smith /*@ 842b4fd4287SBarry Smith SNESGetJacobian - Returns the Jacobian matrix and optionally the user 843b4fd4287SBarry Smith provided context for evaluating the Jacobian. 844b4fd4287SBarry Smith 845b4fd4287SBarry Smith Input Parameter: 846b4fd4287SBarry Smith . snes - the nonlinear solver context 847b4fd4287SBarry Smith 848b4fd4287SBarry Smith Output Parameters: 849b4fd4287SBarry Smith . A - location to stash Jacobian matrix (or PETSC_NULL) 850b4fd4287SBarry Smith . B - location to stash preconditioner matrix (or PETSC_NULL) 851b4fd4287SBarry Smith . ctx - location to stash Jacobian ctx (or PETSC_NULL) 852b4fd4287SBarry Smith 853b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian() 854b4fd4287SBarry Smith @*/ 855b4fd4287SBarry Smith int SNESGetJacobian(SNES snes,Mat *A,Mat *B, void **ctx) 856b4fd4287SBarry Smith { 85762fef451SLois Curfman McInnes if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1, 85862fef451SLois Curfman McInnes "SNESSetJacobian:For SNES_NONLINEAR_EQUATIONS only"); 859b4fd4287SBarry Smith if (A) *A = snes->jacobian; 860b4fd4287SBarry Smith if (B) *B = snes->jacobian_pre; 861b4fd4287SBarry Smith if (ctx) *ctx = snes->jacP; 862b4fd4287SBarry Smith return 0; 863b4fd4287SBarry Smith } 864b4fd4287SBarry Smith 8659b94acceSBarry Smith /*@C 8669b94acceSBarry Smith SNESSetHessian - Sets the function to compute Hessian as well as the 8679b94acceSBarry Smith location to store it. 8689b94acceSBarry Smith 8699b94acceSBarry Smith Input Parameters: 8709b94acceSBarry Smith . snes - the SNES context 8719b94acceSBarry Smith . A - Hessian matrix 8729b94acceSBarry Smith . B - preconditioner matrix (usually same as the Hessian) 8739b94acceSBarry Smith . func - Jacobian evaluation routine 8749b94acceSBarry Smith . ctx - optional user-defined context for private data for the 8759b94acceSBarry Smith Hessian evaluation routine (may be null) 8769b94acceSBarry Smith 8779b94acceSBarry Smith Calling sequence of func: 8789b94acceSBarry Smith . func (SNES,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 8799b94acceSBarry Smith 8809b94acceSBarry Smith . x - input vector 8819b94acceSBarry Smith . A - Hessian matrix 8829b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 883ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 884ac21db08SLois Curfman McInnes structure (same as flag in SLESSetOperators()) 8859b94acceSBarry Smith . ctx - optional user-defined Hessian context 8869b94acceSBarry Smith 8879b94acceSBarry Smith Notes: 888ac21db08SLois Curfman McInnes See SLESSetOperators() for information about setting the flag input 889ac21db08SLois Curfman McInnes parameter in the routine func(). Be sure to read this information! 890ac21db08SLois Curfman McInnes 8919b94acceSBarry Smith The function func() takes Mat * as the matrix arguments rather than Mat. 8929b94acceSBarry Smith This allows the Hessian evaluation routine to replace A and/or B with a 8939b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 8949b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 8959b94acceSBarry Smith throughout the global iterations. 8969b94acceSBarry Smith 8979b94acceSBarry Smith .keywords: SNES, nonlinear, set, Hessian, matrix 8989b94acceSBarry Smith 899ac21db08SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESSetGradient(), SLESSetOperators() 9009b94acceSBarry Smith @*/ 9019b94acceSBarry Smith int SNESSetHessian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*, 9029b94acceSBarry Smith MatStructure*,void*),void *ctx) 9039b94acceSBarry Smith { 90477c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 9059b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 90648d91487SBarry Smith "SNESSetHessian:For SNES_UNCONSTRAINED_MINIMIZATION only"); 9079b94acceSBarry Smith snes->computejacobian = func; 9089b94acceSBarry Smith snes->jacP = ctx; 9099b94acceSBarry Smith snes->jacobian = A; 9109b94acceSBarry Smith snes->jacobian_pre = B; 9119b94acceSBarry Smith return 0; 9129b94acceSBarry Smith } 9139b94acceSBarry Smith 91462fef451SLois Curfman McInnes /*@ 91562fef451SLois Curfman McInnes SNESGetHessian - Returns the Hessian matrix and optionally the user 91662fef451SLois Curfman McInnes provided context for evaluating the Hessian. 91762fef451SLois Curfman McInnes 91862fef451SLois Curfman McInnes Input Parameter: 91962fef451SLois Curfman McInnes . snes - the nonlinear solver context 92062fef451SLois Curfman McInnes 92162fef451SLois Curfman McInnes Output Parameters: 92262fef451SLois Curfman McInnes . A - location to stash Hessian matrix (or PETSC_NULL) 92362fef451SLois Curfman McInnes . B - location to stash preconditioner matrix (or PETSC_NULL) 92462fef451SLois Curfman McInnes . ctx - location to stash Hessian ctx (or PETSC_NULL) 92562fef451SLois Curfman McInnes 92662fef451SLois Curfman McInnes .seealso: SNESSetHessian(), SNESComputeHessian() 92762fef451SLois Curfman McInnes @*/ 92862fef451SLois Curfman McInnes int SNESGetHessian(SNES snes,Mat *A,Mat *B, void **ctx) 92962fef451SLois Curfman McInnes { 93062fef451SLois Curfman McInnes if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 93162fef451SLois Curfman McInnes "SNESSetHessian:For SNES_UNCONSTRAINED_MINIMIZATION only"); 93262fef451SLois Curfman McInnes if (A) *A = snes->jacobian; 93362fef451SLois Curfman McInnes if (B) *B = snes->jacobian_pre; 93462fef451SLois Curfman McInnes if (ctx) *ctx = snes->jacP; 93562fef451SLois Curfman McInnes return 0; 93662fef451SLois Curfman McInnes } 93762fef451SLois Curfman McInnes 9389b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 9399b94acceSBarry Smith 9409b94acceSBarry Smith /*@ 9419b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 942272ac6f2SLois Curfman McInnes of a nonlinear solver. 9439b94acceSBarry Smith 9449b94acceSBarry Smith Input Parameter: 9459b94acceSBarry Smith . snes - the SNES context 9468ddd3da0SLois Curfman McInnes . x - the solution vector 9479b94acceSBarry Smith 948272ac6f2SLois Curfman McInnes Notes: 949272ac6f2SLois Curfman McInnes For basic use of the SNES solvers the user need not explicitly call 950272ac6f2SLois Curfman McInnes SNESSetUp(), since these actions will automatically occur during 951272ac6f2SLois Curfman McInnes the call to SNESSolve(). However, if one wishes to control this 952272ac6f2SLois Curfman McInnes phase separately, SNESSetUp() should be called after SNESCreate() 953272ac6f2SLois Curfman McInnes and optional routines of the form SNESSetXXX(), but before SNESSolve(). 954272ac6f2SLois Curfman McInnes 9559b94acceSBarry Smith .keywords: SNES, nonlinear, setup 9569b94acceSBarry Smith 9579b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 9589b94acceSBarry Smith @*/ 9598ddd3da0SLois Curfman McInnes int SNESSetUp(SNES snes,Vec x) 9609b94acceSBarry Smith { 961272ac6f2SLois Curfman McInnes int ierr, flg; 96277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 96377c4ece6SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 9648ddd3da0SLois Curfman McInnes snes->vec_sol = snes->vec_sol_always = x; 9659b94acceSBarry Smith 966272ac6f2SLois Curfman McInnes ierr = OptionsHasName(snes->prefix,"-snes_mf", &flg); CHKERRQ(ierr); 96731615d04SLois Curfman McInnes if (flg) { 968272ac6f2SLois Curfman McInnes Mat J; 969272ac6f2SLois Curfman McInnes ierr = SNESDefaultMatrixFreeMatCreate(snes,snes->vec_sol,&J);CHKERRQ(ierr); 970272ac6f2SLois Curfman McInnes PLogObjectParent(snes,J); 971272ac6f2SLois Curfman McInnes snes->mfshell = J; 97283e56afdSLois Curfman McInnes if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 97383e56afdSLois Curfman McInnes ierr = SNESSetJacobian(snes,J,J,0,snes->funP); CHKERRQ(ierr); 97494a424c1SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free Jacobian routines\n"); 97583e56afdSLois Curfman McInnes } 97683e56afdSLois Curfman McInnes else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 97783e56afdSLois Curfman McInnes ierr = SNESSetHessian(snes,J,J,0,snes->funP); CHKERRQ(ierr); 97894a424c1SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free Hessian routines\n"); 97983e56afdSLois Curfman McInnes } else SETERRQ(1,"SNESSetUp:Method class doesn't support matrix-free option"); 980272ac6f2SLois Curfman McInnes } 9819b94acceSBarry Smith if ((snes->method_class == SNES_NONLINEAR_EQUATIONS)) { 98237fcc0dbSBarry Smith if (!snes->vec_func) SETERRQ(1,"SNESSetUp:Must call SNESSetFunction() first"); 98337fcc0dbSBarry Smith if (!snes->computefunction) SETERRQ(1,"SNESSetUp:Must call SNESSetFunction() first"); 98437fcc0dbSBarry Smith if (!snes->jacobian) SETERRQ(1,"SNESSetUp:Must call SNESSetJacobian() first"); 985d804570eSBarry Smith if (snes->vec_func == snes->vec_sol) SETERRQ(1,"SNESSetUp:Solution vector cannot be function vector"); 986a703fe33SLois Curfman McInnes 987a703fe33SLois Curfman McInnes /* Set the KSP stopping criterion to use the Eisenstat-Walker method */ 988a703fe33SLois Curfman McInnes if (snes->ksp_ewconv && snes->type != SNES_EQ_NTR) { 989a703fe33SLois Curfman McInnes SLES sles; KSP ksp; 990a703fe33SLois Curfman McInnes ierr = SNESGetSLES(snes,&sles); CHKERRQ(ierr); 991a703fe33SLois Curfman McInnes ierr = SLESGetKSP(sles,&ksp); CHKERRQ(ierr); 992a703fe33SLois Curfman McInnes ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private, 993a703fe33SLois Curfman McInnes (void *)snes); CHKERRQ(ierr); 994a703fe33SLois Curfman McInnes } 9959b94acceSBarry Smith } else if ((snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION)) { 99637fcc0dbSBarry Smith if (!snes->vec_func) SETERRQ(1,"SNESSetUp:Must call SNESSetGradient() first"); 99737fcc0dbSBarry Smith if (!snes->computefunction) SETERRQ(1,"SNESSetUp:Must call SNESSetGradient() first"); 99837fcc0dbSBarry Smith if (!snes->computeumfunction) 99937fcc0dbSBarry Smith SETERRQ(1,"SNESSetUp:Must call SNESSetMinimizationFunction() first"); 100037fcc0dbSBarry Smith if (!snes->jacobian) SETERRQ(1,"SNESSetUp:Must call SNESSetHessian() first"); 10019b94acceSBarry Smith } else SETERRQ(1,"SNESSetUp:Unknown method class"); 1002a703fe33SLois Curfman McInnes if (snes->setup) {ierr = (*snes->setup)(snes); CHKERRQ(ierr);} 1003a703fe33SLois Curfman McInnes snes->setup_called = 1; 1004a703fe33SLois Curfman McInnes return 0; 10059b94acceSBarry Smith } 10069b94acceSBarry Smith 10079b94acceSBarry Smith /*@C 10089b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 10099b94acceSBarry Smith with SNESCreate(). 10109b94acceSBarry Smith 10119b94acceSBarry Smith Input Parameter: 10129b94acceSBarry Smith . snes - the SNES context 10139b94acceSBarry Smith 10149b94acceSBarry Smith .keywords: SNES, nonlinear, destroy 10159b94acceSBarry Smith 101663a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve() 10179b94acceSBarry Smith @*/ 10189b94acceSBarry Smith int SNESDestroy(SNES snes) 10199b94acceSBarry Smith { 10209b94acceSBarry Smith int ierr; 102177c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 10229b94acceSBarry Smith ierr = (*(snes)->destroy)((PetscObject)snes); CHKERRQ(ierr); 10230452661fSBarry Smith if (snes->kspconvctx) PetscFree(snes->kspconvctx); 10249b94acceSBarry Smith if (snes->mfshell) MatDestroy(snes->mfshell); 10259b94acceSBarry Smith ierr = SLESDestroy(snes->sles); CHKERRQ(ierr); 10263e2e452bSBarry Smith if (snes->xmonitor) SNESLGMonitorDestroy(snes->xmonitor); 10279b94acceSBarry Smith PLogObjectDestroy((PetscObject)snes); 10280452661fSBarry Smith PetscHeaderDestroy((PetscObject)snes); 10299b94acceSBarry Smith return 0; 10309b94acceSBarry Smith } 10319b94acceSBarry Smith 10329b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 10339b94acceSBarry Smith 10349b94acceSBarry Smith /*@ 1035d7a720efSLois Curfman McInnes SNESSetTolerances - Sets various parameters used in convergence tests. 10369b94acceSBarry Smith 10379b94acceSBarry Smith Input Parameters: 10389b94acceSBarry Smith . snes - the SNES context 1039d7a720efSLois Curfman McInnes . atol - tolerance 10409b94acceSBarry Smith 10419b94acceSBarry Smith Options Database Key: 1042d7a720efSLois Curfman McInnes $ -snes_atol <atol> - absolute convergence tolerance 1043d7a720efSLois Curfman McInnes $ -snes_rtol <rtol> - relative convergence tolerance 1044d7a720efSLois Curfman McInnes $ -snes_stol <stol> - convergence tolerance in terms of 1045d7a720efSLois Curfman McInnes $ the norm of the change in the solution between steps 1046d7a720efSLois Curfman McInnes $ -snes_max_it <maxit> - maximum number of iterations 1047d7a720efSLois Curfman McInnes $ -snes_max_funcs <maxf> - maximum number of function evaluations 10489b94acceSBarry Smith 1049d7a720efSLois Curfman McInnes Notes: 10509b94acceSBarry Smith The default maximum number of iterations is 50. 10519b94acceSBarry Smith The default maximum number of function evaluations is 1000. 10529b94acceSBarry Smith 1053d7a720efSLois Curfman McInnes .keywords: SNES, nonlinear, set, absolute, convergence, tolerance 10549b94acceSBarry Smith 1055d7a720efSLois Curfman McInnes .seealso: SNESSetTrustRegionTolerance(), SNESSetMinimizationFunctionTolerance() 10569b94acceSBarry Smith @*/ 1057d7a720efSLois Curfman McInnes int SNESSetTolerances(SNES snes,double atol,double rtol,double stol,int maxit,int maxf) 10589b94acceSBarry Smith { 105977c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1060d7a720efSLois Curfman McInnes if (atol != PETSC_DEFAULT) snes->atol = atol; 1061d7a720efSLois Curfman McInnes if (rtol != PETSC_DEFAULT) snes->rtol = rtol; 1062d7a720efSLois Curfman McInnes if (stol != PETSC_DEFAULT) snes->xtol = stol; 1063d7a720efSLois Curfman McInnes if (maxit != PETSC_DEFAULT) snes->max_its = maxit; 1064d7a720efSLois Curfman McInnes if (maxf != PETSC_DEFAULT) snes->max_funcs = maxf; 10659b94acceSBarry Smith return 0; 10669b94acceSBarry Smith } 10679b94acceSBarry Smith 10689b94acceSBarry Smith /*@ 10699b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 10709b94acceSBarry Smith 10719b94acceSBarry Smith Input Parameters: 10729b94acceSBarry Smith . snes - the SNES context 10739b94acceSBarry Smith . tol - tolerance 10749b94acceSBarry Smith 10759b94acceSBarry Smith Options Database Key: 1076d7a720efSLois Curfman McInnes $ -snes_trtol <tol> 10779b94acceSBarry Smith 10789b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance 10799b94acceSBarry Smith 1080d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetMinimizationFunctionTolerance() 10819b94acceSBarry Smith @*/ 10829b94acceSBarry Smith int SNESSetTrustRegionTolerance(SNES snes,double tol) 10839b94acceSBarry Smith { 108477c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 10859b94acceSBarry Smith snes->deltatol = tol; 10869b94acceSBarry Smith return 0; 10879b94acceSBarry Smith } 10889b94acceSBarry Smith 10899b94acceSBarry Smith /*@ 109077c4ece6SBarry Smith SNESSetMinimizationFunctionTolerance - Sets the minimum allowable function tolerance 10919b94acceSBarry Smith for unconstrained minimization solvers. 10929b94acceSBarry Smith 10939b94acceSBarry Smith Input Parameters: 10949b94acceSBarry Smith . snes - the SNES context 10959b94acceSBarry Smith . ftol - minimum function tolerance 10969b94acceSBarry Smith 10979b94acceSBarry Smith Options Database Key: 1098d7a720efSLois Curfman McInnes $ -snes_fmin <ftol> 10999b94acceSBarry Smith 11009b94acceSBarry Smith Note: 110177c4ece6SBarry Smith SNESSetMinimizationFunctionTolerance() is valid for SNES_UNCONSTRAINED_MINIMIZATION 11029b94acceSBarry Smith methods only. 11039b94acceSBarry Smith 11049b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimum, convergence, function, tolerance 11059b94acceSBarry Smith 1106d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetTrustRegionTolerance() 11079b94acceSBarry Smith @*/ 110877c4ece6SBarry Smith int SNESSetMinimizationFunctionTolerance(SNES snes,double ftol) 11099b94acceSBarry Smith { 111077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 11119b94acceSBarry Smith snes->fmin = ftol; 11129b94acceSBarry Smith return 0; 11139b94acceSBarry Smith } 11149b94acceSBarry Smith 11159b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 11169b94acceSBarry Smith 11179b94acceSBarry Smith /*@C 11189b94acceSBarry Smith SNESSetMonitor - Sets the function that is to be used at every 11199b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 11209b94acceSBarry Smith progress. 11219b94acceSBarry Smith 11229b94acceSBarry Smith Input Parameters: 11239b94acceSBarry Smith . snes - the SNES context 11249b94acceSBarry Smith . func - monitoring routine 11259b94acceSBarry Smith . mctx - optional user-defined context for private data for the 11269b94acceSBarry Smith monitor routine (may be null) 11279b94acceSBarry Smith 11289b94acceSBarry Smith Calling sequence of func: 1129682d7d0cSBarry Smith int func(SNES snes,int its, Vec x,Vec f,double norm,void *mctx) 11309b94acceSBarry Smith 11319b94acceSBarry Smith $ snes - the SNES context 11329b94acceSBarry Smith $ its - iteration number 11339b94acceSBarry Smith $ mctx - optional monitoring context 11349b94acceSBarry Smith $ 11359b94acceSBarry Smith $ SNES_NONLINEAR_EQUATIONS methods: 11369b94acceSBarry Smith $ norm - 2-norm function value (may be estimated) 11379b94acceSBarry Smith $ 11389b94acceSBarry Smith $ SNES_UNCONSTRAINED_MINIMIZATION methods: 11399b94acceSBarry Smith $ norm - 2-norm gradient value (may be estimated) 11409b94acceSBarry Smith 11419b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor 11429b94acceSBarry Smith 11439b94acceSBarry Smith .seealso: SNESDefaultMonitor() 11449b94acceSBarry Smith @*/ 11459b94acceSBarry Smith int SNESSetMonitor( SNES snes, int (*func)(SNES,int,double,void*), 11469b94acceSBarry Smith void *mctx ) 11479b94acceSBarry Smith { 11489b94acceSBarry Smith snes->monitor = func; 11499b94acceSBarry Smith snes->monP = (void*)mctx; 11509b94acceSBarry Smith return 0; 11519b94acceSBarry Smith } 11529b94acceSBarry Smith 11539b94acceSBarry Smith /*@C 11549b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 11559b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 11569b94acceSBarry Smith 11579b94acceSBarry Smith Input Parameters: 11589b94acceSBarry Smith . snes - the SNES context 11599b94acceSBarry Smith . func - routine to test for convergence 11609b94acceSBarry Smith . cctx - optional context for private data for the convergence routine 11619b94acceSBarry Smith (may be null) 11629b94acceSBarry Smith 11639b94acceSBarry Smith Calling sequence of func: 11649b94acceSBarry Smith int func (SNES snes,double xnorm,double gnorm, 11659b94acceSBarry Smith double f,void *cctx) 11669b94acceSBarry Smith 11679b94acceSBarry Smith $ snes - the SNES context 11689b94acceSBarry Smith $ cctx - optional convergence context 11699b94acceSBarry Smith $ xnorm - 2-norm of current iterate 11709b94acceSBarry Smith $ 11719b94acceSBarry Smith $ SNES_NONLINEAR_EQUATIONS methods: 11729b94acceSBarry Smith $ gnorm - 2-norm of current step 11739b94acceSBarry Smith $ f - 2-norm of function 11749b94acceSBarry Smith $ 11759b94acceSBarry Smith $ SNES_UNCONSTRAINED_MINIMIZATION methods: 11769b94acceSBarry Smith $ gnorm - 2-norm of current gradient 11779b94acceSBarry Smith $ f - function value 11789b94acceSBarry Smith 11799b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test 11809b94acceSBarry Smith 11819b94acceSBarry Smith .seealso: SNESDefaultConverged() 11829b94acceSBarry Smith @*/ 11839b94acceSBarry Smith int SNESSetConvergenceTest(SNES snes, 11849b94acceSBarry Smith int (*func)(SNES,double,double,double,void*),void *cctx) 11859b94acceSBarry Smith { 11869b94acceSBarry Smith (snes)->converged = func; 11879b94acceSBarry Smith (snes)->cnvP = cctx; 11889b94acceSBarry Smith return 0; 11899b94acceSBarry Smith } 11909b94acceSBarry Smith 11919b94acceSBarry Smith /* 11929b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 11939b94acceSBarry Smith positive parameter delta. 11949b94acceSBarry Smith 11959b94acceSBarry Smith Input Parameters: 11969b94acceSBarry Smith . snes - the SNES context 11979b94acceSBarry Smith . y - approximate solution of linear system 11989b94acceSBarry Smith . fnorm - 2-norm of current function 11999b94acceSBarry Smith . delta - trust region size 12009b94acceSBarry Smith 12019b94acceSBarry Smith Output Parameters: 12029b94acceSBarry Smith . gpnorm - predicted function norm at the new point, assuming local 12039b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 12049b94acceSBarry Smith region, and exceeds zero otherwise. 12059b94acceSBarry Smith . ynorm - 2-norm of the step 12069b94acceSBarry Smith 12079b94acceSBarry Smith Note: 1208ddd12767SBarry Smith For non-trust region methods such as SNES_EQ_NLS, the parameter delta 12099b94acceSBarry Smith is set to be the maximum allowable step size. 12109b94acceSBarry Smith 12119b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step 12129b94acceSBarry Smith */ 12139b94acceSBarry Smith int SNESScaleStep_Private(SNES snes,Vec y,double *fnorm,double *delta, 12149b94acceSBarry Smith double *gpnorm,double *ynorm) 12159b94acceSBarry Smith { 12169b94acceSBarry Smith double norm; 12179b94acceSBarry Smith Scalar cnorm; 1218cddf8d76SBarry Smith VecNorm(y,NORM_2, &norm ); 12199b94acceSBarry Smith if (norm > *delta) { 12209b94acceSBarry Smith norm = *delta/norm; 12219b94acceSBarry Smith *gpnorm = (1.0 - norm)*(*fnorm); 12229b94acceSBarry Smith cnorm = norm; 12239b94acceSBarry Smith VecScale( &cnorm, y ); 12249b94acceSBarry Smith *ynorm = *delta; 12259b94acceSBarry Smith } else { 12269b94acceSBarry Smith *gpnorm = 0.0; 12279b94acceSBarry Smith *ynorm = norm; 12289b94acceSBarry Smith } 12299b94acceSBarry Smith return 0; 12309b94acceSBarry Smith } 12319b94acceSBarry Smith 12329b94acceSBarry Smith /*@ 12339b94acceSBarry Smith SNESSolve - Solves a nonlinear system. Call SNESSolve after calling 123463a78c88SLois Curfman McInnes SNESCreate() and optional routines of the form SNESSetXXX(). 12359b94acceSBarry Smith 12369b94acceSBarry Smith Input Parameter: 12379b94acceSBarry Smith . snes - the SNES context 12388ddd3da0SLois Curfman McInnes . x - the solution vector 12399b94acceSBarry Smith 12409b94acceSBarry Smith Output Parameter: 12419b94acceSBarry Smith its - number of iterations until termination 12429b94acceSBarry Smith 12438ddd3da0SLois Curfman McInnes Note: 12448ddd3da0SLois Curfman McInnes The user should initialize the vector, x, with the initial guess 12458ddd3da0SLois Curfman McInnes for the nonlinear solve prior to calling SNESSolve. In particular, 12468ddd3da0SLois Curfman McInnes to employ an initial guess of zero, the user should explicitly set 12478ddd3da0SLois Curfman McInnes this vector to zero by calling VecSet(). 12488ddd3da0SLois Curfman McInnes 12499b94acceSBarry Smith .keywords: SNES, nonlinear, solve 12509b94acceSBarry Smith 125163a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESDestroy() 12529b94acceSBarry Smith @*/ 12538ddd3da0SLois Curfman McInnes int SNESSolve(SNES snes,Vec x,int *its) 12549b94acceSBarry Smith { 12553c7409f5SSatish Balay int ierr, flg; 1256052efed2SBarry Smith 125777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1258c4fc05e7SBarry Smith if (!snes->setup_called) {ierr = SNESSetUp(snes,x); CHKERRQ(ierr);} 1259c4fc05e7SBarry Smith else {snes->vec_sol = snes->vec_sol_always = x;} 12609b94acceSBarry Smith PLogEventBegin(SNES_Solve,snes,0,0,0); 12619b94acceSBarry Smith ierr = (*(snes)->solve)(snes,its); CHKERRQ(ierr); 12629b94acceSBarry Smith PLogEventEnd(SNES_Solve,snes,0,0,0); 12633c7409f5SSatish Balay ierr = OptionsHasName(PETSC_NULL,"-snes_view", &flg); CHKERRQ(ierr); 12643c7409f5SSatish Balay if (flg) { ierr = SNESView(snes,STDOUT_VIEWER_WORLD); CHKERRQ(ierr); } 12659b94acceSBarry Smith return 0; 12669b94acceSBarry Smith } 12679b94acceSBarry Smith 12689b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 126937fcc0dbSBarry Smith static NRList *__SNESList = 0; 12709b94acceSBarry Smith 12719b94acceSBarry Smith /*@ 12724b0e389bSBarry Smith SNESSetType - Sets the method for the nonlinear solver. 12739b94acceSBarry Smith 12749b94acceSBarry Smith Input Parameters: 12759b94acceSBarry Smith . snes - the SNES context 12769b94acceSBarry Smith . method - a known method 12779b94acceSBarry Smith 12789b94acceSBarry Smith Notes: 12799b94acceSBarry Smith See "petsc/include/snes.h" for available methods (for instance) 12809b94acceSBarry Smith $ Systems of nonlinear equations: 1281ddd12767SBarry Smith $ SNES_EQ_NLS - Newton's method with line search 1282ddd12767SBarry Smith $ SNES_EQ_NTR - Newton's method with trust region 12839b94acceSBarry Smith $ Unconstrained minimization: 12849b94acceSBarry Smith $ SNES_UM_NTR - Newton's method with trust region 1285a703fe33SLois Curfman McInnes $ SNES_UM_NLS - Newton's method with line search 12869b94acceSBarry Smith 12879b94acceSBarry Smith Options Database Command: 12884b0e389bSBarry Smith $ -snes_type <method> 12899b94acceSBarry Smith $ Use -help for a list of available methods 12909b94acceSBarry Smith $ (for instance, ls or tr) 1291a703fe33SLois Curfman McInnes 1292a703fe33SLois Curfman McInnes .keysords: SNES, set, method 12939b94acceSBarry Smith @*/ 12944b0e389bSBarry Smith int SNESSetType(SNES snes,SNESType method) 12959b94acceSBarry Smith { 12969b94acceSBarry Smith int (*r)(SNES); 129777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 12989b94acceSBarry Smith /* Get the function pointers for the iterative method requested */ 129937fcc0dbSBarry Smith if (!__SNESList) {SNESRegisterAll();} 130037fcc0dbSBarry Smith if (!__SNESList) {SETERRQ(1,"SNESSetType:Could not get methods");} 130137fcc0dbSBarry Smith r = (int (*)(SNES))NRFindRoutine( __SNESList, (int)method, (char *)0 ); 13024b0e389bSBarry Smith if (!r) {SETERRQ(1,"SNESSetType:Unknown method");} 13030452661fSBarry Smith if (snes->data) PetscFree(snes->data); 13049b94acceSBarry Smith snes->set_method_called = 1; 13059b94acceSBarry Smith return (*r)(snes); 13069b94acceSBarry Smith } 13079b94acceSBarry Smith 13089b94acceSBarry Smith /* --------------------------------------------------------------------- */ 13099b94acceSBarry Smith /*@C 13109b94acceSBarry Smith SNESRegister - Adds the method to the nonlinear solver package, given 13114b0e389bSBarry Smith a function pointer and a nonlinear solver name of the type SNESType. 13129b94acceSBarry Smith 13139b94acceSBarry Smith Input Parameters: 1314ddd12767SBarry Smith . name - for instance SNES_EQ_NLS, SNES_EQ_NTR, ... 13159b94acceSBarry Smith . sname - corfunPonding string for name 13169b94acceSBarry Smith . create - routine to create method context 13179b94acceSBarry Smith 13189b94acceSBarry Smith .keywords: SNES, nonlinear, register 13199b94acceSBarry Smith 13209b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterDestroy() 13219b94acceSBarry Smith @*/ 13229b94acceSBarry Smith int SNESRegister(int name, char *sname, int (*create)(SNES)) 13239b94acceSBarry Smith { 13249b94acceSBarry Smith int ierr; 132537fcc0dbSBarry Smith if (!__SNESList) {ierr = NRCreate(&__SNESList); CHKERRQ(ierr);} 132637fcc0dbSBarry Smith NRRegister( __SNESList, name, sname, (int (*)(void*))create ); 13279b94acceSBarry Smith return 0; 13289b94acceSBarry Smith } 13299b94acceSBarry Smith /* --------------------------------------------------------------------- */ 13309b94acceSBarry Smith /*@C 13319b94acceSBarry Smith SNESRegisterDestroy - Frees the list of nonlinear solvers that were 13329b94acceSBarry Smith registered by SNESRegister(). 13339b94acceSBarry Smith 13349b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy 13359b94acceSBarry Smith 13369b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll() 13379b94acceSBarry Smith @*/ 13389b94acceSBarry Smith int SNESRegisterDestroy() 13399b94acceSBarry Smith { 134037fcc0dbSBarry Smith if (__SNESList) { 134137fcc0dbSBarry Smith NRDestroy( __SNESList ); 134237fcc0dbSBarry Smith __SNESList = 0; 13439b94acceSBarry Smith } 13449b94acceSBarry Smith return 0; 13459b94acceSBarry Smith } 13469b94acceSBarry Smith 13479b94acceSBarry Smith /* 13484b0e389bSBarry Smith SNESGetTypeFromOptions_Private - Sets the selected method from the 13499b94acceSBarry Smith options database. 13509b94acceSBarry Smith 13519b94acceSBarry Smith Input Parameter: 13529b94acceSBarry Smith . ctx - the SNES context 13539b94acceSBarry Smith 13549b94acceSBarry Smith Output Parameter: 13559b94acceSBarry Smith . method - solver method 13569b94acceSBarry Smith 13579b94acceSBarry Smith Returns: 13589b94acceSBarry Smith Returns 1 if the method is found; 0 otherwise. 13599b94acceSBarry Smith 13609b94acceSBarry Smith Options Database Key: 13614b0e389bSBarry Smith $ -snes_type method 13629b94acceSBarry Smith */ 1363052efed2SBarry Smith int SNESGetTypeFromOptions_Private(SNES ctx,SNESType *method,int *flg) 13649b94acceSBarry Smith { 1365052efed2SBarry Smith int ierr; 13669b94acceSBarry Smith char sbuf[50]; 1367052efed2SBarry Smith ierr = OptionsGetString(ctx->prefix,"-snes_type", sbuf, 50, flg); CHKERRQ(ierr); 1368052efed2SBarry Smith if (*flg) { 1369052efed2SBarry Smith if (!__SNESList) {ierr = SNESRegisterAll(); CHKERRQ(ierr);} 137037fcc0dbSBarry Smith *method = (SNESType)NRFindID( __SNESList, sbuf ); 13719b94acceSBarry Smith } 13729b94acceSBarry Smith return 0; 13739b94acceSBarry Smith } 13749b94acceSBarry Smith 13759b94acceSBarry Smith /*@C 13769a28b0a6SLois Curfman McInnes SNESGetType - Gets the SNES method type and name (as a string). 13779b94acceSBarry Smith 13789b94acceSBarry Smith Input Parameter: 13794b0e389bSBarry Smith . snes - nonlinear solver context 13809b94acceSBarry Smith 13819b94acceSBarry Smith Output Parameter: 13829a28b0a6SLois Curfman McInnes . method - SNES method (or use PETSC_NULL) 13839a28b0a6SLois Curfman McInnes . name - name of SNES method (or use PETSC_NULL) 13849b94acceSBarry Smith 13859b94acceSBarry Smith .keywords: SNES, nonlinear, get, method, name 13869b94acceSBarry Smith @*/ 13874b0e389bSBarry Smith int SNESGetType(SNES snes, SNESType *method,char **name) 13889b94acceSBarry Smith { 138906a9b0adSLois Curfman McInnes int ierr; 139037fcc0dbSBarry Smith if (!__SNESList) {ierr = SNESRegisterAll(); CHKERRQ(ierr);} 13914b0e389bSBarry Smith if (method) *method = (SNESType) snes->type; 139237fcc0dbSBarry Smith if (name) *name = NRFindName( __SNESList, (int) snes->type ); 13939b94acceSBarry Smith return 0; 13949b94acceSBarry Smith } 13959b94acceSBarry Smith 13969b94acceSBarry Smith #include <stdio.h> 13979b94acceSBarry Smith /* 13984b0e389bSBarry Smith SNESPrintTypes_Private - Prints the SNES methods available from the 13999b94acceSBarry Smith options database. 14009b94acceSBarry Smith 14019b94acceSBarry Smith Input Parameters: 140233455573SSatish Balay . comm - communicator (usually MPI_COMM_WORLD) 14039b94acceSBarry Smith . prefix - prefix (usually "-") 14044b0e389bSBarry Smith . name - the options database name (by default "snes_type") 14059b94acceSBarry Smith */ 140633455573SSatish Balay int SNESPrintTypes_Private(MPI_Comm comm,char* prefix,char *name) 14079b94acceSBarry Smith { 14089b94acceSBarry Smith FuncList *entry; 140937fcc0dbSBarry Smith if (!__SNESList) {SNESRegisterAll();} 141037fcc0dbSBarry Smith entry = __SNESList->head; 141177c4ece6SBarry Smith PetscPrintf(comm," %s%s (one of)",prefix,name); 14129b94acceSBarry Smith while (entry) { 141377c4ece6SBarry Smith PetscPrintf(comm," %s",entry->name); 14149b94acceSBarry Smith entry = entry->next; 14159b94acceSBarry Smith } 141677c4ece6SBarry Smith PetscPrintf(comm,"\n"); 14179b94acceSBarry Smith return 0; 14189b94acceSBarry Smith } 14199b94acceSBarry Smith 14209b94acceSBarry Smith /*@C 14219b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 14229b94acceSBarry Smith stored. 14239b94acceSBarry Smith 14249b94acceSBarry Smith Input Parameter: 14259b94acceSBarry Smith . snes - the SNES context 14269b94acceSBarry Smith 14279b94acceSBarry Smith Output Parameter: 14289b94acceSBarry Smith . x - the solution 14299b94acceSBarry Smith 14309b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution 14319b94acceSBarry Smith 1432a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESGetGradient(), SNESGetSolutionUpdate() 14339b94acceSBarry Smith @*/ 14349b94acceSBarry Smith int SNESGetSolution(SNES snes,Vec *x) 14359b94acceSBarry Smith { 143677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 14379b94acceSBarry Smith *x = snes->vec_sol_always; 14389b94acceSBarry Smith return 0; 14399b94acceSBarry Smith } 14409b94acceSBarry Smith 14419b94acceSBarry Smith /*@C 14429b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 14439b94acceSBarry Smith stored. 14449b94acceSBarry Smith 14459b94acceSBarry Smith Input Parameter: 14469b94acceSBarry Smith . snes - the SNES context 14479b94acceSBarry Smith 14489b94acceSBarry Smith Output Parameter: 14499b94acceSBarry Smith . x - the solution update 14509b94acceSBarry Smith 14519b94acceSBarry Smith Notes: 14529b94acceSBarry Smith This vector is implementation dependent. 14539b94acceSBarry Smith 14549b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update 14559b94acceSBarry Smith 14569b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction 14579b94acceSBarry Smith @*/ 14589b94acceSBarry Smith int SNESGetSolutionUpdate(SNES snes,Vec *x) 14599b94acceSBarry Smith { 146077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 14619b94acceSBarry Smith *x = snes->vec_sol_update_always; 14629b94acceSBarry Smith return 0; 14639b94acceSBarry Smith } 14649b94acceSBarry Smith 14659b94acceSBarry Smith /*@C 14663638b69dSLois Curfman McInnes SNESGetFunction - Returns the vector where the function is stored. 14679b94acceSBarry Smith 14689b94acceSBarry Smith Input Parameter: 14699b94acceSBarry Smith . snes - the SNES context 14709b94acceSBarry Smith 14719b94acceSBarry Smith Output Parameter: 14723638b69dSLois Curfman McInnes . r - the function 14739b94acceSBarry Smith 14749b94acceSBarry Smith Notes: 14759b94acceSBarry Smith SNESGetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only 14769b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 14779b94acceSBarry Smith SNESGetMinimizationFunction() and SNESGetGradient(); 14789b94acceSBarry Smith 1479a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function 14809b94acceSBarry Smith 148131615d04SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution(), SNESGetMinimizationFunction(), 148231615d04SLois Curfman McInnes SNESGetGradient() 14839b94acceSBarry Smith @*/ 14849b94acceSBarry Smith int SNESGetFunction(SNES snes,Vec *r) 14859b94acceSBarry Smith { 148677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 14879b94acceSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1, 148848d91487SBarry Smith "SNESGetFunction:For SNES_NONLINEAR_EQUATIONS only"); 14899b94acceSBarry Smith *r = snes->vec_func_always; 14909b94acceSBarry Smith return 0; 14919b94acceSBarry Smith } 14929b94acceSBarry Smith 14939b94acceSBarry Smith /*@C 14943638b69dSLois Curfman McInnes SNESGetGradient - Returns the vector where the gradient is stored. 14959b94acceSBarry Smith 14969b94acceSBarry Smith Input Parameter: 14979b94acceSBarry Smith . snes - the SNES context 14989b94acceSBarry Smith 14999b94acceSBarry Smith Output Parameter: 15009b94acceSBarry Smith . r - the gradient 15019b94acceSBarry Smith 15029b94acceSBarry Smith Notes: 15039b94acceSBarry Smith SNESGetGradient() is valid for SNES_UNCONSTRAINED_MINIMIZATION methods 15049b94acceSBarry Smith only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 15059b94acceSBarry Smith SNESGetFunction(). 15069b94acceSBarry Smith 15079b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient 15089b94acceSBarry Smith 150931615d04SLois Curfman McInnes .seealso: SNESGetMinimizationFunction(), SNESGetSolution(), SNESGetFunction() 15109b94acceSBarry Smith @*/ 15119b94acceSBarry Smith int SNESGetGradient(SNES snes,Vec *r) 15129b94acceSBarry Smith { 151377c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 15149b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 151548d91487SBarry Smith "SNESGetGradient:For SNES_UNCONSTRAINED_MINIMIZATION only"); 15169b94acceSBarry Smith *r = snes->vec_func_always; 15179b94acceSBarry Smith return 0; 15189b94acceSBarry Smith } 15199b94acceSBarry Smith 15209b94acceSBarry Smith /*@ 15219b94acceSBarry Smith SNESGetMinimizationFunction - Returns the scalar function value for 15229b94acceSBarry Smith unconstrained minimization problems. 15239b94acceSBarry Smith 15249b94acceSBarry Smith Input Parameter: 15259b94acceSBarry Smith . snes - the SNES context 15269b94acceSBarry Smith 15279b94acceSBarry Smith Output Parameter: 15289b94acceSBarry Smith . r - the function 15299b94acceSBarry Smith 15309b94acceSBarry Smith Notes: 15319b94acceSBarry Smith SNESGetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 15329b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 15339b94acceSBarry Smith SNESGetFunction(). 15349b94acceSBarry Smith 15359b94acceSBarry Smith .keywords: SNES, nonlinear, get, function 15369b94acceSBarry Smith 153731615d04SLois Curfman McInnes .seealso: SNESGetGradient(), SNESGetSolution(), SNESGetFunction() 15389b94acceSBarry Smith @*/ 15399b94acceSBarry Smith int SNESGetMinimizationFunction(SNES snes,double *r) 15409b94acceSBarry Smith { 154177c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 15429b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 154348d91487SBarry Smith "SNESGetMinimizationFunction:For SNES_UNCONSTRAINED_MINIMIZATION only"); 15449b94acceSBarry Smith *r = snes->fc; 15459b94acceSBarry Smith return 0; 15469b94acceSBarry Smith } 15479b94acceSBarry Smith 15483c7409f5SSatish Balay /*@C 15493c7409f5SSatish Balay SNESSetOptionsPrefix - Sets the prefix used for searching for all 15503c7409f5SSatish Balay SNES options in the database. 15513c7409f5SSatish Balay 15523c7409f5SSatish Balay Input Parameter: 15533c7409f5SSatish Balay . snes - the SNES context 15543c7409f5SSatish Balay . prefix - the prefix to prepend to all option names 15553c7409f5SSatish Balay 15563c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database 1557a86d99e1SLois Curfman McInnes 1558a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions() 15593c7409f5SSatish Balay @*/ 15603c7409f5SSatish Balay int SNESSetOptionsPrefix(SNES snes,char *prefix) 15613c7409f5SSatish Balay { 15623c7409f5SSatish Balay int ierr; 15633c7409f5SSatish Balay 156477c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 15653c7409f5SSatish Balay ierr = PetscObjectSetPrefix((PetscObject)snes, prefix); CHKERRQ(ierr); 15663c7409f5SSatish Balay ierr = SLESSetOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr); 15673c7409f5SSatish Balay return 0; 15683c7409f5SSatish Balay } 15693c7409f5SSatish Balay 15703c7409f5SSatish Balay /*@C 15713c7409f5SSatish Balay SNESAppendOptionsPrefix - Append to the prefix used for searching for all 15723c7409f5SSatish Balay SNES options in the database. 15733c7409f5SSatish Balay 15743c7409f5SSatish Balay Input Parameter: 15753c7409f5SSatish Balay . snes - the SNES context 15763c7409f5SSatish Balay . prefix - the prefix to prepend to all option names 15773c7409f5SSatish Balay 15783c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database 1579a86d99e1SLois Curfman McInnes 1580a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix() 15813c7409f5SSatish Balay @*/ 15823c7409f5SSatish Balay int SNESAppendOptionsPrefix(SNES snes,char *prefix) 15833c7409f5SSatish Balay { 15843c7409f5SSatish Balay int ierr; 15853c7409f5SSatish Balay 158677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 15873c7409f5SSatish Balay ierr = PetscObjectAppendPrefix((PetscObject)snes, prefix); CHKERRQ(ierr); 15883c7409f5SSatish Balay ierr = SLESAppendOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr); 15893c7409f5SSatish Balay return 0; 15903c7409f5SSatish Balay } 15913c7409f5SSatish Balay 1592c83590e2SSatish Balay /*@ 15933c7409f5SSatish Balay SNESGetOptionsPrefix - Sets the prefix used for searching for all 15943c7409f5SSatish Balay SNES options in the database. 15953c7409f5SSatish Balay 15963c7409f5SSatish Balay Input Parameter: 15973c7409f5SSatish Balay . snes - the SNES context 15983c7409f5SSatish Balay 15993c7409f5SSatish Balay Output Parameter: 16003c7409f5SSatish Balay . prefix - pointer to the prefix string used 16013c7409f5SSatish Balay 16023c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database 1603a86d99e1SLois Curfman McInnes 1604a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix() 16053c7409f5SSatish Balay @*/ 16063c7409f5SSatish Balay int SNESGetOptionsPrefix(SNES snes,char **prefix) 16073c7409f5SSatish Balay { 16083c7409f5SSatish Balay int ierr; 16093c7409f5SSatish Balay 161077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 16113c7409f5SSatish Balay ierr = PetscObjectGetPrefix((PetscObject)snes, prefix); CHKERRQ(ierr); 16123c7409f5SSatish Balay return 0; 16133c7409f5SSatish Balay } 16143c7409f5SSatish Balay 16153c7409f5SSatish Balay 16169b94acceSBarry Smith 16179b94acceSBarry Smith 16189b94acceSBarry Smith 1619