Actual source code: plexsubmesh.c

petsc-dev 2014-08-26
Report Typos and Errors
  1: #include <petsc-private/dmpleximpl.h>   /*I      "petscdmplex.h"   I*/
  2: #include <petscsf.h>

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

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

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

 25: /*@
 26:   DMPlexMarkBoundaryFaces - Mark all faces on the boundary

 28:   Not Collective

 30:   Input Parameter:
 31: . dm - The original DM

 33:   Output Parameter:
 34: . label - The DMLabel marking boundary faces with value 1

 36:   Level: developer

 38: .seealso: DMLabelCreate(), DMPlexCreateLabel()
 39: @*/
 40: PetscErrorCode DMPlexMarkBoundaryFaces(DM dm, DMLabel label)
 41: {

 45:   DMPlexMarkBoundaryFaces_Internal(dm, 0, label);
 46:   return(0);
 47: }

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

 54:   Input Parameters:
 55: + dm - The DM
 56: - label - A DMLabel marking the surface points

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

 61:   Level: developer

 63: .seealso: DMPlexLabelCohesiveComplete()
 64: @*/
 65: PetscErrorCode DMPlexLabelComplete(DM dm, DMLabel label)
 66: {
 67:   IS              valueIS;
 68:   const PetscInt *values;
 69:   PetscInt        numValues, v;
 70:   PetscErrorCode  ierr;

 73:   DMLabelGetNumValues(label, &numValues);
 74:   DMLabelGetValueIS(label, &valueIS);
 75:   ISGetIndices(valueIS, &values);
 76:   for (v = 0; v < numValues; ++v) {
 77:     IS              pointIS;
 78:     const PetscInt *points;
 79:     PetscInt        numPoints, p;

 81:     DMLabelGetStratumSize(label, values[v], &numPoints);
 82:     DMLabelGetStratumIS(label, values[v], &pointIS);
 83:     ISGetIndices(pointIS, &points);
 84:     for (p = 0; p < numPoints; ++p) {
 85:       PetscInt *closure = NULL;
 86:       PetscInt  closureSize, c;

 88:       DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);
 89:       for (c = 0; c < closureSize*2; c += 2) {
 90:         DMLabelSetValue(label, closure[c], values[v]);
 91:       }
 92:       DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);
 93:     }
 94:     ISRestoreIndices(pointIS, &points);
 95:     ISDestroy(&pointIS);
 96:   }
 97:   ISRestoreIndices(valueIS, &values);
 98:   ISDestroy(&valueIS);
 99:   return(0);
100: }

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

107:   Input Parameters:
108: + dm - The DM
109: - label - A DMLabel marking the surface points

111:   Output Parameter:
112: . label - A DMLabel incorporating cells

114:   Level: developer

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

118: .seealso: DMPlexLabelComplete(), DMPlexLabelCohesiveComplete()
119: @*/
120: PetscErrorCode DMPlexLabelAddCells(DM dm, DMLabel label)
121: {
122:   IS              valueIS;
123:   const PetscInt *values;
124:   PetscInt        numValues, v, cStart, cEnd;
125:   PetscErrorCode  ierr;

128:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
129:   DMLabelGetNumValues(label, &numValues);
130:   DMLabelGetValueIS(label, &valueIS);
131:   ISGetIndices(valueIS, &values);
132:   for (v = 0; v < numValues; ++v) {
133:     IS              pointIS;
134:     const PetscInt *points;
135:     PetscInt        numPoints, p;

137:     DMLabelGetStratumSize(label, values[v], &numPoints);
138:     DMLabelGetStratumIS(label, values[v], &pointIS);
139:     ISGetIndices(pointIS, &points);
140:     for (p = 0; p < numPoints; ++p) {
141:       PetscInt *closure = NULL;
142:       PetscInt  closureSize, point;

144:       DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure);
145:       point = closure[(closureSize-1)*2];
146:       if ((point >= cStart) && (point < cEnd)) {DMLabelSetValue(label, point, values[v]);}
147:       DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure);
148:     }
149:     ISRestoreIndices(pointIS, &points);
150:     ISDestroy(&pointIS);
151:   }
152:   ISRestoreIndices(valueIS, &values);
153:   ISDestroy(&valueIS);
154:   return(0);
155: }

159: PETSC_STATIC_INLINE PetscInt DMPlexShiftPoint_Internal(PetscInt p, PetscInt depth, PetscInt depthMax[], PetscInt depthEnd[], PetscInt depthShift[])
160: {
161:   if (depth < 0) return p;
162:   /* Normal Cells                 */ if (p < depthMax[depth])                return p;
163:   /* Hybrid Cells+Normal Vertices */ if (p < depthMax[0])                    return p + depthShift[depth];
164:   /* Hybrid Vertices+Normal Faces */ if (depth < 2 || p < depthMax[depth-1]) return p + depthShift[depth] + depthShift[0];
165:   /* Hybrid Faces+Normal Edges    */ if (depth < 3 || p < depthMax[depth-2]) return p + depthShift[depth] + depthShift[0] + depthShift[depth-1];
166:   /* Hybrid Edges                 */                                         return p + depthShift[depth] + depthShift[0] + depthShift[depth-1] + depthShift[depth-2];
167: }

171: static PetscErrorCode DMPlexShiftSizes_Internal(DM dm, PetscInt depthShift[], DM dmNew)
172: {
173:   PetscInt      *depthMax, *depthEnd;
174:   PetscInt       depth = 0, d, pStart, pEnd, p;

178:   DMPlexGetDepth(dm, &depth);
179:   if (depth < 0) return(0);
180:   PetscMalloc2(depth+1,&depthMax,depth+1,&depthEnd);
181:   /* Step 1: Expand chart */
182:   DMPlexGetChart(dm, &pStart, &pEnd);
183:   DMPlexGetHybridBounds(dm, &depthMax[depth], depth > 0 ? &depthMax[depth-1] : NULL, &depthMax[1], &depthMax[0]);
184:   for (d = 0; d <= depth; ++d) {
185:     pEnd += depthShift[d];
186:     DMPlexGetDepthStratum(dm, d, NULL, &depthEnd[d]);
187:     depthMax[d] = depthMax[d] < 0 ? depthEnd[d] : depthMax[d];
188:   }
189:   DMPlexSetChart(dmNew, pStart, pEnd);
190:   /* Step 2: Set cone and support sizes */
191:   for (d = 0; d <= depth; ++d) {
192:     DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);
193:     for (p = pStart; p < pEnd; ++p) {
194:       PetscInt newp = DMPlexShiftPoint_Internal(p, depth, depthMax, depthEnd, depthShift);
195:       PetscInt size;

197:       DMPlexGetConeSize(dm, p, &size);
198:       DMPlexSetConeSize(dmNew, newp, size);
199:       DMPlexGetSupportSize(dm, p, &size);
200:       DMPlexSetSupportSize(dmNew, newp, size);
201:     }
202:   }
203:   PetscFree2(depthMax,depthEnd);
204:   return(0);
205: }

209: static PetscErrorCode DMPlexShiftPoints_Internal(DM dm, PetscInt depthShift[], DM dmNew)
210: {
211:   PetscInt      *depthEnd, *depthMax, *newpoints;
212:   PetscInt       depth = 0, d, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, pStart, pEnd, p;

216:   DMPlexGetDepth(dm, &depth);
217:   if (depth < 0) return(0);
218:   DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);
219:   DMPlexGetMaxSizes(dmNew, &maxConeSizeNew, &maxSupportSizeNew);
220:   PetscMalloc3(depth+1,&depthEnd,depth+1,&depthMax,PetscMax(PetscMax(maxConeSize, maxSupportSize), PetscMax(maxConeSizeNew, maxSupportSizeNew)),&newpoints);
221:   DMPlexGetHybridBounds(dm, &depthMax[depth], depth > 0 ? &depthMax[depth-1] : NULL, &depthMax[1], &depthMax[0]);
222:   for (d = 0; d <= depth; ++d) {
223:     DMPlexGetDepthStratum(dm, d, NULL, &depthEnd[d]);
224:     depthMax[d] = depthMax[d] < 0 ? depthEnd[d] : depthMax[d];
225:   }
226:   /* Step 5: Set cones and supports */
227:   DMPlexGetChart(dm, &pStart, &pEnd);
228:   for (p = pStart; p < pEnd; ++p) {
229:     const PetscInt *points = NULL, *orientations = NULL;
230:     PetscInt        size,sizeNew, i, newp = DMPlexShiftPoint_Internal(p, depth, depthMax, depthEnd, depthShift);

232:     DMPlexGetConeSize(dm, p, &size);
233:     DMPlexGetCone(dm, p, &points);
234:     DMPlexGetConeOrientation(dm, p, &orientations);
235:     for (i = 0; i < size; ++i) {
236:       newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthMax, depthEnd, depthShift);
237:     }
238:     DMPlexSetCone(dmNew, newp, newpoints);
239:     DMPlexSetConeOrientation(dmNew, newp, orientations);
240:     DMPlexGetSupportSize(dm, p, &size);
241:     DMPlexGetSupportSize(dmNew, newp, &sizeNew);
242:     DMPlexGetSupport(dm, p, &points);
243:     for (i = 0; i < size; ++i) {
244:       newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthMax, depthEnd, depthShift);
245:     }
246:     for (i = size; i < sizeNew; ++i) newpoints[i] = 0;
247:     DMPlexSetSupport(dmNew, newp, newpoints);
248:   }
249:   PetscFree3(depthEnd,depthMax,newpoints);
250:   return(0);
251: }

255: static PetscErrorCode DMPlexShiftCoordinates_Internal(DM dm, PetscInt depthShift[], DM dmNew)
256: {
257:   PetscSection   coordSection, newCoordSection;
258:   Vec            coordinates, newCoordinates;
259:   PetscScalar   *coords, *newCoords;
260:   PetscInt      *depthEnd, coordSize;
261:   PetscInt       dim, depth = 0, d, vStart, vEnd, vStartNew, vEndNew, v;

265:   DMGetDimension(dm, &dim);
266:   DMPlexGetDepth(dm, &depth);
267:   PetscMalloc1((depth+1), &depthEnd);
268:   for (d = 0; d <= depth; ++d) {
269:     DMPlexGetDepthStratum(dm, d, NULL, &depthEnd[d]);
270:   }
271:   /* Step 8: Convert coordinates */
272:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
273:   DMPlexGetDepthStratum(dmNew, 0, &vStartNew, &vEndNew);
274:   DMGetCoordinateSection(dm, &coordSection);
275:   PetscSectionCreate(PetscObjectComm((PetscObject)dm), &newCoordSection);
276:   PetscSectionSetNumFields(newCoordSection, 1);
277:   PetscSectionSetFieldComponents(newCoordSection, 0, dim);
278:   PetscSectionSetChart(newCoordSection, vStartNew, vEndNew);
279:   for (v = vStartNew; v < vEndNew; ++v) {
280:     PetscSectionSetDof(newCoordSection, v, dim);
281:     PetscSectionSetFieldDof(newCoordSection, v, 0, dim);
282:   }
283:   PetscSectionSetUp(newCoordSection);
284:   DMSetCoordinateSection(dmNew, PETSC_DETERMINE, newCoordSection);
285:   PetscSectionGetStorageSize(newCoordSection, &coordSize);
286:   VecCreate(PetscObjectComm((PetscObject)dm), &newCoordinates);
287:   PetscObjectSetName((PetscObject) newCoordinates, "coordinates");
288:   VecSetSizes(newCoordinates, coordSize, PETSC_DETERMINE);
289:   VecSetType(newCoordinates,VECSTANDARD);
290:   DMSetCoordinatesLocal(dmNew, newCoordinates);
291:   DMGetCoordinatesLocal(dm, &coordinates);
292:   VecGetArray(coordinates, &coords);
293:   VecGetArray(newCoordinates, &newCoords);
294:   for (v = vStart; v < vEnd; ++v) {
295:     PetscInt dof, off, noff, d;

297:     PetscSectionGetDof(coordSection, v, &dof);
298:     PetscSectionGetOffset(coordSection, v, &off);
299:     PetscSectionGetOffset(newCoordSection, DMPlexShiftPoint_Internal(v, depth, depthEnd, depthEnd, depthShift), &noff);
300:     for (d = 0; d < dof; ++d) {
301:       newCoords[noff+d] = coords[off+d];
302:     }
303:   }
304:   VecRestoreArray(coordinates, &coords);
305:   VecRestoreArray(newCoordinates, &newCoords);
306:   VecDestroy(&newCoordinates);
307:   PetscSectionDestroy(&newCoordSection);
308:   PetscFree(depthEnd);
309:   return(0);
310: }

314: static PetscErrorCode DMPlexShiftSF_Internal(DM dm, PetscInt depthShift[], DM dmNew)
315: {
316:   PetscInt          *depthMax, *depthEnd;
317:   PetscInt           depth = 0, d;
318:   PetscSF            sfPoint, sfPointNew;
319:   const PetscSFNode *remotePoints;
320:   PetscSFNode       *gremotePoints;
321:   const PetscInt    *localPoints;
322:   PetscInt          *glocalPoints, *newLocation, *newRemoteLocation;
323:   PetscInt           numRoots, numLeaves, l, pStart, pEnd, totShift = 0;
324:   PetscErrorCode     ierr;

327:   DMPlexGetDepth(dm, &depth);
328:   PetscMalloc2(depth+1,&depthMax,depth+1,&depthEnd);
329:   DMPlexGetHybridBounds(dm, depth >= 0 ? &depthMax[depth] : NULL, depth>1 ? &depthMax[depth-1] : NULL, depth>2 ? &depthMax[1] : NULL, depth >= 0 ? &depthMax[0] : NULL);
330:   for (d = 0; d <= depth; ++d) {
331:     totShift += depthShift[d];
332:     DMPlexGetDepthStratum(dm, d, NULL, &depthEnd[d]);
333:     depthMax[d] = depthMax[d] < 0 ? depthEnd[d] : depthMax[d];
334:   }
335:   /* Step 9: Convert pointSF */
336:   DMGetPointSF(dm, &sfPoint);
337:   DMGetPointSF(dmNew, &sfPointNew);
338:   DMPlexGetChart(dm, &pStart, &pEnd);
339:   PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);
340:   if (numRoots >= 0) {
341:     PetscMalloc2(numRoots,&newLocation,pEnd-pStart,&newRemoteLocation);
342:     for (l=0; l<numRoots; l++) newLocation[l] = DMPlexShiftPoint_Internal(l, depth, depthMax, depthEnd, depthShift);
343:     PetscSFBcastBegin(sfPoint, MPIU_INT, newLocation, newRemoteLocation);
344:     PetscSFBcastEnd(sfPoint, MPIU_INT, newLocation, newRemoteLocation);
345:     PetscMalloc1(numLeaves,    &glocalPoints);
346:     PetscMalloc1(numLeaves, &gremotePoints);
347:     for (l = 0; l < numLeaves; ++l) {
348:       glocalPoints[l]        = DMPlexShiftPoint_Internal(localPoints[l], depth, depthMax, depthEnd, depthShift);
349:       gremotePoints[l].rank  = remotePoints[l].rank;
350:       gremotePoints[l].index = newRemoteLocation[localPoints[l]];
351:     }
352:     PetscFree2(newLocation,newRemoteLocation);
353:     PetscSFSetGraph(sfPointNew, numRoots + totShift, numLeaves, glocalPoints, PETSC_OWN_POINTER, gremotePoints, PETSC_OWN_POINTER);
354:   }
355:   PetscFree2(depthMax,depthEnd);
356:   return(0);
357: }

361: static PetscErrorCode DMPlexShiftLabels_Internal(DM dm, PetscInt depthShift[], DM dmNew)
362: {
363:   PetscSF            sfPoint;
364:   DMLabel            vtkLabel, ghostLabel;
365:   PetscInt          *depthMax, *depthEnd;
366:   const PetscSFNode *leafRemote;
367:   const PetscInt    *leafLocal;
368:   PetscInt           depth = 0, d, numLeaves, numLabels, l, cStart, cEnd, c, fStart, fEnd, f;
369:   PetscMPIInt        rank;
370:   PetscErrorCode     ierr;

373:   DMPlexGetDepth(dm, &depth);
374:   PetscMalloc2(depth+1,&depthMax,depth+1,&depthEnd);
375:   DMPlexGetHybridBounds(dm, depth >= 0 ? &depthMax[depth] : NULL, depth>1 ? &depthMax[depth-1] : NULL, depth>2 ? &depthMax[1] : NULL, depth >= 0 ? &depthMax[0] : NULL);
376:   for (d = 0; d <= depth; ++d) {
377:     DMPlexGetDepthStratum(dm, d, NULL, &depthEnd[d]);
378:     depthMax[d] = depthMax[d] < 0 ? depthEnd[d] : depthMax[d];
379:   }
380:   /* Step 10: Convert labels */
381:   DMPlexGetNumLabels(dm, &numLabels);
382:   for (l = 0; l < numLabels; ++l) {
383:     DMLabel         label, newlabel;
384:     const char     *lname;
385:     PetscBool       isDepth;
386:     IS              valueIS;
387:     const PetscInt *values;
388:     PetscInt        numValues, val;

390:     DMPlexGetLabelName(dm, l, &lname);
391:     PetscStrcmp(lname, "depth", &isDepth);
392:     if (isDepth) continue;
393:     DMPlexCreateLabel(dmNew, lname);
394:     DMPlexGetLabel(dm, lname, &label);
395:     DMPlexGetLabel(dmNew, lname, &newlabel);
396:     DMLabelGetValueIS(label, &valueIS);
397:     ISGetLocalSize(valueIS, &numValues);
398:     ISGetIndices(valueIS, &values);
399:     for (val = 0; val < numValues; ++val) {
400:       IS              pointIS;
401:       const PetscInt *points;
402:       PetscInt        numPoints, p;

404:       DMLabelGetStratumIS(label, values[val], &pointIS);
405:       ISGetLocalSize(pointIS, &numPoints);
406:       ISGetIndices(pointIS, &points);
407:       for (p = 0; p < numPoints; ++p) {
408:         const PetscInt newpoint = DMPlexShiftPoint_Internal(points[p], depth, depthMax, depthEnd, depthShift);

410:         DMLabelSetValue(newlabel, newpoint, values[val]);
411:       }
412:       ISRestoreIndices(pointIS, &points);
413:       ISDestroy(&pointIS);
414:     }
415:     ISRestoreIndices(valueIS, &values);
416:     ISDestroy(&valueIS);
417:   }
418:   PetscFree2(depthMax,depthEnd);
419:   /* Step 11: Make label for output (vtk) and to mark ghost points (ghost) */
420:   MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);
421:   DMGetPointSF(dm, &sfPoint);
422:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
423:   PetscSFGetGraph(sfPoint, NULL, &numLeaves, &leafLocal, &leafRemote);
424:   DMPlexCreateLabel(dmNew, "vtk");
425:   DMPlexCreateLabel(dmNew, "ghost");
426:   DMPlexGetLabel(dmNew, "vtk", &vtkLabel);
427:   DMPlexGetLabel(dmNew, "ghost", &ghostLabel);
428:   for (l = 0, c = cStart; l < numLeaves && c < cEnd; ++l, ++c) {
429:     for (; c < leafLocal[l] && c < cEnd; ++c) {
430:       DMLabelSetValue(vtkLabel, c, 1);
431:     }
432:     if (leafLocal[l] >= cEnd) break;
433:     if (leafRemote[l].rank == rank) {
434:       DMLabelSetValue(vtkLabel, c, 1);
435:     } else {
436:       DMLabelSetValue(ghostLabel, c, 2);
437:     }
438:   }
439:   for (; c < cEnd; ++c) {
440:     DMLabelSetValue(vtkLabel, c, 1);
441:   }
442:   if (0) {
443:     PetscViewerASCIISynchronizedAllow(PETSC_VIEWER_STDOUT_WORLD, PETSC_TRUE);
444:     DMLabelView(vtkLabel, PETSC_VIEWER_STDOUT_WORLD);
445:     PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD);
446:   }
447:   DMPlexGetHeightStratum(dmNew, 1, &fStart, &fEnd);
448:   for (f = fStart; f < fEnd; ++f) {
449:     PetscInt numCells;

451:     DMPlexGetSupportSize(dmNew, f, &numCells);
452:     if (numCells < 2) {
453:       DMLabelSetValue(ghostLabel, f, 1);
454:     } else {
455:       const PetscInt *cells = NULL;
456:       PetscInt        vA, vB;

458:       DMPlexGetSupport(dmNew, f, &cells);
459:       DMLabelGetValue(vtkLabel, cells[0], &vA);
460:       DMLabelGetValue(vtkLabel, cells[1], &vB);
461:       if (!vA && !vB) {DMLabelSetValue(ghostLabel, f, 1);}
462:     }
463:   }
464:   if (0) {
465:     PetscViewerASCIISynchronizedAllow(PETSC_VIEWER_STDOUT_WORLD, PETSC_TRUE);
466:     DMLabelView(ghostLabel, PETSC_VIEWER_STDOUT_WORLD);
467:     PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD);
468:   }
469:   return(0);
470: }

474: static PetscErrorCode DMPlexConstructGhostCells_Internal(DM dm, DMLabel label, PetscInt *numGhostCells, DM gdm)
475: {
476:   IS              valueIS;
477:   const PetscInt *values;
478:   PetscInt       *depthShift;
479:   PetscInt        depth = 0, Ng, numFS, fs, fStart, fEnd, ghostCell, cEnd, c;
480:   PetscErrorCode  ierr;

483:   DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);
484:   /* Count ghost cells */
485:   DMLabelGetValueIS(label, &valueIS);
486:   ISGetLocalSize(valueIS, &numFS);
487:   ISGetIndices(valueIS, &values);
488:   Ng   = 0;
489:   for (fs = 0; fs < numFS; ++fs) {
490:     IS              faceIS;
491:     const PetscInt *faces;
492:     PetscInt        numFaces, f, numBdFaces = 0;

494:     DMLabelGetStratumIS(label, values[fs], &faceIS);
495:     ISGetLocalSize(faceIS, &numFaces);
496:     ISGetIndices(faceIS, &faces);
497:     for (f = 0; f < numFaces; ++f) {
498:       if ((faces[f] >= fStart) && (faces[f] < fEnd)) ++numBdFaces;
499:     }
500:     Ng += numBdFaces;
501:     ISDestroy(&faceIS);
502:   }
503:   DMPlexGetDepth(dm, &depth);
504:   PetscMalloc1((depth+1), &depthShift);
505:   PetscMemzero(depthShift, (depth+1) * sizeof(PetscInt));
506:   if (depth >= 0) depthShift[depth] = Ng;
507:   DMPlexShiftSizes_Internal(dm, depthShift, gdm);
508:   /* Step 3: Set cone/support sizes for new points */
509:   DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);
510:   DMPlexSetHybridBounds(gdm, cEnd, PETSC_DETERMINE, PETSC_DETERMINE, PETSC_DETERMINE);
511:   for (c = cEnd; c < cEnd + Ng; ++c) {
512:     DMPlexSetConeSize(gdm, c, 1);
513:   }
514:   for (fs = 0; fs < numFS; ++fs) {
515:     IS              faceIS;
516:     const PetscInt *faces;
517:     PetscInt        numFaces, f;

519:     DMLabelGetStratumIS(label, values[fs], &faceIS);
520:     ISGetLocalSize(faceIS, &numFaces);
521:     ISGetIndices(faceIS, &faces);
522:     for (f = 0; f < numFaces; ++f) {
523:       PetscInt size;

525:       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
526:       DMPlexGetSupportSize(dm, faces[f], &size);
527:       if (size != 1) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "DM has boundary face %d with %d support cells", faces[f], size);
528:       DMPlexSetSupportSize(gdm, faces[f] + Ng, 2);
529:     }
530:     ISRestoreIndices(faceIS, &faces);
531:     ISDestroy(&faceIS);
532:   }
533:   /* Step 4: Setup ghosted DM */
534:   DMSetUp(gdm);
535:   DMPlexShiftPoints_Internal(dm, depthShift, gdm);
536:   /* Step 6: Set cones and supports for new points */
537:   ghostCell = cEnd;
538:   for (fs = 0; fs < numFS; ++fs) {
539:     IS              faceIS;
540:     const PetscInt *faces;
541:     PetscInt        numFaces, f;

543:     DMLabelGetStratumIS(label, values[fs], &faceIS);
544:     ISGetLocalSize(faceIS, &numFaces);
545:     ISGetIndices(faceIS, &faces);
546:     for (f = 0; f < numFaces; ++f) {
547:       PetscInt newFace = faces[f] + Ng;

549:       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
550:       DMPlexSetCone(gdm, ghostCell, &newFace);
551:       DMPlexInsertSupport(gdm, newFace, 1, ghostCell);
552:       ++ghostCell;
553:     }
554:     ISRestoreIndices(faceIS, &faces);
555:     ISDestroy(&faceIS);
556:   }
557:   ISRestoreIndices(valueIS, &values);
558:   ISDestroy(&valueIS);
559:   /* Step 7: Stratify */
560:   DMPlexStratify(gdm);
561:   DMPlexShiftCoordinates_Internal(dm, depthShift, gdm);
562:   DMPlexShiftSF_Internal(dm, depthShift, gdm);
563:   DMPlexShiftLabels_Internal(dm, depthShift, gdm);
564:   PetscFree(depthShift);
565:   if (numGhostCells) *numGhostCells = Ng;
566:   return(0);
567: }

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

574:   Collective on dm

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

580:   Output Parameters:
581: + numGhostCells - The number of ghost cells added to the DM
582: - dmGhosted - The new DM

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

586:   Level: developer

588: .seealso: DMCreate()
589: @*/
590: PetscErrorCode DMPlexConstructGhostCells(DM dm, const char labelName[], PetscInt *numGhostCells, DM *dmGhosted)
591: {
592:   DM             gdm;
593:   DMLabel        label;
594:   const char    *name = labelName ? labelName : "Face Sets";
595:   PetscInt       dim;
596:   PetscBool      flag;

603:   DMCreate(PetscObjectComm((PetscObject)dm), &gdm);
604:   DMSetType(gdm, DMPLEX);
605:   DMGetDimension(dm, &dim);
606:   DMSetDimension(gdm, dim);
607:   DMPlexGetAdjacencyUseCone(dm, &flag);
608:   DMPlexSetAdjacencyUseCone(gdm, flag);
609:   DMPlexGetAdjacencyUseClosure(dm, &flag);
610:   DMPlexSetAdjacencyUseClosure(gdm, flag);
611:   DMPlexGetLabel(dm, name, &label);
612:   if (!label) {
613:     /* Get label for boundary faces */
614:     DMPlexCreateLabel(dm, name);
615:     DMPlexGetLabel(dm, name, &label);
616:     DMPlexMarkBoundaryFaces(dm, label);
617:   }
618:   DMPlexConstructGhostCells_Internal(dm, label, numGhostCells, gdm);
619:   DMPlexCopyBoundary(dm, gdm);
620:   *dmGhosted = gdm;
621:   return(0);
622: }

626: /*
627:   We are adding three kinds of points here:
628:     Replicated:     Copies of points which exist in the mesh, such as vertices identified across a fault
629:     Non-replicated: Points which exist on the fault, but are not replicated
630:     Hybrid:         Entirely new points, such as cohesive cells

632:   When creating subsequent cohesive cells, we shift the old hybrid cells to the end of the numbering at
633:   each depth so that the new split/hybrid points can be inserted as a block.
634: */
635: static PetscErrorCode DMPlexConstructCohesiveCells_Internal(DM dm, DMLabel label, DM sdm)
636: {
637:   MPI_Comm         comm;
638:   IS               valueIS;
639:   PetscInt         numSP = 0;       /* The number of depths for which we have replicated points */
640:   const PetscInt  *values;          /* List of depths for which we have replicated points */
641:   IS              *splitIS;
642:   IS              *unsplitIS;
643:   PetscInt        *numSplitPoints;     /* The number of replicated points at each depth */
644:   PetscInt        *numUnsplitPoints;   /* The number of non-replicated points at each depth which still give rise to hybrid points */
645:   PetscInt        *numHybridPoints;    /* The number of new hybrid points at each depth */
646:   PetscInt        *numHybridPointsOld; /* The number of existing hybrid points at each depth */
647:   const PetscInt **splitPoints;        /* Replicated points for each depth */
648:   const PetscInt **unsplitPoints;      /* Non-replicated points for each depth */
649:   PetscSection     coordSection;
650:   Vec              coordinates;
651:   PetscScalar     *coords;
652:   PetscInt         depths[4];          /* Depths in the order that plex points are numbered */
653:   PetscInt        *depthMax;           /* The first hybrid point at each depth in the original mesh */
654:   PetscInt        *depthEnd;           /* The point limit at each depth in the original mesh */
655:   PetscInt        *depthShift;         /* Number of replicated+hybrid points at each depth */
656:   PetscInt        *depthOffset;        /* Prefix sums of depthShift */
657:   PetscInt        *pMaxNew;            /* The first replicated point at each depth in the new mesh, hybrids come after this */
658:   PetscInt        *coneNew, *coneONew, *supportNew;
659:   PetscInt         shift = 100, shift2 = 200, depth = 0, dep, dim, d, sp, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, numLabels, vStart, vEnd, pEnd, p, v;
660:   PetscErrorCode   ierr;

663:   PetscObjectGetComm((PetscObject)dm,&comm);
664:   DMGetDimension(dm, &dim);
665:   DMPlexGetDepth(dm, &depth);
666:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
667:   depths[0] = depth;
668:   depths[1] = 0;
669:   depths[2] = depth-1;
670:   depths[3] = 1;
671:   /* Count split points and add cohesive cells */
672:   DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);
673:   PetscMalloc6(depth+1,&depthMax,depth+1,&depthEnd,depth+1,&depthShift,depth+1,&depthOffset,depth+1,&pMaxNew,depth+1,&numHybridPointsOld);
674:   PetscMalloc7(depth+1,&splitIS,depth+1,&unsplitIS,depth+1,&numSplitPoints,depth+1,&numUnsplitPoints,depth+1,&numHybridPoints,depth+1,&splitPoints,depth+1,&unsplitPoints);
675:   PetscMemzero(depthShift,  (depth+1) * sizeof(PetscInt));
676:   PetscMemzero(depthOffset, (depth+1) * sizeof(PetscInt));
677:   DMPlexGetHybridBounds(dm, depth >= 0 ? &depthMax[depth] : NULL, depth>1 ? &depthMax[depth-1] : NULL, depth>2 ? &depthMax[1] : NULL, depth >= 0 ? &depthMax[0] : NULL);
678:   for (d = 0; d <= depth; ++d) {
679:     DMPlexGetDepthStratum(dm, d, NULL, &pMaxNew[d]);
680:     depthEnd[d]           = pMaxNew[d];
681:     depthMax[d]           = depthMax[d] < 0 ? depthEnd[d] : depthMax[d];
682:     numSplitPoints[d]     = 0;
683:     numUnsplitPoints[d]   = 0;
684:     numHybridPoints[d]    = 0;
685:     numHybridPointsOld[d] = depthMax[d] < 0 ? 0 : depthEnd[d] - depthMax[d];
686:     splitPoints[d]        = NULL;
687:     unsplitPoints[d]      = NULL;
688:     splitIS[d]            = NULL;
689:     unsplitIS[d]          = NULL;
690:   }
691:   if (label) {
692:     DMLabelGetValueIS(label, &valueIS);
693:     ISGetLocalSize(valueIS, &numSP);
694:     ISGetIndices(valueIS, &values);
695:   }
696:   for (sp = 0; sp < numSP; ++sp) {
697:     const PetscInt dep = values[sp];

699:     if ((dep < 0) || (dep > depth)) continue;
700:     DMLabelGetStratumIS(label, dep, &splitIS[dep]);
701:     if (splitIS[dep]) {
702:       ISGetLocalSize(splitIS[dep], &numSplitPoints[dep]);
703:       ISGetIndices(splitIS[dep], &splitPoints[dep]);
704:     }
705:     DMLabelGetStratumIS(label, shift2+dep, &unsplitIS[dep]);
706:     if (unsplitIS[dep]) {
707:       ISGetLocalSize(unsplitIS[dep], &numUnsplitPoints[dep]);
708:       ISGetIndices(unsplitIS[dep], &unsplitPoints[dep]);
709:     }
710:   }
711:   /* Calculate number of hybrid points */
712:   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   */
713:   for (d = 0; d <= depth; ++d) depthShift[d]          = numSplitPoints[d] + numHybridPoints[d];
714:   for (d = 1; d <= depth; ++d) depthOffset[depths[d]] = depthOffset[depths[d-1]] + depthShift[depths[d-1]];
715:   for (d = 0; d <= depth; ++d) pMaxNew[d]            += depthOffset[d] - numHybridPointsOld[d];
716:   DMPlexShiftSizes_Internal(dm, depthShift, sdm);
717:   /* Step 3: Set cone/support sizes for new points */
718:   for (dep = 0; dep <= depth; ++dep) {
719:     for (p = 0; p < numSplitPoints[dep]; ++p) {
720:       const PetscInt  oldp   = splitPoints[dep][p];
721:       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthMax, depthEnd, depthShift) /*oldp + depthOffset[dep]*/;
722:       const PetscInt  splitp = p    + pMaxNew[dep];
723:       const PetscInt *support;
724:       PetscInt        coneSize, supportSize, qf, qn, qp, e;

726:       DMPlexGetConeSize(dm, oldp, &coneSize);
727:       DMPlexSetConeSize(sdm, splitp, coneSize);
728:       DMPlexGetSupportSize(dm, oldp, &supportSize);
729:       DMPlexSetSupportSize(sdm, splitp, supportSize);
730:       if (dep == depth-1) {
731:         const PetscInt hybcell = p + pMaxNew[dep+1] + numSplitPoints[dep+1];

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

738:         DMPlexGetSupport(dm, oldp, &support);
739:         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
740:           PetscInt val;

742:           DMLabelGetValue(label, support[e], &val);
743:           if (val == 1) ++qf;
744:           if ((val == 1) || (val ==  (shift + 1))) ++qn;
745:           if ((val == 1) || (val == -(shift + 1))) ++qp;
746:         }
747:         /* Split old vertex: Edges into original vertex and new cohesive edge */
748:         DMPlexSetSupportSize(sdm, newp, qn+1);
749:         /* Split new vertex: Edges into split vertex and new cohesive edge */
750:         DMPlexSetSupportSize(sdm, splitp, qp+1);
751:         /* Add hybrid edge */
752:         DMPlexSetConeSize(sdm, hybedge, 2);
753:         DMPlexSetSupportSize(sdm, hybedge, qf);
754:       } else if (dep == dim-2) {
755:         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];

757:         DMPlexGetSupport(dm, oldp, &support);
758:         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
759:           PetscInt val;

761:           DMLabelGetValue(label, support[e], &val);
762:           if (val == dim-1) ++qf;
763:           if ((val == dim-1) || (val ==  (shift + dim-1))) ++qn;
764:           if ((val == dim-1) || (val == -(shift + dim-1))) ++qp;
765:         }
766:         /* Split old edge: Faces into original edge and cohesive face (positive side?) */
767:         DMPlexSetSupportSize(sdm, newp, qn+1);
768:         /* Split new edge: Faces into split edge and cohesive face (negative side?) */
769:         DMPlexSetSupportSize(sdm, splitp, qp+1);
770:         /* Add hybrid face */
771:         DMPlexSetConeSize(sdm, hybface, 4);
772:         DMPlexSetSupportSize(sdm, hybface, qf);
773:       }
774:     }
775:   }
776:   for (dep = 0; dep <= depth; ++dep) {
777:     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
778:       const PetscInt  oldp   = unsplitPoints[dep][p];
779:       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthMax, depthEnd, depthShift) /*oldp + depthOffset[dep]*/;
780:       const PetscInt *support;
781:       PetscInt        coneSize, supportSize, qf, e, s;

783:       DMPlexGetConeSize(dm, oldp, &coneSize);
784:       DMPlexGetSupportSize(dm, oldp, &supportSize);
785:       DMPlexGetSupport(dm, oldp, &support);
786:       if (dep == 0) {
787:         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];

789:         /* Unsplit vertex: Edges into original vertex, split edges, and new cohesive edge twice */
790:         for (s = 0, qf = 0; s < supportSize; ++s, ++qf) {
791:           PetscFindInt(support[s], numSplitPoints[dep+1], splitPoints[dep+1], &e);
792:           if (e >= 0) ++qf;
793:         }
794:         DMPlexSetSupportSize(sdm, newp, qf+2);
795:         /* Add hybrid edge */
796:         DMPlexSetConeSize(sdm, hybedge, 2);
797:         for (e = 0, qf = 0; e < supportSize; ++e) {
798:           PetscInt val;

800:           DMLabelGetValue(label, support[e], &val);
801:           /* Split and unsplit edges produce hybrid faces */
802:           if (val == 1) ++qf;
803:           if (val == (shift2 + 1)) ++qf;
804:         }
805:         DMPlexSetSupportSize(sdm, hybedge, qf);
806:       } else if (dep == dim-2) {
807:         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
808:         PetscInt       val;

810:         for (e = 0, qf = 0; e < supportSize; ++e) {
811:           DMLabelGetValue(label, support[e], &val);
812:           if (val == dim-1) qf += 2;
813:           else              ++qf;
814:         }
815:         /* Unsplit edge: Faces into original edge, split face, and cohesive face twice */
816:         DMPlexSetSupportSize(sdm, newp, qf+2);
817:         /* Add hybrid face */
818:         for (e = 0, qf = 0; e < supportSize; ++e) {
819:           DMLabelGetValue(label, support[e], &val);
820:           if (val == dim-1) ++qf;
821:         }
822:         DMPlexSetConeSize(sdm, hybface, 4);
823:         DMPlexSetSupportSize(sdm, hybface, qf);
824:       }
825:     }
826:   }
827:   /* Step 4: Setup split DM */
828:   DMSetUp(sdm);
829:   DMPlexShiftPoints_Internal(dm, depthShift, sdm);
830:   DMPlexGetMaxSizes(sdm, &maxConeSizeNew, &maxSupportSizeNew);
831:   PetscMalloc3(PetscMax(maxConeSize, maxConeSizeNew)*3,&coneNew,PetscMax(maxConeSize, maxConeSizeNew)*3,&coneONew,PetscMax(maxSupportSize, maxSupportSizeNew),&supportNew);
832:   /* Step 6: Set cones and supports for new points */
833:   for (dep = 0; dep <= depth; ++dep) {
834:     for (p = 0; p < numSplitPoints[dep]; ++p) {
835:       const PetscInt  oldp   = splitPoints[dep][p];
836:       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthMax, depthEnd, depthShift) /*oldp + depthOffset[dep]*/;
837:       const PetscInt  splitp = p    + pMaxNew[dep];
838:       const PetscInt *cone, *support, *ornt;
839:       PetscInt        coneSize, supportSize, q, qf, qn, qp, v, e, s;

841:       DMPlexGetConeSize(dm, oldp, &coneSize);
842:       DMPlexGetCone(dm, oldp, &cone);
843:       DMPlexGetConeOrientation(dm, oldp, &ornt);
844:       DMPlexGetSupportSize(dm, oldp, &supportSize);
845:       DMPlexGetSupport(dm, oldp, &support);
846:       if (dep == depth-1) {
847:         PetscBool       hasUnsplit = PETSC_FALSE;
848:         const PetscInt  hybcell    = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
849:         const PetscInt *supportF;

851:         /* Split face:       copy in old face to new face to start */
852:         DMPlexGetSupport(sdm, newp,  &supportF);
853:         DMPlexSetSupport(sdm, splitp, supportF);
854:         /* Split old face:   old vertices/edges in cone so no change */
855:         /* Split new face:   new vertices/edges in cone */
856:         for (q = 0; q < coneSize; ++q) {
857:           PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);
858:           if (v < 0) {
859:             PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);
860:             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);
861:             coneNew[2+q] = DMPlexShiftPoint_Internal(cone[q], depth, depthMax, depthEnd, depthShift) /*cone[q] + depthOffset[dep-1]*/;
862:             hasUnsplit   = PETSC_TRUE;
863:           } else {
864:             coneNew[2+q] = v + pMaxNew[dep-1];
865:             if (dep > 1) {
866:               const PetscInt *econe;
867:               PetscInt        econeSize, r, vs, vu;

869:               DMPlexGetConeSize(dm, cone[q], &econeSize);
870:               DMPlexGetCone(dm, cone[q], &econe);
871:               for (r = 0; r < econeSize; ++r) {
872:                 PetscFindInt(econe[r], numSplitPoints[dep-2],   splitPoints[dep-2],   &vs);
873:                 PetscFindInt(econe[r], numUnsplitPoints[dep-2], unsplitPoints[dep-2], &vu);
874:                 if (vs >= 0) continue;
875:                 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);
876:                 hasUnsplit   = PETSC_TRUE;
877:               }
878:             }
879:           }
880:         }
881:         DMPlexSetCone(sdm, splitp, &coneNew[2]);
882:         DMPlexSetConeOrientation(sdm, splitp, ornt);
883:         /* Face support */
884:         for (s = 0; s < supportSize; ++s) {
885:           PetscInt val;

887:           DMLabelGetValue(label, support[s], &val);
888:           if (val < 0) {
889:             /* Split old face:   Replace negative side cell with cohesive cell */
890:              DMPlexInsertSupport(sdm, newp, s, hybcell);
891:           } else {
892:             /* Split new face:   Replace positive side cell with cohesive cell */
893:             DMPlexInsertSupport(sdm, splitp, s, hybcell);
894:             /* Get orientation for cohesive face */
895:             {
896:               const PetscInt *ncone, *nconeO;
897:               PetscInt        nconeSize, nc;

899:               DMPlexGetConeSize(dm, support[s], &nconeSize);
900:               DMPlexGetCone(dm, support[s], &ncone);
901:               DMPlexGetConeOrientation(dm, support[s], &nconeO);
902:               for (nc = 0; nc < nconeSize; ++nc) {
903:                 if (ncone[nc] == oldp) {
904:                   coneONew[0] = nconeO[nc];
905:                   break;
906:                 }
907:               }
908:               if (nc >= nconeSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate face %d in neighboring cell %d", oldp, support[s]);
909:             }
910:           }
911:         }
912:         /* Cohesive cell:    Old and new split face, then new cohesive faces */
913:         coneNew[0]  = newp;   /* Extracted negative side orientation above */
914:         coneNew[1]  = splitp;
915:         coneONew[1] = coneONew[0];
916:         for (q = 0; q < coneSize; ++q) {
917:           PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);
918:           if (v < 0) {
919:             PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);
920:             coneNew[2+q]  = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
921:             coneONew[2+q] = 0;
922:           } else {
923:             coneNew[2+q]  = v + pMaxNew[dep] + numSplitPoints[dep];
924:           }
925:           coneONew[2+q] = 0;
926:         }
927:         DMPlexSetCone(sdm, hybcell, coneNew);
928:         DMPlexSetConeOrientation(sdm, hybcell, coneONew);
929:         /* Label the hybrid cells on the boundary of the split */
930:         if (hasUnsplit) {DMLabelSetValue(label, -hybcell, dim);}
931:       } else if (dep == 0) {
932:         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1];

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

938:           DMLabelGetValue(label, support[e], &val);
939:           if ((val == 1) || (val == (shift + 1))) {
940:             supportNew[qn++] = DMPlexShiftPoint_Internal(support[e], depth, depthMax, depthEnd, depthShift) /*support[e] + depthOffset[dep+1]*/;
941:           }
942:         }
943:         supportNew[qn] = hybedge;
944:         DMPlexSetSupport(sdm, newp, supportNew);
945:         /* Split new vertex: Edges in new split faces and new cohesive edge */
946:         for (e = 0, qp = 0; e < supportSize; ++e) {
947:           PetscInt val, edge;

949:           DMLabelGetValue(label, support[e], &val);
950:           if (val == 1) {
951:             PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);
952:             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
953:             supportNew[qp++] = edge + pMaxNew[dep+1];
954:           } else if (val == -(shift + 1)) {
955:             supportNew[qp++] = DMPlexShiftPoint_Internal(support[e], depth, depthMax, depthEnd, depthShift) /*support[e] + depthOffset[dep+1]*/;
956:           }
957:         }
958:         supportNew[qp] = hybedge;
959:         DMPlexSetSupport(sdm, splitp, supportNew);
960:         /* Hybrid edge:    Old and new split vertex */
961:         coneNew[0] = newp;
962:         coneNew[1] = splitp;
963:         DMPlexSetCone(sdm, hybedge, coneNew);
964:         for (e = 0, qf = 0; e < supportSize; ++e) {
965:           PetscInt val, edge;

967:           DMLabelGetValue(label, support[e], &val);
968:           if (val == 1) {
969:             PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);
970:             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
971:             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2];
972:           }
973:         }
974:         DMPlexSetSupport(sdm, hybedge, supportNew);
975:       } else if (dep == dim-2) {
976:         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];

978:         /* Split old edge:   old vertices in cone so no change */
979:         /* Split new edge:   new vertices in cone */
980:         for (q = 0; q < coneSize; ++q) {
981:           PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);
982:           if (v < 0) {
983:             PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);
984:             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);
985:             coneNew[q] = DMPlexShiftPoint_Internal(cone[q], depth, depthMax, depthEnd, depthShift) /*cone[q] + depthOffset[dep-1]*/;
986:           } else {
987:             coneNew[q] = v + pMaxNew[dep-1];
988:           }
989:         }
990:         DMPlexSetCone(sdm, splitp, coneNew);
991:         /* Split old edge: Faces in positive side cells and old split faces */
992:         for (e = 0, q = 0; e < supportSize; ++e) {
993:           PetscInt val;

995:           DMLabelGetValue(label, support[e], &val);
996:           if (val == dim-1) {
997:             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthMax, depthEnd, depthShift) /*support[e] + depthOffset[dep+1]*/;
998:           } else if (val == (shift + dim-1)) {
999:             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthMax, depthEnd, depthShift) /*support[e] + depthOffset[dep+1]*/;
1000:           }
1001:         }
1002:         supportNew[q++] = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1003:         DMPlexSetSupport(sdm, newp, supportNew);
1004:         /* Split new edge: Faces in negative side cells and new split faces */
1005:         for (e = 0, q = 0; e < supportSize; ++e) {
1006:           PetscInt val, face;

1008:           DMLabelGetValue(label, support[e], &val);
1009:           if (val == dim-1) {
1010:             PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &face);
1011:             if (face < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[e]);
1012:             supportNew[q++] = face + pMaxNew[dep+1];
1013:           } else if (val == -(shift + dim-1)) {
1014:             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthMax, depthEnd, depthShift) /*support[e] + depthOffset[dep+1]*/;
1015:           }
1016:         }
1017:         supportNew[q++] = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1018:         DMPlexSetSupport(sdm, splitp, supportNew);
1019:         /* Hybrid face */
1020:         coneNew[0] = newp;
1021:         coneNew[1] = splitp;
1022:         for (v = 0; v < coneSize; ++v) {
1023:           PetscInt vertex;
1024:           PetscFindInt(cone[v], numSplitPoints[dep-1], splitPoints[dep-1], &vertex);
1025:           if (vertex < 0) {
1026:             PetscFindInt(cone[v], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &vertex);
1027:             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);
1028:             coneNew[2+v] = vertex + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1029:           } else {
1030:             coneNew[2+v] = vertex + pMaxNew[dep] + numSplitPoints[dep];
1031:           }
1032:         }
1033:         DMPlexSetCone(sdm, hybface, coneNew);
1034:         for (e = 0, qf = 0; e < supportSize; ++e) {
1035:           PetscInt val, face;

1037:           DMLabelGetValue(label, support[e], &val);
1038:           if (val == dim-1) {
1039:             PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &face);
1040:             if (face < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[e]);
1041:             supportNew[qf++] = face + pMaxNew[dep+2] + numSplitPoints[dep+2];
1042:           }
1043:         }
1044:         DMPlexSetSupport(sdm, hybface, supportNew);
1045:       }
1046:     }
1047:   }
1048:   for (dep = 0; dep <= depth; ++dep) {
1049:     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
1050:       const PetscInt  oldp   = unsplitPoints[dep][p];
1051:       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthMax, depthEnd, depthShift) /*oldp + depthOffset[dep]*/;
1052:       const PetscInt *cone, *support, *ornt;
1053:       PetscInt        coneSize, supportSize, supportSizeNew, q, qf, e, f, s;

1055:       DMPlexGetConeSize(dm, oldp, &coneSize);
1056:       DMPlexGetCone(dm, oldp, &cone);
1057:       DMPlexGetConeOrientation(dm, oldp, &ornt);
1058:       DMPlexGetSupportSize(dm, oldp, &supportSize);
1059:       DMPlexGetSupport(dm, oldp, &support);
1060:       if (dep == 0) {
1061:         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];

1063:         /* Unsplit vertex */
1064:         DMPlexGetSupportSize(sdm, newp, &supportSizeNew);
1065:         for (s = 0, q = 0; s < supportSize; ++s) {
1066:           supportNew[q++] = DMPlexShiftPoint_Internal(support[s], depth, depthMax, depthEnd, depthShift) /*support[s] + depthOffset[dep+1]*/;
1067:           PetscFindInt(support[s], numSplitPoints[dep+1], splitPoints[dep+1], &e);
1068:           if (e >= 0) {
1069:             supportNew[q++] = e + pMaxNew[dep+1];
1070:           }
1071:         }
1072:         supportNew[q++] = hybedge;
1073:         supportNew[q++] = hybedge;
1074:         if (q != supportSizeNew) SETERRQ3(comm, PETSC_ERR_ARG_WRONG, "Support size %d != %d for vertex %d", q, supportSizeNew, newp);
1075:         DMPlexSetSupport(sdm, newp, supportNew);
1076:         /* Hybrid edge */
1077:         coneNew[0] = newp;
1078:         coneNew[1] = newp;
1079:         DMPlexSetCone(sdm, hybedge, coneNew);
1080:         for (e = 0, qf = 0; e < supportSize; ++e) {
1081:           PetscInt val, edge;

1083:           DMLabelGetValue(label, support[e], &val);
1084:           if (val == 1) {
1085:             PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);
1086:             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
1087:             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2];
1088:           } else if  (val ==  (shift2 + 1)) {
1089:             PetscFindInt(support[e], numUnsplitPoints[dep+1], unsplitPoints[dep+1], &edge);
1090:             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a unsplit edge", support[e]);
1091:             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2] + numSplitPoints[dep+1];
1092:           }
1093:         }
1094:         DMPlexSetSupport(sdm, hybedge, supportNew);
1095:       } else if (dep == dim-2) {
1096:         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];

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

1102:           DMLabelGetValue(label, support[f], &val);
1103:           if (val == dim-1) {
1104:             PetscFindInt(support[f], numSplitPoints[dep+1], splitPoints[dep+1], &face);
1105:             if (face < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[f]);
1106:             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthMax, depthEnd, depthShift) /*support[f] + depthOffset[dep+1]*/;
1107:             supportNew[qf++] = face + pMaxNew[dep+1];
1108:           } else {
1109:             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthMax, depthEnd, depthShift) /*support[f] + depthOffset[dep+1]*/;
1110:           }
1111:         }
1112:         supportNew[qf++] = hybface;
1113:         supportNew[qf++] = hybface;
1114:         DMPlexGetSupportSize(sdm, newp, &supportSizeNew);
1115:         if (qf != supportSizeNew) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Support size for unsplit edge %d is %d != %d\n", newp, qf, supportSizeNew);
1116:         DMPlexSetSupport(sdm, newp, supportNew);
1117:         /* Add hybrid face */
1118:         coneNew[0] = newp;
1119:         coneNew[1] = newp;
1120:         PetscFindInt(cone[0], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);
1121:         if (v < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Vertex %d is not an unsplit vertex", cone[0]);
1122:         coneNew[2] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1123:         PetscFindInt(cone[1], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);
1124:         if (v < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Vertex %d is not an unsplit vertex", cone[1]);
1125:         coneNew[3] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1126:         DMPlexSetCone(sdm, hybface, coneNew);
1127:         for (f = 0, qf = 0; f < supportSize; ++f) {
1128:           PetscInt val, face;

1130:           DMLabelGetValue(label, support[f], &val);
1131:           if (val == dim-1) {
1132:             PetscFindInt(support[f], numSplitPoints[dep+1], splitPoints[dep+1], &face);
1133:             supportNew[qf++] = face + pMaxNew[dep+2] + numSplitPoints[dep+2];
1134:           }
1135:         }
1136:         DMPlexGetSupportSize(sdm, hybface, &supportSizeNew);
1137:         if (qf != supportSizeNew) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Support size for hybrid face %d is %d != %d\n", hybface, qf, supportSizeNew);
1138:         DMPlexSetSupport(sdm, hybface, supportNew);
1139:       }
1140:     }
1141:   }
1142:   /* Step 6b: Replace split points in negative side cones */
1143:   for (sp = 0; sp < numSP; ++sp) {
1144:     PetscInt        dep = values[sp];
1145:     IS              pIS;
1146:     PetscInt        numPoints;
1147:     const PetscInt *points;

1149:     if (dep >= 0) continue;
1150:     DMLabelGetStratumIS(label, dep, &pIS);
1151:     if (!pIS) continue;
1152:     dep  = -dep - shift;
1153:     ISGetLocalSize(pIS, &numPoints);
1154:     ISGetIndices(pIS, &points);
1155:     for (p = 0; p < numPoints; ++p) {
1156:       const PetscInt  oldp = points[p];
1157:       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthMax, depthEnd, depthShift) /*depthOffset[dep] + oldp*/;
1158:       const PetscInt *cone;
1159:       PetscInt        coneSize, c;
1160:       /* PetscBool       replaced = PETSC_FALSE; */

1162:       /* Negative edge: replace split vertex */
1163:       /* Negative cell: replace split face */
1164:       DMPlexGetConeSize(sdm, newp, &coneSize);
1165:       DMPlexGetCone(sdm, newp, &cone);
1166:       for (c = 0; c < coneSize; ++c) {
1167:         const PetscInt coldp = cone[c] - depthOffset[dep-1];
1168:         PetscInt       csplitp, cp, val;

1170:         DMLabelGetValue(label, coldp, &val);
1171:         if (val == dep-1) {
1172:           PetscFindInt(coldp, numSplitPoints[dep-1], splitPoints[dep-1], &cp);
1173:           if (cp < 0) SETERRQ2(comm, PETSC_ERR_ARG_WRONG, "Point %d is not a split point of dimension %d", oldp, dep-1);
1174:           csplitp  = pMaxNew[dep-1] + cp;
1175:           DMPlexInsertCone(sdm, newp, c, csplitp);
1176:           /* replaced = PETSC_TRUE; */
1177:         }
1178:       }
1179:       /* Cells with only a vertex or edge on the submesh have no replacement */
1180:       /* if (!replaced) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "The cone of point %d does not contain split points", oldp); */
1181:     }
1182:     ISRestoreIndices(pIS, &points);
1183:     ISDestroy(&pIS);
1184:   }
1185:   /* Step 7: Stratify */
1186:   DMPlexStratify(sdm);
1187:   /* Step 8: Coordinates */
1188:   DMPlexShiftCoordinates_Internal(dm, depthShift, sdm);
1189:   DMGetCoordinateSection(sdm, &coordSection);
1190:   DMGetCoordinatesLocal(sdm, &coordinates);
1191:   VecGetArray(coordinates, &coords);
1192:   for (v = 0; v < (numSplitPoints ? numSplitPoints[0] : 0); ++v) {
1193:     const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[0][v], depth, depthMax, depthEnd, depthShift) /*depthOffset[0] + splitPoints[0][v]*/;
1194:     const PetscInt splitp = pMaxNew[0] + v;
1195:     PetscInt       dof, off, soff, d;

1197:     PetscSectionGetDof(coordSection, newp, &dof);
1198:     PetscSectionGetOffset(coordSection, newp, &off);
1199:     PetscSectionGetOffset(coordSection, splitp, &soff);
1200:     for (d = 0; d < dof; ++d) coords[soff+d] = coords[off+d];
1201:   }
1202:   VecRestoreArray(coordinates, &coords);
1203:   /* Step 9: SF, if I can figure this out we can split the mesh in parallel */
1204:   DMPlexShiftSF_Internal(dm, depthShift, sdm);
1205:   /* Step 10: Labels */
1206:   DMPlexShiftLabels_Internal(dm, depthShift, sdm);
1207:   DMPlexGetNumLabels(sdm, &numLabels);
1208:   for (dep = 0; dep <= depth; ++dep) {
1209:     for (p = 0; p < numSplitPoints[dep]; ++p) {
1210:       const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[dep][p], depth, depthMax, depthEnd, depthShift) /*depthOffset[dep] + splitPoints[dep][p]*/;
1211:       const PetscInt splitp = pMaxNew[dep] + p;
1212:       PetscInt       l;

1214:       for (l = 0; l < numLabels; ++l) {
1215:         DMLabel     mlabel;
1216:         const char *lname;
1217:         PetscInt    val;
1218:         PetscBool   isDepth;

1220:         DMPlexGetLabelName(sdm, l, &lname);
1221:         PetscStrcmp(lname, "depth", &isDepth);
1222:         if (isDepth) continue;
1223:         DMPlexGetLabel(sdm, lname, &mlabel);
1224:         DMLabelGetValue(mlabel, newp, &val);
1225:         if (val >= 0) {
1226:           DMLabelSetValue(mlabel, splitp, val);
1227: #if 0
1228:           /* Do not put cohesive edges into the label */
1229:           if (dep == 0) {
1230:             const PetscInt cedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1231:             DMLabelSetValue(mlabel, cedge, val);
1232:           } else if (dep == dim-2) {
1233:             const PetscInt cface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1234:             DMLabelSetValue(mlabel, cface, val);
1235:           }
1236:           /* Do not put cohesive faces into the label */
1237: #endif
1238:         }
1239:       }
1240:     }
1241:   }
1242:   for (sp = 0; sp < numSP; ++sp) {
1243:     const PetscInt dep = values[sp];

1245:     if ((dep < 0) || (dep > depth)) continue;
1246:     if (splitIS[dep]) {ISRestoreIndices(splitIS[dep], &splitPoints[dep]);}
1247:     ISDestroy(&splitIS[dep]);
1248:     if (unsplitIS[dep]) {ISRestoreIndices(unsplitIS[dep], &unsplitPoints[dep]);}
1249:     ISDestroy(&unsplitIS[dep]);
1250:   }
1251:   if (label) {
1252:     ISRestoreIndices(valueIS, &values);
1253:     ISDestroy(&valueIS);
1254:   }
1255:   for (d = 0; d <= depth; ++d) {
1256:     DMPlexGetDepthStratum(sdm, d, NULL, &pEnd);
1257:     pMaxNew[d] = pEnd - numHybridPoints[d] - numHybridPointsOld[d];
1258:   }
1259:   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);
1260:   PetscFree3(coneNew, coneONew, supportNew);
1261:   PetscFree6(depthMax, depthEnd, depthShift, depthOffset, pMaxNew, numHybridPointsOld);
1262:   PetscFree7(splitIS, unsplitIS, numSplitPoints, numUnsplitPoints, numHybridPoints, splitPoints, unsplitPoints);
1263:   return(0);
1264: }

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

1271:   Collective on dm

1273:   Input Parameters:
1274: + dm - The original DM
1275: - label - The label specifying the boundary faces (this could be auto-generated)

1277:   Output Parameters:
1278: - dmSplit - The new DM

1280:   Level: developer

1282: .seealso: DMCreate(), DMPlexLabelCohesiveComplete()
1283: @*/
1284: PetscErrorCode DMPlexConstructCohesiveCells(DM dm, DMLabel label, DM *dmSplit)
1285: {
1286:   DM             sdm;
1287:   PetscInt       dim;

1293:   DMCreate(PetscObjectComm((PetscObject)dm), &sdm);
1294:   DMSetType(sdm, DMPLEX);
1295:   DMGetDimension(dm, &dim);
1296:   DMSetDimension(sdm, dim);
1297:   switch (dim) {
1298:   case 2:
1299:   case 3:
1300:     DMPlexConstructCohesiveCells_Internal(dm, label, sdm);
1301:     break;
1302:   default:
1303:     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct cohesive cells for dimension %d", dim);
1304:   }
1305:   *dmSplit = sdm;
1306:   return(0);
1307: }

1311: /* Returns the side of the surface for a given cell with a face on the surface */
1312: static PetscErrorCode GetSurfaceSide_Static(DM dm, DM subdm, PetscInt numSubpoints, const PetscInt *subpoints, PetscInt cell, PetscInt face, PetscBool *pos)
1313: {
1314:   const PetscInt *cone, *ornt;
1315:   PetscInt        dim, coneSize, c;
1316:   PetscErrorCode  ierr;

1319:   *pos = PETSC_TRUE;
1320:   DMGetDimension(dm, &dim);
1321:   DMPlexGetConeSize(dm, cell, &coneSize);
1322:   DMPlexGetCone(dm, cell, &cone);
1323:   DMPlexGetConeOrientation(dm, cell, &ornt);
1324:   for (c = 0; c < coneSize; ++c) {
1325:     if (cone[c] == face) {
1326:       PetscInt o = ornt[c];

1328:       if (subdm) {
1329:         const PetscInt *subcone, *subornt;
1330:         PetscInt        subpoint, subface, subconeSize, sc;

1332:         PetscFindInt(cell, numSubpoints, subpoints, &subpoint);
1333:         PetscFindInt(face, numSubpoints, subpoints, &subface);
1334:         DMPlexGetConeSize(subdm, subpoint, &subconeSize);
1335:         DMPlexGetCone(subdm, subpoint, &subcone);
1336:         DMPlexGetConeOrientation(subdm, subpoint, &subornt);
1337:         for (sc = 0; sc < subconeSize; ++sc) {
1338:           if (subcone[sc] == subface) {
1339:             o = subornt[0];
1340:             break;
1341:           }
1342:         }
1343:         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);
1344:       }
1345:       if (o >= 0) *pos = PETSC_TRUE;
1346:       else        *pos = PETSC_FALSE;
1347:       break;
1348:     }
1349:   }
1350:   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);
1351:   return(0);
1352: }

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

1360:   Input Parameters:
1361: + dm     - The DM
1362: . label  - A DMLabel marking the surface
1363: . blabel - A DMLabel marking the vertices on the boundary which will not be duplicated, or NULL to find them automatically
1364: . flip   - Flag to flip the submesh normal and replace points on the other side
1365: - subdm  - The subDM associated with the label, or NULL

1367:   Output Parameter:
1368: . label - A DMLabel marking all surface points

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

1372:   Level: developer

1374: .seealso: DMPlexConstructCohesiveCells(), DMPlexLabelComplete()
1375: @*/
1376: PetscErrorCode DMPlexLabelCohesiveComplete(DM dm, DMLabel label, DMLabel blabel, PetscBool flip, DM subdm)
1377: {
1378:   DMLabel         depthLabel;
1379:   IS              dimIS, subpointIS, facePosIS, faceNegIS, crossEdgeIS = NULL;
1380:   const PetscInt *points, *subpoints;
1381:   const PetscInt  rev   = flip ? -1 : 1;
1382:   PetscInt        shift = 100, shift2 = 200, dim, dep, cStart, cEnd, fStart, fEnd, vStart, vEnd, numPoints, numSubpoints, p, val;
1383:   PetscErrorCode  ierr;

1386:   DMPlexGetDepthLabel(dm, &depthLabel);
1387:   DMGetDimension(dm, &dim);
1388:   if (subdm) {
1389:     DMPlexCreateSubpointIS(subdm, &subpointIS);
1390:     if (subpointIS) {
1391:       ISGetLocalSize(subpointIS, &numSubpoints);
1392:       ISGetIndices(subpointIS, &subpoints);
1393:     }
1394:   }
1395:   /* Mark cell on the fault, and its faces which touch the fault: cell orientation for face gives the side of the fault */
1396:   DMLabelGetStratumIS(label, dim-1, &dimIS);
1397:   if (!dimIS) return(0);
1398:   ISGetLocalSize(dimIS, &numPoints);
1399:   ISGetIndices(dimIS, &points);
1400:   for (p = 0; p < numPoints; ++p) { /* Loop over fault faces */
1401:     const PetscInt *support;
1402:     PetscInt        supportSize, s;

1404:     DMPlexGetSupportSize(dm, points[p], &supportSize);
1405:     if (supportSize != 2) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Split face %d has %d != 2 supports", points[p], supportSize);
1406:     DMPlexGetSupport(dm, points[p], &support);
1407:     for (s = 0; s < supportSize; ++s) {
1408:       const PetscInt *cone;
1409:       PetscInt        coneSize, c;
1410:       PetscBool       pos;

1412:       GetSurfaceSide_Static(dm, subdm, numSubpoints, subpoints, support[s], points[p], &pos);
1413:       if (pos) {DMLabelSetValue(label, support[s],  rev*(shift+dim));}
1414:       else     {DMLabelSetValue(label, support[s], -rev*(shift+dim));}
1415:       if (rev < 0) pos = !pos ? PETSC_TRUE : PETSC_FALSE;
1416:       /* Put faces touching the fault in the label */
1417:       DMPlexGetConeSize(dm, support[s], &coneSize);
1418:       DMPlexGetCone(dm, support[s], &cone);
1419:       for (c = 0; c < coneSize; ++c) {
1420:         const PetscInt point = cone[c];

1422:         DMLabelGetValue(label, point, &val);
1423:         if (val == -1) {
1424:           PetscInt *closure = NULL;
1425:           PetscInt  closureSize, cl;

1427:           DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);
1428:           for (cl = 0; cl < closureSize*2; cl += 2) {
1429:             const PetscInt clp  = closure[cl];
1430:             PetscInt       bval = -1;

1432:             DMLabelGetValue(label, clp, &val);
1433:             if (blabel) {DMLabelGetValue(blabel, clp, &bval);}
1434:             if ((val >= 0) && (val < dim-1) && (bval < 0)) {
1435:               DMLabelSetValue(label, point, pos == PETSC_TRUE ? shift+dim-1 : -(shift+dim-1));
1436:               break;
1437:             }
1438:           }
1439:           DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);
1440:         }
1441:       }
1442:     }
1443:   }
1444:   ISRestoreIndices(dimIS, &points);
1445:   ISDestroy(&dimIS);
1446:   if (subdm) {
1447:     if (subpointIS) {ISRestoreIndices(subpointIS, &subpoints);}
1448:     ISDestroy(&subpointIS);
1449:   }
1450:   /* Mark boundary points as unsplit */
1451:   if (blabel) {
1452:     DMLabelGetStratumIS(blabel, 1, &dimIS);
1453:     ISGetLocalSize(dimIS, &numPoints);
1454:     ISGetIndices(dimIS, &points);
1455:     for (p = 0; p < numPoints; ++p) {
1456:       const PetscInt point = points[p];
1457:       PetscInt       val, bval;

1459:       DMLabelGetValue(blabel, point, &bval);
1460:       if (bval >= 0) {
1461:         DMLabelGetValue(label, point, &val);
1462:         if ((val < 0) || (val > dim)) {
1463:           /* This could be a point added from splitting a vertex on an adjacent fault, otherwise its just wrong */
1464:           DMLabelClearValue(blabel, point, bval);
1465:         }
1466:       }
1467:     }
1468:     for (p = 0; p < numPoints; ++p) {
1469:       const PetscInt point = points[p];
1470:       PetscInt       val, bval;

1472:       DMLabelGetValue(blabel, point, &bval);
1473:       if (bval >= 0) {
1474:         const PetscInt *cone,    *support;
1475:         PetscInt        coneSize, supportSize, s, valA, valB, valE;

1477:         /* Mark as unsplit */
1478:         DMLabelGetValue(label, point, &val);
1479:         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);
1480:         DMLabelClearValue(label, point, val);
1481:         DMLabelSetValue(label, point, shift2+val);
1482:         /* Check for cross-edge */
1483:         if (val != 0) continue;
1484:         DMPlexGetSupport(dm, point, &support);
1485:         DMPlexGetSupportSize(dm, point, &supportSize);
1486:         for (s = 0; s < supportSize; ++s) {
1487:           DMPlexGetCone(dm, support[s], &cone);
1488:           DMPlexGetConeSize(dm, support[s], &coneSize);
1489:           if (coneSize != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Edge %D has %D vertices != 2", support[s], coneSize);
1490:           DMLabelGetValue(blabel, cone[0], &valA);
1491:           DMLabelGetValue(blabel, cone[1], &valB);
1492:           DMLabelGetValue(blabel, support[s], &valE);
1493:           if ((valE < 0) && (valA >= 0) && (valB >= 0)) {DMLabelSetValue(blabel, support[s], 2);}
1494:         }
1495:       }
1496:     }
1497:     ISRestoreIndices(dimIS, &points);
1498:     ISDestroy(&dimIS);
1499:   }
1500:   /* Search for other cells/faces/edges connected to the fault by a vertex */
1501:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
1502:   DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);
1503:   DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);
1504:   DMLabelGetStratumIS(label, 0, &dimIS);
1505:   if (blabel) {DMLabelGetStratumIS(blabel, 2, &crossEdgeIS);}
1506:   if (dimIS && crossEdgeIS) {
1507:     IS vertIS = dimIS;

1509:     ISExpand(vertIS, crossEdgeIS, &dimIS);
1510:     ISDestroy(&crossEdgeIS);
1511:     ISDestroy(&vertIS);
1512:   }
1513:   if (!dimIS) return(0);
1514:   ISGetLocalSize(dimIS, &numPoints);
1515:   ISGetIndices(dimIS, &points);
1516:   for (p = 0; p < numPoints; ++p) { /* Loop over fault vertices */
1517:     PetscInt *star = NULL;
1518:     PetscInt  starSize, s;
1519:     PetscInt  again = 1;  /* 0: Finished 1: Keep iterating after a change 2: No change */

1521:     /* All points connected to the fault are inside a cell, so at the top level we will only check cells */
1522:     DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &starSize, &star);
1523:     while (again) {
1524:       if (again > 1) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Could not classify all cells connected to the fault");
1525:       again = 0;
1526:       for (s = 0; s < starSize*2; s += 2) {
1527:         const PetscInt  point = star[s];
1528:         const PetscInt *cone;
1529:         PetscInt        coneSize, c;

1531:         if ((point < cStart) || (point >= cEnd)) continue;
1532:         DMLabelGetValue(label, point, &val);
1533:         if (val != -1) continue;
1534:         again = again == 1 ? 1 : 2;
1535:         DMPlexGetConeSize(dm, point, &coneSize);
1536:         DMPlexGetCone(dm, point, &cone);
1537:         for (c = 0; c < coneSize; ++c) {
1538:           DMLabelGetValue(label, cone[c], &val);
1539:           if (val != -1) {
1540:             const PetscInt *ccone;
1541:             PetscInt        cconeSize, cc, side;

1543:             if (abs(val) < shift) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %d on cell %d has an invalid label %d", cone[c], point, val);
1544:             if (val > 0) side =  1;
1545:             else         side = -1;
1546:             DMLabelSetValue(label, point, side*(shift+dim));
1547:             /* Mark cell faces which touch the fault */
1548:             DMPlexGetConeSize(dm, point, &cconeSize);
1549:             DMPlexGetCone(dm, point, &ccone);
1550:             for (cc = 0; cc < cconeSize; ++cc) {
1551:               PetscInt *closure = NULL;
1552:               PetscInt  closureSize, cl;

1554:               DMLabelGetValue(label, ccone[cc], &val);
1555:               if (val != -1) continue;
1556:               DMPlexGetTransitiveClosure(dm, ccone[cc], PETSC_TRUE, &closureSize, &closure);
1557:               for (cl = 0; cl < closureSize*2; cl += 2) {
1558:                 const PetscInt clp = closure[cl];

1560:                 DMLabelGetValue(label, clp, &val);
1561:                 if (val == -1) continue;
1562:                 DMLabelSetValue(label, ccone[cc], side*(shift+dim-1));
1563:                 break;
1564:               }
1565:               DMPlexRestoreTransitiveClosure(dm, ccone[cc], PETSC_TRUE, &closureSize, &closure);
1566:             }
1567:             again = 1;
1568:             break;
1569:           }
1570:         }
1571:       }
1572:     }
1573:     /* Classify the rest by cell membership */
1574:     for (s = 0; s < starSize*2; s += 2) {
1575:       const PetscInt point = star[s];

1577:       DMLabelGetValue(label, point, &val);
1578:       if (val == -1) {
1579:         PetscInt  *sstar = NULL;
1580:         PetscInt   sstarSize, ss;
1581:         PetscBool  marked = PETSC_FALSE;

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

1587:           if ((spoint < cStart) || (spoint >= cEnd)) continue;
1588:           DMLabelGetValue(label, spoint, &val);
1589:           if (val == -1) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %d in star of %d does not have a valid label", spoint, point);
1590:           DMLabelGetValue(depthLabel, point, &dep);
1591:           if (val > 0) {
1592:             DMLabelSetValue(label, point,   shift+dep);
1593:           } else {
1594:             DMLabelSetValue(label, point, -(shift+dep));
1595:           }
1596:           marked = PETSC_TRUE;
1597:           break;
1598:         }
1599:         DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &sstarSize, &sstar);
1600:         if (!marked) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Point %d could not be classified", point);
1601:       }
1602:     }
1603:     DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &starSize, &star);
1604:   }
1605:   ISRestoreIndices(dimIS, &points);
1606:   ISDestroy(&dimIS);
1607:   /* If any faces touching the fault divide cells on either side, split them */
1608:   DMLabelGetStratumIS(label,   shift+dim-1,  &facePosIS);
1609:   DMLabelGetStratumIS(label, -(shift+dim-1), &faceNegIS);
1610:   ISExpand(facePosIS, faceNegIS, &dimIS);
1611:   ISDestroy(&facePosIS);
1612:   ISDestroy(&faceNegIS);
1613:   ISGetLocalSize(dimIS, &numPoints);
1614:   ISGetIndices(dimIS, &points);
1615:   for (p = 0; p < numPoints; ++p) {
1616:     const PetscInt  point = points[p];
1617:     const PetscInt *support;
1618:     PetscInt        supportSize, valA, valB;

1620:     DMPlexGetSupportSize(dm, point, &supportSize);
1621:     if (supportSize != 2) continue;
1622:     DMPlexGetSupport(dm, point, &support);
1623:     DMLabelGetValue(label, support[0], &valA);
1624:     DMLabelGetValue(label, support[1], &valB);
1625:     if ((valA == -1) || (valB == -1)) continue;
1626:     if (valA*valB > 0) continue;
1627:     /* Split the face */
1628:     DMLabelGetValue(label, point, &valA);
1629:     DMLabelClearValue(label, point, valA);
1630:     DMLabelSetValue(label, point, dim-1);
1631:     /* Label its closure:
1632:       unmarked: label as unsplit
1633:       incident: relabel as split
1634:       split:    do nothing
1635:     */
1636:     {
1637:       PetscInt *closure = NULL;
1638:       PetscInt  closureSize, cl;

1640:       DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);
1641:       for (cl = 0; cl < closureSize*2; cl += 2) {
1642:         DMLabelGetValue(label, closure[cl], &valA);
1643:         if (valA == -1) { /* Mark as unsplit */
1644:           DMLabelGetValue(depthLabel, closure[cl], &dep);
1645:           DMLabelSetValue(label, closure[cl], shift2+dep);
1646:         } else if (((valA >= shift) && (valA < shift2)) || ((valA <= -shift) && (valA > -shift2))) {
1647:           DMLabelGetValue(depthLabel, closure[cl], &dep);
1648:           DMLabelClearValue(label, closure[cl], valA);
1649:           DMLabelSetValue(label, closure[cl], dep);
1650:         }
1651:       }
1652:       DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);
1653:     }
1654:   }
1655:   ISRestoreIndices(dimIS, &points);
1656:   ISDestroy(&dimIS);
1657:   return(0);
1658: }

1662: /*@C
1663:   DMPlexCreateHybridMesh - Create a mesh with hybrid cells along an internal interface

1665:   Collective on dm

1667:   Input Parameters:
1668: + dm - The original DM
1669: - labelName - The label specifying the interface vertices

1671:   Output Parameters:
1672: + hybridLabel - The label fully marking the interface
1673: - dmHybrid - The new DM

1675:   Level: developer

1677: .seealso: DMPlexConstructCohesiveCells(), DMPlexLabelCohesiveComplete(), DMCreate()
1678: @*/
1679: PetscErrorCode DMPlexCreateHybridMesh(DM dm, DMLabel label, DMLabel *hybridLabel, DM *dmHybrid)
1680: {
1681:   DM             idm;
1682:   DMLabel        subpointMap, hlabel;
1683:   PetscInt       dim;

1690:   DMGetDimension(dm, &dim);
1691:   DMPlexCreateSubmesh(dm, label, 1, &idm);
1692:   DMPlexOrient(idm);
1693:   DMPlexGetSubpointMap(idm, &subpointMap);
1694:   DMLabelDuplicate(subpointMap, &hlabel);
1695:   DMLabelClearStratum(hlabel, dim);
1696:   DMPlexLabelCohesiveComplete(dm, hlabel, NULL, PETSC_FALSE, idm);
1697:   DMDestroy(&idm);
1698:   DMPlexConstructCohesiveCells(dm, hlabel, dmHybrid);
1699:   if (hybridLabel) *hybridLabel = hlabel;
1700:   else             {DMLabelDestroy(&hlabel);}
1701:   return(0);
1702: }

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

1708:      For any marked cell, the marked vertices constitute a single face
1709: */
1710: static PetscErrorCode DMPlexMarkSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, DM subdm)
1711: {
1712:   IS               subvertexIS = NULL;
1713:   const PetscInt  *subvertices;
1714:   PetscInt        *pStart, *pEnd, *pMax, pSize;
1715:   PetscInt         depth, dim, d, numSubVerticesInitial = 0, v;
1716:   PetscErrorCode   ierr;

1719:   *numFaces = 0;
1720:   *nFV      = 0;
1721:   DMPlexGetDepth(dm, &depth);
1722:   DMGetDimension(dm, &dim);
1723:   pSize = PetscMax(depth, dim) + 1;
1724:   PetscMalloc3(pSize,&pStart,pSize,&pEnd,pSize,&pMax);
1725:   DMPlexGetHybridBounds(dm, depth >= 0 ? &pMax[depth] : NULL, depth>1 ? &pMax[depth-1] : NULL, depth>2 ? &pMax[1] : NULL, &pMax[0]);
1726:   for (d = 0; d <= depth; ++d) {
1727:     DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);
1728:     if (pMax[d] >= 0) pEnd[d] = PetscMin(pEnd[d], pMax[d]);
1729:   }
1730:   /* Loop over initial vertices and mark all faces in the collective star() */
1731:   if (vertexLabel) {DMLabelGetStratumIS(vertexLabel, value, &subvertexIS);}
1732:   if (subvertexIS) {
1733:     ISGetSize(subvertexIS, &numSubVerticesInitial);
1734:     ISGetIndices(subvertexIS, &subvertices);
1735:   }
1736:   for (v = 0; v < numSubVerticesInitial; ++v) {
1737:     const PetscInt vertex = subvertices[v];
1738:     PetscInt      *star   = NULL;
1739:     PetscInt       starSize, s, numCells = 0, c;

1741:     DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);
1742:     for (s = 0; s < starSize*2; s += 2) {
1743:       const PetscInt point = star[s];
1744:       if ((point >= pStart[depth]) && (point < pEnd[depth])) star[numCells++] = point;
1745:     }
1746:     for (c = 0; c < numCells; ++c) {
1747:       const PetscInt cell    = star[c];
1748:       PetscInt      *closure = NULL;
1749:       PetscInt       closureSize, cl;
1750:       PetscInt       cellLoc, numCorners = 0, faceSize = 0;

1752:       DMLabelGetValue(subpointMap, cell, &cellLoc);
1753:       if (cellLoc == 2) continue;
1754:       if (cellLoc >= 0) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %d has dimension %d in the surface label", cell, cellLoc);
1755:       DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);
1756:       for (cl = 0; cl < closureSize*2; cl += 2) {
1757:         const PetscInt point = closure[cl];
1758:         PetscInt       vertexLoc;

1760:         if ((point >= pStart[0]) && (point < pEnd[0])) {
1761:           ++numCorners;
1762:           DMLabelGetValue(vertexLabel, point, &vertexLoc);
1763:           if (vertexLoc == value) closure[faceSize++] = point;
1764:         }
1765:       }
1766:       if (!(*nFV)) {DMPlexGetNumFaceVertices(dm, dim, numCorners, nFV);}
1767:       if (faceSize > *nFV) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %d of an element on the surface", faceSize);
1768:       if (faceSize == *nFV) {
1769:         const PetscInt *cells = NULL;
1770:         PetscInt        numCells, nc;

1772:         ++(*numFaces);
1773:         for (cl = 0; cl < faceSize; ++cl) {
1774:           DMLabelSetValue(subpointMap, closure[cl], 0);
1775:         }
1776:         DMPlexGetJoin(dm, faceSize, closure, &numCells, &cells);
1777:         for (nc = 0; nc < numCells; ++nc) {
1778:           DMLabelSetValue(subpointMap, cells[nc], 2);
1779:         }
1780:         DMPlexRestoreJoin(dm, faceSize, closure, &numCells, &cells);
1781:       }
1782:       DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);
1783:     }
1784:     DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);
1785:   }
1786:   if (subvertexIS) {
1787:     ISRestoreIndices(subvertexIS, &subvertices);
1788:   }
1789:   ISDestroy(&subvertexIS);
1790:   PetscFree3(pStart,pEnd,pMax);
1791:   return(0);
1792: }

1796: static PetscErrorCode DMPlexMarkSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, DM subdm)
1797: {
1798:   IS               subvertexIS;
1799:   const PetscInt  *subvertices;
1800:   PetscInt        *pStart, *pEnd, *pMax;
1801:   PetscInt         dim, d, numSubVerticesInitial = 0, v;
1802:   PetscErrorCode   ierr;

1805:   DMGetDimension(dm, &dim);
1806:   PetscMalloc3(dim+1,&pStart,dim+1,&pEnd,dim+1,&pMax);
1807:   DMPlexGetHybridBounds(dm, &pMax[dim], dim>1 ? &pMax[dim-1] : NULL, dim > 2 ? &pMax[1] : NULL, &pMax[0]);
1808:   for (d = 0; d <= dim; ++d) {
1809:     DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);
1810:     if (pMax[d] >= 0) pEnd[d] = PetscMin(pEnd[d], pMax[d]);
1811:   }
1812:   /* Loop over initial vertices and mark all faces in the collective star() */
1813:   DMLabelGetStratumIS(vertexLabel, value, &subvertexIS);
1814:   if (subvertexIS) {
1815:     ISGetSize(subvertexIS, &numSubVerticesInitial);
1816:     ISGetIndices(subvertexIS, &subvertices);
1817:   }
1818:   for (v = 0; v < numSubVerticesInitial; ++v) {
1819:     const PetscInt vertex = subvertices[v];
1820:     PetscInt      *star   = NULL;
1821:     PetscInt       starSize, s, numFaces = 0, f;

1823:     DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);
1824:     for (s = 0; s < starSize*2; s += 2) {
1825:       const PetscInt point = star[s];
1826:       if ((point >= pStart[dim-1]) && (point < pEnd[dim-1])) star[numFaces++] = point;
1827:     }
1828:     for (f = 0; f < numFaces; ++f) {
1829:       const PetscInt face    = star[f];
1830:       PetscInt      *closure = NULL;
1831:       PetscInt       closureSize, c;
1832:       PetscInt       faceLoc;

1834:       DMLabelGetValue(subpointMap, face, &faceLoc);
1835:       if (faceLoc == dim-1) continue;
1836:       if (faceLoc >= 0) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %d has dimension %d in the surface label", face, faceLoc);
1837:       DMPlexGetTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure);
1838:       for (c = 0; c < closureSize*2; c += 2) {
1839:         const PetscInt point = closure[c];
1840:         PetscInt       vertexLoc;

1842:         if ((point >= pStart[0]) && (point < pEnd[0])) {
1843:           DMLabelGetValue(vertexLabel, point, &vertexLoc);
1844:           if (vertexLoc != value) break;
1845:         }
1846:       }
1847:       if (c == closureSize*2) {
1848:         const PetscInt *support;
1849:         PetscInt        supportSize, s;

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

1854:           for (d = 0; d < dim; ++d) {
1855:             if ((point >= pStart[d]) && (point < pEnd[d])) {
1856:               DMLabelSetValue(subpointMap, point, d);
1857:               break;
1858:             }
1859:           }
1860:         }
1861:         DMPlexGetSupportSize(dm, face, &supportSize);
1862:         DMPlexGetSupport(dm, face, &support);
1863:         for (s = 0; s < supportSize; ++s) {
1864:           DMLabelSetValue(subpointMap, support[s], dim);
1865:         }
1866:       }
1867:       DMPlexRestoreTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure);
1868:     }
1869:     DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);
1870:   }
1871:   if (subvertexIS) {
1872:     ISRestoreIndices(subvertexIS, &subvertices);
1873:   }
1874:   ISDestroy(&subvertexIS);
1875:   PetscFree3(pStart,pEnd,pMax);
1876:   return(0);
1877: }

1881: static PetscErrorCode DMPlexMarkCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char labelname[], PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, PetscInt *subCells[], DM subdm)
1882: {
1883:   DMLabel         label = NULL;
1884:   const PetscInt *cone;
1885:   PetscInt        dim, cMax, cEnd, c, subc = 0, p, coneSize;
1886:   PetscErrorCode  ierr;

1889:   *numFaces = 0;
1890:   *nFV = 0;
1891:   if (labelname) {DMPlexGetLabel(dm, labelname, &label);}
1892:   *subCells = NULL;
1893:   DMGetDimension(dm, &dim);
1894:   DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);
1895:   DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);
1896:   if (cMax < 0) return(0);
1897:   if (label) {
1898:     for (c = cMax; c < cEnd; ++c) {
1899:       PetscInt val;

1901:       DMLabelGetValue(label, c, &val);
1902:       if (val == value) {
1903:         ++(*numFaces);
1904:         DMPlexGetConeSize(dm, c, &coneSize);
1905:       }
1906:     }
1907:   } else {
1908:     *numFaces = cEnd - cMax;
1909:     DMPlexGetConeSize(dm, cMax, &coneSize);
1910:   }
1911:   *nFV = hasLagrange ? coneSize/3 : coneSize/2;
1912:   PetscMalloc1(*numFaces *2, subCells);
1913:   for (c = cMax; c < cEnd; ++c) {
1914:     const PetscInt *cells;
1915:     PetscInt        numCells;

1917:     if (label) {
1918:       PetscInt val;

1920:       DMLabelGetValue(label, c, &val);
1921:       if (val != value) continue;
1922:     }
1923:     DMPlexGetCone(dm, c, &cone);
1924:     for (p = 0; p < *nFV; ++p) {
1925:       DMLabelSetValue(subpointMap, cone[p], 0);
1926:     }
1927:     /* Negative face */
1928:     DMPlexGetJoin(dm, *nFV, cone, &numCells, &cells);
1929:     /* Not true in parallel
1930:     if (numCells != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
1931:     for (p = 0; p < numCells; ++p) {
1932:       DMLabelSetValue(subpointMap, cells[p], 2);
1933:       (*subCells)[subc++] = cells[p];
1934:     }
1935:     DMPlexRestoreJoin(dm, *nFV, cone, &numCells, &cells);
1936:     /* Positive face is not included */
1937:   }
1938:   return(0);
1939: }

1943: static PetscErrorCode DMPlexMarkCohesiveSubmesh_Interpolated(DM dm, const char labelname[], PetscInt value, DMLabel subpointMap, DM subdm)
1944: {
1945:   DMLabel        label = NULL;
1946:   PetscInt      *pStart, *pEnd;
1947:   PetscInt       dim, cMax, cEnd, c, d;

1951:   if (labelname) {DMPlexGetLabel(dm, labelname, &label);}
1952:   DMGetDimension(dm, &dim);
1953:   DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);
1954:   DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);
1955:   if (cMax < 0) return(0);
1956:   PetscMalloc2(dim+1,&pStart,dim+1,&pEnd);
1957:   for (d = 0; d <= dim; ++d) {DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);}
1958:   for (c = cMax; c < cEnd; ++c) {
1959:     const PetscInt *cone;
1960:     PetscInt       *closure = NULL;
1961:     PetscInt        fconeSize, coneSize, closureSize, cl, val;

1963:     if (label) {
1964:       DMLabelGetValue(label, c, &val);
1965:       if (val != value) continue;
1966:     }
1967:     DMPlexGetConeSize(dm, c, &coneSize);
1968:     DMPlexGetCone(dm, c, &cone);
1969:     DMPlexGetConeSize(dm, cone[0], &fconeSize);
1970:     if (coneSize != (fconeSize ? fconeSize : 1) + 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells");
1971:     /* Negative face */
1972:     DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure);
1973:     for (cl = 0; cl < closureSize*2; cl += 2) {
1974:       const PetscInt point = closure[cl];

1976:       for (d = 0; d <= dim; ++d) {
1977:         if ((point >= pStart[d]) && (point < pEnd[d])) {
1978:           DMLabelSetValue(subpointMap, point, d);
1979:           break;
1980:         }
1981:       }
1982:     }
1983:     DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure);
1984:     /* Cells -- positive face is not included */
1985:     for (cl = 0; cl < 1; ++cl) {
1986:       const PetscInt *support;
1987:       PetscInt        supportSize, s;

1989:       DMPlexGetSupportSize(dm, cone[cl], &supportSize);
1990:       /* if (supportSize != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive faces should separate two cells"); */
1991:       DMPlexGetSupport(dm, cone[cl], &support);
1992:       for (s = 0; s < supportSize; ++s) {
1993:         DMLabelSetValue(subpointMap, support[s], dim);
1994:       }
1995:     }
1996:   }
1997:   PetscFree2(pStart, pEnd);
1998:   return(0);
1999: }

2003: PetscErrorCode DMPlexGetFaceOrientation(DM dm, PetscInt cell, PetscInt numCorners, PetscInt indices[], PetscInt oppositeVertex, PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2004: {
2005:   MPI_Comm       comm;
2006:   PetscBool      posOrient = PETSC_FALSE;
2007:   const PetscInt debug     = 0;
2008:   PetscInt       cellDim, faceSize, f;

2012:   PetscObjectGetComm((PetscObject)dm,&comm);
2013:   DMGetDimension(dm, &cellDim);
2014:   if (debug) {PetscPrintf(comm, "cellDim: %d numCorners: %d\n", cellDim, numCorners);}

2016:   if (cellDim == 1 && numCorners == 2) {
2017:     /* Triangle */
2018:     faceSize  = numCorners-1;
2019:     posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2020:   } else if (cellDim == 2 && numCorners == 3) {
2021:     /* Triangle */
2022:     faceSize  = numCorners-1;
2023:     posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2024:   } else if (cellDim == 3 && numCorners == 4) {
2025:     /* Tetrahedron */
2026:     faceSize  = numCorners-1;
2027:     posOrient = (oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2028:   } else if (cellDim == 1 && numCorners == 3) {
2029:     /* Quadratic line */
2030:     faceSize  = 1;
2031:     posOrient = PETSC_TRUE;
2032:   } else if (cellDim == 2 && numCorners == 4) {
2033:     /* Quads */
2034:     faceSize = 2;
2035:     if ((indices[1] > indices[0]) && (indices[1] - indices[0] == 1)) {
2036:       posOrient = PETSC_TRUE;
2037:     } else if ((indices[0] == 3) && (indices[1] == 0)) {
2038:       posOrient = PETSC_TRUE;
2039:     } else {
2040:       if (((indices[0] > indices[1]) && (indices[0] - indices[1] == 1)) || ((indices[0] == 0) && (indices[1] == 3))) {
2041:         posOrient = PETSC_FALSE;
2042:       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossedge");
2043:     }
2044:   } else if (cellDim == 2 && numCorners == 6) {
2045:     /* Quadratic triangle (I hate this) */
2046:     /* Edges are determined by the first 2 vertices (corners of edges) */
2047:     const PetscInt faceSizeTri = 3;
2048:     PetscInt       sortedIndices[3], i, iFace;
2049:     PetscBool      found                    = PETSC_FALSE;
2050:     PetscInt       faceVerticesTriSorted[9] = {
2051:       0, 3,  4, /* bottom */
2052:       1, 4,  5, /* right */
2053:       2, 3,  5, /* left */
2054:     };
2055:     PetscInt       faceVerticesTri[9] = {
2056:       0, 3,  4, /* bottom */
2057:       1, 4,  5, /* right */
2058:       2, 5,  3, /* left */
2059:     };

2061:     faceSize = faceSizeTri;
2062:     for (i = 0; i < faceSizeTri; ++i) sortedIndices[i] = indices[i];
2063:     PetscSortInt(faceSizeTri, sortedIndices);
2064:     for (iFace = 0; iFace < 3; ++iFace) {
2065:       const PetscInt ii = iFace*faceSizeTri;
2066:       PetscInt       fVertex, cVertex;

2068:       if ((sortedIndices[0] == faceVerticesTriSorted[ii+0]) &&
2069:           (sortedIndices[1] == faceVerticesTriSorted[ii+1])) {
2070:         for (fVertex = 0; fVertex < faceSizeTri; ++fVertex) {
2071:           for (cVertex = 0; cVertex < faceSizeTri; ++cVertex) {
2072:             if (indices[cVertex] == faceVerticesTri[ii+fVertex]) {
2073:               faceVertices[fVertex] = origVertices[cVertex];
2074:               break;
2075:             }
2076:           }
2077:         }
2078:         found = PETSC_TRUE;
2079:         break;
2080:       }
2081:     }
2082:     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid tri crossface");
2083:     if (posOriented) *posOriented = PETSC_TRUE;
2084:     return(0);
2085:   } else if (cellDim == 2 && numCorners == 9) {
2086:     /* Quadratic quad (I hate this) */
2087:     /* Edges are determined by the first 2 vertices (corners of edges) */
2088:     const PetscInt faceSizeQuad = 3;
2089:     PetscInt       sortedIndices[3], i, iFace;
2090:     PetscBool      found                      = PETSC_FALSE;
2091:     PetscInt       faceVerticesQuadSorted[12] = {
2092:       0, 1,  4, /* bottom */
2093:       1, 2,  5, /* right */
2094:       2, 3,  6, /* top */
2095:       0, 3,  7, /* left */
2096:     };
2097:     PetscInt       faceVerticesQuad[12] = {
2098:       0, 1,  4, /* bottom */
2099:       1, 2,  5, /* right */
2100:       2, 3,  6, /* top */
2101:       3, 0,  7, /* left */
2102:     };

2104:     faceSize = faceSizeQuad;
2105:     for (i = 0; i < faceSizeQuad; ++i) sortedIndices[i] = indices[i];
2106:     PetscSortInt(faceSizeQuad, sortedIndices);
2107:     for (iFace = 0; iFace < 4; ++iFace) {
2108:       const PetscInt ii = iFace*faceSizeQuad;
2109:       PetscInt       fVertex, cVertex;

2111:       if ((sortedIndices[0] == faceVerticesQuadSorted[ii+0]) &&
2112:           (sortedIndices[1] == faceVerticesQuadSorted[ii+1])) {
2113:         for (fVertex = 0; fVertex < faceSizeQuad; ++fVertex) {
2114:           for (cVertex = 0; cVertex < faceSizeQuad; ++cVertex) {
2115:             if (indices[cVertex] == faceVerticesQuad[ii+fVertex]) {
2116:               faceVertices[fVertex] = origVertices[cVertex];
2117:               break;
2118:             }
2119:           }
2120:         }
2121:         found = PETSC_TRUE;
2122:         break;
2123:       }
2124:     }
2125:     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossface");
2126:     if (posOriented) *posOriented = PETSC_TRUE;
2127:     return(0);
2128:   } else if (cellDim == 3 && numCorners == 8) {
2129:     /* Hexes
2130:        A hex is two oriented quads with the normal of the first
2131:        pointing up at the second.

2133:           7---6
2134:          /|  /|
2135:         4---5 |
2136:         | 1-|-2
2137:         |/  |/
2138:         0---3

2140:         Faces are determined by the first 4 vertices (corners of faces) */
2141:     const PetscInt faceSizeHex = 4;
2142:     PetscInt       sortedIndices[4], i, iFace;
2143:     PetscBool      found                     = PETSC_FALSE;
2144:     PetscInt       faceVerticesHexSorted[24] = {
2145:       0, 1, 2, 3,  /* bottom */
2146:       4, 5, 6, 7,  /* top */
2147:       0, 3, 4, 5,  /* front */
2148:       2, 3, 5, 6,  /* right */
2149:       1, 2, 6, 7,  /* back */
2150:       0, 1, 4, 7,  /* left */
2151:     };
2152:     PetscInt       faceVerticesHex[24] = {
2153:       1, 2, 3, 0,  /* bottom */
2154:       4, 5, 6, 7,  /* top */
2155:       0, 3, 5, 4,  /* front */
2156:       3, 2, 6, 5,  /* right */
2157:       2, 1, 7, 6,  /* back */
2158:       1, 0, 4, 7,  /* left */
2159:     };

2161:     faceSize = faceSizeHex;
2162:     for (i = 0; i < faceSizeHex; ++i) sortedIndices[i] = indices[i];
2163:     PetscSortInt(faceSizeHex, sortedIndices);
2164:     for (iFace = 0; iFace < 6; ++iFace) {
2165:       const PetscInt ii = iFace*faceSizeHex;
2166:       PetscInt       fVertex, cVertex;

2168:       if ((sortedIndices[0] == faceVerticesHexSorted[ii+0]) &&
2169:           (sortedIndices[1] == faceVerticesHexSorted[ii+1]) &&
2170:           (sortedIndices[2] == faceVerticesHexSorted[ii+2]) &&
2171:           (sortedIndices[3] == faceVerticesHexSorted[ii+3])) {
2172:         for (fVertex = 0; fVertex < faceSizeHex; ++fVertex) {
2173:           for (cVertex = 0; cVertex < faceSizeHex; ++cVertex) {
2174:             if (indices[cVertex] == faceVerticesHex[ii+fVertex]) {
2175:               faceVertices[fVertex] = origVertices[cVertex];
2176:               break;
2177:             }
2178:           }
2179:         }
2180:         found = PETSC_TRUE;
2181:         break;
2182:       }
2183:     }
2184:     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2185:     if (posOriented) *posOriented = PETSC_TRUE;
2186:     return(0);
2187:   } else if (cellDim == 3 && numCorners == 10) {
2188:     /* Quadratic tet */
2189:     /* Faces are determined by the first 3 vertices (corners of faces) */
2190:     const PetscInt faceSizeTet = 6;
2191:     PetscInt       sortedIndices[6], i, iFace;
2192:     PetscBool      found                     = PETSC_FALSE;
2193:     PetscInt       faceVerticesTetSorted[24] = {
2194:       0, 1, 2,  6, 7, 8, /* bottom */
2195:       0, 3, 4,  6, 7, 9,  /* front */
2196:       1, 4, 5,  7, 8, 9,  /* right */
2197:       2, 3, 5,  6, 8, 9,  /* left */
2198:     };
2199:     PetscInt       faceVerticesTet[24] = {
2200:       0, 1, 2,  6, 7, 8, /* bottom */
2201:       0, 4, 3,  6, 7, 9,  /* front */
2202:       1, 5, 4,  7, 8, 9,  /* right */
2203:       2, 3, 5,  8, 6, 9,  /* left */
2204:     };

2206:     faceSize = faceSizeTet;
2207:     for (i = 0; i < faceSizeTet; ++i) sortedIndices[i] = indices[i];
2208:     PetscSortInt(faceSizeTet, sortedIndices);
2209:     for (iFace=0; iFace < 4; ++iFace) {
2210:       const PetscInt ii = iFace*faceSizeTet;
2211:       PetscInt       fVertex, cVertex;

2213:       if ((sortedIndices[0] == faceVerticesTetSorted[ii+0]) &&
2214:           (sortedIndices[1] == faceVerticesTetSorted[ii+1]) &&
2215:           (sortedIndices[2] == faceVerticesTetSorted[ii+2]) &&
2216:           (sortedIndices[3] == faceVerticesTetSorted[ii+3])) {
2217:         for (fVertex = 0; fVertex < faceSizeTet; ++fVertex) {
2218:           for (cVertex = 0; cVertex < faceSizeTet; ++cVertex) {
2219:             if (indices[cVertex] == faceVerticesTet[ii+fVertex]) {
2220:               faceVertices[fVertex] = origVertices[cVertex];
2221:               break;
2222:             }
2223:           }
2224:         }
2225:         found = PETSC_TRUE;
2226:         break;
2227:       }
2228:     }
2229:     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid tet crossface");
2230:     if (posOriented) *posOriented = PETSC_TRUE;
2231:     return(0);
2232:   } else if (cellDim == 3 && numCorners == 27) {
2233:     /* Quadratic hexes (I hate this)
2234:        A hex is two oriented quads with the normal of the first
2235:        pointing up at the second.

2237:          7---6
2238:         /|  /|
2239:        4---5 |
2240:        | 3-|-2
2241:        |/  |/
2242:        0---1

2244:        Faces are determined by the first 4 vertices (corners of faces) */
2245:     const PetscInt faceSizeQuadHex = 9;
2246:     PetscInt       sortedIndices[9], i, iFace;
2247:     PetscBool      found                         = PETSC_FALSE;
2248:     PetscInt       faceVerticesQuadHexSorted[54] = {
2249:       0, 1, 2, 3,  8, 9, 10, 11,  24, /* bottom */
2250:       4, 5, 6, 7,  12, 13, 14, 15,  25, /* top */
2251:       0, 1, 4, 5,  8, 12, 16, 17,  22, /* front */
2252:       1, 2, 5, 6,  9, 13, 17, 18,  21, /* right */
2253:       2, 3, 6, 7,  10, 14, 18, 19,  23, /* back */
2254:       0, 3, 4, 7,  11, 15, 16, 19,  20, /* left */
2255:     };
2256:     PetscInt       faceVerticesQuadHex[54] = {
2257:       3, 2, 1, 0,  10, 9, 8, 11,  24, /* bottom */
2258:       4, 5, 6, 7,  12, 13, 14, 15,  25, /* top */
2259:       0, 1, 5, 4,  8, 17, 12, 16,  22, /* front */
2260:       1, 2, 6, 5,  9, 18, 13, 17,  21, /* right */
2261:       2, 3, 7, 6,  10, 19, 14, 18,  23, /* back */
2262:       3, 0, 4, 7,  11, 16, 15, 19,  20 /* left */
2263:     };

2265:     faceSize = faceSizeQuadHex;
2266:     for (i = 0; i < faceSizeQuadHex; ++i) sortedIndices[i] = indices[i];
2267:     PetscSortInt(faceSizeQuadHex, sortedIndices);
2268:     for (iFace = 0; iFace < 6; ++iFace) {
2269:       const PetscInt ii = iFace*faceSizeQuadHex;
2270:       PetscInt       fVertex, cVertex;

2272:       if ((sortedIndices[0] == faceVerticesQuadHexSorted[ii+0]) &&
2273:           (sortedIndices[1] == faceVerticesQuadHexSorted[ii+1]) &&
2274:           (sortedIndices[2] == faceVerticesQuadHexSorted[ii+2]) &&
2275:           (sortedIndices[3] == faceVerticesQuadHexSorted[ii+3])) {
2276:         for (fVertex = 0; fVertex < faceSizeQuadHex; ++fVertex) {
2277:           for (cVertex = 0; cVertex < faceSizeQuadHex; ++cVertex) {
2278:             if (indices[cVertex] == faceVerticesQuadHex[ii+fVertex]) {
2279:               faceVertices[fVertex] = origVertices[cVertex];
2280:               break;
2281:             }
2282:           }
2283:         }
2284:         found = PETSC_TRUE;
2285:         break;
2286:       }
2287:     }
2288:     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2289:     if (posOriented) *posOriented = PETSC_TRUE;
2290:     return(0);
2291:   } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Unknown cell type for faceOrientation().");
2292:   if (!posOrient) {
2293:     if (debug) {PetscPrintf(comm, "  Reversing initial face orientation\n");}
2294:     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[faceSize-1 - f];
2295:   } else {
2296:     if (debug) {PetscPrintf(comm, "  Keeping initial face orientation\n");}
2297:     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[f];
2298:   }
2299:   if (posOriented) *posOriented = posOrient;
2300:   return(0);
2301: }

2305: /*
2306:     Given a cell and a face, as a set of vertices,
2307:       return the oriented face, as a set of vertices, in faceVertices
2308:     The orientation is such that the face normal points out of the cell
2309: */
2310: PetscErrorCode DMPlexGetOrientedFace(DM dm, PetscInt cell, PetscInt faceSize, const PetscInt face[], PetscInt numCorners, PetscInt indices[], PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2311: {
2312:   const PetscInt *cone = NULL;
2313:   PetscInt        coneSize, v, f, v2;
2314:   PetscInt        oppositeVertex = -1;
2315:   PetscErrorCode  ierr;

2318:   DMPlexGetConeSize(dm, cell, &coneSize);
2319:   DMPlexGetCone(dm, cell, &cone);
2320:   for (v = 0, v2 = 0; v < coneSize; ++v) {
2321:     PetscBool found = PETSC_FALSE;

2323:     for (f = 0; f < faceSize; ++f) {
2324:       if (face[f] == cone[v]) {
2325:         found = PETSC_TRUE; break;
2326:       }
2327:     }
2328:     if (found) {
2329:       indices[v2]      = v;
2330:       origVertices[v2] = cone[v];
2331:       ++v2;
2332:     } else {
2333:       oppositeVertex = v;
2334:     }
2335:   }
2336:   DMPlexGetFaceOrientation(dm, cell, numCorners, indices, oppositeVertex, origVertices, faceVertices, posOriented);
2337:   return(0);
2338: }

2342: /*
2343:   DMPlexInsertFace_Internal - Puts a face into the mesh

2345:   Not collective

2347:   Input Parameters:
2348:   + dm              - The DMPlex
2349:   . numFaceVertex   - The number of vertices in the face
2350:   . faceVertices    - The vertices in the face for dm
2351:   . subfaceVertices - The vertices in the face for subdm
2352:   . numCorners      - The number of vertices in the cell
2353:   . cell            - A cell in dm containing the face
2354:   . subcell         - A cell in subdm containing the face
2355:   . firstFace       - First face in the mesh
2356:   - newFacePoint    - Next face in the mesh

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

2361:   Level: developer
2362: */
2363: 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)
2364: {
2365:   MPI_Comm        comm;
2366:   DM_Plex        *submesh = (DM_Plex*) subdm->data;
2367:   const PetscInt *faces;
2368:   PetscInt        numFaces, coneSize;
2369:   PetscErrorCode  ierr;

2372:   PetscObjectGetComm((PetscObject)dm,&comm);
2373:   DMPlexGetConeSize(subdm, subcell, &coneSize);
2374:   if (coneSize != 1) SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cone size of cell %d is %d != 1", cell, coneSize);
2375: #if 0
2376:   /* Cannot use this because support() has not been constructed yet */
2377:   DMPlexGetJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces);
2378: #else
2379:   {
2380:     PetscInt f;

2382:     numFaces = 0;
2383:     DMGetWorkArray(subdm, 1, PETSC_INT, (void **) &faces);
2384:     for (f = firstFace; f < *newFacePoint; ++f) {
2385:       PetscInt dof, off, d;

2387:       PetscSectionGetDof(submesh->coneSection, f, &dof);
2388:       PetscSectionGetOffset(submesh->coneSection, f, &off);
2389:       /* Yes, I know this is quadratic, but I expect the sizes to be <5 */
2390:       for (d = 0; d < dof; ++d) {
2391:         const PetscInt p = submesh->cones[off+d];
2392:         PetscInt       v;

2394:         for (v = 0; v < numFaceVertices; ++v) {
2395:           if (subfaceVertices[v] == p) break;
2396:         }
2397:         if (v == numFaceVertices) break;
2398:       }
2399:       if (d == dof) {
2400:         numFaces               = 1;
2401:         ((PetscInt*) faces)[0] = f;
2402:       }
2403:     }
2404:   }
2405: #endif
2406:   if (numFaces > 1) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Vertex set had %d faces, not one", numFaces);
2407:   else if (numFaces == 1) {
2408:     /* Add the other cell neighbor for this face */
2409:     DMPlexSetCone(subdm, subcell, faces);
2410:   } else {
2411:     PetscInt *indices, *origVertices, *orientedVertices, *orientedSubVertices, v, ov;
2412:     PetscBool posOriented;

2414:     DMGetWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), PETSC_INT, &orientedVertices);
2415:     origVertices        = &orientedVertices[numFaceVertices];
2416:     indices             = &orientedVertices[numFaceVertices*2];
2417:     orientedSubVertices = &orientedVertices[numFaceVertices*3];
2418:     DMPlexGetOrientedFace(dm, cell, numFaceVertices, faceVertices, numCorners, indices, origVertices, orientedVertices, &posOriented);
2419:     /* TODO: I know that routine should return a permutation, not the indices */
2420:     for (v = 0; v < numFaceVertices; ++v) {
2421:       const PetscInt vertex = faceVertices[v], subvertex = subfaceVertices[v];
2422:       for (ov = 0; ov < numFaceVertices; ++ov) {
2423:         if (orientedVertices[ov] == vertex) {
2424:           orientedSubVertices[ov] = subvertex;
2425:           break;
2426:         }
2427:       }
2428:       if (ov == numFaceVertices) SETERRQ1(comm, PETSC_ERR_PLIB, "Could not find face vertex %d in orientated set", vertex);
2429:     }
2430:     DMPlexSetCone(subdm, *newFacePoint, orientedSubVertices);
2431:     DMPlexSetCone(subdm, subcell, newFacePoint);
2432:     DMRestoreWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), PETSC_INT, &orientedVertices);
2433:     ++(*newFacePoint);
2434:   }
2435: #if 0
2436:   DMPlexRestoreJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces);
2437: #else
2438:   DMRestoreWorkArray(subdm, 1, PETSC_INT, (void **) &faces);
2439: #endif
2440:   return(0);
2441: }

2445: static PetscErrorCode DMPlexCreateSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm)
2446: {
2447:   MPI_Comm        comm;
2448:   DMLabel         subpointMap;
2449:   IS              subvertexIS,  subcellIS;
2450:   const PetscInt *subVertices, *subCells;
2451:   PetscInt        numSubVertices, firstSubVertex, numSubCells;
2452:   PetscInt       *subface, maxConeSize, numSubFaces = 0, firstSubFace, newFacePoint, nFV = 0;
2453:   PetscInt        vStart, vEnd, c, f;
2454:   PetscErrorCode  ierr;

2457:   PetscObjectGetComm((PetscObject)dm,&comm);
2458:   /* Create subpointMap which marks the submesh */
2459:   DMLabelCreate("subpoint_map", &subpointMap);
2460:   DMPlexSetSubpointMap(subdm, subpointMap);
2461:   DMLabelDestroy(&subpointMap);
2462:   if (vertexLabel) {DMPlexMarkSubmesh_Uninterpolated(dm, vertexLabel, value, subpointMap, &numSubFaces, &nFV, subdm);}
2463:   /* Setup chart */
2464:   DMLabelGetStratumSize(subpointMap, 0, &numSubVertices);
2465:   DMLabelGetStratumSize(subpointMap, 2, &numSubCells);
2466:   DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices);
2467:   DMPlexSetVTKCellHeight(subdm, 1);
2468:   /* Set cone sizes */
2469:   firstSubVertex = numSubCells;
2470:   firstSubFace   = numSubCells+numSubVertices;
2471:   newFacePoint   = firstSubFace;
2472:   DMLabelGetStratumIS(subpointMap, 0, &subvertexIS);
2473:   if (subvertexIS) {ISGetIndices(subvertexIS, &subVertices);}
2474:   DMLabelGetStratumIS(subpointMap, 2, &subcellIS);
2475:   if (subcellIS) {ISGetIndices(subcellIS, &subCells);}
2476:   for (c = 0; c < numSubCells; ++c) {
2477:     DMPlexSetConeSize(subdm, c, 1);
2478:   }
2479:   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
2480:     DMPlexSetConeSize(subdm, f, nFV);
2481:   }
2482:   DMSetUp(subdm);
2483:   /* Create face cones */
2484:   DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
2485:   DMPlexGetMaxSizes(dm, &maxConeSize, NULL);
2486:   DMGetWorkArray(subdm, maxConeSize, PETSC_INT, (void**) &subface);
2487:   for (c = 0; c < numSubCells; ++c) {
2488:     const PetscInt cell    = subCells[c];
2489:     const PetscInt subcell = c;
2490:     PetscInt      *closure = NULL;
2491:     PetscInt       closureSize, cl, numCorners = 0, faceSize = 0;

2493:     DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);
2494:     for (cl = 0; cl < closureSize*2; cl += 2) {
2495:       const PetscInt point = closure[cl];
2496:       PetscInt       subVertex;

2498:       if ((point >= vStart) && (point < vEnd)) {
2499:         ++numCorners;
2500:         PetscFindInt(point, numSubVertices, subVertices, &subVertex);
2501:         if (subVertex >= 0) {
2502:           closure[faceSize] = point;
2503:           subface[faceSize] = firstSubVertex+subVertex;
2504:           ++faceSize;
2505:         }
2506:       }
2507:     }
2508:     if (faceSize > nFV) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %d of an element on the surface", faceSize);
2509:     if (faceSize == nFV) {
2510:       DMPlexInsertFace_Internal(dm, subdm, faceSize, closure, subface, numCorners, cell, subcell, firstSubFace, &newFacePoint);
2511:     }
2512:     DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);
2513:   }
2514:   DMRestoreWorkArray(subdm, maxConeSize, PETSC_INT, (void**) &subface);
2515:   DMPlexSymmetrize(subdm);
2516:   DMPlexStratify(subdm);
2517:   /* Build coordinates */
2518:   {
2519:     PetscSection coordSection, subCoordSection;
2520:     Vec          coordinates, subCoordinates;
2521:     PetscScalar *coords, *subCoords;
2522:     PetscInt     numComp, coordSize, v;

2524:     DMGetCoordinateSection(dm, &coordSection);
2525:     DMGetCoordinatesLocal(dm, &coordinates);
2526:     DMGetCoordinateSection(subdm, &subCoordSection);
2527:     PetscSectionSetNumFields(subCoordSection, 1);
2528:     PetscSectionGetFieldComponents(coordSection, 0, &numComp);
2529:     PetscSectionSetFieldComponents(subCoordSection, 0, numComp);
2530:     PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices);
2531:     for (v = 0; v < numSubVertices; ++v) {
2532:       const PetscInt vertex    = subVertices[v];
2533:       const PetscInt subvertex = firstSubVertex+v;
2534:       PetscInt       dof;

2536:       PetscSectionGetDof(coordSection, vertex, &dof);
2537:       PetscSectionSetDof(subCoordSection, subvertex, dof);
2538:       PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);
2539:     }
2540:     PetscSectionSetUp(subCoordSection);
2541:     PetscSectionGetStorageSize(subCoordSection, &coordSize);
2542:     VecCreate(comm, &subCoordinates);
2543:     VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);
2544:     VecSetType(subCoordinates,VECSTANDARD);
2545:     if (coordSize) {
2546:       VecGetArray(coordinates,    &coords);
2547:       VecGetArray(subCoordinates, &subCoords);
2548:       for (v = 0; v < numSubVertices; ++v) {
2549:         const PetscInt vertex    = subVertices[v];
2550:         const PetscInt subvertex = firstSubVertex+v;
2551:         PetscInt       dof, off, sdof, soff, d;

2553:         PetscSectionGetDof(coordSection, vertex, &dof);
2554:         PetscSectionGetOffset(coordSection, vertex, &off);
2555:         PetscSectionGetDof(subCoordSection, subvertex, &sdof);
2556:         PetscSectionGetOffset(subCoordSection, subvertex, &soff);
2557:         if (dof != sdof) SETERRQ4(comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
2558:         for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
2559:       }
2560:       VecRestoreArray(coordinates,    &coords);
2561:       VecRestoreArray(subCoordinates, &subCoords);
2562:     }
2563:     DMSetCoordinatesLocal(subdm, subCoordinates);
2564:     VecDestroy(&subCoordinates);
2565:   }
2566:   /* Cleanup */
2567:   if (subvertexIS) {ISRestoreIndices(subvertexIS, &subVertices);}
2568:   ISDestroy(&subvertexIS);
2569:   if (subcellIS) {ISRestoreIndices(subcellIS, &subCells);}
2570:   ISDestroy(&subcellIS);
2571:   return(0);
2572: }

2576: static PetscErrorCode DMPlexCreateSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm)
2577: {
2578:   MPI_Comm         comm;
2579:   DMLabel          subpointMap;
2580:   IS              *subpointIS;
2581:   const PetscInt **subpoints;
2582:   PetscInt        *numSubPoints, *firstSubPoint, *coneNew, *coneONew;
2583:   PetscInt         totSubPoints = 0, maxConeSize, dim, p, d, v;
2584:   PetscErrorCode   ierr;

2587:   PetscObjectGetComm((PetscObject)dm,&comm);
2588:   /* Create subpointMap which marks the submesh */
2589:   DMLabelCreate("subpoint_map", &subpointMap);
2590:   DMPlexSetSubpointMap(subdm, subpointMap);
2591:   DMLabelDestroy(&subpointMap);
2592:   if (vertexLabel) {DMPlexMarkSubmesh_Interpolated(dm, vertexLabel, value, subpointMap, subdm);}
2593:   /* Setup chart */
2594:   DMGetDimension(dm, &dim);
2595:   PetscMalloc4(dim+1,&numSubPoints,dim+1,&firstSubPoint,dim+1,&subpointIS,dim+1,&subpoints);
2596:   for (d = 0; d <= dim; ++d) {
2597:     DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d]);
2598:     totSubPoints += numSubPoints[d];
2599:   }
2600:   DMPlexSetChart(subdm, 0, totSubPoints);
2601:   DMPlexSetVTKCellHeight(subdm, 1);
2602:   /* Set cone sizes */
2603:   firstSubPoint[dim] = 0;
2604:   firstSubPoint[0]   = firstSubPoint[dim] + numSubPoints[dim];
2605:   if (dim > 1) {firstSubPoint[dim-1] = firstSubPoint[0]     + numSubPoints[0];}
2606:   if (dim > 2) {firstSubPoint[dim-2] = firstSubPoint[dim-1] + numSubPoints[dim-1];}
2607:   for (d = 0; d <= dim; ++d) {
2608:     DMLabelGetStratumIS(subpointMap, d, &subpointIS[d]);
2609:     if (subpointIS[d]) {ISGetIndices(subpointIS[d], &subpoints[d]);}
2610:   }
2611:   for (d = 0; d <= dim; ++d) {
2612:     for (p = 0; p < numSubPoints[d]; ++p) {
2613:       const PetscInt  point    = subpoints[d][p];
2614:       const PetscInt  subpoint = firstSubPoint[d] + p;
2615:       const PetscInt *cone;
2616:       PetscInt        coneSize, coneSizeNew, c, val;

2618:       DMPlexGetConeSize(dm, point, &coneSize);
2619:       DMPlexSetConeSize(subdm, subpoint, coneSize);
2620:       if (d == dim) {
2621:         DMPlexGetCone(dm, point, &cone);
2622:         for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
2623:           DMLabelGetValue(subpointMap, cone[c], &val);
2624:           if (val >= 0) coneSizeNew++;
2625:         }
2626:         DMPlexSetConeSize(subdm, subpoint, coneSizeNew);
2627:       }
2628:     }
2629:   }
2630:   DMSetUp(subdm);
2631:   /* Set cones */
2632:   DMPlexGetMaxSizes(dm, &maxConeSize, NULL);
2633:   PetscMalloc2(maxConeSize,&coneNew,maxConeSize,&coneONew);
2634:   for (d = 0; d <= dim; ++d) {
2635:     for (p = 0; p < numSubPoints[d]; ++p) {
2636:       const PetscInt  point    = subpoints[d][p];
2637:       const PetscInt  subpoint = firstSubPoint[d] + p;
2638:       const PetscInt *cone, *ornt;
2639:       PetscInt        coneSize, subconeSize, coneSizeNew, c, subc;

2641:       DMPlexGetConeSize(dm, point, &coneSize);
2642:       DMPlexGetConeSize(subdm, subpoint, &subconeSize);
2643:       DMPlexGetCone(dm, point, &cone);
2644:       DMPlexGetConeOrientation(dm, point, &ornt);
2645:       for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
2646:         PetscFindInt(cone[c], numSubPoints[d-1], subpoints[d-1], &subc);
2647:         if (subc >= 0) {
2648:           coneNew[coneSizeNew]  = firstSubPoint[d-1] + subc;
2649:           coneONew[coneSizeNew] = ornt[c];
2650:           ++coneSizeNew;
2651:         }
2652:       }
2653:       if (coneSizeNew != subconeSize) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of cone points located %d does not match subcone size %d", coneSizeNew, subconeSize);
2654:       DMPlexSetCone(subdm, subpoint, coneNew);
2655:       DMPlexSetConeOrientation(subdm, subpoint, coneONew);
2656:     }
2657:   }
2658:   PetscFree2(coneNew,coneONew);
2659:   DMPlexSymmetrize(subdm);
2660:   DMPlexStratify(subdm);
2661:   /* Build coordinates */
2662:   {
2663:     PetscSection coordSection, subCoordSection;
2664:     Vec          coordinates, subCoordinates;
2665:     PetscScalar *coords, *subCoords;
2666:     PetscInt     numComp, coordSize;

2668:     DMGetCoordinateSection(dm, &coordSection);
2669:     DMGetCoordinatesLocal(dm, &coordinates);
2670:     DMGetCoordinateSection(subdm, &subCoordSection);
2671:     PetscSectionSetNumFields(subCoordSection, 1);
2672:     PetscSectionGetFieldComponents(coordSection, 0, &numComp);
2673:     PetscSectionSetFieldComponents(subCoordSection, 0, numComp);
2674:     PetscSectionSetChart(subCoordSection, firstSubPoint[0], firstSubPoint[0]+numSubPoints[0]);
2675:     for (v = 0; v < numSubPoints[0]; ++v) {
2676:       const PetscInt vertex    = subpoints[0][v];
2677:       const PetscInt subvertex = firstSubPoint[0]+v;
2678:       PetscInt       dof;

2680:       PetscSectionGetDof(coordSection, vertex, &dof);
2681:       PetscSectionSetDof(subCoordSection, subvertex, dof);
2682:       PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);
2683:     }
2684:     PetscSectionSetUp(subCoordSection);
2685:     PetscSectionGetStorageSize(subCoordSection, &coordSize);
2686:     VecCreate(comm, &subCoordinates);
2687:     VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);
2688:     VecSetType(subCoordinates,VECSTANDARD);
2689:     VecGetArray(coordinates,    &coords);
2690:     VecGetArray(subCoordinates, &subCoords);
2691:     for (v = 0; v < numSubPoints[0]; ++v) {
2692:       const PetscInt vertex    = subpoints[0][v];
2693:       const PetscInt subvertex = firstSubPoint[0]+v;
2694:       PetscInt dof, off, sdof, soff, d;

2696:       PetscSectionGetDof(coordSection, vertex, &dof);
2697:       PetscSectionGetOffset(coordSection, vertex, &off);
2698:       PetscSectionGetDof(subCoordSection, subvertex, &sdof);
2699:       PetscSectionGetOffset(subCoordSection, subvertex, &soff);
2700:       if (dof != sdof) SETERRQ4(comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
2701:       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
2702:     }
2703:     VecRestoreArray(coordinates,    &coords);
2704:     VecRestoreArray(subCoordinates, &subCoords);
2705:     DMSetCoordinatesLocal(subdm, subCoordinates);
2706:     VecDestroy(&subCoordinates);
2707:   }
2708:   /* Cleanup */
2709:   for (d = 0; d <= dim; ++d) {
2710:     if (subpointIS[d]) {ISRestoreIndices(subpointIS[d], &subpoints[d]);}
2711:     ISDestroy(&subpointIS[d]);
2712:   }
2713:   PetscFree4(numSubPoints,firstSubPoint,subpointIS,subpoints);
2714:   return(0);
2715: }

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

2722:   Input Parameters:
2723: + dm           - The original mesh
2724: . vertexLabel  - The DMLabel marking vertices contained in the surface
2725: - value        - The label value to use

2727:   Output Parameter:
2728: . subdm - The surface mesh

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

2732:   Level: developer

2734: .seealso: DMPlexGetSubpointMap(), DMPlexGetLabel(), DMLabelSetValue()
2735: @*/
2736: PetscErrorCode DMPlexCreateSubmesh(DM dm, DMLabel vertexLabel, PetscInt value, DM *subdm)
2737: {
2738:   PetscInt       dim, depth;

2744:   DMGetDimension(dm, &dim);
2745:   DMPlexGetDepth(dm, &depth);
2746:   DMCreate(PetscObjectComm((PetscObject)dm), subdm);
2747:   DMSetType(*subdm, DMPLEX);
2748:   DMSetDimension(*subdm, dim-1);
2749:   if (depth == dim) {
2750:     DMPlexCreateSubmesh_Interpolated(dm, vertexLabel, value, *subdm);
2751:   } else {
2752:     DMPlexCreateSubmesh_Uninterpolated(dm, vertexLabel, value, *subdm);
2753:   }
2754:   return(0);
2755: }

2759: PETSC_STATIC_INLINE PetscInt DMPlexFilterPoint_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[])
2760: {
2761:   PetscInt       subPoint;

2764:   PetscFindInt(point, numSubPoints, subPoints, &subPoint); if (ierr < 0) return ierr;
2765:   return subPoint < 0 ? subPoint : firstSubPoint+subPoint;
2766: }

2770: static PetscErrorCode DMPlexCreateCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM subdm)
2771: {
2772:   MPI_Comm        comm;
2773:   DMLabel         subpointMap;
2774:   IS              subvertexIS;
2775:   const PetscInt *subVertices;
2776:   PetscInt        numSubVertices, firstSubVertex, numSubCells, *subCells = NULL;
2777:   PetscInt       *subface, maxConeSize, numSubFaces, firstSubFace, newFacePoint, nFV;
2778:   PetscInt        cMax, c, f;
2779:   PetscErrorCode  ierr;

2782:   PetscObjectGetComm((PetscObject)dm, &comm);
2783:   /* Create subpointMap which marks the submesh */
2784:   DMLabelCreate("subpoint_map", &subpointMap);
2785:   DMPlexSetSubpointMap(subdm, subpointMap);
2786:   DMLabelDestroy(&subpointMap);
2787:   DMPlexMarkCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, subpointMap, &numSubFaces, &nFV, &subCells, subdm);
2788:   /* Setup chart */
2789:   DMLabelGetStratumSize(subpointMap, 0, &numSubVertices);
2790:   DMLabelGetStratumSize(subpointMap, 2, &numSubCells);
2791:   DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices);
2792:   DMPlexSetVTKCellHeight(subdm, 1);
2793:   /* Set cone sizes */
2794:   firstSubVertex = numSubCells;
2795:   firstSubFace   = numSubCells+numSubVertices;
2796:   newFacePoint   = firstSubFace;
2797:   DMLabelGetStratumIS(subpointMap, 0, &subvertexIS);
2798:   if (subvertexIS) {ISGetIndices(subvertexIS, &subVertices);}
2799:   for (c = 0; c < numSubCells; ++c) {
2800:     DMPlexSetConeSize(subdm, c, 1);
2801:   }
2802:   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
2803:     DMPlexSetConeSize(subdm, f, nFV);
2804:   }
2805:   DMSetUp(subdm);
2806:   /* Create face cones */
2807:   DMPlexGetMaxSizes(dm, &maxConeSize, NULL);
2808:   DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);
2809:   DMGetWorkArray(subdm, maxConeSize, PETSC_INT, (void**) &subface);
2810:   for (c = 0; c < numSubCells; ++c) {
2811:     const PetscInt  cell    = subCells[c];
2812:     const PetscInt  subcell = c;
2813:     const PetscInt *cone, *cells;
2814:     PetscInt        numCells, subVertex, p, v;

2816:     if (cell < cMax) continue;
2817:     DMPlexGetCone(dm, cell, &cone);
2818:     for (v = 0; v < nFV; ++v) {
2819:       PetscFindInt(cone[v], numSubVertices, subVertices, &subVertex);
2820:       subface[v] = firstSubVertex+subVertex;
2821:     }
2822:     DMPlexSetCone(subdm, newFacePoint, subface);
2823:     DMPlexSetCone(subdm, subcell, &newFacePoint);
2824:     DMPlexGetJoin(dm, nFV, cone, &numCells, &cells);
2825:     /* Not true in parallel
2826:     if (numCells != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
2827:     for (p = 0; p < numCells; ++p) {
2828:       PetscInt negsubcell;

2830:       if (cells[p] >= cMax) continue;
2831:       /* I know this is a crap search */
2832:       for (negsubcell = 0; negsubcell < numSubCells; ++negsubcell) {
2833:         if (subCells[negsubcell] == cells[p]) break;
2834:       }
2835:       if (negsubcell == numSubCells) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find negative face neighbor for cohesive cell %d", cell);
2836:       DMPlexSetCone(subdm, negsubcell, &newFacePoint);
2837:     }
2838:     DMPlexRestoreJoin(dm, nFV, cone, &numCells, &cells);
2839:     ++newFacePoint;
2840:   }
2841:   DMRestoreWorkArray(subdm, maxConeSize, PETSC_INT, (void**) &subface);
2842:   DMPlexSymmetrize(subdm);
2843:   DMPlexStratify(subdm);
2844:   /* Build coordinates */
2845:   {
2846:     PetscSection coordSection, subCoordSection;
2847:     Vec          coordinates, subCoordinates;
2848:     PetscScalar *coords, *subCoords;
2849:     PetscInt     numComp, coordSize, v;

2851:     DMGetCoordinateSection(dm, &coordSection);
2852:     DMGetCoordinatesLocal(dm, &coordinates);
2853:     DMGetCoordinateSection(subdm, &subCoordSection);
2854:     PetscSectionSetNumFields(subCoordSection, 1);
2855:     PetscSectionGetFieldComponents(coordSection, 0, &numComp);
2856:     PetscSectionSetFieldComponents(subCoordSection, 0, numComp);
2857:     PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices);
2858:     for (v = 0; v < numSubVertices; ++v) {
2859:       const PetscInt vertex    = subVertices[v];
2860:       const PetscInt subvertex = firstSubVertex+v;
2861:       PetscInt       dof;

2863:       PetscSectionGetDof(coordSection, vertex, &dof);
2864:       PetscSectionSetDof(subCoordSection, subvertex, dof);
2865:       PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);
2866:     }
2867:     PetscSectionSetUp(subCoordSection);
2868:     PetscSectionGetStorageSize(subCoordSection, &coordSize);
2869:     VecCreate(comm, &subCoordinates);
2870:     VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);
2871:     VecSetType(subCoordinates,VECSTANDARD);
2872:     VecGetArray(coordinates,    &coords);
2873:     VecGetArray(subCoordinates, &subCoords);
2874:     for (v = 0; v < numSubVertices; ++v) {
2875:       const PetscInt vertex    = subVertices[v];
2876:       const PetscInt subvertex = firstSubVertex+v;
2877:       PetscInt       dof, off, sdof, soff, d;

2879:       PetscSectionGetDof(coordSection, vertex, &dof);
2880:       PetscSectionGetOffset(coordSection, vertex, &off);
2881:       PetscSectionGetDof(subCoordSection, subvertex, &sdof);
2882:       PetscSectionGetOffset(subCoordSection, subvertex, &soff);
2883:       if (dof != sdof) SETERRQ4(comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
2884:       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
2885:     }
2886:     VecRestoreArray(coordinates,    &coords);
2887:     VecRestoreArray(subCoordinates, &subCoords);
2888:     DMSetCoordinatesLocal(subdm, subCoordinates);
2889:     VecDestroy(&subCoordinates);
2890:   }
2891:   /* Build SF */
2892:   CHKMEMQ;
2893:   {
2894:     PetscSF            sfPoint, sfPointSub;
2895:     const PetscSFNode *remotePoints;
2896:     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
2897:     const PetscInt    *localPoints;
2898:     PetscInt          *slocalPoints;
2899:     PetscInt           numRoots, numLeaves, numSubRoots = numSubCells+numSubFaces+numSubVertices, numSubLeaves = 0, l, sl, ll, pStart, pEnd, p, vStart, vEnd;
2900:     PetscMPIInt        rank;

2902:     MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);
2903:     DMGetPointSF(dm, &sfPoint);
2904:     DMGetPointSF(subdm, &sfPointSub);
2905:     DMPlexGetChart(dm, &pStart, &pEnd);
2906:     DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);
2907:     PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);
2908:     if (numRoots >= 0) {
2909:       /* Only vertices should be shared */
2910:       PetscMalloc2(pEnd-pStart,&newLocalPoints,numRoots,&newOwners);
2911:       for (p = 0; p < pEnd-pStart; ++p) {
2912:         newLocalPoints[p].rank  = -2;
2913:         newLocalPoints[p].index = -2;
2914:       }
2915:       /* Set subleaves */
2916:       for (l = 0; l < numLeaves; ++l) {
2917:         const PetscInt point    = localPoints[l];
2918:         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);

2920:         if ((point < vStart) && (point >= vEnd)) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Should not be mapping anything but vertices, %d", point);
2921:         if (subPoint < 0) continue;
2922:         newLocalPoints[point-pStart].rank  = rank;
2923:         newLocalPoints[point-pStart].index = subPoint;
2924:         ++numSubLeaves;
2925:       }
2926:       /* Must put in owned subpoints */
2927:       for (p = pStart; p < pEnd; ++p) {
2928:         const PetscInt subPoint = DMPlexFilterPoint_Internal(p, firstSubVertex, numSubVertices, subVertices);

2930:         if (subPoint < 0) {
2931:           newOwners[p-pStart].rank  = -3;
2932:           newOwners[p-pStart].index = -3;
2933:         } else {
2934:           newOwners[p-pStart].rank  = rank;
2935:           newOwners[p-pStart].index = subPoint;
2936:         }
2937:       }
2938:       PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);
2939:       PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);
2940:       PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints);
2941:       PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints);
2942:       PetscMalloc1(numSubLeaves,    &slocalPoints);
2943:       PetscMalloc1(numSubLeaves, &sremotePoints);
2944:       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
2945:         const PetscInt point    = localPoints[l];
2946:         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);

2948:         if (subPoint < 0) continue;
2949:         if (newLocalPoints[point].rank == rank) {++ll; continue;}
2950:         slocalPoints[sl]        = subPoint;
2951:         sremotePoints[sl].rank  = newLocalPoints[point].rank;
2952:         sremotePoints[sl].index = newLocalPoints[point].index;
2953:         if (sremotePoints[sl].rank  < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %d", point);
2954:         if (sremotePoints[sl].index < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %d", point);
2955:         ++sl;
2956:       }
2957:       PetscFree2(newLocalPoints,newOwners);
2958:       if (sl + ll != numSubLeaves) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %d + %d != %d", sl, ll, numSubLeaves);
2959:       PetscSFSetGraph(sfPointSub, numSubRoots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER);
2960:     }
2961:   }
2962:   CHKMEMQ;
2963:   /* Cleanup */
2964:   if (subvertexIS) {ISRestoreIndices(subvertexIS, &subVertices);}
2965:   ISDestroy(&subvertexIS);
2966:   PetscFree(subCells);
2967:   return(0);
2968: }

2972: static PetscErrorCode DMPlexCreateCohesiveSubmesh_Interpolated(DM dm, const char label[], PetscInt value, DM subdm)
2973: {
2974:   MPI_Comm         comm;
2975:   DMLabel          subpointMap;
2976:   IS              *subpointIS;
2977:   const PetscInt **subpoints;
2978:   PetscInt        *numSubPoints, *firstSubPoint, *coneNew, *orntNew;
2979:   PetscInt         totSubPoints = 0, maxConeSize, dim, p, d, v;
2980:   PetscErrorCode   ierr;

2983:   PetscObjectGetComm((PetscObject)dm,&comm);
2984:   /* Create subpointMap which marks the submesh */
2985:   DMLabelCreate("subpoint_map", &subpointMap);
2986:   DMPlexSetSubpointMap(subdm, subpointMap);
2987:   DMLabelDestroy(&subpointMap);
2988:   DMPlexMarkCohesiveSubmesh_Interpolated(dm, label, value, subpointMap, subdm);
2989:   /* Setup chart */
2990:   DMGetDimension(dm, &dim);
2991:   PetscMalloc4(dim+1,&numSubPoints,dim+1,&firstSubPoint,dim+1,&subpointIS,dim+1,&subpoints);
2992:   for (d = 0; d <= dim; ++d) {
2993:     DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d]);
2994:     totSubPoints += numSubPoints[d];
2995:   }
2996:   DMPlexSetChart(subdm, 0, totSubPoints);
2997:   DMPlexSetVTKCellHeight(subdm, 1);
2998:   /* Set cone sizes */
2999:   firstSubPoint[dim] = 0;
3000:   firstSubPoint[0]   = firstSubPoint[dim] + numSubPoints[dim];
3001:   if (dim > 1) {firstSubPoint[dim-1] = firstSubPoint[0]     + numSubPoints[0];}
3002:   if (dim > 2) {firstSubPoint[dim-2] = firstSubPoint[dim-1] + numSubPoints[dim-1];}
3003:   for (d = 0; d <= dim; ++d) {
3004:     DMLabelGetStratumIS(subpointMap, d, &subpointIS[d]);
3005:     if (subpointIS[d]) {ISGetIndices(subpointIS[d], &subpoints[d]);}
3006:   }
3007:   for (d = 0; d <= dim; ++d) {
3008:     for (p = 0; p < numSubPoints[d]; ++p) {
3009:       const PetscInt  point    = subpoints[d][p];
3010:       const PetscInt  subpoint = firstSubPoint[d] + p;
3011:       const PetscInt *cone;
3012:       PetscInt        coneSize, coneSizeNew, c, val;

3014:       DMPlexGetConeSize(dm, point, &coneSize);
3015:       DMPlexSetConeSize(subdm, subpoint, coneSize);
3016:       if (d == dim) {
3017:         DMPlexGetCone(dm, point, &cone);
3018:         for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
3019:           DMLabelGetValue(subpointMap, cone[c], &val);
3020:           if (val >= 0) coneSizeNew++;
3021:         }
3022:         DMPlexSetConeSize(subdm, subpoint, coneSizeNew);
3023:       }
3024:     }
3025:   }
3026:   DMSetUp(subdm);
3027:   /* Set cones */
3028:   DMPlexGetMaxSizes(dm, &maxConeSize, NULL);
3029:   PetscMalloc2(maxConeSize,&coneNew,maxConeSize,&orntNew);
3030:   for (d = 0; d <= dim; ++d) {
3031:     for (p = 0; p < numSubPoints[d]; ++p) {
3032:       const PetscInt  point    = subpoints[d][p];
3033:       const PetscInt  subpoint = firstSubPoint[d] + p;
3034:       const PetscInt *cone, *ornt;
3035:       PetscInt        coneSize, subconeSize, coneSizeNew, c, subc;

3037:       DMPlexGetConeSize(dm, point, &coneSize);
3038:       DMPlexGetConeSize(subdm, subpoint, &subconeSize);
3039:       DMPlexGetCone(dm, point, &cone);
3040:       DMPlexGetConeOrientation(dm, point, &ornt);
3041:       for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
3042:         PetscFindInt(cone[c], numSubPoints[d-1], subpoints[d-1], &subc);
3043:         if (subc >= 0) {
3044:           coneNew[coneSizeNew]   = firstSubPoint[d-1] + subc;
3045:           orntNew[coneSizeNew++] = ornt[c];
3046:         }
3047:       }
3048:       if (coneSizeNew != subconeSize) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of cone points located %d does not match subcone size %d", coneSizeNew, subconeSize);
3049:       DMPlexSetCone(subdm, subpoint, coneNew);
3050:       DMPlexSetConeOrientation(subdm, subpoint, orntNew);
3051:     }
3052:   }
3053:   PetscFree2(coneNew,orntNew);
3054:   DMPlexSymmetrize(subdm);
3055:   DMPlexStratify(subdm);
3056:   /* Build coordinates */
3057:   {
3058:     PetscSection coordSection, subCoordSection;
3059:     Vec          coordinates, subCoordinates;
3060:     PetscScalar *coords, *subCoords;
3061:     PetscInt     numComp, coordSize;

3063:     DMGetCoordinateSection(dm, &coordSection);
3064:     DMGetCoordinatesLocal(dm, &coordinates);
3065:     DMGetCoordinateSection(subdm, &subCoordSection);
3066:     PetscSectionSetNumFields(subCoordSection, 1);
3067:     PetscSectionGetFieldComponents(coordSection, 0, &numComp);
3068:     PetscSectionSetFieldComponents(subCoordSection, 0, numComp);
3069:     PetscSectionSetChart(subCoordSection, firstSubPoint[0], firstSubPoint[0]+numSubPoints[0]);
3070:     for (v = 0; v < numSubPoints[0]; ++v) {
3071:       const PetscInt vertex    = subpoints[0][v];
3072:       const PetscInt subvertex = firstSubPoint[0]+v;
3073:       PetscInt       dof;

3075:       PetscSectionGetDof(coordSection, vertex, &dof);
3076:       PetscSectionSetDof(subCoordSection, subvertex, dof);
3077:       PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);
3078:     }
3079:     PetscSectionSetUp(subCoordSection);
3080:     PetscSectionGetStorageSize(subCoordSection, &coordSize);
3081:     VecCreate(comm, &subCoordinates);
3082:     VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);
3083:     VecSetType(subCoordinates,VECSTANDARD);
3084:     VecGetArray(coordinates,    &coords);
3085:     VecGetArray(subCoordinates, &subCoords);
3086:     for (v = 0; v < numSubPoints[0]; ++v) {
3087:       const PetscInt vertex    = subpoints[0][v];
3088:       const PetscInt subvertex = firstSubPoint[0]+v;
3089:       PetscInt dof, off, sdof, soff, d;

3091:       PetscSectionGetDof(coordSection, vertex, &dof);
3092:       PetscSectionGetOffset(coordSection, vertex, &off);
3093:       PetscSectionGetDof(subCoordSection, subvertex, &sdof);
3094:       PetscSectionGetOffset(subCoordSection, subvertex, &soff);
3095:       if (dof != sdof) SETERRQ4(comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
3096:       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3097:     }
3098:     VecRestoreArray(coordinates,    &coords);
3099:     VecRestoreArray(subCoordinates, &subCoords);
3100:     DMSetCoordinatesLocal(subdm, subCoordinates);
3101:     VecDestroy(&subCoordinates);
3102:   }
3103:   /* Build SF: We need this complexity because subpoints might not be selected on the owning process */
3104:   {
3105:     PetscSF            sfPoint, sfPointSub;
3106:     IS                 subpIS;
3107:     const PetscSFNode *remotePoints;
3108:     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
3109:     const PetscInt    *localPoints, *subpoints;
3110:     PetscInt          *slocalPoints;
3111:     PetscInt           numRoots, numLeaves, numSubpoints = 0, numSubroots, numSubleaves = 0, l, sl, ll, pStart, pEnd, p;
3112:     PetscMPIInt        rank;

3114:     MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);
3115:     DMGetPointSF(dm, &sfPoint);
3116:     DMGetPointSF(subdm, &sfPointSub);
3117:     DMPlexGetChart(dm, &pStart, &pEnd);
3118:     DMPlexGetChart(subdm, NULL, &numSubroots);
3119:     DMPlexCreateSubpointIS(subdm, &subpIS);
3120:     if (subpIS) {
3121:       ISGetIndices(subpIS, &subpoints);
3122:       ISGetLocalSize(subpIS, &numSubpoints);
3123:     }
3124:     PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);
3125:     if (numRoots >= 0) {
3126:       PetscMalloc2(pEnd-pStart,&newLocalPoints,numRoots,&newOwners);
3127:       for (p = 0; p < pEnd-pStart; ++p) {
3128:         newLocalPoints[p].rank  = -2;
3129:         newLocalPoints[p].index = -2;
3130:       }
3131:       /* Set subleaves */
3132:       for (l = 0; l < numLeaves; ++l) {
3133:         const PetscInt point    = localPoints[l];
3134:         const PetscInt subpoint = DMPlexFilterPoint_Internal(point, 0, numSubpoints, subpoints);

3136:         if (subpoint < 0) continue;
3137:         newLocalPoints[point-pStart].rank  = rank;
3138:         newLocalPoints[point-pStart].index = subpoint;
3139:         ++numSubleaves;
3140:       }
3141:       /* Must put in owned subpoints */
3142:       for (p = pStart; p < pEnd; ++p) {
3143:         const PetscInt subpoint = DMPlexFilterPoint_Internal(p, 0, numSubpoints, subpoints);

3145:         if (subpoint < 0) {
3146:           newOwners[p-pStart].rank  = -3;
3147:           newOwners[p-pStart].index = -3;
3148:         } else {
3149:           newOwners[p-pStart].rank  = rank;
3150:           newOwners[p-pStart].index = subpoint;
3151:         }
3152:       }
3153:       PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);
3154:       PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);
3155:       PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints);
3156:       PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints);
3157:       PetscMalloc(numSubleaves * sizeof(PetscInt),    &slocalPoints);
3158:       PetscMalloc(numSubleaves * sizeof(PetscSFNode), &sremotePoints);
3159:       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
3160:         const PetscInt point    = localPoints[l];
3161:         const PetscInt subpoint = DMPlexFilterPoint_Internal(point, 0, numSubpoints, subpoints);

3163:         if (subpoint < 0) continue;
3164:         if (newLocalPoints[point].rank == rank) {++ll; continue;}
3165:         slocalPoints[sl]        = subpoint;
3166:         sremotePoints[sl].rank  = newLocalPoints[point].rank;
3167:         sremotePoints[sl].index = newLocalPoints[point].index;
3168:         if (sremotePoints[sl].rank  < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %d", point);
3169:         if (sremotePoints[sl].index < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %d", point);
3170:         ++sl;
3171:       }
3172:       if (sl + ll != numSubleaves) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %d + %d != %d", sl, ll, numSubleaves);
3173:       PetscFree2(newLocalPoints,newOwners);
3174:       PetscSFSetGraph(sfPointSub, numSubroots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER);
3175:     }
3176:     if (subpIS) {
3177:       ISRestoreIndices(subpIS, &subpoints);
3178:       ISDestroy(&subpIS);
3179:     }
3180:   }
3181:   /* Cleanup */
3182:   for (d = 0; d <= dim; ++d) {
3183:     if (subpointIS[d]) {ISRestoreIndices(subpointIS[d], &subpoints[d]);}
3184:     ISDestroy(&subpointIS[d]);
3185:   }
3186:   PetscFree4(numSubPoints,firstSubPoint,subpointIS,subpoints);
3187:   return(0);
3188: }

3192: /*
3193:   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.

3195:   Input Parameters:
3196: + dm          - The original mesh
3197: . hasLagrange - The mesh has Lagrange unknowns in the cohesive cells
3198: . label       - A label name, or NULL
3199: - value  - A label value

3201:   Output Parameter:
3202: . subdm - The surface mesh

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

3206:   Level: developer

3208: .seealso: DMPlexGetSubpointMap(), DMPlexCreateSubmesh()
3209: */
3210: PetscErrorCode DMPlexCreateCohesiveSubmesh(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM *subdm)
3211: {
3212:   PetscInt       dim, depth;

3218:   DMGetDimension(dm, &dim);
3219:   DMPlexGetDepth(dm, &depth);
3220:   DMCreate(PetscObjectComm((PetscObject)dm), subdm);
3221:   DMSetType(*subdm, DMPLEX);
3222:   DMSetDimension(*subdm, dim-1);
3223:   if (depth == dim) {
3224:     DMPlexCreateCohesiveSubmesh_Interpolated(dm, label, value, *subdm);
3225:   } else {
3226:     DMPlexCreateCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, *subdm);
3227:   }
3228:   return(0);
3229: }

3233: /*@
3234:   DMPlexGetSubpointMap - Returns a DMLabel with point dimension as values

3236:   Input Parameter:
3237: . dm - The submesh DM

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

3242:   Level: developer

3244: .seealso: DMPlexCreateSubmesh(), DMPlexCreateSubpointIS()
3245: @*/
3246: PetscErrorCode DMPlexGetSubpointMap(DM dm, DMLabel *subpointMap)
3247: {
3248:   DM_Plex *mesh = (DM_Plex*) dm->data;

3253:   *subpointMap = mesh->subpointMap;
3254:   return(0);
3255: }

3259: /* Note: Should normally not be called by the user, since it is set in DMPlexCreateSubmesh() */
3260: PetscErrorCode DMPlexSetSubpointMap(DM dm, DMLabel subpointMap)
3261: {
3262:   DM_Plex       *mesh = (DM_Plex *) dm->data;
3263:   DMLabel        tmp;

3268:   tmp  = mesh->subpointMap;
3269:   mesh->subpointMap = subpointMap;
3270:   ++mesh->subpointMap->refct;
3271:   DMLabelDestroy(&tmp);
3272:   return(0);
3273: }

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

3280:   Input Parameter:
3281: . dm - The submesh DM

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

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

3288:   Level: developer

3290: .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointMap()
3291: @*/
3292: PetscErrorCode DMPlexCreateSubpointIS(DM dm, IS *subpointIS)
3293: {
3294:   MPI_Comm        comm;
3295:   DMLabel         subpointMap;
3296:   IS              is;
3297:   const PetscInt *opoints;
3298:   PetscInt       *points, *depths;
3299:   PetscInt        depth, depStart, depEnd, d, pStart, pEnd, p, n, off;
3300:   PetscErrorCode  ierr;

3305:   PetscObjectGetComm((PetscObject)dm,&comm);
3306:   *subpointIS = NULL;
3307:   DMPlexGetSubpointMap(dm, &subpointMap);
3308:   DMPlexGetDepth(dm, &depth);
3309:   if (subpointMap && depth >= 0) {
3310:     DMPlexGetChart(dm, &pStart, &pEnd);
3311:     if (pStart) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Submeshes must start the point numbering at 0, not %d", pStart);
3312:     DMGetWorkArray(dm, depth+1, PETSC_INT, &depths);
3313:     depths[0] = depth;
3314:     depths[1] = 0;
3315:     for(d = 2; d <= depth; ++d) {depths[d] = depth+1 - d;}
3316:     PetscMalloc1(pEnd, &points);
3317:     for(d = 0, off = 0; d <= depth; ++d) {
3318:       const PetscInt dep = depths[d];

3320:       DMPlexGetDepthStratum(dm, dep, &depStart, &depEnd);
3321:       DMLabelGetStratumSize(subpointMap, dep, &n);
3322:       if (((d < 2) && (depth > 1)) || (d == 1)) { /* Only check vertices and cells for now since the map is broken for others */
3323:         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);
3324:       } else {
3325:         if (!n) {
3326:           if (d == 0) {
3327:             /* Missing cells */
3328:             for(p = 0; p < depEnd-depStart; ++p, ++off) points[off] = -1;
3329:           } else {
3330:             /* Missing faces */
3331:             for(p = 0; p < depEnd-depStart; ++p, ++off) points[off] = PETSC_MAX_INT;
3332:           }
3333:         }
3334:       }
3335:       if (n) {
3336:         DMLabelGetStratumIS(subpointMap, dep, &is);
3337:         ISGetIndices(is, &opoints);
3338:         for(p = 0; p < n; ++p, ++off) points[off] = opoints[p];
3339:         ISRestoreIndices(is, &opoints);
3340:         ISDestroy(&is);
3341:       }
3342:     }
3343:     DMRestoreWorkArray(dm, depth+1, PETSC_INT, &depths);
3344:     if (off != pEnd) SETERRQ2(comm, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %d should be %d", off, pEnd);
3345:     ISCreateGeneral(PETSC_COMM_SELF, pEnd, points, PETSC_OWN_POINTER, subpointIS);
3346:   }
3347:   return(0);
3348: }