Actual source code: plexsubmesh.c

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

  5: 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: 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: /*@C
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: 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:     Given a cell and a face, as a set of vertices,
2471:       return the oriented face, as a set of vertices, in faceVertices
2472:     The orientation is such that the face normal points out of the cell
2473: */
2474: PetscErrorCode DMPlexGetOrientedFace(DM dm, PetscInt cell, PetscInt faceSize, const PetscInt face[], PetscInt numCorners, PetscInt indices[], PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2475: {
2476:   const PetscInt *cone = NULL;
2477:   PetscInt        coneSize, v, f, v2;
2478:   PetscInt        oppositeVertex = -1;
2479:   PetscErrorCode  ierr;

2482:   DMPlexGetConeSize(dm, cell, &coneSize);
2483:   DMPlexGetCone(dm, cell, &cone);
2484:   for (v = 0, v2 = 0; v < coneSize; ++v) {
2485:     PetscBool found = PETSC_FALSE;

2487:     for (f = 0; f < faceSize; ++f) {
2488:       if (face[f] == cone[v]) {
2489:         found = PETSC_TRUE; break;
2490:       }
2491:     }
2492:     if (found) {
2493:       indices[v2]      = v;
2494:       origVertices[v2] = cone[v];
2495:       ++v2;
2496:     } else {
2497:       oppositeVertex = v;
2498:     }
2499:   }
2500:   DMPlexGetFaceOrientation(dm, cell, numCorners, indices, oppositeVertex, origVertices, faceVertices, posOriented);
2501:   return(0);
2502: }

2504: /*
2505:   DMPlexInsertFace_Internal - Puts a face into the mesh

2507:   Not collective

2509:   Input Parameters:
2510:   + dm              - The DMPlex
2511:   . numFaceVertex   - The number of vertices in the face
2512:   . faceVertices    - The vertices in the face for dm
2513:   . subfaceVertices - The vertices in the face for subdm
2514:   . numCorners      - The number of vertices in the cell
2515:   . cell            - A cell in dm containing the face
2516:   . subcell         - A cell in subdm containing the face
2517:   . firstFace       - First face in the mesh
2518:   - newFacePoint    - Next face in the mesh

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

2523:   Level: developer
2524: */
2525: 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)
2526: {
2527:   MPI_Comm        comm;
2528:   DM_Plex        *submesh = (DM_Plex*) subdm->data;
2529:   const PetscInt *faces;
2530:   PetscInt        numFaces, coneSize;
2531:   PetscErrorCode  ierr;

2534:   PetscObjectGetComm((PetscObject)dm,&comm);
2535:   DMPlexGetConeSize(subdm, subcell, &coneSize);
2536:   if (coneSize != 1) SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cone size of cell %d is %d != 1", cell, coneSize);
2537: #if 0
2538:   /* Cannot use this because support() has not been constructed yet */
2539:   DMPlexGetJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces);
2540: #else
2541:   {
2542:     PetscInt f;

2544:     numFaces = 0;
2545:     DMGetWorkArray(subdm, 1, PETSC_INT, (void **) &faces);
2546:     for (f = firstFace; f < *newFacePoint; ++f) {
2547:       PetscInt dof, off, d;

2549:       PetscSectionGetDof(submesh->coneSection, f, &dof);
2550:       PetscSectionGetOffset(submesh->coneSection, f, &off);
2551:       /* Yes, I know this is quadratic, but I expect the sizes to be <5 */
2552:       for (d = 0; d < dof; ++d) {
2553:         const PetscInt p = submesh->cones[off+d];
2554:         PetscInt       v;

2556:         for (v = 0; v < numFaceVertices; ++v) {
2557:           if (subfaceVertices[v] == p) break;
2558:         }
2559:         if (v == numFaceVertices) break;
2560:       }
2561:       if (d == dof) {
2562:         numFaces               = 1;
2563:         ((PetscInt*) faces)[0] = f;
2564:       }
2565:     }
2566:   }
2567: #endif
2568:   if (numFaces > 1) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Vertex set had %d faces, not one", numFaces);
2569:   else if (numFaces == 1) {
2570:     /* Add the other cell neighbor for this face */
2571:     DMPlexSetCone(subdm, subcell, faces);
2572:   } else {
2573:     PetscInt *indices, *origVertices, *orientedVertices, *orientedSubVertices, v, ov;
2574:     PetscBool posOriented;

2576:     DMGetWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), PETSC_INT, &orientedVertices);
2577:     origVertices        = &orientedVertices[numFaceVertices];
2578:     indices             = &orientedVertices[numFaceVertices*2];
2579:     orientedSubVertices = &orientedVertices[numFaceVertices*3];
2580:     DMPlexGetOrientedFace(dm, cell, numFaceVertices, faceVertices, numCorners, indices, origVertices, orientedVertices, &posOriented);
2581:     /* TODO: I know that routine should return a permutation, not the indices */
2582:     for (v = 0; v < numFaceVertices; ++v) {
2583:       const PetscInt vertex = faceVertices[v], subvertex = subfaceVertices[v];
2584:       for (ov = 0; ov < numFaceVertices; ++ov) {
2585:         if (orientedVertices[ov] == vertex) {
2586:           orientedSubVertices[ov] = subvertex;
2587:           break;
2588:         }
2589:       }
2590:       if (ov == numFaceVertices) SETERRQ1(comm, PETSC_ERR_PLIB, "Could not find face vertex %d in orientated set", vertex);
2591:     }
2592:     DMPlexSetCone(subdm, *newFacePoint, orientedSubVertices);
2593:     DMPlexSetCone(subdm, subcell, newFacePoint);
2594:     DMRestoreWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), PETSC_INT, &orientedVertices);
2595:     ++(*newFacePoint);
2596:   }
2597: #if 0
2598:   DMPlexRestoreJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces);
2599: #else
2600:   DMRestoreWorkArray(subdm, 1, PETSC_INT, (void **) &faces);
2601: #endif
2602:   return(0);
2603: }

2605: static PetscErrorCode DMPlexCreateSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm)
2606: {
2607:   MPI_Comm        comm;
2608:   DMLabel         subpointMap;
2609:   IS              subvertexIS,  subcellIS;
2610:   const PetscInt *subVertices, *subCells;
2611:   PetscInt        numSubVertices, firstSubVertex, numSubCells;
2612:   PetscInt       *subface, maxConeSize, numSubFaces = 0, firstSubFace, newFacePoint, nFV = 0;
2613:   PetscInt        vStart, vEnd, c, f;
2614:   PetscErrorCode  ierr;

2617:   PetscObjectGetComm((PetscObject)dm,&comm);
2618:   /* Create subpointMap which marks the submesh */
2619:   DMLabelCreate("subpoint_map", &subpointMap);
2620:   DMPlexSetSubpointMap(subdm, subpointMap);
2621:   DMLabelDestroy(&subpointMap);
2622:   if (vertexLabel) {DMPlexMarkSubmesh_Uninterpolated(dm, vertexLabel, value, subpointMap, &numSubFaces, &nFV, subdm);}
2623:   /* Setup chart */
2624:   DMLabelGetStratumSize(subpointMap, 0, &numSubVertices);
2625:   DMLabelGetStratumSize(subpointMap, 2, &numSubCells);
2626:   DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices);
2627:   DMPlexSetVTKCellHeight(subdm, 1);
2628:   /* Set cone sizes */
2629:   firstSubVertex = numSubCells;
2630:   firstSubFace   = numSubCells+numSubVertices;
2631:   newFacePoint   = firstSubFace;
2632:   DMLabelGetStratumIS(subpointMap, 0, &subvertexIS);
2633:   if (subvertexIS) {ISGetIndices(subvertexIS, &subVertices);}
2634:   DMLabelGetStratumIS(subpointMap, 2, &subcellIS);
2635:   if (subcellIS) {ISGetIndices(subcellIS, &subCells);}
2636:   for (c = 0; c < numSubCells; ++c) {
2637:     DMPlexSetConeSize(subdm, c, 1);
2638:   }
2639:   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
2640:     DMPlexSetConeSize(subdm, f, nFV);
2641:   }
2642:   DMSetUp(subdm);
2643:   /* Create face cones */
2644:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
2645:   DMPlexGetMaxSizes(dm, &maxConeSize, NULL);
2646:   DMGetWorkArray(subdm, maxConeSize, PETSC_INT, (void**) &subface);
2647:   for (c = 0; c < numSubCells; ++c) {
2648:     const PetscInt cell    = subCells[c];
2649:     const PetscInt subcell = c;
2650:     PetscInt      *closure = NULL;
2651:     PetscInt       closureSize, cl, numCorners = 0, faceSize = 0;

2653:     DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);
2654:     for (cl = 0; cl < closureSize*2; cl += 2) {
2655:       const PetscInt point = closure[cl];
2656:       PetscInt       subVertex;

2658:       if ((point >= vStart) && (point < vEnd)) {
2659:         ++numCorners;
2660:         PetscFindInt(point, numSubVertices, subVertices, &subVertex);
2661:         if (subVertex >= 0) {
2662:           closure[faceSize] = point;
2663:           subface[faceSize] = firstSubVertex+subVertex;
2664:           ++faceSize;
2665:         }
2666:       }
2667:     }
2668:     if (faceSize > nFV) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %d of an element on the surface", faceSize);
2669:     if (faceSize == nFV) {
2670:       DMPlexInsertFace_Internal(dm, subdm, faceSize, closure, subface, numCorners, cell, subcell, firstSubFace, &newFacePoint);
2671:     }
2672:     DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);
2673:   }
2674:   DMRestoreWorkArray(subdm, maxConeSize, PETSC_INT, (void**) &subface);
2675:   DMPlexSymmetrize(subdm);
2676:   DMPlexStratify(subdm);
2677:   /* Build coordinates */
2678:   {
2679:     PetscSection coordSection, subCoordSection;
2680:     Vec          coordinates, subCoordinates;
2681:     PetscScalar *coords, *subCoords;
2682:     PetscInt     numComp, coordSize, v;
2683:     const char  *name;

2685:     DMGetCoordinateSection(dm, &coordSection);
2686:     DMGetCoordinatesLocal(dm, &coordinates);
2687:     DMGetCoordinateSection(subdm, &subCoordSection);
2688:     PetscSectionSetNumFields(subCoordSection, 1);
2689:     PetscSectionGetFieldComponents(coordSection, 0, &numComp);
2690:     PetscSectionSetFieldComponents(subCoordSection, 0, numComp);
2691:     PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices);
2692:     for (v = 0; v < numSubVertices; ++v) {
2693:       const PetscInt vertex    = subVertices[v];
2694:       const PetscInt subvertex = firstSubVertex+v;
2695:       PetscInt       dof;

2697:       PetscSectionGetDof(coordSection, vertex, &dof);
2698:       PetscSectionSetDof(subCoordSection, subvertex, dof);
2699:       PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);
2700:     }
2701:     PetscSectionSetUp(subCoordSection);
2702:     PetscSectionGetStorageSize(subCoordSection, &coordSize);
2703:     VecCreate(PETSC_COMM_SELF, &subCoordinates);
2704:     PetscObjectGetName((PetscObject)coordinates,&name);
2705:     PetscObjectSetName((PetscObject)subCoordinates,name);
2706:     VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);
2707:     VecSetType(subCoordinates,VECSTANDARD);
2708:     if (coordSize) {
2709:       VecGetArray(coordinates,    &coords);
2710:       VecGetArray(subCoordinates, &subCoords);
2711:       for (v = 0; v < numSubVertices; ++v) {
2712:         const PetscInt vertex    = subVertices[v];
2713:         const PetscInt subvertex = firstSubVertex+v;
2714:         PetscInt       dof, off, sdof, soff, d;

2716:         PetscSectionGetDof(coordSection, vertex, &dof);
2717:         PetscSectionGetOffset(coordSection, vertex, &off);
2718:         PetscSectionGetDof(subCoordSection, subvertex, &sdof);
2719:         PetscSectionGetOffset(subCoordSection, subvertex, &soff);
2720:         if (dof != sdof) SETERRQ4(comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
2721:         for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
2722:       }
2723:       VecRestoreArray(coordinates,    &coords);
2724:       VecRestoreArray(subCoordinates, &subCoords);
2725:     }
2726:     DMSetCoordinatesLocal(subdm, subCoordinates);
2727:     VecDestroy(&subCoordinates);
2728:   }
2729:   /* Cleanup */
2730:   if (subvertexIS) {ISRestoreIndices(subvertexIS, &subVertices);}
2731:   ISDestroy(&subvertexIS);
2732:   if (subcellIS) {ISRestoreIndices(subcellIS, &subCells);}
2733:   ISDestroy(&subcellIS);
2734:   return(0);
2735: }

2737: PETSC_STATIC_INLINE PetscInt DMPlexFilterPoint_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[])
2738: {
2739:   PetscInt       subPoint;

2742:   PetscFindInt(point, numSubPoints, subPoints, &subPoint); if (ierr < 0) return ierr;
2743:   return subPoint < 0 ? subPoint : firstSubPoint+subPoint;
2744: }

2746: static PetscErrorCode DMPlexCreateSubmeshGeneric_Interpolated(DM dm, DMLabel label, PetscInt value, PetscBool isCohesive, PetscInt cellHeight, DM subdm)
2747: {
2748:   MPI_Comm         comm;
2749:   DMLabel          subpointMap;
2750:   IS              *subpointIS;
2751:   const PetscInt **subpoints;
2752:   PetscInt        *numSubPoints, *firstSubPoint, *coneNew, *orntNew;
2753:   PetscInt         totSubPoints = 0, maxConeSize, dim, p, d, v;
2754:   PetscErrorCode   ierr;

2757:   PetscObjectGetComm((PetscObject)dm,&comm);
2758:   /* Create subpointMap which marks the submesh */
2759:   DMLabelCreate("subpoint_map", &subpointMap);
2760:   DMPlexSetSubpointMap(subdm, subpointMap);
2761:   if (cellHeight) {
2762:     if (isCohesive) {DMPlexMarkCohesiveSubmesh_Interpolated(dm, label, value, subpointMap, subdm);}
2763:     else            {DMPlexMarkSubmesh_Interpolated(dm, label, value, subpointMap, subdm);}
2764:   } else {
2765:     DMLabel         depth;
2766:     IS              pointIS;
2767:     const PetscInt *points;
2768:     PetscInt        numPoints;

2770:     DMPlexGetDepthLabel(dm, &depth);
2771:     DMLabelGetStratumSize(label, value, &numPoints);
2772:     DMLabelGetStratumIS(label, value, &pointIS);
2773:     ISGetIndices(pointIS, &points);
2774:     for (p = 0; p < numPoints; ++p) {
2775:       PetscInt *closure = NULL;
2776:       PetscInt  closureSize, c, pdim;

2778:       DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);
2779:       for (c = 0; c < closureSize*2; c += 2) {
2780:         DMLabelGetValue(depth, closure[c], &pdim);
2781:         DMLabelSetValue(subpointMap, closure[c], pdim);
2782:       }
2783:       DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);
2784:     }
2785:     ISRestoreIndices(pointIS, &points);
2786:     ISDestroy(&pointIS);
2787:   }
2788:   DMLabelDestroy(&subpointMap);
2789:   /* Setup chart */
2790:   DMGetDimension(dm, &dim);
2791:   PetscMalloc4(dim+1,&numSubPoints,dim+1,&firstSubPoint,dim+1,&subpointIS,dim+1,&subpoints);
2792:   for (d = 0; d <= dim; ++d) {
2793:     DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d]);
2794:     totSubPoints += numSubPoints[d];
2795:   }
2796:   DMPlexSetChart(subdm, 0, totSubPoints);
2797:   DMPlexSetVTKCellHeight(subdm, cellHeight);
2798:   /* Set cone sizes */
2799:   firstSubPoint[dim] = 0;
2800:   firstSubPoint[0]   = firstSubPoint[dim] + numSubPoints[dim];
2801:   if (dim > 1) {firstSubPoint[dim-1] = firstSubPoint[0]     + numSubPoints[0];}
2802:   if (dim > 2) {firstSubPoint[dim-2] = firstSubPoint[dim-1] + numSubPoints[dim-1];}
2803:   for (d = 0; d <= dim; ++d) {
2804:     DMLabelGetStratumIS(subpointMap, d, &subpointIS[d]);
2805:     if (subpointIS[d]) {ISGetIndices(subpointIS[d], &subpoints[d]);}
2806:   }
2807:   for (d = 0; d <= dim; ++d) {
2808:     for (p = 0; p < numSubPoints[d]; ++p) {
2809:       const PetscInt  point    = subpoints[d][p];
2810:       const PetscInt  subpoint = firstSubPoint[d] + p;
2811:       const PetscInt *cone;
2812:       PetscInt        coneSize, coneSizeNew, c, val;

2814:       DMPlexGetConeSize(dm, point, &coneSize);
2815:       DMPlexSetConeSize(subdm, subpoint, coneSize);
2816:       if (cellHeight && (d == dim)) {
2817:         DMPlexGetCone(dm, point, &cone);
2818:         for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
2819:           DMLabelGetValue(subpointMap, cone[c], &val);
2820:           if (val >= 0) coneSizeNew++;
2821:         }
2822:         DMPlexSetConeSize(subdm, subpoint, coneSizeNew);
2823:       }
2824:     }
2825:   }
2826:   DMSetUp(subdm);
2827:   /* Set cones */
2828:   DMPlexGetMaxSizes(dm, &maxConeSize, NULL);
2829:   PetscMalloc2(maxConeSize,&coneNew,maxConeSize,&orntNew);
2830:   for (d = 0; d <= dim; ++d) {
2831:     for (p = 0; p < numSubPoints[d]; ++p) {
2832:       const PetscInt  point    = subpoints[d][p];
2833:       const PetscInt  subpoint = firstSubPoint[d] + p;
2834:       const PetscInt *cone, *ornt;
2835:       PetscInt        coneSize, subconeSize, coneSizeNew, c, subc;

2837:       DMPlexGetConeSize(dm, point, &coneSize);
2838:       DMPlexGetConeSize(subdm, subpoint, &subconeSize);
2839:       DMPlexGetCone(dm, point, &cone);
2840:       DMPlexGetConeOrientation(dm, point, &ornt);
2841:       for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
2842:         PetscFindInt(cone[c], numSubPoints[d-1], subpoints[d-1], &subc);
2843:         if (subc >= 0) {
2844:           coneNew[coneSizeNew] = firstSubPoint[d-1] + subc;
2845:           orntNew[coneSizeNew] = ornt[c];
2846:           ++coneSizeNew;
2847:         }
2848:       }
2849:       if (coneSizeNew != subconeSize) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of cone points located %d does not match subcone size %d", coneSizeNew, subconeSize);
2850:       DMPlexSetCone(subdm, subpoint, coneNew);
2851:       DMPlexSetConeOrientation(subdm, subpoint, orntNew);
2852:     }
2853:   }
2854:   PetscFree2(coneNew,orntNew);
2855:   DMPlexSymmetrize(subdm);
2856:   DMPlexStratify(subdm);
2857:   /* Build coordinates */
2858:   {
2859:     PetscSection coordSection, subCoordSection;
2860:     Vec          coordinates, subCoordinates;
2861:     PetscScalar *coords, *subCoords;
2862:     PetscInt     numComp, coordSize;
2863:     const char  *name;

2865:     DMGetCoordinateSection(dm, &coordSection);
2866:     DMGetCoordinatesLocal(dm, &coordinates);
2867:     DMGetCoordinateSection(subdm, &subCoordSection);
2868:     PetscSectionSetNumFields(subCoordSection, 1);
2869:     PetscSectionGetFieldComponents(coordSection, 0, &numComp);
2870:     PetscSectionSetFieldComponents(subCoordSection, 0, numComp);
2871:     PetscSectionSetChart(subCoordSection, firstSubPoint[0], firstSubPoint[0]+numSubPoints[0]);
2872:     for (v = 0; v < numSubPoints[0]; ++v) {
2873:       const PetscInt vertex    = subpoints[0][v];
2874:       const PetscInt subvertex = firstSubPoint[0]+v;
2875:       PetscInt       dof;

2877:       PetscSectionGetDof(coordSection, vertex, &dof);
2878:       PetscSectionSetDof(subCoordSection, subvertex, dof);
2879:       PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);
2880:     }
2881:     PetscSectionSetUp(subCoordSection);
2882:     PetscSectionGetStorageSize(subCoordSection, &coordSize);
2883:     VecCreate(PETSC_COMM_SELF, &subCoordinates);
2884:     PetscObjectGetName((PetscObject)coordinates,&name);
2885:     PetscObjectSetName((PetscObject)subCoordinates,name);
2886:     VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);
2887:     VecSetType(subCoordinates,VECSTANDARD);
2888:     VecGetArray(coordinates,    &coords);
2889:     VecGetArray(subCoordinates, &subCoords);
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, off, sdof, soff, d;

2895:       PetscSectionGetDof(coordSection, vertex, &dof);
2896:       PetscSectionGetOffset(coordSection, vertex, &off);
2897:       PetscSectionGetDof(subCoordSection, subvertex, &sdof);
2898:       PetscSectionGetOffset(subCoordSection, subvertex, &soff);
2899:       if (dof != sdof) SETERRQ4(comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
2900:       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
2901:     }
2902:     VecRestoreArray(coordinates,    &coords);
2903:     VecRestoreArray(subCoordinates, &subCoords);
2904:     DMSetCoordinatesLocal(subdm, subCoordinates);
2905:     VecDestroy(&subCoordinates);
2906:   }
2907:   /* Build SF: We need this complexity because subpoints might not be selected on the owning process */
2908:   {
2909:     PetscSF            sfPoint, sfPointSub;
2910:     IS                 subpIS;
2911:     const PetscSFNode *remotePoints;
2912:     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
2913:     const PetscInt    *localPoints, *subpoints;
2914:     PetscInt          *slocalPoints;
2915:     PetscInt           numRoots, numLeaves, numSubpoints = 0, numSubroots, numSubleaves = 0, l, sl, ll, pStart, pEnd, p;
2916:     PetscMPIInt        rank;

2918:     MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);
2919:     DMGetPointSF(dm, &sfPoint);
2920:     DMGetPointSF(subdm, &sfPointSub);
2921:     DMPlexGetChart(dm, &pStart, &pEnd);
2922:     DMPlexGetChart(subdm, NULL, &numSubroots);
2923:     DMPlexCreateSubpointIS(subdm, &subpIS);
2924:     if (subpIS) {
2925:       ISGetIndices(subpIS, &subpoints);
2926:       ISGetLocalSize(subpIS, &numSubpoints);
2927:     }
2928:     PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);
2929:     if (numRoots >= 0) {
2930:       PetscMalloc2(pEnd-pStart,&newLocalPoints,numRoots,&newOwners);
2931:       for (p = 0; p < pEnd-pStart; ++p) {
2932:         newLocalPoints[p].rank  = -2;
2933:         newLocalPoints[p].index = -2;
2934:       }
2935:       /* Set subleaves */
2936:       for (l = 0; l < numLeaves; ++l) {
2937:         const PetscInt point    = localPoints[l];
2938:         const PetscInt subpoint = DMPlexFilterPoint_Internal(point, 0, numSubpoints, subpoints);

2940:         if (subpoint < 0) continue;
2941:         newLocalPoints[point-pStart].rank  = rank;
2942:         newLocalPoints[point-pStart].index = subpoint;
2943:         ++numSubleaves;
2944:       }
2945:       /* Must put in owned subpoints */
2946:       for (p = pStart; p < pEnd; ++p) {
2947:         const PetscInt subpoint = DMPlexFilterPoint_Internal(p, 0, numSubpoints, subpoints);

2949:         if (subpoint < 0) {
2950:           newOwners[p-pStart].rank  = -3;
2951:           newOwners[p-pStart].index = -3;
2952:         } else {
2953:           newOwners[p-pStart].rank  = rank;
2954:           newOwners[p-pStart].index = subpoint;
2955:         }
2956:       }
2957:       PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);
2958:       PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);
2959:       PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints);
2960:       PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints);
2961:       PetscMalloc1(numSubleaves, &slocalPoints);
2962:       PetscMalloc1(numSubleaves, &sremotePoints);
2963:       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
2964:         const PetscInt point    = localPoints[l];
2965:         const PetscInt subpoint = DMPlexFilterPoint_Internal(point, 0, numSubpoints, subpoints);

2967:         if (subpoint < 0) continue;
2968:         if (newLocalPoints[point].rank == rank) {++ll; continue;}
2969:         slocalPoints[sl]        = subpoint;
2970:         sremotePoints[sl].rank  = newLocalPoints[point].rank;
2971:         sremotePoints[sl].index = newLocalPoints[point].index;
2972:         if (sremotePoints[sl].rank  < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %d", point);
2973:         if (sremotePoints[sl].index < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %d", point);
2974:         ++sl;
2975:       }
2976:       if (sl + ll != numSubleaves) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %d + %d != %d", sl, ll, numSubleaves);
2977:       PetscFree2(newLocalPoints,newOwners);
2978:       PetscSFSetGraph(sfPointSub, numSubroots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER);
2979:     }
2980:     if (subpIS) {
2981:       ISRestoreIndices(subpIS, &subpoints);
2982:       ISDestroy(&subpIS);
2983:     }
2984:   }
2985:   /* Cleanup */
2986:   for (d = 0; d <= dim; ++d) {
2987:     if (subpointIS[d]) {ISRestoreIndices(subpointIS[d], &subpoints[d]);}
2988:     ISDestroy(&subpointIS[d]);
2989:   }
2990:   PetscFree4(numSubPoints,firstSubPoint,subpointIS,subpoints);
2991:   return(0);
2992: }

2994: static PetscErrorCode DMPlexCreateSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm)
2995: {

2999:   DMPlexCreateSubmeshGeneric_Interpolated(dm, vertexLabel, value, PETSC_FALSE, 1, subdm);
3000:   return(0);
3001: }

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

3006:   Input Parameters:
3007: + dm           - The original mesh
3008: . vertexLabel  - The DMLabel marking vertices contained in the surface
3009: - value        - The label value to use

3011:   Output Parameter:
3012: . subdm - The surface mesh

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

3016:   Level: developer

3018: .seealso: DMPlexGetSubpointMap(), DMGetLabel(), DMLabelSetValue()
3019: @*/
3020: PetscErrorCode DMPlexCreateSubmesh(DM dm, DMLabel vertexLabel, PetscInt value, DM *subdm)
3021: {
3022:   PetscInt       dim, depth;

3028:   DMGetDimension(dm, &dim);
3029:   DMPlexGetDepth(dm, &depth);
3030:   DMCreate(PetscObjectComm((PetscObject)dm), subdm);
3031:   DMSetType(*subdm, DMPLEX);
3032:   DMSetDimension(*subdm, dim-1);
3033:   if (depth == dim) {
3034:     DMPlexCreateSubmesh_Interpolated(dm, vertexLabel, value, *subdm);
3035:   } else {
3036:     DMPlexCreateSubmesh_Uninterpolated(dm, vertexLabel, value, *subdm);
3037:   }
3038:   return(0);
3039: }

3041: static PetscErrorCode DMPlexCreateCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM subdm)
3042: {
3043:   MPI_Comm        comm;
3044:   DMLabel         subpointMap;
3045:   IS              subvertexIS;
3046:   const PetscInt *subVertices;
3047:   PetscInt        numSubVertices, firstSubVertex, numSubCells, *subCells = NULL;
3048:   PetscInt       *subface, maxConeSize, numSubFaces, firstSubFace, newFacePoint, nFV;
3049:   PetscInt        cMax, c, f;
3050:   PetscErrorCode  ierr;

3053:   PetscObjectGetComm((PetscObject)dm, &comm);
3054:   /* Create subpointMap which marks the submesh */
3055:   DMLabelCreate("subpoint_map", &subpointMap);
3056:   DMPlexSetSubpointMap(subdm, subpointMap);
3057:   DMLabelDestroy(&subpointMap);
3058:   DMPlexMarkCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, subpointMap, &numSubFaces, &nFV, &subCells, subdm);
3059:   /* Setup chart */
3060:   DMLabelGetStratumSize(subpointMap, 0, &numSubVertices);
3061:   DMLabelGetStratumSize(subpointMap, 2, &numSubCells);
3062:   DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices);
3063:   DMPlexSetVTKCellHeight(subdm, 1);
3064:   /* Set cone sizes */
3065:   firstSubVertex = numSubCells;
3066:   firstSubFace   = numSubCells+numSubVertices;
3067:   newFacePoint   = firstSubFace;
3068:   DMLabelGetStratumIS(subpointMap, 0, &subvertexIS);
3069:   if (subvertexIS) {ISGetIndices(subvertexIS, &subVertices);}
3070:   for (c = 0; c < numSubCells; ++c) {
3071:     DMPlexSetConeSize(subdm, c, 1);
3072:   }
3073:   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
3074:     DMPlexSetConeSize(subdm, f, nFV);
3075:   }
3076:   DMSetUp(subdm);
3077:   /* Create face cones */
3078:   DMPlexGetMaxSizes(dm, &maxConeSize, NULL);
3079:   DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);
3080:   DMGetWorkArray(subdm, maxConeSize, PETSC_INT, (void**) &subface);
3081:   for (c = 0; c < numSubCells; ++c) {
3082:     const PetscInt  cell    = subCells[c];
3083:     const PetscInt  subcell = c;
3084:     const PetscInt *cone, *cells;
3085:     PetscInt        numCells, subVertex, p, v;

3087:     if (cell < cMax) continue;
3088:     DMPlexGetCone(dm, cell, &cone);
3089:     for (v = 0; v < nFV; ++v) {
3090:       PetscFindInt(cone[v], numSubVertices, subVertices, &subVertex);
3091:       subface[v] = firstSubVertex+subVertex;
3092:     }
3093:     DMPlexSetCone(subdm, newFacePoint, subface);
3094:     DMPlexSetCone(subdm, subcell, &newFacePoint);
3095:     DMPlexGetJoin(dm, nFV, cone, &numCells, &cells);
3096:     /* Not true in parallel
3097:     if (numCells != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
3098:     for (p = 0; p < numCells; ++p) {
3099:       PetscInt negsubcell;

3101:       if (cells[p] >= cMax) continue;
3102:       /* I know this is a crap search */
3103:       for (negsubcell = 0; negsubcell < numSubCells; ++negsubcell) {
3104:         if (subCells[negsubcell] == cells[p]) break;
3105:       }
3106:       if (negsubcell == numSubCells) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find negative face neighbor for cohesive cell %d", cell);
3107:       DMPlexSetCone(subdm, negsubcell, &newFacePoint);
3108:     }
3109:     DMPlexRestoreJoin(dm, nFV, cone, &numCells, &cells);
3110:     ++newFacePoint;
3111:   }
3112:   DMRestoreWorkArray(subdm, maxConeSize, PETSC_INT, (void**) &subface);
3113:   DMPlexSymmetrize(subdm);
3114:   DMPlexStratify(subdm);
3115:   /* Build coordinates */
3116:   {
3117:     PetscSection coordSection, subCoordSection;
3118:     Vec          coordinates, subCoordinates;
3119:     PetscScalar *coords, *subCoords;
3120:     PetscInt     numComp, coordSize, v;
3121:     const char  *name;

3123:     DMGetCoordinateSection(dm, &coordSection);
3124:     DMGetCoordinatesLocal(dm, &coordinates);
3125:     DMGetCoordinateSection(subdm, &subCoordSection);
3126:     PetscSectionSetNumFields(subCoordSection, 1);
3127:     PetscSectionGetFieldComponents(coordSection, 0, &numComp);
3128:     PetscSectionSetFieldComponents(subCoordSection, 0, numComp);
3129:     PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices);
3130:     for (v = 0; v < numSubVertices; ++v) {
3131:       const PetscInt vertex    = subVertices[v];
3132:       const PetscInt subvertex = firstSubVertex+v;
3133:       PetscInt       dof;

3135:       PetscSectionGetDof(coordSection, vertex, &dof);
3136:       PetscSectionSetDof(subCoordSection, subvertex, dof);
3137:       PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);
3138:     }
3139:     PetscSectionSetUp(subCoordSection);
3140:     PetscSectionGetStorageSize(subCoordSection, &coordSize);
3141:     VecCreate(PETSC_COMM_SELF, &subCoordinates);
3142:     PetscObjectGetName((PetscObject)coordinates,&name);
3143:     PetscObjectSetName((PetscObject)subCoordinates,name);
3144:     VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);
3145:     VecSetType(subCoordinates,VECSTANDARD);
3146:     VecGetArray(coordinates,    &coords);
3147:     VecGetArray(subCoordinates, &subCoords);
3148:     for (v = 0; v < numSubVertices; ++v) {
3149:       const PetscInt vertex    = subVertices[v];
3150:       const PetscInt subvertex = firstSubVertex+v;
3151:       PetscInt       dof, off, sdof, soff, d;

3153:       PetscSectionGetDof(coordSection, vertex, &dof);
3154:       PetscSectionGetOffset(coordSection, vertex, &off);
3155:       PetscSectionGetDof(subCoordSection, subvertex, &sdof);
3156:       PetscSectionGetOffset(subCoordSection, subvertex, &soff);
3157:       if (dof != sdof) SETERRQ4(comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
3158:       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3159:     }
3160:     VecRestoreArray(coordinates,    &coords);
3161:     VecRestoreArray(subCoordinates, &subCoords);
3162:     DMSetCoordinatesLocal(subdm, subCoordinates);
3163:     VecDestroy(&subCoordinates);
3164:   }
3165:   /* Build SF */
3166:   CHKMEMQ;
3167:   {
3168:     PetscSF            sfPoint, sfPointSub;
3169:     const PetscSFNode *remotePoints;
3170:     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
3171:     const PetscInt    *localPoints;
3172:     PetscInt          *slocalPoints;
3173:     PetscInt           numRoots, numLeaves, numSubRoots = numSubCells+numSubFaces+numSubVertices, numSubLeaves = 0, l, sl, ll, pStart, pEnd, p, vStart, vEnd;
3174:     PetscMPIInt        rank;

3176:     MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);
3177:     DMGetPointSF(dm, &sfPoint);
3178:     DMGetPointSF(subdm, &sfPointSub);
3179:     DMPlexGetChart(dm, &pStart, &pEnd);
3180:     DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
3181:     PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);
3182:     if (numRoots >= 0) {
3183:       /* Only vertices should be shared */
3184:       PetscMalloc2(pEnd-pStart,&newLocalPoints,numRoots,&newOwners);
3185:       for (p = 0; p < pEnd-pStart; ++p) {
3186:         newLocalPoints[p].rank  = -2;
3187:         newLocalPoints[p].index = -2;
3188:       }
3189:       /* Set subleaves */
3190:       for (l = 0; l < numLeaves; ++l) {
3191:         const PetscInt point    = localPoints[l];
3192:         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);

3194:         if ((point < vStart) && (point >= vEnd)) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Should not be mapping anything but vertices, %d", point);
3195:         if (subPoint < 0) continue;
3196:         newLocalPoints[point-pStart].rank  = rank;
3197:         newLocalPoints[point-pStart].index = subPoint;
3198:         ++numSubLeaves;
3199:       }
3200:       /* Must put in owned subpoints */
3201:       for (p = pStart; p < pEnd; ++p) {
3202:         const PetscInt subPoint = DMPlexFilterPoint_Internal(p, firstSubVertex, numSubVertices, subVertices);

3204:         if (subPoint < 0) {
3205:           newOwners[p-pStart].rank  = -3;
3206:           newOwners[p-pStart].index = -3;
3207:         } else {
3208:           newOwners[p-pStart].rank  = rank;
3209:           newOwners[p-pStart].index = subPoint;
3210:         }
3211:       }
3212:       PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);
3213:       PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);
3214:       PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints);
3215:       PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints);
3216:       PetscMalloc1(numSubLeaves,    &slocalPoints);
3217:       PetscMalloc1(numSubLeaves, &sremotePoints);
3218:       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
3219:         const PetscInt point    = localPoints[l];
3220:         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);

3222:         if (subPoint < 0) continue;
3223:         if (newLocalPoints[point].rank == rank) {++ll; continue;}
3224:         slocalPoints[sl]        = subPoint;
3225:         sremotePoints[sl].rank  = newLocalPoints[point].rank;
3226:         sremotePoints[sl].index = newLocalPoints[point].index;
3227:         if (sremotePoints[sl].rank  < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %d", point);
3228:         if (sremotePoints[sl].index < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %d", point);
3229:         ++sl;
3230:       }
3231:       PetscFree2(newLocalPoints,newOwners);
3232:       if (sl + ll != numSubLeaves) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %d + %d != %d", sl, ll, numSubLeaves);
3233:       PetscSFSetGraph(sfPointSub, numSubRoots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER);
3234:     }
3235:   }
3236:   CHKMEMQ;
3237:   /* Cleanup */
3238:   if (subvertexIS) {ISRestoreIndices(subvertexIS, &subVertices);}
3239:   ISDestroy(&subvertexIS);
3240:   PetscFree(subCells);
3241:   return(0);
3242: }

3244: static PetscErrorCode DMPlexCreateCohesiveSubmesh_Interpolated(DM dm, const char labelname[], PetscInt value, DM subdm)
3245: {
3246:   DMLabel        label = NULL;

3250:   if (labelname) {DMGetLabel(dm, labelname, &label);}
3251:   DMPlexCreateSubmeshGeneric_Interpolated(dm, label, value, PETSC_TRUE, 1, subdm);
3252:   return(0);
3253: }

3255: /*
3256:   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.

3258:   Input Parameters:
3259: + dm          - The original mesh
3260: . hasLagrange - The mesh has Lagrange unknowns in the cohesive cells
3261: . label       - A label name, or NULL
3262: - value  - A label value

3264:   Output Parameter:
3265: . subdm - The surface mesh

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

3269:   Level: developer

3271: .seealso: DMPlexGetSubpointMap(), DMPlexCreateSubmesh()
3272: */
3273: PetscErrorCode DMPlexCreateCohesiveSubmesh(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM *subdm)
3274: {
3275:   PetscInt       dim, depth;

3281:   DMGetDimension(dm, &dim);
3282:   DMPlexGetDepth(dm, &depth);
3283:   DMCreate(PetscObjectComm((PetscObject)dm), subdm);
3284:   DMSetType(*subdm, DMPLEX);
3285:   DMSetDimension(*subdm, dim-1);
3286:   if (depth == dim) {
3287:     DMPlexCreateCohesiveSubmesh_Interpolated(dm, label, value, *subdm);
3288:   } else {
3289:     DMPlexCreateCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, *subdm);
3290:   }
3291:   return(0);
3292: }

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

3297:   Input Parameters:
3298: + dm        - The original mesh
3299: . cellLabel - The DMLabel marking cells contained in the new mesh
3300: - value     - The label value to use

3302:   Output Parameter:
3303: . subdm - The new mesh

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

3307:   Level: developer

3309: .seealso: DMPlexGetSubpointMap(), DMGetLabel(), DMLabelSetValue()
3310: @*/
3311: PetscErrorCode DMPlexFilter(DM dm, DMLabel cellLabel, PetscInt value, DM *subdm)
3312: {
3313:   PetscInt       dim;

3319:   DMGetDimension(dm, &dim);
3320:   DMCreate(PetscObjectComm((PetscObject) dm), subdm);
3321:   DMSetType(*subdm, DMPLEX);
3322:   DMSetDimension(*subdm, dim);
3323:   /* Extract submesh in place, could be empty on some procs, could have inconsistency if procs do not both extract a shared cell */
3324:   DMPlexCreateSubmeshGeneric_Interpolated(dm, cellLabel, value, PETSC_FALSE, 0, *subdm);
3325:   return(0);
3326: }

3328: /*@
3329:   DMPlexGetSubpointMap - Returns a DMLabel with point dimension as values

3331:   Input Parameter:
3332: . dm - The submesh DM

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

3337:   Level: developer

3339: .seealso: DMPlexCreateSubmesh(), DMPlexCreateSubpointIS()
3340: @*/
3341: PetscErrorCode DMPlexGetSubpointMap(DM dm, DMLabel *subpointMap)
3342: {
3346:   *subpointMap = ((DM_Plex*) dm->data)->subpointMap;
3347:   return(0);
3348: }

3350: /* Note: Should normally not be called by the user, since it is set in DMPlexCreateSubmesh() */
3351: PetscErrorCode DMPlexSetSubpointMap(DM dm, DMLabel subpointMap)
3352: {
3353:   DM_Plex       *mesh = (DM_Plex *) dm->data;
3354:   DMLabel        tmp;

3359:   tmp  = mesh->subpointMap;
3360:   mesh->subpointMap = subpointMap;
3361:   ++mesh->subpointMap->refct;
3362:   DMLabelDestroy(&tmp);
3363:   return(0);
3364: }

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

3369:   Input Parameter:
3370: . dm - The submesh DM

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

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

3377:   Level: developer

3379: .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointMap()
3380: @*/
3381: PetscErrorCode DMPlexCreateSubpointIS(DM dm, IS *subpointIS)
3382: {
3383:   MPI_Comm        comm;
3384:   DMLabel         subpointMap;
3385:   IS              is;
3386:   const PetscInt *opoints;
3387:   PetscInt       *points, *depths;
3388:   PetscInt        depth, depStart, depEnd, d, pStart, pEnd, p, n, off;
3389:   PetscErrorCode  ierr;

3394:   PetscObjectGetComm((PetscObject)dm,&comm);
3395:   *subpointIS = NULL;
3396:   DMPlexGetSubpointMap(dm, &subpointMap);
3397:   DMPlexGetDepth(dm, &depth);
3398:   if (subpointMap && depth >= 0) {
3399:     DMPlexGetChart(dm, &pStart, &pEnd);
3400:     if (pStart) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Submeshes must start the point numbering at 0, not %d", pStart);
3401:     DMGetWorkArray(dm, depth+1, PETSC_INT, &depths);
3402:     depths[0] = depth;
3403:     depths[1] = 0;
3404:     for(d = 2; d <= depth; ++d) {depths[d] = depth+1 - d;}
3405:     PetscMalloc1(pEnd, &points);
3406:     for(d = 0, off = 0; d <= depth; ++d) {
3407:       const PetscInt dep = depths[d];

3409:       DMPlexGetDepthStratum(dm, dep, &depStart, &depEnd);
3410:       DMLabelGetStratumSize(subpointMap, dep, &n);
3411:       if (((d < 2) && (depth > 1)) || (d == 1)) { /* Only check vertices and cells for now since the map is broken for others */
3412:         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);
3413:       } else {
3414:         if (!n) {
3415:           if (d == 0) {
3416:             /* Missing cells */
3417:             for(p = 0; p < depEnd-depStart; ++p, ++off) points[off] = -1;
3418:           } else {
3419:             /* Missing faces */
3420:             for(p = 0; p < depEnd-depStart; ++p, ++off) points[off] = PETSC_MAX_INT;
3421:           }
3422:         }
3423:       }
3424:       if (n) {
3425:         DMLabelGetStratumIS(subpointMap, dep, &is);
3426:         ISGetIndices(is, &opoints);
3427:         for(p = 0; p < n; ++p, ++off) points[off] = opoints[p];
3428:         ISRestoreIndices(is, &opoints);
3429:         ISDestroy(&is);
3430:       }
3431:     }
3432:     DMRestoreWorkArray(dm, depth+1, PETSC_INT, &depths);
3433:     if (off != pEnd) SETERRQ2(comm, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %d should be %d", off, pEnd);
3434:     ISCreateGeneral(PETSC_COMM_SELF, pEnd, points, PETSC_OWN_POINTER, subpointIS);
3435:   }
3436:   return(0);
3437: }