Actual source code: dmpleximpl.h

  1: #pragma once

  3: #include <petscmat.h>
  4: #include <petscdmplex.h>
  5: #include <petscdmplextransformtypes.h>
  6: #include <petscsf.h>
  7: #include <petsc/private/dmimpl.h>

  9: #if PetscDefined(HAVE_EXODUSII)
 10:   #include <exodusII.h>
 11: #endif

 13: PETSC_INTERN PetscBool  Plexcite;
 14: PETSC_INTERN const char PlexCitation[];

 16: PETSC_EXTERN PetscLogEvent DMPLEX_Interpolate;
 17: PETSC_EXTERN PetscLogEvent DMPLEX_Partition;
 18: PETSC_EXTERN PetscLogEvent DMPLEX_PartSelf;
 19: PETSC_EXTERN PetscLogEvent DMPLEX_PartLabelInvert;
 20: PETSC_EXTERN PetscLogEvent DMPLEX_PartLabelCreateSF;
 21: PETSC_EXTERN PetscLogEvent DMPLEX_PartStratSF;
 22: PETSC_EXTERN PetscLogEvent DMPLEX_CreatePointSF;
 23: PETSC_EXTERN PetscLogEvent DMPLEX_Distribute;
 24: PETSC_EXTERN PetscLogEvent DMPLEX_DistributeMultistage;
 25: PETSC_EXTERN PetscLogEvent DMPLEX_DistributeCones;
 26: PETSC_EXTERN PetscLogEvent DMPLEX_DistributeLabels;
 27: PETSC_EXTERN PetscLogEvent DMPLEX_DistributeSF;
 28: PETSC_EXTERN PetscLogEvent DMPLEX_DistributeOverlap;
 29: PETSC_EXTERN PetscLogEvent DMPLEX_DistributeField;
 30: PETSC_EXTERN PetscLogEvent DMPLEX_DistributeData;
 31: PETSC_EXTERN PetscLogEvent DMPLEX_Migrate;
 32: PETSC_EXTERN PetscLogEvent DMPLEX_InterpolateSF;
 33: PETSC_EXTERN PetscLogEvent DMPLEX_GlobalToNaturalBegin;
 34: PETSC_EXTERN PetscLogEvent DMPLEX_GlobalToNaturalEnd;
 35: PETSC_EXTERN PetscLogEvent DMPLEX_NaturalToGlobalBegin;
 36: PETSC_EXTERN PetscLogEvent DMPLEX_NaturalToGlobalEnd;
 37: PETSC_EXTERN PetscLogEvent DMPLEX_Stratify;
 38: PETSC_EXTERN PetscLogEvent DMPLEX_Symmetrize;
 39: PETSC_EXTERN PetscLogEvent DMPLEX_Preallocate;
 40: PETSC_EXTERN PetscLogEvent DMPLEX_ResidualFEM;
 41: PETSC_EXTERN PetscLogEvent DMPLEX_JacobianFEM;
 42: PETSC_EXTERN PetscLogEvent DMPLEX_InterpolatorFEM;
 43: PETSC_EXTERN PetscLogEvent DMPLEX_InjectorFEM;
 44: PETSC_EXTERN PetscLogEvent DMPLEX_IntegralFEM;
 45: PETSC_EXTERN PetscLogEvent DMPLEX_CreateGmsh;
 46: PETSC_EXTERN PetscLogEvent DMPLEX_CreateBoxSFC;
 47: PETSC_EXTERN PetscLogEvent DMPLEX_RebalanceSharedPoints;
 48: PETSC_EXTERN PetscLogEvent DMPLEX_CreateFromFile;
 49: PETSC_EXTERN PetscLogEvent DMPLEX_CreateFromOptions;
 50: PETSC_EXTERN PetscLogEvent DMPLEX_BuildFromCellList;
 51: PETSC_EXTERN PetscLogEvent DMPLEX_BuildCoordinatesFromCellList;
 52: PETSC_EXTERN PetscLogEvent DMPLEX_LocatePoints;
 53: PETSC_EXTERN PetscLogEvent DMPLEX_TopologyView;
 54: PETSC_EXTERN PetscLogEvent DMPLEX_DistributionView;
 55: PETSC_EXTERN PetscLogEvent DMPLEX_LabelsView;
 56: PETSC_EXTERN PetscLogEvent DMPLEX_CoordinatesView;
 57: PETSC_EXTERN PetscLogEvent DMPLEX_SectionView;
 58: PETSC_EXTERN PetscLogEvent DMPLEX_GlobalVectorView;
 59: PETSC_EXTERN PetscLogEvent DMPLEX_LocalVectorView;
 60: PETSC_EXTERN PetscLogEvent DMPLEX_TopologyLoad;
 61: PETSC_EXTERN PetscLogEvent DMPLEX_DistributionLoad;
 62: PETSC_EXTERN PetscLogEvent DMPLEX_LabelsLoad;
 63: PETSC_EXTERN PetscLogEvent DMPLEX_CoordinatesLoad;
 64: PETSC_EXTERN PetscLogEvent DMPLEX_SectionLoad;
 65: PETSC_EXTERN PetscLogEvent DMPLEX_GlobalVectorLoad;
 66: PETSC_EXTERN PetscLogEvent DMPLEX_LocalVectorLoad;
 67: PETSC_EXTERN PetscLogEvent DMPLEX_MetricEnforceSPD;
 68: PETSC_EXTERN PetscLogEvent DMPLEX_MetricNormalize;
 69: PETSC_EXTERN PetscLogEvent DMPLEX_MetricAverage;
 70: PETSC_EXTERN PetscLogEvent DMPLEX_MetricIntersection;
 71: PETSC_EXTERN PetscLogEvent DMPLEX_Generate;
 72: PETSC_EXTERN PetscLogEvent DMPLEX_GetLocalOffsets;

 74: PETSC_EXTERN PetscLogEvent DMPLEX_RebalBuildGraph;
 75: PETSC_EXTERN PetscLogEvent DMPLEX_RebalRewriteSF;
 76: PETSC_EXTERN PetscLogEvent DMPLEX_RebalGatherGraph;
 77: PETSC_EXTERN PetscLogEvent DMPLEX_RebalPartition;
 78: PETSC_EXTERN PetscLogEvent DMPLEX_RebalScatterPart;
 79: PETSC_EXTERN PetscLogEvent DMPLEX_Uninterpolate;

 81: struct _n_PetscGridHash {
 82:   PetscInt     dim;
 83:   PetscReal    lower[3];    /* The lower-left corner */
 84:   PetscReal    upper[3];    /* The upper-right corner */
 85:   PetscReal    extent[3];   /* The box size */
 86:   PetscReal    h[3];        /* The subbox size */
 87:   PetscInt     n[3];        /* The number of subboxes */
 88:   PetscSection cellSection; /* Offsets for cells in each subbox*/
 89:   IS           cells;       /* List of cells in each subbox */
 90:   DMLabel      cellsSparse; /* Sparse storage for cell map */
 91: };

 93: typedef struct {
 94:   PetscBool isotropic;               /* Is the metric isotropic? */
 95:   PetscBool uniform;                 /* Is the metric uniform? */
 96:   PetscBool restrictAnisotropyFirst; /* Should anisotropy or normalization come first? */
 97:   PetscBool noInsert;                /* Should node insertion/deletion be turned off? */
 98:   PetscBool noSwap;                  /* Should facet swapping be turned off? */
 99:   PetscBool noMove;                  /* Should node movement be turned off? */
100:   PetscBool noSurf;                  /* Should surface modification be turned off? */
101:   PetscReal h_min, h_max;            /* Minimum/maximum tolerated metric magnitudes */
102:   PetscReal a_max;                   /* Maximum tolerated anisotropy */
103:   PetscReal targetComplexity;        /* Target metric complexity */
104:   PetscReal p;                       /* Degree for L-p normalization methods */
105:   PetscReal gradationFactor;         /* Maximum tolerated length ratio for adjacent edges */
106:   PetscReal hausdorffNumber;         /* Max. distance between piecewise linear representation of boundary and reconstructed ideal boundary */
107:   PetscInt  numIter;                 /* Number of ParMmg mesh adaptation iterations */
108:   PetscInt  verbosity;               /* Level of verbosity for remesher (-1 = no output, 10 = maximum) */
109: } DMPlexMetricCtx;

111: /* Point Numbering in Plex:

113:    Points are numbered contiguously by stratum. Strate are organized as follows:

115:    First Stratum:  Cells [height 0]
116:    Second Stratum: Vertices [depth 0]
117:    Third Stratum:  Faces [height 1]
118:    Fourth Stratum: Edges [depth 1]

120:    We do this so that the numbering of a cell-vertex mesh does not change after interpolation. Within a given stratum,
121:    we allow additional segregation of by cell type.
122: */
123: typedef struct {
124:   PetscInt refct;

126:   PetscSection coneSection;      /* Layout of cones (inedges for DAG) */
127:   PetscInt    *cones;            /* Cone for each point */
128:   PetscInt    *coneOrientations; /* Orientation of each cone point, means cone traversal should start on point 'o', and if negative start on -(o+1) and go in reverse */
129:   PetscSection supportSection;   /* Layout of cones (inedges for DAG) */
130:   PetscInt    *supports;         /* Cone for each point */

132:   struct {                  // DMPolytopeType is an enum (usually size 4), but this needs frequent access
133:     uint8_t value_as_uint8; // in a struct to guard for stronger typing
134:   } *cellTypes;

136:   /* Transformation */
137:   DMPlexTransform tr;                                               /* Type of transform used to define an ephemeral mesh */
138:   char           *transformType;                                    /* Type of transform for uniform cell refinement */
139:   PetscBool       refinementUniform;                                /* Flag for uniform cell refinement */
140:   PetscReal       refinementLimit;                                  /* Maximum volume for refined cell */
141:   PetscErrorCode (*refinementFunc)(const PetscReal[], PetscReal *); /* Function giving the maximum volume for refined cell */

143:   /* Interpolation */
144:   DMPlexInterpolatedFlag interpolated;
145:   DMPlexInterpolatedFlag interpolatedCollective;
146:   PetscBool              interpolatePreferTensor; // When different orderings exist, prefer the tensor order

148:   /* Ordering */
149:   DMReorderDefaultFlag reorderDefault; /* Reorder the DM by default */

151:   /* Distribution */
152:   PetscBool distDefault;      /* Distribute the DM by default */
153:   PetscInt  overlap;          /* Overlap of the partitions as passed to DMPlexDistribute() or DMPlexDistributeOverlap() */
154:   PetscInt  numOvLabels;      /* The number of labels used for candidate overlap points */
155:   DMLabel   ovLabels[16];     /* Labels used for candidate overlap points */
156:   PetscInt  ovValues[16];     /* Label values used for candidate overlap points */
157:   PetscInt  numOvExLabels;    /* The number of labels used for exclusion */
158:   DMLabel   ovExLabels[16];   /* Labels used to exclude points from the overlap */
159:   PetscInt  ovExValues[16];   /* Label values used to exclude points from the overlap */
160:   char     *distributionName; /* Name of the specific parallel distribution of the DM */
161:   MPI_Comm  nonempty_comm;    /* Communicator used for visualization when some processes do not have cells */

163:   /* Hierarchy */
164:   PetscBool regularRefinement; /* This flag signals that we are a regular refinement of coarseMesh */

166:   /* Generation */
167:   char            *tetgenOpts;            // COmmand line options for Tetgen
168:   PetscReal        tetgenRadiusEdgeBound; // Maximum tetgen radius-edge ratio
169:   PetscReal        tetgenDihedralBound;   // Minimum tetgen dihedral angle
170:   char            *triangleOpts;          // Command line options for Triangle
171:   PetscReal        triangleAngBound;      // Minimum triangle angle
172:   PetscPartitioner partitioner;           // Partitioner object
173:   PetscBool        partitionBalance;      // Evenly divide partition overlap when distributing
174:   PetscBool        remeshBd;

176:   /* Submesh */
177:   DMLabel          subpointMap;   /* Label each original mesh point in the submesh with its depth, subpoint are the implicit numbering */
178:   IS               subpointIS;    /* IS holding point number in the enclosing mesh of every point in the submesh chart */
179:   PetscObjectState subpointState; /* The state of subpointMap when the subpointIS was last created */

181:   /* Labels and numbering */
182:   PetscObjectState depthState;    /* State of depth label, so that we can determine if a user changes it */
183:   PetscObjectState celltypeState; /* State of celltype label, so that we can determine if a user changes it */
184:   IS               globalVertexNumbers;
185:   IS               globalCellNumbers;

187:   /* Constraints */
188:   PetscSection anchorSection;          /* maps constrained points to anchor points */
189:   IS           anchorIS;               /* anchors indexed by the above section */
190:   PetscErrorCode (*createanchors)(DM); /* automatically compute anchors (probably from tree constraints) */
191:   PetscErrorCode (*computeanchormatrix)(DM, PetscSection, PetscSection, Mat);

193:   /* Tree: automatically construct constraints for hierarchically non-conforming meshes */
194:   PetscSection parentSection; /* dof == 1 if point has parent */
195:   PetscInt    *parents;       /* point to parent */
196:   PetscInt    *childIDs;      /* point to child ID */
197:   PetscSection childSection;  /* inverse of parent section */
198:   PetscInt    *children;      /* point to children */
199:   DM           referenceTree; /* reference tree to which child ID's refer */
200:   PetscErrorCode (*getchildsymmetry)(DM, PetscInt, PetscInt, PetscInt, PetscInt, PetscInt, PetscInt *, PetscInt *);

202:   /* MATIS support */
203:   PetscSection subdomainSection;

205:   /* Adjacency */
206:   PetscBool useAnchors;                                                          /* Replace constrained points with their anchors in adjacency lists */
207:   PetscErrorCode (*useradjacency)(DM, PetscInt, PetscInt *, PetscInt[], void *); /* User callback for adjacency */
208:   void *useradjacencyctx;                                                        /* User context for callback */

210:   // Periodicity
211:   struct {
212:     // Specified by the user
213:     PetscInt num_face_sfs;          // number of face_sfs
214:     PetscSF *face_sfs;              // root(donor faces) <-- leaf(local faces)
215:     PetscScalar (*transform)[4][4]; // geometric transform
216:     // Created eagerly (depends on points)
217:     PetscSF composed_sf; // root(non-periodic global points) <-- leaf(local points)
218:     IS     *periodic_points;
219:   } periodic;

221:   /* Projection */
222:   PetscInt maxProjectionHeight; /* maximum height of cells used in DMPlexProject functions */
223:   PetscInt activePoint;         /* current active point in iteration */

225:   /* Output */
226:   PetscInt  vtkCellHeight;          /* The height of cells for output, default is 0 */
227:   PetscReal scale[NUM_PETSC_UNITS]; /* The scale for each SI unit */

229:   /* Geometry */
230:   PetscReal     minradius;       /* Minimum distance from cell centroid to face */
231:   PetscBool     useHashLocation; /* Use grid hashing for point location */
232:   PetscGridHash lbox;            /* Local box for searching */
233:   PetscPointFn *coordFunc;       /* Function used to remap newly introduced vertices */

235:   /* Neighbors */
236:   PetscMPIInt *neighbors;

238:   /* Metric */
239:   DMPlexMetricCtx *metricCtx;

241:   /* FEM */
242:   PetscBool useCeed;      /* This should convert to a registration system when there are more FEM backends */
243:   PetscBool useMatClPerm; /* Use the closure permutation when assembling matrices */

245:   /* CAD */
246:   PetscBool ignoreModel; /* If TRUE, Plex refinement will skip Snap-To-Geometry feature ignoring attached CAD geometry information */

248:   // Transforms
249:   PetscBool       saveTransform; // Flag to save transform, if this mesh was produced by a transform
250:   DMPlexTransform transform;     // Transform producing this mesh (note that this will hold on to the original mesh too)

252:   /* Debugging */
253:   PetscBool printSetValues;
254:   PetscInt  printAdj;
255:   PetscInt  printFEM;
256:   PetscInt  printFVM;
257:   PetscInt  printL2;
258:   PetscInt  printOrient;
259:   PetscInt  printLocate;
260:   PetscInt  printProject;
261:   PetscInt  printCohesive;
262:   PetscReal printTol;
263: } DM_Plex;

265: PETSC_INTERN PetscErrorCode DMPlexCopy_Internal(DM, PetscBool, PetscBool, DM);
266: PETSC_INTERN PetscErrorCode DMPlexReplace_Internal(DM, DM *);
267: PETSC_INTERN PetscErrorCode DMPlexCopyEGADSInfo_Internal(DM, DM);

269: PETSC_INTERN PetscErrorCode DMPlexVTKWriteAll_VTU(DM, PetscViewer);
270: PETSC_INTERN PetscErrorCode VecView_Plex_Local(Vec, PetscViewer);
271: PETSC_INTERN PetscErrorCode VecView_Plex_Native(Vec, PetscViewer);
272: PETSC_INTERN PetscErrorCode VecView_Plex(Vec, PetscViewer);
273: PETSC_INTERN PetscErrorCode VecLoad_Plex_Local(Vec, PetscViewer);
274: PETSC_INTERN PetscErrorCode VecLoad_Plex_Native(Vec, PetscViewer);
275: PETSC_INTERN PetscErrorCode VecLoad_Plex(Vec, PetscViewer);
276: PETSC_INTERN PetscErrorCode DMPlexGetFieldType_Internal(DM, PetscSection, PetscInt, PetscInt *, PetscInt *, PetscViewerVTKFieldType *);
277: PETSC_INTERN PetscErrorCode DMPlexGetFieldTypes_Internal(DM, PetscSection, PetscInt, PetscInt *, PetscInt **, PetscInt **, PetscViewerVTKFieldType **);
278: PETSC_INTERN PetscErrorCode DMPlexRestoreFieldTypes_Internal(DM, PetscSection, PetscInt, PetscInt *, PetscInt **, PetscInt **, PetscViewerVTKFieldType **);
279: PETSC_INTERN PetscErrorCode DMPlexView_GLVis(DM, PetscViewer);
280: PETSC_INTERN PetscErrorCode DMSetUpGLVisViewer_Plex(PetscObject, PetscViewer);
281: #if PetscDefined(HAVE_HDF5)
282: PETSC_INTERN PetscErrorCode DMPlexTopologyView_HDF5_Internal(DM, IS, PetscViewer);
283: PETSC_INTERN PetscErrorCode DMPlexCoordinatesView_HDF5_Internal(DM, PetscViewer);
284: PETSC_INTERN PetscErrorCode DMPlexLabelsView_HDF5_Internal(DM, IS, PetscViewer);
285: PETSC_INTERN PetscErrorCode DMPlexSectionView_HDF5_Internal(DM, PetscViewer, DM);
286: PETSC_INTERN PetscErrorCode DMPlexGlobalVectorView_HDF5_Internal(DM, PetscViewer, DM, Vec);
287: PETSC_INTERN PetscErrorCode DMPlexLocalVectorView_HDF5_Internal(DM, PetscViewer, DM, Vec);
288: PETSC_INTERN PetscErrorCode DMPlexTopologyLoad_HDF5_Internal(DM, PetscViewer, PetscSF *);
289: PETSC_INTERN PetscErrorCode DMPlexCoordinatesLoad_HDF5_Internal(DM, PetscViewer, PetscSF);
290: PETSC_INTERN PetscErrorCode DMPlexLabelsLoad_HDF5_Internal(DM, PetscViewer, PetscSF);
291: PETSC_INTERN PetscErrorCode DMPlexSectionLoad_HDF5_Internal(DM, PetscViewer, DM, PetscSF, PetscSF *, PetscSF *);
292: PETSC_INTERN PetscErrorCode DMPlexVecLoad_HDF5_Internal(DM, PetscViewer, DM, PetscSF, Vec);
293: PETSC_INTERN PetscErrorCode DMPlexView_HDF5_Internal(DM, PetscViewer);
294: PETSC_INTERN PetscErrorCode DMPlexLoad_HDF5_Internal(DM, PetscViewer);
295: PETSC_INTERN PetscErrorCode DMPlexLoad_HDF5_Xdmf_Internal(DM, PetscViewer);
296: PETSC_INTERN PetscErrorCode VecView_Plex_HDF5_Internal(Vec, PetscViewer);
297: PETSC_INTERN PetscErrorCode VecView_Plex_HDF5_Native_Internal(Vec, PetscViewer);
298: PETSC_INTERN PetscErrorCode VecView_Plex_Local_HDF5_Internal(Vec, PetscViewer);
299: PETSC_INTERN PetscErrorCode VecLoad_Plex_HDF5_Internal(Vec, PetscViewer);
300: PETSC_INTERN PetscErrorCode VecLoad_Plex_HDF5_Native_Internal(Vec, PetscViewer);
301: #endif
302: PETSC_EXTERN PetscErrorCode VecView_Plex_Local_CGNS(Vec, PetscViewer);
303: #if PetscDefined(HAVE_CGNS)
304: PETSC_EXTERN PetscErrorCode VecLoad_Plex_CGNS_Internal(Vec, PetscViewer);
305: #endif

307: PETSC_INTERN PetscErrorCode DMPlexClosurePoints_Private(DM, PetscInt, const PetscInt[], IS *);
308: PETSC_INTERN PetscErrorCode DMSetFromOptions_NonRefinement_Plex(DM, PetscOptionItems);
309: PETSC_INTERN PetscErrorCode DMSetFromOptions_Overlap_Plex(DM, PetscOptionItems, PetscInt *);
310: PETSC_INTERN PetscErrorCode DMCoarsen_Plex(DM, MPI_Comm, DM *);
311: PETSC_INTERN PetscErrorCode DMCoarsenHierarchy_Plex(DM, PetscInt, DM[]);
312: PETSC_INTERN PetscErrorCode DMRefine_Plex(DM, MPI_Comm, DM *);
313: PETSC_INTERN PetscErrorCode DMRefineHierarchy_Plex(DM, PetscInt, DM[]);
314: PETSC_INTERN PetscErrorCode DMAdaptLabel_Plex(DM, Vec, DMLabel, DMLabel, DM *);
315: PETSC_INTERN PetscErrorCode DMExtrude_Plex(DM, PetscInt, DM *);
316: PETSC_INTERN PetscErrorCode DMPlexInsertBoundaryValues_Plex(DM, PetscBool, Vec, PetscReal, Vec, Vec, Vec);
317: PETSC_INTERN PetscErrorCode DMPlexInsertTimeDerivativeBoundaryValues_Plex(DM, PetscBool, Vec, PetscReal, Vec, Vec, Vec);
318: PETSC_INTERN PetscErrorCode DMPlexInsertBounds_Plex(DM, PetscBool, PetscReal, Vec);
319: PETSC_INTERN PetscErrorCode DMProjectFunctionLocal_Plex(DM, PetscReal, PetscErrorCode (**)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **, InsertMode, Vec);
320: PETSC_INTERN PetscErrorCode DMProjectFunctionLabelLocal_Plex(DM, PetscReal, DMLabel, PetscInt, const PetscInt[], PetscInt, const PetscInt[], PetscErrorCode (**)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **, InsertMode, Vec);
321: PETSC_INTERN PetscErrorCode DMProjectFieldLocal_Plex(DM, PetscReal, Vec, void (**)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]), InsertMode, Vec);
322: PETSC_INTERN PetscErrorCode DMProjectFieldLabelLocal_Plex(DM, PetscReal, DMLabel, PetscInt, const PetscInt[], PetscInt, const PetscInt[], Vec, void (**)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]), InsertMode, Vec);
323: PETSC_INTERN PetscErrorCode DMProjectBdFieldLabelLocal_Plex(DM, PetscReal, DMLabel, PetscInt, const PetscInt[], PetscInt, const PetscInt[], Vec, void (**)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]), InsertMode, Vec);
324: PETSC_INTERN PetscErrorCode DMComputeL2Diff_Plex(DM, PetscReal, PetscErrorCode (**)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **, Vec, PetscReal *);
325: PETSC_INTERN PetscErrorCode DMComputeL2GradientDiff_Plex(DM, PetscReal, PetscErrorCode (**)(PetscInt, PetscReal, const PetscReal[], const PetscReal[], PetscInt, PetscScalar *, void *), void **, Vec, const PetscReal[], PetscReal *);
326: PETSC_INTERN PetscErrorCode DMComputeL2FieldDiff_Plex(DM, PetscReal, PetscErrorCode (**)(PetscInt, PetscReal, const PetscReal[], PetscInt, PetscScalar *, void *), void **, Vec, PetscReal *);
327: PETSC_INTERN PetscErrorCode DMLocatePoints_Plex(DM, Vec, DMPointLocationType, PetscSF);

329: #if PetscDefined(HAVE_EXODUSII)
330: PETSC_INTERN PetscErrorCode DMView_PlexExodusII(DM, PetscViewer);
331: PETSC_INTERN PetscErrorCode VecView_PlexExodusII_Internal(Vec, PetscViewer);
332: PETSC_INTERN PetscErrorCode VecLoad_PlexExodusII_Internal(Vec, PetscViewer);
333: #endif
334: PETSC_INTERN PetscErrorCode DMView_PlexCGNS(DM, PetscViewer);
335: PETSC_INTERN PetscErrorCode DMPlexCreateCGNSFromFile_Internal(MPI_Comm, const char *, PetscBool, DM *);
336: PETSC_INTERN PetscErrorCode DMPlexCreateCGNS_Internal_Serial(MPI_Comm, PetscInt, PetscBool, DM *);
337: PETSC_INTERN PetscErrorCode DMPlexCreateCGNS_Internal_Parallel(MPI_Comm, PetscInt, PetscBool, DM *);
338: PETSC_INTERN PetscErrorCode DMPlexVTKGetCellType_Internal(DM, PetscInt, PetscInt, PetscInt *);
339: PETSC_INTERN PetscErrorCode DMPlexGetAdjacency_Internal(DM, PetscInt, PetscBool, PetscBool, PetscBool, PetscInt *, PetscInt *[]);
340: PETSC_INTERN PetscErrorCode DMPlexGetMaxAdjacencySize_Internal(DM, PetscBool, PetscInt *);
341: PETSC_INTERN PetscErrorCode DMPlexGetRawFaces_Internal(DM, DMPolytopeType, const PetscInt[], PetscInt *, const DMPolytopeType *[], const PetscInt *[], const PetscInt *[]);
342: PETSC_INTERN PetscErrorCode DMPlexRestoreRawFaces_Internal(DM, DMPolytopeType, const PetscInt[], PetscInt *, const DMPolytopeType *[], const PetscInt *[], const PetscInt *[]);
343: PETSC_INTERN PetscErrorCode DMPlexComputeCellType_Internal(DM, PetscInt, PetscInt, DMPolytopeType *);
344: PETSC_INTERN PetscErrorCode DMPlexVecSetFieldClosure_Internal(DM, PetscSection, Vec, PetscBool[], PetscInt, PetscInt, const PetscInt[], DMLabel, PetscInt, const PetscScalar[], InsertMode);
345: PETSC_EXTERN PetscErrorCode DMPlexCreateReferenceTree_SetTree(DM, PetscSection, PetscInt[], PetscInt[]);
346: PETSC_EXTERN PetscErrorCode DMPlexCreateReferenceTree_Union(DM, DM, const char *, DM *);
347: PETSC_EXTERN PetscErrorCode DMPlexComputeInterpolatorTree(DM, DM, PetscSF, PetscInt *, Mat);
348: PETSC_EXTERN PetscErrorCode DMPlexComputeInjectorTree(DM, DM, PetscSF, PetscInt *, Mat);
349: PETSC_INTERN PetscErrorCode DMPlexAnchorsModifyMat(DM, PetscSection, PetscInt, PetscInt, const PetscInt[], const PetscInt ***, const PetscScalar[], PetscInt *, PetscInt *, PetscInt *[], PetscScalar *[], PetscInt[], PetscBool);
350: PETSC_INTERN PetscErrorCode DMPlexAnchorsModifyMat_Internal(DM, PetscSection, PetscInt, PetscInt, const PetscInt[], const PetscInt ***, PetscInt, PetscInt, const PetscScalar[], PetscInt *, PetscInt *, PetscInt *[], PetscScalar *[], PetscInt[], PetscBool, PetscBool);
351: PETSC_INTERN PetscErrorCode DMPlexAnchorsGetSubMatModification(DM, PetscSection, PetscInt, PetscInt, const PetscInt[], const PetscInt ***, PetscInt *, PetscInt *, PetscInt *[], PetscInt[], PetscScalar *[]);
352: PETSC_INTERN PetscErrorCode DMPlexLocatePoint_Internal(DM, PetscInt, const PetscScalar[], PetscInt, PetscInt *);
353: /* this is PETSC_EXTERN just because of src/dm/impls/plex/tests/ex18.c */
354: PETSC_EXTERN PetscErrorCode DMPlexOrientInterface_Internal(DM);
355: PETSC_INTERN PetscErrorCode DMPlexOrientCells_Internal(DM, IS, IS);

357: /* Applications may use this function */
358: PETSC_EXTERN PetscErrorCode DMPlexCreateNumbering_Plex(DM, PetscInt, PetscInt, PetscInt, PetscInt *, PetscSF, IS *);

360: PETSC_INTERN PetscErrorCode DMPlexInterpolateInPlace_Internal(DM);
361: PETSC_INTERN PetscErrorCode DMPlexCreateBoxMesh_Tensor_SFC_Internal(DM, PetscInt, const PetscInt[], const PetscReal[], const PetscReal[], const DMBoundaryType[], PetscBool);
362: PETSC_INTERN PetscErrorCode DMPlexMigrateIsoperiodicFaceSF_Internal(DM, DM, PetscSF);
363: PETSC_INTERN PetscErrorCode DMPlexCurveTypeResolve_Internal(MPI_Comm, DMPlexCurveType, PetscBool *);
364: PETSC_INTERN PetscErrorCode DMPlexGetCellOrderingByCurve_Internal(DM, DMPlexCurveType, PetscInt, PetscInt, PetscInt[]);
365: PETSC_INTERN PetscErrorCode DMPlexCreateVertexNumbering_Internal(DM, PetscBool, IS *);
366: PETSC_INTERN PetscErrorCode DMPlexRefine_Internal(DM, Vec, DMLabel, DMLabel, DM *);
367: PETSC_INTERN PetscErrorCode DMPlexCoarsen_Internal(DM, Vec, DMLabel, DMLabel, DM *);
368: PETSC_INTERN PetscErrorCode DMCreateMatrix_Plex(DM, Mat *);

370: PETSC_INTERN PetscErrorCode DMPlexGetOverlap_Plex(DM, PetscInt *);
371: PETSC_INTERN PetscErrorCode DMPlexSetOverlap_Plex(DM, DM, PetscInt);
372: PETSC_INTERN PetscErrorCode DMPlexDistributeGetDefault_Plex(DM, PetscBool *);
373: PETSC_INTERN PetscErrorCode DMPlexDistributeSetDefault_Plex(DM, PetscBool);
374: PETSC_INTERN PetscErrorCode DMPlexReorderGetDefault_Plex(DM, DMReorderDefaultFlag *);
375: PETSC_INTERN PetscErrorCode DMPlexReorderSetDefault_Plex(DM, DMReorderDefaultFlag);
376: PETSC_INTERN PetscErrorCode DMPlexGetUseCeed_Plex(DM, PetscBool *);
377: PETSC_INTERN PetscErrorCode DMPlexSetUseCeed_Plex(DM, PetscBool);
378: PETSC_INTERN PetscErrorCode DMReorderSectionGetDefault_Plex(DM, DMReorderDefaultFlag *);
379: PETSC_INTERN PetscErrorCode DMReorderSectionSetDefault_Plex(DM, DMReorderDefaultFlag);
380: PETSC_INTERN PetscErrorCode DMReorderSectionGetType_Plex(DM, MatOrderingType *);
381: PETSC_INTERN PetscErrorCode DMReorderSectionSetType_Plex(DM, MatOrderingType);

383: #if 1
384: static inline PetscInt DihedralInvert(PetscInt N, PetscInt a)
385: {
386:   return (a <= 0) ? a : (N - a);
387: }

389: static inline PetscInt DihedralCompose(PetscInt N, PetscInt a, PetscInt b)
390: {
391:   if (!N) return 0;
392:   return (a >= 0) ? ((b >= 0) ? ((a + b) % N) : -(((a - b - 1) % N) + 1)) : ((b >= 0) ? -(((N - b - a - 1) % N) + 1) : ((N + b - a) % N));
393: }

395: static inline PetscInt DihedralSwap(PetscInt N, PetscInt a, PetscInt b)
396: {
397:   return DihedralCompose(N, DihedralInvert(N, a), b);
398: }
399: #else
400: /* TODO
401:    This is a reimplementation of the tensor dihedral symmetries using the new orientations.
402:    These should be turned on when we convert to new-style orientations in p4est.
403: */
404: /* invert dihedral symmetry: return a^-1,
405:  * using the representation described in
406:  * DMPlexGetConeOrientation() */
407: static inline PetscInt DihedralInvert(PetscInt N, PetscInt a)
408: {
409:   switch (N) {
410:   case 0:
411:     return 0;
412:   case 2:
413:     return DMPolytopeTypeComposeOrientationInv(DM_POLYTOPE_SEGMENT, 0, a);
414:   case 4:
415:     return DMPolytopeTypeComposeOrientationInv(DM_POLYTOPE_QUADRILATERAL, 0, a);
416:   case 8:
417:     return DMPolytopeTypeComposeOrientationInv(DM_POLYTOPE_HEXAHEDRON, 0, a);
418:   default:
419:     SETERRABORT(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid celltype for DihedralInvert()");
420:   }
421:   return 0;
422: }

424: /* compose dihedral symmetry: return b * a,
425:  * using the representation described in
426:  * DMPlexGetConeOrientation() */
427: static inline PetscInt DihedralCompose(PetscInt N, PetscInt a, PetscInt b)
428: {
429:   switch (N) {
430:   case 0:
431:     return 0;
432:   case 2:
433:     return DMPolytopeTypeComposeOrientationInv(DM_POLYTOPE_SEGMENT, b, a);
434:   case 4:
435:     return DMPolytopeTypeComposeOrientationInv(DM_POLYTOPE_QUADRILATERAL, b, a);
436:   case 8:
437:     return DMPolytopeTypeComposeOrientationInv(DM_POLYTOPE_HEXAHEDRON, b, a);
438:   default:
439:     SETERRABORT(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid celltype for DihedralCompose()");
440:   }
441:   return 0;
442: }

444: /* swap dihedral symmetries: return b * a^-1,
445:  * using the representation described in
446:  * DMPlexGetConeOrientation() */
447: static inline PetscInt DihedralSwap(PetscInt N, PetscInt a, PetscInt b)
448: {
449:   switch (N) {
450:   case 0:
451:     return 0;
452:   case 2:
453:     return DMPolytopeTypeComposeOrientationInv(DM_POLYTOPE_SEGMENT, b, a);
454:   case 4:
455:     return DMPolytopeTypeComposeOrientationInv(DM_POLYTOPE_QUADRILATERAL, b, a);
456:   case 8:
457:     return DMPolytopeTypeComposeOrientationInv(DM_POLYTOPE_HEXAHEDRON, b, a);
458:   default:
459:     SETERRABORT(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid celltype for DihedralCompose()");
460:   }
461:   return 0;
462: }
463: #endif
464: PETSC_INTERN PetscInt       DMPolytopeConvertNewOrientation_Internal(DMPolytopeType, PetscInt);
465: PETSC_INTERN PetscInt       DMPolytopeConvertOldOrientation_Internal(DMPolytopeType, PetscInt);
466: PETSC_INTERN PetscErrorCode DMPlexConvertOldOrientations_Internal(DM);

468: PETSC_SINGLE_LIBRARY_VISIBILITY_INTERNAL PetscErrorCode DMPlexComputeIntegral_Internal(DM, Vec, PetscInt, PetscInt, PetscScalar *, void *);
469: PETSC_INTERN PetscErrorCode                             DMPlexReconstructGradients_Internal(DM, PetscFV, PetscInt, PetscInt, Vec, Vec, Vec, Vec);

471: /* Matvec with A in row-major storage, x and y can be aliased */
472: static inline void DMPlex_Mult2D_Internal(const PetscScalar A[], PetscInt ldx, const PetscScalar x[], PetscScalar y[])
473: {
474:   const PetscScalar z[2] = {x[0 * ldx], x[1 * ldx]};
475:   y[0 * ldx]             = A[0] * z[0] + A[1] * z[1];
476:   y[1 * ldx]             = A[2] * z[0] + A[3] * z[1];
477:   (void)PetscLogFlops(6.0);
478: }
479: static inline void DMPlex_Mult3D_Internal(const PetscScalar A[], PetscInt ldx, const PetscScalar x[], PetscScalar y[])
480: {
481:   const PetscScalar z[3] = {x[0 * ldx], x[1 * ldx], x[2 * ldx]};
482:   y[0 * ldx]             = A[0] * z[0] + A[1] * z[1] + A[2] * z[2];
483:   y[1 * ldx]             = A[3] * z[0] + A[4] * z[1] + A[5] * z[2];
484:   y[2 * ldx]             = A[6] * z[0] + A[7] * z[1] + A[8] * z[2];
485:   (void)PetscLogFlops(15.0);
486: }
487: static inline void DMPlex_MultTranspose2D_Internal(const PetscScalar A[], PetscInt ldx, const PetscScalar x[], PetscScalar y[])
488: {
489:   const PetscScalar z[2] = {x[0 * ldx], x[1 * ldx]};
490:   y[0 * ldx]             = A[0] * z[0] + A[2] * z[1];
491:   y[1 * ldx]             = A[1] * z[0] + A[3] * z[1];
492:   (void)PetscLogFlops(6.0);
493: }
494: static inline void DMPlex_MultTranspose3D_Internal(const PetscScalar A[], PetscInt ldx, const PetscScalar x[], PetscScalar y[])
495: {
496:   const PetscScalar z[3] = {x[0 * ldx], x[1 * ldx], x[2 * ldx]};
497:   y[0 * ldx]             = PetscConj(A[0]) * z[0] + PetscConj(A[3]) * z[1] + PetscConj(A[6]) * z[2];
498:   y[1 * ldx]             = PetscConj(A[1]) * z[0] + PetscConj(A[4]) * z[1] + PetscConj(A[7]) * z[2];
499:   y[2 * ldx]             = PetscConj(A[2]) * z[0] + PetscConj(A[5]) * z[1] + PetscConj(A[8]) * z[2];
500:   (void)PetscLogFlops(15.0);
501: }
502: static inline void DMPlex_Mult2DReal_Internal(const PetscReal A[], PetscInt ldx, const PetscScalar x[], PetscScalar y[])
503: {
504:   const PetscScalar z[2] = {x[0 * ldx], x[1 * ldx]};
505:   y[0 * ldx]             = A[0] * z[0] + A[1] * z[1];
506:   y[1 * ldx]             = A[2] * z[0] + A[3] * z[1];
507:   (void)PetscLogFlops(6.0);
508: }
509: static inline void DMPlex_Mult3DReal_Internal(const PetscReal A[], PetscInt ldx, const PetscScalar x[], PetscScalar y[])
510: {
511:   const PetscScalar z[3] = {x[0 * ldx], x[1 * ldx], x[2 * ldx]};
512:   y[0 * ldx]             = A[0] * z[0] + A[1] * z[1] + A[2] * z[2];
513:   y[1 * ldx]             = A[3] * z[0] + A[4] * z[1] + A[5] * z[2];
514:   y[2 * ldx]             = A[6] * z[0] + A[7] * z[1] + A[8] * z[2];
515:   (void)PetscLogFlops(15.0);
516: }
517: static inline void DMPlex_MultAdd2DReal_Internal(const PetscReal A[], PetscInt ldx, const PetscScalar x[], PetscScalar y[])
518: {
519:   const PetscScalar z[2] = {x[0 * ldx], x[1 * ldx]};
520:   y[0 * ldx] += A[0] * z[0] + A[1] * z[1];
521:   y[1 * ldx] += A[2] * z[0] + A[3] * z[1];
522:   (void)PetscLogFlops(6.0);
523: }
524: static inline void DMPlex_MultAdd3DReal_Internal(const PetscReal A[], PetscInt ldx, const PetscScalar x[], PetscScalar y[])
525: {
526:   const PetscScalar z[3] = {x[0 * ldx], x[1 * ldx], x[2 * ldx]};
527:   y[0 * ldx] += A[0] * z[0] + A[1] * z[1] + A[2] * z[2];
528:   y[1 * ldx] += A[3] * z[0] + A[4] * z[1] + A[5] * z[2];
529:   y[2 * ldx] += A[6] * z[0] + A[7] * z[1] + A[8] * z[2];
530:   (void)PetscLogFlops(15.0);
531: }
532: /*
533:   A: packed, row-major m x n array
534:   x: length m array
535:   y: length n arra
536:   ldx: the stride in x and y

538:   A[i,j] = A[i * n + j]
539:   A^T[j,i] = A[i,j]
540: */
541: static inline void DMPlex_MultTransposeReal_Internal(const PetscReal A[], PetscInt m, PetscInt n, PetscInt ldx, const PetscScalar x[], PetscScalar y[])
542: {
543:   PetscScalar z[3];
544:   PetscInt    i, j;
545:   for (i = 0; i < m; ++i) z[i] = x[i * ldx];
546:   for (j = 0; j < n; ++j) {
547:     const PetscInt l = j * ldx;
548:     y[l]             = 0;
549:     for (i = 0; i < m; ++i) y[l] += A[i * n + j] * z[i];
550:   }
551:   (void)PetscLogFlops(2 * m * n);
552: }
553: static inline void DMPlex_MultTranspose2DReal_Internal(const PetscReal A[], PetscInt ldx, const PetscScalar x[], PetscScalar y[])
554: {
555:   const PetscScalar z[2] = {x[0 * ldx], x[1 * ldx]};
556:   y[0 * ldx]             = A[0] * z[0] + A[2] * z[1];
557:   y[1 * ldx]             = A[1] * z[0] + A[3] * z[1];
558:   (void)PetscLogFlops(6.0);
559: }
560: static inline void DMPlex_MultTranspose3DReal_Internal(const PetscReal A[], PetscInt ldx, const PetscScalar x[], PetscScalar y[])
561: {
562:   const PetscScalar z[3] = {x[0 * ldx], x[1 * ldx], x[2 * ldx]};
563:   y[0 * ldx]             = A[0] * z[0] + A[3] * z[1] + A[6] * z[2];
564:   y[1 * ldx]             = A[1] * z[0] + A[4] * z[1] + A[7] * z[2];
565:   y[2 * ldx]             = A[2] * z[0] + A[5] * z[1] + A[8] * z[2];
566:   (void)PetscLogFlops(15.0);
567: }

569: static inline void DMPlex_MatMult2D_Internal(const PetscScalar A[], PetscInt n, PetscInt ldb, const PetscScalar B[], PetscScalar C[])
570: {
571: #define PLEX_DIM__ 2
572:   PetscScalar z[PLEX_DIM__];
573:   for (PetscInt j = 0; j < n; ++j) {
574:     for (int d = 0; d < PLEX_DIM__; ++d) z[d] = B[d * ldb + j];
575:     DMPlex_Mult2D_Internal(A, 1, z, z);
576:     for (int d = 0; d < PLEX_DIM__; ++d) C[d * ldb + j] = z[d];
577:   }
578:   (void)PetscLogFlops(8.0 * n);
579: #undef PLEX_DIM__
580: }
581: static inline void DMPlex_MatMult3D_Internal(const PetscScalar A[], PetscInt n, PetscInt ldb, const PetscScalar B[], PetscScalar C[])
582: {
583: #define PLEX_DIM__ 3
584:   PetscScalar z[PLEX_DIM__];
585:   for (PetscInt j = 0; j < n; ++j) {
586:     for (int d = 0; d < PLEX_DIM__; ++d) z[d] = B[d * ldb + j];
587:     DMPlex_Mult3D_Internal(A, 1, z, z);
588:     for (int d = 0; d < PLEX_DIM__; ++d) C[d * ldb + j] = z[d];
589:   }
590:   (void)PetscLogFlops(8.0 * n);
591: #undef PLEX_DIM__
592: }
593: static inline void DMPlex_MatMultTranspose2D_Internal(const PetscScalar A[], PetscInt n, PetscInt ldb, const PetscScalar B[], PetscScalar C[])
594: {
595: #define PLEX_DIM__ 2
596:   PetscScalar z[PLEX_DIM__];
597:   for (PetscInt j = 0; j < n; ++j) {
598:     for (int d = 0; d < PLEX_DIM__; ++d) z[d] = B[d * ldb + j];
599:     DMPlex_MultTranspose2D_Internal(A, 1, z, z);
600:     for (int d = 0; d < PLEX_DIM__; ++d) C[d * ldb + j] = z[d];
601:   }
602:   (void)PetscLogFlops(8.0 * n);
603: #undef PLEX_DIM__
604: }
605: static inline void DMPlex_MatMultTranspose3D_Internal(const PetscScalar A[], PetscInt n, PetscInt ldb, const PetscScalar B[], PetscScalar C[])
606: {
607: #define PLEX_DIM__ 3
608:   PetscScalar z[PLEX_DIM__];
609:   for (PetscInt j = 0; j < n; ++j) {
610:     for (int d = 0; d < PLEX_DIM__; ++d) z[d] = B[d * ldb + j];
611:     DMPlex_MultTranspose3D_Internal(A, 1, z, z);
612:     for (int d = 0; d < PLEX_DIM__; ++d) C[d * ldb + j] = z[d];
613:   }
614:   (void)PetscLogFlops(8.0 * n);
615: #undef PLEX_DIM__
616: }

618: static inline void DMPlex_MatMultLeft2D_Internal(const PetscScalar A[], PetscInt m, PetscInt ldb, const PetscScalar B[], PetscScalar C[])
619: {
620:   for (PetscInt j = 0; j < m; ++j) DMPlex_MultTranspose2D_Internal(A, 1, &B[j * ldb], &C[j * ldb]);
621:   (void)PetscLogFlops(8.0 * m);
622: }
623: static inline void DMPlex_MatMultLeft3D_Internal(const PetscScalar A[], PetscInt m, PetscInt ldb, const PetscScalar B[], PetscScalar C[])
624: {
625:   for (PetscInt j = 0; j < m; ++j) DMPlex_MultTranspose3D_Internal(A, 1, &B[j * ldb], &C[j * ldb]);
626:   (void)PetscLogFlops(8.0 * m);
627: }
628: static inline void DMPlex_MatMultTransposeLeft2D_Internal(const PetscScalar A[], PetscInt m, PetscInt ldb, const PetscScalar B[], PetscScalar C[])
629: {
630:   for (PetscInt j = 0; j < m; ++j) DMPlex_Mult2D_Internal(A, 1, &B[j * ldb], &C[j * ldb]);
631:   (void)PetscLogFlops(8.0 * m);
632: }
633: static inline void DMPlex_MatMultTransposeLeft3D_Internal(const PetscScalar A[], PetscInt m, PetscInt ldb, const PetscScalar B[], PetscScalar C[])
634: {
635:   for (PetscInt j = 0; j < m; ++j) DMPlex_Mult3D_Internal(A, 1, &B[j * ldb], &C[j * ldb]);
636:   (void)PetscLogFlops(8.0 * m);
637: }

639: static inline void DMPlex_PTAP2DReal_Internal(const PetscReal P[], const PetscScalar A[], PetscScalar C[])
640: {
641:   PetscScalar out[4];
642:   PetscInt    i, j, k, l;
643:   for (i = 0; i < 2; ++i) {
644:     for (j = 0; j < 2; ++j) {
645:       out[i * 2 + j] = 0.;
646:       for (k = 0; k < 2; ++k) {
647:         for (l = 0; l < 2; ++l) out[i * 2 + j] += P[k * 2 + i] * A[k * 2 + l] * P[l * 2 + j];
648:       }
649:     }
650:   }
651:   for (i = 0; i < 2 * 2; ++i) C[i] = out[i];
652:   (void)PetscLogFlops(48.0);
653: }
654: static inline void DMPlex_PTAP3DReal_Internal(const PetscReal P[], const PetscScalar A[], PetscScalar C[])
655: {
656:   PetscScalar out[9];
657:   PetscInt    i, j, k, l;
658:   for (i = 0; i < 3; ++i) {
659:     for (j = 0; j < 3; ++j) {
660:       out[i * 3 + j] = 0.;
661:       for (k = 0; k < 3; ++k) {
662:         for (l = 0; l < 3; ++l) out[i * 3 + j] += P[k * 3 + i] * A[k * 3 + l] * P[l * 3 + j];
663:       }
664:     }
665:   }
666:   for (i = 0; i < 2 * 2; ++i) C[i] = out[i];
667:   (void)PetscLogFlops(243.0);
668: }
669: /* TODO Fix for aliasing of A and C */
670: static inline void DMPlex_PTAPReal_Internal(const PetscReal P[], PetscInt m, PetscInt n, const PetscScalar A[], PetscScalar C[])
671: {
672:   PetscInt i, j, k, l;
673:   for (i = 0; i < n; ++i) {
674:     for (j = 0; j < n; ++j) {
675:       C[i * n + j] = 0.;
676:       for (k = 0; k < m; ++k) {
677:         for (l = 0; l < m; ++l) C[i * n + j] += P[k * n + i] * A[k * m + l] * P[l * n + j];
678:       }
679:     }
680:   }
681:   (void)PetscLogFlops(243.0);
682: }

684: static inline void DMPlex_Transpose2D_Internal(PetscScalar A[])
685: {
686:   PetscScalar tmp;
687:   tmp  = A[1];
688:   A[1] = A[2];
689:   A[2] = tmp;
690: }
691: static inline void DMPlex_Transpose3D_Internal(PetscScalar A[])
692: {
693:   PetscScalar tmp;
694:   tmp  = A[1];
695:   A[1] = A[3];
696:   A[3] = tmp;
697:   tmp  = A[2];
698:   A[2] = A[6];
699:   A[6] = tmp;
700:   tmp  = A[5];
701:   A[5] = A[7];
702:   A[7] = tmp;
703: }

705: static inline void DMPlex_Invert2D_Internal(PetscReal invJ[], PetscReal J[], PetscReal detJ)
706: {
707:   // Allow zero volume cells
708:   const PetscReal invDet = detJ == 0 ? 1.0 : (PetscReal)1.0 / detJ;

710:   invJ[0] = invDet * J[3];
711:   invJ[1] = -invDet * J[1];
712:   invJ[2] = -invDet * J[2];
713:   invJ[3] = invDet * J[0];
714:   (void)PetscLogFlops(5.0);
715: }

717: static inline void DMPlex_Invert3D_Internal(PetscReal invJ[], PetscReal J[], PetscReal detJ)
718: {
719:   // Allow zero volume cells
720:   const PetscReal invDet = detJ == 0 ? 1.0 : (PetscReal)1.0 / detJ;

722:   invJ[0 * 3 + 0] = invDet * (J[1 * 3 + 1] * J[2 * 3 + 2] - J[1 * 3 + 2] * J[2 * 3 + 1]);
723:   invJ[0 * 3 + 1] = invDet * (J[0 * 3 + 2] * J[2 * 3 + 1] - J[0 * 3 + 1] * J[2 * 3 + 2]);
724:   invJ[0 * 3 + 2] = invDet * (J[0 * 3 + 1] * J[1 * 3 + 2] - J[0 * 3 + 2] * J[1 * 3 + 1]);
725:   invJ[1 * 3 + 0] = invDet * (J[1 * 3 + 2] * J[2 * 3 + 0] - J[1 * 3 + 0] * J[2 * 3 + 2]);
726:   invJ[1 * 3 + 1] = invDet * (J[0 * 3 + 0] * J[2 * 3 + 2] - J[0 * 3 + 2] * J[2 * 3 + 0]);
727:   invJ[1 * 3 + 2] = invDet * (J[0 * 3 + 2] * J[1 * 3 + 0] - J[0 * 3 + 0] * J[1 * 3 + 2]);
728:   invJ[2 * 3 + 0] = invDet * (J[1 * 3 + 0] * J[2 * 3 + 1] - J[1 * 3 + 1] * J[2 * 3 + 0]);
729:   invJ[2 * 3 + 1] = invDet * (J[0 * 3 + 1] * J[2 * 3 + 0] - J[0 * 3 + 0] * J[2 * 3 + 1]);
730:   invJ[2 * 3 + 2] = invDet * (J[0 * 3 + 0] * J[1 * 3 + 1] - J[0 * 3 + 1] * J[1 * 3 + 0]);
731:   (void)PetscLogFlops(37.0);
732: }

734: static inline void DMPlex_Det2D_Internal(PetscReal *detJ, const PetscReal J[])
735: {
736:   *detJ = J[0] * J[3] - J[1] * J[2];
737:   (void)PetscLogFlops(3.0);
738: }

740: static inline void DMPlex_Det3D_Internal(PetscReal *detJ, const PetscReal J[])
741: {
742:   *detJ = (J[0 * 3 + 0] * (J[1 * 3 + 1] * J[2 * 3 + 2] - J[1 * 3 + 2] * J[2 * 3 + 1]) + J[0 * 3 + 1] * (J[1 * 3 + 2] * J[2 * 3 + 0] - J[1 * 3 + 0] * J[2 * 3 + 2]) + J[0 * 3 + 2] * (J[1 * 3 + 0] * J[2 * 3 + 1] - J[1 * 3 + 1] * J[2 * 3 + 0]));
743:   (void)PetscLogFlops(12.0);
744: }

746: static inline void DMPlex_Det2D_Scalar_Internal(PetscReal *detJ, const PetscScalar J[])
747: {
748:   *detJ = PetscRealPart(J[0]) * PetscRealPart(J[3]) - PetscRealPart(J[1]) * PetscRealPart(J[2]);
749:   (void)PetscLogFlops(3.0);
750: }

752: static inline void DMPlex_Det3D_Scalar_Internal(PetscReal *detJ, const PetscScalar J[])
753: {
754:   *detJ = (PetscRealPart(J[0 * 3 + 0]) * (PetscRealPart(J[1 * 3 + 1]) * PetscRealPart(J[2 * 3 + 2]) - PetscRealPart(J[1 * 3 + 2]) * PetscRealPart(J[2 * 3 + 1])) + PetscRealPart(J[0 * 3 + 1]) * (PetscRealPart(J[1 * 3 + 2]) * PetscRealPart(J[2 * 3 + 0]) - PetscRealPart(J[1 * 3 + 0]) * PetscRealPart(J[2 * 3 + 2])) + PetscRealPart(J[0 * 3 + 2]) * (PetscRealPart(J[1 * 3 + 0]) * PetscRealPart(J[2 * 3 + 1]) - PetscRealPart(J[1 * 3 + 1]) * PetscRealPart(J[2 * 3 + 0])));
755:   (void)PetscLogFlops(12.0);
756: }

758: static inline void DMPlex_WaxpyD_Internal(PetscInt dim, PetscReal a, const PetscReal *x, const PetscReal *y, PetscReal *w)
759: {
760:   PetscInt d;
761:   for (d = 0; d < dim; ++d) w[d] = a * x[d] + y[d];
762: }

764: static inline PetscReal DMPlex_DotD_Internal(PetscInt dim, const PetscScalar *x, const PetscReal *y)
765: {
766:   PetscReal sum = 0.0;
767:   PetscInt  d;
768:   for (d = 0; d < dim; ++d) sum += PetscRealPart(x[d]) * y[d];
769:   return sum;
770: }

772: static inline PetscReal DMPlex_DotRealD_Internal(PetscInt dim, const PetscReal *x, const PetscReal *y)
773: {
774:   PetscReal sum = 0.0;
775:   PetscInt  d;
776:   for (d = 0; d < dim; ++d) sum += x[d] * y[d];
777:   return sum;
778: }

780: static inline PetscReal DMPlex_NormD_Internal(PetscInt dim, const PetscReal *x)
781: {
782:   PetscReal sum = 0.0;
783:   PetscInt  d;
784:   for (d = 0; d < dim; ++d) sum += x[d] * x[d];
785:   return PetscSqrtReal(sum);
786: }

788: static inline PetscReal DMPlex_DistD_Internal(PetscInt dim, const PetscScalar *x, const PetscScalar *y)
789: {
790:   PetscReal sum = 0.0;
791:   PetscInt  d;
792:   for (d = 0; d < dim; ++d) sum += PetscRealPart(PetscConj(x[d] - y[d]) * (x[d] - y[d]));
793:   return PetscSqrtReal(sum);
794: }

796: static inline PetscReal DMPlex_DistRealD_Internal(PetscInt dim, const PetscReal *x, const PetscReal *y)
797: {
798:   PetscReal sum = 0.0;
799:   PetscInt  d;
800:   for (d = 0; d < dim; ++d) sum += (x[d] - y[d]) * (x[d] - y[d]);
801:   return PetscSqrtReal(sum);
802: }

804: PETSC_INTERN PetscErrorCode DMPlexGetPointDualSpaceFEM(DM, PetscInt, PetscInt, PetscDualSpace *);
805: PETSC_INTERN PetscErrorCode DMPlexGetIndicesPoint_Internal(PetscSection, PetscBool, PetscInt, PetscInt, PetscInt *, PetscBool, const PetscInt[], const PetscInt[], PetscInt[]);
806: PETSC_INTERN PetscErrorCode DMPlexGetIndicesPointFields_Internal(PetscSection, PetscBool, PetscInt, PetscInt, PetscInt[], PetscBool, const PetscInt ***, PetscInt, const PetscInt[], PetscInt[]);
807: PETSC_INTERN PetscErrorCode DMPlexGetTransitiveClosure_Internal(DM, PetscInt, PetscInt, PetscBool, PetscInt *, PetscInt *[]);
808: PETSC_INTERN PetscErrorCode DMPlexMatSetClosure_Internal(DM, PetscSection, PetscSection, PetscBool, Mat, PetscInt, const PetscScalar[], InsertMode);

810: PETSC_SINGLE_LIBRARY_VISIBILITY_INTERNAL PetscErrorCode DMPlexGetAllCells_Internal(DM, IS *);
811: PETSC_INTERN PetscErrorCode                             DMPlexGetAllFaces_Internal(DM, IS *);
812: PETSC_EXTERN PetscErrorCode                             DMSNESGetFEGeom(DMField, IS, PetscQuadrature, PetscFEGeomMode, PetscFEGeom **);
813: PETSC_EXTERN PetscErrorCode                             DMSNESRestoreFEGeom(DMField, IS, PetscQuadrature, PetscBool, PetscFEGeom **);
814: PETSC_SINGLE_LIBRARY_VISIBILITY_INTERNAL PetscErrorCode DMPlexComputeResidual_Patch_Internal(DM, PetscSection, IS, PetscReal, Vec, Vec, Vec, void *);
815: PETSC_SINGLE_LIBRARY_VISIBILITY_INTERNAL PetscErrorCode DMPlexComputeJacobian_Patch_Internal(DM, PetscSection, PetscSection, IS, PetscReal, PetscReal, Vec, Vec, Mat, Mat, void *);
816: PETSC_INTERN PetscErrorCode                             DMCreateSubDomainDM_Plex(DM, DMLabel, PetscInt, IS *, DM *);
817: PETSC_INTERN PetscErrorCode                             DMPlexBasisTransformPoint_Internal(DM, DM, Vec, PetscInt, PetscBool[], PetscBool, PetscScalar *);
818: PETSC_INTERN PetscErrorCode                             DMPlexBasisTransformPointTensor_Internal(DM, DM, Vec, PetscInt, PetscBool, PetscInt, PetscScalar *);
819: PETSC_INTERN PetscErrorCode                             DMPlexBasisTransformApplyReal_Internal(DM, const PetscReal[], PetscBool, PetscInt, const PetscReal *, PetscReal *, void *);
820: PETSC_INTERN PetscErrorCode                             DMPlexBasisTransformApply_Internal(DM, const PetscReal[], PetscBool, PetscInt, const PetscScalar *, PetscScalar *, void *);
821: PETSC_INTERN PetscErrorCode                             DMCreateNeumannOverlap_Plex(DM, IS *, Mat *, PetscErrorCode (**)(Mat, PetscReal, Vec, Vec, PetscReal, IS, void *), void **);
822: PETSC_INTERN PetscErrorCode                             DMPlexMarkBoundaryFaces_Internal(DM, PetscInt, PetscInt, DMLabel, PetscBool);
823: PETSC_INTERN PetscErrorCode                             DMPlexDistributeOverlap_Internal(DM, PetscInt, MPI_Comm, const char *, PetscSF *, DM *);

825: PETSC_INTERN PetscErrorCode DMPlexInterpolateFaces_Internal(DM, PetscInt, DM);

827: PETSC_INTERN PetscErrorCode DMPlexMarkSubmesh_Interpolated(DM, DMLabel, PetscInt, PetscBool, PetscBool, DMLabel, DM);

829: PETSC_INTERN PetscErrorCode DMPeriodicCoordinateSetUp_Internal(DM);

831: /* Functions in the vtable */
832: PETSC_INTERN PetscErrorCode DMCreateInterpolation_Plex(DM dmCoarse, DM dmFine, Mat *interpolation, Vec *scaling);
833: PETSC_INTERN PetscErrorCode DMCreateInjection_Plex(DM dmCoarse, DM dmFine, Mat *mat);
834: PETSC_INTERN PetscErrorCode DMCreateMassMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mat);
835: PETSC_INTERN PetscErrorCode DMCreateMassMatrixLumped_Plex(DM, Vec *, Vec *);
836: PETSC_INTERN PetscErrorCode DMCreateGradientMatrix_Plex(DM dmCoarse, DM dmFine, Mat *mat);
837: PETSC_INTERN PetscErrorCode DMCreateLocalSection_Plex(DM dm);
838: PETSC_INTERN PetscErrorCode DMCreateDefaultConstraints_Plex(DM dm);
839: PETSC_INTERN PetscErrorCode DMCreateMatrix_Plex(DM dm, Mat *J);
840: PETSC_INTERN PetscErrorCode DMCreateCoordinateDM_Plex(DM dm, DM *cdm);
841: PETSC_INTERN PetscErrorCode DMCreateCellCoordinateDM_Plex(DM dm, DM *cdm);
842: PETSC_INTERN PetscErrorCode DMCreateCoordinateField_Plex(DM dm, DMField *field);
843: PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm);
844: PETSC_INTERN PetscErrorCode DMSetUp_Plex(DM dm);
845: PETSC_INTERN PetscErrorCode DMDestroy_Plex(DM dm);
846: PETSC_INTERN PetscErrorCode DMView_Plex(DM dm, PetscViewer viewer);
847: PETSC_INTERN PetscErrorCode DMLoad_Plex(DM dm, PetscViewer viewer);
848: PETSC_INTERN PetscErrorCode DMCreateSubDM_Plex(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm);
849: PETSC_INTERN PetscErrorCode DMCreateSuperDM_Plex(DM dms[], PetscInt len, IS **is, DM *superdm);
850: PETSC_INTERN PetscErrorCode DMCreateDomainDecompositionScatters_Plex(DM, PetscInt, DM *, VecScatter **, VecScatter **, VecScatter **);
851: PETSC_INTERN PetscErrorCode DMCreateDomainDecomposition_Plex(DM, PetscInt *, char ***, IS **, IS **, DM **);
852: PETSC_INTERN PetscErrorCode DMCreateSectionPermutation_Plex(DM dm, IS *permutation, PetscBT *blockStarts);
853: PETSC_INTERN PetscErrorCode DMPlexCellUnsplitVertices_Internal(DM, PetscInt, DMPolytopeType, PetscInt *);

855: // Coordinate mapping functions
856: PETSC_INTERN void coordMap_identity(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]);
857: PETSC_INTERN void coordMap_rotate(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]);
858: PETSC_INTERN void coordMap_shear(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]);
859: PETSC_INTERN void coordMap_flare(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]);
860: PETSC_INTERN void coordMap_annulus(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]);
861: PETSC_INTERN void coordMap_shell(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]);
862: PETSC_INTERN void coordMap_sinusoid(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]);
863: PETSC_INTERN void coordMap_torus(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]);

865: PETSC_INTERN PetscErrorCode DMSnapToGeomModel_EGADS(DM, PetscInt, PetscInt, const PetscScalar[], PetscScalar[]);

867: // FIXME: DM with Attached CAD Models (STEP, IGES, BRep, EGADS, EGADSlite)

869: // Coordinate <-> Reference mapping functions
870: PETSC_INTERN PetscErrorCode DMPlexCoordinatesToReference_FE(DM, PetscFE, PetscInt, PetscInt, const PetscReal[], PetscReal[], Vec, PetscInt, PetscInt, PetscInt, PetscReal *);
871: PETSC_INTERN PetscErrorCode DMPlexReferenceToCoordinates_FE(DM, PetscFE, PetscInt, PetscInt, const PetscReal[], PetscReal[], Vec, PetscInt, PetscInt);