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