19b94acceSBarry Smith #ifndef lint 2*70f55243SBarry Smith static char vcid[] = "$Id: snes.c,v 1.77 1996/07/22 17:11:37 bsmith Exp bsmith $"; 39b94acceSBarry Smith #endif 49b94acceSBarry Smith 59b94acceSBarry Smith #include "draw.h" /*I "draw.h" I*/ 6*70f55243SBarry Smith #include "src/snes/snesimpl.h" /*I "snes.h" I*/ 7f5eb4b81SSatish Balay #include "src/sys/nreg.h" 89b94acceSBarry Smith #include "pinclude/pviewer.h" 99b94acceSBarry Smith #include <math.h> 109b94acceSBarry Smith 11052efed2SBarry Smith extern int SNESGetTypeFromOptions_Private(SNES,SNESType*,int*); 1233455573SSatish Balay extern int SNESPrintTypes_Private(MPI_Comm,char*,char*); 139b94acceSBarry Smith 149b94acceSBarry Smith /*@ 159b94acceSBarry Smith SNESView - Prints the SNES data structure. 169b94acceSBarry Smith 179b94acceSBarry Smith Input Parameters: 189b94acceSBarry Smith . SNES - the SNES context 199b94acceSBarry Smith . viewer - visualization context 209b94acceSBarry Smith 219b94acceSBarry Smith Options Database Key: 229b94acceSBarry Smith $ -snes_view : calls SNESView() at end of SNESSolve() 239b94acceSBarry Smith 249b94acceSBarry Smith Notes: 259b94acceSBarry Smith The available visualization contexts include 266d4a8577SBarry Smith $ VIEWER_STDOUT_SELF - standard output (default) 276d4a8577SBarry Smith $ VIEWER_STDOUT_WORLD - synchronized standard 289b94acceSBarry Smith $ output where only the first processor opens 299b94acceSBarry Smith $ the file. All other processors send their 309b94acceSBarry Smith $ data to the first processor to print. 319b94acceSBarry Smith 329b94acceSBarry Smith The user can open alternative vistualization contexts with 33dbb450caSBarry Smith $ ViewerFileOpenASCII() - output to a specified file 349b94acceSBarry Smith 359b94acceSBarry Smith .keywords: SNES, view 369b94acceSBarry Smith 37dbb450caSBarry Smith .seealso: ViewerFileOpenASCII() 389b94acceSBarry Smith @*/ 399b94acceSBarry Smith int SNESView(SNES snes,Viewer viewer) 409b94acceSBarry Smith { 419b94acceSBarry Smith SNES_KSP_EW_ConvCtx *kctx; 429b94acceSBarry Smith FILE *fd; 439b94acceSBarry Smith int ierr; 449b94acceSBarry Smith SLES sles; 459b94acceSBarry Smith char *method; 4619bcc07fSBarry Smith ViewerType vtype; 479b94acceSBarry Smith 4874679c65SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 4974679c65SBarry Smith if (viewer) {PetscValidHeader(viewer);} 5074679c65SBarry Smith else { viewer = VIEWER_STDOUT_SELF; } 5174679c65SBarry Smith 5219bcc07fSBarry Smith ierr = ViewerGetType(viewer,&vtype); CHKERRQ(ierr); 5319bcc07fSBarry Smith if (vtype == ASCII_FILE_VIEWER || vtype == ASCII_FILES_VIEWER) { 5490ace30eSBarry Smith ierr = ViewerASCIIGetPointer(viewer,&fd); CHKERRQ(ierr); 5577c4ece6SBarry Smith PetscFPrintf(snes->comm,fd,"SNES Object:\n"); 564b0e389bSBarry Smith SNESGetType(snes,PETSC_NULL,&method); 5777c4ece6SBarry Smith PetscFPrintf(snes->comm,fd," method: %s\n",method); 589b94acceSBarry Smith if (snes->view) (*snes->view)((PetscObject)snes,viewer); 5977c4ece6SBarry Smith PetscFPrintf(snes->comm,fd, 609b94acceSBarry Smith " maximum iterations=%d, maximum function evaluations=%d\n", 619b94acceSBarry Smith snes->max_its,snes->max_funcs); 6277c4ece6SBarry Smith PetscFPrintf(snes->comm,fd, 639b94acceSBarry Smith " tolerances: relative=%g, absolute=%g, truncation=%g, solution=%g\n", 649b94acceSBarry Smith snes->rtol, snes->atol, snes->trunctol, snes->xtol); 659b94acceSBarry Smith if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) 6677c4ece6SBarry Smith PetscFPrintf(snes->comm,fd," min function tolerance=%g\n",snes->fmin); 679b94acceSBarry Smith if (snes->ksp_ewconv) { 689b94acceSBarry Smith kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx; 699b94acceSBarry Smith if (kctx) { 7077c4ece6SBarry Smith PetscFPrintf(snes->comm,fd, 719b94acceSBarry Smith " Eisenstat-Walker computation of KSP relative tolerance (version %d)\n", 729b94acceSBarry Smith kctx->version); 7377c4ece6SBarry Smith PetscFPrintf(snes->comm,fd, 749b94acceSBarry Smith " rtol_0=%g, rtol_max=%g, threshold=%g\n",kctx->rtol_0, 759b94acceSBarry Smith kctx->rtol_max,kctx->threshold); 7677c4ece6SBarry Smith PetscFPrintf(snes->comm,fd," gamma=%g, alpha=%g, alpha2=%g\n", 779b94acceSBarry Smith kctx->gamma,kctx->alpha,kctx->alpha2); 789b94acceSBarry Smith } 799b94acceSBarry Smith } 80c30f285eSLois Curfman McInnes } else if (vtype == STRING_VIEWER) { 81c30f285eSLois Curfman McInnes SNESGetType(snes,PETSC_NULL,&method); 82c30f285eSLois Curfman McInnes ViewerStringSPrintf(viewer," %-3.3s",method); 8319bcc07fSBarry Smith } 849b94acceSBarry Smith SNESGetSLES(snes,&sles); 859b94acceSBarry Smith ierr = SLESView(sles,viewer); CHKERRQ(ierr); 869b94acceSBarry Smith return 0; 879b94acceSBarry Smith } 889b94acceSBarry Smith 899b94acceSBarry Smith /*@ 90682d7d0cSBarry Smith SNESSetFromOptions - Sets various SNES and SLES parameters from user options. 919b94acceSBarry Smith 929b94acceSBarry Smith Input Parameter: 939b94acceSBarry Smith . snes - the SNES context 949b94acceSBarry Smith 959b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database 969b94acceSBarry Smith 97a86d99e1SLois Curfman McInnes .seealso: SNESPrintHelp(), SNESSetOptionsPrefix() 989b94acceSBarry Smith @*/ 999b94acceSBarry Smith int SNESSetFromOptions(SNES snes) 1009b94acceSBarry Smith { 1014b0e389bSBarry Smith SNESType method; 1029b94acceSBarry Smith double tmp; 1039b94acceSBarry Smith SLES sles; 1043c7409f5SSatish Balay int ierr, flg; 1053c7409f5SSatish Balay int version = PETSC_DEFAULT; 1069b94acceSBarry Smith double rtol_0 = PETSC_DEFAULT; 1079b94acceSBarry Smith double rtol_max = PETSC_DEFAULT; 1089b94acceSBarry Smith double gamma2 = PETSC_DEFAULT; 1099b94acceSBarry Smith double alpha = PETSC_DEFAULT; 1109b94acceSBarry Smith double alpha2 = PETSC_DEFAULT; 1119b94acceSBarry Smith double threshold = PETSC_DEFAULT; 1129b94acceSBarry Smith 11377c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 11474679c65SBarry Smith if (snes->setup_called) SETERRQ(1,"SNESSetFromOptions:Must call prior to SNESSetUp"); 115052efed2SBarry Smith ierr = SNESGetTypeFromOptions_Private(snes,&method,&flg); CHKERRQ(ierr); 116052efed2SBarry Smith if (flg) { 117052efed2SBarry Smith ierr = SNESSetType(snes,method); CHKERRQ(ierr); 1189b94acceSBarry Smith } 1195334005bSBarry Smith else if (!snes->set_method_called) { 1205334005bSBarry Smith if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 12140191667SLois Curfman McInnes ierr = SNESSetType(snes,SNES_EQ_LS); CHKERRQ(ierr); 1225334005bSBarry Smith } 1235334005bSBarry Smith else { 12440191667SLois Curfman McInnes ierr = SNESSetType(snes,SNES_UM_TR); CHKERRQ(ierr); 1255334005bSBarry Smith } 1265334005bSBarry Smith } 1275334005bSBarry Smith 1283c7409f5SSatish Balay ierr = OptionsHasName(PETSC_NULL,"-help", &flg); CHKERRQ(ierr); 1293c7409f5SSatish Balay if (flg) { SNESPrintHelp(snes); } 1303c7409f5SSatish Balay ierr = OptionsGetDouble(snes->prefix,"-snes_stol",&tmp, &flg); CHKERRQ(ierr); 131d7a720efSLois Curfman McInnes if (flg) {SNESSetTolerances(snes,PETSC_DEFAULT,PETSC_DEFAULT,tmp,PETSC_DEFAULT,PETSC_DEFAULT);} 1323c7409f5SSatish Balay ierr = OptionsGetDouble(snes->prefix,"-snes_atol",&tmp, &flg); CHKERRQ(ierr); 133d7a720efSLois Curfman McInnes if (flg) {SNESSetTolerances(snes,tmp,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);} 1343c7409f5SSatish Balay ierr = OptionsGetDouble(snes->prefix,"-snes_rtol",&tmp, &flg); CHKERRQ(ierr); 135d7a720efSLois Curfman McInnes if (flg) {SNESSetTolerances(snes,PETSC_DEFAULT,tmp,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT); } 1363c7409f5SSatish Balay ierr = OptionsGetInt(snes->prefix,"-snes_max_it",&snes->max_its, &flg); CHKERRQ(ierr); 1373c7409f5SSatish Balay ierr = OptionsGetInt(snes->prefix,"-snes_max_funcs",&snes->max_funcs, &flg);CHKERRQ(ierr); 138d7a720efSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_trtol",&tmp, &flg); CHKERRQ(ierr); 139d7a720efSLois Curfman McInnes if (flg) { SNESSetTrustRegionTolerance(snes,tmp); } 140d7a720efSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_fmin",&tmp, &flg); CHKERRQ(ierr); 141d7a720efSLois Curfman McInnes if (flg) { SNESSetMinimizationFunctionTolerance(snes,tmp); } 1423c7409f5SSatish Balay ierr = OptionsHasName(snes->prefix,"-snes_ksp_ew_conv", &flg); CHKERRQ(ierr); 1433c7409f5SSatish Balay if (flg) { snes->ksp_ewconv = 1; } 144b18e04deSLois Curfman McInnes ierr = OptionsGetInt(snes->prefix,"-snes_ksp_ew_version",&version,&flg); CHKERRQ(ierr); 145b18e04deSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtol0",&rtol_0,&flg); CHKERRQ(ierr); 146b18e04deSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtolmax",&rtol_max,&flg);CHKERRQ(ierr); 147b18e04deSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_gamma",&gamma2,&flg); CHKERRQ(ierr); 148b18e04deSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha",&alpha,&flg); CHKERRQ(ierr); 149b18e04deSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha2",&alpha2,&flg); CHKERRQ(ierr); 150b18e04deSLois Curfman McInnes ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_threshold",&threshold,&flg);CHKERRQ(ierr); 1519b94acceSBarry Smith ierr = SNES_KSP_SetParametersEW(snes,version,rtol_0,rtol_max,gamma2,alpha, 1529b94acceSBarry Smith alpha2,threshold); CHKERRQ(ierr); 1533c7409f5SSatish Balay ierr = OptionsHasName(snes->prefix,"-snes_monitor", &flg); CHKERRQ(ierr); 1543c7409f5SSatish Balay if (flg) { SNESSetMonitor(snes,SNESDefaultMonitor,0); } 1553c7409f5SSatish Balay ierr = OptionsHasName(snes->prefix,"-snes_smonitor", &flg); CHKERRQ(ierr); 1563c7409f5SSatish Balay if (flg) { SNESSetMonitor(snes,SNESDefaultSMonitor,0); } 1573c7409f5SSatish Balay ierr = OptionsHasName(snes->prefix,"-snes_xmonitor", &flg); CHKERRQ(ierr); 1583c7409f5SSatish Balay if (flg) { 15917699dbbSLois Curfman McInnes int rank = 0; 160d7e8b826SBarry Smith DrawLG lg; 16117699dbbSLois Curfman McInnes MPI_Initialized(&rank); 16217699dbbSLois Curfman McInnes if (rank) MPI_Comm_rank(snes->comm,&rank); 16317699dbbSLois Curfman McInnes if (!rank) { 1649b94acceSBarry Smith ierr = SNESLGMonitorCreate(0,0,0,0,300,300,&lg); CHKERRQ(ierr); 1659b94acceSBarry Smith ierr = SNESSetMonitor(snes,SNESLGMonitor,(void *)lg); CHKERRQ(ierr); 166f8c826e1SBarry Smith snes->xmonitor = lg; 1679b94acceSBarry Smith PLogObjectParent(snes,lg); 1689b94acceSBarry Smith } 1699b94acceSBarry Smith } 1703c7409f5SSatish Balay ierr = OptionsHasName(snes->prefix,"-snes_fd", &flg); CHKERRQ(ierr); 1713c7409f5SSatish Balay if (flg && snes->method_class == SNES_NONLINEAR_EQUATIONS) { 1729b94acceSBarry Smith ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre, 1739b94acceSBarry Smith SNESDefaultComputeJacobian,snes->funP); CHKERRQ(ierr); 17494a424c1SBarry Smith PLogInfo(snes, 17531615d04SLois Curfman McInnes "SNESSetFromOptions: Setting default finite difference Jacobian matrix\n"); 17631615d04SLois Curfman McInnes } 17731615d04SLois Curfman McInnes else if (flg && snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 17831615d04SLois Curfman McInnes ierr = SNESSetHessian(snes,snes->jacobian,snes->jacobian_pre, 17931615d04SLois Curfman McInnes SNESDefaultComputeHessian,snes->funP); CHKERRQ(ierr); 18094a424c1SBarry Smith PLogInfo(snes, 18131615d04SLois Curfman McInnes "SNESSetFromOptions: Setting default finite difference Hessian matrix\n"); 1829b94acceSBarry Smith } 1839b94acceSBarry Smith ierr = SNESGetSLES(snes,&sles); CHKERRQ(ierr); 1849b94acceSBarry Smith ierr = SLESSetFromOptions(sles); CHKERRQ(ierr); 1859b94acceSBarry Smith if (!snes->setfromoptions) return 0; 1869b94acceSBarry Smith return (*snes->setfromoptions)(snes); 1879b94acceSBarry Smith } 1889b94acceSBarry Smith 1899b94acceSBarry Smith /*@ 1909b94acceSBarry Smith SNESPrintHelp - Prints all options for the SNES component. 1919b94acceSBarry Smith 1929b94acceSBarry Smith Input Parameter: 1939b94acceSBarry Smith . snes - the SNES context 1949b94acceSBarry Smith 195a703fe33SLois Curfman McInnes Options Database Keys: 196a703fe33SLois Curfman McInnes $ -help, -h 197a703fe33SLois Curfman McInnes 1989b94acceSBarry Smith .keywords: SNES, nonlinear, help 1999b94acceSBarry Smith 200682d7d0cSBarry Smith .seealso: SNESSetFromOptions() 2019b94acceSBarry Smith @*/ 2029b94acceSBarry Smith int SNESPrintHelp(SNES snes) 2039b94acceSBarry Smith { 2046daaf66cSBarry Smith char p[64]; 2056daaf66cSBarry Smith SNES_KSP_EW_ConvCtx *kctx; 2066daaf66cSBarry Smith 20777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2086daaf66cSBarry Smith 2096daaf66cSBarry Smith PetscStrcpy(p,"-"); 2106daaf66cSBarry Smith if (snes->prefix) PetscStrcat(p, snes->prefix); 2116daaf66cSBarry Smith 2126daaf66cSBarry Smith kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx; 2136daaf66cSBarry Smith 21477c4ece6SBarry Smith PetscPrintf(snes->comm,"SNES options ----------------------------\n"); 21533455573SSatish Balay SNESPrintTypes_Private(snes->comm,p,"snes_type"); 21677c4ece6SBarry Smith PetscPrintf(snes->comm," %ssnes_monitor: use default SNES monitor\n",p); 21777c4ece6SBarry Smith PetscPrintf(snes->comm," %ssnes_view: view SNES info after each nonlinear solve\n",p); 218b18e04deSLois Curfman McInnes PetscPrintf(snes->comm," %ssnes_max_it <its>: max iterations (default %d)\n",p,snes->max_its); 219b18e04deSLois Curfman McInnes PetscPrintf(snes->comm," %ssnes_max_funcs <maxf>: max function evals (default %d)\n",p,snes->max_funcs); 220b18e04deSLois Curfman McInnes PetscPrintf(snes->comm," %ssnes_stol <stol>: successive step tolerance (default %g)\n",p,snes->xtol); 221b18e04deSLois Curfman McInnes PetscPrintf(snes->comm," %ssnes_atol <atol>: absolute tolerance (default %g)\n",p,snes->atol); 222b18e04deSLois Curfman McInnes PetscPrintf(snes->comm," %ssnes_rtol <rtol>: relative tolerance (default %g)\n",p,snes->rtol); 223b18e04deSLois Curfman McInnes PetscPrintf(snes->comm," %ssnes_trtol <trtol>: trust region parameter tolerance (default %g)\n",p,snes->deltatol); 224b18e04deSLois Curfman McInnes if (snes->type == SNES_NONLINEAR_EQUATIONS) { 22577c4ece6SBarry Smith PetscPrintf(snes->comm, 2269b94acceSBarry Smith " options for solving systems of nonlinear equations only:\n"); 22777c4ece6SBarry Smith PetscPrintf(snes->comm," %ssnes_fd: use finite differences for Jacobian\n",p); 22877c4ece6SBarry Smith PetscPrintf(snes->comm," %ssnes_mf: use matrix-free Jacobian\n",p); 22977c4ece6SBarry Smith PetscPrintf(snes->comm," %ssnes_ksp_ew_conv: use Eisenstat-Walker computation of KSP rtol. Params are:\n",p); 23077c4ece6SBarry Smith PetscPrintf(snes->comm, 231b18e04deSLois Curfman McInnes " %ssnes_ksp_ew_version <version> (1 or 2, default is %d)\n",p,kctx->version); 23277c4ece6SBarry Smith PetscPrintf(snes->comm, 233b18e04deSLois Curfman McInnes " %ssnes_ksp_ew_rtol0 <rtol0> (0 <= rtol0 < 1, default %g)\n",p,kctx->rtol_0); 23477c4ece6SBarry Smith PetscPrintf(snes->comm, 235b18e04deSLois Curfman McInnes " %ssnes_ksp_ew_rtolmax <rtolmax> (0 <= rtolmax < 1, default %g)\n",p,kctx->rtol_max); 23677c4ece6SBarry Smith PetscPrintf(snes->comm, 237b18e04deSLois Curfman McInnes " %ssnes_ksp_ew_gamma <gamma> (0 <= gamma <= 1, default %g)\n",p,kctx->gamma); 23877c4ece6SBarry Smith PetscPrintf(snes->comm, 239b18e04deSLois Curfman McInnes " %ssnes_ksp_ew_alpha <alpha> (1 < alpha <= 2, default %g)\n",p,kctx->alpha); 24077c4ece6SBarry Smith PetscPrintf(snes->comm, 241b18e04deSLois Curfman McInnes " %ssnes_ksp_ew_alpha2 <alpha2> (default %g)\n",p,kctx->alpha2); 24277c4ece6SBarry Smith PetscPrintf(snes->comm, 243b18e04deSLois Curfman McInnes " %ssnes_ksp_ew_threshold <threshold> (0 < threshold < 1, default %g)\n",p,kctx->threshold); 244b18e04deSLois Curfman McInnes } 245b18e04deSLois Curfman McInnes else if (snes->type == SNES_UNCONSTRAINED_MINIMIZATION) { 24677c4ece6SBarry Smith PetscPrintf(snes->comm, 2479b94acceSBarry Smith " options for solving unconstrained minimization problems only:\n"); 248b18e04deSLois Curfman McInnes PetscPrintf(snes->comm," %ssnes_fmin <ftol>: minimum function tolerance (default %g)\n",p,snes->fmin); 24977c4ece6SBarry Smith PetscPrintf(snes->comm," %ssnes_fd: use finite differences for Hessian\n",p); 25077c4ece6SBarry Smith PetscPrintf(snes->comm," %ssnes_mf: use matrix-free Hessian\n",p); 251b18e04deSLois Curfman McInnes } 2524537a946SLois Curfman McInnes PetscPrintf(snes->comm," Run program with -help %ssnes_type <method> for help on ",p); 25377c4ece6SBarry Smith PetscPrintf(snes->comm,"a particular method\n"); 2546daaf66cSBarry Smith if (snes->printhelp) (*snes->printhelp)(snes,p); 2559b94acceSBarry Smith return 0; 2569b94acceSBarry Smith } 2579b94acceSBarry Smith /*@ 2589b94acceSBarry Smith SNESSetApplicationContext - Sets the optional user-defined context for 2599b94acceSBarry Smith the nonlinear solvers. 2609b94acceSBarry Smith 2619b94acceSBarry Smith Input Parameters: 2629b94acceSBarry Smith . snes - the SNES context 2639b94acceSBarry Smith . usrP - optional user context 2649b94acceSBarry Smith 2659b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context 2669b94acceSBarry Smith 2679b94acceSBarry Smith .seealso: SNESGetApplicationContext() 2689b94acceSBarry Smith @*/ 2699b94acceSBarry Smith int SNESSetApplicationContext(SNES snes,void *usrP) 2709b94acceSBarry Smith { 27177c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2729b94acceSBarry Smith snes->user = usrP; 2739b94acceSBarry Smith return 0; 2749b94acceSBarry Smith } 27574679c65SBarry 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 { 29277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2939b94acceSBarry Smith *usrP = snes->user; 2949b94acceSBarry Smith return 0; 2959b94acceSBarry Smith } 29674679c65SBarry Smith 2979b94acceSBarry Smith /*@ 2989b94acceSBarry Smith SNESGetIterationNumber - Gets the current iteration number of the 2999b94acceSBarry Smith nonlinear solver. 3009b94acceSBarry Smith 3019b94acceSBarry Smith Input Parameter: 3029b94acceSBarry Smith . snes - SNES context 3039b94acceSBarry Smith 3049b94acceSBarry Smith Output Parameter: 3059b94acceSBarry Smith . iter - iteration number 3069b94acceSBarry Smith 3079b94acceSBarry Smith .keywords: SNES, nonlinear, get, iteration, number 3089b94acceSBarry Smith @*/ 3099b94acceSBarry Smith int SNESGetIterationNumber(SNES snes,int* iter) 3109b94acceSBarry Smith { 31177c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 31274679c65SBarry Smith PetscValidIntPointer(iter); 3139b94acceSBarry Smith *iter = snes->iter; 3149b94acceSBarry Smith return 0; 3159b94acceSBarry Smith } 31674679c65SBarry Smith 3179b94acceSBarry Smith /*@ 3189b94acceSBarry Smith SNESGetFunctionNorm - Gets the norm of the current function that was set 3199b94acceSBarry Smith with SNESSSetFunction(). 3209b94acceSBarry Smith 3219b94acceSBarry Smith Input Parameter: 3229b94acceSBarry Smith . snes - SNES context 3239b94acceSBarry Smith 3249b94acceSBarry Smith Output Parameter: 3259b94acceSBarry Smith . fnorm - 2-norm of function 3269b94acceSBarry Smith 3279b94acceSBarry Smith Note: 3289b94acceSBarry Smith SNESGetFunctionNorm() is valid for SNES_NONLINEAR_EQUATIONS methods only. 329a86d99e1SLois Curfman McInnes A related routine for SNES_UNCONSTRAINED_MINIMIZATION methods is 330a86d99e1SLois Curfman McInnes SNESGetGradientNorm(). 3319b94acceSBarry Smith 3329b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm 333a86d99e1SLois Curfman McInnes 334a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction() 3359b94acceSBarry Smith @*/ 3369b94acceSBarry Smith int SNESGetFunctionNorm(SNES snes,Scalar *fnorm) 3379b94acceSBarry Smith { 33877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 33974679c65SBarry Smith PetscValidScalarPointer(fnorm); 34074679c65SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 34174679c65SBarry Smith SETERRQ(1,"SNESGetFunctionNorm:For SNES_NONLINEAR_EQUATIONS only"); 34274679c65SBarry Smith } 3439b94acceSBarry Smith *fnorm = snes->norm; 3449b94acceSBarry Smith return 0; 3459b94acceSBarry Smith } 34674679c65SBarry Smith 3479b94acceSBarry Smith /*@ 3489b94acceSBarry Smith SNESGetGradientNorm - Gets the norm of the current gradient that was set 3499b94acceSBarry Smith with SNESSSetGradient(). 3509b94acceSBarry Smith 3519b94acceSBarry Smith Input Parameter: 3529b94acceSBarry Smith . snes - SNES context 3539b94acceSBarry Smith 3549b94acceSBarry Smith Output Parameter: 3559b94acceSBarry Smith . fnorm - 2-norm of gradient 3569b94acceSBarry Smith 3579b94acceSBarry Smith Note: 3589b94acceSBarry Smith SNESGetGradientNorm() is valid for SNES_UNCONSTRAINED_MINIMIZATION 359a86d99e1SLois Curfman McInnes methods only. A related routine for SNES_NONLINEAR_EQUATIONS methods 360a86d99e1SLois Curfman McInnes is SNESGetFunctionNorm(). 3619b94acceSBarry Smith 3629b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient, norm 363a86d99e1SLois Curfman McInnes 364a86d99e1SLois Curfman McInnes .seelso: SNESSetGradient() 3659b94acceSBarry Smith @*/ 3669b94acceSBarry Smith int SNESGetGradientNorm(SNES snes,Scalar *gnorm) 3679b94acceSBarry Smith { 36877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 36974679c65SBarry Smith PetscValidScalarPointer(gnorm); 37074679c65SBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 37174679c65SBarry Smith SETERRQ(1,"SNESGetGradientNorm:For SNES_UNCONSTRAINED_MINIMIZATION only"); 37274679c65SBarry Smith } 3739b94acceSBarry Smith *gnorm = snes->norm; 3749b94acceSBarry Smith return 0; 3759b94acceSBarry Smith } 37674679c65SBarry Smith 3779b94acceSBarry Smith /*@ 3789b94acceSBarry Smith SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps 3799b94acceSBarry Smith attempted by the nonlinear solver. 3809b94acceSBarry Smith 3819b94acceSBarry Smith Input Parameter: 3829b94acceSBarry Smith . snes - SNES context 3839b94acceSBarry Smith 3849b94acceSBarry Smith Output Parameter: 3859b94acceSBarry Smith . nfails - number of unsuccessful steps attempted 3869b94acceSBarry Smith 3879b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 3889b94acceSBarry Smith @*/ 3899b94acceSBarry Smith int SNESGetNumberUnsuccessfulSteps(SNES snes,int* nfails) 3909b94acceSBarry Smith { 39177c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 39274679c65SBarry Smith PetscValidIntPointer(nfails); 3939b94acceSBarry Smith *nfails = snes->nfailures; 3949b94acceSBarry Smith return 0; 3959b94acceSBarry Smith } 3969b94acceSBarry Smith /*@C 3979b94acceSBarry Smith SNESGetSLES - Returns the SLES context for a SNES solver. 3989b94acceSBarry Smith 3999b94acceSBarry Smith Input Parameter: 4009b94acceSBarry Smith . snes - the SNES context 4019b94acceSBarry Smith 4029b94acceSBarry Smith Output Parameter: 4039b94acceSBarry Smith . sles - the SLES context 4049b94acceSBarry Smith 4059b94acceSBarry Smith Notes: 4069b94acceSBarry Smith The user can then directly manipulate the SLES context to set various 4079b94acceSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 4089b94acceSBarry Smith KSP and PC contexts as well. 4099b94acceSBarry Smith 4109b94acceSBarry Smith .keywords: SNES, nonlinear, get, SLES, context 4119b94acceSBarry Smith 4129b94acceSBarry Smith .seealso: SLESGetPC(), SLESGetKSP() 4139b94acceSBarry Smith @*/ 4149b94acceSBarry Smith int SNESGetSLES(SNES snes,SLES *sles) 4159b94acceSBarry Smith { 41677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 4179b94acceSBarry Smith *sles = snes->sles; 4189b94acceSBarry Smith return 0; 4199b94acceSBarry Smith } 4209b94acceSBarry Smith /* -----------------------------------------------------------*/ 4219b94acceSBarry Smith /*@C 4229b94acceSBarry Smith SNESCreate - Creates a nonlinear solver context. 4239b94acceSBarry Smith 4249b94acceSBarry Smith Input Parameter: 4259b94acceSBarry Smith . comm - MPI communicator 4269b94acceSBarry Smith . type - type of method, one of 4279b94acceSBarry Smith $ SNES_NONLINEAR_EQUATIONS 4289b94acceSBarry Smith $ (for systems of nonlinear equations) 4299b94acceSBarry Smith $ SNES_UNCONSTRAINED_MINIMIZATION 4309b94acceSBarry Smith $ (for unconstrained minimization) 4319b94acceSBarry Smith 4329b94acceSBarry Smith Output Parameter: 4339b94acceSBarry Smith . outsnes - the new SNES context 4349b94acceSBarry Smith 4359b94acceSBarry Smith .keywords: SNES, nonlinear, create, context 4369b94acceSBarry Smith 43763a78c88SLois Curfman McInnes .seealso: SNESSolve(), SNESDestroy() 4389b94acceSBarry Smith @*/ 4394b0e389bSBarry Smith int SNESCreate(MPI_Comm comm,SNESProblemType type,SNES *outsnes) 4409b94acceSBarry Smith { 4419b94acceSBarry Smith int ierr; 4429b94acceSBarry Smith SNES snes; 4439b94acceSBarry Smith SNES_KSP_EW_ConvCtx *kctx; 44437fcc0dbSBarry Smith 4459b94acceSBarry Smith *outsnes = 0; 4462a0acf01SLois Curfman McInnes if (type != SNES_UNCONSTRAINED_MINIMIZATION && type != SNES_NONLINEAR_EQUATIONS) 4472a0acf01SLois Curfman McInnes SETERRQ(1,"SNESCreate:incorrect method type"); 4480452661fSBarry Smith PetscHeaderCreate(snes,_SNES,SNES_COOKIE,SNES_UNKNOWN_METHOD,comm); 4499b94acceSBarry Smith PLogObjectCreate(snes); 4509b94acceSBarry Smith snes->max_its = 50; 4519b94acceSBarry Smith snes->max_funcs = 1000; 4529b94acceSBarry Smith snes->norm = 0.0; 4535a2d0531SBarry Smith if (type == SNES_UNCONSTRAINED_MINIMIZATION) { 454b18e04deSLois Curfman McInnes snes->rtol = 1.e-8; 455b18e04deSLois Curfman McInnes snes->ttol = 0.0; 4569b94acceSBarry Smith snes->atol = 1.e-10; 4575a2d0531SBarry Smith } 458b4874afaSBarry Smith else { 459b4874afaSBarry Smith snes->rtol = 1.e-8; 460b4874afaSBarry Smith snes->ttol = 0.0; 461b4874afaSBarry Smith snes->atol = 1.e-50; 462b4874afaSBarry Smith } 4639b94acceSBarry Smith snes->xtol = 1.e-8; 464b18e04deSLois Curfman McInnes snes->trunctol = 1.e-12; /* no longer used */ 4659b94acceSBarry Smith snes->nfuncs = 0; 4669b94acceSBarry Smith snes->nfailures = 0; 4679b94acceSBarry Smith snes->monitor = 0; 4689b94acceSBarry Smith snes->data = 0; 4699b94acceSBarry Smith snes->view = 0; 4709b94acceSBarry Smith snes->computeumfunction = 0; 4719b94acceSBarry Smith snes->umfunP = 0; 4729b94acceSBarry Smith snes->fc = 0; 4739b94acceSBarry Smith snes->deltatol = 1.e-12; 4749b94acceSBarry Smith snes->fmin = -1.e30; 4759b94acceSBarry Smith snes->method_class = type; 4769b94acceSBarry Smith snes->set_method_called = 0; 477a703fe33SLois Curfman McInnes snes->setup_called = 0; 4789b94acceSBarry Smith snes->ksp_ewconv = 0; 4797d1a2b2bSBarry Smith snes->type = -1; 4806f24a144SLois Curfman McInnes snes->xmonitor = 0; 4816f24a144SLois Curfman McInnes snes->vwork = 0; 4826f24a144SLois Curfman McInnes snes->nwork = 0; 4839b94acceSBarry Smith 4849b94acceSBarry Smith /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 4850452661fSBarry Smith kctx = PetscNew(SNES_KSP_EW_ConvCtx); CHKPTRQ(kctx); 4869b94acceSBarry Smith snes->kspconvctx = (void*)kctx; 4879b94acceSBarry Smith kctx->version = 2; 4889b94acceSBarry Smith kctx->rtol_0 = .3; /* Eisenstat and Walker suggest rtol_0=.5, but 4899b94acceSBarry Smith this was too large for some test cases */ 4909b94acceSBarry Smith kctx->rtol_last = 0; 4919b94acceSBarry Smith kctx->rtol_max = .9; 4929b94acceSBarry Smith kctx->gamma = 1.0; 4939b94acceSBarry Smith kctx->alpha2 = .5*(1.0 + sqrt(5.0)); 4949b94acceSBarry Smith kctx->alpha = kctx->alpha2; 4959b94acceSBarry Smith kctx->threshold = .1; 4969b94acceSBarry Smith kctx->lresid_last = 0; 4979b94acceSBarry Smith kctx->norm_last = 0; 4989b94acceSBarry Smith 4999b94acceSBarry Smith ierr = SLESCreate(comm,&snes->sles); CHKERRQ(ierr); 5009b94acceSBarry Smith PLogObjectParent(snes,snes->sles) 5015334005bSBarry Smith 5029b94acceSBarry Smith *outsnes = snes; 5039b94acceSBarry Smith return 0; 5049b94acceSBarry Smith } 5059b94acceSBarry Smith 5069b94acceSBarry Smith /* --------------------------------------------------------------- */ 5079b94acceSBarry Smith /*@C 5089b94acceSBarry Smith SNESSetFunction - Sets the function evaluation routine and function 5099b94acceSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 5109b94acceSBarry Smith equations. 5119b94acceSBarry Smith 5129b94acceSBarry Smith Input Parameters: 5139b94acceSBarry Smith . snes - the SNES context 5149b94acceSBarry Smith . func - function evaluation routine 5159b94acceSBarry Smith . ctx - optional user-defined function context 5169b94acceSBarry Smith . r - vector to store function value 5179b94acceSBarry Smith 5189b94acceSBarry Smith Calling sequence of func: 5199b94acceSBarry Smith . func (SNES, Vec x, Vec f, void *ctx); 5209b94acceSBarry Smith 5219b94acceSBarry Smith . x - input vector 5225334005bSBarry Smith . f - vector function 5239b94acceSBarry Smith . ctx - optional user-defined context for private data for the 5249b94acceSBarry Smith function evaluation routine (may be null) 5259b94acceSBarry Smith 5269b94acceSBarry Smith Notes: 5279b94acceSBarry Smith The Newton-like methods typically solve linear systems of the form 5289b94acceSBarry Smith $ f'(x) x = -f(x), 5299b94acceSBarry Smith $ where f'(x) denotes the Jacobian matrix and f(x) is the function. 5309b94acceSBarry Smith 5319b94acceSBarry Smith SNESSetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only. 5329b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 5339b94acceSBarry Smith SNESSetMinimizationFunction() and SNESSetGradient(); 5349b94acceSBarry Smith 5359b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 5369b94acceSBarry Smith 537a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 5389b94acceSBarry Smith @*/ 5395334005bSBarry Smith int SNESSetFunction( SNES snes, Vec r, int (*func)(SNES,Vec,Vec,void*),void *ctx) 5409b94acceSBarry Smith { 54177c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 5429b94acceSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1, 54348d91487SBarry Smith "SNESSetFunction:For SNES_NONLINEAR_EQUATIONS only"); 5449b94acceSBarry Smith snes->computefunction = func; 5459b94acceSBarry Smith snes->vec_func = snes->vec_func_always = r; 5469b94acceSBarry Smith snes->funP = ctx; 5479b94acceSBarry Smith return 0; 5489b94acceSBarry Smith } 5499b94acceSBarry Smith 5509b94acceSBarry Smith /*@ 5519b94acceSBarry Smith SNESComputeFunction - Computes the function that has been set with 5529b94acceSBarry Smith SNESSetFunction(). 5539b94acceSBarry Smith 5549b94acceSBarry Smith Input Parameters: 5559b94acceSBarry Smith . snes - the SNES context 5569b94acceSBarry Smith . x - input vector 5579b94acceSBarry Smith 5589b94acceSBarry Smith Output Parameter: 5593638b69dSLois Curfman McInnes . y - function vector, as set by SNESSetFunction() 5609b94acceSBarry Smith 561a86d99e1SLois Curfman McInnes n Notes: 5629b94acceSBarry Smith SNESComputeFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only. 5639b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 5649b94acceSBarry Smith SNESComputeMinimizationFunction() and SNESComputeGradient(); 5659b94acceSBarry Smith 5669b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function 5679b94acceSBarry Smith 568a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction() 5699b94acceSBarry Smith @*/ 5709b94acceSBarry Smith int SNESComputeFunction(SNES snes,Vec x, Vec y) 5719b94acceSBarry Smith { 5729b94acceSBarry Smith int ierr; 5739b94acceSBarry Smith 57474679c65SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) 57574679c65SBarry Smith SETERRQ(1,"SNESComputeFunction: For SNES_NONLINEAR_EQUATIONS only"); 5769b94acceSBarry Smith PLogEventBegin(SNES_FunctionEval,snes,x,y,0); 5779b94acceSBarry Smith ierr = (*snes->computefunction)(snes,x,y,snes->funP); CHKERRQ(ierr); 5789b94acceSBarry Smith PLogEventEnd(SNES_FunctionEval,snes,x,y,0); 5799b94acceSBarry Smith return 0; 5809b94acceSBarry Smith } 5819b94acceSBarry Smith 5829b94acceSBarry Smith /*@C 5839b94acceSBarry Smith SNESSetMinimizationFunction - Sets the function evaluation routine for 5849b94acceSBarry Smith unconstrained minimization. 5859b94acceSBarry Smith 5869b94acceSBarry Smith Input Parameters: 5879b94acceSBarry Smith . snes - the SNES context 5889b94acceSBarry Smith . func - function evaluation routine 5899b94acceSBarry Smith . ctx - optional user-defined function context 5909b94acceSBarry Smith 5919b94acceSBarry Smith Calling sequence of func: 5929b94acceSBarry Smith . func (SNES snes,Vec x,double *f,void *ctx); 5939b94acceSBarry Smith 5949b94acceSBarry Smith . x - input vector 5959b94acceSBarry Smith . f - function 5969b94acceSBarry Smith . ctx - optional user-defined context for private data for the 5979b94acceSBarry Smith function evaluation routine (may be null) 5989b94acceSBarry Smith 5999b94acceSBarry Smith Notes: 6009b94acceSBarry Smith SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 6019b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 6029b94acceSBarry Smith SNESSetFunction(). 6039b94acceSBarry Smith 6049b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimization, function 6059b94acceSBarry Smith 606a86d99e1SLois Curfman McInnes .seealso: SNESGetMinimizationFunction(), SNESComputeMinimizationFunction(), 607a86d99e1SLois Curfman McInnes SNESSetHessian(), SNESSetGradient() 6089b94acceSBarry Smith @*/ 6099b94acceSBarry Smith int SNESSetMinimizationFunction(SNES snes,int (*func)(SNES,Vec,double*,void*), 6109b94acceSBarry Smith void *ctx) 6119b94acceSBarry Smith { 61277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 6139b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 61448d91487SBarry Smith "SNESSetMinimizationFunction:Only for SNES_UNCONSTRAINED_MINIMIZATION"); 6159b94acceSBarry Smith snes->computeumfunction = func; 6169b94acceSBarry Smith snes->umfunP = ctx; 6179b94acceSBarry Smith return 0; 6189b94acceSBarry Smith } 6199b94acceSBarry Smith 6209b94acceSBarry Smith /*@ 6219b94acceSBarry Smith SNESComputeMinimizationFunction - Computes the function that has been 6229b94acceSBarry Smith set with SNESSetMinimizationFunction(). 6239b94acceSBarry Smith 6249b94acceSBarry Smith Input Parameters: 6259b94acceSBarry Smith . snes - the SNES context 6269b94acceSBarry Smith . x - input vector 6279b94acceSBarry Smith 6289b94acceSBarry Smith Output Parameter: 6299b94acceSBarry Smith . y - function value 6309b94acceSBarry Smith 6319b94acceSBarry Smith Notes: 6329b94acceSBarry Smith SNESComputeMinimizationFunction() is valid only for 6339b94acceSBarry Smith SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 6349b94acceSBarry Smith SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction(). 635a86d99e1SLois Curfman McInnes 636a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, minimization, function 637a86d99e1SLois Curfman McInnes 638a86d99e1SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESGetMinimizationFunction(), 639a86d99e1SLois Curfman McInnes SNESComputeGradient(), SNESComputeHessian() 6409b94acceSBarry Smith @*/ 6419b94acceSBarry Smith int SNESComputeMinimizationFunction(SNES snes,Vec x,double *y) 6429b94acceSBarry Smith { 6439b94acceSBarry Smith int ierr; 6449b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 64548d91487SBarry Smith "SNESComputeMinimizationFunction:Only for SNES_UNCONSTRAINED_MINIMIZATION"); 6469b94acceSBarry Smith PLogEventBegin(SNES_MinimizationFunctionEval,snes,x,y,0); 6479b94acceSBarry Smith ierr = (*snes->computeumfunction)(snes,x,y,snes->umfunP); CHKERRQ(ierr); 6489b94acceSBarry Smith PLogEventEnd(SNES_MinimizationFunctionEval,snes,x,y,0); 6499b94acceSBarry Smith return 0; 6509b94acceSBarry Smith } 6519b94acceSBarry Smith 6529b94acceSBarry Smith /*@C 6539b94acceSBarry Smith SNESSetGradient - Sets the gradient evaluation routine and gradient 6549b94acceSBarry Smith vector for use by the SNES routines. 6559b94acceSBarry Smith 6569b94acceSBarry Smith Input Parameters: 6579b94acceSBarry Smith . snes - the SNES context 6589b94acceSBarry Smith . func - function evaluation routine 6599b94acceSBarry Smith . ctx - optional user-defined function context 6609b94acceSBarry Smith . r - vector to store gradient value 6619b94acceSBarry Smith 6629b94acceSBarry Smith Calling sequence of func: 6639b94acceSBarry Smith . func (SNES, Vec x, Vec g, void *ctx); 6649b94acceSBarry Smith 6659b94acceSBarry Smith . x - input vector 6669b94acceSBarry Smith . g - gradient vector 6679b94acceSBarry Smith . ctx - optional user-defined context for private data for the 6689b94acceSBarry Smith function evaluation routine (may be null) 6699b94acceSBarry Smith 6709b94acceSBarry Smith Notes: 6719b94acceSBarry Smith SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 6729b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 6739b94acceSBarry Smith SNESSetFunction(). 6749b94acceSBarry Smith 6759b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 6769b94acceSBarry Smith 677a86d99e1SLois Curfman McInnes .seealso: SNESGetGradient(), SNESComputeGradient(), SNESSetHessian(), 678a86d99e1SLois Curfman McInnes SNESSetMinimizationFunction(), 6799b94acceSBarry Smith @*/ 68074679c65SBarry Smith int SNESSetGradient(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),void *ctx) 6819b94acceSBarry Smith { 68277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 6839b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 68448d91487SBarry Smith "SNESSetGradient:For SNES_UNCONSTRAINED_MINIMIZATION only"); 6859b94acceSBarry Smith snes->computefunction = func; 6869b94acceSBarry Smith snes->vec_func = snes->vec_func_always = r; 6879b94acceSBarry Smith snes->funP = ctx; 6889b94acceSBarry Smith return 0; 6899b94acceSBarry Smith } 6909b94acceSBarry Smith 6919b94acceSBarry Smith /*@ 692a86d99e1SLois Curfman McInnes SNESComputeGradient - Computes the gradient that has been set with 693a86d99e1SLois Curfman McInnes SNESSetGradient(). 6949b94acceSBarry Smith 6959b94acceSBarry Smith Input Parameters: 6969b94acceSBarry Smith . snes - the SNES context 6979b94acceSBarry Smith . x - input vector 6989b94acceSBarry Smith 6999b94acceSBarry Smith Output Parameter: 7009b94acceSBarry Smith . y - gradient vector 7019b94acceSBarry Smith 7029b94acceSBarry Smith Notes: 7039b94acceSBarry Smith SNESComputeGradient() is valid only for 7049b94acceSBarry Smith SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 7059b94acceSBarry Smith SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction(). 706a86d99e1SLois Curfman McInnes 707a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, gradient 708a86d99e1SLois Curfman McInnes 709a86d99e1SLois Curfman McInnes .seealso: SNESSetGradient(), SNESGetGradient(), 710a86d99e1SLois Curfman McInnes SNESComputeMinimizationFunction(), SNESComputeHessian() 7119b94acceSBarry Smith @*/ 7129b94acceSBarry Smith int SNESComputeGradient(SNES snes,Vec x, Vec y) 7139b94acceSBarry Smith { 7149b94acceSBarry Smith int ierr; 71574679c65SBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) 71674679c65SBarry Smith SETERRQ(1,"SNESComputeGradient:For SNES_UNCONSTRAINED_MINIMIZATION only"); 7179b94acceSBarry Smith PLogEventBegin(SNES_GradientEval,snes,x,y,0); 7189b94acceSBarry Smith ierr = (*snes->computefunction)(snes,x,y,snes->funP); CHKERRQ(ierr); 7199b94acceSBarry Smith PLogEventEnd(SNES_GradientEval,snes,x,y,0); 7209b94acceSBarry Smith return 0; 7219b94acceSBarry Smith } 7229b94acceSBarry Smith 72362fef451SLois Curfman McInnes /*@ 72462fef451SLois Curfman McInnes SNESComputeJacobian - Computes the Jacobian matrix that has been 72562fef451SLois Curfman McInnes set with SNESSetJacobian(). 72662fef451SLois Curfman McInnes 72762fef451SLois Curfman McInnes Input Parameters: 72862fef451SLois Curfman McInnes . snes - the SNES context 72962fef451SLois Curfman McInnes . x - input vector 73062fef451SLois Curfman McInnes 73162fef451SLois Curfman McInnes Output Parameters: 73262fef451SLois Curfman McInnes . A - Jacobian matrix 73362fef451SLois Curfman McInnes . B - optional preconditioning matrix 73462fef451SLois Curfman McInnes . flag - flag indicating matrix structure 73562fef451SLois Curfman McInnes 73662fef451SLois Curfman McInnes Notes: 73762fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 73862fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 73962fef451SLois Curfman McInnes 74062fef451SLois Curfman McInnes See SLESSetOperators() for information about setting the flag parameter. 74162fef451SLois Curfman McInnes 74262fef451SLois Curfman McInnes SNESComputeJacobian() is valid only for SNES_NONLINEAR_EQUATIONS 74362fef451SLois Curfman McInnes methods. An analogous routine for SNES_UNCONSTRAINED_MINIMIZATION 74462fef451SLois Curfman McInnes methods is SNESComputeJacobian(). 74562fef451SLois Curfman McInnes 74662fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix 74762fef451SLois Curfman McInnes 74862fef451SLois Curfman McInnes .seealso: SNESSetJacobian(), SLESSetOperators() 74962fef451SLois Curfman McInnes @*/ 7509b94acceSBarry Smith int SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 7519b94acceSBarry Smith { 7529b94acceSBarry Smith int ierr; 75374679c65SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) 75474679c65SBarry Smith SETERRQ(1,"SNESComputeJacobian: For SNES_NONLINEAR_EQUATIONS only"); 7559b94acceSBarry Smith if (!snes->computejacobian) return 0; 7569b94acceSBarry Smith PLogEventBegin(SNES_JacobianEval,snes,X,*A,*B); 757c4fc05e7SBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 7589b94acceSBarry Smith ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP); CHKERRQ(ierr); 7599b94acceSBarry Smith PLogEventEnd(SNES_JacobianEval,snes,X,*A,*B); 7606d84be18SBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 76177c4ece6SBarry Smith PetscValidHeaderSpecific(*A,MAT_COOKIE); 76277c4ece6SBarry Smith PetscValidHeaderSpecific(*B,MAT_COOKIE); 7639b94acceSBarry Smith return 0; 7649b94acceSBarry Smith } 7659b94acceSBarry Smith 76662fef451SLois Curfman McInnes /*@ 76762fef451SLois Curfman McInnes SNESComputeHessian - Computes the Hessian matrix that has been 76862fef451SLois Curfman McInnes set with SNESSetHessian(). 76962fef451SLois Curfman McInnes 77062fef451SLois Curfman McInnes Input Parameters: 77162fef451SLois Curfman McInnes . snes - the SNES context 77262fef451SLois Curfman McInnes . x - input vector 77362fef451SLois Curfman McInnes 77462fef451SLois Curfman McInnes Output Parameters: 77562fef451SLois Curfman McInnes . A - Hessian matrix 77662fef451SLois Curfman McInnes . B - optional preconditioning matrix 77762fef451SLois Curfman McInnes . flag - flag indicating matrix structure 77862fef451SLois Curfman McInnes 77962fef451SLois Curfman McInnes Notes: 78062fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 78162fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 78262fef451SLois Curfman McInnes 78362fef451SLois Curfman McInnes See SLESSetOperators() for information about setting the flag parameter. 78462fef451SLois Curfman McInnes 78562fef451SLois Curfman McInnes SNESComputeHessian() is valid only for 78662fef451SLois Curfman McInnes SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 78762fef451SLois Curfman McInnes SNES_NONLINEAR_EQUATIONS methods is SNESComputeJacobian(). 78862fef451SLois Curfman McInnes 78962fef451SLois Curfman McInnes .keywords: SNES, compute, Hessian, matrix 79062fef451SLois Curfman McInnes 791a86d99e1SLois Curfman McInnes .seealso: SNESSetHessian(), SLESSetOperators(), SNESComputeGradient(), 792a86d99e1SLois Curfman McInnes SNESComputeMinimizationFunction() 79362fef451SLois Curfman McInnes @*/ 79462fef451SLois Curfman McInnes int SNESComputeHessian(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag) 7959b94acceSBarry Smith { 7969b94acceSBarry Smith int ierr; 79774679c65SBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) 79874679c65SBarry Smith SETERRQ(1,"SNESComputeHessian:For SNES_UNCONSTRAINED_MINIMIZATION only"); 7999b94acceSBarry Smith if (!snes->computejacobian) return 0; 80062fef451SLois Curfman McInnes PLogEventBegin(SNES_HessianEval,snes,x,*A,*B); 8010f4a323eSLois Curfman McInnes *flag = DIFFERENT_NONZERO_PATTERN; 80262fef451SLois Curfman McInnes ierr = (*snes->computejacobian)(snes,x,A,B,flag,snes->jacP); CHKERRQ(ierr); 80362fef451SLois Curfman McInnes PLogEventEnd(SNES_HessianEval,snes,x,*A,*B); 8040f4a323eSLois Curfman McInnes /* make sure user returned a correct Jacobian and preconditioner */ 80577c4ece6SBarry Smith PetscValidHeaderSpecific(*A,MAT_COOKIE); 80677c4ece6SBarry Smith PetscValidHeaderSpecific(*B,MAT_COOKIE); 8079b94acceSBarry Smith return 0; 8089b94acceSBarry Smith } 8099b94acceSBarry Smith 8109b94acceSBarry Smith /*@C 8119b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 8129b94acceSBarry Smith location to store it. 8139b94acceSBarry Smith 8149b94acceSBarry Smith Input Parameters: 8159b94acceSBarry Smith . snes - the SNES context 8169b94acceSBarry Smith . A - Jacobian matrix 8179b94acceSBarry Smith . B - preconditioner matrix (usually same as the Jacobian) 8189b94acceSBarry Smith . func - Jacobian evaluation routine 8199b94acceSBarry Smith . ctx - optional user-defined context for private data for the 8209b94acceSBarry Smith Jacobian evaluation routine (may be null) 8219b94acceSBarry Smith 8229b94acceSBarry Smith Calling sequence of func: 8239b94acceSBarry Smith . func (SNES,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 8249b94acceSBarry Smith 8259b94acceSBarry Smith . x - input vector 8269b94acceSBarry Smith . A - Jacobian matrix 8279b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 828ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 829ac21db08SLois Curfman McInnes structure (same as flag in SLESSetOperators()) 8309b94acceSBarry Smith . ctx - optional user-defined Jacobian context 8319b94acceSBarry Smith 8329b94acceSBarry Smith Notes: 833ac21db08SLois Curfman McInnes See SLESSetOperators() for information about setting the flag input 834ac21db08SLois Curfman McInnes parameter in the routine func(). Be sure to read this information! 835ac21db08SLois Curfman McInnes 836ac21db08SLois Curfman McInnes The routine func() takes Mat * as the matrix arguments rather than Mat. 8379b94acceSBarry Smith This allows the Jacobian evaluation routine to replace A and/or B with a 8389b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 8399b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 8409b94acceSBarry Smith throughout the global iterations. 8419b94acceSBarry Smith 8429b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix 8439b94acceSBarry Smith 844ac21db08SLois Curfman McInnes .seealso: SLESSetOperators(), SNESSetFunction() 8459b94acceSBarry Smith @*/ 8469b94acceSBarry Smith int SNESSetJacobian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*, 8479b94acceSBarry Smith MatStructure*,void*),void *ctx) 8489b94acceSBarry Smith { 84977c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 85074679c65SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) 85174679c65SBarry Smith SETERRQ(1,"SNESSetJacobian:For SNES_NONLINEAR_EQUATIONS only"); 8529b94acceSBarry Smith snes->computejacobian = func; 8539b94acceSBarry Smith snes->jacP = ctx; 8549b94acceSBarry Smith snes->jacobian = A; 8559b94acceSBarry Smith snes->jacobian_pre = B; 8569b94acceSBarry Smith return 0; 8579b94acceSBarry Smith } 85862fef451SLois Curfman McInnes 859b4fd4287SBarry Smith /*@ 860b4fd4287SBarry Smith SNESGetJacobian - Returns the Jacobian matrix and optionally the user 861b4fd4287SBarry Smith provided context for evaluating the Jacobian. 862b4fd4287SBarry Smith 863b4fd4287SBarry Smith Input Parameter: 864b4fd4287SBarry Smith . snes - the nonlinear solver context 865b4fd4287SBarry Smith 866b4fd4287SBarry Smith Output Parameters: 867b4fd4287SBarry Smith . A - location to stash Jacobian matrix (or PETSC_NULL) 868b4fd4287SBarry Smith . B - location to stash preconditioner matrix (or PETSC_NULL) 869b4fd4287SBarry Smith . ctx - location to stash Jacobian ctx (or PETSC_NULL) 870b4fd4287SBarry Smith 871b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian() 872b4fd4287SBarry Smith @*/ 873b4fd4287SBarry Smith int SNESGetJacobian(SNES snes,Mat *A,Mat *B, void **ctx) 874b4fd4287SBarry Smith { 87574679c65SBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) 87674679c65SBarry Smith SETERRQ(1,"SNESSetJacobian:For SNES_NONLINEAR_EQUATIONS only"); 877b4fd4287SBarry Smith if (A) *A = snes->jacobian; 878b4fd4287SBarry Smith if (B) *B = snes->jacobian_pre; 879b4fd4287SBarry Smith if (ctx) *ctx = snes->jacP; 880b4fd4287SBarry Smith return 0; 881b4fd4287SBarry Smith } 882b4fd4287SBarry Smith 8839b94acceSBarry Smith /*@C 8849b94acceSBarry Smith SNESSetHessian - Sets the function to compute Hessian as well as the 8859b94acceSBarry Smith location to store it. 8869b94acceSBarry Smith 8879b94acceSBarry Smith Input Parameters: 8889b94acceSBarry Smith . snes - the SNES context 8899b94acceSBarry Smith . A - Hessian matrix 8909b94acceSBarry Smith . B - preconditioner matrix (usually same as the Hessian) 8919b94acceSBarry Smith . func - Jacobian evaluation routine 8929b94acceSBarry Smith . ctx - optional user-defined context for private data for the 8939b94acceSBarry Smith Hessian evaluation routine (may be null) 8949b94acceSBarry Smith 8959b94acceSBarry Smith Calling sequence of func: 8969b94acceSBarry Smith . func (SNES,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 8979b94acceSBarry Smith 8989b94acceSBarry Smith . x - input vector 8999b94acceSBarry Smith . A - Hessian matrix 9009b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 901ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 902ac21db08SLois Curfman McInnes structure (same as flag in SLESSetOperators()) 9039b94acceSBarry Smith . ctx - optional user-defined Hessian context 9049b94acceSBarry Smith 9059b94acceSBarry Smith Notes: 906ac21db08SLois Curfman McInnes See SLESSetOperators() for information about setting the flag input 907ac21db08SLois Curfman McInnes parameter in the routine func(). Be sure to read this information! 908ac21db08SLois Curfman McInnes 9099b94acceSBarry Smith The function func() takes Mat * as the matrix arguments rather than Mat. 9109b94acceSBarry Smith This allows the Hessian evaluation routine to replace A and/or B with a 9119b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 9129b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 9139b94acceSBarry Smith throughout the global iterations. 9149b94acceSBarry Smith 9159b94acceSBarry Smith .keywords: SNES, nonlinear, set, Hessian, matrix 9169b94acceSBarry Smith 917ac21db08SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESSetGradient(), SLESSetOperators() 9189b94acceSBarry Smith @*/ 9199b94acceSBarry Smith int SNESSetHessian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*, 9209b94acceSBarry Smith MatStructure*,void*),void *ctx) 9219b94acceSBarry Smith { 92277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 92374679c65SBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) 92474679c65SBarry Smith SETERRQ(1,"SNESSetHessian:For SNES_UNCONSTRAINED_MINIMIZATION only"); 9259b94acceSBarry Smith snes->computejacobian = func; 9269b94acceSBarry Smith snes->jacP = ctx; 9279b94acceSBarry Smith snes->jacobian = A; 9289b94acceSBarry Smith snes->jacobian_pre = B; 9299b94acceSBarry Smith return 0; 9309b94acceSBarry Smith } 9319b94acceSBarry Smith 93262fef451SLois Curfman McInnes /*@ 93362fef451SLois Curfman McInnes SNESGetHessian - Returns the Hessian matrix and optionally the user 93462fef451SLois Curfman McInnes provided context for evaluating the Hessian. 93562fef451SLois Curfman McInnes 93662fef451SLois Curfman McInnes Input Parameter: 93762fef451SLois Curfman McInnes . snes - the nonlinear solver context 93862fef451SLois Curfman McInnes 93962fef451SLois Curfman McInnes Output Parameters: 94062fef451SLois Curfman McInnes . A - location to stash Hessian matrix (or PETSC_NULL) 94162fef451SLois Curfman McInnes . B - location to stash preconditioner matrix (or PETSC_NULL) 94262fef451SLois Curfman McInnes . ctx - location to stash Hessian ctx (or PETSC_NULL) 94362fef451SLois Curfman McInnes 94462fef451SLois Curfman McInnes .seealso: SNESSetHessian(), SNESComputeHessian() 94562fef451SLois Curfman McInnes @*/ 94662fef451SLois Curfman McInnes int SNESGetHessian(SNES snes,Mat *A,Mat *B, void **ctx) 94762fef451SLois Curfman McInnes { 94874679c65SBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) 94974679c65SBarry Smith SETERRQ(1,"SNESSetHessian:For SNES_UNCONSTRAINED_MINIMIZATION only"); 95062fef451SLois Curfman McInnes if (A) *A = snes->jacobian; 95162fef451SLois Curfman McInnes if (B) *B = snes->jacobian_pre; 95262fef451SLois Curfman McInnes if (ctx) *ctx = snes->jacP; 95362fef451SLois Curfman McInnes return 0; 95462fef451SLois Curfman McInnes } 95562fef451SLois Curfman McInnes 9569b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 9579b94acceSBarry Smith 9589b94acceSBarry Smith /*@ 9599b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 960272ac6f2SLois Curfman McInnes of a nonlinear solver. 9619b94acceSBarry Smith 9629b94acceSBarry Smith Input Parameter: 9639b94acceSBarry Smith . snes - the SNES context 9648ddd3da0SLois Curfman McInnes . x - the solution vector 9659b94acceSBarry Smith 966272ac6f2SLois Curfman McInnes Notes: 967272ac6f2SLois Curfman McInnes For basic use of the SNES solvers the user need not explicitly call 968272ac6f2SLois Curfman McInnes SNESSetUp(), since these actions will automatically occur during 969272ac6f2SLois Curfman McInnes the call to SNESSolve(). However, if one wishes to control this 970272ac6f2SLois Curfman McInnes phase separately, SNESSetUp() should be called after SNESCreate() 971272ac6f2SLois Curfman McInnes and optional routines of the form SNESSetXXX(), but before SNESSolve(). 972272ac6f2SLois Curfman McInnes 9739b94acceSBarry Smith .keywords: SNES, nonlinear, setup 9749b94acceSBarry Smith 9759b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 9769b94acceSBarry Smith @*/ 9778ddd3da0SLois Curfman McInnes int SNESSetUp(SNES snes,Vec x) 9789b94acceSBarry Smith { 979272ac6f2SLois Curfman McInnes int ierr, flg; 98077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 98177c4ece6SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 9828ddd3da0SLois Curfman McInnes snes->vec_sol = snes->vec_sol_always = x; 9839b94acceSBarry Smith 984272ac6f2SLois Curfman McInnes ierr = OptionsHasName(snes->prefix,"-snes_mf", &flg); CHKERRQ(ierr); 98531615d04SLois Curfman McInnes if (flg) { 986272ac6f2SLois Curfman McInnes Mat J; 987272ac6f2SLois Curfman McInnes ierr = SNESDefaultMatrixFreeMatCreate(snes,snes->vec_sol,&J);CHKERRQ(ierr); 988272ac6f2SLois Curfman McInnes PLogObjectParent(snes,J); 989272ac6f2SLois Curfman McInnes snes->mfshell = J; 99083e56afdSLois Curfman McInnes if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 99183e56afdSLois Curfman McInnes ierr = SNESSetJacobian(snes,J,J,0,snes->funP); CHKERRQ(ierr); 99294a424c1SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free Jacobian routines\n"); 99383e56afdSLois Curfman McInnes } 99483e56afdSLois Curfman McInnes else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 99583e56afdSLois Curfman McInnes ierr = SNESSetHessian(snes,J,J,0,snes->funP); CHKERRQ(ierr); 99694a424c1SBarry Smith PLogInfo(snes,"SNESSetUp: Setting default matrix-free Hessian routines\n"); 99783e56afdSLois Curfman McInnes } else SETERRQ(1,"SNESSetUp:Method class doesn't support matrix-free option"); 998272ac6f2SLois Curfman McInnes } 9999b94acceSBarry Smith if ((snes->method_class == SNES_NONLINEAR_EQUATIONS)) { 100037fcc0dbSBarry Smith if (!snes->vec_func) SETERRQ(1,"SNESSetUp:Must call SNESSetFunction() first"); 100137fcc0dbSBarry Smith if (!snes->computefunction) SETERRQ(1,"SNESSetUp:Must call SNESSetFunction() first"); 100237fcc0dbSBarry Smith if (!snes->jacobian) SETERRQ(1,"SNESSetUp:Must call SNESSetJacobian() first"); 1003d804570eSBarry Smith if (snes->vec_func == snes->vec_sol) SETERRQ(1,"SNESSetUp:Solution vector cannot be function vector"); 1004a703fe33SLois Curfman McInnes 1005a703fe33SLois Curfman McInnes /* Set the KSP stopping criterion to use the Eisenstat-Walker method */ 100640191667SLois Curfman McInnes if (snes->ksp_ewconv && snes->type != SNES_EQ_TR) { 1007a703fe33SLois Curfman McInnes SLES sles; KSP ksp; 1008a703fe33SLois Curfman McInnes ierr = SNESGetSLES(snes,&sles); CHKERRQ(ierr); 1009a703fe33SLois Curfman McInnes ierr = SLESGetKSP(sles,&ksp); CHKERRQ(ierr); 1010a703fe33SLois Curfman McInnes ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private, 1011a703fe33SLois Curfman McInnes (void *)snes); CHKERRQ(ierr); 1012a703fe33SLois Curfman McInnes } 10139b94acceSBarry Smith } else if ((snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION)) { 101437fcc0dbSBarry Smith if (!snes->vec_func) SETERRQ(1,"SNESSetUp:Must call SNESSetGradient() first"); 101537fcc0dbSBarry Smith if (!snes->computefunction) SETERRQ(1,"SNESSetUp:Must call SNESSetGradient() first"); 101637fcc0dbSBarry Smith if (!snes->computeumfunction) 101737fcc0dbSBarry Smith SETERRQ(1,"SNESSetUp:Must call SNESSetMinimizationFunction() first"); 101837fcc0dbSBarry Smith if (!snes->jacobian) SETERRQ(1,"SNESSetUp:Must call SNESSetHessian() first"); 10199b94acceSBarry Smith } else SETERRQ(1,"SNESSetUp:Unknown method class"); 1020a703fe33SLois Curfman McInnes if (snes->setup) {ierr = (*snes->setup)(snes); CHKERRQ(ierr);} 1021a703fe33SLois Curfman McInnes snes->setup_called = 1; 1022a703fe33SLois Curfman McInnes return 0; 10239b94acceSBarry Smith } 10249b94acceSBarry Smith 10259b94acceSBarry Smith /*@C 10269b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 10279b94acceSBarry Smith with SNESCreate(). 10289b94acceSBarry Smith 10299b94acceSBarry Smith Input Parameter: 10309b94acceSBarry Smith . snes - the SNES context 10319b94acceSBarry Smith 10329b94acceSBarry Smith .keywords: SNES, nonlinear, destroy 10339b94acceSBarry Smith 103463a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve() 10359b94acceSBarry Smith @*/ 10369b94acceSBarry Smith int SNESDestroy(SNES snes) 10379b94acceSBarry Smith { 10389b94acceSBarry Smith int ierr; 103977c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 10409b94acceSBarry Smith ierr = (*(snes)->destroy)((PetscObject)snes); CHKERRQ(ierr); 10410452661fSBarry Smith if (snes->kspconvctx) PetscFree(snes->kspconvctx); 10429b94acceSBarry Smith if (snes->mfshell) MatDestroy(snes->mfshell); 10439b94acceSBarry Smith ierr = SLESDestroy(snes->sles); CHKERRQ(ierr); 10443e2e452bSBarry Smith if (snes->xmonitor) SNESLGMonitorDestroy(snes->xmonitor); 10456f24a144SLois Curfman McInnes if (snes->vwork) VecDestroyVecs(snes->vwork,snes->nvwork); 10469b94acceSBarry Smith PLogObjectDestroy((PetscObject)snes); 10470452661fSBarry Smith PetscHeaderDestroy((PetscObject)snes); 10489b94acceSBarry Smith return 0; 10499b94acceSBarry Smith } 10509b94acceSBarry Smith 10519b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 10529b94acceSBarry Smith 10539b94acceSBarry Smith /*@ 1054d7a720efSLois Curfman McInnes SNESSetTolerances - Sets various parameters used in convergence tests. 10559b94acceSBarry Smith 10569b94acceSBarry Smith Input Parameters: 10579b94acceSBarry Smith . snes - the SNES context 1058d7a720efSLois Curfman McInnes . atol - tolerance 10599b94acceSBarry Smith 10609b94acceSBarry Smith Options Database Key: 1061d7a720efSLois Curfman McInnes $ -snes_atol <atol> - absolute convergence tolerance 1062d7a720efSLois Curfman McInnes $ -snes_rtol <rtol> - relative convergence tolerance 1063d7a720efSLois Curfman McInnes $ -snes_stol <stol> - convergence tolerance in terms of 1064d7a720efSLois Curfman McInnes $ the norm of the change in the solution between steps 1065d7a720efSLois Curfman McInnes $ -snes_max_it <maxit> - maximum number of iterations 1066d7a720efSLois Curfman McInnes $ -snes_max_funcs <maxf> - maximum number of function evaluations 10679b94acceSBarry Smith 1068d7a720efSLois Curfman McInnes Notes: 10699b94acceSBarry Smith The default maximum number of iterations is 50. 10709b94acceSBarry Smith The default maximum number of function evaluations is 1000. 10719b94acceSBarry Smith 1072d7a720efSLois Curfman McInnes .keywords: SNES, nonlinear, set, absolute, convergence, tolerance 10739b94acceSBarry Smith 1074d7a720efSLois Curfman McInnes .seealso: SNESSetTrustRegionTolerance(), SNESSetMinimizationFunctionTolerance() 10759b94acceSBarry Smith @*/ 1076d7a720efSLois Curfman McInnes int SNESSetTolerances(SNES snes,double atol,double rtol,double stol,int maxit,int maxf) 10779b94acceSBarry Smith { 107877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1079d7a720efSLois Curfman McInnes if (atol != PETSC_DEFAULT) snes->atol = atol; 1080d7a720efSLois Curfman McInnes if (rtol != PETSC_DEFAULT) snes->rtol = rtol; 1081d7a720efSLois Curfman McInnes if (stol != PETSC_DEFAULT) snes->xtol = stol; 1082d7a720efSLois Curfman McInnes if (maxit != PETSC_DEFAULT) snes->max_its = maxit; 1083d7a720efSLois Curfman McInnes if (maxf != PETSC_DEFAULT) snes->max_funcs = maxf; 10849b94acceSBarry Smith return 0; 10859b94acceSBarry Smith } 10869b94acceSBarry Smith 10879b94acceSBarry Smith /*@ 10889b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 10899b94acceSBarry Smith 10909b94acceSBarry Smith Input Parameters: 10919b94acceSBarry Smith . snes - the SNES context 10929b94acceSBarry Smith . tol - tolerance 10939b94acceSBarry Smith 10949b94acceSBarry Smith Options Database Key: 1095d7a720efSLois Curfman McInnes $ -snes_trtol <tol> 10969b94acceSBarry Smith 10979b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance 10989b94acceSBarry Smith 1099d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetMinimizationFunctionTolerance() 11009b94acceSBarry Smith @*/ 11019b94acceSBarry Smith int SNESSetTrustRegionTolerance(SNES snes,double tol) 11029b94acceSBarry Smith { 110377c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 11049b94acceSBarry Smith snes->deltatol = tol; 11059b94acceSBarry Smith return 0; 11069b94acceSBarry Smith } 11079b94acceSBarry Smith 11089b94acceSBarry Smith /*@ 110977c4ece6SBarry Smith SNESSetMinimizationFunctionTolerance - Sets the minimum allowable function tolerance 11109b94acceSBarry Smith for unconstrained minimization solvers. 11119b94acceSBarry Smith 11129b94acceSBarry Smith Input Parameters: 11139b94acceSBarry Smith . snes - the SNES context 11149b94acceSBarry Smith . ftol - minimum function tolerance 11159b94acceSBarry Smith 11169b94acceSBarry Smith Options Database Key: 1117d7a720efSLois Curfman McInnes $ -snes_fmin <ftol> 11189b94acceSBarry Smith 11199b94acceSBarry Smith Note: 112077c4ece6SBarry Smith SNESSetMinimizationFunctionTolerance() is valid for SNES_UNCONSTRAINED_MINIMIZATION 11219b94acceSBarry Smith methods only. 11229b94acceSBarry Smith 11239b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimum, convergence, function, tolerance 11249b94acceSBarry Smith 1125d7a720efSLois Curfman McInnes .seealso: SNESSetTolerances(), SNESSetTrustRegionTolerance() 11269b94acceSBarry Smith @*/ 112777c4ece6SBarry Smith int SNESSetMinimizationFunctionTolerance(SNES snes,double ftol) 11289b94acceSBarry Smith { 112977c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 11309b94acceSBarry Smith snes->fmin = ftol; 11319b94acceSBarry Smith return 0; 11329b94acceSBarry Smith } 11339b94acceSBarry Smith 11349b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 11359b94acceSBarry Smith 11369b94acceSBarry Smith /*@C 11379b94acceSBarry Smith SNESSetMonitor - Sets the function that is to be used at every 11389b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 11399b94acceSBarry Smith progress. 11409b94acceSBarry Smith 11419b94acceSBarry Smith Input Parameters: 11429b94acceSBarry Smith . snes - the SNES context 11439b94acceSBarry Smith . func - monitoring routine 11449b94acceSBarry Smith . mctx - optional user-defined context for private data for the 11459b94acceSBarry Smith monitor routine (may be null) 11469b94acceSBarry Smith 11479b94acceSBarry Smith Calling sequence of func: 1148682d7d0cSBarry Smith int func(SNES snes,int its, Vec x,Vec f,double norm,void *mctx) 11499b94acceSBarry Smith 11509b94acceSBarry Smith $ snes - the SNES context 11519b94acceSBarry Smith $ its - iteration number 11529b94acceSBarry Smith $ mctx - optional monitoring context 11539b94acceSBarry Smith $ 11549b94acceSBarry Smith $ SNES_NONLINEAR_EQUATIONS methods: 11559b94acceSBarry Smith $ norm - 2-norm function value (may be estimated) 11569b94acceSBarry Smith $ 11579b94acceSBarry Smith $ SNES_UNCONSTRAINED_MINIMIZATION methods: 11589b94acceSBarry Smith $ norm - 2-norm gradient value (may be estimated) 11599b94acceSBarry Smith 11609b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor 11619b94acceSBarry Smith 11629b94acceSBarry Smith .seealso: SNESDefaultMonitor() 11639b94acceSBarry Smith @*/ 116474679c65SBarry Smith int SNESSetMonitor( SNES snes, int (*func)(SNES,int,double,void*),void *mctx ) 11659b94acceSBarry Smith { 11669b94acceSBarry Smith snes->monitor = func; 116774679c65SBarry Smith snes->monP = mctx; 11689b94acceSBarry Smith return 0; 11699b94acceSBarry Smith } 11709b94acceSBarry Smith 11719b94acceSBarry Smith /*@C 11729b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 11739b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 11749b94acceSBarry Smith 11759b94acceSBarry Smith Input Parameters: 11769b94acceSBarry Smith . snes - the SNES context 11779b94acceSBarry Smith . func - routine to test for convergence 11789b94acceSBarry Smith . cctx - optional context for private data for the convergence routine 11799b94acceSBarry Smith (may be null) 11809b94acceSBarry Smith 11819b94acceSBarry Smith Calling sequence of func: 11829b94acceSBarry Smith int func (SNES snes,double xnorm,double gnorm, 11839b94acceSBarry Smith double f,void *cctx) 11849b94acceSBarry Smith 11859b94acceSBarry Smith $ snes - the SNES context 11869b94acceSBarry Smith $ cctx - optional convergence context 11879b94acceSBarry Smith $ xnorm - 2-norm of current iterate 11889b94acceSBarry Smith $ 11899b94acceSBarry Smith $ SNES_NONLINEAR_EQUATIONS methods: 11909b94acceSBarry Smith $ gnorm - 2-norm of current step 11919b94acceSBarry Smith $ f - 2-norm of function 11929b94acceSBarry Smith $ 11939b94acceSBarry Smith $ SNES_UNCONSTRAINED_MINIMIZATION methods: 11949b94acceSBarry Smith $ gnorm - 2-norm of current gradient 11959b94acceSBarry Smith $ f - function value 11969b94acceSBarry Smith 11979b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test 11989b94acceSBarry Smith 119940191667SLois Curfman McInnes .seealso: SNESConverged_EQ_LS(), SNESConverged_EQ_TR(), 120040191667SLois Curfman McInnes SNESConverged_UM_LS(), SNESConverged_UM_TR() 12019b94acceSBarry Smith @*/ 120274679c65SBarry Smith int SNESSetConvergenceTest(SNES snes,int (*func)(SNES,double,double,double,void*),void *cctx) 12039b94acceSBarry Smith { 12049b94acceSBarry Smith (snes)->converged = func; 12059b94acceSBarry Smith (snes)->cnvP = cctx; 12069b94acceSBarry Smith return 0; 12079b94acceSBarry Smith } 12089b94acceSBarry Smith 12099b94acceSBarry Smith /* 12109b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 12119b94acceSBarry Smith positive parameter delta. 12129b94acceSBarry Smith 12139b94acceSBarry Smith Input Parameters: 12149b94acceSBarry Smith . snes - the SNES context 12159b94acceSBarry Smith . y - approximate solution of linear system 12169b94acceSBarry Smith . fnorm - 2-norm of current function 12179b94acceSBarry Smith . delta - trust region size 12189b94acceSBarry Smith 12199b94acceSBarry Smith Output Parameters: 12209b94acceSBarry Smith . gpnorm - predicted function norm at the new point, assuming local 12219b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 12229b94acceSBarry Smith region, and exceeds zero otherwise. 12239b94acceSBarry Smith . ynorm - 2-norm of the step 12249b94acceSBarry Smith 12259b94acceSBarry Smith Note: 122640191667SLois Curfman McInnes For non-trust region methods such as SNES_EQ_LS, the parameter delta 12279b94acceSBarry Smith is set to be the maximum allowable step size. 12289b94acceSBarry Smith 12299b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step 12309b94acceSBarry Smith */ 12319b94acceSBarry Smith int SNESScaleStep_Private(SNES snes,Vec y,double *fnorm,double *delta, 12329b94acceSBarry Smith double *gpnorm,double *ynorm) 12339b94acceSBarry Smith { 12349b94acceSBarry Smith double norm; 12359b94acceSBarry Smith Scalar cnorm; 1236cddf8d76SBarry Smith VecNorm(y,NORM_2, &norm ); 12379b94acceSBarry Smith if (norm > *delta) { 12389b94acceSBarry Smith norm = *delta/norm; 12399b94acceSBarry Smith *gpnorm = (1.0 - norm)*(*fnorm); 12409b94acceSBarry Smith cnorm = norm; 12419b94acceSBarry Smith VecScale( &cnorm, y ); 12429b94acceSBarry Smith *ynorm = *delta; 12439b94acceSBarry Smith } else { 12449b94acceSBarry Smith *gpnorm = 0.0; 12459b94acceSBarry Smith *ynorm = norm; 12469b94acceSBarry Smith } 12479b94acceSBarry Smith return 0; 12489b94acceSBarry Smith } 12499b94acceSBarry Smith 12509b94acceSBarry Smith /*@ 12519b94acceSBarry Smith SNESSolve - Solves a nonlinear system. Call SNESSolve after calling 125263a78c88SLois Curfman McInnes SNESCreate() and optional routines of the form SNESSetXXX(). 12539b94acceSBarry Smith 12549b94acceSBarry Smith Input Parameter: 12559b94acceSBarry Smith . snes - the SNES context 12568ddd3da0SLois Curfman McInnes . x - the solution vector 12579b94acceSBarry Smith 12589b94acceSBarry Smith Output Parameter: 12599b94acceSBarry Smith its - number of iterations until termination 12609b94acceSBarry Smith 12618ddd3da0SLois Curfman McInnes Note: 12628ddd3da0SLois Curfman McInnes The user should initialize the vector, x, with the initial guess 12638ddd3da0SLois Curfman McInnes for the nonlinear solve prior to calling SNESSolve. In particular, 12648ddd3da0SLois Curfman McInnes to employ an initial guess of zero, the user should explicitly set 12658ddd3da0SLois Curfman McInnes this vector to zero by calling VecSet(). 12668ddd3da0SLois Curfman McInnes 12679b94acceSBarry Smith .keywords: SNES, nonlinear, solve 12689b94acceSBarry Smith 126963a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESDestroy() 12709b94acceSBarry Smith @*/ 12718ddd3da0SLois Curfman McInnes int SNESSolve(SNES snes,Vec x,int *its) 12729b94acceSBarry Smith { 12733c7409f5SSatish Balay int ierr, flg; 1274052efed2SBarry Smith 127577c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 127674679c65SBarry Smith PetscValidIntPointer(its); 1277c4fc05e7SBarry Smith if (!snes->setup_called) {ierr = SNESSetUp(snes,x); CHKERRQ(ierr);} 1278c4fc05e7SBarry Smith else {snes->vec_sol = snes->vec_sol_always = x;} 12799b94acceSBarry Smith PLogEventBegin(SNES_Solve,snes,0,0,0); 12809b94acceSBarry Smith ierr = (*(snes)->solve)(snes,its); CHKERRQ(ierr); 12819b94acceSBarry Smith PLogEventEnd(SNES_Solve,snes,0,0,0); 12823c7409f5SSatish Balay ierr = OptionsHasName(PETSC_NULL,"-snes_view", &flg); CHKERRQ(ierr); 12836d4a8577SBarry Smith if (flg) { ierr = SNESView(snes,VIEWER_STDOUT_WORLD); CHKERRQ(ierr); } 12849b94acceSBarry Smith return 0; 12859b94acceSBarry Smith } 12869b94acceSBarry Smith 12879b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 128837fcc0dbSBarry Smith static NRList *__SNESList = 0; 12899b94acceSBarry Smith 12909b94acceSBarry Smith /*@ 12914b0e389bSBarry Smith SNESSetType - Sets the method for the nonlinear solver. 12929b94acceSBarry Smith 12939b94acceSBarry Smith Input Parameters: 12949b94acceSBarry Smith . snes - the SNES context 12959b94acceSBarry Smith . method - a known method 12969b94acceSBarry Smith 12979b94acceSBarry Smith Notes: 12989b94acceSBarry Smith See "petsc/include/snes.h" for available methods (for instance) 12999b94acceSBarry Smith $ Systems of nonlinear equations: 130040191667SLois Curfman McInnes $ SNES_EQ_LS - Newton's method with line search 130140191667SLois Curfman McInnes $ SNES_EQ_TR - Newton's method with trust region 13029b94acceSBarry Smith $ Unconstrained minimization: 130340191667SLois Curfman McInnes $ SNES_UM_TR - Newton's method with trust region 130440191667SLois Curfman McInnes $ SNES_UM_LS - Newton's method with line search 13059b94acceSBarry Smith 13069b94acceSBarry Smith Options Database Command: 13074b0e389bSBarry Smith $ -snes_type <method> 13089b94acceSBarry Smith $ Use -help for a list of available methods 13099b94acceSBarry Smith $ (for instance, ls or tr) 1310a703fe33SLois Curfman McInnes 1311a703fe33SLois Curfman McInnes .keysords: SNES, set, method 13129b94acceSBarry Smith @*/ 13134b0e389bSBarry Smith int SNESSetType(SNES snes,SNESType method) 13149b94acceSBarry Smith { 13159b94acceSBarry Smith int (*r)(SNES); 131677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 13177d1a2b2bSBarry Smith if (snes->type == (int) method) return 0; 13187d1a2b2bSBarry Smith 13199b94acceSBarry Smith /* Get the function pointers for the iterative method requested */ 132037fcc0dbSBarry Smith if (!__SNESList) {SNESRegisterAll();} 132137fcc0dbSBarry Smith if (!__SNESList) {SETERRQ(1,"SNESSetType:Could not get methods");} 132237fcc0dbSBarry Smith r = (int (*)(SNES))NRFindRoutine( __SNESList, (int)method, (char *)0 ); 13234b0e389bSBarry Smith if (!r) {SETERRQ(1,"SNESSetType:Unknown method");} 13240452661fSBarry Smith if (snes->data) PetscFree(snes->data); 13259b94acceSBarry Smith snes->set_method_called = 1; 13269b94acceSBarry Smith return (*r)(snes); 13279b94acceSBarry Smith } 13289b94acceSBarry Smith 13299b94acceSBarry Smith /* --------------------------------------------------------------------- */ 13309b94acceSBarry Smith /*@C 13319b94acceSBarry Smith SNESRegister - Adds the method to the nonlinear solver package, given 13324b0e389bSBarry Smith a function pointer and a nonlinear solver name of the type SNESType. 13339b94acceSBarry Smith 13349b94acceSBarry Smith Input Parameters: 133540191667SLois Curfman McInnes . name - for instance SNES_EQ_LS, SNES_EQ_TR, ... 133640191667SLois Curfman McInnes . sname - corresponding string for name 13379b94acceSBarry Smith . create - routine to create method context 13389b94acceSBarry Smith 13399b94acceSBarry Smith .keywords: SNES, nonlinear, register 13409b94acceSBarry Smith 13419b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterDestroy() 13429b94acceSBarry Smith @*/ 13439b94acceSBarry Smith int SNESRegister(int name, char *sname, int (*create)(SNES)) 13449b94acceSBarry Smith { 13459b94acceSBarry Smith int ierr; 134637fcc0dbSBarry Smith if (!__SNESList) {ierr = NRCreate(&__SNESList); CHKERRQ(ierr);} 134737fcc0dbSBarry Smith NRRegister( __SNESList, name, sname, (int (*)(void*))create ); 13489b94acceSBarry Smith return 0; 13499b94acceSBarry Smith } 13509b94acceSBarry Smith /* --------------------------------------------------------------------- */ 13519b94acceSBarry Smith /*@C 13529b94acceSBarry Smith SNESRegisterDestroy - Frees the list of nonlinear solvers that were 13539b94acceSBarry Smith registered by SNESRegister(). 13549b94acceSBarry Smith 13559b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy 13569b94acceSBarry Smith 13579b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll() 13589b94acceSBarry Smith @*/ 13599b94acceSBarry Smith int SNESRegisterDestroy() 13609b94acceSBarry Smith { 136137fcc0dbSBarry Smith if (__SNESList) { 136237fcc0dbSBarry Smith NRDestroy( __SNESList ); 136337fcc0dbSBarry Smith __SNESList = 0; 13649b94acceSBarry Smith } 13659b94acceSBarry Smith return 0; 13669b94acceSBarry Smith } 13679b94acceSBarry Smith 13689b94acceSBarry Smith /* 13694b0e389bSBarry Smith SNESGetTypeFromOptions_Private - Sets the selected method from the 13709b94acceSBarry Smith options database. 13719b94acceSBarry Smith 13729b94acceSBarry Smith Input Parameter: 13739b94acceSBarry Smith . ctx - the SNES context 13749b94acceSBarry Smith 13759b94acceSBarry Smith Output Parameter: 13769b94acceSBarry Smith . method - solver method 13779b94acceSBarry Smith 13789b94acceSBarry Smith Returns: 13799b94acceSBarry Smith Returns 1 if the method is found; 0 otherwise. 13809b94acceSBarry Smith 13819b94acceSBarry Smith Options Database Key: 13824b0e389bSBarry Smith $ -snes_type method 13839b94acceSBarry Smith */ 1384052efed2SBarry Smith int SNESGetTypeFromOptions_Private(SNES ctx,SNESType *method,int *flg) 13859b94acceSBarry Smith { 1386052efed2SBarry Smith int ierr; 13879b94acceSBarry Smith char sbuf[50]; 1388052efed2SBarry Smith ierr = OptionsGetString(ctx->prefix,"-snes_type", sbuf, 50, flg); CHKERRQ(ierr); 1389052efed2SBarry Smith if (*flg) { 1390052efed2SBarry Smith if (!__SNESList) {ierr = SNESRegisterAll(); CHKERRQ(ierr);} 139137fcc0dbSBarry Smith *method = (SNESType)NRFindID( __SNESList, sbuf ); 13929b94acceSBarry Smith } 13939b94acceSBarry Smith return 0; 13949b94acceSBarry Smith } 13959b94acceSBarry Smith 13969b94acceSBarry Smith /*@C 13979a28b0a6SLois Curfman McInnes SNESGetType - Gets the SNES method type and name (as a string). 13989b94acceSBarry Smith 13999b94acceSBarry Smith Input Parameter: 14004b0e389bSBarry Smith . snes - nonlinear solver context 14019b94acceSBarry Smith 14029b94acceSBarry Smith Output Parameter: 14039a28b0a6SLois Curfman McInnes . method - SNES method (or use PETSC_NULL) 14049a28b0a6SLois Curfman McInnes . name - name of SNES method (or use PETSC_NULL) 14059b94acceSBarry Smith 14069b94acceSBarry Smith .keywords: SNES, nonlinear, get, method, name 14079b94acceSBarry Smith @*/ 14084b0e389bSBarry Smith int SNESGetType(SNES snes, SNESType *method,char **name) 14099b94acceSBarry Smith { 141006a9b0adSLois Curfman McInnes int ierr; 141137fcc0dbSBarry Smith if (!__SNESList) {ierr = SNESRegisterAll(); CHKERRQ(ierr);} 14124b0e389bSBarry Smith if (method) *method = (SNESType) snes->type; 141337fcc0dbSBarry Smith if (name) *name = NRFindName( __SNESList, (int) snes->type ); 14149b94acceSBarry Smith return 0; 14159b94acceSBarry Smith } 14169b94acceSBarry Smith 14179b94acceSBarry Smith #include <stdio.h> 14189b94acceSBarry Smith /* 14194b0e389bSBarry Smith SNESPrintTypes_Private - Prints the SNES methods available from the 14209b94acceSBarry Smith options database. 14219b94acceSBarry Smith 14229b94acceSBarry Smith Input Parameters: 142333455573SSatish Balay . comm - communicator (usually MPI_COMM_WORLD) 14249b94acceSBarry Smith . prefix - prefix (usually "-") 14254b0e389bSBarry Smith . name - the options database name (by default "snes_type") 14269b94acceSBarry Smith */ 142733455573SSatish Balay int SNESPrintTypes_Private(MPI_Comm comm,char* prefix,char *name) 14289b94acceSBarry Smith { 14299b94acceSBarry Smith FuncList *entry; 143037fcc0dbSBarry Smith if (!__SNESList) {SNESRegisterAll();} 143137fcc0dbSBarry Smith entry = __SNESList->head; 143277c4ece6SBarry Smith PetscPrintf(comm," %s%s (one of)",prefix,name); 14339b94acceSBarry Smith while (entry) { 143477c4ece6SBarry Smith PetscPrintf(comm," %s",entry->name); 14359b94acceSBarry Smith entry = entry->next; 14369b94acceSBarry Smith } 143777c4ece6SBarry Smith PetscPrintf(comm,"\n"); 14389b94acceSBarry Smith return 0; 14399b94acceSBarry Smith } 14409b94acceSBarry Smith 14419b94acceSBarry Smith /*@C 14429b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution 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 14509b94acceSBarry Smith 14519b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution 14529b94acceSBarry Smith 1453a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESGetGradient(), SNESGetSolutionUpdate() 14549b94acceSBarry Smith @*/ 14559b94acceSBarry Smith int SNESGetSolution(SNES snes,Vec *x) 14569b94acceSBarry Smith { 145777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 14589b94acceSBarry Smith *x = snes->vec_sol_always; 14599b94acceSBarry Smith return 0; 14609b94acceSBarry Smith } 14619b94acceSBarry Smith 14629b94acceSBarry Smith /*@C 14639b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 14649b94acceSBarry Smith stored. 14659b94acceSBarry Smith 14669b94acceSBarry Smith Input Parameter: 14679b94acceSBarry Smith . snes - the SNES context 14689b94acceSBarry Smith 14699b94acceSBarry Smith Output Parameter: 14709b94acceSBarry Smith . x - the solution update 14719b94acceSBarry Smith 14729b94acceSBarry Smith Notes: 14739b94acceSBarry Smith This vector is implementation dependent. 14749b94acceSBarry Smith 14759b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update 14769b94acceSBarry Smith 14779b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction 14789b94acceSBarry Smith @*/ 14799b94acceSBarry Smith int SNESGetSolutionUpdate(SNES snes,Vec *x) 14809b94acceSBarry Smith { 148177c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 14829b94acceSBarry Smith *x = snes->vec_sol_update_always; 14839b94acceSBarry Smith return 0; 14849b94acceSBarry Smith } 14859b94acceSBarry Smith 14869b94acceSBarry Smith /*@C 14873638b69dSLois Curfman McInnes SNESGetFunction - Returns the vector where the function is stored. 14889b94acceSBarry Smith 14899b94acceSBarry Smith Input Parameter: 14909b94acceSBarry Smith . snes - the SNES context 14919b94acceSBarry Smith 14929b94acceSBarry Smith Output Parameter: 14933638b69dSLois Curfman McInnes . r - the function 14949b94acceSBarry Smith 14959b94acceSBarry Smith Notes: 14969b94acceSBarry Smith SNESGetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only 14979b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 14989b94acceSBarry Smith SNESGetMinimizationFunction() and SNESGetGradient(); 14999b94acceSBarry Smith 1500a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function 15019b94acceSBarry Smith 150231615d04SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution(), SNESGetMinimizationFunction(), 150331615d04SLois Curfman McInnes SNESGetGradient() 15049b94acceSBarry Smith @*/ 15059b94acceSBarry Smith int SNESGetFunction(SNES snes,Vec *r) 15069b94acceSBarry Smith { 150777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 15089b94acceSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1, 150948d91487SBarry Smith "SNESGetFunction:For SNES_NONLINEAR_EQUATIONS only"); 15109b94acceSBarry Smith *r = snes->vec_func_always; 15119b94acceSBarry Smith return 0; 15129b94acceSBarry Smith } 15139b94acceSBarry Smith 15149b94acceSBarry Smith /*@C 15153638b69dSLois Curfman McInnes SNESGetGradient - Returns the vector where the gradient is stored. 15169b94acceSBarry Smith 15179b94acceSBarry Smith Input Parameter: 15189b94acceSBarry Smith . snes - the SNES context 15199b94acceSBarry Smith 15209b94acceSBarry Smith Output Parameter: 15219b94acceSBarry Smith . r - the gradient 15229b94acceSBarry Smith 15239b94acceSBarry Smith Notes: 15249b94acceSBarry Smith SNESGetGradient() is valid for SNES_UNCONSTRAINED_MINIMIZATION methods 15259b94acceSBarry Smith only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 15269b94acceSBarry Smith SNESGetFunction(). 15279b94acceSBarry Smith 15289b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient 15299b94acceSBarry Smith 153031615d04SLois Curfman McInnes .seealso: SNESGetMinimizationFunction(), SNESGetSolution(), SNESGetFunction() 15319b94acceSBarry Smith @*/ 15329b94acceSBarry Smith int SNESGetGradient(SNES snes,Vec *r) 15339b94acceSBarry Smith { 153477c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 15359b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 153648d91487SBarry Smith "SNESGetGradient:For SNES_UNCONSTRAINED_MINIMIZATION only"); 15379b94acceSBarry Smith *r = snes->vec_func_always; 15389b94acceSBarry Smith return 0; 15399b94acceSBarry Smith } 15409b94acceSBarry Smith 15419b94acceSBarry Smith /*@ 15429b94acceSBarry Smith SNESGetMinimizationFunction - Returns the scalar function value for 15439b94acceSBarry Smith unconstrained minimization problems. 15449b94acceSBarry Smith 15459b94acceSBarry Smith Input Parameter: 15469b94acceSBarry Smith . snes - the SNES context 15479b94acceSBarry Smith 15489b94acceSBarry Smith Output Parameter: 15499b94acceSBarry Smith . r - the function 15509b94acceSBarry Smith 15519b94acceSBarry Smith Notes: 15529b94acceSBarry Smith SNESGetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 15539b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 15549b94acceSBarry Smith SNESGetFunction(). 15559b94acceSBarry Smith 15569b94acceSBarry Smith .keywords: SNES, nonlinear, get, function 15579b94acceSBarry Smith 155831615d04SLois Curfman McInnes .seealso: SNESGetGradient(), SNESGetSolution(), SNESGetFunction() 15599b94acceSBarry Smith @*/ 15609b94acceSBarry Smith int SNESGetMinimizationFunction(SNES snes,double *r) 15619b94acceSBarry Smith { 156277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 156374679c65SBarry Smith PetscValidScalarPointer(r); 15649b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 156548d91487SBarry Smith "SNESGetMinimizationFunction:For SNES_UNCONSTRAINED_MINIMIZATION only"); 15669b94acceSBarry Smith *r = snes->fc; 15679b94acceSBarry Smith return 0; 15689b94acceSBarry Smith } 15699b94acceSBarry Smith 15703c7409f5SSatish Balay /*@C 15713c7409f5SSatish Balay SNESSetOptionsPrefix - Sets 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, set, options, prefix, database 1579a86d99e1SLois Curfman McInnes 1580a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions() 15813c7409f5SSatish Balay @*/ 15823c7409f5SSatish Balay int SNESSetOptionsPrefix(SNES snes,char *prefix) 15833c7409f5SSatish Balay { 15843c7409f5SSatish Balay int ierr; 15853c7409f5SSatish Balay 158677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 15873c7409f5SSatish Balay ierr = PetscObjectSetPrefix((PetscObject)snes, prefix); CHKERRQ(ierr); 15883c7409f5SSatish Balay ierr = SLESSetOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr); 15893c7409f5SSatish Balay return 0; 15903c7409f5SSatish Balay } 15913c7409f5SSatish Balay 15923c7409f5SSatish Balay /*@C 15933c7409f5SSatish Balay SNESAppendOptionsPrefix - Append to 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 . prefix - the prefix to prepend to all option names 15993c7409f5SSatish Balay 16003c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database 1601a86d99e1SLois Curfman McInnes 1602a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix() 16033c7409f5SSatish Balay @*/ 16043c7409f5SSatish Balay int SNESAppendOptionsPrefix(SNES snes,char *prefix) 16053c7409f5SSatish Balay { 16063c7409f5SSatish Balay int ierr; 16073c7409f5SSatish Balay 160877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 16093c7409f5SSatish Balay ierr = PetscObjectAppendPrefix((PetscObject)snes, prefix); CHKERRQ(ierr); 16103c7409f5SSatish Balay ierr = SLESAppendOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr); 16113c7409f5SSatish Balay return 0; 16123c7409f5SSatish Balay } 16133c7409f5SSatish Balay 1614c83590e2SSatish Balay /*@ 16153c7409f5SSatish Balay SNESGetOptionsPrefix - Sets the prefix used for searching for all 16163c7409f5SSatish Balay SNES options in the database. 16173c7409f5SSatish Balay 16183c7409f5SSatish Balay Input Parameter: 16193c7409f5SSatish Balay . snes - the SNES context 16203c7409f5SSatish Balay 16213c7409f5SSatish Balay Output Parameter: 16223c7409f5SSatish Balay . prefix - pointer to the prefix string used 16233c7409f5SSatish Balay 16243c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database 1625a86d99e1SLois Curfman McInnes 1626a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix() 16273c7409f5SSatish Balay @*/ 16283c7409f5SSatish Balay int SNESGetOptionsPrefix(SNES snes,char **prefix) 16293c7409f5SSatish Balay { 16303c7409f5SSatish Balay int ierr; 16313c7409f5SSatish Balay 163277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 16333c7409f5SSatish Balay ierr = PetscObjectGetPrefix((PetscObject)snes, prefix); CHKERRQ(ierr); 16343c7409f5SSatish Balay return 0; 16353c7409f5SSatish Balay } 16363c7409f5SSatish Balay 16373c7409f5SSatish Balay 16389b94acceSBarry Smith 16399b94acceSBarry Smith 16409b94acceSBarry Smith 1641