xref: /petsc/src/ksp/pc/impls/mg/mg.c (revision f3fbd5355936bed48b7956a540fdcc016dd18626)
1 #define PETSCKSP_DLL
2 
3 /*
4     Defines the multigrid preconditioner interface.
5 */
6 #include "../src/ksp/pc/impls/mg/mgimpl.h"                    /*I "petscmg.h" I*/
7 
8 
9 #undef __FUNCT__
10 #define __FUNCT__ "PCMGMCycle_Private"
11 PetscErrorCode PCMGMCycle_Private(PC pc,PC_MG_Levels **mglevels,PCRichardsonConvergedReason *reason)
12 {
13   PC_MG_Levels   *mg = *mglevels,*mgc;
14   PetscErrorCode ierr;
15   PetscInt       cycles = (mg->level == 1) ? 1 : (PetscInt) mg->cycles;
16 
17   PetscFunctionBegin;
18 
19   if (mg->eventsmoothsolve) {ierr = PetscLogEventBegin(mg->eventsmoothsolve,0,0,0,0);CHKERRQ(ierr);}
20   ierr = KSPSolve(mg->smoothd,mg->b,mg->x);CHKERRQ(ierr);  /* pre-smooth */
21   if (mg->eventsmoothsolve) {ierr = PetscLogEventEnd(mg->eventsmoothsolve,0,0,0,0);CHKERRQ(ierr);}
22   if (mg->level) {  /* not the coarsest grid */
23     if (mg->eventresidual) {ierr = PetscLogEventBegin(mg->eventresidual,0,0,0,0);CHKERRQ(ierr);}
24     ierr = (*mg->residual)(mg->A,mg->b,mg->x,mg->r);CHKERRQ(ierr);
25     if (mg->eventresidual) {ierr = PetscLogEventEnd(mg->eventresidual,0,0,0,0);CHKERRQ(ierr);}
26 
27     /* if on finest level and have convergence criteria set */
28     if (mg->level == mg->levels-1 && mg->ttol && reason) {
29       PetscReal rnorm;
30       ierr = VecNorm(mg->r,NORM_2,&rnorm);CHKERRQ(ierr);
31       if (rnorm <= mg->ttol) {
32         if (rnorm < mg->abstol) {
33           *reason = PCRICHARDSON_CONVERGED_ATOL;
34           ierr = PetscInfo2(pc,"Linear solver has converged. Residual norm %G is less than absolute tolerance %G\n",rnorm,mg->abstol);CHKERRQ(ierr);
35         } else {
36           *reason = PCRICHARDSON_CONVERGED_RTOL;
37           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);
38         }
39         PetscFunctionReturn(0);
40       }
41     }
42 
43     mgc = *(mglevels - 1);
44     if (mg->eventinterprestrict) {ierr = PetscLogEventBegin(mg->eventinterprestrict,0,0,0,0);CHKERRQ(ierr);}
45     ierr = MatRestrict(mg->restrct,mg->r,mgc->b);CHKERRQ(ierr);
46     if (mg->eventinterprestrict) {ierr = PetscLogEventEnd(mg->eventinterprestrict,0,0,0,0);CHKERRQ(ierr);}
47     ierr = VecSet(mgc->x,0.0);CHKERRQ(ierr);
48     while (cycles--) {
49       ierr = PCMGMCycle_Private(pc,mglevels-1,reason);CHKERRQ(ierr);
50     }
51     if (mg->eventinterprestrict) {ierr = PetscLogEventBegin(mg->eventinterprestrict,0,0,0,0);CHKERRQ(ierr);}
52     ierr = MatInterpolateAdd(mg->interpolate,mgc->x,mg->x,mg->x);CHKERRQ(ierr);
53     if (mg->eventinterprestrict) {ierr = PetscLogEventEnd(mg->eventinterprestrict,0,0,0,0);CHKERRQ(ierr);}
54     if (mg->eventsmoothsolve) {ierr = PetscLogEventBegin(mg->eventsmoothsolve,0,0,0,0);CHKERRQ(ierr);}
55     ierr = KSPSolve(mg->smoothu,mg->b,mg->x);CHKERRQ(ierr);    /* post smooth */
56     if (mg->eventsmoothsolve) {ierr = PetscLogEventEnd(mg->eventsmoothsolve,0,0,0,0);CHKERRQ(ierr);}
57   }
58   PetscFunctionReturn(0);
59 }
60 
61 #undef __FUNCT__
62 #define __FUNCT__ "PCApplyRichardson_MG"
63 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)
64 {
65   PC_MG          *mg = (PC_MG*)pc->data;
66   PC_MG_Levels   **mglevels = mg->levels;
67   PetscErrorCode ierr;
68   PetscInt       levels = mglevels[0]->levels,i;
69 
70   PetscFunctionBegin;
71   mglevels[levels-1]->b    = b;
72   mglevels[levels-1]->x    = x;
73 
74   mglevels[levels-1]->rtol = rtol;
75   mglevels[levels-1]->abstol = abstol;
76   mglevels[levels-1]->dtol = dtol;
77   if (rtol) {
78     /* compute initial residual norm for relative convergence test */
79     PetscReal rnorm;
80     if (zeroguess) {
81       ierr               = VecNorm(b,NORM_2,&rnorm);CHKERRQ(ierr);
82     } else {
83       ierr               = (*mglevels[levels-1]->residual)(mglevels[levels-1]->A,b,x,w);CHKERRQ(ierr);
84       ierr               = VecNorm(w,NORM_2,&rnorm);CHKERRQ(ierr);
85     }
86     mglevels[levels-1]->ttol = PetscMax(rtol*rnorm,abstol);
87   } else if (abstol) {
88     mglevels[levels-1]->ttol = abstol;
89   } else {
90     mglevels[levels-1]->ttol = 0.0;
91   }
92 
93   /* since smoother is applied to full system, not just residual we need to make sure that smoothers don't
94      stop prematurely do to small residual */
95   for (i=1; i<levels; i++) {
96     ierr = KSPSetTolerances(mglevels[i]->smoothu,0,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
97     if (mglevels[i]->smoothu != mglevels[i]->smoothd) {
98       ierr = KSPSetTolerances(mglevels[i]->smoothd,0,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
99     }
100   }
101 
102   *reason = (PCRichardsonConvergedReason)0;
103   for (i=0; i<its; i++) {
104     ierr = PCMGMCycle_Private(pc,mglevels+levels-1,reason);CHKERRQ(ierr);
105     if (*reason) break;
106   }
107   if (!*reason) *reason = PCRICHARDSON_CONVERGED_ITS;
108   *outits = i;
109   PetscFunctionReturn(0);
110 }
111 
112 #undef __FUNCT__
113 #define __FUNCT__ "PCMGSetLevels"
114 /*@C
115    PCMGSetLevels - Sets the number of levels to use with MG.
116    Must be called before any other MG routine.
117 
118    Collective on PC
119 
120    Input Parameters:
121 +  pc - the preconditioner context
122 .  levels - the number of levels
123 -  comms - optional communicators for each level; this is to allow solving the coarser problems
124            on smaller sets of processors. Use PETSC_NULL_OBJECT for default in Fortran
125 
126    Level: intermediate
127 
128    Notes:
129      If the number of levels is one then the multigrid uses the -mg_levels prefix
130   for setting the level options rather than the -mg_coarse prefix.
131 
132 .keywords: MG, set, levels, multigrid
133 
134 .seealso: PCMGSetType(), PCMGGetLevels()
135 @*/
136 PetscErrorCode PETSCKSP_DLLEXPORT PCMGSetLevels(PC pc,PetscInt levels,MPI_Comm *comms)
137 {
138   PetscErrorCode ierr;
139   PC_MG          *mg = (PC_MG*)pc->data;
140   MPI_Comm       comm = ((PetscObject)pc)->comm;
141   PC_MG_Levels   **mglevels;
142   PetscInt       i;
143   PetscMPIInt    size;
144   const char     *prefix;
145   PC             ipc;
146 
147   PetscFunctionBegin;
148   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
149   if (mg->nlevels > -1) {
150     SETERRQ(PETSC_ERR_ORDER,"Number levels already set for MG\n  make sure that you call PCMGSetLevels() before KSPSetFromOptions()");
151   }
152   if (mg->levels) SETERRQ(PETSC_ERR_PLIB,"Internal error in PETSc, this array should not yet exist");
153 
154   mg->nlevels = levels;
155 
156   ierr = PetscMalloc(levels*sizeof(PC_MG*),&mglevels);CHKERRQ(ierr);
157   ierr = PetscLogObjectMemory(pc,levels*(sizeof(PC_MG*)));CHKERRQ(ierr);
158 
159   ierr = PCGetOptionsPrefix(pc,&prefix);CHKERRQ(ierr);
160 
161   for (i=0; i<levels; i++) {
162     ierr = PetscNewLog(pc,PC_MG_Levels,&mglevels[i]);CHKERRQ(ierr);
163     mglevels[i]->level           = i;
164     mglevels[i]->levels          = levels;
165     mglevels[i]->cycles          = PC_MG_CYCLE_V;
166     mglevels[i]->galerkin        = PETSC_FALSE;
167     mglevels[i]->galerkinused    = PETSC_FALSE;
168     mglevels[i]->default_smoothu = 1;
169     mglevels[i]->default_smoothd = 1;
170 
171     if (comms) comm = comms[i];
172     ierr = KSPCreate(comm,&mglevels[i]->smoothd);CHKERRQ(ierr);
173     ierr = PetscObjectIncrementTabLevel((PetscObject)mglevels[i]->smoothd,(PetscObject)pc,levels-i);CHKERRQ(ierr);
174     ierr = KSPSetTolerances(mglevels[i]->smoothd,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT, mglevels[i]->default_smoothd);CHKERRQ(ierr);
175     ierr = KSPSetOptionsPrefix(mglevels[i]->smoothd,prefix);CHKERRQ(ierr);
176 
177     /* do special stuff for coarse grid */
178     if (!i && levels > 1) {
179       ierr = KSPAppendOptionsPrefix(mglevels[0]->smoothd,"mg_coarse_");CHKERRQ(ierr);
180 
181       /* coarse solve is (redundant) LU by default */
182       ierr = KSPSetType(mglevels[0]->smoothd,KSPPREONLY);CHKERRQ(ierr);
183       ierr = KSPGetPC(mglevels[0]->smoothd,&ipc);CHKERRQ(ierr);
184       ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
185       if (size > 1) {
186         ierr = PCSetType(ipc,PCREDUNDANT);CHKERRQ(ierr);
187       } else {
188         ierr = PCSetType(ipc,PCLU);CHKERRQ(ierr);
189       }
190 
191     } else {
192       char tprefix[128];
193       sprintf(tprefix,"mg_levels_%d_",(int)i);
194       ierr = KSPAppendOptionsPrefix(mglevels[i]->smoothd,tprefix);CHKERRQ(ierr);
195     }
196     ierr = PetscLogObjectParent(pc,mglevels[i]->smoothd);CHKERRQ(ierr);
197     mglevels[i]->smoothu             = mglevels[i]->smoothd;
198     mglevels[i]->rtol                = 0.0;
199     mglevels[i]->abstol              = 0.0;
200     mglevels[i]->dtol                = 0.0;
201     mglevels[i]->ttol                = 0.0;
202     mglevels[i]->eventsmoothsetup    = 0;
203     mglevels[i]->eventsmoothsolve    = 0;
204     mglevels[i]->eventresidual       = 0;
205     mglevels[i]->eventinterprestrict = 0;
206     mglevels[i]->cyclesperpcapply    = 1;
207   }
208   mglevels[0]->am          = PC_MG_MULTIPLICATIVE;
209   mg->levels               = mglevels;
210   pc->ops->applyrichardson = PCApplyRichardson_MG;
211   PetscFunctionReturn(0);
212 }
213 
214 #undef __FUNCT__
215 #define __FUNCT__ "PCDestroy_MG"
216 PetscErrorCode PCDestroy_MG(PC pc)
217 {
218   PC_MG          *mg = (PC_MG*)pc->data;
219   PC_MG_Levels   **mglevels = mg->levels;
220   PetscErrorCode ierr;
221   PetscInt       i,n;
222 
223   PetscFunctionBegin;
224   if (mglevels) {
225     n = mglevels[0]->levels;
226     for (i=0; i<n-1; i++) {
227       if (mglevels[i+1]->r) {ierr = VecDestroy(mglevels[i+1]->r);CHKERRQ(ierr);}
228       if (mglevels[i]->b) {ierr = VecDestroy(mglevels[i]->b);CHKERRQ(ierr);}
229       if (mglevels[i]->x) {ierr = VecDestroy(mglevels[i]->x);CHKERRQ(ierr);}
230       if (mglevels[i+1]->restrct) {ierr = MatDestroy(mglevels[i+1]->restrct);CHKERRQ(ierr);}
231       if (mglevels[i+1]->interpolate) {ierr = MatDestroy(mglevels[i+1]->interpolate);CHKERRQ(ierr);}
232     }
233 
234     for (i=0; i<n; i++) {
235       if (mglevels[i]->smoothd != mglevels[i]->smoothu) {
236 	ierr = KSPDestroy(mglevels[i]->smoothd);CHKERRQ(ierr);
237       }
238       ierr = KSPDestroy(mglevels[i]->smoothu);CHKERRQ(ierr);
239       ierr = PetscFree(mglevels[i]);CHKERRQ(ierr);
240     }
241     ierr = PetscFree(mglevels);CHKERRQ(ierr);
242   }
243   ierr = PetscFree(mg);CHKERRQ(ierr);
244   PetscFunctionReturn(0);
245 }
246 
247 
248 
249 EXTERN PetscErrorCode PCMGACycle_Private(PC_MG_Levels**);
250 EXTERN PetscErrorCode PCMGFCycle_Private(PC,PC_MG_Levels**);
251 EXTERN PetscErrorCode PCMGKCycle_Private(PC_MG_Levels**);
252 
253 /*
254    PCApply_MG - Runs either an additive, multiplicative, Kaskadic
255              or full cycle of multigrid.
256 
257   Note:
258   A simple wrapper which calls PCMGMCycle(),PCMGACycle(), or PCMGFCycle().
259 */
260 #undef __FUNCT__
261 #define __FUNCT__ "PCApply_MG"
262 static PetscErrorCode PCApply_MG(PC pc,Vec b,Vec x)
263 {
264   PC_MG          *mg = (PC_MG*)pc->data;
265   PC_MG_Levels   **mglevels = mg->levels;
266   PetscErrorCode ierr;
267   PetscInt       levels = mglevels[0]->levels,i;
268 
269   PetscFunctionBegin;
270   mglevels[levels-1]->b = b;
271   mglevels[levels-1]->x = x;
272   if (mglevels[0]->am == PC_MG_MULTIPLICATIVE) {
273     ierr = VecSet(x,0.0);CHKERRQ(ierr);
274     for (i=0; i<mglevels[0]->cyclesperpcapply; i++) {
275       ierr = PCMGMCycle_Private(pc,mglevels+levels-1,PETSC_NULL);CHKERRQ(ierr);
276     }
277   }
278   else if (mglevels[0]->am == PC_MG_ADDITIVE) {
279     ierr = PCMGACycle_Private(mglevels);CHKERRQ(ierr);
280   }
281   else if (mglevels[0]->am == PC_MG_KASKADE) {
282     ierr = PCMGKCycle_Private(mglevels);CHKERRQ(ierr);
283   }
284   else {
285     ierr = PCMGFCycle_Private(pc,mglevels);CHKERRQ(ierr);
286   }
287   PetscFunctionReturn(0);
288 }
289 
290 
291 #undef __FUNCT__
292 #define __FUNCT__ "PCSetFromOptions_MG"
293 PetscErrorCode PCSetFromOptions_MG(PC pc)
294 {
295   PetscErrorCode ierr;
296   PetscInt       m,levels = 1,cycles;
297   PetscTruth     flg;
298   PC_MG          *mg = (PC_MG*)pc->data;
299   PC_MG_Levels   **mglevels = mg->levels;
300   PCMGType       mgtype;
301   PCMGCycleType  mgctype;
302 
303   PetscFunctionBegin;
304   ierr = PetscOptionsHead("Multigrid options");CHKERRQ(ierr);
305     if (!pc->data) {
306       ierr = PetscOptionsInt("-pc_mg_levels","Number of Levels","PCMGSetLevels",levels,&levels,&flg);CHKERRQ(ierr);
307       ierr = PCMGSetLevels(pc,levels,PETSC_NULL);CHKERRQ(ierr);
308       mglevels = mg->levels;
309     }
310     mgctype = (PCMGCycleType) mglevels[0]->cycles;
311     ierr = PetscOptionsEnum("-pc_mg_cycle_type","V cycle or for W-cycle","PCMGSetCycleType",PCMGCycleTypes,(PetscEnum)mgctype,(PetscEnum*)&mgctype,&flg);CHKERRQ(ierr);
312     if (flg) {
313       ierr = PCMGSetCycleType(pc,mgctype);CHKERRQ(ierr);
314     };
315     flg  = PETSC_FALSE;
316     ierr = PetscOptionsTruth("-pc_mg_galerkin","Use Galerkin process to compute coarser operators","PCMGSetGalerkin",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
317     if (flg) {
318       ierr = PCMGSetGalerkin(pc);CHKERRQ(ierr);
319     }
320     ierr = PetscOptionsInt("-pc_mg_smoothup","Number of post-smoothing steps","PCMGSetNumberSmoothUp",1,&m,&flg);CHKERRQ(ierr);
321     if (flg) {
322       ierr = PCMGSetNumberSmoothUp(pc,m);CHKERRQ(ierr);
323     }
324     ierr = PetscOptionsInt("-pc_mg_smoothdown","Number of pre-smoothing steps","PCMGSetNumberSmoothDown",1,&m,&flg);CHKERRQ(ierr);
325     if (flg) {
326       ierr = PCMGSetNumberSmoothDown(pc,m);CHKERRQ(ierr);
327     }
328     mgtype = mglevels[0]->am;
329     ierr = PetscOptionsEnum("-pc_mg_type","Multigrid type","PCMGSetType",PCMGTypes,(PetscEnum)mgtype,(PetscEnum*)&mgtype,&flg);CHKERRQ(ierr);
330     if (flg) {
331       ierr = PCMGSetType(pc,mgtype);CHKERRQ(ierr);
332     }
333     if (mglevels[0]->am == PC_MG_MULTIPLICATIVE) {
334       ierr = PetscOptionsInt("-pc_mg_multiplicative_cycles","Number of cycles for each preconditioner step","PCMGSetLevels",mglevels[0]->cyclesperpcapply,&cycles,&flg);CHKERRQ(ierr);
335       if (flg) {
336 	ierr = PCMGMultiplicativeSetCycles(pc,cycles);CHKERRQ(ierr);
337       }
338     }
339     flg  = PETSC_FALSE;
340     ierr = PetscOptionsTruth("-pc_mg_log","Log times for each multigrid level","None",flg,&flg,PETSC_NULL);CHKERRQ(ierr);
341     if (flg) {
342       PetscInt i;
343       char     eventname[128];
344       if (!mg) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
345       levels = mglevels[0]->levels;
346       for (i=0; i<levels; i++) {
347         sprintf(eventname,"MGSetup Level %d",(int)i);
348         ierr = PetscLogEventRegister(eventname,((PetscObject)pc)->cookie,&mglevels[i]->eventsmoothsetup);CHKERRQ(ierr);
349         sprintf(eventname,"MGSmooth Level %d",(int)i);
350         ierr = PetscLogEventRegister(eventname,((PetscObject)pc)->cookie,&mglevels[i]->eventsmoothsolve);CHKERRQ(ierr);
351         if (i) {
352           sprintf(eventname,"MGResid Level %d",(int)i);
353           ierr = PetscLogEventRegister(eventname,((PetscObject)pc)->cookie,&mglevels[i]->eventresidual);CHKERRQ(ierr);
354           sprintf(eventname,"MGInterp Level %d",(int)i);
355           ierr = PetscLogEventRegister(eventname,((PetscObject)pc)->cookie,&mglevels[i]->eventinterprestrict);CHKERRQ(ierr);
356         }
357       }
358     }
359   ierr = PetscOptionsTail();CHKERRQ(ierr);
360   PetscFunctionReturn(0);
361 }
362 
363 const char *PCMGTypes[] = {"MULTIPLICATIVE","ADDITIVE","FULL","KASKADE","PCMGType","PC_MG",0};
364 const char *PCMGCycleTypes[] = {"invalid","v","w","PCMGCycleType","PC_MG_CYCLE",0};
365 
366 #undef __FUNCT__
367 #define __FUNCT__ "PCView_MG"
368 PetscErrorCode PCView_MG(PC pc,PetscViewer viewer)
369 {
370   PC_MG          *mg = (PC_MG*)pc->data;
371   PC_MG_Levels   **mglevels = mg->levels;
372   PetscErrorCode ierr;
373   PetscInt       levels = mglevels[0]->levels,i;
374   PetscTruth     iascii;
375 
376   PetscFunctionBegin;
377   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
378   if (iascii) {
379     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     if (mglevels[0]->am == PC_MG_MULTIPLICATIVE) {
381       ierr = PetscViewerASCIIPrintf(viewer,"    Cycles per PCApply=%d\n",mglevels[0]->cyclesperpcapply);CHKERRQ(ierr);
382     }
383     if (mglevels[0]->galerkin) {
384       ierr = PetscViewerASCIIPrintf(viewer,"    Using Galerkin computed coarse grid matrices\n");CHKERRQ(ierr);
385     }
386     for (i=0; i<levels; i++) {
387       if (!i) {
388         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       } else {
390         ierr = PetscViewerASCIIPrintf(viewer,"Down solver (pre-smoother) on level %D smooths=%D --------------------\n",i,mglevels[i]->default_smoothd);CHKERRQ(ierr);
391       }
392       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
393       ierr = KSPView(mglevels[i]->smoothd,viewer);CHKERRQ(ierr);
394       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
395       if (i && mglevels[i]->smoothd == mglevels[i]->smoothu) {
396         ierr = PetscViewerASCIIPrintf(viewer,"Up solver (post-smoother) same as down solver (pre-smoother)\n");CHKERRQ(ierr);
397       } else if (i){
398         ierr = PetscViewerASCIIPrintf(viewer,"Up solver (post-smoother) on level %D smooths=%D --------------------\n",i,mglevels[i]->default_smoothu);CHKERRQ(ierr);
399         ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
400         ierr = KSPView(mglevels[i]->smoothu,viewer);CHKERRQ(ierr);
401         ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
402       }
403     }
404   } else {
405     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported for PCMG",((PetscObject)viewer)->type_name);
406   }
407   PetscFunctionReturn(0);
408 }
409 
410 /*
411     Calls setup for the KSP on each level
412 */
413 #undef __FUNCT__
414 #define __FUNCT__ "PCSetUp_MG"
415 PetscErrorCode PCSetUp_MG(PC pc)
416 {
417   PC_MG                   *mg = (PC_MG*)pc->data;
418   PC_MG_Levels            **mglevels = mg->levels;
419   PetscErrorCode          ierr;
420   PetscInt                i,n = mglevels[0]->levels;
421   PC                      cpc,mpc;
422   PetscTruth              preonly,lu,redundant,cholesky,monitor = PETSC_FALSE,dump = PETSC_FALSE,opsset;
423   PetscViewerASCIIMonitor ascii;
424   PetscViewer             viewer = PETSC_NULL;
425   MPI_Comm                comm;
426   Mat                     dA,dB;
427   MatStructure            uflag;
428   Vec                     tvec;
429 
430   PetscFunctionBegin;
431 
432   /* If user did not provide fine grid operators OR operator was not updated since last global KSPSetOperators() */
433   /* so use those from global PC */
434   /* Is this what we always want? What if user wants to keep old one? */
435   ierr = KSPGetOperatorsSet(mglevels[n-1]->smoothd,PETSC_NULL,&opsset);CHKERRQ(ierr);
436   ierr = KSPGetPC(mglevels[0]->smoothd,&cpc);CHKERRQ(ierr);
437   ierr = KSPGetPC(mglevels[n-1]->smoothd,&mpc);CHKERRQ(ierr);
438   if (!opsset || ((cpc->setupcalled == 1) && (mpc->setupcalled == 2))) {
439     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     ierr = KSPSetOperators(mglevels[n-1]->smoothd,pc->mat,pc->pmat,pc->flag);CHKERRQ(ierr);
441   }
442 
443   if (mglevels[0]->galerkin) {
444     Mat B;
445     mglevels[0]->galerkinused = PETSC_TRUE;
446     /* currently only handle case where mat and pmat are the same on coarser levels */
447     ierr = KSPGetOperators(mglevels[n-1]->smoothd,&dA,&dB,&uflag);CHKERRQ(ierr);
448     if (!pc->setupcalled) {
449       for (i=n-2; i>-1; i--) {
450         ierr = MatPtAP(dB,mglevels[i+1]->interpolate,MAT_INITIAL_MATRIX,1.0,&B);CHKERRQ(ierr);
451         ierr = KSPSetOperators(mglevels[i]->smoothd,B,B,uflag);CHKERRQ(ierr);
452 	if (i != n-2) {ierr = PetscObjectDereference((PetscObject)dB);CHKERRQ(ierr);}
453         dB   = B;
454       }
455       ierr = PetscObjectDereference((PetscObject)dB);CHKERRQ(ierr);
456     } else {
457       for (i=n-2; i>-1; i--) {
458         ierr = KSPGetOperators(mglevels[i]->smoothd,PETSC_NULL,&B,PETSC_NULL);CHKERRQ(ierr);
459         ierr = MatPtAP(dB,mglevels[i+1]->interpolate,MAT_REUSE_MATRIX,1.0,&B);CHKERRQ(ierr);
460         ierr = KSPSetOperators(mglevels[i]->smoothd,B,B,uflag);CHKERRQ(ierr);
461         dB   = B;
462       }
463     }
464   }
465 
466   if (!pc->setupcalled) {
467     ierr = PetscOptionsGetTruth(0,"-pc_mg_monitor",&monitor,PETSC_NULL);CHKERRQ(ierr);
468 
469     for (i=0; i<n; i++) {
470       if (monitor) {
471         ierr = PetscObjectGetComm((PetscObject)mglevels[i]->smoothd,&comm);CHKERRQ(ierr);
472         ierr = PetscViewerASCIIMonitorCreate(comm,"stdout",n-i,&ascii);CHKERRQ(ierr);
473         ierr = KSPMonitorSet(mglevels[i]->smoothd,KSPMonitorDefault,ascii,(PetscErrorCode(*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
474       }
475       ierr = KSPSetFromOptions(mglevels[i]->smoothd);CHKERRQ(ierr);
476     }
477     for (i=1; i<n; i++) {
478       if (mglevels[i]->smoothu && (mglevels[i]->smoothu != mglevels[i]->smoothd)) {
479         if (monitor) {
480           ierr = PetscObjectGetComm((PetscObject)mglevels[i]->smoothu,&comm);CHKERRQ(ierr);
481           ierr = PetscViewerASCIIMonitorCreate(comm,"stdout",n-i,&ascii);CHKERRQ(ierr);
482           ierr = KSPMonitorSet(mglevels[i]->smoothu,KSPMonitorDefault,ascii,(PetscErrorCode(*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
483         }
484         ierr = KSPSetFromOptions(mglevels[i]->smoothu);CHKERRQ(ierr);
485       }
486     }
487     for (i=1; i<n; i++) {
488       if (!mglevels[i]->residual) {
489         Mat mat;
490         ierr = KSPGetOperators(mglevels[i]->smoothd,PETSC_NULL,&mat,PETSC_NULL);CHKERRQ(ierr);
491         ierr = PCMGSetResidual(pc,i,PCMGDefaultResidual,mat);CHKERRQ(ierr);
492       }
493       if (mglevels[i]->restrct && !mglevels[i]->interpolate) {
494         ierr = PCMGSetInterpolation(pc,i,mglevels[i]->restrct);CHKERRQ(ierr);
495       }
496       if (!mglevels[i]->restrct && mglevels[i]->interpolate) {
497         ierr = PCMGSetRestriction(pc,i,mglevels[i]->interpolate);CHKERRQ(ierr);
498       }
499 #if defined(PETSC_USE_DEBUG)
500       if (!mglevels[i]->restrct || !mglevels[i]->interpolate) {
501         SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Need to set restriction or interpolation on level %d",(int)i);
502       }
503 #endif
504     }
505     for (i=0; i<n-1; i++) {
506       if (!mglevels[i]->b) {
507         Vec *vec;
508         ierr = KSPGetVecs(mglevels[i]->smoothd,1,&vec,0,PETSC_NULL);CHKERRQ(ierr);
509         ierr = PCMGSetRhs(pc,i,*vec);CHKERRQ(ierr);
510         ierr = VecDestroy(*vec);CHKERRQ(ierr);
511         ierr = PetscFree(vec);CHKERRQ(ierr);
512       }
513       if (!mglevels[i]->r && i) {
514         ierr = VecDuplicate(mglevels[i]->b,&tvec);CHKERRQ(ierr);
515         ierr = PCMGSetR(pc,i,tvec);CHKERRQ(ierr);
516         ierr = VecDestroy(tvec);CHKERRQ(ierr);
517       }
518       if (!mglevels[i]->x) {
519         ierr = VecDuplicate(mglevels[i]->b,&tvec);CHKERRQ(ierr);
520         ierr = PCMGSetX(pc,i,tvec);CHKERRQ(ierr);
521         ierr = VecDestroy(tvec);CHKERRQ(ierr);
522       }
523     }
524     if (n != 1 && !mglevels[n-1]->r) {
525       /* PCMGSetR() on the finest level if user did not supply it */
526       Vec *vec;
527       ierr = KSPGetVecs(mglevels[n-1]->smoothd,1,&vec,0,PETSC_NULL);CHKERRQ(ierr);
528       ierr = PCMGSetR(pc,n-1,*vec);CHKERRQ(ierr);
529       ierr = VecDestroy(*vec);CHKERRQ(ierr);
530       ierr = PetscFree(vec);CHKERRQ(ierr);
531     }
532   }
533 
534 
535   for (i=1; i<n; i++) {
536     if (mglevels[i]->smoothu == mglevels[i]->smoothd) {
537       /* if doing only down then initial guess is zero */
538       ierr = KSPSetInitialGuessNonzero(mglevels[i]->smoothd,PETSC_TRUE);CHKERRQ(ierr);
539     }
540     if (mglevels[i]->eventsmoothsetup) {ierr = PetscLogEventBegin(mglevels[i]->eventsmoothsetup,0,0,0,0);CHKERRQ(ierr);}
541     ierr = KSPSetUp(mglevels[i]->smoothd);CHKERRQ(ierr);
542     if (mglevels[i]->eventsmoothsetup) {ierr = PetscLogEventEnd(mglevels[i]->eventsmoothsetup,0,0,0,0);CHKERRQ(ierr);}
543   }
544   for (i=1; i<n; i++) {
545     if (mglevels[i]->smoothu && mglevels[i]->smoothu != mglevels[i]->smoothd) {
546       Mat          downmat,downpmat;
547       MatStructure matflag;
548       PetscTruth   opsset;
549 
550       /* check if operators have been set for up, if not use down operators to set them */
551       ierr = KSPGetOperatorsSet(mglevels[i]->smoothu,&opsset,PETSC_NULL);CHKERRQ(ierr);
552       if (!opsset) {
553         ierr = KSPGetOperators(mglevels[i]->smoothd,&downmat,&downpmat,&matflag);CHKERRQ(ierr);
554         ierr = KSPSetOperators(mglevels[i]->smoothu,downmat,downpmat,matflag);CHKERRQ(ierr);
555       }
556 
557       ierr = KSPSetInitialGuessNonzero(mglevels[i]->smoothu,PETSC_TRUE);CHKERRQ(ierr);
558       if (mglevels[i]->eventsmoothsetup) {ierr = PetscLogEventBegin(mglevels[i]->eventsmoothsetup,0,0,0,0);CHKERRQ(ierr);}
559       ierr = KSPSetUp(mglevels[i]->smoothu);CHKERRQ(ierr);
560       if (mglevels[i]->eventsmoothsetup) {ierr = PetscLogEventEnd(mglevels[i]->eventsmoothsetup,0,0,0,0);CHKERRQ(ierr);}
561     }
562   }
563 
564   /*
565       If coarse solver is not direct method then DO NOT USE preonly
566   */
567   ierr = PetscTypeCompare((PetscObject)mglevels[0]->smoothd,KSPPREONLY,&preonly);CHKERRQ(ierr);
568   if (preonly) {
569     ierr = PetscTypeCompare((PetscObject)cpc,PCLU,&lu);CHKERRQ(ierr);
570     ierr = PetscTypeCompare((PetscObject)cpc,PCREDUNDANT,&redundant);CHKERRQ(ierr);
571     ierr = PetscTypeCompare((PetscObject)cpc,PCCHOLESKY,&cholesky);CHKERRQ(ierr);
572     if (!lu && !redundant && !cholesky) {
573       ierr = KSPSetType(mglevels[0]->smoothd,KSPGMRES);CHKERRQ(ierr);
574     }
575   }
576 
577   if (!pc->setupcalled) {
578     if (monitor) {
579       ierr = PetscObjectGetComm((PetscObject)mglevels[0]->smoothd,&comm);CHKERRQ(ierr);
580       ierr = PetscViewerASCIIMonitorCreate(comm,"stdout",n,&ascii);CHKERRQ(ierr);
581       ierr = KSPMonitorSet(mglevels[0]->smoothd,KSPMonitorDefault,ascii,(PetscErrorCode(*)(void*))PetscViewerASCIIMonitorDestroy);CHKERRQ(ierr);
582     }
583     ierr = KSPSetFromOptions(mglevels[0]->smoothd);CHKERRQ(ierr);
584   }
585 
586   if (mglevels[0]->eventsmoothsetup) {ierr = PetscLogEventBegin(mglevels[0]->eventsmoothsetup,0,0,0,0);CHKERRQ(ierr);}
587   ierr = KSPSetUp(mglevels[0]->smoothd);CHKERRQ(ierr);
588   if (mglevels[0]->eventsmoothsetup) {ierr = PetscLogEventEnd(mglevels[0]->eventsmoothsetup,0,0,0,0);CHKERRQ(ierr);}
589 
590   /*
591      Dump the interpolation/restriction matrices plus the
592    Jacobian/stiffness on each level. This allows Matlab users to
593    easily check if the Galerkin condition A_c = R A_f R^T is satisfied.
594 
595    Only support one or the other at the same time.
596   */
597 #if defined(PETSC_USE_SOCKET_VIEWER)
598   ierr = PetscOptionsGetTruth(((PetscObject)pc)->prefix,"-pc_mg_dump_matlab",&dump,PETSC_NULL);CHKERRQ(ierr);
599   if (dump) {
600     viewer = PETSC_VIEWER_SOCKET_(((PetscObject)pc)->comm);
601   }
602   dump = PETSC_FALSE;
603 #endif
604   ierr = PetscOptionsGetTruth(((PetscObject)pc)->prefix,"-pc_mg_dump_binary",&dump,PETSC_NULL);CHKERRQ(ierr);
605   if (dump) {
606     viewer = PETSC_VIEWER_BINARY_(((PetscObject)pc)->comm);
607   }
608 
609   if (viewer) {
610     for (i=1; i<n; i++) {
611       ierr = MatView(mglevels[i]->restrct,viewer);CHKERRQ(ierr);
612     }
613     for (i=0; i<n; i++) {
614       ierr = KSPGetPC(mglevels[i]->smoothd,&pc);CHKERRQ(ierr);
615       ierr = MatView(pc->mat,viewer);CHKERRQ(ierr);
616     }
617   }
618   PetscFunctionReturn(0);
619 }
620 
621 /* -------------------------------------------------------------------------------------*/
622 
623 #undef __FUNCT__
624 #define __FUNCT__ "PCMGGetLevels"
625 /*@
626    PCMGGetLevels - Gets the number of levels to use with MG.
627 
628    Not Collective
629 
630    Input Parameter:
631 .  pc - the preconditioner context
632 
633    Output parameter:
634 .  levels - the number of levels
635 
636    Level: advanced
637 
638 .keywords: MG, get, levels, multigrid
639 
640 .seealso: PCMGSetLevels()
641 @*/
642 PetscErrorCode PETSCKSP_DLLEXPORT PCMGGetLevels(PC pc,PetscInt *levels)
643 {
644   PC_MG *mg = (PC_MG*)pc->data;
645 
646   PetscFunctionBegin;
647   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
648   PetscValidIntPointer(levels,2);
649   *levels = mg->nlevels;
650   PetscFunctionReturn(0);
651 }
652 
653 #undef __FUNCT__
654 #define __FUNCT__ "PCMGSetType"
655 /*@
656    PCMGSetType - Determines the form of multigrid to use:
657    multiplicative, additive, full, or the Kaskade algorithm.
658 
659    Collective on PC
660 
661    Input Parameters:
662 +  pc - the preconditioner context
663 -  form - multigrid form, one of PC_MG_MULTIPLICATIVE, PC_MG_ADDITIVE,
664    PC_MG_FULL, PC_MG_KASKADE
665 
666    Options Database Key:
667 .  -pc_mg_type <form> - Sets <form>, one of multiplicative,
668    additive, full, kaskade
669 
670    Level: advanced
671 
672 .keywords: MG, set, method, multiplicative, additive, full, Kaskade, multigrid
673 
674 .seealso: PCMGSetLevels()
675 @*/
676 PetscErrorCode PETSCKSP_DLLEXPORT PCMGSetType(PC pc,PCMGType form)
677 {
678   PC_MG                   *mg = (PC_MG*)pc->data;
679   PC_MG_Levels            **mglevels = mg->levels;
680 
681   PetscFunctionBegin;
682   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
683   mglevels[0]->am = form;
684   if (form == PC_MG_MULTIPLICATIVE) pc->ops->applyrichardson = PCApplyRichardson_MG;
685   else pc->ops->applyrichardson = 0;
686   PetscFunctionReturn(0);
687 }
688 
689 #undef __FUNCT__
690 #define __FUNCT__ "PCMGSetCycleType"
691 /*@
692    PCMGSetCycleType - Sets the type cycles to use.  Use PCMGSetCycleTypeOnLevel() for more
693    complicated cycling.
694 
695    Collective on PC
696 
697    Input Parameters:
698 +  pc - the multigrid context
699 -  PC_MG_CYCLE_V or PC_MG_CYCLE_W
700 
701    Options Database Key:
702 $  -pc_mg_cycle_type v or w
703 
704    Level: advanced
705 
706 .keywords: MG, set, cycles, V-cycle, W-cycle, multigrid
707 
708 .seealso: PCMGSetCycleTypeOnLevel()
709 @*/
710 PetscErrorCode PETSCKSP_DLLEXPORT PCMGSetCycleType(PC pc,PCMGCycleType n)
711 {
712   PC_MG        *mg = (PC_MG*)pc->data;
713   PC_MG_Levels **mglevels = mg->levels;
714   PetscInt     i,levels;
715 
716   PetscFunctionBegin;
717   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
718   if (!mglevels) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
719   levels = mglevels[0]->levels;
720 
721   for (i=0; i<levels; i++) {
722     mglevels[i]->cycles  = n;
723   }
724   PetscFunctionReturn(0);
725 }
726 
727 #undef __FUNCT__
728 #define __FUNCT__ "PCMGMultiplicativeSetCycles"
729 /*@
730    PCMGMultiplicativeSetCycles - Sets the number of cycles to use for each preconditioner step
731          of multigrid when PCMGType of PC_MG_MULTIPLICATIVE is used
732 
733    Collective on PC
734 
735    Input Parameters:
736 +  pc - the multigrid context
737 -  n - number of cycles (default is 1)
738 
739    Options Database Key:
740 $  -pc_mg_multiplicative_cycles n
741 
742    Level: advanced
743 
744    Notes: This is not associated with setting a v or w cycle, that is set with PCMGSetCycleType()
745 
746 .keywords: MG, set, cycles, V-cycle, W-cycle, multigrid
747 
748 .seealso: PCMGSetCycleTypeOnLevel(), PCMGSetCycleType()
749 @*/
750 PetscErrorCode PETSCKSP_DLLEXPORT PCMGMultiplicativeSetCycles(PC pc,PetscInt n)
751 {
752   PC_MG        *mg = (PC_MG*)pc->data;
753   PC_MG_Levels **mglevels = mg->levels;
754   PetscInt     i,levels;
755 
756   PetscFunctionBegin;
757   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
758   if (!mglevels) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
759   levels = mglevels[0]->levels;
760 
761   for (i=0; i<levels; i++) {
762     mglevels[i]->cyclesperpcapply  = n;
763   }
764   PetscFunctionReturn(0);
765 }
766 
767 #undef __FUNCT__
768 #define __FUNCT__ "PCMGSetGalerkin"
769 /*@
770    PCMGSetGalerkin - Causes the coarser grid matrices to be computed from the
771       finest grid via the Galerkin process: A_i-1 = r_i * A_i * r_i^t
772 
773    Collective on PC
774 
775    Input Parameters:
776 .  pc - the multigrid context
777 
778    Options Database Key:
779 $  -pc_mg_galerkin
780 
781    Level: intermediate
782 
783 .keywords: MG, set, Galerkin
784 
785 .seealso: PCMGGetGalerkin()
786 
787 @*/
788 PetscErrorCode PETSCKSP_DLLEXPORT PCMGSetGalerkin(PC pc)
789 {
790   PC_MG        *mg = (PC_MG*)pc->data;
791   PC_MG_Levels **mglevels = mg->levels;
792   PetscInt     i,levels;
793 
794   PetscFunctionBegin;
795   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
796   if (!mglevels) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
797   levels = mglevels[0]->levels;
798 
799   for (i=0; i<levels; i++) {
800     mglevels[i]->galerkin = PETSC_TRUE;
801   }
802   PetscFunctionReturn(0);
803 }
804 
805 #undef __FUNCT__
806 #define __FUNCT__ "PCMGGetGalerkin"
807 /*@
808    PCMGGetGalerkin - Checks if Galerkin multigrid is being used, i.e.
809       A_i-1 = r_i * A_i * r_i^t
810 
811    Not Collective
812 
813    Input Parameter:
814 .  pc - the multigrid context
815 
816    Output Parameter:
817 .  gelerkin - PETSC_TRUE or PETSC_FALSE
818 
819    Options Database Key:
820 $  -pc_mg_galerkin
821 
822    Level: intermediate
823 
824 .keywords: MG, set, Galerkin
825 
826 .seealso: PCMGSetGalerkin()
827 
828 @*/
829 PetscErrorCode PETSCKSP_DLLEXPORT PCMGGetGalerkin(PC pc,PetscTruth *galerkin)
830 {
831   PC_MG        *mg = (PC_MG*)pc->data;
832   PC_MG_Levels **mglevels = mg->levels;
833 
834   PetscFunctionBegin;
835   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
836   if (!mglevels) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
837   *galerkin = mglevels[0]->galerkin;
838   PetscFunctionReturn(0);
839 }
840 
841 #undef __FUNCT__
842 #define __FUNCT__ "PCMGSetNumberSmoothDown"
843 /*@
844    PCMGSetNumberSmoothDown - Sets the number of pre-smoothing steps to
845    use on all levels. Use PCMGGetSmootherDown() to set different
846    pre-smoothing steps on different levels.
847 
848    Collective on PC
849 
850    Input Parameters:
851 +  mg - the multigrid context
852 -  n - the number of smoothing steps
853 
854    Options Database Key:
855 .  -pc_mg_smoothdown <n> - Sets number of pre-smoothing steps
856 
857    Level: advanced
858 
859 .keywords: MG, smooth, down, pre-smoothing, steps, multigrid
860 
861 .seealso: PCMGSetNumberSmoothUp()
862 @*/
863 PetscErrorCode PETSCKSP_DLLEXPORT PCMGSetNumberSmoothDown(PC pc,PetscInt n)
864 {
865   PC_MG          *mg = (PC_MG*)pc->data;
866   PC_MG_Levels   **mglevels = mg->levels;
867   PetscErrorCode ierr;
868   PetscInt       i,levels;
869 
870   PetscFunctionBegin;
871   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
872   if (!mglevels) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
873   levels = mglevels[0]->levels;
874 
875   for (i=1; i<levels; i++) {
876     /* make sure smoother up and down are different */
877     ierr = PCMGGetSmootherUp(pc,i,PETSC_NULL);CHKERRQ(ierr);
878     ierr = KSPSetTolerances(mglevels[i]->smoothd,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,n);CHKERRQ(ierr);
879     mglevels[i]->default_smoothd = n;
880   }
881   PetscFunctionReturn(0);
882 }
883 
884 #undef __FUNCT__
885 #define __FUNCT__ "PCMGSetNumberSmoothUp"
886 /*@
887    PCMGSetNumberSmoothUp - Sets the number of post-smoothing steps to use
888    on all levels. Use PCMGGetSmootherUp() to set different numbers of
889    post-smoothing steps on different levels.
890 
891    Collective on PC
892 
893    Input Parameters:
894 +  mg - the multigrid context
895 -  n - the number of smoothing steps
896 
897    Options Database Key:
898 .  -pc_mg_smoothup <n> - Sets number of post-smoothing steps
899 
900    Level: advanced
901 
902    Note: this does not set a value on the coarsest grid, since we assume that
903     there is no separate smooth up on the coarsest grid.
904 
905 .keywords: MG, smooth, up, post-smoothing, steps, multigrid
906 
907 .seealso: PCMGSetNumberSmoothDown()
908 @*/
909 PetscErrorCode PETSCKSP_DLLEXPORT PCMGSetNumberSmoothUp(PC pc,PetscInt n)
910 {
911   PC_MG          *mg = (PC_MG*)pc->data;
912   PC_MG_Levels   **mglevels = mg->levels;
913   PetscErrorCode ierr;
914   PetscInt       i,levels;
915 
916   PetscFunctionBegin;
917   PetscValidHeaderSpecific(pc,PC_COOKIE,1);
918   if (!mglevels) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
919   levels = mglevels[0]->levels;
920 
921   for (i=1; i<levels; i++) {
922     /* make sure smoother up and down are different */
923     ierr = PCMGGetSmootherUp(pc,i,PETSC_NULL);CHKERRQ(ierr);
924     ierr = KSPSetTolerances(mglevels[i]->smoothu,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,n);CHKERRQ(ierr);
925     mglevels[i]->default_smoothu = n;
926   }
927   PetscFunctionReturn(0);
928 }
929 
930 /* ----------------------------------------------------------------------------------------*/
931 
932 /*MC
933    PCMG - Use multigrid preconditioning. This preconditioner requires you provide additional
934     information about the coarser grid matrices and restriction/interpolation operators.
935 
936    Options Database Keys:
937 +  -pc_mg_levels <nlevels> - number of levels including finest
938 .  -pc_mg_cycles v or w
939 .  -pc_mg_smoothup <n> - number of smoothing steps after interpolation
940 .  -pc_mg_smoothdown <n> - number of smoothing steps before applying restriction operator
941 .  -pc_mg_type <additive,multiplicative,full,cascade> - multiplicative is the default
942 .  -pc_mg_log - log information about time spent on each level of the solver
943 .  -pc_mg_monitor - print information on the multigrid convergence
944 .  -pc_mg_galerkin - use Galerkin process to compute coarser operators
945 -  -pc_mg_dump_matlab - dumps the matrices for each level and the restriction/interpolation matrices
946                         to the Socket viewer for reading from Matlab.
947 
948    Notes:
949 
950    Level: intermediate
951 
952    Concepts: multigrid/multilevel
953 
954 .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC, PCMGType,
955            PCMGSetLevels(), PCMGGetLevels(), PCMGSetType(), PCMGSetCycleType(), PCMGSetNumberSmoothDown(),
956            PCMGSetNumberSmoothUp(), PCMGGetCoarseSolve(), PCMGSetResidual(), PCMGSetInterpolation(),
957            PCMGSetRestriction(), PCMGGetSmoother(), PCMGGetSmootherUp(), PCMGGetSmootherDown(),
958            PCMGSetCycleTypeOnLevel(), PCMGSetRhs(), PCMGSetX(), PCMGSetR()
959 M*/
960 
961 EXTERN_C_BEGIN
962 #undef __FUNCT__
963 #define __FUNCT__ "PCCreate_MG"
964 PetscErrorCode PETSCKSP_DLLEXPORT PCCreate_MG(PC pc)
965 {
966   PC_MG          *mg;
967   PetscErrorCode ierr;
968 
969   PetscFunctionBegin;
970   ierr        = PetscNewLog(pc,PC_MG,&mg);CHKERRQ(ierr);
971   pc->data    = (void*)mg;
972   mg->nlevels = -1;
973 
974   pc->ops->apply          = PCApply_MG;
975   pc->ops->setup          = PCSetUp_MG;
976   pc->ops->destroy        = PCDestroy_MG;
977   pc->ops->setfromoptions = PCSetFromOptions_MG;
978   pc->ops->view           = PCView_MG;
979   PetscFunctionReturn(0);
980 }
981 EXTERN_C_END
982