Actual source code: mg.c

petsc-3.8.3 2017-12-09
Report Typos and Errors

  2: /*
  3:     Defines the multigrid preconditioner interface.
  4: */
  5:  #include <petsc/private/pcmgimpl.h>
  6:  #include <petscdm.h>

  8: PetscErrorCode PCMGMCycle_Private(PC pc,PC_MG_Levels **mglevelsin,PCRichardsonConvergedReason *reason)
  9: {
 10:   PC_MG          *mg = (PC_MG*)pc->data;
 11:   PC_MG_Levels   *mgc,*mglevels = *mglevelsin;
 13:   PetscInt       cycles = (mglevels->level == 1) ? 1 : (PetscInt) mglevels->cycles;
 14:   PC             subpc;
 15:   PCFailedReason pcreason;

 18:   if (mglevels->eventsmoothsolve) {PetscLogEventBegin(mglevels->eventsmoothsolve,0,0,0,0);}
 19:   KSPSolve(mglevels->smoothd,mglevels->b,mglevels->x);  /* pre-smooth */
 20:   KSPGetPC(mglevels->smoothd,&subpc);
 21:   PCGetSetUpFailedReason(subpc,&pcreason);
 22:   if (pcreason) {
 23:     pc->failedreason = PC_SUBPC_ERROR;
 24:   }
 25:   if (mglevels->eventsmoothsolve) {PetscLogEventEnd(mglevels->eventsmoothsolve,0,0,0,0);}
 26:   if (mglevels->level) {  /* not the coarsest grid */
 27:     if (mglevels->eventresidual) {PetscLogEventBegin(mglevels->eventresidual,0,0,0,0);}
 28:     (*mglevels->residual)(mglevels->A,mglevels->b,mglevels->x,mglevels->r);
 29:     if (mglevels->eventresidual) {PetscLogEventEnd(mglevels->eventresidual,0,0,0,0);}

 31:     /* if on finest level and have convergence criteria set */
 32:     if (mglevels->level == mglevels->levels-1 && mg->ttol && reason) {
 33:       PetscReal rnorm;
 34:       VecNorm(mglevels->r,NORM_2,&rnorm);
 35:       if (rnorm <= mg->ttol) {
 36:         if (rnorm < mg->abstol) {
 37:           *reason = PCRICHARDSON_CONVERGED_ATOL;
 38:           PetscInfo2(pc,"Linear solver has converged. Residual norm %g is less than absolute tolerance %g\n",(double)rnorm,(double)mg->abstol);
 39:         } else {
 40:           *reason = PCRICHARDSON_CONVERGED_RTOL;
 41:           PetscInfo2(pc,"Linear solver has converged. Residual norm %g is less than relative tolerance times initial residual norm %g\n",(double)rnorm,(double)mg->ttol);
 42:         }
 43:         return(0);
 44:       }
 45:     }

 47:     mgc = *(mglevelsin - 1);
 48:     if (mglevels->eventinterprestrict) {PetscLogEventBegin(mglevels->eventinterprestrict,0,0,0,0);}
 49:     MatRestrict(mglevels->restrct,mglevels->r,mgc->b);
 50:     if (mglevels->eventinterprestrict) {PetscLogEventEnd(mglevels->eventinterprestrict,0,0,0,0);}
 51:     VecSet(mgc->x,0.0);
 52:     while (cycles--) {
 53:       PCMGMCycle_Private(pc,mglevelsin-1,reason);
 54:     }
 55:     if (mglevels->eventinterprestrict) {PetscLogEventBegin(mglevels->eventinterprestrict,0,0,0,0);}
 56:     MatInterpolateAdd(mglevels->interpolate,mgc->x,mglevels->x,mglevels->x);
 57:     if (mglevels->eventinterprestrict) {PetscLogEventEnd(mglevels->eventinterprestrict,0,0,0,0);}
 58:     if (mglevels->eventsmoothsolve) {PetscLogEventBegin(mglevels->eventsmoothsolve,0,0,0,0);}
 59:     KSPSolve(mglevels->smoothu,mglevels->b,mglevels->x);    /* post smooth */
 60:     if (mglevels->eventsmoothsolve) {PetscLogEventEnd(mglevels->eventsmoothsolve,0,0,0,0);}
 61:   }
 62:   return(0);
 63: }

 65: static PetscErrorCode PCApplyRichardson_MG(PC pc,Vec b,Vec x,Vec w,PetscReal rtol,PetscReal abstol, PetscReal dtol,PetscInt its,PetscBool zeroguess,PetscInt *outits,PCRichardsonConvergedReason *reason)
 66: {
 67:   PC_MG          *mg        = (PC_MG*)pc->data;
 68:   PC_MG_Levels   **mglevels = mg->levels;
 70:   PetscInt       levels = mglevels[0]->levels,i;

 73:   /* When the DM is supplying the matrix then it will not exist until here */
 74:   for (i=0; i<levels; i++) {
 75:     if (!mglevels[i]->A) {
 76:       KSPGetOperators(mglevels[i]->smoothu,&mglevels[i]->A,NULL);
 77:       PetscObjectReference((PetscObject)mglevels[i]->A);
 78:     }
 79:   }
 80:   mglevels[levels-1]->b = b;
 81:   mglevels[levels-1]->x = x;

 83:   mg->rtol   = rtol;
 84:   mg->abstol = abstol;
 85:   mg->dtol   = dtol;
 86:   if (rtol) {
 87:     /* compute initial residual norm for relative convergence test */
 88:     PetscReal rnorm;
 89:     if (zeroguess) {
 90:       VecNorm(b,NORM_2,&rnorm);
 91:     } else {
 92:       (*mglevels[levels-1]->residual)(mglevels[levels-1]->A,b,x,w);
 93:       VecNorm(w,NORM_2,&rnorm);
 94:     }
 95:     mg->ttol = PetscMax(rtol*rnorm,abstol);
 96:   } else if (abstol) mg->ttol = abstol;
 97:   else mg->ttol = 0.0;

 99:   /* since smoother is applied to full system, not just residual we need to make sure that smoothers don't
100:      stop prematurely due to small residual */
101:   for (i=1; i<levels; i++) {
102:     KSPSetTolerances(mglevels[i]->smoothu,0,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);
103:     if (mglevels[i]->smoothu != mglevels[i]->smoothd) {
104:       /* For Richardson the initial guess is nonzero since it is solving in each cycle the original system not just applying as a preconditioner */
105:       KSPSetInitialGuessNonzero(mglevels[i]->smoothd,PETSC_TRUE);
106:       KSPSetTolerances(mglevels[i]->smoothd,0,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);
107:     }
108:   }

110:   *reason = (PCRichardsonConvergedReason)0;
111:   for (i=0; i<its; i++) {
112:     PCMGMCycle_Private(pc,mglevels+levels-1,reason);
113:     if (*reason) break;
114:   }
115:   if (!*reason) *reason = PCRICHARDSON_CONVERGED_ITS;
116:   *outits = i;
117:   return(0);
118: }

120: PetscErrorCode PCReset_MG(PC pc)
121: {
122:   PC_MG          *mg        = (PC_MG*)pc->data;
123:   PC_MG_Levels   **mglevels = mg->levels;
125:   PetscInt       i,n;

128:   if (mglevels) {
129:     n = mglevels[0]->levels;
130:     for (i=0; i<n-1; i++) {
131:       VecDestroy(&mglevels[i+1]->r);
132:       VecDestroy(&mglevels[i]->b);
133:       VecDestroy(&mglevels[i]->x);
134:       MatDestroy(&mglevels[i+1]->restrct);
135:       MatDestroy(&mglevels[i+1]->interpolate);
136:       VecDestroy(&mglevels[i+1]->rscale);
137:     }

139:     for (i=0; i<n; i++) {
140:       MatDestroy(&mglevels[i]->A);
141:       if (mglevels[i]->smoothd != mglevels[i]->smoothu) {
142:         KSPReset(mglevels[i]->smoothd);
143:       }
144:       KSPReset(mglevels[i]->smoothu);
145:     }
146:   }
147:   return(0);
148: }

150: /*@C
151:    PCMGSetLevels - Sets the number of levels to use with MG.
152:    Must be called before any other MG routine.

154:    Logically Collective on PC

156:    Input Parameters:
157: +  pc - the preconditioner context
158: .  levels - the number of levels
159: -  comms - optional communicators for each level; this is to allow solving the coarser problems
160:            on smaller sets of processors.

162:    Level: intermediate

164:    Notes:
165:      If the number of levels is one then the multigrid uses the -mg_levels prefix
166:   for setting the level options rather than the -mg_coarse prefix.

168: .keywords: MG, set, levels, multigrid

170: .seealso: PCMGSetType(), PCMGGetLevels()
171: @*/
172: PetscErrorCode  PCMGSetLevels(PC pc,PetscInt levels,MPI_Comm *comms)
173: {
175:   PC_MG          *mg        = (PC_MG*)pc->data;
176:   MPI_Comm       comm;
177:   PC_MG_Levels   **mglevels = mg->levels;
178:   PCMGType       mgtype     = mg->am;
179:   PetscInt       mgctype    = (PetscInt) PC_MG_CYCLE_V;
180:   PetscInt       i;
181:   PetscMPIInt    size;
182:   const char     *prefix;
183:   PC             ipc;
184:   PetscInt       n;

189:   if (mg->nlevels == levels) return(0);
190:   PetscObjectGetComm((PetscObject)pc,&comm);
191:   if (mglevels) {
192:     mgctype = mglevels[0]->cycles;
193:     /* changing the number of levels so free up the previous stuff */
194:     PCReset_MG(pc);
195:     n    = mglevels[0]->levels;
196:     for (i=0; i<n; i++) {
197:       if (mglevels[i]->smoothd != mglevels[i]->smoothu) {
198:         KSPDestroy(&mglevels[i]->smoothd);
199:       }
200:       KSPDestroy(&mglevels[i]->smoothu);
201:       PetscFree(mglevels[i]);
202:     }
203:     PetscFree(mg->levels);
204:   }

206:   mg->nlevels = levels;

208:   PetscMalloc1(levels,&mglevels);
209:   PetscLogObjectMemory((PetscObject)pc,levels*(sizeof(PC_MG*)));

211:   PCGetOptionsPrefix(pc,&prefix);

213:   mg->stageApply = 0;
214:   for (i=0; i<levels; i++) {
215:     PetscNewLog(pc,&mglevels[i]);

217:     mglevels[i]->level               = i;
218:     mglevels[i]->levels              = levels;
219:     mglevels[i]->cycles              = mgctype;
220:     mg->default_smoothu              = 2;
221:     mg->default_smoothd              = 2;
222:     mglevels[i]->eventsmoothsetup    = 0;
223:     mglevels[i]->eventsmoothsolve    = 0;
224:     mglevels[i]->eventresidual       = 0;
225:     mglevels[i]->eventinterprestrict = 0;

227:     if (comms) comm = comms[i];
228:     KSPCreate(comm,&mglevels[i]->smoothd);
229:     KSPSetErrorIfNotConverged(mglevels[i]->smoothd,pc->erroriffailure);
230:     PetscObjectIncrementTabLevel((PetscObject)mglevels[i]->smoothd,(PetscObject)pc,levels-i);
231:     KSPSetOptionsPrefix(mglevels[i]->smoothd,prefix);
232:     PetscObjectComposedDataSetInt((PetscObject) mglevels[i]->smoothd, PetscMGLevelId, mglevels[i]->level);
233:     if (i || levels == 1) {
234:       char tprefix[128];

236:       KSPSetType(mglevels[i]->smoothd,KSPCHEBYSHEV);
237:       KSPSetConvergenceTest(mglevels[i]->smoothd,KSPConvergedSkip,NULL,NULL);
238:       KSPSetNormType(mglevels[i]->smoothd,KSP_NORM_NONE);
239:       KSPGetPC(mglevels[i]->smoothd,&ipc);
240:       PCSetType(ipc,PCSOR);
241:       KSPSetTolerances(mglevels[i]->smoothd,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT, mg->default_smoothd);

243:       sprintf(tprefix,"mg_levels_%d_",(int)i);
244:       KSPAppendOptionsPrefix(mglevels[i]->smoothd,tprefix);
245:     } else {
246:       KSPAppendOptionsPrefix(mglevels[0]->smoothd,"mg_coarse_");

248:       /* coarse solve is (redundant) LU by default; set shifttype NONZERO to avoid annoying zero-pivot in LU preconditioner */
249:       KSPSetType(mglevels[0]->smoothd,KSPPREONLY);
250:       KSPGetPC(mglevels[0]->smoothd,&ipc);
251:       MPI_Comm_size(comm,&size);
252:       if (size > 1) {
253:         PCSetType(ipc,PCREDUNDANT);
254:       } else {
255:         PCSetType(ipc,PCLU);
256:       }
257:       PCFactorSetShiftType(ipc,MAT_SHIFT_INBLOCKS);
258:     }
259:     PetscLogObjectParent((PetscObject)pc,(PetscObject)mglevels[i]->smoothd);

261:     mglevels[i]->smoothu = mglevels[i]->smoothd;
262:     mg->rtol             = 0.0;
263:     mg->abstol           = 0.0;
264:     mg->dtol             = 0.0;
265:     mg->ttol             = 0.0;
266:     mg->cyclesperpcapply = 1;
267:   }
268:   mg->levels = mglevels;
269:   PCMGSetType(pc,mgtype);
270:   return(0);
271: }


274: PetscErrorCode PCDestroy_MG(PC pc)
275: {
277:   PC_MG          *mg        = (PC_MG*)pc->data;
278:   PC_MG_Levels   **mglevels = mg->levels;
279:   PetscInt       i,n;

282:   PCReset_MG(pc);
283:   if (mglevels) {
284:     n = mglevels[0]->levels;
285:     for (i=0; i<n; i++) {
286:       if (mglevels[i]->smoothd != mglevels[i]->smoothu) {
287:         KSPDestroy(&mglevels[i]->smoothd);
288:       }
289:       KSPDestroy(&mglevels[i]->smoothu);
290:       PetscFree(mglevels[i]);
291:     }
292:     PetscFree(mg->levels);
293:   }
294:   PetscFree(pc->data);
295:   return(0);
296: }



300: extern PetscErrorCode PCMGACycle_Private(PC,PC_MG_Levels**);
301: extern PetscErrorCode PCMGFCycle_Private(PC,PC_MG_Levels**);
302: extern PetscErrorCode PCMGKCycle_Private(PC,PC_MG_Levels**);

304: /*
305:    PCApply_MG - Runs either an additive, multiplicative, Kaskadic
306:              or full cycle of multigrid.

308:   Note:
309:   A simple wrapper which calls PCMGMCycle(),PCMGACycle(), or PCMGFCycle().
310: */
311: static PetscErrorCode PCApply_MG(PC pc,Vec b,Vec x)
312: {
313:   PC_MG          *mg        = (PC_MG*)pc->data;
314:   PC_MG_Levels   **mglevels = mg->levels;
316:   PetscInt       levels = mglevels[0]->levels,i;

319:   if (mg->stageApply) {PetscLogStagePush(mg->stageApply);}
320:   /* When the DM is supplying the matrix then it will not exist until here */
321:   for (i=0; i<levels; i++) {
322:     if (!mglevels[i]->A) {
323:       KSPGetOperators(mglevels[i]->smoothu,&mglevels[i]->A,NULL);
324:       PetscObjectReference((PetscObject)mglevels[i]->A);
325:     }
326:   }

328:   mglevels[levels-1]->b = b;
329:   mglevels[levels-1]->x = x;
330:   if (mg->am == PC_MG_MULTIPLICATIVE) {
331:     VecSet(x,0.0);
332:     for (i=0; i<mg->cyclesperpcapply; i++) {
333:       PCMGMCycle_Private(pc,mglevels+levels-1,NULL);
334:     }
335:   } else if (mg->am == PC_MG_ADDITIVE) {
336:     PCMGACycle_Private(pc,mglevels);
337:   } else if (mg->am == PC_MG_KASKADE) {
338:     PCMGKCycle_Private(pc,mglevels);
339:   } else {
340:     PCMGFCycle_Private(pc,mglevels);
341:   }
342:   if (mg->stageApply) {PetscLogStagePop();}
343:   return(0);
344: }


347: PetscErrorCode PCSetFromOptions_MG(PetscOptionItems *PetscOptionsObject,PC pc)
348: {
349:   PetscErrorCode   ierr;
350:   PetscInt         m,levels,cycles;
351:   PetscBool        flg;
352:   PC_MG            *mg = (PC_MG*)pc->data;
353:   PC_MG_Levels     **mglevels;
354:   PCMGType         mgtype;
355:   PCMGCycleType    mgctype;
356:   PCMGGalerkinType gtype;

359:   levels = PetscMax(mg->nlevels,1);
360:   PetscOptionsHead(PetscOptionsObject,"Multigrid options");
361:   PetscOptionsInt("-pc_mg_levels","Number of Levels","PCMGSetLevels",levels,&levels,&flg);
362:   if (!flg && !mg->levels && pc->dm) {
363:     DMGetRefineLevel(pc->dm,&levels);
364:     levels++;
365:     mg->usedmfornumberoflevels = PETSC_TRUE;
366:   }
367:   PCMGSetLevels(pc,levels,NULL);
368:   mglevels = mg->levels;

370:   mgctype = (PCMGCycleType) mglevels[0]->cycles;
371:   PetscOptionsEnum("-pc_mg_cycle_type","V cycle or for W-cycle","PCMGSetCycleType",PCMGCycleTypes,(PetscEnum)mgctype,(PetscEnum*)&mgctype,&flg);
372:   if (flg) {
373:     PCMGSetCycleType(pc,mgctype);
374:   }
375:   gtype = mg->galerkin;
376:   PetscOptionsEnum("-pc_mg_galerkin","Use Galerkin process to compute coarser operators","PCMGSetGalerkin",PCMGGalerkinTypes,(PetscEnum)gtype,(PetscEnum*)&gtype,&flg);
377:   if (flg) {
378:     PCMGSetGalerkin(pc,gtype);
379:   }
380:   PetscOptionsInt("-pc_mg_smoothup","Number of post-smoothing steps","PCMGSetNumberSmoothUp",mg->default_smoothu,&m,&flg);
381:   if (flg) {
382:     PCMGSetNumberSmoothUp(pc,m);
383:   }
384:   PetscOptionsInt("-pc_mg_smoothdown","Number of pre-smoothing steps","PCMGSetNumberSmoothDown",mg->default_smoothd,&m,&flg);
385:   if (flg) {
386:     PCMGSetNumberSmoothDown(pc,m);
387:   }
388:   mgtype = mg->am;
389:   PetscOptionsEnum("-pc_mg_type","Multigrid type","PCMGSetType",PCMGTypes,(PetscEnum)mgtype,(PetscEnum*)&mgtype,&flg);
390:   if (flg) {
391:     PCMGSetType(pc,mgtype);
392:   }
393:   if (mg->am == PC_MG_MULTIPLICATIVE) {
394:     PetscOptionsInt("-pc_mg_multiplicative_cycles","Number of cycles for each preconditioner step","PCMGMultiplicativeSetCycles",mg->cyclesperpcapply,&cycles,&flg);
395:     if (flg) {
396:       PCMGMultiplicativeSetCycles(pc,cycles);
397:     }
398:   }
399:   flg  = PETSC_FALSE;
400:   PetscOptionsBool("-pc_mg_log","Log times for each multigrid level","None",flg,&flg,NULL);
401:   if (flg) {
402:     PetscInt i;
403:     char     eventname[128];

405:     levels = mglevels[0]->levels;
406:     for (i=0; i<levels; i++) {
407:       sprintf(eventname,"MGSetup Level %d",(int)i);
408:       PetscLogEventRegister(eventname,((PetscObject)pc)->classid,&mglevels[i]->eventsmoothsetup);
409:       sprintf(eventname,"MGSmooth Level %d",(int)i);
410:       PetscLogEventRegister(eventname,((PetscObject)pc)->classid,&mglevels[i]->eventsmoothsolve);
411:       if (i) {
412:         sprintf(eventname,"MGResid Level %d",(int)i);
413:         PetscLogEventRegister(eventname,((PetscObject)pc)->classid,&mglevels[i]->eventresidual);
414:         sprintf(eventname,"MGInterp Level %d",(int)i);
415:         PetscLogEventRegister(eventname,((PetscObject)pc)->classid,&mglevels[i]->eventinterprestrict);
416:       }
417:     }

419: #if defined(PETSC_USE_LOG)
420:     {
421:       const char    *sname = "MG Apply";
422:       PetscStageLog stageLog;
423:       PetscInt      st;

426:       PetscLogGetStageLog(&stageLog);
427:       for (st = 0; st < stageLog->numStages; ++st) {
428:         PetscBool same;

430:         PetscStrcmp(stageLog->stageInfo[st].name, sname, &same);
431:         if (same) mg->stageApply = st;
432:       }
433:       if (!mg->stageApply) {
434:         PetscLogStageRegister(sname, &mg->stageApply);
435:       }
436:     }
437: #endif
438:   }
439:   PetscOptionsTail();
440:   return(0);
441: }

443: const char *const PCMGTypes[] = {"MULTIPLICATIVE","ADDITIVE","FULL","KASKADE","PCMGType","PC_MG",0};
444: const char *const PCMGCycleTypes[] = {"invalid","v","w","PCMGCycleType","PC_MG_CYCLE",0};
445: const char *const PCMGGalerkinTypes[] = {"both","pmat","mat","none","external","PCMGGalerkinType","PC_MG_GALERKIN",0};

447:  #include <petscdraw.h>
448: PetscErrorCode PCView_MG(PC pc,PetscViewer viewer)
449: {
450:   PC_MG          *mg        = (PC_MG*)pc->data;
451:   PC_MG_Levels   **mglevels = mg->levels;
453:   PetscInt       levels = mglevels ? mglevels[0]->levels : 0,i;
454:   PetscBool      iascii,isbinary,isdraw;

457:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
458:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
459:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
460:   if (iascii) {
461:     const char *cyclename = levels ? (mglevels[0]->cycles == PC_MG_CYCLE_V ? "v" : "w") : "unknown";
462:     PetscViewerASCIIPrintf(viewer,"  type is %s, levels=%D cycles=%s\n", PCMGTypes[mg->am],levels,cyclename);
463:     if (mg->am == PC_MG_MULTIPLICATIVE) {
464:       PetscViewerASCIIPrintf(viewer,"    Cycles per PCApply=%d\n",mg->cyclesperpcapply);
465:     }
466:     if (mg->galerkin == PC_MG_GALERKIN_BOTH) {
467:       PetscViewerASCIIPrintf(viewer,"    Using Galerkin computed coarse grid matrices\n");
468:     } else if (mg->galerkin == PC_MG_GALERKIN_PMAT) {
469:       PetscViewerASCIIPrintf(viewer,"    Using Galerkin computed coarse grid matrices for pmat\n");
470:     } else if (mg->galerkin == PC_MG_GALERKIN_MAT) {
471:       PetscViewerASCIIPrintf(viewer,"    Using Galerkin computed coarse grid matrices for mat\n");
472:     } else if (mg->galerkin == PC_MG_GALERKIN_EXTERNAL) {
473:       PetscViewerASCIIPrintf(viewer,"    Using externally compute Galerkin coarse grid matrices\n");
474:     } else {
475:       PetscViewerASCIIPrintf(viewer,"    Not using Galerkin computed coarse grid matrices\n");
476:     }
477:     if (mg->view){
478:       (*mg->view)(pc,viewer);
479:     }
480:     for (i=0; i<levels; i++) {
481:       if (!i) {
482:         PetscViewerASCIIPrintf(viewer,"Coarse grid solver -- level -------------------------------\n",i);
483:       } else {
484:         PetscViewerASCIIPrintf(viewer,"Down solver (pre-smoother) on level %D -------------------------------\n",i);
485:       }
486:       PetscViewerASCIIPushTab(viewer);
487:       KSPView(mglevels[i]->smoothd,viewer);
488:       PetscViewerASCIIPopTab(viewer);
489:       if (i && mglevels[i]->smoothd == mglevels[i]->smoothu) {
490:         PetscViewerASCIIPrintf(viewer,"Up solver (post-smoother) same as down solver (pre-smoother)\n");
491:       } else if (i) {
492:         PetscViewerASCIIPrintf(viewer,"Up solver (post-smoother) on level %D -------------------------------\n",i);
493:         PetscViewerASCIIPushTab(viewer);
494:         KSPView(mglevels[i]->smoothu,viewer);
495:         PetscViewerASCIIPopTab(viewer);
496:       }
497:     }
498:   } else if (isbinary) {
499:     for (i=levels-1; i>=0; i--) {
500:       KSPView(mglevels[i]->smoothd,viewer);
501:       if (i && mglevels[i]->smoothd != mglevels[i]->smoothu) {
502:         KSPView(mglevels[i]->smoothu,viewer);
503:       }
504:     }
505:   } else if (isdraw) {
506:     PetscDraw draw;
507:     PetscReal x,w,y,bottom,th;
508:     PetscViewerDrawGetDraw(viewer,0,&draw);
509:     PetscDrawGetCurrentPoint(draw,&x,&y);
510:     PetscDrawStringGetSize(draw,NULL,&th);
511:     bottom = y - th;
512:     for (i=levels-1; i>=0; i--) {
513:       if (!mglevels[i]->smoothu || (mglevels[i]->smoothu == mglevels[i]->smoothd)) {
514:         PetscDrawPushCurrentPoint(draw,x,bottom);
515:         KSPView(mglevels[i]->smoothd,viewer);
516:         PetscDrawPopCurrentPoint(draw);
517:       } else {
518:         w    = 0.5*PetscMin(1.0-x,x);
519:         PetscDrawPushCurrentPoint(draw,x+w,bottom);
520:         KSPView(mglevels[i]->smoothd,viewer);
521:         PetscDrawPopCurrentPoint(draw);
522:         PetscDrawPushCurrentPoint(draw,x-w,bottom);
523:         KSPView(mglevels[i]->smoothu,viewer);
524:         PetscDrawPopCurrentPoint(draw);
525:       }
526:       PetscDrawGetBoundingBox(draw,NULL,&bottom,NULL,NULL);
527:       bottom -= th;
528:     }
529:   }
530:   return(0);
531: }

533:  #include <petsc/private/dmimpl.h>
534:  #include <petsc/private/kspimpl.h>

536: /*
537:     Calls setup for the KSP on each level
538: */
539: PetscErrorCode PCSetUp_MG(PC pc)
540: {
541:   PC_MG          *mg        = (PC_MG*)pc->data;
542:   PC_MG_Levels   **mglevels = mg->levels;
544:   PetscInt       i,n;
545:   PC             cpc;
546:   PetscBool      dump = PETSC_FALSE,opsset,use_amat,missinginterpolate = PETSC_FALSE;
547:   Mat            dA,dB;
548:   Vec            tvec;
549:   DM             *dms;
550:   PetscViewer    viewer = 0;
551:   PetscBool      dAeqdB = PETSC_FALSE, needRestricts = PETSC_FALSE;

554:   if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels with PCMGSetLevels() before setting up");
555:   n = mglevels[0]->levels;
556:   /* FIX: Move this to PCSetFromOptions_MG? */
557:   if (mg->usedmfornumberoflevels) {
558:     PetscInt levels;
559:     DMGetRefineLevel(pc->dm,&levels);
560:     levels++;
561:     if (levels > n) { /* the problem is now being solved on a finer grid */
562:       PCMGSetLevels(pc,levels,NULL);
563:       n        = levels;
564:       PCSetFromOptions(pc); /* it is bad to call this here, but otherwise will never be called for the new hierarchy */
565:       mglevels =  mg->levels;
566:     }
567:   }
568:   KSPGetPC(mglevels[0]->smoothd,&cpc);


571:   /* If user did not provide fine grid operators OR operator was not updated since last global KSPSetOperators() */
572:   /* so use those from global PC */
573:   /* Is this what we always want? What if user wants to keep old one? */
574:   KSPGetOperatorsSet(mglevels[n-1]->smoothd,NULL,&opsset);
575:   if (opsset) {
576:     Mat mmat;
577:     KSPGetOperators(mglevels[n-1]->smoothd,NULL,&mmat);
578:     if (mmat == pc->pmat) opsset = PETSC_FALSE;
579:   }

581:   if (!opsset) {
582:     PCGetUseAmat(pc,&use_amat);
583:     if(use_amat){
584:       PetscInfo(pc,"Using outer operators to define finest grid operator \n  because PCMGGetSmoother(pc,nlevels-1,&ksp);KSPSetOperators(ksp,...); was not called.\n");
585:       KSPSetOperators(mglevels[n-1]->smoothd,pc->mat,pc->pmat);
586:     }
587:     else {
588:       PetscInfo(pc,"Using matrix (pmat) operators to define finest grid operator \n  because PCMGGetSmoother(pc,nlevels-1,&ksp);KSPSetOperators(ksp,...); was not called.\n");
589:       KSPSetOperators(mglevels[n-1]->smoothd,pc->pmat,pc->pmat);
590:     }
591:   }

593:   for (i=n-1; i>0; i--) {
594:     if (!(mglevels[i]->interpolate || mglevels[i]->restrct)) {
595:       missinginterpolate = PETSC_TRUE;
596:       continue;
597:     }
598:   }

600:   KSPGetOperators(mglevels[n-1]->smoothd,&dA,&dB);
601:   if (dA == dB) dAeqdB = PETSC_TRUE;
602:   if ((mg->galerkin == PC_MG_GALERKIN_NONE) || (((mg->galerkin == PC_MG_GALERKIN_PMAT) || (mg->galerkin == PC_MG_GALERKIN_MAT)) && !dAeqdB)) {
603:     needRestricts = PETSC_TRUE;  /* user must compute either mat, pmat, or both so must restrict x to coarser levels */
604:   }


607:   /*
608:    Skipping if user has provided all interpolation/restriction needed (since DM might not be able to produce them (when coming from SNES/TS)
609:    Skipping for galerkin==2 (externally managed hierarchy such as ML and GAMG). Cleaner logic here would be great. Wrap ML/GAMG as DMs?
610:   */
611:   if (missinginterpolate && pc->dm && mg->galerkin != PC_MG_GALERKIN_EXTERNAL && !pc->setupcalled) {
612:     /* construct the interpolation from the DMs */
613:     Mat p;
614:     Vec rscale;
615:     PetscMalloc1(n,&dms);
616:     dms[n-1] = pc->dm;
617:     /* Separately create them so we do not get DMKSP interference between levels */
618:     for (i=n-2; i>-1; i--) {DMCoarsen(dms[i+1],MPI_COMM_NULL,&dms[i]);}
619:     for (i=n-2; i>-1; i--) {
620:       DMKSP     kdm;
621:       PetscBool dmhasrestrict;
622:       KSPSetDM(mglevels[i]->smoothd,dms[i]);
623:       if (!needRestricts) {KSPSetDMActive(mglevels[i]->smoothd,PETSC_FALSE);}
624:       DMGetDMKSPWrite(dms[i],&kdm);
625:       /* Ugly hack so that the next KSPSetUp() will use the RHS that we set. A better fix is to change dmActive to take
626:        * a bitwise OR of computing the matrix, RHS, and initial iterate. */
627:       kdm->ops->computerhs = NULL;
628:       kdm->rhsctx          = NULL;
629:       if (!mglevels[i+1]->interpolate) {
630:         DMCreateInterpolation(dms[i],dms[i+1],&p,&rscale);
631:         PCMGSetInterpolation(pc,i+1,p);
632:         if (rscale) {PCMGSetRScale(pc,i+1,rscale);}
633:         VecDestroy(&rscale);
634:         MatDestroy(&p);
635:       }
636:       DMHasCreateRestriction(dms[i],&dmhasrestrict);
637:       if (dmhasrestrict && !mglevels[i+1]->restrct){
638:         DMCreateRestriction(dms[i],dms[i+1],&p);
639:         PCMGSetRestriction(pc,i+1,p);
640:         MatDestroy(&p);
641:       }
642:     }

644:     for (i=n-2; i>-1; i--) {DMDestroy(&dms[i]);}
645:     PetscFree(dms);
646:   }

648:   if (pc->dm && !pc->setupcalled) {
649:     /* finest smoother also gets DM but it is not active, independent of whether galerkin==PC_MG_GALERKIN_EXTERNAL */
650:     KSPSetDM(mglevels[n-1]->smoothd,pc->dm);
651:     KSPSetDMActive(mglevels[n-1]->smoothd,PETSC_FALSE);
652:   }

654:   if (mg->galerkin < PC_MG_GALERKIN_NONE) {
655:     Mat       A,B;
656:     PetscBool doA = PETSC_FALSE,doB = PETSC_FALSE;
657:     MatReuse  reuse = MAT_INITIAL_MATRIX;

659:     if ((mg->galerkin == PC_MG_GALERKIN_PMAT) || (mg->galerkin == PC_MG_GALERKIN_BOTH)) doB = PETSC_TRUE;
660:     if ((mg->galerkin == PC_MG_GALERKIN_MAT) || ((mg->galerkin == PC_MG_GALERKIN_BOTH) && (dA != dB))) doA = PETSC_TRUE;
661:     if (pc->setupcalled) reuse = MAT_REUSE_MATRIX;
662:     for (i=n-2; i>-1; i--) {
663:       if (!mglevels[i+1]->restrct && !mglevels[i+1]->interpolate) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must provide interpolation or restriction for each MG level except level 0");
664:       if (!mglevels[i+1]->interpolate) {
665:         PCMGSetInterpolation(pc,i+1,mglevels[i+1]->restrct);
666:       }
667:       if (!mglevels[i+1]->restrct) {
668:         PCMGSetRestriction(pc,i+1,mglevels[i+1]->interpolate);
669:       }
670:       if (reuse == MAT_REUSE_MATRIX) {
671:         KSPGetOperators(mglevels[i]->smoothd,&A,&B);
672:       }
673:       if (doA) {
674:         MatGalerkin(mglevels[i+1]->restrct,dA,mglevels[i+1]->interpolate,reuse,1.0,&A);
675:       }
676:       if (doB) {
677:         MatGalerkin(mglevels[i+1]->restrct,dB,mglevels[i+1]->interpolate,reuse,1.0,&B);
678:       }
679:       /* the management of the PetscObjectReference() and PetscObjecDereference() below is rather delicate */
680:       if (!doA && dAeqdB) {
681:         if (reuse == MAT_INITIAL_MATRIX) {PetscObjectReference((PetscObject)B);}
682:         A = B;
683:       } else if (!doA && reuse == MAT_INITIAL_MATRIX ) {
684:         KSPGetOperators(mglevels[i]->smoothd,&A,NULL);
685:         PetscObjectReference((PetscObject)A);
686:       }
687:       if (!doB && dAeqdB) {
688:         if (reuse == MAT_INITIAL_MATRIX) {PetscObjectReference((PetscObject)A);}
689:         B = A;
690:       } else if (!doB && reuse == MAT_INITIAL_MATRIX) {
691:         KSPGetOperators(mglevels[i]->smoothd,NULL,&B);
692:         PetscObjectReference((PetscObject)B);
693:       }
694:       if (reuse == MAT_INITIAL_MATRIX) {
695:         KSPSetOperators(mglevels[i]->smoothd,A,B);
696:         PetscObjectDereference((PetscObject)A);
697:         PetscObjectDereference((PetscObject)B);
698:       }
699:       dA = A;
700:       dB = B;
701:     }
702:   }
703:   if (needRestricts && pc->dm && pc->dm->x) {
704:     /* need to restrict Jacobian location to coarser meshes for evaluation */
705:     for (i=n-2; i>-1; i--) {
706:       Mat R;
707:       Vec rscale;
708:       if (!mglevels[i]->smoothd->dm->x) {
709:         Vec *vecs;
710:         KSPCreateVecs(mglevels[i]->smoothd,1,&vecs,0,NULL);
711:         mglevels[i]->smoothd->dm->x = vecs[0];
712:         PetscFree(vecs);
713:       }
714:       PCMGGetRestriction(pc,i+1,&R);
715:       PCMGGetRScale(pc,i+1,&rscale);
716:       MatRestrict(R,mglevels[i+1]->smoothd->dm->x,mglevels[i]->smoothd->dm->x);
717:       VecPointwiseMult(mglevels[i]->smoothd->dm->x,mglevels[i]->smoothd->dm->x,rscale);
718:     }
719:   }
720:   if (needRestricts && pc->dm) {
721:     for (i=n-2; i>=0; i--) {
722:       DM  dmfine,dmcoarse;
723:       Mat Restrict,Inject;
724:       Vec rscale;
725:       KSPGetDM(mglevels[i+1]->smoothd,&dmfine);
726:       KSPGetDM(mglevels[i]->smoothd,&dmcoarse);
727:       PCMGGetRestriction(pc,i+1,&Restrict);
728:       PCMGGetRScale(pc,i+1,&rscale);
729:       Inject = NULL;      /* Callback should create it if it needs Injection */
730:       DMRestrict(dmfine,Restrict,rscale,Inject,dmcoarse);
731:     }
732:   }

734:   if (!pc->setupcalled) {
735:     for (i=0; i<n; i++) {
736:       KSPSetFromOptions(mglevels[i]->smoothd);
737:     }
738:     for (i=1; i<n; i++) {
739:       if (mglevels[i]->smoothu && (mglevels[i]->smoothu != mglevels[i]->smoothd)) {
740:         KSPSetFromOptions(mglevels[i]->smoothu);
741:       }
742:     }
743:     /* insure that if either interpolation or restriction is set the other other one is set */
744:     for (i=1; i<n; i++) {
745:       PCMGGetInterpolation(pc,i,NULL);
746:       PCMGGetRestriction(pc,i,NULL);
747:     }
748:     for (i=0; i<n-1; i++) {
749:       if (!mglevels[i]->b) {
750:         Vec *vec;
751:         KSPCreateVecs(mglevels[i]->smoothd,1,&vec,0,NULL);
752:         PCMGSetRhs(pc,i,*vec);
753:         VecDestroy(vec);
754:         PetscFree(vec);
755:       }
756:       if (!mglevels[i]->r && i) {
757:         VecDuplicate(mglevels[i]->b,&tvec);
758:         PCMGSetR(pc,i,tvec);
759:         VecDestroy(&tvec);
760:       }
761:       if (!mglevels[i]->x) {
762:         VecDuplicate(mglevels[i]->b,&tvec);
763:         PCMGSetX(pc,i,tvec);
764:         VecDestroy(&tvec);
765:       }
766:     }
767:     if (n != 1 && !mglevels[n-1]->r) {
768:       /* PCMGSetR() on the finest level if user did not supply it */
769:       Vec *vec;
770:       KSPCreateVecs(mglevels[n-1]->smoothd,1,&vec,0,NULL);
771:       PCMGSetR(pc,n-1,*vec);
772:       VecDestroy(vec);
773:       PetscFree(vec);
774:     }
775:   }

777:   if (pc->dm) {
778:     /* need to tell all the coarser levels to rebuild the matrix using the DM for that level */
779:     for (i=0; i<n-1; i++) {
780:       if (mglevels[i]->smoothd->setupstage != KSP_SETUP_NEW) mglevels[i]->smoothd->setupstage = KSP_SETUP_NEWMATRIX;
781:     }
782:   }

784:   for (i=1; i<n; i++) {
785:     if (mglevels[i]->smoothu == mglevels[i]->smoothd || mg->am == PC_MG_FULL || mg->am == PC_MG_KASKADE || mg->cyclesperpcapply > 1){
786:       /* if doing only down then initial guess is zero */
787:       KSPSetInitialGuessNonzero(mglevels[i]->smoothd,PETSC_TRUE);
788:     }
789:     if (mglevels[i]->eventsmoothsetup) {PetscLogEventBegin(mglevels[i]->eventsmoothsetup,0,0,0,0);}
790:     KSPSetUp(mglevels[i]->smoothd);
791:     if (mglevels[i]->smoothd->reason == KSP_DIVERGED_PCSETUP_FAILED) {
792:       pc->failedreason = PC_SUBPC_ERROR;
793:     }
794:     if (mglevels[i]->eventsmoothsetup) {PetscLogEventEnd(mglevels[i]->eventsmoothsetup,0,0,0,0);}
795:     if (!mglevels[i]->residual) {
796:       Mat mat;
797:       KSPGetOperators(mglevels[i]->smoothd,&mat,NULL);
798:       PCMGSetResidual(pc,i,PCMGResidualDefault,mat);
799:     }
800:   }
801:   for (i=1; i<n; i++) {
802:     if (mglevels[i]->smoothu && mglevels[i]->smoothu != mglevels[i]->smoothd) {
803:       Mat downmat,downpmat;

805:       /* check if operators have been set for up, if not use down operators to set them */
806:       KSPGetOperatorsSet(mglevels[i]->smoothu,&opsset,NULL);
807:       if (!opsset) {
808:         KSPGetOperators(mglevels[i]->smoothd,&downmat,&downpmat);
809:         KSPSetOperators(mglevels[i]->smoothu,downmat,downpmat);
810:       }

812:       KSPSetInitialGuessNonzero(mglevels[i]->smoothu,PETSC_TRUE);
813:       if (mglevels[i]->eventsmoothsetup) {PetscLogEventBegin(mglevels[i]->eventsmoothsetup,0,0,0,0);}
814:       KSPSetUp(mglevels[i]->smoothu);
815:       if (mglevels[i]->smoothu->reason == KSP_DIVERGED_PCSETUP_FAILED) {
816:         pc->failedreason = PC_SUBPC_ERROR;
817:       }
818:       if (mglevels[i]->eventsmoothsetup) {PetscLogEventEnd(mglevels[i]->eventsmoothsetup,0,0,0,0);}
819:     }
820:   }

822:   if (mglevels[0]->eventsmoothsetup) {PetscLogEventBegin(mglevels[0]->eventsmoothsetup,0,0,0,0);}
823:   KSPSetUp(mglevels[0]->smoothd);
824:   if (mglevels[0]->smoothd->reason == KSP_DIVERGED_PCSETUP_FAILED) {
825:     pc->failedreason = PC_SUBPC_ERROR;
826:   }
827:   if (mglevels[0]->eventsmoothsetup) {PetscLogEventEnd(mglevels[0]->eventsmoothsetup,0,0,0,0);}

829:   /*
830:      Dump the interpolation/restriction matrices plus the
831:    Jacobian/stiffness on each level. This allows MATLAB users to
832:    easily check if the Galerkin condition A_c = R A_f R^T is satisfied.

834:    Only support one or the other at the same time.
835:   */
836: #if defined(PETSC_USE_SOCKET_VIEWER)
837:   PetscOptionsGetBool(((PetscObject)pc)->options,((PetscObject)pc)->prefix,"-pc_mg_dump_matlab",&dump,NULL);
838:   if (dump) viewer = PETSC_VIEWER_SOCKET_(PetscObjectComm((PetscObject)pc));
839:   dump = PETSC_FALSE;
840: #endif
841:   PetscOptionsGetBool(((PetscObject)pc)->options,((PetscObject)pc)->prefix,"-pc_mg_dump_binary",&dump,NULL);
842:   if (dump) viewer = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)pc));

844:   if (viewer) {
845:     for (i=1; i<n; i++) {
846:       MatView(mglevels[i]->restrct,viewer);
847:     }
848:     for (i=0; i<n; i++) {
849:       KSPGetPC(mglevels[i]->smoothd,&pc);
850:       MatView(pc->mat,viewer);
851:     }
852:   }
853:   return(0);
854: }

856: /* -------------------------------------------------------------------------------------*/

858: /*@
859:    PCMGGetLevels - Gets the number of levels to use with MG.

861:    Not Collective

863:    Input Parameter:
864: .  pc - the preconditioner context

866:    Output parameter:
867: .  levels - the number of levels

869:    Level: advanced

871: .keywords: MG, get, levels, multigrid

873: .seealso: PCMGSetLevels()
874: @*/
875: PetscErrorCode  PCMGGetLevels(PC pc,PetscInt *levels)
876: {
877:   PC_MG *mg = (PC_MG*)pc->data;

882:   *levels = mg->nlevels;
883:   return(0);
884: }

886: /*@
887:    PCMGSetType - Determines the form of multigrid to use:
888:    multiplicative, additive, full, or the Kaskade algorithm.

890:    Logically Collective on PC

892:    Input Parameters:
893: +  pc - the preconditioner context
894: -  form - multigrid form, one of PC_MG_MULTIPLICATIVE, PC_MG_ADDITIVE,
895:    PC_MG_FULL, PC_MG_KASKADE

897:    Options Database Key:
898: .  -pc_mg_type <form> - Sets <form>, one of multiplicative,
899:    additive, full, kaskade

901:    Level: advanced

903: .keywords: MG, set, method, multiplicative, additive, full, Kaskade, multigrid

905: .seealso: PCMGSetLevels()
906: @*/
907: PetscErrorCode  PCMGSetType(PC pc,PCMGType form)
908: {
909:   PC_MG *mg = (PC_MG*)pc->data;

914:   mg->am = form;
915:   if (form == PC_MG_MULTIPLICATIVE) pc->ops->applyrichardson = PCApplyRichardson_MG;
916:   else pc->ops->applyrichardson = NULL;
917:   return(0);
918: }

920: /*@
921:    PCMGGetType - Determines the form of multigrid to use:
922:    multiplicative, additive, full, or the Kaskade algorithm.

924:    Logically Collective on PC

926:    Input Parameter:
927: .  pc - the preconditioner context

929:    Output Parameter:
930: .  type - one of PC_MG_MULTIPLICATIVE, PC_MG_ADDITIVE,PC_MG_FULL, PC_MG_KASKADE


933:    Level: advanced

935: .keywords: MG, set, method, multiplicative, additive, full, Kaskade, multigrid

937: .seealso: PCMGSetLevels()
938: @*/
939: PetscErrorCode  PCMGGetType(PC pc,PCMGType *type)
940: {
941:   PC_MG *mg = (PC_MG*)pc->data;

945:   *type = mg->am;
946:   return(0);
947: }

949: /*@
950:    PCMGSetCycleType - Sets the type cycles to use.  Use PCMGSetCycleTypeOnLevel() for more
951:    complicated cycling.

953:    Logically Collective on PC

955:    Input Parameters:
956: +  pc - the multigrid context
957: -  n - either PC_MG_CYCLE_V or PC_MG_CYCLE_W

959:    Options Database Key:
960: .  -pc_mg_cycle_type <v,w> - provide the cycle desired

962:    Level: advanced

964: .keywords: MG, set, cycles, V-cycle, W-cycle, multigrid

966: .seealso: PCMGSetCycleTypeOnLevel()
967: @*/
968: PetscErrorCode  PCMGSetCycleType(PC pc,PCMGCycleType n)
969: {
970:   PC_MG        *mg        = (PC_MG*)pc->data;
971:   PC_MG_Levels **mglevels = mg->levels;
972:   PetscInt     i,levels;

977:   if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ORDER,"Must set MG levels with PCMGSetLevels() before calling");
978:   levels = mglevels[0]->levels;
979:   for (i=0; i<levels; i++) mglevels[i]->cycles = n;
980:   return(0);
981: }

983: /*@
984:    PCMGMultiplicativeSetCycles - Sets the number of cycles to use for each preconditioner step
985:          of multigrid when PCMGType of PC_MG_MULTIPLICATIVE is used

987:    Logically Collective on PC

989:    Input Parameters:
990: +  pc - the multigrid context
991: -  n - number of cycles (default is 1)

993:    Options Database Key:
994: .  -pc_mg_multiplicative_cycles n

996:    Level: advanced

998:    Notes: This is not associated with setting a v or w cycle, that is set with PCMGSetCycleType()

1000: .keywords: MG, set, cycles, V-cycle, W-cycle, multigrid

1002: .seealso: PCMGSetCycleTypeOnLevel(), PCMGSetCycleType()
1003: @*/
1004: PetscErrorCode  PCMGMultiplicativeSetCycles(PC pc,PetscInt n)
1005: {
1006:   PC_MG        *mg        = (PC_MG*)pc->data;

1011:   mg->cyclesperpcapply = n;
1012:   return(0);
1013: }

1015: PetscErrorCode PCMGSetGalerkin_MG(PC pc,PCMGGalerkinType use)
1016: {
1017:   PC_MG *mg = (PC_MG*)pc->data;

1020:   mg->galerkin = use;
1021:   return(0);
1022: }

1024: /*@
1025:    PCMGSetGalerkin - Causes the coarser grid matrices to be computed from the
1026:       finest grid via the Galerkin process: A_i-1 = r_i * A_i * p_i

1028:    Logically Collective on PC

1030:    Input Parameters:
1031: +  pc - the multigrid context
1032: -  use - one of PC_MG_GALERKIN_BOTH,PC_MG_GALERKIN_PMAT,PC_MG_GALERKIN_MAT, or PC_MG_GALERKIN_NONE

1034:    Options Database Key:
1035: .  -pc_mg_galerkin <both,pmat,mat,none>

1037:    Level: intermediate

1039:    Notes: Some codes that use PCMG such as PCGAMG use Galerkin internally while constructing the hierarchy and thus do not
1040:      use the PCMG construction of the coarser grids.

1042: .keywords: MG, set, Galerkin

1044: .seealso: PCMGGetGalerkin(), PCMGGalerkinType

1046: @*/
1047: PetscErrorCode PCMGSetGalerkin(PC pc,PCMGGalerkinType use)
1048: {

1053:   PetscTryMethod(pc,"PCMGSetGalerkin_C",(PC,PCMGGalerkinType),(pc,use));
1054:   return(0);
1055: }

1057: /*@
1058:    PCMGGetGalerkin - Checks if Galerkin multigrid is being used, i.e.
1059:       A_i-1 = r_i * A_i * p_i

1061:    Not Collective

1063:    Input Parameter:
1064: .  pc - the multigrid context

1066:    Output Parameter:
1067: .  galerkin - one of PC_MG_GALERKIN_BOTH,PC_MG_GALERKIN_PMAT,PC_MG_GALERKIN_MAT, PC_MG_GALERKIN_NONE, or PC_MG_GALERKIN_EXTERNAL

1069:    Level: intermediate

1071: .keywords: MG, set, Galerkin

1073: .seealso: PCMGSetGalerkin(), PCMGGalerkinType

1075: @*/
1076: PetscErrorCode  PCMGGetGalerkin(PC pc,PCMGGalerkinType  *galerkin)
1077: {
1078:   PC_MG *mg = (PC_MG*)pc->data;

1082:   *galerkin = mg->galerkin;
1083:   return(0);
1084: }

1086: /*@
1087:    PCMGSetNumberSmoothDown - Sets the number of pre-smoothing steps to
1088:    use on all levels. Use PCMGGetSmootherDown() to set different
1089:    pre-smoothing steps on different levels.

1091:    Logically Collective on PC

1093:    Input Parameters:
1094: +  mg - the multigrid context
1095: -  n - the number of smoothing steps

1097:    Options Database Key:
1098: .  -pc_mg_smoothdown <n> - Sets number of pre-smoothing steps

1100:    Level: advanced
1101:     If the number of smoothing steps is changed in this call then the PCMGGetSmoothUp() will be called and now the
1102:    up smoother will no longer share the same KSP object as the down smoother. Use PCMGSetNumberSmooth() to set the same
1103:    number of smoothing steps for pre and post smoothing.

1105: .keywords: MG, smooth, down, pre-smoothing, steps, multigrid

1107: .seealso: PCMGSetNumberSmoothUp(), PCMGSetNumberSmooth()
1108: @*/
1109: PetscErrorCode  PCMGSetNumberSmoothDown(PC pc,PetscInt n)
1110: {
1111:   PC_MG          *mg        = (PC_MG*)pc->data;
1112:   PC_MG_Levels   **mglevels = mg->levels;
1114:   PetscInt       i,levels;

1119:   if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ORDER,"Must set MG levels with PCMGSetLevels() before calling");
1120:   levels = mglevels[0]->levels;
1121:   for (i=1; i<levels; i++) {
1122:     PetscInt nc;
1123:     KSPGetTolerances(mglevels[i]->smoothd,NULL,NULL,NULL,&nc);
1124:     if (nc == n) continue;

1126:     /* make sure smoother up and down are different */
1127:     PCMGGetSmootherUp(pc,i,NULL);
1128:     KSPSetTolerances(mglevels[i]->smoothd,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,n);

1130:     mg->default_smoothd = n;
1131:   }
1132:   return(0);
1133: }

1135: /*@
1136:    PCMGSetNumberSmoothUp - Sets the number of post-smoothing steps to use
1137:    on all levels. Use PCMGGetSmootherUp() to set different numbers of
1138:    post-smoothing steps on different levels.

1140:    Logically Collective on PC

1142:    Input Parameters:
1143: +  mg - the multigrid context
1144: -  n - the number of smoothing steps

1146:    Options Database Key:
1147: .  -pc_mg_smoothup <n> - Sets number of post-smoothing steps

1149:    Level: advanced

1151:    Notes: this does not set a value on the coarsest grid, since we assume that
1152:     there is no separate smooth up on the coarsest grid.

1154:     If the number of smoothing steps is changed in this call then the PCMGGetSmoothUp() will be called and now the 
1155:    up smoother will no longer share the same KSP object as the down smoother. Use PCMGSetNumberSmooth() to set the same
1156:    number of smoothing steps for pre and post smoothing.


1159: .keywords: MG, smooth, up, post-smoothing, steps, multigrid

1161: .seealso: PCMGSetNumberSmoothDown(), PCMGSetNumberSmooth()
1162: @*/
1163: PetscErrorCode  PCMGSetNumberSmoothUp(PC pc,PetscInt n)
1164: {
1165:   PC_MG          *mg        = (PC_MG*)pc->data;
1166:   PC_MG_Levels   **mglevels = mg->levels;
1168:   PetscInt       i,levels;

1173:   if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ORDER,"Must set MG levels with PCMGSetLevels() before calling");
1174:   levels = mglevels[0]->levels;

1176:   for (i=1; i<levels; i++) {
1177:     if (mglevels[i]->smoothu == mglevels[i]->smoothd) {
1178:       PetscInt nc;
1179:       KSPGetTolerances(mglevels[i]->smoothd,NULL,NULL,NULL,&nc);
1180:       if (nc == n) continue;
1181:     }

1183:     /* make sure smoother up and down are different */
1184:     PCMGGetSmootherUp(pc,i,NULL);
1185:     KSPSetTolerances(mglevels[i]->smoothu,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,n);

1187:     mg->default_smoothu = n;
1188:   }
1189:   return(0);
1190: }

1192: /*@
1193:    PCMGSetNumberSmooth - Sets the number of pre and post-smoothing steps to use
1194:    on all levels. Use PCMGSetSmoothUp() and PCMGSetSmoothDown() set different numbers of
1195:    pre ad post-smoothing steps

1197:    Logically Collective on PC

1199:    Input Parameters:
1200: +  mg - the multigrid context
1201: -  n - the number of smoothing steps

1203:    Options Database Key:
1204: +  -mg_levels_ksp_max_it <n> - Sets number of pre and post-smoothing steps
1205: .  -pc_mg_smooth_down <n> - Sets number of pre-smoothing steps (if setting different pre and post amounts)
1206: -  -pc_mg_smooth_up <n> - Sets number of post-smoothing steps (if setting different pre and post amounts)

1208:    Level: advanced

1210:    Notes: this does not set a value on the coarsest grid, since we assume that
1211:     there is no separate smooth up on the coarsest grid.

1213: .keywords: MG, smooth, up, post-smoothing, steps, multigrid

1215: .seealso: PCMGSetNumberSmoothDown(), PCMGSetNumberSmoothUp()
1216: @*/
1217: PetscErrorCode  PCMGSetNumberSmooth(PC pc,PetscInt n)
1218: {
1219:   PC_MG          *mg        = (PC_MG*)pc->data;
1220:   PC_MG_Levels   **mglevels = mg->levels;
1222:   PetscInt       i,levels;

1227:   if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ORDER,"Must set MG levels with PCMGSetLevels() before calling");
1228:   levels = mglevels[0]->levels;

1230:   for (i=1; i<levels; i++) {
1231:     KSPSetTolerances(mglevels[i]->smoothu,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,n);
1232:     KSPSetTolerances(mglevels[i]->smoothd,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,n);
1233:     mg->default_smoothu = n;
1234:     mg->default_smoothd = n;
1235:   }
1236:   return(0);
1237: }

1239: /* ----------------------------------------------------------------------------------------*/

1241: /*MC
1242:    PCMG - Use multigrid preconditioning. This preconditioner requires you provide additional
1243:     information about the coarser grid matrices and restriction/interpolation operators.

1245:    Options Database Keys:
1246: +  -pc_mg_levels <nlevels> - number of levels including finest
1247: .  -pc_mg_cycle_type <v,w> - 
1248: .  -pc_mg_smoothup <n> - number of smoothing steps after interpolation
1249: .  -pc_mg_smoothdown <n> - number of smoothing steps before applying restriction operator
1250: .  -pc_mg_type <additive,multiplicative,full,kaskade> - multiplicative is the default
1251: .  -pc_mg_log - log information about time spent on each level of the solver
1252: .  -pc_mg_galerkin <both,pmat,mat,none> - use Galerkin process to compute coarser operators, i.e. Acoarse = R A R'
1253: .  -pc_mg_multiplicative_cycles - number of cycles to use as the preconditioner (defaults to 1)
1254: .  -pc_mg_dump_matlab - dumps the matrices for each level and the restriction/interpolation matrices
1255:                         to the Socket viewer for reading from MATLAB.
1256: -  -pc_mg_dump_binary - dumps the matrices for each level and the restriction/interpolation matrices
1257:                         to the binary output file called binaryoutput

1259:    Notes: If one uses a Krylov method such GMRES or CG as the smoother than one must use KSPFGMRES, KSPGCG, or KSPRICHARDSON as the outer Krylov method

1261:        When run with a single level the smoother options are used on that level NOT the coarse grid solver options

1263:        When run with KSPRICHARDSON the convergence test changes slightly if monitor is turned on. The iteration count may change slightly. This
1264:        is because without monitoring the residual norm is computed WITHIN each multigrid cycle on the finest level after the pre-smoothing
1265:        (because the residual has just been computed for the multigrid algorithm and is hence available for free) while with monitoring the
1266:        residual is computed at the end of each cycle.

1268:    Level: intermediate

1270:    Concepts: multigrid/multilevel

1272: .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC, PCMGType, PCEXOTIC, PCGAMG, PCML, PCHYPRE
1273:            PCMGSetLevels(), PCMGGetLevels(), PCMGSetType(), PCMGSetCycleType(), PCMGSetNumberSmoothDown(),
1274:            PCMGSetNumberSmoothUp(), PCMGGetCoarseSolve(), PCMGSetResidual(), PCMGSetInterpolation(),
1275:            PCMGSetRestriction(), PCMGGetSmoother(), PCMGGetSmootherUp(), PCMGGetSmootherDown(),
1276:            PCMGSetCycleTypeOnLevel(), PCMGSetRhs(), PCMGSetX(), PCMGSetR()
1277: M*/

1279: PETSC_EXTERN PetscErrorCode PCCreate_MG(PC pc)
1280: {
1281:   PC_MG          *mg;

1285:   PetscNewLog(pc,&mg);
1286:   pc->data     = (void*)mg;
1287:   mg->nlevels  = -1;
1288:   mg->am       = PC_MG_MULTIPLICATIVE;
1289:   mg->galerkin = PC_MG_GALERKIN_NONE;

1291:   pc->useAmat = PETSC_TRUE;

1293:   pc->ops->apply          = PCApply_MG;
1294:   pc->ops->setup          = PCSetUp_MG;
1295:   pc->ops->reset          = PCReset_MG;
1296:   pc->ops->destroy        = PCDestroy_MG;
1297:   pc->ops->setfromoptions = PCSetFromOptions_MG;
1298:   pc->ops->view           = PCView_MG;

1300:   PetscObjectComposeFunction((PetscObject)pc,"PCMGSetGalerkin_C",PCMGSetGalerkin_MG);
1301:   return(0);
1302: }