1 #define PETSCKSP_DLL 2 3 /* 4 The basic KSP routines, Create, View etc. are here. 5 */ 6 #include "private/kspimpl.h" /*I "petscksp.h" I*/ 7 8 /* Logging support */ 9 PetscClassId PETSCKSP_DLLEXPORT KSP_CLASSID; 10 PetscLogEvent KSP_GMRESOrthogonalization, KSP_SetUp, KSP_Solve; 11 12 /* 13 Contains the list of registered KSP routines 14 */ 15 PetscFList KSPList = 0; 16 PetscTruth KSPRegisterAllCalled = PETSC_FALSE; 17 18 #undef __FUNCT__ 19 #define __FUNCT__ "KSPView" 20 /*@C 21 KSPView - Prints the KSP data structure. 22 23 Collective on KSP 24 25 Input Parameters: 26 + ksp - the Krylov space context 27 - viewer - visualization context 28 29 Options Database Keys: 30 . -ksp_view - print the ksp data structure at the end of a KSPSolve call 31 32 Note: 33 The available visualization contexts include 34 + PETSC_VIEWER_STDOUT_SELF - standard output (default) 35 - PETSC_VIEWER_STDOUT_WORLD - synchronized standard 36 output where only the first processor opens 37 the file. All other processors send their 38 data to the first processor to print. 39 40 The user can open an alternative visualization context with 41 PetscViewerASCIIOpen() - output to a specified file. 42 43 Level: beginner 44 45 .keywords: KSP, view 46 47 .seealso: PCView(), PetscViewerASCIIOpen() 48 @*/ 49 PetscErrorCode PETSCKSP_DLLEXPORT KSPView(KSP ksp,PetscViewer viewer) 50 { 51 const KSPType type; 52 PetscErrorCode ierr; 53 PetscTruth iascii; 54 55 PetscFunctionBegin; 56 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 57 if (!viewer) viewer = PETSC_VIEWER_STDOUT_(((PetscObject)ksp)->comm); 58 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 59 PetscCheckSameComm(ksp,1,viewer,2); 60 61 ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 62 if (iascii) { 63 ierr = KSPGetType(ksp,&type);CHKERRQ(ierr); 64 if (((PetscObject)ksp)->prefix) { 65 ierr = PetscViewerASCIIPrintf(viewer,"KSP Object:(%s)\n",((PetscObject)ksp)->prefix);CHKERRQ(ierr); 66 } else { 67 ierr = PetscViewerASCIIPrintf(viewer,"KSP Object:\n");CHKERRQ(ierr); 68 } 69 if (type) { 70 ierr = PetscViewerASCIIPrintf(viewer," type: %s\n",type);CHKERRQ(ierr); 71 } else { 72 ierr = PetscViewerASCIIPrintf(viewer," type: not yet set\n");CHKERRQ(ierr); 73 } 74 if (ksp->ops->view) { 75 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 76 ierr = (*ksp->ops->view)(ksp,viewer);CHKERRQ(ierr); 77 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 78 } 79 if (ksp->guess_zero) {ierr = PetscViewerASCIIPrintf(viewer," maximum iterations=%D, initial guess is zero\n",ksp->max_it);CHKERRQ(ierr);} 80 else {ierr = PetscViewerASCIIPrintf(viewer," maximum iterations=%D\n", ksp->max_it);CHKERRQ(ierr);} 81 if (ksp->guess_knoll) {ierr = PetscViewerASCIIPrintf(viewer," using preconditioner applied to right hand side for initial guess\n");CHKERRQ(ierr);} 82 ierr = PetscViewerASCIIPrintf(viewer," tolerances: relative=%G, absolute=%G, divergence=%G\n",ksp->rtol,ksp->abstol,ksp->divtol);CHKERRQ(ierr); 83 if (ksp->pc_side == PC_RIGHT) {ierr = PetscViewerASCIIPrintf(viewer," right preconditioning\n");CHKERRQ(ierr);} 84 else if (ksp->pc_side == PC_SYMMETRIC) {ierr = PetscViewerASCIIPrintf(viewer," symmetric preconditioning\n");CHKERRQ(ierr);} 85 else {ierr = PetscViewerASCIIPrintf(viewer," left preconditioning\n");CHKERRQ(ierr);} 86 if (ksp->guess) {ierr = PetscViewerASCIIPrintf(viewer," using Fischers initial guess method %D with size %D\n",ksp->guess->method,ksp->guess->maxl);CHKERRQ(ierr);} 87 if (ksp->dscale) {ierr = PetscViewerASCIIPrintf(viewer," diagonally scaled system\n");CHKERRQ(ierr);} 88 if (ksp->nullsp) {ierr = PetscViewerASCIIPrintf(viewer," has attached null space\n");CHKERRQ(ierr);} 89 if (!ksp->guess_zero) {ierr = PetscViewerASCIIPrintf(viewer," using nonzero initial guess\n");CHKERRQ(ierr);} 90 ierr = PetscViewerASCIIPrintf(viewer," using %s norm type for convergence test\n",KSPNormTypes[ksp->normtype]);CHKERRQ(ierr); 91 } else { 92 if (ksp->ops->view) { 93 ierr = (*ksp->ops->view)(ksp,viewer);CHKERRQ(ierr); 94 } 95 } 96 if (!ksp->pc) {ierr = KSPGetPC(ksp,&ksp->pc);CHKERRQ(ierr);} 97 ierr = PCView(ksp->pc,viewer);CHKERRQ(ierr); 98 PetscFunctionReturn(0); 99 } 100 101 102 #undef __FUNCT__ 103 #define __FUNCT__ "KSPSetNormType" 104 /*@ 105 KSPSetNormType - Sets the norm that is used for convergence testing. 106 107 Logically Collective on KSP 108 109 Input Parameter: 110 + ksp - Krylov solver context 111 - normtype - one of 112 $ KSP_NORM_NO - skips computing the norm, this should only be used if you are using 113 $ the Krylov method as a smoother with a fixed small number of iterations. 114 $ Implicitly sets KSPSkipConverged as KSP convergence test. 115 $ Supported only by CG, Richardson, Bi-CG-stab, CR, and CGS methods. 116 $ KSP_NORM_PRECONDITIONED - the default for left preconditioned solves, uses the l2 norm 117 $ of the preconditioned residual 118 $ KSP_NORM_UNPRECONDITIONED - uses the l2 norm of the true b - Ax residual, supported only by 119 $ CG, CHEBYCHEV, and RICHARDSON, automatically true for right (see KSPSetPCSide()) 120 $ preconditioning.. 121 $ KSP_NORM_NATURAL - supported by KSPCG, KSPCR, KSPCGNE, KSPCGS 122 123 124 Options Database Key: 125 . -ksp_norm_type <none,preconditioned,unpreconditioned,natural> 126 127 Notes: 128 Currently only works with the CG, Richardson, Bi-CG-stab, CR, and CGS methods. 129 130 Level: advanced 131 132 .keywords: KSP, create, context, norms 133 134 .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSPSkipConverged() 135 @*/ 136 PetscErrorCode PETSCKSP_DLLEXPORT KSPSetNormType(KSP ksp,KSPNormType normtype) 137 { 138 PetscErrorCode ierr; 139 140 PetscFunctionBegin; 141 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 142 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 Logically 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 Logically 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->setupstage == KSP_SETUP_NEWRHS) ksp->setupstage = KSP_SETUP_NEWMATRIX; /* so that next solve call will call PCSetUp() on new matrix */ 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 same as Amat. 387 - flag - flag indicating information about the preconditioner matrix structure 388 during successive linear solves. This flag is ignored the first time a 389 linear system is solved, and thus is irrelevant when solving just one linear 390 system. 391 392 Level: intermediate 393 394 Notes: DOES NOT increase the reference counts of the matrix, so you should NOT destroy them. 395 396 .keywords: KSP, set, get, operators, matrix, preconditioner, linear system 397 398 .seealso: KSPSolve(), KSPGetPC(), PCGetOperators(), PCSetOperators(), KSPSetOperators(), KSPGetOperatorsSet() 399 @*/ 400 PetscErrorCode PETSCKSP_DLLEXPORT KSPGetOperators(KSP ksp,Mat *Amat,Mat *Pmat,MatStructure *flag) 401 { 402 PetscErrorCode ierr; 403 404 PetscFunctionBegin; 405 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 406 if (!ksp->pc) {ierr = KSPGetPC(ksp,&ksp->pc);CHKERRQ(ierr);} 407 ierr = PCGetOperators(ksp->pc,Amat,Pmat,flag);CHKERRQ(ierr); 408 PetscFunctionReturn(0); 409 } 410 411 #undef __FUNCT__ 412 #define __FUNCT__ "KSPGetOperatorsSet" 413 /*@C 414 KSPGetOperatorsSet - Determines if the matrix associated with the linear system and 415 possibly a different one associated with the preconditioner have been set in the KSP. 416 417 Not collective, though the results on all processes should be the same 418 419 Input Parameter: 420 . pc - the preconditioner context 421 422 Output Parameters: 423 + mat - the matrix associated with the linear system was set 424 - pmat - matrix associated with the preconditioner was set, usually the same 425 426 Level: intermediate 427 428 .keywords: KSP, get, operators, matrix, linear system 429 430 .seealso: PCSetOperators(), KSPGetOperators(), KSPSetOperators(), PCGetOperators(), PCGetOperatorsSet() 431 @*/ 432 PetscErrorCode PETSCKSP_DLLEXPORT KSPGetOperatorsSet(KSP ksp,PetscTruth *mat,PetscTruth *pmat) 433 { 434 PetscErrorCode ierr; 435 436 PetscFunctionBegin; 437 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 438 if (!ksp->pc) {ierr = KSPGetPC(ksp,&ksp->pc);CHKERRQ(ierr);} 439 ierr = PCGetOperatorsSet(ksp->pc,mat,pmat);CHKERRQ(ierr); 440 PetscFunctionReturn(0); 441 } 442 443 #undef __FUNCT__ 444 #define __FUNCT__ "KSPCreate" 445 /*@ 446 KSPCreate - Creates the default KSP context. 447 448 Collective on MPI_Comm 449 450 Input Parameter: 451 . comm - MPI communicator 452 453 Output Parameter: 454 . ksp - location to put the KSP context 455 456 Notes: 457 The default KSP type is GMRES with a restart of 30, using modified Gram-Schmidt 458 orthogonalization. 459 460 Level: beginner 461 462 .keywords: KSP, create, context 463 464 .seealso: KSPSetUp(), KSPSolve(), KSPDestroy(), KSP 465 @*/ 466 PetscErrorCode PETSCKSP_DLLEXPORT KSPCreate(MPI_Comm comm,KSP *inksp) 467 { 468 KSP ksp; 469 PetscErrorCode ierr; 470 void *ctx; 471 472 PetscFunctionBegin; 473 PetscValidPointer(inksp,2); 474 *inksp = 0; 475 #ifndef PETSC_USE_DYNAMIC_LIBRARIES 476 ierr = KSPInitializePackage(PETSC_NULL);CHKERRQ(ierr); 477 #endif 478 479 ierr = PetscHeaderCreate(ksp,_p_KSP,struct _KSPOps,KSP_CLASSID,-1,"KSP",comm,KSPDestroy,KSPView);CHKERRQ(ierr); 480 481 ksp->max_it = 10000; 482 ksp->pc_side = PC_LEFT; 483 ksp->rtol = 1.e-5; 484 ksp->abstol = 1.e-50; 485 ksp->divtol = 1.e4; 486 487 ksp->chknorm = -1; 488 ksp->normtype = KSP_NORM_PRECONDITIONED; 489 ksp->rnorm = 0.0; 490 ksp->its = 0; 491 ksp->guess_zero = PETSC_TRUE; 492 ksp->calc_sings = PETSC_FALSE; 493 ksp->res_hist = PETSC_NULL; 494 ksp->res_hist_alloc = PETSC_NULL; 495 ksp->res_hist_len = 0; 496 ksp->res_hist_max = 0; 497 ksp->res_hist_reset = PETSC_TRUE; 498 ksp->numbermonitors = 0; 499 500 ierr = KSPDefaultConvergedCreate(&ctx);CHKERRQ(ierr); 501 ierr = KSPSetConvergenceTest(ksp,KSPDefaultConverged,ctx,KSPDefaultConvergedDestroy);CHKERRQ(ierr); 502 ksp->ops->buildsolution = KSPDefaultBuildSolution; 503 ksp->ops->buildresidual = KSPDefaultBuildResidual; 504 505 ksp->vec_sol = 0; 506 ksp->vec_rhs = 0; 507 ksp->pc = 0; 508 ksp->data = 0; 509 ksp->nwork = 0; 510 ksp->work = 0; 511 ksp->reason = KSP_CONVERGED_ITERATING; 512 ksp->setupstage = KSP_SETUP_NEW; 513 514 ierr = PetscPublishAll(ksp);CHKERRQ(ierr); 515 *inksp = ksp; 516 PetscFunctionReturn(0); 517 } 518 519 #undef __FUNCT__ 520 #define __FUNCT__ "KSPSetType" 521 /*@C 522 KSPSetType - Builds KSP for a particular solver. 523 524 Logically Collective on KSP 525 526 Input Parameters: 527 + ksp - the Krylov space context 528 - type - a known method 529 530 Options Database Key: 531 . -ksp_type <method> - Sets the method; use -help for a list 532 of available methods (for instance, cg or gmres) 533 534 Notes: 535 See "petsc/include/petscksp.h" for available methods (for instance, 536 KSPCG or KSPGMRES). 537 538 Normally, it is best to use the KSPSetFromOptions() command and 539 then set the KSP type from the options database rather than by using 540 this routine. Using the options database provides the user with 541 maximum flexibility in evaluating the many different Krylov methods. 542 The KSPSetType() routine is provided for those situations where it 543 is necessary to set the iterative solver independently of the command 544 line or options database. This might be the case, for example, when 545 the choice of iterative solver changes during the execution of the 546 program, and the user's application is taking responsibility for 547 choosing the appropriate method. In other words, this routine is 548 not for beginners. 549 550 Level: intermediate 551 552 .keywords: KSP, set, method 553 554 .seealso: PCSetType(), KSPType 555 556 @*/ 557 PetscErrorCode PETSCKSP_DLLEXPORT KSPSetType(KSP ksp, const KSPType type) 558 { 559 PetscErrorCode ierr,(*r)(KSP); 560 PetscTruth match; 561 562 PetscFunctionBegin; 563 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 564 PetscValidCharPointer(type,2); 565 566 ierr = PetscTypeCompare((PetscObject)ksp,type,&match);CHKERRQ(ierr); 567 if (match) PetscFunctionReturn(0); 568 569 ierr = PetscFListFind(KSPList,((PetscObject)ksp)->comm,type,(void (**)(void)) &r);CHKERRQ(ierr); 570 if (!r) SETERRQ1(((PetscObject)ksp)->comm,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested KSP type %s",type); 571 /* Destroy the previous private KSP context */ 572 if (ksp->ops->destroy) { ierr = (*ksp->ops->destroy)(ksp);CHKERRQ(ierr); } 573 /* Reinitialize function pointers in KSPOps structure */ 574 ierr = PetscMemzero(ksp->ops,sizeof(struct _KSPOps));CHKERRQ(ierr); 575 ksp->ops->buildsolution = KSPDefaultBuildSolution; 576 ksp->ops->buildresidual = KSPDefaultBuildResidual; 577 /* Call the KSPCreate_XXX routine for this particular Krylov solver */ 578 ksp->setupstage = KSP_SETUP_NEW; 579 ierr = (*r)(ksp);CHKERRQ(ierr); 580 ierr = PetscObjectChangeTypeName((PetscObject)ksp,type);CHKERRQ(ierr); 581 PetscFunctionReturn(0); 582 } 583 584 #undef __FUNCT__ 585 #define __FUNCT__ "KSPRegisterDestroy" 586 /*@ 587 KSPRegisterDestroy - Frees the list of KSP methods that were 588 registered by KSPRegisterDynamic(). 589 590 Not Collective 591 592 Level: advanced 593 594 .keywords: KSP, register, destroy 595 596 .seealso: KSPRegisterDynamic(), KSPRegisterAll() 597 @*/ 598 PetscErrorCode PETSCKSP_DLLEXPORT KSPRegisterDestroy(void) 599 { 600 PetscErrorCode ierr; 601 602 PetscFunctionBegin; 603 ierr = PetscFListDestroy(&KSPList);CHKERRQ(ierr); 604 KSPRegisterAllCalled = PETSC_FALSE; 605 PetscFunctionReturn(0); 606 } 607 608 #undef __FUNCT__ 609 #define __FUNCT__ "KSPGetType" 610 /*@C 611 KSPGetType - Gets the KSP type as a string from the KSP object. 612 613 Not Collective 614 615 Input Parameter: 616 . ksp - Krylov context 617 618 Output Parameter: 619 . name - name of KSP method 620 621 Level: intermediate 622 623 .keywords: KSP, get, method, name 624 625 .seealso: KSPSetType() 626 @*/ 627 PetscErrorCode PETSCKSP_DLLEXPORT KSPGetType(KSP ksp,const KSPType *type) 628 { 629 PetscFunctionBegin; 630 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 631 PetscValidPointer(type,2); 632 *type = ((PetscObject)ksp)->type_name; 633 PetscFunctionReturn(0); 634 } 635 636 #undef __FUNCT__ 637 #define __FUNCT__ "KSPRegister" 638 /*@C 639 KSPRegister - See KSPRegisterDynamic() 640 641 Level: advanced 642 @*/ 643 PetscErrorCode PETSCKSP_DLLEXPORT KSPRegister(const char sname[],const char path[],const char name[],PetscErrorCode (*function)(KSP)) 644 { 645 PetscErrorCode ierr; 646 char fullname[PETSC_MAX_PATH_LEN]; 647 648 PetscFunctionBegin; 649 ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 650 ierr = PetscFListAdd(&KSPList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 651 PetscFunctionReturn(0); 652 } 653 654 #undef __FUNCT__ 655 #define __FUNCT__ "KSPSetNullSpace" 656 /*@ 657 KSPSetNullSpace - Sets the null space of the operator 658 659 Logically Collective on KSP 660 661 Input Parameters: 662 + ksp - the Krylov space object 663 - nullsp - the null space of the operator 664 665 Level: advanced 666 667 .seealso: KSPSetOperators(), MatNullSpaceCreate(), KSPGetNullSpace() 668 @*/ 669 PetscErrorCode PETSCKSP_DLLEXPORT KSPSetNullSpace(KSP ksp,MatNullSpace nullsp) 670 { 671 PetscErrorCode ierr; 672 673 PetscFunctionBegin; 674 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 675 PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_CLASSID,2); 676 ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr); 677 if (ksp->nullsp) { ierr = MatNullSpaceDestroy(ksp->nullsp);CHKERRQ(ierr); } 678 ksp->nullsp = nullsp; 679 PetscFunctionReturn(0); 680 } 681 682 #undef __FUNCT__ 683 #define __FUNCT__ "KSPGetNullSpace" 684 /*@ 685 KSPGetNullSpace - Gets the null space of the operator 686 687 Not Collective 688 689 Input Parameters: 690 + ksp - the Krylov space object 691 - nullsp - the null space of the operator 692 693 Level: advanced 694 695 .seealso: KSPSetOperators(), MatNullSpaceCreate(), KSPSetNullSpace() 696 @*/ 697 PetscErrorCode PETSCKSP_DLLEXPORT KSPGetNullSpace(KSP ksp,MatNullSpace *nullsp) 698 { 699 PetscFunctionBegin; 700 PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); 701 PetscValidPointer(nullsp,2); 702 *nullsp = ksp->nullsp; 703 PetscFunctionReturn(0); 704 } 705 706