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