xref: /petsc/src/ksp/pc/impls/mg/mg.c (revision 2bf68e3e0f2a61f71e7c65bee250bfa1c8ce0cdb)
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
166*2bf68e3eSBarry Smith            on smaller sets of processors.
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);
2360ee9a668SBarry Smith     ierr = PetscObjectIncrementTabLevel((PetscObject)mglevels[i]->smoothd,(PetscObject)pc,levels-i);CHKERRQ(ierr);
2370ee9a668SBarry Smith     ierr = KSPSetOptionsPrefix(mglevels[i]->smoothd,prefix);CHKERRQ(ierr);
2380ee9a668SBarry Smith     ierr = PetscObjectComposedDataSetInt((PetscObject) mglevels[i]->smoothd, PetscMGLevelId, mglevels[i]->level);CHKERRQ(ierr);
2390ee9a668SBarry Smith     if (i || levels == 1) {
2400ee9a668SBarry Smith       char tprefix[128];
2410ee9a668SBarry Smith 
242336babb1SJed Brown       ierr = KSPSetType(mglevels[i]->smoothd,KSPCHEBYSHEV);CHKERRQ(ierr);
2430059c7bdSJed Brown       ierr = KSPSetConvergenceTest(mglevels[i]->smoothd,KSPConvergedSkip,NULL,NULL);CHKERRQ(ierr);
244336babb1SJed Brown       ierr = KSPSetNormType(mglevels[i]->smoothd,KSP_NORM_NONE);CHKERRQ(ierr);
245336babb1SJed Brown       ierr = KSPGetPC(mglevels[i]->smoothd,&ipc);CHKERRQ(ierr);
246336babb1SJed Brown       ierr = PCSetType(ipc,PCSOR);CHKERRQ(ierr);
2470ee9a668SBarry Smith       ierr = KSPSetTolerances(mglevels[i]->smoothd,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT, mg->default_smoothd);CHKERRQ(ierr);
248f3fbd535SBarry Smith 
2490ee9a668SBarry Smith       sprintf(tprefix,"mg_levels_%d_",(int)i);
2500ee9a668SBarry Smith       ierr = KSPAppendOptionsPrefix(mglevels[i]->smoothd,tprefix);CHKERRQ(ierr);
2510ee9a668SBarry Smith     } else {
252f3fbd535SBarry Smith       ierr = KSPAppendOptionsPrefix(mglevels[0]->smoothd,"mg_coarse_");CHKERRQ(ierr);
253f3fbd535SBarry Smith 
2549dbfc187SHong Zhang       /* coarse solve is (redundant) LU by default; set shifttype NONZERO to avoid annoying zero-pivot in LU preconditioner */
255f3fbd535SBarry Smith       ierr = KSPSetType(mglevels[0]->smoothd,KSPPREONLY);CHKERRQ(ierr);
256f3fbd535SBarry Smith       ierr = KSPGetPC(mglevels[0]->smoothd,&ipc);CHKERRQ(ierr);
257f3fbd535SBarry Smith       ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
258f3fbd535SBarry Smith       if (size > 1) {
259f3fbd535SBarry Smith         ierr = PCSetType(ipc,PCREDUNDANT);CHKERRQ(ierr);
260f3fbd535SBarry Smith       } else {
261f3fbd535SBarry Smith         ierr = PCSetType(ipc,PCLU);CHKERRQ(ierr);
262f3fbd535SBarry Smith       }
263753b7fb9SBarry Smith       ierr = PCFactorSetShiftType(ipc,MAT_SHIFT_INBLOCKS);CHKERRQ(ierr);
264f3fbd535SBarry Smith     }
2653bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)mglevels[i]->smoothd);CHKERRQ(ierr);
2662fa5cd67SKarl Rupp 
267f3fbd535SBarry Smith     mglevels[i]->smoothu = mglevels[i]->smoothd;
26831567311SBarry Smith     mg->rtol             = 0.0;
26931567311SBarry Smith     mg->abstol           = 0.0;
27031567311SBarry Smith     mg->dtol             = 0.0;
27131567311SBarry Smith     mg->ttol             = 0.0;
27231567311SBarry Smith     mg->cyclesperpcapply = 1;
273f3fbd535SBarry Smith   }
274f3fbd535SBarry Smith   mg->levels = mglevels;
27510eca3edSLisandro Dalcin   ierr = PCMGSetType(pc,mgtype);CHKERRQ(ierr);
2764b9ad928SBarry Smith   PetscFunctionReturn(0);
2774b9ad928SBarry Smith }
2784b9ad928SBarry Smith 
279c07bf074SBarry Smith 
280c07bf074SBarry Smith #undef __FUNCT__
281c07bf074SBarry Smith #define __FUNCT__ "PCDestroy_MG"
282c07bf074SBarry Smith PetscErrorCode PCDestroy_MG(PC pc)
283c07bf074SBarry Smith {
284c07bf074SBarry Smith   PetscErrorCode ierr;
285a06653b4SBarry Smith   PC_MG          *mg        = (PC_MG*)pc->data;
286a06653b4SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
287a06653b4SBarry Smith   PetscInt       i,n;
288c07bf074SBarry Smith 
289c07bf074SBarry Smith   PetscFunctionBegin;
290a06653b4SBarry Smith   ierr = PCReset_MG(pc);CHKERRQ(ierr);
291a06653b4SBarry Smith   if (mglevels) {
292a06653b4SBarry Smith     n = mglevels[0]->levels;
293a06653b4SBarry Smith     for (i=0; i<n; i++) {
294a06653b4SBarry Smith       if (mglevels[i]->smoothd != mglevels[i]->smoothu) {
2956bf464f9SBarry Smith         ierr = KSPDestroy(&mglevels[i]->smoothd);CHKERRQ(ierr);
296a06653b4SBarry Smith       }
2976bf464f9SBarry Smith       ierr = KSPDestroy(&mglevels[i]->smoothu);CHKERRQ(ierr);
298a06653b4SBarry Smith       ierr = PetscFree(mglevels[i]);CHKERRQ(ierr);
299a06653b4SBarry Smith     }
300a06653b4SBarry Smith     ierr = PetscFree(mg->levels);CHKERRQ(ierr);
301a06653b4SBarry Smith   }
302c31cb41cSBarry Smith   ierr = PetscFree(pc->data);CHKERRQ(ierr);
303f3fbd535SBarry Smith   PetscFunctionReturn(0);
304f3fbd535SBarry Smith }
305f3fbd535SBarry Smith 
306f3fbd535SBarry Smith 
307f3fbd535SBarry Smith 
30809573ac7SBarry Smith extern PetscErrorCode PCMGACycle_Private(PC,PC_MG_Levels**);
30909573ac7SBarry Smith extern PetscErrorCode PCMGFCycle_Private(PC,PC_MG_Levels**);
31009573ac7SBarry Smith extern PetscErrorCode PCMGKCycle_Private(PC,PC_MG_Levels**);
311f3fbd535SBarry Smith 
312f3fbd535SBarry Smith /*
313f3fbd535SBarry Smith    PCApply_MG - Runs either an additive, multiplicative, Kaskadic
314f3fbd535SBarry Smith              or full cycle of multigrid.
315f3fbd535SBarry Smith 
316f3fbd535SBarry Smith   Note:
317f3fbd535SBarry Smith   A simple wrapper which calls PCMGMCycle(),PCMGACycle(), or PCMGFCycle().
318f3fbd535SBarry Smith */
319f3fbd535SBarry Smith #undef __FUNCT__
320f3fbd535SBarry Smith #define __FUNCT__ "PCApply_MG"
321f3fbd535SBarry Smith static PetscErrorCode PCApply_MG(PC pc,Vec b,Vec x)
322f3fbd535SBarry Smith {
323f3fbd535SBarry Smith   PC_MG          *mg        = (PC_MG*)pc->data;
324f3fbd535SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
325f3fbd535SBarry Smith   PetscErrorCode ierr;
326f3fbd535SBarry Smith   PetscInt       levels = mglevels[0]->levels,i;
327f3fbd535SBarry Smith 
328f3fbd535SBarry Smith   PetscFunctionBegin;
329a9db87a2SMatthew G Knepley   if (mg->stageApply) {ierr = PetscLogStagePush(mg->stageApply);CHKERRQ(ierr);}
330e1d8e5deSBarry Smith   /* When the DM is supplying the matrix then it will not exist until here */
331298cc208SBarry Smith   for (i=0; i<levels; i++) {
332e1d8e5deSBarry Smith     if (!mglevels[i]->A) {
33323ee1639SBarry Smith       ierr = KSPGetOperators(mglevels[i]->smoothu,&mglevels[i]->A,NULL);CHKERRQ(ierr);
334298cc208SBarry Smith       ierr = PetscObjectReference((PetscObject)mglevels[i]->A);CHKERRQ(ierr);
335e1d8e5deSBarry Smith     }
336e1d8e5deSBarry Smith   }
337e1d8e5deSBarry Smith 
338f3fbd535SBarry Smith   mglevels[levels-1]->b = b;
339f3fbd535SBarry Smith   mglevels[levels-1]->x = x;
34031567311SBarry Smith   if (mg->am == PC_MG_MULTIPLICATIVE) {
341f3fbd535SBarry Smith     ierr = VecSet(x,0.0);CHKERRQ(ierr);
34231567311SBarry Smith     for (i=0; i<mg->cyclesperpcapply; i++) {
3430298fd71SBarry Smith       ierr = PCMGMCycle_Private(pc,mglevels+levels-1,NULL);CHKERRQ(ierr);
344f3fbd535SBarry Smith     }
3452fa5cd67SKarl Rupp   } else if (mg->am == PC_MG_ADDITIVE) {
34631567311SBarry Smith     ierr = PCMGACycle_Private(pc,mglevels);CHKERRQ(ierr);
3472fa5cd67SKarl Rupp   } else if (mg->am == PC_MG_KASKADE) {
34831567311SBarry Smith     ierr = PCMGKCycle_Private(pc,mglevels);CHKERRQ(ierr);
3492fa5cd67SKarl Rupp   } else {
350f3fbd535SBarry Smith     ierr = PCMGFCycle_Private(pc,mglevels);CHKERRQ(ierr);
351f3fbd535SBarry Smith   }
352a9db87a2SMatthew G Knepley   if (mg->stageApply) {ierr = PetscLogStagePop();CHKERRQ(ierr);}
353f3fbd535SBarry Smith   PetscFunctionReturn(0);
354f3fbd535SBarry Smith }
355f3fbd535SBarry Smith 
356f3fbd535SBarry Smith 
357f3fbd535SBarry Smith #undef __FUNCT__
358f3fbd535SBarry Smith #define __FUNCT__ "PCSetFromOptions_MG"
3594416b707SBarry Smith PetscErrorCode PCSetFromOptions_MG(PetscOptionItems *PetscOptionsObject,PC pc)
360f3fbd535SBarry Smith {
361f3fbd535SBarry Smith   PetscErrorCode   ierr;
362f3fbd535SBarry Smith   PetscInt         m,levels = 1,cycles;
3632134b1e4SBarry Smith   PetscBool        flg;
364f3fbd535SBarry Smith   PC_MG            *mg        = (PC_MG*)pc->data;
3653d3eaba7SBarry Smith   PC_MG_Levels     **mglevels;
366f3fbd535SBarry Smith   PCMGType         mgtype;
367f3fbd535SBarry Smith   PCMGCycleType    mgctype;
3682134b1e4SBarry Smith   PCMGGalerkinType gtype;
369f3fbd535SBarry Smith 
370f3fbd535SBarry Smith   PetscFunctionBegin;
371e55864a3SBarry Smith   ierr = PetscOptionsHead(PetscOptionsObject,"Multigrid options");CHKERRQ(ierr);
3723aeaf226SBarry Smith   if (!mg->levels) {
373f3fbd535SBarry Smith     ierr = PetscOptionsInt("-pc_mg_levels","Number of Levels","PCMGSetLevels",levels,&levels,&flg);CHKERRQ(ierr);
374eb3f98d2SBarry Smith     if (!flg && pc->dm) {
375eb3f98d2SBarry Smith       ierr = DMGetRefineLevel(pc->dm,&levels);CHKERRQ(ierr);
376eb3f98d2SBarry Smith       levels++;
3773aeaf226SBarry Smith       mg->usedmfornumberoflevels = PETSC_TRUE;
378eb3f98d2SBarry Smith     }
3790298fd71SBarry Smith     ierr = PCMGSetLevels(pc,levels,NULL);CHKERRQ(ierr);
380f3fbd535SBarry Smith   }
3813aeaf226SBarry Smith   mglevels = mg->levels;
3823aeaf226SBarry Smith 
383f3fbd535SBarry Smith   mgctype = (PCMGCycleType) mglevels[0]->cycles;
384f3fbd535SBarry Smith   ierr    = PetscOptionsEnum("-pc_mg_cycle_type","V cycle or for W-cycle","PCMGSetCycleType",PCMGCycleTypes,(PetscEnum)mgctype,(PetscEnum*)&mgctype,&flg);CHKERRQ(ierr);
385f3fbd535SBarry Smith   if (flg) {
386f3fbd535SBarry Smith     ierr = PCMGSetCycleType(pc,mgctype);CHKERRQ(ierr);
3872fa5cd67SKarl Rupp   }
3882134b1e4SBarry Smith   gtype = mg->galerkin;
3892134b1e4SBarry Smith   ierr = PetscOptionsEnum("-pc_mg_galerkin","Use Galerkin process to compute coarser operators","PCMGSetGalerkin",PCMGGalerkinTypes,(PetscEnum)gtype,(PetscEnum*)&gtype,&flg);CHKERRQ(ierr);
3902134b1e4SBarry Smith   if (flg) {
3912134b1e4SBarry Smith     ierr = PCMGSetGalerkin(pc,gtype);CHKERRQ(ierr);
392f3fbd535SBarry Smith   }
39394ae4db5SBarry Smith   ierr = PetscOptionsInt("-pc_mg_smoothup","Number of post-smoothing steps","PCMGSetNumberSmoothUp",mg->default_smoothu,&m,&flg);CHKERRQ(ierr);
394f3fbd535SBarry Smith   if (flg) {
395f3fbd535SBarry Smith     ierr = PCMGSetNumberSmoothUp(pc,m);CHKERRQ(ierr);
396f3fbd535SBarry Smith   }
39794ae4db5SBarry Smith   ierr = PetscOptionsInt("-pc_mg_smoothdown","Number of pre-smoothing steps","PCMGSetNumberSmoothDown",mg->default_smoothd,&m,&flg);CHKERRQ(ierr);
398f3fbd535SBarry Smith   if (flg) {
399f3fbd535SBarry Smith     ierr = PCMGSetNumberSmoothDown(pc,m);CHKERRQ(ierr);
400f3fbd535SBarry Smith   }
40131567311SBarry Smith   mgtype = mg->am;
402f3fbd535SBarry Smith   ierr   = PetscOptionsEnum("-pc_mg_type","Multigrid type","PCMGSetType",PCMGTypes,(PetscEnum)mgtype,(PetscEnum*)&mgtype,&flg);CHKERRQ(ierr);
403f3fbd535SBarry Smith   if (flg) {
404f3fbd535SBarry Smith     ierr = PCMGSetType(pc,mgtype);CHKERRQ(ierr);
405f3fbd535SBarry Smith   }
40631567311SBarry Smith   if (mg->am == PC_MG_MULTIPLICATIVE) {
4075363c1e0SLisandro Dalcin     ierr = PetscOptionsInt("-pc_mg_multiplicative_cycles","Number of cycles for each preconditioner step","PCMGMultiplicativeSetCycles",mg->cyclesperpcapply,&cycles,&flg);CHKERRQ(ierr);
408f3fbd535SBarry Smith     if (flg) {
409f3fbd535SBarry Smith       ierr = PCMGMultiplicativeSetCycles(pc,cycles);CHKERRQ(ierr);
410f3fbd535SBarry Smith     }
411f3fbd535SBarry Smith   }
412f3fbd535SBarry Smith   flg  = PETSC_FALSE;
4130298fd71SBarry Smith   ierr = PetscOptionsBool("-pc_mg_log","Log times for each multigrid level","None",flg,&flg,NULL);CHKERRQ(ierr);
414f3fbd535SBarry Smith   if (flg) {
415f3fbd535SBarry Smith     PetscInt i;
416f3fbd535SBarry Smith     char     eventname[128];
417ce94432eSBarry Smith     if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
418f3fbd535SBarry Smith     levels = mglevels[0]->levels;
419f3fbd535SBarry Smith     for (i=0; i<levels; i++) {
420f3fbd535SBarry Smith       sprintf(eventname,"MGSetup Level %d",(int)i);
42163e6d426SJed Brown       ierr = PetscLogEventRegister(eventname,((PetscObject)pc)->classid,&mglevels[i]->eventsmoothsetup);CHKERRQ(ierr);
422f3fbd535SBarry Smith       sprintf(eventname,"MGSmooth Level %d",(int)i);
42363e6d426SJed Brown       ierr = PetscLogEventRegister(eventname,((PetscObject)pc)->classid,&mglevels[i]->eventsmoothsolve);CHKERRQ(ierr);
424f3fbd535SBarry Smith       if (i) {
425f3fbd535SBarry Smith         sprintf(eventname,"MGResid Level %d",(int)i);
42663e6d426SJed Brown         ierr = PetscLogEventRegister(eventname,((PetscObject)pc)->classid,&mglevels[i]->eventresidual);CHKERRQ(ierr);
427f3fbd535SBarry Smith         sprintf(eventname,"MGInterp Level %d",(int)i);
42863e6d426SJed Brown         ierr = PetscLogEventRegister(eventname,((PetscObject)pc)->classid,&mglevels[i]->eventinterprestrict);CHKERRQ(ierr);
429f3fbd535SBarry Smith       }
430f3fbd535SBarry Smith     }
431eec35531SMatthew G Knepley 
432ec60ca73SSatish Balay #if defined(PETSC_USE_LOG)
433eec35531SMatthew G Knepley     {
434eec35531SMatthew G Knepley       const char    *sname = "MG Apply";
435eec35531SMatthew G Knepley       PetscStageLog stageLog;
436eec35531SMatthew G Knepley       PetscInt      st;
437eec35531SMatthew G Knepley 
438eec35531SMatthew G Knepley       PetscFunctionBegin;
439eec35531SMatthew G Knepley       ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
440eec35531SMatthew G Knepley       for (st = 0; st < stageLog->numStages; ++st) {
441eec35531SMatthew G Knepley         PetscBool same;
442eec35531SMatthew G Knepley 
443eec35531SMatthew G Knepley         ierr = PetscStrcmp(stageLog->stageInfo[st].name, sname, &same);CHKERRQ(ierr);
4442fa5cd67SKarl Rupp         if (same) mg->stageApply = st;
445eec35531SMatthew G Knepley       }
446eec35531SMatthew G Knepley       if (!mg->stageApply) {
447eec35531SMatthew G Knepley         ierr = PetscLogStageRegister(sname, &mg->stageApply);CHKERRQ(ierr);
448eec35531SMatthew G Knepley       }
449eec35531SMatthew G Knepley     }
450ec60ca73SSatish Balay #endif
451f3fbd535SBarry Smith   }
452f3fbd535SBarry Smith   ierr = PetscOptionsTail();CHKERRQ(ierr);
453f3fbd535SBarry Smith   PetscFunctionReturn(0);
454f3fbd535SBarry Smith }
455f3fbd535SBarry Smith 
4566a6fc655SJed Brown const char *const PCMGTypes[] = {"MULTIPLICATIVE","ADDITIVE","FULL","KASKADE","PCMGType","PC_MG",0};
4576a6fc655SJed Brown const char *const PCMGCycleTypes[] = {"invalid","v","w","PCMGCycleType","PC_MG_CYCLE",0};
4582134b1e4SBarry Smith const char *const PCMGGalerkinTypes[] = {"both","pmat","mat","none","external","PCMGGalerkinType","PC_MG_GALERKIN",0};
459f3fbd535SBarry Smith 
4609804daf3SBarry Smith #include <petscdraw.h>
461f3fbd535SBarry Smith #undef __FUNCT__
462f3fbd535SBarry Smith #define __FUNCT__ "PCView_MG"
463f3fbd535SBarry Smith PetscErrorCode PCView_MG(PC pc,PetscViewer viewer)
464f3fbd535SBarry Smith {
465f3fbd535SBarry Smith   PC_MG          *mg        = (PC_MG*)pc->data;
466f3fbd535SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
467f3fbd535SBarry Smith   PetscErrorCode ierr;
468e3deeeafSJed Brown   PetscInt       levels = mglevels ? mglevels[0]->levels : 0,i;
469219b1045SBarry Smith   PetscBool      iascii,isbinary,isdraw;
470f3fbd535SBarry Smith 
471f3fbd535SBarry Smith   PetscFunctionBegin;
472251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
4735b0b0462SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
474219b1045SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
475f3fbd535SBarry Smith   if (iascii) {
476e3deeeafSJed Brown     const char *cyclename = levels ? (mglevels[0]->cycles == PC_MG_CYCLE_V ? "v" : "w") : "unknown";
477e3deeeafSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  MG: type is %s, levels=%D cycles=%s\n", PCMGTypes[mg->am],levels,cyclename);CHKERRQ(ierr);
47831567311SBarry Smith     if (mg->am == PC_MG_MULTIPLICATIVE) {
47931567311SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"    Cycles per PCApply=%d\n",mg->cyclesperpcapply);CHKERRQ(ierr);
480f3fbd535SBarry Smith     }
4812134b1e4SBarry Smith     if (mg->galerkin == PC_MG_GALERKIN_BOTH) {
482f3fbd535SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"    Using Galerkin computed coarse grid matrices\n");CHKERRQ(ierr);
4832134b1e4SBarry Smith     } else if (mg->galerkin == PC_MG_GALERKIN_PMAT) {
4842134b1e4SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"    Using Galerkin computed coarse grid matrices for pmat\n");CHKERRQ(ierr);
4852134b1e4SBarry Smith     } else if (mg->galerkin == PC_MG_GALERKIN_MAT) {
4862134b1e4SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"    Using Galerkin computed coarse grid matrices for mat\n");CHKERRQ(ierr);
4872134b1e4SBarry Smith     } else if (mg->galerkin == PC_MG_GALERKIN_EXTERNAL) {
4882134b1e4SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"    Using externally compute Galerkin coarse grid matrices\n");CHKERRQ(ierr);
4894f66f45eSBarry Smith     } else {
4904f66f45eSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"    Not using Galerkin computed coarse grid matrices\n");CHKERRQ(ierr);
491f3fbd535SBarry Smith     }
4925adeb434SBarry Smith     if (mg->view){
4935adeb434SBarry Smith       ierr = (*mg->view)(pc,viewer);CHKERRQ(ierr);
4945adeb434SBarry Smith     }
495f3fbd535SBarry Smith     for (i=0; i<levels; i++) {
496f3fbd535SBarry Smith       if (!i) {
49789cce641SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"Coarse grid solver -- level -------------------------------\n",i);CHKERRQ(ierr);
498f3fbd535SBarry Smith       } else {
49989cce641SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"Down solver (pre-smoother) on level %D -------------------------------\n",i);CHKERRQ(ierr);
500f3fbd535SBarry Smith       }
501f3fbd535SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
502f3fbd535SBarry Smith       ierr = KSPView(mglevels[i]->smoothd,viewer);CHKERRQ(ierr);
503f3fbd535SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
504f3fbd535SBarry Smith       if (i && mglevels[i]->smoothd == mglevels[i]->smoothu) {
505f3fbd535SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"Up solver (post-smoother) same as down solver (pre-smoother)\n");CHKERRQ(ierr);
506f3fbd535SBarry Smith       } else if (i) {
50789cce641SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"Up solver (post-smoother) on level %D -------------------------------\n",i);CHKERRQ(ierr);
508f3fbd535SBarry Smith         ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
509f3fbd535SBarry Smith         ierr = KSPView(mglevels[i]->smoothu,viewer);CHKERRQ(ierr);
510f3fbd535SBarry Smith         ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
511f3fbd535SBarry Smith       }
512f3fbd535SBarry Smith     }
5135b0b0462SBarry Smith   } else if (isbinary) {
5145b0b0462SBarry Smith     for (i=levels-1; i>=0; i--) {
5155b0b0462SBarry Smith       ierr = KSPView(mglevels[i]->smoothd,viewer);CHKERRQ(ierr);
5165b0b0462SBarry Smith       if (i && mglevels[i]->smoothd != mglevels[i]->smoothu) {
5175b0b0462SBarry Smith         ierr = KSPView(mglevels[i]->smoothu,viewer);CHKERRQ(ierr);
5185b0b0462SBarry Smith       }
5195b0b0462SBarry Smith     }
520219b1045SBarry Smith   } else if (isdraw) {
521219b1045SBarry Smith     PetscDraw draw;
522b4375e8dSPeter Brune     PetscReal x,w,y,bottom,th;
523219b1045SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
524219b1045SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
5250298fd71SBarry Smith     ierr   = PetscDrawStringGetSize(draw,NULL,&th);CHKERRQ(ierr);
526219b1045SBarry Smith     bottom = y - th;
527219b1045SBarry Smith     for (i=levels-1; i>=0; i--) {
528b4375e8dSPeter Brune       if (!mglevels[i]->smoothu || (mglevels[i]->smoothu == mglevels[i]->smoothd)) {
529219b1045SBarry Smith         ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
530219b1045SBarry Smith         ierr = KSPView(mglevels[i]->smoothd,viewer);CHKERRQ(ierr);
531219b1045SBarry Smith         ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
532b4375e8dSPeter Brune       } else {
533b4375e8dSPeter Brune         w    = 0.5*PetscMin(1.0-x,x);
534b4375e8dSPeter Brune         ierr = PetscDrawPushCurrentPoint(draw,x+w,bottom);CHKERRQ(ierr);
535b4375e8dSPeter Brune         ierr = KSPView(mglevels[i]->smoothd,viewer);CHKERRQ(ierr);
536b4375e8dSPeter Brune         ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
537b4375e8dSPeter Brune         ierr = PetscDrawPushCurrentPoint(draw,x-w,bottom);CHKERRQ(ierr);
538b4375e8dSPeter Brune         ierr = KSPView(mglevels[i]->smoothu,viewer);CHKERRQ(ierr);
539b4375e8dSPeter Brune         ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
540b4375e8dSPeter Brune       }
5410298fd71SBarry Smith       ierr    = PetscDrawGetBoundingBox(draw,NULL,&bottom,NULL,NULL);CHKERRQ(ierr);
5421cd381d6SBarry Smith       bottom -= th;
543219b1045SBarry Smith     }
5445b0b0462SBarry Smith   }
545f3fbd535SBarry Smith   PetscFunctionReturn(0);
546f3fbd535SBarry Smith }
547f3fbd535SBarry Smith 
548af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
549af0996ceSBarry Smith #include <petsc/private/kspimpl.h>
550cab2e9ccSBarry Smith 
551f3fbd535SBarry Smith /*
552f3fbd535SBarry Smith     Calls setup for the KSP on each level
553f3fbd535SBarry Smith */
554f3fbd535SBarry Smith #undef __FUNCT__
555f3fbd535SBarry Smith #define __FUNCT__ "PCSetUp_MG"
556f3fbd535SBarry Smith PetscErrorCode PCSetUp_MG(PC pc)
557f3fbd535SBarry Smith {
558f3fbd535SBarry Smith   PC_MG          *mg        = (PC_MG*)pc->data;
559f3fbd535SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
560f3fbd535SBarry Smith   PetscErrorCode ierr;
561f3fbd535SBarry Smith   PetscInt       i,n = mglevels[0]->levels;
56298e04984SBarry Smith   PC             cpc;
5633db492dfSBarry Smith   PetscBool      dump = PETSC_FALSE,opsset,use_amat,missinginterpolate = PETSC_FALSE;
564f3fbd535SBarry Smith   Mat            dA,dB;
565f3fbd535SBarry Smith   Vec            tvec;
566218a07d4SBarry Smith   DM             *dms;
567649052a6SBarry Smith   PetscViewer    viewer = 0;
5682134b1e4SBarry Smith   PetscBool      dAeqdB = PETSC_FALSE, needRestricts = PETSC_FALSE;
569f3fbd535SBarry Smith 
570f3fbd535SBarry Smith   PetscFunctionBegin;
57101bc414fSDmitry Karpeev   /* FIX: Move this to PCSetFromOptions_MG? */
5723aeaf226SBarry Smith   if (mg->usedmfornumberoflevels) {
5733aeaf226SBarry Smith     PetscInt levels;
5743aeaf226SBarry Smith     ierr = DMGetRefineLevel(pc->dm,&levels);CHKERRQ(ierr);
5753aeaf226SBarry Smith     levels++;
5763aeaf226SBarry Smith     if (levels > n) { /* the problem is now being solved on a finer grid */
5770298fd71SBarry Smith       ierr     = PCMGSetLevels(pc,levels,NULL);CHKERRQ(ierr);
5783aeaf226SBarry Smith       n        = levels;
5793aeaf226SBarry Smith       ierr     = PCSetFromOptions(pc);CHKERRQ(ierr); /* it is bad to call this here, but otherwise will never be called for the new hierarchy */
5803aeaf226SBarry Smith       mglevels =  mg->levels;
5813aeaf226SBarry Smith     }
5823aeaf226SBarry Smith   }
58398e04984SBarry Smith   ierr = KSPGetPC(mglevels[0]->smoothd,&cpc);CHKERRQ(ierr);
5843aeaf226SBarry Smith 
585f3fbd535SBarry Smith 
586f3fbd535SBarry Smith   /* If user did not provide fine grid operators OR operator was not updated since last global KSPSetOperators() */
587f3fbd535SBarry Smith   /* so use those from global PC */
588f3fbd535SBarry Smith   /* Is this what we always want? What if user wants to keep old one? */
5890298fd71SBarry Smith   ierr = KSPGetOperatorsSet(mglevels[n-1]->smoothd,NULL,&opsset);CHKERRQ(ierr);
59098e04984SBarry Smith   if (opsset) {
59198e04984SBarry Smith     Mat mmat;
59223ee1639SBarry Smith     ierr = KSPGetOperators(mglevels[n-1]->smoothd,NULL,&mmat);CHKERRQ(ierr);
59398e04984SBarry Smith     if (mmat == pc->pmat) opsset = PETSC_FALSE;
59498e04984SBarry Smith   }
5954a5f07a5SMark F. Adams 
5964a5f07a5SMark F. Adams   if (!opsset) {
59771b23a65SMark F. Adams     ierr = PCGetUseAmat(pc,&use_amat);CHKERRQ(ierr);
5984a5f07a5SMark F. Adams     if(use_amat){
599f3fbd535SBarry 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);
60023ee1639SBarry Smith       ierr = KSPSetOperators(mglevels[n-1]->smoothd,pc->mat,pc->pmat);CHKERRQ(ierr);
601f3fbd535SBarry Smith     }
6024a5f07a5SMark F. Adams     else {
6034a5f07a5SMark 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);
60423ee1639SBarry Smith       ierr = KSPSetOperators(mglevels[n-1]->smoothd,pc->pmat,pc->pmat);CHKERRQ(ierr);
6054a5f07a5SMark F. Adams     }
6064a5f07a5SMark F. Adams   }
607f3fbd535SBarry Smith 
6089c7ffb39SBarry Smith   for (i=n-1; i>0; i--) {
6099c7ffb39SBarry Smith     if (!(mglevels[i]->interpolate || mglevels[i]->restrct)) {
6109c7ffb39SBarry Smith       missinginterpolate = PETSC_TRUE;
6119c7ffb39SBarry Smith       continue;
6129c7ffb39SBarry Smith     }
6139c7ffb39SBarry Smith   }
6142134b1e4SBarry Smith 
6152134b1e4SBarry Smith   ierr = KSPGetOperators(mglevels[n-1]->smoothd,&dA,&dB);CHKERRQ(ierr);
6162134b1e4SBarry Smith   if (dA == dB) dAeqdB = PETSC_TRUE;
6172134b1e4SBarry Smith   if ((mg->galerkin == PC_MG_GALERKIN_NONE) || (((mg->galerkin == PC_MG_GALERKIN_PMAT) || (mg->galerkin == PC_MG_GALERKIN_MAT)) && !dAeqdB)) {
6182134b1e4SBarry Smith     needRestricts = PETSC_TRUE;  /* user must compute either mat, pmat, or both so must restrict x to coarser levels */
6192134b1e4SBarry Smith   }
6202134b1e4SBarry Smith 
6212134b1e4SBarry Smith 
6229c7ffb39SBarry Smith   /*
6239c7ffb39SBarry Smith    Skipping if user has provided all interpolation/restriction needed (since DM might not be able to produce them (when coming from SNES/TS)
6249c7ffb39SBarry Smith    Skipping for galerkin==2 (externally managed hierarchy such as ML and GAMG). Cleaner logic here would be great. Wrap ML/GAMG as DMs?
6259c7ffb39SBarry Smith   */
6262134b1e4SBarry Smith   if (missinginterpolate && pc->dm && mg->galerkin != PC_MG_GALERKIN_EXTERNAL && !pc->setupcalled) {
6272d2b81a6SBarry Smith     /* construct the interpolation from the DMs */
6282e499ae9SBarry Smith     Mat p;
62973250ac0SBarry Smith     Vec rscale;
630785e854fSJed Brown     ierr     = PetscMalloc1(n,&dms);CHKERRQ(ierr);
631218a07d4SBarry Smith     dms[n-1] = pc->dm;
632ef1267afSMatthew G. Knepley     /* Separately create them so we do not get DMKSP interference between levels */
633ef1267afSMatthew G. Knepley     for (i=n-2; i>-1; i--) {ierr = DMCoarsen(dms[i+1],MPI_COMM_NULL,&dms[i]);CHKERRQ(ierr);}
634218a07d4SBarry Smith     for (i=n-2; i>-1; i--) {
635942e3340SBarry Smith       DMKSP     kdm;
6363ad4599aSBarry Smith       PetscBool dmhasrestrict;
6373c0c59f3SBarry Smith       ierr = KSPSetDM(mglevels[i]->smoothd,dms[i]);CHKERRQ(ierr);
6382134b1e4SBarry Smith       if (!needRestricts) {ierr = KSPSetDMActive(mglevels[i]->smoothd,PETSC_FALSE);CHKERRQ(ierr);}
639942e3340SBarry Smith       ierr = DMGetDMKSPWrite(dms[i],&kdm);CHKERRQ(ierr);
640d1a3e677SJed Brown       /* Ugly hack so that the next KSPSetUp() will use the RHS that we set. A better fix is to change dmActive to take
641d1a3e677SJed Brown        * a bitwise OR of computing the matrix, RHS, and initial iterate. */
6420298fd71SBarry Smith       kdm->ops->computerhs = NULL;
6430298fd71SBarry Smith       kdm->rhsctx          = NULL;
64424c3aa18SBarry Smith       if (!mglevels[i+1]->interpolate) {
645e727c939SJed Brown         ierr = DMCreateInterpolation(dms[i],dms[i+1],&p,&rscale);CHKERRQ(ierr);
6462d2b81a6SBarry Smith         ierr = PCMGSetInterpolation(pc,i+1,p);CHKERRQ(ierr);
64700ac8be1SBarry Smith         if (rscale) {ierr = PCMGSetRScale(pc,i+1,rscale);CHKERRQ(ierr);}
64873250ac0SBarry Smith         ierr = VecDestroy(&rscale);CHKERRQ(ierr);
6496bf464f9SBarry Smith         ierr = MatDestroy(&p);CHKERRQ(ierr);
650218a07d4SBarry Smith       }
6513ad4599aSBarry Smith       ierr = DMHasCreateRestriction(dms[i],&dmhasrestrict);CHKERRQ(ierr);
6523ad4599aSBarry Smith       if (dmhasrestrict && !mglevels[i+1]->restrct){
6533ad4599aSBarry Smith         ierr = DMCreateRestriction(dms[i],dms[i+1],&p);CHKERRQ(ierr);
6543ad4599aSBarry Smith         ierr = PCMGSetRestriction(pc,i+1,p);CHKERRQ(ierr);
6553ad4599aSBarry Smith         ierr = MatDestroy(&p);CHKERRQ(ierr);
6563ad4599aSBarry Smith       }
65724c3aa18SBarry Smith     }
6582d2b81a6SBarry Smith 
659ef1267afSMatthew G. Knepley     for (i=n-2; i>-1; i--) {ierr = DMDestroy(&dms[i]);CHKERRQ(ierr);}
6602d2b81a6SBarry Smith     ierr = PetscFree(dms);CHKERRQ(ierr);
661ce4cda84SJed Brown   }
662cab2e9ccSBarry Smith 
663ce4cda84SJed Brown   if (pc->dm && !pc->setupcalled) {
6642134b1e4SBarry Smith     /* finest smoother also gets DM but it is not active, independent of whether galerkin==PC_MG_GALERKIN_EXTERNAL */
665cab2e9ccSBarry Smith     ierr = KSPSetDM(mglevels[n-1]->smoothd,pc->dm);CHKERRQ(ierr);
666cab2e9ccSBarry Smith     ierr = KSPSetDMActive(mglevels[n-1]->smoothd,PETSC_FALSE);CHKERRQ(ierr);
667218a07d4SBarry Smith   }
668218a07d4SBarry Smith 
6692134b1e4SBarry Smith   if (mg->galerkin < PC_MG_GALERKIN_NONE) {
6702134b1e4SBarry Smith     Mat       A,B;
6712134b1e4SBarry Smith     PetscBool doA = PETSC_FALSE,doB = PETSC_FALSE;
6722134b1e4SBarry Smith     MatReuse  reuse = MAT_INITIAL_MATRIX;
6732134b1e4SBarry Smith 
6742134b1e4SBarry Smith     if ((mg->galerkin == PC_MG_GALERKIN_PMAT) || (mg->galerkin == PC_MG_GALERKIN_BOTH)) doB = PETSC_TRUE;
6752134b1e4SBarry Smith     if ((mg->galerkin == PC_MG_GALERKIN_MAT) || ((mg->galerkin == PC_MG_GALERKIN_BOTH) && (dA != dB))) doA = PETSC_TRUE;
6762134b1e4SBarry Smith     if (pc->setupcalled) reuse = MAT_REUSE_MATRIX;
677f3fbd535SBarry Smith     for (i=n-2; i>-1; i--) {
678b9367914SBarry 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");
679b9367914SBarry Smith       if (!mglevels[i+1]->interpolate) {
680b9367914SBarry Smith         ierr = PCMGSetInterpolation(pc,i+1,mglevels[i+1]->restrct);CHKERRQ(ierr);
681b9367914SBarry Smith       }
682b9367914SBarry Smith       if (!mglevels[i+1]->restrct) {
683b9367914SBarry Smith         ierr = PCMGSetRestriction(pc,i+1,mglevels[i+1]->interpolate);CHKERRQ(ierr);
684b9367914SBarry Smith       }
6852134b1e4SBarry Smith       if (reuse == MAT_REUSE_MATRIX) {
6862134b1e4SBarry Smith         ierr = KSPGetOperators(mglevels[i]->smoothd,&A,&B);CHKERRQ(ierr);
6872134b1e4SBarry Smith       }
6882134b1e4SBarry Smith       if (doA) {
6892df6c5c3SPatrick Farrell         ierr = MatGalerkin(mglevels[i+1]->restrct,dA,mglevels[i+1]->interpolate,reuse,1.0,&A);CHKERRQ(ierr);
6902134b1e4SBarry Smith       }
6912134b1e4SBarry Smith       if (doB) {
6922df6c5c3SPatrick Farrell         ierr = MatGalerkin(mglevels[i+1]->restrct,dB,mglevels[i+1]->interpolate,reuse,1.0,&B);CHKERRQ(ierr);
693b9367914SBarry Smith       }
6942134b1e4SBarry Smith       /* the management of the PetscObjectReference() and PetscObjecDereference() below is rather delicate */
6952134b1e4SBarry Smith       if (!doA && dAeqdB) {
6962134b1e4SBarry Smith         if (reuse == MAT_INITIAL_MATRIX) {ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);}
6972134b1e4SBarry Smith         A = B;
6982134b1e4SBarry Smith       } else if (!doA && reuse == MAT_INITIAL_MATRIX ) {
6992134b1e4SBarry Smith         ierr = KSPGetOperators(mglevels[i]->smoothd,&A,NULL);CHKERRQ(ierr);
7002134b1e4SBarry Smith         ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
701b9367914SBarry Smith       }
7022134b1e4SBarry Smith       if (!doB && dAeqdB) {
7032134b1e4SBarry Smith         if (reuse == MAT_INITIAL_MATRIX) {ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);}
7042134b1e4SBarry Smith         B = A;
7052134b1e4SBarry Smith       } else if (!doB && reuse == MAT_INITIAL_MATRIX) {
70623ee1639SBarry Smith         ierr = KSPGetOperators(mglevels[i]->smoothd,NULL,&B);CHKERRQ(ierr);
7072134b1e4SBarry Smith         ierr = PetscObjectReference((PetscObject)B);CHKERRQ(ierr);
7087d537d92SJed Brown       }
7092134b1e4SBarry Smith       if (reuse == MAT_INITIAL_MATRIX) {
7102134b1e4SBarry Smith         ierr = KSPSetOperators(mglevels[i]->smoothd,A,B);CHKERRQ(ierr);
7112134b1e4SBarry Smith         ierr = PetscObjectDereference((PetscObject)A);CHKERRQ(ierr);
7122134b1e4SBarry Smith         ierr = PetscObjectDereference((PetscObject)B);CHKERRQ(ierr);
7132134b1e4SBarry Smith       }
7142134b1e4SBarry Smith       dA = A;
715f3fbd535SBarry Smith       dB = B;
716f3fbd535SBarry Smith     }
717f3fbd535SBarry Smith   }
7182134b1e4SBarry Smith   if (needRestricts && pc->dm && pc->dm->x) {
719cab2e9ccSBarry Smith     /* need to restrict Jacobian location to coarser meshes for evaluation */
720cab2e9ccSBarry Smith     for (i=n-2; i>-1; i--) {
721c88c5224SJed Brown       Mat R;
722c88c5224SJed Brown       Vec rscale;
723cab2e9ccSBarry Smith       if (!mglevels[i]->smoothd->dm->x) {
724cab2e9ccSBarry Smith         Vec *vecs;
7252a7a6963SBarry Smith         ierr = KSPCreateVecs(mglevels[i]->smoothd,1,&vecs,0,NULL);CHKERRQ(ierr);
726cab2e9ccSBarry Smith         mglevels[i]->smoothd->dm->x = vecs[0];
727cab2e9ccSBarry Smith         ierr = PetscFree(vecs);CHKERRQ(ierr);
728cab2e9ccSBarry Smith       }
729c88c5224SJed Brown       ierr = PCMGGetRestriction(pc,i+1,&R);CHKERRQ(ierr);
730c88c5224SJed Brown       ierr = PCMGGetRScale(pc,i+1,&rscale);CHKERRQ(ierr);
731c88c5224SJed Brown       ierr = MatRestrict(R,mglevels[i+1]->smoothd->dm->x,mglevels[i]->smoothd->dm->x);CHKERRQ(ierr);
732c88c5224SJed Brown       ierr = VecPointwiseMult(mglevels[i]->smoothd->dm->x,mglevels[i]->smoothd->dm->x,rscale);CHKERRQ(ierr);
733cab2e9ccSBarry Smith     }
734f3fbd535SBarry Smith   }
7352134b1e4SBarry Smith   if (needRestricts && pc->dm) {
736caa4e7f2SJed Brown     for (i=n-2; i>=0; i--) {
737ccceb50cSJed Brown       DM  dmfine,dmcoarse;
738caa4e7f2SJed Brown       Mat Restrict,Inject;
739caa4e7f2SJed Brown       Vec rscale;
740ccceb50cSJed Brown       ierr   = KSPGetDM(mglevels[i+1]->smoothd,&dmfine);CHKERRQ(ierr);
741ccceb50cSJed Brown       ierr   = KSPGetDM(mglevels[i]->smoothd,&dmcoarse);CHKERRQ(ierr);
742caa4e7f2SJed Brown       ierr   = PCMGGetRestriction(pc,i+1,&Restrict);CHKERRQ(ierr);
743caa4e7f2SJed Brown       ierr   = PCMGGetRScale(pc,i+1,&rscale);CHKERRQ(ierr);
7440298fd71SBarry Smith       Inject = NULL;      /* Callback should create it if it needs Injection */
745caa4e7f2SJed Brown       ierr   = DMRestrict(dmfine,Restrict,rscale,Inject,dmcoarse);CHKERRQ(ierr);
746caa4e7f2SJed Brown     }
747caa4e7f2SJed Brown   }
748f3fbd535SBarry Smith 
749f3fbd535SBarry Smith   if (!pc->setupcalled) {
750f3fbd535SBarry Smith     for (i=0; i<n; i++) {
751f3fbd535SBarry Smith       ierr = KSPSetFromOptions(mglevels[i]->smoothd);CHKERRQ(ierr);
752f3fbd535SBarry Smith     }
753f3fbd535SBarry Smith     for (i=1; i<n; i++) {
754f3fbd535SBarry Smith       if (mglevels[i]->smoothu && (mglevels[i]->smoothu != mglevels[i]->smoothd)) {
755f3fbd535SBarry Smith         ierr = KSPSetFromOptions(mglevels[i]->smoothu);CHKERRQ(ierr);
756f3fbd535SBarry Smith       }
757f3fbd535SBarry Smith     }
7583ad4599aSBarry Smith     /* insure that if either interpolation or restriction is set the other other one is set */
759f3fbd535SBarry Smith     for (i=1; i<n; i++) {
7603ad4599aSBarry Smith       ierr = PCMGGetInterpolation(pc,i,NULL);CHKERRQ(ierr);
7613ad4599aSBarry Smith       ierr = PCMGGetRestriction(pc,i,NULL);CHKERRQ(ierr);
762f3fbd535SBarry Smith     }
763f3fbd535SBarry Smith     for (i=0; i<n-1; i++) {
764f3fbd535SBarry Smith       if (!mglevels[i]->b) {
765f3fbd535SBarry Smith         Vec *vec;
7662a7a6963SBarry Smith         ierr = KSPCreateVecs(mglevels[i]->smoothd,1,&vec,0,NULL);CHKERRQ(ierr);
767f3fbd535SBarry Smith         ierr = PCMGSetRhs(pc,i,*vec);CHKERRQ(ierr);
7686bf464f9SBarry Smith         ierr = VecDestroy(vec);CHKERRQ(ierr);
769f3fbd535SBarry Smith         ierr = PetscFree(vec);CHKERRQ(ierr);
770f3fbd535SBarry Smith       }
771f3fbd535SBarry Smith       if (!mglevels[i]->r && i) {
772f3fbd535SBarry Smith         ierr = VecDuplicate(mglevels[i]->b,&tvec);CHKERRQ(ierr);
773f3fbd535SBarry Smith         ierr = PCMGSetR(pc,i,tvec);CHKERRQ(ierr);
7746bf464f9SBarry Smith         ierr = VecDestroy(&tvec);CHKERRQ(ierr);
775f3fbd535SBarry Smith       }
776f3fbd535SBarry Smith       if (!mglevels[i]->x) {
777f3fbd535SBarry Smith         ierr = VecDuplicate(mglevels[i]->b,&tvec);CHKERRQ(ierr);
778f3fbd535SBarry Smith         ierr = PCMGSetX(pc,i,tvec);CHKERRQ(ierr);
7796bf464f9SBarry Smith         ierr = VecDestroy(&tvec);CHKERRQ(ierr);
780f3fbd535SBarry Smith       }
781f3fbd535SBarry Smith     }
782f3fbd535SBarry Smith     if (n != 1 && !mglevels[n-1]->r) {
783f3fbd535SBarry Smith       /* PCMGSetR() on the finest level if user did not supply it */
784f3fbd535SBarry Smith       Vec *vec;
7852a7a6963SBarry Smith       ierr = KSPCreateVecs(mglevels[n-1]->smoothd,1,&vec,0,NULL);CHKERRQ(ierr);
786f3fbd535SBarry Smith       ierr = PCMGSetR(pc,n-1,*vec);CHKERRQ(ierr);
7876bf464f9SBarry Smith       ierr = VecDestroy(vec);CHKERRQ(ierr);
788f3fbd535SBarry Smith       ierr = PetscFree(vec);CHKERRQ(ierr);
789f3fbd535SBarry Smith     }
790f3fbd535SBarry Smith   }
791f3fbd535SBarry Smith 
79298e04984SBarry Smith   if (pc->dm) {
79398e04984SBarry Smith     /* need to tell all the coarser levels to rebuild the matrix using the DM for that level */
79498e04984SBarry Smith     for (i=0; i<n-1; i++) {
79598e04984SBarry Smith       if (mglevels[i]->smoothd->setupstage != KSP_SETUP_NEW) mglevels[i]->smoothd->setupstage = KSP_SETUP_NEWMATRIX;
79698e04984SBarry Smith     }
79798e04984SBarry Smith   }
798f3fbd535SBarry Smith 
799f3fbd535SBarry Smith   for (i=1; i<n; i++) {
8002a21e185SBarry Smith     if (mglevels[i]->smoothu == mglevels[i]->smoothd || mg->am == PC_MG_FULL || mg->am == PC_MG_KASKADE || mg->cyclesperpcapply > 1){
801f3fbd535SBarry Smith       /* if doing only down then initial guess is zero */
802f3fbd535SBarry Smith       ierr = KSPSetInitialGuessNonzero(mglevels[i]->smoothd,PETSC_TRUE);CHKERRQ(ierr);
803f3fbd535SBarry Smith     }
80463e6d426SJed Brown     if (mglevels[i]->eventsmoothsetup) {ierr = PetscLogEventBegin(mglevels[i]->eventsmoothsetup,0,0,0,0);CHKERRQ(ierr);}
805f3fbd535SBarry Smith     ierr = KSPSetUp(mglevels[i]->smoothd);CHKERRQ(ierr);
806899639b0SHong Zhang     if (mglevels[i]->smoothd->reason == KSP_DIVERGED_PCSETUP_FAILED) {
807899639b0SHong Zhang       pc->failedreason = PC_SUBPC_ERROR;
808899639b0SHong Zhang     }
80963e6d426SJed Brown     if (mglevels[i]->eventsmoothsetup) {ierr = PetscLogEventEnd(mglevels[i]->eventsmoothsetup,0,0,0,0);CHKERRQ(ierr);}
810d42688cbSBarry Smith     if (!mglevels[i]->residual) {
811d42688cbSBarry Smith       Mat mat;
81223ee1639SBarry Smith       ierr = KSPGetOperators(mglevels[i]->smoothd,NULL,&mat);CHKERRQ(ierr);
81354b2cd4bSJed Brown       ierr = PCMGSetResidual(pc,i,PCMGResidualDefault,mat);CHKERRQ(ierr);
814d42688cbSBarry Smith     }
815f3fbd535SBarry Smith   }
816f3fbd535SBarry Smith   for (i=1; i<n; i++) {
817f3fbd535SBarry Smith     if (mglevels[i]->smoothu && mglevels[i]->smoothu != mglevels[i]->smoothd) {
818f3fbd535SBarry Smith       Mat downmat,downpmat;
819f3fbd535SBarry Smith 
820f3fbd535SBarry Smith       /* check if operators have been set for up, if not use down operators to set them */
8210298fd71SBarry Smith       ierr = KSPGetOperatorsSet(mglevels[i]->smoothu,&opsset,NULL);CHKERRQ(ierr);
822f3fbd535SBarry Smith       if (!opsset) {
82323ee1639SBarry Smith         ierr = KSPGetOperators(mglevels[i]->smoothd,&downmat,&downpmat);CHKERRQ(ierr);
82423ee1639SBarry Smith         ierr = KSPSetOperators(mglevels[i]->smoothu,downmat,downpmat);CHKERRQ(ierr);
825f3fbd535SBarry Smith       }
826f3fbd535SBarry Smith 
827f3fbd535SBarry Smith       ierr = KSPSetInitialGuessNonzero(mglevels[i]->smoothu,PETSC_TRUE);CHKERRQ(ierr);
82863e6d426SJed Brown       if (mglevels[i]->eventsmoothsetup) {ierr = PetscLogEventBegin(mglevels[i]->eventsmoothsetup,0,0,0,0);CHKERRQ(ierr);}
829f3fbd535SBarry Smith       ierr = KSPSetUp(mglevels[i]->smoothu);CHKERRQ(ierr);
830899639b0SHong Zhang       if (mglevels[i]->smoothu->reason == KSP_DIVERGED_PCSETUP_FAILED) {
831899639b0SHong Zhang         pc->failedreason = PC_SUBPC_ERROR;
832899639b0SHong Zhang       }
83363e6d426SJed Brown       if (mglevels[i]->eventsmoothsetup) {ierr = PetscLogEventEnd(mglevels[i]->eventsmoothsetup,0,0,0,0);CHKERRQ(ierr);}
834f3fbd535SBarry Smith     }
835f3fbd535SBarry Smith   }
836f3fbd535SBarry Smith 
83763e6d426SJed Brown   if (mglevels[0]->eventsmoothsetup) {ierr = PetscLogEventBegin(mglevels[0]->eventsmoothsetup,0,0,0,0);CHKERRQ(ierr);}
838f3fbd535SBarry Smith   ierr = KSPSetUp(mglevels[0]->smoothd);CHKERRQ(ierr);
839899639b0SHong Zhang   if (mglevels[0]->smoothd->reason == KSP_DIVERGED_PCSETUP_FAILED) {
840899639b0SHong Zhang     pc->failedreason = PC_SUBPC_ERROR;
841899639b0SHong Zhang   }
84263e6d426SJed Brown   if (mglevels[0]->eventsmoothsetup) {ierr = PetscLogEventEnd(mglevels[0]->eventsmoothsetup,0,0,0,0);CHKERRQ(ierr);}
843f3fbd535SBarry Smith 
844f3fbd535SBarry Smith   /*
845f3fbd535SBarry Smith      Dump the interpolation/restriction matrices plus the
846e3c5b3baSBarry Smith    Jacobian/stiffness on each level. This allows MATLAB users to
847f3fbd535SBarry Smith    easily check if the Galerkin condition A_c = R A_f R^T is satisfied.
848f3fbd535SBarry Smith 
849f3fbd535SBarry Smith    Only support one or the other at the same time.
850f3fbd535SBarry Smith   */
851f3fbd535SBarry Smith #if defined(PETSC_USE_SOCKET_VIEWER)
852c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)pc)->options,((PetscObject)pc)->prefix,"-pc_mg_dump_matlab",&dump,NULL);CHKERRQ(ierr);
853ce94432eSBarry Smith   if (dump) viewer = PETSC_VIEWER_SOCKET_(PetscObjectComm((PetscObject)pc));
854f3fbd535SBarry Smith   dump = PETSC_FALSE;
855f3fbd535SBarry Smith #endif
856c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)pc)->options,((PetscObject)pc)->prefix,"-pc_mg_dump_binary",&dump,NULL);CHKERRQ(ierr);
857ce94432eSBarry Smith   if (dump) viewer = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)pc));
858f3fbd535SBarry Smith 
859f3fbd535SBarry Smith   if (viewer) {
860f3fbd535SBarry Smith     for (i=1; i<n; i++) {
861f3fbd535SBarry Smith       ierr = MatView(mglevels[i]->restrct,viewer);CHKERRQ(ierr);
862f3fbd535SBarry Smith     }
863f3fbd535SBarry Smith     for (i=0; i<n; i++) {
864f3fbd535SBarry Smith       ierr = KSPGetPC(mglevels[i]->smoothd,&pc);CHKERRQ(ierr);
865f3fbd535SBarry Smith       ierr = MatView(pc->mat,viewer);CHKERRQ(ierr);
866f3fbd535SBarry Smith     }
867f3fbd535SBarry Smith   }
868f3fbd535SBarry Smith   PetscFunctionReturn(0);
869f3fbd535SBarry Smith }
870f3fbd535SBarry Smith 
871f3fbd535SBarry Smith /* -------------------------------------------------------------------------------------*/
872f3fbd535SBarry Smith 
873f3fbd535SBarry Smith #undef __FUNCT__
8749dcbbd2bSBarry Smith #define __FUNCT__ "PCMGGetLevels"
8754b9ad928SBarry Smith /*@
87697177400SBarry Smith    PCMGGetLevels - Gets the number of levels to use with MG.
8774b9ad928SBarry Smith 
8784b9ad928SBarry Smith    Not Collective
8794b9ad928SBarry Smith 
8804b9ad928SBarry Smith    Input Parameter:
8814b9ad928SBarry Smith .  pc - the preconditioner context
8824b9ad928SBarry Smith 
8834b9ad928SBarry Smith    Output parameter:
8844b9ad928SBarry Smith .  levels - the number of levels
8854b9ad928SBarry Smith 
8864b9ad928SBarry Smith    Level: advanced
8874b9ad928SBarry Smith 
8884b9ad928SBarry Smith .keywords: MG, get, levels, multigrid
8894b9ad928SBarry Smith 
89097177400SBarry Smith .seealso: PCMGSetLevels()
8914b9ad928SBarry Smith @*/
8927087cfbeSBarry Smith PetscErrorCode  PCMGGetLevels(PC pc,PetscInt *levels)
8934b9ad928SBarry Smith {
894f3fbd535SBarry Smith   PC_MG *mg = (PC_MG*)pc->data;
8954b9ad928SBarry Smith 
8964b9ad928SBarry Smith   PetscFunctionBegin;
8970700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
8984482741eSBarry Smith   PetscValidIntPointer(levels,2);
899f3fbd535SBarry Smith   *levels = mg->nlevels;
9004b9ad928SBarry Smith   PetscFunctionReturn(0);
9014b9ad928SBarry Smith }
9024b9ad928SBarry Smith 
9034b9ad928SBarry Smith #undef __FUNCT__
9049dcbbd2bSBarry Smith #define __FUNCT__ "PCMGSetType"
9054b9ad928SBarry Smith /*@
90697177400SBarry Smith    PCMGSetType - Determines the form of multigrid to use:
9074b9ad928SBarry Smith    multiplicative, additive, full, or the Kaskade algorithm.
9084b9ad928SBarry Smith 
909ad4df100SBarry Smith    Logically Collective on PC
9104b9ad928SBarry Smith 
9114b9ad928SBarry Smith    Input Parameters:
9124b9ad928SBarry Smith +  pc - the preconditioner context
9139dcbbd2bSBarry Smith -  form - multigrid form, one of PC_MG_MULTIPLICATIVE, PC_MG_ADDITIVE,
9149dcbbd2bSBarry Smith    PC_MG_FULL, PC_MG_KASKADE
9154b9ad928SBarry Smith 
9164b9ad928SBarry Smith    Options Database Key:
9174b9ad928SBarry Smith .  -pc_mg_type <form> - Sets <form>, one of multiplicative,
9184b9ad928SBarry Smith    additive, full, kaskade
9194b9ad928SBarry Smith 
9204b9ad928SBarry Smith    Level: advanced
9214b9ad928SBarry Smith 
9224b9ad928SBarry Smith .keywords: MG, set, method, multiplicative, additive, full, Kaskade, multigrid
9234b9ad928SBarry Smith 
92497177400SBarry Smith .seealso: PCMGSetLevels()
9254b9ad928SBarry Smith @*/
9267087cfbeSBarry Smith PetscErrorCode  PCMGSetType(PC pc,PCMGType form)
9274b9ad928SBarry Smith {
928f3fbd535SBarry Smith   PC_MG *mg = (PC_MG*)pc->data;
9294b9ad928SBarry Smith 
9304b9ad928SBarry Smith   PetscFunctionBegin;
9310700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
932c5eb9154SBarry Smith   PetscValidLogicalCollectiveEnum(pc,form,2);
93331567311SBarry Smith   mg->am = form;
9349dcbbd2bSBarry Smith   if (form == PC_MG_MULTIPLICATIVE) pc->ops->applyrichardson = PCApplyRichardson_MG;
935c60c7ad4SBarry Smith   else pc->ops->applyrichardson = NULL;
936c60c7ad4SBarry Smith   PetscFunctionReturn(0);
937c60c7ad4SBarry Smith }
938c60c7ad4SBarry Smith 
939f0af28e8SLisandro Dalcin #undef __FUNCT__
940f0af28e8SLisandro Dalcin #define __FUNCT__ "PCMGGetType"
941c60c7ad4SBarry Smith /*@
942c60c7ad4SBarry Smith    PCMGGetType - Determines the form of multigrid to use:
943c60c7ad4SBarry Smith    multiplicative, additive, full, or the Kaskade algorithm.
944c60c7ad4SBarry Smith 
945c60c7ad4SBarry Smith    Logically Collective on PC
946c60c7ad4SBarry Smith 
947c60c7ad4SBarry Smith    Input Parameter:
948c60c7ad4SBarry Smith .  pc - the preconditioner context
949c60c7ad4SBarry Smith 
950c60c7ad4SBarry Smith    Output Parameter:
951c60c7ad4SBarry Smith .  type - one of PC_MG_MULTIPLICATIVE, PC_MG_ADDITIVE,PC_MG_FULL, PC_MG_KASKADE
952c60c7ad4SBarry Smith 
953c60c7ad4SBarry Smith 
954c60c7ad4SBarry Smith    Level: advanced
955c60c7ad4SBarry Smith 
956c60c7ad4SBarry Smith .keywords: MG, set, method, multiplicative, additive, full, Kaskade, multigrid
957c60c7ad4SBarry Smith 
958c60c7ad4SBarry Smith .seealso: PCMGSetLevels()
959c60c7ad4SBarry Smith @*/
960c60c7ad4SBarry Smith PetscErrorCode  PCMGGetType(PC pc,PCMGType *type)
961c60c7ad4SBarry Smith {
962c60c7ad4SBarry Smith   PC_MG *mg = (PC_MG*)pc->data;
963c60c7ad4SBarry Smith 
964c60c7ad4SBarry Smith   PetscFunctionBegin;
965c60c7ad4SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
966c60c7ad4SBarry Smith   *type = mg->am;
9674b9ad928SBarry Smith   PetscFunctionReturn(0);
9684b9ad928SBarry Smith }
9694b9ad928SBarry Smith 
9704b9ad928SBarry Smith #undef __FUNCT__
9710d353602SBarry Smith #define __FUNCT__ "PCMGSetCycleType"
9724b9ad928SBarry Smith /*@
9730d353602SBarry Smith    PCMGSetCycleType - Sets the type cycles to use.  Use PCMGSetCycleTypeOnLevel() for more
9744b9ad928SBarry Smith    complicated cycling.
9754b9ad928SBarry Smith 
976ad4df100SBarry Smith    Logically Collective on PC
9774b9ad928SBarry Smith 
9784b9ad928SBarry Smith    Input Parameters:
979c2be2410SBarry Smith +  pc - the multigrid context
9800d353602SBarry Smith -  PC_MG_CYCLE_V or PC_MG_CYCLE_W
9814b9ad928SBarry Smith 
9824b9ad928SBarry Smith    Options Database Key:
9834446f3b4SBarry Smith .  -pc_mg_cycle_type <v,w>
9844b9ad928SBarry Smith 
9854b9ad928SBarry Smith    Level: advanced
9864b9ad928SBarry Smith 
9874b9ad928SBarry Smith .keywords: MG, set, cycles, V-cycle, W-cycle, multigrid
9884b9ad928SBarry Smith 
9890d353602SBarry Smith .seealso: PCMGSetCycleTypeOnLevel()
9904b9ad928SBarry Smith @*/
9917087cfbeSBarry Smith PetscErrorCode  PCMGSetCycleType(PC pc,PCMGCycleType n)
9924b9ad928SBarry Smith {
993f3fbd535SBarry Smith   PC_MG        *mg        = (PC_MG*)pc->data;
994f3fbd535SBarry Smith   PC_MG_Levels **mglevels = mg->levels;
99579416396SBarry Smith   PetscInt     i,levels;
9964b9ad928SBarry Smith 
9974b9ad928SBarry Smith   PetscFunctionBegin;
9980700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
999ce94432eSBarry Smith   if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
100069fbec6eSBarry Smith   PetscValidLogicalCollectiveEnum(pc,n,2);
1001f3fbd535SBarry Smith   levels = mglevels[0]->levels;
10024b9ad928SBarry Smith 
10032fa5cd67SKarl Rupp   for (i=0; i<levels; i++) mglevels[i]->cycles = n;
10044b9ad928SBarry Smith   PetscFunctionReturn(0);
10054b9ad928SBarry Smith }
10064b9ad928SBarry Smith 
10074b9ad928SBarry Smith #undef __FUNCT__
10088cc2d5dfSBarry Smith #define __FUNCT__ "PCMGMultiplicativeSetCycles"
10098cc2d5dfSBarry Smith /*@
10108cc2d5dfSBarry Smith    PCMGMultiplicativeSetCycles - Sets the number of cycles to use for each preconditioner step
10118cc2d5dfSBarry Smith          of multigrid when PCMGType of PC_MG_MULTIPLICATIVE is used
10128cc2d5dfSBarry Smith 
1013ad4df100SBarry Smith    Logically Collective on PC
10148cc2d5dfSBarry Smith 
10158cc2d5dfSBarry Smith    Input Parameters:
10168cc2d5dfSBarry Smith +  pc - the multigrid context
10178cc2d5dfSBarry Smith -  n - number of cycles (default is 1)
10188cc2d5dfSBarry Smith 
10198cc2d5dfSBarry Smith    Options Database Key:
1020e1bc860dSBarry Smith .  -pc_mg_multiplicative_cycles n
10218cc2d5dfSBarry Smith 
10228cc2d5dfSBarry Smith    Level: advanced
10238cc2d5dfSBarry Smith 
10248cc2d5dfSBarry Smith    Notes: This is not associated with setting a v or w cycle, that is set with PCMGSetCycleType()
10258cc2d5dfSBarry Smith 
10268cc2d5dfSBarry Smith .keywords: MG, set, cycles, V-cycle, W-cycle, multigrid
10278cc2d5dfSBarry Smith 
10288cc2d5dfSBarry Smith .seealso: PCMGSetCycleTypeOnLevel(), PCMGSetCycleType()
10298cc2d5dfSBarry Smith @*/
10307087cfbeSBarry Smith PetscErrorCode  PCMGMultiplicativeSetCycles(PC pc,PetscInt n)
10318cc2d5dfSBarry Smith {
1032f3fbd535SBarry Smith   PC_MG        *mg        = (PC_MG*)pc->data;
10338cc2d5dfSBarry Smith 
10348cc2d5dfSBarry Smith   PetscFunctionBegin;
10350700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
1036c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(pc,n,2);
10372a21e185SBarry Smith   mg->cyclesperpcapply = n;
10388cc2d5dfSBarry Smith   PetscFunctionReturn(0);
10398cc2d5dfSBarry Smith }
10408cc2d5dfSBarry Smith 
10418cc2d5dfSBarry Smith #undef __FUNCT__
1042fb15c04eSBarry Smith #define __FUNCT__ "PCMGSetGalerkin_MG"
10432134b1e4SBarry Smith PetscErrorCode PCMGSetGalerkin_MG(PC pc,PCMGGalerkinType use)
1044fb15c04eSBarry Smith {
1045fb15c04eSBarry Smith   PC_MG *mg = (PC_MG*)pc->data;
1046fb15c04eSBarry Smith 
1047fb15c04eSBarry Smith   PetscFunctionBegin;
10482134b1e4SBarry Smith   mg->galerkin = use;
1049fb15c04eSBarry Smith   PetscFunctionReturn(0);
1050fb15c04eSBarry Smith }
1051fb15c04eSBarry Smith 
1052fb15c04eSBarry Smith #undef __FUNCT__
10539dcbbd2bSBarry Smith #define __FUNCT__ "PCMGSetGalerkin"
1054c2be2410SBarry Smith /*@
105597177400SBarry Smith    PCMGSetGalerkin - Causes the coarser grid matrices to be computed from the
105682b87d87SMatthew G. Knepley       finest grid via the Galerkin process: A_i-1 = r_i * A_i * p_i
1057c2be2410SBarry Smith 
1058ad4df100SBarry Smith    Logically Collective on PC
1059c2be2410SBarry Smith 
1060c2be2410SBarry Smith    Input Parameters:
1061c91913d3SJed Brown +  pc - the multigrid context
10622134b1e4SBarry Smith -  use - one of PC_MG_GALERKIN_BOTH,PC_MG_GALERKIN_PMAT,PC_MG_GALERKIN_MAT, or PC_MG_GALERKIN_NONE
1063c2be2410SBarry Smith 
1064c2be2410SBarry Smith    Options Database Key:
10652134b1e4SBarry Smith .  -pc_mg_galerkin <both,pmat,mat,none>
1066c2be2410SBarry Smith 
1067c2be2410SBarry Smith    Level: intermediate
1068c2be2410SBarry Smith 
10691c1aac46SBarry Smith    Notes: Some codes that use PCMG such as PCGAMG use Galerkin internally while constructing the hierarchy and thus do not
10701c1aac46SBarry Smith      use the PCMG construction of the coarser grids.
10711c1aac46SBarry Smith 
1072c2be2410SBarry Smith .keywords: MG, set, Galerkin
1073c2be2410SBarry Smith 
10742134b1e4SBarry Smith .seealso: PCMGGetGalerkin(), PCMGGalerkinType
10753fc8bf9cSBarry Smith 
1076c2be2410SBarry Smith @*/
10772134b1e4SBarry Smith PetscErrorCode PCMGSetGalerkin(PC pc,PCMGGalerkinType use)
1078c2be2410SBarry Smith {
1079fb15c04eSBarry Smith   PetscErrorCode ierr;
1080c2be2410SBarry Smith 
1081c2be2410SBarry Smith   PetscFunctionBegin;
10820700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
10832134b1e4SBarry Smith   ierr = PetscTryMethod(pc,"PCMGSetGalerkin_C",(PC,PCMGGalerkinType),(pc,use));CHKERRQ(ierr);
1084c2be2410SBarry Smith   PetscFunctionReturn(0);
1085c2be2410SBarry Smith }
1086c2be2410SBarry Smith 
1087c2be2410SBarry Smith #undef __FUNCT__
10883fc8bf9cSBarry Smith #define __FUNCT__ "PCMGGetGalerkin"
10893fc8bf9cSBarry Smith /*@
10903fc8bf9cSBarry Smith    PCMGGetGalerkin - Checks if Galerkin multigrid is being used, i.e.
109182b87d87SMatthew G. Knepley       A_i-1 = r_i * A_i * p_i
10923fc8bf9cSBarry Smith 
10933fc8bf9cSBarry Smith    Not Collective
10943fc8bf9cSBarry Smith 
10953fc8bf9cSBarry Smith    Input Parameter:
10963fc8bf9cSBarry Smith .  pc - the multigrid context
10973fc8bf9cSBarry Smith 
10983fc8bf9cSBarry Smith    Output Parameter:
10992134b1e4SBarry Smith .  galerkin - one of PC_MG_GALERKIN_BOTH,PC_MG_GALERKIN_PMAT,PC_MG_GALERKIN_MAT, PC_MG_GALERKIN_NONE, or PC_MG_GALERKIN_EXTERNAL
11003fc8bf9cSBarry Smith 
11013fc8bf9cSBarry Smith    Level: intermediate
11023fc8bf9cSBarry Smith 
11033fc8bf9cSBarry Smith .keywords: MG, set, Galerkin
11043fc8bf9cSBarry Smith 
11052134b1e4SBarry Smith .seealso: PCMGSetGalerkin(), PCMGGalerkinType
11063fc8bf9cSBarry Smith 
11073fc8bf9cSBarry Smith @*/
11082134b1e4SBarry Smith PetscErrorCode  PCMGGetGalerkin(PC pc,PCMGGalerkinType  *galerkin)
11093fc8bf9cSBarry Smith {
1110f3fbd535SBarry Smith   PC_MG *mg = (PC_MG*)pc->data;
11113fc8bf9cSBarry Smith 
11123fc8bf9cSBarry Smith   PetscFunctionBegin;
11130700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
11142134b1e4SBarry Smith   *galerkin = mg->galerkin;
11153fc8bf9cSBarry Smith   PetscFunctionReturn(0);
11163fc8bf9cSBarry Smith }
11173fc8bf9cSBarry Smith 
11183fc8bf9cSBarry Smith #undef __FUNCT__
11199dcbbd2bSBarry Smith #define __FUNCT__ "PCMGSetNumberSmoothDown"
11204b9ad928SBarry Smith /*@
112197177400SBarry Smith    PCMGSetNumberSmoothDown - Sets the number of pre-smoothing steps to
112297177400SBarry Smith    use on all levels. Use PCMGGetSmootherDown() to set different
11234b9ad928SBarry Smith    pre-smoothing steps on different levels.
11244b9ad928SBarry Smith 
1125ad4df100SBarry Smith    Logically Collective on PC
11264b9ad928SBarry Smith 
11274b9ad928SBarry Smith    Input Parameters:
11284b9ad928SBarry Smith +  mg - the multigrid context
11294b9ad928SBarry Smith -  n - the number of smoothing steps
11304b9ad928SBarry Smith 
11314b9ad928SBarry Smith    Options Database Key:
11324b9ad928SBarry Smith .  -pc_mg_smoothdown <n> - Sets number of pre-smoothing steps
11334b9ad928SBarry Smith 
11344b9ad928SBarry Smith    Level: advanced
11358c75dd1cSBarry Smith     If the number of smoothing steps is changed in this call then the PCMGGetSmoothUp() will be called and now the
113606792cafSBarry Smith    up smoother will no longer share the same KSP object as the down smoother. Use PCMGSetNumberSmooth() to set the same
113706792cafSBarry Smith    number of smoothing steps for pre and post smoothing.
11384b9ad928SBarry Smith 
11394b9ad928SBarry Smith .keywords: MG, smooth, down, pre-smoothing, steps, multigrid
11404b9ad928SBarry Smith 
114106792cafSBarry Smith .seealso: PCMGSetNumberSmoothUp(), PCMGSetNumberSmooth()
11424b9ad928SBarry Smith @*/
11437087cfbeSBarry Smith PetscErrorCode  PCMGSetNumberSmoothDown(PC pc,PetscInt n)
11444b9ad928SBarry Smith {
1145f3fbd535SBarry Smith   PC_MG          *mg        = (PC_MG*)pc->data;
1146f3fbd535SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
11476849ba73SBarry Smith   PetscErrorCode ierr;
114879416396SBarry Smith   PetscInt       i,levels;
11494b9ad928SBarry Smith 
11504b9ad928SBarry Smith   PetscFunctionBegin;
11510700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
1152ce94432eSBarry Smith   if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
1153c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(pc,n,2);
1154f3fbd535SBarry Smith   levels = mglevels[0]->levels;
11554b9ad928SBarry Smith 
1156b05257ddSBarry Smith   for (i=1; i<levels; i++) {
11578c75dd1cSBarry Smith     PetscInt nc;
11588c75dd1cSBarry Smith     ierr = KSPGetTolerances(mglevels[i]->smoothd,NULL,NULL,NULL,&nc);CHKERRQ(ierr);
11598c75dd1cSBarry Smith     if (nc == n) continue;
11608c75dd1cSBarry Smith 
11614b9ad928SBarry Smith     /* make sure smoother up and down are different */
11620298fd71SBarry Smith     ierr = PCMGGetSmootherUp(pc,i,NULL);CHKERRQ(ierr);
1163f3fbd535SBarry Smith     ierr = KSPSetTolerances(mglevels[i]->smoothd,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,n);CHKERRQ(ierr);
11642fa5cd67SKarl Rupp 
116531567311SBarry Smith     mg->default_smoothd = n;
11664b9ad928SBarry Smith   }
11674b9ad928SBarry Smith   PetscFunctionReturn(0);
11684b9ad928SBarry Smith }
11694b9ad928SBarry Smith 
11704b9ad928SBarry Smith #undef __FUNCT__
11719dcbbd2bSBarry Smith #define __FUNCT__ "PCMGSetNumberSmoothUp"
11724b9ad928SBarry Smith /*@
117397177400SBarry Smith    PCMGSetNumberSmoothUp - Sets the number of post-smoothing steps to use
117497177400SBarry Smith    on all levels. Use PCMGGetSmootherUp() to set different numbers of
11754b9ad928SBarry Smith    post-smoothing steps on different levels.
11764b9ad928SBarry Smith 
1177ad4df100SBarry Smith    Logically Collective on PC
11784b9ad928SBarry Smith 
11794b9ad928SBarry Smith    Input Parameters:
11804b9ad928SBarry Smith +  mg - the multigrid context
11814b9ad928SBarry Smith -  n - the number of smoothing steps
11824b9ad928SBarry Smith 
11834b9ad928SBarry Smith    Options Database Key:
11844b9ad928SBarry Smith .  -pc_mg_smoothup <n> - Sets number of post-smoothing steps
11854b9ad928SBarry Smith 
11864b9ad928SBarry Smith    Level: advanced
11874b9ad928SBarry Smith 
11888c75dd1cSBarry Smith    Notes: this does not set a value on the coarsest grid, since we assume that
1189a8c7a070SBarry Smith     there is no separate smooth up on the coarsest grid.
11904b9ad928SBarry Smith 
11918c75dd1cSBarry Smith     If the number of smoothing steps is changed in this call then the PCMGGetSmoothUp() will be called and now the
119206792cafSBarry Smith    up smoother will no longer share the same KSP object as the down smoother. Use PCMGSetNumberSmooth() to set the same
119306792cafSBarry Smith    number of smoothing steps for pre and post smoothing.
119406792cafSBarry Smith 
11958c75dd1cSBarry Smith 
11964b9ad928SBarry Smith .keywords: MG, smooth, up, post-smoothing, steps, multigrid
11974b9ad928SBarry Smith 
119806792cafSBarry Smith .seealso: PCMGSetNumberSmoothDown(), PCMGSetNumberSmooth()
11994b9ad928SBarry Smith @*/
12007087cfbeSBarry Smith PetscErrorCode  PCMGSetNumberSmoothUp(PC pc,PetscInt n)
12014b9ad928SBarry Smith {
1202f3fbd535SBarry Smith   PC_MG          *mg        = (PC_MG*)pc->data;
1203f3fbd535SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
12046849ba73SBarry Smith   PetscErrorCode ierr;
120579416396SBarry Smith   PetscInt       i,levels;
12064b9ad928SBarry Smith 
12074b9ad928SBarry Smith   PetscFunctionBegin;
12080700a824SBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
1209ce94432eSBarry Smith   if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
1210c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(pc,n,2);
1211f3fbd535SBarry Smith   levels = mglevels[0]->levels;
12124b9ad928SBarry Smith 
12134b9ad928SBarry Smith   for (i=1; i<levels; i++) {
12148c75dd1cSBarry Smith     if (mglevels[i]->smoothu == mglevels[i]->smoothd) {
12158c75dd1cSBarry Smith       PetscInt nc;
12168c75dd1cSBarry Smith       ierr = KSPGetTolerances(mglevels[i]->smoothd,NULL,NULL,NULL,&nc);CHKERRQ(ierr);
12178c75dd1cSBarry Smith       if (nc == n) continue;
12188c75dd1cSBarry Smith     }
12198c75dd1cSBarry Smith 
12204b9ad928SBarry Smith     /* make sure smoother up and down are different */
12210298fd71SBarry Smith     ierr = PCMGGetSmootherUp(pc,i,NULL);CHKERRQ(ierr);
1222f3fbd535SBarry Smith     ierr = KSPSetTolerances(mglevels[i]->smoothu,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,n);CHKERRQ(ierr);
12232fa5cd67SKarl Rupp 
122431567311SBarry Smith     mg->default_smoothu = n;
12254b9ad928SBarry Smith   }
12264b9ad928SBarry Smith   PetscFunctionReturn(0);
12274b9ad928SBarry Smith }
12284b9ad928SBarry Smith 
122906792cafSBarry Smith #undef __FUNCT__
123006792cafSBarry Smith #define __FUNCT__ "PCMGSetNumberSmooth"
123106792cafSBarry Smith /*@
123206792cafSBarry Smith    PCMGSetNumberSmooth - Sets the number of pre and post-smoothing steps to use
123306792cafSBarry Smith    on all levels. Use PCMGSetSmoothUp() and PCMGSetSmoothDown() set different numbers of
123406792cafSBarry Smith    pre ad post-smoothing steps
123506792cafSBarry Smith 
123606792cafSBarry Smith    Logically Collective on PC
123706792cafSBarry Smith 
123806792cafSBarry Smith    Input Parameters:
123906792cafSBarry Smith +  mg - the multigrid context
124006792cafSBarry Smith -  n - the number of smoothing steps
124106792cafSBarry Smith 
124206792cafSBarry Smith    Options Database Key:
124306792cafSBarry Smith +  -mg_levels_ksp_max_it <n> - Sets number of pre and post-smoothing steps
124406792cafSBarry Smith .  -pc_mg_smooth_down <n> - Sets number of pre-smoothing steps (if setting different pre and post amounts)
124506792cafSBarry Smith -  -pc_mg_smooth_up <n> - Sets number of post-smoothing steps (if setting different pre and post amounts)
124606792cafSBarry Smith 
124706792cafSBarry Smith    Level: advanced
124806792cafSBarry Smith 
124906792cafSBarry Smith    Notes: this does not set a value on the coarsest grid, since we assume that
125006792cafSBarry Smith     there is no separate smooth up on the coarsest grid.
125106792cafSBarry Smith 
125206792cafSBarry Smith .keywords: MG, smooth, up, post-smoothing, steps, multigrid
125306792cafSBarry Smith 
125406792cafSBarry Smith .seealso: PCMGSetNumberSmoothDown(), PCMGSetNumberSmoothUp()
125506792cafSBarry Smith @*/
125606792cafSBarry Smith PetscErrorCode  PCMGSetNumberSmooth(PC pc,PetscInt n)
125706792cafSBarry Smith {
125806792cafSBarry Smith   PC_MG          *mg        = (PC_MG*)pc->data;
125906792cafSBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
126006792cafSBarry Smith   PetscErrorCode ierr;
126106792cafSBarry Smith   PetscInt       i,levels;
126206792cafSBarry Smith 
126306792cafSBarry Smith   PetscFunctionBegin;
126406792cafSBarry Smith   PetscValidHeaderSpecific(pc,PC_CLASSID,1);
126506792cafSBarry Smith   if (!mglevels) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
126606792cafSBarry Smith   PetscValidLogicalCollectiveInt(pc,n,2);
126706792cafSBarry Smith   levels = mglevels[0]->levels;
126806792cafSBarry Smith 
126906792cafSBarry Smith   for (i=1; i<levels; i++) {
127006792cafSBarry Smith     ierr = KSPSetTolerances(mglevels[i]->smoothu,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,n);CHKERRQ(ierr);
127106792cafSBarry Smith     ierr = KSPSetTolerances(mglevels[i]->smoothd,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,n);CHKERRQ(ierr);
127206792cafSBarry Smith     mg->default_smoothu = n;
127306792cafSBarry Smith     mg->default_smoothd = n;
127406792cafSBarry Smith   }
127506792cafSBarry Smith   PetscFunctionReturn(0);
127606792cafSBarry Smith }
127706792cafSBarry Smith 
12784b9ad928SBarry Smith /* ----------------------------------------------------------------------------------------*/
12794b9ad928SBarry Smith 
12803b09bd56SBarry Smith /*MC
1281ccb205f8SBarry Smith    PCMG - Use multigrid preconditioning. This preconditioner requires you provide additional
12823b09bd56SBarry Smith     information about the coarser grid matrices and restriction/interpolation operators.
12833b09bd56SBarry Smith 
12843b09bd56SBarry Smith    Options Database Keys:
12853b09bd56SBarry Smith +  -pc_mg_levels <nlevels> - number of levels including finest
12864afba7b0SPatrick Sanan .  -pc_mg_cycle_type <v,w> -
128779416396SBarry Smith .  -pc_mg_smoothup <n> - number of smoothing steps after interpolation
12883b09bd56SBarry Smith .  -pc_mg_smoothdown <n> - number of smoothing steps before applying restriction operator
12898c1c2452SJed Brown .  -pc_mg_type <additive,multiplicative,full,kaskade> - multiplicative is the default
12903b09bd56SBarry Smith .  -pc_mg_log - log information about time spent on each level of the solver
12912134b1e4SBarry Smith .  -pc_mg_galerkin <both,pmat,mat,none> - use Galerkin process to compute coarser operators, i.e. Acoarse = R A R'
12928cf6037eSBarry Smith .  -pc_mg_multiplicative_cycles - number of cycles to use as the preconditioner (defaults to 1)
12938cf6037eSBarry Smith .  -pc_mg_dump_matlab - dumps the matrices for each level and the restriction/interpolation matrices
1294e3c5b3baSBarry Smith                         to the Socket viewer for reading from MATLAB.
12958cf6037eSBarry Smith -  -pc_mg_dump_binary - dumps the matrices for each level and the restriction/interpolation matrices
12968cf6037eSBarry Smith                         to the binary output file called binaryoutput
12973b09bd56SBarry Smith 
1298e941fc33SBarry Smith    Notes: If one uses a Krylov method such GMRES or CG as the smoother than one must use KSPFGMRES, KSPGCG, or KSPRICHARDSON as the outer Krylov method
12993b09bd56SBarry Smith 
13008cf6037eSBarry Smith        When run with a single level the smoother options are used on that level NOT the coarse grid solver options
13018cf6037eSBarry Smith 
13023b09bd56SBarry Smith    Level: intermediate
13033b09bd56SBarry Smith 
13048f87f92bSBarry Smith    Concepts: multigrid/multilevel
13053b09bd56SBarry Smith 
13068cf6037eSBarry Smith .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC, PCMGType, PCEXOTIC, PCGAMG, PCML, PCHYPRE
13070d353602SBarry Smith            PCMGSetLevels(), PCMGGetLevels(), PCMGSetType(), PCMGSetCycleType(), PCMGSetNumberSmoothDown(),
130897177400SBarry Smith            PCMGSetNumberSmoothUp(), PCMGGetCoarseSolve(), PCMGSetResidual(), PCMGSetInterpolation(),
130997177400SBarry Smith            PCMGSetRestriction(), PCMGGetSmoother(), PCMGGetSmootherUp(), PCMGGetSmootherDown(),
13100d353602SBarry Smith            PCMGSetCycleTypeOnLevel(), PCMGSetRhs(), PCMGSetX(), PCMGSetR()
13113b09bd56SBarry Smith M*/
13123b09bd56SBarry Smith 
13134b9ad928SBarry Smith #undef __FUNCT__
13144b9ad928SBarry Smith #define __FUNCT__ "PCCreate_MG"
13158cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PCCreate_MG(PC pc)
13164b9ad928SBarry Smith {
1317f3fbd535SBarry Smith   PC_MG          *mg;
1318f3fbd535SBarry Smith   PetscErrorCode ierr;
1319f3fbd535SBarry Smith 
13204b9ad928SBarry Smith   PetscFunctionBegin;
1321b00a9115SJed Brown   ierr         = PetscNewLog(pc,&mg);CHKERRQ(ierr);
1322f3fbd535SBarry Smith   pc->data     = (void*)mg;
1323f3fbd535SBarry Smith   mg->nlevels  = -1;
132410eca3edSLisandro Dalcin   mg->am       = PC_MG_MULTIPLICATIVE;
13252134b1e4SBarry Smith   mg->galerkin = PC_MG_GALERKIN_NONE;
1326f3fbd535SBarry Smith 
132737a44384SMark Adams   pc->useAmat = PETSC_TRUE;
132837a44384SMark Adams 
13294b9ad928SBarry Smith   pc->ops->apply          = PCApply_MG;
13304b9ad928SBarry Smith   pc->ops->setup          = PCSetUp_MG;
1331a06653b4SBarry Smith   pc->ops->reset          = PCReset_MG;
13324b9ad928SBarry Smith   pc->ops->destroy        = PCDestroy_MG;
13334b9ad928SBarry Smith   pc->ops->setfromoptions = PCSetFromOptions_MG;
13344b9ad928SBarry Smith   pc->ops->view           = PCView_MG;
1335fb15c04eSBarry Smith 
1336fb15c04eSBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)pc,"PCMGSetGalerkin_C",PCMGSetGalerkin_MG);CHKERRQ(ierr);
13374b9ad928SBarry Smith   PetscFunctionReturn(0);
13384b9ad928SBarry Smith }
1339