Actual source code: plexsubmesh.c

petsc-master 2017-03-28
Report Typos and Errors
  1:  #include <petsc/private/dmpleximpl.h>
  2:  #include <petsc/private/dmlabelimpl.h>
  3:  #include <petscsf.h>

  5: static PetscErrorCode DMPlexMarkBoundaryFaces_Internal(DM dm, PetscInt cellHeight, DMLabel label)
  6: {
  7:   PetscInt       fStart, fEnd, f;

 12:   DMPlexGetHeightStratum(dm, cellHeight+1, &fStart, &fEnd);
 13:   for (f = fStart; f < fEnd; ++f) {
 14:     PetscInt supportSize;

 16:     DMPlexGetSupportSize(dm, f, &supportSize);
 17:     if (supportSize == 1) {DMLabelSetValue(label, f, 1);}
 18:   }
 19:   return(0);
 20: }

 22: /*@
 23:   DMPlexMarkBoundaryFaces - Mark all faces on the boundary

 25:   Not Collective

 27:   Input Parameter:
 28: . dm - The original DM

 30:   Output Parameter:
 31: . label - The DMLabel marking boundary faces with value 1

 33:   Level: developer

 35: .seealso: DMLabelCreate(), DMCreateLabel()
 36: @*/
 37: PetscErrorCode DMPlexMarkBoundaryFaces(DM dm, DMLabel label)
 38: {

 42:   DMPlexMarkBoundaryFaces_Internal(dm, 0, label);
 43:   return(0);
 44: }

 46: static PetscErrorCode DMPlexLabelComplete_Internal(DM dm, DMLabel label, PetscBool completeCells)
 47: {
 48:   IS              valueIS;
 49:   const PetscInt *values;
 50:   PetscInt        numValues, v, cStart, cEnd;
 51:   PetscErrorCode  ierr;

 54:   DMLabelGetNumValues(label, &numValues);
 55:   DMLabelGetValueIS(label, &valueIS);
 56:   DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);
 57:   ISGetIndices(valueIS, &values);
 58:   for (v = 0; v < numValues; ++v) {
 59:     IS              pointIS;
 60:     const PetscInt *points;
 61:     PetscInt        numPoints, p;

 63:     DMLabelGetStratumSize(label, values[v], &numPoints);
 64:     DMLabelGetStratumIS(label, values[v], &pointIS);
 65:     ISGetIndices(pointIS, &points);
 66:     for (p = 0; p < numPoints; ++p) {
 67:       PetscInt  q = points[p];
 68:       PetscInt *closure = NULL;
 69:       PetscInt  closureSize, c;

 71:       if (cStart <= q && q < cEnd && !completeCells) { /* skip cells */
 72:         continue;
 73:       }
 74:       DMPlexGetTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure);
 75:       for (c = 0; c < closureSize*2; c += 2) {
 76:         DMLabelSetValue(label, closure[c], values[v]);
 77:       }
 78:       DMPlexRestoreTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure);
 79:     }
 80:     ISRestoreIndices(pointIS, &points);
 81:     ISDestroy(&pointIS);
 82:   }
 83:   ISRestoreIndices(valueIS, &values);
 84:   ISDestroy(&valueIS);
 85:   return(0);
 86: }

 88: /*@
 89:   DMPlexLabelComplete - Starting with a label marking points on a surface, we add the transitive closure to the surface

 91:   Input Parameters:
 92: + dm - The DM
 93: - label - A DMLabel marking the surface points

 95:   Output Parameter:
 96: . label - A DMLabel marking all surface points in the transitive closure

 98:   Level: developer

100: .seealso: DMPlexLabelCohesiveComplete()
101: @*/
102: PetscErrorCode DMPlexLabelComplete(DM dm, DMLabel label)
103: {

107:   DMPlexLabelComplete_Internal(dm, label, PETSC_TRUE);
108:   return(0);
109: }

111: /*@
112:   DMPlexLabelAddCells - Starting with a label marking faces on a surface, we add a cell for each face

114:   Input Parameters:
115: + dm - The DM
116: - label - A DMLabel marking the surface points

118:   Output Parameter:
119: . label - A DMLabel incorporating cells

121:   Level: developer

123:   Note: The cells allow FEM boundary conditions to be applied using the cell geometry

125: .seealso: DMPlexLabelComplete(), DMPlexLabelCohesiveComplete()
126: @*/
127: PetscErrorCode DMPlexLabelAddCells(DM dm, DMLabel label)
128: {
129:   IS              valueIS;
130:   const PetscInt *values;
131:   PetscInt        numValues, v, cStart, cEnd, cEndInterior;
132:   PetscErrorCode  ierr;

135:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
136:   DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);
137:   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
138:   DMLabelGetNumValues(label, &numValues);
139:   DMLabelGetValueIS(label, &valueIS);
140:   ISGetIndices(valueIS, &values);
141:   for (v = 0; v < numValues; ++v) {
142:     IS              pointIS;
143:     const PetscInt *points;
144:     PetscInt        numPoints, p;

146:     DMLabelGetStratumSize(label, values[v], &numPoints);
147:     DMLabelGetStratumIS(label, values[v], &pointIS);
148:     ISGetIndices(pointIS, &points);
149:     for (p = 0; p < numPoints; ++p) {
150:       PetscInt *closure = NULL;
151:       PetscInt  closureSize, point, cl;

153:       DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure);
154:       for (cl = closureSize-1; cl > 0; --cl) {
155:         point = closure[cl*2];
156:         if ((point >= cStart) && (point < cEnd)) {DMLabelSetValue(label, point, values[v]); break;}
157:       }
158:       DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure);
159:     }
160:     ISRestoreIndices(pointIS, &points);
161:     ISDestroy(&pointIS);
162:   }
163:   ISRestoreIndices(valueIS, &values);
164:   ISDestroy(&valueIS);
165:   return(0);
166: }

168: /*@
169:   DMPlexLabelClearCells - Remove cells from a label

171:   Input Parameters:
172: + dm - The DM
173: - label - A DMLabel marking surface points and their adjacent cells

175:   Output Parameter:
176: . label - A DMLabel without cells

178:   Level: developer

180:   Note: This undoes DMPlexLabelAddCells()

182: .seealso: DMPlexLabelComplete(), DMPlexLabelCohesiveComplete(), DMPlexLabelAddCells()
183: @*/
184: PetscErrorCode DMPlexLabelClearCells(DM dm, DMLabel label)
185: {
186:   IS              valueIS;
187:   const PetscInt *values;
188:   PetscInt        numValues, v, cStart, cEnd, cEndInterior;
189:   PetscErrorCode  ierr;

192:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
193:   DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);
194:   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
195:   DMLabelGetNumValues(label, &numValues);
196:   DMLabelGetValueIS(label, &valueIS);
197:   ISGetIndices(valueIS, &values);
198:   for (v = 0; v < numValues; ++v) {
199:     IS              pointIS;
200:     const PetscInt *points;
201:     PetscInt        numPoints, p;

203:     DMLabelGetStratumSize(label, values[v], &numPoints);
204:     DMLabelGetStratumIS(label, values[v], &pointIS);
205:     ISGetIndices(pointIS, &points);
206:     for (p = 0; p < numPoints; ++p) {
207:       PetscInt point = points[p];

209:       if (point >= cStart && point < cEnd) {
210:         DMLabelClearValue(label,point,values[v]);
211:       }
212:     }
213:     ISRestoreIndices(pointIS, &points);
214:     ISDestroy(&pointIS);
215:   }
216:   ISRestoreIndices(valueIS, &values);
217:   ISDestroy(&valueIS);
218:   return(0);
219: }

221: /* take (oldEnd, added) pairs, ordered by height and convert them to (oldstart, newstart) pairs, ordered by ascending
222:  * index (skipping first, which is (0,0)) */
223: PETSC_STATIC_INLINE PetscErrorCode DMPlexShiftPointSetUp_Internal(PetscInt depth, PetscInt depthShift[])
224: {
225:   PetscInt d, off = 0;

228:   /* sort by (oldend): yes this is an O(n^2) sort, we expect depth <= 3 */
229:   for (d = 0; d < depth; d++) {
230:     PetscInt firstd = d;
231:     PetscInt firstStart = depthShift[2*d];
232:     PetscInt e;

234:     for (e = d+1; e <= depth; e++) {
235:       if (depthShift[2*e] < firstStart) {
236:         firstd = e;
237:         firstStart = depthShift[2*d];
238:       }
239:     }
240:     if (firstd != d) {
241:       PetscInt swap[2];

243:       e = firstd;
244:       swap[0] = depthShift[2*d];
245:       swap[1] = depthShift[2*d+1];
246:       depthShift[2*d]   = depthShift[2*e];
247:       depthShift[2*d+1] = depthShift[2*e+1];
248:       depthShift[2*e]   = swap[0];
249:       depthShift[2*e+1] = swap[1];
250:     }
251:   }
252:   /* convert (oldstart, added) to (oldstart, newstart) */
253:   for (d = 0; d <= depth; d++) {
254:     off += depthShift[2*d+1];
255:     depthShift[2*d+1] = depthShift[2*d] + off;
256:   }
257:   return(0);
258: }

260: /* depthShift is a list of (old, new) pairs */
261: PETSC_STATIC_INLINE PetscInt DMPlexShiftPoint_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
262: {
263:   PetscInt d;
264:   PetscInt newOff = 0;

266:   for (d = 0; d <= depth; d++) {
267:     if (p < depthShift[2*d]) return p + newOff;
268:     else newOff = depthShift[2*d+1] - depthShift[2*d];
269:   }
270:   return p + newOff;
271: }

273: /* depthShift is a list of (old, new) pairs */
274: PETSC_STATIC_INLINE PetscInt DMPlexShiftPointInverse_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
275: {
276:   PetscInt d;
277:   PetscInt newOff = 0;

279:   for (d = 0; d <= depth; d++) {
280:     if (p < depthShift[2*d+1]) return p + newOff;
281:     else newOff = depthShift[2*d] - depthShift[2*d+1];
282:   }
283:   return p + newOff;
284: }

286: static PetscErrorCode DMPlexShiftSizes_Internal(DM dm, PetscInt depthShift[], DM dmNew)
287: {
288:   PetscInt       depth = 0, d, pStart, pEnd, p;

292:   DMPlexGetDepth(dm, &depth);
293:   if (depth < 0) return(0);
294:   /* Step 1: Expand chart */
295:   DMPlexGetChart(dm, &pStart, &pEnd);
296:   pEnd = DMPlexShiftPoint_Internal(pEnd,depth,depthShift);
297:   DMPlexSetChart(dmNew, pStart, pEnd);
298:   /* Step 2: Set cone and support sizes */
299:   for (d = 0; d <= depth; ++d) {
300:     DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);
301:     for (p = pStart; p < pEnd; ++p) {
302:       PetscInt newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
303:       PetscInt size;

305:       DMPlexGetConeSize(dm, p, &size);
306:       DMPlexSetConeSize(dmNew, newp, size);
307:       DMPlexGetSupportSize(dm, p, &size);
308:       DMPlexSetSupportSize(dmNew, newp, size);
309:     }
310:   }
311:   return(0);
312: }

314: static PetscErrorCode DMPlexShiftPoints_Internal(DM dm, PetscInt depthShift[], DM dmNew)
315: {
316:   PetscInt      *newpoints;
317:   PetscInt       depth = 0, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, pStart, pEnd, p;

321:   DMPlexGetDepth(dm, &depth);
322:   if (depth < 0) return(0);
323:   DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);
324:   DMPlexGetMaxSizes(dmNew, &maxConeSizeNew, &maxSupportSizeNew);
325:   PetscMalloc1(PetscMax(PetscMax(maxConeSize, maxSupportSize), PetscMax(maxConeSizeNew, maxSupportSizeNew)),&newpoints);
326:   /* Step 5: Set cones and supports */
327:   DMPlexGetChart(dm, &pStart, &pEnd);
328:   for (p = pStart; p < pEnd; ++p) {
329:     const PetscInt *points = NULL, *orientations = NULL;
330:     PetscInt        size,sizeNew, i, newp = DMPlexShiftPoint_Internal(p, depth, depthShift);

332:     DMPlexGetConeSize(dm, p, &size);
333:     DMPlexGetCone(dm, p, &points);
334:     DMPlexGetConeOrientation(dm, p, &orientations);
335:     for (i = 0; i < size; ++i) {
336:       newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
337:     }
338:     DMPlexSetCone(dmNew, newp, newpoints);
339:     DMPlexSetConeOrientation(dmNew, newp, orientations);
340:     DMPlexGetSupportSize(dm, p, &size);
341:     DMPlexGetSupportSize(dmNew, newp, &sizeNew);
342:     DMPlexGetSupport(dm, p, &points);
343:     for (i = 0; i < size; ++i) {
344:       newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
345:     }
346:     for (i = size; i < sizeNew; ++i) newpoints[i] = 0;
347:     DMPlexSetSupport(dmNew, newp, newpoints);
348:   }
349:   PetscFree(newpoints);
350:   return(0);
351: }

353: static PetscErrorCode DMPlexShiftCoordinates_Internal(DM dm, PetscInt depthShift[], DM dmNew)
354: {
355:   PetscSection   coordSection, newCoordSection;
356:   Vec            coordinates, newCoordinates;
357:   PetscScalar   *coords, *newCoords;
358:   PetscInt       coordSize, sStart, sEnd;
359:   PetscInt       dim, depth = 0, cStart, cEnd, cStartNew, cEndNew, c, vStart, vEnd, vStartNew, vEndNew, v;
360:   PetscBool      hasCells;

364:   DMGetCoordinateDim(dm, &dim);
365:   DMPlexGetDepth(dm, &depth);
366:   /* Step 8: Convert coordinates */
367:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
368:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
369:   DMPlexGetDepthStratum(dmNew, 0, &vStartNew, &vEndNew);
370:   DMPlexGetHeightStratum(dmNew, 0, &cStartNew, &cEndNew);
371:   DMGetCoordinateSection(dm, &coordSection);
372:   PetscSectionCreate(PetscObjectComm((PetscObject)dm), &newCoordSection);
373:   PetscSectionSetNumFields(newCoordSection, 1);
374:   PetscSectionSetFieldComponents(newCoordSection, 0, dim);
375:   PetscSectionGetChart(coordSection, &sStart, &sEnd);
376:   hasCells = sStart == cStart ? PETSC_TRUE : PETSC_FALSE;
377:   PetscSectionSetChart(newCoordSection, hasCells ? cStartNew : vStartNew, vEndNew);
378:   if (hasCells) {
379:     for (c = cStart; c < cEnd; ++c) {
380:       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof;

382:       PetscSectionGetDof(coordSection, c, &dof);
383:       PetscSectionSetDof(newCoordSection, cNew, dof);
384:       PetscSectionSetFieldDof(newCoordSection, cNew, 0, dof);
385:     }
386:   }
387:   for (v = vStartNew; v < vEndNew; ++v) {
388:     PetscSectionSetDof(newCoordSection, v, dim);
389:     PetscSectionSetFieldDof(newCoordSection, v, 0, dim);
390:   }
391:   PetscSectionSetUp(newCoordSection);
392:   DMSetCoordinateSection(dmNew, PETSC_DETERMINE, newCoordSection);
393:   PetscSectionGetStorageSize(newCoordSection, &coordSize);
394:   VecCreate(PETSC_COMM_SELF, &newCoordinates);
395:   PetscObjectSetName((PetscObject) newCoordinates, "coordinates");
396:   VecSetSizes(newCoordinates, coordSize, PETSC_DETERMINE);
397:   VecSetBlockSize(newCoordinates, dim);
398:   VecSetType(newCoordinates,VECSTANDARD);
399:   DMSetCoordinatesLocal(dmNew, newCoordinates);
400:   DMGetCoordinatesLocal(dm, &coordinates);
401:   VecGetArray(coordinates, &coords);
402:   VecGetArray(newCoordinates, &newCoords);
403:   if (hasCells) {
404:     for (c = cStart; c < cEnd; ++c) {
405:       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof, off, noff, d;

407:       PetscSectionGetDof(coordSection, c, &dof);
408:       PetscSectionGetOffset(coordSection, c, &off);
409:       PetscSectionGetOffset(newCoordSection, cNew, &noff);
410:       for (d = 0; d < dof; ++d) newCoords[noff+d] = coords[off+d];
411:     }
412:   }
413:   for (v = vStart; v < vEnd; ++v) {
414:     PetscInt dof, off, noff, d;

416:     PetscSectionGetDof(coordSection, v, &dof);
417:     PetscSectionGetOffset(coordSection, v, &off);
418:     PetscSectionGetOffset(newCoordSection, DMPlexShiftPoint_Internal(v, depth, depthShift), &noff);
419:     for (d = 0; d < dof; ++d) newCoords[noff+d] = coords[off+d];
420:   }
421:   VecRestoreArray(coordinates, &coords);
422:   VecRestoreArray(newCoordinates, &newCoords);
423:   VecDestroy(&newCoordinates);
424:   PetscSectionDestroy(&newCoordSection);
425:   return(0);
426: }

428: static PetscErrorCode DMPlexShiftSF_Internal(DM dm, PetscInt depthShift[], DM dmNew)
429: {
430:   PetscInt           depth = 0;
431:   PetscSF            sfPoint, sfPointNew;
432:   const PetscSFNode *remotePoints;
433:   PetscSFNode       *gremotePoints;
434:   const PetscInt    *localPoints;
435:   PetscInt          *glocalPoints, *newLocation, *newRemoteLocation;
436:   PetscInt           numRoots, numLeaves, l, pStart, pEnd, totShift = 0;
437:   PetscErrorCode     ierr;

440:   DMPlexGetDepth(dm, &depth);
441:   /* Step 9: Convert pointSF */
442:   DMGetPointSF(dm, &sfPoint);
443:   DMGetPointSF(dmNew, &sfPointNew);
444:   DMPlexGetChart(dm, &pStart, &pEnd);
445:   PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);
446:   totShift = DMPlexShiftPoint_Internal(pEnd,depth,depthShift) - pEnd;
447:   if (numRoots >= 0) {
448:     PetscMalloc2(numRoots,&newLocation,pEnd-pStart,&newRemoteLocation);
449:     for (l=0; l<numRoots; l++) newLocation[l] = DMPlexShiftPoint_Internal(l, depth, depthShift);
450:     PetscSFBcastBegin(sfPoint, MPIU_INT, newLocation, newRemoteLocation);
451:     PetscSFBcastEnd(sfPoint, MPIU_INT, newLocation, newRemoteLocation);
452:     PetscMalloc1(numLeaves,    &glocalPoints);
453:     PetscMalloc1(numLeaves, &gremotePoints);
454:     for (l = 0; l < numLeaves; ++l) {
455:       glocalPoints[l]        = DMPlexShiftPoint_Internal(localPoints[l], depth, depthShift);
456:       gremotePoints[l].rank  = remotePoints[l].rank;
457:       gremotePoints[l].index = newRemoteLocation[localPoints[l]];
458:     }
459:     PetscFree2(newLocation,newRemoteLocation);
460:     PetscSFSetGraph(sfPointNew, numRoots + totShift, numLeaves, glocalPoints, PETSC_OWN_POINTER, gremotePoints, PETSC_OWN_POINTER);
461:   }
462:   return(0);
463: }

465: static PetscErrorCode DMPlexShiftLabels_Internal(DM dm, PetscInt depthShift[], DM dmNew)
466: {
467:   PetscSF            sfPoint;
468:   DMLabel            vtkLabel, ghostLabel;
469:   const PetscSFNode *leafRemote;
470:   const PetscInt    *leafLocal;
471:   PetscInt           depth = 0, numLeaves, numLabels, l, cStart, cEnd, c, fStart, fEnd, f;
472:   PetscMPIInt        rank;
473:   PetscErrorCode     ierr;

476:   DMPlexGetDepth(dm, &depth);
477:   /* Step 10: Convert labels */
478:   DMGetNumLabels(dm, &numLabels);
479:   for (l = 0; l < numLabels; ++l) {
480:     DMLabel         label, newlabel;
481:     const char     *lname;
482:     PetscBool       isDepth;
483:     IS              valueIS;
484:     const PetscInt *values;
485:     PetscInt        numValues, val;

487:     DMGetLabelName(dm, l, &lname);
488:     PetscStrcmp(lname, "depth", &isDepth);
489:     if (isDepth) continue;
490:     DMCreateLabel(dmNew, lname);
491:     DMGetLabel(dm, lname, &label);
492:     DMGetLabel(dmNew, lname, &newlabel);
493:     DMLabelGetDefaultValue(label,&val);
494:     DMLabelSetDefaultValue(newlabel,val);
495:     DMLabelGetValueIS(label, &valueIS);
496:     ISGetLocalSize(valueIS, &numValues);
497:     ISGetIndices(valueIS, &values);
498:     for (val = 0; val < numValues; ++val) {
499:       IS              pointIS;
500:       const PetscInt *points;
501:       PetscInt        numPoints, p;

503:       DMLabelGetStratumIS(label, values[val], &pointIS);
504:       ISGetLocalSize(pointIS, &numPoints);
505:       ISGetIndices(pointIS, &points);
506:       for (p = 0; p < numPoints; ++p) {
507:         const PetscInt newpoint = DMPlexShiftPoint_Internal(points[p], depth, depthShift);

509:         DMLabelSetValue(newlabel, newpoint, values[val]);
510:       }
511:       ISRestoreIndices(pointIS, &points);
512:       ISDestroy(&pointIS);
513:     }
514:     ISRestoreIndices(valueIS, &values);
515:     ISDestroy(&valueIS);
516:   }
517:   /* Step 11: Make label for output (vtk) and to mark ghost points (ghost) */
518:   MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
519:   DMGetPointSF(dm, &sfPoint);
520:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
521:   PetscSFGetGraph(sfPoint, NULL, &numLeaves, &leafLocal, &leafRemote);
522:   DMCreateLabel(dmNew, "vtk");
523:   DMCreateLabel(dmNew, "ghost");
524:   DMGetLabel(dmNew, "vtk", &vtkLabel);
525:   DMGetLabel(dmNew, "ghost", &ghostLabel);
526:   for (l = 0, c = cStart; l < numLeaves && c < cEnd; ++l, ++c) {
527:     for (; c < leafLocal[l] && c < cEnd; ++c) {
528:       DMLabelSetValue(vtkLabel, c, 1);
529:     }
530:     if (leafLocal[l] >= cEnd) break;
531:     if (leafRemote[l].rank == rank) {
532:       DMLabelSetValue(vtkLabel, c, 1);
533:     } else {
534:       DMLabelSetValue(ghostLabel, c, 2);
535:     }
536:   }
537:   for (; c < cEnd; ++c) {
538:     DMLabelSetValue(vtkLabel, c, 1);
539:   }
540:   if (0) {
541:     DMLabelView(vtkLabel, PETSC_VIEWER_STDOUT_WORLD);
542:   }
543:   DMPlexGetHeightStratum(dmNew, 1, &fStart, &fEnd);
544:   for (f = fStart; f < fEnd; ++f) {
545:     PetscInt numCells;

547:     DMPlexGetSupportSize(dmNew, f, &numCells);
548:     if (numCells < 2) {
549:       DMLabelSetValue(ghostLabel, f, 1);
550:     } else {
551:       const PetscInt *cells = NULL;
552:       PetscInt        vA, vB;

554:       DMPlexGetSupport(dmNew, f, &cells);
555:       DMLabelGetValue(vtkLabel, cells[0], &vA);
556:       DMLabelGetValue(vtkLabel, cells[1], &vB);
557:       if (vA != 1 && vB != 1) {DMLabelSetValue(ghostLabel, f, 1);}
558:     }
559:   }
560:   if (0) {
561:     DMLabelView(ghostLabel, PETSC_VIEWER_STDOUT_WORLD);
562:   }
563:   return(0);
564: }

566: static PetscErrorCode DMPlexShiftTree_Internal(DM dm, PetscInt depthShift[], DM dmNew)
567: {
568:   DM             refTree;
569:   PetscSection   pSec;
570:   PetscInt       *parents, *childIDs;

574:   DMPlexGetReferenceTree(dm,&refTree);
575:   DMPlexSetReferenceTree(dmNew,refTree);
576:   DMPlexGetTree(dm,&pSec,&parents,&childIDs,NULL,NULL);
577:   if (pSec) {
578:     PetscInt p, pStart, pEnd, *parentsShifted, pStartShifted, pEndShifted, depth;
579:     PetscInt *childIDsShifted;
580:     PetscSection pSecShifted;

582:     PetscSectionGetChart(pSec,&pStart,&pEnd);
583:     DMPlexGetDepth(dm,&depth);
584:     pStartShifted = DMPlexShiftPoint_Internal(pStart,depth,depthShift);
585:     pEndShifted   = DMPlexShiftPoint_Internal(pEnd,depth,depthShift);
586:     PetscMalloc2(pEndShifted - pStartShifted,&parentsShifted,pEndShifted-pStartShifted,&childIDsShifted);
587:     PetscSectionCreate(PetscObjectComm((PetscObject)dmNew),&pSecShifted);
588:     PetscSectionSetChart(pSecShifted,pStartShifted,pEndShifted);
589:     for (p = pStartShifted; p < pEndShifted; p++) {
590:       /* start off assuming no children */
591:       PetscSectionSetDof(pSecShifted,p,0);
592:     }
593:     for (p = pStart; p < pEnd; p++) {
594:       PetscInt dof;
595:       PetscInt pNew = DMPlexShiftPoint_Internal(p,depth,depthShift);

597:       PetscSectionGetDof(pSec,p,&dof);
598:       PetscSectionSetDof(pSecShifted,pNew,dof);
599:     }
600:     PetscSectionSetUp(pSecShifted);
601:     for (p = pStart; p < pEnd; p++) {
602:       PetscInt dof;
603:       PetscInt pNew = DMPlexShiftPoint_Internal(p,depth,depthShift);

605:       PetscSectionGetDof(pSec,p,&dof);
606:       if (dof) {
607:         PetscInt off, offNew;

609:         PetscSectionGetOffset(pSec,p,&off);
610:         PetscSectionGetOffset(pSecShifted,pNew,&offNew);
611:         parentsShifted[offNew] = DMPlexShiftPoint_Internal(parents[off],depth,depthShift);
612:         childIDsShifted[offNew] = childIDs[off];
613:       }
614:     }
615:     DMPlexSetTree(dmNew,pSecShifted,parentsShifted,childIDsShifted);
616:     PetscFree2(parentsShifted,childIDsShifted);
617:     PetscSectionDestroy(&pSecShifted);
618:   }
619:   return(0);
620: }

622: static PetscErrorCode DMPlexConstructGhostCells_Internal(DM dm, DMLabel label, PetscInt *numGhostCells, DM gdm)
623: {
624:   PetscSF         sf;
625:   IS              valueIS;
626:   const PetscInt *values, *leaves;
627:   PetscInt       *depthShift;
628:   PetscInt        d, depth = 0, nleaves, loc, Ng, numFS, fs, fStart, fEnd, ghostCell, cEnd, c;
629:   PetscErrorCode  ierr;

632:   DMGetPointSF(dm, &sf);
633:   PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);
634:   nleaves = PetscMax(0, nleaves);
635:   DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);
636:   /* Count ghost cells */
637:   DMLabelGetValueIS(label, &valueIS);
638:   ISGetLocalSize(valueIS, &numFS);
639:   ISGetIndices(valueIS, &values);
640:   Ng   = 0;
641:   for (fs = 0; fs < numFS; ++fs) {
642:     IS              faceIS;
643:     const PetscInt *faces;
644:     PetscInt        numFaces, f, numBdFaces = 0;

646:     DMLabelGetStratumIS(label, values[fs], &faceIS);
647:     ISGetLocalSize(faceIS, &numFaces);
648:     ISGetIndices(faceIS, &faces);
649:     for (f = 0; f < numFaces; ++f) {
650:       PetscInt numChildren;

652:       PetscFindInt(faces[f], nleaves, leaves, &loc);
653:       DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL);
654:       /* non-local and ancestors points don't get to register ghosts */
655:       if (loc >= 0 || numChildren) continue;
656:       if ((faces[f] >= fStart) && (faces[f] < fEnd)) ++numBdFaces;
657:     }
658:     Ng += numBdFaces;
659:     ISDestroy(&faceIS);
660:   }
661:   DMPlexGetDepth(dm, &depth);
662:   PetscMalloc1(2*(depth+1), &depthShift);
663:   for (d = 0; d <= depth; d++) {
664:     PetscInt dEnd;

666:     DMPlexGetDepthStratum(dm,d,NULL,&dEnd);
667:     depthShift[2*d]   = dEnd;
668:     depthShift[2*d+1] = 0;
669:   }
670:   if (depth >= 0) depthShift[2*depth+1] = Ng;
671:   DMPlexShiftPointSetUp_Internal(depth,depthShift);
672:   DMPlexShiftSizes_Internal(dm, depthShift, gdm);
673:   /* Step 3: Set cone/support sizes for new points */
674:   DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);
675:   DMPlexSetHybridBounds(gdm, cEnd, PETSC_DETERMINE, PETSC_DETERMINE, PETSC_DETERMINE);
676:   for (c = cEnd; c < cEnd + Ng; ++c) {
677:     DMPlexSetConeSize(gdm, c, 1);
678:   }
679:   for (fs = 0; fs < numFS; ++fs) {
680:     IS              faceIS;
681:     const PetscInt *faces;
682:     PetscInt        numFaces, f;

684:     DMLabelGetStratumIS(label, values[fs], &faceIS);
685:     ISGetLocalSize(faceIS, &numFaces);
686:     ISGetIndices(faceIS, &faces);
687:     for (f = 0; f < numFaces; ++f) {
688:       PetscInt size, numChildren;

690:       PetscFindInt(faces[f], nleaves, leaves, &loc);
691:       DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL);
692:       if (loc >= 0 || numChildren) continue;
693:       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
694:       DMPlexGetSupportSize(dm, faces[f], &size);
695:       if (size != 1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM has boundary face %d with %d support cells", faces[f], size);
696:       DMPlexSetSupportSize(gdm, faces[f] + Ng, 2);
697:     }
698:     ISRestoreIndices(faceIS, &faces);
699:     ISDestroy(&faceIS);
700:   }
701:   /* Step 4: Setup ghosted DM */
702:   DMSetUp(gdm);
703:   DMPlexShiftPoints_Internal(dm, depthShift, gdm);
704:   /* Step 6: Set cones and supports for new points */
705:   ghostCell = cEnd;
706:   for (fs = 0; fs < numFS; ++fs) {
707:     IS              faceIS;
708:     const PetscInt *faces;
709:     PetscInt        numFaces, f;

711:     DMLabelGetStratumIS(label, values[fs], &faceIS);
712:     ISGetLocalSize(faceIS, &numFaces);
713:     ISGetIndices(faceIS, &faces);
714:     for (f = 0; f < numFaces; ++f) {
715:       PetscInt newFace = faces[f] + Ng, numChildren;

717:       PetscFindInt(faces[f], nleaves, leaves, &loc);
718:       DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL);
719:       if (loc >= 0 || numChildren) continue;
720:       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
721:       DMPlexSetCone(gdm, ghostCell, &newFace);
722:       DMPlexInsertSupport(gdm, newFace, 1, ghostCell);
723:       ++ghostCell;
724:     }
725:     ISRestoreIndices(faceIS, &faces);
726:     ISDestroy(&faceIS);
727:   }
728:   ISRestoreIndices(valueIS, &values);
729:   ISDestroy(&valueIS);
730:   /* Step 7: Stratify */
731:   DMPlexStratify(gdm);
732:   DMPlexShiftCoordinates_Internal(dm, depthShift, gdm);
733:   DMPlexShiftSF_Internal(dm, depthShift, gdm);
734:   DMPlexShiftLabels_Internal(dm, depthShift, gdm);
735:   DMPlexShiftTree_Internal(dm, depthShift, gdm);
736:   PetscFree(depthShift);
737:   /* Step 7: Periodicity */
738:   if (dm->maxCell) {
739:     const PetscReal *maxCell, *L;
740:     const DMBoundaryType *bd;
741:     DMGetPeriodicity(dm,  &maxCell, &L, &bd);
742:     DMSetPeriodicity(gdm,  maxCell,  L,  bd);
743:   }
744:   if (numGhostCells) *numGhostCells = Ng;
745:   return(0);
746: }

748: /*@C
749:   DMPlexConstructGhostCells - Construct ghost cells which connect to every boundary face

751:   Collective on dm

753:   Input Parameters:
754: + dm - The original DM
755: - labelName - The label specifying the boundary faces, or "Face Sets" if this is NULL

757:   Output Parameters:
758: + numGhostCells - The number of ghost cells added to the DM
759: - dmGhosted - The new DM

761:   Note: If no label exists of that name, one will be created marking all boundary faces

763:   Level: developer

765: .seealso: DMCreate()
766: @*/
767: PetscErrorCode DMPlexConstructGhostCells(DM dm, const char labelName[], PetscInt *numGhostCells, DM *dmGhosted)
768: {
769:   DM             gdm;
770:   DMLabel        label;
771:   const char    *name = labelName ? labelName : "Face Sets";
772:   PetscInt       dim;
773:   PetscBool      flag;

780:   DMCreate(PetscObjectComm((PetscObject)dm), &gdm);
781:   DMSetType(gdm, DMPLEX);
782:   DMGetDimension(dm, &dim);
783:   DMSetDimension(gdm, dim);
784:   DMPlexGetAdjacencyUseCone(dm, &flag);
785:   DMPlexSetAdjacencyUseCone(gdm, flag);
786:   DMPlexGetAdjacencyUseClosure(dm, &flag);
787:   DMPlexSetAdjacencyUseClosure(gdm, flag);
788:   DMGetLabel(dm, name, &label);
789:   if (!label) {
790:     /* Get label for boundary faces */
791:     DMCreateLabel(dm, name);
792:     DMGetLabel(dm, name, &label);
793:     DMPlexMarkBoundaryFaces(dm, label);
794:   }
795:   DMPlexConstructGhostCells_Internal(dm, label, numGhostCells, gdm);
796:   DMCopyBoundary(dm, gdm);
797:   *dmGhosted = gdm;
798:   return(0);
799: }

801: /*
802:   We are adding three kinds of points here:
803:     Replicated:     Copies of points which exist in the mesh, such as vertices identified across a fault
804:     Non-replicated: Points which exist on the fault, but are not replicated
805:     Hybrid:         Entirely new points, such as cohesive cells

807:   When creating subsequent cohesive cells, we shift the old hybrid cells to the end of the numbering at
808:   each depth so that the new split/hybrid points can be inserted as a block.
809: */
810: static PetscErrorCode DMPlexConstructCohesiveCells_Internal(DM dm, DMLabel label, DM sdm)
811: {
812:   MPI_Comm         comm;
813:   IS               valueIS;
814:   PetscInt         numSP = 0;       /* The number of depths for which we have replicated points */
815:   const PetscInt  *values;          /* List of depths for which we have replicated points */
816:   IS              *splitIS;
817:   IS              *unsplitIS;
818:   PetscInt        *numSplitPoints;     /* The number of replicated points at each depth */
819:   PetscInt        *numUnsplitPoints;   /* The number of non-replicated points at each depth which still give rise to hybrid points */
820:   PetscInt        *numHybridPoints;    /* The number of new hybrid points at each depth */
821:   PetscInt        *numHybridPointsOld; /* The number of existing hybrid points at each depth */
822:   const PetscInt **splitPoints;        /* Replicated points for each depth */
823:   const PetscInt **unsplitPoints;      /* Non-replicated points for each depth */
824:   PetscSection     coordSection;
825:   Vec              coordinates;
826:   PetscScalar     *coords;
827:   PetscInt        *depthMax;           /* The first hybrid point at each depth in the original mesh */
828:   PetscInt        *depthEnd;           /* The point limit at each depth in the original mesh */
829:   PetscInt        *depthShift;         /* Number of replicated+hybrid points at each depth */
830:   PetscInt        *pMaxNew;            /* The first replicated point at each depth in the new mesh, hybrids come after this */
831:   PetscInt        *coneNew, *coneONew, *supportNew;
832:   PetscInt         shift = 100, shift2 = 200, depth = 0, dep, dim, d, sp, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, numLabels, vStart, vEnd, pEnd, p, v;
833:   PetscErrorCode   ierr;

836:   PetscObjectGetComm((PetscObject)dm,&comm);
837:   DMGetDimension(dm, &dim);
838:   DMPlexGetDepth(dm, &depth);
839:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
840:   /* Count split points and add cohesive cells */
841:   DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);
842:   PetscMalloc5(depth+1,&depthMax,depth+1,&depthEnd,2*(depth+1),&depthShift,depth+1,&pMaxNew,depth+1,&numHybridPointsOld);
843:   PetscMalloc7(depth+1,&splitIS,depth+1,&unsplitIS,depth+1,&numSplitPoints,depth+1,&numUnsplitPoints,depth+1,&numHybridPoints,depth+1,&splitPoints,depth+1,&unsplitPoints);
844:   DMPlexGetHybridBounds(dm, depth >= 0 ? &depthMax[depth] : NULL, depth>1 ? &depthMax[depth-1] : NULL, depth>2 ? &depthMax[1] : NULL, depth >= 0 ? &depthMax[0] : NULL);
845:   for (d = 0; d <= depth; ++d) {
846:     DMPlexGetDepthStratum(dm, d, NULL, &pMaxNew[d]);
847:     depthEnd[d]           = pMaxNew[d];
848:     depthMax[d]           = depthMax[d] < 0 ? depthEnd[d] : depthMax[d];
849:     numSplitPoints[d]     = 0;
850:     numUnsplitPoints[d]   = 0;
851:     numHybridPoints[d]    = 0;
852:     numHybridPointsOld[d] = depthMax[d] < 0 ? 0 : depthEnd[d] - depthMax[d];
853:     splitPoints[d]        = NULL;
854:     unsplitPoints[d]      = NULL;
855:     splitIS[d]            = NULL;
856:     unsplitIS[d]          = NULL;
857:     /* we are shifting the existing hybrid points with the stratum behind them, so
858:      * the split comes at the end of the normal points, i.e., at depthMax[d] */
859:     depthShift[2*d]       = depthMax[d];
860:     depthShift[2*d+1]     = 0;
861:   }
862:   if (label) {
863:     DMLabelGetValueIS(label, &valueIS);
864:     ISGetLocalSize(valueIS, &numSP);
865:     ISGetIndices(valueIS, &values);
866:   }
867:   for (sp = 0; sp < numSP; ++sp) {
868:     const PetscInt dep = values[sp];

870:     if ((dep < 0) || (dep > depth)) continue;
871:     DMLabelGetStratumIS(label, dep, &splitIS[dep]);
872:     if (splitIS[dep]) {
873:       ISGetLocalSize(splitIS[dep], &numSplitPoints[dep]);
874:       ISGetIndices(splitIS[dep], &splitPoints[dep]);
875:     }
876:     DMLabelGetStratumIS(label, shift2+dep, &unsplitIS[dep]);
877:     if (unsplitIS[dep]) {
878:       ISGetLocalSize(unsplitIS[dep], &numUnsplitPoints[dep]);
879:       ISGetIndices(unsplitIS[dep], &unsplitPoints[dep]);
880:     }
881:   }
882:   /* Calculate number of hybrid points */
883:   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   */
884:   for (d = 0; d <= depth; ++d) depthShift[2*d+1]      = numSplitPoints[d] + numHybridPoints[d];
885:   DMPlexShiftPointSetUp_Internal(depth,depthShift);
886:   /* the end of the points in this stratum that come before the new points:
887:    * shifting pMaxNew[d] gets the new start of the next stratum, then count back the old hybrid points and the newly
888:    * added points */
889:   for (d = 0; d <= depth; ++d) pMaxNew[d]             = DMPlexShiftPoint_Internal(pMaxNew[d],depth,depthShift) - (numHybridPointsOld[d] + numSplitPoints[d] + numHybridPoints[d]);
890:   DMPlexShiftSizes_Internal(dm, depthShift, sdm);
891:   /* Step 3: Set cone/support sizes for new points */
892:   for (dep = 0; dep <= depth; ++dep) {
893:     for (p = 0; p < numSplitPoints[dep]; ++p) {
894:       const PetscInt  oldp   = splitPoints[dep][p];
895:       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
896:       const PetscInt  splitp = p    + pMaxNew[dep];
897:       const PetscInt *support;
898:       PetscInt        coneSize, supportSize, qf, qn, qp, e;

900:       DMPlexGetConeSize(dm, oldp, &coneSize);
901:       DMPlexSetConeSize(sdm, splitp, coneSize);
902:       DMPlexGetSupportSize(dm, oldp, &supportSize);
903:       DMPlexSetSupportSize(sdm, splitp, supportSize);
904:       if (dep == depth-1) {
905:         const PetscInt hybcell = p + pMaxNew[dep+1] + numSplitPoints[dep+1];

907:         /* Add cohesive cells, they are prisms */
908:         DMPlexSetConeSize(sdm, hybcell, 2 + coneSize);
909:       } else if (dep == 0) {
910:         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1];

912:         DMPlexGetSupport(dm, oldp, &support);
913:         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
914:           PetscInt val;

916:           DMLabelGetValue(label, support[e], &val);
917:           if (val == 1) ++qf;
918:           if ((val == 1) || (val ==  (shift + 1))) ++qn;
919:           if ((val == 1) || (val == -(shift + 1))) ++qp;
920:         }
921:         /* Split old vertex: Edges into original vertex and new cohesive edge */
922:         DMPlexSetSupportSize(sdm, newp, qn+1);
923:         /* Split new vertex: Edges into split vertex and new cohesive edge */
924:         DMPlexSetSupportSize(sdm, splitp, qp+1);
925:         /* Add hybrid edge */
926:         DMPlexSetConeSize(sdm, hybedge, 2);
927:         DMPlexSetSupportSize(sdm, hybedge, qf);
928:       } else if (dep == dim-2) {
929:         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];

931:         DMPlexGetSupport(dm, oldp, &support);
932:         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
933:           PetscInt val;

935:           DMLabelGetValue(label, support[e], &val);
936:           if (val == dim-1) ++qf;
937:           if ((val == dim-1) || (val ==  (shift + dim-1))) ++qn;
938:           if ((val == dim-1) || (val == -(shift + dim-1))) ++qp;
939:         }
940:         /* Split old edge: Faces into original edge and cohesive face (positive side?) */
941:         DMPlexSetSupportSize(sdm, newp, qn+1);
942:         /* Split new edge: Faces into split edge and cohesive face (negative side?) */
943:         DMPlexSetSupportSize(sdm, splitp, qp+1);
944:         /* Add hybrid face */
945:         DMPlexSetConeSize(sdm, hybface, 4);
946:         DMPlexSetSupportSize(sdm, hybface, qf);
947:       }
948:     }
949:   }
950:   for (dep = 0; dep <= depth; ++dep) {
951:     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
952:       const PetscInt  oldp   = unsplitPoints[dep][p];
953:       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
954:       const PetscInt *support;
955:       PetscInt        coneSize, supportSize, qf, e, s;

957:       DMPlexGetConeSize(dm, oldp, &coneSize);
958:       DMPlexGetSupportSize(dm, oldp, &supportSize);
959:       DMPlexGetSupport(dm, oldp, &support);
960:       if (dep == 0) {
961:         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];

963:         /* Unsplit vertex: Edges into original vertex, split edges, and new cohesive edge twice */
964:         for (s = 0, qf = 0; s < supportSize; ++s, ++qf) {
965:           PetscFindInt(support[s], numSplitPoints[dep+1], splitPoints[dep+1], &e);
966:           if (e >= 0) ++qf;
967:         }
968:         DMPlexSetSupportSize(sdm, newp, qf+2);
969:         /* Add hybrid edge */
970:         DMPlexSetConeSize(sdm, hybedge, 2);
971:         for (e = 0, qf = 0; e < supportSize; ++e) {
972:           PetscInt val;

974:           DMLabelGetValue(label, support[e], &val);
975:           /* Split and unsplit edges produce hybrid faces */
976:           if (val == 1) ++qf;
977:           if (val == (shift2 + 1)) ++qf;
978:         }
979:         DMPlexSetSupportSize(sdm, hybedge, qf);
980:       } else if (dep == dim-2) {
981:         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
982:         PetscInt       val;

984:         for (e = 0, qf = 0; e < supportSize; ++e) {
985:           DMLabelGetValue(label, support[e], &val);
986:           if (val == dim-1) qf += 2;
987:           else              ++qf;
988:         }
989:         /* Unsplit edge: Faces into original edge, split face, and cohesive face twice */
990:         DMPlexSetSupportSize(sdm, newp, qf+2);
991:         /* Add hybrid face */
992:         for (e = 0, qf = 0; e < supportSize; ++e) {
993:           DMLabelGetValue(label, support[e], &val);
994:           if (val == dim-1) ++qf;
995:         }
996:         DMPlexSetConeSize(sdm, hybface, 4);
997:         DMPlexSetSupportSize(sdm, hybface, qf);
998:       }
999:     }
1000:   }
1001:   /* Step 4: Setup split DM */
1002:   DMSetUp(sdm);
1003:   DMPlexShiftPoints_Internal(dm, depthShift, sdm);
1004:   DMPlexGetMaxSizes(sdm, &maxConeSizeNew, &maxSupportSizeNew);
1005:   PetscMalloc3(PetscMax(maxConeSize, maxConeSizeNew)*3,&coneNew,PetscMax(maxConeSize, maxConeSizeNew)*3,&coneONew,PetscMax(maxSupportSize, maxSupportSizeNew),&supportNew);
1006:   /* Step 6: Set cones and supports for new points */
1007:   for (dep = 0; dep <= depth; ++dep) {
1008:     for (p = 0; p < numSplitPoints[dep]; ++p) {
1009:       const PetscInt  oldp   = splitPoints[dep][p];
1010:       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
1011:       const PetscInt  splitp = p    + pMaxNew[dep];
1012:       const PetscInt *cone, *support, *ornt;
1013:       PetscInt        coneSize, supportSize, q, qf, qn, qp, v, e, s;

1015:       DMPlexGetConeSize(dm, oldp, &coneSize);
1016:       DMPlexGetCone(dm, oldp, &cone);
1017:       DMPlexGetConeOrientation(dm, oldp, &ornt);
1018:       DMPlexGetSupportSize(dm, oldp, &supportSize);
1019:       DMPlexGetSupport(dm, oldp, &support);
1020:       if (dep == depth-1) {
1021:         PetscBool       hasUnsplit = PETSC_FALSE;
1022:         const PetscInt  hybcell    = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1023:         const PetscInt *supportF;

1025:         /* Split face:       copy in old face to new face to start */
1026:         DMPlexGetSupport(sdm, newp,  &supportF);
1027:         DMPlexSetSupport(sdm, splitp, supportF);
1028:         /* Split old face:   old vertices/edges in cone so no change */
1029:         /* Split new face:   new vertices/edges in cone */
1030:         for (q = 0; q < coneSize; ++q) {
1031:           PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);
1032:           if (v < 0) {
1033:             PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);
1034:             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);
1035:             coneNew[2+q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
1036:             hasUnsplit   = PETSC_TRUE;
1037:           } else {
1038:             coneNew[2+q] = v + pMaxNew[dep-1];
1039:             if (dep > 1) {
1040:               const PetscInt *econe;
1041:               PetscInt        econeSize, r, vs, vu;

1043:               DMPlexGetConeSize(dm, cone[q], &econeSize);
1044:               DMPlexGetCone(dm, cone[q], &econe);
1045:               for (r = 0; r < econeSize; ++r) {
1046:                 PetscFindInt(econe[r], numSplitPoints[dep-2],   splitPoints[dep-2],   &vs);
1047:                 PetscFindInt(econe[r], numUnsplitPoints[dep-2], unsplitPoints[dep-2], &vu);
1048:                 if (vs >= 0) continue;
1049:                 if (vu < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %d in split or unsplit points of depth %d", econe[r], dep-2);
1050:                 hasUnsplit   = PETSC_TRUE;
1051:               }
1052:             }
1053:           }
1054:         }
1055:         DMPlexSetCone(sdm, splitp, &coneNew[2]);
1056:         DMPlexSetConeOrientation(sdm, splitp, ornt);
1057:         /* Face support */
1058:         for (s = 0; s < supportSize; ++s) {
1059:           PetscInt val;

1061:           DMLabelGetValue(label, support[s], &val);
1062:           if (val < 0) {
1063:             /* Split old face:   Replace negative side cell with cohesive cell */
1064:              DMPlexInsertSupport(sdm, newp, s, hybcell);
1065:           } else {
1066:             /* Split new face:   Replace positive side cell with cohesive cell */
1067:             DMPlexInsertSupport(sdm, splitp, s, hybcell);
1068:             /* Get orientation for cohesive face */
1069:             {
1070:               const PetscInt *ncone, *nconeO;
1071:               PetscInt        nconeSize, nc;

1073:               DMPlexGetConeSize(dm, support[s], &nconeSize);
1074:               DMPlexGetCone(dm, support[s], &ncone);
1075:               DMPlexGetConeOrientation(dm, support[s], &nconeO);
1076:               for (nc = 0; nc < nconeSize; ++nc) {
1077:                 if (ncone[nc] == oldp) {
1078:                   coneONew[0] = nconeO[nc];
1079:                   break;
1080:                 }
1081:               }
1082:               if (nc >= nconeSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate face %d in neighboring cell %d", oldp, support[s]);
1083:             }
1084:           }
1085:         }
1086:         /* Cohesive cell:    Old and new split face, then new cohesive faces */
1087:         coneNew[0]  = newp;   /* Extracted negative side orientation above */
1088:         coneNew[1]  = splitp;
1089:         coneONew[1] = coneONew[0];
1090:         for (q = 0; q < coneSize; ++q) {
1091:           PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);
1092:           if (v < 0) {
1093:             PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);
1094:             coneNew[2+q]  = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1095:             coneONew[2+q] = 0;
1096:           } else {
1097:             coneNew[2+q]  = v + pMaxNew[dep] + numSplitPoints[dep];
1098:           }
1099:           coneONew[2+q] = 0;
1100:         }
1101:         DMPlexSetCone(sdm, hybcell, coneNew);
1102:         DMPlexSetConeOrientation(sdm, hybcell, coneONew);
1103:         /* Label the hybrid cells on the boundary of the split */
1104:         if (hasUnsplit) {DMLabelSetValue(label, -hybcell, dim);}
1105:       } else if (dep == 0) {
1106:         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1];

1108:         /* Split old vertex: Edges in old split faces and new cohesive edge */
1109:         for (e = 0, qn = 0; e < supportSize; ++e) {
1110:           PetscInt val;

1112:           DMLabelGetValue(label, support[e], &val);
1113:           if ((val == 1) || (val == (shift + 1))) {
1114:             supportNew[qn++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1115:           }
1116:         }
1117:         supportNew[qn] = hybedge;
1118:         DMPlexSetSupport(sdm, newp, supportNew);
1119:         /* Split new vertex: Edges in new split faces and new cohesive edge */
1120:         for (e = 0, qp = 0; e < supportSize; ++e) {
1121:           PetscInt val, edge;

1123:           DMLabelGetValue(label, support[e], &val);
1124:           if (val == 1) {
1125:             PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);
1126:             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
1127:             supportNew[qp++] = edge + pMaxNew[dep+1];
1128:           } else if (val == -(shift + 1)) {
1129:             supportNew[qp++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1130:           }
1131:         }
1132:         supportNew[qp] = hybedge;
1133:         DMPlexSetSupport(sdm, splitp, supportNew);
1134:         /* Hybrid edge:    Old and new split vertex */
1135:         coneNew[0] = newp;
1136:         coneNew[1] = splitp;
1137:         DMPlexSetCone(sdm, hybedge, coneNew);
1138:         for (e = 0, qf = 0; e < supportSize; ++e) {
1139:           PetscInt val, edge;

1141:           DMLabelGetValue(label, support[e], &val);
1142:           if (val == 1) {
1143:             PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);
1144:             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
1145:             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2];
1146:           }
1147:         }
1148:         DMPlexSetSupport(sdm, hybedge, supportNew);
1149:       } else if (dep == dim-2) {
1150:         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];

1152:         /* Split old edge:   old vertices in cone so no change */
1153:         /* Split new edge:   new vertices in cone */
1154:         for (q = 0; q < coneSize; ++q) {
1155:           PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);
1156:           if (v < 0) {
1157:             PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);
1158:             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);
1159:             coneNew[q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
1160:           } else {
1161:             coneNew[q] = v + pMaxNew[dep-1];
1162:           }
1163:         }
1164:         DMPlexSetCone(sdm, splitp, coneNew);
1165:         /* Split old edge: Faces in positive side cells and old split faces */
1166:         for (e = 0, q = 0; e < supportSize; ++e) {
1167:           PetscInt val;

1169:           DMLabelGetValue(label, support[e], &val);
1170:           if (val == dim-1) {
1171:             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1172:           } else if (val == (shift + dim-1)) {
1173:             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1174:           }
1175:         }
1176:         supportNew[q++] = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1177:         DMPlexSetSupport(sdm, newp, supportNew);
1178:         /* Split new edge: Faces in negative side cells and new split faces */
1179:         for (e = 0, q = 0; e < supportSize; ++e) {
1180:           PetscInt val, face;

1182:           DMLabelGetValue(label, support[e], &val);
1183:           if (val == dim-1) {
1184:             PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &face);
1185:             if (face < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[e]);
1186:             supportNew[q++] = face + pMaxNew[dep+1];
1187:           } else if (val == -(shift + dim-1)) {
1188:             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1189:           }
1190:         }
1191:         supportNew[q++] = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1192:         DMPlexSetSupport(sdm, splitp, supportNew);
1193:         /* Hybrid face */
1194:         coneNew[0] = newp;
1195:         coneNew[1] = splitp;
1196:         for (v = 0; v < coneSize; ++v) {
1197:           PetscInt vertex;
1198:           PetscFindInt(cone[v], numSplitPoints[dep-1], splitPoints[dep-1], &vertex);
1199:           if (vertex < 0) {
1200:             PetscFindInt(cone[v], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &vertex);
1201:             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);
1202:             coneNew[2+v] = vertex + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1203:           } else {
1204:             coneNew[2+v] = vertex + pMaxNew[dep] + numSplitPoints[dep];
1205:           }
1206:         }
1207:         DMPlexSetCone(sdm, hybface, coneNew);
1208:         for (e = 0, qf = 0; e < supportSize; ++e) {
1209:           PetscInt val, face;

1211:           DMLabelGetValue(label, support[e], &val);
1212:           if (val == dim-1) {
1213:             PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &face);
1214:             if (face < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[e]);
1215:             supportNew[qf++] = face + pMaxNew[dep+2] + numSplitPoints[dep+2];
1216:           }
1217:         }
1218:         DMPlexSetSupport(sdm, hybface, supportNew);
1219:       }
1220:     }
1221:   }
1222:   for (dep = 0; dep <= depth; ++dep) {
1223:     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
1224:       const PetscInt  oldp   = unsplitPoints[dep][p];
1225:       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
1226:       const PetscInt *cone, *support, *ornt;
1227:       PetscInt        coneSize, supportSize, supportSizeNew, q, qf, e, f, s;

1229:       DMPlexGetConeSize(dm, oldp, &coneSize);
1230:       DMPlexGetCone(dm, oldp, &cone);
1231:       DMPlexGetConeOrientation(dm, oldp, &ornt);
1232:       DMPlexGetSupportSize(dm, oldp, &supportSize);
1233:       DMPlexGetSupport(dm, oldp, &support);
1234:       if (dep == 0) {
1235:         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];

1237:         /* Unsplit vertex */
1238:         DMPlexGetSupportSize(sdm, newp, &supportSizeNew);
1239:         for (s = 0, q = 0; s < supportSize; ++s) {
1240:           supportNew[q++] = DMPlexShiftPoint_Internal(support[s], depth, depthShift) /*support[s] + depthOffset[dep+1]*/;
1241:           PetscFindInt(support[s], numSplitPoints[dep+1], splitPoints[dep+1], &e);
1242:           if (e >= 0) {
1243:             supportNew[q++] = e + pMaxNew[dep+1];
1244:           }
1245:         }
1246:         supportNew[q++] = hybedge;
1247:         supportNew[q++] = hybedge;
1248:         if (q != supportSizeNew) SETERRQ3(comm, PETSC_ERR_ARG_WRONG, "Support size %d != %d for vertex %d", q, supportSizeNew, newp);
1249:         DMPlexSetSupport(sdm, newp, supportNew);
1250:         /* Hybrid edge */
1251:         coneNew[0] = newp;
1252:         coneNew[1] = newp;
1253:         DMPlexSetCone(sdm, hybedge, coneNew);
1254:         for (e = 0, qf = 0; e < supportSize; ++e) {
1255:           PetscInt val, edge;

1257:           DMLabelGetValue(label, support[e], &val);
1258:           if (val == 1) {
1259:             PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);
1260:             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
1261:             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2];
1262:           } else if  (val ==  (shift2 + 1)) {
1263:             PetscFindInt(support[e], numUnsplitPoints[dep+1], unsplitPoints[dep+1], &edge);
1264:             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a unsplit edge", support[e]);
1265:             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2] + numSplitPoints[dep+1];
1266:           }
1267:         }
1268:         DMPlexSetSupport(sdm, hybedge, supportNew);
1269:       } else if (dep == dim-2) {
1270:         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];

1272:         /* Unsplit edge: Faces into original edge, split face, and hybrid face twice */
1273:         for (f = 0, qf = 0; f < supportSize; ++f) {
1274:           PetscInt val, face;

1276:           DMLabelGetValue(label, support[f], &val);
1277:           if (val == dim-1) {
1278:             PetscFindInt(support[f], numSplitPoints[dep+1], splitPoints[dep+1], &face);
1279:             if (face < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[f]);
1280:             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1281:             supportNew[qf++] = face + pMaxNew[dep+1];
1282:           } else {
1283:             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1284:           }
1285:         }
1286:         supportNew[qf++] = hybface;
1287:         supportNew[qf++] = hybface;
1288:         DMPlexGetSupportSize(sdm, newp, &supportSizeNew);
1289:         if (qf != supportSizeNew) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Support size for unsplit edge %d is %d != %d\n", newp, qf, supportSizeNew);
1290:         DMPlexSetSupport(sdm, newp, supportNew);
1291:         /* Add hybrid face */
1292:         coneNew[0] = newp;
1293:         coneNew[1] = newp;
1294:         PetscFindInt(cone[0], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);
1295:         if (v < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Vertex %d is not an unsplit vertex", cone[0]);
1296:         coneNew[2] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1297:         PetscFindInt(cone[1], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);
1298:         if (v < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Vertex %d is not an unsplit vertex", cone[1]);
1299:         coneNew[3] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1300:         DMPlexSetCone(sdm, hybface, coneNew);
1301:         for (f = 0, qf = 0; f < supportSize; ++f) {
1302:           PetscInt val, face;

1304:           DMLabelGetValue(label, support[f], &val);
1305:           if (val == dim-1) {
1306:             PetscFindInt(support[f], numSplitPoints[dep+1], splitPoints[dep+1], &face);
1307:             supportNew[qf++] = face + pMaxNew[dep+2] + numSplitPoints[dep+2];
1308:           }
1309:         }
1310:         DMPlexGetSupportSize(sdm, hybface, &supportSizeNew);
1311:         if (qf != supportSizeNew) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Support size for hybrid face %d is %d != %d\n", hybface, qf, supportSizeNew);
1312:         DMPlexSetSupport(sdm, hybface, supportNew);
1313:       }
1314:     }
1315:   }
1316:   /* Step 6b: Replace split points in negative side cones */
1317:   for (sp = 0; sp < numSP; ++sp) {
1318:     PetscInt        dep = values[sp];
1319:     IS              pIS;
1320:     PetscInt        numPoints;
1321:     const PetscInt *points;

1323:     if (dep >= 0) continue;
1324:     DMLabelGetStratumIS(label, dep, &pIS);
1325:     if (!pIS) continue;
1326:     dep  = -dep - shift;
1327:     ISGetLocalSize(pIS, &numPoints);
1328:     ISGetIndices(pIS, &points);
1329:     for (p = 0; p < numPoints; ++p) {
1330:       const PetscInt  oldp = points[p];
1331:       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*depthOffset[dep] + oldp*/;
1332:       const PetscInt *cone;
1333:       PetscInt        coneSize, c;
1334:       /* PetscBool       replaced = PETSC_FALSE; */

1336:       /* Negative edge: replace split vertex */
1337:       /* Negative cell: replace split face */
1338:       DMPlexGetConeSize(sdm, newp, &coneSize);
1339:       DMPlexGetCone(sdm, newp, &cone);
1340:       for (c = 0; c < coneSize; ++c) {
1341:         const PetscInt coldp = DMPlexShiftPointInverse_Internal(cone[c],depth,depthShift);
1342:         PetscInt       csplitp, cp, val;

1344:         DMLabelGetValue(label, coldp, &val);
1345:         if (val == dep-1) {
1346:           PetscFindInt(coldp, numSplitPoints[dep-1], splitPoints[dep-1], &cp);
1347:           if (cp < 0) SETERRQ2(comm, PETSC_ERR_ARG_WRONG, "Point %d is not a split point of dimension %d", oldp, dep-1);
1348:           csplitp  = pMaxNew[dep-1] + cp;
1349:           DMPlexInsertCone(sdm, newp, c, csplitp);
1350:           /* replaced = PETSC_TRUE; */
1351:         }
1352:       }
1353:       /* Cells with only a vertex or edge on the submesh have no replacement */
1354:       /* if (!replaced) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "The cone of point %d does not contain split points", oldp); */
1355:     }
1356:     ISRestoreIndices(pIS, &points);
1357:     ISDestroy(&pIS);
1358:   }
1359:   /* Step 7: Stratify */
1360:   DMPlexStratify(sdm);
1361:   /* Step 8: Coordinates */
1362:   DMPlexShiftCoordinates_Internal(dm, depthShift, sdm);
1363:   DMGetCoordinateSection(sdm, &coordSection);
1364:   DMGetCoordinatesLocal(sdm, &coordinates);
1365:   VecGetArray(coordinates, &coords);
1366:   for (v = 0; v < (numSplitPoints ? numSplitPoints[0] : 0); ++v) {
1367:     const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[0][v], depth, depthShift) /*depthOffset[0] + splitPoints[0][v]*/;
1368:     const PetscInt splitp = pMaxNew[0] + v;
1369:     PetscInt       dof, off, soff, d;

1371:     PetscSectionGetDof(coordSection, newp, &dof);
1372:     PetscSectionGetOffset(coordSection, newp, &off);
1373:     PetscSectionGetOffset(coordSection, splitp, &soff);
1374:     for (d = 0; d < dof; ++d) coords[soff+d] = coords[off+d];
1375:   }
1376:   VecRestoreArray(coordinates, &coords);
1377:   /* Step 9: SF, if I can figure this out we can split the mesh in parallel */
1378:   DMPlexShiftSF_Internal(dm, depthShift, sdm);
1379:   /* Step 10: Labels */
1380:   DMPlexShiftLabels_Internal(dm, depthShift, sdm);
1381:   DMGetNumLabels(sdm, &numLabels);
1382:   for (dep = 0; dep <= depth; ++dep) {
1383:     for (p = 0; p < numSplitPoints[dep]; ++p) {
1384:       const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[dep][p], depth, depthShift) /*depthOffset[dep] + splitPoints[dep][p]*/;
1385:       const PetscInt splitp = pMaxNew[dep] + p;
1386:       PetscInt       l;

1388:       for (l = 0; l < numLabels; ++l) {
1389:         DMLabel     mlabel;
1390:         const char *lname;
1391:         PetscInt    val;
1392:         PetscBool   isDepth;

1394:         DMGetLabelName(sdm, l, &lname);
1395:         PetscStrcmp(lname, "depth", &isDepth);
1396:         if (isDepth) continue;
1397:         DMGetLabel(sdm, lname, &mlabel);
1398:         DMLabelGetValue(mlabel, newp, &val);
1399:         if (val >= 0) {
1400:           DMLabelSetValue(mlabel, splitp, val);
1401: #if 0
1402:           /* Do not put cohesive edges into the label */
1403:           if (dep == 0) {
1404:             const PetscInt cedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1405:             DMLabelSetValue(mlabel, cedge, val);
1406:           } else if (dep == dim-2) {
1407:             const PetscInt cface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1408:             DMLabelSetValue(mlabel, cface, val);
1409:           }
1410:           /* Do not put cohesive faces into the label */
1411: #endif
1412:         }
1413:       }
1414:     }
1415:   }
1416:   for (sp = 0; sp < numSP; ++sp) {
1417:     const PetscInt dep = values[sp];

1419:     if ((dep < 0) || (dep > depth)) continue;
1420:     if (splitIS[dep]) {ISRestoreIndices(splitIS[dep], &splitPoints[dep]);}
1421:     ISDestroy(&splitIS[dep]);
1422:     if (unsplitIS[dep]) {ISRestoreIndices(unsplitIS[dep], &unsplitPoints[dep]);}
1423:     ISDestroy(&unsplitIS[dep]);
1424:   }
1425:   if (label) {
1426:     ISRestoreIndices(valueIS, &values);
1427:     ISDestroy(&valueIS);
1428:   }
1429:   for (d = 0; d <= depth; ++d) {
1430:     DMPlexGetDepthStratum(sdm, d, NULL, &pEnd);
1431:     pMaxNew[d] = pEnd - numHybridPoints[d] - numHybridPointsOld[d];
1432:   }
1433:   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);
1434:   PetscFree3(coneNew, coneONew, supportNew);
1435:   PetscFree5(depthMax, depthEnd, depthShift, pMaxNew, numHybridPointsOld);
1436:   PetscFree7(splitIS, unsplitIS, numSplitPoints, numUnsplitPoints, numHybridPoints, splitPoints, unsplitPoints);
1437:   return(0);
1438: }

1440: /*@C
1441:   DMPlexConstructCohesiveCells - Construct cohesive cells which split the face along an internal interface

1443:   Collective on dm

1445:   Input Parameters:
1446: + dm - The original DM
1447: - label - The label specifying the boundary faces (this could be auto-generated)

1449:   Output Parameters:
1450: - dmSplit - The new DM

1452:   Level: developer

1454: .seealso: DMCreate(), DMPlexLabelCohesiveComplete()
1455: @*/
1456: PetscErrorCode DMPlexConstructCohesiveCells(DM dm, DMLabel label, DM *dmSplit)
1457: {
1458:   DM             sdm;
1459:   PetscInt       dim;

1465:   DMCreate(PetscObjectComm((PetscObject)dm), &sdm);
1466:   DMSetType(sdm, DMPLEX);
1467:   DMGetDimension(dm, &dim);
1468:   DMSetDimension(sdm, dim);
1469:   switch (dim) {
1470:   case 2:
1471:   case 3:
1472:     DMPlexConstructCohesiveCells_Internal(dm, label, sdm);
1473:     break;
1474:   default:
1475:     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct cohesive cells for dimension %d", dim);
1476:   }
1477:   *dmSplit = sdm;
1478:   return(0);
1479: }

1481: /* Returns the side of the surface for a given cell with a face on the surface */
1482: static PetscErrorCode GetSurfaceSide_Static(DM dm, DM subdm, PetscInt numSubpoints, const PetscInt *subpoints, PetscInt cell, PetscInt face, PetscBool *pos)
1483: {
1484:   const PetscInt *cone, *ornt;
1485:   PetscInt        dim, coneSize, c;
1486:   PetscErrorCode  ierr;

1489:   *pos = PETSC_TRUE;
1490:   DMGetDimension(dm, &dim);
1491:   DMPlexGetConeSize(dm, cell, &coneSize);
1492:   DMPlexGetCone(dm, cell, &cone);
1493:   DMPlexGetConeOrientation(dm, cell, &ornt);
1494:   for (c = 0; c < coneSize; ++c) {
1495:     if (cone[c] == face) {
1496:       PetscInt o = ornt[c];

1498:       if (subdm) {
1499:         const PetscInt *subcone, *subornt;
1500:         PetscInt        subpoint, subface, subconeSize, sc;

1502:         PetscFindInt(cell, numSubpoints, subpoints, &subpoint);
1503:         PetscFindInt(face, numSubpoints, subpoints, &subface);
1504:         DMPlexGetConeSize(subdm, subpoint, &subconeSize);
1505:         DMPlexGetCone(subdm, subpoint, &subcone);
1506:         DMPlexGetConeOrientation(subdm, subpoint, &subornt);
1507:         for (sc = 0; sc < subconeSize; ++sc) {
1508:           if (subcone[sc] == subface) {
1509:             o = subornt[0];
1510:             break;
1511:           }
1512:         }
1513:         if (sc >= subconeSize) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find subpoint %d (%d) in cone for subpoint %d (%d)", subface, face, subpoint, cell);
1514:       }
1515:       if (o >= 0) *pos = PETSC_TRUE;
1516:       else        *pos = PETSC_FALSE;
1517:       break;
1518:     }
1519:   }
1520:   if (c == coneSize) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Cell %d in split face %d support does not have it in the cone", cell, face);
1521:   return(0);
1522: }

1524: /*@
1525:   DMPlexLabelCohesiveComplete - Starting with a label marking points on an internal surface, we add all other mesh pieces
1526:   to complete the surface

1528:   Input Parameters:
1529: + dm     - The DM
1530: . label  - A DMLabel marking the surface
1531: . blabel - A DMLabel marking the vertices on the boundary which will not be duplicated, or NULL to find them automatically
1532: . flip   - Flag to flip the submesh normal and replace points on the other side
1533: - subdm  - The subDM associated with the label, or NULL

1535:   Output Parameter:
1536: . label - A DMLabel marking all surface points

1538:   Note: The vertices in blabel are called "unsplit" in the terminology from hybrid cell creation.

1540:   Level: developer

1542: .seealso: DMPlexConstructCohesiveCells(), DMPlexLabelComplete()
1543: @*/
1544: PetscErrorCode DMPlexLabelCohesiveComplete(DM dm, DMLabel label, DMLabel blabel, PetscBool flip, DM subdm)
1545: {
1546:   DMLabel         depthLabel;
1547:   IS              dimIS, subpointIS = NULL, facePosIS, faceNegIS, crossEdgeIS = NULL;
1548:   const PetscInt *points, *subpoints;
1549:   const PetscInt  rev   = flip ? -1 : 1;
1550:   PetscInt       *pMax;
1551:   PetscInt        shift = 100, shift2 = 200, dim, depth, pSize, dep, cStart, cEnd, cMax, fStart, fEnd, vStart, vEnd, numPoints, numSubpoints, p, val;
1552:   PetscErrorCode  ierr;

1555:   DMPlexGetDepth(dm, &depth);
1556:   DMGetDimension(dm, &dim);
1557:   pSize = PetscMax(depth, dim) + 1;
1558:   PetscMalloc1(pSize,&pMax);
1559:   DMPlexGetHybridBounds(dm, depth >= 0 ? &pMax[depth] : NULL, depth>1 ? &pMax[depth-1] : NULL, depth>2 ? &pMax[1] : NULL, &pMax[0]);
1560:   DMPlexGetDepthLabel(dm, &depthLabel);
1561:   DMGetDimension(dm, &dim);
1562:   if (subdm) {
1563:     DMPlexCreateSubpointIS(subdm, &subpointIS);
1564:     if (subpointIS) {
1565:       ISGetLocalSize(subpointIS, &numSubpoints);
1566:       ISGetIndices(subpointIS, &subpoints);
1567:     }
1568:   }
1569:   /* Mark cell on the fault, and its faces which touch the fault: cell orientation for face gives the side of the fault */
1570:   DMLabelGetStratumIS(label, dim-1, &dimIS);
1571:   if (!dimIS) {
1572:     PetscFree(pMax);
1573:     ISDestroy(&subpointIS);
1574:     return(0);
1575:   }
1576:   ISGetLocalSize(dimIS, &numPoints);
1577:   ISGetIndices(dimIS, &points);
1578:   for (p = 0; p < numPoints; ++p) { /* Loop over fault faces */
1579:     const PetscInt *support;
1580:     PetscInt        supportSize, s;

1582:     DMPlexGetSupportSize(dm, points[p], &supportSize);
1583:     if (supportSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Split face %d has %d != 2 supports", points[p], supportSize);
1584:     DMPlexGetSupport(dm, points[p], &support);
1585:     for (s = 0; s < supportSize; ++s) {
1586:       const PetscInt *cone;
1587:       PetscInt        coneSize, c;
1588:       PetscBool       pos;

1590:       GetSurfaceSide_Static(dm, subdm, numSubpoints, subpoints, support[s], points[p], &pos);
1591:       if (pos) {DMLabelSetValue(label, support[s],  rev*(shift+dim));}
1592:       else     {DMLabelSetValue(label, support[s], -rev*(shift+dim));}
1593:       if (rev < 0) pos = !pos ? PETSC_TRUE : PETSC_FALSE;
1594:       /* Put faces touching the fault in the label */
1595:       DMPlexGetConeSize(dm, support[s], &coneSize);
1596:       DMPlexGetCone(dm, support[s], &cone);
1597:       for (c = 0; c < coneSize; ++c) {
1598:         const PetscInt point = cone[c];

1600:         DMLabelGetValue(label, point, &val);
1601:         if (val == -1) {
1602:           PetscInt *closure = NULL;
1603:           PetscInt  closureSize, cl;

1605:           DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);
1606:           for (cl = 0; cl < closureSize*2; cl += 2) {
1607:             const PetscInt clp  = closure[cl];
1608:             PetscInt       bval = -1;

1610:             DMLabelGetValue(label, clp, &val);
1611:             if (blabel) {DMLabelGetValue(blabel, clp, &bval);}
1612:             if ((val >= 0) && (val < dim-1) && (bval < 0)) {
1613:               DMLabelSetValue(label, point, pos == PETSC_TRUE ? shift+dim-1 : -(shift+dim-1));
1614:               break;
1615:             }
1616:           }
1617:           DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);
1618:         }
1619:       }
1620:     }
1621:   }
1622:   ISRestoreIndices(dimIS, &points);
1623:   ISDestroy(&dimIS);
1624:   if (subpointIS) {ISRestoreIndices(subpointIS, &subpoints);}
1625:   ISDestroy(&subpointIS);
1626:   /* Mark boundary points as unsplit */
1627:   if (blabel) {
1628:     DMLabelGetStratumIS(blabel, 1, &dimIS);
1629:     ISGetLocalSize(dimIS, &numPoints);
1630:     ISGetIndices(dimIS, &points);
1631:     for (p = 0; p < numPoints; ++p) {
1632:       const PetscInt point = points[p];
1633:       PetscInt       val, bval;

1635:       DMLabelGetValue(blabel, point, &bval);
1636:       if (bval >= 0) {
1637:         DMLabelGetValue(label, point, &val);
1638:         if ((val < 0) || (val > dim)) {
1639:           /* This could be a point added from splitting a vertex on an adjacent fault, otherwise its just wrong */
1640:           DMLabelClearValue(blabel, point, bval);
1641:         }
1642:       }
1643:     }
1644:     for (p = 0; p < numPoints; ++p) {
1645:       const PetscInt point = points[p];
1646:       PetscInt       val, bval;

1648:       DMLabelGetValue(blabel, point, &bval);
1649:       if (bval >= 0) {
1650:         const PetscInt *cone,    *support;
1651:         PetscInt        coneSize, supportSize, s, valA, valB, valE;

1653:         /* Mark as unsplit */
1654:         DMLabelGetValue(label, point, &val);
1655:         if ((val < 0) || (val > dim)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %d has label value %d, should be part of the fault", point, val);
1656:         DMLabelClearValue(label, point, val);
1657:         DMLabelSetValue(label, point, shift2+val);
1658:         /* Check for cross-edge
1659:              A cross-edge has endpoints which are both on the boundary of the surface, but the edge itself is not. */
1660:         if (val != 0) continue;
1661:         DMPlexGetSupport(dm, point, &support);
1662:         DMPlexGetSupportSize(dm, point, &supportSize);
1663:         for (s = 0; s < supportSize; ++s) {
1664:           DMPlexGetCone(dm, support[s], &cone);
1665:           DMPlexGetConeSize(dm, support[s], &coneSize);
1666:           if (coneSize != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Edge %D has %D vertices != 2", support[s], coneSize);
1667:           DMLabelGetValue(blabel, cone[0], &valA);
1668:           DMLabelGetValue(blabel, cone[1], &valB);
1669:           DMLabelGetValue(blabel, support[s], &valE);
1670:           if ((valE < 0) && (valA >= 0) && (valB >= 0) && (cone[0] != cone[1])) {DMLabelSetValue(blabel, support[s], 2);}
1671:         }
1672:       }
1673:     }
1674:     ISRestoreIndices(dimIS, &points);
1675:     ISDestroy(&dimIS);
1676:   }
1677:   /* Search for other cells/faces/edges connected to the fault by a vertex */
1678:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
1679:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
1680:   DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);
1681:   cMax = cMax < 0 ? cEnd : cMax;
1682:   DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);
1683:   DMLabelGetStratumIS(label, 0, &dimIS);
1684:   if (blabel) {DMLabelGetStratumIS(blabel, 2, &crossEdgeIS);}
1685:   if (dimIS && crossEdgeIS) {
1686:     IS vertIS = dimIS;

1688:     ISExpand(vertIS, crossEdgeIS, &dimIS);
1689:     ISDestroy(&crossEdgeIS);
1690:     ISDestroy(&vertIS);
1691:   }
1692:   if (!dimIS) {
1693:     PetscFree(pMax);
1694:     return(0);
1695:   }
1696:   ISGetLocalSize(dimIS, &numPoints);
1697:   ISGetIndices(dimIS, &points);
1698:   for (p = 0; p < numPoints; ++p) { /* Loop over fault vertices */
1699:     PetscInt *star = NULL;
1700:     PetscInt  starSize, s;
1701:     PetscInt  again = 1;  /* 0: Finished 1: Keep iterating after a change 2: No change */

1703:     /* All points connected to the fault are inside a cell, so at the top level we will only check cells */
1704:     DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &starSize, &star);
1705:     while (again) {
1706:       if (again > 1) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Could not classify all cells connected to the fault");
1707:       again = 0;
1708:       for (s = 0; s < starSize*2; s += 2) {
1709:         const PetscInt  point = star[s];
1710:         const PetscInt *cone;
1711:         PetscInt        coneSize, c;

1713:         if ((point < cStart) || (point >= cMax)) continue;
1714:         DMLabelGetValue(label, point, &val);
1715:         if (val != -1) continue;
1716:         again = again == 1 ? 1 : 2;
1717:         DMPlexGetConeSize(dm, point, &coneSize);
1718:         DMPlexGetCone(dm, point, &cone);
1719:         for (c = 0; c < coneSize; ++c) {
1720:           DMLabelGetValue(label, cone[c], &val);
1721:           if (val != -1) {
1722:             const PetscInt *ccone;
1723:             PetscInt        cconeSize, cc, side;

1725:             if (PetscAbs(val) < shift) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %d on cell %d has an invalid label %d", cone[c], point, val);
1726:             if (val > 0) side =  1;
1727:             else         side = -1;
1728:             DMLabelSetValue(label, point, side*(shift+dim));
1729:             /* Mark cell faces which touch the fault */
1730:             DMPlexGetConeSize(dm, point, &cconeSize);
1731:             DMPlexGetCone(dm, point, &ccone);
1732:             for (cc = 0; cc < cconeSize; ++cc) {
1733:               PetscInt *closure = NULL;
1734:               PetscInt  closureSize, cl;

1736:               DMLabelGetValue(label, ccone[cc], &val);
1737:               if (val != -1) continue;
1738:               DMPlexGetTransitiveClosure(dm, ccone[cc], PETSC_TRUE, &closureSize, &closure);
1739:               for (cl = 0; cl < closureSize*2; cl += 2) {
1740:                 const PetscInt clp = closure[cl];

1742:                 DMLabelGetValue(label, clp, &val);
1743:                 if (val == -1) continue;
1744:                 DMLabelSetValue(label, ccone[cc], side*(shift+dim-1));
1745:                 break;
1746:               }
1747:               DMPlexRestoreTransitiveClosure(dm, ccone[cc], PETSC_TRUE, &closureSize, &closure);
1748:             }
1749:             again = 1;
1750:             break;
1751:           }
1752:         }
1753:       }
1754:     }
1755:     /* Classify the rest by cell membership */
1756:     for (s = 0; s < starSize*2; s += 2) {
1757:       const PetscInt point = star[s];

1759:       DMLabelGetValue(label, point, &val);
1760:       if (val == -1) {
1761:         PetscInt  *sstar = NULL;
1762:         PetscInt   sstarSize, ss;
1763:         PetscBool  marked = PETSC_FALSE;

1765:         DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &sstarSize, &sstar);
1766:         for (ss = 0; ss < sstarSize*2; ss += 2) {
1767:           const PetscInt spoint = sstar[ss];

1769:           if ((spoint < cStart) || (spoint >= cMax)) continue;
1770:           DMLabelGetValue(label, spoint, &val);
1771:           if (val == -1) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %d in star of %d does not have a valid label", spoint, point);
1772:           DMLabelGetValue(depthLabel, point, &dep);
1773:           if (val > 0) {
1774:             DMLabelSetValue(label, point,   shift+dep);
1775:           } else {
1776:             DMLabelSetValue(label, point, -(shift+dep));
1777:           }
1778:           marked = PETSC_TRUE;
1779:           break;
1780:         }
1781:         DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &sstarSize, &sstar);
1782:         DMLabelGetValue(depthLabel, point, &dep);
1783:         if (point < pMax[dep] && !marked) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Point %d could not be classified", point);
1784:       }
1785:     }
1786:     DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &starSize, &star);
1787:   }
1788:   ISRestoreIndices(dimIS, &points);
1789:   ISDestroy(&dimIS);
1790:   /* If any faces touching the fault divide cells on either side, split them */
1791:   DMLabelGetStratumIS(label,   shift+dim-1,  &facePosIS);
1792:   DMLabelGetStratumIS(label, -(shift+dim-1), &faceNegIS);
1793:   ISExpand(facePosIS, faceNegIS, &dimIS);
1794:   ISDestroy(&facePosIS);
1795:   ISDestroy(&faceNegIS);
1796:   ISGetLocalSize(dimIS, &numPoints);
1797:   ISGetIndices(dimIS, &points);
1798:   for (p = 0; p < numPoints; ++p) {
1799:     const PetscInt  point = points[p];
1800:     const PetscInt *support;
1801:     PetscInt        supportSize, valA, valB;

1803:     DMPlexGetSupportSize(dm, point, &supportSize);
1804:     if (supportSize != 2) continue;
1805:     DMPlexGetSupport(dm, point, &support);
1806:     DMLabelGetValue(label, support[0], &valA);
1807:     DMLabelGetValue(label, support[1], &valB);
1808:     if ((valA == -1) || (valB == -1)) continue;
1809:     if (valA*valB > 0) continue;
1810:     /* Split the face */
1811:     DMLabelGetValue(label, point, &valA);
1812:     DMLabelClearValue(label, point, valA);
1813:     DMLabelSetValue(label, point, dim-1);
1814:     /* Label its closure:
1815:       unmarked: label as unsplit
1816:       incident: relabel as split
1817:       split:    do nothing
1818:     */
1819:     {
1820:       PetscInt *closure = NULL;
1821:       PetscInt  closureSize, cl;

1823:       DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);
1824:       for (cl = 0; cl < closureSize*2; cl += 2) {
1825:         DMLabelGetValue(label, closure[cl], &valA);
1826:         if (valA == -1) { /* Mark as unsplit */
1827:           DMLabelGetValue(depthLabel, closure[cl], &dep);
1828:           DMLabelSetValue(label, closure[cl], shift2+dep);
1829:         } else if (((valA >= shift) && (valA < shift2)) || ((valA <= -shift) && (valA > -shift2))) {
1830:           DMLabelGetValue(depthLabel, closure[cl], &dep);
1831:           DMLabelClearValue(label, closure[cl], valA);
1832:           DMLabelSetValue(label, closure[cl], dep);
1833:         }
1834:       }
1835:       DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);
1836:     }
1837:   }
1838:   ISRestoreIndices(dimIS, &points);
1839:   ISDestroy(&dimIS);
1840:   PetscFree(pMax);
1841:   return(0);
1842: }

1844: /*@
1845:   DMPlexCreateHybridMesh - Create a mesh with hybrid cells along an internal interface

1847:   Collective on dm

1849:   Input Parameters:
1850: + dm - The original DM
1851: - labelName - The label specifying the interface vertices

1853:   Output Parameters:
1854: + hybridLabel - The label fully marking the interface
1855: - dmHybrid - The new DM

1857:   Level: developer

1859: .seealso: DMPlexConstructCohesiveCells(), DMPlexLabelCohesiveComplete(), DMCreate()
1860: @*/
1861: PetscErrorCode DMPlexCreateHybridMesh(DM dm, DMLabel label, DMLabel *hybridLabel, DM *dmHybrid)
1862: {
1863:   DM             idm;
1864:   DMLabel        subpointMap, hlabel;
1865:   PetscInt       dim;

1872:   DMGetDimension(dm, &dim);
1873:   DMPlexCreateSubmesh(dm, label, 1, &idm);
1874:   DMPlexOrient(idm);
1875:   DMPlexGetSubpointMap(idm, &subpointMap);
1876:   DMLabelDuplicate(subpointMap, &hlabel);
1877:   DMLabelClearStratum(hlabel, dim);
1878:   DMPlexLabelCohesiveComplete(dm, hlabel, NULL, PETSC_FALSE, idm);
1879:   DMDestroy(&idm);
1880:   DMPlexConstructCohesiveCells(dm, hlabel, dmHybrid);
1881:   if (hybridLabel) *hybridLabel = hlabel;
1882:   else             {DMLabelDestroy(&hlabel);}
1883:   return(0);
1884: }

1886: /* Here we need the explicit assumption that:

1888:      For any marked cell, the marked vertices constitute a single face
1889: */
1890: static PetscErrorCode DMPlexMarkSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, DM subdm)
1891: {
1892:   IS               subvertexIS = NULL;
1893:   const PetscInt  *subvertices;
1894:   PetscInt        *pStart, *pEnd, *pMax, pSize;
1895:   PetscInt         depth, dim, d, numSubVerticesInitial = 0, v;
1896:   PetscErrorCode   ierr;

1899:   *numFaces = 0;
1900:   *nFV      = 0;
1901:   DMPlexGetDepth(dm, &depth);
1902:   DMGetDimension(dm, &dim);
1903:   pSize = PetscMax(depth, dim) + 1;
1904:   PetscMalloc3(pSize,&pStart,pSize,&pEnd,pSize,&pMax);
1905:   DMPlexGetHybridBounds(dm, depth >= 0 ? &pMax[depth] : NULL, depth>1 ? &pMax[depth-1] : NULL, depth>2 ? &pMax[1] : NULL, &pMax[0]);
1906:   for (d = 0; d <= depth; ++d) {
1907:     DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);
1908:     if (pMax[d] >= 0) pEnd[d] = PetscMin(pEnd[d], pMax[d]);
1909:   }
1910:   /* Loop over initial vertices and mark all faces in the collective star() */
1911:   if (vertexLabel) {DMLabelGetStratumIS(vertexLabel, value, &subvertexIS);}
1912:   if (subvertexIS) {
1913:     ISGetSize(subvertexIS, &numSubVerticesInitial);
1914:     ISGetIndices(subvertexIS, &subvertices);
1915:   }
1916:   for (v = 0; v < numSubVerticesInitial; ++v) {
1917:     const PetscInt vertex = subvertices[v];
1918:     PetscInt      *star   = NULL;
1919:     PetscInt       starSize, s, numCells = 0, c;

1921:     DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);
1922:     for (s = 0; s < starSize*2; s += 2) {
1923:       const PetscInt point = star[s];
1924:       if ((point >= pStart[depth]) && (point < pEnd[depth])) star[numCells++] = point;
1925:     }
1926:     for (c = 0; c < numCells; ++c) {
1927:       const PetscInt cell    = star[c];
1928:       PetscInt      *closure = NULL;
1929:       PetscInt       closureSize, cl;
1930:       PetscInt       cellLoc, numCorners = 0, faceSize = 0;

1932:       DMLabelGetValue(subpointMap, cell, &cellLoc);
1933:       if (cellLoc == 2) continue;
1934:       if (cellLoc >= 0) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %d has dimension %d in the surface label", cell, cellLoc);
1935:       DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);
1936:       for (cl = 0; cl < closureSize*2; cl += 2) {
1937:         const PetscInt point = closure[cl];
1938:         PetscInt       vertexLoc;

1940:         if ((point >= pStart[0]) && (point < pEnd[0])) {
1941:           ++numCorners;
1942:           DMLabelGetValue(vertexLabel, point, &vertexLoc);
1943:           if (vertexLoc == value) closure[faceSize++] = point;
1944:         }
1945:       }
1946:       if (!(*nFV)) {DMPlexGetNumFaceVertices(dm, dim, numCorners, nFV);}
1947:       if (faceSize > *nFV) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %d of an element on the surface", faceSize);
1948:       if (faceSize == *nFV) {
1949:         const PetscInt *cells = NULL;
1950:         PetscInt        numCells, nc;

1952:         ++(*numFaces);
1953:         for (cl = 0; cl < faceSize; ++cl) {
1954:           DMLabelSetValue(subpointMap, closure[cl], 0);
1955:         }
1956:         DMPlexGetJoin(dm, faceSize, closure, &numCells, &cells);
1957:         for (nc = 0; nc < numCells; ++nc) {
1958:           DMLabelSetValue(subpointMap, cells[nc], 2);
1959:         }
1960:         DMPlexRestoreJoin(dm, faceSize, closure, &numCells, &cells);
1961:       }
1962:       DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);
1963:     }
1964:     DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);
1965:   }
1966:   if (subvertexIS) {
1967:     ISRestoreIndices(subvertexIS, &subvertices);
1968:   }
1969:   ISDestroy(&subvertexIS);
1970:   PetscFree3(pStart,pEnd,pMax);
1971:   return(0);
1972: }

1974: static PetscErrorCode DMPlexMarkSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, DM subdm)
1975: {
1976:   IS               subvertexIS = NULL;
1977:   const PetscInt  *subvertices;
1978:   PetscInt        *pStart, *pEnd, *pMax;
1979:   PetscInt         dim, d, numSubVerticesInitial = 0, v;
1980:   PetscErrorCode   ierr;

1983:   DMGetDimension(dm, &dim);
1984:   PetscMalloc3(dim+1,&pStart,dim+1,&pEnd,dim+1,&pMax);
1985:   DMPlexGetHybridBounds(dm, &pMax[dim], dim>1 ? &pMax[dim-1] : NULL, dim > 2 ? &pMax[1] : NULL, &pMax[0]);
1986:   for (d = 0; d <= dim; ++d) {
1987:     DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);
1988:     if (pMax[d] >= 0) pEnd[d] = PetscMin(pEnd[d], pMax[d]);
1989:   }
1990:   /* Loop over initial vertices and mark all faces in the collective star() */
1991:   if (vertexLabel) {
1992:     DMLabelGetStratumIS(vertexLabel, value, &subvertexIS);
1993:     if (subvertexIS) {
1994:       ISGetSize(subvertexIS, &numSubVerticesInitial);
1995:       ISGetIndices(subvertexIS, &subvertices);
1996:     }
1997:   }
1998:   for (v = 0; v < numSubVerticesInitial; ++v) {
1999:     const PetscInt vertex = subvertices[v];
2000:     PetscInt      *star   = NULL;
2001:     PetscInt       starSize, s, numFaces = 0, f;

2003:     DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);
2004:     for (s = 0; s < starSize*2; s += 2) {
2005:       const PetscInt point = star[s];
2006:       if ((point >= pStart[dim-1]) && (point < pEnd[dim-1])) star[numFaces++] = point;
2007:     }
2008:     for (f = 0; f < numFaces; ++f) {
2009:       const PetscInt face    = star[f];
2010:       PetscInt      *closure = NULL;
2011:       PetscInt       closureSize, c;
2012:       PetscInt       faceLoc;

2014:       DMLabelGetValue(subpointMap, face, &faceLoc);
2015:       if (faceLoc == dim-1) continue;
2016:       if (faceLoc >= 0) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %d has dimension %d in the surface label", face, faceLoc);
2017:       DMPlexGetTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure);
2018:       for (c = 0; c < closureSize*2; c += 2) {
2019:         const PetscInt point = closure[c];
2020:         PetscInt       vertexLoc;

2022:         if ((point >= pStart[0]) && (point < pEnd[0])) {
2023:           DMLabelGetValue(vertexLabel, point, &vertexLoc);
2024:           if (vertexLoc != value) break;
2025:         }
2026:       }
2027:       if (c == closureSize*2) {
2028:         const PetscInt *support;
2029:         PetscInt        supportSize, s;

2031:         for (c = 0; c < closureSize*2; c += 2) {
2032:           const PetscInt point = closure[c];

2034:           for (d = 0; d < dim; ++d) {
2035:             if ((point >= pStart[d]) && (point < pEnd[d])) {
2036:               DMLabelSetValue(subpointMap, point, d);
2037:               break;
2038:             }
2039:           }
2040:         }
2041:         DMPlexGetSupportSize(dm, face, &supportSize);
2042:         DMPlexGetSupport(dm, face, &support);
2043:         for (s = 0; s < supportSize; ++s) {
2044:           DMLabelSetValue(subpointMap, support[s], dim);
2045:         }
2046:       }
2047:       DMPlexRestoreTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure);
2048:     }
2049:     DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);
2050:   }
2051:   if (subvertexIS) {ISRestoreIndices(subvertexIS, &subvertices);}
2052:   ISDestroy(&subvertexIS);
2053:   PetscFree3(pStart,pEnd,pMax);
2054:   return(0);
2055: }

2057: static PetscErrorCode DMPlexMarkCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char labelname[], PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, PetscInt *subCells[], DM subdm)
2058: {
2059:   DMLabel         label = NULL;
2060:   const PetscInt *cone;
2061:   PetscInt        dim, cMax, cEnd, c, subc = 0, p, coneSize = -1;
2062:   PetscErrorCode  ierr;

2065:   *numFaces = 0;
2066:   *nFV = 0;
2067:   if (labelname) {DMGetLabel(dm, labelname, &label);}
2068:   *subCells = NULL;
2069:   DMGetDimension(dm, &dim);
2070:   DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);
2071:   DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);
2072:   if (cMax < 0) return(0);
2073:   if (label) {
2074:     for (c = cMax; c < cEnd; ++c) {
2075:       PetscInt val;

2077:       DMLabelGetValue(label, c, &val);
2078:       if (val == value) {
2079:         ++(*numFaces);
2080:         DMPlexGetConeSize(dm, c, &coneSize);
2081:       }
2082:     }
2083:   } else {
2084:     *numFaces = cEnd - cMax;
2085:     DMPlexGetConeSize(dm, cMax, &coneSize);
2086:   }
2087:   PetscMalloc1(*numFaces *2, subCells);
2088:   if (!(*numFaces)) return(0);
2089:   *nFV = hasLagrange ? coneSize/3 : coneSize/2;
2090:   for (c = cMax; c < cEnd; ++c) {
2091:     const PetscInt *cells;
2092:     PetscInt        numCells;

2094:     if (label) {
2095:       PetscInt val;

2097:       DMLabelGetValue(label, c, &val);
2098:       if (val != value) continue;
2099:     }
2100:     DMPlexGetCone(dm, c, &cone);
2101:     for (p = 0; p < *nFV; ++p) {
2102:       DMLabelSetValue(subpointMap, cone[p], 0);
2103:     }
2104:     /* Negative face */
2105:     DMPlexGetJoin(dm, *nFV, cone, &numCells, &cells);
2106:     /* Not true in parallel
2107:     if (numCells != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
2108:     for (p = 0; p < numCells; ++p) {
2109:       DMLabelSetValue(subpointMap, cells[p], 2);
2110:       (*subCells)[subc++] = cells[p];
2111:     }
2112:     DMPlexRestoreJoin(dm, *nFV, cone, &numCells, &cells);
2113:     /* Positive face is not included */
2114:   }
2115:   return(0);
2116: }

2118: static PetscErrorCode DMPlexMarkCohesiveSubmesh_Interpolated(DM dm, DMLabel label, PetscInt value, DMLabel subpointMap, DM subdm)
2119: {
2120:   PetscInt      *pStart, *pEnd;
2121:   PetscInt       dim, cMax, cEnd, c, d;

2125:   DMGetDimension(dm, &dim);
2126:   DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);
2127:   DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);
2128:   if (cMax < 0) return(0);
2129:   PetscMalloc2(dim+1,&pStart,dim+1,&pEnd);
2130:   for (d = 0; d <= dim; ++d) {DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);}
2131:   for (c = cMax; c < cEnd; ++c) {
2132:     const PetscInt *cone;
2133:     PetscInt       *closure = NULL;
2134:     PetscInt        fconeSize, coneSize, closureSize, cl, val;

2136:     if (label) {
2137:       DMLabelGetValue(label, c, &val);
2138:       if (val != value) continue;
2139:     }
2140:     DMPlexGetConeSize(dm, c, &coneSize);
2141:     DMPlexGetCone(dm, c, &cone);
2142:     DMPlexGetConeSize(dm, cone[0], &fconeSize);
2143:     if (coneSize != (fconeSize ? fconeSize : 1) + 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells");
2144:     /* Negative face */
2145:     DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure);
2146:     for (cl = 0; cl < closureSize*2; cl += 2) {
2147:       const PetscInt point = closure[cl];

2149:       for (d = 0; d <= dim; ++d) {
2150:         if ((point >= pStart[d]) && (point < pEnd[d])) {
2151:           DMLabelSetValue(subpointMap, point, d);
2152:           break;
2153:         }
2154:       }
2155:     }
2156:     DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure);
2157:     /* Cells -- positive face is not included */
2158:     for (cl = 0; cl < 1; ++cl) {
2159:       const PetscInt *support;
2160:       PetscInt        supportSize, s;

2162:       DMPlexGetSupportSize(dm, cone[cl], &supportSize);
2163:       /* if (supportSize != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive faces should separate two cells"); */
2164:       DMPlexGetSupport(dm, cone[cl], &support);
2165:       for (s = 0; s < supportSize; ++s) {
2166:         DMLabelSetValue(subpointMap, support[s], dim);
2167:       }
2168:     }
2169:   }
2170:   PetscFree2(pStart, pEnd);
2171:   return(0);
2172: }

2174: static PetscErrorCode DMPlexGetFaceOrientation(DM dm, PetscInt cell, PetscInt numCorners, PetscInt indices[], PetscInt oppositeVertex, PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2175: {
2176:   MPI_Comm       comm;
2177:   PetscBool      posOrient = PETSC_FALSE;
2178:   const PetscInt debug     = 0;
2179:   PetscInt       cellDim, faceSize, f;

2183:   PetscObjectGetComm((PetscObject)dm,&comm);
2184:   DMGetDimension(dm, &cellDim);
2185:   if (debug) {PetscPrintf(comm, "cellDim: %d numCorners: %d\n", cellDim, numCorners);}

2187:   if (cellDim == 1 && numCorners == 2) {
2188:     /* Triangle */
2189:     faceSize  = numCorners-1;
2190:     posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2191:   } else if (cellDim == 2 && numCorners == 3) {
2192:     /* Triangle */
2193:     faceSize  = numCorners-1;
2194:     posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2195:   } else if (cellDim == 3 && numCorners == 4) {
2196:     /* Tetrahedron */
2197:     faceSize  = numCorners-1;
2198:     posOrient = (oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2199:   } else if (cellDim == 1 && numCorners == 3) {
2200:     /* Quadratic line */
2201:     faceSize  = 1;
2202:     posOrient = PETSC_TRUE;
2203:   } else if (cellDim == 2 && numCorners == 4) {
2204:     /* Quads */
2205:     faceSize = 2;
2206:     if ((indices[1] > indices[0]) && (indices[1] - indices[0] == 1)) {
2207:       posOrient = PETSC_TRUE;
2208:     } else if ((indices[0] == 3) && (indices[1] == 0)) {
2209:       posOrient = PETSC_TRUE;
2210:     } else {
2211:       if (((indices[0] > indices[1]) && (indices[0] - indices[1] == 1)) || ((indices[0] == 0) && (indices[1] == 3))) {
2212:         posOrient = PETSC_FALSE;
2213:       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossedge");
2214:     }
2215:   } else if (cellDim == 2 && numCorners == 6) {
2216:     /* Quadratic triangle (I hate this) */
2217:     /* Edges are determined by the first 2 vertices (corners of edges) */
2218:     const PetscInt faceSizeTri = 3;
2219:     PetscInt       sortedIndices[3], i, iFace;
2220:     PetscBool      found                    = PETSC_FALSE;
2221:     PetscInt       faceVerticesTriSorted[9] = {
2222:       0, 3,  4, /* bottom */
2223:       1, 4,  5, /* right */
2224:       2, 3,  5, /* left */
2225:     };
2226:     PetscInt       faceVerticesTri[9] = {
2227:       0, 3,  4, /* bottom */
2228:       1, 4,  5, /* right */
2229:       2, 5,  3, /* left */
2230:     };

2232:     for (i = 0; i < faceSizeTri; ++i) sortedIndices[i] = indices[i];
2233:     PetscSortInt(faceSizeTri, sortedIndices);
2234:     for (iFace = 0; iFace < 3; ++iFace) {
2235:       const PetscInt ii = iFace*faceSizeTri;
2236:       PetscInt       fVertex, cVertex;

2238:       if ((sortedIndices[0] == faceVerticesTriSorted[ii+0]) &&
2239:           (sortedIndices[1] == faceVerticesTriSorted[ii+1])) {
2240:         for (fVertex = 0; fVertex < faceSizeTri; ++fVertex) {
2241:           for (cVertex = 0; cVertex < faceSizeTri; ++cVertex) {
2242:             if (indices[cVertex] == faceVerticesTri[ii+fVertex]) {
2243:               faceVertices[fVertex] = origVertices[cVertex];
2244:               break;
2245:             }
2246:           }
2247:         }
2248:         found = PETSC_TRUE;
2249:         break;
2250:       }
2251:     }
2252:     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid tri crossface");
2253:     if (posOriented) *posOriented = PETSC_TRUE;
2254:     return(0);
2255:   } else if (cellDim == 2 && numCorners == 9) {
2256:     /* Quadratic quad (I hate this) */
2257:     /* Edges are determined by the first 2 vertices (corners of edges) */
2258:     const PetscInt faceSizeQuad = 3;
2259:     PetscInt       sortedIndices[3], i, iFace;
2260:     PetscBool      found                      = PETSC_FALSE;
2261:     PetscInt       faceVerticesQuadSorted[12] = {
2262:       0, 1,  4, /* bottom */
2263:       1, 2,  5, /* right */
2264:       2, 3,  6, /* top */
2265:       0, 3,  7, /* left */
2266:     };
2267:     PetscInt       faceVerticesQuad[12] = {
2268:       0, 1,  4, /* bottom */
2269:       1, 2,  5, /* right */
2270:       2, 3,  6, /* top */
2271:       3, 0,  7, /* left */
2272:     };

2274:     for (i = 0; i < faceSizeQuad; ++i) sortedIndices[i] = indices[i];
2275:     PetscSortInt(faceSizeQuad, sortedIndices);
2276:     for (iFace = 0; iFace < 4; ++iFace) {
2277:       const PetscInt ii = iFace*faceSizeQuad;
2278:       PetscInt       fVertex, cVertex;

2280:       if ((sortedIndices[0] == faceVerticesQuadSorted[ii+0]) &&
2281:           (sortedIndices[1] == faceVerticesQuadSorted[ii+1])) {
2282:         for (fVertex = 0; fVertex < faceSizeQuad; ++fVertex) {
2283:           for (cVertex = 0; cVertex < faceSizeQuad; ++cVertex) {
2284:             if (indices[cVertex] == faceVerticesQuad[ii+fVertex]) {
2285:               faceVertices[fVertex] = origVertices[cVertex];
2286:               break;
2287:             }
2288:           }
2289:         }
2290:         found = PETSC_TRUE;
2291:         break;
2292:       }
2293:     }
2294:     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossface");
2295:     if (posOriented) *posOriented = PETSC_TRUE;
2296:     return(0);
2297:   } else if (cellDim == 3 && numCorners == 8) {
2298:     /* Hexes
2299:        A hex is two oriented quads with the normal of the first
2300:        pointing up at the second.

2302:           7---6
2303:          /|  /|
2304:         4---5 |
2305:         | 1-|-2
2306:         |/  |/
2307:         0---3

2309:         Faces are determined by the first 4 vertices (corners of faces) */
2310:     const PetscInt faceSizeHex = 4;
2311:     PetscInt       sortedIndices[4], i, iFace;
2312:     PetscBool      found                     = PETSC_FALSE;
2313:     PetscInt       faceVerticesHexSorted[24] = {
2314:       0, 1, 2, 3,  /* bottom */
2315:       4, 5, 6, 7,  /* top */
2316:       0, 3, 4, 5,  /* front */
2317:       2, 3, 5, 6,  /* right */
2318:       1, 2, 6, 7,  /* back */
2319:       0, 1, 4, 7,  /* left */
2320:     };
2321:     PetscInt       faceVerticesHex[24] = {
2322:       1, 2, 3, 0,  /* bottom */
2323:       4, 5, 6, 7,  /* top */
2324:       0, 3, 5, 4,  /* front */
2325:       3, 2, 6, 5,  /* right */
2326:       2, 1, 7, 6,  /* back */
2327:       1, 0, 4, 7,  /* left */
2328:     };

2330:     for (i = 0; i < faceSizeHex; ++i) sortedIndices[i] = indices[i];
2331:     PetscSortInt(faceSizeHex, sortedIndices);
2332:     for (iFace = 0; iFace < 6; ++iFace) {
2333:       const PetscInt ii = iFace*faceSizeHex;
2334:       PetscInt       fVertex, cVertex;

2336:       if ((sortedIndices[0] == faceVerticesHexSorted[ii+0]) &&
2337:           (sortedIndices[1] == faceVerticesHexSorted[ii+1]) &&
2338:           (sortedIndices[2] == faceVerticesHexSorted[ii+2]) &&
2339:           (sortedIndices[3] == faceVerticesHexSorted[ii+3])) {
2340:         for (fVertex = 0; fVertex < faceSizeHex; ++fVertex) {
2341:           for (cVertex = 0; cVertex < faceSizeHex; ++cVertex) {
2342:             if (indices[cVertex] == faceVerticesHex[ii+fVertex]) {
2343:               faceVertices[fVertex] = origVertices[cVertex];
2344:               break;
2345:             }
2346:           }
2347:         }
2348:         found = PETSC_TRUE;
2349:         break;
2350:       }
2351:     }
2352:     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2353:     if (posOriented) *posOriented = PETSC_TRUE;
2354:     return(0);
2355:   } else if (cellDim == 3 && numCorners == 10) {
2356:     /* Quadratic tet */
2357:     /* Faces are determined by the first 3 vertices (corners of faces) */
2358:     const PetscInt faceSizeTet = 6;
2359:     PetscInt       sortedIndices[6], i, iFace;
2360:     PetscBool      found                     = PETSC_FALSE;
2361:     PetscInt       faceVerticesTetSorted[24] = {
2362:       0, 1, 2,  6, 7, 8, /* bottom */
2363:       0, 3, 4,  6, 7, 9,  /* front */
2364:       1, 4, 5,  7, 8, 9,  /* right */
2365:       2, 3, 5,  6, 8, 9,  /* left */
2366:     };
2367:     PetscInt       faceVerticesTet[24] = {
2368:       0, 1, 2,  6, 7, 8, /* bottom */
2369:       0, 4, 3,  6, 7, 9,  /* front */
2370:       1, 5, 4,  7, 8, 9,  /* right */
2371:       2, 3, 5,  8, 6, 9,  /* left */
2372:     };

2374:     for (i = 0; i < faceSizeTet; ++i) sortedIndices[i] = indices[i];
2375:     PetscSortInt(faceSizeTet, sortedIndices);
2376:     for (iFace=0; iFace < 4; ++iFace) {
2377:       const PetscInt ii = iFace*faceSizeTet;
2378:       PetscInt       fVertex, cVertex;

2380:       if ((sortedIndices[0] == faceVerticesTetSorted[ii+0]) &&
2381:           (sortedIndices[1] == faceVerticesTetSorted[ii+1]) &&
2382:           (sortedIndices[2] == faceVerticesTetSorted[ii+2]) &&
2383:           (sortedIndices[3] == faceVerticesTetSorted[ii+3])) {
2384:         for (fVertex = 0; fVertex < faceSizeTet; ++fVertex) {
2385:           for (cVertex = 0; cVertex < faceSizeTet; ++cVertex) {
2386:             if (indices[cVertex] == faceVerticesTet[ii+fVertex]) {
2387:               faceVertices[fVertex] = origVertices[cVertex];
2388:               break;
2389:             }
2390:           }
2391:         }
2392:         found = PETSC_TRUE;
2393:         break;
2394:       }
2395:     }
2396:     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid tet crossface");
2397:     if (posOriented) *posOriented = PETSC_TRUE;
2398:     return(0);
2399:   } else if (cellDim == 3 && numCorners == 27) {
2400:     /* Quadratic hexes (I hate this)
2401:        A hex is two oriented quads with the normal of the first
2402:        pointing up at the second.

2404:          7---6
2405:         /|  /|
2406:        4---5 |
2407:        | 3-|-2
2408:        |/  |/
2409:        0---1

2411:        Faces are determined by the first 4 vertices (corners of faces) */
2412:     const PetscInt faceSizeQuadHex = 9;
2413:     PetscInt       sortedIndices[9], i, iFace;
2414:     PetscBool      found                         = PETSC_FALSE;
2415:     PetscInt       faceVerticesQuadHexSorted[54] = {
2416:       0, 1, 2, 3,  8, 9, 10, 11,  24, /* bottom */
2417:       4, 5, 6, 7,  12, 13, 14, 15,  25, /* top */
2418:       0, 1, 4, 5,  8, 12, 16, 17,  22, /* front */
2419:       1, 2, 5, 6,  9, 13, 17, 18,  21, /* right */
2420:       2, 3, 6, 7,  10, 14, 18, 19,  23, /* back */
2421:       0, 3, 4, 7,  11, 15, 16, 19,  20, /* left */
2422:     };
2423:     PetscInt       faceVerticesQuadHex[54] = {
2424:       3, 2, 1, 0,  10, 9, 8, 11,  24, /* bottom */
2425:       4, 5, 6, 7,  12, 13, 14, 15,  25, /* top */
2426:       0, 1, 5, 4,  8, 17, 12, 16,  22, /* front */
2427:       1, 2, 6, 5,  9, 18, 13, 17,  21, /* right */
2428:       2, 3, 7, 6,  10, 19, 14, 18,  23, /* back */
2429:       3, 0, 4, 7,  11, 16, 15, 19,  20 /* left */
2430:     };

2432:     for (i = 0; i < faceSizeQuadHex; ++i) sortedIndices[i] = indices[i];
2433:     PetscSortInt(faceSizeQuadHex, sortedIndices);
2434:     for (iFace = 0; iFace < 6; ++iFace) {
2435:       const PetscInt ii = iFace*faceSizeQuadHex;
2436:       PetscInt       fVertex, cVertex;

2438:       if ((sortedIndices[0] == faceVerticesQuadHexSorted[ii+0]) &&
2439:           (sortedIndices[1] == faceVerticesQuadHexSorted[ii+1]) &&
2440:           (sortedIndices[2] == faceVerticesQuadHexSorted[ii+2]) &&
2441:           (sortedIndices[3] == faceVerticesQuadHexSorted[ii+3])) {
2442:         for (fVertex = 0; fVertex < faceSizeQuadHex; ++fVertex) {
2443:           for (cVertex = 0; cVertex < faceSizeQuadHex; ++cVertex) {
2444:             if (indices[cVertex] == faceVerticesQuadHex[ii+fVertex]) {
2445:               faceVertices[fVertex] = origVertices[cVertex];
2446:               break;
2447:             }
2448:           }
2449:         }
2450:         found = PETSC_TRUE;
2451:         break;
2452:       }
2453:     }
2454:     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2455:     if (posOriented) *posOriented = PETSC_TRUE;
2456:     return(0);
2457:   } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Unknown cell type for faceOrientation().");
2458:   if (!posOrient) {
2459:     if (debug) {PetscPrintf(comm, "  Reversing initial face orientation\n");}
2460:     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[faceSize-1 - f];
2461:   } else {
2462:     if (debug) {PetscPrintf(comm, "  Keeping initial face orientation\n");}
2463:     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[f];
2464:   }
2465:   if (posOriented) *posOriented = posOrient;
2466:   return(0);
2467: }

2469: /*@
2470:   DMPlexGetOrientedFace - Given a cell and a face, as a set of vertices, return the oriented face, as a set of vertices,
2471:   in faceVertices. The orientation is such that the face normal points out of the cell

2473:   Not collective

2475:   Input Parameters:
2476: + dm           - The original mesh
2477: . cell         - The cell mesh point
2478: . faceSize     - The number of vertices on the face
2479: . face         - The face vertices
2480: . numCorners   - The number of vertices on the cell
2481: . indices      - Local numbering of face vertices in cell cone
2482: - origVertices - Original face vertices

2484:   Output Parameter:
2485: + faceVertices - The face vertices properly oriented
2486: - posOriented  - PETSC_TRUE if the face was oriented with outward normal

2488:   Level: developer

2490: .seealso: DMPlexGetCone()
2491: @*/
2492: PetscErrorCode DMPlexGetOrientedFace(DM dm, PetscInt cell, PetscInt faceSize, const PetscInt face[], PetscInt numCorners, PetscInt indices[], PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2493: {
2494:   const PetscInt *cone = NULL;
2495:   PetscInt        coneSize, v, f, v2;
2496:   PetscInt        oppositeVertex = -1;
2497:   PetscErrorCode  ierr;

2500:   DMPlexGetConeSize(dm, cell, &coneSize);
2501:   DMPlexGetCone(dm, cell, &cone);
2502:   for (v = 0, v2 = 0; v < coneSize; ++v) {
2503:     PetscBool found = PETSC_FALSE;

2505:     for (f = 0; f < faceSize; ++f) {
2506:       if (face[f] == cone[v]) {
2507:         found = PETSC_TRUE; break;
2508:       }
2509:     }
2510:     if (found) {
2511:       indices[v2]      = v;
2512:       origVertices[v2] = cone[v];
2513:       ++v2;
2514:     } else {
2515:       oppositeVertex = v;
2516:     }
2517:   }
2518:   DMPlexGetFaceOrientation(dm, cell, numCorners, indices, oppositeVertex, origVertices, faceVertices, posOriented);
2519:   return(0);
2520: }

2522: /*
2523:   DMPlexInsertFace_Internal - Puts a face into the mesh

2525:   Not collective

2527:   Input Parameters:
2528:   + dm              - The DMPlex
2529:   . numFaceVertex   - The number of vertices in the face
2530:   . faceVertices    - The vertices in the face for dm
2531:   . subfaceVertices - The vertices in the face for subdm
2532:   . numCorners      - The number of vertices in the cell
2533:   . cell            - A cell in dm containing the face
2534:   . subcell         - A cell in subdm containing the face
2535:   . firstFace       - First face in the mesh
2536:   - newFacePoint    - Next face in the mesh

2538:   Output Parameters:
2539:   . newFacePoint - Contains next face point number on input, updated on output

2541:   Level: developer
2542: */
2543: 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)
2544: {
2545:   MPI_Comm        comm;
2546:   DM_Plex        *submesh = (DM_Plex*) subdm->data;
2547:   const PetscInt *faces;
2548:   PetscInt        numFaces, coneSize;
2549:   PetscErrorCode  ierr;

2552:   PetscObjectGetComm((PetscObject)dm,&comm);
2553:   DMPlexGetConeSize(subdm, subcell, &coneSize);
2554:   if (coneSize != 1) SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cone size of cell %d is %d != 1", cell, coneSize);
2555: #if 0
2556:   /* Cannot use this because support() has not been constructed yet */
2557:   DMPlexGetJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces);
2558: #else
2559:   {
2560:     PetscInt f;

2562:     numFaces = 0;
2563:     DMGetWorkArray(subdm, 1, PETSC_INT, (void **) &faces);
2564:     for (f = firstFace; f < *newFacePoint; ++f) {
2565:       PetscInt dof, off, d;

2567:       PetscSectionGetDof(submesh->coneSection, f, &dof);
2568:       PetscSectionGetOffset(submesh->coneSection, f, &off);
2569:       /* Yes, I know this is quadratic, but I expect the sizes to be <5 */
2570:       for (d = 0; d < dof; ++d) {
2571:         const PetscInt p = submesh->cones[off+d];
2572:         PetscInt       v;

2574:         for (v = 0; v < numFaceVertices; ++v) {
2575:           if (subfaceVertices[v] == p) break;
2576:         }
2577:         if (v == numFaceVertices) break;
2578:       }
2579:       if (d == dof) {
2580:         numFaces               = 1;
2581:         ((PetscInt*) faces)[0] = f;
2582:       }
2583:     }
2584:   }
2585: #endif
2586:   if (numFaces > 1) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Vertex set had %d faces, not one", numFaces);
2587:   else if (numFaces == 1) {
2588:     /* Add the other cell neighbor for this face */
2589:     DMPlexSetCone(subdm, subcell, faces);
2590:   } else {
2591:     PetscInt *indices, *origVertices, *orientedVertices, *orientedSubVertices, v, ov;
2592:     PetscBool posOriented;

2594:     DMGetWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), PETSC_INT, &orientedVertices);
2595:     origVertices        = &orientedVertices[numFaceVertices];
2596:     indices             = &orientedVertices[numFaceVertices*2];
2597:     orientedSubVertices = &orientedVertices[numFaceVertices*3];
2598:     DMPlexGetOrientedFace(dm, cell, numFaceVertices, faceVertices, numCorners, indices, origVertices, orientedVertices, &posOriented);
2599:     /* TODO: I know that routine should return a permutation, not the indices */
2600:     for (v = 0; v < numFaceVertices; ++v) {
2601:       const PetscInt vertex = faceVertices[v], subvertex = subfaceVertices[v];
2602:       for (ov = 0; ov < numFaceVertices; ++ov) {
2603:         if (orientedVertices[ov] == vertex) {
2604:           orientedSubVertices[ov] = subvertex;
2605:           break;
2606:         }
2607:       }
2608:       if (ov == numFaceVertices) SETERRQ1(comm, PETSC_ERR_PLIB, "Could not find face vertex %d in orientated set", vertex);
2609:     }
2610:     DMPlexSetCone(subdm, *newFacePoint, orientedSubVertices);
2611:     DMPlexSetCone(subdm, subcell, newFacePoint);
2612:     DMRestoreWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), PETSC_INT, &orientedVertices);
2613:     ++(*newFacePoint);
2614:   }
2615: #if 0
2616:   DMPlexRestoreJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces);
2617: #else
2618:   DMRestoreWorkArray(subdm, 1, PETSC_INT, (void **) &faces);
2619: #endif
2620:   return(0);
2621: }

2623: static PetscErrorCode DMPlexCreateSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm)
2624: {
2625:   MPI_Comm        comm;
2626:   DMLabel         subpointMap;
2627:   IS              subvertexIS,  subcellIS;
2628:   const PetscInt *subVertices, *subCells;
2629:   PetscInt        numSubVertices, firstSubVertex, numSubCells;
2630:   PetscInt       *subface, maxConeSize, numSubFaces = 0, firstSubFace, newFacePoint, nFV = 0;
2631:   PetscInt        vStart, vEnd, c, f;
2632:   PetscErrorCode  ierr;

2635:   PetscObjectGetComm((PetscObject)dm,&comm);
2636:   /* Create subpointMap which marks the submesh */
2637:   DMLabelCreate("subpoint_map", &subpointMap);
2638:   DMPlexSetSubpointMap(subdm, subpointMap);
2639:   DMLabelDestroy(&subpointMap);
2640:   if (vertexLabel) {DMPlexMarkSubmesh_Uninterpolated(dm, vertexLabel, value, subpointMap, &numSubFaces, &nFV, subdm);}
2641:   /* Setup chart */
2642:   DMLabelGetStratumSize(subpointMap, 0, &numSubVertices);
2643:   DMLabelGetStratumSize(subpointMap, 2, &numSubCells);
2644:   DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices);
2645:   DMPlexSetVTKCellHeight(subdm, 1);
2646:   /* Set cone sizes */
2647:   firstSubVertex = numSubCells;
2648:   firstSubFace   = numSubCells+numSubVertices;
2649:   newFacePoint   = firstSubFace;
2650:   DMLabelGetStratumIS(subpointMap, 0, &subvertexIS);
2651:   if (subvertexIS) {ISGetIndices(subvertexIS, &subVertices);}
2652:   DMLabelGetStratumIS(subpointMap, 2, &subcellIS);
2653:   if (subcellIS) {ISGetIndices(subcellIS, &subCells);}
2654:   for (c = 0; c < numSubCells; ++c) {
2655:     DMPlexSetConeSize(subdm, c, 1);
2656:   }
2657:   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
2658:     DMPlexSetConeSize(subdm, f, nFV);
2659:   }
2660:   DMSetUp(subdm);
2661:   /* Create face cones */
2662:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
2663:   DMPlexGetMaxSizes(dm, &maxConeSize, NULL);
2664:   DMGetWorkArray(subdm, maxConeSize, PETSC_INT, (void**) &subface);
2665:   for (c = 0; c < numSubCells; ++c) {
2666:     const PetscInt cell    = subCells[c];
2667:     const PetscInt subcell = c;
2668:     PetscInt      *closure = NULL;
2669:     PetscInt       closureSize, cl, numCorners = 0, faceSize = 0;

2671:     DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);
2672:     for (cl = 0; cl < closureSize*2; cl += 2) {
2673:       const PetscInt point = closure[cl];
2674:       PetscInt       subVertex;

2676:       if ((point >= vStart) && (point < vEnd)) {
2677:         ++numCorners;
2678:         PetscFindInt(point, numSubVertices, subVertices, &subVertex);
2679:         if (subVertex >= 0) {
2680:           closure[faceSize] = point;
2681:           subface[faceSize] = firstSubVertex+subVertex;
2682:           ++faceSize;
2683:         }
2684:       }
2685:     }
2686:     if (faceSize > nFV) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %d of an element on the surface", faceSize);
2687:     if (faceSize == nFV) {
2688:       DMPlexInsertFace_Internal(dm, subdm, faceSize, closure, subface, numCorners, cell, subcell, firstSubFace, &newFacePoint);
2689:     }
2690:     DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);
2691:   }
2692:   DMRestoreWorkArray(subdm, maxConeSize, PETSC_INT, (void**) &subface);
2693:   DMPlexSymmetrize(subdm);
2694:   DMPlexStratify(subdm);
2695:   /* Build coordinates */
2696:   {
2697:     PetscSection coordSection, subCoordSection;
2698:     Vec          coordinates, subCoordinates;
2699:     PetscScalar *coords, *subCoords;
2700:     PetscInt     numComp, coordSize, v;
2701:     const char  *name;

2703:     DMGetCoordinateSection(dm, &coordSection);
2704:     DMGetCoordinatesLocal(dm, &coordinates);
2705:     DMGetCoordinateSection(subdm, &subCoordSection);
2706:     PetscSectionSetNumFields(subCoordSection, 1);
2707:     PetscSectionGetFieldComponents(coordSection, 0, &numComp);
2708:     PetscSectionSetFieldComponents(subCoordSection, 0, numComp);
2709:     PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices);
2710:     for (v = 0; v < numSubVertices; ++v) {
2711:       const PetscInt vertex    = subVertices[v];
2712:       const PetscInt subvertex = firstSubVertex+v;
2713:       PetscInt       dof;

2715:       PetscSectionGetDof(coordSection, vertex, &dof);
2716:       PetscSectionSetDof(subCoordSection, subvertex, dof);
2717:       PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);
2718:     }
2719:     PetscSectionSetUp(subCoordSection);
2720:     PetscSectionGetStorageSize(subCoordSection, &coordSize);
2721:     VecCreate(PETSC_COMM_SELF, &subCoordinates);
2722:     PetscObjectGetName((PetscObject)coordinates,&name);
2723:     PetscObjectSetName((PetscObject)subCoordinates,name);
2724:     VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);
2725:     VecSetType(subCoordinates,VECSTANDARD);
2726:     if (coordSize) {
2727:       VecGetArray(coordinates,    &coords);
2728:       VecGetArray(subCoordinates, &subCoords);
2729:       for (v = 0; v < numSubVertices; ++v) {
2730:         const PetscInt vertex    = subVertices[v];
2731:         const PetscInt subvertex = firstSubVertex+v;
2732:         PetscInt       dof, off, sdof, soff, d;

2734:         PetscSectionGetDof(coordSection, vertex, &dof);
2735:         PetscSectionGetOffset(coordSection, vertex, &off);
2736:         PetscSectionGetDof(subCoordSection, subvertex, &sdof);
2737:         PetscSectionGetOffset(subCoordSection, subvertex, &soff);
2738:         if (dof != sdof) SETERRQ4(comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
2739:         for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
2740:       }
2741:       VecRestoreArray(coordinates,    &coords);
2742:       VecRestoreArray(subCoordinates, &subCoords);
2743:     }
2744:     DMSetCoordinatesLocal(subdm, subCoordinates);
2745:     VecDestroy(&subCoordinates);
2746:   }
2747:   /* Cleanup */
2748:   if (subvertexIS) {ISRestoreIndices(subvertexIS, &subVertices);}
2749:   ISDestroy(&subvertexIS);
2750:   if (subcellIS) {ISRestoreIndices(subcellIS, &subCells);}
2751:   ISDestroy(&subcellIS);
2752:   return(0);
2753: }

2755: PETSC_STATIC_INLINE PetscInt DMPlexFilterPoint_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[])
2756: {
2757:   PetscInt       subPoint;

2760:   PetscFindInt(point, numSubPoints, subPoints, &subPoint); if (ierr < 0) return ierr;
2761:   return subPoint < 0 ? subPoint : firstSubPoint+subPoint;
2762: }

2764: static PetscErrorCode DMPlexCreateSubmeshGeneric_Interpolated(DM dm, DMLabel label, PetscInt value, PetscBool isCohesive, PetscInt cellHeight, DM subdm)
2765: {
2766:   MPI_Comm         comm;
2767:   DMLabel          subpointMap;
2768:   IS              *subpointIS;
2769:   const PetscInt **subpoints;
2770:   PetscInt        *numSubPoints, *firstSubPoint, *coneNew, *orntNew;
2771:   PetscInt         totSubPoints = 0, maxConeSize, dim, p, d, v;
2772:   PetscErrorCode   ierr;

2775:   PetscObjectGetComm((PetscObject)dm,&comm);
2776:   /* Create subpointMap which marks the submesh */
2777:   DMLabelCreate("subpoint_map", &subpointMap);
2778:   DMPlexSetSubpointMap(subdm, subpointMap);
2779:   if (cellHeight) {
2780:     if (isCohesive) {DMPlexMarkCohesiveSubmesh_Interpolated(dm, label, value, subpointMap, subdm);}
2781:     else            {DMPlexMarkSubmesh_Interpolated(dm, label, value, subpointMap, subdm);}
2782:   } else {
2783:     DMLabel         depth;
2784:     IS              pointIS;
2785:     const PetscInt *points;
2786:     PetscInt        numPoints;

2788:     DMPlexGetDepthLabel(dm, &depth);
2789:     DMLabelGetStratumSize(label, value, &numPoints);
2790:     DMLabelGetStratumIS(label, value, &pointIS);
2791:     ISGetIndices(pointIS, &points);
2792:     for (p = 0; p < numPoints; ++p) {
2793:       PetscInt *closure = NULL;
2794:       PetscInt  closureSize, c, pdim;

2796:       DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);
2797:       for (c = 0; c < closureSize*2; c += 2) {
2798:         DMLabelGetValue(depth, closure[c], &pdim);
2799:         DMLabelSetValue(subpointMap, closure[c], pdim);
2800:       }
2801:       DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);
2802:     }
2803:     ISRestoreIndices(pointIS, &points);
2804:     ISDestroy(&pointIS);
2805:   }
2806:   DMLabelDestroy(&subpointMap);
2807:   /* Setup chart */
2808:   DMGetDimension(dm, &dim);
2809:   PetscMalloc4(dim+1,&numSubPoints,dim+1,&firstSubPoint,dim+1,&subpointIS,dim+1,&subpoints);
2810:   for (d = 0; d <= dim; ++d) {
2811:     DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d]);
2812:     totSubPoints += numSubPoints[d];
2813:   }
2814:   DMPlexSetChart(subdm, 0, totSubPoints);
2815:   DMPlexSetVTKCellHeight(subdm, cellHeight);
2816:   /* Set cone sizes */
2817:   firstSubPoint[dim] = 0;
2818:   firstSubPoint[0]   = firstSubPoint[dim] + numSubPoints[dim];
2819:   if (dim > 1) {firstSubPoint[dim-1] = firstSubPoint[0]     + numSubPoints[0];}
2820:   if (dim > 2) {firstSubPoint[dim-2] = firstSubPoint[dim-1] + numSubPoints[dim-1];}
2821:   for (d = 0; d <= dim; ++d) {
2822:     DMLabelGetStratumIS(subpointMap, d, &subpointIS[d]);
2823:     if (subpointIS[d]) {ISGetIndices(subpointIS[d], &subpoints[d]);}
2824:   }
2825:   for (d = 0; d <= dim; ++d) {
2826:     for (p = 0; p < numSubPoints[d]; ++p) {
2827:       const PetscInt  point    = subpoints[d][p];
2828:       const PetscInt  subpoint = firstSubPoint[d] + p;
2829:       const PetscInt *cone;
2830:       PetscInt        coneSize, coneSizeNew, c, val;

2832:       DMPlexGetConeSize(dm, point, &coneSize);
2833:       DMPlexSetConeSize(subdm, subpoint, coneSize);
2834:       if (cellHeight && (d == dim)) {
2835:         DMPlexGetCone(dm, point, &cone);
2836:         for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
2837:           DMLabelGetValue(subpointMap, cone[c], &val);
2838:           if (val >= 0) coneSizeNew++;
2839:         }
2840:         DMPlexSetConeSize(subdm, subpoint, coneSizeNew);
2841:       }
2842:     }
2843:   }
2844:   DMSetUp(subdm);
2845:   /* Set cones */
2846:   DMPlexGetMaxSizes(dm, &maxConeSize, NULL);
2847:   PetscMalloc2(maxConeSize,&coneNew,maxConeSize,&orntNew);
2848:   for (d = 0; d <= dim; ++d) {
2849:     for (p = 0; p < numSubPoints[d]; ++p) {
2850:       const PetscInt  point    = subpoints[d][p];
2851:       const PetscInt  subpoint = firstSubPoint[d] + p;
2852:       const PetscInt *cone, *ornt;
2853:       PetscInt        coneSize, subconeSize, coneSizeNew, c, subc;

2855:       DMPlexGetConeSize(dm, point, &coneSize);
2856:       DMPlexGetConeSize(subdm, subpoint, &subconeSize);
2857:       DMPlexGetCone(dm, point, &cone);
2858:       DMPlexGetConeOrientation(dm, point, &ornt);
2859:       for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
2860:         PetscFindInt(cone[c], numSubPoints[d-1], subpoints[d-1], &subc);
2861:         if (subc >= 0) {
2862:           coneNew[coneSizeNew] = firstSubPoint[d-1] + subc;
2863:           orntNew[coneSizeNew] = ornt[c];
2864:           ++coneSizeNew;
2865:         }
2866:       }
2867:       if (coneSizeNew != subconeSize) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of cone points located %d does not match subcone size %d", coneSizeNew, subconeSize);
2868:       DMPlexSetCone(subdm, subpoint, coneNew);
2869:       DMPlexSetConeOrientation(subdm, subpoint, orntNew);
2870:     }
2871:   }
2872:   PetscFree2(coneNew,orntNew);
2873:   DMPlexSymmetrize(subdm);
2874:   DMPlexStratify(subdm);
2875:   /* Build coordinates */
2876:   {
2877:     PetscSection coordSection, subCoordSection;
2878:     Vec          coordinates, subCoordinates;
2879:     PetscScalar *coords, *subCoords;
2880:     PetscInt     numComp, coordSize;
2881:     const char  *name;

2883:     DMGetCoordinateSection(dm, &coordSection);
2884:     DMGetCoordinatesLocal(dm, &coordinates);
2885:     DMGetCoordinateSection(subdm, &subCoordSection);
2886:     PetscSectionSetNumFields(subCoordSection, 1);
2887:     PetscSectionGetFieldComponents(coordSection, 0, &numComp);
2888:     PetscSectionSetFieldComponents(subCoordSection, 0, numComp);
2889:     PetscSectionSetChart(subCoordSection, firstSubPoint[0], firstSubPoint[0]+numSubPoints[0]);
2890:     for (v = 0; v < numSubPoints[0]; ++v) {
2891:       const PetscInt vertex    = subpoints[0][v];
2892:       const PetscInt subvertex = firstSubPoint[0]+v;
2893:       PetscInt       dof;

2895:       PetscSectionGetDof(coordSection, vertex, &dof);
2896:       PetscSectionSetDof(subCoordSection, subvertex, dof);
2897:       PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);
2898:     }
2899:     PetscSectionSetUp(subCoordSection);
2900:     PetscSectionGetStorageSize(subCoordSection, &coordSize);
2901:     VecCreate(PETSC_COMM_SELF, &subCoordinates);
2902:     PetscObjectGetName((PetscObject)coordinates,&name);
2903:     PetscObjectSetName((PetscObject)subCoordinates,name);
2904:     VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);
2905:     VecSetType(subCoordinates,VECSTANDARD);
2906:     VecGetArray(coordinates,    &coords);
2907:     VecGetArray(subCoordinates, &subCoords);
2908:     for (v = 0; v < numSubPoints[0]; ++v) {
2909:       const PetscInt vertex    = subpoints[0][v];
2910:       const PetscInt subvertex = firstSubPoint[0]+v;
2911:       PetscInt dof, off, sdof, soff, d;

2913:       PetscSectionGetDof(coordSection, vertex, &dof);
2914:       PetscSectionGetOffset(coordSection, vertex, &off);
2915:       PetscSectionGetDof(subCoordSection, subvertex, &sdof);
2916:       PetscSectionGetOffset(subCoordSection, subvertex, &soff);
2917:       if (dof != sdof) SETERRQ4(comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
2918:       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
2919:     }
2920:     VecRestoreArray(coordinates,    &coords);
2921:     VecRestoreArray(subCoordinates, &subCoords);
2922:     DMSetCoordinatesLocal(subdm, subCoordinates);
2923:     VecDestroy(&subCoordinates);
2924:   }
2925:   /* Build SF: We need this complexity because subpoints might not be selected on the owning process */
2926:   {
2927:     PetscSF            sfPoint, sfPointSub;
2928:     IS                 subpIS;
2929:     const PetscSFNode *remotePoints;
2930:     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
2931:     const PetscInt    *localPoints, *subpoints;
2932:     PetscInt          *slocalPoints;
2933:     PetscInt           numRoots, numLeaves, numSubpoints = 0, numSubroots, numSubleaves = 0, l, sl, ll, pStart, pEnd, p;
2934:     PetscMPIInt        rank;

2936:     MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);
2937:     DMGetPointSF(dm, &sfPoint);
2938:     DMGetPointSF(subdm, &sfPointSub);
2939:     DMPlexGetChart(dm, &pStart, &pEnd);
2940:     DMPlexGetChart(subdm, NULL, &numSubroots);
2941:     DMPlexCreateSubpointIS(subdm, &subpIS);
2942:     if (subpIS) {
2943:       ISGetIndices(subpIS, &subpoints);
2944:       ISGetLocalSize(subpIS, &numSubpoints);
2945:     }
2946:     PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);
2947:     if (numRoots >= 0) {
2948:       PetscMalloc2(pEnd-pStart,&newLocalPoints,numRoots,&newOwners);
2949:       for (p = 0; p < pEnd-pStart; ++p) {
2950:         newLocalPoints[p].rank  = -2;
2951:         newLocalPoints[p].index = -2;
2952:       }
2953:       /* Set subleaves */
2954:       for (l = 0; l < numLeaves; ++l) {
2955:         const PetscInt point    = localPoints[l];
2956:         const PetscInt subpoint = DMPlexFilterPoint_Internal(point, 0, numSubpoints, subpoints);

2958:         if (subpoint < 0) continue;
2959:         newLocalPoints[point-pStart].rank  = rank;
2960:         newLocalPoints[point-pStart].index = subpoint;
2961:         ++numSubleaves;
2962:       }
2963:       /* Must put in owned subpoints */
2964:       for (p = pStart; p < pEnd; ++p) {
2965:         const PetscInt subpoint = DMPlexFilterPoint_Internal(p, 0, numSubpoints, subpoints);

2967:         if (subpoint < 0) {
2968:           newOwners[p-pStart].rank  = -3;
2969:           newOwners[p-pStart].index = -3;
2970:         } else {
2971:           newOwners[p-pStart].rank  = rank;
2972:           newOwners[p-pStart].index = subpoint;
2973:         }
2974:       }
2975:       PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);
2976:       PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);
2977:       PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints);
2978:       PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints);
2979:       PetscMalloc1(numSubleaves, &slocalPoints);
2980:       PetscMalloc1(numSubleaves, &sremotePoints);
2981:       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
2982:         const PetscInt point    = localPoints[l];
2983:         const PetscInt subpoint = DMPlexFilterPoint_Internal(point, 0, numSubpoints, subpoints);

2985:         if (subpoint < 0) continue;
2986:         if (newLocalPoints[point].rank == rank) {++ll; continue;}
2987:         slocalPoints[sl]        = subpoint;
2988:         sremotePoints[sl].rank  = newLocalPoints[point].rank;
2989:         sremotePoints[sl].index = newLocalPoints[point].index;
2990:         if (sremotePoints[sl].rank  < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %d", point);
2991:         if (sremotePoints[sl].index < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %d", point);
2992:         ++sl;
2993:       }
2994:       if (sl + ll != numSubleaves) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %d + %d != %d", sl, ll, numSubleaves);
2995:       PetscFree2(newLocalPoints,newOwners);
2996:       PetscSFSetGraph(sfPointSub, numSubroots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER);
2997:     }
2998:     if (subpIS) {
2999:       ISRestoreIndices(subpIS, &subpoints);
3000:       ISDestroy(&subpIS);
3001:     }
3002:   }
3003:   /* Cleanup */
3004:   for (d = 0; d <= dim; ++d) {
3005:     if (subpointIS[d]) {ISRestoreIndices(subpointIS[d], &subpoints[d]);}
3006:     ISDestroy(&subpointIS[d]);
3007:   }
3008:   PetscFree4(numSubPoints,firstSubPoint,subpointIS,subpoints);
3009:   return(0);
3010: }

3012: static PetscErrorCode DMPlexCreateSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm)
3013: {

3017:   DMPlexCreateSubmeshGeneric_Interpolated(dm, vertexLabel, value, PETSC_FALSE, 1, subdm);
3018:   return(0);
3019: }

3021: /*@
3022:   DMPlexCreateSubmesh - Extract a hypersurface from the mesh using vertices defined by a label

3024:   Input Parameters:
3025: + dm           - The original mesh
3026: . vertexLabel  - The DMLabel marking vertices contained in the surface
3027: - value        - The label value to use

3029:   Output Parameter:
3030: . subdm - The surface mesh

3032:   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().

3034:   Level: developer

3036: .seealso: DMPlexGetSubpointMap(), DMGetLabel(), DMLabelSetValue()
3037: @*/
3038: PetscErrorCode DMPlexCreateSubmesh(DM dm, DMLabel vertexLabel, PetscInt value, DM *subdm)
3039: {
3040:   PetscInt       dim, depth;

3046:   DMGetDimension(dm, &dim);
3047:   DMPlexGetDepth(dm, &depth);
3048:   DMCreate(PetscObjectComm((PetscObject)dm), subdm);
3049:   DMSetType(*subdm, DMPLEX);
3050:   DMSetDimension(*subdm, dim-1);
3051:   if (depth == dim) {
3052:     DMPlexCreateSubmesh_Interpolated(dm, vertexLabel, value, *subdm);
3053:   } else {
3054:     DMPlexCreateSubmesh_Uninterpolated(dm, vertexLabel, value, *subdm);
3055:   }
3056:   return(0);
3057: }

3059: static PetscErrorCode DMPlexCreateCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM subdm)
3060: {
3061:   MPI_Comm        comm;
3062:   DMLabel         subpointMap;
3063:   IS              subvertexIS;
3064:   const PetscInt *subVertices;
3065:   PetscInt        numSubVertices, firstSubVertex, numSubCells, *subCells = NULL;
3066:   PetscInt       *subface, maxConeSize, numSubFaces, firstSubFace, newFacePoint, nFV;
3067:   PetscInt        cMax, c, f;
3068:   PetscErrorCode  ierr;

3071:   PetscObjectGetComm((PetscObject)dm, &comm);
3072:   /* Create subpointMap which marks the submesh */
3073:   DMLabelCreate("subpoint_map", &subpointMap);
3074:   DMPlexSetSubpointMap(subdm, subpointMap);
3075:   DMLabelDestroy(&subpointMap);
3076:   DMPlexMarkCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, subpointMap, &numSubFaces, &nFV, &subCells, subdm);
3077:   /* Setup chart */
3078:   DMLabelGetStratumSize(subpointMap, 0, &numSubVertices);
3079:   DMLabelGetStratumSize(subpointMap, 2, &numSubCells);
3080:   DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices);
3081:   DMPlexSetVTKCellHeight(subdm, 1);
3082:   /* Set cone sizes */
3083:   firstSubVertex = numSubCells;
3084:   firstSubFace   = numSubCells+numSubVertices;
3085:   newFacePoint   = firstSubFace;
3086:   DMLabelGetStratumIS(subpointMap, 0, &subvertexIS);
3087:   if (subvertexIS) {ISGetIndices(subvertexIS, &subVertices);}
3088:   for (c = 0; c < numSubCells; ++c) {
3089:     DMPlexSetConeSize(subdm, c, 1);
3090:   }
3091:   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
3092:     DMPlexSetConeSize(subdm, f, nFV);
3093:   }
3094:   DMSetUp(subdm);
3095:   /* Create face cones */
3096:   DMPlexGetMaxSizes(dm, &maxConeSize, NULL);
3097:   DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);
3098:   DMGetWorkArray(subdm, maxConeSize, PETSC_INT, (void**) &subface);
3099:   for (c = 0; c < numSubCells; ++c) {
3100:     const PetscInt  cell    = subCells[c];
3101:     const PetscInt  subcell = c;
3102:     const PetscInt *cone, *cells;
3103:     PetscInt        numCells, subVertex, p, v;

3105:     if (cell < cMax) continue;
3106:     DMPlexGetCone(dm, cell, &cone);
3107:     for (v = 0; v < nFV; ++v) {
3108:       PetscFindInt(cone[v], numSubVertices, subVertices, &subVertex);
3109:       subface[v] = firstSubVertex+subVertex;
3110:     }
3111:     DMPlexSetCone(subdm, newFacePoint, subface);
3112:     DMPlexSetCone(subdm, subcell, &newFacePoint);
3113:     DMPlexGetJoin(dm, nFV, cone, &numCells, &cells);
3114:     /* Not true in parallel
3115:     if (numCells != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
3116:     for (p = 0; p < numCells; ++p) {
3117:       PetscInt negsubcell;

3119:       if (cells[p] >= cMax) continue;
3120:       /* I know this is a crap search */
3121:       for (negsubcell = 0; negsubcell < numSubCells; ++negsubcell) {
3122:         if (subCells[negsubcell] == cells[p]) break;
3123:       }
3124:       if (negsubcell == numSubCells) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find negative face neighbor for cohesive cell %d", cell);
3125:       DMPlexSetCone(subdm, negsubcell, &newFacePoint);
3126:     }
3127:     DMPlexRestoreJoin(dm, nFV, cone, &numCells, &cells);
3128:     ++newFacePoint;
3129:   }
3130:   DMRestoreWorkArray(subdm, maxConeSize, PETSC_INT, (void**) &subface);
3131:   DMPlexSymmetrize(subdm);
3132:   DMPlexStratify(subdm);
3133:   /* Build coordinates */
3134:   {
3135:     PetscSection coordSection, subCoordSection;
3136:     Vec          coordinates, subCoordinates;
3137:     PetscScalar *coords, *subCoords;
3138:     PetscInt     numComp, coordSize, v;
3139:     const char  *name;

3141:     DMGetCoordinateSection(dm, &coordSection);
3142:     DMGetCoordinatesLocal(dm, &coordinates);
3143:     DMGetCoordinateSection(subdm, &subCoordSection);
3144:     PetscSectionSetNumFields(subCoordSection, 1);
3145:     PetscSectionGetFieldComponents(coordSection, 0, &numComp);
3146:     PetscSectionSetFieldComponents(subCoordSection, 0, numComp);
3147:     PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices);
3148:     for (v = 0; v < numSubVertices; ++v) {
3149:       const PetscInt vertex    = subVertices[v];
3150:       const PetscInt subvertex = firstSubVertex+v;
3151:       PetscInt       dof;

3153:       PetscSectionGetDof(coordSection, vertex, &dof);
3154:       PetscSectionSetDof(subCoordSection, subvertex, dof);
3155:       PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);
3156:     }
3157:     PetscSectionSetUp(subCoordSection);
3158:     PetscSectionGetStorageSize(subCoordSection, &coordSize);
3159:     VecCreate(PETSC_COMM_SELF, &subCoordinates);
3160:     PetscObjectGetName((PetscObject)coordinates,&name);
3161:     PetscObjectSetName((PetscObject)subCoordinates,name);
3162:     VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);
3163:     VecSetType(subCoordinates,VECSTANDARD);
3164:     VecGetArray(coordinates,    &coords);
3165:     VecGetArray(subCoordinates, &subCoords);
3166:     for (v = 0; v < numSubVertices; ++v) {
3167:       const PetscInt vertex    = subVertices[v];
3168:       const PetscInt subvertex = firstSubVertex+v;
3169:       PetscInt       dof, off, sdof, soff, d;

3171:       PetscSectionGetDof(coordSection, vertex, &dof);
3172:       PetscSectionGetOffset(coordSection, vertex, &off);
3173:       PetscSectionGetDof(subCoordSection, subvertex, &sdof);
3174:       PetscSectionGetOffset(subCoordSection, subvertex, &soff);
3175:       if (dof != sdof) SETERRQ4(comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
3176:       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3177:     }
3178:     VecRestoreArray(coordinates,    &coords);
3179:     VecRestoreArray(subCoordinates, &subCoords);
3180:     DMSetCoordinatesLocal(subdm, subCoordinates);
3181:     VecDestroy(&subCoordinates);
3182:   }
3183:   /* Build SF */
3184:   CHKMEMQ;
3185:   {
3186:     PetscSF            sfPoint, sfPointSub;
3187:     const PetscSFNode *remotePoints;
3188:     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
3189:     const PetscInt    *localPoints;
3190:     PetscInt          *slocalPoints;
3191:     PetscInt           numRoots, numLeaves, numSubRoots = numSubCells+numSubFaces+numSubVertices, numSubLeaves = 0, l, sl, ll, pStart, pEnd, p, vStart, vEnd;
3192:     PetscMPIInt        rank;

3194:     MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);
3195:     DMGetPointSF(dm, &sfPoint);
3196:     DMGetPointSF(subdm, &sfPointSub);
3197:     DMPlexGetChart(dm, &pStart, &pEnd);
3198:     DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
3199:     PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);
3200:     if (numRoots >= 0) {
3201:       /* Only vertices should be shared */
3202:       PetscMalloc2(pEnd-pStart,&newLocalPoints,numRoots,&newOwners);
3203:       for (p = 0; p < pEnd-pStart; ++p) {
3204:         newLocalPoints[p].rank  = -2;
3205:         newLocalPoints[p].index = -2;
3206:       }
3207:       /* Set subleaves */
3208:       for (l = 0; l < numLeaves; ++l) {
3209:         const PetscInt point    = localPoints[l];
3210:         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);

3212:         if ((point < vStart) && (point >= vEnd)) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Should not be mapping anything but vertices, %d", point);
3213:         if (subPoint < 0) continue;
3214:         newLocalPoints[point-pStart].rank  = rank;
3215:         newLocalPoints[point-pStart].index = subPoint;
3216:         ++numSubLeaves;
3217:       }
3218:       /* Must put in owned subpoints */
3219:       for (p = pStart; p < pEnd; ++p) {
3220:         const PetscInt subPoint = DMPlexFilterPoint_Internal(p, firstSubVertex, numSubVertices, subVertices);

3222:         if (subPoint < 0) {
3223:           newOwners[p-pStart].rank  = -3;
3224:           newOwners[p-pStart].index = -3;
3225:         } else {
3226:           newOwners[p-pStart].rank  = rank;
3227:           newOwners[p-pStart].index = subPoint;
3228:         }
3229:       }
3230:       PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);
3231:       PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);
3232:       PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints);
3233:       PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints);
3234:       PetscMalloc1(numSubLeaves,    &slocalPoints);
3235:       PetscMalloc1(numSubLeaves, &sremotePoints);
3236:       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
3237:         const PetscInt point    = localPoints[l];
3238:         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);

3240:         if (subPoint < 0) continue;
3241:         if (newLocalPoints[point].rank == rank) {++ll; continue;}
3242:         slocalPoints[sl]        = subPoint;
3243:         sremotePoints[sl].rank  = newLocalPoints[point].rank;
3244:         sremotePoints[sl].index = newLocalPoints[point].index;
3245:         if (sremotePoints[sl].rank  < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %d", point);
3246:         if (sremotePoints[sl].index < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %d", point);
3247:         ++sl;
3248:       }
3249:       PetscFree2(newLocalPoints,newOwners);
3250:       if (sl + ll != numSubLeaves) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %d + %d != %d", sl, ll, numSubLeaves);
3251:       PetscSFSetGraph(sfPointSub, numSubRoots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER);
3252:     }
3253:   }
3254:   CHKMEMQ;
3255:   /* Cleanup */
3256:   if (subvertexIS) {ISRestoreIndices(subvertexIS, &subVertices);}
3257:   ISDestroy(&subvertexIS);
3258:   PetscFree(subCells);
3259:   return(0);
3260: }

3262: static PetscErrorCode DMPlexCreateCohesiveSubmesh_Interpolated(DM dm, const char labelname[], PetscInt value, DM subdm)
3263: {
3264:   DMLabel        label = NULL;

3268:   if (labelname) {DMGetLabel(dm, labelname, &label);}
3269:   DMPlexCreateSubmeshGeneric_Interpolated(dm, label, value, PETSC_TRUE, 1, subdm);
3270:   return(0);
3271: }

3273: /*@C
3274:   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.

3276:   Input Parameters:
3277: + dm          - The original mesh
3278: . hasLagrange - The mesh has Lagrange unknowns in the cohesive cells
3279: . label       - A label name, or NULL
3280: - value  - A label value

3282:   Output Parameter:
3283: . subdm - The surface mesh

3285:   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().

3287:   Level: developer

3289: .seealso: DMPlexGetSubpointMap(), DMPlexCreateSubmesh()
3290: @*/
3291: PetscErrorCode DMPlexCreateCohesiveSubmesh(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM *subdm)
3292: {
3293:   PetscInt       dim, depth;

3299:   DMGetDimension(dm, &dim);
3300:   DMPlexGetDepth(dm, &depth);
3301:   DMCreate(PetscObjectComm((PetscObject)dm), subdm);
3302:   DMSetType(*subdm, DMPLEX);
3303:   DMSetDimension(*subdm, dim-1);
3304:   if (depth == dim) {
3305:     DMPlexCreateCohesiveSubmesh_Interpolated(dm, label, value, *subdm);
3306:   } else {
3307:     DMPlexCreateCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, *subdm);
3308:   }
3309:   return(0);
3310: }

3312: /*@
3313:   DMPlexFilter - Extract a subset of mesh cells defined by a label as a separate mesh

3315:   Input Parameters:
3316: + dm        - The original mesh
3317: . cellLabel - The DMLabel marking cells contained in the new mesh
3318: - value     - The label value to use

3320:   Output Parameter:
3321: . subdm - The new mesh

3323:   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().

3325:   Level: developer

3327: .seealso: DMPlexGetSubpointMap(), DMGetLabel(), DMLabelSetValue()
3328: @*/
3329: PetscErrorCode DMPlexFilter(DM dm, DMLabel cellLabel, PetscInt value, DM *subdm)
3330: {
3331:   PetscInt       dim;

3337:   DMGetDimension(dm, &dim);
3338:   DMCreate(PetscObjectComm((PetscObject) dm), subdm);
3339:   DMSetType(*subdm, DMPLEX);
3340:   DMSetDimension(*subdm, dim);
3341:   /* Extract submesh in place, could be empty on some procs, could have inconsistency if procs do not both extract a shared cell */
3342:   DMPlexCreateSubmeshGeneric_Interpolated(dm, cellLabel, value, PETSC_FALSE, 0, *subdm);
3343:   return(0);
3344: }

3346: /*@
3347:   DMPlexGetSubpointMap - Returns a DMLabel with point dimension as values

3349:   Input Parameter:
3350: . dm - The submesh DM

3352:   Output Parameter:
3353: . subpointMap - The DMLabel of all the points from the original mesh in this submesh, or NULL if this is not a submesh

3355:   Level: developer

3357: .seealso: DMPlexCreateSubmesh(), DMPlexCreateSubpointIS()
3358: @*/
3359: PetscErrorCode DMPlexGetSubpointMap(DM dm, DMLabel *subpointMap)
3360: {
3364:   *subpointMap = ((DM_Plex*) dm->data)->subpointMap;
3365:   return(0);
3366: }

3368: /*@
3369:   DMPlexSetSubpointMap - Sets the DMLabel with point dimension as values

3371:   Input Parameters:
3372: + dm - The submesh DM
3373: - subpointMap - The DMLabel of all the points from the original mesh in this submesh

3375:   Note: Should normally not be called by the user, since it is set in DMPlexCreateSubmesh()

3377:   Level: developer

3379: .seealso: DMPlexCreateSubmesh(), DMPlexCreateSubpointIS()
3380: @*/
3381: PetscErrorCode DMPlexSetSubpointMap(DM dm, DMLabel subpointMap)
3382: {
3383:   DM_Plex       *mesh = (DM_Plex *) dm->data;
3384:   DMLabel        tmp;

3389:   tmp  = mesh->subpointMap;
3390:   mesh->subpointMap = subpointMap;
3391:   ++mesh->subpointMap->refct;
3392:   DMLabelDestroy(&tmp);
3393:   return(0);
3394: }

3396: /*@
3397:   DMPlexCreateSubpointIS - Creates an IS covering the entire subdm chart with the original points as data

3399:   Input Parameter:
3400: . dm - The submesh DM

3402:   Output Parameter:
3403: . subpointIS - The IS of all the points from the original mesh in this submesh, or NULL if this is not a submesh

3405:   Note: This IS is guaranteed to be sorted by the construction of the submesh

3407:   Level: developer

3409: .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointMap()
3410: @*/
3411: PetscErrorCode DMPlexCreateSubpointIS(DM dm, IS *subpointIS)
3412: {
3413:   MPI_Comm        comm;
3414:   DMLabel         subpointMap;
3415:   IS              is;
3416:   const PetscInt *opoints;
3417:   PetscInt       *points, *depths;
3418:   PetscInt        depth, depStart, depEnd, d, pStart, pEnd, p, n, off;
3419:   PetscErrorCode  ierr;

3424:   PetscObjectGetComm((PetscObject)dm,&comm);
3425:   *subpointIS = NULL;
3426:   DMPlexGetSubpointMap(dm, &subpointMap);
3427:   DMPlexGetDepth(dm, &depth);
3428:   if (subpointMap && depth >= 0) {
3429:     DMPlexGetChart(dm, &pStart, &pEnd);
3430:     if (pStart) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Submeshes must start the point numbering at 0, not %d", pStart);
3431:     DMGetWorkArray(dm, depth+1, PETSC_INT, &depths);
3432:     depths[0] = depth;
3433:     depths[1] = 0;
3434:     for(d = 2; d <= depth; ++d) {depths[d] = depth+1 - d;}
3435:     PetscMalloc1(pEnd, &points);
3436:     for(d = 0, off = 0; d <= depth; ++d) {
3437:       const PetscInt dep = depths[d];

3439:       DMPlexGetDepthStratum(dm, dep, &depStart, &depEnd);
3440:       DMLabelGetStratumSize(subpointMap, dep, &n);
3441:       if (((d < 2) && (depth > 1)) || (d == 1)) { /* Only check vertices and cells for now since the map is broken for others */
3442:         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);
3443:       } else {
3444:         if (!n) {
3445:           if (d == 0) {
3446:             /* Missing cells */
3447:             for(p = 0; p < depEnd-depStart; ++p, ++off) points[off] = -1;
3448:           } else {
3449:             /* Missing faces */
3450:             for(p = 0; p < depEnd-depStart; ++p, ++off) points[off] = PETSC_MAX_INT;
3451:           }
3452:         }
3453:       }
3454:       if (n) {
3455:         DMLabelGetStratumIS(subpointMap, dep, &is);
3456:         ISGetIndices(is, &opoints);
3457:         for(p = 0; p < n; ++p, ++off) points[off] = opoints[p];
3458:         ISRestoreIndices(is, &opoints);
3459:         ISDestroy(&is);
3460:       }
3461:     }
3462:     DMRestoreWorkArray(dm, depth+1, PETSC_INT, &depths);
3463:     if (off != pEnd) SETERRQ2(comm, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %d should be %d", off, pEnd);
3464:     ISCreateGeneral(PETSC_COMM_SELF, pEnd, points, PETSC_OWN_POINTER, subpointIS);
3465:   }
3466:   return(0);
3467: }