1 /*$Id: mg.c,v 1.128 2001/10/03 22:12:40 balay Exp $*/ 2 /* 3 Defines the multigrid preconditioner interface. 4 */ 5 #include "src/ksp/pc/impls/mg/mgimpl.h" /*I "petscmg.h" I*/ 6 7 8 /* 9 MGMCycle_Private - Given an MG structure created with MGCreate() runs 10 one multiplicative cycle down through the levels and 11 back up. 12 13 Input Parameter: 14 . mg - structure created with MGCreate(). 15 */ 16 #undef __FUNCT__ 17 #define __FUNCT__ "MGMCycle_Private" 18 int MGMCycle_Private(MG *mglevels,PetscTruth *converged) 19 { 20 MG mg = *mglevels,mgc; 21 int cycles = mg->cycles,ierr; 22 PetscScalar zero = 0.0; 23 24 PetscFunctionBegin; 25 if (converged) *converged = PETSC_FALSE; 26 27 if (mg->eventsolve) {ierr = PetscLogEventBegin(mg->eventsolve,0,0,0,0);CHKERRQ(ierr);} 28 ierr = KSPSetRhs(mg->smoothd,mg->b);CHKERRQ(ierr); 29 ierr = KSPSetSolution(mg->smoothd,mg->x);CHKERRQ(ierr); 30 ierr = KSPSolve(mg->smoothd);CHKERRQ(ierr); 31 if (mg->eventsolve) {ierr = PetscLogEventEnd(mg->eventsolve,0,0,0,0);CHKERRQ(ierr);} 32 if (mg->level) { /* not the coarsest grid */ 33 ierr = (*mg->residual)(mg->A,mg->b,mg->x,mg->r);CHKERRQ(ierr); 34 35 /* if on finest level and have convergence criteria set */ 36 if (mg->level == mg->levels-1 && mg->ttol) { 37 PetscReal rnorm; 38 ierr = VecNorm(mg->r,NORM_2,&rnorm);CHKERRQ(ierr); 39 if (rnorm <= mg->ttol) { 40 *converged = PETSC_TRUE; 41 if (rnorm < mg->atol) { 42 PetscLogInfo(0,"Linear solver has converged. Residual norm %g is less than absolute tolerance %g\n",rnorm,mg->atol); 43 } else { 44 PetscLogInfo(0,"Linear solver has converged. Residual norm %g is less than relative tolerance times initial residual norm %g\n",rnorm,mg->ttol); 45 } 46 PetscFunctionReturn(0); 47 } 48 } 49 50 mgc = *(mglevels - 1); 51 ierr = MatRestrict(mg->restrct,mg->r,mgc->b);CHKERRQ(ierr); 52 ierr = VecSet(&zero,mgc->x);CHKERRQ(ierr); 53 while (cycles--) { 54 ierr = MGMCycle_Private(mglevels-1,converged);CHKERRQ(ierr); 55 } 56 ierr = MatInterpolateAdd(mg->interpolate,mgc->x,mg->x,mg->x);CHKERRQ(ierr); 57 if (mg->eventsolve) {ierr = PetscLogEventBegin(mg->eventsolve,0,0,0,0);CHKERRQ(ierr);} 58 ierr = KSPSetRhs(mg->smoothu,mg->b);CHKERRQ(ierr); 59 ierr = KSPSetSolution(mg->smoothu,mg->x);CHKERRQ(ierr); 60 ierr = KSPSolve(mg->smoothu);CHKERRQ(ierr); 61 if (mg->eventsolve) {ierr = PetscLogEventEnd(mg->eventsolve,0,0,0,0);CHKERRQ(ierr);} 62 } 63 PetscFunctionReturn(0); 64 } 65 66 /* 67 MGCreate_Private - Creates a MG structure for use with the 68 multigrid code. Level 0 is the coarsest. (But the 69 finest level is stored first in the array). 70 71 */ 72 #undef __FUNCT__ 73 #define __FUNCT__ "MGCreate_Private" 74 static int MGCreate_Private(MPI_Comm comm,int levels,PC pc,MPI_Comm *comms,MG **result) 75 { 76 MG *mg; 77 int i,ierr,size; 78 char *prefix; 79 PC ipc; 80 81 PetscFunctionBegin; 82 ierr = PetscMalloc(levels*sizeof(MG),&mg);CHKERRQ(ierr); 83 PetscLogObjectMemory(pc,levels*(sizeof(MG)+sizeof(struct _MG))); 84 85 ierr = PCGetOptionsPrefix(pc,&prefix);CHKERRQ(ierr); 86 87 for (i=0; i<levels; i++) { 88 ierr = PetscNew(struct _MG,&mg[i]);CHKERRQ(ierr); 89 ierr = PetscMemzero(mg[i],sizeof(struct _MG));CHKERRQ(ierr); 90 mg[i]->level = i; 91 mg[i]->levels = levels; 92 mg[i]->cycles = 1; 93 94 if (comms) comm = comms[i]; 95 ierr = KSPCreate(comm,&mg[i]->smoothd);CHKERRQ(ierr); 96 ierr = KSPSetTolerances(mg[i]->smoothd,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,1);CHKERRQ(ierr); 97 ierr = KSPSetOptionsPrefix(mg[i]->smoothd,prefix);CHKERRQ(ierr); 98 99 /* do special stuff for coarse grid */ 100 if (!i && levels > 1) { 101 ierr = KSPAppendOptionsPrefix(mg[0]->smoothd,"mg_coarse_");CHKERRQ(ierr); 102 103 /* coarse solve is (redundant) LU by default */ 104 ierr = KSPSetType(mg[0]->smoothd,KSPPREONLY);CHKERRQ(ierr); 105 ierr = KSPGetPC(mg[0]->smoothd,&ipc);CHKERRQ(ierr); 106 ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 107 if (size > 1) { 108 ierr = PCSetType(ipc,PCREDUNDANT);CHKERRQ(ierr); 109 ierr = PCRedundantGetPC(ipc,&ipc);CHKERRQ(ierr); 110 } 111 ierr = PCSetType(ipc,PCLU);CHKERRQ(ierr); 112 113 } else { 114 ierr = KSPAppendOptionsPrefix(mg[i]->smoothd,"mg_levels_");CHKERRQ(ierr); 115 } 116 PetscLogObjectParent(pc,mg[i]->smoothd); 117 mg[i]->smoothu = mg[i]->smoothd; 118 mg[i]->default_smoothu = 10000; 119 mg[i]->default_smoothd = 10000; 120 mg[i]->rtol = 0.0; 121 mg[i]->atol = 0.0; 122 mg[i]->dtol = 0.0; 123 mg[i]->ttol = 0.0; 124 mg[i]->eventsetup = 0; 125 mg[i]->eventsolve = 0; 126 } 127 *result = mg; 128 PetscFunctionReturn(0); 129 } 130 131 #undef __FUNCT__ 132 #define __FUNCT__ "PCDestroy_MG" 133 static int PCDestroy_MG(PC pc) 134 { 135 MG *mg = (MG*)pc->data; 136 int i,n = mg[0]->levels,ierr; 137 138 PetscFunctionBegin; 139 for (i=0; i<n; i++) { 140 if (mg[i]->smoothd != mg[i]->smoothu) { 141 ierr = KSPDestroy(mg[i]->smoothd);CHKERRQ(ierr); 142 } 143 ierr = KSPDestroy(mg[i]->smoothu);CHKERRQ(ierr); 144 ierr = PetscFree(mg[i]);CHKERRQ(ierr); 145 } 146 ierr = PetscFree(mg);CHKERRQ(ierr); 147 PetscFunctionReturn(0); 148 } 149 150 151 152 EXTERN int MGACycle_Private(MG*); 153 EXTERN int MGFCycle_Private(MG*); 154 EXTERN int MGKCycle_Private(MG*); 155 156 /* 157 PCApply_MG - Runs either an additive, multiplicative, Kaskadic 158 or full cycle of multigrid. 159 160 Note: 161 A simple wrapper which calls MGMCycle(),MGACycle(), or MGFCycle(). 162 */ 163 #undef __FUNCT__ 164 #define __FUNCT__ "PCApply_MG" 165 static int PCApply_MG(PC pc,Vec b,Vec x) 166 { 167 MG *mg = (MG*)pc->data; 168 PetscScalar zero = 0.0; 169 int levels = mg[0]->levels,ierr; 170 171 PetscFunctionBegin; 172 mg[levels-1]->b = b; 173 mg[levels-1]->x = x; 174 if (mg[0]->am == MGMULTIPLICATIVE) { 175 ierr = VecSet(&zero,x);CHKERRQ(ierr); 176 ierr = MGMCycle_Private(mg+levels-1,PETSC_NULL);CHKERRQ(ierr); 177 } 178 else if (mg[0]->am == MGADDITIVE) { 179 ierr = MGACycle_Private(mg);CHKERRQ(ierr); 180 } 181 else if (mg[0]->am == MGKASKADE) { 182 ierr = MGKCycle_Private(mg);CHKERRQ(ierr); 183 } 184 else { 185 ierr = MGFCycle_Private(mg);CHKERRQ(ierr); 186 } 187 PetscFunctionReturn(0); 188 } 189 190 #undef __FUNCT__ 191 #define __FUNCT__ "PCApplyRichardson_MG" 192 static int PCApplyRichardson_MG(PC pc,Vec b,Vec x,Vec w,PetscReal rtol,PetscReal atol, PetscReal dtol,int its) 193 { 194 MG *mg = (MG*)pc->data; 195 int ierr,levels = mg[0]->levels; 196 PetscTruth converged = PETSC_FALSE; 197 198 PetscFunctionBegin; 199 mg[levels-1]->b = b; 200 mg[levels-1]->x = x; 201 202 mg[levels-1]->rtol = rtol; 203 mg[levels-1]->atol = atol; 204 mg[levels-1]->dtol = dtol; 205 if (rtol) { 206 /* compute initial residual norm for relative convergence test */ 207 PetscReal rnorm; 208 ierr = (*mg[levels-1]->residual)(mg[levels-1]->A,b,x,w);CHKERRQ(ierr); 209 ierr = VecNorm(w,NORM_2,&rnorm);CHKERRQ(ierr); 210 mg[levels-1]->ttol = PetscMax(rtol*rnorm,atol); 211 } else if (atol) { 212 mg[levels-1]->ttol = atol; 213 } else { 214 mg[levels-1]->ttol = 0.0; 215 } 216 217 while (its-- && !converged) { 218 ierr = MGMCycle_Private(mg+levels-1,&converged);CHKERRQ(ierr); 219 } 220 PetscFunctionReturn(0); 221 } 222 223 #undef __FUNCT__ 224 #define __FUNCT__ "PCSetFromOptions_MG" 225 static int PCSetFromOptions_MG(PC pc) 226 { 227 int ierr,indx,m,levels = 1; 228 PetscTruth flg; 229 const char *type[] = {"additive","multiplicative","full","cascade","kascade"}; 230 231 PetscFunctionBegin; 232 233 ierr = PetscOptionsHead("Multigrid options");CHKERRQ(ierr); 234 if (!pc->data) { 235 ierr = PetscOptionsInt("-pc_mg_levels","Number of Levels","MGSetLevels",levels,&levels,&flg);CHKERRQ(ierr); 236 ierr = MGSetLevels(pc,levels,PETSC_NULL);CHKERRQ(ierr); 237 } 238 ierr = PetscOptionsInt("-pc_mg_cycles","1 for V cycle, 2 for W-cycle","MGSetCycles",1,&m,&flg);CHKERRQ(ierr); 239 if (flg) { 240 ierr = MGSetCycles(pc,m);CHKERRQ(ierr); 241 } 242 ierr = PetscOptionsInt("-pc_mg_smoothup","Number of post-smoothing steps","MGSetNumberSmoothUp",1,&m,&flg);CHKERRQ(ierr); 243 if (flg) { 244 ierr = MGSetNumberSmoothUp(pc,m);CHKERRQ(ierr); 245 } 246 ierr = PetscOptionsInt("-pc_mg_smoothdown","Number of pre-smoothing steps","MGSetNumberSmoothDown",1,&m,&flg);CHKERRQ(ierr); 247 if (flg) { 248 ierr = MGSetNumberSmoothDown(pc,m);CHKERRQ(ierr); 249 } 250 ierr = PetscOptionsEList("-pc_mg_type","Multigrid type","MGSetType",type,5,type[1],&indx,&flg);CHKERRQ(ierr); 251 if (flg) { 252 MGType mg = (MGType) 0; 253 switch (indx) { 254 case 0: 255 mg = MGADDITIVE; 256 break; 257 case 1: 258 mg = MGMULTIPLICATIVE; 259 break; 260 case 2: 261 mg = MGFULL; 262 break; 263 case 3: 264 mg = MGKASKADE; 265 break; 266 case 4: 267 mg = MGKASKADE; 268 break; 269 } 270 ierr = MGSetType(pc,mg);CHKERRQ(ierr); 271 } 272 ierr = PetscOptionsName("-pc_mg_log","Log times for each multigrid level","None",&flg);CHKERRQ(ierr); 273 if (flg) { 274 MG *mg = (MG*)pc->data; 275 int i; 276 char eventname[128]; 277 if (!mg) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling"); 278 levels = mg[0]->levels; 279 for (i=0; i<levels; i++) { 280 sprintf(eventname,"MSetup Level %d",i); 281 ierr = PetscLogEventRegister(&mg[i]->eventsetup,eventname,pc->cookie);CHKERRQ(ierr); 282 sprintf(eventname,"MGSolve Level %d",i); 283 ierr = PetscLogEventRegister(&mg[i]->eventsolve,eventname,pc->cookie);CHKERRQ(ierr); 284 } 285 } 286 ierr = PetscOptionsTail();CHKERRQ(ierr); 287 PetscFunctionReturn(0); 288 } 289 290 #undef __FUNCT__ 291 #define __FUNCT__ "PCView_MG" 292 static int PCView_MG(PC pc,PetscViewer viewer) 293 { 294 MG *mg = (MG*)pc->data; 295 int ierr,levels = mg[0]->levels,i; 296 const char *cstring; 297 PetscTruth isascii; 298 299 PetscFunctionBegin; 300 ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr); 301 if (isascii) { 302 if (mg[0]->am == MGMULTIPLICATIVE) cstring = "multiplicative"; 303 else if (mg[0]->am == MGADDITIVE) cstring = "additive"; 304 else if (mg[0]->am == MGFULL) cstring = "full"; 305 else if (mg[0]->am == MGKASKADE) cstring = "Kaskade"; 306 else cstring = "unknown"; 307 ierr = PetscViewerASCIIPrintf(viewer," MG: type is %s, levels=%d cycles=%d, pre-smooths=%d, post-smooths=%d\n", 308 cstring,levels,mg[0]->cycles,mg[0]->default_smoothd,mg[0]->default_smoothu);CHKERRQ(ierr); 309 for (i=0; i<levels; i++) { 310 ierr = PetscViewerASCIIPrintf(viewer,"Down solver (pre-smoother) on level %d -------------------------------\n",i);CHKERRQ(ierr); 311 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 312 ierr = KSPView(mg[i]->smoothd,viewer);CHKERRQ(ierr); 313 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 314 if (mg[i]->smoothd == mg[i]->smoothu) { 315 ierr = PetscViewerASCIIPrintf(viewer,"Up solver (post-smoother) same as down solver (pre-smoother)\n");CHKERRQ(ierr); 316 } else { 317 ierr = PetscViewerASCIIPrintf(viewer,"Up solver (post-smoother) on level %d -------------------------------\n",i);CHKERRQ(ierr); 318 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 319 ierr = KSPView(mg[i]->smoothu,viewer);CHKERRQ(ierr); 320 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 321 } 322 } 323 } else { 324 SETERRQ1(1,"Viewer type %s not supported for PCMG",((PetscObject)viewer)->type_name); 325 } 326 PetscFunctionReturn(0); 327 } 328 329 /* 330 Calls setup for the KSP on each level 331 */ 332 #undef __FUNCT__ 333 #define __FUNCT__ "PCSetUp_MG" 334 static int PCSetUp_MG(PC pc) 335 { 336 MG *mg = (MG*)pc->data; 337 int ierr,i,n = mg[0]->levels; 338 PC cpc; 339 PetscTruth preonly,lu,redundant,monitor = PETSC_FALSE,dump; 340 PetscViewer ascii; 341 MPI_Comm comm; 342 343 PetscFunctionBegin; 344 /* 345 temporarily stick pc->vec into mg[0]->b and x so that 346 KSPSetUp is happy. Since currently those slots are empty. 347 */ 348 mg[n-1]->x = pc->vec; 349 mg[n-1]->b = pc->vec; 350 351 if (pc->setupcalled == 0) { 352 ierr = PetscOptionsHasName(0,"-pc_mg_monitor",&monitor);CHKERRQ(ierr); 353 354 for (i=1; i<n; i++) { 355 if (mg[i]->smoothd) { 356 if (monitor) { 357 ierr = PetscObjectGetComm((PetscObject)mg[i]->smoothd,&comm);CHKERRQ(ierr); 358 ierr = PetscViewerASCIIOpen(comm,"stdout",&ascii);CHKERRQ(ierr); 359 ierr = PetscViewerASCIISetTab(ascii,n-i);CHKERRQ(ierr); 360 ierr = KSPSetMonitor(mg[i]->smoothd,KSPDefaultMonitor,ascii,(int(*)(void*))PetscViewerDestroy);CHKERRQ(ierr); 361 } 362 ierr = KSPSetFromOptions(mg[i]->smoothd);CHKERRQ(ierr); 363 } 364 } 365 for (i=0; i<n; i++) { 366 if (mg[i]->smoothu && mg[i]->smoothu != mg[i]->smoothd) { 367 if (monitor) { 368 ierr = PetscObjectGetComm((PetscObject)mg[i]->smoothu,&comm);CHKERRQ(ierr); 369 ierr = PetscViewerASCIIOpen(comm,"stdout",&ascii);CHKERRQ(ierr); 370 ierr = PetscViewerASCIISetTab(ascii,n-i);CHKERRQ(ierr); 371 ierr = KSPSetMonitor(mg[i]->smoothu,KSPDefaultMonitor,ascii,(int(*)(void*))PetscViewerDestroy);CHKERRQ(ierr); 372 } 373 ierr = KSPSetFromOptions(mg[i]->smoothu);CHKERRQ(ierr); 374 } 375 } 376 } 377 378 for (i=1; i<n; i++) { 379 if (mg[i]->smoothd) { 380 ierr = KSPSetInitialGuessNonzero(mg[i]->smoothd,PETSC_TRUE);CHKERRQ(ierr); 381 if (mg[i]->eventsetup) {ierr = PetscLogEventBegin(mg[i]->eventsetup,0,0,0,0);CHKERRQ(ierr);} 382 ierr = KSPSetRhs(mg[i]->smoothd,mg[i]->b);CHKERRQ(ierr); 383 ierr = KSPSetSolution(mg[i]->smoothd,mg[i]->x);CHKERRQ(ierr); 384 ierr = KSPSetUp(mg[i]->smoothd);CHKERRQ(ierr); 385 if (mg[i]->eventsetup) {ierr = PetscLogEventEnd(mg[i]->eventsetup,0,0,0,0);CHKERRQ(ierr);} 386 } 387 } 388 for (i=0; i<n; i++) { 389 if (mg[i]->smoothu && mg[i]->smoothu != mg[i]->smoothd) { 390 ierr = KSPSetInitialGuessNonzero(mg[i]->smoothu,PETSC_TRUE);CHKERRQ(ierr); 391 if (mg[i]->eventsetup) {ierr = PetscLogEventBegin(mg[i]->eventsetup,0,0,0,0);CHKERRQ(ierr);} 392 ierr = KSPSetRhs(mg[i]->smoothu,mg[i]->b);CHKERRQ(ierr); 393 ierr = KSPSetSolution(mg[i]->smoothu,mg[i]->x);CHKERRQ(ierr); 394 ierr = KSPSetUp(mg[i]->smoothu);CHKERRQ(ierr); 395 if (mg[i]->eventsetup) {ierr = PetscLogEventEnd(mg[i]->eventsetup,0,0,0,0);CHKERRQ(ierr);} 396 } 397 } 398 399 /* 400 If coarse solver is not direct method then DO NOT USE preonly 401 */ 402 ierr = PetscTypeCompare((PetscObject)mg[0]->smoothd,KSPPREONLY,&preonly);CHKERRQ(ierr); 403 if (preonly) { 404 ierr = KSPGetPC(mg[0]->smoothd,&cpc);CHKERRQ(ierr); 405 ierr = PetscTypeCompare((PetscObject)cpc,PCLU,&lu);CHKERRQ(ierr); 406 ierr = PetscTypeCompare((PetscObject)cpc,PCREDUNDANT,&redundant);CHKERRQ(ierr); 407 if (!lu && !redundant) { 408 ierr = KSPSetType(mg[0]->smoothd,KSPGMRES);CHKERRQ(ierr); 409 } 410 } 411 412 if (pc->setupcalled == 0) { 413 if (monitor) { 414 ierr = PetscObjectGetComm((PetscObject)mg[0]->smoothd,&comm);CHKERRQ(ierr); 415 ierr = PetscViewerASCIIOpen(comm,"stdout",&ascii);CHKERRQ(ierr); 416 ierr = PetscViewerASCIISetTab(ascii,n);CHKERRQ(ierr); 417 ierr = KSPSetMonitor(mg[0]->smoothd,KSPDefaultMonitor,ascii,(int(*)(void*))PetscViewerDestroy);CHKERRQ(ierr); 418 } 419 ierr = KSPSetFromOptions(mg[0]->smoothd);CHKERRQ(ierr); 420 } 421 422 if (mg[0]->eventsetup) {ierr = PetscLogEventBegin(mg[0]->eventsetup,0,0,0,0);CHKERRQ(ierr);} 423 ierr = KSPSetRhs(mg[0]->smoothd,mg[0]->b);CHKERRQ(ierr); 424 ierr = KSPSetSolution(mg[0]->smoothd,mg[0]->x);CHKERRQ(ierr); 425 ierr = KSPSetUp(mg[0]->smoothd);CHKERRQ(ierr); 426 if (mg[0]->eventsetup) {ierr = PetscLogEventEnd(mg[0]->eventsetup,0,0,0,0);CHKERRQ(ierr);} 427 428 /* 429 Dump the interpolation/restriction matrices to matlab plus the 430 Jacobian/stiffness on each level. This allows Matlab users to 431 easily check if the Galerkin condition A_c = R A_f R^T is satisfied */ 432 ierr = PetscOptionsHasName(pc->prefix,"-pc_mg_dump_matlab",&dump);CHKERRQ(ierr); 433 if (dump) { 434 for (i=1; i<n; i++) { 435 ierr = MatView(mg[i]->restrct,PETSC_VIEWER_SOCKET_(pc->comm));CHKERRQ(ierr); 436 } 437 for (i=0; i<n; i++) { 438 ierr = KSPGetPC(mg[i]->smoothd,&pc);CHKERRQ(ierr); 439 ierr = MatView(pc->mat,PETSC_VIEWER_SOCKET_(pc->comm));CHKERRQ(ierr); 440 } 441 } 442 443 PetscFunctionReturn(0); 444 } 445 446 /* -------------------------------------------------------------------------------------*/ 447 448 #undef __FUNCT__ 449 #define __FUNCT__ "MGSetLevels" 450 /*@C 451 MGSetLevels - Sets the number of levels to use with MG. 452 Must be called before any other MG routine. 453 454 Collective on PC 455 456 Input Parameters: 457 + pc - the preconditioner context 458 . levels - the number of levels 459 - comms - optional communicators for each level; this is to allow solving the coarser problems 460 on smaller sets of processors. Use PETSC_NULL_OBJECT for default in Fortran 461 462 Level: intermediate 463 464 Notes: 465 If the number of levels is one then the multigrid uses the -mg_levels prefix 466 for setting the level options rather than the -mg_coarse prefix. 467 468 .keywords: MG, set, levels, multigrid 469 470 .seealso: MGSetType(), MGGetLevels() 471 @*/ 472 int MGSetLevels(PC pc,int levels,MPI_Comm *comms) 473 { 474 int ierr; 475 MG *mg; 476 477 PetscFunctionBegin; 478 PetscValidHeaderSpecific(pc,PC_COOKIE); 479 480 if (pc->data) { 481 SETERRQ(1,"Number levels already set for MG\n\ 482 make sure that you call MGSetLevels() before KSPSetFromOptions()"); 483 } 484 ierr = MGCreate_Private(pc->comm,levels,pc,comms,&mg);CHKERRQ(ierr); 485 mg[0]->am = MGMULTIPLICATIVE; 486 pc->data = (void*)mg; 487 pc->ops->applyrichardson = PCApplyRichardson_MG; 488 PetscFunctionReturn(0); 489 } 490 491 #undef __FUNCT__ 492 #define __FUNCT__ "MGGetLevels" 493 /*@ 494 MGGetLevels - Gets the number of levels to use with MG. 495 496 Not Collective 497 498 Input Parameter: 499 . pc - the preconditioner context 500 501 Output parameter: 502 . levels - the number of levels 503 504 Level: advanced 505 506 .keywords: MG, get, levels, multigrid 507 508 .seealso: MGSetLevels() 509 @*/ 510 int MGGetLevels(PC pc,int *levels) 511 { 512 MG *mg; 513 514 PetscFunctionBegin; 515 PetscValidHeaderSpecific(pc,PC_COOKIE); 516 517 mg = (MG*)pc->data; 518 *levels = mg[0]->levels; 519 PetscFunctionReturn(0); 520 } 521 522 #undef __FUNCT__ 523 #define __FUNCT__ "MGSetType" 524 /*@ 525 MGSetType - Determines the form of multigrid to use: 526 multiplicative, additive, full, or the Kaskade algorithm. 527 528 Collective on PC 529 530 Input Parameters: 531 + pc - the preconditioner context 532 - form - multigrid form, one of MGMULTIPLICATIVE, MGADDITIVE, 533 MGFULL, MGKASKADE 534 535 Options Database Key: 536 . -pc_mg_type <form> - Sets <form>, one of multiplicative, 537 additive, full, kaskade 538 539 Level: advanced 540 541 .keywords: MG, set, method, multiplicative, additive, full, Kaskade, multigrid 542 543 .seealso: MGSetLevels() 544 @*/ 545 int MGSetType(PC pc,MGType form) 546 { 547 MG *mg; 548 549 PetscFunctionBegin; 550 PetscValidHeaderSpecific(pc,PC_COOKIE); 551 mg = (MG*)pc->data; 552 553 if (!mg) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling"); 554 mg[0]->am = form; 555 if (form == MGMULTIPLICATIVE) pc->ops->applyrichardson = PCApplyRichardson_MG; 556 else pc->ops->applyrichardson = 0; 557 PetscFunctionReturn(0); 558 } 559 560 #undef __FUNCT__ 561 #define __FUNCT__ "MGSetCycles" 562 /*@ 563 MGSetCycles - Sets the type cycles to use. Use MGSetCyclesOnLevel() for more 564 complicated cycling. 565 566 Collective on PC 567 568 Input Parameters: 569 + mg - the multigrid context 570 - n - the number of cycles 571 572 Options Database Key: 573 $ -pc_mg_cycles n - 1 denotes a V-cycle; 2 denotes a W-cycle. 574 575 Level: advanced 576 577 .keywords: MG, set, cycles, V-cycle, W-cycle, multigrid 578 579 .seealso: MGSetCyclesOnLevel() 580 @*/ 581 int MGSetCycles(PC pc,int n) 582 { 583 MG *mg; 584 int i,levels; 585 586 PetscFunctionBegin; 587 PetscValidHeaderSpecific(pc,PC_COOKIE); 588 mg = (MG*)pc->data; 589 if (!mg) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling"); 590 levels = mg[0]->levels; 591 592 for (i=0; i<levels; i++) { 593 mg[i]->cycles = n; 594 } 595 PetscFunctionReturn(0); 596 } 597 598 #undef __FUNCT__ 599 #define __FUNCT__ "MGCheck" 600 /*@ 601 MGCheck - Checks that all components of the MG structure have 602 been set. 603 604 Collective on PC 605 606 Input Parameters: 607 . mg - the MG structure 608 609 Level: advanced 610 611 .keywords: MG, check, set, multigrid 612 @*/ 613 int MGCheck(PC pc) 614 { 615 MG *mg; 616 int i,n,count = 0; 617 618 PetscFunctionBegin; 619 PetscValidHeaderSpecific(pc,PC_COOKIE); 620 mg = (MG*)pc->data; 621 622 if (!mg) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling"); 623 624 n = mg[0]->levels; 625 626 for (i=1; i<n; i++) { 627 if (!mg[i]->restrct) { 628 (*PetscErrorPrintf)("No restrict set level %d \n",n-i); count++; 629 } 630 if (!mg[i]->interpolate) { 631 (*PetscErrorPrintf)("No interpolate set level %d \n",n-i); count++; 632 } 633 if (!mg[i]->residual) { 634 (*PetscErrorPrintf)("No residual set level %d \n",n-i); count++; 635 } 636 if (!mg[i]->smoothu) { 637 (*PetscErrorPrintf)("No smoothup set level %d \n",n-i); count++; 638 } 639 if (!mg[i]->smoothd) { 640 (*PetscErrorPrintf)("No smoothdown set level %d \n",n-i); count++; 641 } 642 if (!mg[i]->r) { 643 (*PetscErrorPrintf)("No r set level %d \n",n-i); count++; 644 } 645 if (!mg[i-1]->x) { 646 (*PetscErrorPrintf)("No x set level %d \n",n-i); count++; 647 } 648 if (!mg[i-1]->b) { 649 (*PetscErrorPrintf)("No b set level %d \n",n-i); count++; 650 } 651 } 652 PetscFunctionReturn(count); 653 } 654 655 656 #undef __FUNCT__ 657 #define __FUNCT__ "MGSetNumberSmoothDown" 658 /*@ 659 MGSetNumberSmoothDown - Sets the number of pre-smoothing steps to 660 use on all levels. Use MGGetSmootherDown() to set different 661 pre-smoothing steps on different levels. 662 663 Collective on PC 664 665 Input Parameters: 666 + mg - the multigrid context 667 - n - the number of smoothing steps 668 669 Options Database Key: 670 . -pc_mg_smoothdown <n> - Sets number of pre-smoothing steps 671 672 Level: advanced 673 674 .keywords: MG, smooth, down, pre-smoothing, steps, multigrid 675 676 .seealso: MGSetNumberSmoothUp() 677 @*/ 678 int MGSetNumberSmoothDown(PC pc,int n) 679 { 680 MG *mg; 681 int i,levels,ierr; 682 683 PetscFunctionBegin; 684 PetscValidHeaderSpecific(pc,PC_COOKIE); 685 mg = (MG*)pc->data; 686 if (!mg) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling"); 687 levels = mg[0]->levels; 688 689 for (i=0; i<levels; i++) { 690 /* make sure smoother up and down are different */ 691 ierr = MGGetSmootherUp(pc,i,PETSC_NULL);CHKERRQ(ierr); 692 ierr = KSPSetTolerances(mg[i]->smoothd,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,n);CHKERRQ(ierr); 693 mg[i]->default_smoothd = n; 694 } 695 PetscFunctionReturn(0); 696 } 697 698 #undef __FUNCT__ 699 #define __FUNCT__ "MGSetNumberSmoothUp" 700 /*@ 701 MGSetNumberSmoothUp - Sets the number of post-smoothing steps to use 702 on all levels. Use MGGetSmootherUp() to set different numbers of 703 post-smoothing steps on different levels. 704 705 Collective on PC 706 707 Input Parameters: 708 + mg - the multigrid context 709 - n - the number of smoothing steps 710 711 Options Database Key: 712 . -pc_mg_smoothup <n> - Sets number of post-smoothing steps 713 714 Level: advanced 715 716 Note: this does not set a value on the coarsest grid, since we assume that 717 there is no seperate smooth up on the coarsest grid. If you want to have a 718 seperate smooth up on the coarsest grid then call MGGetSmoothUp(pc,0,&ksp); 719 720 .keywords: MG, smooth, up, post-smoothing, steps, multigrid 721 722 .seealso: MGSetNumberSmoothDown() 723 @*/ 724 int MGSetNumberSmoothUp(PC pc,int n) 725 { 726 MG *mg; 727 int i,levels,ierr; 728 729 PetscFunctionBegin; 730 PetscValidHeaderSpecific(pc,PC_COOKIE); 731 mg = (MG*)pc->data; 732 if (!mg) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling"); 733 levels = mg[0]->levels; 734 735 for (i=1; i<levels; i++) { 736 /* make sure smoother up and down are different */ 737 ierr = MGGetSmootherUp(pc,i,PETSC_NULL);CHKERRQ(ierr); 738 ierr = KSPSetTolerances(mg[i]->smoothu,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,n);CHKERRQ(ierr); 739 mg[i]->default_smoothu = n; 740 } 741 PetscFunctionReturn(0); 742 } 743 744 /* ----------------------------------------------------------------------------------------*/ 745 746 EXTERN_C_BEGIN 747 #undef __FUNCT__ 748 #define __FUNCT__ "PCCreate_MG" 749 int PCCreate_MG(PC pc) 750 { 751 PetscFunctionBegin; 752 pc->ops->apply = PCApply_MG; 753 pc->ops->setup = PCSetUp_MG; 754 pc->ops->destroy = PCDestroy_MG; 755 pc->ops->setfromoptions = PCSetFromOptions_MG; 756 pc->ops->view = PCView_MG; 757 758 pc->data = (void*)0; 759 PetscFunctionReturn(0); 760 } 761 EXTERN_C_END 762