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