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