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