xref: /petsc/src/dm/impls/plex/plexinterpolate.c (revision c49d9212298956d63c360e8e3f57cd97cef5e808)
1 #include <petsc-private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2 #include <../src/sys/utils/hash.h>
3 
4 #undef __FUNCT__
5 #define __FUNCT__ "DMPlexGetFaces_Internal"
6 /*
7   DMPlexGetFaces_Internal - Gets groups of vertices that correspond to faces for the given cell
8 */
9 PetscErrorCode DMPlexGetFaces_Internal(DM dm, PetscInt dim, PetscInt p, PetscInt *numFaces, PetscInt *faceSize, const PetscInt *faces[])
10 {
11   const PetscInt *cone = NULL;
12   PetscInt        maxConeSize, maxSupportSize, coneSize;
13   PetscErrorCode  ierr;
14 
15   PetscFunctionBegin;
16   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
18   ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
19   ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
20   ierr = DMPlexGetRawFaces_Internal(dm, dim, coneSize, cone, numFaces, faceSize, faces);CHKERRQ(ierr);
21   PetscFunctionReturn(0);
22 }
23 
24 #undef __FUNCT__
25 #define __FUNCT__ "DMPlexRestoreFaces_Internal"
26 /*
27   DMPlexRestoreFaces_Internal - Restores the array
28 */
29 PetscErrorCode DMPlexRestoreFaces_Internal(DM dm, PetscInt dim, PetscInt p, PetscInt *numFaces, PetscInt *faceSize, const PetscInt *faces[])
30 {
31   PetscErrorCode  ierr;
32 
33   PetscFunctionBegin;
34   ierr = DMRestoreWorkArray(dm, 0, PETSC_INT, (void *) faces);CHKERRQ(ierr);
35   PetscFunctionReturn(0);
36 }
37 
38 #undef __FUNCT__
39 #define __FUNCT__ "DMPlexGetRawFaces_Internal"
40 /*
41   DMPlexGetRawFaces_Internal - Gets groups of vertices that correspond to faces for the given cone
42 */
43 PetscErrorCode DMPlexGetRawFaces_Internal(DM dm, PetscInt dim, PetscInt coneSize, const PetscInt cone[], PetscInt *numFaces, PetscInt *faceSize, const PetscInt *faces[])
44 {
45   PetscInt       *facesTmp;
46   PetscInt        maxConeSize, maxSupportSize;
47   PetscErrorCode  ierr;
48 
49   PetscFunctionBegin;
50   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
51   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
52   if (faces) {ierr = DMGetWorkArray(dm, PetscSqr(PetscMax(maxConeSize, maxSupportSize)), PETSC_INT, &facesTmp);CHKERRQ(ierr);}
53   switch (dim) {
54   case 1:
55     switch (coneSize) {
56     case 2:
57       if (faces) {
58         facesTmp[0] = cone[0]; facesTmp[1] = cone[1];
59         *faces = facesTmp;
60       }
61       if (numFaces) *numFaces         = 2;
62       if (faceSize) *faceSize         = 1;
63       break;
64     default:
65       SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone size %D not supported for dimension %D", coneSize, dim);
66     }
67     break;
68   case 2:
69     switch (coneSize) {
70     case 3:
71       if (faces) {
72         facesTmp[0] = cone[0]; facesTmp[1] = cone[1];
73         facesTmp[2] = cone[1]; facesTmp[3] = cone[2];
74         facesTmp[4] = cone[2]; facesTmp[5] = cone[0];
75         *faces = facesTmp;
76       }
77       if (numFaces) *numFaces         = 3;
78       if (faceSize) *faceSize         = 2;
79       break;
80     case 4:
81       /* Vertices follow right hand rule */
82       if (faces) {
83         facesTmp[0] = cone[0]; facesTmp[1] = cone[1];
84         facesTmp[2] = cone[1]; facesTmp[3] = cone[2];
85         facesTmp[4] = cone[2]; facesTmp[5] = cone[3];
86         facesTmp[6] = cone[3]; facesTmp[7] = cone[0];
87         *faces = facesTmp;
88       }
89       if (numFaces) *numFaces         = 4;
90       if (faceSize) *faceSize         = 2;
91       break;
92     default:
93       SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone size %D not supported for dimension %D", coneSize, dim);
94     }
95     break;
96   case 3:
97     switch (coneSize) {
98     case 3:
99       if (faces) {
100         facesTmp[0] = cone[0]; facesTmp[1] = cone[1];
101         facesTmp[2] = cone[1]; facesTmp[3] = cone[2];
102         facesTmp[4] = cone[2]; facesTmp[5] = cone[0];
103         *faces = facesTmp;
104       }
105       if (numFaces) *numFaces         = 3;
106       if (faceSize) *faceSize         = 2;
107       break;
108     case 4:
109       /* Vertices of first face follow right hand rule and normal points away from last vertex */
110       if (faces) {
111         facesTmp[0] = cone[0]; facesTmp[1]  = cone[1]; facesTmp[2]  = cone[2];
112         facesTmp[3] = cone[0]; facesTmp[4]  = cone[3]; facesTmp[5]  = cone[1];
113         facesTmp[6] = cone[0]; facesTmp[7]  = cone[2]; facesTmp[8]  = cone[3];
114         facesTmp[9] = cone[2]; facesTmp[10] = cone[1]; facesTmp[11] = cone[3];
115         *faces = facesTmp;
116       }
117       if (numFaces) *numFaces         = 4;
118       if (faceSize) *faceSize         = 3;
119       break;
120     case 8:
121       if (faces) {
122         facesTmp[0]  = cone[0]; facesTmp[1]  = cone[1]; facesTmp[2]  = cone[2]; facesTmp[3]  = cone[3]; /* Bottom */
123         facesTmp[4]  = cone[4]; facesTmp[5]  = cone[5]; facesTmp[6]  = cone[6]; facesTmp[7]  = cone[7]; /* Top */
124         facesTmp[8]  = cone[0]; facesTmp[9]  = cone[3]; facesTmp[10] = cone[5]; facesTmp[11] = cone[4]; /* Front */
125         facesTmp[12] = cone[2]; facesTmp[13] = cone[1]; facesTmp[14] = cone[7]; facesTmp[15] = cone[6]; /* Back */
126         facesTmp[16] = cone[3]; facesTmp[17] = cone[2]; facesTmp[18] = cone[6]; facesTmp[19] = cone[5]; /* Right */
127         facesTmp[20] = cone[0]; facesTmp[21] = cone[4]; facesTmp[22] = cone[7]; facesTmp[23] = cone[1]; /* Left */
128         *faces = facesTmp;
129       }
130       if (numFaces) *numFaces         = 6;
131       if (faceSize) *faceSize         = 4;
132       break;
133     default:
134       SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cone size %D not supported for dimension %D", coneSize, dim);
135     }
136     break;
137   default:
138     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Dimension %D not supported", dim);
139   }
140   PetscFunctionReturn(0);
141 }
142 
143 #undef __FUNCT__
144 #define __FUNCT__ "DMPlexInterpolateFaces_Internal"
145 /* This interpolates faces for cells at some stratum */
146 static PetscErrorCode DMPlexInterpolateFaces_Internal(DM dm, PetscInt cellDepth, DM idm)
147 {
148   DMLabel        subpointMap;
149   PetscHashIJKL  faceTable;
150   PetscInt      *pStart, *pEnd;
151   PetscInt       cellDim, depth, faceDepth = cellDepth, numPoints = 0, faceSizeAll = 0, face, c, d;
152   PetscErrorCode ierr;
153 
154   PetscFunctionBegin;
155   ierr = DMPlexGetDimension(dm, &cellDim);CHKERRQ(ierr);
156   /* HACK: I need a better way to determine face dimension, or an alternative to GetFaces() */
157   ierr = DMPlexGetSubpointMap(dm, &subpointMap);CHKERRQ(ierr);
158   if (subpointMap) ++cellDim;
159   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
160   ++depth;
161   ++cellDepth;
162   cellDim -= depth - cellDepth;
163   ierr = PetscMalloc2(depth+1,PetscInt,&pStart,depth+1,PetscInt,&pEnd);CHKERRQ(ierr);
164   for (d = depth-1; d >= faceDepth; --d) {
165     ierr = DMPlexGetDepthStratum(dm, d, &pStart[d+1], &pEnd[d+1]);CHKERRQ(ierr);
166   }
167   ierr = DMPlexGetDepthStratum(dm, -1, NULL, &pStart[faceDepth]);CHKERRQ(ierr);
168   pEnd[faceDepth] = pStart[faceDepth];
169   for (d = faceDepth-1; d >= 0; --d) {
170     ierr = DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);
171   }
172   if (pEnd[cellDepth] > pStart[cellDepth]) {ierr = DMPlexGetFaces_Internal(dm, cellDim, pStart[cellDepth], NULL, &faceSizeAll, NULL);CHKERRQ(ierr);}
173   if (faceSizeAll > 4) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Do not support interpolation of meshes with faces of %D vertices", faceSizeAll);
174   ierr = PetscHashIJKLCreate(&faceTable);CHKERRQ(ierr);
175   ierr = PetscHashIJKLSetMultivalued(faceTable, PETSC_FALSE);CHKERRQ(ierr);
176   for (c = pStart[cellDepth], face = pStart[faceDepth]; c < pEnd[cellDepth]; ++c) {
177     const PetscInt *cellFaces;
178     PetscInt        numCellFaces, faceSize, cf, f;
179 
180     ierr = DMPlexGetFaces_Internal(dm, cellDim, c, &numCellFaces, &faceSize, &cellFaces);CHKERRQ(ierr);
181     if (faceSize != faceSizeAll) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Inconsistent face for cell %D of size %D != %D", c, faceSize, faceSizeAll);
182     for (cf = 0; cf < numCellFaces; ++cf) {
183       const PetscInt  *cellFace = &cellFaces[cf*faceSize];
184       PetscHashIJKLKey key;
185 
186       if (faceSize == 2) {
187         key.i = PetscMin(cellFace[0], cellFace[1]);
188         key.j = PetscMax(cellFace[0], cellFace[1]);
189         key.k = 0;
190         key.l = 0;
191       } else {
192         key.i = cellFace[0]; key.j = cellFace[1]; key.k = cellFace[2]; key.l = faceSize > 3 ? cellFace[3] : 0;
193         ierr = PetscSortInt(faceSize, (PetscInt *) &key);
194       }
195       ierr  = PetscHashIJKLGet(faceTable, key, &f);CHKERRQ(ierr);
196       if (f < 0) {
197         ierr = PetscHashIJKLAdd(faceTable, key, face);CHKERRQ(ierr);
198         f    = face++;
199       }
200     }
201     ierr = DMPlexRestoreFaces_Internal(dm, cellDim, c, &numCellFaces, &faceSize, &cellFaces);CHKERRQ(ierr);
202   }
203   pEnd[faceDepth] = face;
204   ierr = PetscHashIJKLDestroy(&faceTable);CHKERRQ(ierr);
205   /* Count new points */
206   for (d = 0; d <= depth; ++d) {
207     numPoints += pEnd[d]-pStart[d];
208   }
209   ierr = DMPlexSetChart(idm, 0, numPoints);CHKERRQ(ierr);
210   /* Set cone sizes */
211   for (d = 0; d <= depth; ++d) {
212     PetscInt coneSize, p;
213 
214     if (d == faceDepth) {
215       for (p = pStart[d]; p < pEnd[d]; ++p) {
216         /* I see no way to do this if we admit faces of different shapes */
217         ierr = DMPlexSetConeSize(idm, p, faceSizeAll);CHKERRQ(ierr);
218       }
219     } else if (d == cellDepth) {
220       for (p = pStart[d]; p < pEnd[d]; ++p) {
221         /* Number of cell faces may be different from number of cell vertices*/
222         ierr = DMPlexGetFaces_Internal(dm, cellDim, p, &coneSize, NULL, NULL);CHKERRQ(ierr);
223         ierr = DMPlexSetConeSize(idm, p, coneSize);CHKERRQ(ierr);
224       }
225     } else {
226       for (p = pStart[d]; p < pEnd[d]; ++p) {
227         ierr = DMPlexGetConeSize(dm, p, &coneSize);CHKERRQ(ierr);
228         ierr = DMPlexSetConeSize(idm, p, coneSize);CHKERRQ(ierr);
229       }
230     }
231   }
232   ierr = DMSetUp(idm);CHKERRQ(ierr);
233   /* Get face cones from subsets of cell vertices */
234   if (faceSizeAll > 4) SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Do not support interpolation of meshes with faces of %D vertices", faceSizeAll);
235   ierr = PetscHashIJKLCreate(&faceTable);CHKERRQ(ierr);
236   ierr = PetscHashIJKLSetMultivalued(faceTable, PETSC_FALSE);CHKERRQ(ierr);
237   for (d = depth; d > cellDepth; --d) {
238     const PetscInt *cone;
239     PetscInt        p;
240 
241     for (p = pStart[d]; p < pEnd[d]; ++p) {
242       ierr = DMPlexGetCone(dm, p, &cone);CHKERRQ(ierr);
243       ierr = DMPlexSetCone(idm, p, cone);CHKERRQ(ierr);
244       ierr = DMPlexGetConeOrientation(dm, p, &cone);CHKERRQ(ierr);
245       ierr = DMPlexSetConeOrientation(idm, p, cone);CHKERRQ(ierr);
246     }
247   }
248   for (c = pStart[cellDepth], face = pStart[faceDepth]; c < pEnd[cellDepth]; ++c) {
249     const PetscInt *cellFaces;
250     PetscInt        numCellFaces, faceSize, cf, f;
251 
252     ierr = DMPlexGetFaces_Internal(dm, cellDim, c, &numCellFaces, &faceSize, &cellFaces);CHKERRQ(ierr);
253     if (faceSize != faceSizeAll) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Inconsistent face for cell %D of size %D != %D", c, faceSize, faceSizeAll);
254     for (cf = 0; cf < numCellFaces; ++cf) {
255       const PetscInt  *cellFace = &cellFaces[cf*faceSize];
256       PetscHashIJKLKey key;
257 
258       if (faceSize == 2) {
259         key.i = PetscMin(cellFace[0], cellFace[1]);
260         key.j = PetscMax(cellFace[0], cellFace[1]);
261         key.k = 0;
262         key.l = 0;
263       } else {
264         key.i = cellFace[0]; key.j = cellFace[1]; key.k = cellFace[2]; key.l = faceSize > 3 ? cellFace[3] : 0;
265         ierr = PetscSortInt(faceSize, (PetscInt *) &key);
266       }
267       ierr  = PetscHashIJKLGet(faceTable, key, &f);CHKERRQ(ierr);
268       if (f < 0) {
269         ierr = DMPlexSetCone(idm, face, cellFace);CHKERRQ(ierr);
270         ierr = PetscHashIJKLAdd(faceTable, key, face);CHKERRQ(ierr);
271         f    = face++;
272         ierr = DMPlexInsertCone(idm, c, cf, f);CHKERRQ(ierr);
273       } else {
274         const PetscInt *cone;
275         PetscInt        coneSize, ornt, i, j;
276 
277         ierr = DMPlexInsertCone(idm, c, cf, f);CHKERRQ(ierr);
278         /* Orient face: Do not allow reverse orientation at the first vertex */
279         ierr = DMPlexGetConeSize(idm, f, &coneSize);CHKERRQ(ierr);
280         ierr = DMPlexGetCone(idm, f, &cone);CHKERRQ(ierr);
281         if (coneSize != faceSize) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid number of face vertices %D for face %D should be %D", coneSize, f, faceSize);
282         /* - First find the initial vertex */
283         for (i = 0; i < faceSize; ++i) if (cellFace[0] == cone[i]) break;
284         /* - Try forward comparison */
285         for (j = 0; j < faceSize; ++j) if (cellFace[j] != cone[(i+j)%faceSize]) break;
286         if (j == faceSize) {
287           if ((faceSize == 2) && (i == 1)) ornt = -2;
288           else                             ornt = i;
289         } else {
290           /* - Try backward comparison */
291           for (j = 0; j < faceSize; ++j) if (cellFace[j] != cone[(i+faceSize-j)%faceSize]) break;
292           if (j == faceSize) {
293             if (i == 0) ornt = -faceSize;
294             else        ornt = -i;
295           } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Could not determine face orientation");
296         }
297         ierr = DMPlexInsertConeOrientation(idm, c, cf, ornt);CHKERRQ(ierr);
298       }
299     }
300     ierr = DMPlexRestoreFaces_Internal(dm, cellDim, c, &numCellFaces, &faceSize, &cellFaces);CHKERRQ(ierr);
301   }
302   if (face != pEnd[faceDepth]) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid number of faces %D should be %D", face-pStart[faceDepth], pEnd[faceDepth]-pStart[faceDepth]);
303   ierr = PetscFree2(pStart,pEnd);CHKERRQ(ierr);
304   ierr = PetscHashIJKLDestroy(&faceTable);CHKERRQ(ierr);
305   ierr = PetscFree2(pStart,pEnd);CHKERRQ(ierr);
306   ierr = DMPlexSymmetrize(idm);CHKERRQ(ierr);
307   ierr = DMPlexStratify(idm);CHKERRQ(ierr);
308   PetscFunctionReturn(0);
309 }
310 
311 #undef __FUNCT__
312 #define __FUNCT__ "DMPlexInterpolate"
313 /*@
314   DMPlexInterpolate - Take in a cell-vertex mesh and return one with all intermediate faces, edges, etc.
315 
316   Collective on DM
317 
318   Input Parameter:
319 . dm - The DMPlex object with only cells and vertices
320 
321   Output Parameter:
322 . dmInt - The complete DMPlex object
323 
324   Level: intermediate
325 
326 .keywords: mesh
327 .seealso: DMPlexUninterpolate(), DMPlexCreateFromCellList()
328 @*/
329 PetscErrorCode DMPlexInterpolate(DM dm, DM *dmInt)
330 {
331   DM             idm, odm = dm;
332   PetscInt       depth, dim, d;
333   PetscErrorCode ierr;
334 
335   PetscFunctionBegin;
336   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
337   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
338   if (dim <= 1) {
339     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
340     idm  = dm;
341   }
342   for (d = 1; d < dim; ++d) {
343     /* Create interpolated mesh */
344     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &idm);CHKERRQ(ierr);
345     ierr = DMSetType(idm, DMPLEX);CHKERRQ(ierr);
346     ierr = DMPlexSetDimension(idm, dim);CHKERRQ(ierr);
347     if (depth > 0) {ierr = DMPlexInterpolateFaces_Internal(odm, 1, idm);CHKERRQ(ierr);}
348     if (odm != dm) {ierr = DMDestroy(&odm);CHKERRQ(ierr);}
349     odm  = idm;
350   }
351   *dmInt = idm;
352   PetscFunctionReturn(0);
353 }
354 
355 #undef __FUNCT__
356 #define __FUNCT__ "DMPlexCopyCoordinates"
357 /*@
358   DMPlexCopyCoordinates - Copy coordinates from one mesh to another with the same vertices
359 
360   Collective on DM
361 
362   Input Parameter:
363 . dmA - The DMPlex object with initial coordinates
364 
365   Output Parameter:
366 . dmB - The DMPlex object with copied coordinates
367 
368   Level: intermediate
369 
370   Note: This is typically used when adding pieces other than vertices to a mesh
371 
372 .keywords: mesh
373 .seealso: DMCopyLabels(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMPlexGetCoordinateSection()
374 @*/
375 PetscErrorCode DMPlexCopyCoordinates(DM dmA, DM dmB)
376 {
377   Vec            coordinatesA, coordinatesB;
378   PetscSection   coordSectionA, coordSectionB;
379   PetscScalar   *coordsA, *coordsB;
380   PetscInt       spaceDim, vStartA, vStartB, vEndA, vEndB, coordSizeB, v, d;
381   PetscErrorCode ierr;
382 
383   PetscFunctionBegin;
384   if (dmA == dmB) PetscFunctionReturn(0);
385   ierr = DMPlexGetDepthStratum(dmA, 0, &vStartA, &vEndA);CHKERRQ(ierr);
386   ierr = DMPlexGetDepthStratum(dmB, 0, &vStartB, &vEndB);CHKERRQ(ierr);
387   if ((vEndA-vStartA) != (vEndB-vStartB)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "The number of vertices in first DM %d != %d in the second DM", vEndA-vStartA, vEndB-vStartB);
388   ierr = DMPlexGetCoordinateSection(dmA, &coordSectionA);CHKERRQ(ierr);
389   ierr = DMPlexGetCoordinateSection(dmB, &coordSectionB);CHKERRQ(ierr);
390   ierr = PetscSectionSetNumFields(coordSectionB, 1);CHKERRQ(ierr);
391   ierr = PetscSectionGetFieldComponents(coordSectionA, 0, &spaceDim);CHKERRQ(ierr);
392   ierr = PetscSectionSetFieldComponents(coordSectionB, 0, spaceDim);CHKERRQ(ierr);
393   ierr = PetscSectionSetChart(coordSectionB, vStartB, vEndB);CHKERRQ(ierr);
394   for (v = vStartB; v < vEndB; ++v) {
395     ierr = PetscSectionSetDof(coordSectionB, v, spaceDim);CHKERRQ(ierr);
396     ierr = PetscSectionSetFieldDof(coordSectionB, v, 0, spaceDim);CHKERRQ(ierr);
397   }
398   ierr = PetscSectionSetUp(coordSectionB);CHKERRQ(ierr);
399   ierr = PetscSectionGetStorageSize(coordSectionB, &coordSizeB);CHKERRQ(ierr);
400   ierr = DMGetCoordinatesLocal(dmA, &coordinatesA);CHKERRQ(ierr);
401   ierr = VecCreate(PetscObjectComm((PetscObject) dmB), &coordinatesB);CHKERRQ(ierr);
402   ierr = PetscObjectSetName((PetscObject) coordinatesB, "coordinates");CHKERRQ(ierr);
403   ierr = VecSetSizes(coordinatesB, coordSizeB, PETSC_DETERMINE);CHKERRQ(ierr);
404   ierr = VecSetType(coordinatesB,VECSTANDARD);CHKERRQ(ierr);
405   ierr = VecGetArray(coordinatesA, &coordsA);CHKERRQ(ierr);
406   ierr = VecGetArray(coordinatesB, &coordsB);CHKERRQ(ierr);
407   for (v = 0; v < vEndB-vStartB; ++v) {
408     for (d = 0; d < spaceDim; ++d) {
409       coordsB[v*spaceDim+d] = coordsA[v*spaceDim+d];
410     }
411   }
412   ierr = VecRestoreArray(coordinatesA, &coordsA);CHKERRQ(ierr);
413   ierr = VecRestoreArray(coordinatesB, &coordsB);CHKERRQ(ierr);
414   ierr = DMSetCoordinatesLocal(dmB, coordinatesB);CHKERRQ(ierr);
415   ierr = VecDestroy(&coordinatesB);CHKERRQ(ierr);
416   PetscFunctionReturn(0);
417 }
418 
419 #undef __FUNCT__
420 #define __FUNCT__ "DMPlexCopyLabels"
421 /*@
422   DMPlexCopyLabels - Copy labels from one mesh to another with a superset of the points
423 
424   Collective on DM
425 
426   Input Parameter:
427 . dmA - The DMPlex object with initial labels
428 
429   Output Parameter:
430 . dmB - The DMPlex object with copied labels
431 
432   Level: intermediate
433 
434   Note: This is typically used when interpolating or otherwise adding to a mesh
435 
436 .keywords: mesh
437 .seealso: DMCopyCoordinates(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMPlexGetCoordinateSection()
438 @*/
439 PetscErrorCode DMPlexCopyLabels(DM dmA, DM dmB)
440 {
441   PetscInt       numLabels, l;
442   PetscErrorCode ierr;
443 
444   PetscFunctionBegin;
445   if (dmA == dmB) PetscFunctionReturn(0);
446   ierr = DMPlexGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
447   for (l = 0; l < numLabels; ++l) {
448     DMLabel     label, labelNew;
449     const char *name;
450     PetscBool   flg;
451 
452     ierr = DMPlexGetLabelName(dmA, l, &name);CHKERRQ(ierr);
453     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
454     if (flg) continue;
455     ierr = DMPlexGetLabel(dmA, name, &label);CHKERRQ(ierr);
456     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
457     ierr = DMPlexAddLabel(dmB, labelNew);CHKERRQ(ierr);
458   }
459   PetscFunctionReturn(0);
460 }
461 
462 #undef __FUNCT__
463 #define __FUNCT__ "DMPlexUninterpolate"
464 /*@
465   DMPlexUninterpolate - Take in a mesh with all intermediate faces, edges, etc. and return a cell-vertex mesh
466 
467   Collective on DM
468 
469   Input Parameter:
470 . dm - The complete DMPlex object
471 
472   Output Parameter:
473 . dmUnint - The DMPlex object with only cells and vertices
474 
475   Level: intermediate
476 
477 .keywords: mesh
478 .seealso: DMPlexInterpolate(), DMPlexCreateFromCellList()
479 @*/
480 PetscErrorCode DMPlexUninterpolate(DM dm, DM *dmUnint)
481 {
482   DM             udm;
483   PetscInt       dim, vStart, vEnd, cStart, cEnd, c, maxConeSize = 0, *cone;
484   PetscErrorCode ierr;
485 
486   PetscFunctionBegin;
487   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
488   if (dim <= 1) {
489     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
490     *dmUnint = dm;
491     PetscFunctionReturn(0);
492   }
493   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
494   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
495   ierr = DMCreate(PetscObjectComm((PetscObject) dm), &udm);CHKERRQ(ierr);
496   ierr = DMSetType(udm, DMPLEX);CHKERRQ(ierr);
497   ierr = DMPlexSetDimension(udm, dim);CHKERRQ(ierr);
498   ierr = DMPlexSetChart(udm, cStart, vEnd);CHKERRQ(ierr);
499   for (c = cStart; c < cEnd; ++c) {
500     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
501 
502     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
503     for (cl = 0; cl < closureSize*2; cl += 2) {
504       const PetscInt p = closure[cl];
505 
506       if ((p >= vStart) && (p < vEnd)) ++coneSize;
507     }
508     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
509     ierr = DMPlexSetConeSize(udm, c, coneSize);CHKERRQ(ierr);
510     maxConeSize = PetscMax(maxConeSize, coneSize);
511   }
512   ierr = DMSetUp(udm);CHKERRQ(ierr);
513   ierr = PetscMalloc(maxConeSize * sizeof(PetscInt), &cone);CHKERRQ(ierr);
514   for (c = cStart; c < cEnd; ++c) {
515     PetscInt *closure = NULL, closureSize, cl, coneSize = 0;
516 
517     ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
518     for (cl = 0; cl < closureSize*2; cl += 2) {
519       const PetscInt p = closure[cl];
520 
521       if ((p >= vStart) && (p < vEnd)) cone[coneSize++] = p;
522     }
523     ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
524     ierr = DMPlexSetCone(udm, c, cone);CHKERRQ(ierr);
525   }
526   ierr = PetscFree(cone);CHKERRQ(ierr);
527   ierr = DMPlexSymmetrize(udm);CHKERRQ(ierr);
528   ierr = DMPlexStratify(udm);CHKERRQ(ierr);
529   *dmUnint = udm;
530   PetscFunctionReturn(0);
531 }
532