19b94acceSBarry Smith #ifndef lint 2*a703fe33SLois Curfman McInnes static char vcid[] = "$Id: snes.c,v 1.21 1995/10/12 04:19:54 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*/ 79b94acceSBarry Smith #include "sys/nreg.h" /*I "sys/nreg.h" I*/ 89b94acceSBarry Smith #include "pinclude/pviewer.h" 99b94acceSBarry Smith #include <math.h> 109b94acceSBarry Smith 119b94acceSBarry Smith extern int SNESGetMethodFromOptions_Private(SNES,SNESMethod*); 129b94acceSBarry Smith extern int SNESPrintMethods_Private(char*,char*); 139b94acceSBarry Smith 149b94acceSBarry Smith /*@ 159b94acceSBarry Smith SNESView - Prints the SNES data structure. 169b94acceSBarry Smith 179b94acceSBarry Smith Input Parameters: 189b94acceSBarry Smith . SNES - the SNES context 199b94acceSBarry Smith . viewer - visualization context 209b94acceSBarry Smith 219b94acceSBarry Smith Options Database Key: 229b94acceSBarry Smith $ -snes_view : calls SNESView() at end of SNESSolve() 239b94acceSBarry Smith 249b94acceSBarry Smith Notes: 259b94acceSBarry Smith The available visualization contexts include 269b94acceSBarry Smith $ STDOUT_VIEWER_SELF - standard output (default) 279b94acceSBarry Smith $ STDOUT_VIEWER_WORLD - synchronized standard 289b94acceSBarry Smith $ output where only the first processor opens 299b94acceSBarry Smith $ the file. All other processors send their 309b94acceSBarry Smith $ data to the first processor to print. 319b94acceSBarry Smith 329b94acceSBarry Smith The user can open alternative vistualization contexts with 33dbb450caSBarry Smith $ ViewerFileOpenASCII() - output to a specified file 349b94acceSBarry Smith 359b94acceSBarry Smith .keywords: SNES, view 369b94acceSBarry Smith 37dbb450caSBarry Smith .seealso: ViewerFileOpenASCII() 389b94acceSBarry Smith @*/ 399b94acceSBarry Smith int SNESView(SNES snes,Viewer viewer) 409b94acceSBarry Smith { 419b94acceSBarry Smith PetscObject vobj = (PetscObject) viewer; 429b94acceSBarry Smith SNES_KSP_EW_ConvCtx *kctx; 439b94acceSBarry Smith FILE *fd; 449b94acceSBarry Smith int ierr; 459b94acceSBarry Smith SLES sles; 469b94acceSBarry Smith char *method; 479b94acceSBarry Smith 489b94acceSBarry Smith if (vobj->cookie == VIEWER_COOKIE && (vobj->type == ASCII_FILE_VIEWER || 499b94acceSBarry Smith vobj->type == ASCII_FILES_VIEWER)) { 509b94acceSBarry Smith ierr = ViewerFileGetPointer_Private(viewer,&fd); CHKERRQ(ierr); 519b94acceSBarry Smith MPIU_fprintf(snes->comm,fd,"SNES Object:\n"); 529b94acceSBarry Smith SNESGetMethodName((SNESMethod)snes->type,&method); 539b94acceSBarry Smith MPIU_fprintf(snes->comm,fd," method: %s\n",method); 549b94acceSBarry Smith if (snes->view) (*snes->view)((PetscObject)snes,viewer); 559b94acceSBarry Smith MPIU_fprintf(snes->comm,fd, 569b94acceSBarry Smith " maximum iterations=%d, maximum function evaluations=%d\n", 579b94acceSBarry Smith snes->max_its,snes->max_funcs); 589b94acceSBarry Smith MPIU_fprintf(snes->comm,fd, 599b94acceSBarry Smith " tolerances: relative=%g, absolute=%g, truncation=%g, solution=%g\n", 609b94acceSBarry Smith snes->rtol, snes->atol, snes->trunctol, snes->xtol); 619b94acceSBarry Smith if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) 629b94acceSBarry Smith MPIU_fprintf(snes->comm,fd," min function tolerance=%g\n",snes->fmin); 639b94acceSBarry Smith if (snes->ksp_ewconv) { 649b94acceSBarry Smith kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx; 659b94acceSBarry Smith if (kctx) { 669b94acceSBarry Smith MPIU_fprintf(snes->comm,fd, 679b94acceSBarry Smith " Eisenstat-Walker computation of KSP relative tolerance (version %d)\n", 689b94acceSBarry Smith kctx->version); 699b94acceSBarry Smith MPIU_fprintf(snes->comm,fd, 709b94acceSBarry Smith " rtol_0=%g, rtol_max=%g, threshold=%g\n",kctx->rtol_0, 719b94acceSBarry Smith kctx->rtol_max,kctx->threshold); 729b94acceSBarry Smith MPIU_fprintf(snes->comm,fd," gamma=%g, alpha=%g, alpha2=%g\n", 739b94acceSBarry Smith kctx->gamma,kctx->alpha,kctx->alpha2); 749b94acceSBarry Smith } 759b94acceSBarry Smith } 769b94acceSBarry Smith SNESGetSLES(snes,&sles); 779b94acceSBarry Smith ierr = SLESView(sles,viewer); CHKERRQ(ierr); 789b94acceSBarry Smith } 799b94acceSBarry Smith return 0; 809b94acceSBarry Smith } 819b94acceSBarry Smith 829b94acceSBarry Smith /*@ 839b94acceSBarry Smith SNESSetFromOptions - Sets various SLES parameters from user options. 849b94acceSBarry Smith 859b94acceSBarry Smith Input Parameter: 869b94acceSBarry Smith . snes - the SNES context 879b94acceSBarry Smith 889b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database 899b94acceSBarry Smith 909b94acceSBarry Smith .seealso: SNESPrintHelp() 919b94acceSBarry Smith @*/ 929b94acceSBarry Smith int SNESSetFromOptions(SNES snes) 939b94acceSBarry Smith { 949b94acceSBarry Smith SNESMethod method; 959b94acceSBarry Smith double tmp; 969b94acceSBarry Smith SLES sles; 979b94acceSBarry Smith int ierr; 989b94acceSBarry Smith int version = PETSC_DEFAULT; 999b94acceSBarry Smith double rtol_0 = PETSC_DEFAULT; 1009b94acceSBarry Smith double rtol_max = PETSC_DEFAULT; 1019b94acceSBarry Smith double gamma2 = PETSC_DEFAULT; 1029b94acceSBarry Smith double alpha = PETSC_DEFAULT; 1039b94acceSBarry Smith double alpha2 = PETSC_DEFAULT; 1049b94acceSBarry Smith double threshold = PETSC_DEFAULT; 1059b94acceSBarry Smith 1069b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 107*a703fe33SLois Curfman McInnes if (snes->setup_called) 108*a703fe33SLois Curfman McInnes SETERRQ(1,"SNESSetFromOptions:Must call prior to SNESSetUp!"); 1099b94acceSBarry Smith if (SNESGetMethodFromOptions_Private(snes,&method)) { 1109b94acceSBarry Smith SNESSetMethod(snes,method); 1119b94acceSBarry Smith } 1129b94acceSBarry Smith if (OptionsHasName(0,"-help")) SNESPrintHelp(snes); 1139b94acceSBarry Smith if (OptionsGetDouble(snes->prefix,"-snes_stol",&tmp)) { 1149b94acceSBarry Smith SNESSetSolutionTolerance(snes,tmp); 1159b94acceSBarry Smith } 1169b94acceSBarry Smith if (OptionsGetDouble(snes->prefix,"-snes_ttol",&tmp)) { 1179b94acceSBarry Smith SNESSetTruncationTolerance(snes,tmp); 1189b94acceSBarry Smith } 1199b94acceSBarry Smith if (OptionsGetDouble(snes->prefix,"-snes_atol",&tmp)) { 1209b94acceSBarry Smith SNESSetAbsoluteTolerance(snes,tmp); 1219b94acceSBarry Smith } 1229b94acceSBarry Smith if (OptionsGetDouble(snes->prefix,"-snes_trtol",&tmp)) { 1239b94acceSBarry Smith SNESSetTrustRegionTolerance(snes,tmp); 1249b94acceSBarry Smith } 1259b94acceSBarry Smith if (OptionsGetDouble(snes->prefix,"-snes_rtol",&tmp)) { 1269b94acceSBarry Smith SNESSetRelativeTolerance(snes,tmp); 1279b94acceSBarry Smith } 1289b94acceSBarry Smith if (OptionsGetDouble(snes->prefix,"-snes_fmin",&tmp)) { 1299b94acceSBarry Smith SNESSetMinFunctionTolerance(snes,tmp); 1309b94acceSBarry Smith } 1319b94acceSBarry Smith OptionsGetInt(snes->prefix,"-snes_max_it",&snes->max_its); 1329b94acceSBarry Smith OptionsGetInt(snes->prefix,"-snes_max_funcs",&snes->max_funcs); 1339b94acceSBarry Smith if (OptionsHasName(snes->prefix,"-snes_ksp_ew_conv")) { 1349b94acceSBarry Smith snes->ksp_ewconv = 1; 1359b94acceSBarry Smith } 1369b94acceSBarry Smith OptionsGetInt(snes->prefix,"-snes_ksp_ew_version",&version); 1379b94acceSBarry Smith OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtol0",&rtol_0); 1389b94acceSBarry Smith OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtolmax",&rtol_max); 1399b94acceSBarry Smith OptionsGetDouble(snes->prefix,"-snes_ksp_ew_gamma",&gamma2); 1409b94acceSBarry Smith OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha",&alpha); 1419b94acceSBarry Smith OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha2",&alpha2); 1429b94acceSBarry Smith OptionsGetDouble(snes->prefix,"-snes_ksp_ew_threshold",&threshold); 1439b94acceSBarry Smith ierr = SNES_KSP_SetParametersEW(snes,version,rtol_0,rtol_max,gamma2,alpha, 1449b94acceSBarry Smith alpha2,threshold); CHKERRQ(ierr); 1459b94acceSBarry Smith if (OptionsHasName(snes->prefix,"-snes_monitor")) { 1469b94acceSBarry Smith SNESSetMonitor(snes,SNESDefaultMonitor,0); 1479b94acceSBarry Smith } 1489b94acceSBarry Smith if (OptionsHasName(snes->prefix,"-snes_smonitor")) { 1499b94acceSBarry Smith SNESSetMonitor(snes,SNESDefaultSMonitor,0); 1509b94acceSBarry Smith } 1519b94acceSBarry Smith if (OptionsHasName(snes->prefix,"-snes_xmonitor")){ 1529b94acceSBarry Smith int mytid = 0; 1539b94acceSBarry Smith DrawLGCtx lg; 1549b94acceSBarry Smith MPI_Initialized(&mytid); 1559b94acceSBarry Smith if (mytid) MPI_Comm_rank(snes->comm,&mytid); 1569b94acceSBarry Smith if (!mytid) { 1579b94acceSBarry Smith ierr = SNESLGMonitorCreate(0,0,0,0,300,300,&lg); CHKERRQ(ierr); 1589b94acceSBarry Smith ierr = SNESSetMonitor(snes,SNESLGMonitor,(void *)lg); CHKERRQ(ierr); 1599b94acceSBarry Smith PLogObjectParent(snes,lg); 1609b94acceSBarry Smith } 1619b94acceSBarry Smith } 1629b94acceSBarry Smith if (OptionsHasName(snes->prefix,"-snes_fd") && 1639b94acceSBarry Smith snes->method_class == SNES_NONLINEAR_EQUATIONS) { 1649b94acceSBarry Smith ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre, 1659b94acceSBarry Smith SNESDefaultComputeJacobian,snes->funP); CHKERRQ(ierr); 1669b94acceSBarry Smith } 1679b94acceSBarry Smith if (OptionsHasName(snes->prefix,"-snes_mf") && 1689b94acceSBarry Smith snes->method_class == SNES_NONLINEAR_EQUATIONS) { 1699b94acceSBarry Smith Mat J; 1709b94acceSBarry Smith ierr = SNESDefaultMatrixFreeMatCreate(snes,snes->vec_sol,&J);CHKERRQ(ierr); 1719b94acceSBarry Smith ierr = SNESSetJacobian(snes,J,J,0,snes->funP); CHKERRQ(ierr); 1729b94acceSBarry Smith PLogObjectParent(snes,J); 1739b94acceSBarry Smith snes->mfshell = J; 1749b94acceSBarry Smith } 1759b94acceSBarry Smith ierr = SNESGetSLES(snes,&sles); CHKERRQ(ierr); 1769b94acceSBarry Smith ierr = SLESSetFromOptions(sles); CHKERRQ(ierr); 1779b94acceSBarry Smith if (!snes->setfromoptions) return 0; 1789b94acceSBarry Smith return (*snes->setfromoptions)(snes); 1799b94acceSBarry Smith } 1809b94acceSBarry Smith 1819b94acceSBarry Smith /*@ 1829b94acceSBarry Smith SNESPrintHelp - Prints all options for the SNES component. 1839b94acceSBarry Smith 1849b94acceSBarry Smith Input Parameter: 1859b94acceSBarry Smith . snes - the SNES context 1869b94acceSBarry Smith 187*a703fe33SLois Curfman McInnes Options Database Keys: 188*a703fe33SLois Curfman McInnes $ -help, -h 189*a703fe33SLois Curfman McInnes 1909b94acceSBarry Smith .keywords: SNES, nonlinear, help 1919b94acceSBarry Smith 1929b94acceSBarry Smith .seealso: SLESSetFromOptions() 1939b94acceSBarry Smith @*/ 1949b94acceSBarry Smith int SNESPrintHelp(SNES snes) 1959b94acceSBarry Smith { 1969b94acceSBarry Smith char *prefix = "-"; 1979b94acceSBarry Smith SNES_KSP_EW_ConvCtx *kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx; 1989b94acceSBarry Smith if (snes->prefix) prefix = snes->prefix; 1999b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 2009b94acceSBarry Smith MPIU_printf(snes->comm,"SNES options ----------------------------\n"); 2019b94acceSBarry Smith SNESPrintMethods_Private(prefix,"snes_method"); 2020de55854SLois Curfman McInnes MPIU_printf(snes->comm," %ssnes_monitor: use default SNES monitor\n",prefix); 2030de55854SLois Curfman McInnes MPIU_printf(snes->comm," %ssnes_view: view SNES info after each nonlinear solve\n",prefix); 2049b94acceSBarry Smith MPIU_printf(snes->comm," %ssnes_max_it its (default %d)\n",prefix,snes->max_its); 2059b94acceSBarry Smith MPIU_printf(snes->comm," %ssnes_stol tol (default %g)\n",prefix,snes->xtol); 2069b94acceSBarry Smith MPIU_printf(snes->comm," %ssnes_atol tol (default %g)\n",prefix,snes->atol); 2079b94acceSBarry Smith MPIU_printf(snes->comm," %ssnes_rtol tol (default %g)\n",prefix,snes->rtol); 2089b94acceSBarry Smith MPIU_printf(snes->comm," %ssnes_ttol tol (default %g)\n",prefix,snes->trunctol); 2099b94acceSBarry Smith MPIU_printf(snes->comm, 2109b94acceSBarry Smith " options for solving systems of nonlinear equations only:\n"); 2119b94acceSBarry Smith MPIU_printf(snes->comm," %ssnes_fd: use finite differences for Jacobian\n",prefix); 2129b94acceSBarry Smith MPIU_printf(snes->comm," %ssnes_mf: use matrix-free Jacobian\n",prefix); 2139b94acceSBarry Smith MPIU_printf(snes->comm," %ssnes_ksp_ew_conv: use Eisenstat-Walker computation of KSP rtol. Params are:\n",prefix); 2149b94acceSBarry Smith MPIU_printf(snes->comm, 2159b94acceSBarry Smith " %ssnes_ksp_ew_version version (1 or 2, default is %d)\n", 2169b94acceSBarry Smith prefix,kctx->version); 2179b94acceSBarry Smith MPIU_printf(snes->comm, 2189b94acceSBarry Smith " %ssnes_ksp_ew_rtol0 rtol0 (0 <= rtol0 < 1, default %g)\n", 2199b94acceSBarry Smith prefix,kctx->rtol_0); 2209b94acceSBarry Smith MPIU_printf(snes->comm, 2219b94acceSBarry Smith " %ssnes_ksp_ew_rtolmax rtolmax (0 <= rtolmax < 1, default %g)\n", 2229b94acceSBarry Smith prefix,kctx->rtol_max); 2239b94acceSBarry Smith MPIU_printf(snes->comm, 2249b94acceSBarry Smith " %ssnes_ksp_ew_gamma gamma (0 <= gamma <= 1, default %g)\n", 2259b94acceSBarry Smith prefix,kctx->gamma); 2269b94acceSBarry Smith MPIU_printf(snes->comm, 2279b94acceSBarry Smith " %ssnes_ksp_ew_alpha alpha (1 < alpha <= 2, default %g)\n", 2289b94acceSBarry Smith prefix,kctx->alpha); 2299b94acceSBarry Smith MPIU_printf(snes->comm, 2309b94acceSBarry Smith " %ssnes_ksp_ew_alpha2 alpha2 (default %g)\n", 2319b94acceSBarry Smith prefix,kctx->alpha2); 2329b94acceSBarry Smith MPIU_printf(snes->comm, 2339b94acceSBarry Smith " %ssnes_ksp_ew_threshold threshold (0 < threshold < 1, default %g)\n", 2349b94acceSBarry Smith prefix,kctx->threshold); 2359b94acceSBarry Smith MPIU_printf(snes->comm, 2369b94acceSBarry Smith " options for solving unconstrained minimization problems only:\n"); 2379b94acceSBarry Smith MPIU_printf(snes->comm," %ssnes_fmin tol (default %g)\n",prefix,snes->fmin); 2389b94acceSBarry Smith MPIU_printf(snes->comm," Run program with %ssnes_method method -help for help on ",prefix); 2399b94acceSBarry Smith MPIU_printf(snes->comm,"a particular method\n"); 2409b94acceSBarry Smith if (snes->printhelp) (*snes->printhelp)(snes); 2419b94acceSBarry Smith return 0; 2429b94acceSBarry Smith } 2439b94acceSBarry Smith /*@ 2449b94acceSBarry Smith SNESSetApplicationContext - Sets the optional user-defined context for 2459b94acceSBarry Smith the nonlinear solvers. 2469b94acceSBarry Smith 2479b94acceSBarry Smith Input Parameters: 2489b94acceSBarry Smith . snes - the SNES context 2499b94acceSBarry Smith . usrP - optional user context 2509b94acceSBarry Smith 2519b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context 2529b94acceSBarry Smith 2539b94acceSBarry Smith .seealso: SNESGetApplicationContext() 2549b94acceSBarry Smith @*/ 2559b94acceSBarry Smith int SNESSetApplicationContext(SNES snes,void *usrP) 2569b94acceSBarry Smith { 2579b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 2589b94acceSBarry Smith snes->user = usrP; 2599b94acceSBarry Smith return 0; 2609b94acceSBarry Smith } 2619b94acceSBarry Smith /*@C 2629b94acceSBarry Smith SNESGetApplicationContext - Gets the user-defined context for the 2639b94acceSBarry Smith nonlinear solvers. 2649b94acceSBarry Smith 2659b94acceSBarry Smith Input Parameter: 2669b94acceSBarry Smith . snes - SNES context 2679b94acceSBarry Smith 2689b94acceSBarry Smith Output Parameter: 2699b94acceSBarry Smith . usrP - user context 2709b94acceSBarry Smith 2719b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context 2729b94acceSBarry Smith 2739b94acceSBarry Smith .seealso: SNESSetApplicationContext() 2749b94acceSBarry Smith @*/ 2759b94acceSBarry Smith int SNESGetApplicationContext( SNES snes, void **usrP ) 2769b94acceSBarry Smith { 2779b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 2789b94acceSBarry Smith *usrP = snes->user; 2799b94acceSBarry Smith return 0; 2809b94acceSBarry Smith } 2819b94acceSBarry Smith /*@ 2829b94acceSBarry Smith SNESGetIterationNumber - Gets the current iteration number of the 2839b94acceSBarry Smith nonlinear solver. 2849b94acceSBarry Smith 2859b94acceSBarry Smith Input Parameter: 2869b94acceSBarry Smith . snes - SNES context 2879b94acceSBarry Smith 2889b94acceSBarry Smith Output Parameter: 2899b94acceSBarry Smith . iter - iteration number 2909b94acceSBarry Smith 2919b94acceSBarry Smith .keywords: SNES, nonlinear, get, iteration, number 2929b94acceSBarry Smith @*/ 2939b94acceSBarry Smith int SNESGetIterationNumber(SNES snes,int* iter) 2949b94acceSBarry Smith { 2959b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 2969b94acceSBarry Smith *iter = snes->iter; 2979b94acceSBarry Smith return 0; 2989b94acceSBarry Smith } 2999b94acceSBarry Smith /*@ 3009b94acceSBarry Smith SNESGetFunctionNorm - Gets the norm of the current function that was set 3019b94acceSBarry Smith with SNESSSetFunction(). 3029b94acceSBarry Smith 3039b94acceSBarry Smith Input Parameter: 3049b94acceSBarry Smith . snes - SNES context 3059b94acceSBarry Smith 3069b94acceSBarry Smith Output Parameter: 3079b94acceSBarry Smith . fnorm - 2-norm of function 3089b94acceSBarry Smith 3099b94acceSBarry Smith Note: 3109b94acceSBarry Smith SNESGetFunctionNorm() is valid for SNES_NONLINEAR_EQUATIONS methods only. 3119b94acceSBarry Smith 3129b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm 3139b94acceSBarry Smith @*/ 3149b94acceSBarry Smith int SNESGetFunctionNorm(SNES snes,Scalar *fnorm) 3159b94acceSBarry Smith { 3169b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 3179b94acceSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1, 31848d91487SBarry Smith "SNESGetFunctionNorm:For SNES_NONLINEAR_EQUATIONS only"); 3199b94acceSBarry Smith *fnorm = snes->norm; 3209b94acceSBarry Smith return 0; 3219b94acceSBarry Smith } 3229b94acceSBarry Smith /*@ 3239b94acceSBarry Smith SNESGetGradientNorm - Gets the norm of the current gradient that was set 3249b94acceSBarry Smith with SNESSSetGradient(). 3259b94acceSBarry Smith 3269b94acceSBarry Smith Input Parameter: 3279b94acceSBarry Smith . snes - SNES context 3289b94acceSBarry Smith 3299b94acceSBarry Smith Output Parameter: 3309b94acceSBarry Smith . fnorm - 2-norm of gradient 3319b94acceSBarry Smith 3329b94acceSBarry Smith Note: 3339b94acceSBarry Smith SNESGetGradientNorm() is valid for SNES_UNCONSTRAINED_MINIMIZATION 3349b94acceSBarry Smith methods only. 3359b94acceSBarry Smith 3369b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient, norm 3379b94acceSBarry Smith @*/ 3389b94acceSBarry Smith int SNESGetGradientNorm(SNES snes,Scalar *gnorm) 3399b94acceSBarry Smith { 3409b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 3419b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 34248d91487SBarry Smith "SNESGetGradientNorm:For SNES_UNCONSTRAINED_MINIMIZATION only"); 3439b94acceSBarry Smith *gnorm = snes->norm; 3449b94acceSBarry Smith return 0; 3459b94acceSBarry Smith } 3469b94acceSBarry Smith /*@ 3479b94acceSBarry Smith SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps 3489b94acceSBarry Smith attempted by the nonlinear solver. 3499b94acceSBarry Smith 3509b94acceSBarry Smith Input Parameter: 3519b94acceSBarry Smith . snes - SNES context 3529b94acceSBarry Smith 3539b94acceSBarry Smith Output Parameter: 3549b94acceSBarry Smith . nfails - number of unsuccessful steps attempted 3559b94acceSBarry Smith 3569b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 3579b94acceSBarry Smith @*/ 3589b94acceSBarry Smith int SNESGetNumberUnsuccessfulSteps(SNES snes,int* nfails) 3599b94acceSBarry Smith { 3609b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 3619b94acceSBarry Smith *nfails = snes->nfailures; 3629b94acceSBarry Smith return 0; 3639b94acceSBarry Smith } 3649b94acceSBarry Smith /*@C 3659b94acceSBarry Smith SNESGetSLES - Returns the SLES context for a SNES solver. 3669b94acceSBarry Smith 3679b94acceSBarry Smith Input Parameter: 3689b94acceSBarry Smith . snes - the SNES context 3699b94acceSBarry Smith 3709b94acceSBarry Smith Output Parameter: 3719b94acceSBarry Smith . sles - the SLES context 3729b94acceSBarry Smith 3739b94acceSBarry Smith Notes: 3749b94acceSBarry Smith The user can then directly manipulate the SLES context to set various 3759b94acceSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 3769b94acceSBarry Smith KSP and PC contexts as well. 3779b94acceSBarry Smith 3789b94acceSBarry Smith .keywords: SNES, nonlinear, get, SLES, context 3799b94acceSBarry Smith 3809b94acceSBarry Smith .seealso: SLESGetPC(), SLESGetKSP() 3819b94acceSBarry Smith @*/ 3829b94acceSBarry Smith int SNESGetSLES(SNES snes,SLES *sles) 3839b94acceSBarry Smith { 3849b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 3859b94acceSBarry Smith *sles = snes->sles; 3869b94acceSBarry Smith return 0; 3879b94acceSBarry Smith } 3889b94acceSBarry Smith /* -----------------------------------------------------------*/ 3899b94acceSBarry Smith /*@C 3909b94acceSBarry Smith SNESCreate - Creates a nonlinear solver context. 3919b94acceSBarry Smith 3929b94acceSBarry Smith Input Parameter: 3939b94acceSBarry Smith . comm - MPI communicator 3949b94acceSBarry Smith . type - type of method, one of 3959b94acceSBarry Smith $ SNES_NONLINEAR_EQUATIONS 3969b94acceSBarry Smith $ (for systems of nonlinear equations) 3979b94acceSBarry Smith $ SNES_UNCONSTRAINED_MINIMIZATION 3989b94acceSBarry Smith $ (for unconstrained minimization) 3999b94acceSBarry Smith 4009b94acceSBarry Smith Output Parameter: 4019b94acceSBarry Smith . outsnes - the new SNES context 4029b94acceSBarry Smith 4039b94acceSBarry Smith .keywords: SNES, nonlinear, create, context 4049b94acceSBarry Smith 4059b94acceSBarry Smith .seealso: SNESSetUp(), SNESSolve(), SNESDestroy() 4069b94acceSBarry Smith @*/ 4079b94acceSBarry Smith int SNESCreate(MPI_Comm comm,SNESType type,SNES *outsnes) 4089b94acceSBarry Smith { 4099b94acceSBarry Smith int ierr; 4109b94acceSBarry Smith SNES snes; 4119b94acceSBarry Smith SNES_KSP_EW_ConvCtx *kctx; 4129b94acceSBarry Smith *outsnes = 0; 4139b94acceSBarry Smith PETSCHEADERCREATE(snes,_SNES,SNES_COOKIE,SNES_UNKNOWN_METHOD,comm); 4149b94acceSBarry Smith PLogObjectCreate(snes); 4159b94acceSBarry Smith snes->max_its = 50; 4169b94acceSBarry Smith snes->max_funcs = 1000; 4179b94acceSBarry Smith snes->norm = 0.0; 4189b94acceSBarry Smith snes->rtol = 1.e-8; 4199b94acceSBarry Smith snes->atol = 1.e-10; 4209b94acceSBarry Smith snes->xtol = 1.e-8; 4219b94acceSBarry Smith snes->trunctol = 1.e-12; 4229b94acceSBarry Smith snes->nfuncs = 0; 4239b94acceSBarry Smith snes->nfailures = 0; 4249b94acceSBarry Smith snes->monitor = 0; 4259b94acceSBarry Smith snes->data = 0; 4269b94acceSBarry Smith snes->view = 0; 4279b94acceSBarry Smith snes->computeumfunction = 0; 4289b94acceSBarry Smith snes->umfunP = 0; 4299b94acceSBarry Smith snes->fc = 0; 4309b94acceSBarry Smith snes->deltatol = 1.e-12; 4319b94acceSBarry Smith snes->fmin = -1.e30; 4329b94acceSBarry Smith snes->method_class = type; 4339b94acceSBarry Smith snes->set_method_called = 0; 434*a703fe33SLois Curfman McInnes snes->setup_called = 0; 4359b94acceSBarry Smith snes->ksp_ewconv = 0; 4369b94acceSBarry Smith 4379b94acceSBarry Smith /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 4389b94acceSBarry Smith kctx = PETSCNEW(SNES_KSP_EW_ConvCtx); CHKPTRQ(kctx); 4399b94acceSBarry Smith snes->kspconvctx = (void*)kctx; 4409b94acceSBarry Smith kctx->version = 2; 4419b94acceSBarry Smith kctx->rtol_0 = .3; /* Eisenstat and Walker suggest rtol_0=.5, but 4429b94acceSBarry Smith this was too large for some test cases */ 4439b94acceSBarry Smith kctx->rtol_last = 0; 4449b94acceSBarry Smith kctx->rtol_max = .9; 4459b94acceSBarry Smith kctx->gamma = 1.0; 4469b94acceSBarry Smith kctx->alpha2 = .5*(1.0 + sqrt(5.0)); 4479b94acceSBarry Smith kctx->alpha = kctx->alpha2; 4489b94acceSBarry Smith kctx->threshold = .1; 4499b94acceSBarry Smith kctx->lresid_last = 0; 4509b94acceSBarry Smith kctx->norm_last = 0; 4519b94acceSBarry Smith 4529b94acceSBarry Smith ierr = SLESCreate(comm,&snes->sles); CHKERRQ(ierr); 4539b94acceSBarry Smith PLogObjectParent(snes,snes->sles) 4549b94acceSBarry Smith *outsnes = snes; 4559b94acceSBarry Smith return 0; 4569b94acceSBarry Smith } 4579b94acceSBarry Smith 4589b94acceSBarry Smith /* --------------------------------------------------------------- */ 4599b94acceSBarry Smith /*@C 4609b94acceSBarry Smith SNESSetFunction - Sets the function evaluation routine and function 4619b94acceSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 4629b94acceSBarry Smith equations. 4639b94acceSBarry Smith 4649b94acceSBarry Smith Input Parameters: 4659b94acceSBarry Smith . snes - the SNES context 4669b94acceSBarry Smith . func - function evaluation routine 4679b94acceSBarry Smith . resid_neg - indicator whether func evaluates f or -f. 468dbb450caSBarry Smith If resid_neg is NEGATIVE_FUNCTION_VALUE, then func evaluates -f; otherwise, 4699b94acceSBarry Smith func evaluates f. 4709b94acceSBarry Smith . ctx - optional user-defined function context 4719b94acceSBarry Smith . r - vector to store function value 4729b94acceSBarry Smith 4739b94acceSBarry Smith Calling sequence of func: 4749b94acceSBarry Smith . func (SNES, Vec x, Vec f, void *ctx); 4759b94acceSBarry Smith 4769b94acceSBarry Smith . x - input vector 4779b94acceSBarry Smith . f - function vector or its negative 4789b94acceSBarry Smith . ctx - optional user-defined context for private data for the 4799b94acceSBarry Smith function evaluation routine (may be null) 4809b94acceSBarry Smith 4819b94acceSBarry Smith Notes: 4829b94acceSBarry Smith The Newton-like methods typically solve linear systems of the form 4839b94acceSBarry Smith $ f'(x) x = -f(x), 4849b94acceSBarry Smith $ where f'(x) denotes the Jacobian matrix and f(x) is the function. 4859b94acceSBarry Smith By setting resid_neg = 1, the user can supply -f(x) directly. 4869b94acceSBarry Smith 4879b94acceSBarry Smith SNESSetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only. 4889b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 4899b94acceSBarry Smith SNESSetMinimizationFunction() and SNESSetGradient(); 4909b94acceSBarry Smith 4919b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 4929b94acceSBarry Smith 4939b94acceSBarry Smith .seealso: SNESGetFunction(), SNESSetJacobian(), SNESSetSolution() 4949b94acceSBarry Smith @*/ 4959b94acceSBarry Smith int SNESSetFunction( SNES snes, Vec r, int (*func)(SNES,Vec,Vec,void*), 496dbb450caSBarry Smith void *ctx,SNESFunctionSign rneg) 4979b94acceSBarry Smith { 4989b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 4999b94acceSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1, 50048d91487SBarry Smith "SNESSetFunction:For SNES_NONLINEAR_EQUATIONS only"); 5019b94acceSBarry Smith snes->computefunction = func; 502dbb450caSBarry Smith snes->rsign = (int) rneg; 5039b94acceSBarry Smith snes->vec_func = snes->vec_func_always = r; 5049b94acceSBarry Smith snes->funP = ctx; 5059b94acceSBarry Smith return 0; 5069b94acceSBarry Smith } 5079b94acceSBarry Smith 5089b94acceSBarry Smith /*@ 5099b94acceSBarry Smith SNESComputeFunction - Computes the function that has been set with 5109b94acceSBarry Smith SNESSetFunction(). 5119b94acceSBarry Smith 5129b94acceSBarry Smith Input Parameters: 5139b94acceSBarry Smith . snes - the SNES context 5149b94acceSBarry Smith . x - input vector 5159b94acceSBarry Smith 5169b94acceSBarry Smith Output Parameter: 5179b94acceSBarry Smith . y - function vector or its negative, as set by SNESSetFunction() 5189b94acceSBarry Smith 5199b94acceSBarry Smith Notes: 5209b94acceSBarry Smith SNESComputeFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only. 5219b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 5229b94acceSBarry Smith SNESComputeMinimizationFunction() and SNESComputeGradient(); 5239b94acceSBarry Smith 5249b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function 5259b94acceSBarry Smith 5269b94acceSBarry Smith .seealso: SNESSetFunction() 5279b94acceSBarry Smith @*/ 5289b94acceSBarry Smith int SNESComputeFunction(SNES snes,Vec x, Vec y) 5299b94acceSBarry Smith { 5309b94acceSBarry Smith int ierr; 5319b94acceSBarry Smith Scalar mone = -1.0; 5329b94acceSBarry Smith 5339b94acceSBarry Smith PLogEventBegin(SNES_FunctionEval,snes,x,y,0); 5349b94acceSBarry Smith ierr = (*snes->computefunction)(snes,x,y,snes->funP); CHKERRQ(ierr); 5359b94acceSBarry Smith if (!snes->rsign) { 5369b94acceSBarry Smith ierr = VecScale(&mone,y); CHKERRQ(ierr); 5379b94acceSBarry Smith } 5389b94acceSBarry Smith PLogEventEnd(SNES_FunctionEval,snes,x,y,0); 5399b94acceSBarry Smith return 0; 5409b94acceSBarry Smith } 5419b94acceSBarry Smith 5429b94acceSBarry Smith /*@C 5439b94acceSBarry Smith SNESSetMinimizationFunction - Sets the function evaluation routine for 5449b94acceSBarry Smith unconstrained minimization. 5459b94acceSBarry Smith 5469b94acceSBarry Smith Input Parameters: 5479b94acceSBarry Smith . snes - the SNES context 5489b94acceSBarry Smith . func - function evaluation routine 5499b94acceSBarry Smith . ctx - optional user-defined function context 5509b94acceSBarry Smith 5519b94acceSBarry Smith Calling sequence of func: 5529b94acceSBarry Smith . func (SNES snes,Vec x,double *f,void *ctx); 5539b94acceSBarry Smith 5549b94acceSBarry Smith . x - input vector 5559b94acceSBarry Smith . f - function 5569b94acceSBarry Smith . ctx - optional user-defined context for private data for the 5579b94acceSBarry Smith function evaluation routine (may be null) 5589b94acceSBarry Smith 5599b94acceSBarry Smith Notes: 5609b94acceSBarry Smith SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 5619b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 5629b94acceSBarry Smith SNESSetFunction(). 5639b94acceSBarry Smith 5649b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimization, function 5659b94acceSBarry Smith 5669b94acceSBarry Smith .seealso: SNESGetMinimizationFunction(), SNESSetHessian(), SNESSetGradient(), 5679b94acceSBarry Smith SNESSetSolution() 5689b94acceSBarry Smith @*/ 5699b94acceSBarry Smith int SNESSetMinimizationFunction(SNES snes,int (*func)(SNES,Vec,double*,void*), 5709b94acceSBarry Smith void *ctx) 5719b94acceSBarry Smith { 5729b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 5739b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 57448d91487SBarry Smith "SNESSetMinimizationFunction:Only for SNES_UNCONSTRAINED_MINIMIZATION"); 5759b94acceSBarry Smith snes->computeumfunction = func; 5769b94acceSBarry Smith snes->umfunP = ctx; 5779b94acceSBarry Smith return 0; 5789b94acceSBarry Smith } 5799b94acceSBarry Smith 5809b94acceSBarry Smith /*@ 5819b94acceSBarry Smith SNESComputeMinimizationFunction - Computes the function that has been 5829b94acceSBarry Smith set with SNESSetMinimizationFunction(). 5839b94acceSBarry Smith 5849b94acceSBarry Smith Input Parameters: 5859b94acceSBarry Smith . snes - the SNES context 5869b94acceSBarry Smith . x - input vector 5879b94acceSBarry Smith 5889b94acceSBarry Smith Output Parameter: 5899b94acceSBarry Smith . y - function value 5909b94acceSBarry Smith 5919b94acceSBarry Smith Notes: 5929b94acceSBarry Smith SNESComputeMinimizationFunction() is valid only for 5939b94acceSBarry Smith SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 5949b94acceSBarry Smith SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction(). 5959b94acceSBarry Smith @*/ 5969b94acceSBarry Smith int SNESComputeMinimizationFunction(SNES snes,Vec x,double *y) 5979b94acceSBarry Smith { 5989b94acceSBarry Smith int ierr; 5999b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 60048d91487SBarry Smith "SNESComputeMinimizationFunction:Only for SNES_UNCONSTRAINED_MINIMIZATION"); 6019b94acceSBarry Smith PLogEventBegin(SNES_MinimizationFunctionEval,snes,x,y,0); 6029b94acceSBarry Smith ierr = (*snes->computeumfunction)(snes,x,y,snes->umfunP); CHKERRQ(ierr); 6039b94acceSBarry Smith PLogEventEnd(SNES_MinimizationFunctionEval,snes,x,y,0); 6049b94acceSBarry Smith return 0; 6059b94acceSBarry Smith } 6069b94acceSBarry Smith 6079b94acceSBarry Smith /*@C 6089b94acceSBarry Smith SNESSetGradient - Sets the gradient evaluation routine and gradient 6099b94acceSBarry Smith vector for use by the SNES routines. 6109b94acceSBarry Smith 6119b94acceSBarry Smith Input Parameters: 6129b94acceSBarry Smith . snes - the SNES context 6139b94acceSBarry Smith . func - function evaluation routine 6149b94acceSBarry Smith . ctx - optional user-defined function context 6159b94acceSBarry Smith . r - vector to store gradient value 6169b94acceSBarry Smith 6179b94acceSBarry Smith Calling sequence of func: 6189b94acceSBarry Smith . func (SNES, Vec x, Vec g, void *ctx); 6199b94acceSBarry Smith 6209b94acceSBarry Smith . x - input vector 6219b94acceSBarry Smith . g - gradient vector 6229b94acceSBarry Smith . ctx - optional user-defined context for private data for the 6239b94acceSBarry Smith function evaluation routine (may be null) 6249b94acceSBarry Smith 6259b94acceSBarry Smith Notes: 6269b94acceSBarry Smith SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 6279b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 6289b94acceSBarry Smith SNESSetFunction(). 6299b94acceSBarry Smith 6309b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 6319b94acceSBarry Smith 6329b94acceSBarry Smith .seealso: SNESGetGradient(), SNESSetHessian(), SNESSetMinimizationFunction(), 6339b94acceSBarry Smith SNESSetSolution() 6349b94acceSBarry Smith @*/ 6359b94acceSBarry Smith int SNESSetGradient(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*), 6369b94acceSBarry Smith void *ctx) 6379b94acceSBarry Smith { 6389b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 6399b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 64048d91487SBarry Smith "SNESSetGradient:For SNES_UNCONSTRAINED_MINIMIZATION only"); 6419b94acceSBarry Smith snes->computefunction = func; 6429b94acceSBarry Smith snes->vec_func = snes->vec_func_always = r; 6439b94acceSBarry Smith snes->funP = ctx; 6449b94acceSBarry Smith return 0; 6459b94acceSBarry Smith } 6469b94acceSBarry Smith 6479b94acceSBarry Smith /*@ 6489b94acceSBarry Smith SNESComputeGradient - Computes the gradient that has been 6499b94acceSBarry Smith set with SNESSetGradient(). 6509b94acceSBarry Smith 6519b94acceSBarry Smith Input Parameters: 6529b94acceSBarry Smith . snes - the SNES context 6539b94acceSBarry Smith . x - input vector 6549b94acceSBarry Smith 6559b94acceSBarry Smith Output Parameter: 6569b94acceSBarry Smith . y - gradient vector 6579b94acceSBarry Smith 6589b94acceSBarry Smith Notes: 6599b94acceSBarry Smith SNESComputeGradient() is valid only for 6609b94acceSBarry Smith SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 6619b94acceSBarry Smith SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction(). 6629b94acceSBarry Smith @*/ 6639b94acceSBarry Smith int SNESComputeGradient(SNES snes,Vec x, Vec y) 6649b94acceSBarry Smith { 6659b94acceSBarry Smith int ierr; 6669b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 66748d91487SBarry Smith "SNESComputeGradient:For SNES_UNCONSTRAINED_MINIMIZATION only"); 6689b94acceSBarry Smith PLogEventBegin(SNES_GradientEval,snes,x,y,0); 6699b94acceSBarry Smith ierr = (*snes->computefunction)(snes,x,y,snes->funP); CHKERRQ(ierr); 6709b94acceSBarry Smith PLogEventEnd(SNES_GradientEval,snes,x,y,0); 6719b94acceSBarry Smith return 0; 6729b94acceSBarry Smith } 6739b94acceSBarry Smith 6749b94acceSBarry Smith int SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 6759b94acceSBarry Smith { 6769b94acceSBarry Smith int ierr; 6779b94acceSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1, 67848d91487SBarry Smith "SNESComputeJacobian: For SNES_NONLINEAR_EQUATIONS only"); 6799b94acceSBarry Smith if (!snes->computejacobian) return 0; 6809b94acceSBarry Smith PLogEventBegin(SNES_JacobianEval,snes,X,*A,*B); 6819b94acceSBarry Smith ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP); CHKERRQ(ierr); 6829b94acceSBarry Smith PLogEventEnd(SNES_JacobianEval,snes,X,*A,*B); 6839b94acceSBarry Smith return 0; 6849b94acceSBarry Smith } 6859b94acceSBarry Smith 6869b94acceSBarry Smith int SNESComputeHessian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 6879b94acceSBarry Smith { 6889b94acceSBarry Smith int ierr; 6899b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 69048d91487SBarry Smith "SNESComputeHessian:For SNES_UNCONSTRAINED_MINIMIZATION only"); 6919b94acceSBarry Smith if (!snes->computejacobian) return 0; 6929b94acceSBarry Smith PLogEventBegin(SNES_HessianEval,snes,X,*A,*B); 6939b94acceSBarry Smith ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP); CHKERRQ(ierr); 6949b94acceSBarry Smith PLogEventEnd(SNES_HessianEval,snes,X,*A,*B); 6959b94acceSBarry Smith return 0; 6969b94acceSBarry Smith } 6979b94acceSBarry Smith 6989b94acceSBarry Smith /*@C 6999b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 7009b94acceSBarry Smith location to store it. 7019b94acceSBarry Smith 7029b94acceSBarry Smith Input Parameters: 7039b94acceSBarry Smith . snes - the SNES context 7049b94acceSBarry Smith . A - Jacobian matrix 7059b94acceSBarry Smith . B - preconditioner matrix (usually same as the Jacobian) 7069b94acceSBarry Smith . func - Jacobian evaluation routine 7079b94acceSBarry Smith . ctx - optional user-defined context for private data for the 7089b94acceSBarry Smith Jacobian evaluation routine (may be null) 7099b94acceSBarry Smith 7109b94acceSBarry Smith Calling sequence of func: 7119b94acceSBarry Smith . func (SNES,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 7129b94acceSBarry Smith 7139b94acceSBarry Smith . x - input vector 7149b94acceSBarry Smith . A - Jacobian matrix 7159b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 7169b94acceSBarry Smith . flag - flag indicating information about matrix structure, 7179b94acceSBarry Smith same as flag in SLESSetOperators() 7189b94acceSBarry Smith . ctx - optional user-defined Jacobian context 7199b94acceSBarry Smith 7209b94acceSBarry Smith Notes: 7219b94acceSBarry Smith The function func() takes Mat * as the matrix arguments rather than Mat. 7229b94acceSBarry Smith This allows the Jacobian evaluation routine to replace A and/or B with a 7239b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 7249b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 7259b94acceSBarry Smith throughout the global iterations. 7269b94acceSBarry Smith 7279b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix 7289b94acceSBarry Smith 7299b94acceSBarry Smith .seealso: SNESSetFunction(), SNESSetSolution() 7309b94acceSBarry Smith @*/ 7319b94acceSBarry Smith int SNESSetJacobian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*, 7329b94acceSBarry Smith MatStructure*,void*),void *ctx) 7339b94acceSBarry Smith { 7349b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 7359b94acceSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1, 73648d91487SBarry Smith "SNESSetJacobian:For SNES_NONLINEAR_EQUATIONS only"); 7379b94acceSBarry Smith snes->computejacobian = func; 7389b94acceSBarry Smith snes->jacP = ctx; 7399b94acceSBarry Smith snes->jacobian = A; 7409b94acceSBarry Smith snes->jacobian_pre = B; 7419b94acceSBarry Smith return 0; 7429b94acceSBarry Smith } 7439b94acceSBarry Smith /*@C 7449b94acceSBarry Smith SNESSetHessian - Sets the function to compute Hessian as well as the 7459b94acceSBarry Smith location to store it. 7469b94acceSBarry Smith 7479b94acceSBarry Smith Input Parameters: 7489b94acceSBarry Smith . snes - the SNES context 7499b94acceSBarry Smith . A - Hessian matrix 7509b94acceSBarry Smith . B - preconditioner matrix (usually same as the Hessian) 7519b94acceSBarry Smith . func - Jacobian evaluation routine 7529b94acceSBarry Smith . ctx - optional user-defined context for private data for the 7539b94acceSBarry Smith Hessian evaluation routine (may be null) 7549b94acceSBarry Smith 7559b94acceSBarry Smith Calling sequence of func: 7569b94acceSBarry Smith . func (SNES,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 7579b94acceSBarry Smith 7589b94acceSBarry Smith . x - input vector 7599b94acceSBarry Smith . A - Hessian matrix 7609b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 7619b94acceSBarry Smith . flag - flag indicating information about matrix structure, 7629b94acceSBarry Smith same as flag in SLESSetOperators() 7639b94acceSBarry Smith . ctx - optional user-defined Hessian context 7649b94acceSBarry Smith 7659b94acceSBarry Smith Notes: 7669b94acceSBarry Smith The function func() takes Mat * as the matrix arguments rather than Mat. 7679b94acceSBarry Smith This allows the Hessian evaluation routine to replace A and/or B with a 7689b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 7699b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 7709b94acceSBarry Smith throughout the global iterations. 7719b94acceSBarry Smith 7729b94acceSBarry Smith .keywords: SNES, nonlinear, set, Hessian, matrix 7739b94acceSBarry Smith 7749b94acceSBarry Smith .seealso: SNESSetMinimizationFunction(), SNESSetSolution(), SNESSetGradient() 7759b94acceSBarry Smith @*/ 7769b94acceSBarry Smith int SNESSetHessian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*, 7779b94acceSBarry Smith MatStructure*,void*),void *ctx) 7789b94acceSBarry Smith { 7799b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 7809b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 78148d91487SBarry Smith "SNESSetHessian:For SNES_UNCONSTRAINED_MINIMIZATION only"); 7829b94acceSBarry Smith snes->computejacobian = func; 7839b94acceSBarry Smith snes->jacP = ctx; 7849b94acceSBarry Smith snes->jacobian = A; 7859b94acceSBarry Smith snes->jacobian_pre = B; 7869b94acceSBarry Smith return 0; 7879b94acceSBarry Smith } 7889b94acceSBarry Smith 7899b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 7909b94acceSBarry Smith 7919b94acceSBarry Smith /*@ 7929b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 7939b94acceSBarry Smith of a nonlinear solver. Call SNESSetUp() after calling SNESCreate() 7949b94acceSBarry Smith and optional routines of the form SNESSetXXX(), but before calling 7959b94acceSBarry Smith SNESSolve(). 7969b94acceSBarry Smith 7979b94acceSBarry Smith Input Parameter: 7989b94acceSBarry Smith . snes - the SNES context 7999b94acceSBarry Smith 8009b94acceSBarry Smith .keywords: SNES, nonlinear, setup 8019b94acceSBarry Smith 8029b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 8039b94acceSBarry Smith @*/ 8049b94acceSBarry Smith int SNESSetUp(SNES snes) 8059b94acceSBarry Smith { 8069b94acceSBarry Smith int ierr; 8079b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 8089b94acceSBarry Smith if (!snes->vec_sol) 80948d91487SBarry Smith SETERRQ(1,"SNESSetUp:Must call SNESSetSolution() first"); 8109b94acceSBarry Smith 8119b94acceSBarry Smith if ((snes->method_class == SNES_NONLINEAR_EQUATIONS)) { 8129b94acceSBarry Smith if (!snes->set_method_called) 8139b94acceSBarry Smith {ierr = SNESSetMethod(snes,SNES_EQ_NLS); CHKERRQ(ierr);} 8149b94acceSBarry Smith if (!snes->vec_func) SETERRQ(1, 81548d91487SBarry Smith "SNESSetUp:Must call SNESSetFunction() first"); 8169b94acceSBarry Smith if (!snes->computefunction) SETERRQ(1, 81748d91487SBarry Smith "SNESSetUp:Must call SNESSetFunction() first"); 8189b94acceSBarry Smith if (!snes->jacobian) SETERRQ(1, 81948d91487SBarry Smith "SNESSetUp:Must call SNESSetJacobian() first"); 820*a703fe33SLois Curfman McInnes 821*a703fe33SLois Curfman McInnes /* Set the KSP stopping criterion to use the Eisenstat-Walker method */ 822*a703fe33SLois Curfman McInnes if (snes->ksp_ewconv && snes->type != SNES_EQ_NTR) { 823*a703fe33SLois Curfman McInnes SLES sles; KSP ksp; 824*a703fe33SLois Curfman McInnes ierr = SNESGetSLES(snes,&sles); CHKERRQ(ierr); 825*a703fe33SLois Curfman McInnes ierr = SLESGetKSP(sles,&ksp); CHKERRQ(ierr); 826*a703fe33SLois Curfman McInnes ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private, 827*a703fe33SLois Curfman McInnes (void *)snes); CHKERRQ(ierr); 828*a703fe33SLois Curfman McInnes } 8299b94acceSBarry Smith } else if ((snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION)) { 8309b94acceSBarry Smith if (!snes->set_method_called) 8319b94acceSBarry Smith {ierr = SNESSetMethod(snes,SNES_UM_NTR); CHKERRQ(ierr);} 8329b94acceSBarry Smith if (!snes->vec_func) SETERRQ(1, 83348d91487SBarry Smith "SNESSetUp:Must call SNESSetGradient() first"); 8349b94acceSBarry Smith if (!snes->computefunction) SETERRQ(1, 83548d91487SBarry Smith "SNESSetUp:Must call SNESSetGradient() first"); 8369b94acceSBarry Smith if (!snes->computeumfunction) SETERRQ(1, 83748d91487SBarry Smith "SNESSetUp:Must call SNESSetMinimizationFunction() first"); 8389b94acceSBarry Smith if (!snes->jacobian) SETERRQ(1, 83948d91487SBarry Smith "SNESSetUp:Must call SNESSetHessian() first"); 8409b94acceSBarry Smith } else SETERRQ(1,"SNESSetUp:Unknown method class"); 841*a703fe33SLois Curfman McInnes if (snes->setup) {ierr = (*snes->setup)(snes); CHKERRQ(ierr);} 842*a703fe33SLois Curfman McInnes snes->setup_called = 1; 843*a703fe33SLois Curfman McInnes return 0; 8449b94acceSBarry Smith } 8459b94acceSBarry Smith 8469b94acceSBarry Smith /*@C 8479b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 8489b94acceSBarry Smith with SNESCreate(). 8499b94acceSBarry Smith 8509b94acceSBarry Smith Input Parameter: 8519b94acceSBarry Smith . snes - the SNES context 8529b94acceSBarry Smith 8539b94acceSBarry Smith .keywords: SNES, nonlinear, destroy 8549b94acceSBarry Smith 8559b94acceSBarry Smith .seealso: SNESCreate(), SNESSetUp(), SNESSolve() 8569b94acceSBarry Smith @*/ 8579b94acceSBarry Smith int SNESDestroy(SNES snes) 8589b94acceSBarry Smith { 8599b94acceSBarry Smith int ierr; 8609b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 8619b94acceSBarry Smith ierr = (*(snes)->destroy)((PetscObject)snes); CHKERRQ(ierr); 8629b94acceSBarry Smith if (snes->kspconvctx) PETSCFREE(snes->kspconvctx); 8639b94acceSBarry Smith if (snes->mfshell) MatDestroy(snes->mfshell); 8649b94acceSBarry Smith ierr = SLESDestroy(snes->sles); CHKERRQ(ierr); 8659b94acceSBarry Smith PLogObjectDestroy((PetscObject)snes); 8669b94acceSBarry Smith PETSCHEADERDESTROY((PetscObject)snes); 8679b94acceSBarry Smith return 0; 8689b94acceSBarry Smith } 8699b94acceSBarry Smith 8709b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 8719b94acceSBarry Smith 8729b94acceSBarry Smith /*@ 8739b94acceSBarry Smith SNESSetMaxIterations - Sets the maximum number of global iterations to use. 8749b94acceSBarry Smith 8759b94acceSBarry Smith Input Parameters: 8769b94acceSBarry Smith . snes - the SNES context 8779b94acceSBarry Smith . maxits - maximum number of iterations to use 8789b94acceSBarry Smith 8799b94acceSBarry Smith Options Database Key: 8809b94acceSBarry Smith $ -snes_max_it maxits 8819b94acceSBarry Smith 8829b94acceSBarry Smith Note: 8839b94acceSBarry Smith The default maximum number of iterations is 50. 8849b94acceSBarry Smith 8859b94acceSBarry Smith .keywords: SNES, nonlinear, set, maximum, iterations 8869b94acceSBarry Smith 8879b94acceSBarry Smith .seealso: SNESSetMaxFunctionEvaluations() 8889b94acceSBarry Smith @*/ 8899b94acceSBarry Smith int SNESSetMaxIterations(SNES snes,int maxits) 8909b94acceSBarry Smith { 8919b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 8929b94acceSBarry Smith snes->max_its = maxits; 8939b94acceSBarry Smith return 0; 8949b94acceSBarry Smith } 8959b94acceSBarry Smith 8969b94acceSBarry Smith /*@ 8979b94acceSBarry Smith SNESSetMaxFunctionEvaluations - Sets the maximum number of function 8989b94acceSBarry Smith evaluations to use. 8999b94acceSBarry Smith 9009b94acceSBarry Smith Input Parameters: 9019b94acceSBarry Smith . snes - the SNES context 9029b94acceSBarry Smith . maxf - maximum number of function evaluations 9039b94acceSBarry Smith 9049b94acceSBarry Smith Options Database Key: 9059b94acceSBarry Smith $ -snes_max_funcs maxf 9069b94acceSBarry Smith 9079b94acceSBarry Smith Note: 9089b94acceSBarry Smith The default maximum number of function evaluations is 1000. 9099b94acceSBarry Smith 9109b94acceSBarry Smith .keywords: SNES, nonlinear, set, maximum, function, evaluations 9119b94acceSBarry Smith 9129b94acceSBarry Smith .seealso: SNESSetMaxIterations() 9139b94acceSBarry Smith @*/ 9149b94acceSBarry Smith int SNESSetMaxFunctionEvaluations(SNES snes,int maxf) 9159b94acceSBarry Smith { 9169b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 9179b94acceSBarry Smith snes->max_funcs = maxf; 9189b94acceSBarry Smith return 0; 9199b94acceSBarry Smith } 9209b94acceSBarry Smith 9219b94acceSBarry Smith /*@ 9229b94acceSBarry Smith SNESSetRelativeTolerance - Sets the relative convergence tolerance. 9239b94acceSBarry Smith 9249b94acceSBarry Smith Input Parameters: 9259b94acceSBarry Smith . snes - the SNES context 9269b94acceSBarry Smith . rtol - tolerance 9279b94acceSBarry Smith 9289b94acceSBarry Smith Options Database Key: 9299b94acceSBarry Smith $ -snes_rtol tol 9309b94acceSBarry Smith 9319b94acceSBarry Smith .keywords: SNES, nonlinear, set, relative, convergence, tolerance 9329b94acceSBarry Smith 9339b94acceSBarry Smith .seealso: SNESSetAbsoluteTolerance(), SNESSetSolutionTolerance(), 9349b94acceSBarry Smith SNESSetTruncationTolerance() 9359b94acceSBarry Smith @*/ 9369b94acceSBarry Smith int SNESSetRelativeTolerance(SNES snes,double rtol) 9379b94acceSBarry Smith { 9389b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 9399b94acceSBarry Smith snes->rtol = rtol; 9409b94acceSBarry Smith return 0; 9419b94acceSBarry Smith } 9429b94acceSBarry Smith 9439b94acceSBarry Smith /*@ 9449b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 9459b94acceSBarry Smith 9469b94acceSBarry Smith Input Parameters: 9479b94acceSBarry Smith . snes - the SNES context 9489b94acceSBarry Smith . tol - tolerance 9499b94acceSBarry Smith 9509b94acceSBarry Smith Options Database Key: 9519b94acceSBarry Smith $ -snes_trtol tol 9529b94acceSBarry Smith 9539b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance 9549b94acceSBarry Smith 9559b94acceSBarry Smith .seealso: SNESSetAbsoluteTolerance(), SNESSetSolutionTolerance(), 9569b94acceSBarry Smith SNESSetTruncationTolerance() 9579b94acceSBarry Smith @*/ 9589b94acceSBarry Smith int SNESSetTrustRegionTolerance(SNES snes,double tol) 9599b94acceSBarry Smith { 9609b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 9619b94acceSBarry Smith snes->deltatol = tol; 9629b94acceSBarry Smith return 0; 9639b94acceSBarry Smith } 9649b94acceSBarry Smith 9659b94acceSBarry Smith /*@ 9669b94acceSBarry Smith SNESSetAbsoluteTolerance - Sets the absolute convergence tolerance. 9679b94acceSBarry Smith 9689b94acceSBarry Smith Input Parameters: 9699b94acceSBarry Smith . snes - the SNES context 9709b94acceSBarry Smith . atol - tolerance 9719b94acceSBarry Smith 9729b94acceSBarry Smith Options Database Key: 9739b94acceSBarry Smith $ -snes_atol tol 9749b94acceSBarry Smith 9759b94acceSBarry Smith .keywords: SNES, nonlinear, set, absolute, convergence, tolerance 9769b94acceSBarry Smith 9779b94acceSBarry Smith .seealso: SNESSetRelativeTolerance(), SNESSetSolutionTolerance(), 9789b94acceSBarry Smith SNESSetTruncationTolerance() 9799b94acceSBarry Smith @*/ 9809b94acceSBarry Smith int SNESSetAbsoluteTolerance(SNES snes,double atol) 9819b94acceSBarry Smith { 9829b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 9839b94acceSBarry Smith snes->atol = atol; 9849b94acceSBarry Smith return 0; 9859b94acceSBarry Smith } 9869b94acceSBarry Smith 9879b94acceSBarry Smith /*@ 9889b94acceSBarry Smith SNESSetTruncationTolerance - Sets the tolerance that may be used by the 9899b94acceSBarry Smith step routines to control the accuracy of the step computation. 9909b94acceSBarry Smith 9919b94acceSBarry Smith Input Parameters: 9929b94acceSBarry Smith . snes - the SNES context 9939b94acceSBarry Smith . tol - tolerance 9949b94acceSBarry Smith 9959b94acceSBarry Smith Options Database Key: 9969b94acceSBarry Smith $ -snes_ttol tol 9979b94acceSBarry Smith 9989b94acceSBarry Smith Notes: 9999b94acceSBarry Smith If the step computation involves an application of the inverse 10009b94acceSBarry Smith Jacobian (or Hessian), this parameter may be used to control the 1001*a703fe33SLois Curfman McInnes accuracy of that application. 10029b94acceSBarry Smith 10039b94acceSBarry Smith .keywords: SNES, nonlinear, set, truncation, tolerance 10049b94acceSBarry Smith 10059b94acceSBarry Smith .seealso: SNESSetRelativeTolerance(), SNESSetSolutionTolerance(), 10069b94acceSBarry Smith SNESSetAbsoluteTolerance() 10079b94acceSBarry Smith @*/ 10089b94acceSBarry Smith int SNESSetTruncationTolerance(SNES snes,double tol) 10099b94acceSBarry Smith { 10109b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 10119b94acceSBarry Smith snes->trunctol = tol; 10129b94acceSBarry Smith return 0; 10139b94acceSBarry Smith } 10149b94acceSBarry Smith 10159b94acceSBarry Smith /*@ 10169b94acceSBarry Smith SNESSetSolutionTolerance - Sets the convergence tolerance in terms of 10179b94acceSBarry Smith the norm of the change in the solution between steps. 10189b94acceSBarry Smith 10199b94acceSBarry Smith Input Parameters: 10209b94acceSBarry Smith . snes - the SNES context 10219b94acceSBarry Smith . tol - tolerance 10229b94acceSBarry Smith 10239b94acceSBarry Smith Options Database Key: 10249b94acceSBarry Smith $ -snes_stol tol 10259b94acceSBarry Smith 10269b94acceSBarry Smith .keywords: SNES, nonlinear, set, solution, tolerance 10279b94acceSBarry Smith 10289b94acceSBarry Smith .seealso: SNESSetTruncationTolerance(), SNESSetRelativeTolerance(), 10299b94acceSBarry Smith SNESSetAbsoluteTolerance() 10309b94acceSBarry Smith @*/ 10319b94acceSBarry Smith int SNESSetSolutionTolerance( SNES snes, double tol ) 10329b94acceSBarry Smith { 10339b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 10349b94acceSBarry Smith snes->xtol = tol; 10359b94acceSBarry Smith return 0; 10369b94acceSBarry Smith } 10379b94acceSBarry Smith 10389b94acceSBarry Smith /*@ 10399b94acceSBarry Smith SNESSetMinFunctionTolerance - Sets the minimum allowable function tolerance 10409b94acceSBarry Smith for unconstrained minimization solvers. 10419b94acceSBarry Smith 10429b94acceSBarry Smith Input Parameters: 10439b94acceSBarry Smith . snes - the SNES context 10449b94acceSBarry Smith . ftol - minimum function tolerance 10459b94acceSBarry Smith 10469b94acceSBarry Smith Options Database Key: 10479b94acceSBarry Smith $ -snes_fmin ftol 10489b94acceSBarry Smith 10499b94acceSBarry Smith Note: 10509b94acceSBarry Smith SNESSetMinFunctionTolerance() is valid for SNES_UNCONSTRAINED_MINIMIZATION 10519b94acceSBarry Smith methods only. 10529b94acceSBarry Smith 10539b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimum, convergence, function, tolerance 10549b94acceSBarry Smith 10559b94acceSBarry Smith .seealso: SNESSetRelativeTolerance(), SNESSetSolutionTolerance(), 10569b94acceSBarry Smith SNESSetTruncationTolerance() 10579b94acceSBarry Smith @*/ 10589b94acceSBarry Smith int SNESSetMinFunctionTolerance(SNES snes,double ftol) 10599b94acceSBarry Smith { 10609b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 10619b94acceSBarry Smith snes->fmin = ftol; 10629b94acceSBarry Smith return 0; 10639b94acceSBarry Smith } 10649b94acceSBarry Smith 10659b94acceSBarry Smith 10669b94acceSBarry Smith 10679b94acceSBarry Smith /* ---------- Routines to set various aspects of nonlinear solver --------- */ 10689b94acceSBarry Smith 10699b94acceSBarry Smith /*@C 10709b94acceSBarry Smith SNESSetSolution - Sets the initial guess routine and solution vector 10719b94acceSBarry Smith for use by the SNES routines. 10729b94acceSBarry Smith 10739b94acceSBarry Smith Input Parameters: 10749b94acceSBarry Smith . snes - the SNES context 10759b94acceSBarry Smith . x - the solution vector 10769b94acceSBarry Smith . func - optional routine to compute an initial guess (may be null) 10779b94acceSBarry Smith . ctx - optional user-defined context for private data for the 10789b94acceSBarry Smith initial guess routine (may be null) 10799b94acceSBarry Smith 10809b94acceSBarry Smith Calling sequence of func: 10819b94acceSBarry Smith int guess(Vec x, void *ctx) 10829b94acceSBarry Smith 10839b94acceSBarry Smith . x - input vector 10849b94acceSBarry Smith . ctx - optional user-defined initial guess context 10859b94acceSBarry Smith 10869b94acceSBarry Smith Note: 10879b94acceSBarry Smith If no initial guess routine is indicated, an initial guess of zero 10889b94acceSBarry Smith will be used. 10899b94acceSBarry Smith 10909b94acceSBarry Smith .keywords: SNES, nonlinear, set, solution, initial guess 10919b94acceSBarry Smith 10929b94acceSBarry Smith .seealso: SNESGetSolution(), SNESSetJacobian(), SNESSetFunction() 10939b94acceSBarry Smith @*/ 10949b94acceSBarry Smith int SNESSetSolution(SNES snes,Vec x,int (*func)(SNES,Vec,void*),void *ctx) 10959b94acceSBarry Smith { 10969b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 10979b94acceSBarry Smith snes->vec_sol = snes->vec_sol_always = x; 10989b94acceSBarry Smith snes->computeinitialguess = func; 10999b94acceSBarry Smith snes->gusP = ctx; 11009b94acceSBarry Smith return 0; 11019b94acceSBarry Smith } 11029b94acceSBarry Smith 11039b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 11049b94acceSBarry Smith 11059b94acceSBarry Smith /*@C 11069b94acceSBarry Smith SNESSetMonitor - Sets the function that is to be used at every 11079b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 11089b94acceSBarry Smith progress. 11099b94acceSBarry Smith 11109b94acceSBarry Smith Input Parameters: 11119b94acceSBarry Smith . snes - the SNES context 11129b94acceSBarry Smith . func - monitoring routine 11139b94acceSBarry Smith . mctx - optional user-defined context for private data for the 11149b94acceSBarry Smith monitor routine (may be null) 11159b94acceSBarry Smith 11169b94acceSBarry Smith Calling sequence of func: 11179b94acceSBarry Smith int func((SNES snes,int its, Vec x,Vec f, 11189b94acceSBarry Smith double norm,void *mctx) 11199b94acceSBarry Smith 11209b94acceSBarry Smith $ snes - the SNES context 11219b94acceSBarry Smith $ its - iteration number 11229b94acceSBarry Smith $ mctx - optional monitoring context 11239b94acceSBarry Smith $ 11249b94acceSBarry Smith $ SNES_NONLINEAR_EQUATIONS methods: 11259b94acceSBarry Smith $ norm - 2-norm function value (may be estimated) 11269b94acceSBarry Smith $ 11279b94acceSBarry Smith $ SNES_UNCONSTRAINED_MINIMIZATION methods: 11289b94acceSBarry Smith $ norm - 2-norm gradient value (may be estimated) 11299b94acceSBarry Smith 11309b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor 11319b94acceSBarry Smith 11329b94acceSBarry Smith .seealso: SNESDefaultMonitor() 11339b94acceSBarry Smith @*/ 11349b94acceSBarry Smith int SNESSetMonitor( SNES snes, int (*func)(SNES,int,double,void*), 11359b94acceSBarry Smith void *mctx ) 11369b94acceSBarry Smith { 11379b94acceSBarry Smith snes->monitor = func; 11389b94acceSBarry Smith snes->monP = (void*)mctx; 11399b94acceSBarry Smith return 0; 11409b94acceSBarry Smith } 11419b94acceSBarry Smith 11429b94acceSBarry Smith /*@C 11439b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 11449b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 11459b94acceSBarry Smith 11469b94acceSBarry Smith Input Parameters: 11479b94acceSBarry Smith . snes - the SNES context 11489b94acceSBarry Smith . func - routine to test for convergence 11499b94acceSBarry Smith . cctx - optional context for private data for the convergence routine 11509b94acceSBarry Smith (may be null) 11519b94acceSBarry Smith 11529b94acceSBarry Smith Calling sequence of func: 11539b94acceSBarry Smith int func (SNES snes,double xnorm,double gnorm, 11549b94acceSBarry Smith double f,void *cctx) 11559b94acceSBarry Smith 11569b94acceSBarry Smith $ snes - the SNES context 11579b94acceSBarry Smith $ cctx - optional convergence context 11589b94acceSBarry Smith $ xnorm - 2-norm of current iterate 11599b94acceSBarry Smith $ 11609b94acceSBarry Smith $ SNES_NONLINEAR_EQUATIONS methods: 11619b94acceSBarry Smith $ gnorm - 2-norm of current step 11629b94acceSBarry Smith $ f - 2-norm of function 11639b94acceSBarry Smith $ 11649b94acceSBarry Smith $ SNES_UNCONSTRAINED_MINIMIZATION methods: 11659b94acceSBarry Smith $ gnorm - 2-norm of current gradient 11669b94acceSBarry Smith $ f - function value 11679b94acceSBarry Smith 11689b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test 11699b94acceSBarry Smith 11709b94acceSBarry Smith .seealso: SNESDefaultConverged() 11719b94acceSBarry Smith @*/ 11729b94acceSBarry Smith int SNESSetConvergenceTest(SNES snes, 11739b94acceSBarry Smith int (*func)(SNES,double,double,double,void*),void *cctx) 11749b94acceSBarry Smith { 11759b94acceSBarry Smith (snes)->converged = func; 11769b94acceSBarry Smith (snes)->cnvP = cctx; 11779b94acceSBarry Smith return 0; 11789b94acceSBarry Smith } 11799b94acceSBarry Smith 11809b94acceSBarry Smith /* 11819b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 11829b94acceSBarry Smith positive parameter delta. 11839b94acceSBarry Smith 11849b94acceSBarry Smith Input Parameters: 11859b94acceSBarry Smith . snes - the SNES context 11869b94acceSBarry Smith . y - approximate solution of linear system 11879b94acceSBarry Smith . fnorm - 2-norm of current function 11889b94acceSBarry Smith . delta - trust region size 11899b94acceSBarry Smith 11909b94acceSBarry Smith Output Parameters: 11919b94acceSBarry Smith . gpnorm - predicted function norm at the new point, assuming local 11929b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 11939b94acceSBarry Smith region, and exceeds zero otherwise. 11949b94acceSBarry Smith . ynorm - 2-norm of the step 11959b94acceSBarry Smith 11969b94acceSBarry Smith Note: 1197ddd12767SBarry Smith For non-trust region methods such as SNES_EQ_NLS, the parameter delta 11989b94acceSBarry Smith is set to be the maximum allowable step size. 11999b94acceSBarry Smith 12009b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step 12019b94acceSBarry Smith */ 12029b94acceSBarry Smith int SNESScaleStep_Private(SNES snes,Vec y,double *fnorm,double *delta, 12039b94acceSBarry Smith double *gpnorm,double *ynorm) 12049b94acceSBarry Smith { 12059b94acceSBarry Smith double norm; 12069b94acceSBarry Smith Scalar cnorm; 12079b94acceSBarry Smith VecNorm(y, &norm ); 12089b94acceSBarry Smith if (norm > *delta) { 12099b94acceSBarry Smith norm = *delta/norm; 12109b94acceSBarry Smith *gpnorm = (1.0 - norm)*(*fnorm); 12119b94acceSBarry Smith cnorm = norm; 12129b94acceSBarry Smith VecScale( &cnorm, y ); 12139b94acceSBarry Smith *ynorm = *delta; 12149b94acceSBarry Smith } else { 12159b94acceSBarry Smith *gpnorm = 0.0; 12169b94acceSBarry Smith *ynorm = norm; 12179b94acceSBarry Smith } 12189b94acceSBarry Smith return 0; 12199b94acceSBarry Smith } 12209b94acceSBarry Smith 12219b94acceSBarry Smith /*@ 12229b94acceSBarry Smith SNESSolve - Solves a nonlinear system. Call SNESSolve after calling 12239b94acceSBarry Smith SNESCreate(), optional routines of the form SNESSetXXX(), and SNESSetUp(). 12249b94acceSBarry Smith 12259b94acceSBarry Smith Input Parameter: 12269b94acceSBarry Smith . snes - the SNES context 12279b94acceSBarry Smith 12289b94acceSBarry Smith Output Parameter: 12299b94acceSBarry Smith its - number of iterations until termination 12309b94acceSBarry Smith 12319b94acceSBarry Smith .keywords: SNES, nonlinear, solve 12329b94acceSBarry Smith 12339b94acceSBarry Smith .seealso: SNESCreate(), SNESSetUp(), SNESDestroy() 12349b94acceSBarry Smith @*/ 12359b94acceSBarry Smith int SNESSolve(SNES snes,int *its) 12369b94acceSBarry Smith { 12379b94acceSBarry Smith int ierr; 12389b94acceSBarry Smith PLogEventBegin(SNES_Solve,snes,0,0,0); 12399b94acceSBarry Smith ierr = (*(snes)->solve)(snes,its); CHKERRQ(ierr); 12409b94acceSBarry Smith PLogEventEnd(SNES_Solve,snes,0,0,0); 12419b94acceSBarry Smith if (OptionsHasName(0,"-snes_view")) { 12429b94acceSBarry Smith SNESView(snes,STDOUT_VIEWER_WORLD); CHKERRQ(ierr); 12439b94acceSBarry Smith } 12449b94acceSBarry Smith return 0; 12459b94acceSBarry Smith } 12469b94acceSBarry Smith 12479b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 12489b94acceSBarry Smith 12499b94acceSBarry Smith /* 12509b94acceSBarry Smith SNESComputeInitialGuess - Manages computation of initial approximation. 12519b94acceSBarry Smith */ 12529b94acceSBarry Smith int SNESComputeInitialGuess( SNES snes,Vec x ) 12539b94acceSBarry Smith { 12549b94acceSBarry Smith int ierr; 12559b94acceSBarry Smith Scalar zero = 0.0; 12569b94acceSBarry Smith if (snes->computeinitialguess) { 12579b94acceSBarry Smith ierr = (*snes->computeinitialguess)(snes, x, snes->gusP); CHKERRQ(ierr); 12589b94acceSBarry Smith } 12599b94acceSBarry Smith else { 12609b94acceSBarry Smith ierr = VecSet(&zero,x); CHKERRQ(ierr); 12619b94acceSBarry Smith } 12629b94acceSBarry Smith return 0; 12639b94acceSBarry Smith } 12649b94acceSBarry Smith 12659b94acceSBarry Smith /* ------------------------------------------------------------------ */ 12669b94acceSBarry Smith 12679b94acceSBarry Smith NRList *__NLList; 12689b94acceSBarry Smith 12699b94acceSBarry Smith /*@ 12709b94acceSBarry Smith SNESSetMethod - Sets the method for the nonlinear solver. 12719b94acceSBarry Smith 12729b94acceSBarry Smith Input Parameters: 12739b94acceSBarry Smith . snes - the SNES context 12749b94acceSBarry Smith . method - a known method 12759b94acceSBarry Smith 12769b94acceSBarry Smith Notes: 12779b94acceSBarry Smith See "petsc/include/snes.h" for available methods (for instance) 12789b94acceSBarry Smith $ Systems of nonlinear equations: 1279ddd12767SBarry Smith $ SNES_EQ_NLS - Newton's method with line search 1280ddd12767SBarry Smith $ SNES_EQ_NTR - Newton's method with trust region 12819b94acceSBarry Smith $ Unconstrained minimization: 12829b94acceSBarry Smith $ SNES_UM_NTR - Newton's method with trust region 1283*a703fe33SLois Curfman McInnes $ SNES_UM_NLS - Newton's method with line search 12849b94acceSBarry Smith 12859b94acceSBarry Smith Options Database Command: 12869b94acceSBarry Smith $ -snes_method <method> 12879b94acceSBarry Smith $ Use -help for a list of available methods 12889b94acceSBarry Smith $ (for instance, ls or tr) 1289*a703fe33SLois Curfman McInnes 1290*a703fe33SLois Curfman McInnes .keysords: SNES, set, method 12919b94acceSBarry Smith @*/ 12929b94acceSBarry Smith int SNESSetMethod(SNES snes,SNESMethod method) 12939b94acceSBarry Smith { 12949b94acceSBarry Smith int (*r)(SNES); 12959b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 12969b94acceSBarry Smith /* Get the function pointers for the iterative method requested */ 12979b94acceSBarry Smith if (!__NLList) {SNESRegisterAll();} 129848d91487SBarry Smith if (!__NLList) {SETERRQ(1,"SNESSetMethod:Could not get methods");} 12999b94acceSBarry Smith r = (int (*)(SNES))NRFindRoutine( __NLList, (int)method, (char *)0 ); 130048d91487SBarry Smith if (!r) {SETERRQ(1,"SNESSetMethod:Unknown method");} 13019b94acceSBarry Smith if (snes->data) PETSCFREE(snes->data); 13029b94acceSBarry Smith snes->set_method_called = 1; 13039b94acceSBarry Smith return (*r)(snes); 13049b94acceSBarry Smith } 13059b94acceSBarry Smith 13069b94acceSBarry Smith /* --------------------------------------------------------------------- */ 13079b94acceSBarry Smith /*@C 13089b94acceSBarry Smith SNESRegister - Adds the method to the nonlinear solver package, given 13099b94acceSBarry Smith a function pointer and a nonlinear solver name of the type SNESMethod. 13109b94acceSBarry Smith 13119b94acceSBarry Smith Input Parameters: 1312ddd12767SBarry Smith . name - for instance SNES_EQ_NLS, SNES_EQ_NTR, ... 13139b94acceSBarry Smith . sname - corfunPonding string for name 13149b94acceSBarry Smith . create - routine to create method context 13159b94acceSBarry Smith 13169b94acceSBarry Smith .keywords: SNES, nonlinear, register 13179b94acceSBarry Smith 13189b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterDestroy() 13199b94acceSBarry Smith @*/ 13209b94acceSBarry Smith int SNESRegister(int name, char *sname, int (*create)(SNES)) 13219b94acceSBarry Smith { 13229b94acceSBarry Smith int ierr; 13239b94acceSBarry Smith if (!__NLList) {ierr = NRCreate(&__NLList); CHKERRQ(ierr);} 13249b94acceSBarry Smith NRRegister( __NLList, name, sname, (int (*)(void*))create ); 13259b94acceSBarry Smith return 0; 13269b94acceSBarry Smith } 13279b94acceSBarry Smith /* --------------------------------------------------------------------- */ 13289b94acceSBarry Smith /*@C 13299b94acceSBarry Smith SNESRegisterDestroy - Frees the list of nonlinear solvers that were 13309b94acceSBarry Smith registered by SNESRegister(). 13319b94acceSBarry Smith 13329b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy 13339b94acceSBarry Smith 13349b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll() 13359b94acceSBarry Smith @*/ 13369b94acceSBarry Smith int SNESRegisterDestroy() 13379b94acceSBarry Smith { 13389b94acceSBarry Smith if (__NLList) { 13399b94acceSBarry Smith NRDestroy( __NLList ); 13409b94acceSBarry Smith __NLList = 0; 13419b94acceSBarry Smith } 13429b94acceSBarry Smith return 0; 13439b94acceSBarry Smith } 13449b94acceSBarry Smith 13459b94acceSBarry Smith /* 13469b94acceSBarry Smith SNESGetMethodFromOptions_Private - Sets the selected method from the 13479b94acceSBarry Smith options database. 13489b94acceSBarry Smith 13499b94acceSBarry Smith Input Parameter: 13509b94acceSBarry Smith . ctx - the SNES context 13519b94acceSBarry Smith 13529b94acceSBarry Smith Output Parameter: 13539b94acceSBarry Smith . method - solver method 13549b94acceSBarry Smith 13559b94acceSBarry Smith Returns: 13569b94acceSBarry Smith Returns 1 if the method is found; 0 otherwise. 13579b94acceSBarry Smith 13589b94acceSBarry Smith Options Database Key: 13599b94acceSBarry Smith $ -snes_method method 13609b94acceSBarry Smith */ 13619b94acceSBarry Smith int SNESGetMethodFromOptions_Private(SNES ctx,SNESMethod *method) 13629b94acceSBarry Smith { 13639b94acceSBarry Smith char sbuf[50]; 13649b94acceSBarry Smith if (OptionsGetString(ctx->prefix,"-snes_method", sbuf, 50 )) { 13659b94acceSBarry Smith if (!__NLList) SNESRegisterAll(); 13669b94acceSBarry Smith *method = (SNESMethod)NRFindID( __NLList, sbuf ); 13679b94acceSBarry Smith return 1; 13689b94acceSBarry Smith } 13699b94acceSBarry Smith return 0; 13709b94acceSBarry Smith } 13719b94acceSBarry Smith 13729b94acceSBarry Smith /*@ 13739b94acceSBarry Smith SNESGetMethodFromContext - Gets the nonlinear solver method from an 13749b94acceSBarry Smith active SNES context. 13759b94acceSBarry Smith 13769b94acceSBarry Smith Input Parameter: 13779b94acceSBarry Smith . snes - the SNES context 13789b94acceSBarry Smith 13799b94acceSBarry Smith Output parameters: 13809b94acceSBarry Smith . method - the method ID 13819b94acceSBarry Smith 13829b94acceSBarry Smith .keywords: SNES, nonlinear, get, method, context, type 13839b94acceSBarry Smith 13849b94acceSBarry Smith .seealso: SNESGetMethodName() 13859b94acceSBarry Smith @*/ 13869b94acceSBarry Smith int SNESGetMethodFromContext(SNES snes, SNESMethod *method) 13879b94acceSBarry Smith { 13889b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 13899b94acceSBarry Smith *method = (SNESMethod) snes->type; 13909b94acceSBarry Smith return 0; 13919b94acceSBarry Smith } 13929b94acceSBarry Smith 13939b94acceSBarry Smith /*@C 13949b94acceSBarry Smith SNESGetMethodName - Gets the SNES method name (as a string) from 13959b94acceSBarry Smith the method type. 13969b94acceSBarry Smith 13979b94acceSBarry Smith Input Parameter: 13989b94acceSBarry Smith . method - SNES method 13999b94acceSBarry Smith 14009b94acceSBarry Smith Output Parameter: 14019b94acceSBarry Smith . name - name of SNES method 14029b94acceSBarry Smith 14039b94acceSBarry Smith .keywords: SNES, nonlinear, get, method, name 14049b94acceSBarry Smith @*/ 14059b94acceSBarry Smith int SNESGetMethodName(SNESMethod method,char **name) 14069b94acceSBarry Smith { 140706a9b0adSLois Curfman McInnes int ierr; 140806a9b0adSLois Curfman McInnes if (!__NLList) {ierr = SNESRegisterAll(); CHKERRQ(ierr);} 14099b94acceSBarry Smith *name = NRFindName( __NLList, (int) method ); 14109b94acceSBarry Smith return 0; 14119b94acceSBarry Smith } 14129b94acceSBarry Smith 14139b94acceSBarry Smith #include <stdio.h> 14149b94acceSBarry Smith /* 14159b94acceSBarry Smith SNESPrintMethods_Private - Prints the SNES methods available from the 14169b94acceSBarry Smith options database. 14179b94acceSBarry Smith 14189b94acceSBarry Smith Input Parameters: 14199b94acceSBarry Smith . prefix - prefix (usually "-") 14209b94acceSBarry Smith . name - the options database name (by default "snes_method") 14219b94acceSBarry Smith */ 14229b94acceSBarry Smith int SNESPrintMethods_Private(char* prefix,char *name) 14239b94acceSBarry Smith { 14249b94acceSBarry Smith FuncList *entry; 14259b94acceSBarry Smith if (!__NLList) {SNESRegisterAll();} 14269b94acceSBarry Smith entry = __NLList->head; 14279b94acceSBarry Smith fprintf(stderr," %s%s (one of)",prefix,name); 14289b94acceSBarry Smith while (entry) { 14299b94acceSBarry Smith fprintf(stderr," %s",entry->name); 14309b94acceSBarry Smith entry = entry->next; 14319b94acceSBarry Smith } 14329b94acceSBarry Smith fprintf(stderr,"\n"); 14339b94acceSBarry Smith return 0; 14349b94acceSBarry Smith } 14359b94acceSBarry Smith 14369b94acceSBarry Smith /*@C 14379b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 14389b94acceSBarry Smith stored. 14399b94acceSBarry Smith 14409b94acceSBarry Smith Input Parameter: 14419b94acceSBarry Smith . snes - the SNES context 14429b94acceSBarry Smith 14439b94acceSBarry Smith Output Parameter: 14449b94acceSBarry Smith . x - the solution 14459b94acceSBarry Smith 14469b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution 14479b94acceSBarry Smith 14489b94acceSBarry Smith .seealso: SNESSetSolution(), SNESGetFunction(), SNESGetSolutionUpdate() 14499b94acceSBarry Smith @*/ 14509b94acceSBarry Smith int SNESGetSolution(SNES snes,Vec *x) 14519b94acceSBarry Smith { 14529b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 14539b94acceSBarry Smith *x = snes->vec_sol_always; 14549b94acceSBarry Smith return 0; 14559b94acceSBarry Smith } 14569b94acceSBarry Smith 14579b94acceSBarry Smith /*@C 14589b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 14599b94acceSBarry Smith stored. 14609b94acceSBarry Smith 14619b94acceSBarry Smith Input Parameter: 14629b94acceSBarry Smith . snes - the SNES context 14639b94acceSBarry Smith 14649b94acceSBarry Smith Output Parameter: 14659b94acceSBarry Smith . x - the solution update 14669b94acceSBarry Smith 14679b94acceSBarry Smith Notes: 14689b94acceSBarry Smith This vector is implementation dependent. 14699b94acceSBarry Smith 14709b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update 14719b94acceSBarry Smith 14729b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction 14739b94acceSBarry Smith @*/ 14749b94acceSBarry Smith int SNESGetSolutionUpdate(SNES snes,Vec *x) 14759b94acceSBarry Smith { 14769b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 14779b94acceSBarry Smith *x = snes->vec_sol_update_always; 14789b94acceSBarry Smith return 0; 14799b94acceSBarry Smith } 14809b94acceSBarry Smith 14819b94acceSBarry Smith /*@C 14829b94acceSBarry Smith SNESGetFunction - Returns the vector where the function is 14839b94acceSBarry Smith stored. Actually usually returns the vector where the negative of 14849b94acceSBarry Smith the function is stored. 14859b94acceSBarry Smith 14869b94acceSBarry Smith Input Parameter: 14879b94acceSBarry Smith . snes - the SNES context 14889b94acceSBarry Smith 14899b94acceSBarry Smith Output Parameter: 14909b94acceSBarry Smith . r - the function (or its negative) 14919b94acceSBarry Smith 14929b94acceSBarry Smith Notes: 14939b94acceSBarry Smith SNESGetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only 14949b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 14959b94acceSBarry Smith SNESGetMinimizationFunction() and SNESGetGradient(); 14969b94acceSBarry Smith 14979b94acceSBarry Smith .keywords: SNES, nonlinear, get function 14989b94acceSBarry Smith 14999b94acceSBarry Smith .seealso: SNESSetFunction(), SNESGetSolution() 15009b94acceSBarry Smith @*/ 15019b94acceSBarry Smith int SNESGetFunction(SNES snes,Vec *r) 15029b94acceSBarry Smith { 15039b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 15049b94acceSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1, 150548d91487SBarry Smith "SNESGetFunction:For SNES_NONLINEAR_EQUATIONS only"); 15069b94acceSBarry Smith *r = snes->vec_func_always; 15079b94acceSBarry Smith return 0; 15089b94acceSBarry Smith } 15099b94acceSBarry Smith 15109b94acceSBarry Smith /*@C 15119b94acceSBarry Smith SNESGetGradient - Returns the vector where the gradient is 15129b94acceSBarry Smith stored. Actually usually returns the vector where the negative of 15139b94acceSBarry Smith the function is stored. 15149b94acceSBarry Smith 15159b94acceSBarry Smith Input Parameter: 15169b94acceSBarry Smith . snes - the SNES context 15179b94acceSBarry Smith 15189b94acceSBarry Smith Output Parameter: 15199b94acceSBarry Smith . r - the gradient 15209b94acceSBarry Smith 15219b94acceSBarry Smith Notes: 15229b94acceSBarry Smith SNESGetGradient() is valid for SNES_UNCONSTRAINED_MINIMIZATION methods 15239b94acceSBarry Smith only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 15249b94acceSBarry Smith SNESGetFunction(). 15259b94acceSBarry Smith 15269b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient 15279b94acceSBarry Smith 15289b94acceSBarry Smith .seealso: SNESGetMinimizationFunction(), SNESGetSolution() 15299b94acceSBarry Smith @*/ 15309b94acceSBarry Smith int SNESGetGradient(SNES snes,Vec *r) 15319b94acceSBarry Smith { 15329b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 15339b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 153448d91487SBarry Smith "SNESGetGradient:For SNES_UNCONSTRAINED_MINIMIZATION only"); 15359b94acceSBarry Smith *r = snes->vec_func_always; 15369b94acceSBarry Smith return 0; 15379b94acceSBarry Smith } 15389b94acceSBarry Smith 15399b94acceSBarry Smith /*@ 15409b94acceSBarry Smith SNESGetMinimizationFunction - Returns the scalar function value for 15419b94acceSBarry Smith unconstrained minimization problems. 15429b94acceSBarry Smith 15439b94acceSBarry Smith Input Parameter: 15449b94acceSBarry Smith . snes - the SNES context 15459b94acceSBarry Smith 15469b94acceSBarry Smith Output Parameter: 15479b94acceSBarry Smith . r - the function 15489b94acceSBarry Smith 15499b94acceSBarry Smith Notes: 15509b94acceSBarry Smith SNESGetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 15519b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 15529b94acceSBarry Smith SNESGetFunction(). 15539b94acceSBarry Smith 15549b94acceSBarry Smith .keywords: SNES, nonlinear, get, function 15559b94acceSBarry Smith 15569b94acceSBarry Smith .seealso: SNESGetGradient(), SNESGetSolution() 15579b94acceSBarry Smith @*/ 15589b94acceSBarry Smith int SNESGetMinimizationFunction(SNES snes,double *r) 15599b94acceSBarry Smith { 15609b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 15619b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 156248d91487SBarry Smith "SNESGetMinimizationFunction:For SNES_UNCONSTRAINED_MINIMIZATION only"); 15639b94acceSBarry Smith *r = snes->fc; 15649b94acceSBarry Smith return 0; 15659b94acceSBarry Smith } 15669b94acceSBarry Smith 15679b94acceSBarry Smith 15689b94acceSBarry Smith 15699b94acceSBarry Smith 15709b94acceSBarry Smith 1571