xref: /petsc/src/ksp/pc/impls/mg/mg.c (revision 3d3eaba7093fd3d75f596ec754341dee3aba7588)
1dba47a55SKris Buschelman 
24b9ad928SBarry Smith /*
34b9ad928SBarry Smith     Defines the multigrid preconditioner interface.
44b9ad928SBarry Smith */
5af0996ceSBarry Smith #include <petsc/private/pcmgimpl.h>                    /*I "petscksp.h" I*/
61e25c274SJed Brown #include <petscdm.h>
74b9ad928SBarry Smith 
84b9ad928SBarry Smith #undef __FUNCT__
99dcbbd2bSBarry Smith #define __FUNCT__ "PCMGMCycle_Private"
1031567311SBarry Smith PetscErrorCode PCMGMCycle_Private(PC pc,PC_MG_Levels **mglevelsin,PCRichardsonConvergedReason *reason)
114b9ad928SBarry Smith {
1231567311SBarry Smith   PC_MG          *mg = (PC_MG*)pc->data;
1331567311SBarry Smith   PC_MG_Levels   *mgc,*mglevels = *mglevelsin;
146849ba73SBarry Smith   PetscErrorCode ierr;
1531567311SBarry Smith   PetscInt       cycles = (mglevels->level == 1) ? 1 : (PetscInt) mglevels->cycles;
16a0808db4SHong Zhang   PC             subpc;
17a0808db4SHong Zhang   PCFailedReason pcreason;
184b9ad928SBarry Smith 
194b9ad928SBarry Smith   PetscFunctionBegin;
2063e6d426SJed Brown   if (mglevels->eventsmoothsolve) {ierr = PetscLogEventBegin(mglevels->eventsmoothsolve,0,0,0,0);CHKERRQ(ierr);}
2131567311SBarry Smith   ierr = KSPSolve(mglevels->smoothd,mglevels->b,mglevels->x);CHKERRQ(ierr);  /* pre-smooth */
22a0808db4SHong Zhang   ierr = KSPGetPC(mglevels->smoothd,&subpc);CHKERRQ(ierr);
23a0808db4SHong Zhang   ierr = PCGetSetUpFailedReason(subpc,&pcreason);CHKERRQ(ierr);
24a0808db4SHong Zhang   if (pcreason) {
25a0808db4SHong Zhang     pc->failedreason = PC_SUBPC_ERROR;
26a0808db4SHong Zhang   }
2763e6d426SJed Brown   if (mglevels->eventsmoothsolve) {ierr = PetscLogEventEnd(mglevels->eventsmoothsolve,0,0,0,0);CHKERRQ(ierr);}
2831567311SBarry Smith   if (mglevels->level) {  /* not the coarsest grid */
2963e6d426SJed Brown     if (mglevels->eventresidual) {ierr = PetscLogEventBegin(mglevels->eventresidual,0,0,0,0);CHKERRQ(ierr);}
3031567311SBarry Smith     ierr = (*mglevels->residual)(mglevels->A,mglevels->b,mglevels->x,mglevels->r);CHKERRQ(ierr);
3163e6d426SJed Brown     if (mglevels->eventresidual) {ierr = PetscLogEventEnd(mglevels->eventresidual,0,0,0,0);CHKERRQ(ierr);}
324b9ad928SBarry Smith 
334b9ad928SBarry Smith     /* if on finest level and have convergence criteria set */
3431567311SBarry Smith     if (mglevels->level == mglevels->levels-1 && mg->ttol && reason) {
354b9ad928SBarry Smith       PetscReal rnorm;
3631567311SBarry Smith       ierr = VecNorm(mglevels->r,NORM_2,&rnorm);CHKERRQ(ierr);
374b9ad928SBarry Smith       if (rnorm <= mg->ttol) {
3870441072SBarry Smith         if (rnorm < mg->abstol) {
394d0a8057SBarry Smith           *reason = PCRICHARDSON_CONVERGED_ATOL;
4057622a8eSBarry Smith           ierr    = PetscInfo2(pc,"Linear solver has converged. Residual norm %g is less than absolute tolerance %g\n",(double)rnorm,(double)mg->abstol);CHKERRQ(ierr);
414b9ad928SBarry Smith         } else {
424d0a8057SBarry Smith           *reason = PCRICHARDSON_CONVERGED_RTOL;
4357622a8eSBarry Smith           ierr    = 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);CHKERRQ(ierr);
444b9ad928SBarry Smith         }
454b9ad928SBarry Smith         PetscFunctionReturn(0);
464b9ad928SBarry Smith       }
474b9ad928SBarry Smith     }
484b9ad928SBarry Smith 
4931567311SBarry Smith     mgc = *(mglevelsin - 1);
5063e6d426SJed Brown     if (mglevels->eventinterprestrict) {ierr = PetscLogEventBegin(mglevels->eventinterprestrict,0,0,0,0);CHKERRQ(ierr);}
5131567311SBarry Smith     ierr = MatRestrict(mglevels->restrct,mglevels->r,mgc->b);CHKERRQ(ierr);
5263e6d426SJed Brown     if (mglevels->eventinterprestrict) {ierr = PetscLogEventEnd(mglevels->eventinterprestrict,0,0,0,0);CHKERRQ(ierr);}
53efb30889SBarry Smith     ierr = VecSet(mgc->x,0.0);CHKERRQ(ierr);
544b9ad928SBarry Smith     while (cycles--) {
5531567311SBarry Smith       ierr = PCMGMCycle_Private(pc,mglevelsin-1,reason);CHKERRQ(ierr);
564b9ad928SBarry Smith     }
5763e6d426SJed Brown     if (mglevels->eventinterprestrict) {ierr = PetscLogEventBegin(mglevels->eventinterprestrict,0,0,0,0);CHKERRQ(ierr);}
5831567311SBarry Smith     ierr = MatInterpolateAdd(mglevels->interpolate,mgc->x,mglevels->x,mglevels->x);CHKERRQ(ierr);
5963e6d426SJed Brown     if (mglevels->eventinterprestrict) {ierr = PetscLogEventEnd(mglevels->eventinterprestrict,0,0,0,0);CHKERRQ(ierr);}
6063e6d426SJed Brown     if (mglevels->eventsmoothsolve) {ierr = PetscLogEventBegin(mglevels->eventsmoothsolve,0,0,0,0);CHKERRQ(ierr);}
6131567311SBarry Smith     ierr = KSPSolve(mglevels->smoothu,mglevels->b,mglevels->x);CHKERRQ(ierr);    /* post smooth */
6263e6d426SJed Brown     if (mglevels->eventsmoothsolve) {ierr = PetscLogEventEnd(mglevels->eventsmoothsolve,0,0,0,0);CHKERRQ(ierr);}
634b9ad928SBarry Smith   }
644b9ad928SBarry Smith   PetscFunctionReturn(0);
654b9ad928SBarry Smith }
664b9ad928SBarry Smith 
674b9ad928SBarry Smith #undef __FUNCT__
684b9ad928SBarry Smith #define __FUNCT__ "PCApplyRichardson_MG"
69ace3abfcSBarry Smith 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)
704b9ad928SBarry Smith {
71f3fbd535SBarry Smith   PC_MG          *mg        = (PC_MG*)pc->data;
72f3fbd535SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
73dfbe8321SBarry Smith   PetscErrorCode ierr;
74f3fbd535SBarry Smith   PetscInt       levels = mglevels[0]->levels,i;
754b9ad928SBarry Smith 
764b9ad928SBarry Smith   PetscFunctionBegin;
77a762d673SBarry Smith   /* When the DM is supplying the matrix then it will not exist until here */
78a762d673SBarry Smith   for (i=0; i<levels; i++) {
79a762d673SBarry Smith     if (!mglevels[i]->A) {
80a762d673SBarry Smith       ierr = KSPGetOperators(mglevels[i]->smoothu,&mglevels[i]->A,NULL);CHKERRQ(ierr);
81a762d673SBarry Smith       ierr = PetscObjectReference((PetscObject)mglevels[i]->A);CHKERRQ(ierr);
82a762d673SBarry Smith     }
83a762d673SBarry Smith   }
84f3fbd535SBarry Smith   mglevels[levels-1]->b = b;
85f3fbd535SBarry Smith   mglevels[levels-1]->x = x;
864b9ad928SBarry Smith 
8731567311SBarry Smith   mg->rtol   = rtol;
8831567311SBarry Smith   mg->abstol = abstol;
8931567311SBarry Smith   mg->dtol   = dtol;
904b9ad928SBarry Smith   if (rtol) {
914b9ad928SBarry Smith     /* compute initial residual norm for relative convergence test */
924b9ad928SBarry Smith     PetscReal rnorm;
937319c654SBarry Smith     if (zeroguess) {
947319c654SBarry Smith       ierr = VecNorm(b,NORM_2,&rnorm);CHKERRQ(ierr);
957319c654SBarry Smith     } else {
96f3fbd535SBarry Smith       ierr = (*mglevels[levels-1]->residual)(mglevels[levels-1]->A,b,x,w);CHKERRQ(ierr);
974b9ad928SBarry Smith       ierr = VecNorm(w,NORM_2,&rnorm);CHKERRQ(ierr);
987319c654SBarry Smith     }
9931567311SBarry Smith     mg->ttol = PetscMax(rtol*rnorm,abstol);
1002fa5cd67SKarl Rupp   } else if (abstol) mg->ttol = abstol;
1012fa5cd67SKarl Rupp   else mg->ttol = 0.0;
1024b9ad928SBarry Smith 
1034d0a8057SBarry Smith   /* since smoother is applied to full system, not just residual we need to make sure that smoothers don't
104336babb1SJed Brown      stop prematurely due to small residual */
1054d0a8057SBarry Smith   for (i=1; i<levels; i++) {
106f3fbd535SBarry Smith     ierr = KSPSetTolerances(mglevels[i]->smoothu,0,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
107f3fbd535SBarry Smith     if (mglevels[i]->smoothu != mglevels[i]->smoothd) {
108f3fbd535SBarry Smith       ierr = KSPSetTolerances(mglevels[i]->smoothd,0,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
1094b9ad928SBarry Smith     }
1104d0a8057SBarry Smith   }
1114d0a8057SBarry Smith 
1124d0a8057SBarry Smith   *reason = (PCRichardsonConvergedReason)0;
1134d0a8057SBarry Smith   for (i=0; i<its; i++) {
114f3fbd535SBarry Smith     ierr = PCMGMCycle_Private(pc,mglevels+levels-1,reason);CHKERRQ(ierr);
1154d0a8057SBarry Smith     if (*reason) break;
1164d0a8057SBarry Smith   }
1174d0a8057SBarry Smith   if (!*reason) *reason = PCRICHARDSON_CONVERGED_ITS;
1184d0a8057SBarry Smith   *outits = i;
1194b9ad928SBarry Smith   PetscFunctionReturn(0);
1204b9ad928SBarry Smith }
1214b9ad928SBarry Smith 
1224b9ad928SBarry Smith #undef __FUNCT__
1233aeaf226SBarry Smith #define __FUNCT__ "PCReset_MG"
1243aeaf226SBarry Smith PetscErrorCode PCReset_MG(PC pc)
1253aeaf226SBarry Smith {
1263aeaf226SBarry Smith   PC_MG          *mg        = (PC_MG*)pc->data;
1273aeaf226SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
1283aeaf226SBarry Smith   PetscErrorCode ierr;
1293aeaf226SBarry Smith   PetscInt       i,n;
1303aeaf226SBarry Smith 
1313aeaf226SBarry Smith   PetscFunctionBegin;
1323aeaf226SBarry Smith   if (mglevels) {
1333aeaf226SBarry Smith     n = mglevels[0]->levels;
1343aeaf226SBarry Smith     for (i=0; i<n-1; i++) {
1353aeaf226SBarry Smith       ierr = VecDestroy(&mglevels[i+1]->r);CHKERRQ(ierr);
1363aeaf226SBarry Smith       ierr = VecDestroy(&mglevels[i]->b);CHKERRQ(ierr);
1373aeaf226SBarry Smith       ierr = VecDestroy(&mglevels[i]->x);CHKERRQ(ierr);
1383aeaf226SBarry Smith       ierr = MatDestroy(&mglevels[i+1]->restrct);CHKERRQ(ierr);
1393aeaf226SBarry Smith       ierr = MatDestroy(&mglevels[i+1]->interpolate);CHKERRQ(ierr);
14073250ac0SBarry Smith       ierr = VecDestroy(&mglevels[i+1]->rscale);CHKERRQ(ierr);
1413aeaf226SBarry Smith     }
1423aeaf226SBarry Smith 
1433aeaf226SBarry Smith     for (i=0; i<n; i++) {
1443aeaf226SBarry Smith       ierr = MatDestroy(&mglevels[i]->A);CHKERRQ(ierr);
1453aeaf226SBarry Smith       if (mglevels[i]->smoothd != mglevels[i]->smoothu) {
1463aeaf226SBarry Smith         ierr = KSPReset(mglevels[i]->smoothd);CHKERRQ(ierr);
1473aeaf226SBarry Smith       }
1483aeaf226SBarry Smith       ierr = KSPReset(mglevels[i]->smoothu);CHKERRQ(ierr);
1493aeaf226SBarry Smith     }
1503aeaf226SBarry Smith   }
1513aeaf226SBarry Smith   PetscFunctionReturn(0);
1523aeaf226SBarry Smith }
1533aeaf226SBarry Smith 
1543aeaf226SBarry Smith #undef __FUNCT__
1559dcbbd2bSBarry Smith #define __FUNCT__ "PCMGSetLevels"
1564b9ad928SBarry Smith /*@C
15797177400SBarry Smith    PCMGSetLevels - Sets the number of levels to use with MG.
1584b9ad928SBarry Smith    Must be called before any other MG routine.
1594b9ad928SBarry Smith 
160ad4df100SBarry Smith    Logically Collective on PC
1614b9ad928SBarry Smith 
1624b9ad928SBarry Smith    Input Parameters:
1634b9ad928SBarry Smith +  pc - the preconditioner context
1644b9ad928SBarry Smith .  levels - the number of levels
1654b9ad928SBarry Smith -  comms - optional communicators for each level; this is to allow solving the coarser problems
1660298fd71SBarry Smith            on smaller sets of processors. Use NULL_OBJECT for default in Fortran
1674b9ad928SBarry Smith 
1684b9ad928SBarry Smith    Level: intermediate
1694b9ad928SBarry Smith 
1704b9ad928SBarry Smith    Notes:
1714b9ad928SBarry Smith      If the number of levels is one then the multigrid uses the -mg_levels prefix
1724b9ad928SBarry Smith   for setting the level options rather than the -mg_coarse prefix.
1734b9ad928SBarry Smith 
1744b9ad928SBarry Smith .keywords: MG, set, levels, multigrid
1754b9ad928SBarry Smith 
17697177400SBarry Smith .seealso: PCMGSetType(), PCMGGetLevels()
1774b9ad928SBarry Smith @*/
1787087cfbeSBarry Smith PetscErrorCode  PCMGSetLevels(PC pc,PetscInt levels,MPI_Comm *comms)
1794b9ad928SBarry Smith {
180dfbe8321SBarry Smith   PetscErrorCode ierr;
181f3fbd535SBarry Smith   PC_MG          *mg        = (PC_MG*)pc->data;
182ce94432eSBarry Smith   MPI_Comm       comm;
1833aeaf226SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
18410eca3edSLisandro Dalcin   PCMGType       mgtype     = mg->am;
18510167fecSBarry Smith   PetscInt       mgctype    = (PetscInt) PC_MG_CYCLE_V;
186f3fbd535SBarry Smith   PetscInt       i;
187f3fbd535SBarry Smith   PetscMPIInt    size;
188f3fbd535SBarry Smith   const char     *prefix;
189f3fbd535SBarry Smith   PC             ipc;
1903aeaf226SBarry Smith   PetscInt       n;
1914b9ad928SBarry Smith 
1924b9ad928SBarry Smith   PetscFunctionBegin;
1930700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
194548767e0SJed Brown   PetscValidLogicalCollectiveInt(pc,levels,2);
195ce94432eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)pc,&comm);CHKERRQ(ierr);
196548767e0SJed Brown   if (mg->nlevels == levels) PetscFunctionReturn(0);
1973aeaf226SBarry Smith   if (mglevels) {
19810eca3edSLisandro Dalcin     mgctype = mglevels[0]->cycles;
1993aeaf226SBarry Smith     /* changing the number of levels so free up the previous stuff */
2003aeaf226SBarry Smith     ierr = PCReset_MG(pc);CHKERRQ(ierr);
2013aeaf226SBarry Smith     n    = mglevels[0]->levels;
2023aeaf226SBarry Smith     for (i=0; i<n; i++) {
2033aeaf226SBarry Smith       if (mglevels[i]->smoothd != mglevels[i]->smoothu) {
2043aeaf226SBarry Smith         ierr = KSPDestroy(&mglevels[i]->smoothd);CHKERRQ(ierr);
2053aeaf226SBarry Smith       }
2063aeaf226SBarry Smith       ierr = KSPDestroy(&mglevels[i]->smoothu);CHKERRQ(ierr);
2073aeaf226SBarry Smith       ierr = PetscFree(mglevels[i]);CHKERRQ(ierr);
2083aeaf226SBarry Smith     }
2093aeaf226SBarry Smith     ierr = PetscFree(mg->levels);CHKERRQ(ierr);
2103aeaf226SBarry Smith   }
211f3fbd535SBarry Smith 
212f3fbd535SBarry Smith   mg->nlevels = levels;
213f3fbd535SBarry Smith 
214785e854fSJed Brown   ierr = PetscMalloc1(levels,&mglevels);CHKERRQ(ierr);
2153bb1ff40SBarry Smith   ierr = PetscLogObjectMemory((PetscObject)pc,levels*(sizeof(PC_MG*)));CHKERRQ(ierr);
216f3fbd535SBarry Smith 
217f3fbd535SBarry Smith   ierr = PCGetOptionsPrefix(pc,&prefix);CHKERRQ(ierr);
218f3fbd535SBarry Smith 
219a9db87a2SMatthew G Knepley   mg->stageApply = 0;
220f3fbd535SBarry Smith   for (i=0; i<levels; i++) {
221b00a9115SJed Brown     ierr = PetscNewLog(pc,&mglevels[i]);CHKERRQ(ierr);
2222fa5cd67SKarl Rupp 
223f3fbd535SBarry Smith     mglevels[i]->level               = i;
224f3fbd535SBarry Smith     mglevels[i]->levels              = levels;
22510eca3edSLisandro Dalcin     mglevels[i]->cycles              = mgctype;
226336babb1SJed Brown     mg->default_smoothu              = 2;
227336babb1SJed Brown     mg->default_smoothd              = 2;
22863e6d426SJed Brown     mglevels[i]->eventsmoothsetup    = 0;
22963e6d426SJed Brown     mglevels[i]->eventsmoothsolve    = 0;
23063e6d426SJed Brown     mglevels[i]->eventresidual       = 0;
23163e6d426SJed Brown     mglevels[i]->eventinterprestrict = 0;
232f3fbd535SBarry Smith 
233f3fbd535SBarry Smith     if (comms) comm = comms[i];
234f3fbd535SBarry Smith     ierr = KSPCreate(comm,&mglevels[i]->smoothd);CHKERRQ(ierr);
235422a814eSBarry Smith     ierr = KSPSetErrorIfNotConverged(mglevels[i]->smoothd,pc->erroriffailure);CHKERRQ(ierr);
236336babb1SJed Brown     ierr = KSPSetType(mglevels[i]->smoothd,KSPCHEBYSHEV);CHKERRQ(ierr);
2370059c7bdSJed Brown     ierr = KSPSetConvergenceTest(mglevels[i]->smoothd,KSPConvergedSkip,NULL,NULL);CHKERRQ(ierr);
238336babb1SJed Brown     ierr = KSPSetNormType(mglevels[i]->smoothd,KSP_NORM_NONE);CHKERRQ(ierr);
239336babb1SJed Brown     ierr = KSPGetPC(mglevels[i]->smoothd,&ipc);CHKERRQ(ierr);
240336babb1SJed Brown     ierr = PCSetType(ipc,PCSOR);CHKERRQ(ierr);
241f3fbd535SBarry Smith     ierr = PetscObjectIncrementTabLevel((PetscObject)mglevels[i]->smoothd,(PetscObject)pc,levels-i);CHKERRQ(ierr);
242336babb1SJed Brown     ierr = KSPSetTolerances(mglevels[i]->smoothd,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT, i ? mg->default_smoothd : 1);CHKERRQ(ierr);
243f3fbd535SBarry Smith     ierr = KSPSetOptionsPrefix(mglevels[i]->smoothd,prefix);CHKERRQ(ierr);
244ab83eea4SMatthew G. Knepley     ierr = PetscObjectComposedDataSetInt((PetscObject) mglevels[i]->smoothd, PetscMGLevelId, mglevels[i]->level);CHKERRQ(ierr);
245f3fbd535SBarry Smith 
246f3fbd535SBarry Smith     /* do special stuff for coarse grid */
247f3fbd535SBarry Smith     if (!i && levels > 1) {
248f3fbd535SBarry Smith       ierr = KSPAppendOptionsPrefix(mglevels[0]->smoothd,"mg_coarse_");CHKERRQ(ierr);
249f3fbd535SBarry Smith 
2509dbfc187SHong Zhang       /* coarse solve is (redundant) LU by default; set shifttype NONZERO to avoid annoying zero-pivot in LU preconditioner */
251f3fbd535SBarry Smith       ierr = KSPSetType(mglevels[0]->smoothd,KSPPREONLY);CHKERRQ(ierr);
252f3fbd535SBarry Smith       ierr = KSPGetPC(mglevels[0]->smoothd,&ipc);CHKERRQ(ierr);
253f3fbd535SBarry Smith       ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
254f3fbd535SBarry Smith       if (size > 1) {
2559dbfc187SHong Zhang         KSP innerksp;
2569dbfc187SHong Zhang         PC  innerpc;
257f3fbd535SBarry Smith         ierr = PCSetType(ipc,PCREDUNDANT);CHKERRQ(ierr);
2589dbfc187SHong Zhang         ierr = PCRedundantGetKSP(ipc,&innerksp);CHKERRQ(ierr);
2599dbfc187SHong Zhang         ierr = KSPGetPC(innerksp,&innerpc);CHKERRQ(ierr);
2609dbfc187SHong Zhang         ierr = PCFactorSetShiftType(innerpc,MAT_SHIFT_INBLOCKS);CHKERRQ(ierr);
261f3fbd535SBarry Smith       } else {
262f3fbd535SBarry Smith         ierr = PCSetType(ipc,PCLU);CHKERRQ(ierr);
2639dbfc187SHong Zhang         ierr = PCFactorSetShiftType(ipc,MAT_SHIFT_INBLOCKS);CHKERRQ(ierr);
264f3fbd535SBarry Smith       }
265f3fbd535SBarry Smith     } else {
266f3fbd535SBarry Smith       char tprefix[128];
267f3fbd535SBarry Smith       sprintf(tprefix,"mg_levels_%d_",(int)i);
268f3fbd535SBarry Smith       ierr = KSPAppendOptionsPrefix(mglevels[i]->smoothd,tprefix);CHKERRQ(ierr);
269f3fbd535SBarry Smith     }
2703bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)mglevels[i]->smoothd);CHKERRQ(ierr);
2712fa5cd67SKarl Rupp 
272f3fbd535SBarry Smith     mglevels[i]->smoothu = mglevels[i]->smoothd;
27331567311SBarry Smith     mg->rtol             = 0.0;
27431567311SBarry Smith     mg->abstol           = 0.0;
27531567311SBarry Smith     mg->dtol             = 0.0;
27631567311SBarry Smith     mg->ttol             = 0.0;
27731567311SBarry Smith     mg->cyclesperpcapply = 1;
278f3fbd535SBarry Smith   }
279f3fbd535SBarry Smith   mg->levels = mglevels;
28010eca3edSLisandro Dalcin   ierr = PCMGSetType(pc,mgtype);CHKERRQ(ierr);
2814b9ad928SBarry Smith   PetscFunctionReturn(0);
2824b9ad928SBarry Smith }
2834b9ad928SBarry Smith 
284c07bf074SBarry Smith 
285c07bf074SBarry Smith #undef __FUNCT__
286c07bf074SBarry Smith #define __FUNCT__ "PCDestroy_MG"
287c07bf074SBarry Smith PetscErrorCode PCDestroy_MG(PC pc)
288c07bf074SBarry Smith {
289c07bf074SBarry Smith   PetscErrorCode ierr;
290a06653b4SBarry Smith   PC_MG          *mg        = (PC_MG*)pc->data;
291a06653b4SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
292a06653b4SBarry Smith   PetscInt       i,n;
293c07bf074SBarry Smith 
294c07bf074SBarry Smith   PetscFunctionBegin;
295a06653b4SBarry Smith   ierr = PCReset_MG(pc);CHKERRQ(ierr);
296a06653b4SBarry Smith   if (mglevels) {
297a06653b4SBarry Smith     n = mglevels[0]->levels;
298a06653b4SBarry Smith     for (i=0; i<n; i++) {
299a06653b4SBarry Smith       if (mglevels[i]->smoothd != mglevels[i]->smoothu) {
3006bf464f9SBarry Smith         ierr = KSPDestroy(&mglevels[i]->smoothd);CHKERRQ(ierr);
301a06653b4SBarry Smith       }
3026bf464f9SBarry Smith       ierr = KSPDestroy(&mglevels[i]->smoothu);CHKERRQ(ierr);
303a06653b4SBarry Smith       ierr = PetscFree(mglevels[i]);CHKERRQ(ierr);
304a06653b4SBarry Smith     }
305a06653b4SBarry Smith     ierr = PetscFree(mg->levels);CHKERRQ(ierr);
306a06653b4SBarry Smith   }
307c31cb41cSBarry Smith   ierr = PetscFree(pc->data);CHKERRQ(ierr);
308f3fbd535SBarry Smith   PetscFunctionReturn(0);
309f3fbd535SBarry Smith }
310f3fbd535SBarry Smith 
311f3fbd535SBarry Smith 
312f3fbd535SBarry Smith 
31309573ac7SBarry Smith extern PetscErrorCode PCMGACycle_Private(PC,PC_MG_Levels**);
31409573ac7SBarry Smith extern PetscErrorCode PCMGFCycle_Private(PC,PC_MG_Levels**);
31509573ac7SBarry Smith extern PetscErrorCode PCMGKCycle_Private(PC,PC_MG_Levels**);
316f3fbd535SBarry Smith 
317f3fbd535SBarry Smith /*
318f3fbd535SBarry Smith    PCApply_MG - Runs either an additive, multiplicative, Kaskadic
319f3fbd535SBarry Smith              or full cycle of multigrid.
320f3fbd535SBarry Smith 
321f3fbd535SBarry Smith   Note:
322f3fbd535SBarry Smith   A simple wrapper which calls PCMGMCycle(),PCMGACycle(), or PCMGFCycle().
323f3fbd535SBarry Smith */
324f3fbd535SBarry Smith #undef __FUNCT__
325f3fbd535SBarry Smith #define __FUNCT__ "PCApply_MG"
326f3fbd535SBarry Smith static PetscErrorCode PCApply_MG(PC pc,Vec b,Vec x)
327f3fbd535SBarry Smith {
328f3fbd535SBarry Smith   PC_MG          *mg        = (PC_MG*)pc->data;
329f3fbd535SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
330f3fbd535SBarry Smith   PetscErrorCode ierr;
331f3fbd535SBarry Smith   PetscInt       levels = mglevels[0]->levels,i;
332f3fbd535SBarry Smith 
333f3fbd535SBarry Smith   PetscFunctionBegin;
334a9db87a2SMatthew G Knepley   if (mg->stageApply) {ierr = PetscLogStagePush(mg->stageApply);CHKERRQ(ierr);}
335e1d8e5deSBarry Smith   /* When the DM is supplying the matrix then it will not exist until here */
336298cc208SBarry Smith   for (i=0; i<levels; i++) {
337e1d8e5deSBarry Smith     if (!mglevels[i]->A) {
33823ee1639SBarry Smith       ierr = KSPGetOperators(mglevels[i]->smoothu,&mglevels[i]->A,NULL);CHKERRQ(ierr);
339298cc208SBarry Smith       ierr = PetscObjectReference((PetscObject)mglevels[i]->A);CHKERRQ(ierr);
340e1d8e5deSBarry Smith     }
341e1d8e5deSBarry Smith   }
342e1d8e5deSBarry Smith 
343f3fbd535SBarry Smith   mglevels[levels-1]->b = b;
344f3fbd535SBarry Smith   mglevels[levels-1]->x = x;
34531567311SBarry Smith   if (mg->am == PC_MG_MULTIPLICATIVE) {
346f3fbd535SBarry Smith     ierr = VecSet(x,0.0);CHKERRQ(ierr);
34731567311SBarry Smith     for (i=0; i<mg->cyclesperpcapply; i++) {
3480298fd71SBarry Smith       ierr = PCMGMCycle_Private(pc,mglevels+levels-1,NULL);CHKERRQ(ierr);
349f3fbd535SBarry Smith     }
3502fa5cd67SKarl Rupp   } else if (mg->am == PC_MG_ADDITIVE) {
35131567311SBarry Smith     ierr = PCMGACycle_Private(pc,mglevels);CHKERRQ(ierr);
3522fa5cd67SKarl Rupp   } else if (mg->am == PC_MG_KASKADE) {
35331567311SBarry Smith     ierr = PCMGKCycle_Private(pc,mglevels);CHKERRQ(ierr);
3542fa5cd67SKarl Rupp   } else {
355f3fbd535SBarry Smith     ierr = PCMGFCycle_Private(pc,mglevels);CHKERRQ(ierr);
356f3fbd535SBarry Smith   }
357a9db87a2SMatthew G Knepley   if (mg->stageApply) {ierr = PetscLogStagePop();CHKERRQ(ierr);}
358f3fbd535SBarry Smith   PetscFunctionReturn(0);
359f3fbd535SBarry Smith }
360f3fbd535SBarry Smith 
361f3fbd535SBarry Smith 
362f3fbd535SBarry Smith #undef __FUNCT__
363f3fbd535SBarry Smith #define __FUNCT__ "PCSetFromOptions_MG"
3644416b707SBarry Smith PetscErrorCode PCSetFromOptions_MG(PetscOptionItems *PetscOptionsObject,PC pc)
365f3fbd535SBarry Smith {
366f3fbd535SBarry Smith   PetscErrorCode ierr;
367f3fbd535SBarry Smith   PetscInt       m,levels = 1,cycles;
368c91913d3SJed Brown   PetscBool      flg,set;
369f3fbd535SBarry Smith   PC_MG          *mg        = (PC_MG*)pc->data;
370*3d3eaba7SBarry Smith   PC_MG_Levels   **mglevels;
371f3fbd535SBarry Smith   PCMGType       mgtype;
372f3fbd535SBarry Smith   PCMGCycleType  mgctype;
373f3fbd535SBarry Smith 
374f3fbd535SBarry Smith   PetscFunctionBegin;
375e55864a3SBarry Smith   ierr = PetscOptionsHead(PetscOptionsObject,"Multigrid options");CHKERRQ(ierr);
3763aeaf226SBarry Smith   if (!mg->levels) {
377f3fbd535SBarry Smith     ierr = PetscOptionsInt("-pc_mg_levels","Number of Levels","PCMGSetLevels",levels,&levels,&flg);CHKERRQ(ierr);
378eb3f98d2SBarry Smith     if (!flg && pc->dm) {
379eb3f98d2SBarry Smith       ierr = DMGetRefineLevel(pc->dm,&levels);CHKERRQ(ierr);
380eb3f98d2SBarry Smith       levels++;
3813aeaf226SBarry Smith       mg->usedmfornumberoflevels = PETSC_TRUE;
382eb3f98d2SBarry Smith     }
3830298fd71SBarry Smith     ierr = PCMGSetLevels(pc,levels,NULL);CHKERRQ(ierr);
384f3fbd535SBarry Smith   }
3853aeaf226SBarry Smith   mglevels = mg->levels;
3863aeaf226SBarry Smith 
387f3fbd535SBarry Smith   mgctype = (PCMGCycleType) mglevels[0]->cycles;
388f3fbd535SBarry Smith   ierr    = PetscOptionsEnum("-pc_mg_cycle_type","V cycle or for W-cycle","PCMGSetCycleType",PCMGCycleTypes,(PetscEnum)mgctype,(PetscEnum*)&mgctype,&flg);CHKERRQ(ierr);
389f3fbd535SBarry Smith   if (flg) {
390f3fbd535SBarry Smith     ierr = PCMGSetCycleType(pc,mgctype);CHKERRQ(ierr);
3912fa5cd67SKarl Rupp   }
392f3fbd535SBarry Smith   flg  = PETSC_FALSE;
393c91913d3SJed Brown   ierr = PetscOptionsBool("-pc_mg_galerkin","Use Galerkin process to compute coarser operators","PCMGSetGalerkin",flg,&flg,&set);CHKERRQ(ierr);
394c91913d3SJed Brown   if (set) {
395c91913d3SJed Brown     ierr = PCMGSetGalerkin(pc,flg);CHKERRQ(ierr);
396f3fbd535SBarry Smith   }
39794ae4db5SBarry Smith   ierr = PetscOptionsInt("-pc_mg_smoothup","Number of post-smoothing steps","PCMGSetNumberSmoothUp",mg->default_smoothu,&m,&flg);CHKERRQ(ierr);
398f3fbd535SBarry Smith   if (flg) {
399f3fbd535SBarry Smith     ierr = PCMGSetNumberSmoothUp(pc,m);CHKERRQ(ierr);
400f3fbd535SBarry Smith   }
40194ae4db5SBarry Smith   ierr = PetscOptionsInt("-pc_mg_smoothdown","Number of pre-smoothing steps","PCMGSetNumberSmoothDown",mg->default_smoothd,&m,&flg);CHKERRQ(ierr);
402f3fbd535SBarry Smith   if (flg) {
403f3fbd535SBarry Smith     ierr = PCMGSetNumberSmoothDown(pc,m);CHKERRQ(ierr);
404f3fbd535SBarry Smith   }
40531567311SBarry Smith   mgtype = mg->am;
406f3fbd535SBarry Smith   ierr   = PetscOptionsEnum("-pc_mg_type","Multigrid type","PCMGSetType",PCMGTypes,(PetscEnum)mgtype,(PetscEnum*)&mgtype,&flg);CHKERRQ(ierr);
407f3fbd535SBarry Smith   if (flg) {
408f3fbd535SBarry Smith     ierr = PCMGSetType(pc,mgtype);CHKERRQ(ierr);
409f3fbd535SBarry Smith   }
41031567311SBarry Smith   if (mg->am == PC_MG_MULTIPLICATIVE) {
4115363c1e0SLisandro Dalcin     ierr = PetscOptionsInt("-pc_mg_multiplicative_cycles","Number of cycles for each preconditioner step","PCMGMultiplicativeSetCycles",mg->cyclesperpcapply,&cycles,&flg);CHKERRQ(ierr);
412f3fbd535SBarry Smith     if (flg) {
413f3fbd535SBarry Smith       ierr = PCMGMultiplicativeSetCycles(pc,cycles);CHKERRQ(ierr);
414f3fbd535SBarry Smith     }
415f3fbd535SBarry Smith   }
416f3fbd535SBarry Smith   flg  = PETSC_FALSE;
4170298fd71SBarry Smith   ierr = PetscOptionsBool("-pc_mg_log","Log times for each multigrid level","None",flg,&flg,NULL);CHKERRQ(ierr);
418f3fbd535SBarry Smith   if (flg) {
419f3fbd535SBarry Smith     PetscInt i;
420f3fbd535SBarry Smith     char     eventname[128];
421ce94432eSBarry Smith     if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
422f3fbd535SBarry Smith     levels = mglevels[0]->levels;
423f3fbd535SBarry Smith     for (i=0; i<levels; i++) {
424f3fbd535SBarry Smith       sprintf(eventname,"MGSetup Level %d",(int)i);
42563e6d426SJed Brown       ierr = PetscLogEventRegister(eventname,((PetscObject)pc)->classid,&mglevels[i]->eventsmoothsetup);CHKERRQ(ierr);
426f3fbd535SBarry Smith       sprintf(eventname,"MGSmooth Level %d",(int)i);
42763e6d426SJed Brown       ierr = PetscLogEventRegister(eventname,((PetscObject)pc)->classid,&mglevels[i]->eventsmoothsolve);CHKERRQ(ierr);
428f3fbd535SBarry Smith       if (i) {
429f3fbd535SBarry Smith         sprintf(eventname,"MGResid Level %d",(int)i);
43063e6d426SJed Brown         ierr = PetscLogEventRegister(eventname,((PetscObject)pc)->classid,&mglevels[i]->eventresidual);CHKERRQ(ierr);
431f3fbd535SBarry Smith         sprintf(eventname,"MGInterp Level %d",(int)i);
43263e6d426SJed Brown         ierr = PetscLogEventRegister(eventname,((PetscObject)pc)->classid,&mglevels[i]->eventinterprestrict);CHKERRQ(ierr);
433f3fbd535SBarry Smith       }
434f3fbd535SBarry Smith     }
435eec35531SMatthew G Knepley 
436ec60ca73SSatish Balay #if defined(PETSC_USE_LOG)
437eec35531SMatthew G Knepley     {
438eec35531SMatthew G Knepley       const char    *sname = "MG Apply";
439eec35531SMatthew G Knepley       PetscStageLog stageLog;
440eec35531SMatthew G Knepley       PetscInt      st;
441eec35531SMatthew G Knepley 
442eec35531SMatthew G Knepley       PetscFunctionBegin;
443eec35531SMatthew G Knepley       ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
444eec35531SMatthew G Knepley       for (st = 0; st < stageLog->numStages; ++st) {
445eec35531SMatthew G Knepley         PetscBool same;
446eec35531SMatthew G Knepley 
447eec35531SMatthew G Knepley         ierr = PetscStrcmp(stageLog->stageInfo[st].name, sname, &same);CHKERRQ(ierr);
4482fa5cd67SKarl Rupp         if (same) mg->stageApply = st;
449eec35531SMatthew G Knepley       }
450eec35531SMatthew G Knepley       if (!mg->stageApply) {
451eec35531SMatthew G Knepley         ierr = PetscLogStageRegister(sname, &mg->stageApply);CHKERRQ(ierr);
452eec35531SMatthew G Knepley       }
453eec35531SMatthew G Knepley     }
454ec60ca73SSatish Balay #endif
455f3fbd535SBarry Smith   }
456f3fbd535SBarry Smith   ierr = PetscOptionsTail();CHKERRQ(ierr);
457f3fbd535SBarry Smith   PetscFunctionReturn(0);
458f3fbd535SBarry Smith }
459f3fbd535SBarry Smith 
4606a6fc655SJed Brown const char *const PCMGTypes[] = {"MULTIPLICATIVE","ADDITIVE","FULL","KASKADE","PCMGType","PC_MG",0};
4616a6fc655SJed Brown const char *const PCMGCycleTypes[] = {"invalid","v","w","PCMGCycleType","PC_MG_CYCLE",0};
462f3fbd535SBarry Smith 
4639804daf3SBarry Smith #include <petscdraw.h>
464f3fbd535SBarry Smith #undef __FUNCT__
465f3fbd535SBarry Smith #define __FUNCT__ "PCView_MG"
466f3fbd535SBarry Smith PetscErrorCode PCView_MG(PC pc,PetscViewer viewer)
467f3fbd535SBarry Smith {
468f3fbd535SBarry Smith   PC_MG          *mg        = (PC_MG*)pc->data;
469f3fbd535SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
470f3fbd535SBarry Smith   PetscErrorCode ierr;
471e3deeeafSJed Brown   PetscInt       levels = mglevels ? mglevels[0]->levels : 0,i;
472219b1045SBarry Smith   PetscBool      iascii,isbinary,isdraw;
473f3fbd535SBarry Smith 
474f3fbd535SBarry Smith   PetscFunctionBegin;
475251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
4765b0b0462SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
477219b1045SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
478f3fbd535SBarry Smith   if (iascii) {
479e3deeeafSJed Brown     const char *cyclename = levels ? (mglevels[0]->cycles == PC_MG_CYCLE_V ? "v" : "w") : "unknown";
480e3deeeafSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  MG: type is %s, levels=%D cycles=%s\n", PCMGTypes[mg->am],levels,cyclename);CHKERRQ(ierr);
48131567311SBarry Smith     if (mg->am == PC_MG_MULTIPLICATIVE) {
48231567311SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"    Cycles per PCApply=%d\n",mg->cyclesperpcapply);CHKERRQ(ierr);
483f3fbd535SBarry Smith     }
484218a07d4SBarry Smith     if (mg->galerkin) {
485f3fbd535SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"    Using Galerkin computed coarse grid matrices\n");CHKERRQ(ierr);
4864f66f45eSBarry Smith     } else {
4874f66f45eSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"    Not using Galerkin computed coarse grid matrices\n");CHKERRQ(ierr);
488f3fbd535SBarry Smith     }
4895adeb434SBarry Smith     if (mg->view){
4905adeb434SBarry Smith       ierr = (*mg->view)(pc,viewer);CHKERRQ(ierr);
4915adeb434SBarry Smith     }
492f3fbd535SBarry Smith     for (i=0; i<levels; i++) {
493f3fbd535SBarry Smith       if (!i) {
49489cce641SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"Coarse grid solver -- level -------------------------------\n",i);CHKERRQ(ierr);
495f3fbd535SBarry Smith       } else {
49689cce641SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"Down solver (pre-smoother) on level %D -------------------------------\n",i);CHKERRQ(ierr);
497f3fbd535SBarry Smith       }
498f3fbd535SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
499f3fbd535SBarry Smith       ierr = KSPView(mglevels[i]->smoothd,viewer);CHKERRQ(ierr);
500f3fbd535SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
501f3fbd535SBarry Smith       if (i && mglevels[i]->smoothd == mglevels[i]->smoothu) {
502f3fbd535SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"Up solver (post-smoother) same as down solver (pre-smoother)\n");CHKERRQ(ierr);
503f3fbd535SBarry Smith       } else if (i) {
50489cce641SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"Up solver (post-smoother) on level %D -------------------------------\n",i);CHKERRQ(ierr);
505f3fbd535SBarry Smith         ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
506f3fbd535SBarry Smith         ierr = KSPView(mglevels[i]->smoothu,viewer);CHKERRQ(ierr);
507f3fbd535SBarry Smith         ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
508f3fbd535SBarry Smith       }
509f3fbd535SBarry Smith     }
5105b0b0462SBarry Smith   } else if (isbinary) {
5115b0b0462SBarry Smith     for (i=levels-1; i>=0; i--) {
5125b0b0462SBarry Smith       ierr = KSPView(mglevels[i]->smoothd,viewer);CHKERRQ(ierr);
5135b0b0462SBarry Smith       if (i && mglevels[i]->smoothd != mglevels[i]->smoothu) {
5145b0b0462SBarry Smith         ierr = KSPView(mglevels[i]->smoothu,viewer);CHKERRQ(ierr);
5155b0b0462SBarry Smith       }
5165b0b0462SBarry Smith     }
517219b1045SBarry Smith   } else if (isdraw) {
518219b1045SBarry Smith     PetscDraw draw;
519b4375e8dSPeter Brune     PetscReal x,w,y,bottom,th;
520219b1045SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
521219b1045SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
5220298fd71SBarry Smith     ierr   = PetscDrawStringGetSize(draw,NULL,&th);CHKERRQ(ierr);
523219b1045SBarry Smith     bottom = y - th;
524219b1045SBarry Smith     for (i=levels-1; i>=0; i--) {
525b4375e8dSPeter Brune       if (!mglevels[i]->smoothu || (mglevels[i]->smoothu == mglevels[i]->smoothd)) {
526219b1045SBarry Smith         ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
527219b1045SBarry Smith         ierr = KSPView(mglevels[i]->smoothd,viewer);CHKERRQ(ierr);
528219b1045SBarry Smith         ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
529b4375e8dSPeter Brune       } else {
530b4375e8dSPeter Brune         w    = 0.5*PetscMin(1.0-x,x);
531b4375e8dSPeter Brune         ierr = PetscDrawPushCurrentPoint(draw,x+w,bottom);CHKERRQ(ierr);
532b4375e8dSPeter Brune         ierr = KSPView(mglevels[i]->smoothd,viewer);CHKERRQ(ierr);
533b4375e8dSPeter Brune         ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
534b4375e8dSPeter Brune         ierr = PetscDrawPushCurrentPoint(draw,x-w,bottom);CHKERRQ(ierr);
535b4375e8dSPeter Brune         ierr = KSPView(mglevels[i]->smoothu,viewer);CHKERRQ(ierr);
536b4375e8dSPeter Brune         ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
537b4375e8dSPeter Brune       }
5380298fd71SBarry Smith       ierr    = PetscDrawGetBoundingBox(draw,NULL,&bottom,NULL,NULL);CHKERRQ(ierr);
5391cd381d6SBarry Smith       bottom -= th;
540219b1045SBarry Smith     }
5415b0b0462SBarry Smith   }
542f3fbd535SBarry Smith   PetscFunctionReturn(0);
543f3fbd535SBarry Smith }
544f3fbd535SBarry Smith 
545af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
546af0996ceSBarry Smith #include <petsc/private/kspimpl.h>
547cab2e9ccSBarry Smith 
548f3fbd535SBarry Smith /*
549f3fbd535SBarry Smith     Calls setup for the KSP on each level
550f3fbd535SBarry Smith */
551f3fbd535SBarry Smith #undef __FUNCT__
552f3fbd535SBarry Smith #define __FUNCT__ "PCSetUp_MG"
553f3fbd535SBarry Smith PetscErrorCode PCSetUp_MG(PC pc)
554f3fbd535SBarry Smith {
555f3fbd535SBarry Smith   PC_MG          *mg        = (PC_MG*)pc->data;
556f3fbd535SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
557f3fbd535SBarry Smith   PetscErrorCode ierr;
558f3fbd535SBarry Smith   PetscInt       i,n = mglevels[0]->levels;
55998e04984SBarry Smith   PC             cpc;
5603db492dfSBarry Smith   PetscBool      dump = PETSC_FALSE,opsset,use_amat,missinginterpolate = PETSC_FALSE;
561f3fbd535SBarry Smith   Mat            dA,dB;
562f3fbd535SBarry Smith   Vec            tvec;
563218a07d4SBarry Smith   DM             *dms;
564649052a6SBarry Smith   PetscViewer    viewer = 0;
565f3fbd535SBarry Smith 
566f3fbd535SBarry Smith   PetscFunctionBegin;
56701bc414fSDmitry Karpeev   /* FIX: Move this to PCSetFromOptions_MG? */
5683aeaf226SBarry Smith   if (mg->usedmfornumberoflevels) {
5693aeaf226SBarry Smith     PetscInt levels;
5703aeaf226SBarry Smith     ierr = DMGetRefineLevel(pc->dm,&levels);CHKERRQ(ierr);
5713aeaf226SBarry Smith     levels++;
5723aeaf226SBarry Smith     if (levels > n) { /* the problem is now being solved on a finer grid */
5730298fd71SBarry Smith       ierr     = PCMGSetLevels(pc,levels,NULL);CHKERRQ(ierr);
5743aeaf226SBarry Smith       n        = levels;
5753aeaf226SBarry Smith       ierr     = PCSetFromOptions(pc);CHKERRQ(ierr); /* it is bad to call this here, but otherwise will never be called for the new hierarchy */
5763aeaf226SBarry Smith       mglevels =  mg->levels;
5773aeaf226SBarry Smith     }
5783aeaf226SBarry Smith   }
57998e04984SBarry Smith   ierr = KSPGetPC(mglevels[0]->smoothd,&cpc);CHKERRQ(ierr);
5803aeaf226SBarry Smith 
581f3fbd535SBarry Smith 
582f3fbd535SBarry Smith   /* If user did not provide fine grid operators OR operator was not updated since last global KSPSetOperators() */
583f3fbd535SBarry Smith   /* so use those from global PC */
584f3fbd535SBarry Smith   /* Is this what we always want? What if user wants to keep old one? */
5850298fd71SBarry Smith   ierr = KSPGetOperatorsSet(mglevels[n-1]->smoothd,NULL,&opsset);CHKERRQ(ierr);
58698e04984SBarry Smith   if (opsset) {
58798e04984SBarry Smith     Mat mmat;
58823ee1639SBarry Smith     ierr = KSPGetOperators(mglevels[n-1]->smoothd,NULL,&mmat);CHKERRQ(ierr);
58998e04984SBarry Smith     if (mmat == pc->pmat) opsset = PETSC_FALSE;
59098e04984SBarry Smith   }
5914a5f07a5SMark F. Adams 
5924a5f07a5SMark F. Adams   if (!opsset) {
59371b23a65SMark F. Adams     ierr = PCGetUseAmat(pc,&use_amat);CHKERRQ(ierr);
5944a5f07a5SMark F. Adams     if(use_amat){
595f3fbd535SBarry Smith       ierr = PetscInfo(pc,"Using outer operators to define finest grid operator \n  because PCMGGetSmoother(pc,nlevels-1,&ksp);KSPSetOperators(ksp,...); was not called.\n");CHKERRQ(ierr);
59623ee1639SBarry Smith       ierr = KSPSetOperators(mglevels[n-1]->smoothd,pc->mat,pc->pmat);CHKERRQ(ierr);
597f3fbd535SBarry Smith     }
5984a5f07a5SMark F. Adams     else {
5994a5f07a5SMark F. Adams       ierr = PetscInfo(pc,"Using matrix (pmat) operators to define finest grid operator \n  because PCMGGetSmoother(pc,nlevels-1,&ksp);KSPSetOperators(ksp,...); was not called.\n");CHKERRQ(ierr);
60023ee1639SBarry Smith       ierr = KSPSetOperators(mglevels[n-1]->smoothd,pc->pmat,pc->pmat);CHKERRQ(ierr);
6014a5f07a5SMark F. Adams     }
6024a5f07a5SMark F. Adams   }
603f3fbd535SBarry Smith 
6049c7ffb39SBarry Smith   for (i=n-1; i>0; i--) {
6059c7ffb39SBarry Smith     if (!(mglevels[i]->interpolate || mglevels[i]->restrct)) {
6069c7ffb39SBarry Smith       missinginterpolate = PETSC_TRUE;
6079c7ffb39SBarry Smith       continue;
6089c7ffb39SBarry Smith     }
6099c7ffb39SBarry Smith   }
6109c7ffb39SBarry Smith   /*
6119c7ffb39SBarry Smith    Skipping if user has provided all interpolation/restriction needed (since DM might not be able to produce them (when coming from SNES/TS)
6129c7ffb39SBarry Smith    Skipping for galerkin==2 (externally managed hierarchy such as ML and GAMG). Cleaner logic here would be great. Wrap ML/GAMG as DMs?
6139c7ffb39SBarry Smith   */
6149c7ffb39SBarry Smith   if (missinginterpolate && pc->dm && mg->galerkin != 2 && !pc->setupcalled) {
6152d2b81a6SBarry Smith     /* construct the interpolation from the DMs */
6162e499ae9SBarry Smith     Mat p;
61773250ac0SBarry Smith     Vec rscale;
618785e854fSJed Brown     ierr     = PetscMalloc1(n,&dms);CHKERRQ(ierr);
619218a07d4SBarry Smith     dms[n-1] = pc->dm;
620ef1267afSMatthew G. Knepley     /* Separately create them so we do not get DMKSP interference between levels */
621ef1267afSMatthew G. Knepley     for (i=n-2; i>-1; i--) {ierr = DMCoarsen(dms[i+1],MPI_COMM_NULL,&dms[i]);CHKERRQ(ierr);}
622218a07d4SBarry Smith     for (i=n-2; i>-1; i--) {
623942e3340SBarry Smith       DMKSP     kdm;
6243ad4599aSBarry Smith       PetscBool dmhasrestrict;
6253c0c59f3SBarry Smith       ierr = KSPSetDM(mglevels[i]->smoothd,dms[i]);CHKERRQ(ierr);
6263c0c59f3SBarry Smith       if (mg->galerkin) {ierr = KSPSetDMActive(mglevels[i]->smoothd,PETSC_FALSE);CHKERRQ(ierr);}
627942e3340SBarry Smith       ierr = DMGetDMKSPWrite(dms[i],&kdm);CHKERRQ(ierr);
628d1a3e677SJed Brown       /* Ugly hack so that the next KSPSetUp() will use the RHS that we set. A better fix is to change dmActive to take
629d1a3e677SJed Brown        * a bitwise OR of computing the matrix, RHS, and initial iterate. */
6300298fd71SBarry Smith       kdm->ops->computerhs = NULL;
6310298fd71SBarry Smith       kdm->rhsctx          = NULL;
63224c3aa18SBarry Smith       if (!mglevels[i+1]->interpolate) {
633e727c939SJed Brown         ierr = DMCreateInterpolation(dms[i],dms[i+1],&p,&rscale);CHKERRQ(ierr);
6342d2b81a6SBarry Smith         ierr = PCMGSetInterpolation(pc,i+1,p);CHKERRQ(ierr);
63500ac8be1SBarry Smith         if (rscale) {ierr = PCMGSetRScale(pc,i+1,rscale);CHKERRQ(ierr);}
63673250ac0SBarry Smith         ierr = VecDestroy(&rscale);CHKERRQ(ierr);
6376bf464f9SBarry Smith         ierr = MatDestroy(&p);CHKERRQ(ierr);
638218a07d4SBarry Smith       }
6393ad4599aSBarry Smith       ierr = DMHasCreateRestriction(dms[i],&dmhasrestrict);CHKERRQ(ierr);
6403ad4599aSBarry Smith       if (dmhasrestrict && !mglevels[i+1]->restrct){
6413ad4599aSBarry Smith         ierr = DMCreateRestriction(dms[i],dms[i+1],&p);CHKERRQ(ierr);
6423ad4599aSBarry Smith         ierr = PCMGSetRestriction(pc,i+1,p);CHKERRQ(ierr);
6433ad4599aSBarry Smith         ierr = MatDestroy(&p);CHKERRQ(ierr);
6443ad4599aSBarry Smith       }
64524c3aa18SBarry Smith     }
6462d2b81a6SBarry Smith 
647ef1267afSMatthew G. Knepley     for (i=n-2; i>-1; i--) {ierr = DMDestroy(&dms[i]);CHKERRQ(ierr);}
6482d2b81a6SBarry Smith     ierr = PetscFree(dms);CHKERRQ(ierr);
649ce4cda84SJed Brown   }
650cab2e9ccSBarry Smith 
651ce4cda84SJed Brown   if (pc->dm && !pc->setupcalled) {
652ce4cda84SJed Brown     /* finest smoother also gets DM but it is not active, independent of whether galerkin==2 */
653cab2e9ccSBarry Smith     ierr = KSPSetDM(mglevels[n-1]->smoothd,pc->dm);CHKERRQ(ierr);
654cab2e9ccSBarry Smith     ierr = KSPSetDMActive(mglevels[n-1]->smoothd,PETSC_FALSE);CHKERRQ(ierr);
655218a07d4SBarry Smith   }
656218a07d4SBarry Smith 
657c91913d3SJed Brown   if (mg->galerkin == 1) {
658f3fbd535SBarry Smith     Mat B;
659f3fbd535SBarry Smith     /* currently only handle case where mat and pmat are the same on coarser levels */
66023ee1639SBarry Smith     ierr = KSPGetOperators(mglevels[n-1]->smoothd,&dA,&dB);CHKERRQ(ierr);
661f3fbd535SBarry Smith     if (!pc->setupcalled) {
662f3fbd535SBarry Smith       for (i=n-2; i>-1; i--) {
663b9367914SBarry Smith         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");
664b9367914SBarry Smith         if (!mglevels[i+1]->interpolate) {
665b9367914SBarry Smith           ierr = PCMGSetInterpolation(pc,i+1,mglevels[i+1]->restrct);CHKERRQ(ierr);
666b9367914SBarry Smith         }
667b9367914SBarry Smith         if (!mglevels[i+1]->restrct) {
668b9367914SBarry Smith           ierr = PCMGSetRestriction(pc,i+1,mglevels[i+1]->interpolate);CHKERRQ(ierr);
669b9367914SBarry Smith         }
670b9367914SBarry Smith         if (mglevels[i+1]->interpolate == mglevels[i+1]->restrct) {
671f3fbd535SBarry Smith           ierr = MatPtAP(dB,mglevels[i+1]->interpolate,MAT_INITIAL_MATRIX,1.0,&B);CHKERRQ(ierr);
6727d537d92SJed Brown         } else {
6737d537d92SJed Brown           ierr = MatMatMatMult(mglevels[i+1]->restrct,dB,mglevels[i+1]->interpolate,MAT_INITIAL_MATRIX,1.0,&B);CHKERRQ(ierr);
6747d537d92SJed Brown         }
67523ee1639SBarry Smith         ierr = KSPSetOperators(mglevels[i]->smoothd,B,B);CHKERRQ(ierr);
676f3fbd535SBarry Smith         if (i != n-2) {ierr = PetscObjectDereference((PetscObject)dB);CHKERRQ(ierr);}
677f3fbd535SBarry Smith         dB = B;
678f3fbd535SBarry Smith       }
679cd9507b2SBarry Smith       if (n > 1) {ierr = PetscObjectDereference((PetscObject)dB);CHKERRQ(ierr);}
680f3fbd535SBarry Smith     } else {
681f3fbd535SBarry Smith       for (i=n-2; i>-1; i--) {
682b9367914SBarry Smith         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");
683b9367914SBarry Smith         if (!mglevels[i+1]->interpolate) {
684b9367914SBarry Smith           ierr = PCMGSetInterpolation(pc,i+1,mglevels[i+1]->restrct);CHKERRQ(ierr);
685b9367914SBarry Smith         }
686b9367914SBarry Smith         if (!mglevels[i+1]->restrct) {
687b9367914SBarry Smith           ierr = PCMGSetRestriction(pc,i+1,mglevels[i+1]->interpolate);CHKERRQ(ierr);
688b9367914SBarry Smith         }
68923ee1639SBarry Smith         ierr = KSPGetOperators(mglevels[i]->smoothd,NULL,&B);CHKERRQ(ierr);
690b9367914SBarry Smith         if (mglevels[i+1]->interpolate == mglevels[i+1]->restrct) {
691f3fbd535SBarry Smith           ierr = MatPtAP(dB,mglevels[i+1]->interpolate,MAT_REUSE_MATRIX,1.0,&B);CHKERRQ(ierr);
6927d537d92SJed Brown         } else {
6937d537d92SJed Brown           ierr = MatMatMatMult(mglevels[i+1]->restrct,dB,mglevels[i+1]->interpolate,MAT_REUSE_MATRIX,1.0,&B);CHKERRQ(ierr);
6947d537d92SJed Brown         }
69523ee1639SBarry Smith         ierr = KSPSetOperators(mglevels[i]->smoothd,B,B);CHKERRQ(ierr);
696f3fbd535SBarry Smith         dB   = B;
697f3fbd535SBarry Smith       }
698f3fbd535SBarry Smith     }
699ce4cda84SJed Brown   } else if (!mg->galerkin && pc->dm && pc->dm->x) {
700cab2e9ccSBarry Smith     /* need to restrict Jacobian location to coarser meshes for evaluation */
701cab2e9ccSBarry Smith     for (i=n-2; i>-1; i--) {
702c88c5224SJed Brown       Mat R;
703c88c5224SJed Brown       Vec rscale;
704cab2e9ccSBarry Smith       if (!mglevels[i]->smoothd->dm->x) {
705cab2e9ccSBarry Smith         Vec *vecs;
7062a7a6963SBarry Smith         ierr = KSPCreateVecs(mglevels[i]->smoothd,1,&vecs,0,NULL);CHKERRQ(ierr);
7072fa5cd67SKarl Rupp 
708cab2e9ccSBarry Smith         mglevels[i]->smoothd->dm->x = vecs[0];
7092fa5cd67SKarl Rupp 
710cab2e9ccSBarry Smith         ierr = PetscFree(vecs);CHKERRQ(ierr);
711cab2e9ccSBarry Smith       }
712c88c5224SJed Brown       ierr = PCMGGetRestriction(pc,i+1,&R);CHKERRQ(ierr);
713c88c5224SJed Brown       ierr = PCMGGetRScale(pc,i+1,&rscale);CHKERRQ(ierr);
714c88c5224SJed Brown       ierr = MatRestrict(R,mglevels[i+1]->smoothd->dm->x,mglevels[i]->smoothd->dm->x);CHKERRQ(ierr);
715c88c5224SJed Brown       ierr = VecPointwiseMult(mglevels[i]->smoothd->dm->x,mglevels[i]->smoothd->dm->x,rscale);CHKERRQ(ierr);
716cab2e9ccSBarry Smith     }
717f3fbd535SBarry Smith   }
718ccceb50cSJed Brown   if (!mg->galerkin && pc->dm) {
719caa4e7f2SJed Brown     for (i=n-2; i>=0; i--) {
720ccceb50cSJed Brown       DM  dmfine,dmcoarse;
721caa4e7f2SJed Brown       Mat Restrict,Inject;
722caa4e7f2SJed Brown       Vec rscale;
723ccceb50cSJed Brown       ierr   = KSPGetDM(mglevels[i+1]->smoothd,&dmfine);CHKERRQ(ierr);
724ccceb50cSJed Brown       ierr   = KSPGetDM(mglevels[i]->smoothd,&dmcoarse);CHKERRQ(ierr);
725caa4e7f2SJed Brown       ierr   = PCMGGetRestriction(pc,i+1,&Restrict);CHKERRQ(ierr);
726caa4e7f2SJed Brown       ierr   = PCMGGetRScale(pc,i+1,&rscale);CHKERRQ(ierr);
7270298fd71SBarry Smith       Inject = NULL;      /* Callback should create it if it needs Injection */
728caa4e7f2SJed Brown       ierr   = DMRestrict(dmfine,Restrict,rscale,Inject,dmcoarse);CHKERRQ(ierr);
729caa4e7f2SJed Brown     }
730caa4e7f2SJed Brown   }
731f3fbd535SBarry Smith 
732f3fbd535SBarry Smith   if (!pc->setupcalled) {
733f3fbd535SBarry Smith     for (i=0; i<n; i++) {
734f3fbd535SBarry Smith       ierr = KSPSetFromOptions(mglevels[i]->smoothd);CHKERRQ(ierr);
735f3fbd535SBarry Smith     }
736f3fbd535SBarry Smith     for (i=1; i<n; i++) {
737f3fbd535SBarry Smith       if (mglevels[i]->smoothu && (mglevels[i]->smoothu != mglevels[i]->smoothd)) {
738f3fbd535SBarry Smith         ierr = KSPSetFromOptions(mglevels[i]->smoothu);CHKERRQ(ierr);
739f3fbd535SBarry Smith       }
740f3fbd535SBarry Smith     }
7413ad4599aSBarry Smith     /* insure that if either interpolation or restriction is set the other other one is set */
742f3fbd535SBarry Smith     for (i=1; i<n; i++) {
7433ad4599aSBarry Smith       ierr = PCMGGetInterpolation(pc,i,NULL);CHKERRQ(ierr);
7443ad4599aSBarry Smith       ierr = PCMGGetRestriction(pc,i,NULL);CHKERRQ(ierr);
745f3fbd535SBarry Smith     }
746f3fbd535SBarry Smith     for (i=0; i<n-1; i++) {
747f3fbd535SBarry Smith       if (!mglevels[i]->b) {
748f3fbd535SBarry Smith         Vec *vec;
7492a7a6963SBarry Smith         ierr = KSPCreateVecs(mglevels[i]->smoothd,1,&vec,0,NULL);CHKERRQ(ierr);
750f3fbd535SBarry Smith         ierr = PCMGSetRhs(pc,i,*vec);CHKERRQ(ierr);
7516bf464f9SBarry Smith         ierr = VecDestroy(vec);CHKERRQ(ierr);
752f3fbd535SBarry Smith         ierr = PetscFree(vec);CHKERRQ(ierr);
753f3fbd535SBarry Smith       }
754f3fbd535SBarry Smith       if (!mglevels[i]->r && i) {
755f3fbd535SBarry Smith         ierr = VecDuplicate(mglevels[i]->b,&tvec);CHKERRQ(ierr);
756f3fbd535SBarry Smith         ierr = PCMGSetR(pc,i,tvec);CHKERRQ(ierr);
7576bf464f9SBarry Smith         ierr = VecDestroy(&tvec);CHKERRQ(ierr);
758f3fbd535SBarry Smith       }
759f3fbd535SBarry Smith       if (!mglevels[i]->x) {
760f3fbd535SBarry Smith         ierr = VecDuplicate(mglevels[i]->b,&tvec);CHKERRQ(ierr);
761f3fbd535SBarry Smith         ierr = PCMGSetX(pc,i,tvec);CHKERRQ(ierr);
7626bf464f9SBarry Smith         ierr = VecDestroy(&tvec);CHKERRQ(ierr);
763f3fbd535SBarry Smith       }
764f3fbd535SBarry Smith     }
765f3fbd535SBarry Smith     if (n != 1 && !mglevels[n-1]->r) {
766f3fbd535SBarry Smith       /* PCMGSetR() on the finest level if user did not supply it */
767f3fbd535SBarry Smith       Vec *vec;
7682a7a6963SBarry Smith       ierr = KSPCreateVecs(mglevels[n-1]->smoothd,1,&vec,0,NULL);CHKERRQ(ierr);
769f3fbd535SBarry Smith       ierr = PCMGSetR(pc,n-1,*vec);CHKERRQ(ierr);
7706bf464f9SBarry Smith       ierr = VecDestroy(vec);CHKERRQ(ierr);
771f3fbd535SBarry Smith       ierr = PetscFree(vec);CHKERRQ(ierr);
772f3fbd535SBarry Smith     }
773f3fbd535SBarry Smith   }
774f3fbd535SBarry Smith 
77598e04984SBarry Smith   if (pc->dm) {
77698e04984SBarry Smith     /* need to tell all the coarser levels to rebuild the matrix using the DM for that level */
77798e04984SBarry Smith     for (i=0; i<n-1; i++) {
77898e04984SBarry Smith       if (mglevels[i]->smoothd->setupstage != KSP_SETUP_NEW) mglevels[i]->smoothd->setupstage = KSP_SETUP_NEWMATRIX;
77998e04984SBarry Smith     }
78098e04984SBarry Smith   }
781f3fbd535SBarry Smith 
782f3fbd535SBarry Smith   for (i=1; i<n; i++) {
7832a21e185SBarry Smith     if (mglevels[i]->smoothu == mglevels[i]->smoothd || mg->am == PC_MG_FULL || mg->am == PC_MG_KASKADE || mg->cyclesperpcapply > 1){
784f3fbd535SBarry Smith       /* if doing only down then initial guess is zero */
785f3fbd535SBarry Smith       ierr = KSPSetInitialGuessNonzero(mglevels[i]->smoothd,PETSC_TRUE);CHKERRQ(ierr);
786f3fbd535SBarry Smith     }
78763e6d426SJed Brown     if (mglevels[i]->eventsmoothsetup) {ierr = PetscLogEventBegin(mglevels[i]->eventsmoothsetup,0,0,0,0);CHKERRQ(ierr);}
788f3fbd535SBarry Smith     ierr = KSPSetUp(mglevels[i]->smoothd);CHKERRQ(ierr);
789899639b0SHong Zhang     if (mglevels[i]->smoothd->reason == KSP_DIVERGED_PCSETUP_FAILED) {
790899639b0SHong Zhang       pc->failedreason = PC_SUBPC_ERROR;
791899639b0SHong Zhang     }
79263e6d426SJed Brown     if (mglevels[i]->eventsmoothsetup) {ierr = PetscLogEventEnd(mglevels[i]->eventsmoothsetup,0,0,0,0);CHKERRQ(ierr);}
793d42688cbSBarry Smith     if (!mglevels[i]->residual) {
794d42688cbSBarry Smith       Mat mat;
79523ee1639SBarry Smith       ierr = KSPGetOperators(mglevels[i]->smoothd,NULL,&mat);CHKERRQ(ierr);
79654b2cd4bSJed Brown       ierr = PCMGSetResidual(pc,i,PCMGResidualDefault,mat);CHKERRQ(ierr);
797d42688cbSBarry Smith     }
798f3fbd535SBarry Smith   }
799f3fbd535SBarry Smith   for (i=1; i<n; i++) {
800f3fbd535SBarry Smith     if (mglevels[i]->smoothu && mglevels[i]->smoothu != mglevels[i]->smoothd) {
801f3fbd535SBarry Smith       Mat          downmat,downpmat;
802f3fbd535SBarry Smith 
803f3fbd535SBarry Smith       /* check if operators have been set for up, if not use down operators to set them */
8040298fd71SBarry Smith       ierr = KSPGetOperatorsSet(mglevels[i]->smoothu,&opsset,NULL);CHKERRQ(ierr);
805f3fbd535SBarry Smith       if (!opsset) {
80623ee1639SBarry Smith         ierr = KSPGetOperators(mglevels[i]->smoothd,&downmat,&downpmat);CHKERRQ(ierr);
80723ee1639SBarry Smith         ierr = KSPSetOperators(mglevels[i]->smoothu,downmat,downpmat);CHKERRQ(ierr);
808f3fbd535SBarry Smith       }
809f3fbd535SBarry Smith 
810f3fbd535SBarry Smith       ierr = KSPSetInitialGuessNonzero(mglevels[i]->smoothu,PETSC_TRUE);CHKERRQ(ierr);
81163e6d426SJed Brown       if (mglevels[i]->eventsmoothsetup) {ierr = PetscLogEventBegin(mglevels[i]->eventsmoothsetup,0,0,0,0);CHKERRQ(ierr);}
812f3fbd535SBarry Smith       ierr = KSPSetUp(mglevels[i]->smoothu);CHKERRQ(ierr);
813899639b0SHong Zhang       if (mglevels[i]->smoothu->reason == KSP_DIVERGED_PCSETUP_FAILED) {
814899639b0SHong Zhang         pc->failedreason = PC_SUBPC_ERROR;
815899639b0SHong Zhang       }
81663e6d426SJed Brown       if (mglevels[i]->eventsmoothsetup) {ierr = PetscLogEventEnd(mglevels[i]->eventsmoothsetup,0,0,0,0);CHKERRQ(ierr);}
817f3fbd535SBarry Smith     }
818f3fbd535SBarry Smith   }
819f3fbd535SBarry Smith 
82063e6d426SJed Brown   if (mglevels[0]->eventsmoothsetup) {ierr = PetscLogEventBegin(mglevels[0]->eventsmoothsetup,0,0,0,0);CHKERRQ(ierr);}
821f3fbd535SBarry Smith   ierr = KSPSetUp(mglevels[0]->smoothd);CHKERRQ(ierr);
822899639b0SHong Zhang   if (mglevels[0]->smoothd->reason == KSP_DIVERGED_PCSETUP_FAILED) {
823899639b0SHong Zhang     pc->failedreason = PC_SUBPC_ERROR;
824899639b0SHong Zhang   }
82563e6d426SJed Brown   if (mglevels[0]->eventsmoothsetup) {ierr = PetscLogEventEnd(mglevels[0]->eventsmoothsetup,0,0,0,0);CHKERRQ(ierr);}
826f3fbd535SBarry Smith 
827f3fbd535SBarry Smith   /*
828f3fbd535SBarry Smith      Dump the interpolation/restriction matrices plus the
829e3c5b3baSBarry Smith    Jacobian/stiffness on each level. This allows MATLAB users to
830f3fbd535SBarry Smith    easily check if the Galerkin condition A_c = R A_f R^T is satisfied.
831f3fbd535SBarry Smith 
832f3fbd535SBarry Smith    Only support one or the other at the same time.
833f3fbd535SBarry Smith   */
834f3fbd535SBarry Smith #if defined(PETSC_USE_SOCKET_VIEWER)
835c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)pc)->options,((PetscObject)pc)->prefix,"-pc_mg_dump_matlab",&dump,NULL);CHKERRQ(ierr);
836ce94432eSBarry Smith   if (dump) viewer = PETSC_VIEWER_SOCKET_(PetscObjectComm((PetscObject)pc));
837f3fbd535SBarry Smith   dump = PETSC_FALSE;
838f3fbd535SBarry Smith #endif
839c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)pc)->options,((PetscObject)pc)->prefix,"-pc_mg_dump_binary",&dump,NULL);CHKERRQ(ierr);
840ce94432eSBarry Smith   if (dump) viewer = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)pc));
841f3fbd535SBarry Smith 
842f3fbd535SBarry Smith   if (viewer) {
843f3fbd535SBarry Smith     for (i=1; i<n; i++) {
844f3fbd535SBarry Smith       ierr = MatView(mglevels[i]->restrct,viewer);CHKERRQ(ierr);
845f3fbd535SBarry Smith     }
846f3fbd535SBarry Smith     for (i=0; i<n; i++) {
847f3fbd535SBarry Smith       ierr = KSPGetPC(mglevels[i]->smoothd,&pc);CHKERRQ(ierr);
848f3fbd535SBarry Smith       ierr = MatView(pc->mat,viewer);CHKERRQ(ierr);
849f3fbd535SBarry Smith     }
850f3fbd535SBarry Smith   }
851f3fbd535SBarry Smith   PetscFunctionReturn(0);
852f3fbd535SBarry Smith }
853f3fbd535SBarry Smith 
854f3fbd535SBarry Smith /* -------------------------------------------------------------------------------------*/
855f3fbd535SBarry Smith 
856f3fbd535SBarry Smith #undef __FUNCT__
8579dcbbd2bSBarry Smith #define __FUNCT__ "PCMGGetLevels"
8584b9ad928SBarry Smith /*@
85997177400SBarry Smith    PCMGGetLevels - Gets the number of levels to use with MG.
8604b9ad928SBarry Smith 
8614b9ad928SBarry Smith    Not Collective
8624b9ad928SBarry Smith 
8634b9ad928SBarry Smith    Input Parameter:
8644b9ad928SBarry Smith .  pc - the preconditioner context
8654b9ad928SBarry Smith 
8664b9ad928SBarry Smith    Output parameter:
8674b9ad928SBarry Smith .  levels - the number of levels
8684b9ad928SBarry Smith 
8694b9ad928SBarry Smith    Level: advanced
8704b9ad928SBarry Smith 
8714b9ad928SBarry Smith .keywords: MG, get, levels, multigrid
8724b9ad928SBarry Smith 
87397177400SBarry Smith .seealso: PCMGSetLevels()
8744b9ad928SBarry Smith @*/
8757087cfbeSBarry Smith PetscErrorCode  PCMGGetLevels(PC pc,PetscInt *levels)
8764b9ad928SBarry Smith {
877f3fbd535SBarry Smith   PC_MG *mg = (PC_MG*)pc->data;
8784b9ad928SBarry Smith 
8794b9ad928SBarry Smith   PetscFunctionBegin;
8800700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
8814482741eSBarry Smith   PetscValidIntPointer(levels,2);
882f3fbd535SBarry Smith   *levels = mg->nlevels;
8834b9ad928SBarry Smith   PetscFunctionReturn(0);
8844b9ad928SBarry Smith }
8854b9ad928SBarry Smith 
8864b9ad928SBarry Smith #undef __FUNCT__
8879dcbbd2bSBarry Smith #define __FUNCT__ "PCMGSetType"
8884b9ad928SBarry Smith /*@
88997177400SBarry Smith    PCMGSetType - Determines the form of multigrid to use:
8904b9ad928SBarry Smith    multiplicative, additive, full, or the Kaskade algorithm.
8914b9ad928SBarry Smith 
892ad4df100SBarry Smith    Logically Collective on PC
8934b9ad928SBarry Smith 
8944b9ad928SBarry Smith    Input Parameters:
8954b9ad928SBarry Smith +  pc - the preconditioner context
8969dcbbd2bSBarry Smith -  form - multigrid form, one of PC_MG_MULTIPLICATIVE, PC_MG_ADDITIVE,
8979dcbbd2bSBarry Smith    PC_MG_FULL, PC_MG_KASKADE
8984b9ad928SBarry Smith 
8994b9ad928SBarry Smith    Options Database Key:
9004b9ad928SBarry Smith .  -pc_mg_type <form> - Sets <form>, one of multiplicative,
9014b9ad928SBarry Smith    additive, full, kaskade
9024b9ad928SBarry Smith 
9034b9ad928SBarry Smith    Level: advanced
9044b9ad928SBarry Smith 
9054b9ad928SBarry Smith .keywords: MG, set, method, multiplicative, additive, full, Kaskade, multigrid
9064b9ad928SBarry Smith 
90797177400SBarry Smith .seealso: PCMGSetLevels()
9084b9ad928SBarry Smith @*/
9097087cfbeSBarry Smith PetscErrorCode  PCMGSetType(PC pc,PCMGType form)
9104b9ad928SBarry Smith {
911f3fbd535SBarry Smith   PC_MG *mg = (PC_MG*)pc->data;
9124b9ad928SBarry Smith 
9134b9ad928SBarry Smith   PetscFunctionBegin;
9140700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
915c5eb9154SBarry Smith   PetscValidLogicalCollectiveEnum(pc,form,2);
91631567311SBarry Smith   mg->am = form;
9179dcbbd2bSBarry Smith   if (form == PC_MG_MULTIPLICATIVE) pc->ops->applyrichardson = PCApplyRichardson_MG;
918c60c7ad4SBarry Smith   else pc->ops->applyrichardson = NULL;
919c60c7ad4SBarry Smith   PetscFunctionReturn(0);
920c60c7ad4SBarry Smith }
921c60c7ad4SBarry Smith 
922f0af28e8SLisandro Dalcin #undef __FUNCT__
923f0af28e8SLisandro Dalcin #define __FUNCT__ "PCMGGetType"
924c60c7ad4SBarry Smith /*@
925c60c7ad4SBarry Smith    PCMGGetType - Determines the form of multigrid to use:
926c60c7ad4SBarry Smith    multiplicative, additive, full, or the Kaskade algorithm.
927c60c7ad4SBarry Smith 
928c60c7ad4SBarry Smith    Logically Collective on PC
929c60c7ad4SBarry Smith 
930c60c7ad4SBarry Smith    Input Parameter:
931c60c7ad4SBarry Smith .  pc - the preconditioner context
932c60c7ad4SBarry Smith 
933c60c7ad4SBarry Smith    Output Parameter:
934c60c7ad4SBarry Smith .  type - one of PC_MG_MULTIPLICATIVE, PC_MG_ADDITIVE,PC_MG_FULL, PC_MG_KASKADE
935c60c7ad4SBarry Smith 
936c60c7ad4SBarry Smith 
937c60c7ad4SBarry Smith    Level: advanced
938c60c7ad4SBarry Smith 
939c60c7ad4SBarry Smith .keywords: MG, set, method, multiplicative, additive, full, Kaskade, multigrid
940c60c7ad4SBarry Smith 
941c60c7ad4SBarry Smith .seealso: PCMGSetLevels()
942c60c7ad4SBarry Smith @*/
943c60c7ad4SBarry Smith PetscErrorCode  PCMGGetType(PC pc,PCMGType *type)
944c60c7ad4SBarry Smith {
945c60c7ad4SBarry Smith   PC_MG *mg = (PC_MG*)pc->data;
946c60c7ad4SBarry Smith 
947c60c7ad4SBarry Smith   PetscFunctionBegin;
948c60c7ad4SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
949c60c7ad4SBarry Smith   *type = mg->am;
9504b9ad928SBarry Smith   PetscFunctionReturn(0);
9514b9ad928SBarry Smith }
9524b9ad928SBarry Smith 
9534b9ad928SBarry Smith #undef __FUNCT__
9540d353602SBarry Smith #define __FUNCT__ "PCMGSetCycleType"
9554b9ad928SBarry Smith /*@
9560d353602SBarry Smith    PCMGSetCycleType - Sets the type cycles to use.  Use PCMGSetCycleTypeOnLevel() for more
9574b9ad928SBarry Smith    complicated cycling.
9584b9ad928SBarry Smith 
959ad4df100SBarry Smith    Logically Collective on PC
9604b9ad928SBarry Smith 
9614b9ad928SBarry Smith    Input Parameters:
962c2be2410SBarry Smith +  pc - the multigrid context
9630d353602SBarry Smith -  PC_MG_CYCLE_V or PC_MG_CYCLE_W
9644b9ad928SBarry Smith 
9654b9ad928SBarry Smith    Options Database Key:
9664446f3b4SBarry Smith .  -pc_mg_cycle_type <v,w>
9674b9ad928SBarry Smith 
9684b9ad928SBarry Smith    Level: advanced
9694b9ad928SBarry Smith 
9704b9ad928SBarry Smith .keywords: MG, set, cycles, V-cycle, W-cycle, multigrid
9714b9ad928SBarry Smith 
9720d353602SBarry Smith .seealso: PCMGSetCycleTypeOnLevel()
9734b9ad928SBarry Smith @*/
9747087cfbeSBarry Smith PetscErrorCode  PCMGSetCycleType(PC pc,PCMGCycleType n)
9754b9ad928SBarry Smith {
976f3fbd535SBarry Smith   PC_MG        *mg        = (PC_MG*)pc->data;
977f3fbd535SBarry Smith   PC_MG_Levels **mglevels = mg->levels;
97879416396SBarry Smith   PetscInt     i,levels;
9794b9ad928SBarry Smith 
9804b9ad928SBarry Smith   PetscFunctionBegin;
9810700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
982ce94432eSBarry Smith   if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
98369fbec6eSBarry Smith   PetscValidLogicalCollectiveEnum(pc,n,2);
984f3fbd535SBarry Smith   levels = mglevels[0]->levels;
9854b9ad928SBarry Smith 
9862fa5cd67SKarl Rupp   for (i=0; i<levels; i++) mglevels[i]->cycles = n;
9874b9ad928SBarry Smith   PetscFunctionReturn(0);
9884b9ad928SBarry Smith }
9894b9ad928SBarry Smith 
9904b9ad928SBarry Smith #undef __FUNCT__
9918cc2d5dfSBarry Smith #define __FUNCT__ "PCMGMultiplicativeSetCycles"
9928cc2d5dfSBarry Smith /*@
9938cc2d5dfSBarry Smith    PCMGMultiplicativeSetCycles - Sets the number of cycles to use for each preconditioner step
9948cc2d5dfSBarry Smith          of multigrid when PCMGType of PC_MG_MULTIPLICATIVE is used
9958cc2d5dfSBarry Smith 
996ad4df100SBarry Smith    Logically Collective on PC
9978cc2d5dfSBarry Smith 
9988cc2d5dfSBarry Smith    Input Parameters:
9998cc2d5dfSBarry Smith +  pc - the multigrid context
10008cc2d5dfSBarry Smith -  n - number of cycles (default is 1)
10018cc2d5dfSBarry Smith 
10028cc2d5dfSBarry Smith    Options Database Key:
1003e1bc860dSBarry Smith .  -pc_mg_multiplicative_cycles n
10048cc2d5dfSBarry Smith 
10058cc2d5dfSBarry Smith    Level: advanced
10068cc2d5dfSBarry Smith 
10078cc2d5dfSBarry Smith    Notes: This is not associated with setting a v or w cycle, that is set with PCMGSetCycleType()
10088cc2d5dfSBarry Smith 
10098cc2d5dfSBarry Smith .keywords: MG, set, cycles, V-cycle, W-cycle, multigrid
10108cc2d5dfSBarry Smith 
10118cc2d5dfSBarry Smith .seealso: PCMGSetCycleTypeOnLevel(), PCMGSetCycleType()
10128cc2d5dfSBarry Smith @*/
10137087cfbeSBarry Smith PetscErrorCode  PCMGMultiplicativeSetCycles(PC pc,PetscInt n)
10148cc2d5dfSBarry Smith {
1015f3fbd535SBarry Smith   PC_MG        *mg        = (PC_MG*)pc->data;
10168cc2d5dfSBarry Smith 
10178cc2d5dfSBarry Smith   PetscFunctionBegin;
10180700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
1019c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(pc,n,2);
10202a21e185SBarry Smith   mg->cyclesperpcapply = n;
10218cc2d5dfSBarry Smith   PetscFunctionReturn(0);
10228cc2d5dfSBarry Smith }
10238cc2d5dfSBarry Smith 
10248cc2d5dfSBarry Smith #undef __FUNCT__
1025fb15c04eSBarry Smith #define __FUNCT__ "PCMGSetGalerkin_MG"
1026fb15c04eSBarry Smith PetscErrorCode PCMGSetGalerkin_MG(PC pc,PetscBool use)
1027fb15c04eSBarry Smith {
1028fb15c04eSBarry Smith   PC_MG *mg = (PC_MG*)pc->data;
1029fb15c04eSBarry Smith 
1030fb15c04eSBarry Smith   PetscFunctionBegin;
1031fb15c04eSBarry Smith   mg->galerkin = use ? 1 : 0;
1032fb15c04eSBarry Smith   PetscFunctionReturn(0);
1033fb15c04eSBarry Smith }
1034fb15c04eSBarry Smith 
1035fb15c04eSBarry Smith #undef __FUNCT__
10369dcbbd2bSBarry Smith #define __FUNCT__ "PCMGSetGalerkin"
1037c2be2410SBarry Smith /*@
103897177400SBarry Smith    PCMGSetGalerkin - Causes the coarser grid matrices to be computed from the
103982b87d87SMatthew G. Knepley       finest grid via the Galerkin process: A_i-1 = r_i * A_i * p_i
1040c2be2410SBarry Smith 
1041ad4df100SBarry Smith    Logically Collective on PC
1042c2be2410SBarry Smith 
1043c2be2410SBarry Smith    Input Parameters:
1044c91913d3SJed Brown +  pc - the multigrid context
1045c91913d3SJed Brown -  use - PETSC_TRUE to use the Galerkin process to compute coarse-level operators
1046c2be2410SBarry Smith 
1047c2be2410SBarry Smith    Options Database Key:
10481c1aac46SBarry Smith .  -pc_mg_galerkin <true,false>
1049c2be2410SBarry Smith 
1050c2be2410SBarry Smith    Level: intermediate
1051c2be2410SBarry Smith 
10521c1aac46SBarry Smith    Notes: Some codes that use PCMG such as PCGAMG use Galerkin internally while constructing the hierarchy and thus do not
10531c1aac46SBarry Smith      use the PCMG construction of the coarser grids.
10541c1aac46SBarry Smith 
1055c2be2410SBarry Smith .keywords: MG, set, Galerkin
1056c2be2410SBarry Smith 
10573fc8bf9cSBarry Smith .seealso: PCMGGetGalerkin()
10583fc8bf9cSBarry Smith 
1059c2be2410SBarry Smith @*/
1060c91913d3SJed Brown PetscErrorCode PCMGSetGalerkin(PC pc,PetscBool use)
1061c2be2410SBarry Smith {
1062fb15c04eSBarry Smith   PetscErrorCode ierr;
1063c2be2410SBarry Smith 
1064c2be2410SBarry Smith   PetscFunctionBegin;
10650700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
10667a4c0024SBarry Smith   ierr = PetscTryMethod(pc,"PCMGSetGalerkin_C",(PC,PetscBool),(pc,use));CHKERRQ(ierr);
1067c2be2410SBarry Smith   PetscFunctionReturn(0);
1068c2be2410SBarry Smith }
1069c2be2410SBarry Smith 
1070c2be2410SBarry Smith #undef __FUNCT__
10713fc8bf9cSBarry Smith #define __FUNCT__ "PCMGGetGalerkin"
10723fc8bf9cSBarry Smith /*@
10733fc8bf9cSBarry Smith    PCMGGetGalerkin - Checks if Galerkin multigrid is being used, i.e.
107482b87d87SMatthew G. Knepley       A_i-1 = r_i * A_i * p_i
10753fc8bf9cSBarry Smith 
10763fc8bf9cSBarry Smith    Not Collective
10773fc8bf9cSBarry Smith 
10783fc8bf9cSBarry Smith    Input Parameter:
10793fc8bf9cSBarry Smith .  pc - the multigrid context
10803fc8bf9cSBarry Smith 
10813fc8bf9cSBarry Smith    Output Parameter:
1082e1bc860dSBarry Smith .  galerkin - PETSC_TRUE or PETSC_FALSE
10833fc8bf9cSBarry Smith 
10843fc8bf9cSBarry Smith    Options Database Key:
1085e1bc860dSBarry Smith .  -pc_mg_galerkin
10863fc8bf9cSBarry Smith 
10873fc8bf9cSBarry Smith    Level: intermediate
10883fc8bf9cSBarry Smith 
10893fc8bf9cSBarry Smith .keywords: MG, set, Galerkin
10903fc8bf9cSBarry Smith 
10913fc8bf9cSBarry Smith .seealso: PCMGSetGalerkin()
10923fc8bf9cSBarry Smith 
10933fc8bf9cSBarry Smith @*/
10947087cfbeSBarry Smith PetscErrorCode  PCMGGetGalerkin(PC pc,PetscBool  *galerkin)
10953fc8bf9cSBarry Smith {
1096f3fbd535SBarry Smith   PC_MG *mg = (PC_MG*)pc->data;
10973fc8bf9cSBarry Smith 
10983fc8bf9cSBarry Smith   PetscFunctionBegin;
10990700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
110013fdb3acSJose Roman   *galerkin = (PetscBool)mg->galerkin;
11013fc8bf9cSBarry Smith   PetscFunctionReturn(0);
11023fc8bf9cSBarry Smith }
11033fc8bf9cSBarry Smith 
11043fc8bf9cSBarry Smith #undef __FUNCT__
11059dcbbd2bSBarry Smith #define __FUNCT__ "PCMGSetNumberSmoothDown"
11064b9ad928SBarry Smith /*@
110797177400SBarry Smith    PCMGSetNumberSmoothDown - Sets the number of pre-smoothing steps to
110897177400SBarry Smith    use on all levels. Use PCMGGetSmootherDown() to set different
11094b9ad928SBarry Smith    pre-smoothing steps on different levels.
11104b9ad928SBarry Smith 
1111ad4df100SBarry Smith    Logically Collective on PC
11124b9ad928SBarry Smith 
11134b9ad928SBarry Smith    Input Parameters:
11144b9ad928SBarry Smith +  mg - the multigrid context
11154b9ad928SBarry Smith -  n - the number of smoothing steps
11164b9ad928SBarry Smith 
11174b9ad928SBarry Smith    Options Database Key:
11184b9ad928SBarry Smith .  -pc_mg_smoothdown <n> - Sets number of pre-smoothing steps
11194b9ad928SBarry Smith 
11204b9ad928SBarry Smith    Level: advanced
11214b9ad928SBarry Smith 
11224b9ad928SBarry Smith .keywords: MG, smooth, down, pre-smoothing, steps, multigrid
11234b9ad928SBarry Smith 
112497177400SBarry Smith .seealso: PCMGSetNumberSmoothUp()
11254b9ad928SBarry Smith @*/
11267087cfbeSBarry Smith PetscErrorCode  PCMGSetNumberSmoothDown(PC pc,PetscInt n)
11274b9ad928SBarry Smith {
1128f3fbd535SBarry Smith   PC_MG          *mg        = (PC_MG*)pc->data;
1129f3fbd535SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
11306849ba73SBarry Smith   PetscErrorCode ierr;
113179416396SBarry Smith   PetscInt       i,levels;
11324b9ad928SBarry Smith 
11334b9ad928SBarry Smith   PetscFunctionBegin;
11340700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
1135ce94432eSBarry Smith   if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
1136c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(pc,n,2);
1137f3fbd535SBarry Smith   levels = mglevels[0]->levels;
11384b9ad928SBarry Smith 
1139b05257ddSBarry Smith   for (i=1; i<levels; i++) {
11404b9ad928SBarry Smith     /* make sure smoother up and down are different */
11410298fd71SBarry Smith     ierr = PCMGGetSmootherUp(pc,i,NULL);CHKERRQ(ierr);
1142f3fbd535SBarry Smith     ierr = KSPSetTolerances(mglevels[i]->smoothd,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,n);CHKERRQ(ierr);
11432fa5cd67SKarl Rupp 
114431567311SBarry Smith     mg->default_smoothd = n;
11454b9ad928SBarry Smith   }
11464b9ad928SBarry Smith   PetscFunctionReturn(0);
11474b9ad928SBarry Smith }
11484b9ad928SBarry Smith 
11494b9ad928SBarry Smith #undef __FUNCT__
11509dcbbd2bSBarry Smith #define __FUNCT__ "PCMGSetNumberSmoothUp"
11514b9ad928SBarry Smith /*@
115297177400SBarry Smith    PCMGSetNumberSmoothUp - Sets the number of post-smoothing steps to use
115397177400SBarry Smith    on all levels. Use PCMGGetSmootherUp() to set different numbers of
11544b9ad928SBarry Smith    post-smoothing steps on different levels.
11554b9ad928SBarry Smith 
1156ad4df100SBarry Smith    Logically Collective on PC
11574b9ad928SBarry Smith 
11584b9ad928SBarry Smith    Input Parameters:
11594b9ad928SBarry Smith +  mg - the multigrid context
11604b9ad928SBarry Smith -  n - the number of smoothing steps
11614b9ad928SBarry Smith 
11624b9ad928SBarry Smith    Options Database Key:
11634b9ad928SBarry Smith .  -pc_mg_smoothup <n> - Sets number of post-smoothing steps
11644b9ad928SBarry Smith 
11654b9ad928SBarry Smith    Level: advanced
11664b9ad928SBarry Smith 
11674b9ad928SBarry Smith    Note: this does not set a value on the coarsest grid, since we assume that
1168a8c7a070SBarry Smith     there is no separate smooth up on the coarsest grid.
11694b9ad928SBarry Smith 
11704b9ad928SBarry Smith .keywords: MG, smooth, up, post-smoothing, steps, multigrid
11714b9ad928SBarry Smith 
117297177400SBarry Smith .seealso: PCMGSetNumberSmoothDown()
11734b9ad928SBarry Smith @*/
11747087cfbeSBarry Smith PetscErrorCode  PCMGSetNumberSmoothUp(PC pc,PetscInt n)
11754b9ad928SBarry Smith {
1176f3fbd535SBarry Smith   PC_MG          *mg        = (PC_MG*)pc->data;
1177f3fbd535SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
11786849ba73SBarry Smith   PetscErrorCode ierr;
117979416396SBarry Smith   PetscInt       i,levels;
11804b9ad928SBarry Smith 
11814b9ad928SBarry Smith   PetscFunctionBegin;
11820700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
1183ce94432eSBarry Smith   if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
1184c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(pc,n,2);
1185f3fbd535SBarry Smith   levels = mglevels[0]->levels;
11864b9ad928SBarry Smith 
11874b9ad928SBarry Smith   for (i=1; i<levels; i++) {
11884b9ad928SBarry Smith     /* make sure smoother up and down are different */
11890298fd71SBarry Smith     ierr = PCMGGetSmootherUp(pc,i,NULL);CHKERRQ(ierr);
1190f3fbd535SBarry Smith     ierr = KSPSetTolerances(mglevels[i]->smoothu,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,n);CHKERRQ(ierr);
11912fa5cd67SKarl Rupp 
119231567311SBarry Smith     mg->default_smoothu = n;
11934b9ad928SBarry Smith   }
11944b9ad928SBarry Smith   PetscFunctionReturn(0);
11954b9ad928SBarry Smith }
11964b9ad928SBarry Smith 
11974b9ad928SBarry Smith /* ----------------------------------------------------------------------------------------*/
11984b9ad928SBarry Smith 
11993b09bd56SBarry Smith /*MC
1200ccb205f8SBarry Smith    PCMG - Use multigrid preconditioning. This preconditioner requires you provide additional
12013b09bd56SBarry Smith     information about the coarser grid matrices and restriction/interpolation operators.
12023b09bd56SBarry Smith 
12033b09bd56SBarry Smith    Options Database Keys:
12043b09bd56SBarry Smith +  -pc_mg_levels <nlevels> - number of levels including finest
12054afba7b0SPatrick Sanan .  -pc_mg_cycle_type <v,w> -
120679416396SBarry Smith .  -pc_mg_smoothup <n> - number of smoothing steps after interpolation
12073b09bd56SBarry Smith .  -pc_mg_smoothdown <n> - number of smoothing steps before applying restriction operator
12088c1c2452SJed Brown .  -pc_mg_type <additive,multiplicative,full,kaskade> - multiplicative is the default
12093b09bd56SBarry Smith .  -pc_mg_log - log information about time spent on each level of the solver
12108cf6037eSBarry Smith .  -pc_mg_galerkin - use Galerkin process to compute coarser operators, i.e. Acoarse = R A R'
12118cf6037eSBarry Smith .  -pc_mg_multiplicative_cycles - number of cycles to use as the preconditioner (defaults to 1)
12128cf6037eSBarry Smith .  -pc_mg_dump_matlab - dumps the matrices for each level and the restriction/interpolation matrices
1213e3c5b3baSBarry Smith                         to the Socket viewer for reading from MATLAB.
12148cf6037eSBarry Smith -  -pc_mg_dump_binary - dumps the matrices for each level and the restriction/interpolation matrices
12158cf6037eSBarry Smith                         to the binary output file called binaryoutput
12163b09bd56SBarry Smith 
121724c3aa18SBarry Smith    Notes: By default this uses GMRES on the fine grid smoother so this should be used with KSPFGMRES or the smoother changed to not use GMRES
12183b09bd56SBarry Smith 
12198cf6037eSBarry Smith        When run with a single level the smoother options are used on that level NOT the coarse grid solver options
12208cf6037eSBarry Smith 
12213b09bd56SBarry Smith    Level: intermediate
12223b09bd56SBarry Smith 
12238f87f92bSBarry Smith    Concepts: multigrid/multilevel
12243b09bd56SBarry Smith 
12258cf6037eSBarry Smith .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC, PCMGType, PCEXOTIC, PCGAMG, PCML, PCHYPRE
12260d353602SBarry Smith            PCMGSetLevels(), PCMGGetLevels(), PCMGSetType(), PCMGSetCycleType(), PCMGSetNumberSmoothDown(),
122797177400SBarry Smith            PCMGSetNumberSmoothUp(), PCMGGetCoarseSolve(), PCMGSetResidual(), PCMGSetInterpolation(),
122897177400SBarry Smith            PCMGSetRestriction(), PCMGGetSmoother(), PCMGGetSmootherUp(), PCMGGetSmootherDown(),
12290d353602SBarry Smith            PCMGSetCycleTypeOnLevel(), PCMGSetRhs(), PCMGSetX(), PCMGSetR()
12303b09bd56SBarry Smith M*/
12313b09bd56SBarry Smith 
12324b9ad928SBarry Smith #undef __FUNCT__
12334b9ad928SBarry Smith #define __FUNCT__ "PCCreate_MG"
12348cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PCCreate_MG(PC pc)
12354b9ad928SBarry Smith {
1236f3fbd535SBarry Smith   PC_MG          *mg;
1237f3fbd535SBarry Smith   PetscErrorCode ierr;
1238f3fbd535SBarry Smith 
12394b9ad928SBarry Smith   PetscFunctionBegin;
1240b00a9115SJed Brown   ierr        = PetscNewLog(pc,&mg);CHKERRQ(ierr);
1241f3fbd535SBarry Smith   pc->data    = (void*)mg;
1242f3fbd535SBarry Smith   mg->nlevels = -1;
124310eca3edSLisandro Dalcin   mg->am      = PC_MG_MULTIPLICATIVE;
1244f3fbd535SBarry Smith 
124537a44384SMark Adams   pc->useAmat = PETSC_TRUE;
124637a44384SMark Adams 
12474b9ad928SBarry Smith   pc->ops->apply          = PCApply_MG;
12484b9ad928SBarry Smith   pc->ops->setup          = PCSetUp_MG;
1249a06653b4SBarry Smith   pc->ops->reset          = PCReset_MG;
12504b9ad928SBarry Smith   pc->ops->destroy        = PCDestroy_MG;
12514b9ad928SBarry Smith   pc->ops->setfromoptions = PCSetFromOptions_MG;
12524b9ad928SBarry Smith   pc->ops->view           = PCView_MG;
1253fb15c04eSBarry Smith 
1254fb15c04eSBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCMGSetGalerkin_C",PCMGSetGalerkin_MG);CHKERRQ(ierr);
12554b9ad928SBarry Smith   PetscFunctionReturn(0);
12564b9ad928SBarry Smith }
1257