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,PETSC_VIEWER_ASCII,&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 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 ksp->normtype = normtype; 143 if (normtype == KSP_NORM_NO) { 144 ierr = KSPSetConvergenceTest(ksp,KSPSkipConverged,0,0);CHKERRQ(ierr); 145 ierr = PetscInfo(ksp,"Warning: setting KSPNormType to skip computing the norm\n\ 146 KSP convergence test is implicitly set to KSPSkipConverged\n");CHKERRQ(ierr); 147 } 148 PetscFunctionReturn(0); 149 } 150 151 #undef __FUNCT__ 152 #define __FUNCT__ "KSPSetCheckNormIteration" 153 /*@ 154 KSPSetCheckNormIteration - Sets the first iteration at which the norm of the residual will be 155 computed and used in the convergence test. 156 157 Collective on KSP 158 159 Input Parameter: 160 + ksp - Krylov solver context 161 - it - use -1 to check at all iterations 162 163 Notes: 164 Currently only works with KSPCG, KSPBCGS and KSPIBCGS 165 166 Use KSPSetNormType(ksp,KSP_NORM_NO) to never check the norm 167 168 On steps where the norm is not computed, the previous norm is still in the variable, so if you run with, for example, 169 -ksp_monitor the residual norm will appear to be unchanged for several iterations (though it is not really unchanged). 170 Level: advanced 171 172 .keywords: KSP, create, context, norms 173 174 .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSPSkipConverged(), KSPSetNormType() 175 @*/ 176 PetscErrorCode PETSCKSP_DLLEXPORT KSPSetCheckNormIteration(KSP ksp,PetscInt it) 177 { 178 PetscFunctionBegin; 179 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 180 ksp->chknorm = it; 181 PetscFunctionReturn(0); 182 } 183 184 #undef __FUNCT__ 185 #define __FUNCT__ "KSPSetLagNorm" 186 /*@ 187 KSPSetLagNorm - Lags the residual norm calculation so that it is computed as part of the MPI_Allreduce() for 188 computing the inner products for the next iteration. This can reduce communication costs at the expense of doing 189 one additional iteration. 190 191 192 Collective on KSP 193 194 Input Parameter: 195 + ksp - Krylov solver context 196 - flg - PETSC_TRUE or PETSC_FALSE 197 198 Options Database Keys: 199 . -ksp_lag_norm - lag the calculated residual norm 200 201 Notes: 202 Currently only works with KSPIBCGS. 203 204 Use KSPSetNormType(ksp,KSP_NORM_NO) to never check the norm 205 206 If you lag the norm and run with, for example, -ksp_monitor, the residual norm reported will be the lagged one. 207 Level: advanced 208 209 .keywords: KSP, create, context, norms 210 211 .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSPSkipConverged(), KSPSetNormType() 212 @*/ 213 PetscErrorCode PETSCKSP_DLLEXPORT KSPSetLagNorm(KSP ksp,PetscTruth flg) 214 { 215 PetscFunctionBegin; 216 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 217 ksp->lagnorm = flg; 218 PetscFunctionReturn(0); 219 } 220 221 #undef __FUNCT__ 222 #define __FUNCT__ "KSPGetNormType" 223 /*@ 224 KSPGetNormType - Gets the norm that is used for convergence testing. 225 226 Not Collective 227 228 Input Parameter: 229 . ksp - Krylov solver context 230 231 Output Parameter: 232 . normtype - norm that is used for convergence testing 233 234 Level: advanced 235 236 .keywords: KSP, create, context, norms 237 238 .seealso: KSPNormType, KSPSetNormType(), KSPSkipConverged() 239 @*/ 240 PetscErrorCode PETSCKSP_DLLEXPORT KSPGetNormType(KSP ksp, KSPNormType *normtype) { 241 PetscFunctionBegin; 242 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 243 PetscValidPointer(normtype, 2); 244 *normtype = ksp->normtype; 245 PetscFunctionReturn(0); 246 } 247 248 #if 0 249 #undef __FUNCT__ 250 #define __FUNCT__ "KSPPublish_Petsc" 251 static PetscErrorCode KSPPublish_Petsc(PetscObject obj) 252 { 253 PetscFunctionBegin; 254 PetscFunctionReturn(0); 255 } 256 #endif 257 258 #undef __FUNCT__ 259 #define __FUNCT__ "KSPSetOperators" 260 /*@ 261 KSPSetOperators - Sets the matrix associated with the linear system 262 and a (possibly) different one associated with the preconditioner. 263 264 Collective on KSP and Mat 265 266 Input Parameters: 267 + ksp - the KSP context 268 . Amat - the matrix associated with the linear system 269 . Pmat - the matrix to be used in constructing the preconditioner, usually the 270 same as Amat. 271 - flag - flag indicating information about the preconditioner matrix structure 272 during successive linear solves. This flag is ignored the first time a 273 linear system is solved, and thus is irrelevant when solving just one linear 274 system. 275 276 Notes: 277 The flag can be used to eliminate unnecessary work in the preconditioner 278 during the repeated solution of linear systems of the same size. The 279 available options are 280 $ SAME_PRECONDITIONER - 281 $ Pmat is identical during successive linear solves. 282 $ This option is intended for folks who are using 283 $ different Amat and Pmat matrices and want to reuse the 284 $ same preconditioner matrix. For example, this option 285 $ saves work by not recomputing incomplete factorization 286 $ for ILU/ICC preconditioners. 287 $ SAME_NONZERO_PATTERN - 288 $ Pmat has the same nonzero structure during 289 $ successive linear solves. 290 $ DIFFERENT_NONZERO_PATTERN - 291 $ Pmat does not have the same nonzero structure. 292 293 Passing a PETSC_NULL for Amat or Pmat removes the matrix that is currently used. 294 295 If you wish to replace either Amat or Pmat but leave the other one untouched then 296 first call KSPGetOperators() to get the one you wish to keep, call PetscObjectReference() 297 on it and then pass it back in in your call to KSPSetOperators(). 298 299 Caution: 300 If you specify SAME_NONZERO_PATTERN, PETSc believes your assertion 301 and does not check the structure of the matrix. If you erroneously 302 claim that the structure is the same when it actually is not, the new 303 preconditioner will not function correctly. Thus, use this optimization 304 feature carefully! 305 306 If in doubt about whether your preconditioner matrix has changed 307 structure or not, use the flag DIFFERENT_NONZERO_PATTERN. 308 309 Level: beginner 310 311 Alternative usage: If the operators have NOT been set with KSP/PCSetOperators() then the operators 312 are created in PC and returned to the user. In this case, if both operators 313 mat and pmat are requested, two DIFFERENT operators will be returned. If 314 only one is requested both operators in the PC will be the same (i.e. as 315 if one had called KSP/PCSetOperators() with the same argument for both Mats). 316 The user must set the sizes of the returned matrices and their type etc just 317 as if the user created them with MatCreate(). For example, 318 319 $ KSP/PCGetOperators(ksp/pc,&mat,PETSC_NULL,PETSC_NULL); is equivalent to 320 $ set size, type, etc of mat 321 322 $ MatCreate(comm,&mat); 323 $ KSP/PCSetOperators(ksp/pc,mat,mat,SAME_NONZERO_PATTERN); 324 $ PetscObjectDereference((PetscObject)mat); 325 $ set size, type, etc of mat 326 327 and 328 329 $ KSP/PCGetOperators(ksp/pc,&mat,&pmat,PETSC_NULL); is equivalent to 330 $ set size, type, etc of mat and pmat 331 332 $ MatCreate(comm,&mat); 333 $ MatCreate(comm,&pmat); 334 $ KSP/PCSetOperators(ksp/pc,mat,pmat,SAME_NONZERO_PATTERN); 335 $ PetscObjectDereference((PetscObject)mat); 336 $ PetscObjectDereference((PetscObject)pmat); 337 $ set size, type, etc of mat and pmat 338 339 The rational for this support is so that when creating a TS, SNES, or KSP the hierarchy 340 of underlying objects (i.e. SNES, KSP, PC, Mat) and their livespans can be completely 341 managed by the top most level object (i.e. the TS, SNES, or KSP). Another way to look 342 at this is when you create a SNES you do not NEED to create a KSP and attach it to 343 the SNES object (the SNES object manages it for you). Similarly when you create a KSP 344 you do not need to attach a PC to it (the KSP object manages the PC object for you). 345 Thus, why should YOU have to create the Mat and attach it to the SNES/KSP/PC, when 346 it can be created for you? 347 348 .keywords: KSP, set, operators, matrix, preconditioner, linear system 349 350 .seealso: KSPSolve(), KSPGetPC(), PCGetOperators(), PCSetOperators(), KSPGetOperators() 351 @*/ 352 PetscErrorCode PETSCKSP_DLLEXPORT KSPSetOperators(KSP ksp,Mat Amat,Mat Pmat,MatStructure flag) 353 { 354 PetscErrorCode ierr; 355 356 PetscFunctionBegin; 357 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 358 if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 359 if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 360 if (Amat) PetscCheckSameComm(ksp,1,Amat,2); 361 if (Pmat) PetscCheckSameComm(ksp,1,Pmat,3); 362 if (!ksp->pc) {ierr = KSPGetPC(ksp,&ksp->pc);CHKERRQ(ierr);} 363 ierr = PCSetOperators(ksp->pc,Amat,Pmat,flag);CHKERRQ(ierr); 364 if (ksp->setupcalled > 1) ksp->setupcalled = 1; /* so that next solve call will call setup */ 365 if (ksp->guess) { 366 ierr = KSPFischerGuessReset(ksp->guess);CHKERRQ(ierr); 367 } 368 PetscFunctionReturn(0); 369 } 370 371 #undef __FUNCT__ 372 #define __FUNCT__ "KSPGetOperators" 373 /*@ 374 KSPGetOperators - Gets the matrix associated with the linear system 375 and a (possibly) different one associated with the preconditioner. 376 377 Collective on KSP and Mat 378 379 Input Parameter: 380 . ksp - the KSP context 381 382 Output Parameters: 383 + Amat - the matrix associated with the linear system 384 . Pmat - the matrix to be used in constructing the preconditioner, usually the 385 same as Amat. 386 - flag - flag indicating information about the preconditioner matrix structure 387 during successive linear solves. This flag is ignored the first time a 388 linear system is solved, and thus is irrelevant when solving just one linear 389 system. 390 391 Level: intermediate 392 393 .keywords: KSP, set, get, operators, matrix, preconditioner, linear system 394 395 .seealso: KSPSolve(), KSPGetPC(), PCGetOperators(), PCSetOperators(), KSPSetOperators(), KSPGetOperatorsSet() 396 @*/ 397 PetscErrorCode PETSCKSP_DLLEXPORT KSPGetOperators(KSP ksp,Mat *Amat,Mat *Pmat,MatStructure *flag) 398 { 399 PetscErrorCode ierr; 400 401 PetscFunctionBegin; 402 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 403 if (!ksp->pc) {ierr = KSPGetPC(ksp,&ksp->pc);CHKERRQ(ierr);} 404 ierr = PCGetOperators(ksp->pc,Amat,Pmat,flag);CHKERRQ(ierr); 405 PetscFunctionReturn(0); 406 } 407 408 #undef __FUNCT__ 409 #define __FUNCT__ "KSPGetOperatorsSet" 410 /*@C 411 KSPGetOperatorsSet - Determines if the matrix associated with the linear system and 412 possibly a different one associated with the preconditioner have been set in the KSP. 413 414 Not collective, though the results on all processes should be the same 415 416 Input Parameter: 417 . pc - the preconditioner context 418 419 Output Parameters: 420 + mat - the matrix associated with the linear system was set 421 - pmat - matrix associated with the preconditioner was set, usually the same 422 423 Level: intermediate 424 425 .keywords: KSP, get, operators, matrix, linear system 426 427 .seealso: PCSetOperators(), KSPGetOperators(), KSPSetOperators(), PCGetOperators(), PCGetOperatorsSet() 428 @*/ 429 PetscErrorCode PETSCKSP_DLLEXPORT KSPGetOperatorsSet(KSP ksp,PetscTruth *mat,PetscTruth *pmat) 430 { 431 PetscErrorCode ierr; 432 433 PetscFunctionBegin; 434 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 435 if (!ksp->pc) {ierr = KSPGetPC(ksp,&ksp->pc);CHKERRQ(ierr);} 436 ierr = PCGetOperatorsSet(ksp->pc,mat,pmat);CHKERRQ(ierr); 437 PetscFunctionReturn(0); 438 } 439 440 #undef __FUNCT__ 441 #define __FUNCT__ "KSPCreate" 442 /*@ 443 KSPCreate - Creates the default KSP context. 444 445 Collective on MPI_Comm 446 447 Input Parameter: 448 . comm - MPI communicator 449 450 Output Parameter: 451 . ksp - location to put the KSP context 452 453 Notes: 454 The default KSP type is GMRES with a restart of 30, using modified Gram-Schmidt 455 orthogonalization. 456 457 Level: beginner 458 459 .keywords: KSP, create, context 460 461 .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSP 462 @*/ 463 PetscErrorCode PETSCKSP_DLLEXPORT KSPCreate(MPI_Comm comm,KSP *inksp) 464 { 465 KSP ksp; 466 PetscErrorCode ierr; 467 void *ctx; 468 469 PetscFunctionBegin; 470 PetscValidPointer(inksp,2); 471 *inksp = 0; 472 #ifndef PETSC_USE_DYNAMIC_LIBRARIES 473 ierr = KSPInitializePackage(PETSC_NULL);CHKERRQ(ierr); 474 #endif 475 476 ierr = PetscHeaderCreate(ksp,_p_KSP,struct _KSPOps,KSP_CLASSID,-1,"KSP",comm,KSPDestroy,KSPView);CHKERRQ(ierr); 477 478 ksp->max_it = 10000; 479 ksp->pc_side = PC_LEFT; 480 ksp->rtol = 1.e-5; 481 ksp->abstol = 1.e-50; 482 ksp->divtol = 1.e4; 483 484 ksp->chknorm = -1; 485 ksp->normtype = KSP_NORM_PRECONDITIONED; 486 ksp->rnorm = 0.0; 487 ksp->its = 0; 488 ksp->guess_zero = PETSC_TRUE; 489 ksp->calc_sings = PETSC_FALSE; 490 ksp->res_hist = PETSC_NULL; 491 ksp->res_hist_alloc = PETSC_NULL; 492 ksp->res_hist_len = 0; 493 ksp->res_hist_max = 0; 494 ksp->res_hist_reset = PETSC_TRUE; 495 ksp->numbermonitors = 0; 496 497 ierr = KSPDefaultConvergedCreate(&ctx);CHKERRQ(ierr); 498 ierr = KSPSetConvergenceTest(ksp,KSPDefaultConverged,ctx,KSPDefaultConvergedDestroy);CHKERRQ(ierr); 499 ksp->ops->buildsolution = KSPDefaultBuildSolution; 500 ksp->ops->buildresidual = KSPDefaultBuildResidual; 501 502 ksp->vec_sol = 0; 503 ksp->vec_rhs = 0; 504 ksp->pc = 0; 505 ksp->data = 0; 506 ksp->nwork = 0; 507 ksp->work = 0; 508 ksp->reason = KSP_CONVERGED_ITERATING; 509 ksp->setupcalled = 0; 510 511 ierr = PetscPublishAll(ksp);CHKERRQ(ierr); 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 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 PETSCKSP_DLLEXPORT KSPSetType(KSP ksp, const KSPType type) 555 { 556 PetscErrorCode ierr,(*r)(KSP); 557 PetscTruth 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,(void (**)(void)) &r);CHKERRQ(ierr); 567 if (!r) SETERRQ1(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->setupcalled = 0; 576 ierr = (*r)(ksp);CHKERRQ(ierr); 577 ierr = PetscObjectChangeTypeName((PetscObject)ksp,type);CHKERRQ(ierr); 578 PetscFunctionReturn(0); 579 } 580 581 #undef __FUNCT__ 582 #define __FUNCT__ "KSPRegisterDestroy" 583 /*@ 584 KSPRegisterDestroy - Frees the list of KSP methods that were 585 registered by KSPRegisterDynamic(). 586 587 Not Collective 588 589 Level: advanced 590 591 .keywords: KSP, register, destroy 592 593 .seealso: KSPRegisterDynamic(), KSPRegisterAll() 594 @*/ 595 PetscErrorCode PETSCKSP_DLLEXPORT KSPRegisterDestroy(void) 596 { 597 PetscErrorCode ierr; 598 599 PetscFunctionBegin; 600 ierr = PetscFListDestroy(&KSPList);CHKERRQ(ierr); 601 KSPRegisterAllCalled = PETSC_FALSE; 602 PetscFunctionReturn(0); 603 } 604 605 #undef __FUNCT__ 606 #define __FUNCT__ "KSPGetType" 607 /*@C 608 KSPGetType - Gets the KSP type as a string from the KSP object. 609 610 Not Collective 611 612 Input Parameter: 613 . ksp - Krylov context 614 615 Output Parameter: 616 . name - name of KSP method 617 618 Level: intermediate 619 620 .keywords: KSP, get, method, name 621 622 .seealso: KSPSetType() 623 @*/ 624 PetscErrorCode PETSCKSP_DLLEXPORT KSPGetType(KSP ksp,const KSPType *type) 625 { 626 PetscFunctionBegin; 627 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 628 PetscValidPointer(type,2); 629 *type = ((PetscObject)ksp)->type_name; 630 PetscFunctionReturn(0); 631 } 632 633 #undef __FUNCT__ 634 #define __FUNCT__ "KSPRegister" 635 /*@C 636 KSPRegister - See KSPRegisterDynamic() 637 638 Level: advanced 639 @*/ 640 PetscErrorCode PETSCKSP_DLLEXPORT KSPRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(KSP)) 641 { 642 PetscErrorCode ierr; 643 char fullname[PETSC_MAX_PATH_LEN]; 644 645 PetscFunctionBegin; 646 ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 647 ierr = PetscFListAdd(&KSPList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 648 PetscFunctionReturn(0); 649 } 650 651 #undef __FUNCT__ 652 #define __FUNCT__ "KSPSetNullSpace" 653 /*@ 654 KSPSetNullSpace - Sets the null space of the operator 655 656 Collective on KSP 657 658 Input Parameters: 659 + ksp - the Krylov space object 660 - nullsp - the null space of the operator 661 662 Level: advanced 663 664 .seealso: KSPSetOperators(), MatNullSpaceCreate(), KSPGetNullSpace() 665 @*/ 666 PetscErrorCode PETSCKSP_DLLEXPORT KSPSetNullSpace(KSP ksp,MatNullSpace nullsp) 667 { 668 PetscErrorCode ierr; 669 670 PetscFunctionBegin; 671 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 672 PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2); 673 ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr); 674 if (ksp->nullsp) { ierr = MatNullSpaceDestroy(ksp->nullsp);CHKERRQ(ierr); } 675 ksp->nullsp = nullsp; 676 PetscFunctionReturn(0); 677 } 678 679 #undef __FUNCT__ 680 #define __FUNCT__ "KSPGetNullSpace" 681 /*@ 682 KSPGetNullSpace - Gets the null space of the operator 683 684 Collective on KSP 685 686 Input Parameters: 687 + ksp - the Krylov space object 688 - nullsp - the null space of the operator 689 690 Level: advanced 691 692 .seealso: KSPSetOperators(), MatNullSpaceCreate(), KSPSetNullSpace() 693 @*/ 694 PetscErrorCode PETSCKSP_DLLEXPORT KSPGetNullSpace(KSP ksp,MatNullSpace *nullsp) 695 { 696 PetscFunctionBegin; 697 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 698 PetscValidPointer(nullsp,2); 699 *nullsp = ksp->nullsp; 700 PetscFunctionReturn(0); 701 } 702 703