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