Actual source code: mgadapt.c

  1: #include <petsc/private/pcmgimpl.h>
  2: #include <petscdm.h>

  4: static PetscErrorCode xfunc(PetscInt dim, PetscReal time, const PetscReal coords[], PetscInt Nc, PetscScalar *u, void *ctx)
  5: {
  6:   PetscInt k = *((PetscInt *)ctx), c;

  8:   for (c = 0; c < Nc; ++c) u[c] = PetscPowRealInt(coords[0], k);
  9:   return 0;
 10: }
 11: static PetscErrorCode yfunc(PetscInt dim, PetscReal time, const PetscReal coords[], PetscInt Nc, PetscScalar *u, void *ctx)
 12: {
 13:   PetscInt k = *((PetscInt *)ctx), c;

 15:   for (c = 0; c < Nc; ++c) u[c] = PetscPowRealInt(coords[1], k);
 16:   return 0;
 17: }
 18: static PetscErrorCode zfunc(PetscInt dim, PetscReal time, const PetscReal coords[], PetscInt Nc, PetscScalar *u, void *ctx)
 19: {
 20:   PetscInt k = *((PetscInt *)ctx), c;

 22:   for (c = 0; c < Nc; ++c) u[c] = PetscPowRealInt(coords[2], k);
 23:   return 0;
 24: }
 25: static PetscErrorCode xsin(PetscInt dim, PetscReal time, const PetscReal coords[], PetscInt Nc, PetscScalar *u, void *ctx)
 26: {
 27:   PetscInt k = *((PetscInt *)ctx), c;

 29:   for (c = 0; c < Nc; ++c) u[c] = PetscSinReal(PETSC_PI * (k + 1) * coords[0]);
 30:   return 0;
 31: }
 32: static PetscErrorCode ysin(PetscInt dim, PetscReal time, const PetscReal coords[], PetscInt Nc, PetscScalar *u, void *ctx)
 33: {
 34:   PetscInt k = *((PetscInt *)ctx), c;

 36:   for (c = 0; c < Nc; ++c) u[c] = PetscSinReal(PETSC_PI * (k + 1) * coords[1]);
 37:   return 0;
 38: }
 39: static PetscErrorCode zsin(PetscInt dim, PetscReal time, const PetscReal coords[], PetscInt Nc, PetscScalar *u, void *ctx)
 40: {
 41:   PetscInt k = *((PetscInt *)ctx), c;

 43:   for (c = 0; c < Nc; ++c) u[c] = PetscSinReal(PETSC_PI * (k + 1) * coords[2]);
 44:   return 0;
 45: }

 47: PetscErrorCode DMSetBasisFunction_Internal(PetscInt Nf, PetscBool usePoly, PetscInt dir, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *))
 48: {
 49:   PetscInt f;

 52:   for (f = 0; f < Nf; ++f) {
 53:     if (usePoly) {
 54:       switch (dir) {
 55:       case 0:
 56:         funcs[f] = xfunc;
 57:         break;
 58:       case 1:
 59:         funcs[f] = yfunc;
 60:         break;
 61:       case 2:
 62:         funcs[f] = zfunc;
 63:         break;
 64:       default:
 65:         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No function for direction %" PetscInt_FMT, dir);
 66:       }
 67:     } else {
 68:       switch (dir) {
 69:       case 0:
 70:         funcs[f] = xsin;
 71:         break;
 72:       case 1:
 73:         funcs[f] = ysin;
 74:         break;
 75:       case 2:
 76:         funcs[f] = zsin;
 77:         break;
 78:       default:
 79:         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No function for direction %" PetscInt_FMT, dir);
 80:       }
 81:     }
 82:   }
 83:   return 0;
 84: }

 86: static PetscErrorCode PCMGCreateCoarseSpaceDefault_Private(PC pc, PetscInt level, PCMGCoarseSpaceType cstype, DM dm, KSP ksp, PetscInt Nc, Mat initialGuess, Mat *coarseSpace)
 87: {
 88:   PetscBool poly = cstype == PCMG_ADAPT_POLYNOMIAL ? PETSC_TRUE : PETSC_FALSE;
 89:   PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *);
 90:   void   **ctxs;
 91:   PetscInt dim, d, Nf, f, k, m, M;
 92:   Vec      tmp;

 94:   Nc = Nc < 0 ? 6 : Nc;
 95:   DMGetCoordinateDim(dm, &dim);
 96:   DMGetNumFields(dm, &Nf);
 98:   PetscMalloc2(Nf, &funcs, Nf, &ctxs);
 99:   DMGetGlobalVector(dm, &tmp);
100:   VecGetSize(tmp, &M);
101:   VecGetLocalSize(tmp, &m);
102:   MatCreateDense(PetscObjectComm((PetscObject)pc), m, PETSC_DECIDE, M, Nc, NULL, coarseSpace);
103:   DMRestoreGlobalVector(dm, &tmp);
104:   for (k = 0; k < Nc / dim; ++k) {
105:     for (f = 0; f < Nf; ++f) ctxs[f] = &k;
106:     for (d = 0; d < dim; ++d) {
107:       MatDenseGetColumnVecWrite(*coarseSpace, k * dim + d, &tmp);
108:       DMSetBasisFunction_Internal(Nf, poly, d, funcs);
109:       DMProjectFunction(dm, 0.0, funcs, ctxs, INSERT_ALL_VALUES, tmp);
110:       MatDenseRestoreColumnVecWrite(*coarseSpace, k * dim + d, &tmp);
111:     }
112:   }
113:   PetscFree2(funcs, ctxs);
114:   return 0;
115: }

117: static PetscErrorCode PCMGCreateCoarseSpace_Polynomial(PC pc, PetscInt level, DM dm, KSP ksp, PetscInt Nc, Mat initialGuess, Mat *coarseSpace)
118: {
119:   PCMGCreateCoarseSpaceDefault_Private(pc, level, PCMG_ADAPT_POLYNOMIAL, dm, ksp, Nc, initialGuess, coarseSpace);
120:   return 0;
121: }

123: PetscErrorCode PCMGCreateCoarseSpace_Harmonic(PC pc, PetscInt level, DM dm, KSP ksp, PetscInt Nc, Mat initialGuess, Mat *coarseSpace)
124: {
125:   PCMGCreateCoarseSpaceDefault_Private(pc, level, PCMG_ADAPT_HARMONIC, dm, ksp, Nc, initialGuess, coarseSpace);
126:   return 0;
127: }

129: /*
130:   PCMGComputeCoarseSpace_Internal - Compute vectors on level l that must be accurately interpolated.

132:   Input Parameters:
133: + pc     - The PCMG
134: . l      - The level
135: . Nc     - The number of vectors requested
136: - cspace - The initial guess for the space, or NULL

138:   Output Parameter:
139: . space  - The space which must be accurately interpolated.

141:   Level: developer

143:   Note: This space is normally used to adapt the interpolator. If Nc is negative, an adaptive choice can be made.

145: .seealso: `PCMGAdaptInterpolator_Private()`
146: */
147: PetscErrorCode PCMGComputeCoarseSpace_Internal(PC pc, PetscInt l, PCMGCoarseSpaceType cstype, PetscInt Nc, Mat cspace, Mat *space)
148: {
149:   PetscErrorCode (*coarseConstructor)(PC, PetscInt, DM, KSP, PetscInt, Mat, Mat *) = NULL;
150:   DM  dm;
151:   KSP smooth;

153:   *space = NULL;
154:   switch (cstype) {
155:   case PCMG_ADAPT_POLYNOMIAL:
156:     coarseConstructor = &PCMGCreateCoarseSpace_Polynomial;
157:     break;
158:   case PCMG_ADAPT_HARMONIC:
159:     coarseConstructor = &PCMGCreateCoarseSpace_Harmonic;
160:     break;
161:   case PCMG_ADAPT_EIGENVECTOR:
162:     Nc = Nc < 0 ? 6 : Nc;
163:     if (l > 0) PCMGGetCoarseSpaceConstructor("BAMG_MEV", &coarseConstructor);
164:     else PCMGGetCoarseSpaceConstructor("BAMG_EV", &coarseConstructor);
165:     break;
166:   case PCMG_ADAPT_GENERALIZED_EIGENVECTOR:
167:     Nc = Nc < 0 ? 6 : Nc;
168:     if (l > 0) PCMGGetCoarseSpaceConstructor("BAMG_MGEV", &coarseConstructor);
169:     else PCMGGetCoarseSpaceConstructor("BAMG_GEV", &coarseConstructor);
170:     break;
171:   case PCMG_ADAPT_GDSW:
172:     coarseConstructor = &PCMGGDSWCreateCoarseSpace_Private;
173:     break;
174:   case PCMG_ADAPT_NONE:
175:     break;
176:   default:
177:     SETERRQ(PetscObjectComm((PetscObject)pc), PETSC_ERR_SUP, "Cannot handle coarse space type %d", cstype);
178:   }
179:   if (coarseConstructor) {
180:     PCMGGetSmoother(pc, l, &smooth);
181:     KSPGetDM(smooth, &dm);
182:     (*coarseConstructor)(pc, l, dm, smooth, Nc, cspace, space);
183:   }
184:   return 0;
185: }

187: /*
188:   PCMGAdaptInterpolator_Internal - Adapt interpolator from level l-1 to level l

190:   Input Parameters:
191: + pc      - The PCMG
192: . l       - The level l
193: . csmooth - The (coarse) smoother for level l-1
194: . fsmooth - The (fine) smoother for level l
195: . cspace  - The (coarse) vectors in the subspace for level l-1
196: - fspace  - The (fine) vectors in the subspace for level l

198:   Level: developer

200:   Note: This routine resets the interpolation and restriction for level l.

202: .seealso: `PCMGComputeCoarseSpace_Private()`
203: */
204: PetscErrorCode PCMGAdaptInterpolator_Internal(PC pc, PetscInt l, KSP csmooth, KSP fsmooth, Mat cspace, Mat fspace)
205: {
206:   PC_MG *mg = (PC_MG *)pc->data;
207:   DM     dm, cdm;
208:   Mat    Interp, InterpAdapt;

210:   /* There is no interpolator for the coarse level */
211:   if (!l) return 0;
212:   KSPGetDM(csmooth, &cdm);
213:   KSPGetDM(fsmooth, &dm);
214:   PCMGGetInterpolation(pc, l, &Interp);
215:   if (Interp == fspace && !cspace) return 0;
216:   DMAdaptInterpolator(cdm, dm, Interp, fsmooth, fspace, cspace, &InterpAdapt, pc);
217:   if (mg->mespMonitor) DMCheckInterpolator(dm, InterpAdapt, cspace, fspace, 0.5 /* PETSC_SMALL */);
218:   PCMGSetInterpolation(pc, l, InterpAdapt);
219:   PCMGSetRestriction(pc, l, InterpAdapt); /* MATT: Remove????? */
220:   MatDestroy(&InterpAdapt);
221:   return 0;
222: }

224: /*
225:   PCMGRecomputeLevelOperators_Internal - Recomputes Galerkin coarse operator when interpolation is adapted

227:   Note: This routine recomputes the Galerkin triple product for the operator on level l.
228: */
229: PetscErrorCode PCMGRecomputeLevelOperators_Internal(PC pc, PetscInt l)
230: {
231:   Mat              fA, fB;                   /* The system and preconditioning operators on level l+1 */
232:   Mat              A, B;                     /* The system and preconditioning operators on level l */
233:   Mat              Interp, Restrc;           /* The interpolation operator from level l to l+1, and restriction operator from level l+1 to l */
234:   KSP              smooth, fsmooth;          /* The smoothers on levels l and l+1 */
235:   PCMGGalerkinType galerkin;                 /* The Galerkin projection flag */
236:   MatReuse         reuse = MAT_REUSE_MATRIX; /* The matrices are always assumed to be present already */
237:   PetscBool        doA   = PETSC_FALSE;      /* Updates the system operator */
238:   PetscBool        doB   = PETSC_FALSE;      /* Updates the preconditioning operator (A == B, then update B) */
239:   PetscInt         n;                        /* The number of multigrid levels */

241:   PCMGGetGalerkin(pc, &galerkin);
242:   if (galerkin >= PC_MG_GALERKIN_NONE) return 0;
243:   PCMGGetLevels(pc, &n);
244:   /* Do not recompute operator for the finest grid */
245:   if (l == n - 1) return 0;
246:   PCMGGetSmoother(pc, l, &smooth);
247:   KSPGetOperators(smooth, &A, &B);
248:   PCMGGetSmoother(pc, l + 1, &fsmooth);
249:   KSPGetOperators(fsmooth, &fA, &fB);
250:   PCMGGetInterpolation(pc, l + 1, &Interp);
251:   PCMGGetRestriction(pc, l + 1, &Restrc);
252:   if ((galerkin == PC_MG_GALERKIN_PMAT) || (galerkin == PC_MG_GALERKIN_BOTH)) doB = PETSC_TRUE;
253:   if ((galerkin == PC_MG_GALERKIN_MAT) || ((galerkin == PC_MG_GALERKIN_BOTH) && (fA != fB))) doA = PETSC_TRUE;
254:   if (doA) MatGalerkin(Restrc, fA, Interp, reuse, 1.0, &A);
255:   if (doB) MatGalerkin(Restrc, fB, Interp, reuse, 1.0, &B);
256:   return 0;
257: }