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 All future calls to KSPSetOperators() must use the same size matrices! 294 295 Passing a PETSC_NULL for Amat or Pmat removes the matrix that is currently used. 296 297 If you wish to replace either Amat or Pmat but leave the other one untouched then 298 first call KSPGetOperators() to get the one you wish to keep, call PetscObjectReference() 299 on it and then pass it back in in your call to KSPSetOperators(). 300 301 Caution: 302 If you specify SAME_NONZERO_PATTERN, PETSc believes your assertion 303 and does not check the structure of the matrix. If you erroneously 304 claim that the structure is the same when it actually is not, the new 305 preconditioner will not function correctly. Thus, use this optimization 306 feature carefully! 307 308 If in doubt about whether your preconditioner matrix has changed 309 structure or not, use the flag DIFFERENT_NONZERO_PATTERN. 310 311 Level: beginner 312 313 Alternative usage: If the operators have NOT been set with KSP/PCSetOperators() then the operators 314 are created in PC and returned to the user. In this case, if both operators 315 mat and pmat are requested, two DIFFERENT operators will be returned. If 316 only one is requested both operators in the PC will be the same (i.e. as 317 if one had called KSP/PCSetOperators() with the same argument for both Mats). 318 The user must set the sizes of the returned matrices and their type etc just 319 as if the user created them with MatCreate(). For example, 320 321 $ KSP/PCGetOperators(ksp/pc,&mat,PETSC_NULL,PETSC_NULL); is equivalent to 322 $ set size, type, etc of mat 323 324 $ MatCreate(comm,&mat); 325 $ KSP/PCSetOperators(ksp/pc,mat,mat,SAME_NONZERO_PATTERN); 326 $ PetscObjectDereference((PetscObject)mat); 327 $ set size, type, etc of mat 328 329 and 330 331 $ KSP/PCGetOperators(ksp/pc,&mat,&pmat,PETSC_NULL); is equivalent to 332 $ set size, type, etc of mat and pmat 333 334 $ MatCreate(comm,&mat); 335 $ MatCreate(comm,&pmat); 336 $ KSP/PCSetOperators(ksp/pc,mat,pmat,SAME_NONZERO_PATTERN); 337 $ PetscObjectDereference((PetscObject)mat); 338 $ PetscObjectDereference((PetscObject)pmat); 339 $ set size, type, etc of mat and pmat 340 341 The rational for this support is so that when creating a TS, SNES, or KSP the hierarchy 342 of underlying objects (i.e. SNES, KSP, PC, Mat) and their livespans can be completely 343 managed by the top most level object (i.e. the TS, SNES, or KSP). Another way to look 344 at this is when you create a SNES you do not NEED to create a KSP and attach it to 345 the SNES object (the SNES object manages it for you). Similarly when you create a KSP 346 you do not need to attach a PC to it (the KSP object manages the PC object for you). 347 Thus, why should YOU have to create the Mat and attach it to the SNES/KSP/PC, when 348 it can be created for you? 349 350 .keywords: KSP, set, operators, matrix, preconditioner, linear system 351 352 .seealso: KSPSolve(), KSPGetPC(), PCGetOperators(), PCSetOperators(), KSPGetOperators() 353 @*/ 354 PetscErrorCode PETSCKSP_DLLEXPORT KSPSetOperators(KSP ksp,Mat Amat,Mat Pmat,MatStructure flag) 355 { 356 PetscErrorCode ierr; 357 358 PetscFunctionBegin; 359 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 360 if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2); 361 if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3); 362 if (Amat) PetscCheckSameComm(ksp,1,Amat,2); 363 if (Pmat) PetscCheckSameComm(ksp,1,Pmat,3); 364 if (!ksp->pc) {ierr = KSPGetPC(ksp,&ksp->pc);CHKERRQ(ierr);} 365 ierr = PCSetOperators(ksp->pc,Amat,Pmat,flag);CHKERRQ(ierr); 366 if (ksp->setupcalled > 1) ksp->setupcalled = 1; /* so that next solve call will call setup */ 367 if (ksp->guess) { 368 ierr = KSPFischerGuessReset(ksp->guess);CHKERRQ(ierr); 369 } 370 PetscFunctionReturn(0); 371 } 372 373 #undef __FUNCT__ 374 #define __FUNCT__ "KSPGetOperators" 375 /*@ 376 KSPGetOperators - Gets the matrix associated with the linear system 377 and a (possibly) different one associated with the preconditioner. 378 379 Collective on KSP and Mat 380 381 Input Parameter: 382 . ksp - the KSP context 383 384 Output Parameters: 385 + Amat - the matrix associated with the linear system 386 . Pmat - the matrix to be used in constructing the preconditioner, usually the 387 same as Amat. 388 - flag - flag indicating information about the preconditioner matrix structure 389 during successive linear solves. This flag is ignored the first time a 390 linear system is solved, and thus is irrelevant when solving just one linear 391 system. 392 393 Level: intermediate 394 395 .keywords: KSP, set, get, operators, matrix, preconditioner, linear system 396 397 .seealso: KSPSolve(), KSPGetPC(), PCGetOperators(), PCSetOperators(), KSPSetOperators(), KSPGetOperatorsSet() 398 @*/ 399 PetscErrorCode PETSCKSP_DLLEXPORT KSPGetOperators(KSP ksp,Mat *Amat,Mat *Pmat,MatStructure *flag) 400 { 401 PetscErrorCode ierr; 402 403 PetscFunctionBegin; 404 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 405 if (!ksp->pc) {ierr = KSPGetPC(ksp,&ksp->pc);CHKERRQ(ierr);} 406 ierr = PCGetOperators(ksp->pc,Amat,Pmat,flag);CHKERRQ(ierr); 407 PetscFunctionReturn(0); 408 } 409 410 #undef __FUNCT__ 411 #define __FUNCT__ "KSPGetOperatorsSet" 412 /*@C 413 KSPGetOperatorsSet - Determines if the matrix associated with the linear system and 414 possibly a different one associated with the preconditioner have been set in the KSP. 415 416 Not collective, though the results on all processes should be the same 417 418 Input Parameter: 419 . pc - the preconditioner context 420 421 Output Parameters: 422 + mat - the matrix associated with the linear system was set 423 - pmat - matrix associated with the preconditioner was set, usually the same 424 425 Level: intermediate 426 427 .keywords: KSP, get, operators, matrix, linear system 428 429 .seealso: PCSetOperators(), KSPGetOperators(), KSPSetOperators(), PCGetOperators(), PCGetOperatorsSet() 430 @*/ 431 PetscErrorCode PETSCKSP_DLLEXPORT KSPGetOperatorsSet(KSP ksp,PetscTruth *mat,PetscTruth *pmat) 432 { 433 PetscErrorCode ierr; 434 435 PetscFunctionBegin; 436 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 437 if (!ksp->pc) {ierr = KSPGetPC(ksp,&ksp->pc);CHKERRQ(ierr);} 438 ierr = PCGetOperatorsSet(ksp->pc,mat,pmat);CHKERRQ(ierr); 439 PetscFunctionReturn(0); 440 } 441 442 #undef __FUNCT__ 443 #define __FUNCT__ "KSPCreate" 444 /*@ 445 KSPCreate - Creates the default KSP context. 446 447 Collective on MPI_Comm 448 449 Input Parameter: 450 . comm - MPI communicator 451 452 Output Parameter: 453 . ksp - location to put the KSP context 454 455 Notes: 456 The default KSP type is GMRES with a restart of 30, using modified Gram-Schmidt 457 orthogonalization. 458 459 Level: beginner 460 461 .keywords: KSP, create, context 462 463 .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSP 464 @*/ 465 PetscErrorCode PETSCKSP_DLLEXPORT KSPCreate(MPI_Comm comm,KSP *inksp) 466 { 467 KSP ksp; 468 PetscErrorCode ierr; 469 void *ctx; 470 471 PetscFunctionBegin; 472 PetscValidPointer(inksp,2); 473 *inksp = 0; 474 #ifndef PETSC_USE_DYNAMIC_LIBRARIES 475 ierr = KSPInitializePackage(PETSC_NULL);CHKERRQ(ierr); 476 #endif 477 478 ierr = PetscHeaderCreate(ksp,_p_KSP,struct _KSPOps,KSP_CLASSID,-1,"KSP",comm,KSPDestroy,KSPView);CHKERRQ(ierr); 479 480 ksp->max_it = 10000; 481 ksp->pc_side = PC_LEFT; 482 ksp->rtol = 1.e-5; 483 ksp->abstol = 1.e-50; 484 ksp->divtol = 1.e4; 485 486 ksp->chknorm = -1; 487 ksp->normtype = KSP_NORM_PRECONDITIONED; 488 ksp->rnorm = 0.0; 489 ksp->its = 0; 490 ksp->guess_zero = PETSC_TRUE; 491 ksp->calc_sings = PETSC_FALSE; 492 ksp->res_hist = PETSC_NULL; 493 ksp->res_hist_alloc = PETSC_NULL; 494 ksp->res_hist_len = 0; 495 ksp->res_hist_max = 0; 496 ksp->res_hist_reset = PETSC_TRUE; 497 ksp->numbermonitors = 0; 498 499 ierr = KSPDefaultConvergedCreate(&ctx);CHKERRQ(ierr); 500 ierr = KSPSetConvergenceTest(ksp,KSPDefaultConverged,ctx,KSPDefaultConvergedDestroy);CHKERRQ(ierr); 501 ksp->ops->buildsolution = KSPDefaultBuildSolution; 502 ksp->ops->buildresidual = KSPDefaultBuildResidual; 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->setupcalled = 0; 512 513 ierr = PetscPublishAll(ksp);CHKERRQ(ierr); 514 *inksp = ksp; 515 PetscFunctionReturn(0); 516 } 517 518 #undef __FUNCT__ 519 #define __FUNCT__ "KSPSetType" 520 /*@C 521 KSPSetType - Builds KSP for a particular solver. 522 523 Collective on KSP 524 525 Input Parameters: 526 + ksp - the Krylov space context 527 - type - a known method 528 529 Options Database Key: 530 . -ksp_type <method> - Sets the method; use -help for a list 531 of available methods (for instance, cg or gmres) 532 533 Notes: 534 See "petsc/include/petscksp.h" for available methods (for instance, 535 KSPCG or KSPGMRES). 536 537 Normally, it is best to use the KSPSetFromOptions() command and 538 then set the KSP type from the options database rather than by using 539 this routine. Using the options database provides the user with 540 maximum flexibility in evaluating the many different Krylov methods. 541 The KSPSetType() routine is provided for those situations where it 542 is necessary to set the iterative solver independently of the command 543 line or options database. This might be the case, for example, when 544 the choice of iterative solver changes during the execution of the 545 program, and the user's application is taking responsibility for 546 choosing the appropriate method. In other words, this routine is 547 not for beginners. 548 549 Level: intermediate 550 551 .keywords: KSP, set, method 552 553 .seealso: PCSetType(), KSPType 554 555 @*/ 556 PetscErrorCode PETSCKSP_DLLEXPORT KSPSetType(KSP ksp, const KSPType type) 557 { 558 PetscErrorCode ierr,(*r)(KSP); 559 PetscTruth match; 560 561 PetscFunctionBegin; 562 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 563 PetscValidCharPointer(type,2); 564 565 ierr = PetscTypeCompare((PetscObject)ksp,type,&match);CHKERRQ(ierr); 566 if (match) PetscFunctionReturn(0); 567 568 ierr = PetscFListFind(KSPList,((PetscObject)ksp)->comm,type,(void (**)(void)) &r);CHKERRQ(ierr); 569 if (!r) SETERRQ1(((PetscObject)ksp)->comm,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested KSP type %s",type); 570 /* Destroy the previous private KSP context */ 571 if (ksp->ops->destroy) { ierr = (*ksp->ops->destroy)(ksp);CHKERRQ(ierr); } 572 /* Reinitialize function pointers in KSPOps structure */ 573 ierr = PetscMemzero(ksp->ops,sizeof(struct _KSPOps));CHKERRQ(ierr); 574 ksp->ops->buildsolution = KSPDefaultBuildSolution; 575 ksp->ops->buildresidual = KSPDefaultBuildResidual; 576 /* Call the KSPCreate_XXX routine for this particular Krylov solver */ 577 ksp->setupcalled = 0; 578 ierr = (*r)(ksp);CHKERRQ(ierr); 579 ierr = PetscObjectChangeTypeName((PetscObject)ksp,type);CHKERRQ(ierr); 580 PetscFunctionReturn(0); 581 } 582 583 #undef __FUNCT__ 584 #define __FUNCT__ "KSPRegisterDestroy" 585 /*@ 586 KSPRegisterDestroy - Frees the list of KSP methods that were 587 registered by KSPRegisterDynamic(). 588 589 Not Collective 590 591 Level: advanced 592 593 .keywords: KSP, register, destroy 594 595 .seealso: KSPRegisterDynamic(), KSPRegisterAll() 596 @*/ 597 PetscErrorCode PETSCKSP_DLLEXPORT KSPRegisterDestroy(void) 598 { 599 PetscErrorCode ierr; 600 601 PetscFunctionBegin; 602 ierr = PetscFListDestroy(&KSPList);CHKERRQ(ierr); 603 KSPRegisterAllCalled = PETSC_FALSE; 604 PetscFunctionReturn(0); 605 } 606 607 #undef __FUNCT__ 608 #define __FUNCT__ "KSPGetType" 609 /*@C 610 KSPGetType - Gets the KSP type as a string from the KSP object. 611 612 Not Collective 613 614 Input Parameter: 615 . ksp - Krylov context 616 617 Output Parameter: 618 . name - name of KSP method 619 620 Level: intermediate 621 622 .keywords: KSP, get, method, name 623 624 .seealso: KSPSetType() 625 @*/ 626 PetscErrorCode PETSCKSP_DLLEXPORT KSPGetType(KSP ksp,const KSPType *type) 627 { 628 PetscFunctionBegin; 629 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 630 PetscValidPointer(type,2); 631 *type = ((PetscObject)ksp)->type_name; 632 PetscFunctionReturn(0); 633 } 634 635 #undef __FUNCT__ 636 #define __FUNCT__ "KSPRegister" 637 /*@C 638 KSPRegister - See KSPRegisterDynamic() 639 640 Level: advanced 641 @*/ 642 PetscErrorCode PETSCKSP_DLLEXPORT KSPRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(KSP)) 643 { 644 PetscErrorCode ierr; 645 char fullname[PETSC_MAX_PATH_LEN]; 646 647 PetscFunctionBegin; 648 ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 649 ierr = PetscFListAdd(&KSPList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 650 PetscFunctionReturn(0); 651 } 652 653 #undef __FUNCT__ 654 #define __FUNCT__ "KSPSetNullSpace" 655 /*@ 656 KSPSetNullSpace - Sets the null space of the operator 657 658 Collective on KSP 659 660 Input Parameters: 661 + ksp - the Krylov space object 662 - nullsp - the null space of the operator 663 664 Level: advanced 665 666 .seealso: KSPSetOperators(), MatNullSpaceCreate(), KSPGetNullSpace() 667 @*/ 668 PetscErrorCode PETSCKSP_DLLEXPORT KSPSetNullSpace(KSP ksp,MatNullSpace nullsp) 669 { 670 PetscErrorCode ierr; 671 672 PetscFunctionBegin; 673 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 674 PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2); 675 ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr); 676 if (ksp->nullsp) { ierr = MatNullSpaceDestroy(ksp->nullsp);CHKERRQ(ierr); } 677 ksp->nullsp = nullsp; 678 PetscFunctionReturn(0); 679 } 680 681 #undef __FUNCT__ 682 #define __FUNCT__ "KSPGetNullSpace" 683 /*@ 684 KSPGetNullSpace - Gets the null space of the operator 685 686 Collective on KSP 687 688 Input Parameters: 689 + ksp - the Krylov space object 690 - nullsp - the null space of the operator 691 692 Level: advanced 693 694 .seealso: KSPSetOperators(), MatNullSpaceCreate(), KSPSetNullSpace() 695 @*/ 696 PetscErrorCode PETSCKSP_DLLEXPORT KSPGetNullSpace(KSP ksp,MatNullSpace *nullsp) 697 { 698 PetscFunctionBegin; 699 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 700 PetscValidPointer(nullsp,2); 701 *nullsp = ksp->nullsp; 702 PetscFunctionReturn(0); 703 } 704 705