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