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