1 #ifdef PETSC_RCS_HEADER 2 static char vcid[] = "$Id: snes.c,v 1.173 1999/02/28 23:22:08 bsmith Exp curfman $"; 3 #endif 4 5 #include "src/snes/snesimpl.h" /*I "snes.h" I*/ 6 #include "src/sys/nreg.h" 7 8 int SNESRegisterAllCalled = 0; 9 FList SNESList = 0; 10 11 #undef __FUNC__ 12 #define __FUNC__ "SNESView" 13 /*@ 14 SNESView - Prints the SNES data structure. 15 16 Collective on SNES, unless Viewer is VIEWER_STDOUT_SELF 17 18 Input Parameters: 19 + SNES - the SNES context 20 - viewer - visualization context 21 22 Options Database Key: 23 . -snes_view - Calls SNESView() at end of SNESSolve() 24 25 Notes: 26 The available visualization contexts include 27 + VIEWER_STDOUT_SELF - standard output (default) 28 - VIEWER_STDOUT_WORLD - synchronized standard 29 output where only the first processor opens 30 the file. All other processors send their 31 data to the first processor to print. 32 33 The user can open an alternative visualization context with 34 ViewerASCIIOpen() - output to a specified file. 35 36 Level: beginner 37 38 .keywords: SNES, view 39 40 .seealso: ViewerASCIIOpen() 41 @*/ 42 int SNESView(SNES snes,Viewer viewer) 43 { 44 SNES_KSP_EW_ConvCtx *kctx; 45 int ierr; 46 SLES sles; 47 char *method; 48 ViewerType vtype; 49 50 PetscFunctionBegin; 51 PetscValidHeaderSpecific(snes,SNES_COOKIE); 52 if (viewer) {PetscValidHeader(viewer);} 53 else { viewer = VIEWER_STDOUT_SELF; } 54 55 ierr = ViewerGetType(viewer,&vtype); CHKERRQ(ierr); 56 if (PetscTypeCompare(vtype,ASCII_VIEWER)) { 57 ViewerASCIIPrintf(viewer,"SNES Object:\n"); 58 SNESGetType(snes,&method); 59 ViewerASCIIPrintf(viewer," method: %s\n",method); 60 if (snes->view) { 61 ierr = ViewerASCIIPushTab(viewer);CHKERRQ(ierr); 62 ierr = (*snes->view)(snes,viewer);CHKERRQ(ierr); 63 ierr = ViewerASCIIPopTab(viewer);CHKERRQ(ierr); 64 } 65 ViewerASCIIPrintf(viewer," maximum iterations=%d, maximum function evaluations=%d\n",snes->max_its,snes->max_funcs); 66 ViewerASCIIPrintf(viewer," tolerances: relative=%g, absolute=%g, truncation=%g, solution=%g\n", 67 snes->rtol, snes->atol, snes->trunctol, snes->xtol); 68 ViewerASCIIPrintf(viewer," total number of linear solver iterations=%d\n",snes->linear_its); 69 ViewerASCIIPrintf(viewer," total number of function evaluations=%d\n",snes->nfuncs); 70 if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 71 ViewerASCIIPrintf(viewer," min function tolerance=%g\n",snes->fmin); 72 } 73 if (snes->ksp_ewconv) { 74 kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx; 75 if (kctx) { 76 ViewerASCIIPrintf(viewer," Eisenstat-Walker computation of KSP relative tolerance (version %d)\n",kctx->version); 77 ViewerASCIIPrintf(viewer," rtol_0=%g, rtol_max=%g, threshold=%g\n",kctx->rtol_0,kctx->rtol_max,kctx->threshold); 78 ViewerASCIIPrintf(viewer," gamma=%g, alpha=%g, alpha2=%g\n",kctx->gamma,kctx->alpha,kctx->alpha2); 79 } 80 } 81 } else if (PetscTypeCompare(vtype,STRING_VIEWER)) { 82 ierr = SNESGetType(snes,&method);CHKERRQ(ierr); 83 ierr = ViewerStringSPrintf(viewer," %-3.3s",method);CHKERRQ(ierr); 84 } 85 ierr = SNESGetSLES(snes,&sles);CHKERRQ(ierr); 86 ierr = ViewerASCIIPushTab(viewer);CHKERRQ(ierr); 87 ierr = SLESView(sles,viewer); CHKERRQ(ierr); 88 ierr = ViewerASCIIPopTab(viewer);CHKERRQ(ierr); 89 PetscFunctionReturn(0); 90 } 91 92 /* 93 We retain a list of functions that also take SNES command 94 line options. These are called at the end SNESSetFromOptions() 95 */ 96 #define MAXSETFROMOPTIONS 5 97 static int numberofsetfromoptions; 98 static int (*othersetfromoptions[MAXSETFROMOPTIONS])(SNES); 99 100 #undef __FUNC__ 101 #define __FUNC__ "SNESAddOptionsChecker" 102 /*@ 103 SNESAddOptionsChecker - Adds an additional function to check for SNES options. 104 105 Not Collective 106 107 Input Parameter: 108 . snescheck - function that checks for options 109 110 Level: developer 111 112 .seealso: SNESSetFromOptions() 113 @*/ 114 int SNESAddOptionsChecker(int (*snescheck)(SNES) ) 115 { 116 PetscFunctionBegin; 117 if (numberofsetfromoptions >= MAXSETFROMOPTIONS) { 118 SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Too many options checkers, only 5 allowed"); 119 } 120 121 othersetfromoptions[numberofsetfromoptions++] = snescheck; 122 PetscFunctionReturn(0); 123 } 124 125 #undef __FUNC__ 126 #define __FUNC__ "SNESSetFromOptions" 127 /*@ 128 SNESSetFromOptions - Sets various SNES and SLES parameters from user options. 129 130 Collective on SNES 131 132 Input Parameter: 133 . snes - the SNES context 134 135 Options Database Keys: 136 + -snes_type <type> - SNES_EQ_LS, SNES_EQ_TR, SNES_UM_TR, SNES_UM_LS etc 137 . -snes_stol - convergence tolerance in terms of the norm 138 of the change in the solution between steps 139 . -snes_atol <atol> - absolute tolerance of residual norm 140 . -snes_rtol <rtol> - relative decrease in tolerance norm from initial 141 . -snes_max_it <max_it> - maximum number of iterations 142 . -snes_max_funcs <max_funcs> - maximum number of function evaluations 143 . -snes_trtol <trtol> - trust region tolerance 144 . -snes_no_convergence_test - skip convergence test in nonlinear or minimization 145 solver; hence iterations will continue until max_it 146 or some other criteria is reached. Saves expense 147 of convergence test 148 . -snes_monitor - prints residual norm at each iteration 149 . -snes_xmonitor - plots residual norm at each iteration 150 . -snes_fd - use finite differences to compute Jacobian; very slow, only for testing 151 - -snes_mf_ksp_monitor - if using matrix-free multiply then print h at each KSP iteration 152 153 Options Database for Eisenstat-Walker method: 154 + -snes_ksp_eq_conv - use Eisenstat-Walker method for determining linear system convergence 155 . -snes_ksp_eq_version ver - version of Eisenstat-Walker method 156 . -snes_ksp_ew_rtol0 <rtol0> - Sets rtol0 157 . -snes_ksp_ew_rtolmax <rtolmax> - Sets rtolmax 158 . -snes_ksp_ew_gamma <gamma> - Sets gamma 159 . -snes_ksp_ew_alpha <alpha> - Sets alpha 160 . -snes_ksp_ew_alpha2 <alpha2> - Sets alpha2 161 - -snes_ksp_ew_threshold <threshold> - Sets threshold 162 163 Notes: 164 To see all options, run your program with the -help option or consult 165 the users manual. 166 167 Level: beginner 168 169 .keywords: SNES, nonlinear, set, options, database 170 171 .seealso: SNESPrintHelp(), SNESSetOptionsPrefix() 172 @*/ 173 int SNESSetFromOptions(SNES snes) 174 { 175 char method[256]; 176 double tmp; 177 SLES sles; 178 int ierr, flg,i,loc[4],nmax = 4; 179 int version = PETSC_DEFAULT; 180 double rtol_0 = PETSC_DEFAULT; 181 double rtol_max = PETSC_DEFAULT; 182 double gamma2 = PETSC_DEFAULT; 183 double alpha = PETSC_DEFAULT; 184 double alpha2 = PETSC_DEFAULT; 185 double threshold = PETSC_DEFAULT; 186 187 PetscFunctionBegin; 188 loc[0] = PETSC_DECIDE; loc[1] = PETSC_DECIDE; loc[2] = 300; loc[3] = 300; 189 190 PetscValidHeaderSpecific(snes,SNES_COOKIE); 191 if (snes->setupcalled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call prior to SNESSetUp()"); 192 193 if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 194 ierr = OptionsGetString(snes->prefix,"-snes_type",method,256,&flg); 195 if (flg) { 196 ierr = SNESSetType(snes,(SNESType) method); CHKERRQ(ierr); 197 } 198 ierr = OptionsGetDouble(snes->prefix,"-snes_stol",&tmp, &flg); CHKERRQ(ierr); 199 if (flg) { 200 ierr = SNESSetTolerances(snes,PETSC_DEFAULT,PETSC_DEFAULT,tmp,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 201 } 202 ierr = OptionsGetDouble(snes->prefix,"-snes_atol",&tmp, &flg); CHKERRQ(ierr); 203 if (flg) { 204 ierr = SNESSetTolerances(snes,tmp,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 205 } 206 ierr = OptionsGetDouble(snes->prefix,"-snes_rtol",&tmp, &flg); CHKERRQ(ierr); 207 if (flg) { 208 ierr = SNESSetTolerances(snes,PETSC_DEFAULT,tmp,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr); 209 } 210 ierr = OptionsGetInt(snes->prefix,"-snes_max_it",&snes->max_its, &flg); CHKERRQ(ierr); 211 ierr = OptionsGetInt(snes->prefix,"-snes_max_funcs",&snes->max_funcs, &flg);CHKERRQ(ierr); 212 ierr = OptionsGetDouble(snes->prefix,"-snes_trtol",&tmp, &flg); CHKERRQ(ierr); 213 if (flg) { ierr = SNESSetTrustRegionTolerance(snes,tmp); CHKERRQ(ierr); } 214 ierr = OptionsGetDouble(snes->prefix,"-snes_fmin",&tmp, &flg); CHKERRQ(ierr); 215 if (flg) { ierr = SNESSetMinimizationFunctionTolerance(snes,tmp); CHKERRQ(ierr);} 216 ierr = OptionsHasName(snes->prefix,"-snes_ksp_ew_conv", &flg); CHKERRQ(ierr); 217 if (flg) { snes->ksp_ewconv = 1; } 218 ierr = OptionsGetInt(snes->prefix,"-snes_ksp_ew_version",&version,&flg); CHKERRQ(ierr); 219 ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtol0",&rtol_0,&flg); CHKERRQ(ierr); 220 ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_rtolmax",&rtol_max,&flg);CHKERRQ(ierr); 221 ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_gamma",&gamma2,&flg); CHKERRQ(ierr); 222 ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha",&alpha,&flg); CHKERRQ(ierr); 223 ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_alpha2",&alpha2,&flg); CHKERRQ(ierr); 224 ierr = OptionsGetDouble(snes->prefix,"-snes_ksp_ew_threshold",&threshold,&flg);CHKERRQ(ierr); 225 226 ierr = OptionsHasName(snes->prefix,"-snes_no_convergence_test",&flg); CHKERRQ(ierr); 227 if (flg) {snes->converged = 0;} 228 229 ierr = SNES_KSP_SetParametersEW(snes,version,rtol_0,rtol_max,gamma2,alpha, 230 alpha2,threshold); CHKERRQ(ierr); 231 ierr = OptionsHasName(snes->prefix,"-snes_cancelmonitors",&flg); CHKERRQ(ierr); 232 if (flg) {ierr = SNESClearMonitor(snes);CHKERRQ(ierr);} 233 ierr = OptionsHasName(snes->prefix,"-snes_monitor",&flg); CHKERRQ(ierr); 234 if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultMonitor,0);CHKERRQ(ierr);} 235 ierr = OptionsHasName(snes->prefix,"-snes_smonitor",&flg); CHKERRQ(ierr); 236 if (flg) {ierr = SNESSetMonitor(snes,SNESDefaultSMonitor,0); CHKERRQ(ierr);} 237 ierr = OptionsHasName(snes->prefix,"-snes_vecmonitor",&flg); CHKERRQ(ierr); 238 if (flg) {ierr = SNESSetMonitor(snes,SNESVecViewMonitor,0); CHKERRQ(ierr);} 239 ierr = OptionsGetIntArray(snes->prefix,"-snes_xmonitor",loc,&nmax,&flg);CHKERRQ(ierr); 240 if (flg) { 241 int rank = 0; 242 DrawLG lg; 243 MPI_Initialized(&rank); 244 if (rank) MPI_Comm_rank(snes->comm,&rank); 245 if (!rank) { 246 ierr = SNESLGMonitorCreate(0,0,loc[0],loc[1],loc[2],loc[3],&lg); CHKERRQ(ierr); 247 ierr = SNESSetMonitor(snes,SNESLGMonitor,(void *)lg); CHKERRQ(ierr); 248 snes->xmonitor = lg; 249 PLogObjectParent(snes,lg); 250 } 251 } 252 253 ierr = OptionsHasName(snes->prefix,"-snes_fd", &flg); CHKERRQ(ierr); 254 if (flg && snes->method_class == SNES_NONLINEAR_EQUATIONS) { 255 ierr = SNESSetJacobian(snes,snes->jacobian,snes->jacobian_pre, 256 SNESDefaultComputeJacobian,snes->funP); CHKERRQ(ierr); 257 PLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Jacobian matrix\n"); 258 } else if (flg && snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 259 ierr = SNESSetHessian(snes,snes->jacobian,snes->jacobian_pre, 260 SNESDefaultComputeHessian,snes->funP); CHKERRQ(ierr); 261 PLogInfo(snes,"SNESSetFromOptions: Setting default finite difference Hessian matrix\n"); 262 } 263 264 for ( i=0; i<numberofsetfromoptions; i++ ) { 265 ierr = (*othersetfromoptions[i])(snes); CHKERRQ(ierr); 266 } 267 268 ierr = SNESGetSLES(snes,&sles); CHKERRQ(ierr); 269 ierr = SLESSetFromOptions(sles); CHKERRQ(ierr); 270 271 /* set the special KSP monitor for matrix-free application */ 272 ierr = OptionsHasName(snes->prefix,"-snes_mf_ksp_monitor",&flg); CHKERRQ(ierr); 273 if (flg) { 274 KSP ksp; 275 ierr = SLESGetKSP(sles,&ksp);CHKERRQ(ierr); 276 ierr = KSPSetMonitor(ksp,MatSNESFDMFKSPMonitor,PETSC_NULL);CHKERRQ(ierr); 277 } 278 279 /* 280 Since the private setfromoptions requires the type to have 281 been set already, we make sure a type is set by this time. 282 */ 283 if (!snes->type_name) { 284 if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 285 ierr = SNESSetType(snes,SNES_EQ_LS); CHKERRQ(ierr); 286 } else { 287 ierr = SNESSetType(snes,SNES_UM_TR); CHKERRQ(ierr); 288 } 289 } 290 291 ierr = OptionsHasName(PETSC_NULL,"-help", &flg); CHKERRQ(ierr); 292 if (flg) { ierr = SNESPrintHelp(snes); CHKERRQ(ierr);} 293 294 if (snes->setfromoptions) { 295 ierr = (*snes->setfromoptions)(snes);CHKERRQ(ierr); 296 } 297 PetscFunctionReturn(0); 298 } 299 300 301 #undef __FUNC__ 302 #define __FUNC__ "SNESSetApplicationContext" 303 /*@ 304 SNESSetApplicationContext - Sets the optional user-defined context for 305 the nonlinear solvers. 306 307 Collective on SNES 308 309 Input Parameters: 310 + snes - the SNES context 311 - usrP - optional user context 312 313 Level: intermediate 314 315 .keywords: SNES, nonlinear, set, application, context 316 317 .seealso: SNESGetApplicationContext() 318 @*/ 319 int SNESSetApplicationContext(SNES snes,void *usrP) 320 { 321 PetscFunctionBegin; 322 PetscValidHeaderSpecific(snes,SNES_COOKIE); 323 snes->user = usrP; 324 PetscFunctionReturn(0); 325 } 326 327 #undef __FUNC__ 328 #define __FUNC__ "SNESGetApplicationContext" 329 /*@C 330 SNESGetApplicationContext - Gets the user-defined context for the 331 nonlinear solvers. 332 333 Not Collective 334 335 Input Parameter: 336 . snes - SNES context 337 338 Output Parameter: 339 . usrP - user context 340 341 Level: intermediate 342 343 .keywords: SNES, nonlinear, get, application, context 344 345 .seealso: SNESSetApplicationContext() 346 @*/ 347 int SNESGetApplicationContext( SNES snes, void **usrP ) 348 { 349 PetscFunctionBegin; 350 PetscValidHeaderSpecific(snes,SNES_COOKIE); 351 *usrP = snes->user; 352 PetscFunctionReturn(0); 353 } 354 355 #undef __FUNC__ 356 #define __FUNC__ "SNESGetIterationNumber" 357 /*@ 358 SNESGetIterationNumber - Gets the number of nonlinear iterations completed 359 at this time. 360 361 Not Collective 362 363 Input Parameter: 364 . snes - SNES context 365 366 Output Parameter: 367 . iter - iteration number 368 369 Notes: 370 For example, during the computation of iteration 2 this would return 1. 371 372 This is useful for using lagged Jacobians (where one does not recompute the 373 Jacobian at each SNES iteration). For example, the code 374 .vb 375 ierr = SNESGetIterationNumber(snes,&it); 376 if (!(it % 2)) { 377 [compute Jacobian here] 378 } 379 .ve 380 can be used in your ComputeJacobian() function to cause the Jacobian to be 381 recomputed every second SNES iteration. 382 383 Level: intermediate 384 385 .keywords: SNES, nonlinear, get, iteration, number 386 @*/ 387 int SNESGetIterationNumber(SNES snes,int* iter) 388 { 389 PetscFunctionBegin; 390 PetscValidHeaderSpecific(snes,SNES_COOKIE); 391 PetscValidIntPointer(iter); 392 *iter = snes->iter; 393 PetscFunctionReturn(0); 394 } 395 396 #undef __FUNC__ 397 #define __FUNC__ "SNESGetFunctionNorm" 398 /*@ 399 SNESGetFunctionNorm - Gets the norm of the current function that was set 400 with SNESSSetFunction(). 401 402 Collective on SNES 403 404 Input Parameter: 405 . snes - SNES context 406 407 Output Parameter: 408 . fnorm - 2-norm of function 409 410 Note: 411 SNESGetFunctionNorm() is valid for SNES_NONLINEAR_EQUATIONS methods only. 412 A related routine for SNES_UNCONSTRAINED_MINIMIZATION methods is 413 SNESGetGradientNorm(). 414 415 Level: intermediate 416 417 .keywords: SNES, nonlinear, get, function, norm 418 419 .seealso: SNESGetFunction() 420 @*/ 421 int SNESGetFunctionNorm(SNES snes,Scalar *fnorm) 422 { 423 PetscFunctionBegin; 424 PetscValidHeaderSpecific(snes,SNES_COOKIE); 425 PetscValidScalarPointer(fnorm); 426 if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 427 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"For SNES_NONLINEAR_EQUATIONS only"); 428 } 429 *fnorm = snes->norm; 430 PetscFunctionReturn(0); 431 } 432 433 #undef __FUNC__ 434 #define __FUNC__ "SNESGetGradientNorm" 435 /*@ 436 SNESGetGradientNorm - Gets the norm of the current gradient that was set 437 with SNESSSetGradient(). 438 439 Collective on SNES 440 441 Input Parameter: 442 . snes - SNES context 443 444 Output Parameter: 445 . fnorm - 2-norm of gradient 446 447 Note: 448 SNESGetGradientNorm() is valid for SNES_UNCONSTRAINED_MINIMIZATION 449 methods only. A related routine for SNES_NONLINEAR_EQUATIONS methods 450 is SNESGetFunctionNorm(). 451 452 Level: intermediate 453 454 .keywords: SNES, nonlinear, get, gradient, norm 455 456 .seelso: SNESSetGradient() 457 @*/ 458 int SNESGetGradientNorm(SNES snes,Scalar *gnorm) 459 { 460 PetscFunctionBegin; 461 PetscValidHeaderSpecific(snes,SNES_COOKIE); 462 PetscValidScalarPointer(gnorm); 463 if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 464 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 465 } 466 *gnorm = snes->norm; 467 PetscFunctionReturn(0); 468 } 469 470 #undef __FUNC__ 471 #define __FUNC__ "SNESGetNumberUnsuccessfulSteps" 472 /*@ 473 SNESGetNumberUnsuccessfulSteps - Gets the number of unsuccessful steps 474 attempted by the nonlinear solver. 475 476 Not Collective 477 478 Input Parameter: 479 . snes - SNES context 480 481 Output Parameter: 482 . nfails - number of unsuccessful steps attempted 483 484 Notes: 485 This counter is reset to zero for each successive call to SNESSolve(). 486 487 Level: intermediate 488 489 .keywords: SNES, nonlinear, get, number, unsuccessful, steps 490 @*/ 491 int SNESGetNumberUnsuccessfulSteps(SNES snes,int* nfails) 492 { 493 PetscFunctionBegin; 494 PetscValidHeaderSpecific(snes,SNES_COOKIE); 495 PetscValidIntPointer(nfails); 496 *nfails = snes->nfailures; 497 PetscFunctionReturn(0); 498 } 499 500 #undef __FUNC__ 501 #define __FUNC__ "SNESGetNumberLinearIterations" 502 /*@ 503 SNESGetNumberLinearIterations - Gets the total number of linear iterations 504 used by the nonlinear solver. 505 506 Not Collective 507 508 Input Parameter: 509 . snes - SNES context 510 511 Output Parameter: 512 . lits - number of linear iterations 513 514 Notes: 515 This counter is reset to zero for each successive call to SNESSolve(). 516 517 Level: intermediate 518 519 .keywords: SNES, nonlinear, get, number, linear, iterations 520 @*/ 521 int SNESGetNumberLinearIterations(SNES snes,int* lits) 522 { 523 PetscFunctionBegin; 524 PetscValidHeaderSpecific(snes,SNES_COOKIE); 525 PetscValidIntPointer(lits); 526 *lits = snes->linear_its; 527 PetscFunctionReturn(0); 528 } 529 530 #undef __FUNC__ 531 #define __FUNC__ "SNESGetSLES" 532 /*@C 533 SNESGetSLES - Returns the SLES context for a SNES solver. 534 535 Not Collective, but if SNES object is parallel, then SLES object is parallel 536 537 Input Parameter: 538 . snes - the SNES context 539 540 Output Parameter: 541 . sles - the SLES context 542 543 Notes: 544 The user can then directly manipulate the SLES context to set various 545 options, etc. Likewise, the user can then extract and manipulate the 546 KSP and PC contexts as well. 547 548 Level: beginner 549 550 .keywords: SNES, nonlinear, get, SLES, context 551 552 .seealso: SLESGetPC(), SLESGetKSP() 553 @*/ 554 int SNESGetSLES(SNES snes,SLES *sles) 555 { 556 PetscFunctionBegin; 557 PetscValidHeaderSpecific(snes,SNES_COOKIE); 558 *sles = snes->sles; 559 PetscFunctionReturn(0); 560 } 561 562 #undef __FUNC__ 563 #define __FUNC__ "SNESPublish_Petsc" 564 static int SNESPublish_Petsc(PetscObject object) 565 { 566 #if defined(HAVE_AMS) 567 SNES v = (SNES) object; 568 int ierr; 569 570 PetscFunctionBegin; 571 572 /* if it is already published then return */ 573 if (v->amem >=0 ) PetscFunctionReturn(0); 574 575 ierr = PetscObjectPublishBaseBegin(object);CHKERRQ(ierr); 576 ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Iteration",&v->iter,1,AMS_INT,AMS_READ, 577 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr); 578 ierr = AMS_Memory_add_field((AMS_Memory)v->amem,"Residual",&v->norm,1,AMS_DOUBLE,AMS_READ, 579 AMS_COMMON,AMS_REDUCT_UNDEF);CHKERRQ(ierr); 580 ierr = PetscObjectPublishBaseEnd(object);CHKERRQ(ierr); 581 #else 582 PetscFunctionBegin; 583 #endif 584 PetscFunctionReturn(0); 585 } 586 587 /* -----------------------------------------------------------*/ 588 #undef __FUNC__ 589 #define __FUNC__ "SNESCreate" 590 /*@C 591 SNESCreate - Creates a nonlinear solver context. 592 593 Collective on MPI_Comm 594 595 Input Parameters: 596 + comm - MPI communicator 597 - type - type of method, either 598 SNES_NONLINEAR_EQUATIONS (for systems of nonlinear equations) 599 or SNES_UNCONSTRAINED_MINIMIZATION (for unconstrained minimization) 600 601 Output Parameter: 602 . outsnes - the new SNES context 603 604 Options Database Keys: 605 + -snes_mf - Activates default matrix-free Jacobian-vector products, 606 and no preconditioning matrix 607 . -snes_mf_operator - Activates default matrix-free Jacobian-vector 608 products, and a user-provided preconditioning matrix 609 as set by SNESSetJacobian() 610 - -snes_fd - Uses (slow!) finite differences to compute Jacobian 611 612 Level: beginner 613 614 .keywords: SNES, nonlinear, create, context 615 616 .seealso: SNESSolve(), SNESDestroy() 617 @*/ 618 int SNESCreate(MPI_Comm comm,SNESProblemType type,SNES *outsnes) 619 { 620 int ierr; 621 SNES snes; 622 SNES_KSP_EW_ConvCtx *kctx; 623 624 PetscFunctionBegin; 625 *outsnes = 0; 626 if (type != SNES_UNCONSTRAINED_MINIMIZATION && type != SNES_NONLINEAR_EQUATIONS){ 627 SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"incorrect method type"); 628 } 629 PetscHeaderCreate(snes,_p_SNES,int,SNES_COOKIE,0,"SNES",comm,SNESDestroy,SNESView); 630 PLogObjectCreate(snes); 631 snes->bops->publish = SNESPublish_Petsc; 632 snes->max_its = 50; 633 snes->max_funcs = 10000; 634 snes->norm = 0.0; 635 if (type == SNES_UNCONSTRAINED_MINIMIZATION) { 636 snes->rtol = 1.e-8; 637 snes->ttol = 0.0; 638 snes->atol = 1.e-10; 639 } else { 640 snes->rtol = 1.e-8; 641 snes->ttol = 0.0; 642 snes->atol = 1.e-50; 643 } 644 snes->xtol = 1.e-8; 645 snes->trunctol = 1.e-12; /* no longer used */ 646 snes->nfuncs = 0; 647 snes->nfailures = 0; 648 snes->linear_its = 0; 649 snes->numbermonitors = 0; 650 snes->data = 0; 651 snes->view = 0; 652 snes->computeumfunction = 0; 653 snes->umfunP = 0; 654 snes->fc = 0; 655 snes->deltatol = 1.e-12; 656 snes->fmin = -1.e30; 657 snes->method_class = type; 658 snes->set_method_called = 0; 659 snes->setupcalled = 0; 660 snes->ksp_ewconv = 0; 661 snes->xmonitor = 0; 662 snes->vwork = 0; 663 snes->nwork = 0; 664 snes->conv_hist_len = 0; 665 snes->conv_hist_max = 0; 666 snes->conv_hist = PETSC_NULL; 667 snes->conv_hist_its = PETSC_NULL; 668 snes->conv_hist_reset = PETSC_TRUE; 669 670 /* Create context to compute Eisenstat-Walker relative tolerance for KSP */ 671 kctx = PetscNew(SNES_KSP_EW_ConvCtx); CHKPTRQ(kctx); 672 PLogObjectMemory(snes,sizeof(SNES_KSP_EW_ConvCtx)); 673 snes->kspconvctx = (void*)kctx; 674 kctx->version = 2; 675 kctx->rtol_0 = .3; /* Eisenstat and Walker suggest rtol_0=.5, but 676 this was too large for some test cases */ 677 kctx->rtol_last = 0; 678 kctx->rtol_max = .9; 679 kctx->gamma = 1.0; 680 kctx->alpha2 = .5*(1.0 + sqrt(5.0)); 681 kctx->alpha = kctx->alpha2; 682 kctx->threshold = .1; 683 kctx->lresid_last = 0; 684 kctx->norm_last = 0; 685 686 ierr = SLESCreate(comm,&snes->sles); CHKERRQ(ierr); 687 PLogObjectParent(snes,snes->sles) 688 689 *outsnes = snes; 690 PetscPublishAll(snes); 691 PetscFunctionReturn(0); 692 } 693 694 /* --------------------------------------------------------------- */ 695 #undef __FUNC__ 696 #define __FUNC__ "SNESSetFunction" 697 /*@C 698 SNESSetFunction - Sets the function evaluation routine and function 699 vector for use by the SNES routines in solving systems of nonlinear 700 equations. 701 702 Collective on SNES 703 704 Input Parameters: 705 + snes - the SNES context 706 . func - function evaluation routine 707 . r - vector to store function value 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,Vec f,void *ctx); 713 714 . f - function vector 715 - ctx - optional user-defined function context 716 717 Notes: 718 The Newton-like methods typically solve linear systems of the form 719 $ f'(x) x = -f(x), 720 where f'(x) denotes the Jacobian matrix and f(x) is the function. 721 722 SNESSetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only. 723 Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 724 SNESSetMinimizationFunction() and SNESSetGradient(); 725 726 Level: beginner 727 728 .keywords: SNES, nonlinear, set, function 729 730 .seealso: SNESGetFunction(), SNESComputeFunction(), SNESSetJacobian() 731 @*/ 732 int SNESSetFunction( SNES snes, Vec r, int (*func)(SNES,Vec,Vec,void*),void *ctx) 733 { 734 PetscFunctionBegin; 735 PetscValidHeaderSpecific(snes,SNES_COOKIE); 736 if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 737 SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 738 } 739 snes->computefunction = func; 740 snes->vec_func = snes->vec_func_always = r; 741 snes->funP = ctx; 742 PetscFunctionReturn(0); 743 } 744 745 #undef __FUNC__ 746 #define __FUNC__ "SNESComputeFunction" 747 /*@ 748 SNESComputeFunction - Calls the function that has been set with 749 SNESSetFunction(). 750 751 Collective on SNES 752 753 Input Parameters: 754 + snes - the SNES context 755 - x - input vector 756 757 Output Parameter: 758 . y - function vector, as set by SNESSetFunction() 759 760 Notes: 761 SNESComputeFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only. 762 Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 763 SNESComputeMinimizationFunction() and SNESComputeGradient(); 764 765 SNESComputeFunction() is typically used within nonlinear solvers 766 implementations, so most users would not generally call this routine 767 themselves. 768 769 Level: developer 770 771 .keywords: SNES, nonlinear, compute, function 772 773 .seealso: SNESSetFunction(), SNESGetFunction() 774 @*/ 775 int SNESComputeFunction(SNES snes,Vec x, Vec y) 776 { 777 int ierr; 778 779 PetscFunctionBegin; 780 if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 781 SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 782 } 783 PLogEventBegin(SNES_FunctionEval,snes,x,y,0); 784 PetscStackPush("SNES user function"); 785 ierr = (*snes->computefunction)(snes,x,y,snes->funP); CHKERRQ(ierr); 786 PetscStackPop; 787 snes->nfuncs++; 788 PLogEventEnd(SNES_FunctionEval,snes,x,y,0); 789 PetscFunctionReturn(0); 790 } 791 792 #undef __FUNC__ 793 #define __FUNC__ "SNESSetMinimizationFunction" 794 /*@C 795 SNESSetMinimizationFunction - Sets the function evaluation routine for 796 unconstrained minimization. 797 798 Collective on SNES 799 800 Input Parameters: 801 + snes - the SNES context 802 . func - function evaluation routine 803 - ctx - [optional] user-defined context for private data for the 804 function evaluation routine (may be PETSC_NULL) 805 806 Calling sequence of func: 807 $ func (SNES snes,Vec x,double *f,void *ctx); 808 809 + x - input vector 810 . f - function 811 - ctx - [optional] user-defined function context 812 813 Level: beginner 814 815 Notes: 816 SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 817 methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 818 SNESSetFunction(). 819 820 .keywords: SNES, nonlinear, set, minimization, function 821 822 .seealso: SNESGetMinimizationFunction(), SNESComputeMinimizationFunction(), 823 SNESSetHessian(), SNESSetGradient() 824 @*/ 825 int SNESSetMinimizationFunction(SNES snes,int (*func)(SNES,Vec,double*,void*), 826 void *ctx) 827 { 828 PetscFunctionBegin; 829 PetscValidHeaderSpecific(snes,SNES_COOKIE); 830 if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 831 SETERRQ(PETSC_ERR_ARG_WRONG,0,"Only for SNES_UNCONSTRAINED_MINIMIZATION"); 832 } 833 snes->computeumfunction = func; 834 snes->umfunP = ctx; 835 PetscFunctionReturn(0); 836 } 837 838 #undef __FUNC__ 839 #define __FUNC__ "SNESComputeMinimizationFunction" 840 /*@ 841 SNESComputeMinimizationFunction - Computes the function that has been 842 set with SNESSetMinimizationFunction(). 843 844 Collective on SNES 845 846 Input Parameters: 847 + snes - the SNES context 848 - x - input vector 849 850 Output Parameter: 851 . y - function value 852 853 Notes: 854 SNESComputeMinimizationFunction() is valid only for 855 SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 856 SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction(). 857 858 SNESComputeMinimizationFunction() is typically used within minimization 859 implementations, so most users would not generally call this routine 860 themselves. 861 862 Level: developer 863 864 .keywords: SNES, nonlinear, compute, minimization, function 865 866 .seealso: SNESSetMinimizationFunction(), SNESGetMinimizationFunction(), 867 SNESComputeGradient(), SNESComputeHessian() 868 @*/ 869 int SNESComputeMinimizationFunction(SNES snes,Vec x,double *y) 870 { 871 int ierr; 872 873 PetscFunctionBegin; 874 if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 875 SETERRQ(PETSC_ERR_ARG_WRONG,0,"Only for SNES_UNCONSTRAINED_MINIMIZATION"); 876 } 877 PLogEventBegin(SNES_MinimizationFunctionEval,snes,x,y,0); 878 PetscStackPush("SNES user minimzation function"); 879 ierr = (*snes->computeumfunction)(snes,x,y,snes->umfunP); CHKERRQ(ierr); 880 PetscStackPop; 881 snes->nfuncs++; 882 PLogEventEnd(SNES_MinimizationFunctionEval,snes,x,y,0); 883 PetscFunctionReturn(0); 884 } 885 886 #undef __FUNC__ 887 #define __FUNC__ "SNESSetGradient" 888 /*@C 889 SNESSetGradient - Sets the gradient evaluation routine and gradient 890 vector for use by the SNES routines. 891 892 Collective on SNES 893 894 Input Parameters: 895 + snes - the SNES context 896 . func - function evaluation routine 897 . ctx - optional user-defined context for private data for the 898 gradient evaluation routine (may be PETSC_NULL) 899 - r - vector to store gradient value 900 901 Calling sequence of func: 902 $ func (SNES, Vec x, Vec g, void *ctx); 903 904 + x - input vector 905 . g - gradient vector 906 - ctx - optional user-defined gradient context 907 908 Notes: 909 SNESSetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 910 methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 911 SNESSetFunction(). 912 913 Level: beginner 914 915 .keywords: SNES, nonlinear, set, function 916 917 .seealso: SNESGetGradient(), SNESComputeGradient(), SNESSetHessian(), 918 SNESSetMinimizationFunction(), 919 @*/ 920 int SNESSetGradient(SNES snes,Vec r,int (*func)(SNES,Vec,Vec,void*),void *ctx) 921 { 922 PetscFunctionBegin; 923 PetscValidHeaderSpecific(snes,SNES_COOKIE); 924 if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 925 SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 926 } 927 snes->computefunction = func; 928 snes->vec_func = snes->vec_func_always = r; 929 snes->funP = ctx; 930 PetscFunctionReturn(0); 931 } 932 933 #undef __FUNC__ 934 #define __FUNC__ "SNESComputeGradient" 935 /*@ 936 SNESComputeGradient - Computes the gradient that has been set with 937 SNESSetGradient(). 938 939 Collective on SNES 940 941 Input Parameters: 942 + snes - the SNES context 943 - x - input vector 944 945 Output Parameter: 946 . y - gradient vector 947 948 Notes: 949 SNESComputeGradient() is valid only for 950 SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 951 SNES_NONLINEAR_EQUATIONS methods is SNESComputeFunction(). 952 953 SNESComputeGradient() is typically used within minimization 954 implementations, so most users would not generally call this routine 955 themselves. 956 957 Level: developer 958 959 .keywords: SNES, nonlinear, compute, gradient 960 961 .seealso: SNESSetGradient(), SNESGetGradient(), 962 SNESComputeMinimizationFunction(), SNESComputeHessian() 963 @*/ 964 int SNESComputeGradient(SNES snes,Vec x, Vec y) 965 { 966 int ierr; 967 968 PetscFunctionBegin; 969 if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 970 SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 971 } 972 PLogEventBegin(SNES_GradientEval,snes,x,y,0); 973 PetscStackPush("SNES user gradient function"); 974 ierr = (*snes->computefunction)(snes,x,y,snes->funP); CHKERRQ(ierr); 975 PetscStackPop; 976 PLogEventEnd(SNES_GradientEval,snes,x,y,0); 977 PetscFunctionReturn(0); 978 } 979 980 #undef __FUNC__ 981 #define __FUNC__ "SNESComputeJacobian" 982 /*@ 983 SNESComputeJacobian - Computes the Jacobian matrix that has been 984 set with SNESSetJacobian(). 985 986 Collective on SNES and Mat 987 988 Input Parameters: 989 + snes - the SNES context 990 - x - input vector 991 992 Output Parameters: 993 + A - Jacobian matrix 994 . B - optional preconditioning matrix 995 - flag - flag indicating matrix structure 996 997 Notes: 998 Most users should not need to explicitly call this routine, as it 999 is used internally within the nonlinear solvers. 1000 1001 See SLESSetOperators() for important information about setting the 1002 flag parameter. 1003 1004 SNESComputeJacobian() is valid only for SNES_NONLINEAR_EQUATIONS 1005 methods. An analogous routine for SNES_UNCONSTRAINED_MINIMIZATION 1006 methods is SNESComputeHessian(). 1007 1008 SNESComputeJacobian() is typically used within nonlinear solver 1009 implementations, so most users would not generally call this routine 1010 themselves. 1011 1012 Level: developer 1013 1014 .keywords: SNES, compute, Jacobian, matrix 1015 1016 .seealso: SNESSetJacobian(), SLESSetOperators() 1017 @*/ 1018 int SNESComputeJacobian(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *flg) 1019 { 1020 int ierr; 1021 1022 PetscFunctionBegin; 1023 if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 1024 SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 1025 } 1026 if (!snes->computejacobian) PetscFunctionReturn(0); 1027 PLogEventBegin(SNES_JacobianEval,snes,X,*A,*B); 1028 *flg = DIFFERENT_NONZERO_PATTERN; 1029 PetscStackPush("SNES user Jacobian function"); 1030 ierr = (*snes->computejacobian)(snes,X,A,B,flg,snes->jacP); CHKERRQ(ierr); 1031 PetscStackPop; 1032 PLogEventEnd(SNES_JacobianEval,snes,X,*A,*B); 1033 /* make sure user returned a correct Jacobian and preconditioner */ 1034 PetscValidHeaderSpecific(*A,MAT_COOKIE); 1035 PetscValidHeaderSpecific(*B,MAT_COOKIE); 1036 PetscFunctionReturn(0); 1037 } 1038 1039 #undef __FUNC__ 1040 #define __FUNC__ "SNESComputeHessian" 1041 /*@ 1042 SNESComputeHessian - Computes the Hessian matrix that has been 1043 set with SNESSetHessian(). 1044 1045 Collective on SNES and Mat 1046 1047 Input Parameters: 1048 + snes - the SNES context 1049 - x - input vector 1050 1051 Output Parameters: 1052 + A - Hessian matrix 1053 . B - optional preconditioning matrix 1054 - flag - flag indicating matrix structure 1055 1056 Notes: 1057 Most users should not need to explicitly call this routine, as it 1058 is used internally within the nonlinear solvers. 1059 1060 See SLESSetOperators() for important information about setting the 1061 flag parameter. 1062 1063 SNESComputeHessian() is valid only for 1064 SNES_UNCONSTRAINED_MINIMIZATION methods. An analogous routine for 1065 SNES_NONLINEAR_EQUATIONS methods is SNESComputeJacobian(). 1066 1067 SNESComputeHessian() is typically used within minimization 1068 implementations, so most users would not generally call this routine 1069 themselves. 1070 1071 Level: developer 1072 1073 .keywords: SNES, compute, Hessian, matrix 1074 1075 .seealso: SNESSetHessian(), SLESSetOperators(), SNESComputeGradient(), 1076 SNESComputeMinimizationFunction() 1077 @*/ 1078 int SNESComputeHessian(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *flag) 1079 { 1080 int ierr; 1081 1082 PetscFunctionBegin; 1083 if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 1084 SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 1085 } 1086 if (!snes->computejacobian) PetscFunctionReturn(0); 1087 PLogEventBegin(SNES_HessianEval,snes,x,*A,*B); 1088 *flag = DIFFERENT_NONZERO_PATTERN; 1089 PetscStackPush("SNES user Hessian function"); 1090 ierr = (*snes->computejacobian)(snes,x,A,B,flag,snes->jacP); CHKERRQ(ierr); 1091 PetscStackPop; 1092 PLogEventEnd(SNES_HessianEval,snes,x,*A,*B); 1093 /* make sure user returned a correct Jacobian and preconditioner */ 1094 PetscValidHeaderSpecific(*A,MAT_COOKIE); 1095 PetscValidHeaderSpecific(*B,MAT_COOKIE); 1096 PetscFunctionReturn(0); 1097 } 1098 1099 #undef __FUNC__ 1100 #define __FUNC__ "SNESSetJacobian" 1101 /*@C 1102 SNESSetJacobian - Sets the function to compute Jacobian as well as the 1103 location to store the matrix. 1104 1105 Collective on SNES and Mat 1106 1107 Input Parameters: 1108 + snes - the SNES context 1109 . A - Jacobian matrix 1110 . B - preconditioner matrix (usually same as the Jacobian) 1111 . func - Jacobian evaluation routine 1112 - ctx - [optional] user-defined context for private data for the 1113 Jacobian evaluation routine (may be PETSC_NULL) 1114 1115 Calling sequence of func: 1116 $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 1117 1118 + x - input vector 1119 . A - Jacobian matrix 1120 . B - preconditioner matrix, usually the same as A 1121 . flag - flag indicating information about the preconditioner matrix 1122 structure (same as flag in SLESSetOperators()) 1123 - ctx - [optional] user-defined Jacobian context 1124 1125 Notes: 1126 See SLESSetOperators() for important information about setting the flag 1127 output parameter in the routine func(). Be sure to read this information! 1128 1129 The routine func() takes Mat * as the matrix arguments rather than Mat. 1130 This allows the Jacobian evaluation routine to replace A and/or B with a 1131 completely new new matrix structure (not just different matrix elements) 1132 when appropriate, for instance, if the nonzero structure is changing 1133 throughout the global iterations. 1134 1135 Level: beginner 1136 1137 .keywords: SNES, nonlinear, set, Jacobian, matrix 1138 1139 .seealso: SLESSetOperators(), SNESSetFunction() 1140 @*/ 1141 int SNESSetJacobian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*, 1142 MatStructure*,void*),void *ctx) 1143 { 1144 PetscFunctionBegin; 1145 PetscValidHeaderSpecific(snes,SNES_COOKIE); 1146 if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 1147 SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 1148 } 1149 snes->computejacobian = func; 1150 snes->jacP = ctx; 1151 snes->jacobian = A; 1152 snes->jacobian_pre = B; 1153 PetscFunctionReturn(0); 1154 } 1155 1156 #undef __FUNC__ 1157 #define __FUNC__ "SNESGetJacobian" 1158 /*@ 1159 SNESGetJacobian - Returns the Jacobian matrix and optionally the user 1160 provided context for evaluating the Jacobian. 1161 1162 Not Collective, but Mat object will be parallel if SNES object is 1163 1164 Input Parameter: 1165 . snes - the nonlinear solver context 1166 1167 Output Parameters: 1168 + A - location to stash Jacobian matrix (or PETSC_NULL) 1169 . B - location to stash preconditioner matrix (or PETSC_NULL) 1170 - ctx - location to stash Jacobian ctx (or PETSC_NULL) 1171 1172 Level: advanced 1173 1174 .seealso: SNESSetJacobian(), SNESComputeJacobian() 1175 @*/ 1176 int SNESGetJacobian(SNES snes,Mat *A,Mat *B, void **ctx) 1177 { 1178 PetscFunctionBegin; 1179 if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 1180 SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 1181 } 1182 if (A) *A = snes->jacobian; 1183 if (B) *B = snes->jacobian_pre; 1184 if (ctx) *ctx = snes->jacP; 1185 PetscFunctionReturn(0); 1186 } 1187 1188 #undef __FUNC__ 1189 #define __FUNC__ "SNESSetHessian" 1190 /*@C 1191 SNESSetHessian - Sets the function to compute Hessian as well as the 1192 location to store the matrix. 1193 1194 Collective on SNES and Mat 1195 1196 Input Parameters: 1197 + snes - the SNES context 1198 . A - Hessian matrix 1199 . B - preconditioner matrix (usually same as the Hessian) 1200 . func - Jacobian evaluation routine 1201 - ctx - [optional] user-defined context for private data for the 1202 Hessian evaluation routine (may be PETSC_NULL) 1203 1204 Calling sequence of func: 1205 $ func (SNES snes,Vec x,Mat *A,Mat *B,int *flag,void *ctx); 1206 1207 + x - input vector 1208 . A - Hessian matrix 1209 . B - preconditioner matrix, usually the same as A 1210 . flag - flag indicating information about the preconditioner matrix 1211 structure (same as flag in SLESSetOperators()) 1212 - ctx - [optional] user-defined Hessian context 1213 1214 Notes: 1215 See SLESSetOperators() for important information about setting the flag 1216 output parameter in the routine func(). Be sure to read this information! 1217 1218 The function func() takes Mat * as the matrix arguments rather than Mat. 1219 This allows the Hessian evaluation routine to replace A and/or B with a 1220 completely new new matrix structure (not just different matrix elements) 1221 when appropriate, for instance, if the nonzero structure is changing 1222 throughout the global iterations. 1223 1224 Level: beginner 1225 1226 .keywords: SNES, nonlinear, set, Hessian, matrix 1227 1228 .seealso: SNESSetMinimizationFunction(), SNESSetGradient(), SLESSetOperators() 1229 @*/ 1230 int SNESSetHessian(SNES snes,Mat A,Mat B,int (*func)(SNES,Vec,Mat*,Mat*, 1231 MatStructure*,void*),void *ctx) 1232 { 1233 PetscFunctionBegin; 1234 PetscValidHeaderSpecific(snes,SNES_COOKIE); 1235 if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 1236 SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 1237 } 1238 snes->computejacobian = func; 1239 snes->jacP = ctx; 1240 snes->jacobian = A; 1241 snes->jacobian_pre = B; 1242 PetscFunctionReturn(0); 1243 } 1244 1245 #undef __FUNC__ 1246 #define __FUNC__ "SNESGetHessian" 1247 /*@ 1248 SNESGetHessian - Returns the Hessian matrix and optionally the user 1249 provided context for evaluating the Hessian. 1250 1251 Not Collective, but Mat object is parallel if SNES object is parallel 1252 1253 Input Parameter: 1254 . snes - the nonlinear solver context 1255 1256 Output Parameters: 1257 + A - location to stash Hessian matrix (or PETSC_NULL) 1258 . B - location to stash preconditioner matrix (or PETSC_NULL) 1259 - ctx - location to stash Hessian ctx (or PETSC_NULL) 1260 1261 Level: advanced 1262 1263 .seealso: SNESSetHessian(), SNESComputeHessian() 1264 1265 .keywords: SNES, get, Hessian 1266 @*/ 1267 int SNESGetHessian(SNES snes,Mat *A,Mat *B, void **ctx) 1268 { 1269 PetscFunctionBegin; 1270 if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION){ 1271 SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 1272 } 1273 if (A) *A = snes->jacobian; 1274 if (B) *B = snes->jacobian_pre; 1275 if (ctx) *ctx = snes->jacP; 1276 PetscFunctionReturn(0); 1277 } 1278 1279 /* ----- Routines to initialize and destroy a nonlinear solver ---- */ 1280 1281 #undef __FUNC__ 1282 #define __FUNC__ "SNESSetUp" 1283 /*@ 1284 SNESSetUp - Sets up the internal data structures for the later use 1285 of a nonlinear solver. 1286 1287 Collective on SNES 1288 1289 Input Parameters: 1290 + snes - the SNES context 1291 - x - the solution vector 1292 1293 Notes: 1294 For basic use of the SNES solvers the user need not explicitly call 1295 SNESSetUp(), since these actions will automatically occur during 1296 the call to SNESSolve(). However, if one wishes to control this 1297 phase separately, SNESSetUp() should be called after SNESCreate() 1298 and optional routines of the form SNESSetXXX(), but before SNESSolve(). 1299 1300 Level: advanced 1301 1302 .keywords: SNES, nonlinear, setup 1303 1304 .seealso: SNESCreate(), SNESSolve(), SNESDestroy() 1305 @*/ 1306 int SNESSetUp(SNES snes,Vec x) 1307 { 1308 int ierr, flg; 1309 1310 PetscFunctionBegin; 1311 PetscValidHeaderSpecific(snes,SNES_COOKIE); 1312 PetscValidHeaderSpecific(x,VEC_COOKIE); 1313 snes->vec_sol = snes->vec_sol_always = x; 1314 1315 ierr = OptionsHasName(snes->prefix,"-snes_mf_operator", &flg); CHKERRQ(ierr); 1316 /* 1317 This version replaces the user provided Jacobian matrix with a 1318 matrix-free version but still employs the user-provided preconditioner matrix 1319 */ 1320 if (flg) { 1321 Mat J; 1322 ierr = MatCreateSNESFDMF(snes,snes->vec_sol,&J);CHKERRQ(ierr); 1323 PLogObjectParent(snes,J); 1324 snes->mfshell = J; 1325 if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 1326 snes->jacobian = J; 1327 PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Jacobian routines\n"); 1328 } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 1329 snes->jacobian = J; 1330 PLogInfo(snes,"SNESSetUp: Setting default matrix-free operator Hessian routines\n"); 1331 } else { 1332 SETERRQ(PETSC_ERR_SUP,0,"Method class doesn't support matrix-free operator option"); 1333 } 1334 ierr = MatSNESFDMFSetFromOptions(J);CHKERRQ(ierr); 1335 } 1336 ierr = OptionsHasName(snes->prefix,"-snes_mf", &flg); CHKERRQ(ierr); 1337 /* 1338 This version replaces both the user-provided Jacobian and the user- 1339 provided preconditioner matrix with the default matrix free version. 1340 */ 1341 if (flg) { 1342 Mat J; 1343 ierr = MatCreateSNESFDMF(snes,snes->vec_sol,&J);CHKERRQ(ierr); 1344 PLogObjectParent(snes,J); 1345 snes->mfshell = J; 1346 if (snes->method_class == SNES_NONLINEAR_EQUATIONS) { 1347 ierr = SNESSetJacobian(snes,J,J,0,snes->funP); CHKERRQ(ierr); 1348 PLogInfo(snes,"SNESSetUp: Setting default matrix-free Jacobian routines\n"); 1349 } else if (snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION) { 1350 ierr = SNESSetHessian(snes,J,J,0,snes->funP); CHKERRQ(ierr); 1351 PLogInfo(snes,"SNESSetUp: Setting default matrix-free Hessian routines\n"); 1352 } else { 1353 SETERRQ(PETSC_ERR_SUP,0,"Method class doesn't support matrix-free option"); 1354 } 1355 ierr = MatSNESFDMFSetFromOptions(J);CHKERRQ(ierr); 1356 } 1357 if ((snes->method_class == SNES_NONLINEAR_EQUATIONS)) { 1358 if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetFunction() first"); 1359 if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetFunction() first"); 1360 if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetJacobian() first"); 1361 if (snes->vec_func == snes->vec_sol) { 1362 SETERRQ(PETSC_ERR_ARG_IDN,0,"Solution vector cannot be function vector"); 1363 } 1364 1365 /* Set the KSP stopping criterion to use the Eisenstat-Walker method */ 1366 if (snes->ksp_ewconv && PetscStrcmp(snes->type_name,SNES_EQ_TR)) { 1367 SLES sles; KSP ksp; 1368 ierr = SNESGetSLES(snes,&sles); CHKERRQ(ierr); 1369 ierr = SLESGetKSP(sles,&ksp); CHKERRQ(ierr); 1370 ierr = KSPSetConvergenceTest(ksp,SNES_KSP_EW_Converged_Private, 1371 (void *)snes); CHKERRQ(ierr); 1372 } 1373 } else if ((snes->method_class == SNES_UNCONSTRAINED_MINIMIZATION)) { 1374 if (!snes->vec_func) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetGradient() first"); 1375 if (!snes->computefunction) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetGradient() first"); 1376 if (!snes->computeumfunction) { 1377 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetMinimizationFunction() first"); 1378 } 1379 if (!snes->jacobian) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call SNESSetHessian() first"); 1380 } else { 1381 SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Unknown method class"); 1382 } 1383 if (snes->setup) {ierr = (*snes->setup)(snes); CHKERRQ(ierr);} 1384 snes->setupcalled = 1; 1385 PetscFunctionReturn(0); 1386 } 1387 1388 #undef __FUNC__ 1389 #define __FUNC__ "SNESDestroy" 1390 /*@C 1391 SNESDestroy - Destroys the nonlinear solver context that was created 1392 with SNESCreate(). 1393 1394 Collective on SNES 1395 1396 Input Parameter: 1397 . snes - the SNES context 1398 1399 Level: beginner 1400 1401 .keywords: SNES, nonlinear, destroy 1402 1403 .seealso: SNESCreate(), SNESSolve() 1404 @*/ 1405 int SNESDestroy(SNES snes) 1406 { 1407 int ierr; 1408 1409 PetscFunctionBegin; 1410 PetscValidHeaderSpecific(snes,SNES_COOKIE); 1411 if (--snes->refct > 0) PetscFunctionReturn(0); 1412 1413 if (snes->destroy) {ierr = (*(snes)->destroy)(snes); CHKERRQ(ierr);} 1414 if (snes->kspconvctx) PetscFree(snes->kspconvctx); 1415 if (snes->mfshell) {ierr = MatDestroy(snes->mfshell);CHKERRQ(ierr);} 1416 ierr = SLESDestroy(snes->sles); CHKERRQ(ierr); 1417 if (snes->xmonitor) {ierr = SNESLGMonitorDestroy(snes->xmonitor);CHKERRQ(ierr);} 1418 if (snes->vwork) {ierr = VecDestroyVecs(snes->vwork,snes->nvwork);CHKERRQ(ierr);} 1419 PLogObjectDestroy((PetscObject)snes); 1420 PetscHeaderDestroy((PetscObject)snes); 1421 PetscFunctionReturn(0); 1422 } 1423 1424 /* ----------- Routines to set solver parameters ---------- */ 1425 1426 #undef __FUNC__ 1427 #define __FUNC__ "SNESSetTolerances" 1428 /*@ 1429 SNESSetTolerances - Sets various parameters used in convergence tests. 1430 1431 Collective on SNES 1432 1433 Input Parameters: 1434 + snes - the SNES context 1435 . atol - absolute convergence tolerance 1436 . rtol - relative convergence tolerance 1437 . stol - convergence tolerance in terms of the norm 1438 of the change in the solution between steps 1439 . maxit - maximum number of iterations 1440 - maxf - maximum number of function evaluations 1441 1442 Options Database Keys: 1443 + -snes_atol <atol> - Sets atol 1444 . -snes_rtol <rtol> - Sets rtol 1445 . -snes_stol <stol> - Sets stol 1446 . -snes_max_it <maxit> - Sets maxit 1447 - -snes_max_funcs <maxf> - Sets maxf 1448 1449 Notes: 1450 The default maximum number of iterations is 50. 1451 The default maximum number of function evaluations is 1000. 1452 1453 Level: intermediate 1454 1455 .keywords: SNES, nonlinear, set, convergence, tolerances 1456 1457 .seealso: SNESSetTrustRegionTolerance(), SNESSetMinimizationFunctionTolerance() 1458 @*/ 1459 int SNESSetTolerances(SNES snes,double atol,double rtol,double stol,int maxit,int maxf) 1460 { 1461 PetscFunctionBegin; 1462 PetscValidHeaderSpecific(snes,SNES_COOKIE); 1463 if (atol != PETSC_DEFAULT) snes->atol = atol; 1464 if (rtol != PETSC_DEFAULT) snes->rtol = rtol; 1465 if (stol != PETSC_DEFAULT) snes->xtol = stol; 1466 if (maxit != PETSC_DEFAULT) snes->max_its = maxit; 1467 if (maxf != PETSC_DEFAULT) snes->max_funcs = maxf; 1468 PetscFunctionReturn(0); 1469 } 1470 1471 #undef __FUNC__ 1472 #define __FUNC__ "SNESGetTolerances" 1473 /*@ 1474 SNESGetTolerances - Gets various parameters used in convergence tests. 1475 1476 Not Collective 1477 1478 Input Parameters: 1479 + snes - the SNES context 1480 . atol - absolute convergence tolerance 1481 . rtol - relative convergence tolerance 1482 . stol - convergence tolerance in terms of the norm 1483 of the change in the solution between steps 1484 . maxit - maximum number of iterations 1485 - maxf - maximum number of function evaluations 1486 1487 Notes: 1488 The user can specify PETSC_NULL for any parameter that is not needed. 1489 1490 Level: intermediate 1491 1492 .keywords: SNES, nonlinear, get, convergence, tolerances 1493 1494 .seealso: SNESSetTolerances() 1495 @*/ 1496 int SNESGetTolerances(SNES snes,double *atol,double *rtol,double *stol,int *maxit,int *maxf) 1497 { 1498 PetscFunctionBegin; 1499 PetscValidHeaderSpecific(snes,SNES_COOKIE); 1500 if (atol) *atol = snes->atol; 1501 if (rtol) *rtol = snes->rtol; 1502 if (stol) *stol = snes->xtol; 1503 if (maxit) *maxit = snes->max_its; 1504 if (maxf) *maxf = snes->max_funcs; 1505 PetscFunctionReturn(0); 1506 } 1507 1508 #undef __FUNC__ 1509 #define __FUNC__ "SNESSetTrustRegionTolerance" 1510 /*@ 1511 SNESSetTrustRegionTolerance - Sets the trust region parameter tolerance. 1512 1513 Collective on SNES 1514 1515 Input Parameters: 1516 + snes - the SNES context 1517 - tol - tolerance 1518 1519 Options Database Key: 1520 . -snes_trtol <tol> - Sets tol 1521 1522 Level: intermediate 1523 1524 .keywords: SNES, nonlinear, set, trust region, tolerance 1525 1526 .seealso: SNESSetTolerances(), SNESSetMinimizationFunctionTolerance() 1527 @*/ 1528 int SNESSetTrustRegionTolerance(SNES snes,double tol) 1529 { 1530 PetscFunctionBegin; 1531 PetscValidHeaderSpecific(snes,SNES_COOKIE); 1532 snes->deltatol = tol; 1533 PetscFunctionReturn(0); 1534 } 1535 1536 #undef __FUNC__ 1537 #define __FUNC__ "SNESSetMinimizationFunctionTolerance" 1538 /*@ 1539 SNESSetMinimizationFunctionTolerance - Sets the minimum allowable function tolerance 1540 for unconstrained minimization solvers. 1541 1542 Collective on SNES 1543 1544 Input Parameters: 1545 + snes - the SNES context 1546 - ftol - minimum function tolerance 1547 1548 Options Database Key: 1549 . -snes_fmin <ftol> - Sets ftol 1550 1551 Note: 1552 SNESSetMinimizationFunctionTolerance() is valid for SNES_UNCONSTRAINED_MINIMIZATION 1553 methods only. 1554 1555 Level: intermediate 1556 1557 .keywords: SNES, nonlinear, set, minimum, convergence, function, tolerance 1558 1559 .seealso: SNESSetTolerances(), SNESSetTrustRegionTolerance() 1560 @*/ 1561 int SNESSetMinimizationFunctionTolerance(SNES snes,double ftol) 1562 { 1563 PetscFunctionBegin; 1564 PetscValidHeaderSpecific(snes,SNES_COOKIE); 1565 snes->fmin = ftol; 1566 PetscFunctionReturn(0); 1567 } 1568 1569 #undef __FUNC__ 1570 #define __FUNC__ "SNESLGMonitor" 1571 int SNESLGMonitor(SNES snes,int it,double norm,void *ctx) 1572 { 1573 int ierr; 1574 1575 PetscFunctionBegin; 1576 ierr = KSPLGMonitor((KSP)snes,it,norm,ctx);CHKERRQ(ierr); 1577 PetscFunctionReturn(0); 1578 } 1579 1580 /* ------------ Routines to set performance monitoring options ----------- */ 1581 1582 #undef __FUNC__ 1583 #define __FUNC__ "SNESSetMonitor" 1584 /*@C 1585 SNESSetMonitor - Sets an ADDITIONAL function that is to be used at every 1586 iteration of the nonlinear solver to display the iteration's 1587 progress. 1588 1589 Collective on SNES 1590 1591 Input Parameters: 1592 + snes - the SNES context 1593 . func - monitoring routine 1594 - mctx - [optional] user-defined context for private data for the 1595 monitor routine (may be PETSC_NULL) 1596 1597 Calling sequence of func: 1598 $ int func(SNES snes,int its, double norm,void *mctx) 1599 1600 + snes - the SNES context 1601 . its - iteration number 1602 . norm - 2-norm function value (may be estimated) 1603 for SNES_NONLINEAR_EQUATIONS methods 1604 . norm - 2-norm gradient value (may be estimated) 1605 for SNES_UNCONSTRAINED_MINIMIZATION methods 1606 - mctx - [optional] monitoring context 1607 1608 Options Database Keys: 1609 + -snes_monitor - sets SNESDefaultMonitor() 1610 . -snes_xmonitor - sets line graph monitor, 1611 uses SNESLGMonitorCreate() 1612 _ -snes_cancelmonitors - cancels all monitors that have 1613 been hardwired into a code by 1614 calls to SNESSetMonitor(), but 1615 does not cancel those set via 1616 the options database. 1617 1618 Notes: 1619 Several different monitoring routines may be set by calling 1620 SNESSetMonitor() multiple times; all will be called in the 1621 order in which they were set. 1622 1623 Level: intermediate 1624 1625 .keywords: SNES, nonlinear, set, monitor 1626 1627 .seealso: SNESDefaultMonitor(), SNESClearMonitor() 1628 @*/ 1629 int SNESSetMonitor( SNES snes, int (*func)(SNES,int,double,void*),void *mctx ) 1630 { 1631 PetscFunctionBegin; 1632 if (snes->numbermonitors >= MAXSNESMONITORS) { 1633 SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Too many monitors set"); 1634 } 1635 1636 snes->monitor[snes->numbermonitors] = func; 1637 snes->monitorcontext[snes->numbermonitors++] = (void*)mctx; 1638 PetscFunctionReturn(0); 1639 } 1640 1641 #undef __FUNC__ 1642 #define __FUNC__ "SNESClearMonitor" 1643 /*@C 1644 SNESClearMonitor - Clears all the monitor functions for a SNES object. 1645 1646 Collective on SNES 1647 1648 Input Parameters: 1649 . snes - the SNES context 1650 1651 Options Database: 1652 . -snes_cancelmonitors - cancels all monitors that have been hardwired 1653 into a code by calls to SNESSetMonitor(), but does not cancel those 1654 set via the options database 1655 1656 Notes: 1657 There is no way to clear one specific monitor from a SNES object. 1658 1659 Level: intermediate 1660 1661 .keywords: SNES, nonlinear, set, monitor 1662 1663 .seealso: SNESDefaultMonitor(), SNESSetMonitor() 1664 @*/ 1665 int SNESClearMonitor( SNES snes ) 1666 { 1667 PetscFunctionBegin; 1668 snes->numbermonitors = 0; 1669 PetscFunctionReturn(0); 1670 } 1671 1672 #undef __FUNC__ 1673 #define __FUNC__ "SNESSetConvergenceTest" 1674 /*@C 1675 SNESSetConvergenceTest - Sets the function that is to be used 1676 to test for convergence of the nonlinear iterative solution. 1677 1678 Collective on SNES 1679 1680 Input Parameters: 1681 + snes - the SNES context 1682 . func - routine to test for convergence 1683 - cctx - [optional] context for private data for the convergence routine 1684 (may be PETSC_NULL) 1685 1686 Calling sequence of func: 1687 $ int func (SNES snes,double xnorm,double gnorm,double f,void *cctx) 1688 1689 + snes - the SNES context 1690 . cctx - [optional] convergence context 1691 . xnorm - 2-norm of current iterate 1692 . gnorm - 2-norm of current step (SNES_NONLINEAR_EQUATIONS methods) 1693 . f - 2-norm of function (SNES_NONLINEAR_EQUATIONS methods) 1694 . gnorm - 2-norm of current gradient (SNES_UNCONSTRAINED_MINIMIZATION methods) 1695 - f - function value (SNES_UNCONSTRAINED_MINIMIZATION methods) 1696 1697 Level: advanced 1698 1699 .keywords: SNES, nonlinear, set, convergence, test 1700 1701 .seealso: SNESConverged_EQ_LS(), SNESConverged_EQ_TR(), 1702 SNESConverged_UM_LS(), SNESConverged_UM_TR() 1703 @*/ 1704 int SNESSetConvergenceTest(SNES snes,int (*func)(SNES,double,double,double,void*),void *cctx) 1705 { 1706 PetscFunctionBegin; 1707 (snes)->converged = func; 1708 (snes)->cnvP = cctx; 1709 PetscFunctionReturn(0); 1710 } 1711 1712 #undef __FUNC__ 1713 #define __FUNC__ "SNESSetConvergenceHistory" 1714 /*@ 1715 SNESSetConvergenceHistory - Sets the array used to hold the convergence history. 1716 1717 Collective on SNES 1718 1719 Input Parameters: 1720 + snes - iterative context obtained from SNESCreate() 1721 . a - array to hold history 1722 . its - integer array holds the number of linear iterations (or 1723 negative if not converged) for each solve. 1724 . na - size of a and its 1725 - reset - PETSC_TRUTH indicates each new nonlinear solve resets the history counter to zero, 1726 else it continues storing new values for new nonlinear solves after the old ones 1727 1728 Notes: 1729 If set, this array will contain the function norms (for 1730 SNES_NONLINEAR_EQUATIONS methods) or gradient norms 1731 (for SNES_UNCONSTRAINED_MINIMIZATION methods) computed 1732 at each step. 1733 1734 This routine is useful, e.g., when running a code for purposes 1735 of accurate performance monitoring, when no I/O should be done 1736 during the section of code that is being timed. 1737 1738 Level: intermediate 1739 1740 .keywords: SNES, set, convergence, history 1741 1742 .seealso: SNESGetConvergenceHistory() 1743 1744 @*/ 1745 int SNESSetConvergenceHistory(SNES snes, double *a, int *its,int na,PetscTruth reset) 1746 { 1747 PetscFunctionBegin; 1748 PetscValidHeaderSpecific(snes,SNES_COOKIE); 1749 if (na) PetscValidScalarPointer(a); 1750 snes->conv_hist = a; 1751 snes->conv_hist_its = its; 1752 snes->conv_hist_max = na; 1753 snes->conv_hist_reset = reset; 1754 PetscFunctionReturn(0); 1755 } 1756 1757 #undef __FUNC__ 1758 #define __FUNC__ "SNESGetConvergenceHistory" 1759 /*@C 1760 SNESGetConvergenceHistory - Gets the array used to hold the convergence history. 1761 1762 Collective on SNES 1763 1764 Input Parameter: 1765 . snes - iterative context obtained from SNESCreate() 1766 1767 Output Parameters: 1768 . a - array to hold history 1769 . its - integer array holds the number of linear iterations (or 1770 negative if not converged) for each solve. 1771 - na - size of a and its 1772 1773 Notes: 1774 The calling sequence for this routine in Fortran is 1775 $ call SNESGetConvergenceHistory(SNES snes, integer na, integer ierr) 1776 1777 This routine is useful, e.g., when running a code for purposes 1778 of accurate performance monitoring, when no I/O should be done 1779 during the section of code that is being timed. 1780 1781 Level: intermediate 1782 1783 .keywords: SNES, get, convergence, history 1784 1785 .seealso: SNESSetConvergencHistory() 1786 1787 @*/ 1788 int SNESGetConvergenceHistory(SNES snes, double **a, int **its,int *na) 1789 { 1790 PetscFunctionBegin; 1791 PetscValidHeaderSpecific(snes,SNES_COOKIE); 1792 if (a) *a = snes->conv_hist; 1793 if (its) *its = snes->conv_hist_its; 1794 if (na) *na = snes->conv_hist_len; 1795 PetscFunctionReturn(0); 1796 } 1797 1798 #undef __FUNC__ 1799 #define __FUNC__ "SNESScaleStep_Private" 1800 /* 1801 SNESScaleStep_Private - Scales a step so that its length is less than the 1802 positive parameter delta. 1803 1804 Input Parameters: 1805 + snes - the SNES context 1806 . y - approximate solution of linear system 1807 . fnorm - 2-norm of current function 1808 - delta - trust region size 1809 1810 Output Parameters: 1811 + gpnorm - predicted function norm at the new point, assuming local 1812 linearization. The value is zero if the step lies within the trust 1813 region, and exceeds zero otherwise. 1814 - ynorm - 2-norm of the step 1815 1816 Note: 1817 For non-trust region methods such as SNES_EQ_LS, the parameter delta 1818 is set to be the maximum allowable step size. 1819 1820 .keywords: SNES, nonlinear, scale, step 1821 */ 1822 int SNESScaleStep_Private(SNES snes,Vec y,double *fnorm,double *delta, 1823 double *gpnorm,double *ynorm) 1824 { 1825 double norm; 1826 Scalar cnorm; 1827 int ierr; 1828 1829 PetscFunctionBegin; 1830 ierr = VecNorm(y,NORM_2, &norm );CHKERRQ(ierr); 1831 if (norm > *delta) { 1832 norm = *delta/norm; 1833 *gpnorm = (1.0 - norm)*(*fnorm); 1834 cnorm = norm; 1835 VecScale( &cnorm, y ); 1836 *ynorm = *delta; 1837 } else { 1838 *gpnorm = 0.0; 1839 *ynorm = norm; 1840 } 1841 PetscFunctionReturn(0); 1842 } 1843 1844 #undef __FUNC__ 1845 #define __FUNC__ "SNESSolve" 1846 /*@ 1847 SNESSolve - Solves a nonlinear system. Call SNESSolve after calling 1848 SNESCreate() and optional routines of the form SNESSetXXX(). 1849 1850 Collective on SNES 1851 1852 Input Parameters: 1853 + snes - the SNES context 1854 - x - the solution vector 1855 1856 Output Parameter: 1857 . its - number of iterations until termination 1858 1859 Notes: 1860 The user should initialize the vector, x, with the initial guess 1861 for the nonlinear solve prior to calling SNESSolve. In particular, 1862 to employ an initial guess of zero, the user should explicitly set 1863 this vector to zero by calling VecSet(). 1864 1865 Level: beginner 1866 1867 .keywords: SNES, nonlinear, solve 1868 1869 .seealso: SNESCreate(), SNESDestroy() 1870 @*/ 1871 int SNESSolve(SNES snes,Vec x,int *its) 1872 { 1873 int ierr, flg; 1874 1875 PetscFunctionBegin; 1876 PetscValidHeaderSpecific(snes,SNES_COOKIE); 1877 PetscValidIntPointer(its); 1878 if (!snes->setupcalled) {ierr = SNESSetUp(snes,x); CHKERRQ(ierr);} 1879 else {snes->vec_sol = snes->vec_sol_always = x;} 1880 if (snes->conv_hist_reset == PETSC_TRUE) snes->conv_hist_len = 0; 1881 PLogEventBegin(SNES_Solve,snes,0,0,0); 1882 snes->nfuncs = 0; snes->linear_its = 0; snes->nfailures = 0; 1883 ierr = (*(snes)->solve)(snes,its); CHKERRQ(ierr); 1884 PLogEventEnd(SNES_Solve,snes,0,0,0); 1885 ierr = OptionsHasName(PETSC_NULL,"-snes_view", &flg); CHKERRQ(ierr); 1886 if (flg) { ierr = SNESView(snes,VIEWER_STDOUT_WORLD); CHKERRQ(ierr); } 1887 PetscFunctionReturn(0); 1888 } 1889 1890 /* --------- Internal routines for SNES Package --------- */ 1891 1892 #undef __FUNC__ 1893 #define __FUNC__ "SNESSetType" 1894 /*@C 1895 SNESSetType - Sets the method for the nonlinear solver. 1896 1897 Collective on SNES 1898 1899 Input Parameters: 1900 + snes - the SNES context 1901 - method - a known method 1902 1903 Options Database Key: 1904 . -snes_type <method> - Sets the method; use -help for a list 1905 of available methods (for instance, ls or tr) 1906 1907 Notes: 1908 See "petsc/include/snes.h" for available methods (for instance) 1909 + SNES_EQ_LS - Newton's method with line search 1910 (systems of nonlinear equations) 1911 . SNES_EQ_TR - Newton's method with trust region 1912 (systems of nonlinear equations) 1913 . SNES_UM_TR - Newton's method with trust region 1914 (unconstrained minimization) 1915 - SNES_UM_LS - Newton's method with line search 1916 (unconstrained minimization) 1917 1918 Normally, it is best to use the SNESSetFromOptions() command and then 1919 set the SNES solver type from the options database rather than by using 1920 this routine. Using the options database provides the user with 1921 maximum flexibility in evaluating the many nonlinear solvers. 1922 The SNESSetType() routine is provided for those situations where it 1923 is necessary to set the nonlinear solver independently of the command 1924 line or options database. This might be the case, for example, when 1925 the choice of solver changes during the execution of the program, 1926 and the user's application is taking responsibility for choosing the 1927 appropriate method. In other words, this routine is not for beginners. 1928 1929 Level: intermediate 1930 1931 .keywords: SNES, set, method 1932 @*/ 1933 int SNESSetType(SNES snes,SNESType method) 1934 { 1935 int ierr; 1936 int (*r)(SNES); 1937 1938 PetscFunctionBegin; 1939 PetscValidHeaderSpecific(snes,SNES_COOKIE); 1940 1941 if (PetscTypeCompare(snes->type_name,method)) PetscFunctionReturn(0); 1942 1943 if (snes->setupcalled) { 1944 ierr = (*(snes)->destroy)(snes);CHKERRQ(ierr); 1945 snes->data = 0; 1946 } 1947 1948 /* Get the function pointers for the iterative method requested */ 1949 if (!SNESRegisterAllCalled) {ierr = SNESRegisterAll(PETSC_NULL); CHKERRQ(ierr);} 1950 1951 ierr = FListFind(snes->comm, SNESList, method,(int (**)(void *)) &r );CHKERRQ(ierr); 1952 1953 if (!r) SETERRQ1(1,1,"Unable to find requested SNES type %s",method); 1954 1955 if (snes->data) PetscFree(snes->data); 1956 snes->data = 0; 1957 ierr = (*r)(snes); CHKERRQ(ierr); 1958 1959 if (snes->type_name) PetscFree(snes->type_name); 1960 snes->type_name = (char *) PetscMalloc((PetscStrlen(method)+1)*sizeof(char));CHKPTRQ(snes->type_name); 1961 PetscStrcpy(snes->type_name,method); 1962 snes->set_method_called = 1; 1963 1964 PetscFunctionReturn(0); 1965 } 1966 1967 1968 /* --------------------------------------------------------------------- */ 1969 #undef __FUNC__ 1970 #define __FUNC__ "SNESRegisterDestroy" 1971 /*@C 1972 SNESRegisterDestroy - Frees the list of nonlinear solvers that were 1973 registered by SNESRegister(). 1974 1975 Not Collective 1976 1977 Level: advanced 1978 1979 .keywords: SNES, nonlinear, register, destroy 1980 1981 .seealso: SNESRegisterAll(), SNESRegisterAll() 1982 @*/ 1983 int SNESRegisterDestroy(void) 1984 { 1985 int ierr; 1986 1987 PetscFunctionBegin; 1988 if (SNESList) { 1989 ierr = FListDestroy( SNESList );CHKERRQ(ierr); 1990 SNESList = 0; 1991 } 1992 SNESRegisterAllCalled = 0; 1993 PetscFunctionReturn(0); 1994 } 1995 1996 #undef __FUNC__ 1997 #define __FUNC__ "SNESGetType" 1998 /*@C 1999 SNESGetType - Gets the SNES method type and name (as a string). 2000 2001 Not Collective 2002 2003 Input Parameter: 2004 . snes - nonlinear solver context 2005 2006 Output Parameter: 2007 . method - SNES method (a charactor string) 2008 2009 Level: intermediate 2010 2011 .keywords: SNES, nonlinear, get, method, name 2012 @*/ 2013 int SNESGetType(SNES snes, SNESType *method) 2014 { 2015 PetscFunctionBegin; 2016 *method = snes->type_name; 2017 PetscFunctionReturn(0); 2018 } 2019 2020 #undef __FUNC__ 2021 #define __FUNC__ "SNESGetSolution" 2022 /*@C 2023 SNESGetSolution - Returns the vector where the approximate solution is 2024 stored. 2025 2026 Not Collective, but Vec is parallel if SNES is parallel 2027 2028 Input Parameter: 2029 . snes - the SNES context 2030 2031 Output Parameter: 2032 . x - the solution 2033 2034 Level: advanced 2035 2036 .keywords: SNES, nonlinear, get, solution 2037 2038 .seealso: SNESGetFunction(), SNESGetGradient(), SNESGetSolutionUpdate() 2039 @*/ 2040 int SNESGetSolution(SNES snes,Vec *x) 2041 { 2042 PetscFunctionBegin; 2043 PetscValidHeaderSpecific(snes,SNES_COOKIE); 2044 *x = snes->vec_sol_always; 2045 PetscFunctionReturn(0); 2046 } 2047 2048 #undef __FUNC__ 2049 #define __FUNC__ "SNESGetSolutionUpdate" 2050 /*@C 2051 SNESGetSolutionUpdate - Returns the vector where the solution update is 2052 stored. 2053 2054 Not Collective, but Vec is parallel if SNES is parallel 2055 2056 Input Parameter: 2057 . snes - the SNES context 2058 2059 Output Parameter: 2060 . x - the solution update 2061 2062 Level: advanced 2063 2064 .keywords: SNES, nonlinear, get, solution, update 2065 2066 .seealso: SNESGetSolution(), SNESGetFunction 2067 @*/ 2068 int SNESGetSolutionUpdate(SNES snes,Vec *x) 2069 { 2070 PetscFunctionBegin; 2071 PetscValidHeaderSpecific(snes,SNES_COOKIE); 2072 *x = snes->vec_sol_update_always; 2073 PetscFunctionReturn(0); 2074 } 2075 2076 #undef __FUNC__ 2077 #define __FUNC__ "SNESGetFunction" 2078 /*@C 2079 SNESGetFunction - Returns the vector where the function is stored. 2080 2081 Not Collective, but Vec is parallel if SNES is parallel 2082 2083 Input Parameter: 2084 . snes - the SNES context 2085 2086 Output Parameter: 2087 . r - the function 2088 2089 Notes: 2090 SNESGetFunction() is valid for SNES_NONLINEAR_EQUATIONS methods only 2091 Analogous routines for SNES_UNCONSTRAINED_MINIMIZATION methods are 2092 SNESGetMinimizationFunction() and SNESGetGradient(); 2093 2094 Level: advanced 2095 2096 .keywords: SNES, nonlinear, get, function 2097 2098 .seealso: SNESSetFunction(), SNESGetSolution(), SNESGetMinimizationFunction(), 2099 SNESGetGradient() 2100 @*/ 2101 int SNESGetFunction(SNES snes,Vec *r) 2102 { 2103 PetscFunctionBegin; 2104 PetscValidHeaderSpecific(snes,SNES_COOKIE); 2105 if (snes->method_class != SNES_NONLINEAR_EQUATIONS) { 2106 SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_NONLINEAR_EQUATIONS only"); 2107 } 2108 *r = snes->vec_func_always; 2109 PetscFunctionReturn(0); 2110 } 2111 2112 #undef __FUNC__ 2113 #define __FUNC__ "SNESGetGradient" 2114 /*@C 2115 SNESGetGradient - Returns the vector where the gradient is stored. 2116 2117 Not Collective, but Vec is parallel if SNES is parallel 2118 2119 Input Parameter: 2120 . snes - the SNES context 2121 2122 Output Parameter: 2123 . r - the gradient 2124 2125 Notes: 2126 SNESGetGradient() is valid for SNES_UNCONSTRAINED_MINIMIZATION methods 2127 only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 2128 SNESGetFunction(). 2129 2130 Level: advanced 2131 2132 .keywords: SNES, nonlinear, get, gradient 2133 2134 .seealso: SNESGetMinimizationFunction(), SNESGetSolution(), SNESGetFunction() 2135 @*/ 2136 int SNESGetGradient(SNES snes,Vec *r) 2137 { 2138 PetscFunctionBegin; 2139 PetscValidHeaderSpecific(snes,SNES_COOKIE); 2140 if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 2141 SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 2142 } 2143 *r = snes->vec_func_always; 2144 PetscFunctionReturn(0); 2145 } 2146 2147 #undef __FUNC__ 2148 #define __FUNC__ "SNESGetMinimizationFunction" 2149 /*@ 2150 SNESGetMinimizationFunction - Returns the scalar function value for 2151 unconstrained minimization problems. 2152 2153 Not Collective 2154 2155 Input Parameter: 2156 . snes - the SNES context 2157 2158 Output Parameter: 2159 . r - the function 2160 2161 Notes: 2162 SNESGetMinimizationFunction() is valid for SNES_UNCONSTRAINED_MINIMIZATION 2163 methods only. An analogous routine for SNES_NONLINEAR_EQUATIONS methods is 2164 SNESGetFunction(). 2165 2166 Level: advanced 2167 2168 .keywords: SNES, nonlinear, get, function 2169 2170 .seealso: SNESGetGradient(), SNESGetSolution(), SNESGetFunction() 2171 @*/ 2172 int SNESGetMinimizationFunction(SNES snes,double *r) 2173 { 2174 PetscFunctionBegin; 2175 PetscValidHeaderSpecific(snes,SNES_COOKIE); 2176 PetscValidScalarPointer(r); 2177 if (snes->method_class != SNES_UNCONSTRAINED_MINIMIZATION) { 2178 SETERRQ(PETSC_ERR_ARG_WRONG,0,"For SNES_UNCONSTRAINED_MINIMIZATION only"); 2179 } 2180 *r = snes->fc; 2181 PetscFunctionReturn(0); 2182 } 2183 2184 #undef __FUNC__ 2185 #define __FUNC__ "SNESSetOptionsPrefix" 2186 /*@C 2187 SNESSetOptionsPrefix - Sets the prefix used for searching for all 2188 SNES options in the database. 2189 2190 Collective on SNES 2191 2192 Input Parameter: 2193 + snes - the SNES context 2194 - prefix - the prefix to prepend to all option names 2195 2196 Notes: 2197 A hyphen (-) must NOT be given at the beginning of the prefix name. 2198 The first character of all runtime options is AUTOMATICALLY the hyphen. 2199 2200 Level: advanced 2201 2202 .keywords: SNES, set, options, prefix, database 2203 2204 .seealso: SNESSetFromOptions() 2205 @*/ 2206 int SNESSetOptionsPrefix(SNES snes,char *prefix) 2207 { 2208 int ierr; 2209 2210 PetscFunctionBegin; 2211 PetscValidHeaderSpecific(snes,SNES_COOKIE); 2212 ierr = PetscObjectSetOptionsPrefix((PetscObject)snes, prefix); CHKERRQ(ierr); 2213 ierr = SLESSetOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr); 2214 PetscFunctionReturn(0); 2215 } 2216 2217 #undef __FUNC__ 2218 #define __FUNC__ "SNESAppendOptionsPrefix" 2219 /*@C 2220 SNESAppendOptionsPrefix - Appends to the prefix used for searching for all 2221 SNES options in the database. 2222 2223 Collective on SNES 2224 2225 Input Parameters: 2226 + snes - the SNES context 2227 - prefix - the prefix to prepend to all option names 2228 2229 Notes: 2230 A hyphen (-) must NOT be given at the beginning of the prefix name. 2231 The first character of all runtime options is AUTOMATICALLY the hyphen. 2232 2233 Level: advanced 2234 2235 .keywords: SNES, append, options, prefix, database 2236 2237 .seealso: SNESGetOptionsPrefix() 2238 @*/ 2239 int SNESAppendOptionsPrefix(SNES snes,char *prefix) 2240 { 2241 int ierr; 2242 2243 PetscFunctionBegin; 2244 PetscValidHeaderSpecific(snes,SNES_COOKIE); 2245 ierr = PetscObjectAppendOptionsPrefix((PetscObject)snes, prefix); CHKERRQ(ierr); 2246 ierr = SLESAppendOptionsPrefix(snes->sles,prefix);CHKERRQ(ierr); 2247 PetscFunctionReturn(0); 2248 } 2249 2250 #undef __FUNC__ 2251 #define __FUNC__ "SNESGetOptionsPrefix" 2252 /*@ 2253 SNESGetOptionsPrefix - Sets the prefix used for searching for all 2254 SNES options in the database. 2255 2256 Not Collective 2257 2258 Input Parameter: 2259 . snes - the SNES context 2260 2261 Output Parameter: 2262 . prefix - pointer to the prefix string used 2263 2264 Level: advanced 2265 2266 .keywords: SNES, get, options, prefix, database 2267 2268 .seealso: SNESAppendOptionsPrefix() 2269 @*/ 2270 int SNESGetOptionsPrefix(SNES snes,char **prefix) 2271 { 2272 int ierr; 2273 2274 PetscFunctionBegin; 2275 PetscValidHeaderSpecific(snes,SNES_COOKIE); 2276 ierr = PetscObjectGetOptionsPrefix((PetscObject)snes, prefix); CHKERRQ(ierr); 2277 PetscFunctionReturn(0); 2278 } 2279 2280 #undef __FUNC__ 2281 #define __FUNC__ "SNESPrintHelp" 2282 /*@ 2283 SNESPrintHelp - Prints all options for the SNES component. 2284 2285 Collective on SNES 2286 2287 Input Parameter: 2288 . snes - the SNES context 2289 2290 Options Database Keys: 2291 + -help - Prints SNES options 2292 - -h - Prints SNES options 2293 2294 Level: beginner 2295 2296 .keywords: SNES, nonlinear, help 2297 2298 .seealso: SNESSetFromOptions() 2299 @*/ 2300 int SNESPrintHelp(SNES snes) 2301 { 2302 char p[64]; 2303 SNES_KSP_EW_ConvCtx *kctx; 2304 int ierr; 2305 2306 PetscFunctionBegin; 2307 PetscValidHeaderSpecific(snes,SNES_COOKIE); 2308 2309 PetscStrcpy(p,"-"); 2310 if (snes->prefix) PetscStrcat(p, snes->prefix); 2311 2312 kctx = (SNES_KSP_EW_ConvCtx *)snes->kspconvctx; 2313 2314 (*PetscHelpPrintf)(snes->comm,"SNES options ------------------------------------------------\n"); 2315 ierr = FListPrintTypes(snes->comm,stdout,snes->prefix,"snes_type",SNESList);CHKERRQ(ierr); 2316 (*PetscHelpPrintf)(snes->comm," %ssnes_view: view SNES info after each nonlinear solve\n",p); 2317 (*PetscHelpPrintf)(snes->comm," %ssnes_max_it <its>: max iterations (default %d)\n",p,snes->max_its); 2318 (*PetscHelpPrintf)(snes->comm," %ssnes_max_funcs <maxf>: max function evals (default %d)\n",p,snes->max_funcs); 2319 (*PetscHelpPrintf)(snes->comm," %ssnes_stol <stol>: successive step tolerance (default %g)\n",p,snes->xtol); 2320 (*PetscHelpPrintf)(snes->comm," %ssnes_atol <atol>: absolute tolerance (default %g)\n",p,snes->atol); 2321 (*PetscHelpPrintf)(snes->comm," %ssnes_rtol <rtol>: relative tolerance (default %g)\n",p,snes->rtol); 2322 (*PetscHelpPrintf)(snes->comm," %ssnes_trtol <trtol>: trust region parameter tolerance (default %g)\n",p,snes->deltatol); 2323 (*PetscHelpPrintf)(snes->comm," SNES Monitoring Options: Choose any of the following\n"); 2324 (*PetscHelpPrintf)(snes->comm," %ssnes_cancelmonitors: cancels all monitors hardwired in code\n",p); 2325 (*PetscHelpPrintf)(snes->comm," %ssnes_monitor: use default SNES convergence monitor, prints\n\ 2326 residual norm at each iteration.\n",p); 2327 (*PetscHelpPrintf)(snes->comm," %ssnes_smonitor: same as the above, but prints fewer digits of the\n\ 2328 residual norm for small residual norms. This is useful to conceal\n\ 2329 meaningless digits that may be different on different machines.\n",p); 2330 (*PetscHelpPrintf)(snes->comm," %ssnes_xmonitor [x,y,w,h]: use X graphics convergence monitor\n",p); 2331 if (snes->type == SNES_NONLINEAR_EQUATIONS) { 2332 (*PetscHelpPrintf)(snes->comm, 2333 " Options for solving systems of nonlinear equations only:\n"); 2334 (*PetscHelpPrintf)(snes->comm," %ssnes_fd: use finite differences for Jacobian\n",p); 2335 (*PetscHelpPrintf)(snes->comm," %ssnes_mf: use matrix-free Jacobian\n",p); 2336 (*PetscHelpPrintf)(snes->comm," %ssnes_mf_operator:use matrix-free Jacobian and user-provided preconditioning matrix\n",p); 2337 (*PetscHelpPrintf)(snes->comm," %ssnes_mf_ksp_monitor - if using matrix-free multiply then prints h at each KSP iteration\n",p); 2338 (*PetscHelpPrintf)(snes->comm," %ssnes_no_convergence_test: Do not test for convergence, always run to SNES max its\n",p); 2339 (*PetscHelpPrintf)(snes->comm," %ssnes_ksp_ew_conv: use Eisenstat-Walker computation of KSP rtol. Params are:\n",p); 2340 (*PetscHelpPrintf)(snes->comm, 2341 " %ssnes_ksp_ew_version <version> (1 or 2, default is %d)\n",p,kctx->version); 2342 (*PetscHelpPrintf)(snes->comm, 2343 " %ssnes_ksp_ew_rtol0 <rtol0> (0 <= rtol0 < 1, default %g)\n",p,kctx->rtol_0); 2344 (*PetscHelpPrintf)(snes->comm, 2345 " %ssnes_ksp_ew_rtolmax <rtolmax> (0 <= rtolmax < 1, default %g)\n",p,kctx->rtol_max); 2346 (*PetscHelpPrintf)(snes->comm, 2347 " %ssnes_ksp_ew_gamma <gamma> (0 <= gamma <= 1, default %g)\n",p,kctx->gamma); 2348 (*PetscHelpPrintf)(snes->comm, 2349 " %ssnes_ksp_ew_alpha <alpha> (1 < alpha <= 2, default %g)\n",p,kctx->alpha); 2350 (*PetscHelpPrintf)(snes->comm, 2351 " %ssnes_ksp_ew_alpha2 <alpha2> (default %g)\n",p,kctx->alpha2); 2352 (*PetscHelpPrintf)(snes->comm, 2353 " %ssnes_ksp_ew_threshold <threshold> (0 < threshold < 1, default %g)\n",p,kctx->threshold); 2354 } else if (snes->type == SNES_UNCONSTRAINED_MINIMIZATION) { 2355 (*PetscHelpPrintf)(snes->comm, 2356 " Options for solving unconstrained minimization problems only:\n"); 2357 (*PetscHelpPrintf)(snes->comm," %ssnes_fmin <ftol>: minimum function tolerance (default %g)\n",p,snes->fmin); 2358 (*PetscHelpPrintf)(snes->comm," %ssnes_fd: use finite differences for Hessian\n",p); 2359 (*PetscHelpPrintf)(snes->comm," %ssnes_mf: use matrix-free Hessian\n",p); 2360 } 2361 (*PetscHelpPrintf)(snes->comm," Run program with -help %ssnes_type <method> for help on ",p); 2362 (*PetscHelpPrintf)(snes->comm,"a particular method\n"); 2363 if (snes->printhelp) { 2364 ierr = (*snes->printhelp)(snes,p);CHKERRQ(ierr); 2365 } 2366 PetscFunctionReturn(0); 2367 } 2368 2369 /*MC 2370 SNESRegister - Adds a method to the nonlinear solver package. 2371 2372 Synopsis: 2373 SNESRegister(char *name_solver,char *path,char *name_create,int (*routine_create)(SNES)) 2374 2375 Not collective 2376 2377 Input Parameters: 2378 + name_solver - name of a new user-defined solver 2379 . path - path (either absolute or relative) the library containing this solver 2380 . name_create - name of routine to create method context 2381 - routine_create - routine to create method context 2382 2383 Notes: 2384 SNESRegister() may be called multiple times to add several user-defined solvers. 2385 2386 If dynamic libraries are used, then the fourth input argument (routine_create) 2387 is ignored. 2388 2389 Sample usage: 2390 .vb 2391 SNESRegister("my_solver",/home/username/my_lib/lib/libg/solaris/mylib.a, 2392 "MySolverCreate",MySolverCreate); 2393 .ve 2394 2395 Then, your solver can be chosen with the procedural interface via 2396 $ SNESSetType(snes,"my_solver") 2397 or at runtime via the option 2398 $ -snes_type my_solver 2399 2400 Level: advanced 2401 2402 .keywords: SNES, nonlinear, register 2403 2404 .seealso: SNESRegisterAll(), SNESRegisterDestroy() 2405 M*/ 2406 2407 #undef __FUNC__ 2408 #define __FUNC__ "SNESRegister_Private" 2409 int SNESRegister_Private(char *sname,char *path,char *name,int (*function)(SNES)) 2410 { 2411 char fullname[256]; 2412 int ierr; 2413 2414 PetscFunctionBegin; 2415 PetscStrcpy(fullname,path); PetscStrcat(fullname,":");PetscStrcat(fullname,name); 2416 ierr = FListAdd_Private(&SNESList,sname,fullname, (int (*)(void*))function);CHKERRQ(ierr); 2417 PetscFunctionReturn(0); 2418 } 2419