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