1 #ifndef lint 2 static char vcid[] = "$Id: snes.c,v 1.110 1997/01/21 03:37:35 bsmith Exp curfman $"; 3 #endif 4 5 #include "draw.h" /*I "draw.h" I*/ 6 #include "src/snes/snesimpl.h" /*I "snes.h" I*/ 7 #include "src/sys/nreg.h" 8 #include "pinclude/pviewer.h" 9 #include <math.h> 10 11 extern int SNESGetTypeFromOptions_Private(SNES,SNESType*,int*); 12 extern int SNESPrintTypes_Private(MPI_Comm,char*,char*); 13 14 #undef __FUNC__ 15 #define __FUNC__ "SNESView" 16 /*@ 17 SNESView - Prints the SNES data structure. 18 19 Input Parameters: 20 . SNES - the SNES context 21 . viewer - visualization context 22 23 Options Database Key: 24 $ -snes_view : calls SNESView() at end of SNESSolve() 25 26 Notes: 27 The available visualization contexts include 28 $ VIEWER_STDOUT_SELF - standard output (default) 29 $ VIEWER_STDOUT_WORLD - synchronized standard 30 $ output where only the first processor opens 31 $ the file. All other processors send their 32 $ data to the first processor to print. 33 34 The user can open alternative vistualization contexts with 35 $ ViewerFileOpenASCII() - output to a specified file 36 37 .keywords: SNES, view 38 39 .seealso: ViewerFileOpenASCII() 40 @*/ 41 int SNESView(SNES snes,Viewer viewer) 42 { 43 SNES_KSP_EW_ConvCtx *kctx; 44 FILE *fd; 45 int ierr; 46 SLES sles; 47 char *method; 48 ViewerType vtype; 49 50 PetscValidHeaderSpecific(snes,SNES_COOKIE); 51 if (viewer) {PetscValidHeader(viewer);} 52 else { viewer = VIEWER_STDOUT_SELF; } 53 54 ierr = ViewerGetType(viewer,&vtype); CHKERRQ(ierr); 55 if (vtype == ASCII_FILE_VIEWER || vtype == ASCII_FILES_VIEWER) { 56 ierr = ViewerASCIIGetPointer(viewer,&fd); CHKERRQ(ierr); 57 PetscFPrintf(snes->comm,fd,"SNES Object:\n"); 58 SNESGetType(snes,PETSC_NULL,&method); 59 PetscFPrintf(snes->comm,fd," method: %s\n",method); 60 if (snes->view) (*snes->view)((PetscObject)snes,viewer); 61 PetscFPrintf(snes->comm,fd, 62 " maximum iterations=%d, maximum function evaluations=%d\n", 63 snes->max_its,snes->max_funcs); 64 PetscFPrintf(snes->comm,fd, 65 " tolerances: relative=%g, absolute=%g, truncation=%g, solution=%g\n", 66 snes->rtol, snes->atol, snes->trunctol, snes->xtol); 67 PetscFPrintf(snes->comm,fd, 68 " total number of linear solver iterations=%d\n",snes->linear_its); 69 PetscFPrintf(snes->comm,fd, 70 " total number of function evaluations=%d\n",snes->nfuncs); 71 if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) 72 PetscFPrintf(snes->comm,fd," min function tolerance=%g\n",snes->fmin); 73 if (snes->ksp_ewconv) { 74 kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx; 75 if (kctx) { 76 PetscFPrintf(snes->comm,fd, 77 " Eisenstat-Walker computation of KSP relative tolerance (version %d)\n", 78 kctx->version); 79 PetscFPrintf(snes->comm,fd, 80 " rtol_0=%g, rtol_max=%g, threshold=%g\n",kctx->rtol_0, 81 kctx->rtol_max,kctx->threshold); 82 PetscFPrintf(snes->comm,fd," gamma=%g, alpha=%g, alpha2=%g\n", 83 kctx->gamma,kctx->alpha,kctx->alpha2); 84 } 85 } 86 } else if (vtype == STRING_VIEWER) { 87 SNESGetType(snes,PETSC_NULL,&method); 88 ViewerStringSPrintf(viewer," %-3.3s",method); 89 } 90 SNESGetSLES(snes,&sles); 91 ierr = SLESView(sles,viewer); CHKERRQ(ierr); 92 return 0; 93 } 94 95 /* 96 We retain a list of functions that also take SNES command 97 line options. These are called at the end SNESSetFromOptions() 98 */ 99 #define MAXSETFROMOPTIONS 5 100 static int numberofsetfromoptions; 101 static int (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES); 102 103 #undef __FUNC__ 104 #define __FUNC__ "SNESAddOptionsChecker" 105 /*@ 106 SNESAddOptionsChecker - Adds an additional function to check for SNES options. 107 108 Input Parameter: 109 . snescheck - function that checks for options 110 111 .seealso: SNESSetFromOptions() 112 @*/ 113 int SNESAddOptionsChecker(int (*snescheck)(SNES) ) 114 { 115 if (numberofsetfromoptions >= MAXSETFROMOPTIONS) { 116 SETERRQ(1,0,"Too many options checkers, only 5 allowed"); 117 } 118 119 othersetfromoptions[numberofsetfromoptions++] = snescheck; 120 return 0; 121 } 122 123 #undef __FUNC__ 124 #define __FUNC__ "SNESSetFromOptions" 125 /*@ 126 SNESSetFromOptions - Sets various SNES and SLES parameters from user options. 127 128 Input Parameter: 129 . snes - the SNES context 130 131 .keywords: SNES, nonlinear, set, options, database 132 133 .seealso: SNESPrintHelp(), SNESSetOptionsPrefix() 134 @*/ 135 int SNESSetFromOptions(SNES snes) 136 { 137 SNESType method; 138 double tmp; 139 SLES sles; 140 int ierr, flg,i,loc[4],nmax = 4; 141 int version = PETSC_DEFAULT; 142 double rtol_0 = PETSC_DEFAULT; 143 double rtol_max = PETSC_DEFAULT; 144 double gamma2 = PETSC_DEFAULT; 145 double alpha = PETSC_DEFAULT; 146 double alpha2 = PETSC_DEFAULT; 147 double threshold = PETSC_DEFAULT; 148 149 loc[0] = PETSC_DECIDE; loc[1] = PETSC_DECIDE; loc[2] = 300; loc[3] = 300; 150 151 152 PetscValidHeaderSpecific(snes,SNES_COOKIE); 153 if (snes->setup_called) SETERRQ(1,0,"Must call prior to SNESSetUp"); 154 ierr = SNESGetTypeFromOptions_Private(snes,&method,&flg); CHKERRQ(ierr); 155 if (flg) { 156 ierr = SNESSetType(snes,method); CHKERRQ(ierr); 157 } 158 else if (!snes->set_method_called) { 159 if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 160 ierr = SNESSetType(snes,SNES_EQ_LS); CHKERRQ(ierr); 161 } 162 else { 163 ierr = SNESSetType(snes,SNES_UM_TR); CHKERRQ(ierr); 164 } 165 } 166 167 ierr = OptionsHasName(PETSC_NULL,"-help", &flg); CHKERRQ(ierr); 168 if (flg) { SNESPrintHelp(snes); } 169 ierr = OptionsGetDouble(snes->prefix,"-snes_stol",&tmp, &flg); CHKERRQ(ierr); 170 if (flg) {SNESSetTolerances(snes,PETSC_DEFAULT,PETSC_DEFAULT,tmp,PETSC_DEFAULT,PETSC_DEFAULT);} 171 ierr = OptionsGetDouble(snes->prefix,"-snes_atol",&tmp, &flg); CHKERRQ(ierr); 172 if (flg) {SNESSetTolerances(snes,tmp,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);} 173 ierr = OptionsGetDouble(snes->prefix,"-snes_rtol",&tmp, &flg); CHKERRQ(ierr); 174 if (flg) {SNESSetTolerances(snes,PETSC_DEFAULT,tmp,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT); } 175 ierr = OptionsGetInt(snes->prefix,"-snes_max_it",&snes->max_its, &flg); CHKERRQ(ierr); 176 ierr = OptionsGetInt(snes->prefix,"-snes_max_funcs",&snes->max_funcs, &flg);CHKERRQ(ierr); 177 ierr = OptionsGetDouble(snes->prefix,"-snes_trtol",&tmp, &flg); CHKERRQ(ierr); 178 if (flg) { SNESSetTrustRegionTolerance(snes,tmp); } 179 ierr = OptionsGetDouble(snes->prefix,"-snes_fmin",&tmp, &flg); CHKERRQ(ierr); 180 if (flg) { SNESSetMinimizationFunctionTolerance(snes,tmp); } 181 ierr = OptionsHasName(snes->prefix,"-snes_ksp_ew_conv", &flg); CHKERRQ(ierr); 182 if (flg) { snes->ksp_ewconv = 1; } 183 ierr = OptionsGetInt(snes->prefix,"-snes_ksp_ew_version",&version,&flg); CHKERRQ(ierr); 184 ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtol0",&rtol_0,&flg); CHKERRQ(ierr); 185 ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtolmax",&rtol_max,&flg);CHKERRQ(ierr); 186 ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_gamma",&gamma2,&flg); CHKERRQ(ierr); 187 ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha",&alpha,&flg); CHKERRQ(ierr); 188 ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha2",&alpha2,&flg); CHKERRQ(ierr); 189 ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_threshold",&threshold,&flg);CHKERRQ(ierr); 190 ierr = SNES_KSP_SetParametersEW(snes,version,rtol_0,rtol_max,gamma2,alpha, 191 alpha2,threshold); CHKERRQ(ierr); 192 ierr = OptionsHasName(snes->prefix,"-snes_cancelmonitors",&flg); CHKERRQ(ierr); 193 if (flg) {ierr = SNESSetMonitor(snes,0,0);CHKERRQ(ierr);} 194 ierr = OptionsHasName(snes->prefix,"-snes_monitor",&flg); CHKERRQ(ierr); 195 if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultMonitor,0);CHKERRQ(ierr);} 196 ierr = OptionsHasName(snes->prefix,"-snes_smonitor",&flg); CHKERRQ(ierr); 197 if (flg) { 198 ierr = SNESSetMonitor(snes,SNESDefaultSMonitor,0); CHKERRQ(ierr); 199 } 200 ierr = OptionsGetIntArray(snes->prefix,"-snes_xmonitor",loc,&nmax,&flg);CHKERRQ(ierr); 201 if (flg) { 202 int rank = 0; 203 DrawLG lg; 204 MPI_Initialized(&rank); 205 if (rank) MPI_Comm_rank(snes->comm,&rank); 206 if (!rank) { 207 ierr = SNESLGMonitorCreate(0,0,loc[0],loc[1],loc[2],loc[3],&lg); CHKERRQ(ierr); 208 ierr = SNESSetMonitor(snes,SNESLGMonitor,(void *)lg); CHKERRQ(ierr); 209 snes->xmonitor = lg; 210 PLogObjectParent(snes,lg); 211 } 212 } 213 ierr = OptionsHasName(snes->prefix,"-snes_fd", &flg); CHKERRQ(ierr); 214 if (flg && snes->method_class == SNES_NONLINEAR_EQUATIONS) { 215 ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre, 216 SNESDefaultComputeJacobian,snes->funP); CHKERRQ(ierr); 217 PLogInfo(snes, 218 "SNESSetFromOptions: Setting default finite difference Jacobian matrix\n"); 219 } 220 else if (flg && snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 221 ierr = SNESSetHessian(snes,snes->jacobian,snes->jacobian_pre, 222 SNESDefaultComputeHessian,snes->funP); CHKERRQ(ierr); 223 PLogInfo(snes, 224 "SNESSetFromOptions: Setting default finite difference Hessian matrix\n"); 225 } 226 227 for ( i=0; i<numberofsetfromoptions; i++ ) { 228 ierr = (*othersetfromoptions[i])(snes); CHKERRQ(ierr); 229 } 230 231 ierr = SNESGetSLES(snes,&sles); CHKERRQ(ierr); 232 ierr = SLESSetFromOptions(sles); CHKERRQ(ierr); 233 if (!snes->setfromoptions) return 0; 234 return (*snes->setfromoptions)(snes); 235 } 236 237 #undef __FUNC__ 238 #define __FUNC__ "SNESPrintHelp" 239 /*@ 240 SNESPrintHelp - Prints all options for the SNES component. 241 242 Input Parameter: 243 . snes - the SNES context 244 245 Options Database Keys: 246 $ -help, -h 247 248 .keywords: SNES, nonlinear, help 249 250 .seealso: SNESSetFromOptions() 251 @*/ 252 int SNESPrintHelp(SNES snes) 253 { 254 char p[64]; 255 SNES_KSP_EW_ConvCtx *kctx; 256 257 PetscValidHeaderSpecific(snes,SNES_COOKIE); 258 259 PetscStrcpy(p,"-"); 260 if (snes->prefix) PetscStrcat(p, snes->prefix); 261 262 kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx; 263 264 PetscPrintf(snes->comm,"SNES options ------------------------------------------------\n"); 265 SNESPrintTypes_Private(snes->comm,p,"snes_type"); 266 PetscPrintf(snes->comm," %ssnes_view: view SNES info after each nonlinear solve\n",p); 267 PetscPrintf(snes->comm," %ssnes_max_it <its>: max iterations (default %d)\n",p,snes->max_its); 268 PetscPrintf(snes->comm," %ssnes_max_funcs <maxf>: max function evals (default %d)\n",p,snes->max_funcs); 269 PetscPrintf(snes->comm," %ssnes_stol <stol>: successive step tolerance (default %g)\n",p,snes->xtol); 270 PetscPrintf(snes->comm," %ssnes_atol <atol>: absolute tolerance (default %g)\n",p,snes->atol); 271 PetscPrintf(snes->comm," %ssnes_rtol <rtol>: relative tolerance (default %g)\n",p,snes->rtol); 272 PetscPrintf(snes->comm," %ssnes_trtol <trtol>: trust region parameter tolerance (default %g)\n",p,snes->deltatol); 273 PetscPrintf(snes->comm," SNES Monitoring Options: Choose any of the following\n"); 274 PetscPrintf(snes->comm," %ssnes_cancelmonitors: cancels all monitors hardwired in code\n",p); 275 PetscPrintf(snes->comm," %ssnes_monitor: use default SNES convergence monitor\n",p); 276 PetscPrintf(snes->comm," %ssnes_xmonitor [x,y,w,h]: use X graphics convergence monitor\n",p); 277 if (snes->type == SNES_NONLINEAR_EQUATIONS) { 278 PetscPrintf(snes->comm, 279 " Options for solving systems of nonlinear equations only:\n"); 280 PetscPrintf(snes->comm," %ssnes_fd: use finite differences for Jacobian\n",p); 281 PetscPrintf(snes->comm," %ssnes_mf: use matrix-free Jacobian\n",p); 282 PetscPrintf(snes->comm," %ssnes_mf_operator:use matrix-free Jacobian and user-provided preconditioning matrix\n",p); 283 PetscPrintf(snes->comm," %ssnes_mf_err: relative error in function evaluation for matrix-free Jacobian\n",p); 284 PetscPrintf(snes->comm," %ssnes_mf_umin: minimum iterate parameter for matrix-free Jacobian\n",p); 285 PetscPrintf(snes->comm," %ssnes_ksp_ew_conv: use Eisenstat-Walker computation of KSP rtol. Params are:\n",p); 286 PetscPrintf(snes->comm, 287 " %ssnes_ksp_ew_version <version> (1 or 2, default is %d)\n",p,kctx->version); 288 PetscPrintf(snes->comm, 289 " %ssnes_ksp_ew_rtol0 <rtol0> (0 <= rtol0 < 1, default %g)\n",p,kctx->rtol_0); 290 PetscPrintf(snes->comm, 291 " %ssnes_ksp_ew_rtolmax <rtolmax> (0 <= rtolmax < 1, default %g)\n",p,kctx->rtol_max); 292 PetscPrintf(snes->comm, 293 " %ssnes_ksp_ew_gamma <gamma> (0 <= gamma <= 1, default %g)\n",p,kctx->gamma); 294 PetscPrintf(snes->comm, 295 " %ssnes_ksp_ew_alpha <alpha> (1 < alpha <= 2, default %g)\n",p,kctx->alpha); 296 PetscPrintf(snes->comm, 297 " %ssnes_ksp_ew_alpha2 <alpha2> (default %g)\n",p,kctx->alpha2); 298 PetscPrintf(snes->comm, 299 " %ssnes_ksp_ew_threshold <threshold> (0 < threshold < 1, default %g)\n",p,kctx->threshold); 300 } 301 else if (snes->type == SNES_UNCONSTRAINED_MINIMIZATION) { 302 PetscPrintf(snes->comm, 303 " Options for solving unconstrained minimization problems only:\n"); 304 PetscPrintf(snes->comm," %ssnes_fmin <ftol>: minimum function tolerance (default %g)\n",p,snes->fmin); 305 PetscPrintf(snes->comm," %ssnes_fd: use finite differences for Hessian\n",p); 306 PetscPrintf(snes->comm," %ssnes_mf: use matrix-free Hessian\n",p); 307 PetscPrintf(snes->comm," %ssnes_mf_err: relative error in gradient evaluation for matrix-free Hessian\n",p); 308 PetscPrintf(snes->comm," %ssnes_mf_umin: minimum iterate parameter for matrix-free Hessian\n",p); 309 } 310 PetscPrintf(snes->comm," Run program with -help %ssnes_type <method> for help on ",p); 311 PetscPrintf(snes->comm,"a particular method\n"); 312 if (snes->printhelp) (*snes->printhelp)(snes,p); 313 return 0; 314 } 315 316 #undef __FUNC__ 317 #define __FUNC__ "SNESSetApplicationContext" 318 /*@ 319 SNESSetApplicationContext - Sets the optional user-defined context for 320 the nonlinear solvers. 321 322 Input Parameters: 323 . snes - the SNES context 324 . usrP - optional user context 325 326 .keywords: SNES, nonlinear, set, application, context 327 328 .seealso: SNESGetApplicationContext() 329 @*/ 330 int SNESSetApplicationContext(SNES snes,void *usrP) 331 { 332 PetscValidHeaderSpecific(snes,SNES_COOKIE); 333 snes->user = usrP; 334 return 0; 335 } 336 337 #undef __FUNC__ 338 #define __FUNC__ "SNESGetApplicationContext" 339 /*@C 340 SNESGetApplicationContext - Gets the user-defined context for the 341 nonlinear solvers. 342 343 Input Parameter: 344 . snes - SNES context 345 346 Output Parameter: 347 . usrP - user context 348 349 .keywords: SNES, nonlinear, get, application, context 350 351 .seealso: SNESSetApplicationContext() 352 @*/ 353 int SNESGetApplicationContext( SNES snes, void **usrP ) 354 { 355 PetscValidHeaderSpecific(snes,SNES_COOKIE); 356 *usrP = snes->user; 357 return 0; 358 } 359 360 #undef __FUNC__ 361 #define __FUNC__ "SNESGetIterationNumber" 362 /*@ 363 SNESGetIterationNumber - Gets the current iteration number of the 364 nonlinear solver. 365 366 Input Parameter: 367 . snes - SNES context 368 369 Output Parameter: 370 . iter - iteration number 371 372 .keywords: SNES, nonlinear, get, iteration, number 373 @*/ 374 int SNESGetIterationNumber(SNES snes,int* iter) 375 { 376 PetscValidHeaderSpecific(snes,SNES_COOKIE); 377 PetscValidIntPointer(iter); 378 *iter = snes->iter; 379 return 0; 380 } 381 382 #undef __FUNC__ 383 #define __FUNC__ "SNESGetFunctionNorm" 384 /*@ 385 SNESGetFunctionNorm - Gets the norm of the current function that was set 386 with SNESSSetFunction(). 387 388 Input Parameter: 389 . snes - SNES context 390 391 Output Parameter: 392 . fnorm - 2-norm of function 393 394 Note: 395 SNESGetFunctionNorm() is valid for SNES_NONLINEAR_EQUATIONS methods only. 396 A related routine for SNES_UNCONSTRAINED_MINIMIZATION methods is 397 SNESGetGradientNorm(). 398 399 .keywords: SNES, nonlinear, get, function, norm 400 401 .seealso: SNESSetFunction() 402 @*/ 403 int SNESGetFunctionNorm(SNES snes,Scalar *fnorm) 404 { 405 PetscValidHeaderSpecific(snes,SNES_COOKIE); 406 PetscValidScalarPointer(fnorm); 407 if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 408 SETERRQ(1,0,"For SNES_NONLINEAR_EQUATIONS only"); 409 } 410 *fnorm = snes->norm; 411 return 0; 412 } 413 414 #undef __FUNC__ 415 #define __FUNC__ "SNESGetGradientNorm" 416 /*@ 417 SNESGetGradientNorm - Gets the norm of the current gradient that was set 418 with SNESSSetGradient(). 419 420 Input Parameter: 421 . snes - SNES context 422 423 Output Parameter: 424 . fnorm - 2-norm of gradient 425 426 Note: 427 SNESGetGradientNorm() is valid for SNES_UNCONSTRAINED_MINIMIZATION 428 methods only. A related routine for SNES_NONLINEAR_EQUATIONS methods 429 is SNESGetFunctionNorm(). 430 431 .keywords: SNES, nonlinear, get, gradient, norm 432 433 .seelso: SNESSetGradient() 434 @*/ 435 int SNESGetGradientNorm(SNES snes,Scalar *gnorm) 436 { 437 PetscValidHeaderSpecific(snes,SNES_COOKIE); 438 PetscValidScalarPointer(gnorm); 439 if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 440 SETERRQ(1,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 441 } 442 *gnorm = snes->norm; 443 return 0; 444 } 445 446 #undef __FUNC__ 447 #define __FUNC__ "SNESGetNumberUnsuccessfulSteps" 448 /*@ 449 SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps 450 attempted by the nonlinear solver. 451 452 Input Parameter: 453 . snes - SNES context 454 455 Output Parameter: 456 . nfails - number of unsuccessful steps attempted 457 458 Notes: 459 This counter is reset to zero for each successive call to SNESSolve(). 460 461 .keywords: SNES, nonlinear, get, number, unsuccessful, steps 462 @*/ 463 int SNESGetNumberUnsuccessfulSteps(SNES snes,int* nfails) 464 { 465 PetscValidHeaderSpecific(snes,SNES_COOKIE); 466 PetscValidIntPointer(nfails); 467 *nfails = snes->nfailures; 468 return 0; 469 } 470 471 #undef __FUNC__ 472 #define __FUNC__ "SNESGetNumberLinearIterations" 473 /*@ 474 SNESGetNumberLinearIterations - Gets the total number of linear iterations 475 used by the nonlinear solver. 476 477 Input Parameter: 478 . snes - SNES context 479 480 Output Parameter: 481 . lits - number of linear iterations 482 483 Notes: 484 This counter is reset to zero for each successive call to SNESSolve(). 485 486 .keywords: SNES, nonlinear, get, number, linear, iterations 487 @*/ 488 int SNESGetNumberLinearIterations(SNES snes,int* lits) 489 { 490 PetscValidHeaderSpecific(snes,SNES_COOKIE); 491 PetscValidIntPointer(lits); 492 *lits = snes->linear_its; 493 return 0; 494 } 495 496 #undef __FUNC__ 497 #define __FUNC__ "SNESGetSLES" 498 /*@C 499 SNESGetSLES - Returns the SLES context for a SNES solver. 500 501 Input Parameter: 502 . snes - the SNES context 503 504 Output Parameter: 505 . sles - the SLES context 506 507 Notes: 508 The user can then directly manipulate the SLES context to set various 509 options, etc. Likewise, the user can then extract and manipulate the 510 KSP and PC contexts as well. 511 512 .keywords: SNES, nonlinear, get, SLES, context 513 514 .seealso: SLESGetPC(), SLESGetKSP() 515 @*/ 516 int SNESGetSLES(SNES snes,SLES *sles) 517 { 518 PetscValidHeaderSpecific(snes,SNES_COOKIE); 519 *sles = snes->sles; 520 return 0; 521 } 522 /* -----------------------------------------------------------*/ 523 #undef __FUNC__ 524 #define __FUNC__ "SNESCreate" 525 /*@C 526 SNESCreate - Creates a nonlinear solver context. 527 528 Input Parameter: 529 . comm - MPI communicator 530 . type - type of method, one of 531 $ SNES_NONLINEAR_EQUATIONS 532 $ (for systems of nonlinear equations) 533 $ SNES_UNCONSTRAINED_MINIMIZATION 534 $ (for unconstrained minimization) 535 536 Output Parameter: 537 . outsnes - the new SNES context 538 539 Options Database Key: 540 $ -snes_mf - use default matrix-free Jacobian-vector products, 541 $ and no preconditioning matrix 542 $ -snes_mf_operator - use default matrix-free Jacobian-vector 543 $ products, and a user-provided preconditioning matrix 544 $ as set by SNESSetJacobian() 545 $ -snes_fd - use (slow!) finite differences to compute Jacobian 546 547 .keywords: SNES, nonlinear, create, context 548 549 .seealso: SNESSolve(), SNESDestroy() 550 @*/ 551 int SNESCreate(MPI_Comm comm,SNESProblemType type,SNES *outsnes) 552 { 553 int ierr; 554 SNES snes; 555 SNES_KSP_EW_ConvCtx *kctx; 556 557 *outsnes = 0; 558 if (type != SNES_UNCONSTRAINED_MINIMIZATION && type != SNES_NONLINEAR_EQUATIONS) 559 SETERRQ(1,0,"incorrect method type"); 560 PetscHeaderCreate(snes,_SNES,SNES_COOKIE,SNES_UNKNOWN_METHOD,comm); 561 PLogObjectCreate(snes); 562 snes->max_its = 50; 563 snes->max_funcs = 1000; 564 snes->norm = 0.0; 565 if (type == SNES_UNCONSTRAINED_MINIMIZATION) { 566 snes->rtol = 1.e-8; 567 snes->ttol = 0.0; 568 snes->atol = 1.e-10; 569 } 570 else { 571 snes->rtol = 1.e-8; 572 snes->ttol = 0.0; 573 snes->atol = 1.e-50; 574 } 575 snes->xtol = 1.e-8; 576 snes->trunctol = 1.e-12; /* no longer used */ 577 snes->nfuncs = 0; 578 snes->nfailures = 0; 579 snes->linear_its = 0; 580 snes->numbermonitors = 0; 581 snes->data = 0; 582 snes->view = 0; 583 snes->computeumfunction = 0; 584 snes->umfunP = 0; 585 snes->fc = 0; 586 snes->deltatol = 1.e-12; 587 snes->fmin = -1.e30; 588 snes->method_class = type; 589 snes->set_method_called = 0; 590 snes->setup_called = 0; 591 snes->ksp_ewconv = 0; 592 snes->type = -1; 593 snes->xmonitor = 0; 594 snes->vwork = 0; 595 snes->nwork = 0; 596 597 /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 598 kctx = PetscNew(SNES_KSP_EW_ConvCtx); CHKPTRQ(kctx); 599 snes->kspconvctx = (void*)kctx; 600 kctx->version = 2; 601 kctx->rtol_0 = .3; /* Eisenstat and Walker suggest rtol_0=.5, but 602 this was too large for some test cases */ 603 kctx->rtol_last = 0; 604 kctx->rtol_max = .9; 605 kctx->gamma = 1.0; 606 kctx->alpha2 = .5*(1.0 + sqrt(5.0)); 607 kctx->alpha = kctx->alpha2; 608 kctx->threshold = .1; 609 kctx->lresid_last = 0; 610 kctx->norm_last = 0; 611 612 ierr = SLESCreate(comm,&snes->sles); CHKERRQ(ierr); 613 PLogObjectParent(snes,snes->sles) 614 615 *outsnes = snes; 616 return 0; 617 } 618 619 /* --------------------------------------------------------------- */ 620 #undef __FUNC__ 621 #define __FUNC__ "SNESSetFunction" 622 /*@C 623 SNESSetFunction - Sets the function evaluation routine and function 624 vector for use by the SNES routines in solving systems of nonlinear 625 equations. 626 627 Input Parameters: 628 . snes - the SNES context 629 . func - function evaluation routine 630 . r - vector to store function value 631 . ctx - [optional] user-defined context for private data for the 632 function evaluation routine (may be PETSC_NULL) 633 634 Calling sequence of func: 635 . func (SNES snes,Vec x,Vec f,void *ctx); 636 637 . x - input vector 638 . f - function vector 639 . ctx - optional user-defined function context 640 641 Notes: 642 The Newton-like methods typically solve linear systems of the form 643 $ f'(x) x = -f(x), 644 $ where f'(x) denotes the Jacobian matrix and f(x) is the function. 645 646 SNESSetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only. 647 Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 648 SNESSetMinimizationFunction() and SNESSetGradient(); 649 650 .keywords: SNES, nonlinear, set, function 651 652 .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 653 @*/ 654 int SNESSetFunction( SNES snes, Vec r, int (*func)(SNES,Vec,Vec,void*),void *ctx) 655 { 656 PetscValidHeaderSpecific(snes,SNES_COOKIE); 657 if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1,0,"For SNES_NONLINEAR_EQUATIONS only"); 658 snes->computefunction = func; 659 snes->vec_func = snes->vec_func_always = r; 660 snes->funP = ctx; 661 return 0; 662 } 663 664 #undef __FUNC__ 665 #define __FUNC__ "SNESComputeFunction" 666 /*@ 667 SNESComputeFunction - Computes the function that has been set with 668 SNESSetFunction(). 669 670 Input Parameters: 671 . snes - the SNES context 672 . x - input vector 673 674 Output Parameter: 675 . y - function vector, as set by SNESSetFunction() 676 677 Notes: 678 SNESComputeFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only. 679 Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 680 SNESComputeMinimizationFunction() and SNESComputeGradient(); 681 682 .keywords: SNES, nonlinear, compute, function 683 684 .seealso: SNESSetFunction(), SNESGetFunction() 685 @*/ 686 int SNESComputeFunction(SNES snes,Vec x, Vec y) 687 { 688 int ierr; 689 690 if (snes->method_class != SNES_NONLINEAR_EQUATIONS) 691 SETERRQ(1,0,"For SNES_NONLINEAR_EQUATIONS only"); 692 PLogEventBegin(SNES_FunctionEval,snes,x,y,0); 693 ierr = (*snes->computefunction)(snes,x,y,snes->funP); CHKERRQ(ierr); 694 snes->nfuncs++; 695 PLogEventEnd(SNES_FunctionEval,snes,x,y,0); 696 return 0; 697 } 698 699 #undef __FUNC__ 700 #define __FUNC__ "SNESSetMinimizationFunction" 701 /*@C 702 SNESSetMinimizationFunction - Sets the function evaluation routine for 703 unconstrained minimization. 704 705 Input Parameters: 706 . snes - the SNES context 707 . func - function evaluation routine 708 . ctx - [optional] user-defined context for private data for the 709 function evaluation routine (may be PETSC_NULL) 710 711 Calling sequence of func: 712 . func (SNES snes,Vec x,double *f,void *ctx); 713 714 . x - input vector 715 . f - function 716 . ctx - [optional] user-defined function context 717 718 Notes: 719 SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 720 methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 721 SNESSetFunction(). 722 723 .keywords: SNES, nonlinear, set, minimization, function 724 725 .seealso: SNESGetMinimizationFunction(), SNESComputeMinimizationFunction(), 726 SNESSetHessian(), SNESSetGradient() 727 @*/ 728 int SNESSetMinimizationFunction(SNES snes,int (*func)(SNES,Vec,double*,void*), 729 void *ctx) 730 { 731 PetscValidHeaderSpecific(snes,SNES_COOKIE); 732 if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,0,"Only for SNES_UNCONSTRAINED_MINIMIZATION"); 733 snes->computeumfunction = func; 734 snes->umfunP = ctx; 735 return 0; 736 } 737 738 #undef __FUNC__ 739 #define __FUNC__ "SNESComputeMinimizationFunction" 740 /*@ 741 SNESComputeMinimizationFunction - Computes the function that has been 742 set with SNESSetMinimizationFunction(). 743 744 Input Parameters: 745 . snes - the SNES context 746 . x - input vector 747 748 Output Parameter: 749 . y - function value 750 751 Notes: 752 SNESComputeMinimizationFunction() is valid only for 753 SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 754 SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction(). 755 756 .keywords: SNES, nonlinear, compute, minimization, function 757 758 .seealso: SNESSetMinimizationFunction(), SNESGetMinimizationFunction(), 759 SNESComputeGradient(), SNESComputeHessian() 760 @*/ 761 int SNESComputeMinimizationFunction(SNES snes,Vec x,double *y) 762 { 763 int ierr; 764 if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,0,"Only for SNES_UNCONSTRAINED_MINIMIZATION"); 765 PLogEventBegin(SNES_MinimizationFunctionEval,snes,x,y,0); 766 ierr = (*snes->computeumfunction)(snes,x,y,snes->umfunP); CHKERRQ(ierr); 767 snes->nfuncs++; 768 PLogEventEnd(SNES_MinimizationFunctionEval,snes,x,y,0); 769 return 0; 770 } 771 772 #undef __FUNC__ 773 #define __FUNC__ "SNESSetGradient" 774 /*@C 775 SNESSetGradient - Sets the gradient evaluation routine and gradient 776 vector for use by the SNES routines. 777 778 Input Parameters: 779 . snes - the SNES context 780 . func - function evaluation routine 781 . ctx - optional user-defined context for private data for the 782 gradient evaluation routine (may be PETSC_NULL) 783 . r - vector to store gradient value 784 785 Calling sequence of func: 786 . func (SNES, Vec x, Vec g, void *ctx); 787 788 . x - input vector 789 . g - gradient vector 790 . ctx - optional user-defined gradient context 791 792 Notes: 793 SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 794 methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 795 SNESSetFunction(). 796 797 .keywords: SNES, nonlinear, set, function 798 799 .seealso: SNESGetGradient(), SNESComputeGradient(), SNESSetHessian(), 800 SNESSetMinimizationFunction(), 801 @*/ 802 int SNESSetGradient(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),void *ctx) 803 { 804 PetscValidHeaderSpecific(snes,SNES_COOKIE); 805 if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 806 snes->computefunction = func; 807 snes->vec_func = snes->vec_func_always = r; 808 snes->funP = ctx; 809 return 0; 810 } 811 812 #undef __FUNC__ 813 #define __FUNC__ "SNESComputeGradient" 814 /*@ 815 SNESComputeGradient - Computes the gradient that has been set with 816 SNESSetGradient(). 817 818 Input Parameters: 819 . snes - the SNES context 820 . x - input vector 821 822 Output Parameter: 823 . y - gradient vector 824 825 Notes: 826 SNESComputeGradient() is valid only for 827 SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 828 SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction(). 829 830 .keywords: SNES, nonlinear, compute, gradient 831 832 .seealso: SNESSetGradient(), SNESGetGradient(), 833 SNESComputeMinimizationFunction(), SNESComputeHessian() 834 @*/ 835 int SNESComputeGradient(SNES snes,Vec x, Vec y) 836 { 837 int ierr; 838 if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) 839 SETERRQ(1,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 840 PLogEventBegin(SNES_GradientEval,snes,x,y,0); 841 ierr = (*snes->computefunction)(snes,x,y,snes->funP); CHKERRQ(ierr); 842 PLogEventEnd(SNES_GradientEval,snes,x,y,0); 843 return 0; 844 } 845 846 #undef __FUNC__ 847 #define __FUNC__ "SNESComputeJacobian" 848 /*@ 849 SNESComputeJacobian - Computes the Jacobian matrix that has been 850 set with SNESSetJacobian(). 851 852 Input Parameters: 853 . snes - the SNES context 854 . x - input vector 855 856 Output Parameters: 857 . A - Jacobian matrix 858 . B - optional preconditioning matrix 859 . flag - flag indicating matrix structure 860 861 Notes: 862 Most users should not need to explicitly call this routine, as it 863 is used internally within the nonlinear solvers. 864 865 See SLESSetOperators() for important information about setting the 866 flag parameter. 867 868 SNESComputeJacobian() is valid only for SNES_NONLINEAR_EQUATIONS 869 methods. An analogous routine for SNES_UNCONSTRAINED_MINIMIZATION 870 methods is SNESComputeJacobian(). 871 872 .keywords: SNES, compute, Jacobian, matrix 873 874 .seealso: SNESSetJacobian(), SLESSetOperators() 875 @*/ 876 int SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 877 { 878 int ierr; 879 if (snes->method_class != SNES_NONLINEAR_EQUATIONS) 880 SETERRQ(1,0,"For SNES_NONLINEAR_EQUATIONS only"); 881 if (!snes->computejacobian) return 0; 882 PLogEventBegin(SNES_JacobianEval,snes,X,*A,*B); 883 *flg = DIFFERENT_NONZERO_PATTERN; 884 ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP); CHKERRQ(ierr); 885 PLogEventEnd(SNES_JacobianEval,snes,X,*A,*B); 886 /* make sure user returned a correct Jacobian and preconditioner */ 887 PetscValidHeaderSpecific(*A,MAT_COOKIE); 888 PetscValidHeaderSpecific(*B,MAT_COOKIE); 889 return 0; 890 } 891 892 #undef __FUNC__ 893 #define __FUNC__ "SNESComputeHessian" 894 /*@ 895 SNESComputeHessian - Computes the Hessian matrix that has been 896 set with SNESSetHessian(). 897 898 Input Parameters: 899 . snes - the SNES context 900 . x - input vector 901 902 Output Parameters: 903 . A - Hessian matrix 904 . B - optional preconditioning matrix 905 . flag - flag indicating matrix structure 906 907 Notes: 908 Most users should not need to explicitly call this routine, as it 909 is used internally within the nonlinear solvers. 910 911 See SLESSetOperators() for important information about setting the 912 flag parameter. 913 914 SNESComputeHessian() is valid only for 915 SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 916 SNES_NONLINEAR_EQUATIONS methods is SNESComputeJacobian(). 917 918 .keywords: SNES, compute, Hessian, matrix 919 920 .seealso: SNESSetHessian(), SLESSetOperators(), SNESComputeGradient(), 921 SNESComputeMinimizationFunction() 922 @*/ 923 int SNESComputeHessian(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag) 924 { 925 int ierr; 926 if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) 927 SETERRQ(1,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 928 if (!snes->computejacobian) return 0; 929 PLogEventBegin(SNES_HessianEval,snes,x,*A,*B); 930 *flag = DIFFERENT_NONZERO_PATTERN; 931 ierr = (*snes->computejacobian)(snes,x,A,B,flag,snes->jacP); CHKERRQ(ierr); 932 PLogEventEnd(SNES_HessianEval,snes,x,*A,*B); 933 /* make sure user returned a correct Jacobian and preconditioner */ 934 PetscValidHeaderSpecific(*A,MAT_COOKIE); 935 PetscValidHeaderSpecific(*B,MAT_COOKIE); 936 return 0; 937 } 938 939 #undef __FUNC__ 940 #define __FUNC__ "SNESSetJacobian" 941 /*@C 942 SNESSetJacobian - Sets the function to compute Jacobian as well as the 943 location to store the matrix. 944 945 Input Parameters: 946 . snes - the SNES context 947 . A - Jacobian matrix 948 . B - preconditioner matrix (usually same as the Jacobian) 949 . func - Jacobian evaluation routine 950 . ctx - [optional] user-defined context for private data for the 951 Jacobian evaluation routine (may be PETSC_NULL) 952 953 Calling sequence of func: 954 . func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 955 956 . x - input vector 957 . A - Jacobian matrix 958 . B - preconditioner matrix, usually the same as A 959 . flag - flag indicating information about the preconditioner matrix 960 structure (same as flag in SLESSetOperators()) 961 . ctx - [optional] user-defined Jacobian context 962 963 Notes: 964 See SLESSetOperators() for important information about setting the flag 965 output parameter in the routine func(). Be sure to read this information! 966 967 The routine func() takes Mat * as the matrix arguments rather than Mat. 968 This allows the Jacobian evaluation routine to replace A and/or B with a 969 completely new new matrix structure (not just different matrix elements) 970 when appropriate, for instance, if the nonzero structure is changing 971 throughout the global iterations. 972 973 .keywords: SNES, nonlinear, set, Jacobian, matrix 974 975 .seealso: SLESSetOperators(), SNESSetFunction() 976 @*/ 977 int SNESSetJacobian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*, 978 MatStructure*,void*),void *ctx) 979 { 980 PetscValidHeaderSpecific(snes,SNES_COOKIE); 981 if (snes->method_class != SNES_NONLINEAR_EQUATIONS) 982 SETERRQ(1,0,"For SNES_NONLINEAR_EQUATIONS only"); 983 snes->computejacobian = func; 984 snes->jacP = ctx; 985 snes->jacobian = A; 986 snes->jacobian_pre = B; 987 return 0; 988 } 989 990 #undef __FUNC__ 991 #define __FUNC__ "SNESGetJacobian" 992 /*@ 993 SNESGetJacobian - Returns the Jacobian matrix and optionally the user 994 provided context for evaluating the Jacobian. 995 996 Input Parameter: 997 . snes - the nonlinear solver context 998 999 Output Parameters: 1000 . A - location to stash Jacobian matrix (or PETSC_NULL) 1001 . B - location to stash preconditioner matrix (or PETSC_NULL) 1002 . ctx - location to stash Jacobian ctx (or PETSC_NULL) 1003 1004 .seealso: SNESSetJacobian(), SNESComputeJacobian() 1005 @*/ 1006 int SNESGetJacobian(SNES snes,Mat *A,Mat *B, void **ctx) 1007 { 1008 if (snes->method_class != SNES_NONLINEAR_EQUATIONS) 1009 SETERRQ(1,0,"For SNES_NONLINEAR_EQUATIONS only"); 1010 if (A) *A = snes->jacobian; 1011 if (B) *B = snes->jacobian_pre; 1012 if (ctx) *ctx = snes->jacP; 1013 return 0; 1014 } 1015 1016 #undef __FUNC__ 1017 #define __FUNC__ "SNESSetHessian" 1018 /*@C 1019 SNESSetHessian - Sets the function to compute Hessian as well as the 1020 location to store the matrix. 1021 1022 Input Parameters: 1023 . snes - the SNES context 1024 . A - Hessian matrix 1025 . B - preconditioner matrix (usually same as the Hessian) 1026 . func - Jacobian evaluation routine 1027 . ctx - [optional] user-defined context for private data for the 1028 Hessian evaluation routine (may be PETSC_NULL) 1029 1030 Calling sequence of func: 1031 . func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 1032 1033 . x - input vector 1034 . A - Hessian matrix 1035 . B - preconditioner matrix, usually the same as A 1036 . flag - flag indicating information about the preconditioner matrix 1037 structure (same as flag in SLESSetOperators()) 1038 . ctx - [optional] user-defined Hessian context 1039 1040 Notes: 1041 See SLESSetOperators() for important information about setting the flag 1042 output parameter in the routine func(). Be sure to read this information! 1043 1044 The function func() takes Mat * as the matrix arguments rather than Mat. 1045 This allows the Hessian evaluation routine to replace A and/or B with a 1046 completely new new matrix structure (not just different matrix elements) 1047 when appropriate, for instance, if the nonzero structure is changing 1048 throughout the global iterations. 1049 1050 .keywords: SNES, nonlinear, set, Hessian, matrix 1051 1052 .seealso: SNESSetMinimizationFunction(), SNESSetGradient(), SLESSetOperators() 1053 @*/ 1054 int SNESSetHessian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*, 1055 MatStructure*,void*),void *ctx) 1056 { 1057 PetscValidHeaderSpecific(snes,SNES_COOKIE); 1058 if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) 1059 SETERRQ(1,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 1060 snes->computejacobian = func; 1061 snes->jacP = ctx; 1062 snes->jacobian = A; 1063 snes->jacobian_pre = B; 1064 return 0; 1065 } 1066 1067 #undef __FUNC__ 1068 #define __FUNC__ "SNESGetHessian" 1069 /*@ 1070 SNESGetHessian - Returns the Hessian matrix and optionally the user 1071 provided context for evaluating the Hessian. 1072 1073 Input Parameter: 1074 . snes - the nonlinear solver context 1075 1076 Output Parameters: 1077 . A - location to stash Hessian matrix (or PETSC_NULL) 1078 . B - location to stash preconditioner matrix (or PETSC_NULL) 1079 . ctx - location to stash Hessian ctx (or PETSC_NULL) 1080 1081 .seealso: SNESSetHessian(), SNESComputeHessian() 1082 @*/ 1083 int SNESGetHessian(SNES snes,Mat *A,Mat *B, void **ctx) 1084 { 1085 if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) 1086 SETERRQ(1,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 1087 if (A) *A = snes->jacobian; 1088 if (B) *B = snes->jacobian_pre; 1089 if (ctx) *ctx = snes->jacP; 1090 return 0; 1091 } 1092 1093 /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 1094 1095 #undef __FUNC__ 1096 #define __FUNC__ "SNESSetUp" 1097 /*@ 1098 SNESSetUp - Sets up the internal data structures for the later use 1099 of a nonlinear solver. 1100 1101 Input Parameter: 1102 . snes - the SNES context 1103 . x - the solution vector 1104 1105 Notes: 1106 For basic use of the SNES solvers the user need not explicitly call 1107 SNESSetUp(), since these actions will automatically occur during 1108 the call to SNESSolve(). However, if one wishes to control this 1109 phase separately, SNESSetUp() should be called after SNESCreate() 1110 and optional routines of the form SNESSetXXX(), but before SNESSolve(). 1111 1112 .keywords: SNES, nonlinear, setup 1113 1114 .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 1115 @*/ 1116 int SNESSetUp(SNES snes,Vec x) 1117 { 1118 int ierr, flg; 1119 PetscValidHeaderSpecific(snes,SNES_COOKIE); 1120 PetscValidHeaderSpecific(x,VEC_COOKIE); 1121 snes->vec_sol = snes->vec_sol_always = x; 1122 1123 ierr = OptionsHasName(snes->prefix,"-snes_mf_operator", &flg); CHKERRQ(ierr); 1124 /* 1125 This version replaces the user provided Jacobian matrix with a 1126 matrix-free version but still employs the user-provided preconditioner matrix 1127 */ 1128 if (flg) { 1129 Mat J; 1130 ierr = SNESDefaultMatrixFreeMatCreate(snes,snes->vec_sol,&J);CHKERRQ(ierr); 1131 PLogObjectParent(snes,J); 1132 snes->mfshell = J; 1133 if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 1134 snes->jacobian = J; 1135 PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Jacobian routines\n"); 1136 } 1137 else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 1138 snes->jacobian = J; 1139 PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Hessian routines\n"); 1140 } else SETERRQ(1,0,"Method class doesn't support matrix-free operator option"); 1141 } 1142 ierr = OptionsHasName(snes->prefix,"-snes_mf", &flg); CHKERRQ(ierr); 1143 /* 1144 This version replaces both the user-provided Jacobian and the user- 1145 provided preconditioner matrix with the default matrix free version. 1146 */ 1147 if (flg) { 1148 Mat J; 1149 ierr = SNESDefaultMatrixFreeMatCreate(snes,snes->vec_sol,&J);CHKERRQ(ierr); 1150 PLogObjectParent(snes,J); 1151 snes->mfshell = J; 1152 if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 1153 ierr = SNESSetJacobian(snes,J,J,0,snes->funP); CHKERRQ(ierr); 1154 PLogInfo(snes,"SNESSetUp: Setting default matrix-free Jacobian routines\n"); 1155 } 1156 else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 1157 ierr = SNESSetHessian(snes,J,J,0,snes->funP); CHKERRQ(ierr); 1158 PLogInfo(snes,"SNESSetUp: Setting default matrix-free Hessian routines\n"); 1159 } else SETERRQ(1,0,"Method class doesn't support matrix-free option"); 1160 } 1161 if ((snes->method_class == SNES_NONLINEAR_EQUATIONS)) { 1162 if (!snes->vec_func) SETERRQ(1,0,"Must call SNESSetFunction() first"); 1163 if (!snes->computefunction) SETERRQ(1,0,"Must call SNESSetFunction() first"); 1164 if (!snes->jacobian) SETERRQ(1,0,"Must call SNESSetJacobian() first"); 1165 if (snes->vec_func == snes->vec_sol) SETERRQ(1,0,"Solution vector cannot be function vector"); 1166 1167 /* Set the KSP stopping criterion to use the Eisenstat-Walker method */ 1168 if (snes->ksp_ewconv && snes->type != SNES_EQ_TR) { 1169 SLES sles; KSP ksp; 1170 ierr = SNESGetSLES(snes,&sles); CHKERRQ(ierr); 1171 ierr = SLESGetKSP(sles,&ksp); CHKERRQ(ierr); 1172 ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private, 1173 (void *)snes); CHKERRQ(ierr); 1174 } 1175 } else if ((snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION)) { 1176 if (!snes->vec_func) SETERRQ(1,0,"Must call SNESSetGradient() first"); 1177 if (!snes->computefunction) SETERRQ(1,0,"Must call SNESSetGradient() first"); 1178 if (!snes->computeumfunction) 1179 SETERRQ(1,0,"Must call SNESSetMinimizationFunction() first"); 1180 if (!snes->jacobian) SETERRQ(1,0,"Must call SNESSetHessian() first"); 1181 } else SETERRQ(1,0,"Unknown method class"); 1182 if (snes->setup) {ierr = (*snes->setup)(snes); CHKERRQ(ierr);} 1183 snes->setup_called = 1; 1184 return 0; 1185 } 1186 1187 #undef __FUNC__ 1188 #define __FUNC__ "SNESDestroy" 1189 /*@C 1190 SNESDestroy - Destroys the nonlinear solver context that was created 1191 with SNESCreate(). 1192 1193 Input Parameter: 1194 . snes - the SNES context 1195 1196 .keywords: SNES, nonlinear, destroy 1197 1198 .seealso: SNESCreate(), SNESSolve() 1199 @*/ 1200 int SNESDestroy(SNES snes) 1201 { 1202 int ierr; 1203 PetscValidHeaderSpecific(snes,SNES_COOKIE); 1204 ierr = (*(snes)->destroy)((PetscObject)snes); CHKERRQ(ierr); 1205 if (snes->kspconvctx) PetscFree(snes->kspconvctx); 1206 if (snes->mfshell) MatDestroy(snes->mfshell); 1207 ierr = SLESDestroy(snes->sles); CHKERRQ(ierr); 1208 if (snes->xmonitor) SNESLGMonitorDestroy(snes->xmonitor); 1209 if (snes->vwork) VecDestroyVecs(snes->vwork,snes->nvwork); 1210 PLogObjectDestroy((PetscObject)snes); 1211 PetscHeaderDestroy((PetscObject)snes); 1212 return 0; 1213 } 1214 1215 /* ----------- Routines to set solver parameters ---------- */ 1216 1217 #undef __FUNC__ 1218 #define __FUNC__ "SNESSetTolerances" 1219 /*@ 1220 SNESSetTolerances - Sets various parameters used in convergence tests. 1221 1222 Input Parameters: 1223 . snes - the SNES context 1224 . atol - absolute convergence tolerance 1225 . rtol - relative convergence tolerance 1226 . stol - convergence tolerance in terms of the norm 1227 of the change in the solution between steps 1228 . maxit - maximum number of iterations 1229 . maxf - maximum number of function evaluations 1230 1231 Options Database Keys: 1232 $ -snes_atol <atol> 1233 $ -snes_rtol <rtol> 1234 $ -snes_stol <stol> 1235 $ -snes_max_it <maxit> 1236 $ -snes_max_funcs <maxf> 1237 1238 Notes: 1239 The default maximum number of iterations is 50. 1240 The default maximum number of function evaluations is 1000. 1241 1242 .keywords: SNES, nonlinear, set, convergence, tolerances 1243 1244 .seealso: SNESSetTrustRegionTolerance(), SNESSetMinimizationFunctionTolerance() 1245 @*/ 1246 int SNESSetTolerances(SNES snes,double atol,double rtol,double stol,int maxit,int maxf) 1247 { 1248 PetscValidHeaderSpecific(snes,SNES_COOKIE); 1249 if (atol != PETSC_DEFAULT) snes->atol = atol; 1250 if (rtol != PETSC_DEFAULT) snes->rtol = rtol; 1251 if (stol != PETSC_DEFAULT) snes->xtol = stol; 1252 if (maxit != PETSC_DEFAULT) snes->max_its = maxit; 1253 if (maxf != PETSC_DEFAULT) snes->max_funcs = maxf; 1254 return 0; 1255 } 1256 1257 #undef __FUNC__ 1258 #define __FUNC__ "SNESGetTolerances" 1259 /*@ 1260 SNESGetTolerances - Gets various parameters used in convergence tests. 1261 1262 Input Parameters: 1263 . snes - the SNES context 1264 . atol - absolute convergence tolerance 1265 . rtol - relative convergence tolerance 1266 . stol - convergence tolerance in terms of the norm 1267 of the change in the solution between steps 1268 . maxit - maximum number of iterations 1269 . maxf - maximum number of function evaluations 1270 1271 Notes: 1272 The user can specify PETSC_NULL for any parameter that is not needed. 1273 1274 .keywords: SNES, nonlinear, get, convergence, tolerances 1275 1276 .seealso: SNESSetTolerances() 1277 @*/ 1278 int SNESGetTolerances(SNES snes,double *atol,double *rtol,double *stol,int *maxit,int *maxf) 1279 { 1280 PetscValidHeaderSpecific(snes,SNES_COOKIE); 1281 if (atol) *atol = snes->atol; 1282 if (rtol) *rtol = snes->rtol; 1283 if (stol) *stol = snes->xtol; 1284 if (maxit) *maxit = snes->max_its; 1285 if (maxf) *maxf = snes->max_funcs; 1286 return 0; 1287 } 1288 1289 #undef __FUNC__ 1290 #define __FUNC__ "SNESSetTrustRegionTolerance" 1291 /*@ 1292 SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 1293 1294 Input Parameters: 1295 . snes - the SNES context 1296 . tol - tolerance 1297 1298 Options Database Key: 1299 $ -snes_trtol <tol> 1300 1301 .keywords: SNES, nonlinear, set, trust region, tolerance 1302 1303 .seealso: SNESSetTolerances(), SNESSetMinimizationFunctionTolerance() 1304 @*/ 1305 int SNESSetTrustRegionTolerance(SNES snes,double tol) 1306 { 1307 PetscValidHeaderSpecific(snes,SNES_COOKIE); 1308 snes->deltatol = tol; 1309 return 0; 1310 } 1311 1312 #undef __FUNC__ 1313 #define __FUNC__ "SNESSetMinimizationFunctionTolerance" 1314 /*@ 1315 SNESSetMinimizationFunctionTolerance - Sets the minimum allowable function tolerance 1316 for unconstrained minimization solvers. 1317 1318 Input Parameters: 1319 . snes - the SNES context 1320 . ftol - minimum function tolerance 1321 1322 Options Database Key: 1323 $ -snes_fmin <ftol> 1324 1325 Note: 1326 SNESSetMinimizationFunctionTolerance() is valid for SNES_UNCONSTRAINED_MINIMIZATION 1327 methods only. 1328 1329 .keywords: SNES, nonlinear, set, minimum, convergence, function, tolerance 1330 1331 .seealso: SNESSetTolerances(), SNESSetTrustRegionTolerance() 1332 @*/ 1333 int SNESSetMinimizationFunctionTolerance(SNES snes,double ftol) 1334 { 1335 PetscValidHeaderSpecific(snes,SNES_COOKIE); 1336 snes->fmin = ftol; 1337 return 0; 1338 } 1339 1340 /* ------------ Routines to set performance monitoring options ----------- */ 1341 1342 #undef __FUNC__ 1343 #define __FUNC__ "SNESSetMonitor" 1344 /*@C 1345 SNESSetMonitor - Sets the function that is to be used at every 1346 iteration of the nonlinear solver to display the iteration's 1347 progress. 1348 1349 Input Parameters: 1350 . snes - the SNES context 1351 . func - monitoring routine 1352 . mctx - [optional] user-defined context for private data for the 1353 monitor routine (may be PETSC_NULL) 1354 1355 Calling sequence of func: 1356 int func(SNES snes,int its, Vec x,Vec f,double norm,void *mctx) 1357 1358 $ snes - the SNES context 1359 $ its - iteration number 1360 $ mctx - [optional] monitoring context 1361 $ 1362 $ SNES_NONLINEAR_EQUATIONS methods: 1363 $ norm - 2-norm function value (may be estimated) 1364 $ 1365 $ SNES_UNCONSTRAINED_MINIMIZATION methods: 1366 $ norm - 2-norm gradient value (may be estimated) 1367 1368 Options Database Keys: 1369 $ -snes_monitor : sets SNESDefaultMonitor() 1370 $ -snes_xmonitor : sets line graph monitor, 1371 $ uses SNESLGMonitorCreate() 1372 $ -snes_cancelmonitors : cancels all monitors that have 1373 $ been hardwired into a code by 1374 $ calls to SNESSetMonitor(), but 1375 $ does not cancel those set via 1376 $ the options database. 1377 1378 1379 Notes: 1380 Several different monitoring routines may be set by calling 1381 SNESSetMonitor() multiple times; all will be called in the 1382 order in which they were set. 1383 1384 .keywords: SNES, nonlinear, set, monitor 1385 1386 .seealso: SNESDefaultMonitor() 1387 @*/ 1388 int SNESSetMonitor( SNES snes, int (*func)(SNES,int,double,void*),void *mctx ) 1389 { 1390 if (!func) { 1391 snes->numbermonitors = 0; 1392 return 0; 1393 } 1394 if (snes->numbermonitors >= MAXSNESMONITORS) { 1395 SETERRQ(1,0,"Too many monitors set"); 1396 } 1397 1398 snes->monitor[snes->numbermonitors] = func; 1399 snes->monitorcontext[snes->numbermonitors++] = (void*)mctx; 1400 return 0; 1401 } 1402 1403 #undef __FUNC__ 1404 #define __FUNC__ "SNESSetConvergenceTest" 1405 /*@C 1406 SNESSetConvergenceTest - Sets the function that is to be used 1407 to test for convergence of the nonlinear iterative solution. 1408 1409 Input Parameters: 1410 . snes - the SNES context 1411 . func - routine to test for convergence 1412 . cctx - [optional] context for private data for the convergence routine 1413 (may be PETSC_NULL) 1414 1415 Calling sequence of func: 1416 int func (SNES snes,double xnorm,double gnorm, 1417 double f,void *cctx) 1418 1419 $ snes - the SNES context 1420 $ cctx - [optional] convergence context 1421 $ xnorm - 2-norm of current iterate 1422 $ 1423 $ SNES_NONLINEAR_EQUATIONS methods: 1424 $ gnorm - 2-norm of current step 1425 $ f - 2-norm of function 1426 $ 1427 $ SNES_UNCONSTRAINED_MINIMIZATION methods: 1428 $ gnorm - 2-norm of current gradient 1429 $ f - function value 1430 1431 .keywords: SNES, nonlinear, set, convergence, test 1432 1433 .seealso: SNESConverged_EQ_LS(), SNESConverged_EQ_TR(), 1434 SNESConverged_UM_LS(), SNESConverged_UM_TR() 1435 @*/ 1436 int SNESSetConvergenceTest(SNES snes,int (*func)(SNES,double,double,double,void*),void *cctx) 1437 { 1438 (snes)->converged = func; 1439 (snes)->cnvP = cctx; 1440 return 0; 1441 } 1442 1443 #undef __FUNC__ 1444 #define __FUNC__ "SNESScaleStep_Private" 1445 /* 1446 SNESScaleStep_Private - Scales a step so that its length is less than the 1447 positive parameter delta. 1448 1449 Input Parameters: 1450 . snes - the SNES context 1451 . y - approximate solution of linear system 1452 . fnorm - 2-norm of current function 1453 . delta - trust region size 1454 1455 Output Parameters: 1456 . gpnorm - predicted function norm at the new point, assuming local 1457 linearization. The value is zero if the step lies within the trust 1458 region, and exceeds zero otherwise. 1459 . ynorm - 2-norm of the step 1460 1461 Note: 1462 For non-trust region methods such as SNES_EQ_LS, the parameter delta 1463 is set to be the maximum allowable step size. 1464 1465 .keywords: SNES, nonlinear, scale, step 1466 */ 1467 int SNESScaleStep_Private(SNES snes,Vec y,double *fnorm,double *delta, 1468 double *gpnorm,double *ynorm) 1469 { 1470 double norm; 1471 Scalar cnorm; 1472 VecNorm(y,NORM_2, &norm ); 1473 if (norm > *delta) { 1474 norm = *delta/norm; 1475 *gpnorm = (1.0 - norm)*(*fnorm); 1476 cnorm = norm; 1477 VecScale( &cnorm, y ); 1478 *ynorm = *delta; 1479 } else { 1480 *gpnorm = 0.0; 1481 *ynorm = norm; 1482 } 1483 return 0; 1484 } 1485 1486 #undef __FUNC__ 1487 #define __FUNC__ "SNESSolve" 1488 /*@ 1489 SNESSolve - Solves a nonlinear system. Call SNESSolve after calling 1490 SNESCreate() and optional routines of the form SNESSetXXX(). 1491 1492 Input Parameter: 1493 . snes - the SNES context 1494 . x - the solution vector 1495 1496 Output Parameter: 1497 its - number of iterations until termination 1498 1499 Note: 1500 The user should initialize the vector, x, with the initial guess 1501 for the nonlinear solve prior to calling SNESSolve. In particular, 1502 to employ an initial guess of zero, the user should explicitly set 1503 this vector to zero by calling VecSet(). 1504 1505 .keywords: SNES, nonlinear, solve 1506 1507 .seealso: SNESCreate(), SNESDestroy() 1508 @*/ 1509 int SNESSolve(SNES snes,Vec x,int *its) 1510 { 1511 int ierr, flg; 1512 1513 PetscValidHeaderSpecific(snes,SNES_COOKIE); 1514 PetscValidIntPointer(its); 1515 if (!snes->setup_called) {ierr = SNESSetUp(snes,x); CHKERRQ(ierr);} 1516 else {snes->vec_sol = snes->vec_sol_always = x;} 1517 PLogEventBegin(SNES_Solve,snes,0,0,0); 1518 snes->nfuncs = 0; snes->linear_its = 0; snes->nfailures = 0; 1519 ierr = (*(snes)->solve)(snes,its); CHKERRQ(ierr); 1520 PLogEventEnd(SNES_Solve,snes,0,0,0); 1521 ierr = OptionsHasName(PETSC_NULL,"-snes_view", &flg); CHKERRQ(ierr); 1522 if (flg) { ierr = SNESView(snes,VIEWER_STDOUT_WORLD); CHKERRQ(ierr); } 1523 return 0; 1524 } 1525 1526 /* --------- Internal routines for SNES Package --------- */ 1527 static NRList *__SNESList = 0; 1528 1529 #undef __FUNC__ 1530 #define __FUNC__ "SNESSetType" 1531 /*@ 1532 SNESSetType - Sets the method for the nonlinear solver. 1533 1534 Input Parameters: 1535 . snes - the SNES context 1536 . method - a known method 1537 1538 Options Database Command: 1539 $ -snes_type <method> 1540 $ Use -help for a list of available methods 1541 $ (for instance, ls or tr) 1542 1543 Notes: 1544 See "petsc/include/snes.h" for available methods (for instance) 1545 $ Systems of nonlinear equations: 1546 $ SNES_EQ_LS - Newton's method with line search 1547 $ SNES_EQ_TR - Newton's method with trust region 1548 $ Unconstrained minimization: 1549 $ SNES_UM_TR - Newton's method with trust region 1550 $ SNES_UM_LS - Newton's method with line search 1551 1552 Normally, it is best to use the SNESSetFromOptions() command and then 1553 set the SNES solver type from the options database rather than by using 1554 this routine. Using the options database provides the user with 1555 maximum flexibility in evaluating the many nonlinear solvers. 1556 The SNESSetType() routine is provided for those situations where it 1557 is necessary to set the nonlinear solver independently of the command 1558 line or options database. This might be the case, for example, when 1559 the choice of solver changes during the execution of the program, 1560 and the user's application is taking responsibility for choosing the 1561 appropriate method. In other words, this routine is for the advanced user. 1562 1563 .keywords: SNES, set, method 1564 @*/ 1565 int SNESSetType(SNES snes,SNESType method) 1566 { 1567 int (*r)(SNES); 1568 PetscValidHeaderSpecific(snes,SNES_COOKIE); 1569 if (snes->type == (int) method) return 0; 1570 1571 /* Get the function pointers for the iterative method requested */ 1572 if (!__SNESList) {SNESRegisterAll();} 1573 if (!__SNESList) {SETERRQ(1,0,"Could not get methods");} 1574 r = (int (*)(SNES))NRFindRoutine( __SNESList, (int)method, (char *)0 ); 1575 if (!r) {SETERRQ(1,0,"Unknown method");} 1576 if (snes->data) PetscFree(snes->data); 1577 snes->set_method_called = 1; 1578 return (*r)(snes); 1579 } 1580 1581 /* --------------------------------------------------------------------- */ 1582 #undef __FUNC__ 1583 #define __FUNC__ "SNESRegister" 1584 /*@C 1585 SNESRegister - Adds the method to the nonlinear solver package, given 1586 a function pointer and a nonlinear solver name of the type SNESType. 1587 1588 Input Parameters: 1589 . name - for instance SNES_EQ_LS, SNES_EQ_TR, ... 1590 . sname - corresponding string for name 1591 . create - routine to create method context 1592 1593 .keywords: SNES, nonlinear, register 1594 1595 .seealso: SNESRegisterAll(), SNESRegisterDestroy() 1596 @*/ 1597 int SNESRegister(int name, char *sname, int (*create)(SNES)) 1598 { 1599 int ierr; 1600 if (!__SNESList) {ierr = NRCreate(&__SNESList); CHKERRQ(ierr);} 1601 NRRegister( __SNESList, name, sname, (int (*)(void*))create ); 1602 return 0; 1603 } 1604 1605 /* --------------------------------------------------------------------- */ 1606 #undef __FUNC__ 1607 #define __FUNC__ "SNESRegisterDestroy" 1608 /*@C 1609 SNESRegisterDestroy - Frees the list of nonlinear solvers that were 1610 registered by SNESRegister(). 1611 1612 .keywords: SNES, nonlinear, register, destroy 1613 1614 .seealso: SNESRegisterAll(), SNESRegisterAll() 1615 @*/ 1616 int SNESRegisterDestroy() 1617 { 1618 if (__SNESList) { 1619 NRDestroy( __SNESList ); 1620 __SNESList = 0; 1621 } 1622 return 0; 1623 } 1624 1625 #undef __FUNC__ 1626 #define __FUNC__ "SNESGetTypeFromOptions_Private" 1627 /* 1628 SNESGetTypeFromOptions_Private - Sets the selected method from the 1629 options database. 1630 1631 Input Parameter: 1632 . ctx - the SNES context 1633 1634 Output Parameter: 1635 . method - solver method 1636 1637 Returns: 1638 Returns 1 if the method is found; 0 otherwise. 1639 1640 Options Database Key: 1641 $ -snes_type method 1642 */ 1643 int SNESGetTypeFromOptions_Private(SNES ctx,SNESType *method,int *flg) 1644 { 1645 int ierr; 1646 char sbuf[50]; 1647 1648 ierr = OptionsGetString(ctx->prefix,"-snes_type",sbuf,50,flg);CHKERRQ(ierr); 1649 if (*flg) { 1650 if (!__SNESList) {ierr = SNESRegisterAll(); CHKERRQ(ierr);} 1651 *method = (SNESType)NRFindID( __SNESList, sbuf ); 1652 } 1653 return 0; 1654 } 1655 1656 #undef __FUNC__ 1657 #define __FUNC__ "SNESGetType" 1658 /*@C 1659 SNESGetType - Gets the SNES method type and name (as a string). 1660 1661 Input Parameter: 1662 . snes - nonlinear solver context 1663 1664 Output Parameter: 1665 . method - SNES method (or use PETSC_NULL) 1666 . name - name of SNES method (or use PETSC_NULL) 1667 1668 .keywords: SNES, nonlinear, get, method, name 1669 @*/ 1670 int SNESGetType(SNES snes, SNESType *method,char **name) 1671 { 1672 int ierr; 1673 if (!__SNESList) {ierr = SNESRegisterAll(); CHKERRQ(ierr);} 1674 if (method) *method = (SNESType) snes->type; 1675 if (name) *name = NRFindName( __SNESList, (int) snes->type ); 1676 return 0; 1677 } 1678 1679 #include <stdio.h> 1680 #undef __FUNC__ 1681 #define __FUNC__ "SNESPrintTypes_Private" 1682 /* 1683 SNESPrintTypes_Private - Prints the SNES methods available from the 1684 options database. 1685 1686 Input Parameters: 1687 . comm - communicator (usually MPI_COMM_WORLD) 1688 . prefix - prefix (usually "-") 1689 . name - the options database name (by default "snes_type") 1690 */ 1691 int SNESPrintTypes_Private(MPI_Comm comm,char* prefix,char *name) 1692 { 1693 FuncList *entry; 1694 if (!__SNESList) {SNESRegisterAll();} 1695 entry = __SNESList->head; 1696 PetscPrintf(comm," %s%s (one of)",prefix,name); 1697 while (entry) { 1698 PetscPrintf(comm," %s",entry->name); 1699 entry = entry->next; 1700 } 1701 PetscPrintf(comm,"\n"); 1702 return 0; 1703 } 1704 1705 #undef __FUNC__ 1706 #define __FUNC__ "SNESGetSolution" 1707 /*@C 1708 SNESGetSolution - Returns the vector where the approximate solution is 1709 stored. 1710 1711 Input Parameter: 1712 . snes - the SNES context 1713 1714 Output Parameter: 1715 . x - the solution 1716 1717 .keywords: SNES, nonlinear, get, solution 1718 1719 .seealso: SNESGetFunction(), SNESGetGradient(), SNESGetSolutionUpdate() 1720 @*/ 1721 int SNESGetSolution(SNES snes,Vec *x) 1722 { 1723 PetscValidHeaderSpecific(snes,SNES_COOKIE); 1724 *x = snes->vec_sol_always; 1725 return 0; 1726 } 1727 1728 #undef __FUNC__ 1729 #define __FUNC__ "SNESGetSolutionUpdate" 1730 /*@C 1731 SNESGetSolutionUpdate - Returns the vector where the solution update is 1732 stored. 1733 1734 Input Parameter: 1735 . snes - the SNES context 1736 1737 Output Parameter: 1738 . x - the solution update 1739 1740 Notes: 1741 This vector is implementation dependent. 1742 1743 .keywords: SNES, nonlinear, get, solution, update 1744 1745 .seealso: SNESGetSolution(), SNESGetFunction 1746 @*/ 1747 int SNESGetSolutionUpdate(SNES snes,Vec *x) 1748 { 1749 PetscValidHeaderSpecific(snes,SNES_COOKIE); 1750 *x = snes->vec_sol_update_always; 1751 return 0; 1752 } 1753 1754 #undef __FUNC__ 1755 #define __FUNC__ "SNESGetFunction" 1756 /*@C 1757 SNESGetFunction - Returns the vector where the function is stored. 1758 1759 Input Parameter: 1760 . snes - the SNES context 1761 1762 Output Parameter: 1763 . r - the function 1764 1765 Notes: 1766 SNESGetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only 1767 Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 1768 SNESGetMinimizationFunction() and SNESGetGradient(); 1769 1770 .keywords: SNES, nonlinear, get, function 1771 1772 .seealso: SNESSetFunction(), SNESGetSolution(), SNESGetMinimizationFunction(), 1773 SNESGetGradient() 1774 @*/ 1775 int SNESGetFunction(SNES snes,Vec *r) 1776 { 1777 PetscValidHeaderSpecific(snes,SNES_COOKIE); 1778 if (snes->method_class != SNES_NONLINEAR_EQUATIONS) SETERRQ(1,0, 1779 "For SNES_NONLINEAR_EQUATIONS only"); 1780 *r = snes->vec_func_always; 1781 return 0; 1782 } 1783 1784 #undef __FUNC__ 1785 #define __FUNC__ "SNESGetGradient" 1786 /*@C 1787 SNESGetGradient - Returns the vector where the gradient is stored. 1788 1789 Input Parameter: 1790 . snes - the SNES context 1791 1792 Output Parameter: 1793 . r - the gradient 1794 1795 Notes: 1796 SNESGetGradient() is valid for SNES_UNCONSTRAINED_MINIMIZATION methods 1797 only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 1798 SNESGetFunction(). 1799 1800 .keywords: SNES, nonlinear, get, gradient 1801 1802 .seealso: SNESGetMinimizationFunction(), SNESGetSolution(), SNESGetFunction() 1803 @*/ 1804 int SNESGetGradient(SNES snes,Vec *r) 1805 { 1806 PetscValidHeaderSpecific(snes,SNES_COOKIE); 1807 if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,0, 1808 "For SNES_UNCONSTRAINED_MINIMIZATION only"); 1809 *r = snes->vec_func_always; 1810 return 0; 1811 } 1812 1813 #undef __FUNC__ 1814 #define __FUNC__ "SNESGetMinimizationFunction" 1815 /*@ 1816 SNESGetMinimizationFunction - Returns the scalar function value for 1817 unconstrained minimization problems. 1818 1819 Input Parameter: 1820 . snes - the SNES context 1821 1822 Output Parameter: 1823 . r - the function 1824 1825 Notes: 1826 SNESGetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 1827 methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 1828 SNESGetFunction(). 1829 1830 .keywords: SNES, nonlinear, get, function 1831 1832 .seealso: SNESGetGradient(), SNESGetSolution(), SNESGetFunction() 1833 @*/ 1834 int SNESGetMinimizationFunction(SNES snes,double *r) 1835 { 1836 PetscValidHeaderSpecific(snes,SNES_COOKIE); 1837 PetscValidScalarPointer(r); 1838 if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) SETERRQ(1,0, 1839 "For SNES_UNCONSTRAINED_MINIMIZATION only"); 1840 *r = snes->fc; 1841 return 0; 1842 } 1843 1844 #undef __FUNC__ 1845 #define __FUNC__ "SNESSetOptionsPrefix" 1846 /*@C 1847 SNESSetOptionsPrefix - Sets the prefix used for searching for all 1848 SNES options in the database. You must NOT include the - at the beginning of 1849 the prefix name. 1850 1851 Input Parameter: 1852 . snes - the SNES context 1853 . prefix - the prefix to prepend to all option names 1854 1855 .keywords: SNES, set, options, prefix, database 1856 1857 .seealso: SNESSetFromOptions() 1858 @*/ 1859 int SNESSetOptionsPrefix(SNES snes,char *prefix) 1860 { 1861 int ierr; 1862 1863 PetscValidHeaderSpecific(snes,SNES_COOKIE); 1864 ierr = PetscObjectSetOptionsPrefix((PetscObject)snes, prefix); CHKERRQ(ierr); 1865 ierr = SLESSetOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr); 1866 return 0; 1867 } 1868 1869 #undef __FUNC__ 1870 #define __FUNC__ "SNESAppendOptionsPrefix" 1871 /*@C 1872 SNESAppendOptionsPrefix - Appends to the prefix used for searching for all 1873 SNES options in the database. You must NOT include the - at the beginning of 1874 the prefix name. 1875 1876 Input Parameter: 1877 . snes - the SNES context 1878 . prefix - the prefix to prepend to all option names 1879 1880 .keywords: SNES, append, options, prefix, database 1881 1882 .seealso: SNESGetOptionsPrefix() 1883 @*/ 1884 int SNESAppendOptionsPrefix(SNES snes,char *prefix) 1885 { 1886 int ierr; 1887 1888 PetscValidHeaderSpecific(snes,SNES_COOKIE); 1889 ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes, prefix); CHKERRQ(ierr); 1890 ierr = SLESAppendOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr); 1891 return 0; 1892 } 1893 1894 #undef __FUNC__ 1895 #define __FUNC__ "SNESGetOptionsPrefix" 1896 /*@ 1897 SNESGetOptionsPrefix - Sets the prefix used for searching for all 1898 SNES options in the database. 1899 1900 Input Parameter: 1901 . snes - the SNES context 1902 1903 Output Parameter: 1904 . prefix - pointer to the prefix string used 1905 1906 .keywords: SNES, get, options, prefix, database 1907 1908 .seealso: SNESAppendOptionsPrefix() 1909 @*/ 1910 int SNESGetOptionsPrefix(SNES snes,char **prefix) 1911 { 1912 int ierr; 1913 1914 PetscValidHeaderSpecific(snes,SNES_COOKIE); 1915 ierr = PetscObjectGetOptionsPrefix((PetscObject)snes, prefix); CHKERRQ(ierr); 1916 return 0; 1917 } 1918 1919 1920 1921 1922 1923