1*9b94acceSBarry Smith #ifndef lint 2*9b94acceSBarry Smith static char vcid[] = "$Id: snes.c,v 1.15 1995/09/06 03:06:25 bsmith Exp bsmith $"; 3*9b94acceSBarry Smith #endif 4*9b94acceSBarry Smith 5*9b94acceSBarry Smith #include "draw.h" /*I "draw.h" I*/ 6*9b94acceSBarry Smith #include "snesimpl.h" /*I "snes.h" I*/ 7*9b94acceSBarry Smith #include "sys/nreg.h" /*I "sys/nreg.h" I*/ 8*9b94acceSBarry Smith #include "pinclude/pviewer.h" 9*9b94acceSBarry Smith #include <math.h> 10*9b94acceSBarry Smith 11*9b94acceSBarry Smith extern int SNESGetMethodFromOptions_Private(SNES,SNESMethod*); 12*9b94acceSBarry Smith extern int SNESPrintMethods_Private(char*,char*); 13*9b94acceSBarry Smith 14*9b94acceSBarry Smith /*@ 15*9b94acceSBarry Smith SNESView - Prints the SNES data structure. 16*9b94acceSBarry Smith 17*9b94acceSBarry Smith Input Parameters: 18*9b94acceSBarry Smith . SNES - the SNES context 19*9b94acceSBarry Smith . viewer - visualization context 20*9b94acceSBarry Smith 21*9b94acceSBarry Smith Options Database Key: 22*9b94acceSBarry Smith $ -snes_view : calls SNESView() at end of SNESSolve() 23*9b94acceSBarry Smith 24*9b94acceSBarry Smith Notes: 25*9b94acceSBarry Smith The available visualization contexts include 26*9b94acceSBarry Smith $ STDOUT_VIEWER_SELF - standard output (default) 27*9b94acceSBarry Smith $ STDOUT_VIEWER_WORLD - synchronized standard 28*9b94acceSBarry Smith $ output where only the first processor opens 29*9b94acceSBarry Smith $ the file. All other processors send their 30*9b94acceSBarry Smith $ data to the first processor to print. 31*9b94acceSBarry Smith 32*9b94acceSBarry Smith The user can open alternative vistualization contexts with 33*9b94acceSBarry Smith $ ViewerFileOpen() - output to a specified file 34*9b94acceSBarry Smith 35*9b94acceSBarry Smith .keywords: SNES, view 36*9b94acceSBarry Smith 37*9b94acceSBarry Smith .seealso: ViewerFileOpen() 38*9b94acceSBarry Smith @*/ 39*9b94acceSBarry Smith int SNESView(SNES snes,Viewer viewer) 40*9b94acceSBarry Smith { 41*9b94acceSBarry Smith PetscObject vobj = (PetscObject) viewer; 42*9b94acceSBarry Smith SNES_KSP_EW_ConvCtx *kctx; 43*9b94acceSBarry Smith FILE *fd; 44*9b94acceSBarry Smith int ierr; 45*9b94acceSBarry Smith SLES sles; 46*9b94acceSBarry Smith char *method; 47*9b94acceSBarry Smith 48*9b94acceSBarry Smith if (vobj->cookie == VIEWER_COOKIE && (vobj->type == ASCII_FILE_VIEWER || 49*9b94acceSBarry Smith vobj->type == ASCII_FILES_VIEWER)) { 50*9b94acceSBarry Smith ierr = ViewerFileGetPointer_Private(viewer,&fd); CHKERRQ(ierr); 51*9b94acceSBarry Smith MPIU_fprintf(snes->comm,fd,"SNES Object:\n"); 52*9b94acceSBarry Smith SNESGetMethodName((SNESMethod)snes->type,&method); 53*9b94acceSBarry Smith MPIU_fprintf(snes->comm,fd," method: %s\n",method); 54*9b94acceSBarry Smith if (snes->view) (*snes->view)((PetscObject)snes,viewer); 55*9b94acceSBarry Smith MPIU_fprintf(snes->comm,fd, 56*9b94acceSBarry Smith " maximum iterations=%d, maximum function evaluations=%d\n", 57*9b94acceSBarry Smith snes->max_its,snes->max_funcs); 58*9b94acceSBarry Smith MPIU_fprintf(snes->comm,fd, 59*9b94acceSBarry Smith " tolerances: relative=%g, absolute=%g, truncation=%g, solution=%g\n", 60*9b94acceSBarry Smith snes->rtol, snes->atol, snes->trunctol, snes->xtol); 61*9b94acceSBarry Smith if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) 62*9b94acceSBarry Smith MPIU_fprintf(snes->comm,fd," min function tolerance=%g\n",snes->fmin); 63*9b94acceSBarry Smith if (snes->ksp_ewconv) { 64*9b94acceSBarry Smith kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx; 65*9b94acceSBarry Smith if (kctx) { 66*9b94acceSBarry Smith MPIU_fprintf(snes->comm,fd, 67*9b94acceSBarry Smith " Eisenstat-Walker computation of KSP relative tolerance (version %d)\n", 68*9b94acceSBarry Smith kctx->version); 69*9b94acceSBarry Smith MPIU_fprintf(snes->comm,fd, 70*9b94acceSBarry Smith " rtol_0=%g, rtol_max=%g, threshold=%g\n",kctx->rtol_0, 71*9b94acceSBarry Smith kctx->rtol_max,kctx->threshold); 72*9b94acceSBarry Smith MPIU_fprintf(snes->comm,fd," gamma=%g, alpha=%g, alpha2=%g\n", 73*9b94acceSBarry Smith kctx->gamma,kctx->alpha,kctx->alpha2); 74*9b94acceSBarry Smith } 75*9b94acceSBarry Smith } 76*9b94acceSBarry Smith SNESGetSLES(snes,&sles); 77*9b94acceSBarry Smith ierr = SLESView(sles,viewer); CHKERRQ(ierr); 78*9b94acceSBarry Smith } 79*9b94acceSBarry Smith return 0; 80*9b94acceSBarry Smith } 81*9b94acceSBarry Smith 82*9b94acceSBarry Smith /*@ 83*9b94acceSBarry Smith SNESSetFromOptions - Sets various SLES parameters from user options. 84*9b94acceSBarry Smith 85*9b94acceSBarry Smith Input Parameter: 86*9b94acceSBarry Smith . snes - the SNES context 87*9b94acceSBarry Smith 88*9b94acceSBarry Smith .keywords: SNES, nonlinear, set, options, database 89*9b94acceSBarry Smith 90*9b94acceSBarry Smith .seealso: SNESPrintHelp() 91*9b94acceSBarry Smith @*/ 92*9b94acceSBarry Smith int SNESSetFromOptions(SNES snes) 93*9b94acceSBarry Smith { 94*9b94acceSBarry Smith SNESMethod method; 95*9b94acceSBarry Smith double tmp; 96*9b94acceSBarry Smith SLES sles; 97*9b94acceSBarry Smith int ierr; 98*9b94acceSBarry Smith int version = PETSC_DEFAULT; 99*9b94acceSBarry Smith double rtol_0 = PETSC_DEFAULT; 100*9b94acceSBarry Smith double rtol_max = PETSC_DEFAULT; 101*9b94acceSBarry Smith double gamma2 = PETSC_DEFAULT; 102*9b94acceSBarry Smith double alpha = PETSC_DEFAULT; 103*9b94acceSBarry Smith double alpha2 = PETSC_DEFAULT; 104*9b94acceSBarry Smith double threshold = PETSC_DEFAULT; 105*9b94acceSBarry Smith 106*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 107*9b94acceSBarry Smith if (SNESGetMethodFromOptions_Private(snes,&method)) { 108*9b94acceSBarry Smith SNESSetMethod(snes,method); 109*9b94acceSBarry Smith } 110*9b94acceSBarry Smith if (OptionsHasName(0,"-help")) SNESPrintHelp(snes); 111*9b94acceSBarry Smith if (OptionsGetDouble(snes->prefix,"-snes_stol",&tmp)) { 112*9b94acceSBarry Smith SNESSetSolutionTolerance(snes,tmp); 113*9b94acceSBarry Smith } 114*9b94acceSBarry Smith if (OptionsGetDouble(snes->prefix,"-snes_ttol",&tmp)) { 115*9b94acceSBarry Smith SNESSetTruncationTolerance(snes,tmp); 116*9b94acceSBarry Smith } 117*9b94acceSBarry Smith if (OptionsGetDouble(snes->prefix,"-snes_atol",&tmp)) { 118*9b94acceSBarry Smith SNESSetAbsoluteTolerance(snes,tmp); 119*9b94acceSBarry Smith } 120*9b94acceSBarry Smith if (OptionsGetDouble(snes->prefix,"-snes_trtol",&tmp)) { 121*9b94acceSBarry Smith SNESSetTrustRegionTolerance(snes,tmp); 122*9b94acceSBarry Smith } 123*9b94acceSBarry Smith if (OptionsGetDouble(snes->prefix,"-snes_rtol",&tmp)) { 124*9b94acceSBarry Smith SNESSetRelativeTolerance(snes,tmp); 125*9b94acceSBarry Smith } 126*9b94acceSBarry Smith if (OptionsGetDouble(snes->prefix,"-snes_fmin",&tmp)) { 127*9b94acceSBarry Smith SNESSetMinFunctionTolerance(snes,tmp); 128*9b94acceSBarry Smith } 129*9b94acceSBarry Smith OptionsGetInt(snes->prefix,"-snes_max_it",&snes->max_its); 130*9b94acceSBarry Smith OptionsGetInt(snes->prefix,"-snes_max_funcs",&snes->max_funcs); 131*9b94acceSBarry Smith if (OptionsHasName(snes->prefix,"-snes_ksp_ew_conv")) { 132*9b94acceSBarry Smith snes->ksp_ewconv = 1; 133*9b94acceSBarry Smith } 134*9b94acceSBarry Smith OptionsGetInt(snes->prefix,"-snes_ksp_ew_version",&version); 135*9b94acceSBarry Smith OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtol0",&rtol_0); 136*9b94acceSBarry Smith OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtolmax",&rtol_max); 137*9b94acceSBarry Smith OptionsGetDouble(snes->prefix,"-snes_ksp_ew_gamma",&gamma2); 138*9b94acceSBarry Smith OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha",&alpha); 139*9b94acceSBarry Smith OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha2",&alpha2); 140*9b94acceSBarry Smith OptionsGetDouble(snes->prefix,"-snes_ksp_ew_threshold",&threshold); 141*9b94acceSBarry Smith ierr = SNES_KSP_SetParametersEW(snes,version,rtol_0,rtol_max,gamma2,alpha, 142*9b94acceSBarry Smith alpha2,threshold); CHKERRQ(ierr); 143*9b94acceSBarry Smith if (OptionsHasName(snes->prefix,"-snes_monitor")) { 144*9b94acceSBarry Smith SNESSetMonitor(snes,SNESDefaultMonitor,0); 145*9b94acceSBarry Smith } 146*9b94acceSBarry Smith if (OptionsHasName(snes->prefix,"-snes_smonitor")) { 147*9b94acceSBarry Smith SNESSetMonitor(snes,SNESDefaultSMonitor,0); 148*9b94acceSBarry Smith } 149*9b94acceSBarry Smith if (OptionsHasName(snes->prefix,"-snes_xmonitor")){ 150*9b94acceSBarry Smith int mytid = 0; 151*9b94acceSBarry Smith DrawLGCtx lg; 152*9b94acceSBarry Smith MPI_Initialized(&mytid); 153*9b94acceSBarry Smith if (mytid) MPI_Comm_rank(snes->comm,&mytid); 154*9b94acceSBarry Smith if (!mytid) { 155*9b94acceSBarry Smith ierr = SNESLGMonitorCreate(0,0,0,0,300,300,&lg); CHKERRQ(ierr); 156*9b94acceSBarry Smith ierr = SNESSetMonitor(snes,SNESLGMonitor,(void *)lg); CHKERRQ(ierr); 157*9b94acceSBarry Smith PLogObjectParent(snes,lg); 158*9b94acceSBarry Smith } 159*9b94acceSBarry Smith } 160*9b94acceSBarry Smith if (OptionsHasName(snes->prefix,"-snes_fd") && 161*9b94acceSBarry Smith snes->method_class == SNES_NONLINEAR_EQUATIONS) { 162*9b94acceSBarry Smith ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre, 163*9b94acceSBarry Smith SNESDefaultComputeJacobian,snes->funP); CHKERRQ(ierr); 164*9b94acceSBarry Smith } 165*9b94acceSBarry Smith if (OptionsHasName(snes->prefix,"-snes_mf") && 166*9b94acceSBarry Smith snes->method_class == SNES_NONLINEAR_EQUATIONS) { 167*9b94acceSBarry Smith Mat J; 168*9b94acceSBarry Smith ierr = SNESDefaultMatrixFreeMatCreate(snes,snes->vec_sol,&J);CHKERRQ(ierr); 169*9b94acceSBarry Smith ierr = SNESSetJacobian(snes,J,J,0,snes->funP); CHKERRQ(ierr); 170*9b94acceSBarry Smith PLogObjectParent(snes,J); 171*9b94acceSBarry Smith snes->mfshell = J; 172*9b94acceSBarry Smith } 173*9b94acceSBarry Smith ierr = SNESGetSLES(snes,&sles); CHKERRQ(ierr); 174*9b94acceSBarry Smith ierr = SLESSetFromOptions(sles); CHKERRQ(ierr); 175*9b94acceSBarry Smith if (!snes->setfromoptions) return 0; 176*9b94acceSBarry Smith return (*snes->setfromoptions)(snes); 177*9b94acceSBarry Smith } 178*9b94acceSBarry Smith 179*9b94acceSBarry Smith /*@ 180*9b94acceSBarry Smith SNESPrintHelp - Prints all options for the SNES component. 181*9b94acceSBarry Smith 182*9b94acceSBarry Smith Input Parameter: 183*9b94acceSBarry Smith . snes - the SNES context 184*9b94acceSBarry Smith 185*9b94acceSBarry Smith .keywords: SNES, nonlinear, help 186*9b94acceSBarry Smith 187*9b94acceSBarry Smith .seealso: SLESSetFromOptions() 188*9b94acceSBarry Smith @*/ 189*9b94acceSBarry Smith int SNESPrintHelp(SNES snes) 190*9b94acceSBarry Smith { 191*9b94acceSBarry Smith char *prefix = "-"; 192*9b94acceSBarry Smith SNES_KSP_EW_ConvCtx *kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx; 193*9b94acceSBarry Smith if (snes->prefix) prefix = snes->prefix; 194*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 195*9b94acceSBarry Smith MPIU_printf(snes->comm,"SNES options ----------------------------\n"); 196*9b94acceSBarry Smith SNESPrintMethods_Private(prefix,"snes_method"); 197*9b94acceSBarry Smith MPIU_printf(snes->comm," %ssnes_monitor\n",prefix); 198*9b94acceSBarry Smith MPIU_printf(snes->comm," %ssnes_view\n",prefix); 199*9b94acceSBarry Smith MPIU_printf(snes->comm," %ssnes_max_it its (default %d)\n",prefix,snes->max_its); 200*9b94acceSBarry Smith MPIU_printf(snes->comm," %ssnes_stol tol (default %g)\n",prefix,snes->xtol); 201*9b94acceSBarry Smith MPIU_printf(snes->comm," %ssnes_atol tol (default %g)\n",prefix,snes->atol); 202*9b94acceSBarry Smith MPIU_printf(snes->comm," %ssnes_rtol tol (default %g)\n",prefix,snes->rtol); 203*9b94acceSBarry Smith MPIU_printf(snes->comm," %ssnes_ttol tol (default %g)\n",prefix,snes->trunctol); 204*9b94acceSBarry Smith MPIU_printf(snes->comm, 205*9b94acceSBarry Smith " options for solving systems of nonlinear equations only:\n"); 206*9b94acceSBarry Smith MPIU_printf(snes->comm," %ssnes_fd: use finite differences for Jacobian\n",prefix); 207*9b94acceSBarry Smith MPIU_printf(snes->comm," %ssnes_mf: use matrix-free Jacobian\n",prefix); 208*9b94acceSBarry Smith MPIU_printf(snes->comm," %ssnes_ksp_ew_conv: use Eisenstat-Walker computation of KSP rtol. Params are:\n",prefix); 209*9b94acceSBarry Smith MPIU_printf(snes->comm, 210*9b94acceSBarry Smith " %ssnes_ksp_ew_version version (1 or 2, default is %d)\n", 211*9b94acceSBarry Smith prefix,kctx->version); 212*9b94acceSBarry Smith MPIU_printf(snes->comm, 213*9b94acceSBarry Smith " %ssnes_ksp_ew_rtol0 rtol0 (0 <= rtol0 < 1, default %g)\n", 214*9b94acceSBarry Smith prefix,kctx->rtol_0); 215*9b94acceSBarry Smith MPIU_printf(snes->comm, 216*9b94acceSBarry Smith " %ssnes_ksp_ew_rtolmax rtolmax (0 <= rtolmax < 1, default %g)\n", 217*9b94acceSBarry Smith prefix,kctx->rtol_max); 218*9b94acceSBarry Smith MPIU_printf(snes->comm, 219*9b94acceSBarry Smith " %ssnes_ksp_ew_gamma gamma (0 <= gamma <= 1, default %g)\n", 220*9b94acceSBarry Smith prefix,kctx->gamma); 221*9b94acceSBarry Smith MPIU_printf(snes->comm, 222*9b94acceSBarry Smith " %ssnes_ksp_ew_alpha alpha (1 < alpha <= 2, default %g)\n", 223*9b94acceSBarry Smith prefix,kctx->alpha); 224*9b94acceSBarry Smith MPIU_printf(snes->comm, 225*9b94acceSBarry Smith " %ssnes_ksp_ew_alpha2 alpha2 (default %g)\n", 226*9b94acceSBarry Smith prefix,kctx->alpha2); 227*9b94acceSBarry Smith MPIU_printf(snes->comm, 228*9b94acceSBarry Smith " %ssnes_ksp_ew_threshold threshold (0 < threshold < 1, default %g)\n", 229*9b94acceSBarry Smith prefix,kctx->threshold); 230*9b94acceSBarry Smith MPIU_printf(snes->comm, 231*9b94acceSBarry Smith " options for solving unconstrained minimization problems only:\n"); 232*9b94acceSBarry Smith MPIU_printf(snes->comm," %ssnes_fmin tol (default %g)\n",prefix,snes->fmin); 233*9b94acceSBarry Smith MPIU_printf(snes->comm," Run program with %ssnes_method method -help for help on ",prefix); 234*9b94acceSBarry Smith MPIU_printf(snes->comm,"a particular method\n"); 235*9b94acceSBarry Smith if (snes->printhelp) (*snes->printhelp)(snes); 236*9b94acceSBarry Smith return 0; 237*9b94acceSBarry Smith } 238*9b94acceSBarry Smith /*@ 239*9b94acceSBarry Smith SNESSetApplicationContext - Sets the optional user-defined context for 240*9b94acceSBarry Smith the nonlinear solvers. 241*9b94acceSBarry Smith 242*9b94acceSBarry Smith Input Parameters: 243*9b94acceSBarry Smith . snes - the SNES context 244*9b94acceSBarry Smith . usrP - optional user context 245*9b94acceSBarry Smith 246*9b94acceSBarry Smith .keywords: SNES, nonlinear, set, application, context 247*9b94acceSBarry Smith 248*9b94acceSBarry Smith .seealso: SNESGetApplicationContext() 249*9b94acceSBarry Smith @*/ 250*9b94acceSBarry Smith int SNESSetApplicationContext(SNES snes,void *usrP) 251*9b94acceSBarry Smith { 252*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 253*9b94acceSBarry Smith snes->user = usrP; 254*9b94acceSBarry Smith return 0; 255*9b94acceSBarry Smith } 256*9b94acceSBarry Smith /*@C 257*9b94acceSBarry Smith SNESGetApplicationContext - Gets the user-defined context for the 258*9b94acceSBarry Smith nonlinear solvers. 259*9b94acceSBarry Smith 260*9b94acceSBarry Smith Input Parameter: 261*9b94acceSBarry Smith . snes - SNES context 262*9b94acceSBarry Smith 263*9b94acceSBarry Smith Output Parameter: 264*9b94acceSBarry Smith . usrP - user context 265*9b94acceSBarry Smith 266*9b94acceSBarry Smith .keywords: SNES, nonlinear, get, application, context 267*9b94acceSBarry Smith 268*9b94acceSBarry Smith .seealso: SNESSetApplicationContext() 269*9b94acceSBarry Smith @*/ 270*9b94acceSBarry Smith int SNESGetApplicationContext( SNES snes, void **usrP ) 271*9b94acceSBarry Smith { 272*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 273*9b94acceSBarry Smith *usrP = snes->user; 274*9b94acceSBarry Smith return 0; 275*9b94acceSBarry Smith } 276*9b94acceSBarry Smith /*@ 277*9b94acceSBarry Smith SNESGetIterationNumber - Gets the current iteration number of the 278*9b94acceSBarry Smith nonlinear solver. 279*9b94acceSBarry Smith 280*9b94acceSBarry Smith Input Parameter: 281*9b94acceSBarry Smith . snes - SNES context 282*9b94acceSBarry Smith 283*9b94acceSBarry Smith Output Parameter: 284*9b94acceSBarry Smith . iter - iteration number 285*9b94acceSBarry Smith 286*9b94acceSBarry Smith .keywords: SNES, nonlinear, get, iteration, number 287*9b94acceSBarry Smith @*/ 288*9b94acceSBarry Smith int SNESGetIterationNumber(SNES snes,int* iter) 289*9b94acceSBarry Smith { 290*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 291*9b94acceSBarry Smith *iter = snes->iter; 292*9b94acceSBarry Smith return 0; 293*9b94acceSBarry Smith } 294*9b94acceSBarry Smith /*@ 295*9b94acceSBarry Smith SNESGetFunctionNorm - Gets the norm of the current function that was set 296*9b94acceSBarry Smith with SNESSSetFunction(). 297*9b94acceSBarry Smith 298*9b94acceSBarry Smith Input Parameter: 299*9b94acceSBarry Smith . snes - SNES context 300*9b94acceSBarry Smith 301*9b94acceSBarry Smith Output Parameter: 302*9b94acceSBarry Smith . fnorm - 2-norm of function 303*9b94acceSBarry Smith 304*9b94acceSBarry Smith Note: 305*9b94acceSBarry Smith SNESGetFunctionNorm() is valid for SNES_NONLINEAR_EQUATIONS methods only. 306*9b94acceSBarry Smith 307*9b94acceSBarry Smith .keywords: SNES, nonlinear, get, function, norm 308*9b94acceSBarry Smith @*/ 309*9b94acceSBarry Smith int SNESGetFunctionNorm(SNES snes,Scalar *fnorm) 310*9b94acceSBarry Smith { 311*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 312*9b94acceSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1, 313*9b94acceSBarry Smith "SNESGetFunctionNorm: Valid for SNES_NONLINEAR_EQUATIONS methods only"); 314*9b94acceSBarry Smith *fnorm = snes->norm; 315*9b94acceSBarry Smith return 0; 316*9b94acceSBarry Smith } 317*9b94acceSBarry Smith /*@ 318*9b94acceSBarry Smith SNESGetGradientNorm - Gets the norm of the current gradient that was set 319*9b94acceSBarry Smith with SNESSSetGradient(). 320*9b94acceSBarry Smith 321*9b94acceSBarry Smith Input Parameter: 322*9b94acceSBarry Smith . snes - SNES context 323*9b94acceSBarry Smith 324*9b94acceSBarry Smith Output Parameter: 325*9b94acceSBarry Smith . fnorm - 2-norm of gradient 326*9b94acceSBarry Smith 327*9b94acceSBarry Smith Note: 328*9b94acceSBarry Smith SNESGetGradientNorm() is valid for SNES_UNCONSTRAINED_MINIMIZATION 329*9b94acceSBarry Smith methods only. 330*9b94acceSBarry Smith 331*9b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient, norm 332*9b94acceSBarry Smith @*/ 333*9b94acceSBarry Smith int SNESGetGradientNorm(SNES snes,Scalar *gnorm) 334*9b94acceSBarry Smith { 335*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 336*9b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 337*9b94acceSBarry Smith "SNESGetGradientNorm: Valid for SNES_UNCONSTRAINED_MINIMIZATION methods only"); 338*9b94acceSBarry Smith *gnorm = snes->norm; 339*9b94acceSBarry Smith return 0; 340*9b94acceSBarry Smith } 341*9b94acceSBarry Smith /*@ 342*9b94acceSBarry Smith SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps 343*9b94acceSBarry Smith attempted by the nonlinear solver. 344*9b94acceSBarry Smith 345*9b94acceSBarry Smith Input Parameter: 346*9b94acceSBarry Smith . snes - SNES context 347*9b94acceSBarry Smith 348*9b94acceSBarry Smith Output Parameter: 349*9b94acceSBarry Smith . nfails - number of unsuccessful steps attempted 350*9b94acceSBarry Smith 351*9b94acceSBarry Smith .keywords: SNES, nonlinear, get, number, unsuccessful, steps 352*9b94acceSBarry Smith @*/ 353*9b94acceSBarry Smith int SNESGetNumberUnsuccessfulSteps(SNES snes,int* nfails) 354*9b94acceSBarry Smith { 355*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 356*9b94acceSBarry Smith *nfails = snes->nfailures; 357*9b94acceSBarry Smith return 0; 358*9b94acceSBarry Smith } 359*9b94acceSBarry Smith /*@C 360*9b94acceSBarry Smith SNESGetSLES - Returns the SLES context for a SNES solver. 361*9b94acceSBarry Smith 362*9b94acceSBarry Smith Input Parameter: 363*9b94acceSBarry Smith . snes - the SNES context 364*9b94acceSBarry Smith 365*9b94acceSBarry Smith Output Parameter: 366*9b94acceSBarry Smith . sles - the SLES context 367*9b94acceSBarry Smith 368*9b94acceSBarry Smith Notes: 369*9b94acceSBarry Smith The user can then directly manipulate the SLES context to set various 370*9b94acceSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 371*9b94acceSBarry Smith KSP and PC contexts as well. 372*9b94acceSBarry Smith 373*9b94acceSBarry Smith .keywords: SNES, nonlinear, get, SLES, context 374*9b94acceSBarry Smith 375*9b94acceSBarry Smith .seealso: SLESGetPC(), SLESGetKSP() 376*9b94acceSBarry Smith @*/ 377*9b94acceSBarry Smith int SNESGetSLES(SNES snes,SLES *sles) 378*9b94acceSBarry Smith { 379*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 380*9b94acceSBarry Smith *sles = snes->sles; 381*9b94acceSBarry Smith return 0; 382*9b94acceSBarry Smith } 383*9b94acceSBarry Smith /* -----------------------------------------------------------*/ 384*9b94acceSBarry Smith /*@C 385*9b94acceSBarry Smith SNESCreate - Creates a nonlinear solver context. 386*9b94acceSBarry Smith 387*9b94acceSBarry Smith Input Parameter: 388*9b94acceSBarry Smith . comm - MPI communicator 389*9b94acceSBarry Smith . type - type of method, one of 390*9b94acceSBarry Smith $ SNES_NONLINEAR_EQUATIONS 391*9b94acceSBarry Smith $ (for systems of nonlinear equations) 392*9b94acceSBarry Smith $ SNES_UNCONSTRAINED_MINIMIZATION 393*9b94acceSBarry Smith $ (for unconstrained minimization) 394*9b94acceSBarry Smith 395*9b94acceSBarry Smith Output Parameter: 396*9b94acceSBarry Smith . outsnes - the new SNES context 397*9b94acceSBarry Smith 398*9b94acceSBarry Smith .keywords: SNES, nonlinear, create, context 399*9b94acceSBarry Smith 400*9b94acceSBarry Smith .seealso: SNESSetUp(), SNESSolve(), SNESDestroy() 401*9b94acceSBarry Smith @*/ 402*9b94acceSBarry Smith int SNESCreate(MPI_Comm comm,SNESType type,SNES *outsnes) 403*9b94acceSBarry Smith { 404*9b94acceSBarry Smith int ierr; 405*9b94acceSBarry Smith SNES snes; 406*9b94acceSBarry Smith SNES_KSP_EW_ConvCtx *kctx; 407*9b94acceSBarry Smith *outsnes = 0; 408*9b94acceSBarry Smith PETSCHEADERCREATE(snes,_SNES,SNES_COOKIE,SNES_UNKNOWN_METHOD,comm); 409*9b94acceSBarry Smith PLogObjectCreate(snes); 410*9b94acceSBarry Smith snes->max_its = 50; 411*9b94acceSBarry Smith snes->max_funcs = 1000; 412*9b94acceSBarry Smith snes->norm = 0.0; 413*9b94acceSBarry Smith snes->rtol = 1.e-8; 414*9b94acceSBarry Smith snes->atol = 1.e-10; 415*9b94acceSBarry Smith snes->xtol = 1.e-8; 416*9b94acceSBarry Smith snes->trunctol = 1.e-12; 417*9b94acceSBarry Smith snes->nfuncs = 0; 418*9b94acceSBarry Smith snes->nfailures = 0; 419*9b94acceSBarry Smith snes->monitor = 0; 420*9b94acceSBarry Smith snes->data = 0; 421*9b94acceSBarry Smith snes->view = 0; 422*9b94acceSBarry Smith snes->computeumfunction = 0; 423*9b94acceSBarry Smith snes->umfunP = 0; 424*9b94acceSBarry Smith snes->fc = 0; 425*9b94acceSBarry Smith snes->deltatol = 1.e-12; 426*9b94acceSBarry Smith snes->fmin = -1.e30; 427*9b94acceSBarry Smith snes->method_class = type; 428*9b94acceSBarry Smith snes->set_method_called = 0; 429*9b94acceSBarry Smith snes->ksp_ewconv = 0; 430*9b94acceSBarry Smith 431*9b94acceSBarry Smith /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 432*9b94acceSBarry Smith kctx = PETSCNEW(SNES_KSP_EW_ConvCtx); CHKPTRQ(kctx); 433*9b94acceSBarry Smith snes->kspconvctx = (void*)kctx; 434*9b94acceSBarry Smith kctx->version = 2; 435*9b94acceSBarry Smith kctx->rtol_0 = .3; /* Eisenstat and Walker suggest rtol_0=.5, but 436*9b94acceSBarry Smith this was too large for some test cases */ 437*9b94acceSBarry Smith kctx->rtol_last = 0; 438*9b94acceSBarry Smith kctx->rtol_max = .9; 439*9b94acceSBarry Smith kctx->gamma = 1.0; 440*9b94acceSBarry Smith kctx->alpha2 = .5*(1.0 + sqrt(5.0)); 441*9b94acceSBarry Smith kctx->alpha = kctx->alpha2; 442*9b94acceSBarry Smith kctx->threshold = .1; 443*9b94acceSBarry Smith kctx->lresid_last = 0; 444*9b94acceSBarry Smith kctx->norm_last = 0; 445*9b94acceSBarry Smith 446*9b94acceSBarry Smith ierr = SLESCreate(comm,&snes->sles); CHKERRQ(ierr); 447*9b94acceSBarry Smith PLogObjectParent(snes,snes->sles) 448*9b94acceSBarry Smith *outsnes = snes; 449*9b94acceSBarry Smith return 0; 450*9b94acceSBarry Smith } 451*9b94acceSBarry Smith 452*9b94acceSBarry Smith /* --------------------------------------------------------------- */ 453*9b94acceSBarry Smith /*@C 454*9b94acceSBarry Smith SNESSetFunction - Sets the function evaluation routine and function 455*9b94acceSBarry Smith vector for use by the SNES routines in solving systems of nonlinear 456*9b94acceSBarry Smith equations. 457*9b94acceSBarry Smith 458*9b94acceSBarry Smith Input Parameters: 459*9b94acceSBarry Smith . snes - the SNES context 460*9b94acceSBarry Smith . func - function evaluation routine 461*9b94acceSBarry Smith . resid_neg - indicator whether func evaluates f or -f. 462*9b94acceSBarry Smith If resid_neg is nonzero, then func evaluates -f; otherwise, 463*9b94acceSBarry Smith func evaluates f. 464*9b94acceSBarry Smith . ctx - optional user-defined function context 465*9b94acceSBarry Smith . r - vector to store function value 466*9b94acceSBarry Smith 467*9b94acceSBarry Smith Calling sequence of func: 468*9b94acceSBarry Smith . func (SNES, Vec x, Vec f, void *ctx); 469*9b94acceSBarry Smith 470*9b94acceSBarry Smith . x - input vector 471*9b94acceSBarry Smith . f - function vector or its negative 472*9b94acceSBarry Smith . ctx - optional user-defined context for private data for the 473*9b94acceSBarry Smith function evaluation routine (may be null) 474*9b94acceSBarry Smith 475*9b94acceSBarry Smith Notes: 476*9b94acceSBarry Smith The Newton-like methods typically solve linear systems of the form 477*9b94acceSBarry Smith $ f'(x) x = -f(x), 478*9b94acceSBarry Smith $ where f'(x) denotes the Jacobian matrix and f(x) is the function. 479*9b94acceSBarry Smith By setting resid_neg = 1, the user can supply -f(x) directly. 480*9b94acceSBarry Smith 481*9b94acceSBarry Smith SNESSetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only. 482*9b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 483*9b94acceSBarry Smith SNESSetMinimizationFunction() and SNESSetGradient(); 484*9b94acceSBarry Smith 485*9b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 486*9b94acceSBarry Smith 487*9b94acceSBarry Smith .seealso: SNESGetFunction(), SNESSetJacobian(), SNESSetSolution() 488*9b94acceSBarry Smith @*/ 489*9b94acceSBarry Smith int SNESSetFunction( SNES snes, Vec r, int (*func)(SNES,Vec,Vec,void*), 490*9b94acceSBarry Smith void *ctx,int rneg) 491*9b94acceSBarry Smith { 492*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 493*9b94acceSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1, 494*9b94acceSBarry Smith "SNESSetFunction: Valid for SNES_NONLINEAR_EQUATIONS methods only"); 495*9b94acceSBarry Smith snes->computefunction = func; 496*9b94acceSBarry Smith snes->rsign = rneg; 497*9b94acceSBarry Smith snes->vec_func = snes->vec_func_always = r; 498*9b94acceSBarry Smith snes->funP = ctx; 499*9b94acceSBarry Smith return 0; 500*9b94acceSBarry Smith } 501*9b94acceSBarry Smith 502*9b94acceSBarry Smith /*@ 503*9b94acceSBarry Smith SNESComputeFunction - Computes the function that has been set with 504*9b94acceSBarry Smith SNESSetFunction(). 505*9b94acceSBarry Smith 506*9b94acceSBarry Smith Input Parameters: 507*9b94acceSBarry Smith . snes - the SNES context 508*9b94acceSBarry Smith . x - input vector 509*9b94acceSBarry Smith 510*9b94acceSBarry Smith Output Parameter: 511*9b94acceSBarry Smith . y - function vector or its negative, as set by SNESSetFunction() 512*9b94acceSBarry Smith 513*9b94acceSBarry Smith Notes: 514*9b94acceSBarry Smith SNESComputeFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only. 515*9b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 516*9b94acceSBarry Smith SNESComputeMinimizationFunction() and SNESComputeGradient(); 517*9b94acceSBarry Smith 518*9b94acceSBarry Smith .keywords: SNES, nonlinear, compute, function 519*9b94acceSBarry Smith 520*9b94acceSBarry Smith .seealso: SNESSetFunction() 521*9b94acceSBarry Smith @*/ 522*9b94acceSBarry Smith int SNESComputeFunction(SNES snes,Vec x, Vec y) 523*9b94acceSBarry Smith { 524*9b94acceSBarry Smith int ierr; 525*9b94acceSBarry Smith Scalar mone = -1.0; 526*9b94acceSBarry Smith 527*9b94acceSBarry Smith PLogEventBegin(SNES_FunctionEval,snes,x,y,0); 528*9b94acceSBarry Smith ierr = (*snes->computefunction)(snes,x,y,snes->funP); CHKERRQ(ierr); 529*9b94acceSBarry Smith if (!snes->rsign) { 530*9b94acceSBarry Smith ierr = VecScale(&mone,y); CHKERRQ(ierr); 531*9b94acceSBarry Smith } 532*9b94acceSBarry Smith PLogEventEnd(SNES_FunctionEval,snes,x,y,0); 533*9b94acceSBarry Smith return 0; 534*9b94acceSBarry Smith } 535*9b94acceSBarry Smith 536*9b94acceSBarry Smith /*@C 537*9b94acceSBarry Smith SNESSetMinimizationFunction - Sets the function evaluation routine for 538*9b94acceSBarry Smith unconstrained minimization. 539*9b94acceSBarry Smith 540*9b94acceSBarry Smith Input Parameters: 541*9b94acceSBarry Smith . snes - the SNES context 542*9b94acceSBarry Smith . func - function evaluation routine 543*9b94acceSBarry Smith . ctx - optional user-defined function context 544*9b94acceSBarry Smith 545*9b94acceSBarry Smith Calling sequence of func: 546*9b94acceSBarry Smith . func (SNES snes,Vec x,double *f,void *ctx); 547*9b94acceSBarry Smith 548*9b94acceSBarry Smith . x - input vector 549*9b94acceSBarry Smith . f - function 550*9b94acceSBarry Smith . ctx - optional user-defined context for private data for the 551*9b94acceSBarry Smith function evaluation routine (may be null) 552*9b94acceSBarry Smith 553*9b94acceSBarry Smith Notes: 554*9b94acceSBarry Smith SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 555*9b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 556*9b94acceSBarry Smith SNESSetFunction(). 557*9b94acceSBarry Smith 558*9b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimization, function 559*9b94acceSBarry Smith 560*9b94acceSBarry Smith .seealso: SNESGetMinimizationFunction(), SNESSetHessian(), SNESSetGradient(), 561*9b94acceSBarry Smith SNESSetSolution() 562*9b94acceSBarry Smith @*/ 563*9b94acceSBarry Smith int SNESSetMinimizationFunction(SNES snes,int (*func)(SNES,Vec,double*,void*), 564*9b94acceSBarry Smith void *ctx) 565*9b94acceSBarry Smith { 566*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 567*9b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 568*9b94acceSBarry Smith "SNESSetMinimizationFunction: Only for SNES_UNCONSTRAINED_MINIMIZATION methods"); 569*9b94acceSBarry Smith snes->computeumfunction = func; 570*9b94acceSBarry Smith snes->umfunP = ctx; 571*9b94acceSBarry Smith return 0; 572*9b94acceSBarry Smith } 573*9b94acceSBarry Smith 574*9b94acceSBarry Smith /*@ 575*9b94acceSBarry Smith SNESComputeMinimizationFunction - Computes the function that has been 576*9b94acceSBarry Smith set with SNESSetMinimizationFunction(). 577*9b94acceSBarry Smith 578*9b94acceSBarry Smith Input Parameters: 579*9b94acceSBarry Smith . snes - the SNES context 580*9b94acceSBarry Smith . x - input vector 581*9b94acceSBarry Smith 582*9b94acceSBarry Smith Output Parameter: 583*9b94acceSBarry Smith . y - function value 584*9b94acceSBarry Smith 585*9b94acceSBarry Smith Notes: 586*9b94acceSBarry Smith SNESComputeMinimizationFunction() is valid only for 587*9b94acceSBarry Smith SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 588*9b94acceSBarry Smith SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction(). 589*9b94acceSBarry Smith @*/ 590*9b94acceSBarry Smith int SNESComputeMinimizationFunction(SNES snes,Vec x,double *y) 591*9b94acceSBarry Smith { 592*9b94acceSBarry Smith int ierr; 593*9b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 594*9b94acceSBarry Smith "SNESComputeMinimizationFunction: Only for SNES_UNCONSTRAINED_MINIMIZATION methods"); 595*9b94acceSBarry Smith PLogEventBegin(SNES_MinimizationFunctionEval,snes,x,y,0); 596*9b94acceSBarry Smith ierr = (*snes->computeumfunction)(snes,x,y,snes->umfunP); CHKERRQ(ierr); 597*9b94acceSBarry Smith PLogEventEnd(SNES_MinimizationFunctionEval,snes,x,y,0); 598*9b94acceSBarry Smith return 0; 599*9b94acceSBarry Smith } 600*9b94acceSBarry Smith 601*9b94acceSBarry Smith /*@C 602*9b94acceSBarry Smith SNESSetGradient - Sets the gradient evaluation routine and gradient 603*9b94acceSBarry Smith vector for use by the SNES routines. 604*9b94acceSBarry Smith 605*9b94acceSBarry Smith Input Parameters: 606*9b94acceSBarry Smith . snes - the SNES context 607*9b94acceSBarry Smith . func - function evaluation routine 608*9b94acceSBarry Smith . ctx - optional user-defined function context 609*9b94acceSBarry Smith . r - vector to store gradient value 610*9b94acceSBarry Smith 611*9b94acceSBarry Smith Calling sequence of func: 612*9b94acceSBarry Smith . func (SNES, Vec x, Vec g, void *ctx); 613*9b94acceSBarry Smith 614*9b94acceSBarry Smith . x - input vector 615*9b94acceSBarry Smith . g - gradient vector 616*9b94acceSBarry Smith . ctx - optional user-defined context for private data for the 617*9b94acceSBarry Smith function evaluation routine (may be null) 618*9b94acceSBarry Smith 619*9b94acceSBarry Smith Notes: 620*9b94acceSBarry Smith SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 621*9b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 622*9b94acceSBarry Smith SNESSetFunction(). 623*9b94acceSBarry Smith 624*9b94acceSBarry Smith .keywords: SNES, nonlinear, set, function 625*9b94acceSBarry Smith 626*9b94acceSBarry Smith .seealso: SNESGetGradient(), SNESSetHessian(), SNESSetMinimizationFunction(), 627*9b94acceSBarry Smith SNESSetSolution() 628*9b94acceSBarry Smith @*/ 629*9b94acceSBarry Smith int SNESSetGradient(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*), 630*9b94acceSBarry Smith void *ctx) 631*9b94acceSBarry Smith { 632*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 633*9b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 634*9b94acceSBarry Smith "SNESSetGradient: Valid for SNES_UNCONSTRAINED_MINIMIZATION methods only"); 635*9b94acceSBarry Smith snes->computefunction = func; 636*9b94acceSBarry Smith snes->vec_func = snes->vec_func_always = r; 637*9b94acceSBarry Smith snes->funP = ctx; 638*9b94acceSBarry Smith return 0; 639*9b94acceSBarry Smith } 640*9b94acceSBarry Smith 641*9b94acceSBarry Smith /*@ 642*9b94acceSBarry Smith SNESComputeGradient - Computes the gradient that has been 643*9b94acceSBarry Smith set with SNESSetGradient(). 644*9b94acceSBarry Smith 645*9b94acceSBarry Smith Input Parameters: 646*9b94acceSBarry Smith . snes - the SNES context 647*9b94acceSBarry Smith . x - input vector 648*9b94acceSBarry Smith 649*9b94acceSBarry Smith Output Parameter: 650*9b94acceSBarry Smith . y - gradient vector 651*9b94acceSBarry Smith 652*9b94acceSBarry Smith Notes: 653*9b94acceSBarry Smith SNESComputeGradient() is valid only for 654*9b94acceSBarry Smith SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 655*9b94acceSBarry Smith SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction(). 656*9b94acceSBarry Smith @*/ 657*9b94acceSBarry Smith int SNESComputeGradient(SNES snes,Vec x, Vec y) 658*9b94acceSBarry Smith { 659*9b94acceSBarry Smith int ierr; 660*9b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 661*9b94acceSBarry Smith "SNESComputeGradient: Valid for SNES_UNCONSTRAINED_MINIMIZATION methods only"); 662*9b94acceSBarry Smith PLogEventBegin(SNES_GradientEval,snes,x,y,0); 663*9b94acceSBarry Smith ierr = (*snes->computefunction)(snes,x,y,snes->funP); CHKERRQ(ierr); 664*9b94acceSBarry Smith PLogEventEnd(SNES_GradientEval,snes,x,y,0); 665*9b94acceSBarry Smith return 0; 666*9b94acceSBarry Smith } 667*9b94acceSBarry Smith 668*9b94acceSBarry Smith int SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 669*9b94acceSBarry Smith { 670*9b94acceSBarry Smith int ierr; 671*9b94acceSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1, 672*9b94acceSBarry Smith "SNESComputeJacobian: Valid for SNES_NONLINEAR_EQUATIONS methods only"); 673*9b94acceSBarry Smith if (!snes->computejacobian) return 0; 674*9b94acceSBarry Smith PLogEventBegin(SNES_JacobianEval,snes,X,*A,*B); 675*9b94acceSBarry Smith ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP); CHKERRQ(ierr); 676*9b94acceSBarry Smith PLogEventEnd(SNES_JacobianEval,snes,X,*A,*B); 677*9b94acceSBarry Smith return 0; 678*9b94acceSBarry Smith } 679*9b94acceSBarry Smith 680*9b94acceSBarry Smith int SNESComputeHessian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 681*9b94acceSBarry Smith { 682*9b94acceSBarry Smith int ierr; 683*9b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 684*9b94acceSBarry Smith "SNESComputeHessian: Valid for SNES_UNCONSTRAINED_MINIMIZATION methods only"); 685*9b94acceSBarry Smith if (!snes->computejacobian) return 0; 686*9b94acceSBarry Smith PLogEventBegin(SNES_HessianEval,snes,X,*A,*B); 687*9b94acceSBarry Smith ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP); CHKERRQ(ierr); 688*9b94acceSBarry Smith PLogEventEnd(SNES_HessianEval,snes,X,*A,*B); 689*9b94acceSBarry Smith return 0; 690*9b94acceSBarry Smith } 691*9b94acceSBarry Smith 692*9b94acceSBarry Smith /*@C 693*9b94acceSBarry Smith SNESSetJacobian - Sets the function to compute Jacobian as well as the 694*9b94acceSBarry Smith location to store it. 695*9b94acceSBarry Smith 696*9b94acceSBarry Smith Input Parameters: 697*9b94acceSBarry Smith . snes - the SNES context 698*9b94acceSBarry Smith . A - Jacobian matrix 699*9b94acceSBarry Smith . B - preconditioner matrix (usually same as the Jacobian) 700*9b94acceSBarry Smith . func - Jacobian evaluation routine 701*9b94acceSBarry Smith . ctx - optional user-defined context for private data for the 702*9b94acceSBarry Smith Jacobian evaluation routine (may be null) 703*9b94acceSBarry Smith 704*9b94acceSBarry Smith Calling sequence of func: 705*9b94acceSBarry Smith . func (SNES,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 706*9b94acceSBarry Smith 707*9b94acceSBarry Smith . x - input vector 708*9b94acceSBarry Smith . A - Jacobian matrix 709*9b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 710*9b94acceSBarry Smith . flag - flag indicating information about matrix structure, 711*9b94acceSBarry Smith same as flag in SLESSetOperators() 712*9b94acceSBarry Smith . ctx - optional user-defined Jacobian context 713*9b94acceSBarry Smith 714*9b94acceSBarry Smith Notes: 715*9b94acceSBarry Smith The function func() takes Mat * as the matrix arguments rather than Mat. 716*9b94acceSBarry Smith This allows the Jacobian evaluation routine to replace A and/or B with a 717*9b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 718*9b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 719*9b94acceSBarry Smith throughout the global iterations. 720*9b94acceSBarry Smith 721*9b94acceSBarry Smith .keywords: SNES, nonlinear, set, Jacobian, matrix 722*9b94acceSBarry Smith 723*9b94acceSBarry Smith .seealso: SNESSetFunction(), SNESSetSolution() 724*9b94acceSBarry Smith @*/ 725*9b94acceSBarry Smith int SNESSetJacobian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*, 726*9b94acceSBarry Smith MatStructure*,void*),void *ctx) 727*9b94acceSBarry Smith { 728*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 729*9b94acceSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1, 730*9b94acceSBarry Smith "SNESSetJacobian: Valid for SNES_NONLINEAR_EQUATIONS methods only"); 731*9b94acceSBarry Smith snes->computejacobian = func; 732*9b94acceSBarry Smith snes->jacP = ctx; 733*9b94acceSBarry Smith snes->jacobian = A; 734*9b94acceSBarry Smith snes->jacobian_pre = B; 735*9b94acceSBarry Smith return 0; 736*9b94acceSBarry Smith } 737*9b94acceSBarry Smith /*@C 738*9b94acceSBarry Smith SNESSetHessian - Sets the function to compute Hessian as well as the 739*9b94acceSBarry Smith location to store it. 740*9b94acceSBarry Smith 741*9b94acceSBarry Smith Input Parameters: 742*9b94acceSBarry Smith . snes - the SNES context 743*9b94acceSBarry Smith . A - Hessian matrix 744*9b94acceSBarry Smith . B - preconditioner matrix (usually same as the Hessian) 745*9b94acceSBarry Smith . func - Jacobian evaluation routine 746*9b94acceSBarry Smith . ctx - optional user-defined context for private data for the 747*9b94acceSBarry Smith Hessian evaluation routine (may be null) 748*9b94acceSBarry Smith 749*9b94acceSBarry Smith Calling sequence of func: 750*9b94acceSBarry Smith . func (SNES,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 751*9b94acceSBarry Smith 752*9b94acceSBarry Smith . x - input vector 753*9b94acceSBarry Smith . A - Hessian matrix 754*9b94acceSBarry Smith . B - preconditioner matrix, usually the same as A 755*9b94acceSBarry Smith . flag - flag indicating information about matrix structure, 756*9b94acceSBarry Smith same as flag in SLESSetOperators() 757*9b94acceSBarry Smith . ctx - optional user-defined Hessian context 758*9b94acceSBarry Smith 759*9b94acceSBarry Smith Notes: 760*9b94acceSBarry Smith The function func() takes Mat * as the matrix arguments rather than Mat. 761*9b94acceSBarry Smith This allows the Hessian evaluation routine to replace A and/or B with a 762*9b94acceSBarry Smith completely new new matrix structure (not just different matrix elements) 763*9b94acceSBarry Smith when appropriate, for instance, if the nonzero structure is changing 764*9b94acceSBarry Smith throughout the global iterations. 765*9b94acceSBarry Smith 766*9b94acceSBarry Smith .keywords: SNES, nonlinear, set, Hessian, matrix 767*9b94acceSBarry Smith 768*9b94acceSBarry Smith .seealso: SNESSetMinimizationFunction(), SNESSetSolution(), SNESSetGradient() 769*9b94acceSBarry Smith @*/ 770*9b94acceSBarry Smith int SNESSetHessian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*, 771*9b94acceSBarry Smith MatStructure*,void*),void *ctx) 772*9b94acceSBarry Smith { 773*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 774*9b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 775*9b94acceSBarry Smith "SNESSetHessian: Valid for SNES_UNCONSTRAINED_MINIMIZATION methods only"); 776*9b94acceSBarry Smith snes->computejacobian = func; 777*9b94acceSBarry Smith snes->jacP = ctx; 778*9b94acceSBarry Smith snes->jacobian = A; 779*9b94acceSBarry Smith snes->jacobian_pre = B; 780*9b94acceSBarry Smith return 0; 781*9b94acceSBarry Smith } 782*9b94acceSBarry Smith 783*9b94acceSBarry Smith /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 784*9b94acceSBarry Smith 785*9b94acceSBarry Smith /*@ 786*9b94acceSBarry Smith SNESSetUp - Sets up the internal data structures for the later use 787*9b94acceSBarry Smith of a nonlinear solver. Call SNESSetUp() after calling SNESCreate() 788*9b94acceSBarry Smith and optional routines of the form SNESSetXXX(), but before calling 789*9b94acceSBarry Smith SNESSolve(). 790*9b94acceSBarry Smith 791*9b94acceSBarry Smith Input Parameter: 792*9b94acceSBarry Smith . snes - the SNES context 793*9b94acceSBarry Smith 794*9b94acceSBarry Smith .keywords: SNES, nonlinear, setup 795*9b94acceSBarry Smith 796*9b94acceSBarry Smith .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 797*9b94acceSBarry Smith @*/ 798*9b94acceSBarry Smith int SNESSetUp(SNES snes) 799*9b94acceSBarry Smith { 800*9b94acceSBarry Smith int ierr; 801*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 802*9b94acceSBarry Smith if (!snes->vec_sol) 803*9b94acceSBarry Smith SETERRQ(1,"SNESSetUp: Must call SNESSetSolution() to set solution vector"); 804*9b94acceSBarry Smith 805*9b94acceSBarry Smith if ((snes->method_class == SNES_NONLINEAR_EQUATIONS)) { 806*9b94acceSBarry Smith if (!snes->set_method_called) 807*9b94acceSBarry Smith {ierr = SNESSetMethod(snes,SNES_EQ_NLS); CHKERRQ(ierr);} 808*9b94acceSBarry Smith if (!snes->vec_func) SETERRQ(1, 809*9b94acceSBarry Smith "SNESSetUp: Must call SNESSetFunction() to set function vector"); 810*9b94acceSBarry Smith if (!snes->computefunction) SETERRQ(1, 811*9b94acceSBarry Smith "SNESSetUp: Must call SNESSetFunction() to set function routine"); 812*9b94acceSBarry Smith if (!snes->jacobian) SETERRQ(1, 813*9b94acceSBarry Smith "SNESSetUp: Must call SNESSetJacobian() to set Jacobian matrix"); 814*9b94acceSBarry Smith } else if ((snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION)) { 815*9b94acceSBarry Smith if (!snes->set_method_called) 816*9b94acceSBarry Smith {ierr = SNESSetMethod(snes,SNES_UM_NTR); CHKERRQ(ierr);} 817*9b94acceSBarry Smith if (!snes->vec_func) SETERRQ(1, 818*9b94acceSBarry Smith "SNESSetUp: Must call SNESSetGradient() to set gradient vector"); 819*9b94acceSBarry Smith if (!snes->computefunction) SETERRQ(1, 820*9b94acceSBarry Smith "SNESSetUp: Must call SNESSetGradient() to set gradient routine"); 821*9b94acceSBarry Smith if (!snes->computeumfunction) SETERRQ(1, 822*9b94acceSBarry Smith "SNESSetUp: Must call SNESSetMinimizationFunction() to set routine"); 823*9b94acceSBarry Smith if (!snes->jacobian) SETERRQ(1, 824*9b94acceSBarry Smith "SNESSetUp: Must call SNESSetHessian() to set Hessian matrix"); 825*9b94acceSBarry Smith } else SETERRQ(1,"SNESSetUp: Unknown method class"); 826*9b94acceSBarry Smith if (snes->setup) return (*snes->setup)(snes); 827*9b94acceSBarry Smith else return 0; 828*9b94acceSBarry Smith } 829*9b94acceSBarry Smith 830*9b94acceSBarry Smith /*@C 831*9b94acceSBarry Smith SNESDestroy - Destroys the nonlinear solver context that was created 832*9b94acceSBarry Smith with SNESCreate(). 833*9b94acceSBarry Smith 834*9b94acceSBarry Smith Input Parameter: 835*9b94acceSBarry Smith . snes - the SNES context 836*9b94acceSBarry Smith 837*9b94acceSBarry Smith .keywords: SNES, nonlinear, destroy 838*9b94acceSBarry Smith 839*9b94acceSBarry Smith .seealso: SNESCreate(), SNESSetUp(), SNESSolve() 840*9b94acceSBarry Smith @*/ 841*9b94acceSBarry Smith int SNESDestroy(SNES snes) 842*9b94acceSBarry Smith { 843*9b94acceSBarry Smith int ierr; 844*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 845*9b94acceSBarry Smith ierr = (*(snes)->destroy)((PetscObject)snes); CHKERRQ(ierr); 846*9b94acceSBarry Smith if (snes->kspconvctx) PETSCFREE(snes->kspconvctx); 847*9b94acceSBarry Smith if (snes->mfshell) MatDestroy(snes->mfshell); 848*9b94acceSBarry Smith ierr = SLESDestroy(snes->sles); CHKERRQ(ierr); 849*9b94acceSBarry Smith PLogObjectDestroy((PetscObject)snes); 850*9b94acceSBarry Smith PETSCHEADERDESTROY((PetscObject)snes); 851*9b94acceSBarry Smith return 0; 852*9b94acceSBarry Smith } 853*9b94acceSBarry Smith 854*9b94acceSBarry Smith /* ----------- Routines to set solver parameters ---------- */ 855*9b94acceSBarry Smith 856*9b94acceSBarry Smith /*@ 857*9b94acceSBarry Smith SNESSetMaxIterations - Sets the maximum number of global iterations to use. 858*9b94acceSBarry Smith 859*9b94acceSBarry Smith Input Parameters: 860*9b94acceSBarry Smith . snes - the SNES context 861*9b94acceSBarry Smith . maxits - maximum number of iterations to use 862*9b94acceSBarry Smith 863*9b94acceSBarry Smith Options Database Key: 864*9b94acceSBarry Smith $ -snes_max_it maxits 865*9b94acceSBarry Smith 866*9b94acceSBarry Smith Note: 867*9b94acceSBarry Smith The default maximum number of iterations is 50. 868*9b94acceSBarry Smith 869*9b94acceSBarry Smith .keywords: SNES, nonlinear, set, maximum, iterations 870*9b94acceSBarry Smith 871*9b94acceSBarry Smith .seealso: SNESSetMaxFunctionEvaluations() 872*9b94acceSBarry Smith @*/ 873*9b94acceSBarry Smith int SNESSetMaxIterations(SNES snes,int maxits) 874*9b94acceSBarry Smith { 875*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 876*9b94acceSBarry Smith snes->max_its = maxits; 877*9b94acceSBarry Smith return 0; 878*9b94acceSBarry Smith } 879*9b94acceSBarry Smith 880*9b94acceSBarry Smith /*@ 881*9b94acceSBarry Smith SNESSetMaxFunctionEvaluations - Sets the maximum number of function 882*9b94acceSBarry Smith evaluations to use. 883*9b94acceSBarry Smith 884*9b94acceSBarry Smith Input Parameters: 885*9b94acceSBarry Smith . snes - the SNES context 886*9b94acceSBarry Smith . maxf - maximum number of function evaluations 887*9b94acceSBarry Smith 888*9b94acceSBarry Smith Options Database Key: 889*9b94acceSBarry Smith $ -snes_max_funcs maxf 890*9b94acceSBarry Smith 891*9b94acceSBarry Smith Note: 892*9b94acceSBarry Smith The default maximum number of function evaluations is 1000. 893*9b94acceSBarry Smith 894*9b94acceSBarry Smith .keywords: SNES, nonlinear, set, maximum, function, evaluations 895*9b94acceSBarry Smith 896*9b94acceSBarry Smith .seealso: SNESSetMaxIterations() 897*9b94acceSBarry Smith @*/ 898*9b94acceSBarry Smith int SNESSetMaxFunctionEvaluations(SNES snes,int maxf) 899*9b94acceSBarry Smith { 900*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 901*9b94acceSBarry Smith snes->max_funcs = maxf; 902*9b94acceSBarry Smith return 0; 903*9b94acceSBarry Smith } 904*9b94acceSBarry Smith 905*9b94acceSBarry Smith /*@ 906*9b94acceSBarry Smith SNESSetRelativeTolerance - Sets the relative convergence tolerance. 907*9b94acceSBarry Smith 908*9b94acceSBarry Smith Input Parameters: 909*9b94acceSBarry Smith . snes - the SNES context 910*9b94acceSBarry Smith . rtol - tolerance 911*9b94acceSBarry Smith 912*9b94acceSBarry Smith Options Database Key: 913*9b94acceSBarry Smith $ -snes_rtol tol 914*9b94acceSBarry Smith 915*9b94acceSBarry Smith .keywords: SNES, nonlinear, set, relative, convergence, tolerance 916*9b94acceSBarry Smith 917*9b94acceSBarry Smith .seealso: SNESSetAbsoluteTolerance(), SNESSetSolutionTolerance(), 918*9b94acceSBarry Smith SNESSetTruncationTolerance() 919*9b94acceSBarry Smith @*/ 920*9b94acceSBarry Smith int SNESSetRelativeTolerance(SNES snes,double rtol) 921*9b94acceSBarry Smith { 922*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 923*9b94acceSBarry Smith snes->rtol = rtol; 924*9b94acceSBarry Smith return 0; 925*9b94acceSBarry Smith } 926*9b94acceSBarry Smith 927*9b94acceSBarry Smith /*@ 928*9b94acceSBarry Smith SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 929*9b94acceSBarry Smith 930*9b94acceSBarry Smith Input Parameters: 931*9b94acceSBarry Smith . snes - the SNES context 932*9b94acceSBarry Smith . tol - tolerance 933*9b94acceSBarry Smith 934*9b94acceSBarry Smith Options Database Key: 935*9b94acceSBarry Smith $ -snes_trtol tol 936*9b94acceSBarry Smith 937*9b94acceSBarry Smith .keywords: SNES, nonlinear, set, trust region, tolerance 938*9b94acceSBarry Smith 939*9b94acceSBarry Smith .seealso: SNESSetAbsoluteTolerance(), SNESSetSolutionTolerance(), 940*9b94acceSBarry Smith SNESSetTruncationTolerance() 941*9b94acceSBarry Smith @*/ 942*9b94acceSBarry Smith int SNESSetTrustRegionTolerance(SNES snes,double tol) 943*9b94acceSBarry Smith { 944*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 945*9b94acceSBarry Smith snes->deltatol = tol; 946*9b94acceSBarry Smith return 0; 947*9b94acceSBarry Smith } 948*9b94acceSBarry Smith 949*9b94acceSBarry Smith /*@ 950*9b94acceSBarry Smith SNESSetAbsoluteTolerance - Sets the absolute convergence tolerance. 951*9b94acceSBarry Smith 952*9b94acceSBarry Smith Input Parameters: 953*9b94acceSBarry Smith . snes - the SNES context 954*9b94acceSBarry Smith . atol - tolerance 955*9b94acceSBarry Smith 956*9b94acceSBarry Smith Options Database Key: 957*9b94acceSBarry Smith $ -snes_atol tol 958*9b94acceSBarry Smith 959*9b94acceSBarry Smith .keywords: SNES, nonlinear, set, absolute, convergence, tolerance 960*9b94acceSBarry Smith 961*9b94acceSBarry Smith .seealso: SNESSetRelativeTolerance(), SNESSetSolutionTolerance(), 962*9b94acceSBarry Smith SNESSetTruncationTolerance() 963*9b94acceSBarry Smith @*/ 964*9b94acceSBarry Smith int SNESSetAbsoluteTolerance(SNES snes,double atol) 965*9b94acceSBarry Smith { 966*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 967*9b94acceSBarry Smith snes->atol = atol; 968*9b94acceSBarry Smith return 0; 969*9b94acceSBarry Smith } 970*9b94acceSBarry Smith 971*9b94acceSBarry Smith /*@ 972*9b94acceSBarry Smith SNESSetTruncationTolerance - Sets the tolerance that may be used by the 973*9b94acceSBarry Smith step routines to control the accuracy of the step computation. 974*9b94acceSBarry Smith 975*9b94acceSBarry Smith Input Parameters: 976*9b94acceSBarry Smith . snes - the SNES context 977*9b94acceSBarry Smith . tol - tolerance 978*9b94acceSBarry Smith 979*9b94acceSBarry Smith Options Database Key: 980*9b94acceSBarry Smith $ -snes_ttol tol 981*9b94acceSBarry Smith 982*9b94acceSBarry Smith Notes: 983*9b94acceSBarry Smith If the step computation involves an application of the inverse 984*9b94acceSBarry Smith Jacobian (or Hessian), this parameter may be used to control the 985*9b94acceSBarry Smith accuracy of that application. In particular, this tolerance is used 986*9b94acceSBarry Smith by SNESKSPDefaultConverged() and SNESKSPQuadraticConverged() to determine 987*9b94acceSBarry Smith the minimum convergence tolerance for the iterative linear solvers. 988*9b94acceSBarry Smith 989*9b94acceSBarry Smith .keywords: SNES, nonlinear, set, truncation, tolerance 990*9b94acceSBarry Smith 991*9b94acceSBarry Smith .seealso: SNESSetRelativeTolerance(), SNESSetSolutionTolerance(), 992*9b94acceSBarry Smith SNESSetAbsoluteTolerance() 993*9b94acceSBarry Smith @*/ 994*9b94acceSBarry Smith int SNESSetTruncationTolerance(SNES snes,double tol) 995*9b94acceSBarry Smith { 996*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 997*9b94acceSBarry Smith snes->trunctol = tol; 998*9b94acceSBarry Smith return 0; 999*9b94acceSBarry Smith } 1000*9b94acceSBarry Smith 1001*9b94acceSBarry Smith /*@ 1002*9b94acceSBarry Smith SNESSetSolutionTolerance - Sets the convergence tolerance in terms of 1003*9b94acceSBarry Smith the norm of the change in the solution between steps. 1004*9b94acceSBarry Smith 1005*9b94acceSBarry Smith Input Parameters: 1006*9b94acceSBarry Smith . snes - the SNES context 1007*9b94acceSBarry Smith . tol - tolerance 1008*9b94acceSBarry Smith 1009*9b94acceSBarry Smith Options Database Key: 1010*9b94acceSBarry Smith $ -snes_stol tol 1011*9b94acceSBarry Smith 1012*9b94acceSBarry Smith .keywords: SNES, nonlinear, set, solution, tolerance 1013*9b94acceSBarry Smith 1014*9b94acceSBarry Smith .seealso: SNESSetTruncationTolerance(), SNESSetRelativeTolerance(), 1015*9b94acceSBarry Smith SNESSetAbsoluteTolerance() 1016*9b94acceSBarry Smith @*/ 1017*9b94acceSBarry Smith int SNESSetSolutionTolerance( SNES snes, double tol ) 1018*9b94acceSBarry Smith { 1019*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 1020*9b94acceSBarry Smith snes->xtol = tol; 1021*9b94acceSBarry Smith return 0; 1022*9b94acceSBarry Smith } 1023*9b94acceSBarry Smith 1024*9b94acceSBarry Smith /*@ 1025*9b94acceSBarry Smith SNESSetMinFunctionTolerance - Sets the minimum allowable function tolerance 1026*9b94acceSBarry Smith for unconstrained minimization solvers. 1027*9b94acceSBarry Smith 1028*9b94acceSBarry Smith Input Parameters: 1029*9b94acceSBarry Smith . snes - the SNES context 1030*9b94acceSBarry Smith . ftol - minimum function tolerance 1031*9b94acceSBarry Smith 1032*9b94acceSBarry Smith Options Database Key: 1033*9b94acceSBarry Smith $ -snes_fmin ftol 1034*9b94acceSBarry Smith 1035*9b94acceSBarry Smith Note: 1036*9b94acceSBarry Smith SNESSetMinFunctionTolerance() is valid for SNES_UNCONSTRAINED_MINIMIZATION 1037*9b94acceSBarry Smith methods only. 1038*9b94acceSBarry Smith 1039*9b94acceSBarry Smith .keywords: SNES, nonlinear, set, minimum, convergence, function, tolerance 1040*9b94acceSBarry Smith 1041*9b94acceSBarry Smith .seealso: SNESSetRelativeTolerance(), SNESSetSolutionTolerance(), 1042*9b94acceSBarry Smith SNESSetTruncationTolerance() 1043*9b94acceSBarry Smith @*/ 1044*9b94acceSBarry Smith int SNESSetMinFunctionTolerance(SNES snes,double ftol) 1045*9b94acceSBarry Smith { 1046*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 1047*9b94acceSBarry Smith snes->fmin = ftol; 1048*9b94acceSBarry Smith return 0; 1049*9b94acceSBarry Smith } 1050*9b94acceSBarry Smith 1051*9b94acceSBarry Smith 1052*9b94acceSBarry Smith 1053*9b94acceSBarry Smith /* ---------- Routines to set various aspects of nonlinear solver --------- */ 1054*9b94acceSBarry Smith 1055*9b94acceSBarry Smith /*@C 1056*9b94acceSBarry Smith SNESSetSolution - Sets the initial guess routine and solution vector 1057*9b94acceSBarry Smith for use by the SNES routines. 1058*9b94acceSBarry Smith 1059*9b94acceSBarry Smith Input Parameters: 1060*9b94acceSBarry Smith . snes - the SNES context 1061*9b94acceSBarry Smith . x - the solution vector 1062*9b94acceSBarry Smith . func - optional routine to compute an initial guess (may be null) 1063*9b94acceSBarry Smith . ctx - optional user-defined context for private data for the 1064*9b94acceSBarry Smith initial guess routine (may be null) 1065*9b94acceSBarry Smith 1066*9b94acceSBarry Smith Calling sequence of func: 1067*9b94acceSBarry Smith int guess(Vec x, void *ctx) 1068*9b94acceSBarry Smith 1069*9b94acceSBarry Smith . x - input vector 1070*9b94acceSBarry Smith . ctx - optional user-defined initial guess context 1071*9b94acceSBarry Smith 1072*9b94acceSBarry Smith Note: 1073*9b94acceSBarry Smith If no initial guess routine is indicated, an initial guess of zero 1074*9b94acceSBarry Smith will be used. 1075*9b94acceSBarry Smith 1076*9b94acceSBarry Smith .keywords: SNES, nonlinear, set, solution, initial guess 1077*9b94acceSBarry Smith 1078*9b94acceSBarry Smith .seealso: SNESGetSolution(), SNESSetJacobian(), SNESSetFunction() 1079*9b94acceSBarry Smith @*/ 1080*9b94acceSBarry Smith int SNESSetSolution(SNES snes,Vec x,int (*func)(SNES,Vec,void*),void *ctx) 1081*9b94acceSBarry Smith { 1082*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 1083*9b94acceSBarry Smith snes->vec_sol = snes->vec_sol_always = x; 1084*9b94acceSBarry Smith snes->computeinitialguess = func; 1085*9b94acceSBarry Smith snes->gusP = ctx; 1086*9b94acceSBarry Smith return 0; 1087*9b94acceSBarry Smith } 1088*9b94acceSBarry Smith 1089*9b94acceSBarry Smith /* ------------ Routines to set performance monitoring options ----------- */ 1090*9b94acceSBarry Smith 1091*9b94acceSBarry Smith /*@C 1092*9b94acceSBarry Smith SNESSetMonitor - Sets the function that is to be used at every 1093*9b94acceSBarry Smith iteration of the nonlinear solver to display the iteration's 1094*9b94acceSBarry Smith progress. 1095*9b94acceSBarry Smith 1096*9b94acceSBarry Smith Input Parameters: 1097*9b94acceSBarry Smith . snes - the SNES context 1098*9b94acceSBarry Smith . func - monitoring routine 1099*9b94acceSBarry Smith . mctx - optional user-defined context for private data for the 1100*9b94acceSBarry Smith monitor routine (may be null) 1101*9b94acceSBarry Smith 1102*9b94acceSBarry Smith Calling sequence of func: 1103*9b94acceSBarry Smith int func((SNES snes,int its, Vec x,Vec f, 1104*9b94acceSBarry Smith double norm,void *mctx) 1105*9b94acceSBarry Smith 1106*9b94acceSBarry Smith $ snes - the SNES context 1107*9b94acceSBarry Smith $ its - iteration number 1108*9b94acceSBarry Smith $ mctx - optional monitoring context 1109*9b94acceSBarry Smith $ 1110*9b94acceSBarry Smith $ SNES_NONLINEAR_EQUATIONS methods: 1111*9b94acceSBarry Smith $ norm - 2-norm function value (may be estimated) 1112*9b94acceSBarry Smith $ 1113*9b94acceSBarry Smith $ SNES_UNCONSTRAINED_MINIMIZATION methods: 1114*9b94acceSBarry Smith $ norm - 2-norm gradient value (may be estimated) 1115*9b94acceSBarry Smith 1116*9b94acceSBarry Smith .keywords: SNES, nonlinear, set, monitor 1117*9b94acceSBarry Smith 1118*9b94acceSBarry Smith .seealso: SNESDefaultMonitor() 1119*9b94acceSBarry Smith @*/ 1120*9b94acceSBarry Smith int SNESSetMonitor( SNES snes, int (*func)(SNES,int,double,void*), 1121*9b94acceSBarry Smith void *mctx ) 1122*9b94acceSBarry Smith { 1123*9b94acceSBarry Smith snes->monitor = func; 1124*9b94acceSBarry Smith snes->monP = (void*)mctx; 1125*9b94acceSBarry Smith return 0; 1126*9b94acceSBarry Smith } 1127*9b94acceSBarry Smith 1128*9b94acceSBarry Smith /*@C 1129*9b94acceSBarry Smith SNESSetConvergenceTest - Sets the function that is to be used 1130*9b94acceSBarry Smith to test for convergence of the nonlinear iterative solution. 1131*9b94acceSBarry Smith 1132*9b94acceSBarry Smith Input Parameters: 1133*9b94acceSBarry Smith . snes - the SNES context 1134*9b94acceSBarry Smith . func - routine to test for convergence 1135*9b94acceSBarry Smith . cctx - optional context for private data for the convergence routine 1136*9b94acceSBarry Smith (may be null) 1137*9b94acceSBarry Smith 1138*9b94acceSBarry Smith Calling sequence of func: 1139*9b94acceSBarry Smith int func (SNES snes,double xnorm,double gnorm, 1140*9b94acceSBarry Smith double f,void *cctx) 1141*9b94acceSBarry Smith 1142*9b94acceSBarry Smith $ snes - the SNES context 1143*9b94acceSBarry Smith $ cctx - optional convergence context 1144*9b94acceSBarry Smith $ xnorm - 2-norm of current iterate 1145*9b94acceSBarry Smith $ 1146*9b94acceSBarry Smith $ SNES_NONLINEAR_EQUATIONS methods: 1147*9b94acceSBarry Smith $ gnorm - 2-norm of current step 1148*9b94acceSBarry Smith $ f - 2-norm of function 1149*9b94acceSBarry Smith $ 1150*9b94acceSBarry Smith $ SNES_UNCONSTRAINED_MINIMIZATION methods: 1151*9b94acceSBarry Smith $ gnorm - 2-norm of current gradient 1152*9b94acceSBarry Smith $ f - function value 1153*9b94acceSBarry Smith 1154*9b94acceSBarry Smith .keywords: SNES, nonlinear, set, convergence, test 1155*9b94acceSBarry Smith 1156*9b94acceSBarry Smith .seealso: SNESDefaultConverged() 1157*9b94acceSBarry Smith @*/ 1158*9b94acceSBarry Smith int SNESSetConvergenceTest(SNES snes, 1159*9b94acceSBarry Smith int (*func)(SNES,double,double,double,void*),void *cctx) 1160*9b94acceSBarry Smith { 1161*9b94acceSBarry Smith (snes)->converged = func; 1162*9b94acceSBarry Smith (snes)->cnvP = cctx; 1163*9b94acceSBarry Smith return 0; 1164*9b94acceSBarry Smith } 1165*9b94acceSBarry Smith 1166*9b94acceSBarry Smith /* 1167*9b94acceSBarry Smith SNESScaleStep_Private - Scales a step so that its length is less than the 1168*9b94acceSBarry Smith positive parameter delta. 1169*9b94acceSBarry Smith 1170*9b94acceSBarry Smith Input Parameters: 1171*9b94acceSBarry Smith . snes - the SNES context 1172*9b94acceSBarry Smith . y - approximate solution of linear system 1173*9b94acceSBarry Smith . fnorm - 2-norm of current function 1174*9b94acceSBarry Smith . delta - trust region size 1175*9b94acceSBarry Smith 1176*9b94acceSBarry Smith Output Parameters: 1177*9b94acceSBarry Smith . gpnorm - predicted function norm at the new point, assuming local 1178*9b94acceSBarry Smith linearization. The value is zero if the step lies within the trust 1179*9b94acceSBarry Smith region, and exceeds zero otherwise. 1180*9b94acceSBarry Smith . ynorm - 2-norm of the step 1181*9b94acceSBarry Smith 1182*9b94acceSBarry Smith Note: 1183*9b94acceSBarry Smith For non-trust region methods such as SNES_NLS, the parameter delta 1184*9b94acceSBarry Smith is set to be the maximum allowable step size. 1185*9b94acceSBarry Smith 1186*9b94acceSBarry Smith .keywords: SNES, nonlinear, scale, step 1187*9b94acceSBarry Smith */ 1188*9b94acceSBarry Smith int SNESScaleStep_Private(SNES snes,Vec y,double *fnorm,double *delta, 1189*9b94acceSBarry Smith double *gpnorm,double *ynorm) 1190*9b94acceSBarry Smith { 1191*9b94acceSBarry Smith double norm; 1192*9b94acceSBarry Smith Scalar cnorm; 1193*9b94acceSBarry Smith VecNorm(y, &norm ); 1194*9b94acceSBarry Smith if (norm > *delta) { 1195*9b94acceSBarry Smith norm = *delta/norm; 1196*9b94acceSBarry Smith *gpnorm = (1.0 - norm)*(*fnorm); 1197*9b94acceSBarry Smith cnorm = norm; 1198*9b94acceSBarry Smith VecScale( &cnorm, y ); 1199*9b94acceSBarry Smith *ynorm = *delta; 1200*9b94acceSBarry Smith } else { 1201*9b94acceSBarry Smith *gpnorm = 0.0; 1202*9b94acceSBarry Smith *ynorm = norm; 1203*9b94acceSBarry Smith } 1204*9b94acceSBarry Smith return 0; 1205*9b94acceSBarry Smith } 1206*9b94acceSBarry Smith 1207*9b94acceSBarry Smith /*@ 1208*9b94acceSBarry Smith SNESSolve - Solves a nonlinear system. Call SNESSolve after calling 1209*9b94acceSBarry Smith SNESCreate(), optional routines of the form SNESSetXXX(), and SNESSetUp(). 1210*9b94acceSBarry Smith 1211*9b94acceSBarry Smith Input Parameter: 1212*9b94acceSBarry Smith . snes - the SNES context 1213*9b94acceSBarry Smith 1214*9b94acceSBarry Smith Output Parameter: 1215*9b94acceSBarry Smith its - number of iterations until termination 1216*9b94acceSBarry Smith 1217*9b94acceSBarry Smith .keywords: SNES, nonlinear, solve 1218*9b94acceSBarry Smith 1219*9b94acceSBarry Smith .seealso: SNESCreate(), SNESSetUp(), SNESDestroy() 1220*9b94acceSBarry Smith @*/ 1221*9b94acceSBarry Smith int SNESSolve(SNES snes,int *its) 1222*9b94acceSBarry Smith { 1223*9b94acceSBarry Smith int ierr; 1224*9b94acceSBarry Smith PLogEventBegin(SNES_Solve,snes,0,0,0); 1225*9b94acceSBarry Smith ierr = (*(snes)->solve)( snes,its ); CHKERRQ(ierr); 1226*9b94acceSBarry Smith PLogEventEnd(SNES_Solve,snes,0,0,0); 1227*9b94acceSBarry Smith if (OptionsHasName(0,"-snes_view")) { 1228*9b94acceSBarry Smith SNESView(snes,STDOUT_VIEWER_WORLD); CHKERRQ(ierr); 1229*9b94acceSBarry Smith } 1230*9b94acceSBarry Smith return 0; 1231*9b94acceSBarry Smith } 1232*9b94acceSBarry Smith 1233*9b94acceSBarry Smith /* --------- Internal routines for SNES Package --------- */ 1234*9b94acceSBarry Smith 1235*9b94acceSBarry Smith /* 1236*9b94acceSBarry Smith SNESComputeInitialGuess - Manages computation of initial approximation. 1237*9b94acceSBarry Smith */ 1238*9b94acceSBarry Smith int SNESComputeInitialGuess( SNES snes,Vec x ) 1239*9b94acceSBarry Smith { 1240*9b94acceSBarry Smith int ierr; 1241*9b94acceSBarry Smith Scalar zero = 0.0; 1242*9b94acceSBarry Smith if (snes->computeinitialguess) { 1243*9b94acceSBarry Smith ierr = (*snes->computeinitialguess)(snes, x, snes->gusP); CHKERRQ(ierr); 1244*9b94acceSBarry Smith } 1245*9b94acceSBarry Smith else { 1246*9b94acceSBarry Smith ierr = VecSet(&zero,x); CHKERRQ(ierr); 1247*9b94acceSBarry Smith } 1248*9b94acceSBarry Smith return 0; 1249*9b94acceSBarry Smith } 1250*9b94acceSBarry Smith 1251*9b94acceSBarry Smith /* ------------------------------------------------------------------ */ 1252*9b94acceSBarry Smith 1253*9b94acceSBarry Smith NRList *__NLList; 1254*9b94acceSBarry Smith 1255*9b94acceSBarry Smith /*@ 1256*9b94acceSBarry Smith SNESSetMethod - Sets the method for the nonlinear solver. 1257*9b94acceSBarry Smith 1258*9b94acceSBarry Smith Input Parameters: 1259*9b94acceSBarry Smith . snes - the SNES context 1260*9b94acceSBarry Smith . method - a known method 1261*9b94acceSBarry Smith 1262*9b94acceSBarry Smith Notes: 1263*9b94acceSBarry Smith See "petsc/include/snes.h" for available methods (for instance) 1264*9b94acceSBarry Smith $ Systems of nonlinear equations: 1265*9b94acceSBarry Smith $ SNES_NLS - Newton's method with line search 1266*9b94acceSBarry Smith $ SNES_NTR - Newton's method with trust region 1267*9b94acceSBarry Smith $ Unconstrained minimization: 1268*9b94acceSBarry Smith $ SNES_UM_NTR - Newton's method with trust region 1269*9b94acceSBarry Smith 1270*9b94acceSBarry Smith Options Database Command: 1271*9b94acceSBarry Smith $ -snes_method <method> 1272*9b94acceSBarry Smith $ Use -help for a list of available methods 1273*9b94acceSBarry Smith $ (for instance, ls or tr) 1274*9b94acceSBarry Smith @*/ 1275*9b94acceSBarry Smith int SNESSetMethod( SNES snes, SNESMethod method) 1276*9b94acceSBarry Smith { 1277*9b94acceSBarry Smith int (*r)(SNES); 1278*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 1279*9b94acceSBarry Smith /* Get the function pointers for the iterative method requested */ 1280*9b94acceSBarry Smith if (!__NLList) {SNESRegisterAll();} 1281*9b94acceSBarry Smith if (!__NLList) {SETERRQ(1,"SNESSetMethod: Could not acquire methods");} 1282*9b94acceSBarry Smith r = (int (*)(SNES))NRFindRoutine( __NLList, (int)method, (char *)0 ); 1283*9b94acceSBarry Smith if (!r) {SETERRQ(1,"SNESSetMethod: Unknown SNES method");} 1284*9b94acceSBarry Smith if (snes->data) PETSCFREE(snes->data); 1285*9b94acceSBarry Smith snes->set_method_called = 1; 1286*9b94acceSBarry Smith return (*r)(snes); 1287*9b94acceSBarry Smith } 1288*9b94acceSBarry Smith 1289*9b94acceSBarry Smith /* --------------------------------------------------------------------- */ 1290*9b94acceSBarry Smith /*@C 1291*9b94acceSBarry Smith SNESRegister - Adds the method to the nonlinear solver package, given 1292*9b94acceSBarry Smith a function pointer and a nonlinear solver name of the type SNESMethod. 1293*9b94acceSBarry Smith 1294*9b94acceSBarry Smith Input Parameters: 1295*9b94acceSBarry Smith . name - for instance SNES_NLS, SNES_NTR, ... 1296*9b94acceSBarry Smith . sname - corfunPonding string for name 1297*9b94acceSBarry Smith . create - routine to create method context 1298*9b94acceSBarry Smith 1299*9b94acceSBarry Smith .keywords: SNES, nonlinear, register 1300*9b94acceSBarry Smith 1301*9b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterDestroy() 1302*9b94acceSBarry Smith @*/ 1303*9b94acceSBarry Smith int SNESRegister(int name, char *sname, int (*create)(SNES)) 1304*9b94acceSBarry Smith { 1305*9b94acceSBarry Smith int ierr; 1306*9b94acceSBarry Smith if (!__NLList) {ierr = NRCreate(&__NLList); CHKERRQ(ierr);} 1307*9b94acceSBarry Smith NRRegister( __NLList, name, sname, (int (*)(void*))create ); 1308*9b94acceSBarry Smith return 0; 1309*9b94acceSBarry Smith } 1310*9b94acceSBarry Smith /* --------------------------------------------------------------------- */ 1311*9b94acceSBarry Smith /*@C 1312*9b94acceSBarry Smith SNESRegisterDestroy - Frees the list of nonlinear solvers that were 1313*9b94acceSBarry Smith registered by SNESRegister(). 1314*9b94acceSBarry Smith 1315*9b94acceSBarry Smith .keywords: SNES, nonlinear, register, destroy 1316*9b94acceSBarry Smith 1317*9b94acceSBarry Smith .seealso: SNESRegisterAll(), SNESRegisterAll() 1318*9b94acceSBarry Smith @*/ 1319*9b94acceSBarry Smith int SNESRegisterDestroy() 1320*9b94acceSBarry Smith { 1321*9b94acceSBarry Smith if (__NLList) { 1322*9b94acceSBarry Smith NRDestroy( __NLList ); 1323*9b94acceSBarry Smith __NLList = 0; 1324*9b94acceSBarry Smith } 1325*9b94acceSBarry Smith return 0; 1326*9b94acceSBarry Smith } 1327*9b94acceSBarry Smith 1328*9b94acceSBarry Smith /* 1329*9b94acceSBarry Smith SNESGetMethodFromOptions_Private - Sets the selected method from the 1330*9b94acceSBarry Smith options database. 1331*9b94acceSBarry Smith 1332*9b94acceSBarry Smith Input Parameter: 1333*9b94acceSBarry Smith . ctx - the SNES context 1334*9b94acceSBarry Smith 1335*9b94acceSBarry Smith Output Parameter: 1336*9b94acceSBarry Smith . method - solver method 1337*9b94acceSBarry Smith 1338*9b94acceSBarry Smith Returns: 1339*9b94acceSBarry Smith Returns 1 if the method is found; 0 otherwise. 1340*9b94acceSBarry Smith 1341*9b94acceSBarry Smith Options Database Key: 1342*9b94acceSBarry Smith $ -snes_method method 1343*9b94acceSBarry Smith */ 1344*9b94acceSBarry Smith int SNESGetMethodFromOptions_Private(SNES ctx,SNESMethod *method) 1345*9b94acceSBarry Smith { 1346*9b94acceSBarry Smith char sbuf[50]; 1347*9b94acceSBarry Smith if (OptionsGetString(ctx->prefix,"-snes_method", sbuf, 50 )) { 1348*9b94acceSBarry Smith if (!__NLList) SNESRegisterAll(); 1349*9b94acceSBarry Smith *method = (SNESMethod)NRFindID( __NLList, sbuf ); 1350*9b94acceSBarry Smith return 1; 1351*9b94acceSBarry Smith } 1352*9b94acceSBarry Smith return 0; 1353*9b94acceSBarry Smith } 1354*9b94acceSBarry Smith 1355*9b94acceSBarry Smith /*@ 1356*9b94acceSBarry Smith SNESGetMethodFromContext - Gets the nonlinear solver method from an 1357*9b94acceSBarry Smith active SNES context. 1358*9b94acceSBarry Smith 1359*9b94acceSBarry Smith Input Parameter: 1360*9b94acceSBarry Smith . snes - the SNES context 1361*9b94acceSBarry Smith 1362*9b94acceSBarry Smith Output parameters: 1363*9b94acceSBarry Smith . method - the method ID 1364*9b94acceSBarry Smith 1365*9b94acceSBarry Smith .keywords: SNES, nonlinear, get, method, context, type 1366*9b94acceSBarry Smith 1367*9b94acceSBarry Smith .seealso: SNESGetMethodName() 1368*9b94acceSBarry Smith @*/ 1369*9b94acceSBarry Smith int SNESGetMethodFromContext(SNES snes, SNESMethod *method) 1370*9b94acceSBarry Smith { 1371*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 1372*9b94acceSBarry Smith *method = (SNESMethod) snes->type; 1373*9b94acceSBarry Smith return 0; 1374*9b94acceSBarry Smith } 1375*9b94acceSBarry Smith 1376*9b94acceSBarry Smith /*@C 1377*9b94acceSBarry Smith SNESGetMethodName - Gets the SNES method name (as a string) from 1378*9b94acceSBarry Smith the method type. 1379*9b94acceSBarry Smith 1380*9b94acceSBarry Smith Input Parameter: 1381*9b94acceSBarry Smith . method - SNES method 1382*9b94acceSBarry Smith 1383*9b94acceSBarry Smith Output Parameter: 1384*9b94acceSBarry Smith . name - name of SNES method 1385*9b94acceSBarry Smith 1386*9b94acceSBarry Smith .keywords: SNES, nonlinear, get, method, name 1387*9b94acceSBarry Smith @*/ 1388*9b94acceSBarry Smith int SNESGetMethodName(SNESMethod method,char **name) 1389*9b94acceSBarry Smith { 1390*9b94acceSBarry Smith if (!__NLList) SNESRegisterAll(); 1391*9b94acceSBarry Smith *name = NRFindName( __NLList, (int) method ); 1392*9b94acceSBarry Smith return 0; 1393*9b94acceSBarry Smith } 1394*9b94acceSBarry Smith 1395*9b94acceSBarry Smith #include <stdio.h> 1396*9b94acceSBarry Smith /* 1397*9b94acceSBarry Smith SNESPrintMethods_Private - Prints the SNES methods available from the 1398*9b94acceSBarry Smith options database. 1399*9b94acceSBarry Smith 1400*9b94acceSBarry Smith Input Parameters: 1401*9b94acceSBarry Smith . prefix - prefix (usually "-") 1402*9b94acceSBarry Smith . name - the options database name (by default "snes_method") 1403*9b94acceSBarry Smith */ 1404*9b94acceSBarry Smith int SNESPrintMethods_Private(char* prefix,char *name) 1405*9b94acceSBarry Smith { 1406*9b94acceSBarry Smith FuncList *entry; 1407*9b94acceSBarry Smith if (!__NLList) {SNESRegisterAll();} 1408*9b94acceSBarry Smith entry = __NLList->head; 1409*9b94acceSBarry Smith fprintf(stderr," %s%s (one of)",prefix,name); 1410*9b94acceSBarry Smith while (entry) { 1411*9b94acceSBarry Smith fprintf(stderr," %s",entry->name); 1412*9b94acceSBarry Smith entry = entry->next; 1413*9b94acceSBarry Smith } 1414*9b94acceSBarry Smith fprintf(stderr,"\n"); 1415*9b94acceSBarry Smith return 0; 1416*9b94acceSBarry Smith } 1417*9b94acceSBarry Smith 1418*9b94acceSBarry Smith /*@C 1419*9b94acceSBarry Smith SNESGetSolution - Returns the vector where the approximate solution is 1420*9b94acceSBarry Smith stored. 1421*9b94acceSBarry Smith 1422*9b94acceSBarry Smith Input Parameter: 1423*9b94acceSBarry Smith . snes - the SNES context 1424*9b94acceSBarry Smith 1425*9b94acceSBarry Smith Output Parameter: 1426*9b94acceSBarry Smith . x - the solution 1427*9b94acceSBarry Smith 1428*9b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution 1429*9b94acceSBarry Smith 1430*9b94acceSBarry Smith .seealso: SNESSetSolution(), SNESGetFunction(), SNESGetSolutionUpdate() 1431*9b94acceSBarry Smith @*/ 1432*9b94acceSBarry Smith int SNESGetSolution(SNES snes,Vec *x) 1433*9b94acceSBarry Smith { 1434*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 1435*9b94acceSBarry Smith *x = snes->vec_sol_always; 1436*9b94acceSBarry Smith return 0; 1437*9b94acceSBarry Smith } 1438*9b94acceSBarry Smith 1439*9b94acceSBarry Smith /*@C 1440*9b94acceSBarry Smith SNESGetSolutionUpdate - Returns the vector where the solution update is 1441*9b94acceSBarry Smith stored. 1442*9b94acceSBarry Smith 1443*9b94acceSBarry Smith Input Parameter: 1444*9b94acceSBarry Smith . snes - the SNES context 1445*9b94acceSBarry Smith 1446*9b94acceSBarry Smith Output Parameter: 1447*9b94acceSBarry Smith . x - the solution update 1448*9b94acceSBarry Smith 1449*9b94acceSBarry Smith Notes: 1450*9b94acceSBarry Smith This vector is implementation dependent. 1451*9b94acceSBarry Smith 1452*9b94acceSBarry Smith .keywords: SNES, nonlinear, get, solution, update 1453*9b94acceSBarry Smith 1454*9b94acceSBarry Smith .seealso: SNESGetSolution(), SNESGetFunction 1455*9b94acceSBarry Smith @*/ 1456*9b94acceSBarry Smith int SNESGetSolutionUpdate(SNES snes,Vec *x) 1457*9b94acceSBarry Smith { 1458*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 1459*9b94acceSBarry Smith *x = snes->vec_sol_update_always; 1460*9b94acceSBarry Smith return 0; 1461*9b94acceSBarry Smith } 1462*9b94acceSBarry Smith 1463*9b94acceSBarry Smith /*@C 1464*9b94acceSBarry Smith SNESGetFunction - Returns the vector where the function is 1465*9b94acceSBarry Smith stored. Actually usually returns the vector where the negative of 1466*9b94acceSBarry Smith the function is stored. 1467*9b94acceSBarry Smith 1468*9b94acceSBarry Smith Input Parameter: 1469*9b94acceSBarry Smith . snes - the SNES context 1470*9b94acceSBarry Smith 1471*9b94acceSBarry Smith Output Parameter: 1472*9b94acceSBarry Smith . r - the function (or its negative) 1473*9b94acceSBarry Smith 1474*9b94acceSBarry Smith Notes: 1475*9b94acceSBarry Smith SNESGetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only 1476*9b94acceSBarry Smith Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 1477*9b94acceSBarry Smith SNESGetMinimizationFunction() and SNESGetGradient(); 1478*9b94acceSBarry Smith 1479*9b94acceSBarry Smith .keywords: SNES, nonlinear, get function 1480*9b94acceSBarry Smith 1481*9b94acceSBarry Smith .seealso: SNESSetFunction(), SNESGetSolution() 1482*9b94acceSBarry Smith @*/ 1483*9b94acceSBarry Smith int SNESGetFunction(SNES snes,Vec *r) 1484*9b94acceSBarry Smith { 1485*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 1486*9b94acceSBarry Smith if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1, 1487*9b94acceSBarry Smith "SNESGetFunction: Valid for SNES_NONLINEAR_EQUATIONS methods only"); 1488*9b94acceSBarry Smith *r = snes->vec_func_always; 1489*9b94acceSBarry Smith return 0; 1490*9b94acceSBarry Smith } 1491*9b94acceSBarry Smith 1492*9b94acceSBarry Smith /*@C 1493*9b94acceSBarry Smith SNESGetGradient - Returns the vector where the gradient is 1494*9b94acceSBarry Smith stored. Actually usually returns the vector where the negative of 1495*9b94acceSBarry Smith the function is stored. 1496*9b94acceSBarry Smith 1497*9b94acceSBarry Smith Input Parameter: 1498*9b94acceSBarry Smith . snes - the SNES context 1499*9b94acceSBarry Smith 1500*9b94acceSBarry Smith Output Parameter: 1501*9b94acceSBarry Smith . r - the gradient 1502*9b94acceSBarry Smith 1503*9b94acceSBarry Smith Notes: 1504*9b94acceSBarry Smith SNESGetGradient() is valid for SNES_UNCONSTRAINED_MINIMIZATION methods 1505*9b94acceSBarry Smith only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 1506*9b94acceSBarry Smith SNESGetFunction(). 1507*9b94acceSBarry Smith 1508*9b94acceSBarry Smith .keywords: SNES, nonlinear, get, gradient 1509*9b94acceSBarry Smith 1510*9b94acceSBarry Smith .seealso: SNESGetMinimizationFunction(), SNESGetSolution() 1511*9b94acceSBarry Smith @*/ 1512*9b94acceSBarry Smith int SNESGetGradient(SNES snes,Vec *r) 1513*9b94acceSBarry Smith { 1514*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 1515*9b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 1516*9b94acceSBarry Smith "SNESGetGradient: Valid for SNES_UNCONSTRAINED_MINIMIZATION methods only"); 1517*9b94acceSBarry Smith *r = snes->vec_func_always; 1518*9b94acceSBarry Smith return 0; 1519*9b94acceSBarry Smith } 1520*9b94acceSBarry Smith 1521*9b94acceSBarry Smith /*@ 1522*9b94acceSBarry Smith SNESGetMinimizationFunction - Returns the scalar function value for 1523*9b94acceSBarry Smith unconstrained minimization problems. 1524*9b94acceSBarry Smith 1525*9b94acceSBarry Smith Input Parameter: 1526*9b94acceSBarry Smith . snes - the SNES context 1527*9b94acceSBarry Smith 1528*9b94acceSBarry Smith Output Parameter: 1529*9b94acceSBarry Smith . r - the function 1530*9b94acceSBarry Smith 1531*9b94acceSBarry Smith Notes: 1532*9b94acceSBarry Smith SNESGetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 1533*9b94acceSBarry Smith methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 1534*9b94acceSBarry Smith SNESGetFunction(). 1535*9b94acceSBarry Smith 1536*9b94acceSBarry Smith .keywords: SNES, nonlinear, get, function 1537*9b94acceSBarry Smith 1538*9b94acceSBarry Smith .seealso: SNESGetGradient(), SNESGetSolution() 1539*9b94acceSBarry Smith @*/ 1540*9b94acceSBarry Smith int SNESGetMinimizationFunction(SNES snes,double *r) 1541*9b94acceSBarry Smith { 1542*9b94acceSBarry Smith PETSCVALIDHEADERSPECIFIC(snes,SNES_COOKIE); 1543*9b94acceSBarry Smith if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1, 1544*9b94acceSBarry Smith "SNESGetMinimizationFunction: Valid for SNES_UNCONSTRAINED_MINIMIZATION methods only"); 1545*9b94acceSBarry Smith *r = snes->fc; 1546*9b94acceSBarry Smith return 0; 1547*9b94acceSBarry Smith } 1548*9b94acceSBarry Smith 1549*9b94acceSBarry Smith 1550*9b94acceSBarry Smith 1551*9b94acceSBarry Smith 1552*9b94acceSBarry Smith 1553