1 #define PETSCKSP_DLL 2 3 /* 4 The basic KSP routines, Create, View etc. are here. 5 */ 6 #include "private/kspimpl.h" /*I "petscksp.h" I*/ 7 8 /* Logging support */ 9 PetscClassId PETSCKSP_DLLEXPORT KSP_CLASSID; 10 PetscLogEvent KSP_GMRESOrthogonalization, KSP_SetUp, KSP_Solve; 11 12 /* 13 Contains the list of registered KSP routines 14 */ 15 PetscFList KSPList = 0; 16 PetscTruth KSPRegisterAllCalled = PETSC_FALSE; 17 18 #undef __FUNCT__ 19 #define __FUNCT__ "KSPView" 20 /*@C 21 KSPView - Prints the KSP data structure. 22 23 Collective on KSP 24 25 Input Parameters: 26 + ksp - the Krylov space context 27 - viewer - visualization context 28 29 Options Database Keys: 30 . -ksp_view - print the ksp data structure at the end of a KSPSolve call 31 32 Note: 33 The available visualization contexts include 34 + PETSC_VIEWER_STDOUT_SELF - standard output (default) 35 - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 36 output where only the first processor opens 37 the file. All other processors send their 38 data to the first processor to print. 39 40 The user can open an alternative visualization context with 41 PetscViewerASCIIOpen() - output to a specified file. 42 43 Level: beginner 44 45 .keywords: KSP, view 46 47 .seealso: PCView(), PetscViewerASCIIOpen() 48 @*/ 49 PetscErrorCode PETSCKSP_DLLEXPORT KSPView(KSP ksp,PetscViewer viewer) 50 { 51 const KSPType type; 52 PetscErrorCode ierr; 53 PetscTruth iascii; 54 55 PetscFunctionBegin; 56 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 57 if (!viewer) viewer = PETSC_VIEWER_STDOUT_(((PetscObject)ksp)->comm); 58 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 59 PetscCheckSameComm(ksp,1,viewer,2); 60 61 ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 62 if (iascii) { 63 ierr = KSPGetType(ksp,&type);CHKERRQ(ierr); 64 if (((PetscObject)ksp)->prefix) { 65 ierr = PetscViewerASCIIPrintf(viewer,"KSP Object:(%s)\n",((PetscObject)ksp)->prefix);CHKERRQ(ierr); 66 } else { 67 ierr = PetscViewerASCIIPrintf(viewer,"KSP Object:\n");CHKERRQ(ierr); 68 } 69 if (type) { 70 ierr = PetscViewerASCIIPrintf(viewer," type: %s\n",type);CHKERRQ(ierr); 71 } else { 72 ierr = PetscViewerASCIIPrintf(viewer," type: not yet set\n");CHKERRQ(ierr); 73 } 74 if (ksp->ops->view) { 75 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 76 ierr = (*ksp->ops->view)(ksp,viewer);CHKERRQ(ierr); 77 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 78 } 79 if (ksp->guess_zero) {ierr = PetscViewerASCIIPrintf(viewer," maximum iterations=%D, initial guess is zero\n",ksp->max_it);CHKERRQ(ierr);} 80 else {ierr = PetscViewerASCIIPrintf(viewer," maximum iterations=%D\n", ksp->max_it);CHKERRQ(ierr);} 81 if (ksp->guess_knoll) {ierr = PetscViewerASCIIPrintf(viewer," using preconditioner applied to right hand side for initial guess\n");CHKERRQ(ierr);} 82 ierr = PetscViewerASCIIPrintf(viewer," tolerances: relative=%G, absolute=%G, divergence=%G\n",ksp->rtol,ksp->abstol,ksp->divtol);CHKERRQ(ierr); 83 if (ksp->pc_side == PC_RIGHT) {ierr = PetscViewerASCIIPrintf(viewer," right preconditioning\n");CHKERRQ(ierr);} 84 else if (ksp->pc_side == PC_SYMMETRIC) {ierr = PetscViewerASCIIPrintf(viewer," symmetric preconditioning\n");CHKERRQ(ierr);} 85 else {ierr = PetscViewerASCIIPrintf(viewer," left preconditioning\n");CHKERRQ(ierr);} 86 if (ksp->guess) {ierr = PetscViewerASCIIPrintf(viewer," using Fischers initial guess method %D with size %D\n",ksp->guess->method,ksp->guess->maxl);CHKERRQ(ierr);} 87 if (ksp->dscale) {ierr = PetscViewerASCIIPrintf(viewer," diagonally scaled system\n");CHKERRQ(ierr);} 88 if (ksp->nullsp) {ierr = PetscViewerASCIIPrintf(viewer," has attached null space\n");CHKERRQ(ierr);} 89 if (!ksp->guess_zero) {ierr = PetscViewerASCIIPrintf(viewer," using nonzero initial guess\n");CHKERRQ(ierr);} 90 ierr = PetscViewerASCIIPrintf(viewer," using %s norm type for convergence test\n",KSPNormTypes[ksp->normtype]);CHKERRQ(ierr); 91 } else { 92 if (ksp->ops->view) { 93 ierr = (*ksp->ops->view)(ksp,viewer);CHKERRQ(ierr); 94 } 95 } 96 if (!ksp->pc) {ierr = KSPGetPC(ksp,&ksp->pc);CHKERRQ(ierr);} 97 ierr = PCView(ksp->pc,viewer);CHKERRQ(ierr); 98 PetscFunctionReturn(0); 99 } 100 101 102 #undef __FUNCT__ 103 #define __FUNCT__ "KSPSetNormType" 104 /*@ 105 KSPSetNormType - Sets the norm that is used for convergence testing. 106 107 Logically Collective on KSP 108 109 Input Parameter: 110 + ksp - Krylov solver context 111 - normtype - one of 112 $ KSP_NORM_NO - skips computing the norm, this should only be used if you are using 113 $ the Krylov method as a smoother with a fixed small number of iterations. 114 $ Implicitly sets KSPSkipConverged as KSP convergence test. 115 $ Supported only by CG, Richardson, Bi-CG-stab, CR, and CGS methods. 116 $ KSP_NORM_PRECONDITIONED - the default for left preconditioned solves, uses the l2 norm 117 $ of the preconditioned residual 118 $ KSP_NORM_UNPRECONDITIONED - uses the l2 norm of the true b - Ax residual, supported only by 119 $ CG, CHEBYCHEV, and RICHARDSON, automatically true for right (see KSPSetPCSide()) 120 $ preconditioning.. 121 $ KSP_NORM_NATURAL - supported by KSPCG, KSPCR, KSPCGNE, KSPCGS 122 123 124 Options Database Key: 125 . -ksp_norm_type <none,preconditioned,unpreconditioned,natural> 126 127 Notes: 128 Currently only works with the CG, Richardson, Bi-CG-stab, CR, and CGS methods. 129 130 Level: advanced 131 132 .keywords: KSP, create, context, norms 133 134 .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSPSkipConverged() 135 @*/ 136 PetscErrorCode PETSCKSP_DLLEXPORT KSPSetNormType(KSP ksp,KSPNormType normtype) 137 { 138 PetscErrorCode ierr; 139 140 PetscFunctionBegin; 141 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 142 PetscValidLogicalCollectiveEnum(ksp,normtype,2); 143 ksp->normtype = normtype; 144 if (normtype == KSP_NORM_NO) { 145 ierr = KSPSetConvergenceTest(ksp,KSPSkipConverged,0,0);CHKERRQ(ierr); 146 ierr = PetscInfo(ksp,"Warning: setting KSPNormType to skip computing the norm\n\ 147 KSP convergence test is implicitly set to KSPSkipConverged\n");CHKERRQ(ierr); 148 } 149 PetscFunctionReturn(0); 150 } 151 152 #undef __FUNCT__ 153 #define __FUNCT__ "KSPSetCheckNormIteration" 154 /*@ 155 KSPSetCheckNormIteration - Sets the first iteration at which the norm of the residual will be 156 computed and used in the convergence test. 157 158 Logically Collective on KSP 159 160 Input Parameter: 161 + ksp - Krylov solver context 162 - it - use -1 to check at all iterations 163 164 Notes: 165 Currently only works with KSPCG, KSPBCGS and KSPIBCGS 166 167 Use KSPSetNormType(ksp,KSP_NORM_NO) to never check the norm 168 169 On steps where the norm is not computed, the previous norm is still in the variable, so if you run with, for example, 170 -ksp_monitor the residual norm will appear to be unchanged for several iterations (though it is not really unchanged). 171 Level: advanced 172 173 .keywords: KSP, create, context, norms 174 175 .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSPSkipConverged(), KSPSetNormType() 176 @*/ 177 PetscErrorCode PETSCKSP_DLLEXPORT KSPSetCheckNormIteration(KSP ksp,PetscInt it) 178 { 179 PetscFunctionBegin; 180 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 181 PetscValidLogicalCollectiveInt(ksp,it,2); 182 ksp->chknorm = it; 183 PetscFunctionReturn(0); 184 } 185 186 #undef __FUNCT__ 187 #define __FUNCT__ "KSPSetLagNorm" 188 /*@ 189 KSPSetLagNorm - Lags the residual norm calculation so that it is computed as part of the MPI_Allreduce() for 190 computing the inner products for the next iteration. This can reduce communication costs at the expense of doing 191 one additional iteration. 192 193 194 Logically Collective on KSP 195 196 Input Parameter: 197 + ksp - Krylov solver context 198 - flg - PETSC_TRUE or PETSC_FALSE 199 200 Options Database Keys: 201 . -ksp_lag_norm - lag the calculated residual norm 202 203 Notes: 204 Currently only works with KSPIBCGS. 205 206 Use KSPSetNormType(ksp,KSP_NORM_NO) to never check the norm 207 208 If you lag the norm and run with, for example, -ksp_monitor, the residual norm reported will be the lagged one. 209 Level: advanced 210 211 .keywords: KSP, create, context, norms 212 213 .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSPSkipConverged(), KSPSetNormType() 214 @*/ 215 PetscErrorCode PETSCKSP_DLLEXPORT KSPSetLagNorm(KSP ksp,PetscTruth flg) 216 { 217 PetscFunctionBegin; 218 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 219 PetscValidLogicalCollectiveTruth(ksp,flg,2); 220 ksp->lagnorm = flg; 221 PetscFunctionReturn(0); 222 } 223 224 #undef __FUNCT__ 225 #define __FUNCT__ "KSPGetNormType" 226 /*@ 227 KSPGetNormType - Gets the norm that is used for convergence testing. 228 229 Not Collective 230 231 Input Parameter: 232 . ksp - Krylov solver context 233 234 Output Parameter: 235 . normtype - norm that is used for convergence testing 236 237 Level: advanced 238 239 .keywords: KSP, create, context, norms 240 241 .seealso: KSPNormType, KSPSetNormType(), KSPSkipConverged() 242 @*/ 243 PetscErrorCode PETSCKSP_DLLEXPORT KSPGetNormType(KSP ksp, KSPNormType *normtype) { 244 PetscFunctionBegin; 245 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 246 PetscValidPointer(normtype, 2); 247 *normtype = ksp->normtype; 248 PetscFunctionReturn(0); 249 } 250 251 #if 0 252 #undef __FUNCT__ 253 #define __FUNCT__ "KSPPublish_Petsc" 254 static PetscErrorCode KSPPublish_Petsc(PetscObject obj) 255 { 256 PetscFunctionBegin; 257 PetscFunctionReturn(0); 258 } 259 #endif 260 261 #undef __FUNCT__ 262 #define __FUNCT__ "KSPSetOperators" 263 /*@ 264 KSPSetOperators - Sets the matrix associated with the linear system 265 and a (possibly) different one associated with the preconditioner. 266 267 Collective on KSP and Mat 268 269 Input Parameters: 270 + ksp - the KSP context 271 . Amat - the matrix associated with the linear system 272 . Pmat - the matrix to be used in constructing the preconditioner, usually the 273 same as Amat. 274 - flag - flag indicating information about the preconditioner matrix structure 275 during successive linear solves. This flag is ignored the first time a 276 linear system is solved, and thus is irrelevant when solving just one linear 277 system. 278 279 Notes: 280 The flag can be used to eliminate unnecessary work in the preconditioner 281 during the repeated solution of linear systems of the same size. The 282 available options are 283 $ SAME_PRECONDITIONER - 284 $ Pmat is identical during successive linear solves. 285 $ This option is intended for folks who are using 286 $ different Amat and Pmat matrices and want to reuse the 287 $ same preconditioner matrix. For example, this option 288 $ saves work by not recomputing incomplete factorization 289 $ for ILU/ICC preconditioners. 290 $ SAME_NONZERO_PATTERN - 291 $ Pmat has the same nonzero structure during 292 $ successive linear solves. 293 $ DIFFERENT_NONZERO_PATTERN - 294 $ Pmat does not have the same nonzero structure. 295 296 All future calls to KSPSetOperators() must use the same size matrices! 297 298 Passing a PETSC_NULL for Amat or Pmat removes the matrix that is currently used. 299 300 If you wish to replace either Amat or Pmat but leave the other one untouched then 301 first call KSPGetOperators() to get the one you wish to keep, call PetscObjectReference() 302 on it and then pass it back in in your call to KSPSetOperators(). 303 304 Caution: 305 If you specify SAME_NONZERO_PATTERN, PETSc believes your assertion 306 and does not check the structure of the matrix. If you erroneously 307 claim that the structure is the same when it actually is not, the new 308 preconditioner will not function correctly. Thus, use this optimization 309 feature carefully! 310 311 If in doubt about whether your preconditioner matrix has changed 312 structure or not, use the flag DIFFERENT_NONZERO_PATTERN. 313 314 Level: beginner 315 316 Alternative usage: If the operators have NOT been set with KSP/PCSetOperators() then the operators 317 are created in PC and returned to the user. In this case, if both operators 318 mat and pmat are requested, two DIFFERENT operators will be returned. If 319 only one is requested both operators in the PC will be the same (i.e. as 320 if one had called KSP/PCSetOperators() with the same argument for both Mats). 321 The user must set the sizes of the returned matrices and their type etc just 322 as if the user created them with MatCreate(). For example, 323 324 $ KSP/PCGetOperators(ksp/pc,&mat,PETSC_NULL,PETSC_NULL); is equivalent to 325 $ set size, type, etc of mat 326 327 $ MatCreate(comm,&mat); 328 $ KSP/PCSetOperators(ksp/pc,mat,mat,SAME_NONZERO_PATTERN); 329 $ PetscObjectDereference((PetscObject)mat); 330 $ set size, type, etc of mat 331 332 and 333 334 $ KSP/PCGetOperators(ksp/pc,&mat,&pmat,PETSC_NULL); is equivalent to 335 $ set size, type, etc of mat and pmat 336 337 $ MatCreate(comm,&mat); 338 $ MatCreate(comm,&pmat); 339 $ KSP/PCSetOperators(ksp/pc,mat,pmat,SAME_NONZERO_PATTERN); 340 $ PetscObjectDereference((PetscObject)mat); 341 $ PetscObjectDereference((PetscObject)pmat); 342 $ set size, type, etc of mat and pmat 343 344 The rational for this support is so that when creating a TS, SNES, or KSP the hierarchy 345 of underlying objects (i.e. SNES, KSP, PC, Mat) and their livespans can be completely 346 managed by the top most level object (i.e. the TS, SNES, or KSP). Another way to look 347 at this is when you create a SNES you do not NEED to create a KSP and attach it to 348 the SNES object (the SNES object manages it for you). Similarly when you create a KSP 349 you do not need to attach a PC to it (the KSP object manages the PC object for you). 350 Thus, why should YOU have to create the Mat and attach it to the SNES/KSP/PC, when 351 it can be created for you? 352 353 .keywords: KSP, set, operators, matrix, preconditioner, linear system 354 355 .seealso: KSPSolve(), KSPGetPC(), PCGetOperators(), PCSetOperators(), KSPGetOperators() 356 @*/ 357 PetscErrorCode PETSCKSP_DLLEXPORT KSPSetOperators(KSP ksp,Mat Amat,Mat Pmat,MatStructure flag) 358 { 359 PetscErrorCode ierr; 360 361 PetscFunctionBegin; 362 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 363 if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 364 if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 365 if (Amat) PetscCheckSameComm(ksp,1,Amat,2); 366 if (Pmat) PetscCheckSameComm(ksp,1,Pmat,3); 367 if (!ksp->pc) {ierr = KSPGetPC(ksp,&ksp->pc);CHKERRQ(ierr);} 368 ierr = PCSetOperators(ksp->pc,Amat,Pmat,flag);CHKERRQ(ierr); 369 if (ksp->setupstage == KSP_SETUP_NEWRHS) ksp->setupstage = KSP_SETUP_NEWMATRIX; /* so that next solve call will call PCSetUp() on new matrix */ 370 if (ksp->guess) { 371 ierr = KSPFischerGuessReset(ksp->guess);CHKERRQ(ierr); 372 } 373 PetscFunctionReturn(0); 374 } 375 376 #undef __FUNCT__ 377 #define __FUNCT__ "KSPGetOperators" 378 /*@ 379 KSPGetOperators - Gets the matrix associated with the linear system 380 and a (possibly) different one associated with the preconditioner. 381 382 Collective on KSP and Mat 383 384 Input Parameter: 385 . ksp - the KSP context 386 387 Output Parameters: 388 + Amat - the matrix associated with the linear system 389 . Pmat - the matrix to be used in constructing the preconditioner, usually the same as Amat. 390 - flag - flag indicating information about the preconditioner matrix structure 391 during successive linear solves. This flag is ignored the first time a 392 linear system is solved, and thus is irrelevant when solving just one linear 393 system. 394 395 Level: intermediate 396 397 Notes: DOES NOT increase the reference counts of the matrix, so you should NOT destroy them. 398 399 .keywords: KSP, set, get, operators, matrix, preconditioner, linear system 400 401 .seealso: KSPSolve(), KSPGetPC(), PCGetOperators(), PCSetOperators(), KSPSetOperators(), KSPGetOperatorsSet() 402 @*/ 403 PetscErrorCode PETSCKSP_DLLEXPORT KSPGetOperators(KSP ksp,Mat *Amat,Mat *Pmat,MatStructure *flag) 404 { 405 PetscErrorCode ierr; 406 407 PetscFunctionBegin; 408 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 409 if (!ksp->pc) {ierr = KSPGetPC(ksp,&ksp->pc);CHKERRQ(ierr);} 410 ierr = PCGetOperators(ksp->pc,Amat,Pmat,flag);CHKERRQ(ierr); 411 PetscFunctionReturn(0); 412 } 413 414 #undef __FUNCT__ 415 #define __FUNCT__ "KSPGetOperatorsSet" 416 /*@C 417 KSPGetOperatorsSet - Determines if the matrix associated with the linear system and 418 possibly a different one associated with the preconditioner have been set in the KSP. 419 420 Not collective, though the results on all processes should be the same 421 422 Input Parameter: 423 . pc - the preconditioner context 424 425 Output Parameters: 426 + mat - the matrix associated with the linear system was set 427 - pmat - matrix associated with the preconditioner was set, usually the same 428 429 Level: intermediate 430 431 .keywords: KSP, get, operators, matrix, linear system 432 433 .seealso: PCSetOperators(), KSPGetOperators(), KSPSetOperators(), PCGetOperators(), PCGetOperatorsSet() 434 @*/ 435 PetscErrorCode PETSCKSP_DLLEXPORT KSPGetOperatorsSet(KSP ksp,PetscTruth *mat,PetscTruth *pmat) 436 { 437 PetscErrorCode ierr; 438 439 PetscFunctionBegin; 440 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 441 if (!ksp->pc) {ierr = KSPGetPC(ksp,&ksp->pc);CHKERRQ(ierr);} 442 ierr = PCGetOperatorsSet(ksp->pc,mat,pmat);CHKERRQ(ierr); 443 PetscFunctionReturn(0); 444 } 445 446 #undef __FUNCT__ 447 #define __FUNCT__ "KSPCreate" 448 /*@ 449 KSPCreate - Creates the default KSP context. 450 451 Collective on MPI_Comm 452 453 Input Parameter: 454 . comm - MPI communicator 455 456 Output Parameter: 457 . ksp - location to put the KSP context 458 459 Notes: 460 The default KSP type is GMRES with a restart of 30, using modified Gram-Schmidt 461 orthogonalization. 462 463 Level: beginner 464 465 .keywords: KSP, create, context 466 467 .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSP 468 @*/ 469 PetscErrorCode PETSCKSP_DLLEXPORT KSPCreate(MPI_Comm comm,KSP *inksp) 470 { 471 KSP ksp; 472 PetscErrorCode ierr; 473 void *ctx; 474 475 PetscFunctionBegin; 476 PetscValidPointer(inksp,2); 477 *inksp = 0; 478 #ifndef PETSC_USE_DYNAMIC_LIBRARIES 479 ierr = KSPInitializePackage(PETSC_NULL);CHKERRQ(ierr); 480 #endif 481 482 ierr = PetscHeaderCreate(ksp,_p_KSP,struct _KSPOps,KSP_CLASSID,-1,"KSP",comm,KSPDestroy,KSPView);CHKERRQ(ierr); 483 484 ksp->max_it = 10000; 485 ksp->pc_side = PC_LEFT; 486 ksp->rtol = 1.e-5; 487 ksp->abstol = 1.e-50; 488 ksp->divtol = 1.e4; 489 490 ksp->chknorm = -1; 491 ksp->normtype = KSP_NORM_PRECONDITIONED; 492 ksp->rnorm = 0.0; 493 ksp->its = 0; 494 ksp->guess_zero = PETSC_TRUE; 495 ksp->calc_sings = PETSC_FALSE; 496 ksp->res_hist = PETSC_NULL; 497 ksp->res_hist_alloc = PETSC_NULL; 498 ksp->res_hist_len = 0; 499 ksp->res_hist_max = 0; 500 ksp->res_hist_reset = PETSC_TRUE; 501 ksp->numbermonitors = 0; 502 503 ierr = KSPDefaultConvergedCreate(&ctx);CHKERRQ(ierr); 504 ierr = KSPSetConvergenceTest(ksp,KSPDefaultConverged,ctx,KSPDefaultConvergedDestroy);CHKERRQ(ierr); 505 ksp->ops->buildsolution = KSPDefaultBuildSolution; 506 ksp->ops->buildresidual = KSPDefaultBuildResidual; 507 508 ksp->vec_sol = 0; 509 ksp->vec_rhs = 0; 510 ksp->pc = 0; 511 ksp->data = 0; 512 ksp->nwork = 0; 513 ksp->work = 0; 514 ksp->reason = KSP_CONVERGED_ITERATING; 515 ksp->setupstage = KSP_SETUP_NEW; 516 517 *inksp = ksp; 518 PetscFunctionReturn(0); 519 } 520 521 #undef __FUNCT__ 522 #define __FUNCT__ "KSPSetType" 523 /*@C 524 KSPSetType - Builds KSP for a particular solver. 525 526 Logically Collective on KSP 527 528 Input Parameters: 529 + ksp - the Krylov space context 530 - type - a known method 531 532 Options Database Key: 533 . -ksp_type <method> - Sets the method; use -help for a list 534 of available methods (for instance, cg or gmres) 535 536 Notes: 537 See "petsc/include/petscksp.h" for available methods (for instance, 538 KSPCG or KSPGMRES). 539 540 Normally, it is best to use the KSPSetFromOptions() command and 541 then set the KSP type from the options database rather than by using 542 this routine. Using the options database provides the user with 543 maximum flexibility in evaluating the many different Krylov methods. 544 The KSPSetType() routine is provided for those situations where it 545 is necessary to set the iterative solver independently of the command 546 line or options database. This might be the case, for example, when 547 the choice of iterative solver changes during the execution of the 548 program, and the user's application is taking responsibility for 549 choosing the appropriate method. In other words, this routine is 550 not for beginners. 551 552 Level: intermediate 553 554 .keywords: KSP, set, method 555 556 .seealso: PCSetType(), KSPType 557 558 @*/ 559 PetscErrorCode PETSCKSP_DLLEXPORT KSPSetType(KSP ksp, const KSPType type) 560 { 561 PetscErrorCode ierr,(*r)(KSP); 562 PetscTruth match; 563 564 PetscFunctionBegin; 565 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 566 PetscValidCharPointer(type,2); 567 568 ierr = PetscTypeCompare((PetscObject)ksp,type,&match);CHKERRQ(ierr); 569 if (match) PetscFunctionReturn(0); 570 571 ierr = PetscFListFind(KSPList,((PetscObject)ksp)->comm,type,(void (**)(void)) &r);CHKERRQ(ierr); 572 if (!r) SETERRQ1(((PetscObject)ksp)->comm,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested KSP type %s",type); 573 /* Destroy the previous private KSP context */ 574 if (ksp->ops->destroy) { ierr = (*ksp->ops->destroy)(ksp);CHKERRQ(ierr); } 575 /* Reinitialize function pointers in KSPOps structure */ 576 ierr = PetscMemzero(ksp->ops,sizeof(struct _KSPOps));CHKERRQ(ierr); 577 ksp->ops->buildsolution = KSPDefaultBuildSolution; 578 ksp->ops->buildresidual = KSPDefaultBuildResidual; 579 /* Call the KSPCreate_XXX routine for this particular Krylov solver */ 580 ksp->setupstage = KSP_SETUP_NEW; 581 ierr = (*r)(ksp);CHKERRQ(ierr); 582 ierr = PetscObjectChangeTypeName((PetscObject)ksp,type);CHKERRQ(ierr); 583 #if defined(PETSC_HAVE_AMS) 584 if (PetscAMSPublishAll) { 585 ierr = PetscObjectAMSPublish((PetscObject)ksp);CHKERRQ(ierr); 586 } 587 #endif 588 PetscFunctionReturn(0); 589 } 590 591 #undef __FUNCT__ 592 #define __FUNCT__ "KSPRegisterDestroy" 593 /*@ 594 KSPRegisterDestroy - Frees the list of KSP methods that were 595 registered by KSPRegisterDynamic(). 596 597 Not Collective 598 599 Level: advanced 600 601 .keywords: KSP, register, destroy 602 603 .seealso: KSPRegisterDynamic(), KSPRegisterAll() 604 @*/ 605 PetscErrorCode PETSCKSP_DLLEXPORT KSPRegisterDestroy(void) 606 { 607 PetscErrorCode ierr; 608 609 PetscFunctionBegin; 610 ierr = PetscFListDestroy(&KSPList);CHKERRQ(ierr); 611 KSPRegisterAllCalled = PETSC_FALSE; 612 PetscFunctionReturn(0); 613 } 614 615 #undef __FUNCT__ 616 #define __FUNCT__ "KSPGetType" 617 /*@C 618 KSPGetType - Gets the KSP type as a string from the KSP object. 619 620 Not Collective 621 622 Input Parameter: 623 . ksp - Krylov context 624 625 Output Parameter: 626 . name - name of KSP method 627 628 Level: intermediate 629 630 .keywords: KSP, get, method, name 631 632 .seealso: KSPSetType() 633 @*/ 634 PetscErrorCode PETSCKSP_DLLEXPORT KSPGetType(KSP ksp,const KSPType *type) 635 { 636 PetscFunctionBegin; 637 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 638 PetscValidPointer(type,2); 639 *type = ((PetscObject)ksp)->type_name; 640 PetscFunctionReturn(0); 641 } 642 643 #undef __FUNCT__ 644 #define __FUNCT__ "KSPRegister" 645 /*@C 646 KSPRegister - See KSPRegisterDynamic() 647 648 Level: advanced 649 @*/ 650 PetscErrorCode PETSCKSP_DLLEXPORT KSPRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(KSP)) 651 { 652 PetscErrorCode ierr; 653 char fullname[PETSC_MAX_PATH_LEN]; 654 655 PetscFunctionBegin; 656 ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 657 ierr = PetscFListAdd(&KSPList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 658 PetscFunctionReturn(0); 659 } 660 661 #undef __FUNCT__ 662 #define __FUNCT__ "KSPSetNullSpace" 663 /*@ 664 KSPSetNullSpace - Sets the null space of the operator 665 666 Logically Collective on KSP 667 668 Input Parameters: 669 + ksp - the Krylov space object 670 - nullsp - the null space of the operator 671 672 Level: advanced 673 674 .seealso: KSPSetOperators(), MatNullSpaceCreate(), KSPGetNullSpace() 675 @*/ 676 PetscErrorCode PETSCKSP_DLLEXPORT KSPSetNullSpace(KSP ksp,MatNullSpace nullsp) 677 { 678 PetscErrorCode ierr; 679 680 PetscFunctionBegin; 681 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 682 PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2); 683 ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr); 684 if (ksp->nullsp) { ierr = MatNullSpaceDestroy(ksp->nullsp);CHKERRQ(ierr); } 685 ksp->nullsp = nullsp; 686 PetscFunctionReturn(0); 687 } 688 689 #undef __FUNCT__ 690 #define __FUNCT__ "KSPGetNullSpace" 691 /*@ 692 KSPGetNullSpace - Gets the null space of the operator 693 694 Not Collective 695 696 Input Parameters: 697 + ksp - the Krylov space object 698 - nullsp - the null space of the operator 699 700 Level: advanced 701 702 .seealso: KSPSetOperators(), MatNullSpaceCreate(), KSPSetNullSpace() 703 @*/ 704 PetscErrorCode PETSCKSP_DLLEXPORT KSPGetNullSpace(KSP ksp,MatNullSpace *nullsp) 705 { 706 PetscFunctionBegin; 707 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 708 PetscValidPointer(nullsp,2); 709 *nullsp = ksp->nullsp; 710 PetscFunctionReturn(0); 711 } 712 713