Actual source code: mg.c

petsc-master 2020-08-06
Report Typos and Errors

  2: /*
  3:     Defines the multigrid preconditioner interface.
  4: */
  5:  #include <petsc/private/pcmgimpl.h>
  6:  #include <petscdm.h>
  7: PETSC_INTERN PetscErrorCode PCPreSolveChangeRHS(PC,PetscBool*);

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

 17:   if (mglevels->eventsmoothsolve) {PetscLogEventBegin(mglevels->eventsmoothsolve,0,0,0,0);}
 18:   KSPSolve(mglevels->smoothd,mglevels->b,mglevels->x);  /* pre-smooth */
 19:   KSPCheckSolve(mglevels->smoothd,pc,mglevels->x);
 20:   if (mglevels->eventsmoothsolve) {PetscLogEventEnd(mglevels->eventsmoothsolve,0,0,0,0);}
 21:   if (mglevels->level) {  /* not the coarsest grid */
 22:     if (mglevels->eventresidual) {PetscLogEventBegin(mglevels->eventresidual,0,0,0,0);}
 23:     (*mglevels->residual)(mglevels->A,mglevels->b,mglevels->x,mglevels->r);
 24:     if (mglevels->eventresidual) {PetscLogEventEnd(mglevels->eventresidual,0,0,0,0);}

 26:     /* if on finest level and have convergence criteria set */
 27:     if (mglevels->level == mglevels->levels-1 && mg->ttol && reason) {
 28:       PetscReal rnorm;
 29:       VecNorm(mglevels->r,NORM_2,&rnorm);
 30:       if (rnorm <= mg->ttol) {
 31:         if (rnorm < mg->abstol) {
 32:           *reason = PCRICHARDSON_CONVERGED_ATOL;
 33:           PetscInfo2(pc,"Linear solver has converged. Residual norm %g is less than absolute tolerance %g\n",(double)rnorm,(double)mg->abstol);
 34:         } else {
 35:           *reason = PCRICHARDSON_CONVERGED_RTOL;
 36:           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);
 37:         }
 38:         return(0);
 39:       }
 40:     }

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

 61: 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)
 62: {
 63:   PC_MG          *mg        = (PC_MG*)pc->data;
 64:   PC_MG_Levels   **mglevels = mg->levels;
 66:   PC             tpc;
 67:   PetscBool      changeu,changed;
 68:   PetscInt       levels = mglevels[0]->levels,i;

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

 79:   KSPGetPC(mglevels[levels-1]->smoothd,&tpc);
 80:   PCPreSolveChangeRHS(tpc,&changed);
 81:   KSPGetPC(mglevels[levels-1]->smoothu,&tpc);
 82:   PCPreSolveChangeRHS(tpc,&changeu);
 83:   if (!changed && !changeu) {
 84:     VecDestroy(&mglevels[levels-1]->b);
 85:     mglevels[levels-1]->b = b;
 86:   } else { /* if the smoother changes the rhs during PreSolve, we cannot use the input vector */
 87:     if (!mglevels[levels-1]->b) {
 88:       Vec *vec;

 90:       KSPCreateVecs(mglevels[levels-1]->smoothd,1,&vec,0,NULL);
 91:       mglevels[levels-1]->b = *vec;
 92:       PetscFree(vec);
 93:     }
 94:     VecCopy(b,mglevels[levels-1]->b);
 95:   }
 96:   mglevels[levels-1]->x = x;

 98:   mg->rtol   = rtol;
 99:   mg->abstol = abstol;
100:   mg->dtol   = dtol;
101:   if (rtol) {
102:     /* compute initial residual norm for relative convergence test */
103:     PetscReal rnorm;
104:     if (zeroguess) {
105:       VecNorm(b,NORM_2,&rnorm);
106:     } else {
107:       (*mglevels[levels-1]->residual)(mglevels[levels-1]->A,b,x,w);
108:       VecNorm(w,NORM_2,&rnorm);
109:     }
110:     mg->ttol = PetscMax(rtol*rnorm,abstol);
111:   } else if (abstol) mg->ttol = abstol;
112:   else mg->ttol = 0.0;

114:   /* since smoother is applied to full system, not just residual we need to make sure that smoothers don't
115:      stop prematurely due to small residual */
116:   for (i=1; i<levels; i++) {
117:     KSPSetTolerances(mglevels[i]->smoothu,0,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);
118:     if (mglevels[i]->smoothu != mglevels[i]->smoothd) {
119:       /* For Richardson the initial guess is nonzero since it is solving in each cycle the original system not just applying as a preconditioner */
120:       KSPSetInitialGuessNonzero(mglevels[i]->smoothd,PETSC_TRUE);
121:       KSPSetTolerances(mglevels[i]->smoothd,0,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);
122:     }
123:   }

125:   *reason = (PCRichardsonConvergedReason)0;
126:   for (i=0; i<its; i++) {
127:     PCMGMCycle_Private(pc,mglevels+levels-1,reason);
128:     if (*reason) break;
129:   }
130:   if (!*reason) *reason = PCRICHARDSON_CONVERGED_ITS;
131:   *outits = i;
132:   if (!changed && !changeu) mglevels[levels-1]->b = NULL;
133:   return(0);
134: }

136: PetscErrorCode PCReset_MG(PC pc)
137: {
138:   PC_MG          *mg        = (PC_MG*)pc->data;
139:   PC_MG_Levels   **mglevels = mg->levels;
141:   PetscInt       i,n;

144:   if (mglevels) {
145:     n = mglevels[0]->levels;
146:     for (i=0; i<n-1; i++) {
147:       VecDestroy(&mglevels[i+1]->r);
148:       VecDestroy(&mglevels[i]->b);
149:       VecDestroy(&mglevels[i]->x);
150:       MatDestroy(&mglevels[i+1]->restrct);
151:       MatDestroy(&mglevels[i+1]->interpolate);
152:       MatDestroy(&mglevels[i+1]->inject);
153:       VecDestroy(&mglevels[i+1]->rscale);
154:     }
155:     /* this is not null only if the smoother on the finest level
156:        changes the rhs during PreSolve */
157:     VecDestroy(&mglevels[n-1]->b);

159:     for (i=0; i<n; i++) {
160:       MatDestroy(&mglevels[i]->A);
161:       if (mglevels[i]->smoothd != mglevels[i]->smoothu) {
162:         KSPReset(mglevels[i]->smoothd);
163:       }
164:       KSPReset(mglevels[i]->smoothu);
165:     }
166:   }
167:   return(0);
168: }

170: PetscErrorCode PCMGSetLevels_MG(PC pc,PetscInt levels,MPI_Comm *comms)
171: {
173:   PC_MG          *mg        = (PC_MG*)pc->data;
174:   MPI_Comm       comm;
175:   PC_MG_Levels   **mglevels = mg->levels;
176:   PCMGType       mgtype     = mg->am;
177:   PetscInt       mgctype    = (PetscInt) PC_MG_CYCLE_V;
178:   PetscInt       i;
179:   PetscMPIInt    size;
180:   const char     *prefix;
181:   PC             ipc;
182:   PetscInt       n;

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

204:   mg->nlevels = levels;

206:   PetscMalloc1(levels,&mglevels);
207:   PetscLogObjectMemory((PetscObject)pc,levels*(sizeof(PC_MG*)));

209:   PCGetOptionsPrefix(pc,&prefix);

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

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

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

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

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

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

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

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

275:    Logically Collective on PC

277:    Input Parameters:
278: +  pc - the preconditioner context
279: .  levels - the number of levels
280: -  comms - optional communicators for each level; this is to allow solving the coarser problems
281:            on smaller sets of processors.

283:    Level: intermediate

285:    Notes:
286:      If the number of levels is one then the multigrid uses the -mg_levels prefix
287:   for setting the level options rather than the -mg_coarse prefix.

289: .seealso: PCMGSetType(), PCMGGetLevels()
290: @*/
291: PetscErrorCode PCMGSetLevels(PC pc,PetscInt levels,MPI_Comm *comms)
292: {

298:   PetscTryMethod(pc,"PCMGSetLevels_C",(PC,PetscInt,MPI_Comm*),(pc,levels,comms));
299:   return(0);
300: }


303: PetscErrorCode PCDestroy_MG(PC pc)
304: {
306:   PC_MG          *mg        = (PC_MG*)pc->data;
307:   PC_MG_Levels   **mglevels = mg->levels;
308:   PetscInt       i,n;

311:   PCReset_MG(pc);
312:   if (mglevels) {
313:     n = mglevels[0]->levels;
314:     for (i=0; i<n; i++) {
315:       if (mglevels[i]->smoothd != mglevels[i]->smoothu) {
316:         KSPDestroy(&mglevels[i]->smoothd);
317:       }
318:       KSPDestroy(&mglevels[i]->smoothu);
319:       PetscFree(mglevels[i]);
320:     }
321:     PetscFree(mg->levels);
322:   }
323:   PetscFree(pc->data);
324:   PetscObjectComposeFunction((PetscObject)pc,"PCGetInterpolations_C",NULL);
325:   PetscObjectComposeFunction((PetscObject)pc,"PCGetCoarseOperators_C",NULL);
326:   return(0);
327: }



331: extern PetscErrorCode PCMGACycle_Private(PC,PC_MG_Levels**);
332: extern PetscErrorCode PCMGFCycle_Private(PC,PC_MG_Levels**);
333: extern PetscErrorCode PCMGKCycle_Private(PC,PC_MG_Levels**);

335: /*
336:    PCApply_MG - Runs either an additive, multiplicative, Kaskadic
337:              or full cycle of multigrid.

339:   Note:
340:   A simple wrapper which calls PCMGMCycle(),PCMGACycle(), or PCMGFCycle().
341: */
342: static PetscErrorCode PCApply_MG(PC pc,Vec b,Vec x)
343: {
344:   PC_MG          *mg        = (PC_MG*)pc->data;
345:   PC_MG_Levels   **mglevels = mg->levels;
347:   PC             tpc;
348:   PetscInt       levels = mglevels[0]->levels,i;
349:   PetscBool      changeu,changed;

352:   if (mg->stageApply) {PetscLogStagePush(mg->stageApply);}
353:   /* When the DM is supplying the matrix then it will not exist until here */
354:   for (i=0; i<levels; i++) {
355:     if (!mglevels[i]->A) {
356:       KSPGetOperators(mglevels[i]->smoothu,&mglevels[i]->A,NULL);
357:       PetscObjectReference((PetscObject)mglevels[i]->A);
358:     }
359:   }

361:   KSPGetPC(mglevels[levels-1]->smoothd,&tpc);
362:   PCPreSolveChangeRHS(tpc,&changed);
363:   KSPGetPC(mglevels[levels-1]->smoothu,&tpc);
364:   PCPreSolveChangeRHS(tpc,&changeu);
365:   if (!changeu && !changed) {
366:     VecDestroy(&mglevels[levels-1]->b);
367:     mglevels[levels-1]->b = b;
368:   } else { /* if the smoother changes the rhs during PreSolve, we cannot use the input vector */
369:     if (!mglevels[levels-1]->b) {
370:       Vec *vec;

372:       KSPCreateVecs(mglevels[levels-1]->smoothd,1,&vec,0,NULL);
373:       mglevels[levels-1]->b = *vec;
374:       PetscFree(vec);
375:     }
376:     VecCopy(b,mglevels[levels-1]->b);
377:   }
378:   mglevels[levels-1]->x = x;

380:   if (mg->am == PC_MG_MULTIPLICATIVE) {
381:     VecSet(x,0.0);
382:     for (i=0; i<mg->cyclesperpcapply; i++) {
383:       PCMGMCycle_Private(pc,mglevels+levels-1,NULL);
384:     }
385:   } else if (mg->am == PC_MG_ADDITIVE) {
386:     PCMGACycle_Private(pc,mglevels);
387:   } else if (mg->am == PC_MG_KASKADE) {
388:     PCMGKCycle_Private(pc,mglevels);
389:   } else {
390:     PCMGFCycle_Private(pc,mglevels);
391:   }
392:   if (mg->stageApply) {PetscLogStagePop();}
393:   if (!changeu && !changed) mglevels[levels-1]->b = NULL;
394:   return(0);
395: }


398: PetscErrorCode PCSetFromOptions_MG(PetscOptionItems *PetscOptionsObject,PC pc)
399: {
400:   PetscErrorCode   ierr;
401:   PetscInt         levels,cycles;
402:   PetscBool        flg;
403:   PC_MG            *mg = (PC_MG*)pc->data;
404:   PC_MG_Levels     **mglevels;
405:   PCMGType         mgtype;
406:   PCMGCycleType    mgctype;
407:   PCMGGalerkinType gtype;

410:   levels = PetscMax(mg->nlevels,1);
411:   PetscOptionsHead(PetscOptionsObject,"Multigrid options");
412:   PetscOptionsInt("-pc_mg_levels","Number of Levels","PCMGSetLevels",levels,&levels,&flg);
413:   if (!flg && !mg->levels && pc->dm) {
414:     DMGetRefineLevel(pc->dm,&levels);
415:     levels++;
416:     mg->usedmfornumberoflevels = PETSC_TRUE;
417:   }
418:   PCMGSetLevels(pc,levels,NULL);
419:   mglevels = mg->levels;

421:   mgctype = (PCMGCycleType) mglevels[0]->cycles;
422:   PetscOptionsEnum("-pc_mg_cycle_type","V cycle or for W-cycle","PCMGSetCycleType",PCMGCycleTypes,(PetscEnum)mgctype,(PetscEnum*)&mgctype,&flg);
423:   if (flg) {
424:     PCMGSetCycleType(pc,mgctype);
425:   }
426:   gtype = mg->galerkin;
427:   PetscOptionsEnum("-pc_mg_galerkin","Use Galerkin process to compute coarser operators","PCMGSetGalerkin",PCMGGalerkinTypes,(PetscEnum)gtype,(PetscEnum*)&gtype,&flg);
428:   if (flg) {
429:     PCMGSetGalerkin(pc,gtype);
430:   }
431:   flg = PETSC_FALSE;
432:   PetscOptionsBool("-pc_mg_distinct_smoothup","Create separate smoothup KSP and append the prefix _up","PCMGSetDistinctSmoothUp",PETSC_FALSE,&flg,NULL);
433:   if (flg) {
434:     PCMGSetDistinctSmoothUp(pc);
435:   }
436:   mgtype = mg->am;
437:   PetscOptionsEnum("-pc_mg_type","Multigrid type","PCMGSetType",PCMGTypes,(PetscEnum)mgtype,(PetscEnum*)&mgtype,&flg);
438:   if (flg) {
439:     PCMGSetType(pc,mgtype);
440:   }
441:   if (mg->am == PC_MG_MULTIPLICATIVE) {
442:     PetscOptionsInt("-pc_mg_multiplicative_cycles","Number of cycles for each preconditioner step","PCMGMultiplicativeSetCycles",mg->cyclesperpcapply,&cycles,&flg);
443:     if (flg) {
444:       PCMGMultiplicativeSetCycles(pc,cycles);
445:     }
446:   }
447:   flg  = PETSC_FALSE;
448:   PetscOptionsBool("-pc_mg_log","Log times for each multigrid level","None",flg,&flg,NULL);
449:   if (flg) {
450:     PetscInt i;
451:     char     eventname[128];

453:     levels = mglevels[0]->levels;
454:     for (i=0; i<levels; i++) {
455:       sprintf(eventname,"MGSetup Level %d",(int)i);
456:       PetscLogEventRegister(eventname,((PetscObject)pc)->classid,&mglevels[i]->eventsmoothsetup);
457:       sprintf(eventname,"MGSmooth Level %d",(int)i);
458:       PetscLogEventRegister(eventname,((PetscObject)pc)->classid,&mglevels[i]->eventsmoothsolve);
459:       if (i) {
460:         sprintf(eventname,"MGResid Level %d",(int)i);
461:         PetscLogEventRegister(eventname,((PetscObject)pc)->classid,&mglevels[i]->eventresidual);
462:         sprintf(eventname,"MGInterp Level %d",(int)i);
463:         PetscLogEventRegister(eventname,((PetscObject)pc)->classid,&mglevels[i]->eventinterprestrict);
464:       }
465:     }

467: #if defined(PETSC_USE_LOG)
468:     {
469:       const char    *sname = "MG Apply";
470:       PetscStageLog stageLog;
471:       PetscInt      st;

473:       PetscLogGetStageLog(&stageLog);
474:       for (st = 0; st < stageLog->numStages; ++st) {
475:         PetscBool same;

477:         PetscStrcmp(stageLog->stageInfo[st].name, sname, &same);
478:         if (same) mg->stageApply = st;
479:       }
480:       if (!mg->stageApply) {
481:         PetscLogStageRegister(sname, &mg->stageApply);
482:       }
483:     }
484: #endif
485:   }
486:   PetscOptionsTail();
487:   return(0);
488: }

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

494:  #include <petscdraw.h>
495: PetscErrorCode PCView_MG(PC pc,PetscViewer viewer)
496: {
497:   PC_MG          *mg        = (PC_MG*)pc->data;
498:   PC_MG_Levels   **mglevels = mg->levels;
500:   PetscInt       levels = mglevels ? mglevels[0]->levels : 0,i;
501:   PetscBool      iascii,isbinary,isdraw;

504:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
505:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
506:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
507:   if (iascii) {
508:     const char *cyclename = levels ? (mglevels[0]->cycles == PC_MG_CYCLE_V ? "v" : "w") : "unknown";
509:     PetscViewerASCIIPrintf(viewer,"  type is %s, levels=%D cycles=%s\n", PCMGTypes[mg->am],levels,cyclename);
510:     if (mg->am == PC_MG_MULTIPLICATIVE) {
511:       PetscViewerASCIIPrintf(viewer,"    Cycles per PCApply=%d\n",mg->cyclesperpcapply);
512:     }
513:     if (mg->galerkin == PC_MG_GALERKIN_BOTH) {
514:       PetscViewerASCIIPrintf(viewer,"    Using Galerkin computed coarse grid matrices\n");
515:     } else if (mg->galerkin == PC_MG_GALERKIN_PMAT) {
516:       PetscViewerASCIIPrintf(viewer,"    Using Galerkin computed coarse grid matrices for pmat\n");
517:     } else if (mg->galerkin == PC_MG_GALERKIN_MAT) {
518:       PetscViewerASCIIPrintf(viewer,"    Using Galerkin computed coarse grid matrices for mat\n");
519:     } else if (mg->galerkin == PC_MG_GALERKIN_EXTERNAL) {
520:       PetscViewerASCIIPrintf(viewer,"    Using externally compute Galerkin coarse grid matrices\n");
521:     } else {
522:       PetscViewerASCIIPrintf(viewer,"    Not using Galerkin computed coarse grid matrices\n");
523:     }
524:     if (mg->view){
525:       (*mg->view)(pc,viewer);
526:     }
527:     for (i=0; i<levels; i++) {
528:       if (!i) {
529:         PetscViewerASCIIPrintf(viewer,"Coarse grid solver -- level -------------------------------\n",i);
530:       } else {
531:         PetscViewerASCIIPrintf(viewer,"Down solver (pre-smoother) on level %D -------------------------------\n",i);
532:       }
533:       PetscViewerASCIIPushTab(viewer);
534:       KSPView(mglevels[i]->smoothd,viewer);
535:       PetscViewerASCIIPopTab(viewer);
536:       if (i && mglevels[i]->smoothd == mglevels[i]->smoothu) {
537:         PetscViewerASCIIPrintf(viewer,"Up solver (post-smoother) same as down solver (pre-smoother)\n");
538:       } else if (i) {
539:         PetscViewerASCIIPrintf(viewer,"Up solver (post-smoother) on level %D -------------------------------\n",i);
540:         PetscViewerASCIIPushTab(viewer);
541:         KSPView(mglevels[i]->smoothu,viewer);
542:         PetscViewerASCIIPopTab(viewer);
543:       }
544:     }
545:   } else if (isbinary) {
546:     for (i=levels-1; i>=0; i--) {
547:       KSPView(mglevels[i]->smoothd,viewer);
548:       if (i && mglevels[i]->smoothd != mglevels[i]->smoothu) {
549:         KSPView(mglevels[i]->smoothu,viewer);
550:       }
551:     }
552:   } else if (isdraw) {
553:     PetscDraw draw;
554:     PetscReal x,w,y,bottom,th;
555:     PetscViewerDrawGetDraw(viewer,0,&draw);
556:     PetscDrawGetCurrentPoint(draw,&x,&y);
557:     PetscDrawStringGetSize(draw,NULL,&th);
558:     bottom = y - th;
559:     for (i=levels-1; i>=0; i--) {
560:       if (!mglevels[i]->smoothu || (mglevels[i]->smoothu == mglevels[i]->smoothd)) {
561:         PetscDrawPushCurrentPoint(draw,x,bottom);
562:         KSPView(mglevels[i]->smoothd,viewer);
563:         PetscDrawPopCurrentPoint(draw);
564:       } else {
565:         w    = 0.5*PetscMin(1.0-x,x);
566:         PetscDrawPushCurrentPoint(draw,x+w,bottom);
567:         KSPView(mglevels[i]->smoothd,viewer);
568:         PetscDrawPopCurrentPoint(draw);
569:         PetscDrawPushCurrentPoint(draw,x-w,bottom);
570:         KSPView(mglevels[i]->smoothu,viewer);
571:         PetscDrawPopCurrentPoint(draw);
572:       }
573:       PetscDrawGetBoundingBox(draw,NULL,&bottom,NULL,NULL);
574:       bottom -= th;
575:     }
576:   }
577:   return(0);
578: }

580:  #include <petsc/private/dmimpl.h>
581:  #include <petsc/private/kspimpl.h>

583: /*
584:     Calls setup for the KSP on each level
585: */
586: PetscErrorCode PCSetUp_MG(PC pc)
587: {
588:   PC_MG          *mg        = (PC_MG*)pc->data;
589:   PC_MG_Levels   **mglevels = mg->levels;
591:   PetscInt       i,n;
592:   PC             cpc;
593:   PetscBool      dump = PETSC_FALSE,opsset,use_amat,missinginterpolate = PETSC_FALSE;
594:   Mat            dA,dB;
595:   Vec            tvec;
596:   DM             *dms;
597:   PetscViewer    viewer = NULL;
598:   PetscBool      dAeqdB = PETSC_FALSE, needRestricts = PETSC_FALSE;

601:   if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels with PCMGSetLevels() before setting up");
602:   n = mglevels[0]->levels;
603:   /* FIX: Move this to PCSetFromOptions_MG? */
604:   if (mg->usedmfornumberoflevels) {
605:     PetscInt levels;
606:     DMGetRefineLevel(pc->dm,&levels);
607:     levels++;
608:     if (levels > n) { /* the problem is now being solved on a finer grid */
609:       PCMGSetLevels(pc,levels,NULL);
610:       n        = levels;
611:       PCSetFromOptions(pc); /* it is bad to call this here, but otherwise will never be called for the new hierarchy */
612:       mglevels = mg->levels;
613:     }
614:   }
615:   KSPGetPC(mglevels[0]->smoothd,&cpc);


618:   /* If user did not provide fine grid operators OR operator was not updated since last global KSPSetOperators() */
619:   /* so use those from global PC */
620:   /* Is this what we always want? What if user wants to keep old one? */
621:   KSPGetOperatorsSet(mglevels[n-1]->smoothd,NULL,&opsset);
622:   if (opsset) {
623:     Mat mmat;
624:     KSPGetOperators(mglevels[n-1]->smoothd,NULL,&mmat);
625:     if (mmat == pc->pmat) opsset = PETSC_FALSE;
626:   }

628:   if (!opsset) {
629:     PCGetUseAmat(pc,&use_amat);
630:     if (use_amat) {
631:       PetscInfo(pc,"Using outer operators to define finest grid operator \n  because PCMGGetSmoother(pc,nlevels-1,&ksp);KSPSetOperators(ksp,...); was not called.\n");
632:       KSPSetOperators(mglevels[n-1]->smoothd,pc->mat,pc->pmat);
633:     } else {
634:       PetscInfo(pc,"Using matrix (pmat) operators to define finest grid operator \n  because PCMGGetSmoother(pc,nlevels-1,&ksp);KSPSetOperators(ksp,...); was not called.\n");
635:       KSPSetOperators(mglevels[n-1]->smoothd,pc->pmat,pc->pmat);
636:     }
637:   }

639:   for (i=n-1; i>0; i--) {
640:     if (!(mglevels[i]->interpolate || mglevels[i]->restrct)) {
641:       missinginterpolate = PETSC_TRUE;
642:       continue;
643:     }
644:   }

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


653:   /*
654:    Skipping if user has provided all interpolation/restriction needed (since DM might not be able to produce them (when coming from SNES/TS)
655:    Skipping for galerkin==2 (externally managed hierarchy such as ML and GAMG). Cleaner logic here would be great. Wrap ML/GAMG as DMs?
656:   */
657:   if (missinginterpolate && pc->dm && mg->galerkin != PC_MG_GALERKIN_EXTERNAL && !pc->setupcalled) {
658:         /* construct the interpolation from the DMs */
659:     Mat p;
660:     Vec rscale;
661:     PetscMalloc1(n,&dms);
662:     dms[n-1] = pc->dm;
663:     /* Separately create them so we do not get DMKSP interference between levels */
664:     for (i=n-2; i>-1; i--) {DMCoarsen(dms[i+1],MPI_COMM_NULL,&dms[i]);}
665:         /*
666:            Force the mat type of coarse level operator to be AIJ because usually we want to use LU for coarse level.
667:            Notice that it can be overwritten by -mat_type because KSPSetUp() reads command line options.
668:            But it is safe to use -dm_mat_type.

670:            The mat type should not be hardcoded like this, we need to find a better way.
671:     DMSetMatType(dms[0],MATAIJ);
672:     */
673:     for (i=n-2; i>-1; i--) {
674:       DMKSP     kdm;
675:       PetscBool dmhasrestrict, dmhasinject;
676:       KSPSetDM(mglevels[i]->smoothd,dms[i]);
677:       if (!needRestricts) {KSPSetDMActive(mglevels[i]->smoothd,PETSC_FALSE);}
678:       if (mglevels[i]->smoothd != mglevels[i]->smoothu) {
679:         KSPSetDM(mglevels[i]->smoothu,dms[i]);
680:         if (!needRestricts) {KSPSetDMActive(mglevels[i]->smoothu,PETSC_FALSE);}
681:       }
682:       DMGetDMKSPWrite(dms[i],&kdm);
683:       /* Ugly hack so that the next KSPSetUp() will use the RHS that we set. A better fix is to change dmActive to take
684:        * a bitwise OR of computing the matrix, RHS, and initial iterate. */
685:       kdm->ops->computerhs = NULL;
686:       kdm->rhsctx          = NULL;
687:       if (!mglevels[i+1]->interpolate) {
688:         DMCreateInterpolation(dms[i],dms[i+1],&p,&rscale);
689:         PCMGSetInterpolation(pc,i+1,p);
690:         if (rscale) {PCMGSetRScale(pc,i+1,rscale);}
691:         VecDestroy(&rscale);
692:         MatDestroy(&p);
693:       }
694:       DMHasCreateRestriction(dms[i],&dmhasrestrict);
695:       if (dmhasrestrict && !mglevels[i+1]->restrct){
696:         DMCreateRestriction(dms[i],dms[i+1],&p);
697:         PCMGSetRestriction(pc,i+1,p);
698:         MatDestroy(&p);
699:       }
700:       DMHasCreateInjection(dms[i],&dmhasinject);
701:       if (dmhasinject && !mglevels[i+1]->inject){
702:         DMCreateInjection(dms[i],dms[i+1],&p);
703:         PCMGSetInjection(pc,i+1,p);
704:         MatDestroy(&p);
705:       }
706:     }

708:     for (i=n-2; i>-1; i--) {DMDestroy(&dms[i]);}
709:     PetscFree(dms);
710:   }

712:   if (pc->dm && !pc->setupcalled) {
713:     /* finest smoother also gets DM but it is not active, independent of whether galerkin==PC_MG_GALERKIN_EXTERNAL */
714:     KSPSetDM(mglevels[n-1]->smoothd,pc->dm);
715:     KSPSetDMActive(mglevels[n-1]->smoothd,PETSC_FALSE);
716:     if (mglevels[n-1]->smoothd != mglevels[n-1]->smoothu) {
717:       KSPSetDM(mglevels[n-1]->smoothu,pc->dm);
718:       KSPSetDMActive(mglevels[n-1]->smoothu,PETSC_FALSE);
719:     }
720:   }

722:   if (mg->galerkin < PC_MG_GALERKIN_NONE) {
723:     Mat       A,B;
724:     PetscBool doA = PETSC_FALSE,doB = PETSC_FALSE;
725:     MatReuse  reuse = MAT_INITIAL_MATRIX;

727:     if ((mg->galerkin == PC_MG_GALERKIN_PMAT) || (mg->galerkin == PC_MG_GALERKIN_BOTH)) doB = PETSC_TRUE;
728:     if ((mg->galerkin == PC_MG_GALERKIN_MAT) || ((mg->galerkin == PC_MG_GALERKIN_BOTH) && (dA != dB))) doA = PETSC_TRUE;
729:     if (pc->setupcalled) reuse = MAT_REUSE_MATRIX;
730:     for (i=n-2; i>-1; i--) {
731:       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");
732:       if (!mglevels[i+1]->interpolate) {
733:         PCMGSetInterpolation(pc,i+1,mglevels[i+1]->restrct);
734:       }
735:       if (!mglevels[i+1]->restrct) {
736:         PCMGSetRestriction(pc,i+1,mglevels[i+1]->interpolate);
737:       }
738:       if (reuse == MAT_REUSE_MATRIX) {
739:         KSPGetOperators(mglevels[i]->smoothd,&A,&B);
740:       }
741:       if (doA) {
742:         MatGalerkin(mglevels[i+1]->restrct,dA,mglevels[i+1]->interpolate,reuse,1.0,&A);
743:       }
744:       if (doB) {
745:         MatGalerkin(mglevels[i+1]->restrct,dB,mglevels[i+1]->interpolate,reuse,1.0,&B);
746:       }
747:       /* the management of the PetscObjectReference() and PetscObjecDereference() below is rather delicate */
748:       if (!doA && dAeqdB) {
749:         if (reuse == MAT_INITIAL_MATRIX) {PetscObjectReference((PetscObject)B);}
750:         A = B;
751:       } else if (!doA && reuse == MAT_INITIAL_MATRIX ) {
752:         KSPGetOperators(mglevels[i]->smoothd,&A,NULL);
753:         PetscObjectReference((PetscObject)A);
754:       }
755:       if (!doB && dAeqdB) {
756:         if (reuse == MAT_INITIAL_MATRIX) {PetscObjectReference((PetscObject)A);}
757:         B = A;
758:       } else if (!doB && reuse == MAT_INITIAL_MATRIX) {
759:         KSPGetOperators(mglevels[i]->smoothd,NULL,&B);
760:         PetscObjectReference((PetscObject)B);
761:       }
762:       if (reuse == MAT_INITIAL_MATRIX) {
763:         KSPSetOperators(mglevels[i]->smoothd,A,B);
764:         PetscObjectDereference((PetscObject)A);
765:         PetscObjectDereference((PetscObject)B);
766:       }
767:       dA = A;
768:       dB = B;
769:     }
770:   }
771:   if (needRestricts && pc->dm) {
772:     for (i=n-2; i>=0; i--) {
773:       DM  dmfine,dmcoarse;
774:       Mat Restrict,Inject;
775:       Vec rscale;
776:       KSPGetDM(mglevels[i+1]->smoothd,&dmfine);
777:       KSPGetDM(mglevels[i]->smoothd,&dmcoarse);
778:       PCMGGetRestriction(pc,i+1,&Restrict);
779:       PCMGGetRScale(pc,i+1,&rscale);
780:       PCMGGetInjection(pc,i+1,&Inject);
781:       DMRestrict(dmfine,Restrict,rscale,Inject,dmcoarse);
782:     }
783:   }

785:   if (!pc->setupcalled) {
786:     for (i=0; i<n; i++) {
787:       KSPSetFromOptions(mglevels[i]->smoothd);
788:     }
789:     for (i=1; i<n; i++) {
790:       if (mglevels[i]->smoothu && (mglevels[i]->smoothu != mglevels[i]->smoothd)) {
791:         KSPSetFromOptions(mglevels[i]->smoothu);
792:       }
793:     }
794:     /* insure that if either interpolation or restriction is set the other other one is set */
795:     for (i=1; i<n; i++) {
796:       PCMGGetInterpolation(pc,i,NULL);
797:       PCMGGetRestriction(pc,i,NULL);
798:     }
799:     for (i=0; i<n-1; i++) {
800:       if (!mglevels[i]->b) {
801:         Vec *vec;
802:         KSPCreateVecs(mglevels[i]->smoothd,1,&vec,0,NULL);
803:         PCMGSetRhs(pc,i,*vec);
804:         VecDestroy(vec);
805:         PetscFree(vec);
806:       }
807:       if (!mglevels[i]->r && i) {
808:         VecDuplicate(mglevels[i]->b,&tvec);
809:         PCMGSetR(pc,i,tvec);
810:         VecDestroy(&tvec);
811:       }
812:       if (!mglevels[i]->x) {
813:         VecDuplicate(mglevels[i]->b,&tvec);
814:         PCMGSetX(pc,i,tvec);
815:         VecDestroy(&tvec);
816:       }
817:     }
818:     if (n != 1 && !mglevels[n-1]->r) {
819:       /* PCMGSetR() on the finest level if user did not supply it */
820:       Vec *vec;
821:       KSPCreateVecs(mglevels[n-1]->smoothd,1,&vec,0,NULL);
822:       PCMGSetR(pc,n-1,*vec);
823:       VecDestroy(vec);
824:       PetscFree(vec);
825:     }
826:   }

828:   if (pc->dm) {
829:     /* need to tell all the coarser levels to rebuild the matrix using the DM for that level */
830:     for (i=0; i<n-1; i++) {
831:       if (mglevels[i]->smoothd->setupstage != KSP_SETUP_NEW) mglevels[i]->smoothd->setupstage = KSP_SETUP_NEWMATRIX;
832:     }
833:   }

835:   for (i=1; i<n; i++) {
836:     if (mglevels[i]->smoothu == mglevels[i]->smoothd || mg->am == PC_MG_FULL || mg->am == PC_MG_KASKADE || mg->cyclesperpcapply > 1){
837:       /* if doing only down then initial guess is zero */
838:       KSPSetInitialGuessNonzero(mglevels[i]->smoothd,PETSC_TRUE);
839:     }
840:     if (mglevels[i]->eventsmoothsetup) {PetscLogEventBegin(mglevels[i]->eventsmoothsetup,0,0,0,0);}
841:     KSPSetUp(mglevels[i]->smoothd);
842:     if (mglevels[i]->smoothd->reason == KSP_DIVERGED_PC_FAILED) {
843:       pc->failedreason = PC_SUBPC_ERROR;
844:     }
845:     if (mglevels[i]->eventsmoothsetup) {PetscLogEventEnd(mglevels[i]->eventsmoothsetup,0,0,0,0);}
846:     if (!mglevels[i]->residual) {
847:       Mat mat;
848:       KSPGetOperators(mglevels[i]->smoothd,&mat,NULL);
849:       PCMGSetResidual(pc,i,PCMGResidualDefault,mat);
850:     }
851:   }
852:   for (i=1; i<n; i++) {
853:     if (mglevels[i]->smoothu && mglevels[i]->smoothu != mglevels[i]->smoothd) {
854:       Mat downmat,downpmat;

856:       /* check if operators have been set for up, if not use down operators to set them */
857:       KSPGetOperatorsSet(mglevels[i]->smoothu,&opsset,NULL);
858:       if (!opsset) {
859:         KSPGetOperators(mglevels[i]->smoothd,&downmat,&downpmat);
860:         KSPSetOperators(mglevels[i]->smoothu,downmat,downpmat);
861:       }

863:       KSPSetInitialGuessNonzero(mglevels[i]->smoothu,PETSC_TRUE);
864:       if (mglevels[i]->eventsmoothsetup) {PetscLogEventBegin(mglevels[i]->eventsmoothsetup,0,0,0,0);}
865:       KSPSetUp(mglevels[i]->smoothu);
866:       if (mglevels[i]->smoothu->reason == KSP_DIVERGED_PC_FAILED) {
867:         pc->failedreason = PC_SUBPC_ERROR;
868:       }
869:       if (mglevels[i]->eventsmoothsetup) {PetscLogEventEnd(mglevels[i]->eventsmoothsetup,0,0,0,0);}
870:     }
871:   }

873:   if (mglevels[0]->eventsmoothsetup) {PetscLogEventBegin(mglevels[0]->eventsmoothsetup,0,0,0,0);}
874:   KSPSetUp(mglevels[0]->smoothd);
875:   if (mglevels[0]->smoothd->reason == KSP_DIVERGED_PC_FAILED) {
876:     pc->failedreason = PC_SUBPC_ERROR;
877:   }
878:   if (mglevels[0]->eventsmoothsetup) {PetscLogEventEnd(mglevels[0]->eventsmoothsetup,0,0,0,0);}

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

885:    Only support one or the other at the same time.
886:   */
887: #if defined(PETSC_USE_SOCKET_VIEWER)
888:   PetscOptionsGetBool(((PetscObject)pc)->options,((PetscObject)pc)->prefix,"-pc_mg_dump_matlab",&dump,NULL);
889:   if (dump) viewer = PETSC_VIEWER_SOCKET_(PetscObjectComm((PetscObject)pc));
890:   dump = PETSC_FALSE;
891: #endif
892:   PetscOptionsGetBool(((PetscObject)pc)->options,((PetscObject)pc)->prefix,"-pc_mg_dump_binary",&dump,NULL);
893:   if (dump) viewer = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)pc));

895:   if (viewer) {
896:     for (i=1; i<n; i++) {
897:       MatView(mglevels[i]->restrct,viewer);
898:     }
899:     for (i=0; i<n; i++) {
900:       KSPGetPC(mglevels[i]->smoothd,&pc);
901:       MatView(pc->mat,viewer);
902:     }
903:   }
904:   return(0);
905: }

907: /* -------------------------------------------------------------------------------------*/

909: PetscErrorCode PCMGGetLevels_MG(PC pc, PetscInt *levels)
910: {
911:   PC_MG *mg = (PC_MG *) pc->data;

914:   *levels = mg->nlevels;
915:   return(0);
916: }

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

921:    Not Collective

923:    Input Parameter:
924: .  pc - the preconditioner context

926:    Output parameter:
927: .  levels - the number of levels

929:    Level: advanced

931: .seealso: PCMGSetLevels()
932: @*/
933: PetscErrorCode PCMGGetLevels(PC pc,PetscInt *levels)
934: {

940:   *levels = 0;
941:   PetscTryMethod(pc,"PCMGGetLevels_C",(PC,PetscInt*),(pc,levels));
942:   return(0);
943: }

945: /*@
946:    PCMGSetType - Determines the form of multigrid to use:
947:    multiplicative, additive, full, or the Kaskade algorithm.

949:    Logically Collective on PC

951:    Input Parameters:
952: +  pc - the preconditioner context
953: -  form - multigrid form, one of PC_MG_MULTIPLICATIVE, PC_MG_ADDITIVE,
954:    PC_MG_FULL, PC_MG_KASKADE

956:    Options Database Key:
957: .  -pc_mg_type <form> - Sets <form>, one of multiplicative,
958:    additive, full, kaskade

960:    Level: advanced

962: .seealso: PCMGSetLevels()
963: @*/
964: PetscErrorCode  PCMGSetType(PC pc,PCMGType form)
965: {
966:   PC_MG *mg = (PC_MG*)pc->data;

971:   mg->am = form;
972:   if (form == PC_MG_MULTIPLICATIVE) pc->ops->applyrichardson = PCApplyRichardson_MG;
973:   else pc->ops->applyrichardson = NULL;
974:   return(0);
975: }

977: /*@
978:    PCMGGetType - Determines the form of multigrid to use:
979:    multiplicative, additive, full, or the Kaskade algorithm.

981:    Logically Collective on PC

983:    Input Parameter:
984: .  pc - the preconditioner context

986:    Output Parameter:
987: .  type - one of PC_MG_MULTIPLICATIVE, PC_MG_ADDITIVE,PC_MG_FULL, PC_MG_KASKADE


990:    Level: advanced

992: .seealso: PCMGSetLevels()
993: @*/
994: PetscErrorCode  PCMGGetType(PC pc,PCMGType *type)
995: {
996:   PC_MG *mg = (PC_MG*)pc->data;

1000:   *type = mg->am;
1001:   return(0);
1002: }

1004: /*@
1005:    PCMGSetCycleType - Sets the type cycles to use.  Use PCMGSetCycleTypeOnLevel() for more
1006:    complicated cycling.

1008:    Logically Collective on PC

1010:    Input Parameters:
1011: +  pc - the multigrid context
1012: -  n - either PC_MG_CYCLE_V or PC_MG_CYCLE_W

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

1017:    Level: advanced

1019: .seealso: PCMGSetCycleTypeOnLevel()
1020: @*/
1021: PetscErrorCode  PCMGSetCycleType(PC pc,PCMGCycleType n)
1022: {
1023:   PC_MG        *mg        = (PC_MG*)pc->data;
1024:   PC_MG_Levels **mglevels = mg->levels;
1025:   PetscInt     i,levels;

1030:   if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ORDER,"Must set MG levels with PCMGSetLevels() before calling");
1031:   levels = mglevels[0]->levels;
1032:   for (i=0; i<levels; i++) mglevels[i]->cycles = n;
1033:   return(0);
1034: }

1036: /*@
1037:    PCMGMultiplicativeSetCycles - Sets the number of cycles to use for each preconditioner step
1038:          of multigrid when PCMGType of PC_MG_MULTIPLICATIVE is used

1040:    Logically Collective on PC

1042:    Input Parameters:
1043: +  pc - the multigrid context
1044: -  n - number of cycles (default is 1)

1046:    Options Database Key:
1047: .  -pc_mg_multiplicative_cycles n

1049:    Level: advanced

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

1054: .seealso: PCMGSetCycleTypeOnLevel(), PCMGSetCycleType()
1055: @*/
1056: PetscErrorCode  PCMGMultiplicativeSetCycles(PC pc,PetscInt n)
1057: {
1058:   PC_MG        *mg        = (PC_MG*)pc->data;

1063:   mg->cyclesperpcapply = n;
1064:   return(0);
1065: }

1067: PetscErrorCode PCMGSetGalerkin_MG(PC pc,PCMGGalerkinType use)
1068: {
1069:   PC_MG *mg = (PC_MG*)pc->data;

1072:   mg->galerkin = use;
1073:   return(0);
1074: }

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

1080:    Logically Collective on PC

1082:    Input Parameters:
1083: +  pc - the multigrid context
1084: -  use - one of PC_MG_GALERKIN_BOTH,PC_MG_GALERKIN_PMAT,PC_MG_GALERKIN_MAT, or PC_MG_GALERKIN_NONE

1086:    Options Database Key:
1087: .  -pc_mg_galerkin <both,pmat,mat,none>

1089:    Level: intermediate

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

1095: .seealso: PCMGGetGalerkin(), PCMGGalerkinType

1097: @*/
1098: PetscErrorCode PCMGSetGalerkin(PC pc,PCMGGalerkinType use)
1099: {

1104:   PetscTryMethod(pc,"PCMGSetGalerkin_C",(PC,PCMGGalerkinType),(pc,use));
1105:   return(0);
1106: }

1108: /*@
1109:    PCMGGetGalerkin - Checks if Galerkin multigrid is being used, i.e.
1110:       A_i-1 = r_i * A_i * p_i

1112:    Not Collective

1114:    Input Parameter:
1115: .  pc - the multigrid context

1117:    Output Parameter:
1118: .  galerkin - one of PC_MG_GALERKIN_BOTH,PC_MG_GALERKIN_PMAT,PC_MG_GALERKIN_MAT, PC_MG_GALERKIN_NONE, or PC_MG_GALERKIN_EXTERNAL

1120:    Level: intermediate

1122: .seealso: PCMGSetGalerkin(), PCMGGalerkinType

1124: @*/
1125: PetscErrorCode  PCMGGetGalerkin(PC pc,PCMGGalerkinType  *galerkin)
1126: {
1127:   PC_MG *mg = (PC_MG*)pc->data;

1131:   *galerkin = mg->galerkin;
1132:   return(0);
1133: }

1135: /*@
1136:    PCMGSetNumberSmooth - Sets the number of pre and post-smoothing steps to use
1137:    on all levels.  Use PCMGDistinctSmoothUp() to create separate up and down smoothers if you want different numbers of
1138:    pre- and post-smoothing steps.

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: .  -mg_levels_ksp_max_it <n> - Sets number of pre and post-smoothing steps

1149:    Level: advanced

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

1155: .seealso: PCMGSetDistinctSmoothUp()
1156: @*/
1157: PetscErrorCode  PCMGSetNumberSmooth(PC pc,PetscInt n)
1158: {
1159:   PC_MG          *mg        = (PC_MG*)pc->data;
1160:   PC_MG_Levels   **mglevels = mg->levels;
1162:   PetscInt       i,levels;

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

1170:   for (i=1; i<levels; i++) {
1171:     KSPSetTolerances(mglevels[i]->smoothu,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,n);
1172:     KSPSetTolerances(mglevels[i]->smoothd,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,n);
1173:     mg->default_smoothu = n;
1174:     mg->default_smoothd = n;
1175:   }
1176:   return(0);
1177: }

1179: /*@
1180:    PCMGSetDistinctSmoothUp - sets the up (post) smoother to be a separate KSP from the down (pre) smoother on all levels
1181:        and adds the suffix _up to the options name

1183:    Logically Collective on PC

1185:    Input Parameters:
1186: .  pc - the preconditioner context

1188:    Options Database Key:
1189: .  -pc_mg_distinct_smoothup

1191:    Level: advanced

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

1197: .seealso: PCMGSetNumberSmooth()
1198: @*/
1199: PetscErrorCode  PCMGSetDistinctSmoothUp(PC pc)
1200: {
1201:   PC_MG          *mg        = (PC_MG*)pc->data;
1202:   PC_MG_Levels   **mglevels = mg->levels;
1204:   PetscInt       i,levels;
1205:   KSP            subksp;

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

1212:   for (i=1; i<levels; i++) {
1213:     const char *prefix = NULL;
1214:     /* make sure smoother up and down are different */
1215:     PCMGGetSmootherUp(pc,i,&subksp);
1216:     KSPGetOptionsPrefix(mglevels[i]->smoothd,&prefix);
1217:     KSPSetOptionsPrefix(subksp,prefix);
1218:     KSPAppendOptionsPrefix(subksp,"up_");
1219:   }
1220:   return(0);
1221: }

1223: /* No new matrices are created, and the coarse operator matrices are the references to the original ones */
1224: PetscErrorCode  PCGetInterpolations_MG(PC pc,PetscInt *num_levels,Mat *interpolations[])
1225: {
1226:   PC_MG          *mg        = (PC_MG*)pc->data;
1227:   PC_MG_Levels   **mglevels = mg->levels;
1228:   Mat            *mat;
1229:   PetscInt       l;

1233:   if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
1234:   PetscMalloc1(mg->nlevels,&mat);
1235:   for (l=1; l< mg->nlevels; l++) {
1236:     mat[l-1] = mglevels[l]->interpolate;
1237:     PetscObjectReference((PetscObject)mat[l-1]);
1238:   }
1239:   *num_levels = mg->nlevels;
1240:   *interpolations = mat;
1241:   return(0);
1242: }

1244: /* No new matrices are created, and the coarse operator matrices are the references to the original ones */
1245: PetscErrorCode  PCGetCoarseOperators_MG(PC pc,PetscInt *num_levels,Mat *coarseOperators[])
1246: {
1247:   PC_MG          *mg        = (PC_MG*)pc->data;
1248:   PC_MG_Levels   **mglevels = mg->levels;
1249:   PetscInt       l;
1250:   Mat            *mat;

1254:   if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
1255:   PetscMalloc1(mg->nlevels,&mat);
1256:   for (l=0; l<mg->nlevels-1; l++) {
1257:     KSPGetOperators(mglevels[l]->smoothd,NULL,&(mat[l]));
1258:     PetscObjectReference((PetscObject)mat[l]);
1259:   }
1260:   *num_levels = mg->nlevels;
1261:   *coarseOperators = mat;
1262:   return(0);
1263: }

1265: /* ----------------------------------------------------------------------------------------*/

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

1271:    Options Database Keys:
1272: +  -pc_mg_levels <nlevels> - number of levels including finest
1273: .  -pc_mg_cycle_type <v,w> - provide the cycle desired
1274: .  -pc_mg_type <additive,multiplicative,full,kaskade> - multiplicative is the default
1275: .  -pc_mg_log - log information about time spent on each level of the solver
1276: .  -pc_mg_distinct_smoothup - configure up (after interpolation) and down (before restriction) smoothers separately (with different options prefixes)
1277: .  -pc_mg_galerkin <both,pmat,mat,none> - use Galerkin process to compute coarser operators, i.e. Acoarse = R A R'
1278: .  -pc_mg_multiplicative_cycles - number of cycles to use as the preconditioner (defaults to 1)
1279: .  -pc_mg_dump_matlab - dumps the matrices for each level and the restriction/interpolation matrices
1280:                         to the Socket viewer for reading from MATLAB.
1281: -  -pc_mg_dump_binary - dumps the matrices for each level and the restriction/interpolation matrices
1282:                         to the binary output file called binaryoutput

1284:    Notes:
1285:     If one uses a Krylov method such GMRES or CG as the smoother then one must use KSPFGMRES, KSPGCR, or KSPRICHARDSON as the outer Krylov method

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

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

1294:    Level: intermediate

1296: .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC, PCMGType, PCEXOTIC, PCGAMG, PCML, PCHYPRE
1297:            PCMGSetLevels(), PCMGGetLevels(), PCMGSetType(), PCMGSetCycleType(),
1298:            PCMGSetDistinctSmoothUp(), PCMGGetCoarseSolve(), PCMGSetResidual(), PCMGSetInterpolation(),
1299:            PCMGSetRestriction(), PCMGGetSmoother(), PCMGGetSmootherUp(), PCMGGetSmootherDown(),
1300:            PCMGSetCycleTypeOnLevel(), PCMGSetRhs(), PCMGSetX(), PCMGSetR()
1301: M*/

1303: PETSC_EXTERN PetscErrorCode PCCreate_MG(PC pc)
1304: {
1305:   PC_MG          *mg;

1309:   PetscNewLog(pc,&mg);
1310:   pc->data     = (void*)mg;
1311:   mg->nlevels  = -1;
1312:   mg->am       = PC_MG_MULTIPLICATIVE;
1313:   mg->galerkin = PC_MG_GALERKIN_NONE;

1315:   pc->useAmat = PETSC_TRUE;

1317:   pc->ops->apply          = PCApply_MG;
1318:   pc->ops->setup          = PCSetUp_MG;
1319:   pc->ops->reset          = PCReset_MG;
1320:   pc->ops->destroy        = PCDestroy_MG;
1321:   pc->ops->setfromoptions = PCSetFromOptions_MG;
1322:   pc->ops->view           = PCView_MG;

1324:   PetscObjectComposeFunction((PetscObject)pc,"PCMGSetGalerkin_C",PCMGSetGalerkin_MG);
1325:   PetscObjectComposeFunction((PetscObject)pc,"PCMGGetLevels_C",PCMGGetLevels_MG);
1326:   PetscObjectComposeFunction((PetscObject)pc,"PCMGSetLevels_C",PCMGSetLevels_MG);
1327:   PetscObjectComposeFunction((PetscObject)pc,"PCGetInterpolations_C",PCGetInterpolations_MG);
1328:   PetscObjectComposeFunction((PetscObject)pc,"PCGetCoarseOperators_C",PCGetCoarseOperators_MG);
1329:   return(0);
1330: }