xref: /petsc/src/dm/impls/plex/generators/tetgen/tetgenerate.cxx (revision 5552b385b77b214b234683fbe1b434751e6350f0)
13a074057SBarry Smith #include <petsc/private/dmpleximpl.h> /*I      "petscdmplex.h"   I*/
23a074057SBarry Smith 
30fdc7489SMatthew Knepley #ifdef PETSC_HAVE_EGADS
40fdc7489SMatthew Knepley   #include <egads.h>
5*5552b385SBrandon   #include <egads_lite.h>
60fdc7489SMatthew Knepley #endif
70fdc7489SMatthew Knepley 
8b83bf3d7SVaclav Hapla #if defined(PETSC_HAVE_TETGEN_TETLIBRARY_NEEDED)
9b83bf3d7SVaclav Hapla   #define TETLIBRARY
10b83bf3d7SVaclav Hapla #endif
11f22e26b7SPierre Jolivet #if defined(__clang__)
12f22e26b7SPierre Jolivet   #pragma clang diagnostic push
13f22e26b7SPierre Jolivet   #pragma clang diagnostic ignored "-Wunused-parameter"
14f22e26b7SPierre Jolivet   #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
15f22e26b7SPierre Jolivet #elif defined(__GNUC__) || defined(__GNUG__)
16f22e26b7SPierre Jolivet   #pragma GCC diagnostic push
17f22e26b7SPierre Jolivet   #pragma GCC diagnostic ignored "-Wunused-parameter"
18f22e26b7SPierre Jolivet #endif
193a074057SBarry Smith #include <tetgen.h>
20f22e26b7SPierre Jolivet #if defined(__clang__)
21f22e26b7SPierre Jolivet   #pragma clang diagnostic pop
22f22e26b7SPierre Jolivet #elif defined(__GNUC__) || defined(__GNUG__)
23f22e26b7SPierre Jolivet   #pragma GCC diagnostic pop
24f22e26b7SPierre Jolivet #endif
253a074057SBarry Smith 
263a074057SBarry Smith /* This is to fix the tetrahedron orientation from TetGen */
27d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexInvertCells_Tetgen(PetscInt numCells, PetscInt numCorners, PetscInt cells[])
28d71ae5a4SJacob Faibussowitsch {
293a074057SBarry Smith   PetscInt bound = numCells * numCorners, coff;
303a074057SBarry Smith 
313a074057SBarry Smith   PetscFunctionBegin;
329371c9d4SSatish Balay #define SWAP(a, b) \
339371c9d4SSatish Balay   do { \
349371c9d4SSatish Balay     PetscInt tmp = (a); \
359371c9d4SSatish Balay     (a)          = (b); \
369371c9d4SSatish Balay     (b)          = tmp; \
379371c9d4SSatish Balay   } while (0)
3896ca5757SLisandro Dalcin   for (coff = 0; coff < bound; coff += numCorners) SWAP(cells[coff], cells[coff + 1]);
3996ca5757SLisandro Dalcin #undef SWAP
403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
413a074057SBarry Smith }
423a074057SBarry Smith 
43d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode DMPlexGenerate_Tetgen(DM boundary, PetscBool interpolate, DM *dm)
44d71ae5a4SJacob Faibussowitsch {
453a074057SBarry Smith   MPI_Comm               comm;
463a074057SBarry Smith   const PetscInt         dim = 3;
473a074057SBarry Smith   ::tetgenio             in;
483a074057SBarry Smith   ::tetgenio             out;
499318fe57SMatthew G. Knepley   PetscContainer         modelObj;
500fdc7489SMatthew Knepley   DMUniversalLabel       universal;
51af226901SMatthew G. Knepley   PetscInt               vStart, vEnd, v, eStart, eEnd, e, fStart, fEnd, f, defVal;
520fdc7489SMatthew Knepley   DMPlexInterpolatedFlag isInterpolated;
533a074057SBarry Smith   PetscMPIInt            rank;
543a074057SBarry Smith 
553a074057SBarry Smith   PetscFunctionBegin;
569566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)boundary, &comm));
579566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
589566063dSJacob Faibussowitsch   PetscCall(DMPlexIsInterpolatedCollective(boundary, &isInterpolated));
599566063dSJacob Faibussowitsch   PetscCall(DMUniversalLabelCreate(boundary, &universal));
60af226901SMatthew G. Knepley   PetscCall(DMLabelGetDefaultValue(universal->label, &defVal));
613a074057SBarry Smith 
629566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(boundary, 0, &vStart, &vEnd));
633a074057SBarry Smith   in.numberofpoints = vEnd - vStart;
643a074057SBarry Smith   if (in.numberofpoints > 0) {
653a074057SBarry Smith     PetscSection       coordSection;
663a074057SBarry Smith     Vec                coordinates;
670fdc7489SMatthew Knepley     const PetscScalar *array;
683a074057SBarry Smith 
693a074057SBarry Smith     in.pointlist       = new double[in.numberofpoints * dim];
703a074057SBarry Smith     in.pointmarkerlist = new int[in.numberofpoints];
713a074057SBarry Smith 
7241e9d8b5SMatthew G. Knepley     PetscCall(PetscArrayzero(in.pointmarkerlist, (size_t)in.numberofpoints));
739566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(boundary, &coordinates));
749566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(boundary, &coordSection));
759566063dSJacob Faibussowitsch     PetscCall(VecGetArrayRead(coordinates, &array));
763a074057SBarry Smith     for (v = vStart; v < vEnd; ++v) {
773a074057SBarry Smith       const PetscInt idx = v - vStart;
780fdc7489SMatthew Knepley       PetscInt       off, d, val;
793a074057SBarry Smith 
809566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, v, &off));
813a074057SBarry Smith       for (d = 0; d < dim; ++d) in.pointlist[idx * dim + d] = PetscRealPart(array[off + d]);
829566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(universal->label, v, &val));
83af226901SMatthew G. Knepley       if (val != defVal) in.pointmarkerlist[idx] = (int)val;
843a074057SBarry Smith     }
859566063dSJacob Faibussowitsch     PetscCall(VecRestoreArrayRead(coordinates, &array));
863a074057SBarry Smith   }
873a074057SBarry Smith 
889566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(boundary, 1, &eStart, &eEnd));
890fdc7489SMatthew Knepley   in.numberofedges = eEnd - eStart;
900fdc7489SMatthew Knepley   if (isInterpolated == DMPLEX_INTERPOLATED_FULL && in.numberofedges > 0) {
910fdc7489SMatthew Knepley     in.edgelist       = new int[in.numberofedges * 2];
920fdc7489SMatthew Knepley     in.edgemarkerlist = new int[in.numberofedges];
930fdc7489SMatthew Knepley     for (e = eStart; e < eEnd; ++e) {
940fdc7489SMatthew Knepley       const PetscInt  idx = e - eStart;
950fdc7489SMatthew Knepley       const PetscInt *cone;
960fdc7489SMatthew Knepley       PetscInt        coneSize, val;
970fdc7489SMatthew Knepley 
989566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(boundary, e, &coneSize));
999566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(boundary, e, &cone));
1000fdc7489SMatthew Knepley       in.edgelist[idx * 2]     = cone[0] - vStart;
1010fdc7489SMatthew Knepley       in.edgelist[idx * 2 + 1] = cone[1] - vStart;
1020fdc7489SMatthew Knepley 
1039566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(universal->label, e, &val));
104af226901SMatthew G. Knepley       if (val != defVal) in.edgemarkerlist[idx] = (int)val;
1050fdc7489SMatthew Knepley     }
1060fdc7489SMatthew Knepley   }
1070fdc7489SMatthew Knepley 
1089566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(boundary, 0, &fStart, &fEnd));
1093a074057SBarry Smith   in.numberoffacets = fEnd - fStart;
1103a074057SBarry Smith   if (in.numberoffacets > 0) {
1113a074057SBarry Smith     in.facetlist       = new tetgenio::facet[in.numberoffacets];
1123a074057SBarry Smith     in.facetmarkerlist = new int[in.numberoffacets];
1133a074057SBarry Smith     for (f = fStart; f < fEnd; ++f) {
1143a074057SBarry Smith       const PetscInt idx    = f - fStart;
115f22e26b7SPierre Jolivet       PetscInt      *points = nullptr, numPoints, p, numVertices = 0, v, val = -1;
1163a074057SBarry Smith 
1173a074057SBarry Smith       in.facetlist[idx].numberofpolygons = 1;
1183a074057SBarry Smith       in.facetlist[idx].polygonlist      = new tetgenio::polygon[in.facetlist[idx].numberofpolygons];
1193a074057SBarry Smith       in.facetlist[idx].numberofholes    = 0;
120f22e26b7SPierre Jolivet       in.facetlist[idx].holelist         = nullptr;
1213a074057SBarry Smith 
1229566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(boundary, f, PETSC_TRUE, &numPoints, &points));
1233a074057SBarry Smith       for (p = 0; p < numPoints * 2; p += 2) {
1243a074057SBarry Smith         const PetscInt point = points[p];
1253a074057SBarry Smith         if ((point >= vStart) && (point < vEnd)) points[numVertices++] = point;
1263a074057SBarry Smith       }
1273a074057SBarry Smith 
1283a074057SBarry Smith       tetgenio::polygon *poly = in.facetlist[idx].polygonlist;
1293a074057SBarry Smith       poly->numberofvertices  = numVertices;
1303a074057SBarry Smith       poly->vertexlist        = new int[poly->numberofvertices];
1313a074057SBarry Smith       for (v = 0; v < numVertices; ++v) {
1323a074057SBarry Smith         const PetscInt vIdx = points[v] - vStart;
1333a074057SBarry Smith         poly->vertexlist[v] = vIdx;
1343a074057SBarry Smith       }
1359566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(universal->label, f, &val));
136af226901SMatthew G. Knepley       if (val != defVal) in.facetmarkerlist[idx] = (int)val;
1379566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(boundary, f, PETSC_TRUE, &numPoints, &points));
1383a074057SBarry Smith     }
1393a074057SBarry Smith   }
140dd400576SPatrick Sanan   if (rank == 0) {
1410fdc7489SMatthew Knepley     DM_Plex *mesh = (DM_Plex *)boundary->data;
1423a074057SBarry Smith     char     args[32];
1433a074057SBarry Smith 
1443a074057SBarry Smith     /* Take away 'Q' for verbose output */
1450fdc7489SMatthew Knepley #ifdef PETSC_HAVE_EGADS
146*5552b385SBrandon     PetscCall(PetscStrncpy(args, "pYqezQY", sizeof(args)));
1470fdc7489SMatthew Knepley #else
148c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(args, "pqezQ", sizeof(args)));
1490fdc7489SMatthew Knepley #endif
1509371c9d4SSatish Balay     if (mesh->tetgenOpts) {
1519371c9d4SSatish Balay       ::tetrahedralize(mesh->tetgenOpts, &in, &out);
1529371c9d4SSatish Balay     } else {
1539371c9d4SSatish Balay       ::tetrahedralize(args, &in, &out);
1549371c9d4SSatish Balay     }
1553a074057SBarry Smith   }
1563a074057SBarry Smith   {
1573a074057SBarry Smith     const PetscInt numCorners  = 4;
1583a074057SBarry Smith     const PetscInt numCells    = out.numberoftetrahedra;
1593a074057SBarry Smith     const PetscInt numVertices = out.numberofpoints;
160f22e26b7SPierre Jolivet     PetscReal     *meshCoords  = nullptr;
161f22e26b7SPierre Jolivet     PetscInt      *cells       = nullptr;
162a4a685f2SJacob Faibussowitsch 
163a4a685f2SJacob Faibussowitsch     if (sizeof(PetscReal) == sizeof(out.pointlist[0])) {
164a4a685f2SJacob Faibussowitsch       meshCoords = (PetscReal *)out.pointlist;
165a4a685f2SJacob Faibussowitsch     } else {
166a4a685f2SJacob Faibussowitsch       PetscInt i;
167a4a685f2SJacob Faibussowitsch 
168a4a685f2SJacob Faibussowitsch       meshCoords = new PetscReal[dim * numVertices];
1690fdc7489SMatthew Knepley       for (i = 0; i < dim * numVertices; ++i) meshCoords[i] = (PetscReal)out.pointlist[i];
170a4a685f2SJacob Faibussowitsch     }
171a4a685f2SJacob Faibussowitsch     if (sizeof(PetscInt) == sizeof(out.tetrahedronlist[0])) {
172a4a685f2SJacob Faibussowitsch       cells = (PetscInt *)out.tetrahedronlist;
173a4a685f2SJacob Faibussowitsch     } else {
174a4a685f2SJacob Faibussowitsch       PetscInt i;
175a4a685f2SJacob Faibussowitsch 
176a4a685f2SJacob Faibussowitsch       cells = new PetscInt[numCells * numCorners];
1770fdc7489SMatthew Knepley       for (i = 0; i < numCells * numCorners; i++) cells[i] = (PetscInt)out.tetrahedronlist[i];
178a4a685f2SJacob Faibussowitsch     }
1793a074057SBarry Smith 
1809566063dSJacob Faibussowitsch     PetscCall(DMPlexInvertCells_Tetgen(numCells, numCorners, cells));
1819566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromCellListPetsc(comm, dim, numCells, numVertices, numCorners, interpolate, cells, dim, meshCoords, dm));
1820fdc7489SMatthew Knepley 
1833a074057SBarry Smith     /* Set labels */
1849566063dSJacob Faibussowitsch     PetscCall(DMUniversalLabelCreateLabels(universal, PETSC_TRUE, *dm));
1853a074057SBarry Smith     for (v = 0; v < numVertices; ++v) {
18648a46eb9SPierre Jolivet       if (out.pointmarkerlist[v]) PetscCall(DMUniversalLabelSetLabelValue(universal, *dm, PETSC_TRUE, v + numCells, out.pointmarkerlist[v]));
1873a074057SBarry Smith     }
1883a074057SBarry Smith     if (interpolate) {
1893a074057SBarry Smith       PetscInt e;
1903a074057SBarry Smith 
1913a074057SBarry Smith       for (e = 0; e < out.numberofedges; e++) {
1923a074057SBarry Smith         if (out.edgemarkerlist[e]) {
1933a074057SBarry Smith           const PetscInt  vertices[2] = {out.edgelist[e * 2 + 0] + numCells, out.edgelist[e * 2 + 1] + numCells};
1943a074057SBarry Smith           const PetscInt *edges;
1953a074057SBarry Smith           PetscInt        numEdges;
1963a074057SBarry Smith 
1979566063dSJacob Faibussowitsch           PetscCall(DMPlexGetJoin(*dm, 2, vertices, &numEdges, &edges));
19863a3b9bcSJacob Faibussowitsch           PetscCheck(numEdges == 1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Two vertices must cover only one edge, not %" PetscInt_FMT, numEdges);
1999566063dSJacob Faibussowitsch           PetscCall(DMUniversalLabelSetLabelValue(universal, *dm, PETSC_TRUE, edges[0], out.edgemarkerlist[e]));
2009566063dSJacob Faibussowitsch           PetscCall(DMPlexRestoreJoin(*dm, 2, vertices, &numEdges, &edges));
2013a074057SBarry Smith         }
2023a074057SBarry Smith       }
2033a074057SBarry Smith       for (f = 0; f < out.numberoftrifaces; f++) {
2043a074057SBarry Smith         if (out.trifacemarkerlist[f]) {
2053a074057SBarry Smith           const PetscInt  vertices[3] = {out.trifacelist[f * 3 + 0] + numCells, out.trifacelist[f * 3 + 1] + numCells, out.trifacelist[f * 3 + 2] + numCells};
2063a074057SBarry Smith           const PetscInt *faces;
2073a074057SBarry Smith           PetscInt        numFaces;
2083a074057SBarry Smith 
2099566063dSJacob Faibussowitsch           PetscCall(DMPlexGetFullJoin(*dm, 3, vertices, &numFaces, &faces));
21063a3b9bcSJacob Faibussowitsch           PetscCheck(numFaces == 1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Three vertices must cover only one face, not %" PetscInt_FMT, numFaces);
2119566063dSJacob Faibussowitsch           PetscCall(DMUniversalLabelSetLabelValue(universal, *dm, PETSC_TRUE, faces[0], out.trifacemarkerlist[f]));
2129566063dSJacob Faibussowitsch           PetscCall(DMPlexRestoreJoin(*dm, 3, vertices, &numFaces, &faces));
2133a074057SBarry Smith         }
2143a074057SBarry Smith       }
2153a074057SBarry Smith     }
2160fdc7489SMatthew Knepley 
2179566063dSJacob Faibussowitsch     PetscCall(PetscObjectQuery((PetscObject)boundary, "EGADS Model", (PetscObject *)&modelObj));
218*5552b385SBrandon     if (!modelObj) { PetscCall(PetscObjectQuery((PetscObject)boundary, "EGADSlite Model", (PetscObject *)&modelObj)); }
219*5552b385SBrandon 
2209318fe57SMatthew G. Knepley     if (modelObj) {
2210fdc7489SMatthew Knepley #ifdef PETSC_HAVE_EGADS
2220fdc7489SMatthew Knepley       DMLabel   bodyLabel;
2230fdc7489SMatthew Knepley       PetscInt  cStart, cEnd, c, eStart, eEnd, fStart, fEnd;
224c1cad2e7SMatthew G. Knepley       PetscBool islite = PETSC_FALSE;
2250fdc7489SMatthew Knepley       ego      *bodies;
2260fdc7489SMatthew Knepley       ego       model, geom;
2270fdc7489SMatthew Knepley       int       Nb, oclass, mtype, *senses;
2280fdc7489SMatthew Knepley 
229*5552b385SBrandon       PetscCall(DMPlexCopyEGADSInfo_Internal(boundary, *dm));
230*5552b385SBrandon 
231*5552b385SBrandon       // Get Attached EGADS Model from Original DMPlex
2329566063dSJacob Faibussowitsch       PetscCall(PetscObjectQuery((PetscObject)boundary, "EGADS Model", (PetscObject *)&modelObj));
233c1cad2e7SMatthew G. Knepley       if (modelObj) {
2349566063dSJacob Faibussowitsch         PetscCall(PetscContainerGetPointer(modelObj, (void **)&model));
235f22e26b7SPierre Jolivet         PetscCall(EG_getTopology(model, &geom, &oclass, &mtype, nullptr, &Nb, &bodies, &senses));
236c1cad2e7SMatthew G. Knepley       } else {
237*5552b385SBrandon         PetscCall(PetscObjectQuery((PetscObject)boundary, "EGADSlite Model", (PetscObject *)&modelObj));
238c1cad2e7SMatthew G. Knepley         if (modelObj) {
2399566063dSJacob Faibussowitsch           PetscCall(PetscContainerGetPointer(modelObj, (void **)&model));
240f22e26b7SPierre Jolivet           PetscCall(EGlite_getTopology(model, &geom, &oclass, &mtype, nullptr, &Nb, &bodies, &senses));
241c1cad2e7SMatthew G. Knepley           islite = PETSC_TRUE;
242c1cad2e7SMatthew G. Knepley         }
243c1cad2e7SMatthew G. Knepley       }
244c1cad2e7SMatthew G. Knepley       if (!modelObj) goto skip_egads;
2450fdc7489SMatthew Knepley 
2460fdc7489SMatthew Knepley       /* Set Cell Labels */
2479566063dSJacob Faibussowitsch       PetscCall(DMGetLabel(*dm, "EGADS Body ID", &bodyLabel));
2489566063dSJacob Faibussowitsch       PetscCall(DMPlexGetHeightStratum(*dm, 0, &cStart, &cEnd));
2499566063dSJacob Faibussowitsch       PetscCall(DMPlexGetHeightStratum(*dm, 1, &fStart, &fEnd));
2509566063dSJacob Faibussowitsch       PetscCall(DMPlexGetDepthStratum(*dm, 1, &eStart, &eEnd));
2510fdc7489SMatthew Knepley 
2520fdc7489SMatthew Knepley       for (c = cStart; c < cEnd; ++c) {
2530fdc7489SMatthew Knepley         PetscReal centroid[3] = {0., 0., 0.};
2540fdc7489SMatthew Knepley         PetscInt  b;
2550fdc7489SMatthew Knepley 
25635cb6cd3SPierre Jolivet         /* Determine what body the cell's centroid is located in */
2570fdc7489SMatthew Knepley         if (!interpolate) {
2580fdc7489SMatthew Knepley           PetscSection coordSection;
2590fdc7489SMatthew Knepley           Vec          coordinates;
260f22e26b7SPierre Jolivet           PetscScalar *coords = nullptr;
2610fdc7489SMatthew Knepley           PetscInt     coordSize, s, d;
2620fdc7489SMatthew Knepley 
2639566063dSJacob Faibussowitsch           PetscCall(DMGetCoordinatesLocal(*dm, &coordinates));
2649566063dSJacob Faibussowitsch           PetscCall(DMGetCoordinateSection(*dm, &coordSection));
2659566063dSJacob Faibussowitsch           PetscCall(DMPlexVecGetClosure(*dm, coordSection, coordinates, c, &coordSize, &coords));
2669371c9d4SSatish Balay           for (s = 0; s < coordSize; ++s)
2679371c9d4SSatish Balay             for (d = 0; d < dim; ++d) centroid[d] += coords[s * dim + d];
2689566063dSJacob Faibussowitsch           PetscCall(DMPlexVecRestoreClosure(*dm, coordSection, coordinates, c, &coordSize, &coords));
269f22e26b7SPierre Jolivet         } else PetscCall(DMPlexComputeCellGeometryFVM(*dm, c, nullptr, centroid, nullptr));
2700fdc7489SMatthew Knepley         for (b = 0; b < Nb; ++b) {
2719371c9d4SSatish Balay           if (islite) {
2729371c9d4SSatish Balay             if (EGlite_inTopology(bodies[b], centroid) == EGADS_SUCCESS) break;
2739371c9d4SSatish Balay           } else {
2749371c9d4SSatish Balay             if (EG_inTopology(bodies[b], centroid) == EGADS_SUCCESS) break;
2759371c9d4SSatish Balay           }
2760fdc7489SMatthew Knepley         }
2770fdc7489SMatthew Knepley         if (b < Nb) {
2780fdc7489SMatthew Knepley           PetscInt  cval    = b, eVal, fVal;
279f22e26b7SPierre Jolivet           PetscInt *closure = nullptr, Ncl, cl;
2800fdc7489SMatthew Knepley 
2819566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(bodyLabel, c, cval));
2829566063dSJacob Faibussowitsch           PetscCall(DMPlexGetTransitiveClosure(*dm, c, PETSC_TRUE, &Ncl, &closure));
2830fdc7489SMatthew Knepley           for (cl = 0; cl < Ncl; cl += 2) {
2840fdc7489SMatthew Knepley             const PetscInt p = closure[cl];
2850fdc7489SMatthew Knepley 
2860fdc7489SMatthew Knepley             if (p >= eStart && p < eEnd) {
2879566063dSJacob Faibussowitsch               PetscCall(DMLabelGetValue(bodyLabel, p, &eVal));
2889566063dSJacob Faibussowitsch               if (eVal < 0) PetscCall(DMLabelSetValue(bodyLabel, p, cval));
2890fdc7489SMatthew Knepley             }
2900fdc7489SMatthew Knepley             if (p >= fStart && p < fEnd) {
2919566063dSJacob Faibussowitsch               PetscCall(DMLabelGetValue(bodyLabel, p, &fVal));
2929566063dSJacob Faibussowitsch               if (fVal < 0) PetscCall(DMLabelSetValue(bodyLabel, p, cval));
2930fdc7489SMatthew Knepley             }
2940fdc7489SMatthew Knepley           }
2959566063dSJacob Faibussowitsch           PetscCall(DMPlexRestoreTransitiveClosure(*dm, c, PETSC_TRUE, &Ncl, &closure));
2960fdc7489SMatthew Knepley         }
2970fdc7489SMatthew Knepley       }
298c1cad2e7SMatthew G. Knepley     skip_egads:;
2990fdc7489SMatthew Knepley #endif
3009318fe57SMatthew G. Knepley     }
3019566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementUniform(*dm, PETSC_FALSE));
3023a074057SBarry Smith   }
3039566063dSJacob Faibussowitsch   PetscCall(DMUniversalLabelDestroy(&universal));
3043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3053a074057SBarry Smith }
3063a074057SBarry Smith 
307d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode DMPlexRefine_Tetgen(DM dm, double *maxVolumes, DM *dmRefined)
308d71ae5a4SJacob Faibussowitsch {
3093a074057SBarry Smith   MPI_Comm               comm;
3103a074057SBarry Smith   const PetscInt         dim = 3;
3113a074057SBarry Smith   ::tetgenio             in;
3123a074057SBarry Smith   ::tetgenio             out;
3139318fe57SMatthew G. Knepley   PetscContainer         modelObj;
3140fdc7489SMatthew Knepley   DMUniversalLabel       universal;
315af226901SMatthew G. Knepley   PetscInt               vStart, vEnd, v, eStart, eEnd, e, fStart, fEnd, f, cStart, cEnd, c, defVal;
3160fdc7489SMatthew Knepley   DMPlexInterpolatedFlag isInterpolated;
3173a074057SBarry Smith   PetscMPIInt            rank;
3183a074057SBarry Smith 
3193a074057SBarry Smith   PetscFunctionBegin;
3209566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
3219566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
3229566063dSJacob Faibussowitsch   PetscCall(DMPlexIsInterpolatedCollective(dm, &isInterpolated));
3239566063dSJacob Faibussowitsch   PetscCall(DMUniversalLabelCreate(dm, &universal));
324af226901SMatthew G. Knepley   PetscCall(DMLabelGetDefaultValue(universal->label, &defVal));
3253a074057SBarry Smith 
3269566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
3273a074057SBarry Smith   in.numberofpoints = vEnd - vStart;
3283a074057SBarry Smith   if (in.numberofpoints > 0) {
3293a074057SBarry Smith     PetscSection coordSection;
3303a074057SBarry Smith     Vec          coordinates;
3313a074057SBarry Smith     PetscScalar *array;
3323a074057SBarry Smith 
3333a074057SBarry Smith     in.pointlist       = new double[in.numberofpoints * dim];
3343a074057SBarry Smith     in.pointmarkerlist = new int[in.numberofpoints];
3353a074057SBarry Smith 
33641e9d8b5SMatthew G. Knepley     PetscCall(PetscArrayzero(in.pointmarkerlist, (size_t)in.numberofpoints));
3379566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
3389566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
3399566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &array));
3403a074057SBarry Smith     for (v = vStart; v < vEnd; ++v) {
3413a074057SBarry Smith       const PetscInt idx = v - vStart;
3420fdc7489SMatthew Knepley       PetscInt       off, d, val;
3433a074057SBarry Smith 
3449566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, v, &off));
3453a074057SBarry Smith       for (d = 0; d < dim; ++d) in.pointlist[idx * dim + d] = PetscRealPart(array[off + d]);
3469566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(universal->label, v, &val));
347af226901SMatthew G. Knepley       if (val != defVal) in.pointmarkerlist[idx] = (int)val;
3483a074057SBarry Smith     }
3499566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &array));
3503a074057SBarry Smith   }
3513a074057SBarry Smith 
3529566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 1, &eStart, &eEnd));
3530fdc7489SMatthew Knepley   in.numberofedges = eEnd - eStart;
3540fdc7489SMatthew Knepley   if (isInterpolated == DMPLEX_INTERPOLATED_FULL && in.numberofedges > 0) {
3550fdc7489SMatthew Knepley     in.edgelist       = new int[in.numberofedges * 2];
3560fdc7489SMatthew Knepley     in.edgemarkerlist = new int[in.numberofedges];
3570fdc7489SMatthew Knepley     for (e = eStart; e < eEnd; ++e) {
3580fdc7489SMatthew Knepley       const PetscInt  idx = e - eStart;
3590fdc7489SMatthew Knepley       const PetscInt *cone;
3600fdc7489SMatthew Knepley       PetscInt        coneSize, val;
3610fdc7489SMatthew Knepley 
3629566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, e, &coneSize));
3639566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, e, &cone));
3640fdc7489SMatthew Knepley       in.edgelist[idx * 2]     = cone[0] - vStart;
3650fdc7489SMatthew Knepley       in.edgelist[idx * 2 + 1] = cone[1] - vStart;
3660fdc7489SMatthew Knepley 
3679566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(universal->label, e, &val));
368af226901SMatthew G. Knepley       if (val != defVal) in.edgemarkerlist[idx] = (int)val;
3690fdc7489SMatthew Knepley     }
3700fdc7489SMatthew Knepley   }
3710fdc7489SMatthew Knepley 
3729566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
3730fdc7489SMatthew Knepley   in.numberoffacets = fEnd - fStart;
3740fdc7489SMatthew Knepley   if (isInterpolated == DMPLEX_INTERPOLATED_FULL && in.numberoffacets > 0) {
3750fdc7489SMatthew Knepley     in.facetlist       = new tetgenio::facet[in.numberoffacets];
3760fdc7489SMatthew Knepley     in.facetmarkerlist = new int[in.numberoffacets];
3770fdc7489SMatthew Knepley     for (f = fStart; f < fEnd; ++f) {
3780fdc7489SMatthew Knepley       const PetscInt idx    = f - fStart;
379f22e26b7SPierre Jolivet       PetscInt      *points = nullptr, numPoints, p, numVertices = 0, v, val;
3800fdc7489SMatthew Knepley 
3810fdc7489SMatthew Knepley       in.facetlist[idx].numberofpolygons = 1;
3820fdc7489SMatthew Knepley       in.facetlist[idx].polygonlist      = new tetgenio::polygon[in.facetlist[idx].numberofpolygons];
3830fdc7489SMatthew Knepley       in.facetlist[idx].numberofholes    = 0;
384f22e26b7SPierre Jolivet       in.facetlist[idx].holelist         = nullptr;
3850fdc7489SMatthew Knepley 
3869566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, f, PETSC_TRUE, &numPoints, &points));
3870fdc7489SMatthew Knepley       for (p = 0; p < numPoints * 2; p += 2) {
3880fdc7489SMatthew Knepley         const PetscInt point = points[p];
3890fdc7489SMatthew Knepley         if ((point >= vStart) && (point < vEnd)) points[numVertices++] = point;
3900fdc7489SMatthew Knepley       }
3910fdc7489SMatthew Knepley 
3920fdc7489SMatthew Knepley       tetgenio::polygon *poly = in.facetlist[idx].polygonlist;
3930fdc7489SMatthew Knepley       poly->numberofvertices  = numVertices;
3940fdc7489SMatthew Knepley       poly->vertexlist        = new int[poly->numberofvertices];
3950fdc7489SMatthew Knepley       for (v = 0; v < numVertices; ++v) {
3960fdc7489SMatthew Knepley         const PetscInt vIdx = points[v] - vStart;
3970fdc7489SMatthew Knepley         poly->vertexlist[v] = vIdx;
3980fdc7489SMatthew Knepley       }
3990fdc7489SMatthew Knepley 
4009566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(universal->label, f, &val));
401af226901SMatthew G. Knepley       if (val != defVal) in.facetmarkerlist[idx] = (int)val;
4020fdc7489SMatthew Knepley 
4039566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, f, PETSC_TRUE, &numPoints, &points));
4040fdc7489SMatthew Knepley     }
4050fdc7489SMatthew Knepley   }
4060fdc7489SMatthew Knepley 
4079566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
4083a074057SBarry Smith   in.numberofcorners       = 4;
4093a074057SBarry Smith   in.numberoftetrahedra    = cEnd - cStart;
4103a074057SBarry Smith   in.tetrahedronvolumelist = (double *)maxVolumes;
4113a074057SBarry Smith   if (in.numberoftetrahedra > 0) {
4123a074057SBarry Smith     in.tetrahedronlist = new int[in.numberoftetrahedra * in.numberofcorners];
4133a074057SBarry Smith     for (c = cStart; c < cEnd; ++c) {
4143a074057SBarry Smith       const PetscInt idx     = c - cStart;
415f22e26b7SPierre Jolivet       PetscInt      *closure = nullptr;
4163a074057SBarry Smith       PetscInt       closureSize;
4173a074057SBarry Smith 
4189566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure));
41963a3b9bcSJacob Faibussowitsch       PetscCheck(!(closureSize != 5) || !(closureSize != 15), comm, PETSC_ERR_ARG_WRONG, "Mesh has cell which is not a tetrahedron, %" PetscInt_FMT " vertices in closure", closureSize);
4200fdc7489SMatthew Knepley       for (v = 0; v < 4; ++v) in.tetrahedronlist[idx * in.numberofcorners + v] = closure[(v + closureSize - 4) * 2] - vStart;
4219566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure));
4223a074057SBarry Smith     }
4233a074057SBarry Smith   }
4240fdc7489SMatthew Knepley 
425dd400576SPatrick Sanan   if (rank == 0) {
4263a074057SBarry Smith     char args[32];
4273a074057SBarry Smith 
4283a074057SBarry Smith     /* Take away 'Q' for verbose output */
429c6a7a370SJeremy L Thompson     PetscCall(PetscStrncpy(args, "qezQra", sizeof(args)));
4303a074057SBarry Smith     ::tetrahedralize(args, &in, &out);
4313a074057SBarry Smith   }
4323a074057SBarry Smith 
433f22e26b7SPierre Jolivet   in.tetrahedronvolumelist = nullptr;
4343a074057SBarry Smith   {
4353a074057SBarry Smith     const PetscInt numCorners  = 4;
4363a074057SBarry Smith     const PetscInt numCells    = out.numberoftetrahedra;
4373a074057SBarry Smith     const PetscInt numVertices = out.numberofpoints;
438f22e26b7SPierre Jolivet     PetscReal     *meshCoords  = nullptr;
439f22e26b7SPierre Jolivet     PetscInt      *cells       = nullptr;
4400fdc7489SMatthew Knepley     PetscBool      interpolate = isInterpolated == DMPLEX_INTERPOLATED_FULL ? PETSC_TRUE : PETSC_FALSE;
4413a074057SBarry Smith 
442a4a685f2SJacob Faibussowitsch     if (sizeof(PetscReal) == sizeof(out.pointlist[0])) {
443a4a685f2SJacob Faibussowitsch       meshCoords = (PetscReal *)out.pointlist;
444a4a685f2SJacob Faibussowitsch     } else {
445a4a685f2SJacob Faibussowitsch       PetscInt i;
446a4a685f2SJacob Faibussowitsch 
447a4a685f2SJacob Faibussowitsch       meshCoords = new PetscReal[dim * numVertices];
4480fdc7489SMatthew Knepley       for (i = 0; i < dim * numVertices; ++i) meshCoords[i] = (PetscReal)out.pointlist[i];
449a4a685f2SJacob Faibussowitsch     }
450a4a685f2SJacob Faibussowitsch     if (sizeof(PetscInt) == sizeof(out.tetrahedronlist[0])) {
451a4a685f2SJacob Faibussowitsch       cells = (PetscInt *)out.tetrahedronlist;
452a4a685f2SJacob Faibussowitsch     } else {
453a4a685f2SJacob Faibussowitsch       PetscInt i;
454a4a685f2SJacob Faibussowitsch 
455a4a685f2SJacob Faibussowitsch       cells = new PetscInt[numCells * numCorners];
4560fdc7489SMatthew Knepley       for (i = 0; i < numCells * numCorners; ++i) cells[i] = (PetscInt)out.tetrahedronlist[i];
457a4a685f2SJacob Faibussowitsch     }
458a4a685f2SJacob Faibussowitsch 
4599566063dSJacob Faibussowitsch     PetscCall(DMPlexInvertCells_Tetgen(numCells, numCorners, cells));
4609566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromCellListPetsc(comm, dim, numCells, numVertices, numCorners, interpolate, cells, dim, meshCoords, dmRefined));
461ad540459SPierre Jolivet     if (sizeof(PetscReal) != sizeof(out.pointlist[0])) delete[] meshCoords;
462ad540459SPierre Jolivet     if (sizeof(PetscInt) != sizeof(out.tetrahedronlist[0])) delete[] cells;
4630fdc7489SMatthew Knepley 
4643a074057SBarry Smith     /* Set labels */
4659566063dSJacob Faibussowitsch     PetscCall(DMUniversalLabelCreateLabels(universal, PETSC_TRUE, *dmRefined));
4663a074057SBarry Smith     for (v = 0; v < numVertices; ++v) {
46748a46eb9SPierre Jolivet       if (out.pointmarkerlist[v]) PetscCall(DMUniversalLabelSetLabelValue(universal, *dmRefined, PETSC_TRUE, v + numCells, out.pointmarkerlist[v]));
4683a074057SBarry Smith     }
4693a074057SBarry Smith     if (interpolate) {
4700fdc7489SMatthew Knepley       PetscInt e, f;
4713a074057SBarry Smith 
4720fdc7489SMatthew Knepley       for (e = 0; e < out.numberofedges; ++e) {
4733a074057SBarry Smith         if (out.edgemarkerlist[e]) {
4743a074057SBarry Smith           const PetscInt  vertices[2] = {out.edgelist[e * 2 + 0] + numCells, out.edgelist[e * 2 + 1] + numCells};
4753a074057SBarry Smith           const PetscInt *edges;
4763a074057SBarry Smith           PetscInt        numEdges;
4773a074057SBarry Smith 
4789566063dSJacob Faibussowitsch           PetscCall(DMPlexGetJoin(*dmRefined, 2, vertices, &numEdges, &edges));
47963a3b9bcSJacob Faibussowitsch           PetscCheck(numEdges == 1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Two vertices must cover only one edge, not %" PetscInt_FMT, numEdges);
4809566063dSJacob Faibussowitsch           PetscCall(DMUniversalLabelSetLabelValue(universal, *dmRefined, PETSC_TRUE, edges[0], out.edgemarkerlist[e]));
4819566063dSJacob Faibussowitsch           PetscCall(DMPlexRestoreJoin(*dmRefined, 2, vertices, &numEdges, &edges));
4823a074057SBarry Smith         }
4833a074057SBarry Smith       }
4840fdc7489SMatthew Knepley       for (f = 0; f < out.numberoftrifaces; ++f) {
4853a074057SBarry Smith         if (out.trifacemarkerlist[f]) {
4863a074057SBarry Smith           const PetscInt  vertices[3] = {out.trifacelist[f * 3 + 0] + numCells, out.trifacelist[f * 3 + 1] + numCells, out.trifacelist[f * 3 + 2] + numCells};
4873a074057SBarry Smith           const PetscInt *faces;
4883a074057SBarry Smith           PetscInt        numFaces;
4893a074057SBarry Smith 
4909566063dSJacob Faibussowitsch           PetscCall(DMPlexGetFullJoin(*dmRefined, 3, vertices, &numFaces, &faces));
49163a3b9bcSJacob Faibussowitsch           PetscCheck(numFaces == 1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Three vertices must cover only one face, not %" PetscInt_FMT, numFaces);
4929566063dSJacob Faibussowitsch           PetscCall(DMUniversalLabelSetLabelValue(universal, *dmRefined, PETSC_TRUE, faces[0], out.trifacemarkerlist[f]));
4939566063dSJacob Faibussowitsch           PetscCall(DMPlexRestoreJoin(*dmRefined, 3, vertices, &numFaces, &faces));
4943a074057SBarry Smith         }
4953a074057SBarry Smith       }
4963a074057SBarry Smith     }
4970fdc7489SMatthew Knepley 
4989566063dSJacob Faibussowitsch     PetscCall(PetscObjectQuery((PetscObject)dm, "EGADS Model", (PetscObject *)&modelObj));
4999318fe57SMatthew G. Knepley     if (modelObj) {
5000fdc7489SMatthew Knepley #ifdef PETSC_HAVE_EGADS
5010fdc7489SMatthew Knepley       DMLabel   bodyLabel;
5020fdc7489SMatthew Knepley       PetscInt  cStart, cEnd, c, eStart, eEnd, fStart, fEnd;
503c1cad2e7SMatthew G. Knepley       PetscBool islite = PETSC_FALSE;
5040fdc7489SMatthew Knepley       ego      *bodies;
5050fdc7489SMatthew Knepley       ego       model, geom;
5060fdc7489SMatthew Knepley       int       Nb, oclass, mtype, *senses;
5070fdc7489SMatthew Knepley 
508*5552b385SBrandon       PetscCall(DMPlexCopyEGADSInfo_Internal(dm, *dmRefined));
509*5552b385SBrandon 
5100fdc7489SMatthew Knepley       /* Get Attached EGADS Model from Original DMPlex */
5119566063dSJacob Faibussowitsch       PetscCall(PetscObjectQuery((PetscObject)dm, "EGADS Model", (PetscObject *)&modelObj));
512c1cad2e7SMatthew G. Knepley       if (modelObj) {
5139566063dSJacob Faibussowitsch         PetscCall(PetscContainerGetPointer(modelObj, (void **)&model));
514f22e26b7SPierre Jolivet         PetscCall(EG_getTopology(model, &geom, &oclass, &mtype, nullptr, &Nb, &bodies, &senses));
515c1cad2e7SMatthew G. Knepley       } else {
516*5552b385SBrandon         PetscCall(PetscObjectQuery((PetscObject)dm, "EGADSlite Model", (PetscObject *)&modelObj));
517c1cad2e7SMatthew G. Knepley         if (modelObj) {
5189566063dSJacob Faibussowitsch           PetscCall(PetscContainerGetPointer(modelObj, (void **)&model));
519f22e26b7SPierre Jolivet           PetscCall(EGlite_getTopology(model, &geom, &oclass, &mtype, nullptr, &Nb, &bodies, &senses));
520c1cad2e7SMatthew G. Knepley           islite = PETSC_TRUE;
521c1cad2e7SMatthew G. Knepley         }
522c1cad2e7SMatthew G. Knepley       }
523c1cad2e7SMatthew G. Knepley       if (!modelObj) goto skip_egads;
5240fdc7489SMatthew Knepley 
5250fdc7489SMatthew Knepley       /* Set Cell Labels */
5269566063dSJacob Faibussowitsch       PetscCall(DMGetLabel(*dmRefined, "EGADS Body ID", &bodyLabel));
5279566063dSJacob Faibussowitsch       PetscCall(DMPlexGetHeightStratum(*dmRefined, 0, &cStart, &cEnd));
5289566063dSJacob Faibussowitsch       PetscCall(DMPlexGetHeightStratum(*dmRefined, 1, &fStart, &fEnd));
5299566063dSJacob Faibussowitsch       PetscCall(DMPlexGetDepthStratum(*dmRefined, 1, &eStart, &eEnd));
5300fdc7489SMatthew Knepley 
5310fdc7489SMatthew Knepley       for (c = cStart; c < cEnd; ++c) {
5320fdc7489SMatthew Knepley         PetscReal centroid[3] = {0., 0., 0.};
5330fdc7489SMatthew Knepley         PetscInt  b;
5340fdc7489SMatthew Knepley 
53535cb6cd3SPierre Jolivet         /* Determine what body the cell's centroid is located in */
5360fdc7489SMatthew Knepley         if (!interpolate) {
5370fdc7489SMatthew Knepley           PetscSection coordSection;
5380fdc7489SMatthew Knepley           Vec          coordinates;
539f22e26b7SPierre Jolivet           PetscScalar *coords = nullptr;
5400fdc7489SMatthew Knepley           PetscInt     coordSize, s, d;
5410fdc7489SMatthew Knepley 
5429566063dSJacob Faibussowitsch           PetscCall(DMGetCoordinatesLocal(*dmRefined, &coordinates));
5439566063dSJacob Faibussowitsch           PetscCall(DMGetCoordinateSection(*dmRefined, &coordSection));
5449566063dSJacob Faibussowitsch           PetscCall(DMPlexVecGetClosure(*dmRefined, coordSection, coordinates, c, &coordSize, &coords));
5459371c9d4SSatish Balay           for (s = 0; s < coordSize; ++s)
5469371c9d4SSatish Balay             for (d = 0; d < dim; ++d) centroid[d] += coords[s * dim + d];
5479566063dSJacob Faibussowitsch           PetscCall(DMPlexVecRestoreClosure(*dmRefined, coordSection, coordinates, c, &coordSize, &coords));
548f22e26b7SPierre Jolivet         } else PetscCall(DMPlexComputeCellGeometryFVM(*dmRefined, c, nullptr, centroid, nullptr));
5490fdc7489SMatthew Knepley         for (b = 0; b < Nb; ++b) {
5509371c9d4SSatish Balay           if (islite) {
5519371c9d4SSatish Balay             if (EGlite_inTopology(bodies[b], centroid) == EGADS_SUCCESS) break;
5529371c9d4SSatish Balay           } else {
5539371c9d4SSatish Balay             if (EG_inTopology(bodies[b], centroid) == EGADS_SUCCESS) break;
5549371c9d4SSatish Balay           }
5550fdc7489SMatthew Knepley         }
5560fdc7489SMatthew Knepley         if (b < Nb) {
5570fdc7489SMatthew Knepley           PetscInt  cval    = b, eVal, fVal;
558f22e26b7SPierre Jolivet           PetscInt *closure = nullptr, Ncl, cl;
5590fdc7489SMatthew Knepley 
5609566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(bodyLabel, c, cval));
5619566063dSJacob Faibussowitsch           PetscCall(DMPlexGetTransitiveClosure(*dmRefined, c, PETSC_TRUE, &Ncl, &closure));
5620fdc7489SMatthew Knepley           for (cl = 0; cl < Ncl; cl += 2) {
5630fdc7489SMatthew Knepley             const PetscInt p = closure[cl];
5640fdc7489SMatthew Knepley 
5650fdc7489SMatthew Knepley             if (p >= eStart && p < eEnd) {
5669566063dSJacob Faibussowitsch               PetscCall(DMLabelGetValue(bodyLabel, p, &eVal));
5679566063dSJacob Faibussowitsch               if (eVal < 0) PetscCall(DMLabelSetValue(bodyLabel, p, cval));
5680fdc7489SMatthew Knepley             }
5690fdc7489SMatthew Knepley             if (p >= fStart && p < fEnd) {
5709566063dSJacob Faibussowitsch               PetscCall(DMLabelGetValue(bodyLabel, p, &fVal));
5719566063dSJacob Faibussowitsch               if (fVal < 0) PetscCall(DMLabelSetValue(bodyLabel, p, cval));
5720fdc7489SMatthew Knepley             }
5730fdc7489SMatthew Knepley           }
5749566063dSJacob Faibussowitsch           PetscCall(DMPlexRestoreTransitiveClosure(*dmRefined, c, PETSC_TRUE, &Ncl, &closure));
5750fdc7489SMatthew Knepley         }
5760fdc7489SMatthew Knepley       }
577c1cad2e7SMatthew G. Knepley     skip_egads:;
5780fdc7489SMatthew Knepley #endif
5799318fe57SMatthew G. Knepley     }
5809566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementUniform(*dmRefined, PETSC_FALSE));
5813a074057SBarry Smith   }
5823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5833a074057SBarry Smith }
584