1 #define PETSCKSP_DLL 2 3 /* 4 Defines the multigrid preconditioner interface. 5 */ 6 #include "src/ksp/pc/impls/mg/mgimpl.h" /*I "petscmg.h" I*/ 7 8 9 #undef __FUNCT__ 10 #define __FUNCT__ "PCMGMCycle_Private" 11 PetscErrorCode PCMGMCycle_Private(PC_MG **mglevels,PetscTruth *converged) 12 { 13 PC_MG *mg = *mglevels,*mgc; 14 PetscErrorCode ierr; 15 PetscInt cycles = mg->cycles; 16 17 PetscFunctionBegin; 18 if (converged) *converged = PETSC_FALSE; 19 20 if (mg->eventsolve) {ierr = PetscLogEventBegin(mg->eventsolve,0,0,0,0);CHKERRQ(ierr);} 21 ierr = KSPSolve(mg->smoothd,mg->b,mg->x);CHKERRQ(ierr); 22 if (mg->eventsolve) {ierr = PetscLogEventEnd(mg->eventsolve,0,0,0,0);CHKERRQ(ierr);} 23 if (mg->level) { /* not the coarsest grid */ 24 ierr = (*mg->residual)(mg->A,mg->b,mg->x,mg->r);CHKERRQ(ierr); 25 26 /* if on finest level and have convergence criteria set */ 27 if (mg->level == mg->levels-1 && mg->ttol) { 28 PetscReal rnorm; 29 ierr = VecNorm(mg->r,NORM_2,&rnorm);CHKERRQ(ierr); 30 if (rnorm <= mg->ttol) { 31 *converged = PETSC_TRUE; 32 if (rnorm < mg->abstol) { 33 ierr = PetscInfo2(0,"Linear solver has converged. Residual norm %G is less than absolute tolerance %G\n",rnorm,mg->abstol);CHKERRQ(ierr); 34 } else { 35 ierr = PetscInfo2(0,"Linear solver has converged. Residual norm %G is less than relative tolerance times initial residual norm %G\n",rnorm,mg->ttol);CHKERRQ(ierr); 36 } 37 PetscFunctionReturn(0); 38 } 39 } 40 41 mgc = *(mglevels - 1); 42 ierr = MatRestrict(mg->restrct,mg->r,mgc->b);CHKERRQ(ierr); 43 ierr = VecSet(mgc->x,0.0);CHKERRQ(ierr); 44 while (cycles--) { 45 ierr = PCMGMCycle_Private(mglevels-1,converged);CHKERRQ(ierr); 46 } 47 ierr = MatInterpolateAdd(mg->interpolate,mgc->x,mg->x,mg->x);CHKERRQ(ierr); 48 if (mg->eventsolve) {ierr = PetscLogEventBegin(mg->eventsolve,0,0,0,0);CHKERRQ(ierr);} 49 ierr = KSPSolve(mg->smoothu,mg->b,mg->x);CHKERRQ(ierr); 50 if (mg->eventsolve) {ierr = PetscLogEventEnd(mg->eventsolve,0,0,0,0);CHKERRQ(ierr);} 51 } 52 PetscFunctionReturn(0); 53 } 54 55 /* 56 PCMGCreate_Private - Creates a PC_MG structure for use with the 57 multigrid code. Level 0 is the coarsest. (But the 58 finest level is stored first in the array). 59 60 */ 61 #undef __FUNCT__ 62 #define __FUNCT__ "PCMGCreate_Private" 63 static PetscErrorCode PCMGCreate_Private(MPI_Comm comm,PetscInt levels,PC pc,MPI_Comm *comms,PC_MG ***result) 64 { 65 PC_MG **mg; 66 PetscErrorCode ierr; 67 PetscInt i; 68 PetscMPIInt size; 69 const char *prefix; 70 PC ipc; 71 72 PetscFunctionBegin; 73 ierr = PetscMalloc(levels*sizeof(PC_MG*),&mg);CHKERRQ(ierr); 74 ierr = PetscLogObjectMemory(pc,levels*(sizeof(PC_MG*)+sizeof(PC_MG)));CHKERRQ(ierr); 75 76 ierr = PCGetOptionsPrefix(pc,&prefix);CHKERRQ(ierr); 77 78 for (i=0; i<levels; i++) { 79 ierr = PetscNew(PC_MG,&mg[i]);CHKERRQ(ierr); 80 mg[i]->level = i; 81 mg[i]->levels = levels; 82 mg[i]->cycles = 1; 83 mg[i]->galerkin = PETSC_FALSE; 84 mg[i]->galerkinused = PETSC_FALSE; 85 mg[i]->default_smoothu = 1; 86 mg[i]->default_smoothd = 1; 87 88 if (comms) comm = comms[i]; 89 ierr = KSPCreate(comm,&mg[i]->smoothd);CHKERRQ(ierr); 90 ierr = KSPSetTolerances(mg[i]->smoothd,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT, mg[i]->default_smoothd);CHKERRQ(ierr); 91 ierr = KSPSetOptionsPrefix(mg[i]->smoothd,prefix);CHKERRQ(ierr); 92 93 /* do special stuff for coarse grid */ 94 if (!i && levels > 1) { 95 ierr = KSPAppendOptionsPrefix(mg[0]->smoothd,"mg_coarse_");CHKERRQ(ierr); 96 97 /* coarse solve is (redundant) LU by default */ 98 ierr = KSPSetType(mg[0]->smoothd,KSPPREONLY);CHKERRQ(ierr); 99 ierr = KSPGetPC(mg[0]->smoothd,&ipc);CHKERRQ(ierr); 100 ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 101 if (size > 1) { 102 ierr = PCSetType(ipc,PCREDUNDANT);CHKERRQ(ierr); 103 ierr = PCRedundantGetPC(ipc,&ipc);CHKERRQ(ierr); 104 } 105 ierr = PCSetType(ipc,PCLU);CHKERRQ(ierr); 106 107 } else { 108 char tprefix[128]; 109 sprintf(tprefix,"mg_levels_%d_",(int)i); 110 ierr = KSPAppendOptionsPrefix(mg[i]->smoothd,tprefix);CHKERRQ(ierr); 111 } 112 ierr = PetscLogObjectParent(pc,mg[i]->smoothd);CHKERRQ(ierr); 113 mg[i]->smoothu = mg[i]->smoothd; 114 mg[i]->rtol = 0.0; 115 mg[i]->abstol = 0.0; 116 mg[i]->dtol = 0.0; 117 mg[i]->ttol = 0.0; 118 mg[i]->eventsetup = 0; 119 mg[i]->eventsolve = 0; 120 } 121 *result = mg; 122 PetscFunctionReturn(0); 123 } 124 125 #undef __FUNCT__ 126 #define __FUNCT__ "PCDestroy_MG" 127 static PetscErrorCode PCDestroy_MG(PC pc) 128 { 129 PC_MG **mg = (PC_MG**)pc->data; 130 PetscErrorCode ierr; 131 PetscInt i,n = mg[0]->levels; 132 133 PetscFunctionBegin; 134 for (i=0; i<n-1; i++) { 135 if (mg[i+1]->r) {ierr = VecDestroy(mg[i+1]->r);CHKERRQ(ierr);} 136 if (mg[i]->b) {ierr = VecDestroy(mg[i]->b);CHKERRQ(ierr);} 137 if (mg[i]->x) {ierr = VecDestroy(mg[i]->x);CHKERRQ(ierr);} 138 if (mg[i+1]->restrct) {ierr = MatDestroy(mg[i+1]->restrct);CHKERRQ(ierr);} 139 if (mg[i+1]->interpolate) {ierr = MatDestroy(mg[i+1]->interpolate);CHKERRQ(ierr);} 140 } 141 142 for (i=0; i<n; i++) { 143 if (mg[i]->smoothd != mg[i]->smoothu) { 144 ierr = KSPDestroy(mg[i]->smoothd);CHKERRQ(ierr); 145 } 146 ierr = KSPDestroy(mg[i]->smoothu);CHKERRQ(ierr); 147 ierr = PetscFree(mg[i]);CHKERRQ(ierr); 148 } 149 ierr = PetscFree(mg);CHKERRQ(ierr); 150 PetscFunctionReturn(0); 151 } 152 153 154 155 EXTERN PetscErrorCode PCMGACycle_Private(PC_MG**); 156 EXTERN PetscErrorCode PCMGFCycle_Private(PC_MG**); 157 EXTERN PetscErrorCode PCMGKCycle_Private(PC_MG**); 158 159 /* 160 PCApply_MG - Runs either an additive, multiplicative, Kaskadic 161 or full cycle of multigrid. 162 163 Note: 164 A simple wrapper which calls PCMGMCycle(),PCMGACycle(), or PCMGFCycle(). 165 */ 166 #undef __FUNCT__ 167 #define __FUNCT__ "PCApply_MG" 168 static PetscErrorCode PCApply_MG(PC pc,Vec b,Vec x) 169 { 170 PC_MG **mg = (PC_MG**)pc->data; 171 PetscErrorCode ierr; 172 PetscInt levels = mg[0]->levels; 173 174 PetscFunctionBegin; 175 mg[levels-1]->b = b; 176 mg[levels-1]->x = x; 177 if (!mg[levels-1]->r && mg[0]->am != PC_MG_ADDITIVE && levels > 1) { 178 Vec tvec; 179 ierr = VecDuplicate(mg[levels-1]->b,&tvec);CHKERRQ(ierr); 180 ierr = PCMGSetR(pc,levels-1,tvec);CHKERRQ(ierr); 181 ierr = VecDestroy(tvec);CHKERRQ(ierr); 182 } 183 if (mg[0]->am == PC_MG_MULTIPLICATIVE) { 184 ierr = VecSet(x,0.0);CHKERRQ(ierr); 185 ierr = PCMGMCycle_Private(mg+levels-1,PETSC_NULL);CHKERRQ(ierr); 186 } 187 else if (mg[0]->am == PC_MG_ADDITIVE) { 188 ierr = PCMGACycle_Private(mg);CHKERRQ(ierr); 189 } 190 else if (mg[0]->am == PC_MG_KASKADE) { 191 ierr = PCMGKCycle_Private(mg);CHKERRQ(ierr); 192 } 193 else { 194 ierr = PCMGFCycle_Private(mg);CHKERRQ(ierr); 195 } 196 PetscFunctionReturn(0); 197 } 198 199 #undef __FUNCT__ 200 #define __FUNCT__ "PCApplyRichardson_MG" 201 static PetscErrorCode PCApplyRichardson_MG(PC pc,Vec b,Vec x,Vec w,PetscReal rtol,PetscReal abstol, PetscReal dtol,PetscInt its) 202 { 203 PC_MG **mg = (PC_MG**)pc->data; 204 PetscErrorCode ierr; 205 PetscInt levels = mg[0]->levels; 206 PetscTruth converged = PETSC_FALSE; 207 208 PetscFunctionBegin; 209 mg[levels-1]->b = b; 210 mg[levels-1]->x = x; 211 212 mg[levels-1]->rtol = rtol; 213 mg[levels-1]->abstol = abstol; 214 mg[levels-1]->dtol = dtol; 215 if (rtol) { 216 /* compute initial residual norm for relative convergence test */ 217 PetscReal rnorm; 218 ierr = (*mg[levels-1]->residual)(mg[levels-1]->A,b,x,w);CHKERRQ(ierr); 219 ierr = VecNorm(w,NORM_2,&rnorm);CHKERRQ(ierr); 220 mg[levels-1]->ttol = PetscMax(rtol*rnorm,abstol); 221 } else if (abstol) { 222 mg[levels-1]->ttol = abstol; 223 } else { 224 mg[levels-1]->ttol = 0.0; 225 } 226 227 while (its-- && !converged) { 228 ierr = PCMGMCycle_Private(mg+levels-1,&converged);CHKERRQ(ierr); 229 } 230 PetscFunctionReturn(0); 231 } 232 233 #undef __FUNCT__ 234 #define __FUNCT__ "PCSetFromOptions_MG" 235 PetscErrorCode PCSetFromOptions_MG(PC pc) 236 { 237 PetscErrorCode ierr; 238 PetscInt m,levels = 1; 239 PetscTruth flg; 240 PC_MG **mg = (PC_MG**)pc->data; 241 PCMGType mgtype; 242 243 PetscFunctionBegin; 244 245 ierr = PetscOptionsHead("Multigrid options");CHKERRQ(ierr); 246 if (!pc->data) { 247 ierr = PetscOptionsInt("-pc_mg_levels","Number of Levels","PCMGSetLevels",levels,&levels,&flg);CHKERRQ(ierr); 248 ierr = PCMGSetLevels(pc,levels,PETSC_NULL);CHKERRQ(ierr); 249 mg = (PC_MG**)pc->data; 250 } 251 mgtype = mg[0]->am; 252 ierr = PetscOptionsInt("-pc_mg_cycles","1 for V cycle, 2 for W-cycle","PCMGSetCycles",1,&m,&flg);CHKERRQ(ierr); 253 if (flg) { 254 ierr = PCMGSetCycles(pc,m);CHKERRQ(ierr); 255 } 256 ierr = PetscOptionsName("-pc_mg_galerkin","Use Galerkin process to compute coarser operators","PCMGSetGalerkin",&flg);CHKERRQ(ierr); 257 if (flg) { 258 ierr = PCMGSetGalerkin(pc);CHKERRQ(ierr); 259 } 260 ierr = PetscOptionsInt("-pc_mg_smoothup","Number of post-smoothing steps","PCMGSetNumberSmoothUp",1,&m,&flg);CHKERRQ(ierr); 261 if (flg) { 262 ierr = PCMGSetNumberSmoothUp(pc,m);CHKERRQ(ierr); 263 } 264 ierr = PetscOptionsInt("-pc_mg_smoothdown","Number of pre-smoothing steps","PCMGSetNumberSmoothDown",1,&m,&flg);CHKERRQ(ierr); 265 if (flg) { 266 ierr = PCMGSetNumberSmoothDown(pc,m);CHKERRQ(ierr); 267 } 268 ierr = PetscOptionsEnum("-pc_mg_type","Multigrid type","PCMGSetType",PCMGTypes,(PetscEnum)mgtype,(PetscEnum*)&mgtype,&flg);CHKERRQ(ierr); 269 if (flg) {ierr = PCMGSetType(pc,mgtype);CHKERRQ(ierr);} 270 ierr = PetscOptionsName("-pc_mg_log","Log times for each multigrid level","None",&flg);CHKERRQ(ierr); 271 if (flg) { 272 PetscInt i; 273 char eventname[128]; 274 if (!mg) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling"); 275 levels = mg[0]->levels; 276 for (i=0; i<levels; i++) { 277 sprintf(eventname,"MSetup Level %d",(int)i); 278 ierr = PetscLogEventRegister(&mg[i]->eventsetup,eventname,pc->cookie);CHKERRQ(ierr); 279 sprintf(eventname,"MGSolve Level %d to 0",(int)i); 280 ierr = PetscLogEventRegister(&mg[i]->eventsolve,eventname,pc->cookie);CHKERRQ(ierr); 281 } 282 } 283 ierr = PetscOptionsTail();CHKERRQ(ierr); 284 PetscFunctionReturn(0); 285 } 286 287 const char *PCMGTypes[] = {"MULTIPLICATIVE","ADDITIVE","FULL","KASKADE","PCMGType","PC_MG",0}; 288 289 #undef __FUNCT__ 290 #define __FUNCT__ "PCView_MG" 291 static PetscErrorCode PCView_MG(PC pc,PetscViewer viewer) 292 { 293 PC_MG **mg = (PC_MG**)pc->data; 294 PetscErrorCode ierr; 295 PetscInt levels = mg[0]->levels,i; 296 PetscTruth iascii; 297 298 PetscFunctionBegin; 299 ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr); 300 if (iascii) { 301 ierr = PetscViewerASCIIPrintf(viewer," MG: type is %s, levels=%D cycles=%D, pre-smooths=%D, post-smooths=%D\n", 302 PCMGTypes[mg[0]->am],levels,mg[0]->cycles,mg[0]->default_smoothd,mg[0]->default_smoothu);CHKERRQ(ierr); 303 if (mg[0]->galerkin) { 304 ierr = PetscViewerASCIIPrintf(viewer," Using Galerkin computed coarse grid matrices\n");CHKERRQ(ierr); 305 } 306 for (i=0; i<levels; i++) { 307 if (!i) { 308 ierr = PetscViewerASCIIPrintf(viewer,"Coarse gride solver -- level %D -------------------------------\n",i);CHKERRQ(ierr); 309 } else { 310 ierr = PetscViewerASCIIPrintf(viewer,"Down solver (pre-smoother) on level %D -------------------------------\n",i);CHKERRQ(ierr); 311 } 312 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 313 ierr = KSPView(mg[i]->smoothd,viewer);CHKERRQ(ierr); 314 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 315 if (i && mg[i]->smoothd == mg[i]->smoothu) { 316 ierr = PetscViewerASCIIPrintf(viewer,"Up solver (post-smoother) same as down solver (pre-smoother)\n");CHKERRQ(ierr); 317 } else if (i){ 318 ierr = PetscViewerASCIIPrintf(viewer,"Up solver (post-smoother) on level %D -------------------------------\n",i);CHKERRQ(ierr); 319 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 320 ierr = KSPView(mg[i]->smoothu,viewer);CHKERRQ(ierr); 321 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 322 } 323 } 324 } else { 325 SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported for PCMG",((PetscObject)viewer)->type_name); 326 } 327 PetscFunctionReturn(0); 328 } 329 330 /* 331 Calls setup for the KSP on each level 332 */ 333 #undef __FUNCT__ 334 #define __FUNCT__ "PCSetUp_MG" 335 static PetscErrorCode PCSetUp_MG(PC pc) 336 { 337 PC_MG **mg = (PC_MG**)pc->data; 338 PetscErrorCode ierr; 339 PetscInt i,n = mg[0]->levels; 340 PC cpc; 341 PetscTruth preonly,lu,redundant,cholesky,monitor = PETSC_FALSE,dump,opsset; 342 PetscViewer ascii; 343 MPI_Comm comm; 344 Mat dA,dB; 345 MatStructure uflag; 346 Vec tvec; 347 348 PetscFunctionBegin; 349 350 /* If user did not provide fine grid operators, use those from PC */ 351 /* BUG BUG BUG This will work ONLY the first time called: hence if the user changes 352 the PC matrices between solves PCMG will continue to use first set provided */ 353 ierr = KSPGetOperatorsSet(mg[n-1]->smoothd,PETSC_NULL,&opsset);CHKERRQ(ierr); 354 if (!opsset) { 355 ierr = PetscInfo(pc,"Using outer operators to define finest grid operator \n because PCMGGetSmoother(pc,nlevels-1,&ksp);KSPSetOperators(ksp,...); was not called.\n");CHKERRQ(ierr); 356 ierr = KSPSetOperators(mg[n-1]->smoothd,pc->mat,pc->pmat,uflag);CHKERRQ(ierr); 357 } 358 359 if (mg[0]->galerkin) { 360 Mat B; 361 mg[0]->galerkinused = PETSC_TRUE; 362 /* currently only handle case where mat and pmat are the same on coarser levels */ 363 ierr = KSPGetOperators(mg[n-1]->smoothd,&dA,&dB,&uflag);CHKERRQ(ierr); 364 if (!pc->setupcalled) { 365 for (i=n-2; i>-1; i--) { 366 ierr = MatPtAP(dB,mg[i+1]->interpolate,MAT_INITIAL_MATRIX,1.0,&B);CHKERRQ(ierr); 367 ierr = KSPSetOperators(mg[i]->smoothd,B,B,uflag);CHKERRQ(ierr); 368 if (i != n-2) {ierr = PetscObjectDereference((PetscObject)dB);CHKERRQ(ierr);} 369 dB = B; 370 } 371 ierr = PetscObjectDereference((PetscObject)dB);CHKERRQ(ierr); 372 } else { 373 for (i=n-2; i>-1; i--) { 374 ierr = KSPGetOperators(mg[i]->smoothd,PETSC_NULL,&B,PETSC_NULL);CHKERRQ(ierr); 375 ierr = MatPtAP(dB,mg[i+1]->interpolate,MAT_REUSE_MATRIX,1.0,&B);CHKERRQ(ierr); 376 ierr = KSPSetOperators(mg[i]->smoothd,B,B,uflag);CHKERRQ(ierr); 377 dB = B; 378 } 379 } 380 } 381 382 if (!pc->setupcalled) { 383 ierr = PetscOptionsHasName(0,"-pc_mg_monitor",&monitor);CHKERRQ(ierr); 384 385 for (i=0; i<n; i++) { 386 if (monitor) { 387 ierr = PetscObjectGetComm((PetscObject)mg[i]->smoothd,&comm);CHKERRQ(ierr); 388 ierr = PetscViewerASCIIOpen(comm,"stdout",&ascii);CHKERRQ(ierr); 389 ierr = PetscViewerASCIISetTab(ascii,n-i);CHKERRQ(ierr); 390 ierr = KSPSetMonitor(mg[i]->smoothd,KSPDefaultMonitor,ascii,(PetscErrorCode(*)(void*))PetscViewerDestroy);CHKERRQ(ierr); 391 } 392 ierr = KSPSetFromOptions(mg[i]->smoothd);CHKERRQ(ierr); 393 } 394 for (i=1; i<n; i++) { 395 if (mg[i]->smoothu && (mg[i]->smoothu != mg[i]->smoothd)) { 396 if (monitor) { 397 ierr = PetscObjectGetComm((PetscObject)mg[i]->smoothu,&comm);CHKERRQ(ierr); 398 ierr = PetscViewerASCIIOpen(comm,"stdout",&ascii);CHKERRQ(ierr); 399 ierr = PetscViewerASCIISetTab(ascii,n-i);CHKERRQ(ierr); 400 ierr = KSPSetMonitor(mg[i]->smoothu,KSPDefaultMonitor,ascii,(PetscErrorCode(*)(void*))PetscViewerDestroy);CHKERRQ(ierr); 401 } 402 ierr = KSPSetFromOptions(mg[i]->smoothu);CHKERRQ(ierr); 403 } 404 } 405 for (i=1; i<n; i++) { 406 if (!mg[i]->residual) { 407 Mat mat; 408 ierr = KSPGetOperators(mg[i]->smoothd,PETSC_NULL,&mat,PETSC_NULL);CHKERRQ(ierr); 409 ierr = PCMGSetResidual(pc,i,PCMGDefaultResidual,mat);CHKERRQ(ierr); 410 } 411 if (mg[i]->restrct && !mg[i]->interpolate) { 412 ierr = PCMGSetInterpolate(pc,i,mg[i]->restrct);CHKERRQ(ierr); 413 } 414 if (!mg[i]->restrct && mg[i]->interpolate) { 415 ierr = PCMGSetRestriction(pc,i,mg[i]->interpolate);CHKERRQ(ierr); 416 } 417 #if defined(PETSC_USE_DEBUG) 418 if (!mg[i]->restrct || !mg[i]->interpolate) { 419 SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Need to set restriction or interpolation on level %d",(int)i); 420 } 421 #endif 422 } 423 for (i=0; i<n-1; i++) { 424 if (!mg[i]->b) { 425 Vec *vec; 426 ierr = KSPGetVecs(mg[i]->smoothd,1,&vec,0,PETSC_NULL);CHKERRQ(ierr); 427 ierr = PCMGSetRhs(pc,i,*vec);CHKERRQ(ierr); 428 ierr = PetscFree(vec);CHKERRQ(ierr); 429 } 430 if (!mg[i]->r && i) { 431 ierr = VecDuplicate(mg[i]->b,&tvec);CHKERRQ(ierr); 432 ierr = PCMGSetR(pc,i,tvec);CHKERRQ(ierr); 433 ierr = VecDestroy(tvec);CHKERRQ(ierr); 434 } 435 if (!mg[i]->x) { 436 ierr = VecDuplicate(mg[i]->b,&tvec);CHKERRQ(ierr); 437 ierr = PCMGSetX(pc,i,tvec);CHKERRQ(ierr); 438 ierr = VecDestroy(tvec);CHKERRQ(ierr); 439 } 440 } 441 } 442 443 444 for (i=1; i<n; i++) { 445 if (mg[i]->smoothu == mg[i]->smoothd) { 446 /* if doing only down then initial guess is zero */ 447 ierr = KSPSetInitialGuessNonzero(mg[i]->smoothd,PETSC_TRUE);CHKERRQ(ierr); 448 } 449 if (mg[i]->eventsetup) {ierr = PetscLogEventBegin(mg[i]->eventsetup,0,0,0,0);CHKERRQ(ierr);} 450 ierr = KSPSetUp(mg[i]->smoothd);CHKERRQ(ierr); 451 if (mg[i]->eventsetup) {ierr = PetscLogEventEnd(mg[i]->eventsetup,0,0,0,0);CHKERRQ(ierr);} 452 } 453 for (i=1; i<n; i++) { 454 if (mg[i]->smoothu && mg[i]->smoothu != mg[i]->smoothd) { 455 Mat downmat,downpmat; 456 MatStructure matflag; 457 PetscTruth opsset; 458 459 /* check if operators have been set for up, if not use down operators to set them */ 460 ierr = KSPGetOperatorsSet(mg[i]->smoothu,&opsset,PETSC_NULL);CHKERRQ(ierr); 461 if (!opsset) { 462 ierr = KSPGetOperators(mg[i]->smoothd,&downmat,&downpmat,&matflag);CHKERRQ(ierr); 463 ierr = KSPSetOperators(mg[i]->smoothu,downmat,downpmat,matflag);CHKERRQ(ierr); 464 } 465 466 ierr = KSPSetInitialGuessNonzero(mg[i]->smoothu,PETSC_TRUE);CHKERRQ(ierr); 467 if (mg[i]->eventsetup) {ierr = PetscLogEventBegin(mg[i]->eventsetup,0,0,0,0);CHKERRQ(ierr);} 468 ierr = KSPSetUp(mg[i]->smoothu);CHKERRQ(ierr); 469 if (mg[i]->eventsetup) {ierr = PetscLogEventEnd(mg[i]->eventsetup,0,0,0,0);CHKERRQ(ierr);} 470 } 471 } 472 473 /* 474 If coarse solver is not direct method then DO NOT USE preonly 475 */ 476 ierr = PetscTypeCompare((PetscObject)mg[0]->smoothd,KSPPREONLY,&preonly);CHKERRQ(ierr); 477 if (preonly) { 478 ierr = KSPGetPC(mg[0]->smoothd,&cpc);CHKERRQ(ierr); 479 ierr = PetscTypeCompare((PetscObject)cpc,PCLU,&lu);CHKERRQ(ierr); 480 ierr = PetscTypeCompare((PetscObject)cpc,PCREDUNDANT,&redundant);CHKERRQ(ierr); 481 ierr = PetscTypeCompare((PetscObject)cpc,PCCHOLESKY,&cholesky);CHKERRQ(ierr); 482 if (!lu && !redundant && !cholesky) { 483 ierr = KSPSetType(mg[0]->smoothd,KSPGMRES);CHKERRQ(ierr); 484 } 485 } 486 487 if (!pc->setupcalled) { 488 if (monitor) { 489 ierr = PetscObjectGetComm((PetscObject)mg[0]->smoothd,&comm);CHKERRQ(ierr); 490 ierr = PetscViewerASCIIOpen(comm,"stdout",&ascii);CHKERRQ(ierr); 491 ierr = PetscViewerASCIISetTab(ascii,n);CHKERRQ(ierr); 492 ierr = KSPSetMonitor(mg[0]->smoothd,KSPDefaultMonitor,ascii,(PetscErrorCode(*)(void*))PetscViewerDestroy);CHKERRQ(ierr); 493 } 494 ierr = KSPSetFromOptions(mg[0]->smoothd);CHKERRQ(ierr); 495 } 496 497 if (mg[0]->eventsetup) {ierr = PetscLogEventBegin(mg[0]->eventsetup,0,0,0,0);CHKERRQ(ierr);} 498 ierr = KSPSetUp(mg[0]->smoothd);CHKERRQ(ierr); 499 if (mg[0]->eventsetup) {ierr = PetscLogEventEnd(mg[0]->eventsetup,0,0,0,0);CHKERRQ(ierr);} 500 501 #if defined(PETSC_USE_SOCKET_VIEWER) 502 /* 503 Dump the interpolation/restriction matrices to matlab plus the 504 Jacobian/stiffness on each level. This allows Matlab users to 505 easily check if the Galerkin condition A_c = R A_f R^T is satisfied */ 506 ierr = PetscOptionsHasName(pc->prefix,"-pc_mg_dump_matlab",&dump);CHKERRQ(ierr); 507 if (dump) { 508 for (i=1; i<n; i++) { 509 ierr = MatView(mg[i]->restrct,PETSC_VIEWER_SOCKET_(pc->comm));CHKERRQ(ierr); 510 } 511 for (i=0; i<n; i++) { 512 ierr = KSPGetPC(mg[i]->smoothd,&pc);CHKERRQ(ierr); 513 ierr = MatView(pc->mat,PETSC_VIEWER_SOCKET_(pc->comm));CHKERRQ(ierr); 514 } 515 } 516 #endif 517 518 ierr = PetscOptionsHasName(pc->prefix,"-pc_mg_dump_binary",&dump);CHKERRQ(ierr); 519 if (dump) { 520 for (i=1; i<n; i++) { 521 ierr = MatView(mg[i]->restrct,PETSC_VIEWER_BINARY_(pc->comm));CHKERRQ(ierr); 522 } 523 for (i=0; i<n; i++) { 524 ierr = KSPGetPC(mg[i]->smoothd,&pc);CHKERRQ(ierr); 525 ierr = MatView(pc->mat,PETSC_VIEWER_BINARY_(pc->comm));CHKERRQ(ierr); 526 } 527 } 528 PetscFunctionReturn(0); 529 } 530 531 /* -------------------------------------------------------------------------------------*/ 532 533 #undef __FUNCT__ 534 #define __FUNCT__ "PCMGSetLevels" 535 /*@C 536 PCMGSetLevels - Sets the number of levels to use with MG. 537 Must be called before any other MG routine. 538 539 Collective on PC 540 541 Input Parameters: 542 + pc - the preconditioner context 543 . levels - the number of levels 544 - comms - optional communicators for each level; this is to allow solving the coarser problems 545 on smaller sets of processors. Use PETSC_NULL_OBJECT for default in Fortran 546 547 Level: intermediate 548 549 Notes: 550 If the number of levels is one then the multigrid uses the -mg_levels prefix 551 for setting the level options rather than the -mg_coarse prefix. 552 553 .keywords: MG, set, levels, multigrid 554 555 .seealso: PCMGSetType(), PCMGGetLevels() 556 @*/ 557 PetscErrorCode PETSCKSP_DLLEXPORT PCMGSetLevels(PC pc,PetscInt levels,MPI_Comm *comms) 558 { 559 PetscErrorCode ierr; 560 PC_MG **mg=0; 561 562 PetscFunctionBegin; 563 PetscValidHeaderSpecific(pc,PC_COOKIE,1); 564 565 if (pc->data) { 566 SETERRQ(PETSC_ERR_ORDER,"Number levels already set for MG\n\ 567 make sure that you call PCMGSetLevels() before KSPSetFromOptions()"); 568 } 569 ierr = PCMGCreate_Private(pc->comm,levels,pc,comms,&mg);CHKERRQ(ierr); 570 mg[0]->am = PC_MG_MULTIPLICATIVE; 571 pc->data = (void*)mg; 572 pc->ops->applyrichardson = PCApplyRichardson_MG; 573 PetscFunctionReturn(0); 574 } 575 576 #undef __FUNCT__ 577 #define __FUNCT__ "PCMGGetLevels" 578 /*@ 579 PCMGGetLevels - Gets the number of levels to use with MG. 580 581 Not Collective 582 583 Input Parameter: 584 . pc - the preconditioner context 585 586 Output parameter: 587 . levels - the number of levels 588 589 Level: advanced 590 591 .keywords: MG, get, levels, multigrid 592 593 .seealso: PCMGSetLevels() 594 @*/ 595 PetscErrorCode PETSCKSP_DLLEXPORT PCMGGetLevels(PC pc,PetscInt *levels) 596 { 597 PC_MG **mg; 598 599 PetscFunctionBegin; 600 PetscValidHeaderSpecific(pc,PC_COOKIE,1); 601 PetscValidIntPointer(levels,2); 602 603 mg = (PC_MG**)pc->data; 604 *levels = mg[0]->levels; 605 PetscFunctionReturn(0); 606 } 607 608 #undef __FUNCT__ 609 #define __FUNCT__ "PCMGSetType" 610 /*@ 611 PCMGSetType - Determines the form of multigrid to use: 612 multiplicative, additive, full, or the Kaskade algorithm. 613 614 Collective on PC 615 616 Input Parameters: 617 + pc - the preconditioner context 618 - form - multigrid form, one of PC_MG_MULTIPLICATIVE, PC_MG_ADDITIVE, 619 PC_MG_FULL, PC_MG_KASKADE 620 621 Options Database Key: 622 . -pc_mg_type <form> - Sets <form>, one of multiplicative, 623 additive, full, kaskade 624 625 Level: advanced 626 627 .keywords: MG, set, method, multiplicative, additive, full, Kaskade, multigrid 628 629 .seealso: PCMGSetLevels() 630 @*/ 631 PetscErrorCode PETSCKSP_DLLEXPORT PCMGSetType(PC pc,PCMGType form) 632 { 633 PC_MG **mg; 634 635 PetscFunctionBegin; 636 PetscValidHeaderSpecific(pc,PC_COOKIE,1); 637 mg = (PC_MG**)pc->data; 638 639 if (!mg) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling"); 640 mg[0]->am = form; 641 if (form == PC_MG_MULTIPLICATIVE) pc->ops->applyrichardson = PCApplyRichardson_MG; 642 else pc->ops->applyrichardson = 0; 643 PetscFunctionReturn(0); 644 } 645 646 #undef __FUNCT__ 647 #define __FUNCT__ "PCMGSetCycles" 648 /*@ 649 PCMGSetCycles - Sets the type cycles to use. Use PCMGSetCyclesOnLevel() for more 650 complicated cycling. 651 652 Collective on PC 653 654 Input Parameters: 655 + pc - the multigrid context 656 - n - the number of cycles 657 658 Options Database Key: 659 $ -pc_mg_cycles n - 1 denotes a V-cycle; 2 denotes a W-cycle. 660 661 Level: advanced 662 663 .keywords: MG, set, cycles, V-cycle, W-cycle, multigrid 664 665 .seealso: PCMGSetCyclesOnLevel() 666 @*/ 667 PetscErrorCode PETSCKSP_DLLEXPORT PCMGSetCycles(PC pc,PetscInt n) 668 { 669 PC_MG **mg; 670 PetscInt i,levels; 671 672 PetscFunctionBegin; 673 PetscValidHeaderSpecific(pc,PC_COOKIE,1); 674 mg = (PC_MG**)pc->data; 675 if (!mg) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling"); 676 levels = mg[0]->levels; 677 678 for (i=0; i<levels; i++) { 679 mg[i]->cycles = n; 680 } 681 PetscFunctionReturn(0); 682 } 683 684 #undef __FUNCT__ 685 #define __FUNCT__ "PCMGSetGalerkin" 686 /*@ 687 PCMGSetGalerkin - Causes the coarser grid matrices to be computed from the 688 finest grid via the Galerkin process: A_i-1 = r_i * A_i * r_i^t 689 690 Collective on PC 691 692 Input Parameters: 693 . pc - the multigrid context 694 695 Options Database Key: 696 $ -pc_mg_galerkin 697 698 Level: intermediate 699 700 .keywords: MG, set, Galerkin 701 702 .seealso: PCMGGetGalerkin() 703 704 @*/ 705 PetscErrorCode PETSCKSP_DLLEXPORT PCMGSetGalerkin(PC pc) 706 { 707 PC_MG **mg; 708 PetscInt i,levels; 709 710 PetscFunctionBegin; 711 PetscValidHeaderSpecific(pc,PC_COOKIE,1); 712 mg = (PC_MG**)pc->data; 713 if (!mg) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling"); 714 levels = mg[0]->levels; 715 716 for (i=0; i<levels; i++) { 717 mg[i]->galerkin = PETSC_TRUE; 718 } 719 PetscFunctionReturn(0); 720 } 721 722 #undef __FUNCT__ 723 #define __FUNCT__ "PCMGGetGalerkin" 724 /*@ 725 PCMGGetGalerkin - Checks if Galerkin multigrid is being used, i.e. 726 A_i-1 = r_i * A_i * r_i^t 727 728 Not Collective 729 730 Input Parameter: 731 . pc - the multigrid context 732 733 Output Parameter: 734 . gelerkin - PETSC_TRUE or PETSC_FALSE 735 736 Options Database Key: 737 $ -pc_mg_galerkin 738 739 Level: intermediate 740 741 .keywords: MG, set, Galerkin 742 743 .seealso: PCMGSetGalerkin() 744 745 @*/ 746 PetscErrorCode PETSCKSP_DLLEXPORT PCMGGetGalerkin(PC pc,PetscTruth *galerkin) 747 { 748 PC_MG **mg; 749 750 PetscFunctionBegin; 751 PetscValidHeaderSpecific(pc,PC_COOKIE,1); 752 mg = (PC_MG**)pc->data; 753 if (!mg) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling"); 754 *galerkin = mg[0]->galerkin; 755 PetscFunctionReturn(0); 756 } 757 758 #undef __FUNCT__ 759 #define __FUNCT__ "PCMGSetNumberSmoothDown" 760 /*@ 761 PCMGSetNumberSmoothDown - Sets the number of pre-smoothing steps to 762 use on all levels. Use PCMGGetSmootherDown() to set different 763 pre-smoothing steps on different levels. 764 765 Collective on PC 766 767 Input Parameters: 768 + mg - the multigrid context 769 - n - the number of smoothing steps 770 771 Options Database Key: 772 . -pc_mg_smoothdown <n> - Sets number of pre-smoothing steps 773 774 Level: advanced 775 776 .keywords: MG, smooth, down, pre-smoothing, steps, multigrid 777 778 .seealso: PCMGSetNumberSmoothUp() 779 @*/ 780 PetscErrorCode PETSCKSP_DLLEXPORT PCMGSetNumberSmoothDown(PC pc,PetscInt n) 781 { 782 PC_MG **mg; 783 PetscErrorCode ierr; 784 PetscInt i,levels; 785 786 PetscFunctionBegin; 787 PetscValidHeaderSpecific(pc,PC_COOKIE,1); 788 mg = (PC_MG**)pc->data; 789 if (!mg) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling"); 790 levels = mg[0]->levels; 791 792 for (i=1; i<levels; i++) { 793 /* make sure smoother up and down are different */ 794 ierr = PCMGGetSmootherUp(pc,i,PETSC_NULL);CHKERRQ(ierr); 795 ierr = KSPSetTolerances(mg[i]->smoothd,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,n);CHKERRQ(ierr); 796 mg[i]->default_smoothd = n; 797 } 798 PetscFunctionReturn(0); 799 } 800 801 #undef __FUNCT__ 802 #define __FUNCT__ "PCMGSetNumberSmoothUp" 803 /*@ 804 PCMGSetNumberSmoothUp - Sets the number of post-smoothing steps to use 805 on all levels. Use PCMGGetSmootherUp() to set different numbers of 806 post-smoothing steps on different levels. 807 808 Collective on PC 809 810 Input Parameters: 811 + mg - the multigrid context 812 - n - the number of smoothing steps 813 814 Options Database Key: 815 . -pc_mg_smoothup <n> - Sets number of post-smoothing steps 816 817 Level: advanced 818 819 Note: this does not set a value on the coarsest grid, since we assume that 820 there is no separate smooth up on the coarsest grid. 821 822 .keywords: MG, smooth, up, post-smoothing, steps, multigrid 823 824 .seealso: PCMGSetNumberSmoothDown() 825 @*/ 826 PetscErrorCode PETSCKSP_DLLEXPORT PCMGSetNumberSmoothUp(PC pc,PetscInt n) 827 { 828 PC_MG **mg; 829 PetscErrorCode ierr; 830 PetscInt i,levels; 831 832 PetscFunctionBegin; 833 PetscValidHeaderSpecific(pc,PC_COOKIE,1); 834 mg = (PC_MG**)pc->data; 835 if (!mg) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling"); 836 levels = mg[0]->levels; 837 838 for (i=1; i<levels; i++) { 839 /* make sure smoother up and down are different */ 840 ierr = PCMGGetSmootherUp(pc,i,PETSC_NULL);CHKERRQ(ierr); 841 ierr = KSPSetTolerances(mg[i]->smoothu,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,n);CHKERRQ(ierr); 842 mg[i]->default_smoothu = n; 843 } 844 PetscFunctionReturn(0); 845 } 846 847 /* ----------------------------------------------------------------------------------------*/ 848 849 /*MC 850 PCMG - Use geometric multigrid preconditioning. This preconditioner requires you provide additional 851 information about the coarser grid matrices and restriction/interpolation operators. 852 853 Options Database Keys: 854 + -pc_mg_levels <nlevels> - number of levels including finest 855 . -pc_mg_cycles 1 or 2 - for V or W-cycle 856 . -pc_mg_smoothup <n> - number of smoothing steps after interpolation 857 . -pc_mg_smoothdown <n> - number of smoothing steps before applying restriction operator 858 . -pc_mg_type <additive,multiplicative,full,cascade> - multiplicative is the default 859 . -pc_mg_log - log information about time spent on each level of the solver 860 . -pc_mg_monitor - print information on the multigrid convergence 861 . -pc_mg_galerkin - use Galerkin process to compute coarser operators 862 - -pc_mg_dump_matlab - dumps the matrices for each level and the restriction/interpolation matrices 863 to the Socket viewer for reading from Matlab. 864 865 Notes: 866 867 Level: intermediate 868 869 Concepts: multigrid 870 871 .seealso: PCCreate(), PCSetType(), PCType (for list of available types), PC, PCMGType, 872 PCMGSetLevels(), PCMGGetLevels(), PCMGSetType(), PCMGSetCycles(), PCMGSetNumberSmoothDown(), 873 PCMGSetNumberSmoothUp(), PCMGGetCoarseSolve(), PCMGSetResidual(), PCMGSetInterpolation(), 874 PCMGSetRestriction(), PCMGGetSmoother(), PCMGGetSmootherUp(), PCMGGetSmootherDown(), 875 PCMGSetCyclesOnLevel(), PCMGSetRhs(), PCMGSetX(), PCMGSetR() 876 M*/ 877 878 EXTERN_C_BEGIN 879 #undef __FUNCT__ 880 #define __FUNCT__ "PCCreate_MG" 881 PetscErrorCode PETSCKSP_DLLEXPORT PCCreate_MG(PC pc) 882 { 883 PetscFunctionBegin; 884 pc->ops->apply = PCApply_MG; 885 pc->ops->setup = PCSetUp_MG; 886 pc->ops->destroy = PCDestroy_MG; 887 pc->ops->setfromoptions = PCSetFromOptions_MG; 888 pc->ops->view = PCView_MG; 889 890 pc->data = (void*)0; 891 PetscFunctionReturn(0); 892 } 893 EXTERN_C_END 894