Actual source code: mg.c
1: /*
2: Defines the multigrid preconditioner interface.
3: */
4: #include <petsc/private/pcmgimpl.h>
5: #include <petsc/private/kspimpl.h>
6: #include <petscdm.h>
7: PETSC_INTERN PetscErrorCode PCPreSolveChangeRHS(PC, PetscBool *);
9: /*
10: Contains the list of registered coarse space construction routines
11: */
12: PetscFunctionList PCMGCoarseList = NULL;
14: PetscErrorCode PCMGMCycle_Private(PC pc, PC_MG_Levels **mglevelsin, PetscBool transpose, PetscBool matapp, PCRichardsonConvergedReason *reason)
15: {
16: PC_MG *mg = (PC_MG *)pc->data;
17: PC_MG_Levels *mgc, *mglevels = *mglevelsin;
18: PetscInt cycles = (mglevels->level == 1) ? 1 : mglevels->cycles;
20: PetscFunctionBegin;
21: if (mglevels->eventsmoothsolve) PetscCall(PetscLogEventBegin(mglevels->eventsmoothsolve, 0, 0, 0, 0));
22: if (!transpose) {
23: if (matapp) {
24: PetscCall(KSPMatSolve(mglevels->smoothd, mglevels->B, mglevels->X)); /* pre-smooth */
25: PetscCall(KSPCheckSolve(mglevels->smoothd, pc, NULL));
26: } else {
27: PetscCall(KSPSolve(mglevels->smoothd, mglevels->b, mglevels->x)); /* pre-smooth */
28: PetscCall(KSPCheckSolve(mglevels->smoothd, pc, mglevels->x));
29: }
30: } else {
31: PetscCheck(!matapp, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Not supported");
32: PetscCall(KSPSolveTranspose(mglevels->smoothu, mglevels->b, mglevels->x)); /* transpose of post-smooth */
33: PetscCall(KSPCheckSolve(mglevels->smoothu, pc, mglevels->x));
34: }
35: if (mglevels->eventsmoothsolve) PetscCall(PetscLogEventEnd(mglevels->eventsmoothsolve, 0, 0, 0, 0));
36: if (mglevels->level) { /* not the coarsest grid */
37: if (mglevels->eventresidual) PetscCall(PetscLogEventBegin(mglevels->eventresidual, 0, 0, 0, 0));
38: if (matapp && !mglevels->R) PetscCall(MatDuplicate(mglevels->B, MAT_DO_NOT_COPY_VALUES, &mglevels->R));
39: if (!transpose) {
40: if (matapp) PetscCall((*mglevels->matresidual)(mglevels->A, mglevels->B, mglevels->X, mglevels->R));
41: else PetscCall((*mglevels->residual)(mglevels->A, mglevels->b, mglevels->x, mglevels->r));
42: } else {
43: if (matapp) PetscCall((*mglevels->matresidualtranspose)(mglevels->A, mglevels->B, mglevels->X, mglevels->R));
44: else PetscCall((*mglevels->residualtranspose)(mglevels->A, mglevels->b, mglevels->x, mglevels->r));
45: }
46: if (mglevels->eventresidual) PetscCall(PetscLogEventEnd(mglevels->eventresidual, 0, 0, 0, 0));
48: /* if on finest level and have convergence criteria set */
49: if (mglevels->level == mglevels->levels - 1 && mg->ttol && reason) {
50: PetscReal rnorm;
52: PetscCall(VecNorm(mglevels->r, NORM_2, &rnorm));
53: if (rnorm <= mg->ttol) {
54: if (rnorm < mg->abstol) {
55: *reason = PCRICHARDSON_CONVERGED_ATOL;
56: PetscCall(PetscInfo(pc, "Linear solver has converged. Residual norm %g is less than absolute tolerance %g\n", (double)rnorm, (double)mg->abstol));
57: } else {
58: *reason = PCRICHARDSON_CONVERGED_RTOL;
59: PetscCall(PetscInfo(pc, "Linear solver has converged. Residual norm %g is less than relative tolerance times initial residual norm %g\n", (double)rnorm, (double)mg->ttol));
60: }
61: PetscFunctionReturn(PETSC_SUCCESS);
62: }
63: }
65: mgc = *(mglevelsin - 1);
66: if (mglevels->eventinterprestrict) PetscCall(PetscLogEventBegin(mglevels->eventinterprestrict, 0, 0, 0, 0));
67: if (!transpose) {
68: if (matapp) PetscCall(MatMatRestrict(mglevels->restrct, mglevels->R, &mgc->B));
69: else PetscCall(MatRestrict(mglevels->restrct, mglevels->r, mgc->b));
70: } else {
71: if (matapp) PetscCall(MatMatRestrict(mglevels->interpolate, mglevels->R, &mgc->B));
72: else PetscCall(MatRestrict(mglevels->interpolate, mglevels->r, mgc->b));
73: }
74: if (mglevels->eventinterprestrict) PetscCall(PetscLogEventEnd(mglevels->eventinterprestrict, 0, 0, 0, 0));
75: if (matapp) {
76: if (!mgc->X) PetscCall(MatDuplicate(mgc->B, MAT_DO_NOT_COPY_VALUES, &mgc->X));
77: else {
78: PetscCall(MatZeroEntries(mgc->X));
79: }
80: } else {
81: PetscCall(VecZeroEntries(mgc->x));
82: }
83: while (cycles--) PetscCall(PCMGMCycle_Private(pc, mglevelsin - 1, transpose, matapp, reason));
84: if (mglevels->eventinterprestrict) PetscCall(PetscLogEventBegin(mglevels->eventinterprestrict, 0, 0, 0, 0));
85: if (!transpose) {
86: if (matapp) PetscCall(MatMatInterpolateAdd(mglevels->interpolate, mgc->X, mglevels->X, &mglevels->X));
87: else PetscCall(MatInterpolateAdd(mglevels->interpolate, mgc->x, mglevels->x, mglevels->x));
88: } else {
89: PetscCall(MatInterpolateAdd(mglevels->restrct, mgc->x, mglevels->x, mglevels->x));
90: }
91: if (mglevels->eventinterprestrict) PetscCall(PetscLogEventEnd(mglevels->eventinterprestrict, 0, 0, 0, 0));
92: if (mglevels->eventsmoothsolve) PetscCall(PetscLogEventBegin(mglevels->eventsmoothsolve, 0, 0, 0, 0));
93: if (!transpose) {
94: if (matapp) {
95: PetscCall(KSPMatSolve(mglevels->smoothu, mglevels->B, mglevels->X)); /* post smooth */
96: PetscCall(KSPCheckSolve(mglevels->smoothu, pc, NULL));
97: } else {
98: PetscCall(KSPSolve(mglevels->smoothu, mglevels->b, mglevels->x)); /* post smooth */
99: PetscCall(KSPCheckSolve(mglevels->smoothu, pc, mglevels->x));
100: }
101: } else {
102: PetscCheck(!matapp, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Not supported");
103: PetscCall(KSPSolveTranspose(mglevels->smoothd, mglevels->b, mglevels->x)); /* post smooth */
104: PetscCall(KSPCheckSolve(mglevels->smoothd, pc, mglevels->x));
105: }
106: if (mglevels->cr) {
107: Mat crA;
109: PetscCheck(!matapp, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Not supported");
110: /* TODO Turn on copy and turn off noisy if we have an exact solution
111: PetscCall(VecCopy(mglevels->x, mglevels->crx));
112: PetscCall(VecCopy(mglevels->b, mglevels->crb)); */
113: PetscCall(KSPGetOperators(mglevels->cr, &crA, NULL));
114: PetscCall(KSPSetNoisy_Private(crA, mglevels->crx));
115: PetscCall(KSPSolve(mglevels->cr, mglevels->crb, mglevels->crx)); /* compatible relaxation */
116: PetscCall(KSPCheckSolve(mglevels->cr, pc, mglevels->crx));
117: }
118: if (mglevels->eventsmoothsolve) PetscCall(PetscLogEventEnd(mglevels->eventsmoothsolve, 0, 0, 0, 0));
119: }
120: PetscFunctionReturn(PETSC_SUCCESS);
121: }
123: static PetscErrorCode PCApplyRichardson_MG(PC pc, Vec b, Vec x, Vec w, PetscReal rtol, PetscReal abstol, PetscReal dtol, PetscInt its, PetscBool zeroguess, PetscInt *outits, PCRichardsonConvergedReason *reason)
124: {
125: PC_MG *mg = (PC_MG *)pc->data;
126: PC_MG_Levels **mglevels = mg->levels;
127: PC tpc;
128: PetscBool changeu, changed;
129: PetscInt levels = mglevels[0]->levels, i;
131: PetscFunctionBegin;
132: /* When the DM is supplying the matrix then it will not exist until here */
133: for (i = 0; i < levels; i++) {
134: if (!mglevels[i]->A) {
135: PetscCall(KSPGetOperators(mglevels[i]->smoothu, &mglevels[i]->A, NULL));
136: PetscCall(PetscObjectReference((PetscObject)mglevels[i]->A));
137: }
138: }
140: PetscCall(KSPGetPC(mglevels[levels - 1]->smoothd, &tpc));
141: PetscCall(PCPreSolveChangeRHS(tpc, &changed));
142: PetscCall(KSPGetPC(mglevels[levels - 1]->smoothu, &tpc));
143: PetscCall(PCPreSolveChangeRHS(tpc, &changeu));
144: if (!changed && !changeu) {
145: PetscCall(VecDestroy(&mglevels[levels - 1]->b));
146: mglevels[levels - 1]->b = b;
147: } else { /* if the smoother changes the rhs during PreSolve, we cannot use the input vector */
148: if (!mglevels[levels - 1]->b) {
149: Vec *vec;
151: PetscCall(KSPCreateVecs(mglevels[levels - 1]->smoothd, 1, &vec, 0, NULL));
152: mglevels[levels - 1]->b = *vec;
153: PetscCall(PetscFree(vec));
154: }
155: PetscCall(VecCopy(b, mglevels[levels - 1]->b));
156: }
157: mglevels[levels - 1]->x = x;
159: mg->rtol = rtol;
160: mg->abstol = abstol;
161: mg->dtol = dtol;
162: if (rtol) {
163: /* compute initial residual norm for relative convergence test */
164: PetscReal rnorm;
166: if (zeroguess) {
167: PetscCall(VecNorm(b, NORM_2, &rnorm));
168: } else {
169: PetscCall((*mglevels[levels - 1]->residual)(mglevels[levels - 1]->A, b, x, w));
170: PetscCall(VecNorm(w, NORM_2, &rnorm));
171: }
172: mg->ttol = PetscMax(rtol * rnorm, abstol);
173: } else if (abstol) mg->ttol = abstol;
174: else mg->ttol = 0.0;
176: /* since smoother is applied to full system, not just residual we need to make sure that smoothers don't
177: stop prematurely due to small residual */
178: for (i = 1; i < levels; i++) {
179: PetscCall(KSPSetTolerances(mglevels[i]->smoothu, 0, PETSC_CURRENT, PETSC_CURRENT, PETSC_CURRENT));
180: if (mglevels[i]->smoothu != mglevels[i]->smoothd) {
181: /* For Richardson the initial guess is nonzero since it is solving in each cycle the original system not just applying as a preconditioner */
182: PetscCall(KSPSetInitialGuessNonzero(mglevels[i]->smoothd, PETSC_TRUE));
183: PetscCall(KSPSetTolerances(mglevels[i]->smoothd, 0, PETSC_CURRENT, PETSC_CURRENT, PETSC_CURRENT));
184: }
185: }
187: *reason = PCRICHARDSON_NOT_SET;
188: for (i = 0; i < its; i++) {
189: PetscCall(PCMGMCycle_Private(pc, mglevels + levels - 1, PETSC_FALSE, PETSC_FALSE, reason));
190: if (*reason) break;
191: }
192: if (*reason == PCRICHARDSON_NOT_SET) *reason = PCRICHARDSON_CONVERGED_ITS;
193: *outits = i;
194: if (!changed && !changeu) mglevels[levels - 1]->b = NULL;
195: PetscFunctionReturn(PETSC_SUCCESS);
196: }
198: PetscErrorCode PCReset_MG(PC pc)
199: {
200: PC_MG *mg = (PC_MG *)pc->data;
201: PC_MG_Levels **mglevels = mg->levels;
202: PetscInt i, n;
204: PetscFunctionBegin;
205: if (mglevels) {
206: n = mglevels[0]->levels;
207: for (i = 0; i < n - 1; i++) {
208: PetscCall(VecDestroy(&mglevels[i + 1]->r));
209: PetscCall(VecDestroy(&mglevels[i]->b));
210: PetscCall(VecDestroy(&mglevels[i]->x));
211: PetscCall(MatDestroy(&mglevels[i + 1]->R));
212: PetscCall(MatDestroy(&mglevels[i]->B));
213: PetscCall(MatDestroy(&mglevels[i]->X));
214: PetscCall(VecDestroy(&mglevels[i]->crx));
215: PetscCall(VecDestroy(&mglevels[i]->crb));
216: PetscCall(MatDestroy(&mglevels[i + 1]->restrct));
217: PetscCall(MatDestroy(&mglevels[i + 1]->interpolate));
218: PetscCall(MatDestroy(&mglevels[i + 1]->inject));
219: PetscCall(VecDestroy(&mglevels[i + 1]->rscale));
220: }
221: PetscCall(VecDestroy(&mglevels[n - 1]->crx));
222: PetscCall(VecDestroy(&mglevels[n - 1]->crb));
223: /* this is not null only if the smoother on the finest level
224: changes the rhs during PreSolve */
225: PetscCall(VecDestroy(&mglevels[n - 1]->b));
226: PetscCall(MatDestroy(&mglevels[n - 1]->B));
228: for (i = 0; i < n; i++) {
229: PetscCall(MatDestroy(&mglevels[i]->coarseSpace));
230: PetscCall(MatDestroy(&mglevels[i]->A));
231: if (mglevels[i]->smoothd != mglevels[i]->smoothu) PetscCall(KSPReset(mglevels[i]->smoothd));
232: PetscCall(KSPReset(mglevels[i]->smoothu));
233: if (mglevels[i]->cr) PetscCall(KSPReset(mglevels[i]->cr));
234: }
235: mg->Nc = 0;
236: }
237: PetscFunctionReturn(PETSC_SUCCESS);
238: }
240: /* Implementing CR
242: We only want to make corrections that ``do not change'' the coarse solution. What we mean by not changing is that if I prolong my coarse solution to the fine grid and then inject that fine solution back to the coarse grid, I get the same answer. Injection is what Brannick calls R. We want the complementary projector to Inj, which we will call S, after Brannick, so that Inj S = 0. Now the orthogonal projector onto the range of Inj^T is
244: Inj^T (Inj Inj^T)^{-1} Inj
246: and if Inj is a VecScatter, as it is now in PETSc, we have
248: Inj^T Inj
250: and
252: S = I - Inj^T Inj
254: since
256: Inj S = Inj - (Inj Inj^T) Inj = 0.
258: Brannick suggests
260: A \to S^T A S \qquad\mathrm{and}\qquad M \to S^T M S
262: but I do not think his :math:`S^T S = I` is correct. Our S is an orthogonal projector, so :math:`S^T S = S^2 = S`. We will use
264: M^{-1} A \to S M^{-1} A S
266: In fact, since it is somewhat hard in PETSc to do the symmetric application, we will just apply S on the left.
268: Check: || Inj P - I ||_F < tol
269: Check: In general, Inj Inj^T = I
270: */
272: typedef struct {
273: PC mg; /* The PCMG object */
274: PetscInt l; /* The multigrid level for this solver */
275: Mat Inj; /* The injection matrix */
276: Mat S; /* I - Inj^T Inj */
277: } CRContext;
279: static PetscErrorCode CRSetup_Private(PC pc)
280: {
281: CRContext *ctx;
282: Mat It;
284: PetscFunctionBeginUser;
285: PetscCall(PCShellGetContext(pc, &ctx));
286: PetscCall(PCMGGetInjection(ctx->mg, ctx->l, &It));
287: PetscCheck(It, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "CR requires that injection be defined for this PCMG");
288: PetscCall(MatCreateTranspose(It, &ctx->Inj));
289: PetscCall(MatCreateNormal(ctx->Inj, &ctx->S));
290: PetscCall(MatScale(ctx->S, -1.0));
291: PetscCall(MatShift(ctx->S, 1.0));
292: PetscFunctionReturn(PETSC_SUCCESS);
293: }
295: static PetscErrorCode CRApply_Private(PC pc, Vec x, Vec y)
296: {
297: CRContext *ctx;
299: PetscFunctionBeginUser;
300: PetscCall(PCShellGetContext(pc, &ctx));
301: PetscCall(MatMult(ctx->S, x, y));
302: PetscFunctionReturn(PETSC_SUCCESS);
303: }
305: static PetscErrorCode CRDestroy_Private(PC pc)
306: {
307: CRContext *ctx;
309: PetscFunctionBeginUser;
310: PetscCall(PCShellGetContext(pc, &ctx));
311: PetscCall(MatDestroy(&ctx->Inj));
312: PetscCall(MatDestroy(&ctx->S));
313: PetscCall(PetscFree(ctx));
314: PetscCall(PCShellSetContext(pc, NULL));
315: PetscFunctionReturn(PETSC_SUCCESS);
316: }
318: static PetscErrorCode CreateCR_Private(PC pc, PetscInt l, PC *cr)
319: {
320: CRContext *ctx;
322: PetscFunctionBeginUser;
323: PetscCall(PCCreate(PetscObjectComm((PetscObject)pc), cr));
324: PetscCall(PetscObjectSetName((PetscObject)*cr, "S (complementary projector to injection)"));
325: PetscCall(PetscCalloc1(1, &ctx));
326: ctx->mg = pc;
327: ctx->l = l;
328: PetscCall(PCSetType(*cr, PCSHELL));
329: PetscCall(PCShellSetContext(*cr, ctx));
330: PetscCall(PCShellSetApply(*cr, CRApply_Private));
331: PetscCall(PCShellSetSetUp(*cr, CRSetup_Private));
332: PetscCall(PCShellSetDestroy(*cr, CRDestroy_Private));
333: PetscFunctionReturn(PETSC_SUCCESS);
334: }
336: PETSC_EXTERN PetscErrorCode PetscOptionsFindPairPrefix_Private(PetscOptions, const char[], const char[], const char *[], const char *[], PetscBool *);
338: PetscErrorCode PCMGSetLevels_MG(PC pc, PetscInt levels, MPI_Comm *comms)
339: {
340: PC_MG *mg = (PC_MG *)pc->data;
341: MPI_Comm comm;
342: PC_MG_Levels **mglevels = mg->levels;
343: PCMGType mgtype = mg->am;
344: PetscInt mgctype = (PetscInt)PC_MG_CYCLE_V;
345: PetscInt i;
346: PetscMPIInt size;
347: const char *prefix;
348: PC ipc;
349: PetscInt n;
351: PetscFunctionBegin;
354: if (mg->nlevels == levels) PetscFunctionReturn(PETSC_SUCCESS);
355: PetscCall(PetscObjectGetComm((PetscObject)pc, &comm));
356: if (mglevels) {
357: mgctype = mglevels[0]->cycles;
358: /* changing the number of levels so free up the previous stuff */
359: PetscCall(PCReset_MG(pc));
360: n = mglevels[0]->levels;
361: for (i = 0; i < n; i++) {
362: if (mglevels[i]->smoothd != mglevels[i]->smoothu) PetscCall(KSPDestroy(&mglevels[i]->smoothd));
363: PetscCall(KSPDestroy(&mglevels[i]->smoothu));
364: PetscCall(KSPDestroy(&mglevels[i]->cr));
365: PetscCall(PetscFree(mglevels[i]));
366: }
367: PetscCall(PetscFree(mg->levels));
368: }
370: mg->nlevels = levels;
372: PetscCall(PetscMalloc1(levels, &mglevels));
374: PetscCall(PCGetOptionsPrefix(pc, &prefix));
376: mg->stageApply = 0;
377: for (i = 0; i < levels; i++) {
378: PetscCall(PetscNew(&mglevels[i]));
380: mglevels[i]->level = i;
381: mglevels[i]->levels = levels;
382: mglevels[i]->cycles = mgctype;
383: mg->default_smoothu = 2;
384: mg->default_smoothd = 2;
385: mglevels[i]->eventsmoothsetup = 0;
386: mglevels[i]->eventsmoothsolve = 0;
387: mglevels[i]->eventresidual = 0;
388: mglevels[i]->eventinterprestrict = 0;
390: if (comms) comm = comms[i];
391: if (comm != MPI_COMM_NULL) {
392: PetscCall(KSPCreate(comm, &mglevels[i]->smoothd));
393: PetscCall(KSPSetNestLevel(mglevels[i]->smoothd, pc->kspnestlevel));
394: PetscCall(KSPSetErrorIfNotConverged(mglevels[i]->smoothd, pc->erroriffailure));
395: PetscCall(PetscObjectIncrementTabLevel((PetscObject)mglevels[i]->smoothd, (PetscObject)pc, levels - i));
396: PetscCall(KSPSetOptionsPrefix(mglevels[i]->smoothd, prefix));
397: PetscCall(PetscObjectComposedDataSetInt((PetscObject)mglevels[i]->smoothd, PetscMGLevelId, mglevels[i]->level));
398: if (i == 0 && levels > 1) { // coarse grid
399: PetscCall(KSPAppendOptionsPrefix(mglevels[0]->smoothd, "mg_coarse_"));
401: /* coarse solve is (redundant) LU by default; set shifttype NONZERO to avoid annoying zero-pivot in LU preconditioner */
402: PetscCall(KSPSetType(mglevels[0]->smoothd, KSPPREONLY));
403: PetscCall(KSPGetPC(mglevels[0]->smoothd, &ipc));
404: PetscCallMPI(MPI_Comm_size(comm, &size));
405: if (size > 1) PetscCall(PCSetType(ipc, PCREDUNDANT));
406: else PetscCall(PCSetType(ipc, PCLU));
407: PetscCall(PCFactorSetShiftType(ipc, MAT_SHIFT_INBLOCKS));
408: } else {
409: char tprefix[128];
411: PetscCall(KSPSetType(mglevels[i]->smoothd, KSPCHEBYSHEV));
412: PetscCall(KSPSetConvergenceTest(mglevels[i]->smoothd, KSPConvergedSkip, NULL, NULL));
413: PetscCall(KSPSetNormType(mglevels[i]->smoothd, KSP_NORM_NONE));
414: PetscCall(KSPGetPC(mglevels[i]->smoothd, &ipc));
415: PetscCall(PCSetType(ipc, PCSOR));
416: PetscCall(KSPSetTolerances(mglevels[i]->smoothd, PETSC_CURRENT, PETSC_CURRENT, PETSC_CURRENT, mg->default_smoothd));
418: if (i == levels - 1 && levels > 1) { // replace 'mg_finegrid_' with 'mg_levels_X_'
419: PetscBool set;
421: PetscCall(PetscOptionsFindPairPrefix_Private(((PetscObject)mglevels[i]->smoothd)->options, ((PetscObject)mglevels[i]->smoothd)->prefix, "-mg_fine_", NULL, NULL, &set));
422: if (set) {
423: if (prefix) PetscCall(PetscSNPrintf(tprefix, 128, "%smg_fine_", prefix));
424: else PetscCall(PetscSNPrintf(tprefix, 128, "mg_fine_"));
425: PetscCall(KSPSetOptionsPrefix(mglevels[i]->smoothd, tprefix));
426: } else {
427: PetscCall(PetscSNPrintf(tprefix, 128, "mg_levels_%" PetscInt_FMT "_", i));
428: PetscCall(KSPAppendOptionsPrefix(mglevels[i]->smoothd, tprefix));
429: }
430: } else {
431: PetscCall(PetscSNPrintf(tprefix, 128, "mg_levels_%" PetscInt_FMT "_", i));
432: PetscCall(KSPAppendOptionsPrefix(mglevels[i]->smoothd, tprefix));
433: }
434: }
435: }
436: mglevels[i]->smoothu = mglevels[i]->smoothd;
437: mg->rtol = 0.0;
438: mg->abstol = 0.0;
439: mg->dtol = 0.0;
440: mg->ttol = 0.0;
441: mg->cyclesperpcapply = 1;
442: }
443: mg->levels = mglevels;
444: PetscCall(PCMGSetType(pc, mgtype));
445: PetscFunctionReturn(PETSC_SUCCESS);
446: }
448: /*@
449: PCMGSetLevels - Sets the number of levels to use with `PCMG`.
450: Must be called before any other `PCMG` routine.
452: Logically Collective
454: Input Parameters:
455: + pc - the preconditioner context
456: . levels - the number of levels
457: - comms - optional communicators for each level; this is to allow solving the coarser problems
458: on smaller sets of processes. For processes that are not included in the computation
459: you must pass `MPI_COMM_NULL`. Use comms = `NULL` to specify that all processes
460: should participate in each level of problem.
462: Options Database Key:
463: . -pc_mg_levels levels - set the number of levels to use
465: Level: intermediate
467: Notes:
468: If the number of levels is one then the multigrid uses the `-mg_levels` prefix
469: for setting the level options rather than the `-mg_coarse` or `-mg_fine` prefix.
471: You can free the information in `comms` after this routine is called.
473: The array of MPI communicators must contain `MPI_COMM_NULL` for those processes that at each level
474: are not participating in the coarser solve. For example, with 2 levels and 1 and 2 processes on
475: the two levels, rank 0 in the original communicator will pass in an array of 2 communicators
476: of size 2 and 1, while rank 1 in the original communicator will pass in array of 2 communicators
477: the first of size 2 and the second of value `MPI_COMM_NULL` since the rank 1 does not participate
478: in the coarse grid solve.
480: Since each coarser level may have a new `MPI_Comm` with fewer processes than the previous, one
481: must take special care in providing the restriction and interpolation operation. We recommend
482: providing these as two step operations; first perform a standard restriction or interpolation on
483: the full number of processes for that level and then use an MPI call to copy the resulting vector
484: array entries (after calls to `VecGetArray()`) to the smaller or larger number of processes, note in both
485: cases the MPI calls must be made on the larger of the two communicators. Traditional MPI send and
486: receives or `MPI_AlltoAllv()` could be used to do the reshuffling of the vector entries.
488: Fortran Notes:
489: Use comms = `PETSC_NULL_MPI_COMM` as the equivalent of `NULL` in the C interface. Note `PETSC_NULL_MPI_COMM`
490: is not `MPI_COMM_NULL`. It is more like `PETSC_NULL_INTEGER`, `PETSC_NULL_REAL` etc.
492: .seealso: [](ch_ksp), `PCMGSetType()`, `PCMGGetLevels()`
493: @*/
494: PetscErrorCode PCMGSetLevels(PC pc, PetscInt levels, MPI_Comm *comms)
495: {
496: PetscFunctionBegin;
498: if (comms) PetscAssertPointer(comms, 3);
499: PetscTryMethod(pc, "PCMGSetLevels_C", (PC, PetscInt, MPI_Comm *), (pc, levels, comms));
500: PetscFunctionReturn(PETSC_SUCCESS);
501: }
503: PetscErrorCode PCDestroy_MG(PC pc)
504: {
505: PC_MG *mg = (PC_MG *)pc->data;
506: PC_MG_Levels **mglevels = mg->levels;
507: PetscInt i, n;
509: PetscFunctionBegin;
510: PetscCall(PCReset_MG(pc));
511: if (mglevels) {
512: n = mglevels[0]->levels;
513: for (i = 0; i < n; i++) {
514: if (mglevels[i]->smoothd != mglevels[i]->smoothu) PetscCall(KSPDestroy(&mglevels[i]->smoothd));
515: PetscCall(KSPDestroy(&mglevels[i]->smoothu));
516: PetscCall(KSPDestroy(&mglevels[i]->cr));
517: PetscCall(PetscFree(mglevels[i]));
518: }
519: PetscCall(PetscFree(mg->levels));
520: }
521: PetscCall(PetscFree(pc->data));
522: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGetInterpolations_C", NULL));
523: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGetCoarseOperators_C", NULL));
524: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGSetGalerkin_C", NULL));
525: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCSetReusePreconditioner_C", NULL));
526: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGGetLevels_C", NULL));
527: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGSetLevels_C", NULL));
528: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGetInterpolations_C", NULL));
529: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGetCoarseOperators_C", NULL));
530: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGSetAdaptInterpolation_C", NULL));
531: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGGetAdaptInterpolation_C", NULL));
532: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGSetAdaptCR_C", NULL));
533: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGGetAdaptCR_C", NULL));
534: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGSetAdaptCoarseSpaceType_C", NULL));
535: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGGetAdaptCoarseSpaceType_C", NULL));
536: PetscFunctionReturn(PETSC_SUCCESS);
537: }
539: /*
540: PCApply_MG - Runs either an additive, multiplicative, Kaskadic
541: or full cycle of multigrid.
543: Note:
544: A simple wrapper which calls PCMGMCycle(),PCMGACycle(), or PCMGFCycle().
545: */
546: static PetscErrorCode PCApply_MG_Internal(PC pc, Vec b, Vec x, Mat B, Mat X, PetscBool transpose)
547: {
548: PC_MG *mg = (PC_MG *)pc->data;
549: PC_MG_Levels **mglevels = mg->levels;
550: PC tpc;
551: PetscInt levels = mglevels[0]->levels, i;
552: PetscBool changeu, changed, matapp;
554: PetscFunctionBegin;
555: matapp = (PetscBool)(B && X);
556: if (mg->stageApply) PetscCall(PetscLogStagePush(mg->stageApply));
557: /* When the DM is supplying the matrix then it will not exist until here */
558: for (i = 0; i < levels; i++) {
559: if (!mglevels[i]->A) {
560: PetscCall(KSPGetOperators(mglevels[i]->smoothu, &mglevels[i]->A, NULL));
561: PetscCall(PetscObjectReference((PetscObject)mglevels[i]->A));
562: }
563: }
565: PetscCall(KSPGetPC(mglevels[levels - 1]->smoothd, &tpc));
566: PetscCall(PCPreSolveChangeRHS(tpc, &changed));
567: PetscCall(KSPGetPC(mglevels[levels - 1]->smoothu, &tpc));
568: PetscCall(PCPreSolveChangeRHS(tpc, &changeu));
569: if (!changeu && !changed) {
570: if (matapp) {
571: PetscCall(MatDestroy(&mglevels[levels - 1]->B));
572: mglevels[levels - 1]->B = B;
573: } else {
574: PetscCall(VecDestroy(&mglevels[levels - 1]->b));
575: mglevels[levels - 1]->b = b;
576: }
577: } else { /* if the smoother changes the rhs during PreSolve, we cannot use the input vector */
578: if (matapp) {
579: if (mglevels[levels - 1]->B) {
580: PetscInt N1, N2;
581: PetscBool flg;
583: PetscCall(MatGetSize(mglevels[levels - 1]->B, NULL, &N1));
584: PetscCall(MatGetSize(B, NULL, &N2));
585: PetscCall(PetscObjectTypeCompare((PetscObject)mglevels[levels - 1]->B, ((PetscObject)B)->type_name, &flg));
586: if (N1 != N2 || !flg) PetscCall(MatDestroy(&mglevels[levels - 1]->B));
587: }
588: if (!mglevels[levels - 1]->B) PetscCall(MatDuplicate(B, MAT_COPY_VALUES, &mglevels[levels - 1]->B));
589: else PetscCall(MatCopy(B, mglevels[levels - 1]->B, SAME_NONZERO_PATTERN));
590: } else {
591: if (!mglevels[levels - 1]->b) {
592: Vec *vec;
594: PetscCall(KSPCreateVecs(mglevels[levels - 1]->smoothd, 1, &vec, 0, NULL));
595: mglevels[levels - 1]->b = *vec;
596: PetscCall(PetscFree(vec));
597: }
598: PetscCall(VecCopy(b, mglevels[levels - 1]->b));
599: }
600: }
601: if (matapp) {
602: mglevels[levels - 1]->X = X;
603: } else {
604: mglevels[levels - 1]->x = x;
605: }
607: /* If coarser Xs are present, it means we have already block applied the PC at least once
608: Reset operators if sizes/type do no match */
609: if (matapp && levels > 1 && mglevels[levels - 2]->X) {
610: PetscInt Xc, Bc;
611: PetscBool flg;
613: PetscCall(MatGetSize(mglevels[levels - 2]->X, NULL, &Xc));
614: PetscCall(MatGetSize(mglevels[levels - 1]->B, NULL, &Bc));
615: PetscCall(PetscObjectTypeCompare((PetscObject)mglevels[levels - 2]->X, ((PetscObject)mglevels[levels - 1]->X)->type_name, &flg));
616: if (Xc != Bc || !flg) {
617: PetscCall(MatDestroy(&mglevels[levels - 1]->R));
618: for (i = 0; i < levels - 1; i++) {
619: PetscCall(MatDestroy(&mglevels[i]->R));
620: PetscCall(MatDestroy(&mglevels[i]->B));
621: PetscCall(MatDestroy(&mglevels[i]->X));
622: }
623: }
624: }
626: if (mg->am == PC_MG_MULTIPLICATIVE) {
627: if (matapp) PetscCall(MatZeroEntries(X));
628: else PetscCall(VecZeroEntries(x));
629: for (i = 0; i < mg->cyclesperpcapply; i++) PetscCall(PCMGMCycle_Private(pc, mglevels + levels - 1, transpose, matapp, NULL));
630: } else if (mg->am == PC_MG_ADDITIVE) {
631: PetscCall(PCMGACycle_Private(pc, mglevels, transpose, matapp));
632: } else if (mg->am == PC_MG_KASKADE) {
633: PetscCall(PCMGKCycle_Private(pc, mglevels, transpose, matapp));
634: } else {
635: PetscCall(PCMGFCycle_Private(pc, mglevels, transpose, matapp));
636: }
637: if (mg->stageApply) PetscCall(PetscLogStagePop());
638: if (!changeu && !changed) {
639: if (matapp) {
640: mglevels[levels - 1]->B = NULL;
641: } else {
642: mglevels[levels - 1]->b = NULL;
643: }
644: }
645: PetscFunctionReturn(PETSC_SUCCESS);
646: }
648: static PetscErrorCode PCApply_MG(PC pc, Vec b, Vec x)
649: {
650: PetscFunctionBegin;
651: PetscCall(PCApply_MG_Internal(pc, b, x, NULL, NULL, PETSC_FALSE));
652: PetscFunctionReturn(PETSC_SUCCESS);
653: }
655: static PetscErrorCode PCApplyTranspose_MG(PC pc, Vec b, Vec x)
656: {
657: PetscFunctionBegin;
658: PetscCall(PCApply_MG_Internal(pc, b, x, NULL, NULL, PETSC_TRUE));
659: PetscFunctionReturn(PETSC_SUCCESS);
660: }
662: static PetscErrorCode PCMatApply_MG(PC pc, Mat b, Mat x)
663: {
664: PetscFunctionBegin;
665: PetscCall(PCApply_MG_Internal(pc, NULL, NULL, b, x, PETSC_FALSE));
666: PetscFunctionReturn(PETSC_SUCCESS);
667: }
669: static PetscErrorCode PCMatApplyTranspose_MG(PC pc, Mat b, Mat x)
670: {
671: PetscFunctionBegin;
672: PetscCall(PCApply_MG_Internal(pc, NULL, NULL, b, x, PETSC_TRUE));
673: PetscFunctionReturn(PETSC_SUCCESS);
674: }
676: PetscErrorCode PCSetFromOptions_MG(PC pc, PetscOptionItems PetscOptionsObject)
677: {
678: PetscInt levels, cycles;
679: PetscBool flg, flg2;
680: PC_MG *mg = (PC_MG *)pc->data;
681: PC_MG_Levels **mglevels;
682: PCMGType mgtype;
683: PCMGCycleType mgctype;
684: PCMGGalerkinType gtype;
685: PCMGCoarseSpaceType coarseSpaceType;
687: PetscFunctionBegin;
688: levels = PetscMax(mg->nlevels, 1);
689: PetscOptionsHeadBegin(PetscOptionsObject, "Multigrid options");
690: PetscCall(PetscOptionsInt("-pc_mg_levels", "Number of Levels", "PCMGSetLevels", levels, &levels, &flg));
691: if (!flg && !mg->levels && pc->dm) {
692: PetscCall(DMGetRefineLevel(pc->dm, &levels));
693: levels++;
694: mg->usedmfornumberoflevels = PETSC_TRUE;
695: }
696: PetscCall(PCMGSetLevels(pc, levels, NULL));
697: mglevels = mg->levels;
699: mgctype = (PCMGCycleType)mglevels[0]->cycles;
700: PetscCall(PetscOptionsEnum("-pc_mg_cycle_type", "V cycle or for W-cycle", "PCMGSetCycleType", PCMGCycleTypes, (PetscEnum)mgctype, (PetscEnum *)&mgctype, &flg));
701: if (flg) PetscCall(PCMGSetCycleType(pc, mgctype));
702: coarseSpaceType = mg->coarseSpaceType;
703: PetscCall(PetscOptionsEnum("-pc_mg_adapt_interp_coarse_space", "Type of adaptive coarse space: none, polynomial, harmonic, eigenvector, generalized_eigenvector, gdsw", "PCMGSetAdaptCoarseSpaceType", PCMGCoarseSpaceTypes, (PetscEnum)coarseSpaceType, (PetscEnum *)&coarseSpaceType, &flg));
704: if (flg) PetscCall(PCMGSetAdaptCoarseSpaceType(pc, coarseSpaceType));
705: PetscCall(PetscOptionsInt("-pc_mg_adapt_interp_n", "Size of the coarse space for adaptive interpolation", "PCMGSetAdaptCoarseSpaceType", mg->Nc, &mg->Nc, &flg));
706: PetscCall(PetscOptionsBool("-pc_mg_mesp_monitor", "Monitor the multilevel eigensolver", "PCMGSetAdaptInterpolation", PETSC_FALSE, &mg->mespMonitor, &flg));
707: flg2 = PETSC_FALSE;
708: PetscCall(PetscOptionsBool("-pc_mg_adapt_cr", "Monitor coarse space quality using Compatible Relaxation (CR)", "PCMGSetAdaptCR", PETSC_FALSE, &flg2, &flg));
709: if (flg) PetscCall(PCMGSetAdaptCR(pc, flg2));
710: flg = PETSC_FALSE;
711: PetscCall(PetscOptionsBool("-pc_mg_distinct_smoothup", "Create separate smoothup KSP and append the prefix _up", "PCMGSetDistinctSmoothUp", PETSC_FALSE, &flg, NULL));
712: if (flg) PetscCall(PCMGSetDistinctSmoothUp(pc));
713: PetscCall(PetscOptionsEnum("-pc_mg_galerkin", "Use Galerkin process to compute coarser operators", "PCMGSetGalerkin", PCMGGalerkinTypes, (PetscEnum)mg->galerkin, (PetscEnum *)>ype, &flg));
714: if (flg) PetscCall(PCMGSetGalerkin(pc, gtype));
715: mgtype = mg->am;
716: PetscCall(PetscOptionsEnum("-pc_mg_type", "Multigrid type", "PCMGSetType", PCMGTypes, (PetscEnum)mgtype, (PetscEnum *)&mgtype, &flg));
717: if (flg) PetscCall(PCMGSetType(pc, mgtype));
718: if (mg->am == PC_MG_MULTIPLICATIVE) {
719: PetscCall(PetscOptionsInt("-pc_mg_multiplicative_cycles", "Number of cycles for each preconditioner step", "PCMGMultiplicativeSetCycles", mg->cyclesperpcapply, &cycles, &flg));
720: if (flg) PetscCall(PCMGMultiplicativeSetCycles(pc, cycles));
721: }
722: flg = PETSC_FALSE;
723: PetscCall(PetscOptionsBool("-pc_mg_log", "Log times for each multigrid level", "None", flg, &flg, NULL));
724: if (flg) {
725: PetscInt i;
726: char eventname[128];
728: levels = mglevels[0]->levels;
729: for (i = 0; i < levels; i++) {
730: PetscCall(PetscSNPrintf(eventname, PETSC_STATIC_ARRAY_LENGTH(eventname), "MGSetup Level %" PetscInt_FMT, i));
731: PetscCall(PetscLogEventRegister(eventname, ((PetscObject)pc)->classid, &mglevels[i]->eventsmoothsetup));
732: PetscCall(PetscSNPrintf(eventname, PETSC_STATIC_ARRAY_LENGTH(eventname), "MGSmooth Level %" PetscInt_FMT, i));
733: PetscCall(PetscLogEventRegister(eventname, ((PetscObject)pc)->classid, &mglevels[i]->eventsmoothsolve));
734: if (i) {
735: PetscCall(PetscSNPrintf(eventname, PETSC_STATIC_ARRAY_LENGTH(eventname), "MGResid Level %" PetscInt_FMT, i));
736: PetscCall(PetscLogEventRegister(eventname, ((PetscObject)pc)->classid, &mglevels[i]->eventresidual));
737: PetscCall(PetscSNPrintf(eventname, PETSC_STATIC_ARRAY_LENGTH(eventname), "MGInterp Level %" PetscInt_FMT, i));
738: PetscCall(PetscLogEventRegister(eventname, ((PetscObject)pc)->classid, &mglevels[i]->eventinterprestrict));
739: }
740: }
742: if (PetscDefined(USE_LOG)) {
743: const char sname[] = "MG Apply";
745: PetscCall(PetscLogStageGetId(sname, &mg->stageApply));
746: if (mg->stageApply < 0) PetscCall(PetscLogStageRegister(sname, &mg->stageApply));
747: }
748: }
749: PetscOptionsHeadEnd();
750: /* Check option consistency */
751: PetscCall(PCMGGetGalerkin(pc, >ype));
752: PetscCall(PCMGGetAdaptInterpolation(pc, &flg));
753: PetscCheck(!flg || !(gtype >= PC_MG_GALERKIN_NONE), PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_INCOMP, "Must use Galerkin coarse operators when adapting the interpolator");
754: PetscFunctionReturn(PETSC_SUCCESS);
755: }
757: const char *const PCMGTypes[] = {"MULTIPLICATIVE", "ADDITIVE", "FULL", "KASKADE", "PCMGType", "PC_MG", NULL};
758: const char *const PCMGCycleTypes[] = {"invalid", "v", "w", "PCMGCycleType", "PC_MG_CYCLE", NULL};
759: const char *const PCMGGalerkinTypes[] = {"both", "pmat", "mat", "none", "external", "PCMGGalerkinType", "PC_MG_GALERKIN", NULL};
760: const char *const PCMGCoarseSpaceTypes[] = {"none", "polynomial", "harmonic", "eigenvector", "generalized_eigenvector", "gdsw", "PCMGCoarseSpaceType", "PCMG_ADAPT_NONE", NULL};
762: #include <petscdraw.h>
763: PetscErrorCode PCView_MG(PC pc, PetscViewer viewer)
764: {
765: PC_MG *mg = (PC_MG *)pc->data;
766: PC_MG_Levels **mglevels = mg->levels;
767: PetscInt levels = mglevels ? mglevels[0]->levels : 0, i;
768: PetscBool isascii, isbinary, isdraw;
770: PetscFunctionBegin;
771: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
772: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
773: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
774: if (isascii) {
775: const char *cyclename = levels ? (mglevels[0]->cycles == PC_MG_CYCLE_V ? "v" : "w") : "unknown";
777: if (levels == 1) PetscCall(PetscViewerASCIIPrintf(viewer, " WARNING: Multigrid is being run with only a single level!\n"));
778: PetscCall(PetscViewerASCIIPrintf(viewer, " type is %s, levels=%" PetscInt_FMT " cycles=%s\n", PCMGTypes[mg->am], levels, cyclename));
779: if (mg->am == PC_MG_MULTIPLICATIVE) PetscCall(PetscViewerASCIIPrintf(viewer, " Cycles per PCApply=%" PetscInt_FMT "\n", mg->cyclesperpcapply));
780: if (mg->galerkin == PC_MG_GALERKIN_BOTH) {
781: PetscCall(PetscViewerASCIIPrintf(viewer, " Using Galerkin computed coarse grid matrices\n"));
782: } else if (mg->galerkin == PC_MG_GALERKIN_PMAT) {
783: PetscCall(PetscViewerASCIIPrintf(viewer, " Using Galerkin computed coarse grid matrices for pmat\n"));
784: } else if (mg->galerkin == PC_MG_GALERKIN_MAT) {
785: PetscCall(PetscViewerASCIIPrintf(viewer, " Using Galerkin computed coarse grid matrices for mat\n"));
786: } else if (mg->galerkin == PC_MG_GALERKIN_EXTERNAL) {
787: PetscCall(PetscViewerASCIIPrintf(viewer, " Using externally compute Galerkin coarse grid matrices\n"));
788: } else {
789: PetscCall(PetscViewerASCIIPrintf(viewer, " Not using Galerkin computed coarse grid matrices\n"));
790: }
791: if (mg->view) PetscCall((*mg->view)(pc, viewer));
792: for (i = 0; i < levels; i++) {
793: if (i) {
794: PetscCall(PetscViewerASCIIPrintf(viewer, "Down solver (pre-smoother) on level %" PetscInt_FMT " -------------------------------\n", i));
795: } else {
796: PetscCall(PetscViewerASCIIPrintf(viewer, "Coarse grid solver -- level %" PetscInt_FMT " -------------------------------\n", i));
797: }
798: PetscCall(PetscViewerASCIIPushTab(viewer));
799: PetscCall(KSPView(mglevels[i]->smoothd, viewer));
800: PetscCall(PetscViewerASCIIPopTab(viewer));
801: if (i && mglevels[i]->smoothd == mglevels[i]->smoothu) {
802: PetscCall(PetscViewerASCIIPrintf(viewer, "Up solver (post-smoother) same as down solver (pre-smoother)\n"));
803: } else if (i) {
804: PetscCall(PetscViewerASCIIPrintf(viewer, "Up solver (post-smoother) on level %" PetscInt_FMT " -------------------------------\n", i));
805: PetscCall(PetscViewerASCIIPushTab(viewer));
806: PetscCall(KSPView(mglevels[i]->smoothu, viewer));
807: PetscCall(PetscViewerASCIIPopTab(viewer));
808: }
809: if (i && mglevels[i]->cr) {
810: PetscCall(PetscViewerASCIIPrintf(viewer, "CR solver on level %" PetscInt_FMT " -------------------------------\n", i));
811: PetscCall(PetscViewerASCIIPushTab(viewer));
812: PetscCall(KSPView(mglevels[i]->cr, viewer));
813: PetscCall(PetscViewerASCIIPopTab(viewer));
814: }
815: }
816: } else if (isbinary) {
817: for (i = levels - 1; i >= 0; i--) {
818: PetscCall(KSPView(mglevels[i]->smoothd, viewer));
819: if (i && mglevels[i]->smoothd != mglevels[i]->smoothu) PetscCall(KSPView(mglevels[i]->smoothu, viewer));
820: }
821: } else if (isdraw) {
822: PetscDraw draw;
823: PetscReal x, w, y, bottom, th;
824: PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
825: PetscCall(PetscDrawGetCurrentPoint(draw, &x, &y));
826: PetscCall(PetscDrawStringGetSize(draw, NULL, &th));
827: bottom = y - th;
828: for (i = levels - 1; i >= 0; i--) {
829: if (!mglevels[i]->smoothu || (mglevels[i]->smoothu == mglevels[i]->smoothd)) {
830: PetscCall(PetscDrawPushCurrentPoint(draw, x, bottom));
831: PetscCall(KSPView(mglevels[i]->smoothd, viewer));
832: PetscCall(PetscDrawPopCurrentPoint(draw));
833: } else {
834: w = 0.5 * PetscMin(1.0 - x, x);
835: PetscCall(PetscDrawPushCurrentPoint(draw, x + w, bottom));
836: PetscCall(KSPView(mglevels[i]->smoothd, viewer));
837: PetscCall(PetscDrawPopCurrentPoint(draw));
838: PetscCall(PetscDrawPushCurrentPoint(draw, x - w, bottom));
839: PetscCall(KSPView(mglevels[i]->smoothu, viewer));
840: PetscCall(PetscDrawPopCurrentPoint(draw));
841: }
842: PetscCall(PetscDrawGetBoundingBox(draw, NULL, &bottom, NULL, NULL));
843: bottom -= th;
844: }
845: }
846: PetscFunctionReturn(PETSC_SUCCESS);
847: }
849: #include <petsc/private/kspimpl.h>
851: /*
852: Calls setup for the KSP on each level
853: */
854: PetscErrorCode PCSetUp_MG(PC pc)
855: {
856: PC_MG *mg = (PC_MG *)pc->data;
857: PC_MG_Levels **mglevels = mg->levels;
858: PetscInt n;
859: PC cpc;
860: PetscBool dump = PETSC_FALSE, opsset, use_amat, missinginterpolate = PETSC_FALSE;
861: Mat dA, dB;
862: Vec tvec;
863: DM *dms;
864: PetscViewer viewer = NULL;
865: PetscBool dAeqdB = PETSC_FALSE, needRestricts = PETSC_FALSE, doCR = PETSC_FALSE;
866: PetscBool adaptInterpolation = mg->adaptInterpolation;
868: PetscFunctionBegin;
869: PetscCheck(mglevels, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Must set MG levels with PCMGSetLevels() before setting up");
870: n = mglevels[0]->levels;
871: /* FIX: Move this to PCSetFromOptions_MG? */
872: if (mg->usedmfornumberoflevels) {
873: PetscInt levels;
875: PetscCall(DMGetRefineLevel(pc->dm, &levels));
876: levels++;
877: if (levels > n) { /* the problem is now being solved on a finer grid */
878: PetscCall(PCMGSetLevels(pc, levels, NULL));
879: n = levels;
880: PetscCall(PCSetFromOptions(pc)); /* it is bad to call this here, but otherwise will never be called for the new hierarchy */
881: mglevels = mg->levels;
882: }
883: }
885: /* If user did not provide fine grid operators OR operator was not updated since last global KSPSetOperators() */
886: /* so use those from global PC */
887: /* Is this what we always want? What if user wants to keep old one? */
888: PetscCall(KSPGetOperatorsSet(mglevels[n - 1]->smoothd, NULL, &opsset));
889: if (opsset) {
890: Mat mmat;
891: PetscCall(KSPGetOperators(mglevels[n - 1]->smoothd, NULL, &mmat));
892: if (mmat == pc->pmat) opsset = PETSC_FALSE;
893: }
894: /* fine grid smoother inherits the reuse-pc flag */
895: PetscCall(KSPGetPC(mglevels[n - 1]->smoothd, &cpc));
896: cpc->reusepreconditioner = pc->reusepreconditioner;
897: PetscCall(KSPGetPC(mglevels[n - 1]->smoothu, &cpc));
898: cpc->reusepreconditioner = pc->reusepreconditioner;
900: /* Create CR solvers */
901: PetscCall(PCMGGetAdaptCR(pc, &doCR));
902: if (doCR) {
903: const char *prefix;
905: PetscCall(PCGetOptionsPrefix(pc, &prefix));
906: for (PetscInt i = 1; i < n; ++i) {
907: PC ipc, cr;
908: char crprefix[128];
910: PetscCall(KSPCreate(PetscObjectComm((PetscObject)pc), &mglevels[i]->cr));
911: PetscCall(KSPSetNestLevel(mglevels[i]->cr, pc->kspnestlevel));
912: PetscCall(KSPSetErrorIfNotConverged(mglevels[i]->cr, PETSC_FALSE));
913: PetscCall(PetscObjectIncrementTabLevel((PetscObject)mglevels[i]->cr, (PetscObject)pc, n - i));
914: PetscCall(KSPSetOptionsPrefix(mglevels[i]->cr, prefix));
915: PetscCall(PetscObjectComposedDataSetInt((PetscObject)mglevels[i]->cr, PetscMGLevelId, mglevels[i]->level));
916: PetscCall(KSPSetType(mglevels[i]->cr, KSPCHEBYSHEV));
917: PetscCall(KSPSetConvergenceTest(mglevels[i]->cr, KSPConvergedSkip, NULL, NULL));
918: PetscCall(KSPSetNormType(mglevels[i]->cr, KSP_NORM_PRECONDITIONED));
919: PetscCall(KSPGetPC(mglevels[i]->cr, &ipc));
921: PetscCall(PCSetType(ipc, PCCOMPOSITE));
922: PetscCall(PCCompositeSetType(ipc, PC_COMPOSITE_MULTIPLICATIVE));
923: PetscCall(PCCompositeAddPCType(ipc, PCSOR));
924: PetscCall(CreateCR_Private(pc, i, &cr));
925: PetscCall(PCCompositeAddPC(ipc, cr));
926: PetscCall(PCDestroy(&cr));
928: PetscCall(KSPSetTolerances(mglevels[i]->cr, PETSC_CURRENT, PETSC_CURRENT, PETSC_CURRENT, mg->default_smoothd));
929: PetscCall(KSPSetInitialGuessNonzero(mglevels[i]->cr, PETSC_TRUE));
930: PetscCall(PetscSNPrintf(crprefix, 128, "mg_levels_%" PetscInt_FMT "_cr_", i));
931: PetscCall(KSPAppendOptionsPrefix(mglevels[i]->cr, crprefix));
932: }
933: }
935: PetscCall(PCGetUseAmat(pc, &use_amat));
936: if (!opsset) {
937: if (use_amat) {
938: PetscCall(PetscInfo(pc, "Using outer operators to define finest grid operator \n because PCMGGetSmoother(pc,nlevels-1,&ksp);KSPSetOperators(ksp,...); was not called.\n"));
939: PetscCall(KSPSetOperators(mglevels[n - 1]->smoothd, pc->mat, pc->pmat));
940: } else {
941: PetscCall(PetscInfo(pc, "Using matrix (pmat) operators to define finest grid operator \n because PCMGGetSmoother(pc,nlevels-1,&ksp);KSPSetOperators(ksp,...); was not called.\n"));
942: PetscCall(KSPSetOperators(mglevels[n - 1]->smoothd, pc->pmat, pc->pmat));
943: }
944: }
946: for (PetscInt i = n - 1; i > 0; i--) {
947: if (!(mglevels[i]->interpolate || mglevels[i]->restrct)) {
948: missinginterpolate = PETSC_TRUE;
949: break;
950: }
951: }
953: PetscCall(KSPGetOperators(mglevels[n - 1]->smoothd, &dA, &dB));
954: if (dA == dB) dAeqdB = PETSC_TRUE;
955: if (mg->galerkin == PC_MG_GALERKIN_NONE || ((mg->galerkin == PC_MG_GALERKIN_PMAT || mg->galerkin == PC_MG_GALERKIN_MAT) && !dAeqdB)) needRestricts = PETSC_TRUE; /* user must compute either mat, pmat, or both so must restrict x to coarser levels */
957: if (pc->dm && !pc->setupcalled) {
958: /* finest smoother also gets DM but it is not active, independent of whether galerkin==PC_MG_GALERKIN_EXTERNAL */
959: PetscCall(KSPSetDM(mglevels[n - 1]->smoothd, pc->dm));
960: PetscCall(KSPSetDMActive(mglevels[n - 1]->smoothd, KSP_DMACTIVE_ALL, PETSC_FALSE));
961: if (mglevels[n - 1]->smoothd != mglevels[n - 1]->smoothu) {
962: PetscCall(KSPSetDM(mglevels[n - 1]->smoothu, pc->dm));
963: PetscCall(KSPSetDMActive(mglevels[n - 1]->smoothu, KSP_DMACTIVE_ALL, PETSC_FALSE));
964: }
965: if (mglevels[n - 1]->cr) {
966: PetscCall(KSPSetDM(mglevels[n - 1]->cr, pc->dm));
967: PetscCall(KSPSetDMActive(mglevels[n - 1]->cr, KSP_DMACTIVE_ALL, PETSC_FALSE));
968: }
969: }
971: /*
972: Skipping if user has provided all interpolation/restriction needed (since DM might not be able to produce them (when coming from SNES/TS)
973: Skipping for externally managed hierarchy (such as ML and GAMG). Cleaner logic here would be great. Wrap ML/GAMG as DMs?
974: */
975: if (missinginterpolate && mg->galerkin != PC_MG_GALERKIN_EXTERNAL && !pc->setupcalled) {
976: /* first see if we can compute a coarse space */
977: if (mg->coarseSpaceType == PCMG_ADAPT_GDSW) {
978: for (PetscInt i = n - 2; i > -1; i--) {
979: if (!mglevels[i + 1]->restrct && !mglevels[i + 1]->interpolate) {
980: PetscCall(PCMGComputeCoarseSpace_Internal(pc, i + 1, mg->coarseSpaceType, mg->Nc, NULL, &mglevels[i + 1]->coarseSpace));
981: PetscCall(PCMGSetInterpolation(pc, i + 1, mglevels[i + 1]->coarseSpace));
982: }
983: }
984: } else { /* construct the interpolation from the DMs */
985: Mat p;
986: Vec rscale;
988: PetscCheck(n == 1 || pc->dm, PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "PC lacks a DM so cannot automatically construct a multigrid hierarchy. Number of levels requested %" PetscInt_FMT, n);
989: PetscCall(PetscMalloc1(n, &dms));
990: dms[n - 1] = pc->dm;
991: /* Separately create them so we do not get DMKSP interference between levels */
992: for (PetscInt i = n - 2; i > -1; i--) PetscCall(DMCoarsen(dms[i + 1], MPI_COMM_NULL, &dms[i]));
993: for (PetscInt i = n - 2; i > -1; i--) {
994: PetscBool dmhasrestrict, dmhasinject;
996: PetscCall(KSPSetDM(mglevels[i]->smoothd, dms[i]));
997: if (!needRestricts) PetscCall(KSPSetDMActive(mglevels[i]->smoothd, KSP_DMACTIVE_ALL, PETSC_FALSE));
998: PetscCall(KSPSetDMActive(mglevels[i]->smoothd, KSP_DMACTIVE_RHS, PETSC_FALSE));
999: if (mglevels[i]->smoothd != mglevels[i]->smoothu) {
1000: PetscCall(KSPSetDM(mglevels[i]->smoothu, dms[i]));
1001: if (!needRestricts) PetscCall(KSPSetDMActive(mglevels[i]->smoothu, KSP_DMACTIVE_ALL, PETSC_FALSE));
1002: PetscCall(KSPSetDMActive(mglevels[i]->smoothu, KSP_DMACTIVE_RHS, PETSC_FALSE));
1003: }
1004: if (mglevels[i]->cr) {
1005: PetscCall(KSPSetDM(mglevels[i]->cr, dms[i]));
1006: if (!needRestricts) PetscCall(KSPSetDMActive(mglevels[i]->cr, KSP_DMACTIVE_ALL, PETSC_FALSE));
1007: PetscCall(KSPSetDMActive(mglevels[i]->cr, KSP_DMACTIVE_RHS, PETSC_FALSE));
1008: }
1009: if (!mglevels[i + 1]->interpolate) {
1010: PetscCall(DMCreateInterpolation(dms[i], dms[i + 1], &p, &rscale));
1011: PetscCall(PCMGSetInterpolation(pc, i + 1, p));
1012: if (rscale) PetscCall(PCMGSetRScale(pc, i + 1, rscale));
1013: PetscCall(VecDestroy(&rscale));
1014: PetscCall(MatDestroy(&p));
1015: }
1016: PetscCall(DMHasCreateRestriction(dms[i], &dmhasrestrict));
1017: if (dmhasrestrict && !mglevels[i + 1]->restrct) {
1018: PetscCall(DMCreateRestriction(dms[i], dms[i + 1], &p));
1019: PetscCall(PCMGSetRestriction(pc, i + 1, p));
1020: PetscCall(MatDestroy(&p));
1021: }
1022: PetscCall(DMHasCreateInjection(dms[i], &dmhasinject));
1023: if (dmhasinject && !mglevels[i + 1]->inject) {
1024: PetscCall(DMCreateInjection(dms[i], dms[i + 1], &p));
1025: PetscCall(PCMGSetInjection(pc, i + 1, p));
1026: PetscCall(MatDestroy(&p));
1027: }
1028: }
1030: for (PetscInt i = n - 2; i > -1; i--) PetscCall(DMDestroy(&dms[i]));
1031: PetscCall(PetscFree(dms));
1032: }
1033: }
1035: if (mg->galerkin < PC_MG_GALERKIN_NONE) {
1036: Mat A, B;
1037: PetscBool doA = PETSC_FALSE, doB = PETSC_FALSE;
1038: MatReuse reuse = MAT_INITIAL_MATRIX;
1040: if (mg->galerkin == PC_MG_GALERKIN_PMAT || mg->galerkin == PC_MG_GALERKIN_BOTH) doB = PETSC_TRUE;
1041: if (mg->galerkin == PC_MG_GALERKIN_MAT || (mg->galerkin == PC_MG_GALERKIN_BOTH && dA != dB)) doA = PETSC_TRUE;
1042: PetscCheck(!doA || use_amat, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "PC_MG_GALERKIN_MAT and PCSetUseAmat(pc, PETSC_FALSE) are incompatible options");
1043: if (pc->setupcalled) reuse = MAT_REUSE_MATRIX;
1044: for (PetscInt i = n - 2; i > -1; i--) {
1045: PetscCheck(mglevels[i + 1]->restrct || mglevels[i + 1]->interpolate, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Must provide interpolation or restriction for each MG level except level 0");
1046: if (!mglevels[i + 1]->interpolate) PetscCall(PCMGSetInterpolation(pc, i + 1, mglevels[i + 1]->restrct));
1047: if (!mglevels[i + 1]->restrct) PetscCall(PCMGSetRestriction(pc, i + 1, mglevels[i + 1]->interpolate));
1048: if (reuse == MAT_REUSE_MATRIX) PetscCall(KSPGetOperators(mglevels[i]->smoothd, &A, &B));
1049: if (doA) PetscCall(MatGalerkin(mglevels[i + 1]->restrct, dA, mglevels[i + 1]->interpolate, reuse, 1.0, &A));
1050: if (doB) PetscCall(MatGalerkin(mglevels[i + 1]->restrct, dB, mglevels[i + 1]->interpolate, reuse, 1.0, &B));
1051: /* the management of the PetscObjectReference() and PetscObjecDereference() below is rather delicate */
1052: if (!doA && dAeqdB) {
1053: if (reuse == MAT_INITIAL_MATRIX) PetscCall(PetscObjectReference((PetscObject)B));
1054: A = B;
1055: } else if (!doA && reuse == MAT_INITIAL_MATRIX) {
1056: PetscCall(KSPGetOperators(mglevels[i]->smoothd, &A, NULL));
1057: PetscCall(PetscObjectReference((PetscObject)A));
1058: }
1059: if (!doB && dAeqdB) {
1060: if (reuse == MAT_INITIAL_MATRIX) PetscCall(PetscObjectReference((PetscObject)A));
1061: B = A;
1062: } else if (!doB && reuse == MAT_INITIAL_MATRIX) {
1063: PetscCall(KSPGetOperators(mglevels[i]->smoothd, NULL, &B));
1064: PetscCall(PetscObjectReference((PetscObject)B));
1065: }
1066: if (reuse == MAT_INITIAL_MATRIX) {
1067: PetscCall(KSPSetOperators(mglevels[i]->smoothd, A, B));
1068: PetscCall(PetscObjectDereference((PetscObject)A));
1069: PetscCall(PetscObjectDereference((PetscObject)B));
1070: }
1071: dA = A;
1072: dB = B;
1073: }
1074: } else { /* PC_MG_GALERKIN_NONE */
1075: if (!use_amat) { /* force KSP(P, P) at all levels */
1076: for (PetscInt i = n - 1; i > -1; i--) {
1077: Mat B;
1078: PetscBool Bopset;
1079: KSP smoothd = mglevels[i]->smoothd;
1081: PetscCall(KSPGetOperatorsSet(smoothd, NULL, &Bopset));
1082: /* This is a chicken-and-egg problem when DMKSP has a create operator callback.
1083: It would be called at KSPSetUp stage, but then it is too late to handle the amat = False case */
1084: if (!Bopset && (smoothd->dmActive & KSP_DMACTIVE_OPERATOR) && smoothd->dm) {
1085: DMKSP kdm;
1087: PetscCall(DMGetDMKSP(smoothd->dm, &kdm));
1088: if (kdm->ops->createoperators) {
1089: Mat A;
1091: A = B = NULL;
1092: PetscCallBack("KSP callback create operators", (*kdm->ops->createoperators)(smoothd, &A, &B, kdm->createoperatorsctx));
1093: PetscCheck(A, PetscObjectComm((PetscObject)smoothd), PETSC_ERR_ARG_WRONGSTATE, "Missing A operator from DMKSPSetCreateOperators() callback");
1094: if (!B) B = A;
1095: if (B == A) PetscCall(PetscObjectReference((PetscObject)B));
1096: PetscCall(KSPSetOperators(smoothd, B, B));
1097: PetscCall(MatDestroy(&A));
1098: PetscCall(MatDestroy(&B));
1099: }
1100: } else {
1101: PetscCheck(Bopset, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Missing Pmat level %" PetscInt_FMT, i);
1102: PetscCall(KSPGetOperators(smoothd, NULL, &B));
1103: PetscCall(KSPSetOperators(smoothd, B, B));
1104: }
1105: }
1106: }
1107: }
1109: /* Adapt interpolation matrices */
1110: if (adaptInterpolation) {
1111: for (PetscInt i = 0; i < n; ++i) {
1112: if (!mglevels[i]->coarseSpace) PetscCall(PCMGComputeCoarseSpace_Internal(pc, i, mg->coarseSpaceType, mg->Nc, !i ? NULL : mglevels[i - 1]->coarseSpace, &mglevels[i]->coarseSpace));
1113: if (i) PetscCall(PCMGAdaptInterpolator_Internal(pc, i, mglevels[i - 1]->smoothu, mglevels[i]->smoothu, mglevels[i - 1]->coarseSpace, mglevels[i]->coarseSpace));
1114: }
1115: for (PetscInt i = n - 2; i > -1; --i) PetscCall(PCMGRecomputeLevelOperators_Internal(pc, i));
1116: }
1118: if (needRestricts && pc->dm) {
1119: for (PetscInt i = n - 2; i >= 0; i--) {
1120: DM dmfine, dmcoarse;
1121: Mat Restrict, Inject;
1122: Vec rscale;
1124: PetscCall(KSPGetDM(mglevels[i + 1]->smoothd, &dmfine));
1125: PetscCall(KSPGetDM(mglevels[i]->smoothd, &dmcoarse));
1126: PetscCall(PCMGGetRestriction(pc, i + 1, &Restrict));
1127: PetscCall(PCMGGetRScale(pc, i + 1, &rscale));
1128: PetscCall(PCMGGetInjection(pc, i + 1, &Inject));
1129: PetscCall(DMRestrict(dmfine, Restrict, rscale, Inject, dmcoarse));
1130: }
1131: }
1133: if (!pc->setupcalled) {
1134: for (PetscInt i = 0; i < n; i++) PetscCall(KSPSetFromOptions(mglevels[i]->smoothd));
1135: for (PetscInt i = 1; i < n; i++) {
1136: if (mglevels[i]->smoothu && (mglevels[i]->smoothu != mglevels[i]->smoothd)) PetscCall(KSPSetFromOptions(mglevels[i]->smoothu));
1137: if (mglevels[i]->cr) PetscCall(KSPSetFromOptions(mglevels[i]->cr));
1138: }
1139: /* insure that if either interpolation or restriction is set the other one is set */
1140: for (PetscInt i = 1; i < n; i++) {
1141: PetscCall(PCMGGetInterpolation(pc, i, NULL));
1142: PetscCall(PCMGGetRestriction(pc, i, NULL));
1143: }
1144: for (PetscInt i = 0; i < n - 1; i++) {
1145: if (!mglevels[i]->b) {
1146: Vec *vec;
1147: PetscCall(KSPCreateVecs(mglevels[i]->smoothd, 1, &vec, 0, NULL));
1148: PetscCall(PCMGSetRhs(pc, i, *vec));
1149: PetscCall(VecDestroy(vec));
1150: PetscCall(PetscFree(vec));
1151: }
1152: if (!mglevels[i]->r && i) {
1153: PetscCall(VecDuplicate(mglevels[i]->b, &tvec));
1154: PetscCall(PCMGSetR(pc, i, tvec));
1155: PetscCall(VecDestroy(&tvec));
1156: }
1157: if (!mglevels[i]->x) {
1158: PetscCall(VecDuplicate(mglevels[i]->b, &tvec));
1159: PetscCall(PCMGSetX(pc, i, tvec));
1160: PetscCall(VecDestroy(&tvec));
1161: }
1162: if (doCR) {
1163: PetscCall(VecDuplicate(mglevels[i]->b, &mglevels[i]->crx));
1164: PetscCall(VecDuplicate(mglevels[i]->b, &mglevels[i]->crb));
1165: }
1166: }
1167: if (n != 1 && !mglevels[n - 1]->r) {
1168: /* PCMGSetR() on the finest level if user did not supply it */
1169: Vec *vec;
1171: PetscCall(KSPCreateVecs(mglevels[n - 1]->smoothd, 1, &vec, 0, NULL));
1172: PetscCall(PCMGSetR(pc, n - 1, *vec));
1173: PetscCall(VecDestroy(vec));
1174: PetscCall(PetscFree(vec));
1175: }
1176: if (doCR) {
1177: PetscCall(VecDuplicate(mglevels[n - 1]->r, &mglevels[n - 1]->crx));
1178: PetscCall(VecDuplicate(mglevels[n - 1]->r, &mglevels[n - 1]->crb));
1179: }
1180: }
1182: if (pc->dm) {
1183: /* need to tell all the coarser levels to rebuild the matrix using the DM for that level */
1184: for (PetscInt i = 0; i < n - 1; i++) {
1185: if (mglevels[i]->smoothd->setupstage != KSP_SETUP_NEW) mglevels[i]->smoothd->setupstage = KSP_SETUP_NEWMATRIX;
1186: }
1187: }
1188: // We got here (PCSetUp_MG) because the matrix has changed, which means the smoother needs to be set up again (e.g.,
1189: // new diagonal for Jacobi). Setting it here allows it to be logged under PCSetUp rather than deep inside a PCApply.
1190: if (mglevels[n - 1]->smoothd->setupstage != KSP_SETUP_NEW) mglevels[n - 1]->smoothd->setupstage = KSP_SETUP_NEWMATRIX;
1192: for (PetscInt i = 1; i < n; i++) {
1193: if (mglevels[i]->smoothu == mglevels[i]->smoothd || mg->am == PC_MG_FULL || mg->am == PC_MG_KASKADE || mg->cyclesperpcapply > 1) {
1194: /* if doing only down then initial guess is zero */
1195: PetscCall(KSPSetInitialGuessNonzero(mglevels[i]->smoothd, PETSC_TRUE));
1196: }
1197: if (mglevels[i]->cr) PetscCall(KSPSetInitialGuessNonzero(mglevels[i]->cr, PETSC_TRUE));
1198: if (mglevels[i]->eventsmoothsetup) PetscCall(PetscLogEventBegin(mglevels[i]->eventsmoothsetup, 0, 0, 0, 0));
1199: PetscCall(KSPSetUp(mglevels[i]->smoothd));
1200: if (mglevels[i]->smoothd->reason) pc->failedreason = PC_SUBPC_ERROR;
1201: if (mglevels[i]->eventsmoothsetup) PetscCall(PetscLogEventEnd(mglevels[i]->eventsmoothsetup, 0, 0, 0, 0));
1202: if (!mglevels[i]->residual) {
1203: Mat mat;
1205: PetscCall(KSPGetOperators(mglevels[i]->smoothd, &mat, NULL));
1206: PetscCall(PCMGSetResidual(pc, i, PCMGResidualDefault, mat));
1207: }
1208: if (!mglevels[i]->residualtranspose) {
1209: Mat mat;
1211: PetscCall(KSPGetOperators(mglevels[i]->smoothd, &mat, NULL));
1212: PetscCall(PCMGSetResidualTranspose(pc, i, PCMGResidualTransposeDefault, mat));
1213: }
1214: }
1215: for (PetscInt i = 1; i < n; i++) {
1216: if (mglevels[i]->smoothu && mglevels[i]->smoothu != mglevels[i]->smoothd) {
1217: Mat downmat, downpmat;
1219: /* check if operators have been set for up, if not use down operators to set them */
1220: PetscCall(KSPGetOperatorsSet(mglevels[i]->smoothu, &opsset, NULL));
1221: if (!opsset) {
1222: PetscCall(KSPGetOperators(mglevels[i]->smoothd, &downmat, &downpmat));
1223: PetscCall(KSPSetOperators(mglevels[i]->smoothu, downmat, downpmat));
1224: }
1226: PetscCall(KSPSetInitialGuessNonzero(mglevels[i]->smoothu, PETSC_TRUE));
1227: if (mglevels[i]->eventsmoothsetup) PetscCall(PetscLogEventBegin(mglevels[i]->eventsmoothsetup, 0, 0, 0, 0));
1228: PetscCall(KSPSetUp(mglevels[i]->smoothu));
1229: if (mglevels[i]->smoothu->reason) pc->failedreason = PC_SUBPC_ERROR;
1230: if (mglevels[i]->eventsmoothsetup) PetscCall(PetscLogEventEnd(mglevels[i]->eventsmoothsetup, 0, 0, 0, 0));
1231: }
1232: if (mglevels[i]->cr) {
1233: Mat downmat, downpmat;
1235: /* check if operators have been set for up, if not use down operators to set them */
1236: PetscCall(KSPGetOperatorsSet(mglevels[i]->cr, &opsset, NULL));
1237: if (!opsset) {
1238: PetscCall(KSPGetOperators(mglevels[i]->smoothd, &downmat, &downpmat));
1239: PetscCall(KSPSetOperators(mglevels[i]->cr, downmat, downpmat));
1240: }
1242: PetscCall(KSPSetInitialGuessNonzero(mglevels[i]->cr, PETSC_TRUE));
1243: if (mglevels[i]->eventsmoothsetup) PetscCall(PetscLogEventBegin(mglevels[i]->eventsmoothsetup, 0, 0, 0, 0));
1244: PetscCall(KSPSetUp(mglevels[i]->cr));
1245: if (mglevels[i]->cr->reason) pc->failedreason = PC_SUBPC_ERROR;
1246: if (mglevels[i]->eventsmoothsetup) PetscCall(PetscLogEventEnd(mglevels[i]->eventsmoothsetup, 0, 0, 0, 0));
1247: }
1248: }
1250: if (mglevels[0]->eventsmoothsetup) PetscCall(PetscLogEventBegin(mglevels[0]->eventsmoothsetup, 0, 0, 0, 0));
1251: PetscCall(KSPSetUp(mglevels[0]->smoothd));
1252: if (mglevels[0]->smoothd->reason) pc->failedreason = PC_SUBPC_ERROR;
1253: if (mglevels[0]->eventsmoothsetup) PetscCall(PetscLogEventEnd(mglevels[0]->eventsmoothsetup, 0, 0, 0, 0));
1255: /*
1256: Dump the interpolation/restriction matrices plus the
1257: Jacobian/stiffness on each level. This allows MATLAB users to
1258: easily check if the Galerkin condition A_c = R A_f R^T is satisfied.
1260: Only support one or the other at the same time.
1261: */
1262: #if PetscDefined(USE_SOCKET_VIEWER)
1263: PetscCall(PetscOptionsGetBool(((PetscObject)pc)->options, ((PetscObject)pc)->prefix, "-pc_mg_dump_matlab", &dump, NULL));
1264: if (dump) viewer = PETSC_VIEWER_SOCKET_(PetscObjectComm((PetscObject)pc));
1265: dump = PETSC_FALSE;
1266: #endif
1267: PetscCall(PetscOptionsGetBool(((PetscObject)pc)->options, ((PetscObject)pc)->prefix, "-pc_mg_dump_binary", &dump, NULL));
1268: if (dump) viewer = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)pc));
1270: if (viewer) {
1271: for (PetscInt i = 1; i < n; i++) PetscCall(MatView(mglevels[i]->restrct, viewer));
1272: for (PetscInt i = 0; i < n; i++) {
1273: PetscCall(KSPGetPC(mglevels[i]->smoothd, &pc));
1274: PetscCall(MatView(pc->mat, viewer));
1275: }
1276: }
1277: PetscFunctionReturn(PETSC_SUCCESS);
1278: }
1280: PetscErrorCode PCMGGetLevels_MG(PC pc, PetscInt *levels)
1281: {
1282: PC_MG *mg = (PC_MG *)pc->data;
1284: PetscFunctionBegin;
1285: *levels = mg->nlevels;
1286: PetscFunctionReturn(PETSC_SUCCESS);
1287: }
1289: /*@
1290: PCMGGetLevels - Gets the number of levels to use with `PCMG`.
1292: Not Collective
1294: Input Parameter:
1295: . pc - the preconditioner context
1297: Output Parameter:
1298: . levels - the number of levels
1300: Level: advanced
1302: .seealso: [](ch_ksp), `PCMG`, `PCMGSetLevels()`
1303: @*/
1304: PetscErrorCode PCMGGetLevels(PC pc, PetscInt *levels)
1305: {
1306: PetscFunctionBegin;
1308: PetscAssertPointer(levels, 2);
1309: *levels = 0;
1310: PetscTryMethod(pc, "PCMGGetLevels_C", (PC, PetscInt *), (pc, levels));
1311: PetscFunctionReturn(PETSC_SUCCESS);
1312: }
1314: /*@
1315: PCMGGetGridComplexity - compute operator and grid complexity of the `PCMG` hierarchy
1317: Input Parameter:
1318: . pc - the preconditioner context
1320: Output Parameters:
1321: + gc - grid complexity, $\frac{\sum_i n_i}{n_0}$, where $n_0$ is the number of unknowns on the finest grid
1322: - oc - operator complexity, $\frac{\sum_i nnz_i}{nnz_0}$, where $nnz_0$ is the number of nonzeros on the finest grid
1324: Level: advanced
1326: .seealso: [](ch_ksp), `PCMG`, `PCMGGetLevels()`, `PCMGSetLevels()`
1327: @*/
1328: PetscErrorCode PCMGGetGridComplexity(PC pc, PetscReal *gc, PetscReal *oc)
1329: {
1330: PC_MG *mg = (PC_MG *)pc->data;
1331: PC_MG_Levels **mglevels = mg->levels;
1332: PetscInt N;
1333: PetscLogDouble nnz0 = 0, sgc = 0, soc = 0, n0 = 0;
1334: MatInfo info;
1336: PetscFunctionBegin;
1338: if (gc) PetscAssertPointer(gc, 2);
1339: if (oc) PetscAssertPointer(oc, 3);
1340: if (!pc->setupcalled) {
1341: if (gc) *gc = 0;
1342: if (oc) *oc = 0;
1343: PetscFunctionReturn(PETSC_SUCCESS);
1344: }
1345: PetscCheck(mg->nlevels > 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "MG has no levels");
1346: for (PetscInt lev = 0; lev < mg->nlevels; lev++) {
1347: Mat dB;
1348: PetscCall(KSPGetOperators(mglevels[lev]->smoothd, NULL, &dB));
1349: PetscCall(MatGetInfo(dB, MAT_GLOBAL_SUM, &info)); /* global reduction */
1350: PetscCall(MatGetSize(dB, &N, NULL));
1351: sgc += N;
1352: soc += info.nz_used;
1353: if (lev == mg->nlevels - 1) {
1354: nnz0 = info.nz_used;
1355: n0 = N;
1356: }
1357: }
1358: PetscCheck(n0 > 0 && gc, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Number for grid points on finest level is not available");
1359: *gc = (PetscReal)(sgc / n0);
1360: if (nnz0 > 0 && oc) *oc = (PetscReal)(soc / nnz0);
1361: PetscFunctionReturn(PETSC_SUCCESS);
1362: }
1364: /*@
1365: PCMGSetType - Determines the type of multigrid to use, either
1366: multiplicative, additive, full, or the Kaskade algorithm.
1368: Logically Collective
1370: Input Parameters:
1371: + pc - the preconditioner context
1372: - form - multigrid form, one of `PC_MG_MULTIPLICATIVE`, `PC_MG_ADDITIVE`, `PC_MG_FULL`, `PC_MG_KASKADE`
1374: Options Database Key:
1375: . -pc_mg_type (multiplicative|additive|full|kaskade) - Sets the type
1377: Level: advanced
1379: .seealso: [](ch_ksp), `PCMGType`, `PCMG`, `PCMGGetLevels()`, `PCMGSetLevels()`, `PCMGGetType()`, `PCMGCycleType`,
1380: `PC_MG_MULTIPLICATIVE`, `PC_MG_ADDITIVE`, `PC_MG_FULL`, `PC_MG_KASKADE`
1381: @*/
1382: PetscErrorCode PCMGSetType(PC pc, PCMGType form)
1383: {
1384: PC_MG *mg = (PC_MG *)pc->data;
1386: PetscFunctionBegin;
1389: mg->am = form;
1390: if (form == PC_MG_MULTIPLICATIVE) pc->ops->applyrichardson = PCApplyRichardson_MG;
1391: else pc->ops->applyrichardson = NULL;
1392: PetscFunctionReturn(PETSC_SUCCESS);
1393: }
1395: /*@
1396: PCMGGetType - Finds the form of multigrid the `PCMG` is using multiplicative, additive, full, or the Kaskade algorithm.
1398: Logically Collective
1400: Input Parameter:
1401: . pc - the preconditioner context
1403: Output Parameter:
1404: . type - one of `PC_MG_MULTIPLICATIVE`, `PC_MG_ADDITIVE`, `PC_MG_FULL`, `PC_MG_KASKADE`, `PCMGCycleType`
1406: Level: advanced
1408: .seealso: [](ch_ksp), `PCMGType`, `PCMG`, `PCMGGetLevels()`, `PCMGSetLevels()`, `PCMGSetType()`,
1409: `PC_MG_MULTIPLICATIVE`, `PC_MG_ADDITIVE`, `PC_MG_FULL`, `PC_MG_KASKADE`
1410: @*/
1411: PetscErrorCode PCMGGetType(PC pc, PCMGType *type)
1412: {
1413: PC_MG *mg = (PC_MG *)pc->data;
1415: PetscFunctionBegin;
1417: *type = mg->am;
1418: PetscFunctionReturn(PETSC_SUCCESS);
1419: }
1421: /*@
1422: PCMGSetCycleType - Sets the type of cycles to use. Use `PCMGSetCycleTypeOnLevel()` for more
1423: complicated cycling.
1425: Logically Collective
1427: Input Parameters:
1428: + pc - the multigrid context
1429: - n - either `PC_MG_CYCLE_V` or `PC_MG_CYCLE_W`
1431: Options Database Key:
1432: . -pc_mg_cycle_type (v|w) - provide the cycle desired
1434: Level: advanced
1436: .seealso: [](ch_ksp), `PCMG`, `PCMGSetCycleTypeOnLevel()`, `PCMGType`, `PCMGCycleType`, `PC_MG_CYCLE_V`, `PC_MG_CYCLE_W`
1437: @*/
1438: PetscErrorCode PCMGSetCycleType(PC pc, PCMGCycleType n)
1439: {
1440: PC_MG *mg = (PC_MG *)pc->data;
1441: PC_MG_Levels **mglevels = mg->levels;
1442: PetscInt i, levels;
1444: PetscFunctionBegin;
1447: PetscCheck(mglevels, PetscObjectComm((PetscObject)pc), PETSC_ERR_ORDER, "Must set MG levels with PCMGSetLevels() before calling");
1448: levels = mglevels[0]->levels;
1449: for (i = 0; i < levels; i++) mglevels[i]->cycles = n;
1450: PetscFunctionReturn(PETSC_SUCCESS);
1451: }
1453: /*@
1454: PCMGMultiplicativeSetCycles - Sets the number of cycles to use for each preconditioner step
1455: of multigrid when `PCMGType` is `PC_MG_MULTIPLICATIVE`
1457: Logically Collective
1459: Input Parameters:
1460: + pc - the multigrid context
1461: - n - number of cycles (default is 1)
1463: Options Database Key:
1464: . -pc_mg_multiplicative_cycles n - set the number of cycles
1466: Level: advanced
1468: Note:
1469: This is not associated with setting a V or W cycle, that is set with `PCMGSetCycleType()`
1471: .seealso: [](ch_ksp), `PCMGSetCycleTypeOnLevel()`, `PCMGSetCycleType()`, `PCMGCycleType`, `PCMGType`, `PC_MG_MULTIPLICATIVE`
1472: @*/
1473: PetscErrorCode PCMGMultiplicativeSetCycles(PC pc, PetscInt n)
1474: {
1475: PC_MG *mg = (PC_MG *)pc->data;
1477: PetscFunctionBegin;
1480: mg->cyclesperpcapply = n;
1481: PetscFunctionReturn(PETSC_SUCCESS);
1482: }
1484: /*
1485: Since the finest level KSP shares the original matrix (of the entire system), it's preconditioner
1486: should not be updated if the whole PC is supposed to reuse the preconditioner
1487: */
1488: static PetscErrorCode PCSetReusePreconditioner_MG(PC pc, PetscBool flag)
1489: {
1490: PC_MG *mg = (PC_MG *)pc->data;
1491: PC_MG_Levels **mglevels = mg->levels;
1492: PetscInt levels;
1493: PC tpc;
1495: PetscFunctionBegin;
1498: if (mglevels) {
1499: levels = mglevels[0]->levels;
1500: PetscCall(KSPGetPC(mglevels[levels - 1]->smoothd, &tpc));
1501: tpc->reusepreconditioner = flag;
1502: PetscCall(KSPGetPC(mglevels[levels - 1]->smoothu, &tpc));
1503: tpc->reusepreconditioner = flag;
1504: }
1505: PetscFunctionReturn(PETSC_SUCCESS);
1506: }
1508: static PetscErrorCode PCMGSetGalerkin_MG(PC pc, PCMGGalerkinType use)
1509: {
1510: PC_MG *mg = (PC_MG *)pc->data;
1512: PetscFunctionBegin;
1513: mg->galerkin = use;
1514: PetscFunctionReturn(PETSC_SUCCESS);
1515: }
1517: /*@
1518: PCMGSetGalerkin - Causes the coarser grid matrices to be computed from the
1519: finest grid via the Galerkin process: $A_{i-1} = r_i * A_i * p_i$.
1521: Logically Collective
1523: Input Parameters:
1524: + pc - the multigrid context
1525: - use - one of `PC_MG_GALERKIN_BOTH`, `PC_MG_GALERKIN_PMAT`, `PC_MG_GALERKIN_MAT`, or `PC_MG_GALERKIN_NONE`
1527: Options Database Key:
1528: . -pc_mg_galerkin (both|pmat|mat|none) - set the matrices to form via the Galerkin process
1530: Level: intermediate
1532: Notes:
1533: Some codes that use `PCMG` such as `PCGAMG` use Galerkin internally while constructing the hierarchy and thus do not
1534: use the `PCMG` construction of the coarser grids.
1536: If this is not used the coarser grid matrices are computed via re-discretization. That is by calling the function associated with the `DM` attached
1537: to the `PC`. For example, for nonlinear solves the function provided with `SNESSetJacobian()`.
1539: .seealso: [](ch_ksp), `PCMG`, `PCMGGetGalerkin()`, `PCMGGalerkinType`, `PC_MG_GALERKIN_BOTH`, `PC_MG_GALERKIN_PMAT`, `PC_MG_GALERKIN_MAT`, `PC_MG_GALERKIN_NONE`
1540: @*/
1541: PetscErrorCode PCMGSetGalerkin(PC pc, PCMGGalerkinType use)
1542: {
1543: PetscFunctionBegin;
1545: PetscTryMethod(pc, "PCMGSetGalerkin_C", (PC, PCMGGalerkinType), (pc, use));
1546: PetscFunctionReturn(PETSC_SUCCESS);
1547: }
1549: /*@
1550: PCMGGetGalerkin - Checks if Galerkin multigrid is being used, i.e. $A_{i-1} = r_i * A_i * p_i$.
1552: Not Collective
1554: Input Parameter:
1555: . pc - the multigrid context
1557: Output Parameter:
1558: . galerkin - one of `PC_MG_GALERKIN_BOTH`,`PC_MG_GALERKIN_PMAT`,`PC_MG_GALERKIN_MAT`, `PC_MG_GALERKIN_NONE`, or `PC_MG_GALERKIN_EXTERNAL`
1560: Level: intermediate
1562: .seealso: [](ch_ksp), `PCMG`, `PCMGSetGalerkin()`, `PCMGGalerkinType`, `PC_MG_GALERKIN_BOTH`, `PC_MG_GALERKIN_PMAT`, `PC_MG_GALERKIN_MAT`, `PC_MG_GALERKIN_NONE`, `PC_MG_GALERKIN_EXTERNAL`
1563: @*/
1564: PetscErrorCode PCMGGetGalerkin(PC pc, PCMGGalerkinType *galerkin)
1565: {
1566: PC_MG *mg = (PC_MG *)pc->data;
1568: PetscFunctionBegin;
1570: *galerkin = mg->galerkin;
1571: PetscFunctionReturn(PETSC_SUCCESS);
1572: }
1574: static PetscErrorCode PCMGSetAdaptInterpolation_MG(PC pc, PetscBool adapt)
1575: {
1576: PC_MG *mg = (PC_MG *)pc->data;
1578: PetscFunctionBegin;
1579: mg->adaptInterpolation = adapt;
1580: PetscFunctionReturn(PETSC_SUCCESS);
1581: }
1583: static PetscErrorCode PCMGGetAdaptInterpolation_MG(PC pc, PetscBool *adapt)
1584: {
1585: PC_MG *mg = (PC_MG *)pc->data;
1587: PetscFunctionBegin;
1588: *adapt = mg->adaptInterpolation;
1589: PetscFunctionReturn(PETSC_SUCCESS);
1590: }
1592: static PetscErrorCode PCMGSetAdaptCoarseSpaceType_MG(PC pc, PCMGCoarseSpaceType ctype)
1593: {
1594: PC_MG *mg = (PC_MG *)pc->data;
1596: PetscFunctionBegin;
1597: mg->adaptInterpolation = ctype != PCMG_ADAPT_NONE ? PETSC_TRUE : PETSC_FALSE;
1598: mg->coarseSpaceType = ctype;
1599: PetscCall(PCMGSetGalerkin(pc, PC_MG_GALERKIN_BOTH));
1600: PetscFunctionReturn(PETSC_SUCCESS);
1601: }
1603: static PetscErrorCode PCMGGetAdaptCoarseSpaceType_MG(PC pc, PCMGCoarseSpaceType *ctype)
1604: {
1605: PC_MG *mg = (PC_MG *)pc->data;
1607: PetscFunctionBegin;
1608: *ctype = mg->coarseSpaceType;
1609: PetscFunctionReturn(PETSC_SUCCESS);
1610: }
1612: static PetscErrorCode PCMGSetAdaptCR_MG(PC pc, PetscBool cr)
1613: {
1614: PC_MG *mg = (PC_MG *)pc->data;
1616: PetscFunctionBegin;
1617: mg->compatibleRelaxation = cr;
1618: PetscFunctionReturn(PETSC_SUCCESS);
1619: }
1621: static PetscErrorCode PCMGGetAdaptCR_MG(PC pc, PetscBool *cr)
1622: {
1623: PC_MG *mg = (PC_MG *)pc->data;
1625: PetscFunctionBegin;
1626: *cr = mg->compatibleRelaxation;
1627: PetscFunctionReturn(PETSC_SUCCESS);
1628: }
1630: /*@
1631: PCMGSetAdaptCoarseSpaceType - Set the type of adaptive coarse space. Adapts or creates the interpolator based upon a vector space which should be accurately
1632: captured by the next coarser mesh, and thus accurately interpolated.
1634: Logically Collective
1636: Input Parameters:
1637: + pc - the multigrid context
1638: - ctype - the type of coarse space
1640: Options Database Keys:
1641: + -pc_mg_adapt_interp_n nmodes - The number of modes to use
1642: - -pc_mg_adapt_interp_coarse_space (none|polynomial|harmonic|eigenvector|generalized_eigenvector|gdsw) - The type of coarse space to use
1644: Level: intermediate
1646: Note:
1647: Requires a `DM` with specific functionality be attached to the `PC`.
1649: Developer Notes:
1650: The options database key `-pc_mg_adapt_interp_coarse_space` should not have interp in it since this function does not have interp in it.
1652: The options database key `-pc_mg_adapt_interp_n` has no functional call equivalent.
1654: .seealso: [](ch_ksp), `PCMG`, `PCMGCoarseSpaceType`, `PCMGGetAdaptCoarseSpaceType()`, `PCMGSetGalerkin()`, `PCMGSetAdaptInterpolation()`, `DM`,
1655: `PCMG_ADAPT_NONE`, `PCMG_ADAPT_POLYNOMIAL`, `PCMG_ADAPT_HARMONIC`, `PCMG_ADAPT_EIGENVECTOR`, `PCMG_ADAPT_GENERALIZED_EIGENVECTOR`,
1656: `PCMG_ADAPT_GDSW`
1657: @*/
1658: PetscErrorCode PCMGSetAdaptCoarseSpaceType(PC pc, PCMGCoarseSpaceType ctype)
1659: {
1660: PetscFunctionBegin;
1663: PetscTryMethod(pc, "PCMGSetAdaptCoarseSpaceType_C", (PC, PCMGCoarseSpaceType), (pc, ctype));
1664: PetscFunctionReturn(PETSC_SUCCESS);
1665: }
1667: /*@
1668: PCMGGetAdaptCoarseSpaceType - Get the type of adaptive coarse space.
1670: Not Collective
1672: Input Parameter:
1673: . pc - the multigrid context
1675: Output Parameter:
1676: . ctype - the type of coarse space
1678: Level: intermediate
1680: .seealso: [](ch_ksp), `PCMG`, `PCMGCoarseSpaceType`, `PCMGSetAdaptCoarseSpaceType()`, `PCMGSetGalerkin()`, `PCMGSetAdaptInterpolation()`
1681: @*/
1682: PetscErrorCode PCMGGetAdaptCoarseSpaceType(PC pc, PCMGCoarseSpaceType *ctype)
1683: {
1684: PetscFunctionBegin;
1686: PetscAssertPointer(ctype, 2);
1687: PetscUseMethod(pc, "PCMGGetAdaptCoarseSpaceType_C", (PC, PCMGCoarseSpaceType *), (pc, ctype));
1688: PetscFunctionReturn(PETSC_SUCCESS);
1689: }
1691: /*@
1692: PCMGSetAdaptInterpolation - Adapt the interpolator based upon a vector space which should be accurately captured by the next coarser mesh, and thus accurately interpolated.
1694: Logically Collective
1696: Input Parameters:
1697: + pc - the multigrid context
1698: - adapt - flag for adaptation of the interpolator
1700: Level: intermediate
1702: Note:
1703: This routine should never be used, rather call `PCMGSetAdaptCoarseSpaceType()`
1705: .seealso: [](ch_ksp), `PCMG`, `PCMGGetAdaptInterpolation()`, `PCMGSetGalerkin()`, `PCMGGetAdaptCoarseSpaceType()`, `PCMGSetAdaptCoarseSpaceType()`
1706: @*/
1707: PetscErrorCode PCMGSetAdaptInterpolation(PC pc, PetscBool adapt)
1708: {
1709: PetscFunctionBegin;
1711: PetscTryMethod(pc, "PCMGSetAdaptInterpolation_C", (PC, PetscBool), (pc, adapt));
1712: PetscFunctionReturn(PETSC_SUCCESS);
1713: }
1715: /*@
1716: PCMGGetAdaptInterpolation - Get the flag to adapt the interpolator based upon a vector space which should be accurately captured by the next coarser mesh,
1717: and thus accurately interpolated.
1719: Not Collective
1721: Input Parameter:
1722: . pc - the multigrid context
1724: Output Parameter:
1725: . adapt - flag for adaptation of the interpolator
1727: Level: intermediate
1729: Note:
1730: This routine should never be used, rather call `PCMGGetAdaptCoarseSpaceType()`
1732: .seealso: [](ch_ksp), `PCMG`, `PCMGSetAdaptInterpolation()`, `PCMGSetGalerkin()`, `PCMGGetAdaptCoarseSpaceType()`, `PCMGSetAdaptCoarseSpaceType()`
1733: @*/
1734: PetscErrorCode PCMGGetAdaptInterpolation(PC pc, PetscBool *adapt)
1735: {
1736: PetscFunctionBegin;
1738: PetscAssertPointer(adapt, 2);
1739: PetscUseMethod(pc, "PCMGGetAdaptInterpolation_C", (PC, PetscBool *), (pc, adapt));
1740: PetscFunctionReturn(PETSC_SUCCESS);
1741: }
1743: /*@
1744: PCMGSetAdaptCR - Monitor the coarse space quality using an auxiliary solve with compatible relaxation.
1746: Logically Collective
1748: Input Parameters:
1749: + pc - the multigrid context
1750: - cr - flag for compatible relaxation
1752: Options Database Key:
1753: . -pc_mg_adapt_cr (true|false) - Turn on compatible relaxation
1755: Level: intermediate
1757: .seealso: [](ch_ksp), `PCMG`, `PCMGGetAdaptCR()`, `PCMGSetAdaptInterpolation()`, `PCMGSetGalerkin()`, `PCMGGetAdaptCoarseSpaceType()`, `PCMGSetAdaptCoarseSpaceType()`
1758: @*/
1759: PetscErrorCode PCMGSetAdaptCR(PC pc, PetscBool cr)
1760: {
1761: PetscFunctionBegin;
1763: PetscTryMethod(pc, "PCMGSetAdaptCR_C", (PC, PetscBool), (pc, cr));
1764: PetscFunctionReturn(PETSC_SUCCESS);
1765: }
1767: /*@
1768: PCMGGetAdaptCR - Get the flag to monitor coarse space quality using an auxiliary solve with compatible relaxation.
1770: Not Collective
1772: Input Parameter:
1773: . pc - the multigrid context
1775: Output Parameter:
1776: . cr - flag for compatible relaxaion
1778: Level: intermediate
1780: .seealso: [](ch_ksp), `PCMGSetAdaptCR()`, `PCMGGetAdaptInterpolation()`, `PCMGSetGalerkin()`, `PCMGGetAdaptCoarseSpaceType()`, `PCMGSetAdaptCoarseSpaceType()`
1781: @*/
1782: PetscErrorCode PCMGGetAdaptCR(PC pc, PetscBool *cr)
1783: {
1784: PetscFunctionBegin;
1786: PetscAssertPointer(cr, 2);
1787: PetscUseMethod(pc, "PCMGGetAdaptCR_C", (PC, PetscBool *), (pc, cr));
1788: PetscFunctionReturn(PETSC_SUCCESS);
1789: }
1791: /*@
1792: PCMGSetNumberSmooth - Sets the number of pre and post-smoothing steps to use
1793: on all levels. Use `PCMGDistinctSmoothUp()` to create separate up and down smoothers if you want different numbers of
1794: pre- and post-smoothing steps.
1796: Logically Collective
1798: Input Parameters:
1799: + pc - the multigrid context
1800: - n - the number of smoothing steps
1802: Options Database Key:
1803: . -mg_levels_ksp_max_it n - Sets number of pre and post-smoothing steps
1805: Level: advanced
1807: Note:
1808: This does not set a value on the coarsest grid, since we assume that there is no separate smooth up on the coarsest grid.
1810: .seealso: [](ch_ksp), `PCMG`, `PCMGSetDistinctSmoothUp()`
1811: @*/
1812: PetscErrorCode PCMGSetNumberSmooth(PC pc, PetscInt n)
1813: {
1814: PC_MG *mg = (PC_MG *)pc->data;
1815: PC_MG_Levels **mglevels = mg->levels;
1816: PetscInt levels;
1818: PetscFunctionBegin;
1821: PetscCheck(mglevels, PetscObjectComm((PetscObject)pc), PETSC_ERR_ORDER, "Must set MG levels with PCMGSetLevels() before calling");
1822: levels = mglevels[0]->levels;
1824: for (PetscInt i = 1; i < levels; i++) {
1825: PetscCall(KSPSetTolerances(mglevels[i]->smoothu, PETSC_CURRENT, PETSC_CURRENT, PETSC_CURRENT, n));
1826: PetscCall(KSPSetTolerances(mglevels[i]->smoothd, PETSC_CURRENT, PETSC_CURRENT, PETSC_CURRENT, n));
1827: mg->default_smoothu = n;
1828: mg->default_smoothd = n;
1829: }
1830: PetscFunctionReturn(PETSC_SUCCESS);
1831: }
1833: /*@
1834: PCMGSetDistinctSmoothUp - sets the up (post) smoother to be a separate `KSP` from the down (pre) smoother on all levels
1835: and adds the suffix _up to the options name
1837: Logically Collective
1839: Input Parameter:
1840: . pc - the preconditioner context
1842: Options Database Key:
1843: . -pc_mg_distinct_smoothup (true|false) - use distinct smoothing objects
1845: Level: advanced
1847: Note:
1848: This does not set a value on the coarsest grid, since we assume that there is no separate smooth up on the coarsest grid.
1850: .seealso: [](ch_ksp), `PCMG`, `PCMGSetNumberSmooth()`
1851: @*/
1852: PetscErrorCode PCMGSetDistinctSmoothUp(PC pc)
1853: {
1854: PC_MG *mg = (PC_MG *)pc->data;
1855: PC_MG_Levels **mglevels = mg->levels;
1856: PetscInt levels;
1857: KSP subksp;
1859: PetscFunctionBegin;
1861: PetscCheck(mglevels, PetscObjectComm((PetscObject)pc), PETSC_ERR_ORDER, "Must set MG levels with PCMGSetLevels() before calling");
1862: levels = mglevels[0]->levels;
1864: for (PetscInt i = 1; i < levels; i++) {
1865: const char *prefix = NULL;
1866: /* make sure smoother up and down are different */
1867: PetscCall(PCMGGetSmootherUp(pc, i, &subksp));
1868: PetscCall(KSPGetOptionsPrefix(mglevels[i]->smoothd, &prefix));
1869: PetscCall(KSPSetOptionsPrefix(subksp, prefix));
1870: PetscCall(KSPAppendOptionsPrefix(subksp, "up_"));
1871: }
1872: PetscFunctionReturn(PETSC_SUCCESS);
1873: }
1875: /* No new matrices are created, and the coarse operator matrices are the references to the original ones */
1876: static PetscErrorCode PCGetInterpolations_MG(PC pc, PetscInt *num_levels, Mat *interpolations[])
1877: {
1878: PC_MG *mg = (PC_MG *)pc->data;
1879: PC_MG_Levels **mglevels = mg->levels;
1880: Mat *mat;
1882: PetscFunctionBegin;
1883: PetscCheck(mglevels, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Must set MG levels before calling");
1884: PetscCall(PetscMalloc1(mg->nlevels, &mat));
1885: for (PetscInt l = 1; l < mg->nlevels; l++) {
1886: mat[l - 1] = mglevels[l]->interpolate;
1887: PetscCall(PetscObjectReference((PetscObject)mat[l - 1]));
1888: }
1889: *num_levels = mg->nlevels;
1890: *interpolations = mat;
1891: PetscFunctionReturn(PETSC_SUCCESS);
1892: }
1894: /* No new matrices are created, and the coarse operator matrices are the references to the original ones */
1895: static PetscErrorCode PCGetCoarseOperators_MG(PC pc, PetscInt *num_levels, Mat *coarseOperators[])
1896: {
1897: PC_MG *mg = (PC_MG *)pc->data;
1898: PC_MG_Levels **mglevels = mg->levels;
1899: Mat *mat;
1901: PetscFunctionBegin;
1902: PetscCheck(mglevels, PetscObjectComm((PetscObject)pc), PETSC_ERR_ARG_WRONGSTATE, "Must set MG levels before calling");
1903: PetscCall(PetscMalloc1(mg->nlevels, &mat));
1904: for (PetscInt l = 0; l < mg->nlevels - 1; l++) {
1905: PetscCall(KSPGetOperators(mglevels[l]->smoothd, NULL, &mat[l]));
1906: PetscCall(PetscObjectReference((PetscObject)mat[l]));
1907: }
1908: *num_levels = mg->nlevels;
1909: *coarseOperators = mat;
1910: PetscFunctionReturn(PETSC_SUCCESS);
1911: }
1913: /*@
1914: PCMGRegisterCoarseSpaceConstructor - Adds a method to the `PCMG` package for coarse space construction.
1916: Not Collective, No Fortran Support
1918: Input Parameters:
1919: + name - name of the constructor
1920: - function - constructor routine, see `PCMGCoarseSpaceConstructorFn`
1922: Level: advanced
1924: Developer Notes:
1925: This is used by `PCMG_ADAPT_EIGENVECTOR` and `PCMG_ADAPT_GENERALIZED_EIGENVECTOR` to utilize the BAMG package
1927: `PCMGSetAdaptCoarseSpaceType()` and `PCMGCoarseSpaceType` should be refactored to use the standard PETSc registration of
1928: types as strings instead of incorrectly using an enum for `PCMGCoarseSpaceType` and thus
1929: requiring this ad hoc nonstandard registration process for BAMG.
1931: .seealso: [](ch_ksp), `PCMGCoarseSpaceConstructorFn`, `PCMG`, `PCMGGetCoarseSpaceConstructor()`, `PCRegister()`,
1932: `PCMGSetAdaptCoarseSpaceType()`, `PCMG_ADAPT_EIGENVECTOR`, `PCMG_ADAPT_GENERALIZED_EIGENVECTOR`
1933: @*/
1934: PetscErrorCode PCMGRegisterCoarseSpaceConstructor(const char name[], PCMGCoarseSpaceConstructorFn *function)
1935: {
1936: PetscFunctionBegin;
1937: PetscCall(PCInitializePackage());
1938: PetscCall(PetscFunctionListAdd(&PCMGCoarseList, name, function));
1939: PetscFunctionReturn(PETSC_SUCCESS);
1940: }
1942: /*@
1943: PCMGGetCoarseSpaceConstructor - Returns the given coarse space construction method.
1945: Not Collective, No Fortran Support
1947: Input Parameter:
1948: . name - name of the constructor
1950: Output Parameter:
1951: . function - constructor routine
1953: Level: advanced
1955: .seealso: [](ch_ksp), `PCMGCoarseSpaceConstructorFn`, `PCMG`, `PCMGRegisterCoarseSpaceConstructor()`, `PCRegister()`
1956: @*/
1957: PetscErrorCode PCMGGetCoarseSpaceConstructor(const char name[], PCMGCoarseSpaceConstructorFn **function)
1958: {
1959: PetscFunctionBegin;
1960: PetscCall(PetscFunctionListFind(PCMGCoarseList, name, function));
1961: PetscFunctionReturn(PETSC_SUCCESS);
1962: }
1964: /*MC
1965: PCMG - Use multigrid preconditioning. This preconditioner requires you provide additional information about the restriction/interpolation
1966: operators using `PCMGSetInterpolation()` and/or `PCMGSetRestriction()`(and possibly the coarser grid matrices) or a `DM` that can provide such information.
1968: Options Database Keys:
1969: + -pc_mg_levels nlevels - number of levels including finest
1970: . -pc_mg_cycle_type (v|w) - provide the cycle desired
1971: . -pc_mg_type (additive|multiplicative|full|kaskade) - multiplicative is the default
1972: . -pc_mg_log - log information about time spent on each level of the solver
1973: . -pc_mg_distinct_smoothup - configure up (after interpolation) and down (before restriction) smoothers separately (with different options prefixes)
1974: . -pc_mg_galerkin (both|pmat|mat|none) - use the Galerkin process to compute coarser operators, i.e., $A_{coarse} = R A_{fine} R^T$
1975: . -pc_mg_multiplicative_cycles ncycles - number of cycles to use as the preconditioner (defaults to 1)
1976: . -pc_mg_dump_matlab - dumps the matrices for each level and the restriction/interpolation matrices
1977: to a `PETSCVIEWERSOCKET` for reading from MATLAB.
1978: - -pc_mg_dump_binary - dumps the matrices for each level and the restriction/interpolation matrices
1979: to the binary output file called binaryoutput
1981: Level: intermediate
1983: Notes:
1984: `PCMG` provides a general framework for implementing multigrid methods. Use `PCGAMG` for PETSc's algebraic multigrid preconditioner, `PCHYPRE` for hypre's
1985: BoomerAMG algebraic multigrid, and `PCML` for Trilinos's ML preconditioner. `PCAMGX` provides access to NVIDIA's AmgX algebraic multigrid.
1987: If you use `KSPSetDM()` (or `SNESSetDM()` or `TSSetDM()`) with an appropriate `DM`, such as `DMDA`, then `PCMG` will use the geometric information
1988: from the `DM` to generate appropriate restriction and interpolation information and construct a geometric multigrid.
1990: If you do not provide an appropriate `DM` and do not provide restriction or interpolation operators with `PCMGSetInterpolation()` and/or `PCMGSetRestriction()`,
1991: then `PCMG` will run multigrid with only a single level (so not really multigrid).
1993: The Krylov solver (if any) and preconditioner (smoother) and their parameters are controlled from the options database with the standard
1994: options database keywords prefixed with `-mg_levels_` to affect all the levels but the coarsest, which is controlled with `-mg_coarse_`,
1995: and the finest where `-mg_fine_` can override `-mg_levels_`. One can set different preconditioners etc on specific levels with the prefix
1996: `-mg_levels_n_` where `n` is the level number (zero being the coarse level. For example
1997: .vb
1998: -mg_levels_ksp_type gmres -mg_levels_pc_type bjacobi -mg_coarse_pc_type svd -mg_levels_2_pc_type sor
1999: .ve
2000: These options also work for controlling the smoothers etc inside `PCGAMG`
2002: If one uses a Krylov method such `KSPGMRES` or `KSPCG` as the smoother than one must use `KSPFGMRES`, `KSPGCR`, or `KSPRICHARDSON` as the outer Krylov method
2004: When run with a single level the smoother options are used on that level NOT the coarse grid solver options
2006: When run with `KSPRICHARDSON` the convergence test changes slightly if monitor is turned on. The iteration count may change slightly. This
2007: is because without monitoring the residual norm is computed WITHIN each multigrid cycle on the finest level after the pre-smoothing
2008: (because the residual has just been computed for the multigrid algorithm and is hence available for free) while with monitoring the
2009: residual is computed at the end of each cycle.
2011: .seealso: [](sec_mg), `PCCreate()`, `PCSetType()`, `PCType`, `PC`, `PCMGType`, `PCEXOTIC`, `PCGAMG`, `PCML`, `PCHYPRE`, `PCAIR`,
2012: `PCMGSetLevels()`, `PCMGGetLevels()`, `PCMGSetType()`, `PCMGSetCycleType()`,
2013: `PCMGSetDistinctSmoothUp()`, `PCMGGetCoarseSolve()`, `PCMGSetResidual()`, `PCMGSetInterpolation()`,
2014: `PCMGSetRestriction()`, `PCMGGetSmoother()`, `PCMGGetSmootherUp()`, `PCMGGetSmootherDown()`,
2015: `PCMGSetCycleTypeOnLevel()`, `PCMGSetRhs()`, `PCMGSetX()`, `PCMGSetR()`,
2016: `PCMGSetAdaptCR()`, `PCMGGetAdaptInterpolation()`, `PCMGSetGalerkin()`, `PCMGGetAdaptCoarseSpaceType()`, `PCMGSetAdaptCoarseSpaceType()`
2017: M*/
2019: PETSC_EXTERN PetscErrorCode PCCreate_MG(PC pc)
2020: {
2021: PC_MG *mg;
2023: PetscFunctionBegin;
2024: PetscCall(PetscNew(&mg));
2025: pc->data = mg;
2026: mg->nlevels = -1;
2027: mg->am = PC_MG_MULTIPLICATIVE;
2028: mg->galerkin = PC_MG_GALERKIN_NONE;
2029: mg->adaptInterpolation = PETSC_FALSE;
2030: mg->Nc = -1;
2031: mg->eigenvalue = -1;
2033: PetscObjectParameterSetDefault(pc, useAmat, PETSC_TRUE);
2035: pc->ops->apply = PCApply_MG;
2036: pc->ops->applytranspose = PCApplyTranspose_MG;
2037: pc->ops->matapply = PCMatApply_MG;
2038: pc->ops->matapplytranspose = PCMatApplyTranspose_MG;
2039: pc->ops->setup = PCSetUp_MG;
2040: pc->ops->reset = PCReset_MG;
2041: pc->ops->destroy = PCDestroy_MG;
2042: pc->ops->setfromoptions = PCSetFromOptions_MG;
2043: pc->ops->view = PCView_MG;
2045: PetscCall(PetscObjectComposedDataRegister(&mg->eigenvalue));
2046: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGSetGalerkin_C", PCMGSetGalerkin_MG));
2047: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCSetReusePreconditioner_C", PCSetReusePreconditioner_MG));
2048: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGGetLevels_C", PCMGGetLevels_MG));
2049: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGSetLevels_C", PCMGSetLevels_MG));
2050: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGetInterpolations_C", PCGetInterpolations_MG));
2051: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCGetCoarseOperators_C", PCGetCoarseOperators_MG));
2052: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGSetAdaptInterpolation_C", PCMGSetAdaptInterpolation_MG));
2053: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGGetAdaptInterpolation_C", PCMGGetAdaptInterpolation_MG));
2054: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGSetAdaptCR_C", PCMGSetAdaptCR_MG));
2055: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGGetAdaptCR_C", PCMGGetAdaptCR_MG));
2056: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGSetAdaptCoarseSpaceType_C", PCMGSetAdaptCoarseSpaceType_MG));
2057: PetscCall(PetscObjectComposeFunction((PetscObject)pc, "PCMGGetAdaptCoarseSpaceType_C", PCMGGetAdaptCoarseSpaceType_MG));
2058: PetscFunctionReturn(PETSC_SUCCESS);
2059: }