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