Actual source code: mg.c

petsc-master 2019-07-16
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 seperate 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",0};
491: const char *const PCMGCycleTypes[] = {"invalid","v","w","PCMGCycleType","PC_MG_CYCLE",0};
492: const char *const PCMGGalerkinTypes[] = {"both","pmat","mat","none","external","PCMGGalerkinType","PC_MG_GALERKIN",0};

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 = 0;
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 && pc->dm->x) {
772:     /* need to restrict Jacobian location to coarser meshes for evaluation */
773:     for (i=n-2; i>-1; i--) {
774:       Mat R;
775:       Vec rscale;
776:       if (!mglevels[i]->smoothd->dm->x) {
777:         Vec *vecs;
778:         KSPCreateVecs(mglevels[i]->smoothd,1,&vecs,0,NULL);
779:         mglevels[i]->smoothd->dm->x = vecs[0];
780:         PetscFree(vecs);
781:       }
782:       PCMGGetRestriction(pc,i+1,&R);
783:       PCMGGetRScale(pc,i+1,&rscale);
784:       MatRestrict(R,mglevels[i+1]->smoothd->dm->x,mglevels[i]->smoothd->dm->x);
785:       VecPointwiseMult(mglevels[i]->smoothd->dm->x,mglevels[i]->smoothd->dm->x,rscale);
786:     }
787:   }
788:   if (needRestricts && pc->dm) {
789:     for (i=n-2; i>=0; i--) {
790:       DM  dmfine,dmcoarse;
791:       Mat Restrict,Inject;
792:       Vec rscale;
793:       KSPGetDM(mglevels[i+1]->smoothd,&dmfine);
794:       KSPGetDM(mglevels[i]->smoothd,&dmcoarse);
795:       PCMGGetRestriction(pc,i+1,&Restrict);
796:       PCMGGetRScale(pc,i+1,&rscale);
797:       PCMGGetInjection(pc,i+1,&Inject);
798:       DMRestrict(dmfine,Restrict,rscale,Inject,dmcoarse);
799:     }
800:   }

802:   if (!pc->setupcalled) {
803:     for (i=0; i<n; i++) {
804:       KSPSetFromOptions(mglevels[i]->smoothd);
805:     }
806:     for (i=1; i<n; i++) {
807:       if (mglevels[i]->smoothu && (mglevels[i]->smoothu != mglevels[i]->smoothd)) {
808:         KSPSetFromOptions(mglevels[i]->smoothu);
809:       }
810:     }
811:     /* insure that if either interpolation or restriction is set the other other one is set */
812:     for (i=1; i<n; i++) {
813:       PCMGGetInterpolation(pc,i,NULL);
814:       PCMGGetRestriction(pc,i,NULL);
815:     }
816:     for (i=0; i<n-1; i++) {
817:       if (!mglevels[i]->b) {
818:         Vec *vec;
819:         KSPCreateVecs(mglevels[i]->smoothd,1,&vec,0,NULL);
820:         PCMGSetRhs(pc,i,*vec);
821:         VecDestroy(vec);
822:         PetscFree(vec);
823:       }
824:       if (!mglevels[i]->r && i) {
825:         VecDuplicate(mglevels[i]->b,&tvec);
826:         PCMGSetR(pc,i,tvec);
827:         VecDestroy(&tvec);
828:       }
829:       if (!mglevels[i]->x) {
830:         VecDuplicate(mglevels[i]->b,&tvec);
831:         PCMGSetX(pc,i,tvec);
832:         VecDestroy(&tvec);
833:       }
834:     }
835:     if (n != 1 && !mglevels[n-1]->r) {
836:       /* PCMGSetR() on the finest level if user did not supply it */
837:       Vec *vec;
838:       KSPCreateVecs(mglevels[n-1]->smoothd,1,&vec,0,NULL);
839:       PCMGSetR(pc,n-1,*vec);
840:       VecDestroy(vec);
841:       PetscFree(vec);
842:     }
843:   }

845:   if (pc->dm) {
846:     /* need to tell all the coarser levels to rebuild the matrix using the DM for that level */
847:     for (i=0; i<n-1; i++) {
848:       if (mglevels[i]->smoothd->setupstage != KSP_SETUP_NEW) mglevels[i]->smoothd->setupstage = KSP_SETUP_NEWMATRIX;
849:     }
850:   }

852:   for (i=1; i<n; i++) {
853:     if (mglevels[i]->smoothu == mglevels[i]->smoothd || mg->am == PC_MG_FULL || mg->am == PC_MG_KASKADE || mg->cyclesperpcapply > 1){
854:       /* if doing only down then initial guess is zero */
855:       KSPSetInitialGuessNonzero(mglevels[i]->smoothd,PETSC_TRUE);
856:     }
857:     if (mglevels[i]->eventsmoothsetup) {PetscLogEventBegin(mglevels[i]->eventsmoothsetup,0,0,0,0);}
858:     KSPSetUp(mglevels[i]->smoothd);
859:     if (mglevels[i]->smoothd->reason == KSP_DIVERGED_PC_FAILED) {
860:       pc->failedreason = PC_SUBPC_ERROR;
861:     }
862:     if (mglevels[i]->eventsmoothsetup) {PetscLogEventEnd(mglevels[i]->eventsmoothsetup,0,0,0,0);}
863:     if (!mglevels[i]->residual) {
864:       Mat mat;
865:       KSPGetOperators(mglevels[i]->smoothd,&mat,NULL);
866:       PCMGSetResidual(pc,i,PCMGResidualDefault,mat);
867:     }
868:   }
869:   for (i=1; i<n; i++) {
870:     if (mglevels[i]->smoothu && mglevels[i]->smoothu != mglevels[i]->smoothd) {
871:       Mat downmat,downpmat;

873:       /* check if operators have been set for up, if not use down operators to set them */
874:       KSPGetOperatorsSet(mglevels[i]->smoothu,&opsset,NULL);
875:       if (!opsset) {
876:         KSPGetOperators(mglevels[i]->smoothd,&downmat,&downpmat);
877:         KSPSetOperators(mglevels[i]->smoothu,downmat,downpmat);
878:       }

880:       KSPSetInitialGuessNonzero(mglevels[i]->smoothu,PETSC_TRUE);
881:       if (mglevels[i]->eventsmoothsetup) {PetscLogEventBegin(mglevels[i]->eventsmoothsetup,0,0,0,0);}
882:       KSPSetUp(mglevels[i]->smoothu);
883:       if (mglevels[i]->smoothu->reason == KSP_DIVERGED_PC_FAILED) {
884:         pc->failedreason = PC_SUBPC_ERROR;
885:       }
886:       if (mglevels[i]->eventsmoothsetup) {PetscLogEventEnd(mglevels[i]->eventsmoothsetup,0,0,0,0);}
887:     }
888:   }

890:   if (mglevels[0]->eventsmoothsetup) {PetscLogEventBegin(mglevels[0]->eventsmoothsetup,0,0,0,0);}
891:   KSPSetUp(mglevels[0]->smoothd);
892:   if (mglevels[0]->smoothd->reason == KSP_DIVERGED_PC_FAILED) {
893:     pc->failedreason = PC_SUBPC_ERROR;
894:   }
895:   if (mglevels[0]->eventsmoothsetup) {PetscLogEventEnd(mglevels[0]->eventsmoothsetup,0,0,0,0);}

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

902:    Only support one or the other at the same time.
903:   */
904: #if defined(PETSC_USE_SOCKET_VIEWER)
905:   PetscOptionsGetBool(((PetscObject)pc)->options,((PetscObject)pc)->prefix,"-pc_mg_dump_matlab",&dump,NULL);
906:   if (dump) viewer = PETSC_VIEWER_SOCKET_(PetscObjectComm((PetscObject)pc));
907:   dump = PETSC_FALSE;
908: #endif
909:   PetscOptionsGetBool(((PetscObject)pc)->options,((PetscObject)pc)->prefix,"-pc_mg_dump_binary",&dump,NULL);
910:   if (dump) viewer = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)pc));

912:   if (viewer) {
913:     for (i=1; i<n; i++) {
914:       MatView(mglevels[i]->restrct,viewer);
915:     }
916:     for (i=0; i<n; i++) {
917:       KSPGetPC(mglevels[i]->smoothd,&pc);
918:       MatView(pc->mat,viewer);
919:     }
920:   }
921:   return(0);
922: }

924: /* -------------------------------------------------------------------------------------*/

926: PetscErrorCode PCMGGetLevels_MG(PC pc, PetscInt *levels)
927: {
928:   PC_MG *mg = (PC_MG *) pc->data;

931:   *levels = mg->nlevels;
932:   return(0);
933: }

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

938:    Not Collective

940:    Input Parameter:
941: .  pc - the preconditioner context

943:    Output parameter:
944: .  levels - the number of levels

946:    Level: advanced

948: .seealso: PCMGSetLevels()
949: @*/
950: PetscErrorCode PCMGGetLevels(PC pc,PetscInt *levels)
951: {

957:   *levels = 0;
958:   PetscTryMethod(pc,"PCMGGetLevels_C",(PC,PetscInt*),(pc,levels));
959:   return(0);
960: }

962: /*@
963:    PCMGSetType - Determines the form of multigrid to use:
964:    multiplicative, additive, full, or the Kaskade algorithm.

966:    Logically Collective on PC

968:    Input Parameters:
969: +  pc - the preconditioner context
970: -  form - multigrid form, one of PC_MG_MULTIPLICATIVE, PC_MG_ADDITIVE,
971:    PC_MG_FULL, PC_MG_KASKADE

973:    Options Database Key:
974: .  -pc_mg_type <form> - Sets <form>, one of multiplicative,
975:    additive, full, kaskade

977:    Level: advanced

979: .seealso: PCMGSetLevels()
980: @*/
981: PetscErrorCode  PCMGSetType(PC pc,PCMGType form)
982: {
983:   PC_MG *mg = (PC_MG*)pc->data;

988:   mg->am = form;
989:   if (form == PC_MG_MULTIPLICATIVE) pc->ops->applyrichardson = PCApplyRichardson_MG;
990:   else pc->ops->applyrichardson = NULL;
991:   return(0);
992: }

994: /*@
995:    PCMGGetType - Determines the form of multigrid to use:
996:    multiplicative, additive, full, or the Kaskade algorithm.

998:    Logically Collective on PC

1000:    Input Parameter:
1001: .  pc - the preconditioner context

1003:    Output Parameter:
1004: .  type - one of PC_MG_MULTIPLICATIVE, PC_MG_ADDITIVE,PC_MG_FULL, PC_MG_KASKADE


1007:    Level: advanced

1009: .seealso: PCMGSetLevels()
1010: @*/
1011: PetscErrorCode  PCMGGetType(PC pc,PCMGType *type)
1012: {
1013:   PC_MG *mg = (PC_MG*)pc->data;

1017:   *type = mg->am;
1018:   return(0);
1019: }

1021: /*@
1022:    PCMGSetCycleType - Sets the type cycles to use.  Use PCMGSetCycleTypeOnLevel() for more
1023:    complicated cycling.

1025:    Logically Collective on PC

1027:    Input Parameters:
1028: +  pc - the multigrid context
1029: -  n - either PC_MG_CYCLE_V or PC_MG_CYCLE_W

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

1034:    Level: advanced

1036: .seealso: PCMGSetCycleTypeOnLevel()
1037: @*/
1038: PetscErrorCode  PCMGSetCycleType(PC pc,PCMGCycleType n)
1039: {
1040:   PC_MG        *mg        = (PC_MG*)pc->data;
1041:   PC_MG_Levels **mglevels = mg->levels;
1042:   PetscInt     i,levels;

1047:   if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ORDER,"Must set MG levels with PCMGSetLevels() before calling");
1048:   levels = mglevels[0]->levels;
1049:   for (i=0; i<levels; i++) mglevels[i]->cycles = n;
1050:   return(0);
1051: }

1053: /*@
1054:    PCMGMultiplicativeSetCycles - Sets the number of cycles to use for each preconditioner step
1055:          of multigrid when PCMGType of PC_MG_MULTIPLICATIVE is used

1057:    Logically Collective on PC

1059:    Input Parameters:
1060: +  pc - the multigrid context
1061: -  n - number of cycles (default is 1)

1063:    Options Database Key:
1064: .  -pc_mg_multiplicative_cycles n

1066:    Level: advanced

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

1071: .seealso: PCMGSetCycleTypeOnLevel(), PCMGSetCycleType()
1072: @*/
1073: PetscErrorCode  PCMGMultiplicativeSetCycles(PC pc,PetscInt n)
1074: {
1075:   PC_MG        *mg        = (PC_MG*)pc->data;

1080:   mg->cyclesperpcapply = n;
1081:   return(0);
1082: }

1084: PetscErrorCode PCMGSetGalerkin_MG(PC pc,PCMGGalerkinType use)
1085: {
1086:   PC_MG *mg = (PC_MG*)pc->data;

1089:   mg->galerkin = use;
1090:   return(0);
1091: }

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

1097:    Logically Collective on PC

1099:    Input Parameters:
1100: +  pc - the multigrid context
1101: -  use - one of PC_MG_GALERKIN_BOTH,PC_MG_GALERKIN_PMAT,PC_MG_GALERKIN_MAT, or PC_MG_GALERKIN_NONE

1103:    Options Database Key:
1104: .  -pc_mg_galerkin <both,pmat,mat,none>

1106:    Level: intermediate

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

1112: .seealso: PCMGGetGalerkin(), PCMGGalerkinType

1114: @*/
1115: PetscErrorCode PCMGSetGalerkin(PC pc,PCMGGalerkinType use)
1116: {

1121:   PetscTryMethod(pc,"PCMGSetGalerkin_C",(PC,PCMGGalerkinType),(pc,use));
1122:   return(0);
1123: }

1125: /*@
1126:    PCMGGetGalerkin - Checks if Galerkin multigrid is being used, i.e.
1127:       A_i-1 = r_i * A_i * p_i

1129:    Not Collective

1131:    Input Parameter:
1132: .  pc - the multigrid context

1134:    Output Parameter:
1135: .  galerkin - one of PC_MG_GALERKIN_BOTH,PC_MG_GALERKIN_PMAT,PC_MG_GALERKIN_MAT, PC_MG_GALERKIN_NONE, or PC_MG_GALERKIN_EXTERNAL

1137:    Level: intermediate

1139: .seealso: PCMGSetGalerkin(), PCMGGalerkinType

1141: @*/
1142: PetscErrorCode  PCMGGetGalerkin(PC pc,PCMGGalerkinType  *galerkin)
1143: {
1144:   PC_MG *mg = (PC_MG*)pc->data;

1148:   *galerkin = mg->galerkin;
1149:   return(0);
1150: }

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

1157:    Logically Collective on PC

1159:    Input Parameters:
1160: +  mg - the multigrid context
1161: -  n - the number of smoothing steps

1163:    Options Database Key: 
1164: .  -mg_levels_ksp_max_it <n> - Sets number of pre and post-smoothing steps

1166:    Level: advanced

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

1172: .seealso: PCMGSetDistinctSmoothUp()
1173: @*/
1174: PetscErrorCode  PCMGSetNumberSmooth(PC pc,PetscInt n)
1175: {
1176:   PC_MG          *mg        = (PC_MG*)pc->data;
1177:   PC_MG_Levels   **mglevels = mg->levels;
1179:   PetscInt       i,levels;

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

1187:   for (i=1; i<levels; i++) {
1188:     KSPSetTolerances(mglevels[i]->smoothu,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,n);
1189:     KSPSetTolerances(mglevels[i]->smoothd,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,n);
1190:     mg->default_smoothu = n;
1191:     mg->default_smoothd = n;
1192:   }
1193:   return(0);
1194: }

1196: /*@
1197:    PCMGSetDistinctSmoothUp - sets the up (post) smoother to be a seperate KSP from the down (pre) smoother on all levels
1198:        and adds the suffix _up to the options name

1200:    Logically Collective on PC

1202:    Input Parameters:
1203: .  pc - the preconditioner context

1205:    Options Database Key:
1206: .  -pc_mg_distinct_smoothup

1208:    Level: advanced

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

1214: .seealso: PCMGSetNumberSmooth()
1215: @*/
1216: PetscErrorCode  PCMGSetDistinctSmoothUp(PC pc)
1217: {
1218:   PC_MG          *mg        = (PC_MG*)pc->data;
1219:   PC_MG_Levels   **mglevels = mg->levels;
1221:   PetscInt       i,levels;
1222:   KSP            subksp;

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

1229:   for (i=1; i<levels; i++) {
1230:     const char *prefix = NULL;
1231:     /* make sure smoother up and down are different */
1232:     PCMGGetSmootherUp(pc,i,&subksp);
1233:     KSPGetOptionsPrefix(mglevels[i]->smoothd,&prefix);
1234:     KSPSetOptionsPrefix(subksp,prefix);
1235:     KSPAppendOptionsPrefix(subksp,"up_");
1236:   }
1237:   return(0);
1238: }

1240: /* No new matrices are created, and the coarse operator matrices are the references to the original ones */
1241: PetscErrorCode  PCGetInterpolations_MG(PC pc,PetscInt *num_levels,Mat *interpolations[])
1242: {
1243:   PC_MG          *mg        = (PC_MG*)pc->data;
1244:   PC_MG_Levels   **mglevels = mg->levels;
1245:   Mat            *mat;
1246:   PetscInt       l;

1250:   if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
1251:   PetscMalloc1(mg->nlevels,&mat);
1252:   for (l=1; l< mg->nlevels; l++) {
1253:     mat[l-1] = mglevels[l]->interpolate;
1254:     PetscObjectReference((PetscObject)mat[l-1]);
1255:   }
1256:   *num_levels = mg->nlevels;
1257:   *interpolations = mat;
1258:   return(0);
1259: }

1261: /* No new matrices are created, and the coarse operator matrices are the references to the original ones */
1262: PetscErrorCode  PCGetCoarseOperators_MG(PC pc,PetscInt *num_levels,Mat *coarseOperators[])
1263: {
1264:   PC_MG          *mg        = (PC_MG*)pc->data;
1265:   PC_MG_Levels   **mglevels = mg->levels;
1266:   PetscInt       l;
1267:   Mat            *mat;

1271:   if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
1272:   PetscMalloc1(mg->nlevels,&mat);
1273:   for (l=0; l<mg->nlevels-1; l++) {
1274:     KSPGetOperators(mglevels[l]->smoothd,NULL,&(mat[l]));
1275:     PetscObjectReference((PetscObject)mat[l]);
1276:   }
1277:   *num_levels = mg->nlevels;
1278:   *coarseOperators = mat;
1279:   return(0);
1280: }

1282: /* ----------------------------------------------------------------------------------------*/

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

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

1301:    Notes:
1302:     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

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

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

1311:    Level: intermediate

1313: .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC, PCMGType, PCEXOTIC, PCGAMG, PCML, PCHYPRE
1314:            PCMGSetLevels(), PCMGGetLevels(), PCMGSetType(), PCMGSetCycleType(),
1315:            PCMGSetDistinctSmoothUp(), PCMGGetCoarseSolve(), PCMGSetResidual(), PCMGSetInterpolation(),
1316:            PCMGSetRestriction(), PCMGGetSmoother(), PCMGGetSmootherUp(), PCMGGetSmootherDown(),
1317:            PCMGSetCycleTypeOnLevel(), PCMGSetRhs(), PCMGSetX(), PCMGSetR()
1318: M*/

1320: PETSC_EXTERN PetscErrorCode PCCreate_MG(PC pc)
1321: {
1322:   PC_MG          *mg;

1326:   PetscNewLog(pc,&mg);
1327:   pc->data     = (void*)mg;
1328:   mg->nlevels  = -1;
1329:   mg->am       = PC_MG_MULTIPLICATIVE;
1330:   mg->galerkin = PC_MG_GALERKIN_NONE;

1332:   pc->useAmat = PETSC_TRUE;

1334:   pc->ops->apply          = PCApply_MG;
1335:   pc->ops->setup          = PCSetUp_MG;
1336:   pc->ops->reset          = PCReset_MG;
1337:   pc->ops->destroy        = PCDestroy_MG;
1338:   pc->ops->setfromoptions = PCSetFromOptions_MG;
1339:   pc->ops->view           = PCView_MG;

1341:   PetscObjectComposeFunction((PetscObject)pc,"PCMGSetGalerkin_C",PCMGSetGalerkin_MG);
1342:   PetscObjectComposeFunction((PetscObject)pc,"PCMGGetLevels_C",PCMGGetLevels_MG);
1343:   PetscObjectComposeFunction((PetscObject)pc,"PCMGSetLevels_C",PCMGSetLevels_MG);
1344:   PetscObjectComposeFunction((PetscObject)pc,"PCGetInterpolations_C",PCGetInterpolations_MG);
1345:   PetscObjectComposeFunction((PetscObject)pc,"PCGetCoarseOperators_C",PCGetCoarseOperators_MG);
1346:   return(0);
1347: }