Actual source code: mg.c

petsc-master 2020-11-28
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: /*
 10:    Contains the list of registered coarse space construction routines
 11: */
 12: PetscFunctionList PCMGCoarseList = NULL;

 14: PetscErrorCode PCMGMCycle_Private(PC pc,PC_MG_Levels **mglevelsin,PCRichardsonConvergedReason *reason)
 15: {
 16:   PC_MG          *mg = (PC_MG*)pc->data;
 17:   PC_MG_Levels   *mgc,*mglevels = *mglevelsin;
 19:   PetscInt       cycles = (mglevels->level == 1) ? 1 : (PetscInt) mglevels->cycles;

 22:   if (mglevels->eventsmoothsolve) {PetscLogEventBegin(mglevels->eventsmoothsolve,0,0,0,0);}
 23:   KSPSolve(mglevels->smoothd,mglevels->b,mglevels->x);  /* pre-smooth */
 24:   KSPCheckSolve(mglevels->smoothd,pc,mglevels->x);
 25:   if (mglevels->eventsmoothsolve) {PetscLogEventEnd(mglevels->eventsmoothsolve,0,0,0,0);}
 26:   if (mglevels->level) {  /* not the coarsest grid */
 27:     if (mglevels->eventresidual) {PetscLogEventBegin(mglevels->eventresidual,0,0,0,0);}
 28:     (*mglevels->residual)(mglevels->A,mglevels->b,mglevels->x,mglevels->r);
 29:     if (mglevels->eventresidual) {PetscLogEventEnd(mglevels->eventresidual,0,0,0,0);}

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

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

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

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

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

 95:       KSPCreateVecs(mglevels[levels-1]->smoothd,1,&vec,0,NULL);
 96:       mglevels[levels-1]->b = *vec;
 97:       PetscFree(vec);
 98:     }
 99:     VecCopy(b,mglevels[levels-1]->b);
100:   }
101:   mglevels[levels-1]->x = x;

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

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

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

141: PetscErrorCode PCReset_MG(PC pc)
142: {
143:   PC_MG          *mg        = (PC_MG*)pc->data;
144:   PC_MG_Levels   **mglevels = mg->levels;
146:   PetscInt       i,c,n;

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

164:     for (i=0; i<n; i++) {
165:       if (mglevels[i]->coarseSpace) for (c = 0; c < mg->Nc; ++c) {VecDestroy(&mglevels[i]->coarseSpace[c]);}
166:       PetscFree(mglevels[i]->coarseSpace);
167:       mglevels[i]->coarseSpace = NULL;
168:       MatDestroy(&mglevels[i]->A);
169:       if (mglevels[i]->smoothd != mglevels[i]->smoothu) {
170:         KSPReset(mglevels[i]->smoothd);
171:       }
172:       KSPReset(mglevels[i]->smoothu);
173:     }
174:     mg->Nc = 0;
175:   }
176:   return(0);
177: }

179: PetscErrorCode PCMGSetLevels_MG(PC pc,PetscInt levels,MPI_Comm *comms)
180: {
182:   PC_MG          *mg        = (PC_MG*)pc->data;
183:   MPI_Comm       comm;
184:   PC_MG_Levels   **mglevels = mg->levels;
185:   PCMGType       mgtype     = mg->am;
186:   PetscInt       mgctype    = (PetscInt) PC_MG_CYCLE_V;
187:   PetscInt       i;
188:   PetscMPIInt    size;
189:   const char     *prefix;
190:   PC             ipc;
191:   PetscInt       n;

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

213:   mg->nlevels = levels;

215:   PetscMalloc1(levels,&mglevels);
216:   PetscLogObjectMemory((PetscObject)pc,levels*(sizeof(PC_MG*)));

218:   PCGetOptionsPrefix(pc,&prefix);

220:   mg->stageApply = 0;
221:   for (i=0; i<levels; i++) {
222:     PetscNewLog(pc,&mglevels[i]);

224:     mglevels[i]->level               = i;
225:     mglevels[i]->levels              = levels;
226:     mglevels[i]->cycles              = mgctype;
227:     mg->default_smoothu              = 2;
228:     mg->default_smoothd              = 2;
229:     mglevels[i]->eventsmoothsetup    = 0;
230:     mglevels[i]->eventsmoothsolve    = 0;
231:     mglevels[i]->eventresidual       = 0;
232:     mglevels[i]->eventinterprestrict = 0;

234:     if (comms) comm = comms[i];
235:     KSPCreate(comm,&mglevels[i]->smoothd);
236:     KSPSetErrorIfNotConverged(mglevels[i]->smoothd,pc->erroriffailure);
237:     PetscObjectIncrementTabLevel((PetscObject)mglevels[i]->smoothd,(PetscObject)pc,levels-i);
238:     KSPSetOptionsPrefix(mglevels[i]->smoothd,prefix);
239:     PetscObjectComposedDataSetInt((PetscObject) mglevels[i]->smoothd, PetscMGLevelId, mglevels[i]->level);
240:     if (i || levels == 1) {
241:       char tprefix[128];

243:       KSPSetType(mglevels[i]->smoothd,KSPCHEBYSHEV);
244:       KSPSetConvergenceTest(mglevels[i]->smoothd,KSPConvergedSkip,NULL,NULL);
245:       KSPSetNormType(mglevels[i]->smoothd,KSP_NORM_NONE);
246:       KSPGetPC(mglevels[i]->smoothd,&ipc);
247:       PCSetType(ipc,PCSOR);
248:       KSPSetTolerances(mglevels[i]->smoothd,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT, mg->default_smoothd);

250:       sprintf(tprefix,"mg_levels_%d_",(int)i);
251:       KSPAppendOptionsPrefix(mglevels[i]->smoothd,tprefix);
252:     } else {
253:       KSPAppendOptionsPrefix(mglevels[0]->smoothd,"mg_coarse_");

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

268:     mglevels[i]->smoothu = mglevels[i]->smoothd;
269:     mg->rtol             = 0.0;
270:     mg->abstol           = 0.0;
271:     mg->dtol             = 0.0;
272:     mg->ttol             = 0.0;
273:     mg->cyclesperpcapply = 1;
274:   }
275:   mg->levels = mglevels;
276:   PCMGSetType(pc,mgtype);
277:   return(0);
278: }

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

284:    Logically Collective on PC

286:    Input Parameters:
287: +  pc - the preconditioner context
288: .  levels - the number of levels
289: -  comms - optional communicators for each level; this is to allow solving the coarser problems
290:            on smaller sets of processors.

292:    Level: intermediate

294:    Notes:
295:      If the number of levels is one then the multigrid uses the -mg_levels prefix
296:   for setting the level options rather than the -mg_coarse prefix.

298: .seealso: PCMGSetType(), PCMGGetLevels()
299: @*/
300: PetscErrorCode PCMGSetLevels(PC pc,PetscInt levels,MPI_Comm *comms)
301: {

307:   PetscTryMethod(pc,"PCMGSetLevels_C",(PC,PetscInt,MPI_Comm*),(pc,levels,comms));
308:   return(0);
309: }


312: PetscErrorCode PCDestroy_MG(PC pc)
313: {
315:   PC_MG          *mg        = (PC_MG*)pc->data;
316:   PC_MG_Levels   **mglevels = mg->levels;
317:   PetscInt       i,n;

320:   PCReset_MG(pc);
321:   if (mglevels) {
322:     n = mglevels[0]->levels;
323:     for (i=0; i<n; i++) {
324:       if (mglevels[i]->smoothd != mglevels[i]->smoothu) {
325:         KSPDestroy(&mglevels[i]->smoothd);
326:       }
327:       KSPDestroy(&mglevels[i]->smoothu);
328:       PetscFree(mglevels[i]);
329:     }
330:     PetscFree(mg->levels);
331:   }
332:   PetscFree(pc->data);
333:   PetscObjectComposeFunction((PetscObject)pc,"PCGetInterpolations_C",NULL);
334:   PetscObjectComposeFunction((PetscObject)pc,"PCGetCoarseOperators_C",NULL);
335:   return(0);
336: }



340: extern PetscErrorCode PCMGACycle_Private(PC,PC_MG_Levels**);
341: extern PetscErrorCode PCMGFCycle_Private(PC,PC_MG_Levels**);
342: extern PetscErrorCode PCMGKCycle_Private(PC,PC_MG_Levels**);

344: /*
345:    PCApply_MG - Runs either an additive, multiplicative, Kaskadic
346:              or full cycle of multigrid.

348:   Note:
349:   A simple wrapper which calls PCMGMCycle(),PCMGACycle(), or PCMGFCycle().
350: */
351: static PetscErrorCode PCApply_MG(PC pc,Vec b,Vec x)
352: {
353:   PC_MG          *mg        = (PC_MG*)pc->data;
354:   PC_MG_Levels   **mglevels = mg->levels;
356:   PC             tpc;
357:   PetscInt       levels = mglevels[0]->levels,i;
358:   PetscBool      changeu,changed;

361:   if (mg->stageApply) {PetscLogStagePush(mg->stageApply);}
362:   /* When the DM is supplying the matrix then it will not exist until here */
363:   for (i=0; i<levels; i++) {
364:     if (!mglevels[i]->A) {
365:       KSPGetOperators(mglevels[i]->smoothu,&mglevels[i]->A,NULL);
366:       PetscObjectReference((PetscObject)mglevels[i]->A);
367:     }
368:   }

370:   KSPGetPC(mglevels[levels-1]->smoothd,&tpc);
371:   PCPreSolveChangeRHS(tpc,&changed);
372:   KSPGetPC(mglevels[levels-1]->smoothu,&tpc);
373:   PCPreSolveChangeRHS(tpc,&changeu);
374:   if (!changeu && !changed) {
375:     VecDestroy(&mglevels[levels-1]->b);
376:     mglevels[levels-1]->b = b;
377:   } else { /* if the smoother changes the rhs during PreSolve, we cannot use the input vector */
378:     if (!mglevels[levels-1]->b) {
379:       Vec *vec;

381:       KSPCreateVecs(mglevels[levels-1]->smoothd,1,&vec,0,NULL);
382:       mglevels[levels-1]->b = *vec;
383:       PetscFree(vec);
384:     }
385:     VecCopy(b,mglevels[levels-1]->b);
386:   }
387:   mglevels[levels-1]->x = x;

389:   if (mg->am == PC_MG_MULTIPLICATIVE) {
390:     VecSet(x,0.0);
391:     for (i=0; i<mg->cyclesperpcapply; i++) {
392:       PCMGMCycle_Private(pc,mglevels+levels-1,NULL);
393:     }
394:   } else if (mg->am == PC_MG_ADDITIVE) {
395:     PCMGACycle_Private(pc,mglevels);
396:   } else if (mg->am == PC_MG_KASKADE) {
397:     PCMGKCycle_Private(pc,mglevels);
398:   } else {
399:     PCMGFCycle_Private(pc,mglevels);
400:   }
401:   if (mg->stageApply) {PetscLogStagePop();}
402:   if (!changeu && !changed) mglevels[levels-1]->b = NULL;
403:   return(0);
404: }


407: PetscErrorCode PCSetFromOptions_MG(PetscOptionItems *PetscOptionsObject,PC pc)
408: {
409:   PetscErrorCode   ierr;
410:   PetscInt         levels,cycles;
411:   PetscBool        flg, flg2;
412:   PC_MG            *mg = (PC_MG*)pc->data;
413:   PC_MG_Levels     **mglevels;
414:   PCMGType         mgtype;
415:   PCMGCycleType    mgctype;
416:   PCMGGalerkinType gtype;

419:   levels = PetscMax(mg->nlevels,1);
420:   PetscOptionsHead(PetscOptionsObject,"Multigrid options");
421:   PetscOptionsInt("-pc_mg_levels","Number of Levels","PCMGSetLevels",levels,&levels,&flg);
422:   if (!flg && !mg->levels && pc->dm) {
423:     DMGetRefineLevel(pc->dm,&levels);
424:     levels++;
425:     mg->usedmfornumberoflevels = PETSC_TRUE;
426:   }
427:   PCMGSetLevels(pc,levels,NULL);
428:   mglevels = mg->levels;

430:   mgctype = (PCMGCycleType) mglevels[0]->cycles;
431:   PetscOptionsEnum("-pc_mg_cycle_type","V cycle or for W-cycle","PCMGSetCycleType",PCMGCycleTypes,(PetscEnum)mgctype,(PetscEnum*)&mgctype,&flg);
432:   if (flg) {
433:     PCMGSetCycleType(pc,mgctype);
434:   }
435:   gtype = mg->galerkin;
436:   PetscOptionsEnum("-pc_mg_galerkin","Use Galerkin process to compute coarser operators","PCMGSetGalerkin",PCMGGalerkinTypes,(PetscEnum)gtype,(PetscEnum*)&gtype,&flg);
437:   if (flg) {
438:     PCMGSetGalerkin(pc,gtype);
439:   }
440:   flg2 = PETSC_FALSE;
441:   PetscOptionsBool("-pc_mg_adapt_interp","Adapt interpolation using some coarse space","PCMGSetAdaptInterpolation",PETSC_FALSE,&flg2,&flg);
442:   if (flg) {PCMGSetAdaptInterpolation(pc, flg2);}
443:   PetscOptionsInt("-pc_mg_adapt_interp_n","Size of the coarse space for adaptive interpolation","PCMGSetCoarseSpace",mg->Nc,&mg->Nc,&flg);
444:   PetscOptionsEnum("-pc_mg_adapt_interp_coarse_space","Type of coarse space: polynomial, harmonic, eigenvector, generalized_eigenvector","PCMGSetAdaptCoarseSpaceType",PCMGCoarseSpaceTypes,(PetscEnum)mg->coarseSpaceType,(PetscEnum*)&mg->coarseSpaceType,&flg);
445:   PetscOptionsBool("-pc_mg_mesp_monitor","Monitor the multilevel eigensolver","PCMGSetAdaptInterpolation",PETSC_FALSE,&mg->mespMonitor,&flg);
446:   flg = PETSC_FALSE;
447:   PetscOptionsBool("-pc_mg_distinct_smoothup","Create separate smoothup KSP and append the prefix _up","PCMGSetDistinctSmoothUp",PETSC_FALSE,&flg,NULL);
448:   if (flg) {
449:     PCMGSetDistinctSmoothUp(pc);
450:   }
451:   mgtype = mg->am;
452:   PetscOptionsEnum("-pc_mg_type","Multigrid type","PCMGSetType",PCMGTypes,(PetscEnum)mgtype,(PetscEnum*)&mgtype,&flg);
453:   if (flg) {
454:     PCMGSetType(pc,mgtype);
455:   }
456:   if (mg->am == PC_MG_MULTIPLICATIVE) {
457:     PetscOptionsInt("-pc_mg_multiplicative_cycles","Number of cycles for each preconditioner step","PCMGMultiplicativeSetCycles",mg->cyclesperpcapply,&cycles,&flg);
458:     if (flg) {
459:       PCMGMultiplicativeSetCycles(pc,cycles);
460:     }
461:   }
462:   flg  = PETSC_FALSE;
463:   PetscOptionsBool("-pc_mg_log","Log times for each multigrid level","None",flg,&flg,NULL);
464:   if (flg) {
465:     PetscInt i;
466:     char     eventname[128];

468:     levels = mglevels[0]->levels;
469:     for (i=0; i<levels; i++) {
470:       sprintf(eventname,"MGSetup Level %d",(int)i);
471:       PetscLogEventRegister(eventname,((PetscObject)pc)->classid,&mglevels[i]->eventsmoothsetup);
472:       sprintf(eventname,"MGSmooth Level %d",(int)i);
473:       PetscLogEventRegister(eventname,((PetscObject)pc)->classid,&mglevels[i]->eventsmoothsolve);
474:       if (i) {
475:         sprintf(eventname,"MGResid Level %d",(int)i);
476:         PetscLogEventRegister(eventname,((PetscObject)pc)->classid,&mglevels[i]->eventresidual);
477:         sprintf(eventname,"MGInterp Level %d",(int)i);
478:         PetscLogEventRegister(eventname,((PetscObject)pc)->classid,&mglevels[i]->eventinterprestrict);
479:       }
480:     }

482: #if defined(PETSC_USE_LOG)
483:     {
484:       const char    *sname = "MG Apply";
485:       PetscStageLog stageLog;
486:       PetscInt      st;

488:       PetscLogGetStageLog(&stageLog);
489:       for (st = 0; st < stageLog->numStages; ++st) {
490:         PetscBool same;

492:         PetscStrcmp(stageLog->stageInfo[st].name, sname, &same);
493:         if (same) mg->stageApply = st;
494:       }
495:       if (!mg->stageApply) {
496:         PetscLogStageRegister(sname, &mg->stageApply);
497:       }
498:     }
499: #endif
500:   }
501:   PetscOptionsTail();
502:   /* Check option consistency */
503:   PCMGGetGalerkin(pc, &gtype);
504:   PCMGGetAdaptInterpolation(pc, &flg);
505:   if (flg && (gtype >= PC_MG_GALERKIN_NONE)) SETERRQ(PetscObjectComm((PetscObject) pc), PETSC_ERR_ARG_INCOMP, "Must use Galerkin coarse operators when adapting the interpolator");
506:   return(0);
507: }

509: const char *const PCMGTypes[] = {"MULTIPLICATIVE","ADDITIVE","FULL","KASKADE","PCMGType","PC_MG",NULL};
510: const char *const PCMGCycleTypes[] = {"invalid","v","w","PCMGCycleType","PC_MG_CYCLE",NULL};
511: const char *const PCMGGalerkinTypes[] = {"both","pmat","mat","none","external","PCMGGalerkinType","PC_MG_GALERKIN",NULL};
512: const char *const PCMGCoarseSpaceTypes[] = {"polynomial","harmonic","eigenvector","generalized_eigenvector","PCMGCoarseSpaceType","PCMG_POLYNOMIAL",NULL};

514: #include <petscdraw.h>
515: PetscErrorCode PCView_MG(PC pc,PetscViewer viewer)
516: {
517:   PC_MG          *mg        = (PC_MG*)pc->data;
518:   PC_MG_Levels   **mglevels = mg->levels;
520:   PetscInt       levels = mglevels ? mglevels[0]->levels : 0,i;
521:   PetscBool      iascii,isbinary,isdraw;

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

600: #include <petsc/private/kspimpl.h>

602: /*
603:     Calls setup for the KSP on each level
604: */
605: PetscErrorCode PCSetUp_MG(PC pc)
606: {
607:   PC_MG          *mg        = (PC_MG*)pc->data;
608:   PC_MG_Levels   **mglevels = mg->levels;
610:   PetscInt       i,n;
611:   PC             cpc;
612:   PetscBool      dump = PETSC_FALSE,opsset,use_amat,missinginterpolate = PETSC_FALSE;
613:   Mat            dA,dB;
614:   Vec            tvec;
615:   DM             *dms;
616:   PetscViewer    viewer = NULL;
617:   PetscBool      dAeqdB = PETSC_FALSE, needRestricts = PETSC_FALSE;

620:   if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels with PCMGSetLevels() before setting up");
621:   n = mglevels[0]->levels;
622:   /* FIX: Move this to PCSetFromOptions_MG? */
623:   if (mg->usedmfornumberoflevels) {
624:     PetscInt levels;
625:     DMGetRefineLevel(pc->dm,&levels);
626:     levels++;
627:     if (levels > n) { /* the problem is now being solved on a finer grid */
628:       PCMGSetLevels(pc,levels,NULL);
629:       n        = levels;
630:       PCSetFromOptions(pc); /* it is bad to call this here, but otherwise will never be called for the new hierarchy */
631:       mglevels = mg->levels;
632:     }
633:   }
634:   KSPGetPC(mglevels[0]->smoothd,&cpc);


637:   /* If user did not provide fine grid operators OR operator was not updated since last global KSPSetOperators() */
638:   /* so use those from global PC */
639:   /* Is this what we always want? What if user wants to keep old one? */
640:   KSPGetOperatorsSet(mglevels[n-1]->smoothd,NULL,&opsset);
641:   if (opsset) {
642:     Mat mmat;
643:     KSPGetOperators(mglevels[n-1]->smoothd,NULL,&mmat);
644:     if (mmat == pc->pmat) opsset = PETSC_FALSE;
645:   }

647:   if (!opsset) {
648:     PCGetUseAmat(pc,&use_amat);
649:     if (use_amat) {
650:       PetscInfo(pc,"Using outer operators to define finest grid operator \n  because PCMGGetSmoother(pc,nlevels-1,&ksp);KSPSetOperators(ksp,...); was not called.\n");
651:       KSPSetOperators(mglevels[n-1]->smoothd,pc->mat,pc->pmat);
652:     } else {
653:       PetscInfo(pc,"Using matrix (pmat) operators to define finest grid operator \n  because PCMGGetSmoother(pc,nlevels-1,&ksp);KSPSetOperators(ksp,...); was not called.\n");
654:       KSPSetOperators(mglevels[n-1]->smoothd,pc->pmat,pc->pmat);
655:     }
656:   }

658:   for (i=n-1; i>0; i--) {
659:     if (!(mglevels[i]->interpolate || mglevels[i]->restrct)) {
660:       missinginterpolate = PETSC_TRUE;
661:       continue;
662:     }
663:   }

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


672:   /*
673:    Skipping if user has provided all interpolation/restriction needed (since DM might not be able to produce them (when coming from SNES/TS)
674:    Skipping for galerkin==2 (externally managed hierarchy such as ML and GAMG). Cleaner logic here would be great. Wrap ML/GAMG as DMs?
675:   */
676:   if (missinginterpolate && pc->dm && mg->galerkin != PC_MG_GALERKIN_EXTERNAL && !pc->setupcalled) {
677:         /* construct the interpolation from the DMs */
678:     Mat p;
679:     Vec rscale;
680:     PetscMalloc1(n,&dms);
681:     dms[n-1] = pc->dm;
682:     /* Separately create them so we do not get DMKSP interference between levels */
683:     for (i=n-2; i>-1; i--) {DMCoarsen(dms[i+1],MPI_COMM_NULL,&dms[i]);}
684:         /*
685:            Force the mat type of coarse level operator to be AIJ because usually we want to use LU for coarse level.
686:            Notice that it can be overwritten by -mat_type because KSPSetUp() reads command line options.
687:            But it is safe to use -dm_mat_type.

689:            The mat type should not be hardcoded like this, we need to find a better way.
690:     DMSetMatType(dms[0],MATAIJ);
691:     */
692:     for (i=n-2; i>-1; i--) {
693:       DMKSP     kdm;
694:       PetscBool dmhasrestrict, dmhasinject;
695:       KSPSetDM(mglevels[i]->smoothd,dms[i]);
696:       if (!needRestricts) {KSPSetDMActive(mglevels[i]->smoothd,PETSC_FALSE);}
697:       if (mglevels[i]->smoothd != mglevels[i]->smoothu) {
698:         KSPSetDM(mglevels[i]->smoothu,dms[i]);
699:         if (!needRestricts) {KSPSetDMActive(mglevels[i]->smoothu,PETSC_FALSE);}
700:       }
701:       DMGetDMKSPWrite(dms[i],&kdm);
702:       /* Ugly hack so that the next KSPSetUp() will use the RHS that we set. A better fix is to change dmActive to take
703:        * a bitwise OR of computing the matrix, RHS, and initial iterate. */
704:       kdm->ops->computerhs = NULL;
705:       kdm->rhsctx          = NULL;
706:       if (!mglevels[i+1]->interpolate) {
707:         DMCreateInterpolation(dms[i],dms[i+1],&p,&rscale);
708:         PCMGSetInterpolation(pc,i+1,p);
709:         if (rscale) {PCMGSetRScale(pc,i+1,rscale);}
710:         VecDestroy(&rscale);
711:         MatDestroy(&p);
712:       }
713:       DMHasCreateRestriction(dms[i],&dmhasrestrict);
714:       if (dmhasrestrict && !mglevels[i+1]->restrct){
715:         DMCreateRestriction(dms[i],dms[i+1],&p);
716:         PCMGSetRestriction(pc,i+1,p);
717:         MatDestroy(&p);
718:       }
719:       DMHasCreateInjection(dms[i],&dmhasinject);
720:       if (dmhasinject && !mglevels[i+1]->inject){
721:         DMCreateInjection(dms[i],dms[i+1],&p);
722:         PCMGSetInjection(pc,i+1,p);
723:         MatDestroy(&p);
724:       }
725:     }

727:     for (i=n-2; i>-1; i--) {DMDestroy(&dms[i]);}
728:     PetscFree(dms);
729:   }

731:   if (pc->dm && !pc->setupcalled) {
732:     /* finest smoother also gets DM but it is not active, independent of whether galerkin==PC_MG_GALERKIN_EXTERNAL */
733:     KSPSetDM(mglevels[n-1]->smoothd,pc->dm);
734:     KSPSetDMActive(mglevels[n-1]->smoothd,PETSC_FALSE);
735:     if (mglevels[n-1]->smoothd != mglevels[n-1]->smoothu) {
736:       KSPSetDM(mglevels[n-1]->smoothu,pc->dm);
737:       KSPSetDMActive(mglevels[n-1]->smoothu,PETSC_FALSE);
738:     }
739:   }

741:   if (mg->galerkin < PC_MG_GALERKIN_NONE) {
742:     Mat       A,B;
743:     PetscBool doA = PETSC_FALSE,doB = PETSC_FALSE;
744:     MatReuse  reuse = MAT_INITIAL_MATRIX;

746:     if ((mg->galerkin == PC_MG_GALERKIN_PMAT) || (mg->galerkin == PC_MG_GALERKIN_BOTH)) doB = PETSC_TRUE;
747:     if ((mg->galerkin == PC_MG_GALERKIN_MAT) || ((mg->galerkin == PC_MG_GALERKIN_BOTH) && (dA != dB))) doA = PETSC_TRUE;
748:     if (pc->setupcalled) reuse = MAT_REUSE_MATRIX;
749:     for (i=n-2; i>-1; i--) {
750:       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");
751:       if (!mglevels[i+1]->interpolate) {
752:         PCMGSetInterpolation(pc,i+1,mglevels[i+1]->restrct);
753:       }
754:       if (!mglevels[i+1]->restrct) {
755:         PCMGSetRestriction(pc,i+1,mglevels[i+1]->interpolate);
756:       }
757:       if (reuse == MAT_REUSE_MATRIX) {
758:         KSPGetOperators(mglevels[i]->smoothd,&A,&B);
759:       }
760:       if (doA) {
761:         MatGalerkin(mglevels[i+1]->restrct,dA,mglevels[i+1]->interpolate,reuse,1.0,&A);
762:       }
763:       if (doB) {
764:         MatGalerkin(mglevels[i+1]->restrct,dB,mglevels[i+1]->interpolate,reuse,1.0,&B);
765:       }
766:       /* the management of the PetscObjectReference() and PetscObjecDereference() below is rather delicate */
767:       if (!doA && dAeqdB) {
768:         if (reuse == MAT_INITIAL_MATRIX) {PetscObjectReference((PetscObject)B);}
769:         A = B;
770:       } else if (!doA && reuse == MAT_INITIAL_MATRIX) {
771:         KSPGetOperators(mglevels[i]->smoothd,&A,NULL);
772:         PetscObjectReference((PetscObject)A);
773:       }
774:       if (!doB && dAeqdB) {
775:         if (reuse == MAT_INITIAL_MATRIX) {PetscObjectReference((PetscObject)A);}
776:         B = A;
777:       } else if (!doB && reuse == MAT_INITIAL_MATRIX) {
778:         KSPGetOperators(mglevels[i]->smoothd,NULL,&B);
779:         PetscObjectReference((PetscObject)B);
780:       }
781:       if (reuse == MAT_INITIAL_MATRIX) {
782:         KSPSetOperators(mglevels[i]->smoothd,A,B);
783:         PetscObjectDereference((PetscObject)A);
784:         PetscObjectDereference((PetscObject)B);
785:       }
786:       dA = A;
787:       dB = B;
788:     }
789:   }


792:   /* Adapt interpolation matrices */
793:   if (mg->adaptInterpolation) {
794:     mg->Nc = mg->Nc < 0 ? 6 : mg->Nc; /* Default to 6 modes */
795:     for (i = 0; i < n; ++i) {
796:       PCMGComputeCoarseSpace_Internal(pc, i, mg->coarseSpaceType, mg->Nc, !i ? NULL : mglevels[i-1]->coarseSpace, &mglevels[i]->coarseSpace);
797:       if (i) {PCMGAdaptInterpolator_Internal(pc, i, mglevels[i-1]->smoothu, mglevels[i]->smoothu, mg->Nc, mglevels[i-1]->coarseSpace, mglevels[i]->coarseSpace);}
798:     }
799:     for (i = n-2; i > -1; --i) {
800:       PCMGRecomputeLevelOperators_Internal(pc, i);
801:     }
802:   }

804:   if (needRestricts && pc->dm) {
805:     for (i=n-2; i>=0; i--) {
806:       DM  dmfine,dmcoarse;
807:       Mat Restrict,Inject;
808:       Vec rscale;
809:       KSPGetDM(mglevels[i+1]->smoothd,&dmfine);
810:       KSPGetDM(mglevels[i]->smoothd,&dmcoarse);
811:       PCMGGetRestriction(pc,i+1,&Restrict);
812:       PCMGGetRScale(pc,i+1,&rscale);
813:       PCMGGetInjection(pc,i+1,&Inject);
814:       DMRestrict(dmfine,Restrict,rscale,Inject,dmcoarse);
815:     }
816:   }

818:   if (!pc->setupcalled) {
819:     for (i=0; i<n; i++) {
820:       KSPSetFromOptions(mglevels[i]->smoothd);
821:     }
822:     for (i=1; i<n; i++) {
823:       if (mglevels[i]->smoothu && (mglevels[i]->smoothu != mglevels[i]->smoothd)) {
824:         KSPSetFromOptions(mglevels[i]->smoothu);
825:       }
826:     }
827:     /* insure that if either interpolation or restriction is set the other other one is set */
828:     for (i=1; i<n; i++) {
829:       PCMGGetInterpolation(pc,i,NULL);
830:       PCMGGetRestriction(pc,i,NULL);
831:     }
832:     for (i=0; i<n-1; i++) {
833:       if (!mglevels[i]->b) {
834:         Vec *vec;
835:         KSPCreateVecs(mglevels[i]->smoothd,1,&vec,0,NULL);
836:         PCMGSetRhs(pc,i,*vec);
837:         VecDestroy(vec);
838:         PetscFree(vec);
839:       }
840:       if (!mglevels[i]->r && i) {
841:         VecDuplicate(mglevels[i]->b,&tvec);
842:         PCMGSetR(pc,i,tvec);
843:         VecDestroy(&tvec);
844:       }
845:       if (!mglevels[i]->x) {
846:         VecDuplicate(mglevels[i]->b,&tvec);
847:         PCMGSetX(pc,i,tvec);
848:         VecDestroy(&tvec);
849:       }
850:     }
851:     if (n != 1 && !mglevels[n-1]->r) {
852:       /* PCMGSetR() on the finest level if user did not supply it */
853:       Vec *vec;
854:       KSPCreateVecs(mglevels[n-1]->smoothd,1,&vec,0,NULL);
855:       PCMGSetR(pc,n-1,*vec);
856:       VecDestroy(vec);
857:       PetscFree(vec);
858:     }
859:   }

861:   if (pc->dm) {
862:     /* need to tell all the coarser levels to rebuild the matrix using the DM for that level */
863:     for (i=0; i<n-1; i++) {
864:       if (mglevels[i]->smoothd->setupstage != KSP_SETUP_NEW) mglevels[i]->smoothd->setupstage = KSP_SETUP_NEWMATRIX;
865:     }
866:   }

868:   for (i=1; i<n; i++) {
869:     if (mglevels[i]->smoothu == mglevels[i]->smoothd || mg->am == PC_MG_FULL || mg->am == PC_MG_KASKADE || mg->cyclesperpcapply > 1){
870:       /* if doing only down then initial guess is zero */
871:       KSPSetInitialGuessNonzero(mglevels[i]->smoothd,PETSC_TRUE);
872:     }
873:     if (mglevels[i]->eventsmoothsetup) {PetscLogEventBegin(mglevels[i]->eventsmoothsetup,0,0,0,0);}
874:     KSPSetUp(mglevels[i]->smoothd);
875:     if (mglevels[i]->smoothd->reason == KSP_DIVERGED_PC_FAILED) {
876:       pc->failedreason = PC_SUBPC_ERROR;
877:     }
878:     if (mglevels[i]->eventsmoothsetup) {PetscLogEventEnd(mglevels[i]->eventsmoothsetup,0,0,0,0);}
879:     if (!mglevels[i]->residual) {
880:       Mat mat;
881:       KSPGetOperators(mglevels[i]->smoothd,&mat,NULL);
882:       PCMGSetResidual(pc,i,PCMGResidualDefault,mat);
883:     }
884:   }
885:   for (i=1; i<n; i++) {
886:     if (mglevels[i]->smoothu && mglevels[i]->smoothu != mglevels[i]->smoothd) {
887:       Mat downmat,downpmat;

889:       /* check if operators have been set for up, if not use down operators to set them */
890:       KSPGetOperatorsSet(mglevels[i]->smoothu,&opsset,NULL);
891:       if (!opsset) {
892:         KSPGetOperators(mglevels[i]->smoothd,&downmat,&downpmat);
893:         KSPSetOperators(mglevels[i]->smoothu,downmat,downpmat);
894:       }

896:       KSPSetInitialGuessNonzero(mglevels[i]->smoothu,PETSC_TRUE);
897:       if (mglevels[i]->eventsmoothsetup) {PetscLogEventBegin(mglevels[i]->eventsmoothsetup,0,0,0,0);}
898:       KSPSetUp(mglevels[i]->smoothu);
899:       if (mglevels[i]->smoothu->reason == KSP_DIVERGED_PC_FAILED) {
900:         pc->failedreason = PC_SUBPC_ERROR;
901:       }
902:       if (mglevels[i]->eventsmoothsetup) {PetscLogEventEnd(mglevels[i]->eventsmoothsetup,0,0,0,0);}
903:     }
904:   }

906:   if (mglevels[0]->eventsmoothsetup) {PetscLogEventBegin(mglevels[0]->eventsmoothsetup,0,0,0,0);}
907:   KSPSetUp(mglevels[0]->smoothd);
908:   if (mglevels[0]->smoothd->reason == KSP_DIVERGED_PC_FAILED) {
909:     pc->failedreason = PC_SUBPC_ERROR;
910:   }
911:   if (mglevels[0]->eventsmoothsetup) {PetscLogEventEnd(mglevels[0]->eventsmoothsetup,0,0,0,0);}

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

918:    Only support one or the other at the same time.
919:   */
920: #if defined(PETSC_USE_SOCKET_VIEWER)
921:   PetscOptionsGetBool(((PetscObject)pc)->options,((PetscObject)pc)->prefix,"-pc_mg_dump_matlab",&dump,NULL);
922:   if (dump) viewer = PETSC_VIEWER_SOCKET_(PetscObjectComm((PetscObject)pc));
923:   dump = PETSC_FALSE;
924: #endif
925:   PetscOptionsGetBool(((PetscObject)pc)->options,((PetscObject)pc)->prefix,"-pc_mg_dump_binary",&dump,NULL);
926:   if (dump) viewer = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)pc));

928:   if (viewer) {
929:     for (i=1; i<n; i++) {
930:       MatView(mglevels[i]->restrct,viewer);
931:     }
932:     for (i=0; i<n; i++) {
933:       KSPGetPC(mglevels[i]->smoothd,&pc);
934:       MatView(pc->mat,viewer);
935:     }
936:   }
937:   return(0);
938: }

940: /* -------------------------------------------------------------------------------------*/

942: PetscErrorCode PCMGGetLevels_MG(PC pc, PetscInt *levels)
943: {
944:   PC_MG *mg = (PC_MG *) pc->data;

947:   *levels = mg->nlevels;
948:   return(0);
949: }

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

954:    Not Collective

956:    Input Parameter:
957: .  pc - the preconditioner context

959:    Output parameter:
960: .  levels - the number of levels

962:    Level: advanced

964: .seealso: PCMGSetLevels()
965: @*/
966: PetscErrorCode PCMGGetLevels(PC pc,PetscInt *levels)
967: {

973:   *levels = 0;
974:   PetscTryMethod(pc,"PCMGGetLevels_C",(PC,PetscInt*),(pc,levels));
975:   return(0);
976: }

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

982:    Logically Collective on PC

984:    Input Parameters:
985: +  pc - the preconditioner context
986: -  form - multigrid form, one of PC_MG_MULTIPLICATIVE, PC_MG_ADDITIVE,
987:    PC_MG_FULL, PC_MG_KASKADE

989:    Options Database Key:
990: .  -pc_mg_type <form> - Sets <form>, one of multiplicative,
991:    additive, full, kaskade

993:    Level: advanced

995: .seealso: PCMGSetLevels()
996: @*/
997: PetscErrorCode  PCMGSetType(PC pc,PCMGType form)
998: {
999:   PC_MG *mg = (PC_MG*)pc->data;

1004:   mg->am = form;
1005:   if (form == PC_MG_MULTIPLICATIVE) pc->ops->applyrichardson = PCApplyRichardson_MG;
1006:   else pc->ops->applyrichardson = NULL;
1007:   return(0);
1008: }

1010: /*@
1011:    PCMGGetType - Determines the form of multigrid to use:
1012:    multiplicative, additive, full, or the Kaskade algorithm.

1014:    Logically Collective on PC

1016:    Input Parameter:
1017: .  pc - the preconditioner context

1019:    Output Parameter:
1020: .  type - one of PC_MG_MULTIPLICATIVE, PC_MG_ADDITIVE,PC_MG_FULL, PC_MG_KASKADE


1023:    Level: advanced

1025: .seealso: PCMGSetLevels()
1026: @*/
1027: PetscErrorCode  PCMGGetType(PC pc,PCMGType *type)
1028: {
1029:   PC_MG *mg = (PC_MG*)pc->data;

1033:   *type = mg->am;
1034:   return(0);
1035: }

1037: /*@
1038:    PCMGSetCycleType - Sets the type cycles to use.  Use PCMGSetCycleTypeOnLevel() for more
1039:    complicated cycling.

1041:    Logically Collective on PC

1043:    Input Parameters:
1044: +  pc - the multigrid context
1045: -  n - either PC_MG_CYCLE_V or PC_MG_CYCLE_W

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

1050:    Level: advanced

1052: .seealso: PCMGSetCycleTypeOnLevel()
1053: @*/
1054: PetscErrorCode  PCMGSetCycleType(PC pc,PCMGCycleType n)
1055: {
1056:   PC_MG        *mg        = (PC_MG*)pc->data;
1057:   PC_MG_Levels **mglevels = mg->levels;
1058:   PetscInt     i,levels;

1063:   if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ORDER,"Must set MG levels with PCMGSetLevels() before calling");
1064:   levels = mglevels[0]->levels;
1065:   for (i=0; i<levels; i++) mglevels[i]->cycles = n;
1066:   return(0);
1067: }

1069: /*@
1070:    PCMGMultiplicativeSetCycles - Sets the number of cycles to use for each preconditioner step
1071:          of multigrid when PCMGType of PC_MG_MULTIPLICATIVE is used

1073:    Logically Collective on PC

1075:    Input Parameters:
1076: +  pc - the multigrid context
1077: -  n - number of cycles (default is 1)

1079:    Options Database Key:
1080: .  -pc_mg_multiplicative_cycles n

1082:    Level: advanced

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

1087: .seealso: PCMGSetCycleTypeOnLevel(), PCMGSetCycleType()
1088: @*/
1089: PetscErrorCode  PCMGMultiplicativeSetCycles(PC pc,PetscInt n)
1090: {
1091:   PC_MG        *mg        = (PC_MG*)pc->data;

1096:   mg->cyclesperpcapply = n;
1097:   return(0);
1098: }

1100: PetscErrorCode PCMGSetGalerkin_MG(PC pc,PCMGGalerkinType use)
1101: {
1102:   PC_MG *mg = (PC_MG*)pc->data;

1105:   mg->galerkin = use;
1106:   return(0);
1107: }

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

1113:    Logically Collective on PC

1115:    Input Parameters:
1116: +  pc - the multigrid context
1117: -  use - one of PC_MG_GALERKIN_BOTH,PC_MG_GALERKIN_PMAT,PC_MG_GALERKIN_MAT, or PC_MG_GALERKIN_NONE

1119:    Options Database Key:
1120: .  -pc_mg_galerkin <both,pmat,mat,none>

1122:    Level: intermediate

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

1128: .seealso: PCMGGetGalerkin(), PCMGGalerkinType

1130: @*/
1131: PetscErrorCode PCMGSetGalerkin(PC pc,PCMGGalerkinType use)
1132: {

1137:   PetscTryMethod(pc,"PCMGSetGalerkin_C",(PC,PCMGGalerkinType),(pc,use));
1138:   return(0);
1139: }

1141: /*@
1142:    PCMGGetGalerkin - Checks if Galerkin multigrid is being used, i.e.
1143:       A_i-1 = r_i * A_i * p_i

1145:    Not Collective

1147:    Input Parameter:
1148: .  pc - the multigrid context

1150:    Output Parameter:
1151: .  galerkin - one of PC_MG_GALERKIN_BOTH,PC_MG_GALERKIN_PMAT,PC_MG_GALERKIN_MAT, PC_MG_GALERKIN_NONE, or PC_MG_GALERKIN_EXTERNAL

1153:    Level: intermediate

1155: .seealso: PCMGSetGalerkin(), PCMGGalerkinType

1157: @*/
1158: PetscErrorCode  PCMGGetGalerkin(PC pc,PCMGGalerkinType  *galerkin)
1159: {
1160:   PC_MG *mg = (PC_MG*)pc->data;

1164:   *galerkin = mg->galerkin;
1165:   return(0);
1166: }

1168: PetscErrorCode PCMGSetAdaptInterpolation_MG(PC pc, PetscBool adapt)
1169: {
1170:   PC_MG *mg = (PC_MG *) pc->data;

1173:   mg->adaptInterpolation = adapt;
1174:   return(0);
1175: }

1177: PetscErrorCode PCMGGetAdaptInterpolation_MG(PC pc, PetscBool *adapt)
1178: {
1179:   PC_MG *mg = (PC_MG *) pc->data;

1182:   *adapt = mg->adaptInterpolation;
1183:   return(0);
1184: }

1186: /*@
1187:   PCMGSetAdaptInterpolation - Adapt the interpolator based upon a vector space which should be accurately captured by the next coarser mesh, and thus accurately interpolated.

1189:   Logically Collective on PC

1191:   Input Parameters:
1192: + pc    - the multigrid context
1193: - adapt - flag for adaptation of the interpolator

1195:   Options Database Keys:
1196: + -pc_mg_adapt_interp                     - Turn on adaptation
1197: . -pc_mg_adapt_interp_n <int>             - The number of modes to use, should be divisible by dimension
1198: - -pc_mg_adapt_interp_coarse_space <type> - The type of coarse space: polynomial, harmonic, eigenvector, generalized_eigenvector

1200:   Level: intermediate

1202: .keywords: MG, set, Galerkin
1203: .seealso: PCMGGetAdaptInterpolation(), PCMGSetGalerkin()
1204: @*/
1205: PetscErrorCode PCMGSetAdaptInterpolation(PC pc, PetscBool adapt)
1206: {

1211:   PetscTryMethod(pc,"PCMGSetAdaptInterpolation_C",(PC,PetscBool),(pc,adapt));
1212:   return(0);
1213: }

1215: /*@
1216:   PCMGGetAdaptInterpolation - Get the flag to adapt the interpolator based upon a vector space which should be accurately captured by the next coarser mesh, and thus accurately interpolated.

1218:   Not collective

1220:   Input Parameter:
1221: . pc    - the multigrid context

1223:   Output Parameter:
1224: . adapt - flag for adaptation of the interpolator

1226:   Level: intermediate

1228: .keywords: MG, set, Galerkin
1229: .seealso: PCMGSetAdaptInterpolation(), PCMGSetGalerkin()
1230: @*/
1231: PetscErrorCode PCMGGetAdaptInterpolation(PC pc, PetscBool *adapt)
1232: {

1238:   PetscUseMethod(pc,"PCMGGetAdaptInterpolation_C",(PC,PetscBool*),(pc,adapt));
1239:   return(0);
1240: }

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

1247:    Logically Collective on PC

1249:    Input Parameters:
1250: +  mg - the multigrid context
1251: -  n - the number of smoothing steps

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

1256:    Level: advanced

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

1262: .seealso: PCMGSetDistinctSmoothUp()
1263: @*/
1264: PetscErrorCode  PCMGSetNumberSmooth(PC pc,PetscInt n)
1265: {
1266:   PC_MG          *mg        = (PC_MG*)pc->data;
1267:   PC_MG_Levels   **mglevels = mg->levels;
1269:   PetscInt       i,levels;

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

1277:   for (i=1; i<levels; i++) {
1278:     KSPSetTolerances(mglevels[i]->smoothu,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,n);
1279:     KSPSetTolerances(mglevels[i]->smoothd,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,n);
1280:     mg->default_smoothu = n;
1281:     mg->default_smoothd = n;
1282:   }
1283:   return(0);
1284: }

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

1290:    Logically Collective on PC

1292:    Input Parameters:
1293: .  pc - the preconditioner context

1295:    Options Database Key:
1296: .  -pc_mg_distinct_smoothup

1298:    Level: advanced

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

1304: .seealso: PCMGSetNumberSmooth()
1305: @*/
1306: PetscErrorCode  PCMGSetDistinctSmoothUp(PC pc)
1307: {
1308:   PC_MG          *mg        = (PC_MG*)pc->data;
1309:   PC_MG_Levels   **mglevels = mg->levels;
1311:   PetscInt       i,levels;
1312:   KSP            subksp;

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

1319:   for (i=1; i<levels; i++) {
1320:     const char *prefix = NULL;
1321:     /* make sure smoother up and down are different */
1322:     PCMGGetSmootherUp(pc,i,&subksp);
1323:     KSPGetOptionsPrefix(mglevels[i]->smoothd,&prefix);
1324:     KSPSetOptionsPrefix(subksp,prefix);
1325:     KSPAppendOptionsPrefix(subksp,"up_");
1326:   }
1327:   return(0);
1328: }

1330: /* No new matrices are created, and the coarse operator matrices are the references to the original ones */
1331: PetscErrorCode  PCGetInterpolations_MG(PC pc,PetscInt *num_levels,Mat *interpolations[])
1332: {
1333:   PC_MG          *mg        = (PC_MG*)pc->data;
1334:   PC_MG_Levels   **mglevels = mg->levels;
1335:   Mat            *mat;
1336:   PetscInt       l;

1340:   if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
1341:   PetscMalloc1(mg->nlevels,&mat);
1342:   for (l=1; l< mg->nlevels; l++) {
1343:     mat[l-1] = mglevels[l]->interpolate;
1344:     PetscObjectReference((PetscObject)mat[l-1]);
1345:   }
1346:   *num_levels = mg->nlevels;
1347:   *interpolations = mat;
1348:   return(0);
1349: }

1351: /* No new matrices are created, and the coarse operator matrices are the references to the original ones */
1352: PetscErrorCode  PCGetCoarseOperators_MG(PC pc,PetscInt *num_levels,Mat *coarseOperators[])
1353: {
1354:   PC_MG          *mg        = (PC_MG*)pc->data;
1355:   PC_MG_Levels   **mglevels = mg->levels;
1356:   PetscInt       l;
1357:   Mat            *mat;

1361:   if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
1362:   PetscMalloc1(mg->nlevels,&mat);
1363:   for (l=0; l<mg->nlevels-1; l++) {
1364:     KSPGetOperators(mglevels[l]->smoothd,NULL,&(mat[l]));
1365:     PetscObjectReference((PetscObject)mat[l]);
1366:   }
1367:   *num_levels = mg->nlevels;
1368:   *coarseOperators = mat;
1369:   return(0);
1370: }

1372: /*@C
1373:   PCMGRegisterCoarseSpaceConstructor -  Adds a method to the PCMG package for coarse space construction.

1375:   Not collective

1377:   Input Parameters:
1378: + name     - name of the constructor
1379: - function - constructor routine

1381:   Notes:
1382:   Calling sequence for the routine:
1383: $ my_csp(PC pc, PetscInt l, DM dm, KSP smooth, PetscInt Nc, const Vec initGuess[], Vec **coarseSp)
1384: $   pc        - The PC object
1385: $   l         - The multigrid level, 0 is the coarse level
1386: $   dm        - The DM for this level
1387: $   smooth    - The level smoother
1388: $   Nc        - The size of the coarse space
1389: $   initGuess - Basis for an initial guess for the space
1390: $   coarseSp  - A basis for the computed coarse space

1392:   Level: advanced

1394: .seealso: PCMGGetCoarseSpaceConstructor(), PCRegister()
1395: @*/
1396: PetscErrorCode PCMGRegisterCoarseSpaceConstructor(const char name[], PetscErrorCode (*function)(PC, PetscInt, DM, KSP, PetscInt, const Vec[], Vec **))
1397: {

1401:   PCInitializePackage();
1402:   PetscFunctionListAdd(&PCMGCoarseList,name,function);
1403:   return(0);
1404: }

1406: /*@C
1407:   PCMGGetCoarseSpaceConstructor -  Returns the given coarse space construction method.

1409:   Not collective

1411:   Input Parameter:
1412: . name     - name of the constructor

1414:   Output Parameter:
1415: . function - constructor routine

1417:   Notes:
1418:   Calling sequence for the routine:
1419: $ my_csp(PC pc, PetscInt l, DM dm, KSP smooth, PetscInt Nc, const Vec initGuess[], Vec **coarseSp)
1420: $   pc        - The PC object
1421: $   l         - The multigrid level, 0 is the coarse level
1422: $   dm        - The DM for this level
1423: $   smooth    - The level smoother
1424: $   Nc        - The size of the coarse space
1425: $   initGuess - Basis for an initial guess for the space
1426: $   coarseSp  - A basis for the computed coarse space

1428:   Level: advanced

1430: .seealso: PCMGRegisterCoarseSpaceConstructor(), PCRegister()
1431: @*/
1432: PetscErrorCode PCMGGetCoarseSpaceConstructor(const char name[], PetscErrorCode (**function)(PC, PetscInt, DM, KSP, PetscInt, const Vec[], Vec **))
1433: {

1437:   PetscFunctionListFind(PCMGCoarseList,name,function);
1438:   return(0);
1439: }

1441: /* ----------------------------------------------------------------------------------------*/

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

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

1460:    Notes:
1461:     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

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

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

1470:    Level: intermediate

1472: .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC, PCMGType, PCEXOTIC, PCGAMG, PCML, PCHYPRE
1473:            PCMGSetLevels(), PCMGGetLevels(), PCMGSetType(), PCMGSetCycleType(),
1474:            PCMGSetDistinctSmoothUp(), PCMGGetCoarseSolve(), PCMGSetResidual(), PCMGSetInterpolation(),
1475:            PCMGSetRestriction(), PCMGGetSmoother(), PCMGGetSmootherUp(), PCMGGetSmootherDown(),
1476:            PCMGSetCycleTypeOnLevel(), PCMGSetRhs(), PCMGSetX(), PCMGSetR()
1477: M*/

1479: PETSC_EXTERN PetscErrorCode PCCreate_MG(PC pc)
1480: {
1481:   PC_MG          *mg;

1485:   PetscNewLog(pc,&mg);
1486:   pc->data     = (void*)mg;
1487:   mg->nlevels  = -1;
1488:   mg->am       = PC_MG_MULTIPLICATIVE;
1489:   mg->galerkin = PC_MG_GALERKIN_NONE;
1490:   mg->adaptInterpolation = PETSC_FALSE;
1491:   mg->Nc                 = -1;
1492:   mg->eigenvalue         = -1;

1494:   pc->useAmat = PETSC_TRUE;

1496:   pc->ops->apply          = PCApply_MG;
1497:   pc->ops->setup          = PCSetUp_MG;
1498:   pc->ops->reset          = PCReset_MG;
1499:   pc->ops->destroy        = PCDestroy_MG;
1500:   pc->ops->setfromoptions = PCSetFromOptions_MG;
1501:   pc->ops->view           = PCView_MG;

1503:   PetscObjectComposedDataRegister(&mg->eigenvalue);
1504:   PetscObjectComposeFunction((PetscObject)pc,"PCMGSetGalerkin_C",PCMGSetGalerkin_MG);
1505:   PetscObjectComposeFunction((PetscObject)pc,"PCMGGetLevels_C",PCMGGetLevels_MG);
1506:   PetscObjectComposeFunction((PetscObject)pc,"PCMGSetLevels_C",PCMGSetLevels_MG);
1507:   PetscObjectComposeFunction((PetscObject)pc,"PCGetInterpolations_C",PCGetInterpolations_MG);
1508:   PetscObjectComposeFunction((PetscObject)pc,"PCGetCoarseOperators_C",PCGetCoarseOperators_MG);
1509:   PetscObjectComposeFunction((PetscObject)pc,"PCMGSetAdaptInterpolation_C",PCMGSetAdaptInterpolation_MG);
1510:   PetscObjectComposeFunction((PetscObject)pc,"PCMGGetAdaptInterpolation_C",PCMGGetAdaptInterpolation_MG);
1511:   return(0);
1512: }