19b94acceSBarry Smith #ifndef lint 2*83e56afdSLois Curfman McInnes static char vcid[] = "$Id: snes.c,v 1.62 1996/03/19 21:28:59 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 } 7619bcc07fSBarry Smith } 779b94acceSBarry Smith SNESGetSLES(snes,&sles); 789b94acceSBarry Smith ierr = SLESView(sles,viewer); CHKERRQ(ierr); 799b94acceSBarry Smith return 0; 809b94acceSBarry Smith } 819b94acceSBarry Smith 829b94acceSBarry Smith /*@ 83682d7d0cSBarry Smith SNESSetFromOptions - Sets various SNES and SLES parameters from user options. 849b94acceSBarry Smith 859b94acceSBarry Smith Input Parameter: 869b94acceSBarry Smith . snes - the SNES context 879b94acceSBarry Smith 889b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database 899b94acceSBarry Smith 90a86d99e1SLois Curfman McInnes .seealso: SNESPrintHelp(), SNESSetOptionsPrefix() 919b94acceSBarry Smith @*/ 929b94acceSBarry Smith int SNESSetFromOptions(SNES snes) 939b94acceSBarry Smith { 944b0e389bSBarry Smith SNESType method; 959b94acceSBarry Smith double tmp; 969b94acceSBarry Smith SLES sles; 973c7409f5SSatish Balay int ierr, flg; 983c7409f5SSatish Balay int version = PETSC_DEFAULT; 999b94acceSBarry Smith double rtol_0 = PETSC_DEFAULT; 1009b94acceSBarry Smith double rtol_max = PETSC_DEFAULT; 1019b94acceSBarry Smith double gamma2 = PETSC_DEFAULT; 1029b94acceSBarry Smith double alpha = PETSC_DEFAULT; 1039b94acceSBarry Smith double alpha2 = PETSC_DEFAULT; 1049b94acceSBarry Smith double threshold = PETSC_DEFAULT; 1059b94acceSBarry Smith 10677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1074b0e389bSBarry Smith if (snes->setup_called)SETERRQ(1,"SNESSetFromOptions:Must call prior to SNESSetUp!"); 108052efed2SBarry Smith ierr = SNESGetTypeFromOptions_Private(snes,&method,&flg); CHKERRQ(ierr); 109052efed2SBarry Smith if (flg) { 110052efed2SBarry Smith ierr = SNESSetType(snes,method); CHKERRQ(ierr); 1119b94acceSBarry Smith } 1125334005bSBarry Smith else if (!snes->set_method_called) { 1135334005bSBarry Smith if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 1145334005bSBarry Smith ierr = SNESSetType(snes,SNES_EQ_NLS); CHKERRQ(ierr); 1155334005bSBarry Smith } 1165334005bSBarry Smith else { 1175334005bSBarry Smith ierr = SNESSetType(snes,SNES_UM_NTR); CHKERRQ(ierr); 1185334005bSBarry Smith } 1195334005bSBarry Smith } 1205334005bSBarry Smith 1213c7409f5SSatish Balay ierr = OptionsHasName(PETSC_NULL,"-help", &flg); CHKERRQ(ierr); 1223c7409f5SSatish Balay if (flg) { SNESPrintHelp(snes); } 1233c7409f5SSatish Balay ierr = OptionsGetDouble(snes->prefix,"-snes_stol",&tmp, &flg); CHKERRQ(ierr); 1243c7409f5SSatish Balay if (flg) { SNESSetSolutionTolerance(snes,tmp); } 1253c7409f5SSatish Balay ierr = OptionsGetDouble(snes->prefix,"-snes_atol",&tmp, &flg); CHKERRQ(ierr); 1263c7409f5SSatish Balay if (flg) { SNESSetAbsoluteTolerance(snes,tmp); } 1273c7409f5SSatish Balay ierr = OptionsGetDouble(snes->prefix,"-snes_trtol",&tmp, &flg); CHKERRQ(ierr); 1283c7409f5SSatish Balay if (flg) { SNESSetTrustRegionTolerance(snes,tmp); } 1293c7409f5SSatish Balay ierr = OptionsGetDouble(snes->prefix,"-snes_rtol",&tmp, &flg); CHKERRQ(ierr); 1303c7409f5SSatish Balay if (flg) { SNESSetRelativeTolerance(snes,tmp); } 1313c7409f5SSatish Balay ierr = OptionsGetDouble(snes->prefix,"-snes_fmin",&tmp, &flg); CHKERRQ(ierr); 13277c4ece6SBarry Smith if (flg) { SNESSetMinimizationFunctionTolerance(snes,tmp); } 1333c7409f5SSatish Balay ierr = OptionsGetInt(snes->prefix,"-snes_max_it",&snes->max_its, &flg); CHKERRQ(ierr); 1343c7409f5SSatish Balay ierr = OptionsGetInt(snes->prefix,"-snes_max_funcs",&snes->max_funcs, &flg);CHKERRQ(ierr); 1353c7409f5SSatish Balay ierr = OptionsHasName(snes->prefix,"-snes_ksp_ew_conv", &flg); CHKERRQ(ierr); 1363c7409f5SSatish Balay if (flg) { snes->ksp_ewconv = 1; } 1373c7409f5SSatish Balay ierr = OptionsGetInt(snes->prefix,"-snes_ksp_ew_version",&version, \ 1383c7409f5SSatish Balay &flg); CHKERRQ(ierr); 1393c7409f5SSatish Balay ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtol0",&rtol_0, \ 1403c7409f5SSatish Balay &flg); CHKERRQ(ierr); 1413c7409f5SSatish Balay ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtolmax",&rtol_max, \ 1423c7409f5SSatish Balay &flg); CHKERRQ(ierr); 1433c7409f5SSatish Balay ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_gamma",&gamma2, \ 1443c7409f5SSatish Balay &flg); CHKERRQ(ierr); 1453c7409f5SSatish Balay ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha",&alpha, \ 1463c7409f5SSatish Balay &flg); CHKERRQ(ierr); 1473c7409f5SSatish Balay ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha2",&alpha2, \ 1483c7409f5SSatish Balay &flg); CHKERRQ(ierr); 1493c7409f5SSatish Balay ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_threshold",&threshold, \ 1503c7409f5SSatish Balay &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); 1669b94acceSBarry Smith PLogObjectParent(snes,lg); 1679b94acceSBarry Smith } 1689b94acceSBarry Smith } 1693c7409f5SSatish Balay ierr = OptionsHasName(snes->prefix,"-snes_fd", &flg); CHKERRQ(ierr); 1703c7409f5SSatish Balay if (flg && snes->method_class == SNES_NONLINEAR_EQUATIONS) { 1719b94acceSBarry Smith ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre, 1729b94acceSBarry Smith SNESDefaultComputeJacobian,snes->funP); CHKERRQ(ierr); 17331615d04SLois Curfman McInnes PLogInfo((PetscObject)snes, 17431615d04SLois Curfman McInnes "SNESSetFromOptions: Setting default finite difference Jacobian matrix\n"); 17531615d04SLois Curfman McInnes } 17631615d04SLois Curfman McInnes else if (flg && snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 17731615d04SLois Curfman McInnes ierr = SNESSetHessian(snes,snes->jacobian,snes->jacobian_pre, 17831615d04SLois Curfman McInnes SNESDefaultComputeHessian,snes->funP); CHKERRQ(ierr); 17931615d04SLois Curfman McInnes PLogInfo((PetscObject)snes, 18031615d04SLois Curfman McInnes "SNESSetFromOptions: Setting default finite difference Hessian matrix\n"); 1819b94acceSBarry Smith } 1829b94acceSBarry Smith ierr = SNESGetSLES(snes,&sles); CHKERRQ(ierr); 1839b94acceSBarry Smith ierr = SLESSetFromOptions(sles); CHKERRQ(ierr); 1849b94acceSBarry Smith if (!snes->setfromoptions) return 0; 1859b94acceSBarry Smith return (*snes->setfromoptions)(snes); 1869b94acceSBarry Smith } 1879b94acceSBarry Smith 1889b94acceSBarry Smith /*@ 1899b94acceSBarry Smith SNESPrintHelp - Prints all options for the SNES component. 1909b94acceSBarry Smith 1919b94acceSBarry Smith Input Parameter: 1929b94acceSBarry Smith . snes - the SNES context 1939b94acceSBarry Smith 194a703fe33SLois Curfman McInnes Options Database Keys: 195a703fe33SLois Curfman McInnes $ -help, -h 196a703fe33SLois Curfman McInnes 1979b94acceSBarry Smith .keywords: SNES, nonlinear, help 1989b94acceSBarry Smith 199682d7d0cSBarry Smith .seealso: SNESSetFromOptions() 2009b94acceSBarry Smith @*/ 2019b94acceSBarry Smith int SNESPrintHelp(SNES snes) 2029b94acceSBarry Smith { 2036daaf66cSBarry Smith char p[64]; 2046daaf66cSBarry Smith SNES_KSP_EW_ConvCtx *kctx; 2056daaf66cSBarry Smith 20677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2076daaf66cSBarry Smith 2086daaf66cSBarry Smith PetscStrcpy(p,"-"); 2096daaf66cSBarry Smith if (snes->prefix) PetscStrcat(p, snes->prefix); 2106daaf66cSBarry Smith 2116daaf66cSBarry Smith kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx; 2126daaf66cSBarry Smith 21377c4ece6SBarry Smith PetscPrintf(snes->comm,"SNES options ----------------------------\n"); 21433455573SSatish Balay SNESPrintTypes_Private(snes->comm,p,"snes_type"); 21577c4ece6SBarry Smith PetscPrintf(snes->comm," %ssnes_monitor: use default SNES monitor\n",p); 21677c4ece6SBarry Smith PetscPrintf(snes->comm," %ssnes_view: view SNES info after each nonlinear solve\n",p); 21777c4ece6SBarry Smith PetscPrintf(snes->comm," %ssnes_max_it its (default %d)\n",p,snes->max_its); 21877c4ece6SBarry Smith PetscPrintf(snes->comm," %ssnes_stol tol (default %g)\n",p,snes->xtol); 21977c4ece6SBarry Smith PetscPrintf(snes->comm," %ssnes_atol tol (default %g)\n",p,snes->atol); 22077c4ece6SBarry Smith PetscPrintf(snes->comm," %ssnes_rtol tol (default %g)\n",p,snes->rtol); 22177c4ece6SBarry Smith PetscPrintf(snes->comm," %ssnes_ttol tol (default %g)\n",p,snes->trunctol); 22277c4ece6SBarry Smith PetscPrintf(snes->comm, 2239b94acceSBarry Smith " options for solving systems of nonlinear equations only:\n"); 22477c4ece6SBarry Smith PetscPrintf(snes->comm," %ssnes_fd: use finite differences for Jacobian\n",p); 22577c4ece6SBarry Smith PetscPrintf(snes->comm," %ssnes_mf: use matrix-free Jacobian\n",p); 22677c4ece6SBarry Smith PetscPrintf(snes->comm," %ssnes_ksp_ew_conv: use Eisenstat-Walker computation of KSP rtol. Params are:\n",p); 22777c4ece6SBarry Smith PetscPrintf(snes->comm, 2289b94acceSBarry Smith " %ssnes_ksp_ew_version version (1 or 2, default is %d)\n", 2296daaf66cSBarry Smith p,kctx->version); 23077c4ece6SBarry Smith PetscPrintf(snes->comm, 2319b94acceSBarry Smith " %ssnes_ksp_ew_rtol0 rtol0 (0 <= rtol0 < 1, default %g)\n", 2326daaf66cSBarry Smith p,kctx->rtol_0); 23377c4ece6SBarry Smith PetscPrintf(snes->comm, 2349b94acceSBarry Smith " %ssnes_ksp_ew_rtolmax rtolmax (0 <= rtolmax < 1, default %g)\n", 2356daaf66cSBarry Smith p,kctx->rtol_max); 23677c4ece6SBarry Smith PetscPrintf(snes->comm, 2379b94acceSBarry Smith " %ssnes_ksp_ew_gamma gamma (0 <= gamma <= 1, default %g)\n", 2386daaf66cSBarry Smith p,kctx->gamma); 23977c4ece6SBarry Smith PetscPrintf(snes->comm, 2409b94acceSBarry Smith " %ssnes_ksp_ew_alpha alpha (1 < alpha <= 2, default %g)\n", 2416daaf66cSBarry Smith p,kctx->alpha); 24277c4ece6SBarry Smith PetscPrintf(snes->comm, 2439b94acceSBarry Smith " %ssnes_ksp_ew_alpha2 alpha2 (default %g)\n", 2446daaf66cSBarry Smith p,kctx->alpha2); 24577c4ece6SBarry Smith PetscPrintf(snes->comm, 2469b94acceSBarry Smith " %ssnes_ksp_ew_threshold threshold (0 < threshold < 1, default %g)\n", 2476daaf66cSBarry Smith p,kctx->threshold); 24877c4ece6SBarry Smith PetscPrintf(snes->comm, 2499b94acceSBarry Smith " options for solving unconstrained minimization problems only:\n"); 25077c4ece6SBarry Smith PetscPrintf(snes->comm," %ssnes_fmin tol (default %g)\n",p,snes->fmin); 25177c4ece6SBarry Smith PetscPrintf(snes->comm," Run program with %ssnes_type method -help for help on ",p); 25277c4ece6SBarry Smith PetscPrintf(snes->comm," %ssnes_fd: use finite differences for Hessian\n",p); 25377c4ece6SBarry Smith PetscPrintf(snes->comm," %ssnes_mf: use matrix-free Hessian\n",p); 25477c4ece6SBarry Smith PetscPrintf(snes->comm,"a particular method\n"); 2556daaf66cSBarry Smith if (snes->printhelp) (*snes->printhelp)(snes,p); 2569b94acceSBarry Smith return 0; 2579b94acceSBarry Smith } 2589b94acceSBarry Smith /*@ 2599b94acceSBarry Smith SNESSetApplicationContext - Sets the optional user-defined context for 2609b94acceSBarry Smith the nonlinear solvers. 2619b94acceSBarry Smith 2629b94acceSBarry Smith Input Parameters: 2639b94acceSBarry Smith . snes - the SNES context 2649b94acceSBarry Smith . usrP - optional user context 2659b94acceSBarry Smith 2669b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context 2679b94acceSBarry Smith 2689b94acceSBarry Smith .seealso: SNESGetApplicationContext() 2699b94acceSBarry Smith @*/ 2709b94acceSBarry Smith int SNESSetApplicationContext(SNES snes,void *usrP) 2719b94acceSBarry Smith { 27277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2739b94acceSBarry Smith snes->user = usrP; 2749b94acceSBarry Smith return 0; 2759b94acceSBarry Smith } 2769b94acceSBarry Smith /*@C 2779b94acceSBarry Smith SNESGetApplicationContext - Gets the user-defined context for the 2789b94acceSBarry Smith nonlinear solvers. 2799b94acceSBarry Smith 2809b94acceSBarry Smith Input Parameter: 2819b94acceSBarry Smith . snes - SNES context 2829b94acceSBarry Smith 2839b94acceSBarry Smith Output Parameter: 2849b94acceSBarry Smith . usrP - user context 2859b94acceSBarry Smith 2869b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context 2879b94acceSBarry Smith 2889b94acceSBarry Smith .seealso: SNESSetApplicationContext() 2899b94acceSBarry Smith @*/ 2909b94acceSBarry Smith int SNESGetApplicationContext( SNES snes, void **usrP ) 2919b94acceSBarry Smith { 29277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 2939b94acceSBarry Smith *usrP = snes->user; 2949b94acceSBarry Smith return 0; 2959b94acceSBarry Smith } 2969b94acceSBarry Smith /*@ 2979b94acceSBarry Smith SNESGetIterationNumber - Gets the current iteration number of the 2989b94acceSBarry Smith nonlinear solver. 2999b94acceSBarry Smith 3009b94acceSBarry Smith Input Parameter: 3019b94acceSBarry Smith . snes - SNES context 3029b94acceSBarry Smith 3039b94acceSBarry Smith Output Parameter: 3049b94acceSBarry Smith . iter - iteration number 3059b94acceSBarry Smith 3069b94acceSBarry Smith .keywords: SNES, nonlinear, get, iteration, number 3079b94acceSBarry Smith @*/ 3089b94acceSBarry Smith int SNESGetIterationNumber(SNES snes,int* iter) 3099b94acceSBarry Smith { 31077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 3119b94acceSBarry Smith *iter = snes->iter; 3129b94acceSBarry Smith return 0; 3139b94acceSBarry Smith } 3149b94acceSBarry Smith /*@ 3159b94acceSBarry Smith SNESGetFunctionNorm - Gets the norm of the current function that was set 3169b94acceSBarry Smith with SNESSSetFunction(). 3179b94acceSBarry Smith 3189b94acceSBarry Smith Input Parameter: 3199b94acceSBarry Smith . snes - SNES context 3209b94acceSBarry Smith 3219b94acceSBarry Smith Output Parameter: 3229b94acceSBarry Smith . fnorm - 2-norm of function 3239b94acceSBarry Smith 3249b94acceSBarry Smith Note: 3259b94acceSBarry Smith SNESGetFunctionNorm() is valid for SNES_NONLINEAR_EQUATIONS methods only. 326a86d99e1SLois Curfman McInnes A related routine for SNES_UNCONSTRAINED_MINIMIZATION methods is 327a86d99e1SLois Curfman McInnes SNESGetGradientNorm(). 3289b94acceSBarry Smith 3299b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm 330a86d99e1SLois Curfman McInnes 331a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction() 3329b94acceSBarry Smith @*/ 3339b94acceSBarry Smith int SNESGetFunctionNorm(SNES snes,Scalar *fnorm) 3349b94acceSBarry Smith { 33577c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 3369b94acceSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1, 33748d91487SBarry Smith "SNESGetFunctionNorm:For SNES_NONLINEAR_EQUATIONS only"); 3389b94acceSBarry Smith *fnorm = snes->norm; 3399b94acceSBarry Smith return 0; 3409b94acceSBarry Smith } 3419b94acceSBarry Smith /*@ 3429b94acceSBarry Smith SNESGetGradientNorm - Gets the norm of the current gradient that was set 3439b94acceSBarry Smith with SNESSSetGradient(). 3449b94acceSBarry Smith 3459b94acceSBarry Smith Input Parameter: 3469b94acceSBarry Smith . snes - SNES context 3479b94acceSBarry Smith 3489b94acceSBarry Smith Output Parameter: 3499b94acceSBarry Smith . fnorm - 2-norm of gradient 3509b94acceSBarry Smith 3519b94acceSBarry Smith Note: 3529b94acceSBarry Smith SNESGetGradientNorm() is valid for SNES_UNCONSTRAINED_MINIMIZATION 353a86d99e1SLois Curfman McInnes methods only. A related routine for SNES_NONLINEAR_EQUATIONS methods 354a86d99e1SLois Curfman McInnes is SNESGetFunctionNorm(). 3559b94acceSBarry Smith 3569b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient, norm 357a86d99e1SLois Curfman McInnes 358a86d99e1SLois Curfman McInnes .seelso: SNESSetGradient() 3599b94acceSBarry Smith @*/ 3609b94acceSBarry Smith int SNESGetGradientNorm(SNES snes,Scalar *gnorm) 3619b94acceSBarry Smith { 36277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 3639b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 36448d91487SBarry Smith "SNESGetGradientNorm:For SNES_UNCONSTRAINED_MINIMIZATION only"); 3659b94acceSBarry Smith *gnorm = snes->norm; 3669b94acceSBarry Smith return 0; 3679b94acceSBarry Smith } 3689b94acceSBarry Smith /*@ 3699b94acceSBarry Smith SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps 3709b94acceSBarry Smith attempted by the nonlinear solver. 3719b94acceSBarry Smith 3729b94acceSBarry Smith Input Parameter: 3739b94acceSBarry Smith . snes - SNES context 3749b94acceSBarry Smith 3759b94acceSBarry Smith Output Parameter: 3769b94acceSBarry Smith . nfails - number of unsuccessful steps attempted 3779b94acceSBarry Smith 3789b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 3799b94acceSBarry Smith @*/ 3809b94acceSBarry Smith int SNESGetNumberUnsuccessfulSteps(SNES snes,int* nfails) 3819b94acceSBarry Smith { 38277c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 3839b94acceSBarry Smith *nfails = snes->nfailures; 3849b94acceSBarry Smith return 0; 3859b94acceSBarry Smith } 3869b94acceSBarry Smith /*@C 3879b94acceSBarry Smith SNESGetSLES - Returns the SLES context for a SNES solver. 3889b94acceSBarry Smith 3899b94acceSBarry Smith Input Parameter: 3909b94acceSBarry Smith . snes - the SNES context 3919b94acceSBarry Smith 3929b94acceSBarry Smith Output Parameter: 3939b94acceSBarry Smith . sles - the SLES context 3949b94acceSBarry Smith 3959b94acceSBarry Smith Notes: 3969b94acceSBarry Smith The user can then directly manipulate the SLES context to set various 3979b94acceSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 3989b94acceSBarry Smith KSP and PC contexts as well. 3999b94acceSBarry Smith 4009b94acceSBarry Smith .keywords: SNES, nonlinear, get, SLES, context 4019b94acceSBarry Smith 4029b94acceSBarry Smith .seealso: SLESGetPC(), SLESGetKSP() 4039b94acceSBarry Smith @*/ 4049b94acceSBarry Smith int SNESGetSLES(SNES snes,SLES *sles) 4059b94acceSBarry Smith { 40677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 4079b94acceSBarry Smith *sles = snes->sles; 4089b94acceSBarry Smith return 0; 4099b94acceSBarry Smith } 4109b94acceSBarry Smith /* -----------------------------------------------------------*/ 4119b94acceSBarry Smith /*@C 4129b94acceSBarry Smith SNESCreate - Creates a nonlinear solver context. 4139b94acceSBarry Smith 4149b94acceSBarry Smith Input Parameter: 4159b94acceSBarry Smith . comm - MPI communicator 4169b94acceSBarry Smith . type - type of method, one of 4179b94acceSBarry Smith $ SNES_NONLINEAR_EQUATIONS 4189b94acceSBarry Smith $ (for systems of nonlinear equations) 4199b94acceSBarry Smith $ SNES_UNCONSTRAINED_MINIMIZATION 4209b94acceSBarry Smith $ (for unconstrained minimization) 4219b94acceSBarry Smith 4229b94acceSBarry Smith Output Parameter: 4239b94acceSBarry Smith . outsnes - the new SNES context 4249b94acceSBarry Smith 4259b94acceSBarry Smith .keywords: SNES, nonlinear, create, context 4269b94acceSBarry Smith 42763a78c88SLois Curfman McInnes .seealso: SNESSolve(), SNESDestroy() 4289b94acceSBarry Smith @*/ 4294b0e389bSBarry Smith int SNESCreate(MPI_Comm comm,SNESProblemType type,SNES *outsnes) 4309b94acceSBarry Smith { 4319b94acceSBarry Smith int ierr; 4329b94acceSBarry Smith SNES snes; 4339b94acceSBarry Smith SNES_KSP_EW_ConvCtx *kctx; 43437fcc0dbSBarry Smith 4359b94acceSBarry Smith *outsnes = 0; 4362a0acf01SLois Curfman McInnes if (type != SNES_UNCONSTRAINED_MINIMIZATION && type != SNES_NONLINEAR_EQUATIONS) 4372a0acf01SLois Curfman McInnes SETERRQ(1,"SNESCreate:incorrect method type"); 4380452661fSBarry Smith PetscHeaderCreate(snes,_SNES,SNES_COOKIE,SNES_UNKNOWN_METHOD,comm); 4399b94acceSBarry Smith PLogObjectCreate(snes); 4409b94acceSBarry Smith snes->max_its = 50; 4419b94acceSBarry Smith snes->max_funcs = 1000; 4429b94acceSBarry Smith snes->norm = 0.0; 4435a2d0531SBarry Smith if (type == SNES_UNCONSTRAINED_MINIMIZATION) { 4442a0acf01SLois Curfman McInnes snes->rtol = 1.e-08; 4459b94acceSBarry Smith snes->atol = 1.e-10; 4465a2d0531SBarry Smith } 447b4874afaSBarry Smith else { 448b4874afaSBarry Smith snes->rtol = 1.e-8; 449b4874afaSBarry Smith snes->ttol = 0.0; 450b4874afaSBarry Smith snes->atol = 1.e-50; 451b4874afaSBarry Smith } 4529b94acceSBarry Smith snes->xtol = 1.e-8; 4539b94acceSBarry Smith snes->trunctol = 1.e-12; 4549b94acceSBarry Smith snes->nfuncs = 0; 4559b94acceSBarry Smith snes->nfailures = 0; 4569b94acceSBarry Smith snes->monitor = 0; 4579b94acceSBarry Smith snes->data = 0; 4589b94acceSBarry Smith snes->view = 0; 4599b94acceSBarry Smith snes->computeumfunction = 0; 4609b94acceSBarry Smith snes->umfunP = 0; 4619b94acceSBarry Smith snes->fc = 0; 4629b94acceSBarry Smith snes->deltatol = 1.e-12; 4639b94acceSBarry Smith snes->fmin = -1.e30; 4649b94acceSBarry Smith snes->method_class = type; 4659b94acceSBarry Smith snes->set_method_called = 0; 466a703fe33SLois Curfman McInnes snes->setup_called = 0; 4679b94acceSBarry Smith snes->ksp_ewconv = 0; 4689b94acceSBarry Smith 4699b94acceSBarry Smith /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 4700452661fSBarry Smith kctx = PetscNew(SNES_KSP_EW_ConvCtx); CHKPTRQ(kctx); 4719b94acceSBarry Smith snes->kspconvctx = (void*)kctx; 4729b94acceSBarry Smith kctx->version = 2; 4739b94acceSBarry Smith kctx->rtol_0 = .3; /* Eisenstat and Walker suggest rtol_0=.5, but 4749b94acceSBarry Smith this was too large for some test cases */ 4759b94acceSBarry Smith kctx->rtol_last = 0; 4769b94acceSBarry Smith kctx->rtol_max = .9; 4779b94acceSBarry Smith kctx->gamma = 1.0; 4789b94acceSBarry Smith kctx->alpha2 = .5*(1.0 + sqrt(5.0)); 4799b94acceSBarry Smith kctx->alpha = kctx->alpha2; 4809b94acceSBarry Smith kctx->threshold = .1; 4819b94acceSBarry Smith kctx->lresid_last = 0; 4829b94acceSBarry Smith kctx->norm_last = 0; 4839b94acceSBarry Smith 4849b94acceSBarry Smith ierr = SLESCreate(comm,&snes->sles); CHKERRQ(ierr); 4859b94acceSBarry Smith PLogObjectParent(snes,snes->sles) 4865334005bSBarry Smith 4879b94acceSBarry Smith *outsnes = snes; 4889b94acceSBarry Smith return 0; 4899b94acceSBarry Smith } 4909b94acceSBarry Smith 4919b94acceSBarry Smith /* --------------------------------------------------------------- */ 4929b94acceSBarry Smith /*@C 4939b94acceSBarry Smith SNESSetFunction - Sets the function evaluation routine and function 4949b94acceSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 4959b94acceSBarry Smith equations. 4969b94acceSBarry Smith 4979b94acceSBarry Smith Input Parameters: 4989b94acceSBarry Smith . snes - the SNES context 4999b94acceSBarry Smith . func - function evaluation routine 5009b94acceSBarry Smith . ctx - optional user-defined function context 5019b94acceSBarry Smith . r - vector to store function value 5029b94acceSBarry Smith 5039b94acceSBarry Smith Calling sequence of func: 5049b94acceSBarry Smith . func (SNES, Vec x, Vec f, void *ctx); 5059b94acceSBarry Smith 5069b94acceSBarry Smith . x - input vector 5075334005bSBarry Smith . f - vector function 5089b94acceSBarry Smith . ctx - optional user-defined context for private data for the 5099b94acceSBarry Smith function evaluation routine (may be null) 5109b94acceSBarry Smith 5119b94acceSBarry Smith Notes: 5129b94acceSBarry Smith The Newton-like methods typically solve linear systems of the form 5139b94acceSBarry Smith $ f'(x) x = -f(x), 5149b94acceSBarry Smith $ where f'(x) denotes the Jacobian matrix and f(x) is the function. 5159b94acceSBarry Smith 5169b94acceSBarry Smith SNESSetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only. 5179b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 5189b94acceSBarry Smith SNESSetMinimizationFunction() and SNESSetGradient(); 5199b94acceSBarry Smith 5209b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 5219b94acceSBarry Smith 522a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 5239b94acceSBarry Smith @*/ 5245334005bSBarry Smith int SNESSetFunction( SNES snes, Vec r, int (*func)(SNES,Vec,Vec,void*),void *ctx) 5259b94acceSBarry Smith { 52677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 5279b94acceSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1, 52848d91487SBarry Smith "SNESSetFunction:For SNES_NONLINEAR_EQUATIONS only"); 5299b94acceSBarry Smith snes->computefunction = func; 5309b94acceSBarry Smith snes->vec_func = snes->vec_func_always = r; 5319b94acceSBarry Smith snes->funP = ctx; 5329b94acceSBarry Smith return 0; 5339b94acceSBarry Smith } 5349b94acceSBarry Smith 5359b94acceSBarry Smith /*@ 5369b94acceSBarry Smith SNESComputeFunction - Computes the function that has been set with 5379b94acceSBarry Smith SNESSetFunction(). 5389b94acceSBarry Smith 5399b94acceSBarry Smith Input Parameters: 5409b94acceSBarry Smith . snes - the SNES context 5419b94acceSBarry Smith . x - input vector 5429b94acceSBarry Smith 5439b94acceSBarry Smith Output Parameter: 5443638b69dSLois Curfman McInnes . y - function vector, as set by SNESSetFunction() 5459b94acceSBarry Smith 546a86d99e1SLois Curfman McInnes n Notes: 5479b94acceSBarry Smith SNESComputeFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only. 5489b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 5499b94acceSBarry Smith SNESComputeMinimizationFunction() and SNESComputeGradient(); 5509b94acceSBarry Smith 5519b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function 5529b94acceSBarry Smith 553a86d99e1SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetFunction() 5549b94acceSBarry Smith @*/ 5559b94acceSBarry Smith int SNESComputeFunction(SNES snes,Vec x, Vec y) 5569b94acceSBarry Smith { 5579b94acceSBarry Smith int ierr; 5589b94acceSBarry Smith 559a86d99e1SLois Curfman McInnes if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1, 560a86d99e1SLois Curfman McInnes "SNESComputeFunction: For SNES_NONLINEAR_EQUATIONS only"); 5619b94acceSBarry Smith PLogEventBegin(SNES_FunctionEval,snes,x,y,0); 5629b94acceSBarry Smith ierr = (*snes->computefunction)(snes,x,y,snes->funP); CHKERRQ(ierr); 5639b94acceSBarry Smith PLogEventEnd(SNES_FunctionEval,snes,x,y,0); 5649b94acceSBarry Smith return 0; 5659b94acceSBarry Smith } 5669b94acceSBarry Smith 5679b94acceSBarry Smith /*@C 5689b94acceSBarry Smith SNESSetMinimizationFunction - Sets the function evaluation routine for 5699b94acceSBarry Smith unconstrained minimization. 5709b94acceSBarry Smith 5719b94acceSBarry Smith Input Parameters: 5729b94acceSBarry Smith . snes - the SNES context 5739b94acceSBarry Smith . func - function evaluation routine 5749b94acceSBarry Smith . ctx - optional user-defined function context 5759b94acceSBarry Smith 5769b94acceSBarry Smith Calling sequence of func: 5779b94acceSBarry Smith . func (SNES snes,Vec x,double *f,void *ctx); 5789b94acceSBarry Smith 5799b94acceSBarry Smith . x - input vector 5809b94acceSBarry Smith . f - function 5819b94acceSBarry Smith . ctx - optional user-defined context for private data for the 5829b94acceSBarry Smith function evaluation routine (may be null) 5839b94acceSBarry Smith 5849b94acceSBarry Smith Notes: 5859b94acceSBarry Smith SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 5869b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 5879b94acceSBarry Smith SNESSetFunction(). 5889b94acceSBarry Smith 5899b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimization, function 5909b94acceSBarry Smith 591a86d99e1SLois Curfman McInnes .seealso: SNESGetMinimizationFunction(), SNESComputeMinimizationFunction(), 592a86d99e1SLois Curfman McInnes SNESSetHessian(), SNESSetGradient() 5939b94acceSBarry Smith @*/ 5949b94acceSBarry Smith int SNESSetMinimizationFunction(SNES snes,int (*func)(SNES,Vec,double*,void*), 5959b94acceSBarry Smith void *ctx) 5969b94acceSBarry Smith { 59777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 5989b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 59948d91487SBarry Smith "SNESSetMinimizationFunction:Only for SNES_UNCONSTRAINED_MINIMIZATION"); 6009b94acceSBarry Smith snes->computeumfunction = func; 6019b94acceSBarry Smith snes->umfunP = ctx; 6029b94acceSBarry Smith return 0; 6039b94acceSBarry Smith } 6049b94acceSBarry Smith 6059b94acceSBarry Smith /*@ 6069b94acceSBarry Smith SNESComputeMinimizationFunction - Computes the function that has been 6079b94acceSBarry Smith set with SNESSetMinimizationFunction(). 6089b94acceSBarry Smith 6099b94acceSBarry Smith Input Parameters: 6109b94acceSBarry Smith . snes - the SNES context 6119b94acceSBarry Smith . x - input vector 6129b94acceSBarry Smith 6139b94acceSBarry Smith Output Parameter: 6149b94acceSBarry Smith . y - function value 6159b94acceSBarry Smith 6169b94acceSBarry Smith Notes: 6179b94acceSBarry Smith SNESComputeMinimizationFunction() is valid only for 6189b94acceSBarry Smith SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 6199b94acceSBarry Smith SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction(). 620a86d99e1SLois Curfman McInnes 621a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, minimization, function 622a86d99e1SLois Curfman McInnes 623a86d99e1SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESGetMinimizationFunction(), 624a86d99e1SLois Curfman McInnes SNESComputeGradient(), SNESComputeHessian() 6259b94acceSBarry Smith @*/ 6269b94acceSBarry Smith int SNESComputeMinimizationFunction(SNES snes,Vec x,double *y) 6279b94acceSBarry Smith { 6289b94acceSBarry Smith int ierr; 6299b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 63048d91487SBarry Smith "SNESComputeMinimizationFunction:Only for SNES_UNCONSTRAINED_MINIMIZATION"); 6319b94acceSBarry Smith PLogEventBegin(SNES_MinimizationFunctionEval,snes,x,y,0); 6329b94acceSBarry Smith ierr = (*snes->computeumfunction)(snes,x,y,snes->umfunP); CHKERRQ(ierr); 6339b94acceSBarry Smith PLogEventEnd(SNES_MinimizationFunctionEval,snes,x,y,0); 6349b94acceSBarry Smith return 0; 6359b94acceSBarry Smith } 6369b94acceSBarry Smith 6379b94acceSBarry Smith /*@C 6389b94acceSBarry Smith SNESSetGradient - Sets the gradient evaluation routine and gradient 6399b94acceSBarry Smith vector for use by the SNES routines. 6409b94acceSBarry Smith 6419b94acceSBarry Smith Input Parameters: 6429b94acceSBarry Smith . snes - the SNES context 6439b94acceSBarry Smith . func - function evaluation routine 6449b94acceSBarry Smith . ctx - optional user-defined function context 6459b94acceSBarry Smith . r - vector to store gradient value 6469b94acceSBarry Smith 6479b94acceSBarry Smith Calling sequence of func: 6489b94acceSBarry Smith . func (SNES, Vec x, Vec g, void *ctx); 6499b94acceSBarry Smith 6509b94acceSBarry Smith . x - input vector 6519b94acceSBarry Smith . g - gradient vector 6529b94acceSBarry Smith . ctx - optional user-defined context for private data for the 6539b94acceSBarry Smith function evaluation routine (may be null) 6549b94acceSBarry Smith 6559b94acceSBarry Smith Notes: 6569b94acceSBarry Smith SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 6579b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 6589b94acceSBarry Smith SNESSetFunction(). 6599b94acceSBarry Smith 6609b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 6619b94acceSBarry Smith 662a86d99e1SLois Curfman McInnes .seealso: SNESGetGradient(), SNESComputeGradient(), SNESSetHessian(), 663a86d99e1SLois Curfman McInnes SNESSetMinimizationFunction(), 6649b94acceSBarry Smith @*/ 6659b94acceSBarry Smith int SNESSetGradient(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*), 6669b94acceSBarry Smith void *ctx) 6679b94acceSBarry Smith { 66877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 6699b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 67048d91487SBarry Smith "SNESSetGradient:For SNES_UNCONSTRAINED_MINIMIZATION only"); 6719b94acceSBarry Smith snes->computefunction = func; 6729b94acceSBarry Smith snes->vec_func = snes->vec_func_always = r; 6739b94acceSBarry Smith snes->funP = ctx; 6749b94acceSBarry Smith return 0; 6759b94acceSBarry Smith } 6769b94acceSBarry Smith 6779b94acceSBarry Smith /*@ 678a86d99e1SLois Curfman McInnes SNESComputeGradient - Computes the gradient that has been set with 679a86d99e1SLois Curfman McInnes SNESSetGradient(). 6809b94acceSBarry Smith 6819b94acceSBarry Smith Input Parameters: 6829b94acceSBarry Smith . snes - the SNES context 6839b94acceSBarry Smith . x - input vector 6849b94acceSBarry Smith 6859b94acceSBarry Smith Output Parameter: 6869b94acceSBarry Smith . y - gradient vector 6879b94acceSBarry Smith 6889b94acceSBarry Smith Notes: 6899b94acceSBarry Smith SNESComputeGradient() is valid only for 6909b94acceSBarry Smith SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 6919b94acceSBarry Smith SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction(). 692a86d99e1SLois Curfman McInnes 693a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, compute, gradient 694a86d99e1SLois Curfman McInnes 695a86d99e1SLois Curfman McInnes .seealso: SNESSetGradient(), SNESGetGradient(), 696a86d99e1SLois Curfman McInnes SNESComputeMinimizationFunction(), SNESComputeHessian() 6979b94acceSBarry Smith @*/ 6989b94acceSBarry Smith int SNESComputeGradient(SNES snes,Vec x, Vec y) 6999b94acceSBarry Smith { 7009b94acceSBarry Smith int ierr; 7019b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 70248d91487SBarry Smith "SNESComputeGradient:For SNES_UNCONSTRAINED_MINIMIZATION only"); 7039b94acceSBarry Smith PLogEventBegin(SNES_GradientEval,snes,x,y,0); 7049b94acceSBarry Smith ierr = (*snes->computefunction)(snes,x,y,snes->funP); CHKERRQ(ierr); 7059b94acceSBarry Smith PLogEventEnd(SNES_GradientEval,snes,x,y,0); 7069b94acceSBarry Smith return 0; 7079b94acceSBarry Smith } 7089b94acceSBarry Smith 70962fef451SLois Curfman McInnes /*@ 71062fef451SLois Curfman McInnes SNESComputeJacobian - Computes the Jacobian matrix that has been 71162fef451SLois Curfman McInnes set with SNESSetJacobian(). 71262fef451SLois Curfman McInnes 71362fef451SLois Curfman McInnes Input Parameters: 71462fef451SLois Curfman McInnes . snes - the SNES context 71562fef451SLois Curfman McInnes . x - input vector 71662fef451SLois Curfman McInnes 71762fef451SLois Curfman McInnes Output Parameters: 71862fef451SLois Curfman McInnes . A - Jacobian matrix 71962fef451SLois Curfman McInnes . B - optional preconditioning matrix 72062fef451SLois Curfman McInnes . flag - flag indicating matrix structure 72162fef451SLois Curfman McInnes 72262fef451SLois Curfman McInnes Notes: 72362fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 72462fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 72562fef451SLois Curfman McInnes 72662fef451SLois Curfman McInnes See SLESSetOperators() for information about setting the flag parameter. 72762fef451SLois Curfman McInnes 72862fef451SLois Curfman McInnes SNESComputeJacobian() is valid only for SNES_NONLINEAR_EQUATIONS 72962fef451SLois Curfman McInnes methods. An analogous routine for SNES_UNCONSTRAINED_MINIMIZATION 73062fef451SLois Curfman McInnes methods is SNESComputeJacobian(). 73162fef451SLois Curfman McInnes 73262fef451SLois Curfman McInnes .keywords: SNES, compute, Jacobian, matrix 73362fef451SLois Curfman McInnes 73462fef451SLois Curfman McInnes .seealso: SNESSetJacobian(), SLESSetOperators() 73562fef451SLois Curfman McInnes @*/ 7369b94acceSBarry Smith int SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 7379b94acceSBarry Smith { 7389b94acceSBarry Smith int ierr; 7399b94acceSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1, 74048d91487SBarry Smith "SNESComputeJacobian: For SNES_NONLINEAR_EQUATIONS only"); 7419b94acceSBarry Smith if (!snes->computejacobian) return 0; 7429b94acceSBarry Smith PLogEventBegin(SNES_JacobianEval,snes,X,*A,*B); 743c4fc05e7SBarry Smith *flg = DIFFERENT_NONZERO_PATTERN; 7449b94acceSBarry Smith ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP); CHKERRQ(ierr); 7459b94acceSBarry Smith PLogEventEnd(SNES_JacobianEval,snes,X,*A,*B); 7466d84be18SBarry Smith /* make sure user returned a correct Jacobian and preconditioner */ 74777c4ece6SBarry Smith PetscValidHeaderSpecific(*A,MAT_COOKIE); 74877c4ece6SBarry Smith PetscValidHeaderSpecific(*B,MAT_COOKIE); 7499b94acceSBarry Smith return 0; 7509b94acceSBarry Smith } 7519b94acceSBarry Smith 75262fef451SLois Curfman McInnes /*@ 75362fef451SLois Curfman McInnes SNESComputeHessian - Computes the Hessian matrix that has been 75462fef451SLois Curfman McInnes set with SNESSetHessian(). 75562fef451SLois Curfman McInnes 75662fef451SLois Curfman McInnes Input Parameters: 75762fef451SLois Curfman McInnes . snes - the SNES context 75862fef451SLois Curfman McInnes . x - input vector 75962fef451SLois Curfman McInnes 76062fef451SLois Curfman McInnes Output Parameters: 76162fef451SLois Curfman McInnes . A - Hessian matrix 76262fef451SLois Curfman McInnes . B - optional preconditioning matrix 76362fef451SLois Curfman McInnes . flag - flag indicating matrix structure 76462fef451SLois Curfman McInnes 76562fef451SLois Curfman McInnes Notes: 76662fef451SLois Curfman McInnes Most users should not need to explicitly call this routine, as it 76762fef451SLois Curfman McInnes is used internally within the nonlinear solvers. 76862fef451SLois Curfman McInnes 76962fef451SLois Curfman McInnes See SLESSetOperators() for information about setting the flag parameter. 77062fef451SLois Curfman McInnes 77162fef451SLois Curfman McInnes SNESComputeHessian() is valid only for 77262fef451SLois Curfman McInnes SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 77362fef451SLois Curfman McInnes SNES_NONLINEAR_EQUATIONS methods is SNESComputeJacobian(). 77462fef451SLois Curfman McInnes 77562fef451SLois Curfman McInnes .keywords: SNES, compute, Hessian, matrix 77662fef451SLois Curfman McInnes 777a86d99e1SLois Curfman McInnes .seealso: SNESSetHessian(), SLESSetOperators(), SNESComputeGradient(), 778a86d99e1SLois Curfman McInnes SNESComputeMinimizationFunction() 77962fef451SLois Curfman McInnes @*/ 78062fef451SLois Curfman McInnes int SNESComputeHessian(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag) 7819b94acceSBarry Smith { 7829b94acceSBarry Smith int ierr; 7839b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 78448d91487SBarry Smith "SNESComputeHessian:For SNES_UNCONSTRAINED_MINIMIZATION only"); 7859b94acceSBarry Smith if (!snes->computejacobian) return 0; 78662fef451SLois Curfman McInnes PLogEventBegin(SNES_HessianEval,snes,x,*A,*B); 7870f4a323eSLois Curfman McInnes *flag = DIFFERENT_NONZERO_PATTERN; 78862fef451SLois Curfman McInnes ierr = (*snes->computejacobian)(snes,x,A,B,flag,snes->jacP); CHKERRQ(ierr); 78962fef451SLois Curfman McInnes PLogEventEnd(SNES_HessianEval,snes,x,*A,*B); 7900f4a323eSLois Curfman McInnes /* make sure user returned a correct Jacobian and preconditioner */ 79177c4ece6SBarry Smith PetscValidHeaderSpecific(*A,MAT_COOKIE); 79277c4ece6SBarry Smith PetscValidHeaderSpecific(*B,MAT_COOKIE); 7939b94acceSBarry Smith return 0; 7949b94acceSBarry Smith } 7959b94acceSBarry Smith 7969b94acceSBarry Smith /*@C 7979b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 7989b94acceSBarry Smith location to store it. 7999b94acceSBarry Smith 8009b94acceSBarry Smith Input Parameters: 8019b94acceSBarry Smith . snes - the SNES context 8029b94acceSBarry Smith . A - Jacobian matrix 8039b94acceSBarry Smith . B - preconditioner matrix (usually same as the Jacobian) 8049b94acceSBarry Smith . func - Jacobian evaluation routine 8059b94acceSBarry Smith . ctx - optional user-defined context for private data for the 8069b94acceSBarry Smith Jacobian evaluation routine (may be null) 8079b94acceSBarry Smith 8089b94acceSBarry Smith Calling sequence of func: 8099b94acceSBarry Smith . func (SNES,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 8109b94acceSBarry Smith 8119b94acceSBarry Smith . x - input vector 8129b94acceSBarry Smith . A - Jacobian matrix 8139b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 814ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 815ac21db08SLois Curfman McInnes structure (same as flag in SLESSetOperators()) 8169b94acceSBarry Smith . ctx - optional user-defined Jacobian context 8179b94acceSBarry Smith 8189b94acceSBarry Smith Notes: 819ac21db08SLois Curfman McInnes See SLESSetOperators() for information about setting the flag input 820ac21db08SLois Curfman McInnes parameter in the routine func(). Be sure to read this information! 821ac21db08SLois Curfman McInnes 822ac21db08SLois Curfman McInnes The routine func() takes Mat * as the matrix arguments rather than Mat. 8239b94acceSBarry Smith This allows the Jacobian evaluation routine to replace A and/or B with a 8249b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 8259b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 8269b94acceSBarry Smith throughout the global iterations. 8279b94acceSBarry Smith 8289b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix 8299b94acceSBarry Smith 830ac21db08SLois Curfman McInnes .seealso: SLESSetOperators(), SNESSetFunction() 8319b94acceSBarry Smith @*/ 8329b94acceSBarry Smith int SNESSetJacobian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*, 8339b94acceSBarry Smith MatStructure*,void*),void *ctx) 8349b94acceSBarry Smith { 83577c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 8369b94acceSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1, 83748d91487SBarry Smith "SNESSetJacobian:For SNES_NONLINEAR_EQUATIONS only"); 8389b94acceSBarry Smith snes->computejacobian = func; 8399b94acceSBarry Smith snes->jacP = ctx; 8409b94acceSBarry Smith snes->jacobian = A; 8419b94acceSBarry Smith snes->jacobian_pre = B; 8429b94acceSBarry Smith return 0; 8439b94acceSBarry Smith } 84462fef451SLois Curfman McInnes 845b4fd4287SBarry Smith /*@ 846b4fd4287SBarry Smith SNESGetJacobian - Returns the Jacobian matrix and optionally the user 847b4fd4287SBarry Smith provided context for evaluating the Jacobian. 848b4fd4287SBarry Smith 849b4fd4287SBarry Smith Input Parameter: 850b4fd4287SBarry Smith . snes - the nonlinear solver context 851b4fd4287SBarry Smith 852b4fd4287SBarry Smith Output Parameters: 853b4fd4287SBarry Smith . A - location to stash Jacobian matrix (or PETSC_NULL) 854b4fd4287SBarry Smith . B - location to stash preconditioner matrix (or PETSC_NULL) 855b4fd4287SBarry Smith . ctx - location to stash Jacobian ctx (or PETSC_NULL) 856b4fd4287SBarry Smith 857b4fd4287SBarry Smith .seealso: SNESSetJacobian(), SNESComputeJacobian() 858b4fd4287SBarry Smith @*/ 859b4fd4287SBarry Smith int SNESGetJacobian(SNES snes,Mat *A,Mat *B, void **ctx) 860b4fd4287SBarry Smith { 86162fef451SLois Curfman McInnes if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1, 86262fef451SLois Curfman McInnes "SNESSetJacobian:For SNES_NONLINEAR_EQUATIONS only"); 863b4fd4287SBarry Smith if (A) *A = snes->jacobian; 864b4fd4287SBarry Smith if (B) *B = snes->jacobian_pre; 865b4fd4287SBarry Smith if (ctx) *ctx = snes->jacP; 866b4fd4287SBarry Smith return 0; 867b4fd4287SBarry Smith } 868b4fd4287SBarry Smith 8699b94acceSBarry Smith /*@C 8709b94acceSBarry Smith SNESSetHessian - Sets the function to compute Hessian as well as the 8719b94acceSBarry Smith location to store it. 8729b94acceSBarry Smith 8739b94acceSBarry Smith Input Parameters: 8749b94acceSBarry Smith . snes - the SNES context 8759b94acceSBarry Smith . A - Hessian matrix 8769b94acceSBarry Smith . B - preconditioner matrix (usually same as the Hessian) 8779b94acceSBarry Smith . func - Jacobian evaluation routine 8789b94acceSBarry Smith . ctx - optional user-defined context for private data for the 8799b94acceSBarry Smith Hessian evaluation routine (may be null) 8809b94acceSBarry Smith 8819b94acceSBarry Smith Calling sequence of func: 8829b94acceSBarry Smith . func (SNES,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 8839b94acceSBarry Smith 8849b94acceSBarry Smith . x - input vector 8859b94acceSBarry Smith . A - Hessian matrix 8869b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 887ac21db08SLois Curfman McInnes . flag - flag indicating information about the preconditioner matrix 888ac21db08SLois Curfman McInnes structure (same as flag in SLESSetOperators()) 8899b94acceSBarry Smith . ctx - optional user-defined Hessian context 8909b94acceSBarry Smith 8919b94acceSBarry Smith Notes: 892ac21db08SLois Curfman McInnes See SLESSetOperators() for information about setting the flag input 893ac21db08SLois Curfman McInnes parameter in the routine func(). Be sure to read this information! 894ac21db08SLois Curfman McInnes 8959b94acceSBarry Smith The function func() takes Mat * as the matrix arguments rather than Mat. 8969b94acceSBarry Smith This allows the Hessian evaluation routine to replace A and/or B with a 8979b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 8989b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 8999b94acceSBarry Smith throughout the global iterations. 9009b94acceSBarry Smith 9019b94acceSBarry Smith .keywords: SNES, nonlinear, set, Hessian, matrix 9029b94acceSBarry Smith 903ac21db08SLois Curfman McInnes .seealso: SNESSetMinimizationFunction(), SNESSetGradient(), SLESSetOperators() 9049b94acceSBarry Smith @*/ 9059b94acceSBarry Smith int SNESSetHessian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*, 9069b94acceSBarry Smith MatStructure*,void*),void *ctx) 9079b94acceSBarry Smith { 90877c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 9099b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 91048d91487SBarry Smith "SNESSetHessian:For SNES_UNCONSTRAINED_MINIMIZATION only"); 9119b94acceSBarry Smith snes->computejacobian = func; 9129b94acceSBarry Smith snes->jacP = ctx; 9139b94acceSBarry Smith snes->jacobian = A; 9149b94acceSBarry Smith snes->jacobian_pre = B; 9159b94acceSBarry Smith return 0; 9169b94acceSBarry Smith } 9179b94acceSBarry Smith 91862fef451SLois Curfman McInnes /*@ 91962fef451SLois Curfman McInnes SNESGetHessian - Returns the Hessian matrix and optionally the user 92062fef451SLois Curfman McInnes provided context for evaluating the Hessian. 92162fef451SLois Curfman McInnes 92262fef451SLois Curfman McInnes Input Parameter: 92362fef451SLois Curfman McInnes . snes - the nonlinear solver context 92462fef451SLois Curfman McInnes 92562fef451SLois Curfman McInnes Output Parameters: 92662fef451SLois Curfman McInnes . A - location to stash Hessian matrix (or PETSC_NULL) 92762fef451SLois Curfman McInnes . B - location to stash preconditioner matrix (or PETSC_NULL) 92862fef451SLois Curfman McInnes . ctx - location to stash Hessian ctx (or PETSC_NULL) 92962fef451SLois Curfman McInnes 93062fef451SLois Curfman McInnes .seealso: SNESSetHessian(), SNESComputeHessian() 93162fef451SLois Curfman McInnes @*/ 93262fef451SLois Curfman McInnes int SNESGetHessian(SNES snes,Mat *A,Mat *B, void **ctx) 93362fef451SLois Curfman McInnes { 93462fef451SLois Curfman McInnes if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 93562fef451SLois Curfman McInnes "SNESSetHessian:For SNES_UNCONSTRAINED_MINIMIZATION only"); 93662fef451SLois Curfman McInnes if (A) *A = snes->jacobian; 93762fef451SLois Curfman McInnes if (B) *B = snes->jacobian_pre; 93862fef451SLois Curfman McInnes if (ctx) *ctx = snes->jacP; 93962fef451SLois Curfman McInnes return 0; 94062fef451SLois Curfman McInnes } 94162fef451SLois Curfman McInnes 9429b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 9439b94acceSBarry Smith 9449b94acceSBarry Smith /*@ 9459b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 946272ac6f2SLois Curfman McInnes of a nonlinear solver. 9479b94acceSBarry Smith 9489b94acceSBarry Smith Input Parameter: 9499b94acceSBarry Smith . snes - the SNES context 9508ddd3da0SLois Curfman McInnes . x - the solution vector 9519b94acceSBarry Smith 952272ac6f2SLois Curfman McInnes Notes: 953272ac6f2SLois Curfman McInnes For basic use of the SNES solvers the user need not explicitly call 954272ac6f2SLois Curfman McInnes SNESSetUp(), since these actions will automatically occur during 955272ac6f2SLois Curfman McInnes the call to SNESSolve(). However, if one wishes to control this 956272ac6f2SLois Curfman McInnes phase separately, SNESSetUp() should be called after SNESCreate() 957272ac6f2SLois Curfman McInnes and optional routines of the form SNESSetXXX(), but before SNESSolve(). 958272ac6f2SLois Curfman McInnes 9599b94acceSBarry Smith .keywords: SNES, nonlinear, setup 9609b94acceSBarry Smith 9619b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 9629b94acceSBarry Smith @*/ 9638ddd3da0SLois Curfman McInnes int SNESSetUp(SNES snes,Vec x) 9649b94acceSBarry Smith { 965272ac6f2SLois Curfman McInnes int ierr, flg; 96677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 96777c4ece6SBarry Smith PetscValidHeaderSpecific(x,VEC_COOKIE); 9688ddd3da0SLois Curfman McInnes snes->vec_sol = snes->vec_sol_always = x; 9699b94acceSBarry Smith 970272ac6f2SLois Curfman McInnes ierr = OptionsHasName(snes->prefix,"-snes_mf", &flg); CHKERRQ(ierr); 97131615d04SLois Curfman McInnes if (flg) { 972272ac6f2SLois Curfman McInnes Mat J; 973272ac6f2SLois Curfman McInnes ierr = SNESDefaultMatrixFreeMatCreate(snes,snes->vec_sol,&J);CHKERRQ(ierr); 974272ac6f2SLois Curfman McInnes PLogObjectParent(snes,J); 975272ac6f2SLois Curfman McInnes snes->mfshell = J; 976*83e56afdSLois Curfman McInnes if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 977*83e56afdSLois Curfman McInnes ierr = SNESSetJacobian(snes,J,J,0,snes->funP); CHKERRQ(ierr); 978*83e56afdSLois Curfman McInnes PLogInfo((PetscObject)snes,"SNESSetUp: Setting default matrix-free Jacobian routines\n"); 979*83e56afdSLois Curfman McInnes } 980*83e56afdSLois Curfman McInnes else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 981*83e56afdSLois Curfman McInnes ierr = SNESSetHessian(snes,J,J,0,snes->funP); CHKERRQ(ierr); 982*83e56afdSLois Curfman McInnes PLogInfo((PetscObject)snes,"SNESSetUp: Setting default matrix-free Hessian routines\n"); 983*83e56afdSLois Curfman McInnes } else SETERRQ(1,"SNESSetUp:Method class doesn't support matrix-free option"); 984272ac6f2SLois Curfman McInnes } 9859b94acceSBarry Smith if ((snes->method_class == SNES_NONLINEAR_EQUATIONS)) { 98637fcc0dbSBarry Smith if (!snes->vec_func) SETERRQ(1,"SNESSetUp:Must call SNESSetFunction() first"); 98737fcc0dbSBarry Smith if (!snes->computefunction) SETERRQ(1,"SNESSetUp:Must call SNESSetFunction() first"); 98837fcc0dbSBarry Smith if (!snes->jacobian) SETERRQ(1,"SNESSetUp:Must call SNESSetJacobian() first"); 989d804570eSBarry Smith if (snes->vec_func == snes->vec_sol) SETERRQ(1,"SNESSetUp:Solution vector cannot be function vector"); 990a703fe33SLois Curfman McInnes 991a703fe33SLois Curfman McInnes /* Set the KSP stopping criterion to use the Eisenstat-Walker method */ 992a703fe33SLois Curfman McInnes if (snes->ksp_ewconv && snes->type != SNES_EQ_NTR) { 993a703fe33SLois Curfman McInnes SLES sles; KSP ksp; 994a703fe33SLois Curfman McInnes ierr = SNESGetSLES(snes,&sles); CHKERRQ(ierr); 995a703fe33SLois Curfman McInnes ierr = SLESGetKSP(sles,&ksp); CHKERRQ(ierr); 996a703fe33SLois Curfman McInnes ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private, 997a703fe33SLois Curfman McInnes (void *)snes); CHKERRQ(ierr); 998a703fe33SLois Curfman McInnes } 9999b94acceSBarry Smith } else if ((snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION)) { 100037fcc0dbSBarry Smith if (!snes->vec_func) SETERRQ(1,"SNESSetUp:Must call SNESSetGradient() first"); 100137fcc0dbSBarry Smith if (!snes->computefunction) SETERRQ(1,"SNESSetUp:Must call SNESSetGradient() first"); 100237fcc0dbSBarry Smith if (!snes->computeumfunction) 100337fcc0dbSBarry Smith SETERRQ(1,"SNESSetUp:Must call SNESSetMinimizationFunction() first"); 100437fcc0dbSBarry Smith if (!snes->jacobian) SETERRQ(1,"SNESSetUp:Must call SNESSetHessian() first"); 10059b94acceSBarry Smith } else SETERRQ(1,"SNESSetUp:Unknown method class"); 1006a703fe33SLois Curfman McInnes if (snes->setup) {ierr = (*snes->setup)(snes); CHKERRQ(ierr);} 1007a703fe33SLois Curfman McInnes snes->setup_called = 1; 1008a703fe33SLois Curfman McInnes return 0; 10099b94acceSBarry Smith } 10109b94acceSBarry Smith 10119b94acceSBarry Smith /*@C 10129b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 10139b94acceSBarry Smith with SNESCreate(). 10149b94acceSBarry Smith 10159b94acceSBarry Smith Input Parameter: 10169b94acceSBarry Smith . snes - the SNES context 10179b94acceSBarry Smith 10189b94acceSBarry Smith .keywords: SNES, nonlinear, destroy 10199b94acceSBarry Smith 102063a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESSolve() 10219b94acceSBarry Smith @*/ 10229b94acceSBarry Smith int SNESDestroy(SNES snes) 10239b94acceSBarry Smith { 10249b94acceSBarry Smith int ierr; 102577c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 10269b94acceSBarry Smith ierr = (*(snes)->destroy)((PetscObject)snes); CHKERRQ(ierr); 10270452661fSBarry Smith if (snes->kspconvctx) PetscFree(snes->kspconvctx); 10289b94acceSBarry Smith if (snes->mfshell) MatDestroy(snes->mfshell); 10299b94acceSBarry Smith ierr = SLESDestroy(snes->sles); CHKERRQ(ierr); 10309b94acceSBarry Smith PLogObjectDestroy((PetscObject)snes); 10310452661fSBarry Smith PetscHeaderDestroy((PetscObject)snes); 10329b94acceSBarry Smith return 0; 10339b94acceSBarry Smith } 10349b94acceSBarry Smith 10359b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 10369b94acceSBarry Smith 10379b94acceSBarry Smith /*@ 10389b94acceSBarry Smith SNESSetMaxIterations - Sets the maximum number of global iterations to use. 10399b94acceSBarry Smith 10409b94acceSBarry Smith Input Parameters: 10419b94acceSBarry Smith . snes - the SNES context 10429b94acceSBarry Smith . maxits - maximum number of iterations to use 10439b94acceSBarry Smith 10449b94acceSBarry Smith Options Database Key: 10459b94acceSBarry Smith $ -snes_max_it maxits 10469b94acceSBarry Smith 10479b94acceSBarry Smith Note: 10489b94acceSBarry Smith The default maximum number of iterations is 50. 10499b94acceSBarry Smith 10509b94acceSBarry Smith .keywords: SNES, nonlinear, set, maximum, iterations 10519b94acceSBarry Smith 10529b94acceSBarry Smith .seealso: SNESSetMaxFunctionEvaluations() 10539b94acceSBarry Smith @*/ 10549b94acceSBarry Smith int SNESSetMaxIterations(SNES snes,int maxits) 10559b94acceSBarry Smith { 105677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 10579b94acceSBarry Smith snes->max_its = maxits; 10589b94acceSBarry Smith return 0; 10599b94acceSBarry Smith } 10609b94acceSBarry Smith 10619b94acceSBarry Smith /*@ 10629b94acceSBarry Smith SNESSetMaxFunctionEvaluations - Sets the maximum number of function 10639b94acceSBarry Smith evaluations to use. 10649b94acceSBarry Smith 10659b94acceSBarry Smith Input Parameters: 10669b94acceSBarry Smith . snes - the SNES context 10679b94acceSBarry Smith . maxf - maximum number of function evaluations 10689b94acceSBarry Smith 10699b94acceSBarry Smith Options Database Key: 10709b94acceSBarry Smith $ -snes_max_funcs maxf 10719b94acceSBarry Smith 10729b94acceSBarry Smith Note: 10739b94acceSBarry Smith The default maximum number of function evaluations is 1000. 10749b94acceSBarry Smith 10759b94acceSBarry Smith .keywords: SNES, nonlinear, set, maximum, function, evaluations 10769b94acceSBarry Smith 10779b94acceSBarry Smith .seealso: SNESSetMaxIterations() 10789b94acceSBarry Smith @*/ 10799b94acceSBarry Smith int SNESSetMaxFunctionEvaluations(SNES snes,int maxf) 10809b94acceSBarry Smith { 108177c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 10829b94acceSBarry Smith snes->max_funcs = maxf; 10839b94acceSBarry Smith return 0; 10849b94acceSBarry Smith } 10859b94acceSBarry Smith 10869b94acceSBarry Smith /*@ 10879b94acceSBarry Smith SNESSetRelativeTolerance - Sets the relative convergence tolerance. 10889b94acceSBarry Smith 10899b94acceSBarry Smith Input Parameters: 10909b94acceSBarry Smith . snes - the SNES context 10919b94acceSBarry Smith . rtol - tolerance 10929b94acceSBarry Smith 10939b94acceSBarry Smith Options Database Key: 10949b94acceSBarry Smith $ -snes_rtol tol 10959b94acceSBarry Smith 10969b94acceSBarry Smith .keywords: SNES, nonlinear, set, relative, convergence, tolerance 10979b94acceSBarry Smith 109877c4ece6SBarry Smith .seealso: SNESSetAbsoluteTolerance(), SNESSetSolutionTolerance() 109977c4ece6SBarry Smith 11009b94acceSBarry Smith @*/ 11019b94acceSBarry Smith int SNESSetRelativeTolerance(SNES snes,double rtol) 11029b94acceSBarry Smith { 110377c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 11049b94acceSBarry Smith snes->rtol = rtol; 11059b94acceSBarry Smith return 0; 11069b94acceSBarry Smith } 11079b94acceSBarry Smith 11089b94acceSBarry Smith /*@ 11099b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 11109b94acceSBarry Smith 11119b94acceSBarry Smith Input Parameters: 11129b94acceSBarry Smith . snes - the SNES context 11139b94acceSBarry Smith . tol - tolerance 11149b94acceSBarry Smith 11159b94acceSBarry Smith Options Database Key: 11169b94acceSBarry Smith $ -snes_trtol tol 11179b94acceSBarry Smith 11189b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance 11199b94acceSBarry Smith 112077c4ece6SBarry Smith .seealso: SNESSetAbsoluteTolerance(), SNESSetSolutionTolerance() 11219b94acceSBarry Smith @*/ 11229b94acceSBarry Smith int SNESSetTrustRegionTolerance(SNES snes,double tol) 11239b94acceSBarry Smith { 112477c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 11259b94acceSBarry Smith snes->deltatol = tol; 11269b94acceSBarry Smith return 0; 11279b94acceSBarry Smith } 11289b94acceSBarry Smith 11299b94acceSBarry Smith /*@ 11309b94acceSBarry Smith SNESSetAbsoluteTolerance - Sets the absolute convergence tolerance. 11319b94acceSBarry Smith 11329b94acceSBarry Smith Input Parameters: 11339b94acceSBarry Smith . snes - the SNES context 11349b94acceSBarry Smith . atol - tolerance 11359b94acceSBarry Smith 11369b94acceSBarry Smith Options Database Key: 11379b94acceSBarry Smith $ -snes_atol tol 11389b94acceSBarry Smith 11399b94acceSBarry Smith .keywords: SNES, nonlinear, set, absolute, convergence, tolerance 11409b94acceSBarry Smith 114177c4ece6SBarry Smith .seealso: SNESSetRelativeTolerance(), SNESSetSolutionTolerance() 11429b94acceSBarry Smith @*/ 11439b94acceSBarry Smith int SNESSetAbsoluteTolerance(SNES snes,double atol) 11449b94acceSBarry Smith { 114577c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 11469b94acceSBarry Smith snes->atol = atol; 11479b94acceSBarry Smith return 0; 11489b94acceSBarry Smith } 11499b94acceSBarry Smith 11509b94acceSBarry Smith /*@ 11519b94acceSBarry Smith SNESSetSolutionTolerance - Sets the convergence tolerance in terms of 11529b94acceSBarry Smith the norm of the change in the solution between steps. 11539b94acceSBarry Smith 11549b94acceSBarry Smith Input Parameters: 11559b94acceSBarry Smith . snes - the SNES context 11569b94acceSBarry Smith . tol - tolerance 11579b94acceSBarry Smith 11589b94acceSBarry Smith Options Database Key: 11599b94acceSBarry Smith $ -snes_stol tol 11609b94acceSBarry Smith 11619b94acceSBarry Smith .keywords: SNES, nonlinear, set, solution, tolerance 11629b94acceSBarry Smith 116377c4ece6SBarry Smith .seealso: SNESSetTruncationTolerance(), SNESSetRelativeTolerance() 11649b94acceSBarry Smith @*/ 11659b94acceSBarry Smith int SNESSetSolutionTolerance( SNES snes, double tol ) 11669b94acceSBarry Smith { 116777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 11689b94acceSBarry Smith snes->xtol = tol; 11699b94acceSBarry Smith return 0; 11709b94acceSBarry Smith } 11719b94acceSBarry Smith 11729b94acceSBarry Smith /*@ 117377c4ece6SBarry Smith SNESSetMinimizationFunctionTolerance - Sets the minimum allowable function tolerance 11749b94acceSBarry Smith for unconstrained minimization solvers. 11759b94acceSBarry Smith 11769b94acceSBarry Smith Input Parameters: 11779b94acceSBarry Smith . snes - the SNES context 11789b94acceSBarry Smith . ftol - minimum function tolerance 11799b94acceSBarry Smith 11809b94acceSBarry Smith Options Database Key: 11819b94acceSBarry Smith $ -snes_fmin ftol 11829b94acceSBarry Smith 11839b94acceSBarry Smith Note: 118477c4ece6SBarry Smith SNESSetMinimizationFunctionTolerance() is valid for SNES_UNCONSTRAINED_MINIMIZATION 11859b94acceSBarry Smith methods only. 11869b94acceSBarry Smith 11879b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimum, convergence, function, tolerance 11889b94acceSBarry Smith 118977c4ece6SBarry Smith .seealso: SNESSetRelativeTolerance(), SNESSetSolutionTolerance() 11909b94acceSBarry Smith @*/ 119177c4ece6SBarry Smith int SNESSetMinimizationFunctionTolerance(SNES snes,double ftol) 11929b94acceSBarry Smith { 119377c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 11949b94acceSBarry Smith snes->fmin = ftol; 11959b94acceSBarry Smith return 0; 11969b94acceSBarry Smith } 11979b94acceSBarry Smith 11989b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 11999b94acceSBarry Smith 12009b94acceSBarry Smith /*@C 12019b94acceSBarry Smith SNESSetMonitor - Sets the function that is to be used at every 12029b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 12039b94acceSBarry Smith progress. 12049b94acceSBarry Smith 12059b94acceSBarry Smith Input Parameters: 12069b94acceSBarry Smith . snes - the SNES context 12079b94acceSBarry Smith . func - monitoring routine 12089b94acceSBarry Smith . mctx - optional user-defined context for private data for the 12099b94acceSBarry Smith monitor routine (may be null) 12109b94acceSBarry Smith 12119b94acceSBarry Smith Calling sequence of func: 1212682d7d0cSBarry Smith int func(SNES snes,int its, Vec x,Vec f,double norm,void *mctx) 12139b94acceSBarry Smith 12149b94acceSBarry Smith $ snes - the SNES context 12159b94acceSBarry Smith $ its - iteration number 12169b94acceSBarry Smith $ mctx - optional monitoring context 12179b94acceSBarry Smith $ 12189b94acceSBarry Smith $ SNES_NONLINEAR_EQUATIONS methods: 12199b94acceSBarry Smith $ norm - 2-norm function value (may be estimated) 12209b94acceSBarry Smith $ 12219b94acceSBarry Smith $ SNES_UNCONSTRAINED_MINIMIZATION methods: 12229b94acceSBarry Smith $ norm - 2-norm gradient value (may be estimated) 12239b94acceSBarry Smith 12249b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor 12259b94acceSBarry Smith 12269b94acceSBarry Smith .seealso: SNESDefaultMonitor() 12279b94acceSBarry Smith @*/ 12289b94acceSBarry Smith int SNESSetMonitor( SNES snes, int (*func)(SNES,int,double,void*), 12299b94acceSBarry Smith void *mctx ) 12309b94acceSBarry Smith { 12319b94acceSBarry Smith snes->monitor = func; 12329b94acceSBarry Smith snes->monP = (void*)mctx; 12339b94acceSBarry Smith return 0; 12349b94acceSBarry Smith } 12359b94acceSBarry Smith 12369b94acceSBarry Smith /*@C 12379b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 12389b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 12399b94acceSBarry Smith 12409b94acceSBarry Smith Input Parameters: 12419b94acceSBarry Smith . snes - the SNES context 12429b94acceSBarry Smith . func - routine to test for convergence 12439b94acceSBarry Smith . cctx - optional context for private data for the convergence routine 12449b94acceSBarry Smith (may be null) 12459b94acceSBarry Smith 12469b94acceSBarry Smith Calling sequence of func: 12479b94acceSBarry Smith int func (SNES snes,double xnorm,double gnorm, 12489b94acceSBarry Smith double f,void *cctx) 12499b94acceSBarry Smith 12509b94acceSBarry Smith $ snes - the SNES context 12519b94acceSBarry Smith $ cctx - optional convergence context 12529b94acceSBarry Smith $ xnorm - 2-norm of current iterate 12539b94acceSBarry Smith $ 12549b94acceSBarry Smith $ SNES_NONLINEAR_EQUATIONS methods: 12559b94acceSBarry Smith $ gnorm - 2-norm of current step 12569b94acceSBarry Smith $ f - 2-norm of function 12579b94acceSBarry Smith $ 12589b94acceSBarry Smith $ SNES_UNCONSTRAINED_MINIMIZATION methods: 12599b94acceSBarry Smith $ gnorm - 2-norm of current gradient 12609b94acceSBarry Smith $ f - function value 12619b94acceSBarry Smith 12629b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test 12639b94acceSBarry Smith 12649b94acceSBarry Smith .seealso: SNESDefaultConverged() 12659b94acceSBarry Smith @*/ 12669b94acceSBarry Smith int SNESSetConvergenceTest(SNES snes, 12679b94acceSBarry Smith int (*func)(SNES,double,double,double,void*),void *cctx) 12689b94acceSBarry Smith { 12699b94acceSBarry Smith (snes)->converged = func; 12709b94acceSBarry Smith (snes)->cnvP = cctx; 12719b94acceSBarry Smith return 0; 12729b94acceSBarry Smith } 12739b94acceSBarry Smith 12749b94acceSBarry Smith /* 12759b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 12769b94acceSBarry Smith positive parameter delta. 12779b94acceSBarry Smith 12789b94acceSBarry Smith Input Parameters: 12799b94acceSBarry Smith . snes - the SNES context 12809b94acceSBarry Smith . y - approximate solution of linear system 12819b94acceSBarry Smith . fnorm - 2-norm of current function 12829b94acceSBarry Smith . delta - trust region size 12839b94acceSBarry Smith 12849b94acceSBarry Smith Output Parameters: 12859b94acceSBarry Smith . gpnorm - predicted function norm at the new point, assuming local 12869b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 12879b94acceSBarry Smith region, and exceeds zero otherwise. 12889b94acceSBarry Smith . ynorm - 2-norm of the step 12899b94acceSBarry Smith 12909b94acceSBarry Smith Note: 1291ddd12767SBarry Smith For non-trust region methods such as SNES_EQ_NLS, the parameter delta 12929b94acceSBarry Smith is set to be the maximum allowable step size. 12939b94acceSBarry Smith 12949b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step 12959b94acceSBarry Smith */ 12969b94acceSBarry Smith int SNESScaleStep_Private(SNES snes,Vec y,double *fnorm,double *delta, 12979b94acceSBarry Smith double *gpnorm,double *ynorm) 12989b94acceSBarry Smith { 12999b94acceSBarry Smith double norm; 13009b94acceSBarry Smith Scalar cnorm; 1301cddf8d76SBarry Smith VecNorm(y,NORM_2, &norm ); 13029b94acceSBarry Smith if (norm > *delta) { 13039b94acceSBarry Smith norm = *delta/norm; 13049b94acceSBarry Smith *gpnorm = (1.0 - norm)*(*fnorm); 13059b94acceSBarry Smith cnorm = norm; 13069b94acceSBarry Smith VecScale( &cnorm, y ); 13079b94acceSBarry Smith *ynorm = *delta; 13089b94acceSBarry Smith } else { 13099b94acceSBarry Smith *gpnorm = 0.0; 13109b94acceSBarry Smith *ynorm = norm; 13119b94acceSBarry Smith } 13129b94acceSBarry Smith return 0; 13139b94acceSBarry Smith } 13149b94acceSBarry Smith 13159b94acceSBarry Smith /*@ 13169b94acceSBarry Smith SNESSolve - Solves a nonlinear system. Call SNESSolve after calling 131763a78c88SLois Curfman McInnes SNESCreate() and optional routines of the form SNESSetXXX(). 13189b94acceSBarry Smith 13199b94acceSBarry Smith Input Parameter: 13209b94acceSBarry Smith . snes - the SNES context 13218ddd3da0SLois Curfman McInnes . x - the solution vector 13229b94acceSBarry Smith 13239b94acceSBarry Smith Output Parameter: 13249b94acceSBarry Smith its - number of iterations until termination 13259b94acceSBarry Smith 13268ddd3da0SLois Curfman McInnes Note: 13278ddd3da0SLois Curfman McInnes The user should initialize the vector, x, with the initial guess 13288ddd3da0SLois Curfman McInnes for the nonlinear solve prior to calling SNESSolve. In particular, 13298ddd3da0SLois Curfman McInnes to employ an initial guess of zero, the user should explicitly set 13308ddd3da0SLois Curfman McInnes this vector to zero by calling VecSet(). 13318ddd3da0SLois Curfman McInnes 13329b94acceSBarry Smith .keywords: SNES, nonlinear, solve 13339b94acceSBarry Smith 133463a78c88SLois Curfman McInnes .seealso: SNESCreate(), SNESDestroy() 13359b94acceSBarry Smith @*/ 13368ddd3da0SLois Curfman McInnes int SNESSolve(SNES snes,Vec x,int *its) 13379b94acceSBarry Smith { 13383c7409f5SSatish Balay int ierr, flg; 1339052efed2SBarry Smith 134077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 1341c4fc05e7SBarry Smith if (!snes->setup_called) {ierr = SNESSetUp(snes,x); CHKERRQ(ierr);} 1342c4fc05e7SBarry Smith else {snes->vec_sol = snes->vec_sol_always = x;} 13439b94acceSBarry Smith PLogEventBegin(SNES_Solve,snes,0,0,0); 13449b94acceSBarry Smith ierr = (*(snes)->solve)(snes,its); CHKERRQ(ierr); 13459b94acceSBarry Smith PLogEventEnd(SNES_Solve,snes,0,0,0); 13463c7409f5SSatish Balay ierr = OptionsHasName(PETSC_NULL,"-snes_view", &flg); CHKERRQ(ierr); 13473c7409f5SSatish Balay if (flg) { ierr = SNESView(snes,STDOUT_VIEWER_WORLD); CHKERRQ(ierr); } 13489b94acceSBarry Smith return 0; 13499b94acceSBarry Smith } 13509b94acceSBarry Smith 13519b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 135237fcc0dbSBarry Smith static NRList *__SNESList = 0; 13539b94acceSBarry Smith 13549b94acceSBarry Smith /*@ 13554b0e389bSBarry Smith SNESSetType - Sets the method for the nonlinear solver. 13569b94acceSBarry Smith 13579b94acceSBarry Smith Input Parameters: 13589b94acceSBarry Smith . snes - the SNES context 13599b94acceSBarry Smith . method - a known method 13609b94acceSBarry Smith 13619b94acceSBarry Smith Notes: 13629b94acceSBarry Smith See "petsc/include/snes.h" for available methods (for instance) 13639b94acceSBarry Smith $ Systems of nonlinear equations: 1364ddd12767SBarry Smith $ SNES_EQ_NLS - Newton's method with line search 1365ddd12767SBarry Smith $ SNES_EQ_NTR - Newton's method with trust region 13669b94acceSBarry Smith $ Unconstrained minimization: 13679b94acceSBarry Smith $ SNES_UM_NTR - Newton's method with trust region 1368a703fe33SLois Curfman McInnes $ SNES_UM_NLS - Newton's method with line search 13699b94acceSBarry Smith 13709b94acceSBarry Smith Options Database Command: 13714b0e389bSBarry Smith $ -snes_type <method> 13729b94acceSBarry Smith $ Use -help for a list of available methods 13739b94acceSBarry Smith $ (for instance, ls or tr) 1374a703fe33SLois Curfman McInnes 1375a703fe33SLois Curfman McInnes .keysords: SNES, set, method 13769b94acceSBarry Smith @*/ 13774b0e389bSBarry Smith int SNESSetType(SNES snes,SNESType method) 13789b94acceSBarry Smith { 13799b94acceSBarry Smith int (*r)(SNES); 138077c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 13819b94acceSBarry Smith /* Get the function pointers for the iterative method requested */ 138237fcc0dbSBarry Smith if (!__SNESList) {SNESRegisterAll();} 138337fcc0dbSBarry Smith if (!__SNESList) {SETERRQ(1,"SNESSetType:Could not get methods");} 138437fcc0dbSBarry Smith r = (int (*)(SNES))NRFindRoutine( __SNESList, (int)method, (char *)0 ); 13854b0e389bSBarry Smith if (!r) {SETERRQ(1,"SNESSetType:Unknown method");} 13860452661fSBarry Smith if (snes->data) PetscFree(snes->data); 13879b94acceSBarry Smith snes->set_method_called = 1; 13889b94acceSBarry Smith return (*r)(snes); 13899b94acceSBarry Smith } 13909b94acceSBarry Smith 13919b94acceSBarry Smith /* --------------------------------------------------------------------- */ 13929b94acceSBarry Smith /*@C 13939b94acceSBarry Smith SNESRegister - Adds the method to the nonlinear solver package, given 13944b0e389bSBarry Smith a function pointer and a nonlinear solver name of the type SNESType. 13959b94acceSBarry Smith 13969b94acceSBarry Smith Input Parameters: 1397ddd12767SBarry Smith . name - for instance SNES_EQ_NLS, SNES_EQ_NTR, ... 13989b94acceSBarry Smith . sname - corfunPonding string for name 13999b94acceSBarry Smith . create - routine to create method context 14009b94acceSBarry Smith 14019b94acceSBarry Smith .keywords: SNES, nonlinear, register 14029b94acceSBarry Smith 14039b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterDestroy() 14049b94acceSBarry Smith @*/ 14059b94acceSBarry Smith int SNESRegister(int name, char *sname, int (*create)(SNES)) 14069b94acceSBarry Smith { 14079b94acceSBarry Smith int ierr; 140837fcc0dbSBarry Smith if (!__SNESList) {ierr = NRCreate(&__SNESList); CHKERRQ(ierr);} 140937fcc0dbSBarry Smith NRRegister( __SNESList, name, sname, (int (*)(void*))create ); 14109b94acceSBarry Smith return 0; 14119b94acceSBarry Smith } 14129b94acceSBarry Smith /* --------------------------------------------------------------------- */ 14139b94acceSBarry Smith /*@C 14149b94acceSBarry Smith SNESRegisterDestroy - Frees the list of nonlinear solvers that were 14159b94acceSBarry Smith registered by SNESRegister(). 14169b94acceSBarry Smith 14179b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy 14189b94acceSBarry Smith 14199b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll() 14209b94acceSBarry Smith @*/ 14219b94acceSBarry Smith int SNESRegisterDestroy() 14229b94acceSBarry Smith { 142337fcc0dbSBarry Smith if (__SNESList) { 142437fcc0dbSBarry Smith NRDestroy( __SNESList ); 142537fcc0dbSBarry Smith __SNESList = 0; 14269b94acceSBarry Smith } 14279b94acceSBarry Smith return 0; 14289b94acceSBarry Smith } 14299b94acceSBarry Smith 14309b94acceSBarry Smith /* 14314b0e389bSBarry Smith SNESGetTypeFromOptions_Private - Sets the selected method from the 14329b94acceSBarry Smith options database. 14339b94acceSBarry Smith 14349b94acceSBarry Smith Input Parameter: 14359b94acceSBarry Smith . ctx - the SNES context 14369b94acceSBarry Smith 14379b94acceSBarry Smith Output Parameter: 14389b94acceSBarry Smith . method - solver method 14399b94acceSBarry Smith 14409b94acceSBarry Smith Returns: 14419b94acceSBarry Smith Returns 1 if the method is found; 0 otherwise. 14429b94acceSBarry Smith 14439b94acceSBarry Smith Options Database Key: 14444b0e389bSBarry Smith $ -snes_type method 14459b94acceSBarry Smith */ 1446052efed2SBarry Smith int SNESGetTypeFromOptions_Private(SNES ctx,SNESType *method,int *flg) 14479b94acceSBarry Smith { 1448052efed2SBarry Smith int ierr; 14499b94acceSBarry Smith char sbuf[50]; 1450052efed2SBarry Smith ierr = OptionsGetString(ctx->prefix,"-snes_type", sbuf, 50, flg); CHKERRQ(ierr); 1451052efed2SBarry Smith if (*flg) { 1452052efed2SBarry Smith if (!__SNESList) {ierr = SNESRegisterAll(); CHKERRQ(ierr);} 145337fcc0dbSBarry Smith *method = (SNESType)NRFindID( __SNESList, sbuf ); 14549b94acceSBarry Smith } 14559b94acceSBarry Smith return 0; 14569b94acceSBarry Smith } 14579b94acceSBarry Smith 14589b94acceSBarry Smith /*@C 14599a28b0a6SLois Curfman McInnes SNESGetType - Gets the SNES method type and name (as a string). 14609b94acceSBarry Smith 14619b94acceSBarry Smith Input Parameter: 14624b0e389bSBarry Smith . snes - nonlinear solver context 14639b94acceSBarry Smith 14649b94acceSBarry Smith Output Parameter: 14659a28b0a6SLois Curfman McInnes . method - SNES method (or use PETSC_NULL) 14669a28b0a6SLois Curfman McInnes . name - name of SNES method (or use PETSC_NULL) 14679b94acceSBarry Smith 14689b94acceSBarry Smith .keywords: SNES, nonlinear, get, method, name 14699b94acceSBarry Smith @*/ 14704b0e389bSBarry Smith int SNESGetType(SNES snes, SNESType *method,char **name) 14719b94acceSBarry Smith { 147206a9b0adSLois Curfman McInnes int ierr; 147337fcc0dbSBarry Smith if (!__SNESList) {ierr = SNESRegisterAll(); CHKERRQ(ierr);} 14744b0e389bSBarry Smith if (method) *method = (SNESType) snes->type; 147537fcc0dbSBarry Smith if (name) *name = NRFindName( __SNESList, (int) snes->type ); 14769b94acceSBarry Smith return 0; 14779b94acceSBarry Smith } 14789b94acceSBarry Smith 14799b94acceSBarry Smith #include <stdio.h> 14809b94acceSBarry Smith /* 14814b0e389bSBarry Smith SNESPrintTypes_Private - Prints the SNES methods available from the 14829b94acceSBarry Smith options database. 14839b94acceSBarry Smith 14849b94acceSBarry Smith Input Parameters: 148533455573SSatish Balay . comm - communicator (usually MPI_COMM_WORLD) 14869b94acceSBarry Smith . prefix - prefix (usually "-") 14874b0e389bSBarry Smith . name - the options database name (by default "snes_type") 14889b94acceSBarry Smith */ 148933455573SSatish Balay int SNESPrintTypes_Private(MPI_Comm comm,char* prefix,char *name) 14909b94acceSBarry Smith { 14919b94acceSBarry Smith FuncList *entry; 149237fcc0dbSBarry Smith if (!__SNESList) {SNESRegisterAll();} 149337fcc0dbSBarry Smith entry = __SNESList->head; 149477c4ece6SBarry Smith PetscPrintf(comm," %s%s (one of)",prefix,name); 14959b94acceSBarry Smith while (entry) { 149677c4ece6SBarry Smith PetscPrintf(comm," %s",entry->name); 14979b94acceSBarry Smith entry = entry->next; 14989b94acceSBarry Smith } 149977c4ece6SBarry Smith PetscPrintf(comm,"\n"); 15009b94acceSBarry Smith return 0; 15019b94acceSBarry Smith } 15029b94acceSBarry Smith 15039b94acceSBarry Smith /*@C 15049b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 15059b94acceSBarry Smith stored. 15069b94acceSBarry Smith 15079b94acceSBarry Smith Input Parameter: 15089b94acceSBarry Smith . snes - the SNES context 15099b94acceSBarry Smith 15109b94acceSBarry Smith Output Parameter: 15119b94acceSBarry Smith . x - the solution 15129b94acceSBarry Smith 15139b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution 15149b94acceSBarry Smith 1515a86d99e1SLois Curfman McInnes .seealso: SNESGetFunction(), SNESGetGradient(), SNESGetSolutionUpdate() 15169b94acceSBarry Smith @*/ 15179b94acceSBarry Smith int SNESGetSolution(SNES snes,Vec *x) 15189b94acceSBarry Smith { 151977c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 15209b94acceSBarry Smith *x = snes->vec_sol_always; 15219b94acceSBarry Smith return 0; 15229b94acceSBarry Smith } 15239b94acceSBarry Smith 15249b94acceSBarry Smith /*@C 15259b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 15269b94acceSBarry Smith stored. 15279b94acceSBarry Smith 15289b94acceSBarry Smith Input Parameter: 15299b94acceSBarry Smith . snes - the SNES context 15309b94acceSBarry Smith 15319b94acceSBarry Smith Output Parameter: 15329b94acceSBarry Smith . x - the solution update 15339b94acceSBarry Smith 15349b94acceSBarry Smith Notes: 15359b94acceSBarry Smith This vector is implementation dependent. 15369b94acceSBarry Smith 15379b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update 15389b94acceSBarry Smith 15399b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction 15409b94acceSBarry Smith @*/ 15419b94acceSBarry Smith int SNESGetSolutionUpdate(SNES snes,Vec *x) 15429b94acceSBarry Smith { 154377c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 15449b94acceSBarry Smith *x = snes->vec_sol_update_always; 15459b94acceSBarry Smith return 0; 15469b94acceSBarry Smith } 15479b94acceSBarry Smith 15489b94acceSBarry Smith /*@C 15493638b69dSLois Curfman McInnes SNESGetFunction - Returns the vector where the function is stored. 15509b94acceSBarry Smith 15519b94acceSBarry Smith Input Parameter: 15529b94acceSBarry Smith . snes - the SNES context 15539b94acceSBarry Smith 15549b94acceSBarry Smith Output Parameter: 15553638b69dSLois Curfman McInnes . r - the function 15569b94acceSBarry Smith 15579b94acceSBarry Smith Notes: 15589b94acceSBarry Smith SNESGetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only 15599b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 15609b94acceSBarry Smith SNESGetMinimizationFunction() and SNESGetGradient(); 15619b94acceSBarry Smith 1562a86d99e1SLois Curfman McInnes .keywords: SNES, nonlinear, get, function 15639b94acceSBarry Smith 156431615d04SLois Curfman McInnes .seealso: SNESSetFunction(), SNESGetSolution(), SNESGetMinimizationFunction(), 156531615d04SLois Curfman McInnes SNESGetGradient() 15669b94acceSBarry Smith @*/ 15679b94acceSBarry Smith int SNESGetFunction(SNES snes,Vec *r) 15689b94acceSBarry Smith { 156977c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 15709b94acceSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1, 157148d91487SBarry Smith "SNESGetFunction:For SNES_NONLINEAR_EQUATIONS only"); 15729b94acceSBarry Smith *r = snes->vec_func_always; 15739b94acceSBarry Smith return 0; 15749b94acceSBarry Smith } 15759b94acceSBarry Smith 15769b94acceSBarry Smith /*@C 15773638b69dSLois Curfman McInnes SNESGetGradient - Returns the vector where the gradient is stored. 15789b94acceSBarry Smith 15799b94acceSBarry Smith Input Parameter: 15809b94acceSBarry Smith . snes - the SNES context 15819b94acceSBarry Smith 15829b94acceSBarry Smith Output Parameter: 15839b94acceSBarry Smith . r - the gradient 15849b94acceSBarry Smith 15859b94acceSBarry Smith Notes: 15869b94acceSBarry Smith SNESGetGradient() is valid for SNES_UNCONSTRAINED_MINIMIZATION methods 15879b94acceSBarry Smith only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 15889b94acceSBarry Smith SNESGetFunction(). 15899b94acceSBarry Smith 15909b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient 15919b94acceSBarry Smith 159231615d04SLois Curfman McInnes .seealso: SNESGetMinimizationFunction(), SNESGetSolution(), SNESGetFunction() 15939b94acceSBarry Smith @*/ 15949b94acceSBarry Smith int SNESGetGradient(SNES snes,Vec *r) 15959b94acceSBarry Smith { 159677c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 15979b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 159848d91487SBarry Smith "SNESGetGradient:For SNES_UNCONSTRAINED_MINIMIZATION only"); 15999b94acceSBarry Smith *r = snes->vec_func_always; 16009b94acceSBarry Smith return 0; 16019b94acceSBarry Smith } 16029b94acceSBarry Smith 16039b94acceSBarry Smith /*@ 16049b94acceSBarry Smith SNESGetMinimizationFunction - Returns the scalar function value for 16059b94acceSBarry Smith unconstrained minimization problems. 16069b94acceSBarry Smith 16079b94acceSBarry Smith Input Parameter: 16089b94acceSBarry Smith . snes - the SNES context 16099b94acceSBarry Smith 16109b94acceSBarry Smith Output Parameter: 16119b94acceSBarry Smith . r - the function 16129b94acceSBarry Smith 16139b94acceSBarry Smith Notes: 16149b94acceSBarry Smith SNESGetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 16159b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 16169b94acceSBarry Smith SNESGetFunction(). 16179b94acceSBarry Smith 16189b94acceSBarry Smith .keywords: SNES, nonlinear, get, function 16199b94acceSBarry Smith 162031615d04SLois Curfman McInnes .seealso: SNESGetGradient(), SNESGetSolution(), SNESGetFunction() 16219b94acceSBarry Smith @*/ 16229b94acceSBarry Smith int SNESGetMinimizationFunction(SNES snes,double *r) 16239b94acceSBarry Smith { 162477c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 16259b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 162648d91487SBarry Smith "SNESGetMinimizationFunction:For SNES_UNCONSTRAINED_MINIMIZATION only"); 16279b94acceSBarry Smith *r = snes->fc; 16289b94acceSBarry Smith return 0; 16299b94acceSBarry Smith } 16309b94acceSBarry Smith 16313c7409f5SSatish Balay /*@C 16323c7409f5SSatish Balay SNESSetOptionsPrefix - Sets the prefix used for searching for all 16333c7409f5SSatish Balay SNES options in the database. 16343c7409f5SSatish Balay 16353c7409f5SSatish Balay Input Parameter: 16363c7409f5SSatish Balay . snes - the SNES context 16373c7409f5SSatish Balay . prefix - the prefix to prepend to all option names 16383c7409f5SSatish Balay 16393c7409f5SSatish Balay .keywords: SNES, set, options, prefix, database 1640a86d99e1SLois Curfman McInnes 1641a86d99e1SLois Curfman McInnes .seealso: SNESSetFromOptions() 16423c7409f5SSatish Balay @*/ 16433c7409f5SSatish Balay int SNESSetOptionsPrefix(SNES snes,char *prefix) 16443c7409f5SSatish Balay { 16453c7409f5SSatish Balay int ierr; 16463c7409f5SSatish Balay 164777c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 16483c7409f5SSatish Balay ierr = PetscObjectSetPrefix((PetscObject)snes, prefix); CHKERRQ(ierr); 16493c7409f5SSatish Balay ierr = SLESSetOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr); 16503c7409f5SSatish Balay return 0; 16513c7409f5SSatish Balay } 16523c7409f5SSatish Balay 16533c7409f5SSatish Balay /*@C 16543c7409f5SSatish Balay SNESAppendOptionsPrefix - Append to the prefix used for searching for all 16553c7409f5SSatish Balay SNES options in the database. 16563c7409f5SSatish Balay 16573c7409f5SSatish Balay Input Parameter: 16583c7409f5SSatish Balay . snes - the SNES context 16593c7409f5SSatish Balay . prefix - the prefix to prepend to all option names 16603c7409f5SSatish Balay 16613c7409f5SSatish Balay .keywords: SNES, append, options, prefix, database 1662a86d99e1SLois Curfman McInnes 1663a86d99e1SLois Curfman McInnes .seealso: SNESGetOptionsPrefix() 16643c7409f5SSatish Balay @*/ 16653c7409f5SSatish Balay int SNESAppendOptionsPrefix(SNES snes,char *prefix) 16663c7409f5SSatish Balay { 16673c7409f5SSatish Balay int ierr; 16683c7409f5SSatish Balay 166977c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 16703c7409f5SSatish Balay ierr = PetscObjectAppendPrefix((PetscObject)snes, prefix); CHKERRQ(ierr); 16713c7409f5SSatish Balay ierr = SLESAppendOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr); 16723c7409f5SSatish Balay return 0; 16733c7409f5SSatish Balay } 16743c7409f5SSatish Balay 1675c83590e2SSatish Balay /*@ 16763c7409f5SSatish Balay SNESGetOptionsPrefix - Sets the prefix used for searching for all 16773c7409f5SSatish Balay SNES options in the database. 16783c7409f5SSatish Balay 16793c7409f5SSatish Balay Input Parameter: 16803c7409f5SSatish Balay . snes - the SNES context 16813c7409f5SSatish Balay 16823c7409f5SSatish Balay Output Parameter: 16833c7409f5SSatish Balay . prefix - pointer to the prefix string used 16843c7409f5SSatish Balay 16853c7409f5SSatish Balay .keywords: SNES, get, options, prefix, database 1686a86d99e1SLois Curfman McInnes 1687a86d99e1SLois Curfman McInnes .seealso: SNESAppendOptionsPrefix() 16883c7409f5SSatish Balay @*/ 16893c7409f5SSatish Balay int SNESGetOptionsPrefix(SNES snes,char **prefix) 16903c7409f5SSatish Balay { 16913c7409f5SSatish Balay int ierr; 16923c7409f5SSatish Balay 169377c4ece6SBarry Smith PetscValidHeaderSpecific(snes,SNES_COOKIE); 16943c7409f5SSatish Balay ierr = PetscObjectGetPrefix((PetscObject)snes, prefix); CHKERRQ(ierr); 16953c7409f5SSatish Balay return 0; 16963c7409f5SSatish Balay } 16973c7409f5SSatish Balay 16983c7409f5SSatish Balay 16999b94acceSBarry Smith 17009b94acceSBarry Smith 17019b94acceSBarry Smith 1702