xref: /petsc/src/dm/impls/plex/plexsubmesh.c (revision dcbb62e8e57ddd64376d1b446ec330de727dfe50)
1 #include <petsc-private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
2 #include <petscsf.h>
3 
4 #undef __FUNCT__
5 #define __FUNCT__ "DMPlexMarkBoundaryFaces"
6 /*@
7   DMPlexMarkBoundaryFaces - Mark all faces on the boundary
8 
9   Not Collective
10 
11   Input Parameter:
12 . dm - The original DM
13 
14   Output Parameter:
15 . label - The DMLabel marking boundary faces with value 1
16 
17   Level: developer
18 
19 .seealso: DMLabelCreate(), DMPlexCreateLabel()
20 @*/
21 PetscErrorCode DMPlexMarkBoundaryFaces(DM dm, DMLabel label)
22 {
23   PetscInt       fStart, fEnd, f;
24   PetscErrorCode ierr;
25 
26   PetscFunctionBegin;
27   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
28   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
29   for (f = fStart; f < fEnd; ++f) {
30     PetscInt supportSize;
31 
32     ierr = DMPlexGetSupportSize(dm, f, &supportSize);CHKERRQ(ierr);
33     if (supportSize == 1) {
34       ierr = DMLabelSetValue(label, f, 1);CHKERRQ(ierr);
35     }
36   }
37   PetscFunctionReturn(0);
38 }
39 
40 #undef __FUNCT__
41 #define __FUNCT__ "DMPlexLabelComplete"
42 /*@
43   DMPlexLabelComplete - Starting with a label marking points on a surface, we add the transitive closure to the surface
44 
45   Input Parameters:
46 + dm - The DM
47 - label - A DMLabel marking the surface points
48 
49   Output Parameter:
50 . label - A DMLabel marking all surface points in the transitive closure
51 
52   Level: developer
53 
54 .seealso: DMPlexLabelCohesiveComplete()
55 @*/
56 PetscErrorCode DMPlexLabelComplete(DM dm, DMLabel label)
57 {
58   IS              valueIS;
59   const PetscInt *values;
60   PetscInt        numValues, v;
61   PetscErrorCode  ierr;
62 
63   PetscFunctionBegin;
64   ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
65   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
66   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
67   for (v = 0; v < numValues; ++v) {
68     IS              pointIS;
69     const PetscInt *points;
70     PetscInt        numPoints, p;
71 
72     ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr);
73     ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
74     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
75     for (p = 0; p < numPoints; ++p) {
76       PetscInt *closure = NULL;
77       PetscInt  closureSize, c;
78 
79       ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
80       for (c = 0; c < closureSize*2; c += 2) {
81         ierr = DMLabelSetValue(label, closure[c], values[v]);CHKERRQ(ierr);
82       }
83       ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
84     }
85     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
86     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
87   }
88   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
89   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
90   PetscFunctionReturn(0);
91 }
92 
93 #undef __FUNCT__
94 #define __FUNCT__ "DMPlexShiftPoint_Internal"
95 PETSC_STATIC_INLINE PetscInt DMPlexShiftPoint_Internal(PetscInt p, PetscInt depth, PetscInt depthEnd[], PetscInt depthShift[])
96 {
97   if (depth < 0) return p;
98   /* Cells    */ if (p < depthEnd[depth])   return p;
99   /* Vertices */ if (p < depthEnd[0])       return p + depthShift[depth];
100   /* Faces    */ if (p < depthEnd[depth-1]) return p + depthShift[depth] + depthShift[0];
101   /* Edges    */                            return p + depthShift[depth] + depthShift[0] + depthShift[depth-1];
102 }
103 
104 #undef __FUNCT__
105 #define __FUNCT__ "DMPlexShiftSizes_Internal"
106 static PetscErrorCode DMPlexShiftSizes_Internal(DM dm, PetscInt depthShift[], DM dmNew)
107 {
108   PetscInt      *depthEnd;
109   PetscInt       depth = 0, d, pStart, pEnd, p;
110   PetscErrorCode ierr;
111 
112   PetscFunctionBegin;
113   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
114   if (depth < 0) PetscFunctionReturn(0);
115   ierr = PetscMalloc((depth+1) * sizeof(PetscInt), &depthEnd);CHKERRQ(ierr);
116   /* Step 1: Expand chart */
117   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
118   for (d = 0; d <= depth; ++d) {
119     pEnd += depthShift[d];
120     ierr  = DMPlexGetDepthStratum(dm, d, NULL, &depthEnd[d]);CHKERRQ(ierr);
121   }
122   ierr = DMPlexSetChart(dmNew, pStart, pEnd);CHKERRQ(ierr);
123   /* Step 2: Set cone and support sizes */
124   for (d = 0; d <= depth; ++d) {
125     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
126     for (p = pStart; p < pEnd; ++p) {
127       PetscInt newp = DMPlexShiftPoint_Internal(p, depth, depthEnd, depthShift);
128       PetscInt size;
129 
130       ierr = DMPlexGetConeSize(dm, p, &size);CHKERRQ(ierr);
131       ierr = DMPlexSetConeSize(dmNew, newp, size);CHKERRQ(ierr);
132       ierr = DMPlexGetSupportSize(dm, p, &size);CHKERRQ(ierr);
133       ierr = DMPlexSetSupportSize(dmNew, newp, size);CHKERRQ(ierr);
134     }
135   }
136   ierr = PetscFree(depthEnd);CHKERRQ(ierr);
137   PetscFunctionReturn(0);
138 }
139 
140 #undef __FUNCT__
141 #define __FUNCT__ "DMPlexShiftPoints_Internal"
142 static PetscErrorCode DMPlexShiftPoints_Internal(DM dm, PetscInt depthShift[], DM dmNew)
143 {
144   PetscInt      *depthEnd, *newpoints;
145   PetscInt       depth = 0, d, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, pStart, pEnd, p;
146   PetscErrorCode ierr;
147 
148   PetscFunctionBegin;
149   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
150   if (depth < 0) PetscFunctionReturn(0);
151   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
152   ierr = DMPlexGetMaxSizes(dmNew, &maxConeSizeNew, &maxSupportSizeNew);CHKERRQ(ierr);
153   ierr = PetscMalloc2(depth+1,PetscInt,&depthEnd,PetscMax(PetscMax(maxConeSize, maxSupportSize), PetscMax(maxConeSizeNew, maxSupportSizeNew)),PetscInt,&newpoints);CHKERRQ(ierr);
154   for (d = 0; d <= depth; ++d) {
155     ierr = DMPlexGetDepthStratum(dm, d, NULL, &depthEnd[d]);CHKERRQ(ierr);
156   }
157   /* Step 5: Set cones and supports */
158   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
159   for (p = pStart; p < pEnd; ++p) {
160     const PetscInt *points = NULL, *orientations = NULL;
161     PetscInt        size,sizeNew, i, newp = DMPlexShiftPoint_Internal(p, depth, depthEnd, depthShift);
162 
163     ierr = DMPlexGetConeSize(dm, p, &size);CHKERRQ(ierr);
164     ierr = DMPlexGetCone(dm, p, &points);CHKERRQ(ierr);
165     ierr = DMPlexGetConeOrientation(dm, p, &orientations);CHKERRQ(ierr);
166     for (i = 0; i < size; ++i) {
167       newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthEnd, depthShift);
168     }
169     ierr = DMPlexSetCone(dmNew, newp, newpoints);CHKERRQ(ierr);
170     ierr = DMPlexSetConeOrientation(dmNew, newp, orientations);CHKERRQ(ierr);
171     ierr = DMPlexGetSupportSize(dm, p, &size);CHKERRQ(ierr);
172     ierr = DMPlexGetSupportSize(dmNew, newp, &sizeNew);CHKERRQ(ierr);
173     ierr = DMPlexGetSupport(dm, p, &points);CHKERRQ(ierr);
174     for (i = 0; i < size; ++i) {
175       newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthEnd, depthShift);
176     }
177     for (i = size; i < sizeNew; ++i) newpoints[i] = 0;
178     ierr = DMPlexSetSupport(dmNew, newp, newpoints);CHKERRQ(ierr);
179   }
180   ierr = PetscFree2(depthEnd,newpoints);CHKERRQ(ierr);
181   PetscFunctionReturn(0);
182 }
183 
184 #undef __FUNCT__
185 #define __FUNCT__ "DMPlexShiftCoordinates_Internal"
186 static PetscErrorCode DMPlexShiftCoordinates_Internal(DM dm, PetscInt depthShift[], DM dmNew)
187 {
188   PetscSection   coordSection, newCoordSection;
189   Vec            coordinates, newCoordinates;
190   PetscScalar   *coords, *newCoords;
191   PetscInt      *depthEnd, coordSize;
192   PetscInt       dim, depth = 0, d, vStart, vEnd, vStartNew, vEndNew, v;
193   PetscErrorCode ierr;
194 
195   PetscFunctionBegin;
196   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
197   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
198   ierr = PetscMalloc((depth+1) * sizeof(PetscInt), &depthEnd);CHKERRQ(ierr);
199   for (d = 0; d <= depth; ++d) {
200     ierr = DMPlexGetDepthStratum(dm, d, NULL, &depthEnd[d]);CHKERRQ(ierr);
201   }
202   /* Step 8: Convert coordinates */
203   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
204   ierr = DMPlexGetDepthStratum(dmNew, 0, &vStartNew, &vEndNew);CHKERRQ(ierr);
205   ierr = DMPlexGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
206   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &newCoordSection);CHKERRQ(ierr);
207   ierr = PetscSectionSetNumFields(newCoordSection, 1);CHKERRQ(ierr);
208   ierr = PetscSectionSetFieldComponents(newCoordSection, 0, dim);CHKERRQ(ierr);
209   ierr = PetscSectionSetChart(newCoordSection, vStartNew, vEndNew);CHKERRQ(ierr);
210   for (v = vStartNew; v < vEndNew; ++v) {
211     ierr = PetscSectionSetDof(newCoordSection, v, dim);CHKERRQ(ierr);
212     ierr = PetscSectionSetFieldDof(newCoordSection, v, 0, dim);CHKERRQ(ierr);
213   }
214   ierr = PetscSectionSetUp(newCoordSection);CHKERRQ(ierr);
215   ierr = DMPlexSetCoordinateSection(dmNew, newCoordSection);CHKERRQ(ierr);
216   ierr = PetscSectionGetStorageSize(newCoordSection, &coordSize);CHKERRQ(ierr);
217   ierr = VecCreate(PetscObjectComm((PetscObject)dm), &newCoordinates);CHKERRQ(ierr);
218   ierr = PetscObjectSetName((PetscObject) newCoordinates, "coordinates");CHKERRQ(ierr);
219   ierr = VecSetSizes(newCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
220   ierr = VecSetType(newCoordinates,VECSTANDARD);CHKERRQ(ierr);
221   ierr = DMSetCoordinatesLocal(dmNew, newCoordinates);CHKERRQ(ierr);
222   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
223   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
224   ierr = VecGetArray(newCoordinates, &newCoords);CHKERRQ(ierr);
225   for (v = vStart; v < vEnd; ++v) {
226     PetscInt dof, off, noff, d;
227 
228     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
229     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
230     ierr = PetscSectionGetOffset(newCoordSection, DMPlexShiftPoint_Internal(v, depth, depthEnd, depthShift), &noff);CHKERRQ(ierr);
231     for (d = 0; d < dof; ++d) {
232       newCoords[noff+d] = coords[off+d];
233     }
234   }
235   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
236   ierr = VecRestoreArray(newCoordinates, &newCoords);CHKERRQ(ierr);
237   ierr = VecDestroy(&newCoordinates);CHKERRQ(ierr);
238   ierr = PetscSectionDestroy(&newCoordSection);CHKERRQ(ierr);
239   ierr = PetscFree(depthEnd);CHKERRQ(ierr);
240   PetscFunctionReturn(0);
241 }
242 
243 #undef __FUNCT__
244 #define __FUNCT__ "DMPlexShiftSF_Internal"
245 static PetscErrorCode DMPlexShiftSF_Internal(DM dm, PetscInt depthShift[], DM dmNew)
246 {
247   PetscInt          *depthEnd;
248   PetscInt           depth = 0, d;
249   PetscSF            sfPoint, sfPointNew;
250   const PetscSFNode *remotePoints;
251   PetscSFNode       *gremotePoints;
252   const PetscInt    *localPoints;
253   PetscInt          *glocalPoints, *newLocation, *newRemoteLocation;
254   PetscInt           numRoots, numLeaves, l, pStart, pEnd, totShift = 0;
255   PetscErrorCode     ierr;
256 
257   PetscFunctionBegin;
258   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
259   ierr = PetscMalloc((depth+1) * sizeof(PetscInt), &depthEnd);CHKERRQ(ierr);
260   for (d = 0; d <= depth; ++d) {
261     totShift += depthShift[d];
262     ierr      = DMPlexGetDepthStratum(dm, d, NULL, &depthEnd[d]);CHKERRQ(ierr);
263   }
264   /* Step 9: Convert pointSF */
265   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
266   ierr = DMGetPointSF(dmNew, &sfPointNew);CHKERRQ(ierr);
267   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
268   ierr = PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr);
269   if (numRoots >= 0) {
270     ierr = PetscMalloc2(numRoots,PetscInt,&newLocation,pEnd-pStart,PetscInt,&newRemoteLocation);CHKERRQ(ierr);
271     for (l=0; l<numRoots; l++) newLocation[l] = DMPlexShiftPoint_Internal(l, depth, depthEnd, depthShift);
272     ierr = PetscSFBcastBegin(sfPoint, MPIU_INT, newLocation, newRemoteLocation);CHKERRQ(ierr);
273     ierr = PetscSFBcastEnd(sfPoint, MPIU_INT, newLocation, newRemoteLocation);CHKERRQ(ierr);
274     ierr = PetscMalloc(numLeaves * sizeof(PetscInt),    &glocalPoints);CHKERRQ(ierr);
275     ierr = PetscMalloc(numLeaves * sizeof(PetscSFNode), &gremotePoints);CHKERRQ(ierr);
276     for (l = 0; l < numLeaves; ++l) {
277       glocalPoints[l]        = DMPlexShiftPoint_Internal(localPoints[l], depth, depthEnd, depthShift);
278       gremotePoints[l].rank  = remotePoints[l].rank;
279       gremotePoints[l].index = newRemoteLocation[localPoints[l]];
280     }
281     ierr = PetscFree2(newLocation,newRemoteLocation);CHKERRQ(ierr);
282     ierr = PetscSFSetGraph(sfPointNew, numRoots + totShift, numLeaves, glocalPoints, PETSC_OWN_POINTER, gremotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
283   }
284   ierr = PetscFree(depthEnd);CHKERRQ(ierr);
285   PetscFunctionReturn(0);
286 }
287 
288 #undef __FUNCT__
289 #define __FUNCT__ "DMPlexShiftLabels_Internal"
290 static PetscErrorCode DMPlexShiftLabels_Internal(DM dm, PetscInt depthShift[], DM dmNew)
291 {
292   PetscSF            sfPoint;
293   DMLabel            vtkLabel, ghostLabel;
294   PetscInt          *depthEnd;
295   const PetscSFNode *leafRemote;
296   const PetscInt    *leafLocal;
297   PetscInt           depth = 0, d, numLeaves, numLabels, l, cStart, cEnd, c, fStart, fEnd, f;
298   PetscMPIInt        rank;
299   PetscErrorCode     ierr;
300 
301   PetscFunctionBegin;
302   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
303   ierr = PetscMalloc((depth+1) * sizeof(PetscInt), &depthEnd);CHKERRQ(ierr);
304   for (d = 0; d <= depth; ++d) {
305     ierr = DMPlexGetDepthStratum(dm, d, NULL, &depthEnd[d]);CHKERRQ(ierr);
306   }
307   /* Step 10: Convert labels */
308   ierr = DMPlexGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
309   for (l = 0; l < numLabels; ++l) {
310     DMLabel         label, newlabel;
311     const char     *lname;
312     PetscBool       isDepth;
313     IS              valueIS;
314     const PetscInt *values;
315     PetscInt        numValues, val;
316 
317     ierr = DMPlexGetLabelName(dm, l, &lname);CHKERRQ(ierr);
318     ierr = PetscStrcmp(lname, "depth", &isDepth);CHKERRQ(ierr);
319     if (isDepth) continue;
320     ierr = DMPlexCreateLabel(dmNew, lname);CHKERRQ(ierr);
321     ierr = DMPlexGetLabel(dm, lname, &label);CHKERRQ(ierr);
322     ierr = DMPlexGetLabel(dmNew, lname, &newlabel);CHKERRQ(ierr);
323     ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
324     ierr = ISGetLocalSize(valueIS, &numValues);CHKERRQ(ierr);
325     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
326     for (val = 0; val < numValues; ++val) {
327       IS              pointIS;
328       const PetscInt *points;
329       PetscInt        numPoints, p;
330 
331       ierr = DMLabelGetStratumIS(label, values[val], &pointIS);CHKERRQ(ierr);
332       ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
333       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
334       for (p = 0; p < numPoints; ++p) {
335         const PetscInt newpoint = DMPlexShiftPoint_Internal(points[p], depth, depthEnd, depthShift);
336 
337         ierr = DMLabelSetValue(newlabel, newpoint, values[val]);CHKERRQ(ierr);
338       }
339       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
340       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
341     }
342     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
343     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
344   }
345   ierr = PetscFree(depthEnd);CHKERRQ(ierr);
346   /* Step 11: Make label for output (vtk) and to mark ghost points (ghost) */
347   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
348   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
349   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
350   ierr = PetscSFGetGraph(sfPoint, NULL, &numLeaves, &leafLocal, &leafRemote);CHKERRQ(ierr);
351   ierr = DMPlexCreateLabel(dmNew, "vtk");CHKERRQ(ierr);
352   ierr = DMPlexCreateLabel(dmNew, "ghost");CHKERRQ(ierr);
353   ierr = DMPlexGetLabel(dmNew, "vtk", &vtkLabel);CHKERRQ(ierr);
354   ierr = DMPlexGetLabel(dmNew, "ghost", &ghostLabel);CHKERRQ(ierr);
355   for (l = 0, c = cStart; l < numLeaves && c < cEnd; ++l, ++c) {
356     for (; c < leafLocal[l] && c < cEnd; ++c) {
357       ierr = DMLabelSetValue(vtkLabel, c, 1);CHKERRQ(ierr);
358     }
359     if (leafLocal[l] >= cEnd) break;
360     if (leafRemote[l].rank == rank) {
361       ierr = DMLabelSetValue(vtkLabel, c, 1);CHKERRQ(ierr);
362     } else {
363       ierr = DMLabelSetValue(ghostLabel, c, 2);CHKERRQ(ierr);
364     }
365   }
366   for (; c < cEnd; ++c) {
367     ierr = DMLabelSetValue(vtkLabel, c, 1);CHKERRQ(ierr);
368   }
369   if (0) {
370     ierr = PetscViewerASCIISynchronizedAllow(PETSC_VIEWER_STDOUT_WORLD, PETSC_TRUE);CHKERRQ(ierr);
371     ierr = DMLabelView(vtkLabel, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
372     ierr = PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
373   }
374   ierr = DMPlexGetHeightStratum(dmNew, 1, &fStart, &fEnd);CHKERRQ(ierr);
375   for (f = fStart; f < fEnd; ++f) {
376     PetscInt numCells;
377 
378     ierr = DMPlexGetSupportSize(dmNew, f, &numCells);CHKERRQ(ierr);
379     if (numCells < 2) {
380       ierr = DMLabelSetValue(ghostLabel, f, 1);CHKERRQ(ierr);
381     } else {
382       const PetscInt *cells = NULL;
383       PetscInt        vA, vB;
384 
385       ierr = DMPlexGetSupport(dmNew, f, &cells);CHKERRQ(ierr);
386       ierr = DMLabelGetValue(vtkLabel, cells[0], &vA);CHKERRQ(ierr);
387       ierr = DMLabelGetValue(vtkLabel, cells[1], &vB);CHKERRQ(ierr);
388       if (!vA && !vB) {ierr = DMLabelSetValue(ghostLabel, f, 1);CHKERRQ(ierr);}
389     }
390   }
391   if (0) {
392     ierr = PetscViewerASCIISynchronizedAllow(PETSC_VIEWER_STDOUT_WORLD, PETSC_TRUE);CHKERRQ(ierr);
393     ierr = DMLabelView(ghostLabel, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
394     ierr = PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
395   }
396   PetscFunctionReturn(0);
397 }
398 
399 #undef __FUNCT__
400 #define __FUNCT__ "DMPlexConstructGhostCells_Internal"
401 static PetscErrorCode DMPlexConstructGhostCells_Internal(DM dm, DMLabel label, PetscInt *numGhostCells, DM gdm)
402 {
403   IS              valueIS;
404   const PetscInt *values;
405   PetscInt       *depthShift;
406   PetscInt        depth = 0, numFS, fs, ghostCell, cEnd, c;
407   PetscErrorCode  ierr;
408 
409   PetscFunctionBegin;
410   /* Count ghost cells */
411   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
412   ierr = ISGetLocalSize(valueIS, &numFS);CHKERRQ(ierr);
413   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
414 
415   *numGhostCells = 0;
416   for (fs = 0; fs < numFS; ++fs) {
417     PetscInt numBdFaces;
418 
419     ierr = DMLabelGetStratumSize(label, values[fs], &numBdFaces);CHKERRQ(ierr);
420 
421     *numGhostCells += numBdFaces;
422   }
423   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
424   ierr = PetscMalloc((depth+1) * sizeof(PetscInt), &depthShift);CHKERRQ(ierr);
425   ierr = PetscMemzero(depthShift, (depth+1) * sizeof(PetscInt));CHKERRQ(ierr);
426   if (depth >= 0) depthShift[depth] = *numGhostCells;
427   ierr = DMPlexShiftSizes_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
428   /* Step 3: Set cone/support sizes for new points */
429   ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
430   for (c = cEnd; c < cEnd + *numGhostCells; ++c) {
431     ierr = DMPlexSetConeSize(gdm, c, 1);CHKERRQ(ierr);
432   }
433   for (fs = 0; fs < numFS; ++fs) {
434     IS              faceIS;
435     const PetscInt *faces;
436     PetscInt        numFaces, f;
437 
438     ierr = DMLabelGetStratumIS(label, values[fs], &faceIS);CHKERRQ(ierr);
439     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
440     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
441     for (f = 0; f < numFaces; ++f) {
442       PetscInt size;
443 
444       ierr = DMPlexGetSupportSize(dm, faces[f], &size);CHKERRQ(ierr);
445       if (size != 1) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "DM has boundary face %d with %d support cells", faces[f], size);
446       ierr = DMPlexSetSupportSize(gdm, faces[f] + *numGhostCells, 2);CHKERRQ(ierr);
447     }
448     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
449     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
450   }
451   /* Step 4: Setup ghosted DM */
452   ierr = DMSetUp(gdm);CHKERRQ(ierr);
453   ierr = DMPlexShiftPoints_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
454   /* Step 6: Set cones and supports for new points */
455   ghostCell = cEnd;
456   for (fs = 0; fs < numFS; ++fs) {
457     IS              faceIS;
458     const PetscInt *faces;
459     PetscInt        numFaces, f;
460 
461     ierr = DMLabelGetStratumIS(label, values[fs], &faceIS);CHKERRQ(ierr);
462     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
463     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
464     for (f = 0; f < numFaces; ++f, ++ghostCell) {
465       PetscInt newFace = faces[f] + *numGhostCells;
466 
467       ierr = DMPlexSetCone(gdm, ghostCell, &newFace);CHKERRQ(ierr);
468       ierr = DMPlexInsertSupport(gdm, newFace, 1, ghostCell);CHKERRQ(ierr);
469     }
470     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
471     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
472   }
473   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
474   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
475   /* Step 7: Stratify */
476   ierr = DMPlexStratify(gdm);CHKERRQ(ierr);
477   ierr = DMPlexShiftCoordinates_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
478   ierr = DMPlexShiftSF_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
479   ierr = DMPlexShiftLabels_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
480   ierr = PetscFree(depthShift);CHKERRQ(ierr);
481   PetscFunctionReturn(0);
482 }
483 
484 #undef __FUNCT__
485 #define __FUNCT__ "DMPlexConstructGhostCells"
486 /*@C
487   DMPlexConstructGhostCells - Construct ghost cells which connect to every boundary face
488 
489   Collective on dm
490 
491   Input Parameters:
492 + dm - The original DM
493 - labelName - The label specifying the boundary faces, or "Face Sets" if this is NULL
494 
495   Output Parameters:
496 + numGhostCells - The number of ghost cells added to the DM
497 - dmGhosted - The new DM
498 
499   Note: If no label exists of that name, one will be created marking all boundary faces
500 
501   Level: developer
502 
503 .seealso: DMCreate()
504 */
505 PetscErrorCode DMPlexConstructGhostCells(DM dm, const char labelName[], PetscInt *numGhostCells, DM *dmGhosted)
506 {
507   DM             gdm;
508   DMLabel        label;
509   const char    *name = labelName ? labelName : "Face Sets";
510   PetscInt       dim;
511   PetscErrorCode ierr;
512 
513   PetscFunctionBegin;
514   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
515   PetscValidPointer(numGhostCells, 3);
516   PetscValidPointer(dmGhosted, 4);
517   ierr = DMCreate(PetscObjectComm((PetscObject)dm), &gdm);CHKERRQ(ierr);
518   ierr = DMSetType(gdm, DMPLEX);CHKERRQ(ierr);
519   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
520   ierr = DMPlexSetDimension(gdm, dim);CHKERRQ(ierr);
521   ierr = DMPlexGetLabel(dm, name, &label);CHKERRQ(ierr);
522   if (!label) {
523     /* Get label for boundary faces */
524     ierr = DMPlexCreateLabel(dm, name);CHKERRQ(ierr);
525     ierr = DMPlexGetLabel(dm, name, &label);CHKERRQ(ierr);
526     ierr = DMPlexMarkBoundaryFaces(dm, label);CHKERRQ(ierr);
527   }
528   ierr = DMPlexConstructGhostCells_Internal(dm, label, numGhostCells, gdm);CHKERRQ(ierr);
529   ierr = DMSetFromOptions(gdm);CHKERRQ(ierr);
530   *dmGhosted = gdm;
531   PetscFunctionReturn(0);
532 }
533 
534 #undef __FUNCT__
535 #define __FUNCT__ "DMPlexConstructCohesiveCells_Internal"
536 /*
537   We are adding two kinds of points here:
538     Replicated: Copies of points which exist in the mesh, such as vertices identified across a fault
539     Hybrid:     Entirely new points, such as cohesive cells
540 */
541 static PetscErrorCode DMPlexConstructCohesiveCells_Internal(DM dm, DMLabel label, DM sdm)
542 {
543   MPI_Comm         comm;
544   IS               valueIS;
545   PetscInt         numSP = 0;       /* The number of depths for which we have replicated points */
546   const PetscInt  *values;          /* List of depths for which we have replicated points */
547   IS              *pointIS;
548   PetscInt        *numSplitPoints;  /* The number of replicated points at each depth */
549   PetscInt        *numHybridPoints; /* The number of hybrid points at each depth */
550   const PetscInt **splitPoints;     /* Replicated points for each depth */
551   PetscSection     coordSection;
552   Vec              coordinates;
553   PetscScalar     *coords;
554   PetscInt         depths[4];       /* Depths in the order that plex points are numbered */
555   PetscInt        *depthShift;      /* Number of replicated+hybrid points at each depth */
556   PetscInt        *depthOffset;     /* Prefix sums of depthShift */
557   PetscInt        *pMaxNew;         /* The first replicated point at each depth in the new mesh, hybrids come after this */
558   PetscInt        *coneNew, *coneONew, *supportNew;
559   PetscInt         shift = 100, depth = 0, dep, dim, d, sp, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, numLabels, vStart, vEnd, pEnd, p, v;
560   PetscErrorCode   ierr;
561 
562   PetscFunctionBegin;
563   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
564   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
565   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
566   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
567   depths[0] = depth;
568   depths[1] = 0;
569   depths[2] = depth-1;
570   depths[3] = 1;
571   /* Count split points and add cohesive cells */
572   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
573   ierr = PetscMalloc3(depth+1,PetscInt,&depthShift,depth+1,PetscInt,&depthOffset,depth+1,PetscInt,&pMaxNew);CHKERRQ(ierr);
574   ierr = PetscMalloc4(depth+1,IS,&pointIS,depth+1,PetscInt,&numSplitPoints,depth+1,PetscInt,&numHybridPoints,depth+1,const PetscInt*,&splitPoints);CHKERRQ(ierr);
575   ierr = PetscMemzero(depthShift,  (depth+1) * sizeof(PetscInt));CHKERRQ(ierr);
576   ierr = PetscMemzero(depthOffset, (depth+1) * sizeof(PetscInt));CHKERRQ(ierr);
577   for (d = 0; d <= depth; ++d) {
578     ierr               = DMPlexGetDepthStratum(dm, d, NULL, &pMaxNew[d]);CHKERRQ(ierr);
579     numSplitPoints[d]  = 0;
580     numHybridPoints[d] = 0;
581     splitPoints[d]     = NULL;
582     pointIS[d]         = NULL;
583   }
584   if (label) {
585     ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
586     ierr = ISGetLocalSize(valueIS, &numSP);CHKERRQ(ierr);
587     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
588   }
589   for (sp = 0; sp < numSP; ++sp) {
590     const PetscInt dep = values[sp];
591 
592     if ((dep < 0) || (dep > depth)) continue;
593     ierr = DMLabelGetStratumIS(label, dep, &pointIS[dep]);CHKERRQ(ierr);
594     if (pointIS[dep]) {
595       ierr = ISGetLocalSize(pointIS[dep], &numSplitPoints[dep]);CHKERRQ(ierr);
596       ierr = ISGetIndices(pointIS[dep], &splitPoints[dep]);CHKERRQ(ierr);
597     }
598   }
599   /* Calculate number of hybrid points */
600   for (d = 1; d <= depth; ++d) numHybridPoints[d]     = numSplitPoints[d-1]; /* There is a hybrid cell/face/edge for every split face/edge/vertex   */
601   for (d = 0; d <= depth; ++d) depthShift[d]          = numSplitPoints[d] + numHybridPoints[d];
602   for (d = 1; d <= depth; ++d) depthOffset[depths[d]] = depthOffset[depths[d-1]] + depthShift[depths[d-1]];
603   for (d = 0; d <= depth; ++d) pMaxNew[d]            += depthOffset[d];
604   ierr = DMPlexShiftSizes_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
605   /* Step 3: Set cone/support sizes for new points */
606   for (dep = 0; dep <= depth; ++dep) {
607     for (p = 0; p < numSplitPoints[dep]; ++p) {
608       const PetscInt  oldp   = splitPoints[dep][p];
609       const PetscInt  newp   = oldp + depthOffset[dep];
610       const PetscInt  splitp = p    + pMaxNew[dep];
611       const PetscInt *support;
612       PetscInt        coneSize, supportSize, qf, qn, qp, e;
613 
614       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
615       ierr = DMPlexSetConeSize(sdm, splitp, coneSize);CHKERRQ(ierr);
616       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
617       ierr = DMPlexSetSupportSize(sdm, splitp, supportSize);CHKERRQ(ierr);
618       if (dep == depth-1) {
619         const PetscInt hybcell = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
620 
621         /* Add cohesive cells, they are prisms */
622         ierr = DMPlexSetConeSize(sdm, hybcell, 2 + coneSize);CHKERRQ(ierr);
623       } else if (dep == 0) {
624         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
625 
626         ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
627         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
628           PetscInt val;
629 
630           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
631           if (val == 1) ++qf;
632           if ((val == 1) || (val ==  (shift + 1))) ++qn;
633           if ((val == 1) || (val == -(shift + 1))) ++qp;
634         }
635         /* Split old vertex: Edges into original vertex and new cohesive edge */
636         ierr = DMPlexSetSupportSize(sdm, newp, qn+1);CHKERRQ(ierr);
637         /* Split new vertex: Edges into split vertex and new cohesive edge */
638         ierr = DMPlexSetSupportSize(sdm, splitp, qp+1);CHKERRQ(ierr);
639         /* Add hybrid edge */
640         ierr = DMPlexSetConeSize(sdm, hybedge, 2);CHKERRQ(ierr);
641         ierr = DMPlexSetSupportSize(sdm, hybedge, qf);CHKERRQ(ierr);
642       } else if (dep == dim-2) {
643         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
644 
645         ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
646         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
647           PetscInt val;
648 
649           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
650           if (val == dim-1) ++qf;
651           if ((val == dim-1) || (val ==  (shift + dim-1))) ++qn;
652           if ((val == dim-1) || (val == -(shift + dim-1))) ++qp;
653         }
654         /* Split old edge: Faces into original edge and cohesive face (positive side?) */
655         ierr = DMPlexSetSupportSize(sdm, newp, qn+1);CHKERRQ(ierr);
656         /* Split new edge: Faces into split edge and cohesive face (negative side?) */
657         ierr = DMPlexSetSupportSize(sdm, splitp, qp+1);CHKERRQ(ierr);
658         /* Add hybrid face */
659         ierr = DMPlexSetConeSize(sdm, hybface, 4);CHKERRQ(ierr);
660         ierr = DMPlexSetSupportSize(sdm, hybface, qf);CHKERRQ(ierr);
661       }
662     }
663   }
664   /* Step 4: Setup split DM */
665   ierr = DMSetUp(sdm);CHKERRQ(ierr);
666   ierr = DMPlexShiftPoints_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
667   ierr = DMPlexGetMaxSizes(sdm, &maxConeSizeNew, &maxSupportSizeNew);CHKERRQ(ierr);
668   ierr = PetscMalloc3(PetscMax(maxConeSize, maxConeSizeNew)*3,PetscInt,&coneNew,PetscMax(maxConeSize, maxConeSizeNew)*3,PetscInt,&coneONew,PetscMax(maxSupportSize, maxSupportSizeNew),PetscInt,&supportNew);CHKERRQ(ierr);
669   /* Step 6: Set cones and supports for new points */
670   for (dep = 0; dep <= depth; ++dep) {
671     for (p = 0; p < numSplitPoints[dep]; ++p) {
672       const PetscInt  oldp   = splitPoints[dep][p];
673       const PetscInt  newp   = oldp + depthOffset[dep];
674       const PetscInt  splitp = p    + pMaxNew[dep];
675       const PetscInt *cone, *support, *ornt;
676       PetscInt        coneSize, supportSize, q, qf, qn, qp, v, e, s;
677 
678       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
679       ierr = DMPlexGetCone(dm, oldp, &cone);CHKERRQ(ierr);
680       ierr = DMPlexGetConeOrientation(dm, oldp, &ornt);CHKERRQ(ierr);
681       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
682       ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
683       if (dep == depth-1) {
684         const PetscInt  hybcell = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
685         const PetscInt *supportF;
686 
687         /* Split face:       copy in old face to new face to start */
688         ierr = DMPlexGetSupport(sdm, newp,  &supportF);CHKERRQ(ierr);
689         ierr = DMPlexSetSupport(sdm, splitp, supportF);CHKERRQ(ierr);
690         /* Split old face:   old vertices/edges in cone so no change */
691         /* Split new face:   new vertices/edges in cone */
692         for (q = 0; q < coneSize; ++q) {
693           ierr = PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);CHKERRQ(ierr);
694 
695           coneNew[2+q] = v + pMaxNew[dep-1];
696         }
697         ierr = DMPlexSetCone(sdm, splitp, &coneNew[2]);CHKERRQ(ierr);
698         ierr = DMPlexSetConeOrientation(sdm, splitp, ornt);CHKERRQ(ierr);
699         /* Face support */
700         for (s = 0; s < supportSize; ++s) {
701           PetscInt val;
702 
703           ierr = DMLabelGetValue(label, support[s], &val);CHKERRQ(ierr);
704           if (val < 0) {
705             /* Split old face:   Replace negative side cell with cohesive cell */
706              ierr = DMPlexInsertSupport(sdm, newp, s, hybcell);CHKERRQ(ierr);
707           } else {
708             /* Split new face:   Replace positive side cell with cohesive cell */
709             ierr = DMPlexInsertSupport(sdm, splitp, s, hybcell);CHKERRQ(ierr);
710             /* Get orientation for cohesive face */
711             {
712               const PetscInt *ncone, *nconeO;
713               PetscInt        nconeSize, nc;
714 
715               ierr = DMPlexGetConeSize(dm, support[s], &nconeSize);CHKERRQ(ierr);
716               ierr = DMPlexGetCone(dm, support[s], &ncone);CHKERRQ(ierr);
717               ierr = DMPlexGetConeOrientation(dm, support[s], &nconeO);CHKERRQ(ierr);
718               for (nc = 0; nc < nconeSize; ++nc) {
719                 if (ncone[nc] == oldp) {
720                   coneONew[0] = nconeO[nc];
721                   break;
722                 }
723               }
724               if (nc >= nconeSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate face %d in neighboring cell %d", oldp, support[s]);
725             }
726           }
727         }
728         /* Cohesive cell:    Old and new split face, then new cohesive faces */
729         coneNew[0]  = newp;   /* Extracted negative side orientation above */
730         coneNew[1]  = splitp;
731         coneONew[1] = coneONew[0];
732         for (q = 0; q < coneSize; ++q) {
733           coneNew[2+q] += (pMaxNew[dep] - pMaxNew[dep-1]) + numSplitPoints[dep];
734           coneONew[2+q] = 0;
735         }
736         ierr = DMPlexSetCone(sdm, hybcell, coneNew);CHKERRQ(ierr);
737         ierr = DMPlexSetConeOrientation(sdm, hybcell, coneONew);CHKERRQ(ierr);
738       } else if (dep == 0) {
739         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
740 
741         /* Split old vertex: Edges in old split faces and new cohesive edge */
742         for (e = 0, qn = 0; e < supportSize; ++e) {
743           PetscInt val;
744 
745           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
746           if ((val == 1) || (val == (shift + 1))) {
747             supportNew[qn++] = support[e] + depthOffset[dep+1];
748           }
749         }
750         supportNew[qn] = hybedge;
751         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
752         /* Split new vertex: Edges in new split faces and new cohesive edge */
753         for (e = 0, qp = 0; e < supportSize; ++e) {
754           PetscInt val, edge;
755 
756           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
757           if (val == 1) {
758             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);CHKERRQ(ierr);
759             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
760             supportNew[qp++] = edge + pMaxNew[dep+1];
761           } else if (val == -(shift + 1)) {
762             supportNew[qp++] = support[e] + depthOffset[dep+1];
763           }
764         }
765         supportNew[qp] = hybedge;
766         ierr = DMPlexSetSupport(sdm, splitp, supportNew);CHKERRQ(ierr);
767         /* Hybrid edge:    Old and new split vertex */
768         coneNew[0] = newp;
769         coneNew[1] = splitp;
770         ierr = DMPlexSetCone(sdm, hybedge, coneNew);CHKERRQ(ierr);
771         for (e = 0, qf = 0; e < supportSize; ++e) {
772           PetscInt val, edge;
773 
774           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
775           if (val == 1) {
776             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);CHKERRQ(ierr);
777             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
778             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2];
779           }
780         }
781         ierr = DMPlexSetSupport(sdm, hybedge, supportNew);CHKERRQ(ierr);
782       } else if (dep == dim-2) {
783         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
784 
785         /* Split old edge:   old vertices in cone so no change */
786         /* Split new edge:   new vertices in cone */
787         for (q = 0; q < coneSize; ++q) {
788           ierr = PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);CHKERRQ(ierr);
789 
790           coneNew[q] = v + pMaxNew[dep-1];
791         }
792         ierr = DMPlexSetCone(sdm, splitp, coneNew);CHKERRQ(ierr);
793         /* Split old edge: Faces in positive side cells and old split faces */
794         for (e = 0, q = 0; e < supportSize; ++e) {
795           PetscInt val;
796 
797           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
798           if (val == dim-1) {
799             supportNew[q++] = support[e] + depthOffset[dep+1];
800           } else if (val == (shift + dim-1)) {
801             supportNew[q++] = support[e] + depthOffset[dep+1];
802           }
803         }
804         supportNew[q++] = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
805         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
806         /* Split new edge: Faces in negative side cells and new split faces */
807         for (e = 0, q = 0; e < supportSize; ++e) {
808           PetscInt val, face;
809 
810           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
811           if (val == dim-1) {
812             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
813             if (face < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[e]);
814             supportNew[q++] = face + pMaxNew[dep+1];
815           } else if (val == -(shift + dim-1)) {
816             supportNew[q++] = support[e] + depthOffset[dep+1];
817           }
818         }
819         supportNew[q++] = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
820         ierr = DMPlexSetSupport(sdm, splitp, supportNew);CHKERRQ(ierr);
821         /* Hybrid face */
822         coneNew[0] = newp;
823         coneNew[1] = splitp;
824         for (v = 0; v < coneSize; ++v) {
825           PetscInt vertex;
826           ierr = PetscFindInt(cone[v], numSplitPoints[dep-1], splitPoints[dep-1], &vertex);CHKERRQ(ierr);
827           if (vertex < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Vertex %d is not a split vertex", support[e]);
828           coneNew[2+v] = vertex + pMaxNew[dep] + numSplitPoints[dep];
829         }
830         ierr = DMPlexSetCone(sdm, hybface, coneNew);CHKERRQ(ierr);
831         for (e = 0, qf = 0; e < supportSize; ++e) {
832           PetscInt val, face;
833 
834           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
835           if (val == dim-1) {
836             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
837             if (face < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[e]);
838             supportNew[qf++] = face + pMaxNew[dep+2] + numSplitPoints[dep+2];
839           }
840         }
841         ierr = DMPlexSetSupport(sdm, hybface, supportNew);CHKERRQ(ierr);
842       }
843     }
844   }
845   /* Step 6b: Replace split points in negative side cones */
846   for (sp = 0; sp < numSP; ++sp) {
847     PetscInt        dep = values[sp];
848     IS              pIS;
849     PetscInt        numPoints;
850     const PetscInt *points;
851 
852     if (dep >= 0) continue;
853     ierr = DMLabelGetStratumIS(label, dep, &pIS);CHKERRQ(ierr);
854     if (!pIS) continue;
855     dep  = -dep - shift;
856     ierr = ISGetLocalSize(pIS, &numPoints);CHKERRQ(ierr);
857     ierr = ISGetIndices(pIS, &points);CHKERRQ(ierr);
858     for (p = 0; p < numPoints; ++p) {
859       const PetscInt  oldp = points[p];
860       const PetscInt  newp = depthOffset[dep] + oldp;
861       const PetscInt *cone;
862       PetscInt        coneSize, c;
863       /* PetscBool       replaced = PETSC_FALSE; */
864 
865       /* Negative edge: replace split vertex */
866       /* Negative cell: replace split face */
867       ierr = DMPlexGetConeSize(sdm, newp, &coneSize);CHKERRQ(ierr);
868       ierr = DMPlexGetCone(sdm, newp, &cone);CHKERRQ(ierr);
869       for (c = 0; c < coneSize; ++c) {
870         const PetscInt coldp = cone[c] - depthOffset[dep-1];
871         PetscInt       csplitp, cp, val;
872 
873         ierr = DMLabelGetValue(label, coldp, &val);CHKERRQ(ierr);
874         if (val == dep-1) {
875           ierr = PetscFindInt(coldp, numSplitPoints[dep-1], splitPoints[dep-1], &cp);CHKERRQ(ierr);
876           if (cp < 0) SETERRQ2(comm, PETSC_ERR_ARG_WRONG, "Point %d is not a split point of dimension %d", oldp, dep-1);
877           csplitp  = pMaxNew[dep-1] + cp;
878           ierr     = DMPlexInsertCone(sdm, newp, c, csplitp);CHKERRQ(ierr);
879           /* replaced = PETSC_TRUE; */
880         }
881       }
882       /* Cells with only a vertex or edge on the submesh have no replacement */
883       /* if (!replaced) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "The cone of point %d does not contain split points", oldp); */
884     }
885     ierr = ISRestoreIndices(pIS, &points);CHKERRQ(ierr);
886     ierr = ISDestroy(&pIS);CHKERRQ(ierr);
887   }
888   /* Step 7: Stratify */
889   ierr = DMPlexStratify(sdm);CHKERRQ(ierr);
890   /* Step 8: Coordinates */
891   ierr = DMPlexShiftCoordinates_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
892   ierr = DMPlexGetCoordinateSection(sdm, &coordSection);CHKERRQ(ierr);
893   ierr = DMGetCoordinatesLocal(sdm, &coordinates);CHKERRQ(ierr);
894   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
895   for (v = 0; v < (numSplitPoints ? numSplitPoints[0] : 0); ++v) {
896     const PetscInt newp   = depthOffset[0] + splitPoints[0][v];
897     const PetscInt splitp = pMaxNew[0] + v;
898     PetscInt       dof, off, soff, d;
899 
900     ierr = PetscSectionGetDof(coordSection, newp, &dof);CHKERRQ(ierr);
901     ierr = PetscSectionGetOffset(coordSection, newp, &off);CHKERRQ(ierr);
902     ierr = PetscSectionGetOffset(coordSection, splitp, &soff);CHKERRQ(ierr);
903     for (d = 0; d < dof; ++d) coords[soff+d] = coords[off+d];
904   }
905   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
906   /* Step 9: SF, if I can figure this out we can split the mesh in parallel */
907   ierr = DMPlexShiftSF_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
908   /* Step 10: Labels */
909   ierr = DMPlexShiftLabels_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
910   ierr = DMPlexGetNumLabels(sdm, &numLabels);CHKERRQ(ierr);
911   for (dep = 0; dep <= depth; ++dep) {
912     for (p = 0; p < numSplitPoints[dep]; ++p) {
913       const PetscInt newp   = depthOffset[dep] + splitPoints[dep][p];
914       const PetscInt splitp = pMaxNew[dep] + p;
915       PetscInt       l;
916 
917       for (l = 0; l < numLabels; ++l) {
918         DMLabel     mlabel;
919         const char *lname;
920         PetscInt    val;
921         PetscBool   isDepth;
922 
923         ierr = DMPlexGetLabelName(sdm, l, &lname);CHKERRQ(ierr);
924         ierr = PetscStrcmp(lname, "depth", &isDepth);CHKERRQ(ierr);
925         if (isDepth) continue;
926         ierr = DMPlexGetLabel(sdm, lname, &mlabel);CHKERRQ(ierr);
927         ierr = DMLabelGetValue(mlabel, newp, &val);CHKERRQ(ierr);
928         if (val >= 0) {
929           ierr = DMLabelSetValue(mlabel, splitp, val);CHKERRQ(ierr);
930 #if 0
931           /* Do not put cohesive edges into the label */
932           if (dep == 0) {
933             const PetscInt cedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
934             ierr = DMLabelSetValue(mlabel, cedge, val);CHKERRQ(ierr);
935           } else if (dep == dim-2) {
936             const PetscInt cface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
937             ierr = DMLabelSetValue(mlabel, cface, val);CHKERRQ(ierr);
938           }
939           /* Do not put cohesive faces into the label */
940 #endif
941         }
942       }
943     }
944   }
945   for (sp = 0; sp < numSP; ++sp) {
946     const PetscInt dep = values[sp];
947 
948     if ((dep < 0) || (dep > depth)) continue;
949     if (pointIS[dep]) {ierr = ISRestoreIndices(pointIS[dep], &splitPoints[dep]);CHKERRQ(ierr);}
950     ierr = ISDestroy(&pointIS[dep]);CHKERRQ(ierr);
951   }
952   if (label) {
953     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
954     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
955   }
956   for (d = 0; d <= depth; ++d) {
957     ierr = DMPlexGetDepthStratum(sdm, d, NULL, &pEnd);CHKERRQ(ierr);
958     pMaxNew[d] = pEnd - numHybridPoints[d];
959   }
960   ierr = DMPlexSetHybridBounds(sdm, depth >= 0 ? pMaxNew[depth] : PETSC_DETERMINE, depth>1 ? pMaxNew[depth-1] : PETSC_DETERMINE, depth>2 ? pMaxNew[1] : PETSC_DETERMINE, depth >= 0 ? pMaxNew[0] : PETSC_DETERMINE);CHKERRQ(ierr);
961   ierr = PetscFree3(depthShift, depthOffset, pMaxNew);CHKERRQ(ierr);
962   ierr = PetscFree3(coneNew, coneONew, supportNew);CHKERRQ(ierr);
963   ierr = PetscFree4(pointIS, numSplitPoints, numHybridPoints, splitPoints);CHKERRQ(ierr);
964   PetscFunctionReturn(0);
965 }
966 
967 #undef __FUNCT__
968 #define __FUNCT__ "DMPlexConstructCohesiveCells"
969 /*@C
970   DMPlexConstructCohesiveCells - Construct cohesive cells which split the face along an internal interface
971 
972   Collective on dm
973 
974   Input Parameters:
975 + dm - The original DM
976 - label - The label specifying the boundary faces (this could be auto-generated)
977 
978   Output Parameters:
979 - dmSplit - The new DM
980 
981   Level: developer
982 
983 .seealso: DMCreate(), DMPlexLabelCohesiveComplete()
984 @*/
985 PetscErrorCode DMPlexConstructCohesiveCells(DM dm, DMLabel label, DM *dmSplit)
986 {
987   DM             sdm;
988   PetscInt       dim;
989   PetscErrorCode ierr;
990 
991   PetscFunctionBegin;
992   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
993   PetscValidPointer(dmSplit, 3);
994   ierr = DMCreate(PetscObjectComm((PetscObject)dm), &sdm);CHKERRQ(ierr);
995   ierr = DMSetType(sdm, DMPLEX);CHKERRQ(ierr);
996   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
997   ierr = DMPlexSetDimension(sdm, dim);CHKERRQ(ierr);
998   switch (dim) {
999   case 2:
1000   case 3:
1001     ierr = DMPlexConstructCohesiveCells_Internal(dm, label, sdm);CHKERRQ(ierr);
1002     break;
1003   default:
1004     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct cohesive cells for dimension %d", dim);
1005   }
1006   *dmSplit = sdm;
1007   PetscFunctionReturn(0);
1008 }
1009 
1010 #undef __FUNCT__
1011 #define __FUNCT__ "DMPlexLabelCohesiveComplete"
1012 /*@
1013   DMPlexLabelCohesiveComplete - Starting with a label marking vertices on an internal surface, we add all other mesh pieces
1014   to complete the surface
1015 
1016   Input Parameters:
1017 + dm - The DM
1018 . label - A DMLabel marking the surface vertices
1019 . flip  - Flag to flip the submesh normal and replace points on the other side
1020 - subdm - The subDM associated with the label, or NULL
1021 
1022   Output Parameter:
1023 . label - A DMLabel marking all surface points
1024 
1025   Level: developer
1026 
1027 .seealso: DMPlexConstructCohesiveCells(), DMPlexLabelComplete()
1028 @*/
1029 PetscErrorCode DMPlexLabelCohesiveComplete(DM dm, DMLabel label, PetscBool flip, DM subdm)
1030 {
1031   DMLabel         depthLabel;
1032   IS              dimIS, subpointIS;
1033   const PetscInt *points, *subpoints;
1034   const PetscInt  rev   = flip ? -1 : 1;
1035   PetscInt        shift = 100, dim, dep, cStart, cEnd, numPoints, numSubpoints, p, val;
1036   PetscErrorCode  ierr;
1037 
1038   PetscFunctionBegin;
1039   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
1040   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
1041   if (subdm) {
1042     ierr = DMPlexCreateSubpointIS(subdm, &subpointIS);CHKERRQ(ierr);
1043     if (subpointIS) {
1044       ierr = ISGetLocalSize(subpointIS, &numSubpoints);CHKERRQ(ierr);
1045       ierr = ISGetIndices(subpointIS, &subpoints);CHKERRQ(ierr);
1046     }
1047   }
1048   /* Mark cell on the fault, and its faces which touch the fault: cell orientation for face gives the side of the fault */
1049   ierr = DMLabelGetStratumIS(label, dim-1, &dimIS);CHKERRQ(ierr);
1050   if (!dimIS) PetscFunctionReturn(0);
1051   ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
1052   ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
1053   for (p = 0; p < numPoints; ++p) { /* Loop over fault faces */
1054     const PetscInt *support;
1055     PetscInt        supportSize, s;
1056 
1057     ierr = DMPlexGetSupportSize(dm, points[p], &supportSize);CHKERRQ(ierr);
1058     if (supportSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Split face %d has %d != 2 supports", points[p], supportSize);
1059     ierr = DMPlexGetSupport(dm, points[p], &support);CHKERRQ(ierr);
1060     for (s = 0; s < supportSize; ++s) {
1061       const PetscInt *cone, *ornt;
1062       PetscInt        coneSize, c;
1063       PetscBool       pos = PETSC_TRUE;
1064 
1065       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
1066       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
1067       ierr = DMPlexGetConeOrientation(dm, support[s], &ornt);CHKERRQ(ierr);
1068       for (c = 0; c < coneSize; ++c) {
1069         if (cone[c] == points[p]) {
1070           PetscInt o = ornt[c];
1071 
1072           if (subdm) {
1073             const PetscInt *subcone, *subornt;
1074             PetscInt        subpoint, subface, subconeSize, sc;
1075 
1076             ierr = PetscFindInt(support[s], numSubpoints, subpoints, &subpoint);CHKERRQ(ierr);
1077             ierr = PetscFindInt(points[p],  numSubpoints, subpoints, &subface);CHKERRQ(ierr);
1078             ierr = DMPlexGetConeSize(subdm, subpoint, &subconeSize);CHKERRQ(ierr);
1079             ierr = DMPlexGetCone(subdm, subpoint, &subcone);CHKERRQ(ierr);
1080             ierr = DMPlexGetConeOrientation(subdm, subpoint, &subornt);CHKERRQ(ierr);
1081             for (sc = 0; sc < subconeSize; ++sc) {
1082               if (subcone[sc] == subface) {
1083                 o = subornt[0];
1084                 break;
1085               }
1086             }
1087             if (sc >= subconeSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find point %d in cone for subpoint %d", points[p], subpoint);
1088           }
1089           if (o >= 0) {
1090             ierr = DMLabelSetValue(label, support[s],  rev*(shift+dim));CHKERRQ(ierr);
1091             pos  = rev > 0 ? PETSC_TRUE : PETSC_FALSE;
1092           } else {
1093             ierr = DMLabelSetValue(label, support[s], -rev*(shift+dim));CHKERRQ(ierr);
1094             pos  = rev > 0 ? PETSC_FALSE : PETSC_TRUE;
1095           }
1096           break;
1097         }
1098       }
1099       if (c == coneSize) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Cell split face %d support does not have it in the cone", points[p]);
1100       /* Put faces touching the fault in the label */
1101       for (c = 0; c < coneSize; ++c) {
1102         const PetscInt point = cone[c];
1103 
1104         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1105         if (val == -1) {
1106           PetscInt *closure = NULL;
1107           PetscInt  closureSize, cl;
1108 
1109           ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1110           for (cl = 0; cl < closureSize*2; cl += 2) {
1111             const PetscInt clp = closure[cl];
1112 
1113             ierr = DMLabelGetValue(label, clp, &val);CHKERRQ(ierr);
1114             if ((val >= 0) && (val < dim-1)) {
1115               ierr = DMLabelSetValue(label, point, pos == PETSC_TRUE ? shift+dim-1 : -(shift+dim-1));CHKERRQ(ierr);
1116               break;
1117             }
1118           }
1119           ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1120         }
1121       }
1122     }
1123   }
1124   if (subdm) {
1125     if (subpointIS) {ierr = ISRestoreIndices(subpointIS, &subpoints);CHKERRQ(ierr);}
1126     ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
1127   }
1128   ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
1129   ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
1130   /* Search for other cells/faces/edges connected to the fault by a vertex */
1131   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1132   ierr = DMLabelGetStratumIS(label, 0, &dimIS);CHKERRQ(ierr);
1133   if (!dimIS) PetscFunctionReturn(0);
1134   ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
1135   ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
1136   for (p = 0; p < numPoints; ++p) { /* Loop over fault vertices */
1137     PetscInt *star = NULL;
1138     PetscInt  starSize, s;
1139     PetscInt  again = 1;  /* 0: Finished 1: Keep iterating after a change 2: No change */
1140 
1141     /* All points connected to the fault are inside a cell, so at the top level we will only check cells */
1142     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
1143     while (again) {
1144       if (again > 1) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Could not classify all cells connected to the fault");
1145       again = 0;
1146       for (s = 0; s < starSize*2; s += 2) {
1147         const PetscInt  point = star[s];
1148         const PetscInt *cone;
1149         PetscInt        coneSize, c;
1150 
1151         if ((point < cStart) || (point >= cEnd)) continue;
1152         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1153         if (val != -1) continue;
1154         again = again == 1 ? 1 : 2;
1155         ierr  = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
1156         ierr  = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
1157         for (c = 0; c < coneSize; ++c) {
1158           ierr = DMLabelGetValue(label, cone[c], &val);CHKERRQ(ierr);
1159           if (val != -1) {
1160             const PetscInt *ccone;
1161             PetscInt        cconeSize, cc, side, st;
1162 
1163             if (abs(val) < shift) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %d on cell %d has an invalid label %d", cone[c], point, val);
1164             if (val > 0) side =  1;
1165             else         side = -1;
1166             ierr = DMLabelSetValue(label, point, side*(shift+dim));CHKERRQ(ierr);
1167             /* Mark cell faces which touch the fault */
1168             ierr = DMPlexGetConeSize(dm, point, &cconeSize);CHKERRQ(ierr);
1169             ierr = DMPlexGetCone(dm, point, &ccone);CHKERRQ(ierr);
1170             for (cc = 0; cc < cconeSize; ++cc) {
1171               PetscInt *closure = NULL;
1172               PetscInt  closureSize, cl;
1173 
1174               ierr = DMPlexGetTransitiveClosure(dm, ccone[cc], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1175               for (cl = 0; cl < closureSize*2; cl += 2) {
1176                 const PetscInt clp = closure[cl];
1177 
1178                 ierr = DMLabelGetValue(label, clp, &val);CHKERRQ(ierr);
1179                 if (val == -1) continue;
1180                 ierr = DMLabelSetValue(label, ccone[cc], side*(shift+dim-1));CHKERRQ(ierr);
1181                 break;
1182               }
1183               ierr = DMPlexRestoreTransitiveClosure(dm, ccone[cc], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1184             }
1185             again = 1;
1186             break;
1187           }
1188         }
1189       }
1190     }
1191     /* Classify the rest by cell membership */
1192     for (s = 0; s < starSize*2; s += 2) {
1193       const PetscInt point = star[s];
1194 
1195       ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1196       if (val == -1) {
1197         PetscInt  *sstar = NULL;
1198         PetscInt   sstarSize, ss;
1199         PetscBool  marked = PETSC_FALSE;
1200 
1201         ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &sstarSize, &sstar);CHKERRQ(ierr);
1202         for (ss = 0; ss < sstarSize*2; ss += 2) {
1203           const PetscInt spoint = sstar[ss];
1204 
1205           if ((spoint < cStart) || (spoint >= cEnd)) continue;
1206           ierr = DMLabelGetValue(label, spoint, &val);CHKERRQ(ierr);
1207           if (val == -1) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %d in star of %d does not have a valid label", spoint, point);
1208           ierr = DMLabelGetValue(depthLabel, point, &dep);CHKERRQ(ierr);
1209           if (val > 0) {
1210             ierr = DMLabelSetValue(label, point,   shift+dep);CHKERRQ(ierr);
1211           } else {
1212             ierr = DMLabelSetValue(label, point, -(shift+dep));CHKERRQ(ierr);
1213           }
1214           marked = PETSC_TRUE;
1215           break;
1216         }
1217         ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &sstarSize, &sstar);CHKERRQ(ierr);
1218         if (!marked) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Point %d could not be classified", point);
1219       }
1220     }
1221     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
1222   }
1223   ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
1224   ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
1225   PetscFunctionReturn(0);
1226 }
1227 
1228 #undef __FUNCT__
1229 #define __FUNCT__ "DMPlexCreateHybridMesh"
1230 /*@C
1231   DMPlexCreateHybridMesh - Create a mesh with hybrid cells along an internal interface
1232 
1233   Collective on dm
1234 
1235   Input Parameters:
1236 + dm - The original DM
1237 - labelName - The label specifying the interface vertices
1238 
1239   Output Parameters:
1240 + hybridLabel - The label fully marking the interface
1241 - dmHybrid - The new DM
1242 
1243   Level: developer
1244 
1245 .seealso: DMPlexConstructCohesiveCells(), DMPlexLabelCohesiveComplete(), DMCreate()
1246 @*/
1247 PetscErrorCode DMPlexCreateHybridMesh(DM dm, DMLabel label, DMLabel *hybridLabel, DM *dmHybrid)
1248 {
1249   DM             idm;
1250   DMLabel        subpointMap, hlabel;
1251   PetscInt       dim;
1252   PetscErrorCode ierr;
1253 
1254   PetscFunctionBegin;
1255   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1256   if (hybridLabel) PetscValidPointer(hybridLabel, 3);
1257   PetscValidPointer(dmHybrid, 4);
1258   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
1259   ierr = DMPlexCreateSubmesh(dm, label, 1, &idm);CHKERRQ(ierr);
1260   ierr = DMPlexOrient(idm);CHKERRQ(ierr);
1261   ierr = DMPlexGetSubpointMap(idm, &subpointMap);CHKERRQ(ierr);
1262   ierr = DMLabelDuplicate(subpointMap, &hlabel);CHKERRQ(ierr);
1263   ierr = DMLabelClearStratum(hlabel, dim);CHKERRQ(ierr);
1264   ierr = DMPlexLabelCohesiveComplete(dm, hlabel, PETSC_FALSE, idm);CHKERRQ(ierr);
1265   ierr = DMDestroy(&idm);CHKERRQ(ierr);
1266   ierr = DMPlexConstructCohesiveCells(dm, hlabel, dmHybrid);CHKERRQ(ierr);
1267   if (hybridLabel) *hybridLabel = hlabel;
1268   else             {ierr = DMLabelDestroy(&hlabel);CHKERRQ(ierr);}
1269   PetscFunctionReturn(0);
1270 }
1271 
1272 #undef __FUNCT__
1273 #define __FUNCT__ "DMPlexMarkSubmesh_Uninterpolated"
1274 /* Here we need the explicit assumption that:
1275 
1276      For any marked cell, the marked vertices constitute a single face
1277 */
1278 static PetscErrorCode DMPlexMarkSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, DM subdm)
1279 {
1280   IS               subvertexIS = NULL;
1281   const PetscInt  *subvertices;
1282   PetscInt        *pStart, *pEnd, *pMax, pSize;
1283   PetscInt         depth, dim, d, numSubVerticesInitial = 0, v;
1284   PetscErrorCode   ierr;
1285 
1286   PetscFunctionBegin;
1287   *numFaces = 0;
1288   *nFV      = 0;
1289   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1290   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
1291   pSize = PetscMax(depth, dim) + 1;
1292   ierr = PetscMalloc3(pSize,PetscInt,&pStart,pSize,PetscInt,&pEnd,pSize,PetscInt,&pMax);CHKERRQ(ierr);
1293   ierr = DMPlexGetHybridBounds(dm, depth >= 0 ? &pMax[depth] : NULL, depth>1 ? &pMax[depth-1] : NULL, depth>2 ? &pMax[1] : NULL, &pMax[0]);CHKERRQ(ierr);
1294   for (d = 0; d <= depth; ++d) {
1295     ierr = DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);
1296     if (pMax[d] >= 0) pEnd[d] = PetscMin(pEnd[d], pMax[d]);
1297   }
1298   /* Loop over initial vertices and mark all faces in the collective star() */
1299   if (vertexLabel) {ierr = DMLabelGetStratumIS(vertexLabel, value, &subvertexIS);CHKERRQ(ierr);}
1300   if (subvertexIS) {
1301     ierr = ISGetSize(subvertexIS, &numSubVerticesInitial);CHKERRQ(ierr);
1302     ierr = ISGetIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
1303   }
1304   for (v = 0; v < numSubVerticesInitial; ++v) {
1305     const PetscInt vertex = subvertices[v];
1306     PetscInt      *star   = NULL;
1307     PetscInt       starSize, s, numCells = 0, c;
1308 
1309     ierr = DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
1310     for (s = 0; s < starSize*2; s += 2) {
1311       const PetscInt point = star[s];
1312       if ((point >= pStart[depth]) && (point < pEnd[depth])) star[numCells++] = point;
1313     }
1314     for (c = 0; c < numCells; ++c) {
1315       const PetscInt cell    = star[c];
1316       PetscInt      *closure = NULL;
1317       PetscInt       closureSize, cl;
1318       PetscInt       cellLoc, numCorners = 0, faceSize = 0;
1319 
1320       ierr = DMLabelGetValue(subpointMap, cell, &cellLoc);CHKERRQ(ierr);
1321       if (cellLoc == 2) continue;
1322       if (cellLoc >= 0) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %d has dimension %d in the surface label", cell, cellLoc);
1323       ierr = DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1324       for (cl = 0; cl < closureSize*2; cl += 2) {
1325         const PetscInt point = closure[cl];
1326         PetscInt       vertexLoc;
1327 
1328         if ((point >= pStart[0]) && (point < pEnd[0])) {
1329           ++numCorners;
1330           ierr = DMLabelGetValue(vertexLabel, point, &vertexLoc);CHKERRQ(ierr);
1331           if (vertexLoc == value) closure[faceSize++] = point;
1332         }
1333       }
1334       if (!(*nFV)) {ierr = DMPlexGetNumFaceVertices(dm, dim, numCorners, nFV);CHKERRQ(ierr);}
1335       if (faceSize > *nFV) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %d of an element on the surface", faceSize);
1336       if (faceSize == *nFV) {
1337         const PetscInt *cells = NULL;
1338         PetscInt        numCells, nc;
1339 
1340         ++(*numFaces);
1341         for (cl = 0; cl < faceSize; ++cl) {
1342           ierr = DMLabelSetValue(subpointMap, closure[cl], 0);CHKERRQ(ierr);
1343         }
1344         ierr = DMPlexGetJoin(dm, faceSize, closure, &numCells, &cells);CHKERRQ(ierr);
1345         for (nc = 0; nc < numCells; ++nc) {
1346           ierr = DMLabelSetValue(subpointMap, cells[nc], 2);CHKERRQ(ierr);
1347         }
1348         ierr = DMPlexRestoreJoin(dm, faceSize, closure, &numCells, &cells);CHKERRQ(ierr);
1349       }
1350       ierr = DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1351     }
1352     ierr = DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
1353   }
1354   if (subvertexIS) {
1355     ierr = ISRestoreIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
1356   }
1357   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
1358   ierr = PetscFree3(pStart,pEnd,pMax);CHKERRQ(ierr);
1359   PetscFunctionReturn(0);
1360 }
1361 
1362 #undef __FUNCT__
1363 #define __FUNCT__ "DMPlexMarkSubmesh_Interpolated"
1364 static PetscErrorCode DMPlexMarkSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, DM subdm)
1365 {
1366   IS               subvertexIS;
1367   const PetscInt  *subvertices;
1368   PetscInt        *pStart, *pEnd, *pMax;
1369   PetscInt         dim, d, numSubVerticesInitial = 0, v;
1370   PetscErrorCode   ierr;
1371 
1372   PetscFunctionBegin;
1373   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
1374   ierr = PetscMalloc3(dim+1,PetscInt,&pStart,dim+1,PetscInt,&pEnd,dim+1,PetscInt,&pMax);CHKERRQ(ierr);
1375   ierr = DMPlexGetHybridBounds(dm, &pMax[dim], dim>1 ? &pMax[dim-1] : NULL, dim > 2 ? &pMax[1] : NULL, &pMax[0]);CHKERRQ(ierr);
1376   for (d = 0; d <= dim; ++d) {
1377     ierr = DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);
1378     if (pMax[d] >= 0) pEnd[d] = PetscMin(pEnd[d], pMax[d]);
1379   }
1380   /* Loop over initial vertices and mark all faces in the collective star() */
1381   ierr = DMLabelGetStratumIS(vertexLabel, value, &subvertexIS);CHKERRQ(ierr);
1382   if (subvertexIS) {
1383     ierr = ISGetSize(subvertexIS, &numSubVerticesInitial);CHKERRQ(ierr);
1384     ierr = ISGetIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
1385   }
1386   for (v = 0; v < numSubVerticesInitial; ++v) {
1387     const PetscInt vertex = subvertices[v];
1388     PetscInt      *star   = NULL;
1389     PetscInt       starSize, s, numFaces = 0, f;
1390 
1391     ierr = DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
1392     for (s = 0; s < starSize*2; s += 2) {
1393       const PetscInt point = star[s];
1394       if ((point >= pStart[dim-1]) && (point < pEnd[dim-1])) star[numFaces++] = point;
1395     }
1396     for (f = 0; f < numFaces; ++f) {
1397       const PetscInt face    = star[f];
1398       PetscInt      *closure = NULL;
1399       PetscInt       closureSize, c;
1400       PetscInt       faceLoc;
1401 
1402       ierr = DMLabelGetValue(subpointMap, face, &faceLoc);CHKERRQ(ierr);
1403       if (faceLoc == dim-1) continue;
1404       if (faceLoc >= 0) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %d has dimension %d in the surface label", face, faceLoc);
1405       ierr = DMPlexGetTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1406       for (c = 0; c < closureSize*2; c += 2) {
1407         const PetscInt point = closure[c];
1408         PetscInt       vertexLoc;
1409 
1410         if ((point >= pStart[0]) && (point < pEnd[0])) {
1411           ierr = DMLabelGetValue(vertexLabel, point, &vertexLoc);CHKERRQ(ierr);
1412           if (vertexLoc != value) break;
1413         }
1414       }
1415       if (c == closureSize*2) {
1416         const PetscInt *support;
1417         PetscInt        supportSize, s;
1418 
1419         for (c = 0; c < closureSize*2; c += 2) {
1420           const PetscInt point = closure[c];
1421 
1422           for (d = 0; d < dim; ++d) {
1423             if ((point >= pStart[d]) && (point < pEnd[d])) {
1424               ierr = DMLabelSetValue(subpointMap, point, d);CHKERRQ(ierr);
1425               break;
1426             }
1427           }
1428         }
1429         ierr = DMPlexGetSupportSize(dm, face, &supportSize);CHKERRQ(ierr);
1430         ierr = DMPlexGetSupport(dm, face, &support);CHKERRQ(ierr);
1431         for (s = 0; s < supportSize; ++s) {
1432           ierr = DMLabelSetValue(subpointMap, support[s], dim);CHKERRQ(ierr);
1433         }
1434       }
1435       ierr = DMPlexRestoreTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1436     }
1437     ierr = DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
1438   }
1439   if (subvertexIS) {
1440     ierr = ISRestoreIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
1441   }
1442   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
1443   ierr = PetscFree3(pStart,pEnd,pMax);CHKERRQ(ierr);
1444   PetscFunctionReturn(0);
1445 }
1446 
1447 #undef __FUNCT__
1448 #define __FUNCT__ "DMPlexMarkCohesiveSubmesh_Uninterpolated"
1449 static PetscErrorCode DMPlexMarkCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char labelname[], PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, PetscInt *subCells[], DM subdm)
1450 {
1451   DMLabel         label = NULL;
1452   const PetscInt *cone;
1453   PetscInt        dim, cMax, cEnd, c, subc = 0, p, coneSize;
1454   PetscErrorCode  ierr;
1455 
1456   PetscFunctionBegin;
1457   *numFaces = 0;
1458   *nFV = 0;
1459   if (labelname) {ierr = DMPlexGetLabel(dm, labelname, &label);CHKERRQ(ierr);}
1460   *subCells = NULL;
1461   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
1462   ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
1463   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
1464   if (cMax < 0) PetscFunctionReturn(0);
1465   if (label) {
1466     for (c = cMax; c < cEnd; ++c) {
1467       PetscInt val;
1468 
1469       ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr);
1470       if (val == value) {
1471         ++(*numFaces);
1472         ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
1473       }
1474     }
1475   } else {
1476     *numFaces = cEnd - cMax;
1477     ierr = DMPlexGetConeSize(dm, cMax, &coneSize);CHKERRQ(ierr);
1478   }
1479   *nFV = hasLagrange ? coneSize/3 : coneSize/2;
1480   ierr = PetscMalloc(*numFaces *2 * sizeof(PetscInt), subCells);CHKERRQ(ierr);
1481   for (c = cMax; c < cEnd; ++c) {
1482     const PetscInt *cells;
1483     PetscInt        numCells;
1484 
1485     if (label) {
1486       PetscInt val;
1487 
1488       ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr);
1489       if (val != value) continue;
1490     }
1491     ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
1492     for (p = 0; p < *nFV; ++p) {
1493       ierr = DMLabelSetValue(subpointMap, cone[p], 0);CHKERRQ(ierr);
1494     }
1495     /* Negative face */
1496     ierr = DMPlexGetJoin(dm, *nFV, cone, &numCells, &cells);CHKERRQ(ierr);
1497     /* Not true in parallel
1498     if (numCells != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
1499     for (p = 0; p < numCells; ++p) {
1500       ierr = DMLabelSetValue(subpointMap, cells[p], 2);CHKERRQ(ierr);
1501       (*subCells)[subc++] = cells[p];
1502     }
1503     ierr = DMPlexRestoreJoin(dm, *nFV, cone, &numCells, &cells);CHKERRQ(ierr);
1504     /* Positive face is not included */
1505   }
1506   PetscFunctionReturn(0);
1507 }
1508 
1509 #undef __FUNCT__
1510 #define __FUNCT__ "DMPlexMarkCohesiveSubmesh_Interpolated"
1511 static PetscErrorCode DMPlexMarkCohesiveSubmesh_Interpolated(DM dm, PetscBool hasLagrange, const char labelname[], PetscInt value, DMLabel subpointMap, DM subdm)
1512 {
1513   DMLabel        label = NULL;
1514   PetscInt      *pStart, *pEnd;
1515   PetscInt       dim, cMax, cEnd, c, d;
1516   PetscErrorCode ierr;
1517 
1518   PetscFunctionBegin;
1519   if (labelname) {ierr = DMPlexGetLabel(dm, labelname, &label);CHKERRQ(ierr);}
1520   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
1521   ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
1522   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
1523   if (cMax < 0) PetscFunctionReturn(0);
1524   ierr = PetscMalloc2(dim+1,PetscInt,&pStart,dim+1,PetscInt,&pEnd);CHKERRQ(ierr);
1525   for (d = 0; d <= dim; ++d) {
1526     ierr = DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);
1527   }
1528   for (c = cMax; c < cEnd; ++c) {
1529     const PetscInt *cone;
1530     PetscInt       *closure = NULL;
1531     PetscInt        coneSize, closureSize, cl;
1532 
1533     if (label) {
1534       PetscInt val;
1535 
1536       ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr);
1537       if (val != value) continue;
1538     }
1539     ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
1540     if (hasLagrange) {
1541       if (coneSize != 3) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells");
1542     } else {
1543       if (coneSize != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells");
1544     }
1545     /* Negative face */
1546     ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
1547     ierr = DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1548     for (cl = 0; cl < closureSize*2; cl += 2) {
1549       const PetscInt point = closure[cl];
1550 
1551       for (d = 0; d <= dim; ++d) {
1552         if ((point >= pStart[d]) && (point < pEnd[d])) {
1553           ierr = DMLabelSetValue(subpointMap, point, d);CHKERRQ(ierr);
1554           break;
1555         }
1556       }
1557     }
1558     ierr = DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1559     /* Cells -- positive face is not included */
1560     for (cl = 0; cl < 1; ++cl) {
1561       const PetscInt *support;
1562       PetscInt        supportSize, s;
1563 
1564       ierr = DMPlexGetSupportSize(dm, cone[cl], &supportSize);CHKERRQ(ierr);
1565       if (supportSize != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive faces should separate two cells");
1566       ierr = DMPlexGetSupport(dm, cone[cl], &support);CHKERRQ(ierr);
1567       for (s = 0; s < supportSize; ++s) {
1568         ierr = DMLabelSetValue(subpointMap, support[s], dim);CHKERRQ(ierr);
1569       }
1570     }
1571   }
1572   ierr = PetscFree2(pStart, pEnd);CHKERRQ(ierr);
1573   PetscFunctionReturn(0);
1574 }
1575 
1576 #undef __FUNCT__
1577 #define __FUNCT__ "DMPlexGetFaceOrientation"
1578 PetscErrorCode DMPlexGetFaceOrientation(DM dm, PetscInt cell, PetscInt numCorners, PetscInt indices[], PetscInt oppositeVertex, PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
1579 {
1580   MPI_Comm       comm;
1581   PetscBool      posOrient = PETSC_FALSE;
1582   const PetscInt debug     = 0;
1583   PetscInt       cellDim, faceSize, f;
1584   PetscErrorCode ierr;
1585 
1586   PetscFunctionBegin;
1587   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
1588   ierr = DMPlexGetDimension(dm, &cellDim);CHKERRQ(ierr);
1589   if (debug) {PetscPrintf(comm, "cellDim: %d numCorners: %d\n", cellDim, numCorners);CHKERRQ(ierr);}
1590 
1591   if (cellDim == 1 && numCorners == 2) {
1592     /* Triangle */
1593     faceSize  = numCorners-1;
1594     posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
1595   } else if (cellDim == 2 && numCorners == 3) {
1596     /* Triangle */
1597     faceSize  = numCorners-1;
1598     posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
1599   } else if (cellDim == 3 && numCorners == 4) {
1600     /* Tetrahedron */
1601     faceSize  = numCorners-1;
1602     posOrient = (oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
1603   } else if (cellDim == 1 && numCorners == 3) {
1604     /* Quadratic line */
1605     faceSize  = 1;
1606     posOrient = PETSC_TRUE;
1607   } else if (cellDim == 2 && numCorners == 4) {
1608     /* Quads */
1609     faceSize = 2;
1610     if ((indices[1] > indices[0]) && (indices[1] - indices[0] == 1)) {
1611       posOrient = PETSC_TRUE;
1612     } else if ((indices[0] == 3) && (indices[1] == 0)) {
1613       posOrient = PETSC_TRUE;
1614     } else {
1615       if (((indices[0] > indices[1]) && (indices[0] - indices[1] == 1)) || ((indices[0] == 0) && (indices[1] == 3))) {
1616         posOrient = PETSC_FALSE;
1617       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossedge");
1618     }
1619   } else if (cellDim == 2 && numCorners == 6) {
1620     /* Quadratic triangle (I hate this) */
1621     /* Edges are determined by the first 2 vertices (corners of edges) */
1622     const PetscInt faceSizeTri = 3;
1623     PetscInt       sortedIndices[3], i, iFace;
1624     PetscBool      found                    = PETSC_FALSE;
1625     PetscInt       faceVerticesTriSorted[9] = {
1626       0, 3,  4, /* bottom */
1627       1, 4,  5, /* right */
1628       2, 3,  5, /* left */
1629     };
1630     PetscInt       faceVerticesTri[9] = {
1631       0, 3,  4, /* bottom */
1632       1, 4,  5, /* right */
1633       2, 5,  3, /* left */
1634     };
1635 
1636     faceSize = faceSizeTri;
1637     for (i = 0; i < faceSizeTri; ++i) sortedIndices[i] = indices[i];
1638     ierr = PetscSortInt(faceSizeTri, sortedIndices);CHKERRQ(ierr);
1639     for (iFace = 0; iFace < 3; ++iFace) {
1640       const PetscInt ii = iFace*faceSizeTri;
1641       PetscInt       fVertex, cVertex;
1642 
1643       if ((sortedIndices[0] == faceVerticesTriSorted[ii+0]) &&
1644           (sortedIndices[1] == faceVerticesTriSorted[ii+1])) {
1645         for (fVertex = 0; fVertex < faceSizeTri; ++fVertex) {
1646           for (cVertex = 0; cVertex < faceSizeTri; ++cVertex) {
1647             if (indices[cVertex] == faceVerticesTri[ii+fVertex]) {
1648               faceVertices[fVertex] = origVertices[cVertex];
1649               break;
1650             }
1651           }
1652         }
1653         found = PETSC_TRUE;
1654         break;
1655       }
1656     }
1657     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid tri crossface");
1658     if (posOriented) *posOriented = PETSC_TRUE;
1659     PetscFunctionReturn(0);
1660   } else if (cellDim == 2 && numCorners == 9) {
1661     /* Quadratic quad (I hate this) */
1662     /* Edges are determined by the first 2 vertices (corners of edges) */
1663     const PetscInt faceSizeQuad = 3;
1664     PetscInt       sortedIndices[3], i, iFace;
1665     PetscBool      found                      = PETSC_FALSE;
1666     PetscInt       faceVerticesQuadSorted[12] = {
1667       0, 1,  4, /* bottom */
1668       1, 2,  5, /* right */
1669       2, 3,  6, /* top */
1670       0, 3,  7, /* left */
1671     };
1672     PetscInt       faceVerticesQuad[12] = {
1673       0, 1,  4, /* bottom */
1674       1, 2,  5, /* right */
1675       2, 3,  6, /* top */
1676       3, 0,  7, /* left */
1677     };
1678 
1679     faceSize = faceSizeQuad;
1680     for (i = 0; i < faceSizeQuad; ++i) sortedIndices[i] = indices[i];
1681     ierr = PetscSortInt(faceSizeQuad, sortedIndices);CHKERRQ(ierr);
1682     for (iFace = 0; iFace < 4; ++iFace) {
1683       const PetscInt ii = iFace*faceSizeQuad;
1684       PetscInt       fVertex, cVertex;
1685 
1686       if ((sortedIndices[0] == faceVerticesQuadSorted[ii+0]) &&
1687           (sortedIndices[1] == faceVerticesQuadSorted[ii+1])) {
1688         for (fVertex = 0; fVertex < faceSizeQuad; ++fVertex) {
1689           for (cVertex = 0; cVertex < faceSizeQuad; ++cVertex) {
1690             if (indices[cVertex] == faceVerticesQuad[ii+fVertex]) {
1691               faceVertices[fVertex] = origVertices[cVertex];
1692               break;
1693             }
1694           }
1695         }
1696         found = PETSC_TRUE;
1697         break;
1698       }
1699     }
1700     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossface");
1701     if (posOriented) *posOriented = PETSC_TRUE;
1702     PetscFunctionReturn(0);
1703   } else if (cellDim == 3 && numCorners == 8) {
1704     /* Hexes
1705        A hex is two oriented quads with the normal of the first
1706        pointing up at the second.
1707 
1708           7---6
1709          /|  /|
1710         4---5 |
1711         | 1-|-2
1712         |/  |/
1713         0---3
1714 
1715         Faces are determined by the first 4 vertices (corners of faces) */
1716     const PetscInt faceSizeHex = 4;
1717     PetscInt       sortedIndices[4], i, iFace;
1718     PetscBool      found                     = PETSC_FALSE;
1719     PetscInt       faceVerticesHexSorted[24] = {
1720       0, 1, 2, 3,  /* bottom */
1721       4, 5, 6, 7,  /* top */
1722       0, 3, 4, 5,  /* front */
1723       2, 3, 5, 6,  /* right */
1724       1, 2, 6, 7,  /* back */
1725       0, 1, 4, 7,  /* left */
1726     };
1727     PetscInt       faceVerticesHex[24] = {
1728       1, 2, 3, 0,  /* bottom */
1729       4, 5, 6, 7,  /* top */
1730       0, 3, 5, 4,  /* front */
1731       3, 2, 6, 5,  /* right */
1732       2, 1, 7, 6,  /* back */
1733       1, 0, 4, 7,  /* left */
1734     };
1735 
1736     faceSize = faceSizeHex;
1737     for (i = 0; i < faceSizeHex; ++i) sortedIndices[i] = indices[i];
1738     ierr = PetscSortInt(faceSizeHex, sortedIndices);CHKERRQ(ierr);
1739     for (iFace = 0; iFace < 6; ++iFace) {
1740       const PetscInt ii = iFace*faceSizeHex;
1741       PetscInt       fVertex, cVertex;
1742 
1743       if ((sortedIndices[0] == faceVerticesHexSorted[ii+0]) &&
1744           (sortedIndices[1] == faceVerticesHexSorted[ii+1]) &&
1745           (sortedIndices[2] == faceVerticesHexSorted[ii+2]) &&
1746           (sortedIndices[3] == faceVerticesHexSorted[ii+3])) {
1747         for (fVertex = 0; fVertex < faceSizeHex; ++fVertex) {
1748           for (cVertex = 0; cVertex < faceSizeHex; ++cVertex) {
1749             if (indices[cVertex] == faceVerticesHex[ii+fVertex]) {
1750               faceVertices[fVertex] = origVertices[cVertex];
1751               break;
1752             }
1753           }
1754         }
1755         found = PETSC_TRUE;
1756         break;
1757       }
1758     }
1759     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
1760     if (posOriented) *posOriented = PETSC_TRUE;
1761     PetscFunctionReturn(0);
1762   } else if (cellDim == 3 && numCorners == 10) {
1763     /* Quadratic tet */
1764     /* Faces are determined by the first 3 vertices (corners of faces) */
1765     const PetscInt faceSizeTet = 6;
1766     PetscInt       sortedIndices[6], i, iFace;
1767     PetscBool      found                     = PETSC_FALSE;
1768     PetscInt       faceVerticesTetSorted[24] = {
1769       0, 1, 2,  6, 7, 8, /* bottom */
1770       0, 3, 4,  6, 7, 9,  /* front */
1771       1, 4, 5,  7, 8, 9,  /* right */
1772       2, 3, 5,  6, 8, 9,  /* left */
1773     };
1774     PetscInt       faceVerticesTet[24] = {
1775       0, 1, 2,  6, 7, 8, /* bottom */
1776       0, 4, 3,  6, 7, 9,  /* front */
1777       1, 5, 4,  7, 8, 9,  /* right */
1778       2, 3, 5,  8, 6, 9,  /* left */
1779     };
1780 
1781     faceSize = faceSizeTet;
1782     for (i = 0; i < faceSizeTet; ++i) sortedIndices[i] = indices[i];
1783     ierr = PetscSortInt(faceSizeTet, sortedIndices);CHKERRQ(ierr);
1784     for (iFace=0; iFace < 4; ++iFace) {
1785       const PetscInt ii = iFace*faceSizeTet;
1786       PetscInt       fVertex, cVertex;
1787 
1788       if ((sortedIndices[0] == faceVerticesTetSorted[ii+0]) &&
1789           (sortedIndices[1] == faceVerticesTetSorted[ii+1]) &&
1790           (sortedIndices[2] == faceVerticesTetSorted[ii+2]) &&
1791           (sortedIndices[3] == faceVerticesTetSorted[ii+3])) {
1792         for (fVertex = 0; fVertex < faceSizeTet; ++fVertex) {
1793           for (cVertex = 0; cVertex < faceSizeTet; ++cVertex) {
1794             if (indices[cVertex] == faceVerticesTet[ii+fVertex]) {
1795               faceVertices[fVertex] = origVertices[cVertex];
1796               break;
1797             }
1798           }
1799         }
1800         found = PETSC_TRUE;
1801         break;
1802       }
1803     }
1804     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid tet crossface");
1805     if (posOriented) *posOriented = PETSC_TRUE;
1806     PetscFunctionReturn(0);
1807   } else if (cellDim == 3 && numCorners == 27) {
1808     /* Quadratic hexes (I hate this)
1809        A hex is two oriented quads with the normal of the first
1810        pointing up at the second.
1811 
1812          7---6
1813         /|  /|
1814        4---5 |
1815        | 3-|-2
1816        |/  |/
1817        0---1
1818 
1819        Faces are determined by the first 4 vertices (corners of faces) */
1820     const PetscInt faceSizeQuadHex = 9;
1821     PetscInt       sortedIndices[9], i, iFace;
1822     PetscBool      found                         = PETSC_FALSE;
1823     PetscInt       faceVerticesQuadHexSorted[54] = {
1824       0, 1, 2, 3,  8, 9, 10, 11,  24, /* bottom */
1825       4, 5, 6, 7,  12, 13, 14, 15,  25, /* top */
1826       0, 1, 4, 5,  8, 12, 16, 17,  22, /* front */
1827       1, 2, 5, 6,  9, 13, 17, 18,  21, /* right */
1828       2, 3, 6, 7,  10, 14, 18, 19,  23, /* back */
1829       0, 3, 4, 7,  11, 15, 16, 19,  20, /* left */
1830     };
1831     PetscInt       faceVerticesQuadHex[54] = {
1832       3, 2, 1, 0,  10, 9, 8, 11,  24, /* bottom */
1833       4, 5, 6, 7,  12, 13, 14, 15,  25, /* top */
1834       0, 1, 5, 4,  8, 17, 12, 16,  22, /* front */
1835       1, 2, 6, 5,  9, 18, 13, 17,  21, /* right */
1836       2, 3, 7, 6,  10, 19, 14, 18,  23, /* back */
1837       3, 0, 4, 7,  11, 16, 15, 19,  20 /* left */
1838     };
1839 
1840     faceSize = faceSizeQuadHex;
1841     for (i = 0; i < faceSizeQuadHex; ++i) sortedIndices[i] = indices[i];
1842     ierr = PetscSortInt(faceSizeQuadHex, sortedIndices);CHKERRQ(ierr);
1843     for (iFace = 0; iFace < 6; ++iFace) {
1844       const PetscInt ii = iFace*faceSizeQuadHex;
1845       PetscInt       fVertex, cVertex;
1846 
1847       if ((sortedIndices[0] == faceVerticesQuadHexSorted[ii+0]) &&
1848           (sortedIndices[1] == faceVerticesQuadHexSorted[ii+1]) &&
1849           (sortedIndices[2] == faceVerticesQuadHexSorted[ii+2]) &&
1850           (sortedIndices[3] == faceVerticesQuadHexSorted[ii+3])) {
1851         for (fVertex = 0; fVertex < faceSizeQuadHex; ++fVertex) {
1852           for (cVertex = 0; cVertex < faceSizeQuadHex; ++cVertex) {
1853             if (indices[cVertex] == faceVerticesQuadHex[ii+fVertex]) {
1854               faceVertices[fVertex] = origVertices[cVertex];
1855               break;
1856             }
1857           }
1858         }
1859         found = PETSC_TRUE;
1860         break;
1861       }
1862     }
1863     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
1864     if (posOriented) *posOriented = PETSC_TRUE;
1865     PetscFunctionReturn(0);
1866   } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Unknown cell type for faceOrientation().");
1867   if (!posOrient) {
1868     if (debug) {ierr = PetscPrintf(comm, "  Reversing initial face orientation\n");CHKERRQ(ierr);}
1869     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[faceSize-1 - f];
1870   } else {
1871     if (debug) {ierr = PetscPrintf(comm, "  Keeping initial face orientation\n");CHKERRQ(ierr);}
1872     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[f];
1873   }
1874   if (posOriented) *posOriented = posOrient;
1875   PetscFunctionReturn(0);
1876 }
1877 
1878 #undef __FUNCT__
1879 #define __FUNCT__ "DMPlexGetOrientedFace"
1880 /*
1881     Given a cell and a face, as a set of vertices,
1882       return the oriented face, as a set of vertices, in faceVertices
1883     The orientation is such that the face normal points out of the cell
1884 */
1885 PetscErrorCode DMPlexGetOrientedFace(DM dm, PetscInt cell, PetscInt faceSize, const PetscInt face[], PetscInt numCorners, PetscInt indices[], PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
1886 {
1887   const PetscInt *cone = NULL;
1888   PetscInt        coneSize, v, f, v2;
1889   PetscInt        oppositeVertex = -1;
1890   PetscErrorCode  ierr;
1891 
1892   PetscFunctionBegin;
1893   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
1894   ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
1895   for (v = 0, v2 = 0; v < coneSize; ++v) {
1896     PetscBool found = PETSC_FALSE;
1897 
1898     for (f = 0; f < faceSize; ++f) {
1899       if (face[f] == cone[v]) {
1900         found = PETSC_TRUE; break;
1901       }
1902     }
1903     if (found) {
1904       indices[v2]      = v;
1905       origVertices[v2] = cone[v];
1906       ++v2;
1907     } else {
1908       oppositeVertex = v;
1909     }
1910   }
1911   ierr = DMPlexGetFaceOrientation(dm, cell, numCorners, indices, oppositeVertex, origVertices, faceVertices, posOriented);CHKERRQ(ierr);
1912   PetscFunctionReturn(0);
1913 }
1914 
1915 #undef __FUNCT__
1916 #define __FUNCT__ "DMPlexInsertFace_Internal"
1917 /*
1918   DMPlexInsertFace_Internal - Puts a face into the mesh
1919 
1920   Not collective
1921 
1922   Input Parameters:
1923   + dm              - The DMPlex
1924   . numFaceVertex   - The number of vertices in the face
1925   . faceVertices    - The vertices in the face for dm
1926   . subfaceVertices - The vertices in the face for subdm
1927   . numCorners      - The number of vertices in the cell
1928   . cell            - A cell in dm containing the face
1929   . subcell         - A cell in subdm containing the face
1930   . firstFace       - First face in the mesh
1931   - newFacePoint    - Next face in the mesh
1932 
1933   Output Parameters:
1934   . newFacePoint - Contains next face point number on input, updated on output
1935 
1936   Level: developer
1937 */
1938 static PetscErrorCode DMPlexInsertFace_Internal(DM dm, DM subdm, PetscInt numFaceVertices, const PetscInt faceVertices[], const PetscInt subfaceVertices[], PetscInt numCorners, PetscInt cell, PetscInt subcell, PetscInt firstFace, PetscInt *newFacePoint)
1939 {
1940   MPI_Comm        comm;
1941   DM_Plex        *submesh = (DM_Plex*) subdm->data;
1942   const PetscInt *faces;
1943   PetscInt        numFaces, coneSize;
1944   PetscErrorCode  ierr;
1945 
1946   PetscFunctionBegin;
1947   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
1948   ierr = DMPlexGetConeSize(subdm, subcell, &coneSize);CHKERRQ(ierr);
1949   if (coneSize != 1) SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cone size of cell %d is %d != 1", cell, coneSize);
1950 #if 0
1951   /* Cannot use this because support() has not been constructed yet */
1952   ierr = DMPlexGetJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces);CHKERRQ(ierr);
1953 #else
1954   {
1955     PetscInt f;
1956 
1957     numFaces = 0;
1958     ierr     = DMGetWorkArray(subdm, 1, PETSC_INT, (void **) &faces);CHKERRQ(ierr);
1959     for (f = firstFace; f < *newFacePoint; ++f) {
1960       PetscInt dof, off, d;
1961 
1962       ierr = PetscSectionGetDof(submesh->coneSection, f, &dof);CHKERRQ(ierr);
1963       ierr = PetscSectionGetOffset(submesh->coneSection, f, &off);CHKERRQ(ierr);
1964       /* Yes, I know this is quadratic, but I expect the sizes to be <5 */
1965       for (d = 0; d < dof; ++d) {
1966         const PetscInt p = submesh->cones[off+d];
1967         PetscInt       v;
1968 
1969         for (v = 0; v < numFaceVertices; ++v) {
1970           if (subfaceVertices[v] == p) break;
1971         }
1972         if (v == numFaceVertices) break;
1973       }
1974       if (d == dof) {
1975         numFaces               = 1;
1976         ((PetscInt*) faces)[0] = f;
1977       }
1978     }
1979   }
1980 #endif
1981   if (numFaces > 1) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Vertex set had %d faces, not one", numFaces);
1982   else if (numFaces == 1) {
1983     /* Add the other cell neighbor for this face */
1984     ierr = DMPlexSetCone(subdm, subcell, faces);CHKERRQ(ierr);
1985   } else {
1986     PetscInt *indices, *origVertices, *orientedVertices, *orientedSubVertices, v, ov;
1987     PetscBool posOriented;
1988 
1989     ierr                = DMGetWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), PETSC_INT, &orientedVertices);CHKERRQ(ierr);
1990     origVertices        = &orientedVertices[numFaceVertices];
1991     indices             = &orientedVertices[numFaceVertices*2];
1992     orientedSubVertices = &orientedVertices[numFaceVertices*3];
1993     ierr                = DMPlexGetOrientedFace(dm, cell, numFaceVertices, faceVertices, numCorners, indices, origVertices, orientedVertices, &posOriented);CHKERRQ(ierr);
1994     /* TODO: I know that routine should return a permutation, not the indices */
1995     for (v = 0; v < numFaceVertices; ++v) {
1996       const PetscInt vertex = faceVertices[v], subvertex = subfaceVertices[v];
1997       for (ov = 0; ov < numFaceVertices; ++ov) {
1998         if (orientedVertices[ov] == vertex) {
1999           orientedSubVertices[ov] = subvertex;
2000           break;
2001         }
2002       }
2003       if (ov == numFaceVertices) SETERRQ1(comm, PETSC_ERR_PLIB, "Could not find face vertex %d in orientated set", vertex);
2004     }
2005     ierr = DMPlexSetCone(subdm, *newFacePoint, orientedSubVertices);CHKERRQ(ierr);
2006     ierr = DMPlexSetCone(subdm, subcell, newFacePoint);CHKERRQ(ierr);
2007     ierr = DMRestoreWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), PETSC_INT, &orientedVertices);CHKERRQ(ierr);
2008     ++(*newFacePoint);
2009   }
2010 #if 0
2011   ierr = DMPlexRestoreJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces);CHKERRQ(ierr);
2012 #else
2013   ierr = DMRestoreWorkArray(subdm, 1, PETSC_INT, (void **) &faces);CHKERRQ(ierr);
2014 #endif
2015   PetscFunctionReturn(0);
2016 }
2017 
2018 #undef __FUNCT__
2019 #define __FUNCT__ "DMPlexCreateSubmesh_Uninterpolated"
2020 static PetscErrorCode DMPlexCreateSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm)
2021 {
2022   MPI_Comm        comm;
2023   DMLabel         subpointMap;
2024   IS              subvertexIS,  subcellIS;
2025   const PetscInt *subVertices, *subCells;
2026   PetscInt        numSubVertices, firstSubVertex, numSubCells;
2027   PetscInt       *subface, maxConeSize, numSubFaces = 0, firstSubFace, newFacePoint, nFV = 0;
2028   PetscInt        vStart, vEnd, c, f;
2029   PetscErrorCode  ierr;
2030 
2031   PetscFunctionBegin;
2032   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2033   /* Create subpointMap which marks the submesh */
2034   ierr = DMLabelCreate("subpoint_map", &subpointMap);CHKERRQ(ierr);
2035   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
2036   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
2037   if (vertexLabel) {ierr = DMPlexMarkSubmesh_Uninterpolated(dm, vertexLabel, value, subpointMap, &numSubFaces, &nFV, subdm);CHKERRQ(ierr);}
2038   /* Setup chart */
2039   ierr = DMLabelGetStratumSize(subpointMap, 0, &numSubVertices);CHKERRQ(ierr);
2040   ierr = DMLabelGetStratumSize(subpointMap, 2, &numSubCells);CHKERRQ(ierr);
2041   ierr = DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices);CHKERRQ(ierr);
2042   ierr = DMPlexSetVTKCellHeight(subdm, 1);CHKERRQ(ierr);
2043   /* Set cone sizes */
2044   firstSubVertex = numSubCells;
2045   firstSubFace   = numSubCells+numSubVertices;
2046   newFacePoint   = firstSubFace;
2047   ierr = DMLabelGetStratumIS(subpointMap, 0, &subvertexIS);CHKERRQ(ierr);
2048   if (subvertexIS) {ierr = ISGetIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
2049   ierr = DMLabelGetStratumIS(subpointMap, 2, &subcellIS);CHKERRQ(ierr);
2050   if (subcellIS) {ierr = ISGetIndices(subcellIS, &subCells);CHKERRQ(ierr);}
2051   for (c = 0; c < numSubCells; ++c) {
2052     ierr = DMPlexSetConeSize(subdm, c, 1);CHKERRQ(ierr);
2053   }
2054   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
2055     ierr = DMPlexSetConeSize(subdm, f, nFV);CHKERRQ(ierr);
2056   }
2057   ierr = DMSetUp(subdm);CHKERRQ(ierr);
2058   /* Create face cones */
2059   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
2060   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
2061   ierr = DMGetWorkArray(subdm, maxConeSize, PETSC_INT, (void**) &subface);CHKERRQ(ierr);
2062   for (c = 0; c < numSubCells; ++c) {
2063     const PetscInt cell    = subCells[c];
2064     const PetscInt subcell = c;
2065     PetscInt      *closure = NULL;
2066     PetscInt       closureSize, cl, numCorners = 0, faceSize = 0;
2067 
2068     ierr = DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2069     for (cl = 0; cl < closureSize*2; cl += 2) {
2070       const PetscInt point = closure[cl];
2071       PetscInt       subVertex;
2072 
2073       if ((point >= vStart) && (point < vEnd)) {
2074         ++numCorners;
2075         ierr = PetscFindInt(point, numSubVertices, subVertices, &subVertex);CHKERRQ(ierr);
2076         if (subVertex >= 0) {
2077           closure[faceSize] = point;
2078           subface[faceSize] = firstSubVertex+subVertex;
2079           ++faceSize;
2080         }
2081       }
2082     }
2083     if (faceSize > nFV) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %d of an element on the surface", faceSize);
2084     if (faceSize == nFV) {
2085       ierr = DMPlexInsertFace_Internal(dm, subdm, faceSize, closure, subface, numCorners, cell, subcell, firstSubFace, &newFacePoint);CHKERRQ(ierr);
2086     }
2087     ierr = DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2088   }
2089   ierr = DMRestoreWorkArray(subdm, maxConeSize, PETSC_INT, (void**) &subface);CHKERRQ(ierr);
2090   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
2091   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
2092   /* Build coordinates */
2093   {
2094     PetscSection coordSection, subCoordSection;
2095     Vec          coordinates, subCoordinates;
2096     PetscScalar *coords, *subCoords;
2097     PetscInt     numComp, coordSize, v;
2098 
2099     ierr = DMPlexGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
2100     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
2101     ierr = DMPlexGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
2102     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
2103     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
2104     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
2105     ierr = PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices);CHKERRQ(ierr);
2106     for (v = 0; v < numSubVertices; ++v) {
2107       const PetscInt vertex    = subVertices[v];
2108       const PetscInt subvertex = firstSubVertex+v;
2109       PetscInt       dof;
2110 
2111       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
2112       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
2113       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
2114     }
2115     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
2116     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
2117     ierr = VecCreate(comm, &subCoordinates);CHKERRQ(ierr);
2118     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
2119     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
2120     if (coordSize) {
2121       ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
2122       ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
2123       for (v = 0; v < numSubVertices; ++v) {
2124         const PetscInt vertex    = subVertices[v];
2125         const PetscInt subvertex = firstSubVertex+v;
2126         PetscInt       dof, off, sdof, soff, d;
2127 
2128         ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
2129         ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
2130         ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
2131         ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
2132         if (dof != sdof) SETERRQ4(comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
2133         for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
2134       }
2135       ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
2136       ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
2137     }
2138     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
2139     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
2140   }
2141   /* Cleanup */
2142   if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
2143   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
2144   if (subcellIS) {ierr = ISRestoreIndices(subcellIS, &subCells);CHKERRQ(ierr);}
2145   ierr = ISDestroy(&subcellIS);CHKERRQ(ierr);
2146   PetscFunctionReturn(0);
2147 }
2148 
2149 #undef __FUNCT__
2150 #define __FUNCT__ "DMPlexCreateSubmesh_Interpolated"
2151 static PetscErrorCode DMPlexCreateSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm)
2152 {
2153   MPI_Comm         comm;
2154   DMLabel          subpointMap;
2155   IS              *subpointIS;
2156   const PetscInt **subpoints;
2157   PetscInt        *numSubPoints, *firstSubPoint, *coneNew, *coneONew;
2158   PetscInt         totSubPoints = 0, maxConeSize, dim, p, d, v;
2159   PetscErrorCode   ierr;
2160 
2161   PetscFunctionBegin;
2162   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2163   /* Create subpointMap which marks the submesh */
2164   ierr = DMLabelCreate("subpoint_map", &subpointMap);CHKERRQ(ierr);
2165   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
2166   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
2167   if (vertexLabel) {ierr = DMPlexMarkSubmesh_Interpolated(dm, vertexLabel, value, subpointMap, subdm);CHKERRQ(ierr);}
2168   /* Setup chart */
2169   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
2170   ierr = PetscMalloc4(dim+1,PetscInt,&numSubPoints,dim+1,PetscInt,&firstSubPoint,dim+1,IS,&subpointIS,dim+1,const PetscInt *,&subpoints);CHKERRQ(ierr);
2171   for (d = 0; d <= dim; ++d) {
2172     ierr = DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d]);CHKERRQ(ierr);
2173     totSubPoints += numSubPoints[d];
2174   }
2175   ierr = DMPlexSetChart(subdm, 0, totSubPoints);CHKERRQ(ierr);
2176   ierr = DMPlexSetVTKCellHeight(subdm, 1);CHKERRQ(ierr);
2177   /* Set cone sizes */
2178   firstSubPoint[dim] = 0;
2179   firstSubPoint[0]   = firstSubPoint[dim] + numSubPoints[dim];
2180   if (dim > 1) {firstSubPoint[dim-1] = firstSubPoint[0]     + numSubPoints[0];}
2181   if (dim > 2) {firstSubPoint[dim-2] = firstSubPoint[dim-1] + numSubPoints[dim-1];}
2182   for (d = 0; d <= dim; ++d) {
2183     ierr = DMLabelGetStratumIS(subpointMap, d, &subpointIS[d]);CHKERRQ(ierr);
2184     ierr = ISGetIndices(subpointIS[d], &subpoints[d]);CHKERRQ(ierr);
2185   }
2186   for (d = 0; d <= dim; ++d) {
2187     for (p = 0; p < numSubPoints[d]; ++p) {
2188       const PetscInt  point    = subpoints[d][p];
2189       const PetscInt  subpoint = firstSubPoint[d] + p;
2190       const PetscInt *cone;
2191       PetscInt        coneSize, coneSizeNew, c, val;
2192 
2193       ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
2194       ierr = DMPlexSetConeSize(subdm, subpoint, coneSize);CHKERRQ(ierr);
2195       if (d == dim) {
2196         ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
2197         for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
2198           ierr = DMLabelGetValue(subpointMap, cone[c], &val);CHKERRQ(ierr);
2199           if (val >= 0) coneSizeNew++;
2200         }
2201         ierr = DMPlexSetConeSize(subdm, subpoint, coneSizeNew);CHKERRQ(ierr);
2202       }
2203     }
2204   }
2205   ierr = DMSetUp(subdm);CHKERRQ(ierr);
2206   /* Set cones */
2207   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
2208   ierr = PetscMalloc2(maxConeSize,PetscInt,&coneNew,maxConeSize,PetscInt,&coneONew);CHKERRQ(ierr);
2209   for (d = 0; d <= dim; ++d) {
2210     for (p = 0; p < numSubPoints[d]; ++p) {
2211       const PetscInt  point    = subpoints[d][p];
2212       const PetscInt  subpoint = firstSubPoint[d] + p;
2213       const PetscInt *cone, *ornt;
2214       PetscInt        coneSize, subconeSize, coneSizeNew, c, subc;
2215 
2216       ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
2217       ierr = DMPlexGetConeSize(subdm, subpoint, &subconeSize);CHKERRQ(ierr);
2218       ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
2219       ierr = DMPlexGetConeOrientation(dm, point, &ornt);CHKERRQ(ierr);
2220       for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
2221         ierr = PetscFindInt(cone[c], numSubPoints[d-1], subpoints[d-1], &subc);CHKERRQ(ierr);
2222         if (subc >= 0) {
2223           coneNew[coneSizeNew]  = firstSubPoint[d-1] + subc;
2224           coneONew[coneSizeNew] = ornt[c];
2225           ++coneSizeNew;
2226         }
2227       }
2228       if (coneSizeNew != subconeSize) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of cone points located %d does not match subcone size %d", coneSizeNew, subconeSize);
2229       ierr = DMPlexSetCone(subdm, subpoint, coneNew);CHKERRQ(ierr);
2230       ierr = DMPlexSetConeOrientation(subdm, subpoint, coneONew);CHKERRQ(ierr);
2231     }
2232   }
2233   ierr = PetscFree2(coneNew,coneONew);CHKERRQ(ierr);
2234   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
2235   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
2236   /* Build coordinates */
2237   {
2238     PetscSection coordSection, subCoordSection;
2239     Vec          coordinates, subCoordinates;
2240     PetscScalar *coords, *subCoords;
2241     PetscInt     numComp, coordSize;
2242 
2243     ierr = DMPlexGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
2244     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
2245     ierr = DMPlexGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
2246     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
2247     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
2248     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
2249     ierr = PetscSectionSetChart(subCoordSection, firstSubPoint[0], firstSubPoint[0]+numSubPoints[0]);CHKERRQ(ierr);
2250     for (v = 0; v < numSubPoints[0]; ++v) {
2251       const PetscInt vertex    = subpoints[0][v];
2252       const PetscInt subvertex = firstSubPoint[0]+v;
2253       PetscInt       dof;
2254 
2255       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
2256       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
2257       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
2258     }
2259     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
2260     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
2261     ierr = VecCreate(comm, &subCoordinates);CHKERRQ(ierr);
2262     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
2263     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
2264     ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
2265     ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
2266     for (v = 0; v < numSubPoints[0]; ++v) {
2267       const PetscInt vertex    = subpoints[0][v];
2268       const PetscInt subvertex = firstSubPoint[0]+v;
2269       PetscInt dof, off, sdof, soff, d;
2270 
2271       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
2272       ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
2273       ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
2274       ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
2275       if (dof != sdof) SETERRQ4(comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
2276       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
2277     }
2278     ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
2279     ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
2280     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
2281     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
2282   }
2283   /* Cleanup */
2284   for (d = 0; d <= dim; ++d) {
2285     ierr = ISRestoreIndices(subpointIS[d], &subpoints[d]);CHKERRQ(ierr);
2286     ierr = ISDestroy(&subpointIS[d]);CHKERRQ(ierr);
2287   }
2288   ierr = PetscFree4(numSubPoints,firstSubPoint,subpointIS,subpoints);CHKERRQ(ierr);
2289   PetscFunctionReturn(0);
2290 }
2291 
2292 #undef __FUNCT__
2293 #define __FUNCT__ "DMPlexCreateSubmesh"
2294 /*@
2295   DMPlexCreateSubmesh - Extract a hypersurface from the mesh using vertices defined by a label
2296 
2297   Input Parameters:
2298 + dm           - The original mesh
2299 . vertexLabel  - The DMLabel marking vertices contained in the surface
2300 - value        - The label value to use
2301 
2302   Output Parameter:
2303 . subdm - The surface mesh
2304 
2305   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
2306 
2307   Level: developer
2308 
2309 .seealso: DMPlexGetSubpointMap(), DMPlexGetLabel(), DMLabelSetValue()
2310 @*/
2311 PetscErrorCode DMPlexCreateSubmesh(DM dm, DMLabel vertexLabel, PetscInt value, DM *subdm)
2312 {
2313   PetscInt       dim, depth;
2314   PetscErrorCode ierr;
2315 
2316   PetscFunctionBegin;
2317   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2318   PetscValidPointer(subdm, 3);
2319   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
2320   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2321   ierr = DMCreate(PetscObjectComm((PetscObject)dm), subdm);CHKERRQ(ierr);
2322   ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr);
2323   ierr = DMPlexSetDimension(*subdm, dim-1);CHKERRQ(ierr);
2324   if (depth == dim) {
2325     ierr = DMPlexCreateSubmesh_Interpolated(dm, vertexLabel, value, *subdm);CHKERRQ(ierr);
2326   } else {
2327     ierr = DMPlexCreateSubmesh_Uninterpolated(dm, vertexLabel, value, *subdm);CHKERRQ(ierr);
2328   }
2329   PetscFunctionReturn(0);
2330 }
2331 
2332 #undef __FUNCT__
2333 #define __FUNCT__ "DMPlexFilterPoint_Internal"
2334 PETSC_STATIC_INLINE PetscInt DMPlexFilterPoint_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[])
2335 {
2336   PetscInt       subPoint;
2337   PetscErrorCode ierr;
2338 
2339   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint); if (ierr < 0) return ierr;
2340   return subPoint < 0 ? subPoint : firstSubPoint+subPoint;
2341 }
2342 
2343 #undef __FUNCT__
2344 #define __FUNCT__ "DMPlexCreateCohesiveSubmesh_Uninterpolated"
2345 static PetscErrorCode DMPlexCreateCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM subdm)
2346 {
2347   MPI_Comm        comm;
2348   DMLabel         subpointMap;
2349   IS              subvertexIS;
2350   const PetscInt *subVertices;
2351   PetscInt        numSubVertices, firstSubVertex, numSubCells, *subCells = NULL;
2352   PetscInt       *subface, maxConeSize, numSubFaces, firstSubFace, newFacePoint, nFV;
2353   PetscInt        cMax, c, f;
2354   PetscErrorCode  ierr;
2355 
2356   PetscFunctionBegin;
2357   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
2358   /* Create subpointMap which marks the submesh */
2359   ierr = DMLabelCreate("subpoint_map", &subpointMap);CHKERRQ(ierr);
2360   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
2361   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
2362   ierr = DMPlexMarkCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, subpointMap, &numSubFaces, &nFV, &subCells, subdm);CHKERRQ(ierr);
2363   /* Setup chart */
2364   ierr = DMLabelGetStratumSize(subpointMap, 0, &numSubVertices);CHKERRQ(ierr);
2365   ierr = DMLabelGetStratumSize(subpointMap, 2, &numSubCells);CHKERRQ(ierr);
2366   ierr = DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices);CHKERRQ(ierr);
2367   ierr = DMPlexSetVTKCellHeight(subdm, 1);CHKERRQ(ierr);
2368   /* Set cone sizes */
2369   firstSubVertex = numSubCells;
2370   firstSubFace   = numSubCells+numSubVertices;
2371   newFacePoint   = firstSubFace;
2372   ierr = DMLabelGetStratumIS(subpointMap, 0, &subvertexIS);CHKERRQ(ierr);
2373   if (subvertexIS) {ierr = ISGetIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
2374   for (c = 0; c < numSubCells; ++c) {
2375     ierr = DMPlexSetConeSize(subdm, c, 1);CHKERRQ(ierr);
2376   }
2377   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
2378     ierr = DMPlexSetConeSize(subdm, f, nFV);CHKERRQ(ierr);
2379   }
2380   ierr = DMSetUp(subdm);CHKERRQ(ierr);
2381   /* Create face cones */
2382   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
2383   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
2384   ierr = DMGetWorkArray(subdm, maxConeSize, PETSC_INT, (void**) &subface);CHKERRQ(ierr);
2385   for (c = 0; c < numSubCells; ++c) {
2386     const PetscInt  cell    = subCells[c];
2387     const PetscInt  subcell = c;
2388     const PetscInt *cone, *cells;
2389     PetscInt        numCells, subVertex, p, v;
2390 
2391     if (cell < cMax) continue;
2392     ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
2393     for (v = 0; v < nFV; ++v) {
2394       ierr = PetscFindInt(cone[v], numSubVertices, subVertices, &subVertex);CHKERRQ(ierr);
2395       subface[v] = firstSubVertex+subVertex;
2396     }
2397     ierr = DMPlexSetCone(subdm, newFacePoint, subface);CHKERRQ(ierr);
2398     ierr = DMPlexSetCone(subdm, subcell, &newFacePoint);CHKERRQ(ierr);
2399     ierr = DMPlexGetJoin(dm, nFV, cone, &numCells, &cells);CHKERRQ(ierr);
2400     /* Not true in parallel
2401     if (numCells != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
2402     for (p = 0; p < numCells; ++p) {
2403       PetscInt negsubcell;
2404 
2405       if (cells[p] >= cMax) continue;
2406       /* I know this is a crap search */
2407       for (negsubcell = 0; negsubcell < numSubCells; ++negsubcell) {
2408         if (subCells[negsubcell] == cells[p]) break;
2409       }
2410       if (negsubcell == numSubCells) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find negative face neighbor for cohesive cell %d", cell);
2411       ierr = DMPlexSetCone(subdm, negsubcell, &newFacePoint);CHKERRQ(ierr);
2412     }
2413     ierr = DMPlexRestoreJoin(dm, nFV, cone, &numCells, &cells);CHKERRQ(ierr);
2414     ++newFacePoint;
2415   }
2416   ierr = DMRestoreWorkArray(subdm, maxConeSize, PETSC_INT, (void**) &subface);CHKERRQ(ierr);
2417   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
2418   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
2419   /* Build coordinates */
2420   {
2421     PetscSection coordSection, subCoordSection;
2422     Vec          coordinates, subCoordinates;
2423     PetscScalar *coords, *subCoords;
2424     PetscInt     numComp, coordSize, v;
2425 
2426     ierr = DMPlexGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
2427     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
2428     ierr = DMPlexGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
2429     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
2430     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
2431     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
2432     ierr = PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices);CHKERRQ(ierr);
2433     for (v = 0; v < numSubVertices; ++v) {
2434       const PetscInt vertex    = subVertices[v];
2435       const PetscInt subvertex = firstSubVertex+v;
2436       PetscInt       dof;
2437 
2438       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
2439       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
2440       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
2441     }
2442     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
2443     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
2444     ierr = VecCreate(comm, &subCoordinates);CHKERRQ(ierr);
2445     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
2446     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
2447     ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
2448     ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
2449     for (v = 0; v < numSubVertices; ++v) {
2450       const PetscInt vertex    = subVertices[v];
2451       const PetscInt subvertex = firstSubVertex+v;
2452       PetscInt       dof, off, sdof, soff, d;
2453 
2454       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
2455       ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
2456       ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
2457       ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
2458       if (dof != sdof) SETERRQ4(comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
2459       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
2460     }
2461     ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
2462     ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
2463     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
2464     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
2465   }
2466   /* Build SF */
2467   CHKMEMQ;
2468   {
2469     PetscSF            sfPoint, sfPointSub;
2470     const PetscSFNode *remotePoints;
2471     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
2472     const PetscInt    *localPoints;
2473     PetscInt          *slocalPoints;
2474     PetscInt           numRoots, numLeaves, numSubRoots = numSubCells+numSubFaces+numSubVertices, numSubLeaves = 0, l, sl, ll, pStart, pEnd, p, vStart, vEnd;
2475     PetscMPIInt        rank;
2476 
2477     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
2478     ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
2479     ierr = DMGetPointSF(subdm, &sfPointSub);CHKERRQ(ierr);
2480     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2481     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
2482     ierr = PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr);
2483     if (numRoots >= 0) {
2484       /* Only vertices should be shared */
2485       ierr = PetscMalloc2(pEnd-pStart,PetscSFNode,&newLocalPoints,numRoots,PetscSFNode,&newOwners);CHKERRQ(ierr);
2486       for (p = 0; p < pEnd-pStart; ++p) {
2487         newLocalPoints[p].rank  = -2;
2488         newLocalPoints[p].index = -2;
2489       }
2490       /* Set subleaves */
2491       for (l = 0; l < numLeaves; ++l) {
2492         const PetscInt point    = localPoints[l];
2493         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
2494 
2495         if ((point < vStart) && (point >= vEnd)) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Should not be mapping anything but vertices, %d", point);
2496         if (subPoint < 0) continue;
2497         newLocalPoints[point-pStart].rank  = rank;
2498         newLocalPoints[point-pStart].index = subPoint;
2499         ++numSubLeaves;
2500       }
2501       /* Must put in owned subpoints */
2502       for (p = pStart; p < pEnd; ++p) {
2503         const PetscInt subPoint = DMPlexFilterPoint_Internal(p, firstSubVertex, numSubVertices, subVertices);
2504 
2505         if (subPoint < 0) {
2506           newOwners[p-pStart].rank  = -3;
2507           newOwners[p-pStart].index = -3;
2508         } else {
2509           newOwners[p-pStart].rank  = rank;
2510           newOwners[p-pStart].index = subPoint;
2511         }
2512       }
2513       ierr = PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
2514       ierr = PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
2515       ierr = PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr);
2516       ierr = PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr);
2517       ierr = PetscMalloc(numSubLeaves * sizeof(PetscInt),    &slocalPoints);CHKERRQ(ierr);
2518       ierr = PetscMalloc(numSubLeaves * sizeof(PetscSFNode), &sremotePoints);CHKERRQ(ierr);
2519       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
2520         const PetscInt point    = localPoints[l];
2521         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
2522 
2523         if (subPoint < 0) continue;
2524         if (newLocalPoints[point].rank == rank) {++ll; continue;}
2525         slocalPoints[sl]        = subPoint;
2526         sremotePoints[sl].rank  = newLocalPoints[point].rank;
2527         sremotePoints[sl].index = newLocalPoints[point].index;
2528         if (sremotePoints[sl].rank  < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %d", point);
2529         if (sremotePoints[sl].index < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %d", point);
2530         ++sl;
2531       }
2532       ierr = PetscFree2(newLocalPoints,newOwners);CHKERRQ(ierr);
2533       if (sl + ll != numSubLeaves) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %d + %d != %d", sl, ll, numSubLeaves);
2534       ierr = PetscSFSetGraph(sfPointSub, numSubRoots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
2535     }
2536   }
2537   CHKMEMQ;
2538   /* Cleanup */
2539   if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
2540   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
2541   ierr = PetscFree(subCells);CHKERRQ(ierr);
2542   PetscFunctionReturn(0);
2543 }
2544 
2545 #undef __FUNCT__
2546 #define __FUNCT__ "DMPlexCreateCohesiveSubmesh_Interpolated"
2547 static PetscErrorCode DMPlexCreateCohesiveSubmesh_Interpolated(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM subdm)
2548 {
2549   MPI_Comm         comm;
2550   DMLabel          subpointMap;
2551   IS              *subpointIS;
2552   const PetscInt **subpoints;
2553   PetscInt        *numSubPoints, *firstSubPoint, *coneNew;
2554   PetscInt         totSubPoints = 0, maxConeSize, dim, p, d, v;
2555   PetscErrorCode   ierr;
2556 
2557   PetscFunctionBegin;
2558   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2559   /* Create subpointMap which marks the submesh */
2560   ierr = DMLabelCreate("subpoint_map", &subpointMap);CHKERRQ(ierr);
2561   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
2562   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
2563   ierr = DMPlexMarkCohesiveSubmesh_Interpolated(dm, hasLagrange, label, value, subpointMap, subdm);CHKERRQ(ierr);
2564   /* Setup chart */
2565   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
2566   ierr = PetscMalloc4(dim+1,PetscInt,&numSubPoints,dim+1,PetscInt,&firstSubPoint,dim+1,IS,&subpointIS,dim+1,const PetscInt *,&subpoints);CHKERRQ(ierr);
2567   for (d = 0; d <= dim; ++d) {
2568     ierr = DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d]);CHKERRQ(ierr);
2569     totSubPoints += numSubPoints[d];
2570   }
2571   ierr = DMPlexSetChart(subdm, 0, totSubPoints);CHKERRQ(ierr);
2572   ierr = DMPlexSetVTKCellHeight(subdm, 1);CHKERRQ(ierr);
2573   /* Set cone sizes */
2574   firstSubPoint[dim] = 0;
2575   firstSubPoint[0]   = firstSubPoint[dim] + numSubPoints[dim];
2576   if (dim > 1) {firstSubPoint[dim-1] = firstSubPoint[0]     + numSubPoints[0];}
2577   if (dim > 2) {firstSubPoint[dim-2] = firstSubPoint[dim-1] + numSubPoints[dim-1];}
2578   for (d = 0; d <= dim; ++d) {
2579     ierr = DMLabelGetStratumIS(subpointMap, d, &subpointIS[d]);CHKERRQ(ierr);
2580     ierr = ISGetIndices(subpointIS[d], &subpoints[d]);CHKERRQ(ierr);
2581   }
2582   for (d = 0; d <= dim; ++d) {
2583     for (p = 0; p < numSubPoints[d]; ++p) {
2584       const PetscInt  point    = subpoints[d][p];
2585       const PetscInt  subpoint = firstSubPoint[d] + p;
2586       const PetscInt *cone;
2587       PetscInt        coneSize, coneSizeNew, c, val;
2588 
2589       ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
2590       ierr = DMPlexSetConeSize(subdm, subpoint, coneSize);CHKERRQ(ierr);
2591       if (d == dim) {
2592         ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
2593         for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
2594           ierr = DMLabelGetValue(subpointMap, cone[c], &val);CHKERRQ(ierr);
2595           if (val >= 0) coneSizeNew++;
2596         }
2597         ierr = DMPlexSetConeSize(subdm, subpoint, coneSizeNew);CHKERRQ(ierr);
2598       }
2599     }
2600   }
2601   ierr = DMSetUp(subdm);CHKERRQ(ierr);
2602   /* Set cones */
2603   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
2604   ierr = PetscMalloc(maxConeSize * sizeof(PetscInt), &coneNew);CHKERRQ(ierr);
2605   for (d = 0; d <= dim; ++d) {
2606     for (p = 0; p < numSubPoints[d]; ++p) {
2607       const PetscInt  point    = subpoints[d][p];
2608       const PetscInt  subpoint = firstSubPoint[d] + p;
2609       const PetscInt *cone;
2610       PetscInt        coneSize, subconeSize, coneSizeNew, c, subc;
2611 
2612       ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
2613       ierr = DMPlexGetConeSize(subdm, subpoint, &subconeSize);CHKERRQ(ierr);
2614       ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
2615       for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
2616         ierr = PetscFindInt(cone[c], numSubPoints[d-1], subpoints[d-1], &subc);CHKERRQ(ierr);
2617         if (subc >= 0) coneNew[coneSizeNew++] = firstSubPoint[d-1] + subc;
2618       }
2619       if (coneSizeNew != subconeSize) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of cone points located %d does not match subcone size %d", coneSizeNew, subconeSize);
2620       ierr = DMPlexSetCone(subdm, subpoint, coneNew);CHKERRQ(ierr);
2621     }
2622   }
2623   ierr = PetscFree(coneNew);CHKERRQ(ierr);
2624   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
2625   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
2626   /* Build coordinates */
2627   {
2628     PetscSection coordSection, subCoordSection;
2629     Vec          coordinates, subCoordinates;
2630     PetscScalar *coords, *subCoords;
2631     PetscInt     numComp, coordSize;
2632 
2633     ierr = DMPlexGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
2634     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
2635     ierr = DMPlexGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
2636     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
2637     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
2638     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
2639     ierr = PetscSectionSetChart(subCoordSection, firstSubPoint[0], firstSubPoint[0]+numSubPoints[0]);CHKERRQ(ierr);
2640     for (v = 0; v < numSubPoints[0]; ++v) {
2641       const PetscInt vertex    = subpoints[0][v];
2642       const PetscInt subvertex = firstSubPoint[0]+v;
2643       PetscInt       dof;
2644 
2645       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
2646       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
2647       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
2648     }
2649     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
2650     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
2651     ierr = VecCreate(comm, &subCoordinates);CHKERRQ(ierr);
2652     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
2653     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
2654     ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
2655     ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
2656     for (v = 0; v < numSubPoints[0]; ++v) {
2657       const PetscInt vertex    = subpoints[0][v];
2658       const PetscInt subvertex = firstSubPoint[0]+v;
2659       PetscInt dof, off, sdof, soff, d;
2660 
2661       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
2662       ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
2663       ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
2664       ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
2665       if (dof != sdof) SETERRQ4(comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
2666       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
2667     }
2668     ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
2669     ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
2670     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
2671     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
2672   }
2673   /* Cleanup */
2674   for (d = 0; d <= dim; ++d) {
2675     ierr = ISRestoreIndices(subpointIS[d], &subpoints[d]);CHKERRQ(ierr);
2676     ierr = ISDestroy(&subpointIS[d]);CHKERRQ(ierr);
2677   }
2678   ierr = PetscFree4(numSubPoints,firstSubPoint,subpointIS,subpoints);CHKERRQ(ierr);
2679   PetscFunctionReturn(0);
2680 }
2681 
2682 #undef __FUNCT__
2683 #define __FUNCT__ "DMPlexCreateCohesiveSubmesh"
2684 /*
2685   DMPlexCreateCohesiveSubmesh - Extract from a mesh with cohesive cells the hypersurface defined by one face of the cells. Optionally, a Label an be given to restrict the cells.
2686 
2687   Input Parameters:
2688 + dm          - The original mesh
2689 . hasLagrange - The mesh has Lagrange unknowns in the cohesive cells
2690 . label       - A label name, or PETSC_NULL
2691 - value  - A label value
2692 
2693   Output Parameter:
2694 . subdm - The surface mesh
2695 
2696   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
2697 
2698   Level: developer
2699 
2700 .seealso: DMPlexGetSubpointMap(), DMPlexCreateSubmesh()
2701 */
2702 PetscErrorCode DMPlexCreateCohesiveSubmesh(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM *subdm)
2703 {
2704   PetscInt       dim, depth;
2705   PetscErrorCode ierr;
2706 
2707   PetscFunctionBegin;
2708   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2709   PetscValidPointer(subdm, 5);
2710   ierr = DMPlexGetDimension(dm, &dim);CHKERRQ(ierr);
2711   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2712   ierr = DMCreate(PetscObjectComm((PetscObject)dm), subdm);CHKERRQ(ierr);
2713   ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr);
2714   ierr = DMPlexSetDimension(*subdm, dim-1);CHKERRQ(ierr);
2715   if (depth == dim) {
2716     ierr = DMPlexCreateCohesiveSubmesh_Interpolated(dm, hasLagrange, label, value, *subdm);CHKERRQ(ierr);
2717   } else {
2718     ierr = DMPlexCreateCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, *subdm);CHKERRQ(ierr);
2719   }
2720   PetscFunctionReturn(0);
2721 }
2722 
2723 #undef __FUNCT__
2724 #define __FUNCT__ "DMPlexGetSubpointMap"
2725 /*@
2726   DMPlexGetSubpointMap - Returns a DMLabel with point dimension as values
2727 
2728   Input Parameter:
2729 . dm - The submesh DM
2730 
2731   Output Parameter:
2732 . subpointMap - The DMLabel of all the points from the original mesh in this submesh, or NULL if this is not a submesh
2733 
2734   Level: developer
2735 
2736 .seealso: DMPlexCreateSubmesh(), DMPlexCreateSubpointIS()
2737 @*/
2738 PetscErrorCode DMPlexGetSubpointMap(DM dm, DMLabel *subpointMap)
2739 {
2740   DM_Plex *mesh = (DM_Plex*) dm->data;
2741 
2742   PetscFunctionBegin;
2743   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2744   PetscValidPointer(subpointMap, 2);
2745   *subpointMap = mesh->subpointMap;
2746   PetscFunctionReturn(0);
2747 }
2748 
2749 #undef __FUNCT__
2750 #define __FUNCT__ "DMPlexSetSubpointMap"
2751 /* Note: Should normally not be called by the user, since it is set in DMPlexCreateSubmesh() */
2752 PetscErrorCode DMPlexSetSubpointMap(DM dm, DMLabel subpointMap)
2753 {
2754   DM_Plex       *mesh = (DM_Plex *) dm->data;
2755   DMLabel        tmp;
2756   PetscErrorCode ierr;
2757 
2758   PetscFunctionBegin;
2759   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2760   tmp  = mesh->subpointMap;
2761   mesh->subpointMap = subpointMap;
2762   ++mesh->subpointMap->refct;
2763   ierr = DMLabelDestroy(&tmp);CHKERRQ(ierr);
2764   PetscFunctionReturn(0);
2765 }
2766 
2767 #undef __FUNCT__
2768 #define __FUNCT__ "DMPlexCreateSubpointIS"
2769 /*@
2770   DMPlexCreateSubpointIS - Creates an IS covering the entire subdm chart with the original points as data
2771 
2772   Input Parameter:
2773 . dm - The submesh DM
2774 
2775   Output Parameter:
2776 . subpointIS - The IS of all the points from the original mesh in this submesh, or NULL if this is not a submesh
2777 
2778   Note: This is IS is guaranteed to be sorted by the construction of the submesh
2779 
2780   Level: developer
2781 
2782 .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointMap()
2783 @*/
2784 PetscErrorCode DMPlexCreateSubpointIS(DM dm, IS *subpointIS)
2785 {
2786   MPI_Comm        comm;
2787   DMLabel         subpointMap;
2788   IS              is;
2789   const PetscInt *opoints;
2790   PetscInt       *points, *depths;
2791   PetscInt        depth, depStart, depEnd, d, pStart, pEnd, p, n, off;
2792   PetscErrorCode  ierr;
2793 
2794   PetscFunctionBegin;
2795   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2796   PetscValidPointer(subpointIS, 2);
2797   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2798   *subpointIS = NULL;
2799   ierr = DMPlexGetSubpointMap(dm, &subpointMap);CHKERRQ(ierr);
2800   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2801   if (subpointMap && depth >= 0) {
2802     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
2803     if (pStart) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Submeshes must start the point numbering at 0, not %d", pStart);
2804     ierr = DMGetWorkArray(dm, depth+1, PETSC_INT, &depths);CHKERRQ(ierr);
2805     depths[0] = depth;
2806     depths[1] = 0;
2807     for(d = 2; d <= depth; ++d) {depths[d] = depth+1 - d;}
2808     ierr = PetscMalloc(pEnd * sizeof(PetscInt), &points);CHKERRQ(ierr);
2809     for(d = 0, off = 0; d <= depth; ++d) {
2810       const PetscInt dep = depths[d];
2811 
2812       ierr = DMPlexGetDepthStratum(dm, dep, &depStart, &depEnd);CHKERRQ(ierr);
2813       ierr = DMLabelGetStratumSize(subpointMap, dep, &n);CHKERRQ(ierr);
2814       if (((d < 2) && (depth > 1)) || (d == 1)) { /* Only check vertices and cells for now since the map is broken for others */
2815         if (n != depEnd-depStart) SETERRQ3(comm, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %d at depth %d should be %d", n, dep, depEnd-depStart);
2816       } else {
2817         if (!n) {
2818           if (d == 0) {
2819             /* Missing cells */
2820             for(p = 0; p < depEnd-depStart; ++p, ++off) points[off] = -1;
2821           } else {
2822             /* Missing faces */
2823             for(p = 0; p < depEnd-depStart; ++p, ++off) points[off] = PETSC_MAX_INT;
2824           }
2825         }
2826       }
2827       if (n) {
2828         ierr = DMLabelGetStratumIS(subpointMap, dep, &is);CHKERRQ(ierr);
2829         ierr = ISGetIndices(is, &opoints);CHKERRQ(ierr);
2830         for(p = 0; p < n; ++p, ++off) points[off] = opoints[p];
2831         ierr = ISRestoreIndices(is, &opoints);CHKERRQ(ierr);
2832         ierr = ISDestroy(&is);CHKERRQ(ierr);
2833       }
2834     }
2835     ierr = DMRestoreWorkArray(dm, depth+1, PETSC_INT, &depths);CHKERRQ(ierr);
2836     if (off != pEnd) SETERRQ2(comm, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %d should be %d", off, pEnd);
2837     ierr = ISCreateGeneral(PETSC_COMM_SELF, pEnd, points, PETSC_OWN_POINTER, subpointIS);CHKERRQ(ierr);
2838   }
2839   PetscFunctionReturn(0);
2840 }
2841