Actual source code: mg.c
1: #define PETSCKSP_DLL
3: /*
4: Defines the multigrid preconditioner interface.
5: */
6: #include src/ksp/pc/impls/mg/mgimpl.h
11: PetscErrorCode PCMGMCycle_Private(PC_MG **mglevels,PetscTruth *converged)
12: {
13: PC_MG *mg = *mglevels,*mgc;
15: PetscInt cycles = (PetscInt) mg->cycles;
18: if (converged) *converged = PETSC_FALSE;
21: KSPSolve(mg->smoothd,mg->b,mg->x); /* pre-smooth */
23: if (mg->level) { /* not the coarsest grid */
25: (*mg->residual)(mg->A,mg->b,mg->x,mg->r);
28: /* if on finest level and have convergence criteria set */
29: if (mg->level == mg->levels-1 && mg->ttol) {
30: PetscReal rnorm;
31: VecNorm(mg->r,NORM_2,&rnorm);
32: if (rnorm <= mg->ttol) {
33: *converged = PETSC_TRUE;
34: if (rnorm < mg->abstol) {
35: PetscInfo2(0,"Linear solver has converged. Residual norm %G is less than absolute tolerance %G\n",rnorm,mg->abstol);
36: } else {
37: PetscInfo2(0,"Linear solver has converged. Residual norm %G is less than relative tolerance times initial residual norm %G\n",rnorm,mg->ttol);
38: }
39: return(0);
40: }
41: }
43: mgc = *(mglevels - 1);
45: MatRestrict(mg->restrct,mg->r,mgc->b);
47: VecSet(mgc->x,0.0);
48: while (cycles--) {
49: PCMGMCycle_Private(mglevels-1,converged);
50: }
52: MatInterpolateAdd(mg->interpolate,mgc->x,mg->x,mg->x);
55: KSPSolve(mg->smoothu,mg->b,mg->x); /* post smooth */
57: }
58: return(0);
59: }
61: /*
62: PCMGCreate_Private - Creates a PC_MG structure for use with the
63: multigrid code. Level 0 is the coarsest. (But the
64: finest level is stored first in the array).
66: */
69: static PetscErrorCode PCMGCreate_Private(MPI_Comm comm,PetscInt levels,PC pc,MPI_Comm *comms,PC_MG ***result)
70: {
71: PC_MG **mg;
73: PetscInt i;
74: PetscMPIInt size;
75: const char *prefix;
76: PC ipc;
79: PetscMalloc(levels*sizeof(PC_MG*),&mg);
80: PetscLogObjectMemory(pc,levels*(sizeof(PC_MG*)+sizeof(PC_MG)));
82: PCGetOptionsPrefix(pc,&prefix);
84: for (i=0; i<levels; i++) {
85: PetscNew(PC_MG,&mg[i]);
86: mg[i]->level = i;
87: mg[i]->levels = levels;
88: mg[i]->cycles = PC_MG_CYCLE_V;
89: mg[i]->galerkin = PETSC_FALSE;
90: mg[i]->galerkinused = PETSC_FALSE;
91: mg[i]->default_smoothu = 1;
92: mg[i]->default_smoothd = 1;
94: if (comms) comm = comms[i];
95: KSPCreate(comm,&mg[i]->smoothd);
96: KSPSetTolerances(mg[i]->smoothd,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT, mg[i]->default_smoothd);
97: KSPSetOptionsPrefix(mg[i]->smoothd,prefix);
99: /* do special stuff for coarse grid */
100: if (!i && levels > 1) {
101: KSPAppendOptionsPrefix(mg[0]->smoothd,"mg_coarse_");
103: /* coarse solve is (redundant) LU by default */
104: KSPSetType(mg[0]->smoothd,KSPPREONLY);
105: KSPGetPC(mg[0]->smoothd,&ipc);
106: MPI_Comm_size(comm,&size);
107: if (size > 1) {
108: PCSetType(ipc,PCREDUNDANT);
109: } else {
110: PCSetType(ipc,PCLU);
111: }
113: } else {
114: char tprefix[128];
115: sprintf(tprefix,"mg_levels_%d_",(int)i);
116: KSPAppendOptionsPrefix(mg[i]->smoothd,tprefix);
117: }
118: PetscLogObjectParent(pc,mg[i]->smoothd);
119: mg[i]->smoothu = mg[i]->smoothd;
120: mg[i]->rtol = 0.0;
121: mg[i]->abstol = 0.0;
122: mg[i]->dtol = 0.0;
123: mg[i]->ttol = 0.0;
124: mg[i]->eventsmoothsetup = 0;
125: mg[i]->eventsmoothsolve = 0;
126: mg[i]->eventresidual = 0;
127: mg[i]->eventinterprestrict = 0;
128: mg[i]->cyclesperpcapply = 1;
129: }
130: *result = mg;
131: return(0);
132: }
136: static PetscErrorCode PCDestroy_MG(PC pc)
137: {
138: PC_MG **mg = (PC_MG**)pc->data;
140: PetscInt i,n = mg[0]->levels;
143: for (i=0; i<n-1; i++) {
144: if (mg[i+1]->r) {VecDestroy(mg[i+1]->r);}
145: if (mg[i]->b) {VecDestroy(mg[i]->b);}
146: if (mg[i]->x) {VecDestroy(mg[i]->x);}
147: if (mg[i+1]->restrct) {MatDestroy(mg[i+1]->restrct);}
148: if (mg[i+1]->interpolate) {MatDestroy(mg[i+1]->interpolate);}
149: }
151: for (i=0; i<n; i++) {
152: if (mg[i]->smoothd != mg[i]->smoothu) {
153: KSPDestroy(mg[i]->smoothd);
154: }
155: KSPDestroy(mg[i]->smoothu);
156: PetscFree(mg[i]);
157: }
158: PetscFree(mg);
159: return(0);
160: }
164: EXTERN PetscErrorCode PCMGACycle_Private(PC_MG**);
165: EXTERN PetscErrorCode PCMGFCycle_Private(PC_MG**);
166: EXTERN PetscErrorCode PCMGKCycle_Private(PC_MG**);
168: /*
169: PCApply_MG - Runs either an additive, multiplicative, Kaskadic
170: or full cycle of multigrid.
172: Note:
173: A simple wrapper which calls PCMGMCycle(),PCMGACycle(), or PCMGFCycle().
174: */
177: static PetscErrorCode PCApply_MG(PC pc,Vec b,Vec x)
178: {
179: PC_MG **mg = (PC_MG**)pc->data;
181: PetscInt levels = mg[0]->levels,i;
184: mg[levels-1]->b = b;
185: mg[levels-1]->x = x;
186: if (!mg[levels-1]->r && mg[0]->am != PC_MG_ADDITIVE && levels > 1) {
187: Vec tvec;
188: VecDuplicate(mg[levels-1]->b,&tvec);
189: PCMGSetR(pc,levels-1,tvec);
190: VecDestroy(tvec);
191: }
192: if (mg[0]->am == PC_MG_MULTIPLICATIVE) {
193: VecSet(x,0.0);
194: for (i=0; i<mg[0]->cyclesperpcapply; i++) {
195: PCMGMCycle_Private(mg+levels-1,PETSC_NULL);
196: }
197: }
198: else if (mg[0]->am == PC_MG_ADDITIVE) {
199: PCMGACycle_Private(mg);
200: }
201: else if (mg[0]->am == PC_MG_KASKADE) {
202: PCMGKCycle_Private(mg);
203: }
204: else {
205: PCMGFCycle_Private(mg);
206: }
207: return(0);
208: }
212: static PetscErrorCode PCApplyRichardson_MG(PC pc,Vec b,Vec x,Vec w,PetscReal rtol,PetscReal abstol, PetscReal dtol,PetscInt its)
213: {
214: PC_MG **mg = (PC_MG**)pc->data;
216: PetscInt levels = mg[0]->levels;
217: PetscTruth converged = PETSC_FALSE;
220: mg[levels-1]->b = b;
221: mg[levels-1]->x = x;
223: mg[levels-1]->rtol = rtol;
224: mg[levels-1]->abstol = abstol;
225: mg[levels-1]->dtol = dtol;
226: if (rtol) {
227: /* compute initial residual norm for relative convergence test */
228: PetscReal rnorm;
229: (*mg[levels-1]->residual)(mg[levels-1]->A,b,x,w);
230: VecNorm(w,NORM_2,&rnorm);
231: mg[levels-1]->ttol = PetscMax(rtol*rnorm,abstol);
232: } else if (abstol) {
233: mg[levels-1]->ttol = abstol;
234: } else {
235: mg[levels-1]->ttol = 0.0;
236: }
238: while (its-- && !converged) {
239: PCMGMCycle_Private(mg+levels-1,&converged);
240: }
241: return(0);
242: }
246: PetscErrorCode PCSetFromOptions_MG(PC pc)
247: {
249: PetscInt m,levels = 1,cycles;
250: PetscTruth flg;
251: PC_MG **mg = (PC_MG**)pc->data;
252: PCMGType mgtype = PC_MG_ADDITIVE;
253: PCMGCycleType mgctype;
256: PetscOptionsHead("Multigrid options");
257: if (!pc->data) {
258: PetscOptionsInt("-pc_mg_levels","Number of Levels","PCMGSetLevels",levels,&levels,&flg);
259: PCMGSetLevels(pc,levels,PETSC_NULL);
260: mg = (PC_MG**)pc->data;
261: }
262: mgctype = (PCMGCycleType) mg[0]->cycles;
263: PetscOptionsEnum("-pc_mg_cycle_type","V cycle or for W-cycle","PCMGSetCycleType",PCMGCycleTypes,(PetscEnum)mgctype,(PetscEnum*)&mgctype,&flg);
264: if (flg) {
265: PCMGSetCycleType(pc,mgctype);
266: };
267: PetscOptionsName("-pc_mg_galerkin","Use Galerkin process to compute coarser operators","PCMGSetGalerkin",&flg);
268: if (flg) {
269: PCMGSetGalerkin(pc);
270: }
271: PetscOptionsInt("-pc_mg_smoothup","Number of post-smoothing steps","PCMGSetNumberSmoothUp",1,&m,&flg);
272: if (flg) {
273: PCMGSetNumberSmoothUp(pc,m);
274: }
275: PetscOptionsInt("-pc_mg_smoothdown","Number of pre-smoothing steps","PCMGSetNumberSmoothDown",1,&m,&flg);
276: if (flg) {
277: PCMGSetNumberSmoothDown(pc,m);
278: }
279: PetscOptionsEnum("-pc_mg_type","Multigrid type","PCMGSetType",PCMGTypes,(PetscEnum)mgtype,(PetscEnum*)&mgtype,&flg);
280: if (flg) {
281: PCMGSetType(pc,mgtype);
282: }
283: if (mg[0]->am == PC_MG_MULTIPLICATIVE) {
284: PetscOptionsInt("-pc_mg_multiplicative_cycles","Number of cycles for each preconditioner step","PCMGSetLevels",mg[0]->cyclesperpcapply,&cycles,&flg);
285: if (flg) {
286: PCMGMultiplicativeSetCycles(pc,cycles);
287: }
288: }
289: PetscOptionsName("-pc_mg_log","Log times for each multigrid level","None",&flg);
290: if (flg) {
291: PetscInt i;
292: char eventname[128];
293: if (!mg) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
294: levels = mg[0]->levels;
295: for (i=0; i<levels; i++) {
296: sprintf(eventname,"MGSetup Level %d",(int)i);
298: sprintf(eventname,"MGSmooth Level %d",(int)i);
300: if (i) {
301: sprintf(eventname,"MGResid Level %d",(int)i);
303: sprintf(eventname,"MGInterp Level %d",(int)i);
305: }
306: }
307: }
308: PetscOptionsTail();
309: return(0);
310: }
312: const char *PCMGTypes[] = {"MULTIPLICATIVE","ADDITIVE","FULL","KASKADE","PCMGType","PC_MG",0};
313: const char *PCMGCycleTypes[] = {"invalid","v","w","PCMGCycleType","PC_MG_CYCLE",0};
317: static PetscErrorCode PCView_MG(PC pc,PetscViewer viewer)
318: {
319: PC_MG **mg = (PC_MG**)pc->data;
321: PetscInt levels = mg[0]->levels,i;
322: PetscTruth iascii;
325: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
326: if (iascii) {
327: PetscViewerASCIIPrintf(viewer," MG: type is %s, levels=%D cycles=%s, pre-smooths=%D, post-smooths=%D\n",
328: PCMGTypes[mg[0]->am],levels,(mg[0]->cycles == PC_MG_CYCLE_V) ? "v" : "w",
329: mg[0]->default_smoothd,mg[0]->default_smoothu);
330: if (mg[0]->galerkin) {
331: PetscViewerASCIIPrintf(viewer," Using Galerkin computed coarse grid matrices\n");
332: }
333: for (i=0; i<levels; i++) {
334: if (!i) {
335: PetscViewerASCIIPrintf(viewer,"Coarse gride solver -- level %D -------------------------------\n",i);
336: } else {
337: PetscViewerASCIIPrintf(viewer,"Down solver (pre-smoother) on level %D -------------------------------\n",i);
338: }
339: PetscViewerASCIIPushTab(viewer);
340: KSPView(mg[i]->smoothd,viewer);
341: PetscViewerASCIIPopTab(viewer);
342: if (i && mg[i]->smoothd == mg[i]->smoothu) {
343: PetscViewerASCIIPrintf(viewer,"Up solver (post-smoother) same as down solver (pre-smoother)\n");
344: } else if (i){
345: PetscViewerASCIIPrintf(viewer,"Up solver (post-smoother) on level %D -------------------------------\n",i);
346: PetscViewerASCIIPushTab(viewer);
347: KSPView(mg[i]->smoothu,viewer);
348: PetscViewerASCIIPopTab(viewer);
349: }
350: }
351: } else {
352: SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported for PCMG",((PetscObject)viewer)->type_name);
353: }
354: return(0);
355: }
357: /*
358: Calls setup for the KSP on each level
359: */
362: static PetscErrorCode PCSetUp_MG(PC pc)
363: {
364: PC_MG **mg = (PC_MG**)pc->data;
365: PetscErrorCode ierr;
366: PetscInt i,n = mg[0]->levels;
367: PC cpc,mpc;
368: PetscTruth preonly,lu,redundant,cholesky,monitor = PETSC_FALSE,dump,opsset;
369: PetscViewerASCIIMonitor ascii;
370: PetscViewer viewer = PETSC_NULL;
371: MPI_Comm comm;
372: Mat dA,dB;
373: MatStructure uflag;
374: Vec tvec;
378: /* If user did not provide fine grid operators OR operator was not updated since last global KSPSetOperators() */
379: /* so use those from global PC */
380: /* Is this what we always want? What if user wants to keep old one? */
381: KSPGetOperatorsSet(mg[n-1]->smoothd,PETSC_NULL,&opsset);
382: KSPGetPC(mg[0]->smoothd,&cpc);
383: KSPGetPC(mg[n-1]->smoothd,&mpc);
384: if (!opsset || ((cpc->setupcalled == 1) && (mpc->setupcalled == 2))) {
385: PetscInfo(pc,"Using outer operators to define finest grid operator \n because PCMGGetSmoother(pc,nlevels-1,&ksp);KSPSetOperators(ksp,...); was not called.\n");
386: KSPSetOperators(mg[n-1]->smoothd,pc->mat,pc->pmat,pc->flag);
387: }
389: if (mg[0]->galerkin) {
390: Mat B;
391: mg[0]->galerkinused = PETSC_TRUE;
392: /* currently only handle case where mat and pmat are the same on coarser levels */
393: KSPGetOperators(mg[n-1]->smoothd,&dA,&dB,&uflag);
394: if (!pc->setupcalled) {
395: for (i=n-2; i>-1; i--) {
396: MatPtAP(dB,mg[i+1]->interpolate,MAT_INITIAL_MATRIX,1.0,&B);
397: KSPSetOperators(mg[i]->smoothd,B,B,uflag);
398: if (i != n-2) {PetscObjectDereference((PetscObject)dB);}
399: dB = B;
400: }
401: PetscObjectDereference((PetscObject)dB);
402: } else {
403: for (i=n-2; i>-1; i--) {
404: KSPGetOperators(mg[i]->smoothd,PETSC_NULL,&B,PETSC_NULL);
405: MatPtAP(dB,mg[i+1]->interpolate,MAT_REUSE_MATRIX,1.0,&B);
406: KSPSetOperators(mg[i]->smoothd,B,B,uflag);
407: dB = B;
408: }
409: }
410: }
412: if (!pc->setupcalled) {
413: PetscOptionsHasName(0,"-pc_mg_monitor",&monitor);
414:
415: for (i=0; i<n; i++) {
416: if (monitor) {
417: PetscObjectGetComm((PetscObject)mg[i]->smoothd,&comm);
418: PetscViewerASCIIMonitorCreate(comm,"stdout",n-i,&ascii);
419: KSPMonitorSet(mg[i]->smoothd,KSPMonitorDefault,ascii,(PetscErrorCode(*)(void*))PetscViewerASCIIMonitorDestroy);
420: }
421: KSPSetFromOptions(mg[i]->smoothd);
422: }
423: for (i=1; i<n; i++) {
424: if (mg[i]->smoothu && (mg[i]->smoothu != mg[i]->smoothd)) {
425: if (monitor) {
426: PetscObjectGetComm((PetscObject)mg[i]->smoothu,&comm);
427: PetscViewerASCIIMonitorCreate(comm,"stdout",n-i,&ascii);
428: KSPMonitorSet(mg[i]->smoothu,KSPMonitorDefault,ascii,(PetscErrorCode(*)(void*))PetscViewerASCIIMonitorDestroy);
429: }
430: KSPSetFromOptions(mg[i]->smoothu);
431: }
432: }
433: for (i=1; i<n; i++) {
434: if (!mg[i]->residual) {
435: Mat mat;
436: KSPGetOperators(mg[i]->smoothd,PETSC_NULL,&mat,PETSC_NULL);
437: PCMGSetResidual(pc,i,PCMGDefaultResidual,mat);
438: }
439: if (mg[i]->restrct && !mg[i]->interpolate) {
440: PCMGSetInterpolation(pc,i,mg[i]->restrct);
441: }
442: if (!mg[i]->restrct && mg[i]->interpolate) {
443: PCMGSetRestriction(pc,i,mg[i]->interpolate);
444: }
445: #if defined(PETSC_USE_DEBUG)
446: if (!mg[i]->restrct || !mg[i]->interpolate) {
447: SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Need to set restriction or interpolation on level %d",(int)i);
448: }
449: #endif
450: }
451: for (i=0; i<n-1; i++) {
452: if (!mg[i]->b) {
453: Vec *vec;
454: KSPGetVecs(mg[i]->smoothd,1,&vec,0,PETSC_NULL);
455: PCMGSetRhs(pc,i,*vec);
456: VecDestroy(*vec);
457: PetscFree(vec);
458: }
459: if (!mg[i]->r && i) {
460: VecDuplicate(mg[i]->b,&tvec);
461: PCMGSetR(pc,i,tvec);
462: VecDestroy(tvec);
463: }
464: if (!mg[i]->x) {
465: VecDuplicate(mg[i]->b,&tvec);
466: PCMGSetX(pc,i,tvec);
467: VecDestroy(tvec);
468: }
469: }
470: }
473: for (i=1; i<n; i++) {
474: if (mg[i]->smoothu == mg[i]->smoothd) {
475: /* if doing only down then initial guess is zero */
476: KSPSetInitialGuessNonzero(mg[i]->smoothd,PETSC_TRUE);
477: }
479: KSPSetUp(mg[i]->smoothd);
481: }
482: for (i=1; i<n; i++) {
483: if (mg[i]->smoothu && mg[i]->smoothu != mg[i]->smoothd) {
484: Mat downmat,downpmat;
485: MatStructure matflag;
486: PetscTruth opsset;
488: /* check if operators have been set for up, if not use down operators to set them */
489: KSPGetOperatorsSet(mg[i]->smoothu,&opsset,PETSC_NULL);
490: if (!opsset) {
491: KSPGetOperators(mg[i]->smoothd,&downmat,&downpmat,&matflag);
492: KSPSetOperators(mg[i]->smoothu,downmat,downpmat,matflag);
493: }
495: KSPSetInitialGuessNonzero(mg[i]->smoothu,PETSC_TRUE);
497: KSPSetUp(mg[i]->smoothu);
499: }
500: }
502: /*
503: If coarse solver is not direct method then DO NOT USE preonly
504: */
505: PetscTypeCompare((PetscObject)mg[0]->smoothd,KSPPREONLY,&preonly);
506: if (preonly) {
507: PetscTypeCompare((PetscObject)cpc,PCLU,&lu);
508: PetscTypeCompare((PetscObject)cpc,PCREDUNDANT,&redundant);
509: PetscTypeCompare((PetscObject)cpc,PCCHOLESKY,&cholesky);
510: if (!lu && !redundant && !cholesky) {
511: KSPSetType(mg[0]->smoothd,KSPGMRES);
512: }
513: }
515: if (!pc->setupcalled) {
516: if (monitor) {
517: PetscObjectGetComm((PetscObject)mg[0]->smoothd,&comm);
518: PetscViewerASCIIMonitorCreate(comm,"stdout",n,&ascii);
519: KSPMonitorSet(mg[0]->smoothd,KSPMonitorDefault,ascii,(PetscErrorCode(*)(void*))PetscViewerASCIIMonitorDestroy);
520: }
521: KSPSetFromOptions(mg[0]->smoothd);
522: }
525: KSPSetUp(mg[0]->smoothd);
528: /*
529: Dump the interpolation/restriction matrices plus the
530: Jacobian/stiffness on each level. This allows Matlab users to
531: easily check if the Galerkin condition A_c = R A_f R^T is satisfied.
533: Only support one or the other at the same time.
534: */
535: #if defined(PETSC_USE_SOCKET_VIEWER)
536: PetscOptionsHasName(pc->prefix,"-pc_mg_dump_matlab",&dump);
537: if (dump) {
538: viewer = PETSC_VIEWER_SOCKET_(pc->comm);
539: }
540: #endif
541: PetscOptionsHasName(pc->prefix,"-pc_mg_dump_binary",&dump);
542: if (dump) {
543: viewer = PETSC_VIEWER_BINARY_(pc->comm);
544: }
546: if (viewer) {
547: for (i=1; i<n; i++) {
548: MatView(mg[i]->restrct,viewer);
549: }
550: for (i=0; i<n; i++) {
551: KSPGetPC(mg[i]->smoothd,&pc);
552: MatView(pc->mat,viewer);
553: }
554: }
555: return(0);
556: }
558: /* -------------------------------------------------------------------------------------*/
562: /*@C
563: PCMGSetLevels - Sets the number of levels to use with MG.
564: Must be called before any other MG routine.
566: Collective on PC
568: Input Parameters:
569: + pc - the preconditioner context
570: . levels - the number of levels
571: - comms - optional communicators for each level; this is to allow solving the coarser problems
572: on smaller sets of processors. Use PETSC_NULL_OBJECT for default in Fortran
574: Level: intermediate
576: Notes:
577: If the number of levels is one then the multigrid uses the -mg_levels prefix
578: for setting the level options rather than the -mg_coarse prefix.
580: .keywords: MG, set, levels, multigrid
582: .seealso: PCMGSetType(), PCMGGetLevels()
583: @*/
584: PetscErrorCode PCMGSetLevels(PC pc,PetscInt levels,MPI_Comm *comms)
585: {
587: PC_MG **mg=0;
592: if (pc->data) {
593: SETERRQ(PETSC_ERR_ORDER,"Number levels already set for MG\n\
594: make sure that you call PCMGSetLevels() before KSPSetFromOptions()");
595: }
596: PCMGCreate_Private(pc->comm,levels,pc,comms,&mg);
597: mg[0]->am = PC_MG_MULTIPLICATIVE;
598: pc->data = (void*)mg;
599: pc->ops->applyrichardson = PCApplyRichardson_MG;
600: return(0);
601: }
605: /*@
606: PCMGGetLevels - Gets the number of levels to use with MG.
608: Not Collective
610: Input Parameter:
611: . pc - the preconditioner context
613: Output parameter:
614: . levels - the number of levels
616: Level: advanced
618: .keywords: MG, get, levels, multigrid
620: .seealso: PCMGSetLevels()
621: @*/
622: PetscErrorCode PCMGGetLevels(PC pc,PetscInt *levels)
623: {
624: PC_MG **mg;
630: mg = (PC_MG**)pc->data;
631: *levels = mg[0]->levels;
632: return(0);
633: }
637: /*@
638: PCMGSetType - Determines the form of multigrid to use:
639: multiplicative, additive, full, or the Kaskade algorithm.
641: Collective on PC
643: Input Parameters:
644: + pc - the preconditioner context
645: - form - multigrid form, one of PC_MG_MULTIPLICATIVE, PC_MG_ADDITIVE,
646: PC_MG_FULL, PC_MG_KASKADE
648: Options Database Key:
649: . -pc_mg_type <form> - Sets <form>, one of multiplicative,
650: additive, full, kaskade
652: Level: advanced
654: .keywords: MG, set, method, multiplicative, additive, full, Kaskade, multigrid
656: .seealso: PCMGSetLevels()
657: @*/
658: PetscErrorCode PCMGSetType(PC pc,PCMGType form)
659: {
660: PC_MG **mg;
664: mg = (PC_MG**)pc->data;
666: if (!mg) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
667: mg[0]->am = form;
668: if (form == PC_MG_MULTIPLICATIVE) pc->ops->applyrichardson = PCApplyRichardson_MG;
669: else pc->ops->applyrichardson = 0;
670: return(0);
671: }
675: /*@
676: PCMGSetCycleType - Sets the type cycles to use. Use PCMGSetCycleTypeOnLevel() for more
677: complicated cycling.
679: Collective on PC
681: Input Parameters:
682: + pc - the multigrid context
683: - PC_MG_CYCLE_V or PC_MG_CYCLE_W
685: Options Database Key:
686: $ -pc_mg_cycle_type v or w
688: Level: advanced
690: .keywords: MG, set, cycles, V-cycle, W-cycle, multigrid
692: .seealso: PCMGSetCycleTypeOnLevel()
693: @*/
694: PetscErrorCode PCMGSetCycleType(PC pc,PCMGCycleType n)
695: {
696: PC_MG **mg;
697: PetscInt i,levels;
701: mg = (PC_MG**)pc->data;
702: if (!mg) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
703: levels = mg[0]->levels;
705: for (i=0; i<levels; i++) {
706: mg[i]->cycles = n;
707: }
708: return(0);
709: }
713: /*@
714: PCMGMultiplicativeSetCycles - Sets the number of cycles to use for each preconditioner step
715: of multigrid when PCMGType of PC_MG_MULTIPLICATIVE is used
717: Collective on PC
719: Input Parameters:
720: + pc - the multigrid context
721: - n - number of cycles (default is 1)
723: Options Database Key:
724: $ -pc_mg_multiplicative_cycles n
726: Level: advanced
728: Notes: This is not associated with setting a v or w cycle, that is set with PCMGSetCycleType()
730: .keywords: MG, set, cycles, V-cycle, W-cycle, multigrid
732: .seealso: PCMGSetCycleTypeOnLevel(), PCMGSetCycleType()
733: @*/
734: PetscErrorCode PCMGMultiplicativeSetCycles(PC pc,PetscInt n)
735: {
736: PC_MG **mg;
737: PetscInt i,levels;
741: mg = (PC_MG**)pc->data;
742: if (!mg) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
743: levels = mg[0]->levels;
745: for (i=0; i<levels; i++) {
746: mg[i]->cyclesperpcapply = n;
747: }
748: return(0);
749: }
753: /*@
754: PCMGSetGalerkin - Causes the coarser grid matrices to be computed from the
755: finest grid via the Galerkin process: A_i-1 = r_i * A_i * r_i^t
757: Collective on PC
759: Input Parameters:
760: . pc - the multigrid context
762: Options Database Key:
763: $ -pc_mg_galerkin
765: Level: intermediate
767: .keywords: MG, set, Galerkin
769: .seealso: PCMGGetGalerkin()
771: @*/
772: PetscErrorCode PCMGSetGalerkin(PC pc)
773: {
774: PC_MG **mg;
775: PetscInt i,levels;
779: mg = (PC_MG**)pc->data;
780: if (!mg) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
781: levels = mg[0]->levels;
783: for (i=0; i<levels; i++) {
784: mg[i]->galerkin = PETSC_TRUE;
785: }
786: return(0);
787: }
791: /*@
792: PCMGGetGalerkin - Checks if Galerkin multigrid is being used, i.e.
793: A_i-1 = r_i * A_i * r_i^t
795: Not Collective
797: Input Parameter:
798: . pc - the multigrid context
800: Output Parameter:
801: . gelerkin - PETSC_TRUE or PETSC_FALSE
803: Options Database Key:
804: $ -pc_mg_galerkin
806: Level: intermediate
808: .keywords: MG, set, Galerkin
810: .seealso: PCMGSetGalerkin()
812: @*/
813: PetscErrorCode PCMGGetGalerkin(PC pc,PetscTruth *galerkin)
814: {
815: PC_MG **mg;
819: mg = (PC_MG**)pc->data;
820: if (!mg) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
821: *galerkin = mg[0]->galerkin;
822: return(0);
823: }
827: /*@
828: PCMGSetNumberSmoothDown - Sets the number of pre-smoothing steps to
829: use on all levels. Use PCMGGetSmootherDown() to set different
830: pre-smoothing steps on different levels.
832: Collective on PC
834: Input Parameters:
835: + mg - the multigrid context
836: - n - the number of smoothing steps
838: Options Database Key:
839: . -pc_mg_smoothdown <n> - Sets number of pre-smoothing steps
841: Level: advanced
843: .keywords: MG, smooth, down, pre-smoothing, steps, multigrid
845: .seealso: PCMGSetNumberSmoothUp()
846: @*/
847: PetscErrorCode PCMGSetNumberSmoothDown(PC pc,PetscInt n)
848: {
849: PC_MG **mg;
851: PetscInt i,levels;
855: mg = (PC_MG**)pc->data;
856: if (!mg) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
857: levels = mg[0]->levels;
859: for (i=1; i<levels; i++) {
860: /* make sure smoother up and down are different */
861: PCMGGetSmootherUp(pc,i,PETSC_NULL);
862: KSPSetTolerances(mg[i]->smoothd,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,n);
863: mg[i]->default_smoothd = n;
864: }
865: return(0);
866: }
870: /*@
871: PCMGSetNumberSmoothUp - Sets the number of post-smoothing steps to use
872: on all levels. Use PCMGGetSmootherUp() to set different numbers of
873: post-smoothing steps on different levels.
875: Collective on PC
877: Input Parameters:
878: + mg - the multigrid context
879: - n - the number of smoothing steps
881: Options Database Key:
882: . -pc_mg_smoothup <n> - Sets number of post-smoothing steps
884: Level: advanced
886: Note: this does not set a value on the coarsest grid, since we assume that
887: there is no separate smooth up on the coarsest grid.
889: .keywords: MG, smooth, up, post-smoothing, steps, multigrid
891: .seealso: PCMGSetNumberSmoothDown()
892: @*/
893: PetscErrorCode PCMGSetNumberSmoothUp(PC pc,PetscInt n)
894: {
895: PC_MG **mg;
897: PetscInt i,levels;
901: mg = (PC_MG**)pc->data;
902: if (!mg) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must set MG levels before calling");
903: levels = mg[0]->levels;
905: for (i=1; i<levels; i++) {
906: /* make sure smoother up and down are different */
907: PCMGGetSmootherUp(pc,i,PETSC_NULL);
908: KSPSetTolerances(mg[i]->smoothu,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,n);
909: mg[i]->default_smoothu = n;
910: }
911: return(0);
912: }
914: /* ----------------------------------------------------------------------------------------*/
916: /*MC
917: PCMG - Use multigrid preconditioning. This preconditioner requires you provide additional
918: information about the coarser grid matrices and restriction/interpolation operators.
920: Options Database Keys:
921: + -pc_mg_levels <nlevels> - number of levels including finest
922: . -pc_mg_cycles v or w
923: . -pc_mg_smoothup <n> - number of smoothing steps after interpolation
924: . -pc_mg_smoothdown <n> - number of smoothing steps before applying restriction operator
925: . -pc_mg_type <additive,multiplicative,full,cascade> - multiplicative is the default
926: . -pc_mg_log - log information about time spent on each level of the solver
927: . -pc_mg_monitor - print information on the multigrid convergence
928: . -pc_mg_galerkin - use Galerkin process to compute coarser operators
929: - -pc_mg_dump_matlab - dumps the matrices for each level and the restriction/interpolation matrices
930: to the Socket viewer for reading from Matlab.
932: Notes:
934: Level: intermediate
936: Concepts: multigrid/multilevel
938: .seealso: PCCreate(), PCSetType(), PCType (for list of available types), PC, PCMGType,
939: PCMGSetLevels(), PCMGGetLevels(), PCMGSetType(), PCMGSetCycleType(), PCMGSetNumberSmoothDown(),
940: PCMGSetNumberSmoothUp(), PCMGGetCoarseSolve(), PCMGSetResidual(), PCMGSetInterpolation(),
941: PCMGSetRestriction(), PCMGGetSmoother(), PCMGGetSmootherUp(), PCMGGetSmootherDown(),
942: PCMGSetCycleTypeOnLevel(), PCMGSetRhs(), PCMGSetX(), PCMGSetR()
943: M*/
948: PetscErrorCode PCCreate_MG(PC pc)
949: {
951: pc->ops->apply = PCApply_MG;
952: pc->ops->setup = PCSetUp_MG;
953: pc->ops->destroy = PCDestroy_MG;
954: pc->ops->setfromoptions = PCSetFromOptions_MG;
955: pc->ops->view = PCView_MG;
957: pc->data = (void*)0;
958: return(0);
959: }