Actual source code: pcmgimpl.h

  1: /*
  2:       Data structure used for Multigrid preconditioner.
  3: */
  4: #pragma once

  6: #include <petsc/private/pcimpl.h>
  7: #include <petscksp.h>
  8: #include <petscpctypes.h>
  9: #define PETSC_MG_MAXLEVELS 15
 10: /*
 11:      Each level has its own copy of this data.
 12:      Level (0) is always the coarsest level and Level (levels-1) is the finest.
 13: */
 14: typedef struct {
 15:   PetscInt cycles;      /* Type of cycle to run: 1 V 2 W */
 16:   PetscInt level;       /* level = 0 coarsest level */
 17:   PetscInt levels;      /* number of active levels used */
 18:   Vec      b;           /* Right hand side */
 19:   Vec      x;           /* Solution */
 20:   Vec      r;           /* Residual */
 21:   Mat      coarseSpace; /* A vector space which should be accurately captured by the next coarser mesh,
 22:                                                   and thus accurately interpolated. The columns of this dense matrix
 23:                                                   correspond to the same function discretized in
 24:                                                   the sequence of spaces. */

 26:   PetscErrorCode (*residual)(Mat, Vec, Vec, Vec);
 27:   PetscErrorCode (*residualtranspose)(Mat, Vec, Vec, Vec);
 28:   PetscErrorCode (*matresidual)(Mat, Mat, Mat, Mat);
 29:   PetscErrorCode (*matresidualtranspose)(Mat, Mat, Mat, Mat);

 31:   Mat           A;       /* matrix used in forming residual*/
 32:   KSP           smoothd; /* pre smoother */
 33:   KSP           smoothu; /* post smoother */
 34:   KSP           cr;      /* post compatible relaxation (cr) */
 35:   Vec           crx;     /* cr solution */
 36:   Vec           crb;     /* cr rhs */
 37:   Mat           interpolate;
 38:   Mat           restrct;          /* restrict is a reserved word in C99 and on Cray */
 39:   Mat           inject;           /* Used for moving state if provided. */
 40:   Vec           rscale;           /* scaling of restriction matrix */
 41:   PetscLogEvent eventsmoothsetup; /* if logging times for each level */
 42:   PetscLogEvent eventsmoothsolve;
 43:   PetscLogEvent eventresidual;
 44:   PetscLogEvent eventinterprestrict;
 45: } PC_MG_Levels;

 47: /*
 48:     This data structure is shared by all the levels.
 49: */
 50: typedef struct {
 51:   PCMGType         am;                     /* Multiplicative, additive or full */
 52:   PetscInt         cyclesperpcapply;       /* Number of cycles to use in each PCApply(), multiplicative only*/
 53:   PetscInt         maxlevels;              /* total number of levels allocated */
 54:   PCMGGalerkinType galerkin;               /* use Galerkin process to compute coarser matrices */
 55:   PetscBool        usedmfornumberoflevels; /* sets the number of levels by getting this information out of the DM */

 57:   PetscBool           adaptInterpolation; /* flag to adapt the interpolator based upon the coarseSpace */
 58:   PCMGCoarseSpaceType coarseSpaceType;    /* Type of coarse space: polynomials, harmonics, eigenvectors, ... */
 59:   PetscInt            Nc;                 /* The number of vectors in coarseSpace */
 60:   PetscBool           mespMonitor;        /* flag to monitor the multilevel eigensolver */

 62:   PetscBool compatibleRelaxation; /* flag to monitor the coarse space quality using an auxiliary solve with compatible relaxation */

 64:   PetscInt       nlevels;
 65:   PC_MG_Levels **levels;
 66:   PetscInt       default_smoothu;          /* number of smooths per level if not over-ridden */
 67:   PetscInt       default_smoothd;          /*  with calls to KSPSetTolerances() */
 68:   PetscReal      rtol, abstol, dtol, ttol; /* tolerances for when running with PCApplyRichardson_MG */

 70:   void         *innerctx; /* optional data for preconditioner, like PCEXOTIC that inherits off of PCMG */
 71:   PetscLogStage stageApply;
 72:   PetscErrorCode (*view)(PC, PetscViewer); /* GAMG and other objects that use PCMG can set their own viewer here */
 73:   PetscReal min_eigen_DinvA[PETSC_MG_MAXLEVELS];
 74:   PetscReal max_eigen_DinvA[PETSC_MG_MAXLEVELS];
 75: } PC_MG;

 77: // Four consecutive work matrices per level hold the RHS, solution, residual, and interpolated correction.
 78: #define PCMGMatB(pc, l) ((pc)->matwork[4 * (l)])
 79: #define PCMGMatX(pc, l) ((pc)->matwork[4 * (l) + 1])
 80: #define PCMGMatR(pc, l) ((pc)->matwork[4 * (l) + 2])
 81: #define PCMGMatW(pc, l) ((pc)->matwork[4 * (l) + 3])

 83: PETSC_INTERN PetscErrorCode PCSetUp_MG(PC);
 84: PETSC_INTERN PetscErrorCode PCReset_MG(PC);
 85: PETSC_INTERN PetscErrorCode PCDestroy_MG(PC);
 86: PETSC_INTERN PetscErrorCode PCSetFromOptions_MG(PC, PetscOptionItems PetscOptionsObject);
 87: PETSC_INTERN PetscErrorCode PCView_MG(PC, PetscViewer);
 88: PETSC_INTERN PetscErrorCode PCMGGetLevels_MG(PC, PetscInt *);
 89: PETSC_INTERN PetscErrorCode PCMGSetLevels_MG(PC, PetscInt, MPI_Comm *);
 90: PETSC_DEPRECATED_FUNCTION(3, 5, 0, "PCMGResidualDefault()", ) static inline PetscErrorCode PCMGResidual_Default(Mat A, Vec b, Vec x, Vec r)
 91: {
 92:   return PCMGResidualDefault(A, b, x, r);
 93: }

 95: PETSC_INTERN PetscErrorCode DMSetBasisFunction_Internal(PetscInt, PetscBool, PetscInt, PetscErrorCode (**)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *));
 96: PETSC_INTERN PetscErrorCode PCMGComputeCoarseSpace_Internal(PC, PetscInt, PCMGCoarseSpaceType, PetscInt, Mat, Mat *);
 97: PETSC_INTERN PetscErrorCode PCMGAdaptInterpolator_Internal(PC, PetscInt, KSP, KSP, Mat, Mat);
 98: PETSC_INTERN PetscErrorCode PCMGRecomputeLevelOperators_Internal(PC, PetscInt);
 99: PETSC_INTERN PetscErrorCode PCMGACycle_Private(PC, PC_MG_Levels **, PetscBool, PetscBool);
100: PETSC_INTERN PetscErrorCode PCMGFCycle_Private(PC, PC_MG_Levels **, PetscBool, PetscBool);
101: PETSC_INTERN PetscErrorCode PCMGKCycle_Private(PC, PC_MG_Levels **, PetscBool, PetscBool);
102: PETSC_INTERN PetscErrorCode PCMGMCycle_Private(PC, PC_MG_Levels **, PetscBool, PetscBool, PCRichardsonConvergedReason *);
103: PETSC_INTERN PetscErrorCode PCMGMatInterpolate_Private(PC, PetscInt, Mat, PetscBool);

105: PETSC_INTERN PetscErrorCode PCMGGDSWCreateCoarseSpace_Private(PC, PetscInt, DM, KSP, PetscInt, Mat, Mat *);