xref: /petsc/src/ksp/pc/impls/mg/mg.c (revision f3fbd5355936bed48b7956a540fdcc016dd18626)
1dba47a55SKris Buschelman #define PETSCKSP_DLL
2dba47a55SKris Buschelman 
34b9ad928SBarry Smith /*
44b9ad928SBarry Smith     Defines the multigrid preconditioner interface.
54b9ad928SBarry Smith */
67c4f633dSBarry Smith #include "../src/ksp/pc/impls/mg/mgimpl.h"                    /*I "petscmg.h" I*/
74b9ad928SBarry Smith 
84b9ad928SBarry Smith 
94b9ad928SBarry Smith #undef __FUNCT__
109dcbbd2bSBarry Smith #define __FUNCT__ "PCMGMCycle_Private"
11*f3fbd535SBarry Smith PetscErrorCode PCMGMCycle_Private(PC pc,PC_MG_Levels **mglevels,PCRichardsonConvergedReason *reason)
124b9ad928SBarry Smith {
13*f3fbd535SBarry Smith   PC_MG_Levels   *mg = *mglevels,*mgc;
146849ba73SBarry Smith   PetscErrorCode ierr;
15e0ef7d32SBarry Smith   PetscInt       cycles = (mg->level == 1) ? 1 : (PetscInt) mg->cycles;
164b9ad928SBarry Smith 
174b9ad928SBarry Smith   PetscFunctionBegin;
184b9ad928SBarry Smith 
1932cf1786SBarry Smith   if (mg->eventsmoothsolve) {ierr = PetscLogEventBegin(mg->eventsmoothsolve,0,0,0,0);CHKERRQ(ierr);}
200d353602SBarry Smith   ierr = KSPSolve(mg->smoothd,mg->b,mg->x);CHKERRQ(ierr);  /* pre-smooth */
2132cf1786SBarry Smith   if (mg->eventsmoothsolve) {ierr = PetscLogEventEnd(mg->eventsmoothsolve,0,0,0,0);CHKERRQ(ierr);}
224b9ad928SBarry Smith   if (mg->level) {  /* not the coarsest grid */
2332cf1786SBarry Smith     if (mg->eventresidual) {ierr = PetscLogEventBegin(mg->eventresidual,0,0,0,0);CHKERRQ(ierr);}
244b9ad928SBarry Smith     ierr = (*mg->residual)(mg->A,mg->b,mg->x,mg->r);CHKERRQ(ierr);
2532cf1786SBarry Smith     if (mg->eventresidual) {ierr = PetscLogEventEnd(mg->eventresidual,0,0,0,0);CHKERRQ(ierr);}
264b9ad928SBarry Smith 
274b9ad928SBarry Smith     /* if on finest level and have convergence criteria set */
284d0a8057SBarry Smith     if (mg->level == mg->levels-1 && mg->ttol && reason) {
294b9ad928SBarry Smith       PetscReal rnorm;
304b9ad928SBarry Smith       ierr = VecNorm(mg->r,NORM_2,&rnorm);CHKERRQ(ierr);
314b9ad928SBarry Smith       if (rnorm <= mg->ttol) {
3270441072SBarry Smith         if (rnorm < mg->abstol) {
334d0a8057SBarry Smith           *reason = PCRICHARDSON_CONVERGED_ATOL;
341e2582c4SBarry Smith           ierr = PetscInfo2(pc,"Linear solver has converged. Residual norm %G is less than absolute tolerance %G\n",rnorm,mg->abstol);CHKERRQ(ierr);
354b9ad928SBarry Smith         } else {
364d0a8057SBarry Smith           *reason = PCRICHARDSON_CONVERGED_RTOL;
371e2582c4SBarry Smith           ierr = PetscInfo2(pc,"Linear solver has converged. Residual norm %G is less than relative tolerance times initial residual norm %G\n",rnorm,mg->ttol);CHKERRQ(ierr);
384b9ad928SBarry Smith         }
394b9ad928SBarry Smith         PetscFunctionReturn(0);
404b9ad928SBarry Smith       }
414b9ad928SBarry Smith     }
424b9ad928SBarry Smith 
434b9ad928SBarry Smith     mgc = *(mglevels - 1);
4432cf1786SBarry Smith     if (mg->eventinterprestrict) {ierr = PetscLogEventBegin(mg->eventinterprestrict,0,0,0,0);CHKERRQ(ierr);}
454b9ad928SBarry Smith     ierr = MatRestrict(mg->restrct,mg->r,mgc->b);CHKERRQ(ierr);
4632cf1786SBarry Smith     if (mg->eventinterprestrict) {ierr = PetscLogEventEnd(mg->eventinterprestrict,0,0,0,0);CHKERRQ(ierr);}
47efb30889SBarry Smith     ierr = VecSet(mgc->x,0.0);CHKERRQ(ierr);
484b9ad928SBarry Smith     while (cycles--) {
494d0a8057SBarry Smith       ierr = PCMGMCycle_Private(pc,mglevels-1,reason);CHKERRQ(ierr);
504b9ad928SBarry Smith     }
5132cf1786SBarry Smith     if (mg->eventinterprestrict) {ierr = PetscLogEventBegin(mg->eventinterprestrict,0,0,0,0);CHKERRQ(ierr);}
524b9ad928SBarry Smith     ierr = MatInterpolateAdd(mg->interpolate,mgc->x,mg->x,mg->x);CHKERRQ(ierr);
5332cf1786SBarry Smith     if (mg->eventinterprestrict) {ierr = PetscLogEventEnd(mg->eventinterprestrict,0,0,0,0);CHKERRQ(ierr);}
5432cf1786SBarry Smith     if (mg->eventsmoothsolve) {ierr = PetscLogEventBegin(mg->eventsmoothsolve,0,0,0,0);CHKERRQ(ierr);}
550d353602SBarry Smith     ierr = KSPSolve(mg->smoothu,mg->b,mg->x);CHKERRQ(ierr);    /* post smooth */
5632cf1786SBarry Smith     if (mg->eventsmoothsolve) {ierr = PetscLogEventEnd(mg->eventsmoothsolve,0,0,0,0);CHKERRQ(ierr);}
574b9ad928SBarry Smith   }
584b9ad928SBarry Smith   PetscFunctionReturn(0);
594b9ad928SBarry Smith }
604b9ad928SBarry Smith 
614b9ad928SBarry Smith #undef __FUNCT__
624b9ad928SBarry Smith #define __FUNCT__ "PCApplyRichardson_MG"
637319c654SBarry Smith static PetscErrorCode PCApplyRichardson_MG(PC pc,Vec b,Vec x,Vec w,PetscReal rtol,PetscReal abstol, PetscReal dtol,PetscInt its,PetscTruth zeroguess,PetscInt *outits,PCRichardsonConvergedReason *reason)
644b9ad928SBarry Smith {
65*f3fbd535SBarry Smith   PC_MG          *mg = (PC_MG*)pc->data;
66*f3fbd535SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
67dfbe8321SBarry Smith   PetscErrorCode ierr;
68*f3fbd535SBarry Smith   PetscInt       levels = mglevels[0]->levels,i;
694b9ad928SBarry Smith 
704b9ad928SBarry Smith   PetscFunctionBegin;
71*f3fbd535SBarry Smith   mglevels[levels-1]->b    = b;
72*f3fbd535SBarry Smith   mglevels[levels-1]->x    = x;
734b9ad928SBarry Smith 
74*f3fbd535SBarry Smith   mglevels[levels-1]->rtol = rtol;
75*f3fbd535SBarry Smith   mglevels[levels-1]->abstol = abstol;
76*f3fbd535SBarry Smith   mglevels[levels-1]->dtol = dtol;
774b9ad928SBarry Smith   if (rtol) {
784b9ad928SBarry Smith     /* compute initial residual norm for relative convergence test */
794b9ad928SBarry Smith     PetscReal rnorm;
807319c654SBarry Smith     if (zeroguess) {
817319c654SBarry Smith       ierr               = VecNorm(b,NORM_2,&rnorm);CHKERRQ(ierr);
827319c654SBarry Smith     } else {
83*f3fbd535SBarry Smith       ierr               = (*mglevels[levels-1]->residual)(mglevels[levels-1]->A,b,x,w);CHKERRQ(ierr);
844b9ad928SBarry Smith       ierr               = VecNorm(w,NORM_2,&rnorm);CHKERRQ(ierr);
857319c654SBarry Smith     }
86*f3fbd535SBarry Smith     mglevels[levels-1]->ttol = PetscMax(rtol*rnorm,abstol);
8770441072SBarry Smith   } else if (abstol) {
88*f3fbd535SBarry Smith     mglevels[levels-1]->ttol = abstol;
894b9ad928SBarry Smith   } else {
90*f3fbd535SBarry Smith     mglevels[levels-1]->ttol = 0.0;
914b9ad928SBarry Smith   }
924b9ad928SBarry Smith 
934d0a8057SBarry Smith   /* since smoother is applied to full system, not just residual we need to make sure that smoothers don't
944d0a8057SBarry Smith      stop prematurely do to small residual */
954d0a8057SBarry Smith   for (i=1; i<levels; i++) {
96*f3fbd535SBarry Smith     ierr = KSPSetTolerances(mglevels[i]->smoothu,0,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
97*f3fbd535SBarry Smith     if (mglevels[i]->smoothu != mglevels[i]->smoothd) {
98*f3fbd535SBarry Smith       ierr = KSPSetTolerances(mglevels[i]->smoothd,0,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
994b9ad928SBarry Smith     }
1004d0a8057SBarry Smith   }
1014d0a8057SBarry Smith 
1024d0a8057SBarry Smith   *reason = (PCRichardsonConvergedReason)0;
1034d0a8057SBarry Smith   for (i=0; i<its; i++) {
104*f3fbd535SBarry Smith     ierr = PCMGMCycle_Private(pc,mglevels+levels-1,reason);CHKERRQ(ierr);
1054d0a8057SBarry Smith     if (*reason) break;
1064d0a8057SBarry Smith   }
1074d0a8057SBarry Smith   if (!*reason) *reason = PCRICHARDSON_CONVERGED_ITS;
1084d0a8057SBarry Smith   *outits = i;
1094b9ad928SBarry Smith   PetscFunctionReturn(0);
1104b9ad928SBarry Smith }
1114b9ad928SBarry Smith 
1124b9ad928SBarry Smith #undef __FUNCT__
1139dcbbd2bSBarry Smith #define __FUNCT__ "PCMGSetLevels"
1144b9ad928SBarry Smith /*@C
11597177400SBarry Smith    PCMGSetLevels - Sets the number of levels to use with MG.
1164b9ad928SBarry Smith    Must be called before any other MG routine.
1174b9ad928SBarry Smith 
1184b9ad928SBarry Smith    Collective on PC
1194b9ad928SBarry Smith 
1204b9ad928SBarry Smith    Input Parameters:
1214b9ad928SBarry Smith +  pc - the preconditioner context
1224b9ad928SBarry Smith .  levels - the number of levels
1234b9ad928SBarry Smith -  comms - optional communicators for each level; this is to allow solving the coarser problems
1244b9ad928SBarry Smith            on smaller sets of processors. Use PETSC_NULL_OBJECT for default in Fortran
1254b9ad928SBarry Smith 
1264b9ad928SBarry Smith    Level: intermediate
1274b9ad928SBarry Smith 
1284b9ad928SBarry Smith    Notes:
1294b9ad928SBarry Smith      If the number of levels is one then the multigrid uses the -mg_levels prefix
1304b9ad928SBarry Smith   for setting the level options rather than the -mg_coarse prefix.
1314b9ad928SBarry Smith 
1324b9ad928SBarry Smith .keywords: MG, set, levels, multigrid
1334b9ad928SBarry Smith 
13497177400SBarry Smith .seealso: PCMGSetType(), PCMGGetLevels()
1354b9ad928SBarry Smith @*/
13697177400SBarry Smith PetscErrorCode PETSCKSP_DLLEXPORT PCMGSetLevels(PC pc,PetscInt levels,MPI_Comm *comms)
1374b9ad928SBarry Smith {
138dfbe8321SBarry Smith   PetscErrorCode ierr;
139*f3fbd535SBarry Smith   PC_MG          *mg = (PC_MG*)pc->data;
140*f3fbd535SBarry Smith   MPI_Comm       comm = ((PetscObject)pc)->comm;
141*f3fbd535SBarry Smith   PC_MG_Levels   **mglevels;
142*f3fbd535SBarry Smith   PetscInt       i;
143*f3fbd535SBarry Smith   PetscMPIInt    size;
144*f3fbd535SBarry Smith   const char     *prefix;
145*f3fbd535SBarry Smith   PC             ipc;
1464b9ad928SBarry Smith 
1474b9ad928SBarry Smith   PetscFunctionBegin;
1484482741eSBarry Smith   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
149*f3fbd535SBarry Smith   if (mg->nlevels > -1) {
150*f3fbd535SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Number levels already set for MG\n  make sure that you call PCMGSetLevels() before KSPSetFromOptions()");
1514b9ad928SBarry Smith   }
152*f3fbd535SBarry Smith   if (mg->levels) SETERRQ(PETSC_ERR_PLIB,"Internal error in PETSc, this array should not yet exist");
153*f3fbd535SBarry Smith 
154*f3fbd535SBarry Smith   mg->nlevels = levels;
155*f3fbd535SBarry Smith 
156*f3fbd535SBarry Smith   ierr = PetscMalloc(levels*sizeof(PC_MG*),&mglevels);CHKERRQ(ierr);
157*f3fbd535SBarry Smith   ierr = PetscLogObjectMemory(pc,levels*(sizeof(PC_MG*)));CHKERRQ(ierr);
158*f3fbd535SBarry Smith 
159*f3fbd535SBarry Smith   ierr = PCGetOptionsPrefix(pc,&prefix);CHKERRQ(ierr);
160*f3fbd535SBarry Smith 
161*f3fbd535SBarry Smith   for (i=0; i<levels; i++) {
162*f3fbd535SBarry Smith     ierr = PetscNewLog(pc,PC_MG_Levels,&mglevels[i]);CHKERRQ(ierr);
163*f3fbd535SBarry Smith     mglevels[i]->level           = i;
164*f3fbd535SBarry Smith     mglevels[i]->levels          = levels;
165*f3fbd535SBarry Smith     mglevels[i]->cycles          = PC_MG_CYCLE_V;
166*f3fbd535SBarry Smith     mglevels[i]->galerkin        = PETSC_FALSE;
167*f3fbd535SBarry Smith     mglevels[i]->galerkinused    = PETSC_FALSE;
168*f3fbd535SBarry Smith     mglevels[i]->default_smoothu = 1;
169*f3fbd535SBarry Smith     mglevels[i]->default_smoothd = 1;
170*f3fbd535SBarry Smith 
171*f3fbd535SBarry Smith     if (comms) comm = comms[i];
172*f3fbd535SBarry Smith     ierr = KSPCreate(comm,&mglevels[i]->smoothd);CHKERRQ(ierr);
173*f3fbd535SBarry Smith     ierr = PetscObjectIncrementTabLevel((PetscObject)mglevels[i]->smoothd,(PetscObject)pc,levels-i);CHKERRQ(ierr);
174*f3fbd535SBarry Smith     ierr = KSPSetTolerances(mglevels[i]->smoothd,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT, mglevels[i]->default_smoothd);CHKERRQ(ierr);
175*f3fbd535SBarry Smith     ierr = KSPSetOptionsPrefix(mglevels[i]->smoothd,prefix);CHKERRQ(ierr);
176*f3fbd535SBarry Smith 
177*f3fbd535SBarry Smith     /* do special stuff for coarse grid */
178*f3fbd535SBarry Smith     if (!i && levels > 1) {
179*f3fbd535SBarry Smith       ierr = KSPAppendOptionsPrefix(mglevels[0]->smoothd,"mg_coarse_");CHKERRQ(ierr);
180*f3fbd535SBarry Smith 
181*f3fbd535SBarry Smith       /* coarse solve is (redundant) LU by default */
182*f3fbd535SBarry Smith       ierr = KSPSetType(mglevels[0]->smoothd,KSPPREONLY);CHKERRQ(ierr);
183*f3fbd535SBarry Smith       ierr = KSPGetPC(mglevels[0]->smoothd,&ipc);CHKERRQ(ierr);
184*f3fbd535SBarry Smith       ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
185*f3fbd535SBarry Smith       if (size > 1) {
186*f3fbd535SBarry Smith         ierr = PCSetType(ipc,PCREDUNDANT);CHKERRQ(ierr);
187*f3fbd535SBarry Smith       } else {
188*f3fbd535SBarry Smith         ierr = PCSetType(ipc,PCLU);CHKERRQ(ierr);
189*f3fbd535SBarry Smith       }
190*f3fbd535SBarry Smith 
191*f3fbd535SBarry Smith     } else {
192*f3fbd535SBarry Smith       char tprefix[128];
193*f3fbd535SBarry Smith       sprintf(tprefix,"mg_levels_%d_",(int)i);
194*f3fbd535SBarry Smith       ierr = KSPAppendOptionsPrefix(mglevels[i]->smoothd,tprefix);CHKERRQ(ierr);
195*f3fbd535SBarry Smith     }
196*f3fbd535SBarry Smith     ierr = PetscLogObjectParent(pc,mglevels[i]->smoothd);CHKERRQ(ierr);
197*f3fbd535SBarry Smith     mglevels[i]->smoothu             = mglevels[i]->smoothd;
198*f3fbd535SBarry Smith     mglevels[i]->rtol                = 0.0;
199*f3fbd535SBarry Smith     mglevels[i]->abstol              = 0.0;
200*f3fbd535SBarry Smith     mglevels[i]->dtol                = 0.0;
201*f3fbd535SBarry Smith     mglevels[i]->ttol                = 0.0;
202*f3fbd535SBarry Smith     mglevels[i]->eventsmoothsetup    = 0;
203*f3fbd535SBarry Smith     mglevels[i]->eventsmoothsolve    = 0;
204*f3fbd535SBarry Smith     mglevels[i]->eventresidual       = 0;
205*f3fbd535SBarry Smith     mglevels[i]->eventinterprestrict = 0;
206*f3fbd535SBarry Smith     mglevels[i]->cyclesperpcapply    = 1;
207*f3fbd535SBarry Smith   }
208*f3fbd535SBarry Smith   mglevels[0]->am          = PC_MG_MULTIPLICATIVE;
209*f3fbd535SBarry Smith   mg->levels               = mglevels;
2104b9ad928SBarry Smith   pc->ops->applyrichardson = PCApplyRichardson_MG;
2114b9ad928SBarry Smith   PetscFunctionReturn(0);
2124b9ad928SBarry Smith }
2134b9ad928SBarry Smith 
2144b9ad928SBarry Smith #undef __FUNCT__
215*f3fbd535SBarry Smith #define __FUNCT__ "PCDestroy_MG"
216*f3fbd535SBarry Smith PetscErrorCode PCDestroy_MG(PC pc)
217*f3fbd535SBarry Smith {
218*f3fbd535SBarry Smith   PC_MG          *mg = (PC_MG*)pc->data;
219*f3fbd535SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
220*f3fbd535SBarry Smith   PetscErrorCode ierr;
221*f3fbd535SBarry Smith   PetscInt       i,n;
222*f3fbd535SBarry Smith 
223*f3fbd535SBarry Smith   PetscFunctionBegin;
224*f3fbd535SBarry Smith   if (mglevels) {
225*f3fbd535SBarry Smith     n = mglevels[0]->levels;
226*f3fbd535SBarry Smith     for (i=0; i<n-1; i++) {
227*f3fbd535SBarry Smith       if (mglevels[i+1]->r) {ierr = VecDestroy(mglevels[i+1]->r);CHKERRQ(ierr);}
228*f3fbd535SBarry Smith       if (mglevels[i]->b) {ierr = VecDestroy(mglevels[i]->b);CHKERRQ(ierr);}
229*f3fbd535SBarry Smith       if (mglevels[i]->x) {ierr = VecDestroy(mglevels[i]->x);CHKERRQ(ierr);}
230*f3fbd535SBarry Smith       if (mglevels[i+1]->restrct) {ierr = MatDestroy(mglevels[i+1]->restrct);CHKERRQ(ierr);}
231*f3fbd535SBarry Smith       if (mglevels[i+1]->interpolate) {ierr = MatDestroy(mglevels[i+1]->interpolate);CHKERRQ(ierr);}
232*f3fbd535SBarry Smith     }
233*f3fbd535SBarry Smith 
234*f3fbd535SBarry Smith     for (i=0; i<n; i++) {
235*f3fbd535SBarry Smith       if (mglevels[i]->smoothd != mglevels[i]->smoothu) {
236*f3fbd535SBarry Smith 	ierr = KSPDestroy(mglevels[i]->smoothd);CHKERRQ(ierr);
237*f3fbd535SBarry Smith       }
238*f3fbd535SBarry Smith       ierr = KSPDestroy(mglevels[i]->smoothu);CHKERRQ(ierr);
239*f3fbd535SBarry Smith       ierr = PetscFree(mglevels[i]);CHKERRQ(ierr);
240*f3fbd535SBarry Smith     }
241*f3fbd535SBarry Smith     ierr = PetscFree(mglevels);CHKERRQ(ierr);
242*f3fbd535SBarry Smith   }
243*f3fbd535SBarry Smith   ierr = PetscFree(mg);CHKERRQ(ierr);
244*f3fbd535SBarry Smith   PetscFunctionReturn(0);
245*f3fbd535SBarry Smith }
246*f3fbd535SBarry Smith 
247*f3fbd535SBarry Smith 
248*f3fbd535SBarry Smith 
249*f3fbd535SBarry Smith EXTERN PetscErrorCode PCMGACycle_Private(PC_MG_Levels**);
250*f3fbd535SBarry Smith EXTERN PetscErrorCode PCMGFCycle_Private(PC,PC_MG_Levels**);
251*f3fbd535SBarry Smith EXTERN PetscErrorCode PCMGKCycle_Private(PC_MG_Levels**);
252*f3fbd535SBarry Smith 
253*f3fbd535SBarry Smith /*
254*f3fbd535SBarry Smith    PCApply_MG - Runs either an additive, multiplicative, Kaskadic
255*f3fbd535SBarry Smith              or full cycle of multigrid.
256*f3fbd535SBarry Smith 
257*f3fbd535SBarry Smith   Note:
258*f3fbd535SBarry Smith   A simple wrapper which calls PCMGMCycle(),PCMGACycle(), or PCMGFCycle().
259*f3fbd535SBarry Smith */
260*f3fbd535SBarry Smith #undef __FUNCT__
261*f3fbd535SBarry Smith #define __FUNCT__ "PCApply_MG"
262*f3fbd535SBarry Smith static PetscErrorCode PCApply_MG(PC pc,Vec b,Vec x)
263*f3fbd535SBarry Smith {
264*f3fbd535SBarry Smith   PC_MG          *mg = (PC_MG*)pc->data;
265*f3fbd535SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
266*f3fbd535SBarry Smith   PetscErrorCode ierr;
267*f3fbd535SBarry Smith   PetscInt       levels = mglevels[0]->levels,i;
268*f3fbd535SBarry Smith 
269*f3fbd535SBarry Smith   PetscFunctionBegin;
270*f3fbd535SBarry Smith   mglevels[levels-1]->b = b;
271*f3fbd535SBarry Smith   mglevels[levels-1]->x = x;
272*f3fbd535SBarry Smith   if (mglevels[0]->am == PC_MG_MULTIPLICATIVE) {
273*f3fbd535SBarry Smith     ierr = VecSet(x,0.0);CHKERRQ(ierr);
274*f3fbd535SBarry Smith     for (i=0; i<mglevels[0]->cyclesperpcapply; i++) {
275*f3fbd535SBarry Smith       ierr = PCMGMCycle_Private(pc,mglevels+levels-1,PETSC_NULL);CHKERRQ(ierr);
276*f3fbd535SBarry Smith     }
277*f3fbd535SBarry Smith   }
278*f3fbd535SBarry Smith   else if (mglevels[0]->am == PC_MG_ADDITIVE) {
279*f3fbd535SBarry Smith     ierr = PCMGACycle_Private(mglevels);CHKERRQ(ierr);
280*f3fbd535SBarry Smith   }
281*f3fbd535SBarry Smith   else if (mglevels[0]->am == PC_MG_KASKADE) {
282*f3fbd535SBarry Smith     ierr = PCMGKCycle_Private(mglevels);CHKERRQ(ierr);
283*f3fbd535SBarry Smith   }
284*f3fbd535SBarry Smith   else {
285*f3fbd535SBarry Smith     ierr = PCMGFCycle_Private(pc,mglevels);CHKERRQ(ierr);
286*f3fbd535SBarry Smith   }
287*f3fbd535SBarry Smith   PetscFunctionReturn(0);
288*f3fbd535SBarry Smith }
289*f3fbd535SBarry Smith 
290*f3fbd535SBarry Smith 
291*f3fbd535SBarry Smith #undef __FUNCT__
292*f3fbd535SBarry Smith #define __FUNCT__ "PCSetFromOptions_MG"
293*f3fbd535SBarry Smith PetscErrorCode PCSetFromOptions_MG(PC pc)
294*f3fbd535SBarry Smith {
295*f3fbd535SBarry Smith   PetscErrorCode ierr;
296*f3fbd535SBarry Smith   PetscInt       m,levels = 1,cycles;
297*f3fbd535SBarry Smith   PetscTruth     flg;
298*f3fbd535SBarry Smith   PC_MG          *mg = (PC_MG*)pc->data;
299*f3fbd535SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
300*f3fbd535SBarry Smith   PCMGType       mgtype;
301*f3fbd535SBarry Smith   PCMGCycleType  mgctype;
302*f3fbd535SBarry Smith 
303*f3fbd535SBarry Smith   PetscFunctionBegin;
304*f3fbd535SBarry Smith   ierr = PetscOptionsHead("Multigrid options");CHKERRQ(ierr);
305*f3fbd535SBarry Smith     if (!pc->data) {
306*f3fbd535SBarry Smith       ierr = PetscOptionsInt("-pc_mg_levels","Number of Levels","PCMGSetLevels",levels,&levels,&flg);CHKERRQ(ierr);
307*f3fbd535SBarry Smith       ierr = PCMGSetLevels(pc,levels,PETSC_NULL);CHKERRQ(ierr);
308*f3fbd535SBarry Smith       mglevels = mg->levels;
309*f3fbd535SBarry Smith     }
310*f3fbd535SBarry Smith     mgctype = (PCMGCycleType) mglevels[0]->cycles;
311*f3fbd535SBarry Smith     ierr = PetscOptionsEnum("-pc_mg_cycle_type","V cycle or for W-cycle","PCMGSetCycleType",PCMGCycleTypes,(PetscEnum)mgctype,(PetscEnum*)&mgctype,&flg);CHKERRQ(ierr);
312*f3fbd535SBarry Smith     if (flg) {
313*f3fbd535SBarry Smith       ierr = PCMGSetCycleType(pc,mgctype);CHKERRQ(ierr);
314*f3fbd535SBarry Smith     };
315*f3fbd535SBarry Smith     flg  = PETSC_FALSE;
316*f3fbd535SBarry Smith     ierr = PetscOptionsTruth("-pc_mg_galerkin","Use Galerkin process to compute coarser operators","PCMGSetGalerkin",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
317*f3fbd535SBarry Smith     if (flg) {
318*f3fbd535SBarry Smith       ierr = PCMGSetGalerkin(pc);CHKERRQ(ierr);
319*f3fbd535SBarry Smith     }
320*f3fbd535SBarry Smith     ierr = PetscOptionsInt("-pc_mg_smoothup","Number of post-smoothing steps","PCMGSetNumberSmoothUp",1,&m,&flg);CHKERRQ(ierr);
321*f3fbd535SBarry Smith     if (flg) {
322*f3fbd535SBarry Smith       ierr = PCMGSetNumberSmoothUp(pc,m);CHKERRQ(ierr);
323*f3fbd535SBarry Smith     }
324*f3fbd535SBarry Smith     ierr = PetscOptionsInt("-pc_mg_smoothdown","Number of pre-smoothing steps","PCMGSetNumberSmoothDown",1,&m,&flg);CHKERRQ(ierr);
325*f3fbd535SBarry Smith     if (flg) {
326*f3fbd535SBarry Smith       ierr = PCMGSetNumberSmoothDown(pc,m);CHKERRQ(ierr);
327*f3fbd535SBarry Smith     }
328*f3fbd535SBarry Smith     mgtype = mglevels[0]->am;
329*f3fbd535SBarry Smith     ierr = PetscOptionsEnum("-pc_mg_type","Multigrid type","PCMGSetType",PCMGTypes,(PetscEnum)mgtype,(PetscEnum*)&mgtype,&flg);CHKERRQ(ierr);
330*f3fbd535SBarry Smith     if (flg) {
331*f3fbd535SBarry Smith       ierr = PCMGSetType(pc,mgtype);CHKERRQ(ierr);
332*f3fbd535SBarry Smith     }
333*f3fbd535SBarry Smith     if (mglevels[0]->am == PC_MG_MULTIPLICATIVE) {
334*f3fbd535SBarry Smith       ierr = PetscOptionsInt("-pc_mg_multiplicative_cycles","Number of cycles for each preconditioner step","PCMGSetLevels",mglevels[0]->cyclesperpcapply,&cycles,&flg);CHKERRQ(ierr);
335*f3fbd535SBarry Smith       if (flg) {
336*f3fbd535SBarry Smith 	ierr = PCMGMultiplicativeSetCycles(pc,cycles);CHKERRQ(ierr);
337*f3fbd535SBarry Smith       }
338*f3fbd535SBarry Smith     }
339*f3fbd535SBarry Smith     flg  = PETSC_FALSE;
340*f3fbd535SBarry Smith     ierr = PetscOptionsTruth("-pc_mg_log","Log times for each multigrid level","None",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
341*f3fbd535SBarry Smith     if (flg) {
342*f3fbd535SBarry Smith       PetscInt i;
343*f3fbd535SBarry Smith       char     eventname[128];
344*f3fbd535SBarry Smith       if (!mg) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
345*f3fbd535SBarry Smith       levels = mglevels[0]->levels;
346*f3fbd535SBarry Smith       for (i=0; i<levels; i++) {
347*f3fbd535SBarry Smith         sprintf(eventname,"MGSetup Level %d",(int)i);
348*f3fbd535SBarry Smith         ierr = PetscLogEventRegister(eventname,((PetscObject)pc)->cookie,&mglevels[i]->eventsmoothsetup);CHKERRQ(ierr);
349*f3fbd535SBarry Smith         sprintf(eventname,"MGSmooth Level %d",(int)i);
350*f3fbd535SBarry Smith         ierr = PetscLogEventRegister(eventname,((PetscObject)pc)->cookie,&mglevels[i]->eventsmoothsolve);CHKERRQ(ierr);
351*f3fbd535SBarry Smith         if (i) {
352*f3fbd535SBarry Smith           sprintf(eventname,"MGResid Level %d",(int)i);
353*f3fbd535SBarry Smith           ierr = PetscLogEventRegister(eventname,((PetscObject)pc)->cookie,&mglevels[i]->eventresidual);CHKERRQ(ierr);
354*f3fbd535SBarry Smith           sprintf(eventname,"MGInterp Level %d",(int)i);
355*f3fbd535SBarry Smith           ierr = PetscLogEventRegister(eventname,((PetscObject)pc)->cookie,&mglevels[i]->eventinterprestrict);CHKERRQ(ierr);
356*f3fbd535SBarry Smith         }
357*f3fbd535SBarry Smith       }
358*f3fbd535SBarry Smith     }
359*f3fbd535SBarry Smith   ierr = PetscOptionsTail();CHKERRQ(ierr);
360*f3fbd535SBarry Smith   PetscFunctionReturn(0);
361*f3fbd535SBarry Smith }
362*f3fbd535SBarry Smith 
363*f3fbd535SBarry Smith const char *PCMGTypes[] = {"MULTIPLICATIVE","ADDITIVE","FULL","KASKADE","PCMGType","PC_MG",0};
364*f3fbd535SBarry Smith const char *PCMGCycleTypes[] = {"invalid","v","w","PCMGCycleType","PC_MG_CYCLE",0};
365*f3fbd535SBarry Smith 
366*f3fbd535SBarry Smith #undef __FUNCT__
367*f3fbd535SBarry Smith #define __FUNCT__ "PCView_MG"
368*f3fbd535SBarry Smith PetscErrorCode PCView_MG(PC pc,PetscViewer viewer)
369*f3fbd535SBarry Smith {
370*f3fbd535SBarry Smith   PC_MG          *mg = (PC_MG*)pc->data;
371*f3fbd535SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
372*f3fbd535SBarry Smith   PetscErrorCode ierr;
373*f3fbd535SBarry Smith   PetscInt       levels = mglevels[0]->levels,i;
374*f3fbd535SBarry Smith   PetscTruth     iascii;
375*f3fbd535SBarry Smith 
376*f3fbd535SBarry Smith   PetscFunctionBegin;
377*f3fbd535SBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
378*f3fbd535SBarry Smith   if (iascii) {
379*f3fbd535SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  MG: type is %s, levels=%D cycles=%s\n", PCMGTypes[mglevels[0]->am],levels,(mglevels[0]->cycles == PC_MG_CYCLE_V) ? "v" : "w");CHKERRQ(ierr);
380*f3fbd535SBarry Smith     if (mglevels[0]->am == PC_MG_MULTIPLICATIVE) {
381*f3fbd535SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"    Cycles per PCApply=%d\n",mglevels[0]->cyclesperpcapply);CHKERRQ(ierr);
382*f3fbd535SBarry Smith     }
383*f3fbd535SBarry Smith     if (mglevels[0]->galerkin) {
384*f3fbd535SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"    Using Galerkin computed coarse grid matrices\n");CHKERRQ(ierr);
385*f3fbd535SBarry Smith     }
386*f3fbd535SBarry Smith     for (i=0; i<levels; i++) {
387*f3fbd535SBarry Smith       if (!i) {
388*f3fbd535SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"Coarse grid solver -- level %D presmooths=%D postsmooths=%D -----\n",i,mglevels[0]->default_smoothd,mglevels[0]->default_smoothu);CHKERRQ(ierr);
389*f3fbd535SBarry Smith       } else {
390*f3fbd535SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"Down solver (pre-smoother) on level %D smooths=%D --------------------\n",i,mglevels[i]->default_smoothd);CHKERRQ(ierr);
391*f3fbd535SBarry Smith       }
392*f3fbd535SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
393*f3fbd535SBarry Smith       ierr = KSPView(mglevels[i]->smoothd,viewer);CHKERRQ(ierr);
394*f3fbd535SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
395*f3fbd535SBarry Smith       if (i && mglevels[i]->smoothd == mglevels[i]->smoothu) {
396*f3fbd535SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"Up solver (post-smoother) same as down solver (pre-smoother)\n");CHKERRQ(ierr);
397*f3fbd535SBarry Smith       } else if (i){
398*f3fbd535SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"Up solver (post-smoother) on level %D smooths=%D --------------------\n",i,mglevels[i]->default_smoothu);CHKERRQ(ierr);
399*f3fbd535SBarry Smith         ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
400*f3fbd535SBarry Smith         ierr = KSPView(mglevels[i]->smoothu,viewer);CHKERRQ(ierr);
401*f3fbd535SBarry Smith         ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
402*f3fbd535SBarry Smith       }
403*f3fbd535SBarry Smith     }
404*f3fbd535SBarry Smith   } else {
405*f3fbd535SBarry Smith     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported for PCMG",((PetscObject)viewer)->type_name);
406*f3fbd535SBarry Smith   }
407*f3fbd535SBarry Smith   PetscFunctionReturn(0);
408*f3fbd535SBarry Smith }
409*f3fbd535SBarry Smith 
410*f3fbd535SBarry Smith /*
411*f3fbd535SBarry Smith     Calls setup for the KSP on each level
412*f3fbd535SBarry Smith */
413*f3fbd535SBarry Smith #undef __FUNCT__
414*f3fbd535SBarry Smith #define __FUNCT__ "PCSetUp_MG"
415*f3fbd535SBarry Smith PetscErrorCode PCSetUp_MG(PC pc)
416*f3fbd535SBarry Smith {
417*f3fbd535SBarry Smith   PC_MG                   *mg = (PC_MG*)pc->data;
418*f3fbd535SBarry Smith   PC_MG_Levels            **mglevels = mg->levels;
419*f3fbd535SBarry Smith   PetscErrorCode          ierr;
420*f3fbd535SBarry Smith   PetscInt                i,n = mglevels[0]->levels;
421*f3fbd535SBarry Smith   PC                      cpc,mpc;
422*f3fbd535SBarry Smith   PetscTruth              preonly,lu,redundant,cholesky,monitor = PETSC_FALSE,dump = PETSC_FALSE,opsset;
423*f3fbd535SBarry Smith   PetscViewerASCIIMonitor ascii;
424*f3fbd535SBarry Smith   PetscViewer             viewer = PETSC_NULL;
425*f3fbd535SBarry Smith   MPI_Comm                comm;
426*f3fbd535SBarry Smith   Mat                     dA,dB;
427*f3fbd535SBarry Smith   MatStructure            uflag;
428*f3fbd535SBarry Smith   Vec                     tvec;
429*f3fbd535SBarry Smith 
430*f3fbd535SBarry Smith   PetscFunctionBegin;
431*f3fbd535SBarry Smith 
432*f3fbd535SBarry Smith   /* If user did not provide fine grid operators OR operator was not updated since last global KSPSetOperators() */
433*f3fbd535SBarry Smith   /* so use those from global PC */
434*f3fbd535SBarry Smith   /* Is this what we always want? What if user wants to keep old one? */
435*f3fbd535SBarry Smith   ierr = KSPGetOperatorsSet(mglevels[n-1]->smoothd,PETSC_NULL,&opsset);CHKERRQ(ierr);
436*f3fbd535SBarry Smith   ierr = KSPGetPC(mglevels[0]->smoothd,&cpc);CHKERRQ(ierr);
437*f3fbd535SBarry Smith   ierr = KSPGetPC(mglevels[n-1]->smoothd,&mpc);CHKERRQ(ierr);
438*f3fbd535SBarry Smith   if (!opsset || ((cpc->setupcalled == 1) && (mpc->setupcalled == 2))) {
439*f3fbd535SBarry 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);
440*f3fbd535SBarry Smith     ierr = KSPSetOperators(mglevels[n-1]->smoothd,pc->mat,pc->pmat,pc->flag);CHKERRQ(ierr);
441*f3fbd535SBarry Smith   }
442*f3fbd535SBarry Smith 
443*f3fbd535SBarry Smith   if (mglevels[0]->galerkin) {
444*f3fbd535SBarry Smith     Mat B;
445*f3fbd535SBarry Smith     mglevels[0]->galerkinused = PETSC_TRUE;
446*f3fbd535SBarry Smith     /* currently only handle case where mat and pmat are the same on coarser levels */
447*f3fbd535SBarry Smith     ierr = KSPGetOperators(mglevels[n-1]->smoothd,&dA,&dB,&uflag);CHKERRQ(ierr);
448*f3fbd535SBarry Smith     if (!pc->setupcalled) {
449*f3fbd535SBarry Smith       for (i=n-2; i>-1; i--) {
450*f3fbd535SBarry Smith         ierr = MatPtAP(dB,mglevels[i+1]->interpolate,MAT_INITIAL_MATRIX,1.0,&B);CHKERRQ(ierr);
451*f3fbd535SBarry Smith         ierr = KSPSetOperators(mglevels[i]->smoothd,B,B,uflag);CHKERRQ(ierr);
452*f3fbd535SBarry Smith 	if (i != n-2) {ierr = PetscObjectDereference((PetscObject)dB);CHKERRQ(ierr);}
453*f3fbd535SBarry Smith         dB   = B;
454*f3fbd535SBarry Smith       }
455*f3fbd535SBarry Smith       ierr = PetscObjectDereference((PetscObject)dB);CHKERRQ(ierr);
456*f3fbd535SBarry Smith     } else {
457*f3fbd535SBarry Smith       for (i=n-2; i>-1; i--) {
458*f3fbd535SBarry Smith         ierr = KSPGetOperators(mglevels[i]->smoothd,PETSC_NULL,&B,PETSC_NULL);CHKERRQ(ierr);
459*f3fbd535SBarry Smith         ierr = MatPtAP(dB,mglevels[i+1]->interpolate,MAT_REUSE_MATRIX,1.0,&B);CHKERRQ(ierr);
460*f3fbd535SBarry Smith         ierr = KSPSetOperators(mglevels[i]->smoothd,B,B,uflag);CHKERRQ(ierr);
461*f3fbd535SBarry Smith         dB   = B;
462*f3fbd535SBarry Smith       }
463*f3fbd535SBarry Smith     }
464*f3fbd535SBarry Smith   }
465*f3fbd535SBarry Smith 
466*f3fbd535SBarry Smith   if (!pc->setupcalled) {
467*f3fbd535SBarry Smith     ierr = PetscOptionsGetTruth(0,"-pc_mg_monitor",&monitor,PETSC_NULL);CHKERRQ(ierr);
468*f3fbd535SBarry Smith 
469*f3fbd535SBarry Smith     for (i=0; i<n; i++) {
470*f3fbd535SBarry Smith       if (monitor) {
471*f3fbd535SBarry Smith         ierr = PetscObjectGetComm((PetscObject)mglevels[i]->smoothd,&comm);CHKERRQ(ierr);
472*f3fbd535SBarry Smith         ierr = PetscViewerASCIIMonitorCreate(comm,"stdout",n-i,&ascii);CHKERRQ(ierr);
473*f3fbd535SBarry Smith         ierr = KSPMonitorSet(mglevels[i]->smoothd,KSPMonitorDefault,ascii,(PetscErrorCode(*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
474*f3fbd535SBarry Smith       }
475*f3fbd535SBarry Smith       ierr = KSPSetFromOptions(mglevels[i]->smoothd);CHKERRQ(ierr);
476*f3fbd535SBarry Smith     }
477*f3fbd535SBarry Smith     for (i=1; i<n; i++) {
478*f3fbd535SBarry Smith       if (mglevels[i]->smoothu && (mglevels[i]->smoothu != mglevels[i]->smoothd)) {
479*f3fbd535SBarry Smith         if (monitor) {
480*f3fbd535SBarry Smith           ierr = PetscObjectGetComm((PetscObject)mglevels[i]->smoothu,&comm);CHKERRQ(ierr);
481*f3fbd535SBarry Smith           ierr = PetscViewerASCIIMonitorCreate(comm,"stdout",n-i,&ascii);CHKERRQ(ierr);
482*f3fbd535SBarry Smith           ierr = KSPMonitorSet(mglevels[i]->smoothu,KSPMonitorDefault,ascii,(PetscErrorCode(*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
483*f3fbd535SBarry Smith         }
484*f3fbd535SBarry Smith         ierr = KSPSetFromOptions(mglevels[i]->smoothu);CHKERRQ(ierr);
485*f3fbd535SBarry Smith       }
486*f3fbd535SBarry Smith     }
487*f3fbd535SBarry Smith     for (i=1; i<n; i++) {
488*f3fbd535SBarry Smith       if (!mglevels[i]->residual) {
489*f3fbd535SBarry Smith         Mat mat;
490*f3fbd535SBarry Smith         ierr = KSPGetOperators(mglevels[i]->smoothd,PETSC_NULL,&mat,PETSC_NULL);CHKERRQ(ierr);
491*f3fbd535SBarry Smith         ierr = PCMGSetResidual(pc,i,PCMGDefaultResidual,mat);CHKERRQ(ierr);
492*f3fbd535SBarry Smith       }
493*f3fbd535SBarry Smith       if (mglevels[i]->restrct && !mglevels[i]->interpolate) {
494*f3fbd535SBarry Smith         ierr = PCMGSetInterpolation(pc,i,mglevels[i]->restrct);CHKERRQ(ierr);
495*f3fbd535SBarry Smith       }
496*f3fbd535SBarry Smith       if (!mglevels[i]->restrct && mglevels[i]->interpolate) {
497*f3fbd535SBarry Smith         ierr = PCMGSetRestriction(pc,i,mglevels[i]->interpolate);CHKERRQ(ierr);
498*f3fbd535SBarry Smith       }
499*f3fbd535SBarry Smith #if defined(PETSC_USE_DEBUG)
500*f3fbd535SBarry Smith       if (!mglevels[i]->restrct || !mglevels[i]->interpolate) {
501*f3fbd535SBarry Smith         SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Need to set restriction or interpolation on level %d",(int)i);
502*f3fbd535SBarry Smith       }
503*f3fbd535SBarry Smith #endif
504*f3fbd535SBarry Smith     }
505*f3fbd535SBarry Smith     for (i=0; i<n-1; i++) {
506*f3fbd535SBarry Smith       if (!mglevels[i]->b) {
507*f3fbd535SBarry Smith         Vec *vec;
508*f3fbd535SBarry Smith         ierr = KSPGetVecs(mglevels[i]->smoothd,1,&vec,0,PETSC_NULL);CHKERRQ(ierr);
509*f3fbd535SBarry Smith         ierr = PCMGSetRhs(pc,i,*vec);CHKERRQ(ierr);
510*f3fbd535SBarry Smith         ierr = VecDestroy(*vec);CHKERRQ(ierr);
511*f3fbd535SBarry Smith         ierr = PetscFree(vec);CHKERRQ(ierr);
512*f3fbd535SBarry Smith       }
513*f3fbd535SBarry Smith       if (!mglevels[i]->r && i) {
514*f3fbd535SBarry Smith         ierr = VecDuplicate(mglevels[i]->b,&tvec);CHKERRQ(ierr);
515*f3fbd535SBarry Smith         ierr = PCMGSetR(pc,i,tvec);CHKERRQ(ierr);
516*f3fbd535SBarry Smith         ierr = VecDestroy(tvec);CHKERRQ(ierr);
517*f3fbd535SBarry Smith       }
518*f3fbd535SBarry Smith       if (!mglevels[i]->x) {
519*f3fbd535SBarry Smith         ierr = VecDuplicate(mglevels[i]->b,&tvec);CHKERRQ(ierr);
520*f3fbd535SBarry Smith         ierr = PCMGSetX(pc,i,tvec);CHKERRQ(ierr);
521*f3fbd535SBarry Smith         ierr = VecDestroy(tvec);CHKERRQ(ierr);
522*f3fbd535SBarry Smith       }
523*f3fbd535SBarry Smith     }
524*f3fbd535SBarry Smith     if (n != 1 && !mglevels[n-1]->r) {
525*f3fbd535SBarry Smith       /* PCMGSetR() on the finest level if user did not supply it */
526*f3fbd535SBarry Smith       Vec *vec;
527*f3fbd535SBarry Smith       ierr = KSPGetVecs(mglevels[n-1]->smoothd,1,&vec,0,PETSC_NULL);CHKERRQ(ierr);
528*f3fbd535SBarry Smith       ierr = PCMGSetR(pc,n-1,*vec);CHKERRQ(ierr);
529*f3fbd535SBarry Smith       ierr = VecDestroy(*vec);CHKERRQ(ierr);
530*f3fbd535SBarry Smith       ierr = PetscFree(vec);CHKERRQ(ierr);
531*f3fbd535SBarry Smith     }
532*f3fbd535SBarry Smith   }
533*f3fbd535SBarry Smith 
534*f3fbd535SBarry Smith 
535*f3fbd535SBarry Smith   for (i=1; i<n; i++) {
536*f3fbd535SBarry Smith     if (mglevels[i]->smoothu == mglevels[i]->smoothd) {
537*f3fbd535SBarry Smith       /* if doing only down then initial guess is zero */
538*f3fbd535SBarry Smith       ierr = KSPSetInitialGuessNonzero(mglevels[i]->smoothd,PETSC_TRUE);CHKERRQ(ierr);
539*f3fbd535SBarry Smith     }
540*f3fbd535SBarry Smith     if (mglevels[i]->eventsmoothsetup) {ierr = PetscLogEventBegin(mglevels[i]->eventsmoothsetup,0,0,0,0);CHKERRQ(ierr);}
541*f3fbd535SBarry Smith     ierr = KSPSetUp(mglevels[i]->smoothd);CHKERRQ(ierr);
542*f3fbd535SBarry Smith     if (mglevels[i]->eventsmoothsetup) {ierr = PetscLogEventEnd(mglevels[i]->eventsmoothsetup,0,0,0,0);CHKERRQ(ierr);}
543*f3fbd535SBarry Smith   }
544*f3fbd535SBarry Smith   for (i=1; i<n; i++) {
545*f3fbd535SBarry Smith     if (mglevels[i]->smoothu && mglevels[i]->smoothu != mglevels[i]->smoothd) {
546*f3fbd535SBarry Smith       Mat          downmat,downpmat;
547*f3fbd535SBarry Smith       MatStructure matflag;
548*f3fbd535SBarry Smith       PetscTruth   opsset;
549*f3fbd535SBarry Smith 
550*f3fbd535SBarry Smith       /* check if operators have been set for up, if not use down operators to set them */
551*f3fbd535SBarry Smith       ierr = KSPGetOperatorsSet(mglevels[i]->smoothu,&opsset,PETSC_NULL);CHKERRQ(ierr);
552*f3fbd535SBarry Smith       if (!opsset) {
553*f3fbd535SBarry Smith         ierr = KSPGetOperators(mglevels[i]->smoothd,&downmat,&downpmat,&matflag);CHKERRQ(ierr);
554*f3fbd535SBarry Smith         ierr = KSPSetOperators(mglevels[i]->smoothu,downmat,downpmat,matflag);CHKERRQ(ierr);
555*f3fbd535SBarry Smith       }
556*f3fbd535SBarry Smith 
557*f3fbd535SBarry Smith       ierr = KSPSetInitialGuessNonzero(mglevels[i]->smoothu,PETSC_TRUE);CHKERRQ(ierr);
558*f3fbd535SBarry Smith       if (mglevels[i]->eventsmoothsetup) {ierr = PetscLogEventBegin(mglevels[i]->eventsmoothsetup,0,0,0,0);CHKERRQ(ierr);}
559*f3fbd535SBarry Smith       ierr = KSPSetUp(mglevels[i]->smoothu);CHKERRQ(ierr);
560*f3fbd535SBarry Smith       if (mglevels[i]->eventsmoothsetup) {ierr = PetscLogEventEnd(mglevels[i]->eventsmoothsetup,0,0,0,0);CHKERRQ(ierr);}
561*f3fbd535SBarry Smith     }
562*f3fbd535SBarry Smith   }
563*f3fbd535SBarry Smith 
564*f3fbd535SBarry Smith   /*
565*f3fbd535SBarry Smith       If coarse solver is not direct method then DO NOT USE preonly
566*f3fbd535SBarry Smith   */
567*f3fbd535SBarry Smith   ierr = PetscTypeCompare((PetscObject)mglevels[0]->smoothd,KSPPREONLY,&preonly);CHKERRQ(ierr);
568*f3fbd535SBarry Smith   if (preonly) {
569*f3fbd535SBarry Smith     ierr = PetscTypeCompare((PetscObject)cpc,PCLU,&lu);CHKERRQ(ierr);
570*f3fbd535SBarry Smith     ierr = PetscTypeCompare((PetscObject)cpc,PCREDUNDANT,&redundant);CHKERRQ(ierr);
571*f3fbd535SBarry Smith     ierr = PetscTypeCompare((PetscObject)cpc,PCCHOLESKY,&cholesky);CHKERRQ(ierr);
572*f3fbd535SBarry Smith     if (!lu && !redundant && !cholesky) {
573*f3fbd535SBarry Smith       ierr = KSPSetType(mglevels[0]->smoothd,KSPGMRES);CHKERRQ(ierr);
574*f3fbd535SBarry Smith     }
575*f3fbd535SBarry Smith   }
576*f3fbd535SBarry Smith 
577*f3fbd535SBarry Smith   if (!pc->setupcalled) {
578*f3fbd535SBarry Smith     if (monitor) {
579*f3fbd535SBarry Smith       ierr = PetscObjectGetComm((PetscObject)mglevels[0]->smoothd,&comm);CHKERRQ(ierr);
580*f3fbd535SBarry Smith       ierr = PetscViewerASCIIMonitorCreate(comm,"stdout",n,&ascii);CHKERRQ(ierr);
581*f3fbd535SBarry Smith       ierr = KSPMonitorSet(mglevels[0]->smoothd,KSPMonitorDefault,ascii,(PetscErrorCode(*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
582*f3fbd535SBarry Smith     }
583*f3fbd535SBarry Smith     ierr = KSPSetFromOptions(mglevels[0]->smoothd);CHKERRQ(ierr);
584*f3fbd535SBarry Smith   }
585*f3fbd535SBarry Smith 
586*f3fbd535SBarry Smith   if (mglevels[0]->eventsmoothsetup) {ierr = PetscLogEventBegin(mglevels[0]->eventsmoothsetup,0,0,0,0);CHKERRQ(ierr);}
587*f3fbd535SBarry Smith   ierr = KSPSetUp(mglevels[0]->smoothd);CHKERRQ(ierr);
588*f3fbd535SBarry Smith   if (mglevels[0]->eventsmoothsetup) {ierr = PetscLogEventEnd(mglevels[0]->eventsmoothsetup,0,0,0,0);CHKERRQ(ierr);}
589*f3fbd535SBarry Smith 
590*f3fbd535SBarry Smith   /*
591*f3fbd535SBarry Smith      Dump the interpolation/restriction matrices plus the
592*f3fbd535SBarry Smith    Jacobian/stiffness on each level. This allows Matlab users to
593*f3fbd535SBarry Smith    easily check if the Galerkin condition A_c = R A_f R^T is satisfied.
594*f3fbd535SBarry Smith 
595*f3fbd535SBarry Smith    Only support one or the other at the same time.
596*f3fbd535SBarry Smith   */
597*f3fbd535SBarry Smith #if defined(PETSC_USE_SOCKET_VIEWER)
598*f3fbd535SBarry Smith   ierr = PetscOptionsGetTruth(((PetscObject)pc)->prefix,"-pc_mg_dump_matlab",&dump,PETSC_NULL);CHKERRQ(ierr);
599*f3fbd535SBarry Smith   if (dump) {
600*f3fbd535SBarry Smith     viewer = PETSC_VIEWER_SOCKET_(((PetscObject)pc)->comm);
601*f3fbd535SBarry Smith   }
602*f3fbd535SBarry Smith   dump = PETSC_FALSE;
603*f3fbd535SBarry Smith #endif
604*f3fbd535SBarry Smith   ierr = PetscOptionsGetTruth(((PetscObject)pc)->prefix,"-pc_mg_dump_binary",&dump,PETSC_NULL);CHKERRQ(ierr);
605*f3fbd535SBarry Smith   if (dump) {
606*f3fbd535SBarry Smith     viewer = PETSC_VIEWER_BINARY_(((PetscObject)pc)->comm);
607*f3fbd535SBarry Smith   }
608*f3fbd535SBarry Smith 
609*f3fbd535SBarry Smith   if (viewer) {
610*f3fbd535SBarry Smith     for (i=1; i<n; i++) {
611*f3fbd535SBarry Smith       ierr = MatView(mglevels[i]->restrct,viewer);CHKERRQ(ierr);
612*f3fbd535SBarry Smith     }
613*f3fbd535SBarry Smith     for (i=0; i<n; i++) {
614*f3fbd535SBarry Smith       ierr = KSPGetPC(mglevels[i]->smoothd,&pc);CHKERRQ(ierr);
615*f3fbd535SBarry Smith       ierr = MatView(pc->mat,viewer);CHKERRQ(ierr);
616*f3fbd535SBarry Smith     }
617*f3fbd535SBarry Smith   }
618*f3fbd535SBarry Smith   PetscFunctionReturn(0);
619*f3fbd535SBarry Smith }
620*f3fbd535SBarry Smith 
621*f3fbd535SBarry Smith /* -------------------------------------------------------------------------------------*/
622*f3fbd535SBarry Smith 
623*f3fbd535SBarry Smith #undef __FUNCT__
6249dcbbd2bSBarry Smith #define __FUNCT__ "PCMGGetLevels"
6254b9ad928SBarry Smith /*@
62697177400SBarry Smith    PCMGGetLevels - Gets the number of levels to use with MG.
6274b9ad928SBarry Smith 
6284b9ad928SBarry Smith    Not Collective
6294b9ad928SBarry Smith 
6304b9ad928SBarry Smith    Input Parameter:
6314b9ad928SBarry Smith .  pc - the preconditioner context
6324b9ad928SBarry Smith 
6334b9ad928SBarry Smith    Output parameter:
6344b9ad928SBarry Smith .  levels - the number of levels
6354b9ad928SBarry Smith 
6364b9ad928SBarry Smith    Level: advanced
6374b9ad928SBarry Smith 
6384b9ad928SBarry Smith .keywords: MG, get, levels, multigrid
6394b9ad928SBarry Smith 
64097177400SBarry Smith .seealso: PCMGSetLevels()
6414b9ad928SBarry Smith @*/
64297177400SBarry Smith PetscErrorCode PETSCKSP_DLLEXPORT PCMGGetLevels(PC pc,PetscInt *levels)
6434b9ad928SBarry Smith {
644*f3fbd535SBarry Smith   PC_MG *mg = (PC_MG*)pc->data;
6454b9ad928SBarry Smith 
6464b9ad928SBarry Smith   PetscFunctionBegin;
6474482741eSBarry Smith   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
6484482741eSBarry Smith   PetscValidIntPointer(levels,2);
649*f3fbd535SBarry Smith   *levels = mg->nlevels;
6504b9ad928SBarry Smith   PetscFunctionReturn(0);
6514b9ad928SBarry Smith }
6524b9ad928SBarry Smith 
6534b9ad928SBarry Smith #undef __FUNCT__
6549dcbbd2bSBarry Smith #define __FUNCT__ "PCMGSetType"
6554b9ad928SBarry Smith /*@
65697177400SBarry Smith    PCMGSetType - Determines the form of multigrid to use:
6574b9ad928SBarry Smith    multiplicative, additive, full, or the Kaskade algorithm.
6584b9ad928SBarry Smith 
6594b9ad928SBarry Smith    Collective on PC
6604b9ad928SBarry Smith 
6614b9ad928SBarry Smith    Input Parameters:
6624b9ad928SBarry Smith +  pc - the preconditioner context
6639dcbbd2bSBarry Smith -  form - multigrid form, one of PC_MG_MULTIPLICATIVE, PC_MG_ADDITIVE,
6649dcbbd2bSBarry Smith    PC_MG_FULL, PC_MG_KASKADE
6654b9ad928SBarry Smith 
6664b9ad928SBarry Smith    Options Database Key:
6674b9ad928SBarry Smith .  -pc_mg_type <form> - Sets <form>, one of multiplicative,
6684b9ad928SBarry Smith    additive, full, kaskade
6694b9ad928SBarry Smith 
6704b9ad928SBarry Smith    Level: advanced
6714b9ad928SBarry Smith 
6724b9ad928SBarry Smith .keywords: MG, set, method, multiplicative, additive, full, Kaskade, multigrid
6734b9ad928SBarry Smith 
67497177400SBarry Smith .seealso: PCMGSetLevels()
6754b9ad928SBarry Smith @*/
6769dcbbd2bSBarry Smith PetscErrorCode PETSCKSP_DLLEXPORT PCMGSetType(PC pc,PCMGType form)
6774b9ad928SBarry Smith {
678*f3fbd535SBarry Smith   PC_MG                   *mg = (PC_MG*)pc->data;
679*f3fbd535SBarry Smith   PC_MG_Levels            **mglevels = mg->levels;
6804b9ad928SBarry Smith 
6814b9ad928SBarry Smith   PetscFunctionBegin;
6824482741eSBarry Smith   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
683*f3fbd535SBarry Smith   mglevels[0]->am = form;
6849dcbbd2bSBarry Smith   if (form == PC_MG_MULTIPLICATIVE) pc->ops->applyrichardson = PCApplyRichardson_MG;
6854b9ad928SBarry Smith   else pc->ops->applyrichardson = 0;
6864b9ad928SBarry Smith   PetscFunctionReturn(0);
6874b9ad928SBarry Smith }
6884b9ad928SBarry Smith 
6894b9ad928SBarry Smith #undef __FUNCT__
6900d353602SBarry Smith #define __FUNCT__ "PCMGSetCycleType"
6914b9ad928SBarry Smith /*@
6920d353602SBarry Smith    PCMGSetCycleType - Sets the type cycles to use.  Use PCMGSetCycleTypeOnLevel() for more
6934b9ad928SBarry Smith    complicated cycling.
6944b9ad928SBarry Smith 
6954b9ad928SBarry Smith    Collective on PC
6964b9ad928SBarry Smith 
6974b9ad928SBarry Smith    Input Parameters:
698c2be2410SBarry Smith +  pc - the multigrid context
6990d353602SBarry Smith -  PC_MG_CYCLE_V or PC_MG_CYCLE_W
7004b9ad928SBarry Smith 
7014b9ad928SBarry Smith    Options Database Key:
7020d353602SBarry Smith $  -pc_mg_cycle_type v or w
7034b9ad928SBarry Smith 
7044b9ad928SBarry Smith    Level: advanced
7054b9ad928SBarry Smith 
7064b9ad928SBarry Smith .keywords: MG, set, cycles, V-cycle, W-cycle, multigrid
7074b9ad928SBarry Smith 
7080d353602SBarry Smith .seealso: PCMGSetCycleTypeOnLevel()
7094b9ad928SBarry Smith @*/
7100d353602SBarry Smith PetscErrorCode PETSCKSP_DLLEXPORT PCMGSetCycleType(PC pc,PCMGCycleType n)
7114b9ad928SBarry Smith {
712*f3fbd535SBarry Smith   PC_MG        *mg = (PC_MG*)pc->data;
713*f3fbd535SBarry Smith   PC_MG_Levels **mglevels = mg->levels;
71479416396SBarry Smith   PetscInt     i,levels;
7154b9ad928SBarry Smith 
7164b9ad928SBarry Smith   PetscFunctionBegin;
7174482741eSBarry Smith   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
718*f3fbd535SBarry Smith   if (!mglevels) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
719*f3fbd535SBarry Smith   levels = mglevels[0]->levels;
7204b9ad928SBarry Smith 
7214b9ad928SBarry Smith   for (i=0; i<levels; i++) {
722*f3fbd535SBarry Smith     mglevels[i]->cycles  = n;
7234b9ad928SBarry Smith   }
7244b9ad928SBarry Smith   PetscFunctionReturn(0);
7254b9ad928SBarry Smith }
7264b9ad928SBarry Smith 
7274b9ad928SBarry Smith #undef __FUNCT__
7288cc2d5dfSBarry Smith #define __FUNCT__ "PCMGMultiplicativeSetCycles"
7298cc2d5dfSBarry Smith /*@
7308cc2d5dfSBarry Smith    PCMGMultiplicativeSetCycles - Sets the number of cycles to use for each preconditioner step
7318cc2d5dfSBarry Smith          of multigrid when PCMGType of PC_MG_MULTIPLICATIVE is used
7328cc2d5dfSBarry Smith 
7338cc2d5dfSBarry Smith    Collective on PC
7348cc2d5dfSBarry Smith 
7358cc2d5dfSBarry Smith    Input Parameters:
7368cc2d5dfSBarry Smith +  pc - the multigrid context
7378cc2d5dfSBarry Smith -  n - number of cycles (default is 1)
7388cc2d5dfSBarry Smith 
7398cc2d5dfSBarry Smith    Options Database Key:
7408cc2d5dfSBarry Smith $  -pc_mg_multiplicative_cycles n
7418cc2d5dfSBarry Smith 
7428cc2d5dfSBarry Smith    Level: advanced
7438cc2d5dfSBarry Smith 
7448cc2d5dfSBarry Smith    Notes: This is not associated with setting a v or w cycle, that is set with PCMGSetCycleType()
7458cc2d5dfSBarry Smith 
7468cc2d5dfSBarry Smith .keywords: MG, set, cycles, V-cycle, W-cycle, multigrid
7478cc2d5dfSBarry Smith 
7488cc2d5dfSBarry Smith .seealso: PCMGSetCycleTypeOnLevel(), PCMGSetCycleType()
7498cc2d5dfSBarry Smith @*/
7508cc2d5dfSBarry Smith PetscErrorCode PETSCKSP_DLLEXPORT PCMGMultiplicativeSetCycles(PC pc,PetscInt n)
7518cc2d5dfSBarry Smith {
752*f3fbd535SBarry Smith   PC_MG        *mg = (PC_MG*)pc->data;
753*f3fbd535SBarry Smith   PC_MG_Levels **mglevels = mg->levels;
7548cc2d5dfSBarry Smith   PetscInt     i,levels;
7558cc2d5dfSBarry Smith 
7568cc2d5dfSBarry Smith   PetscFunctionBegin;
7578cc2d5dfSBarry Smith   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
758*f3fbd535SBarry Smith   if (!mglevels) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
759*f3fbd535SBarry Smith   levels = mglevels[0]->levels;
7608cc2d5dfSBarry Smith 
7618cc2d5dfSBarry Smith   for (i=0; i<levels; i++) {
762*f3fbd535SBarry Smith     mglevels[i]->cyclesperpcapply  = n;
7638cc2d5dfSBarry Smith   }
7648cc2d5dfSBarry Smith   PetscFunctionReturn(0);
7658cc2d5dfSBarry Smith }
7668cc2d5dfSBarry Smith 
7678cc2d5dfSBarry Smith #undef __FUNCT__
7689dcbbd2bSBarry Smith #define __FUNCT__ "PCMGSetGalerkin"
769c2be2410SBarry Smith /*@
77097177400SBarry Smith    PCMGSetGalerkin - Causes the coarser grid matrices to be computed from the
771c2be2410SBarry Smith       finest grid via the Galerkin process: A_i-1 = r_i * A_i * r_i^t
772c2be2410SBarry Smith 
773c2be2410SBarry Smith    Collective on PC
774c2be2410SBarry Smith 
775c2be2410SBarry Smith    Input Parameters:
7763fc8bf9cSBarry Smith .  pc - the multigrid context
777c2be2410SBarry Smith 
778c2be2410SBarry Smith    Options Database Key:
779c2be2410SBarry Smith $  -pc_mg_galerkin
780c2be2410SBarry Smith 
781c2be2410SBarry Smith    Level: intermediate
782c2be2410SBarry Smith 
783c2be2410SBarry Smith .keywords: MG, set, Galerkin
784c2be2410SBarry Smith 
7853fc8bf9cSBarry Smith .seealso: PCMGGetGalerkin()
7863fc8bf9cSBarry Smith 
787c2be2410SBarry Smith @*/
78897177400SBarry Smith PetscErrorCode PETSCKSP_DLLEXPORT PCMGSetGalerkin(PC pc)
789c2be2410SBarry Smith {
790*f3fbd535SBarry Smith   PC_MG        *mg = (PC_MG*)pc->data;
791*f3fbd535SBarry Smith   PC_MG_Levels **mglevels = mg->levels;
792c2be2410SBarry Smith   PetscInt     i,levels;
793c2be2410SBarry Smith 
794c2be2410SBarry Smith   PetscFunctionBegin;
795c2be2410SBarry Smith   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
796*f3fbd535SBarry Smith   if (!mglevels) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
797*f3fbd535SBarry Smith   levels = mglevels[0]->levels;
798c2be2410SBarry Smith 
799c2be2410SBarry Smith   for (i=0; i<levels; i++) {
800*f3fbd535SBarry Smith     mglevels[i]->galerkin = PETSC_TRUE;
801c2be2410SBarry Smith   }
802c2be2410SBarry Smith   PetscFunctionReturn(0);
803c2be2410SBarry Smith }
804c2be2410SBarry Smith 
805c2be2410SBarry Smith #undef __FUNCT__
8063fc8bf9cSBarry Smith #define __FUNCT__ "PCMGGetGalerkin"
8073fc8bf9cSBarry Smith /*@
8083fc8bf9cSBarry Smith    PCMGGetGalerkin - Checks if Galerkin multigrid is being used, i.e.
8093fc8bf9cSBarry Smith       A_i-1 = r_i * A_i * r_i^t
8103fc8bf9cSBarry Smith 
8113fc8bf9cSBarry Smith    Not Collective
8123fc8bf9cSBarry Smith 
8133fc8bf9cSBarry Smith    Input Parameter:
8143fc8bf9cSBarry Smith .  pc - the multigrid context
8153fc8bf9cSBarry Smith 
8163fc8bf9cSBarry Smith    Output Parameter:
8173fc8bf9cSBarry Smith .  gelerkin - PETSC_TRUE or PETSC_FALSE
8183fc8bf9cSBarry Smith 
8193fc8bf9cSBarry Smith    Options Database Key:
8203fc8bf9cSBarry Smith $  -pc_mg_galerkin
8213fc8bf9cSBarry Smith 
8223fc8bf9cSBarry Smith    Level: intermediate
8233fc8bf9cSBarry Smith 
8243fc8bf9cSBarry Smith .keywords: MG, set, Galerkin
8253fc8bf9cSBarry Smith 
8263fc8bf9cSBarry Smith .seealso: PCMGSetGalerkin()
8273fc8bf9cSBarry Smith 
8283fc8bf9cSBarry Smith @*/
8293fc8bf9cSBarry Smith PetscErrorCode PETSCKSP_DLLEXPORT PCMGGetGalerkin(PC pc,PetscTruth *galerkin)
8303fc8bf9cSBarry Smith {
831*f3fbd535SBarry Smith   PC_MG        *mg = (PC_MG*)pc->data;
832*f3fbd535SBarry Smith   PC_MG_Levels **mglevels = mg->levels;
8333fc8bf9cSBarry Smith 
8343fc8bf9cSBarry Smith   PetscFunctionBegin;
8353fc8bf9cSBarry Smith   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
836*f3fbd535SBarry Smith   if (!mglevels) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
837*f3fbd535SBarry Smith   *galerkin = mglevels[0]->galerkin;
8383fc8bf9cSBarry Smith   PetscFunctionReturn(0);
8393fc8bf9cSBarry Smith }
8403fc8bf9cSBarry Smith 
8413fc8bf9cSBarry Smith #undef __FUNCT__
8429dcbbd2bSBarry Smith #define __FUNCT__ "PCMGSetNumberSmoothDown"
8434b9ad928SBarry Smith /*@
84497177400SBarry Smith    PCMGSetNumberSmoothDown - Sets the number of pre-smoothing steps to
84597177400SBarry Smith    use on all levels. Use PCMGGetSmootherDown() to set different
8464b9ad928SBarry Smith    pre-smoothing steps on different levels.
8474b9ad928SBarry Smith 
8484b9ad928SBarry Smith    Collective on PC
8494b9ad928SBarry Smith 
8504b9ad928SBarry Smith    Input Parameters:
8514b9ad928SBarry Smith +  mg - the multigrid context
8524b9ad928SBarry Smith -  n - the number of smoothing steps
8534b9ad928SBarry Smith 
8544b9ad928SBarry Smith    Options Database Key:
8554b9ad928SBarry Smith .  -pc_mg_smoothdown <n> - Sets number of pre-smoothing steps
8564b9ad928SBarry Smith 
8574b9ad928SBarry Smith    Level: advanced
8584b9ad928SBarry Smith 
8594b9ad928SBarry Smith .keywords: MG, smooth, down, pre-smoothing, steps, multigrid
8604b9ad928SBarry Smith 
86197177400SBarry Smith .seealso: PCMGSetNumberSmoothUp()
8624b9ad928SBarry Smith @*/
86397177400SBarry Smith PetscErrorCode PETSCKSP_DLLEXPORT PCMGSetNumberSmoothDown(PC pc,PetscInt n)
8644b9ad928SBarry Smith {
865*f3fbd535SBarry Smith   PC_MG          *mg = (PC_MG*)pc->data;
866*f3fbd535SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
8676849ba73SBarry Smith   PetscErrorCode ierr;
86879416396SBarry Smith   PetscInt       i,levels;
8694b9ad928SBarry Smith 
8704b9ad928SBarry Smith   PetscFunctionBegin;
8714482741eSBarry Smith   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
872*f3fbd535SBarry Smith   if (!mglevels) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
873*f3fbd535SBarry Smith   levels = mglevels[0]->levels;
8744b9ad928SBarry Smith 
875b05257ddSBarry Smith   for (i=1; i<levels; i++) {
8764b9ad928SBarry Smith     /* make sure smoother up and down are different */
87797177400SBarry Smith     ierr = PCMGGetSmootherUp(pc,i,PETSC_NULL);CHKERRQ(ierr);
878*f3fbd535SBarry Smith     ierr = KSPSetTolerances(mglevels[i]->smoothd,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,n);CHKERRQ(ierr);
879*f3fbd535SBarry Smith     mglevels[i]->default_smoothd = n;
8804b9ad928SBarry Smith   }
8814b9ad928SBarry Smith   PetscFunctionReturn(0);
8824b9ad928SBarry Smith }
8834b9ad928SBarry Smith 
8844b9ad928SBarry Smith #undef __FUNCT__
8859dcbbd2bSBarry Smith #define __FUNCT__ "PCMGSetNumberSmoothUp"
8864b9ad928SBarry Smith /*@
88797177400SBarry Smith    PCMGSetNumberSmoothUp - Sets the number of post-smoothing steps to use
88897177400SBarry Smith    on all levels. Use PCMGGetSmootherUp() to set different numbers of
8894b9ad928SBarry Smith    post-smoothing steps on different levels.
8904b9ad928SBarry Smith 
8914b9ad928SBarry Smith    Collective on PC
8924b9ad928SBarry Smith 
8934b9ad928SBarry Smith    Input Parameters:
8944b9ad928SBarry Smith +  mg - the multigrid context
8954b9ad928SBarry Smith -  n - the number of smoothing steps
8964b9ad928SBarry Smith 
8974b9ad928SBarry Smith    Options Database Key:
8984b9ad928SBarry Smith .  -pc_mg_smoothup <n> - Sets number of post-smoothing steps
8994b9ad928SBarry Smith 
9004b9ad928SBarry Smith    Level: advanced
9014b9ad928SBarry Smith 
9024b9ad928SBarry Smith    Note: this does not set a value on the coarsest grid, since we assume that
903a8c7a070SBarry Smith     there is no separate smooth up on the coarsest grid.
9044b9ad928SBarry Smith 
9054b9ad928SBarry Smith .keywords: MG, smooth, up, post-smoothing, steps, multigrid
9064b9ad928SBarry Smith 
90797177400SBarry Smith .seealso: PCMGSetNumberSmoothDown()
9084b9ad928SBarry Smith @*/
90997177400SBarry Smith PetscErrorCode PETSCKSP_DLLEXPORT PCMGSetNumberSmoothUp(PC pc,PetscInt n)
9104b9ad928SBarry Smith {
911*f3fbd535SBarry Smith   PC_MG          *mg = (PC_MG*)pc->data;
912*f3fbd535SBarry Smith   PC_MG_Levels   **mglevels = mg->levels;
9136849ba73SBarry Smith   PetscErrorCode ierr;
91479416396SBarry Smith   PetscInt       i,levels;
9154b9ad928SBarry Smith 
9164b9ad928SBarry Smith   PetscFunctionBegin;
9174482741eSBarry Smith   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
918*f3fbd535SBarry Smith   if (!mglevels) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
919*f3fbd535SBarry Smith   levels = mglevels[0]->levels;
9204b9ad928SBarry Smith 
9214b9ad928SBarry Smith   for (i=1; i<levels; i++) {
9224b9ad928SBarry Smith     /* make sure smoother up and down are different */
92397177400SBarry Smith     ierr = PCMGGetSmootherUp(pc,i,PETSC_NULL);CHKERRQ(ierr);
924*f3fbd535SBarry Smith     ierr = KSPSetTolerances(mglevels[i]->smoothu,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,n);CHKERRQ(ierr);
925*f3fbd535SBarry Smith     mglevels[i]->default_smoothu = n;
9264b9ad928SBarry Smith   }
9274b9ad928SBarry Smith   PetscFunctionReturn(0);
9284b9ad928SBarry Smith }
9294b9ad928SBarry Smith 
9304b9ad928SBarry Smith /* ----------------------------------------------------------------------------------------*/
9314b9ad928SBarry Smith 
9323b09bd56SBarry Smith /*MC
933ccb205f8SBarry Smith    PCMG - Use multigrid preconditioning. This preconditioner requires you provide additional
9343b09bd56SBarry Smith     information about the coarser grid matrices and restriction/interpolation operators.
9353b09bd56SBarry Smith 
9363b09bd56SBarry Smith    Options Database Keys:
9373b09bd56SBarry Smith +  -pc_mg_levels <nlevels> - number of levels including finest
9380d353602SBarry Smith .  -pc_mg_cycles v or w
93979416396SBarry Smith .  -pc_mg_smoothup <n> - number of smoothing steps after interpolation
9403b09bd56SBarry Smith .  -pc_mg_smoothdown <n> - number of smoothing steps before applying restriction operator
9413b09bd56SBarry Smith .  -pc_mg_type <additive,multiplicative,full,cascade> - multiplicative is the default
9423b09bd56SBarry Smith .  -pc_mg_log - log information about time spent on each level of the solver
9433b09bd56SBarry Smith .  -pc_mg_monitor - print information on the multigrid convergence
94468eff7e6SBarry Smith .  -pc_mg_galerkin - use Galerkin process to compute coarser operators
9453b09bd56SBarry Smith -  -pc_mg_dump_matlab - dumps the matrices for each level and the restriction/interpolation matrices
9463b09bd56SBarry Smith                         to the Socket viewer for reading from Matlab.
9473b09bd56SBarry Smith 
9483b09bd56SBarry Smith    Notes:
9493b09bd56SBarry Smith 
9503b09bd56SBarry Smith    Level: intermediate
9513b09bd56SBarry Smith 
9528f87f92bSBarry Smith    Concepts: multigrid/multilevel
9533b09bd56SBarry Smith 
9543b09bd56SBarry Smith .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC, PCMGType,
9550d353602SBarry Smith            PCMGSetLevels(), PCMGGetLevels(), PCMGSetType(), PCMGSetCycleType(), PCMGSetNumberSmoothDown(),
95697177400SBarry Smith            PCMGSetNumberSmoothUp(), PCMGGetCoarseSolve(), PCMGSetResidual(), PCMGSetInterpolation(),
95797177400SBarry Smith            PCMGSetRestriction(), PCMGGetSmoother(), PCMGGetSmootherUp(), PCMGGetSmootherDown(),
9580d353602SBarry Smith            PCMGSetCycleTypeOnLevel(), PCMGSetRhs(), PCMGSetX(), PCMGSetR()
9593b09bd56SBarry Smith M*/
9603b09bd56SBarry Smith 
9614b9ad928SBarry Smith EXTERN_C_BEGIN
9624b9ad928SBarry Smith #undef __FUNCT__
9634b9ad928SBarry Smith #define __FUNCT__ "PCCreate_MG"
964dba47a55SKris Buschelman PetscErrorCode PETSCKSP_DLLEXPORT PCCreate_MG(PC pc)
9654b9ad928SBarry Smith {
966*f3fbd535SBarry Smith   PC_MG          *mg;
967*f3fbd535SBarry Smith   PetscErrorCode ierr;
968*f3fbd535SBarry Smith 
9694b9ad928SBarry Smith   PetscFunctionBegin;
970*f3fbd535SBarry Smith   ierr        = PetscNewLog(pc,PC_MG,&mg);CHKERRQ(ierr);
971*f3fbd535SBarry Smith   pc->data    = (void*)mg;
972*f3fbd535SBarry Smith   mg->nlevels = -1;
973*f3fbd535SBarry Smith 
9744b9ad928SBarry Smith   pc->ops->apply          = PCApply_MG;
9754b9ad928SBarry Smith   pc->ops->setup          = PCSetUp_MG;
9764b9ad928SBarry Smith   pc->ops->destroy        = PCDestroy_MG;
9774b9ad928SBarry Smith   pc->ops->setfromoptions = PCSetFromOptions_MG;
9784b9ad928SBarry Smith   pc->ops->view           = PCView_MG;
9794b9ad928SBarry Smith   PetscFunctionReturn(0);
9804b9ad928SBarry Smith }
9814b9ad928SBarry Smith EXTERN_C_END
982