xref: /petsc/src/dm/impls/plex/adaptors/parmmg/parmmgadapt.c (revision e0f5bfbec699682fa3e8b8532b1176849ea4e12a)
1 #include "../mmgcommon.h" /*I      "petscdmplex.h"   I*/
2 #include <parmmg/libparmmg.h>
3 
4 PetscBool  ParMmgCite       = PETSC_FALSE;
5 const char ParMmgCitation[] = "@techreport{cirrottola:hal-02386837,\n"
6                               "  title       = {Parallel unstructured mesh adaptation using iterative remeshing and repartitioning},\n"
7                               "  institution = {Inria Bordeaux},\n"
8                               "  author      = {L. Cirrottola and A. Froehly},\n"
9                               "  number      = {9307},\n"
10                               "  note        = {\\url{https://hal.inria.fr/hal-02386837}},\n"
11                               "  year        = {2019}\n}\n";
12 
13 PETSC_EXTERN PetscErrorCode DMAdaptMetric_ParMmg_Plex(DM dm, Vec vertexMetric, DMLabel bdLabel, DMLabel rgLabel, DM *dmNew) {
14   MPI_Comm           comm;
15   const char        *bdName = "_boundary_";
16   const char        *rgName = "_regions_";
17   DM                 udm, cdm;
18   DMLabel            bdLabelNew, rgLabelNew;
19   const char        *bdLabelName, *rgLabelName;
20   IS                 globalVertexNum;
21   PetscSection       coordSection;
22   Vec                coordinates;
23   PetscSF            sf;
24   const PetscScalar *coords, *met;
25   PetscReal         *vertices, *metric, *verticesNew, *verticesNewLoc, gradationFactor, hausdorffNumber;
26   PetscInt          *cells, *cellsNew, *cellTags, *cellTagsNew, *verTags, *verTagsNew;
27   PetscInt          *bdFaces, *faceTags, *facesNew, *faceTagsNew;
28   PetscInt          *corners, *requiredCells, *requiredVer, *ridges, *requiredFaces;
29   PetscInt           cStart, cEnd, c, numCells, fStart, fEnd, f, numFaceTags, vStart, vEnd, v, numVertices;
30   PetscInt           dim, off, coff, maxConeSize, bdSize, i, j, k, Neq, verbosity, numIter;
31   PetscInt          *numVerInterfaces, *ngbRanks, *verNgbRank, *interfaces_lv, *interfaces_gv, *intOffset;
32   PetscInt           niranks, nrranks, numNgbRanks, numVerNgbRanksTotal, count, sliceSize, p, r, n, lv, gv;
33   PetscInt          *gv_new, *owners, *verticesNewSorted, pStart, pEnd;
34   PetscInt           numCellsNew, numVerticesNew, numCornersNew, numFacesNew, numVerticesNewLoc;
35   const PetscInt    *gV, *ioffset, *irootloc, *roffset, *rmine, *rremote;
36   PetscBool          flg = PETSC_FALSE, noInsert, noSwap, noMove, noSurf, isotropic, uniform;
37   const PetscMPIInt *iranks, *rranks;
38   PetscMPIInt        numProcs, rank;
39   PMMG_pParMesh      parmesh = NULL;
40 
41   PetscFunctionBegin;
42   PetscCall(PetscCitationsRegister(ParMmgCitation, &ParMmgCite));
43   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
44   PetscCallMPI(MPI_Comm_size(comm, &numProcs));
45   PetscCallMPI(MPI_Comm_rank(comm, &rank));
46   if (bdLabel) {
47     PetscCall(PetscObjectGetName((PetscObject)bdLabel, &bdLabelName));
48     PetscCall(PetscStrcmp(bdLabelName, bdName, &flg));
49     PetscCheck(!flg, comm, PETSC_ERR_ARG_WRONG, "\"%s\" cannot be used as label for boundary facets", bdLabelName);
50   }
51   if (rgLabel) {
52     PetscCall(PetscObjectGetName((PetscObject)rgLabel, &rgLabelName));
53     PetscCall(PetscStrcmp(rgLabelName, rgName, &flg));
54     PetscCheck(!flg, comm, PETSC_ERR_ARG_WRONG, "\"%s\" cannot be used as label for element tags", rgLabelName);
55   }
56 
57   /* Get mesh information */
58   PetscCall(DMGetDimension(dm, &dim));
59   PetscCheck(dim == 3, comm, PETSC_ERR_ARG_OUTOFRANGE, "ParMmg only works in 3D.");
60   Neq = (dim * (dim + 1)) / 2;
61   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
62   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
63   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
64   PetscCall(DMPlexUninterpolate(dm, &udm));
65   PetscCall(DMPlexGetMaxSizes(udm, &maxConeSize, NULL));
66   numCells    = cEnd - cStart;
67   numVertices = vEnd - vStart;
68 
69   /* Get cell offsets */
70   PetscCall(PetscMalloc1(numCells * maxConeSize, &cells));
71   for (c = 0, coff = 0; c < numCells; ++c) {
72     const PetscInt *cone;
73     PetscInt        coneSize, cl;
74 
75     PetscCall(DMPlexGetConeSize(udm, c, &coneSize));
76     PetscCall(DMPlexGetCone(udm, c, &cone));
77     for (cl = 0; cl < coneSize; ++cl) cells[coff++] = cone[cl] - vStart + 1;
78   }
79 
80   /* Get vertex coordinate array */
81   PetscCall(DMGetCoordinateDM(dm, &cdm));
82   PetscCall(DMGetLocalSection(cdm, &coordSection));
83   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
84   PetscCall(VecGetArrayRead(coordinates, &coords));
85   PetscCall(PetscMalloc2(numVertices * Neq, &metric, dim * numVertices, &vertices));
86   for (v = 0; v < vEnd - vStart; ++v) {
87     PetscCall(PetscSectionGetOffset(coordSection, v + vStart, &off));
88     for (i = 0; i < dim; ++i) vertices[dim * v + i] = PetscRealPart(coords[off + i]);
89   }
90   PetscCall(VecRestoreArrayRead(coordinates, &coords));
91 
92   /* Get face tags */
93   if (!bdLabel) {
94     flg = PETSC_TRUE;
95     PetscCall(DMLabelCreate(PETSC_COMM_SELF, bdName, &bdLabel));
96     PetscCall(DMPlexMarkBoundaryFaces(dm, 1, bdLabel));
97   }
98   PetscCall(DMLabelGetBounds(bdLabel, &pStart, &pEnd));
99   for (f = pStart, bdSize = 0, numFaceTags = 0; f < pEnd; ++f) {
100     PetscBool hasPoint;
101     PetscInt *closure = NULL, closureSize, cl;
102 
103     PetscCall(DMLabelHasPoint(bdLabel, f, &hasPoint));
104     if ((!hasPoint) || (f < fStart) || (f >= fEnd)) continue;
105     numFaceTags++;
106 
107     PetscCall(DMPlexGetTransitiveClosure(dm, f, PETSC_TRUE, &closureSize, &closure));
108     for (cl = 0; cl < closureSize * 2; cl += 2) {
109       if ((closure[cl] >= vStart) && (closure[cl] < vEnd)) ++bdSize;
110     }
111     PetscCall(DMPlexRestoreTransitiveClosure(dm, f, PETSC_TRUE, &closureSize, &closure));
112   }
113   PetscCall(PetscMalloc2(bdSize, &bdFaces, numFaceTags, &faceTags));
114   for (f = pStart, bdSize = 0, numFaceTags = 0; f < pEnd; ++f) {
115     PetscBool hasPoint;
116     PetscInt *closure = NULL, closureSize, cl;
117 
118     PetscCall(DMLabelHasPoint(bdLabel, f, &hasPoint));
119     if ((!hasPoint) || (f < fStart) || (f >= fEnd)) continue;
120 
121     PetscCall(DMPlexGetTransitiveClosure(dm, f, PETSC_TRUE, &closureSize, &closure));
122     for (cl = 0; cl < closureSize * 2; cl += 2) {
123       if ((closure[cl] >= vStart) && (closure[cl] < vEnd)) bdFaces[bdSize++] = closure[cl] - vStart + 1;
124     }
125     PetscCall(DMPlexRestoreTransitiveClosure(dm, f, PETSC_TRUE, &closureSize, &closure));
126     PetscCall(DMLabelGetValue(bdLabel, f, &faceTags[numFaceTags++]));
127   }
128 
129   /* Get cell tags */
130   PetscCall(PetscCalloc2(numVertices, &verTags, numCells, &cellTags));
131   if (rgLabel) {
132     for (c = cStart; c < cEnd; ++c) PetscCall(DMLabelGetValue(rgLabel, c, &cellTags[c]));
133   }
134 
135   /* Get metric */
136   PetscCall(VecViewFromOptions(vertexMetric, NULL, "-adapt_metric_view"));
137   PetscCall(VecGetArrayRead(vertexMetric, &met));
138   PetscCall(DMPlexMetricIsIsotropic(dm, &isotropic));
139   PetscCall(DMPlexMetricIsUniform(dm, &uniform));
140   for (v = 0; v < (vEnd - vStart); ++v) {
141     for (i = 0, k = 0; i < dim; ++i) {
142       for (j = i; j < dim; ++j, ++k) {
143         if (isotropic) {
144           if (i == j) {
145             if (uniform) metric[Neq * v + k] = PetscRealPart(met[0]);
146             else metric[Neq * v + k] = PetscRealPart(met[v]);
147           } else metric[Neq * v + k] = 0.0;
148         } else metric[Neq * v + k] = PetscRealPart(met[dim * dim * v + dim * i + j]);
149       }
150     }
151   }
152   PetscCall(VecRestoreArrayRead(vertexMetric, &met));
153 
154   /* Build ParMMG communicators: the list of vertices between two partitions  */
155   niranks = nrranks = 0;
156   numNgbRanks       = 0;
157   if (numProcs > 1) {
158     PetscCall(DMGetPointSF(dm, &sf));
159     PetscCall(PetscSFSetUp(sf));
160     PetscCall(PetscSFGetLeafRanks(sf, &niranks, &iranks, &ioffset, &irootloc));
161     PetscCall(PetscSFGetRootRanks(sf, &nrranks, &rranks, &roffset, &rmine, &rremote));
162     PetscCall(PetscCalloc1(numProcs, &numVerInterfaces));
163 
164     /* Count number of roots associated with each leaf */
165     for (r = 0; r < niranks; ++r) {
166       for (i = ioffset[r], count = 0; i < ioffset[r + 1]; ++i) {
167         if (irootloc[i] >= vStart && irootloc[i] < vEnd) count++;
168       }
169       numVerInterfaces[iranks[r]] += count;
170     }
171 
172     /* Count number of leaves associated with each root */
173     for (r = 0; r < nrranks; ++r) {
174       for (i = roffset[r], count = 0; i < roffset[r + 1]; ++i) {
175         if (rmine[i] >= vStart && rmine[i] < vEnd) count++;
176       }
177       numVerInterfaces[rranks[r]] += count;
178     }
179 
180     /* Count global number of ranks */
181     for (p = 0; p < numProcs; ++p) {
182       if (numVerInterfaces[p]) numNgbRanks++;
183     }
184 
185     /* Provide numbers of vertex interfaces */
186     PetscCall(PetscMalloc2(numNgbRanks, &ngbRanks, numNgbRanks, &verNgbRank));
187     for (p = 0, n = 0; p < numProcs; ++p) {
188       if (numVerInterfaces[p]) {
189         ngbRanks[n]   = p;
190         verNgbRank[n] = numVerInterfaces[p];
191         n++;
192       }
193     }
194     numVerNgbRanksTotal = 0;
195     for (p = 0; p < numNgbRanks; ++p) numVerNgbRanksTotal += verNgbRank[p];
196 
197     /* For each neighbor, fill in interface arrays */
198     PetscCall(PetscMalloc3(numVerNgbRanksTotal, &interfaces_lv, numVerNgbRanksTotal, &interfaces_gv, numNgbRanks + 1, &intOffset));
199     intOffset[0] = 0;
200     for (p = 0, r = 0, i = 0; p < numNgbRanks; ++p) {
201       intOffset[p + 1] = intOffset[p];
202 
203       /* Leaf case */
204       if (iranks && iranks[i] == ngbRanks[p]) {
205         /* Add the right slice of irootloc at the right place */
206         sliceSize = ioffset[i + 1] - ioffset[i];
207         for (j = 0, count = 0; j < sliceSize; ++j) {
208           PetscCheck(ioffset[i] + j < ioffset[niranks], comm, PETSC_ERR_ARG_OUTOFRANGE, "Leaf index %" PetscInt_FMT " out of range (expected < %" PetscInt_FMT ")", ioffset[i] + j, ioffset[niranks]);
209           v = irootloc[ioffset[i] + j];
210           if (v >= vStart && v < vEnd) {
211             PetscCheck(intOffset[p + 1] + count < numVerNgbRanksTotal, comm, PETSC_ERR_ARG_OUTOFRANGE, "Leaf interface index %" PetscInt_FMT " out of range (expected < %" PetscInt_FMT ")", intOffset[p + 1] + count, numVerNgbRanksTotal);
212             interfaces_lv[intOffset[p + 1] + count] = v - vStart;
213             count++;
214           }
215         }
216         intOffset[p + 1] += count;
217         i++;
218       }
219 
220       /* Root case */
221       if (rranks && rranks[r] == ngbRanks[p]) {
222         /* Add the right slice of rmine at the right place */
223         sliceSize = roffset[r + 1] - roffset[r];
224         for (j = 0, count = 0; j < sliceSize; ++j) {
225           PetscCheck(roffset[r] + j < roffset[nrranks], comm, PETSC_ERR_ARG_OUTOFRANGE, "Root index %" PetscInt_FMT " out of range (expected < %" PetscInt_FMT ")", roffset[r] + j, roffset[nrranks]);
226           v = rmine[roffset[r] + j];
227           if (v >= vStart && v < vEnd) {
228             PetscCheck(intOffset[p + 1] + count < numVerNgbRanksTotal, comm, PETSC_ERR_ARG_OUTOFRANGE, "Root interface index %" PetscInt_FMT " out of range (expected < %" PetscInt_FMT ")", intOffset[p + 1] + count, numVerNgbRanksTotal);
229             interfaces_lv[intOffset[p + 1] + count] = v - vStart;
230             count++;
231           }
232         }
233         intOffset[p + 1] += count;
234         r++;
235       }
236 
237       /* Check validity of offsets */
238       PetscCheck(intOffset[p + 1] == intOffset[p] + verNgbRank[p], comm, PETSC_ERR_ARG_OUTOFRANGE, "Missing offsets (expected %" PetscInt_FMT ", got %" PetscInt_FMT ")", intOffset[p] + verNgbRank[p], intOffset[p + 1]);
239     }
240     PetscCall(DMPlexGetVertexNumbering(udm, &globalVertexNum));
241     PetscCall(ISGetIndices(globalVertexNum, &gV));
242     for (i = 0; i < numVerNgbRanksTotal; ++i) {
243       v                = gV[interfaces_lv[i]];
244       interfaces_gv[i] = v < 0 ? -v - 1 : v;
245       interfaces_lv[i] += 1;
246       interfaces_gv[i] += 1;
247     }
248     PetscCall(ISRestoreIndices(globalVertexNum, &gV));
249     PetscCall(PetscFree(numVerInterfaces));
250   }
251   PetscCall(DMDestroy(&udm));
252 
253   /* Send the data to ParMmg and remesh */
254   PetscCall(DMPlexMetricNoInsertion(dm, &noInsert));
255   PetscCall(DMPlexMetricNoSwapping(dm, &noSwap));
256   PetscCall(DMPlexMetricNoMovement(dm, &noMove));
257   PetscCall(DMPlexMetricNoSurf(dm, &noSurf));
258   PetscCall(DMPlexMetricGetVerbosity(dm, &verbosity));
259   PetscCall(DMPlexMetricGetNumIterations(dm, &numIter));
260   PetscCall(DMPlexMetricGetGradationFactor(dm, &gradationFactor));
261   PetscCall(DMPlexMetricGetHausdorffNumber(dm, &hausdorffNumber));
262   PetscCallMMG_NONSTANDARD(PMMG_Init_parMesh(PMMG_ARG_start, PMMG_ARG_ppParMesh, &parmesh, PMMG_ARG_pMesh, PMMG_ARG_pMet, PMMG_ARG_dim, 3, PMMG_ARG_MPIComm, comm, PMMG_ARG_end));
263   PetscCallMMG_NONSTANDARD(PMMG_Set_meshSize(parmesh, numVertices, numCells, 0, numFaceTags, 0, 0));
264   PetscCallMMG_NONSTANDARD(PMMG_Set_iparameter(parmesh, PMMG_IPARAM_APImode, PMMG_APIDISTRIB_nodes));
265   PetscCallMMG_NONSTANDARD(PMMG_Set_iparameter(parmesh, PMMG_IPARAM_noinsert, noInsert));
266   PetscCallMMG_NONSTANDARD(PMMG_Set_iparameter(parmesh, PMMG_IPARAM_noswap, noSwap));
267   PetscCallMMG_NONSTANDARD(PMMG_Set_iparameter(parmesh, PMMG_IPARAM_nomove, noMove));
268   PetscCallMMG_NONSTANDARD(PMMG_Set_iparameter(parmesh, PMMG_IPARAM_nosurf, noSurf));
269   PetscCallMMG_NONSTANDARD(PMMG_Set_iparameter(parmesh, PMMG_IPARAM_verbose, verbosity));
270   PetscCallMMG_NONSTANDARD(PMMG_Set_iparameter(parmesh, PMMG_IPARAM_globalNum, 1));
271   PetscCallMMG_NONSTANDARD(PMMG_Set_iparameter(parmesh, PMMG_IPARAM_niter, numIter));
272   PetscCallMMG_NONSTANDARD(PMMG_Set_dparameter(parmesh, PMMG_DPARAM_hgrad, gradationFactor));
273   PetscCallMMG_NONSTANDARD(PMMG_Set_dparameter(parmesh, PMMG_DPARAM_hausd, hausdorffNumber));
274   PetscCallMMG_NONSTANDARD(PMMG_Set_vertices(parmesh, vertices, verTags));
275   PetscCallMMG_NONSTANDARD(PMMG_Set_tetrahedra(parmesh, cells, cellTags));
276   PetscCallMMG_NONSTANDARD(PMMG_Set_triangles(parmesh, bdFaces, faceTags));
277   PetscCallMMG_NONSTANDARD(PMMG_Set_metSize(parmesh, MMG5_Vertex, numVertices, MMG5_Tensor));
278   PetscCallMMG_NONSTANDARD(PMMG_Set_tensorMets(parmesh, metric));
279   PetscCallMMG_NONSTANDARD(PMMG_Set_numberOfNodeCommunicators(parmesh, numNgbRanks));
280   for (c = 0; c < numNgbRanks; ++c) {
281     PetscCallMMG_NONSTANDARD(PMMG_Set_ithNodeCommunicatorSize(parmesh, c, ngbRanks[c], intOffset[c + 1] - intOffset[c]));
282     PetscCallMMG_NONSTANDARD(PMMG_Set_ithNodeCommunicator_nodes(parmesh, c, &interfaces_lv[intOffset[c]], &interfaces_gv[intOffset[c]], 1));
283   }
284   PetscCallMMG(PMMG_parmmglib_distributed(parmesh));
285   PetscCall(PetscFree(cells));
286   PetscCall(PetscFree2(metric, vertices));
287   PetscCall(PetscFree2(bdFaces, faceTags));
288   PetscCall(PetscFree2(verTags, cellTags));
289   if (numProcs > 1) {
290     PetscCall(PetscFree2(ngbRanks, verNgbRank));
291     PetscCall(PetscFree3(interfaces_lv, interfaces_gv, intOffset));
292   }
293 
294   /* Retrieve mesh from Mmg */
295   numCornersNew = 4;
296   PetscCallMMG_NONSTANDARD(PMMG_Get_meshSize(parmesh, &numVerticesNew, &numCellsNew, 0, &numFacesNew, 0, 0));
297   PetscCall(PetscMalloc4(dim * numVerticesNew, &verticesNew, numVerticesNew, &verTagsNew, numVerticesNew, &corners, numVerticesNew, &requiredVer));
298   PetscCall(PetscMalloc3((dim + 1) * numCellsNew, &cellsNew, numCellsNew, &cellTagsNew, numCellsNew, &requiredCells));
299   PetscCall(PetscMalloc4(dim * numFacesNew, &facesNew, numFacesNew, &faceTagsNew, numFacesNew, &ridges, numFacesNew, &requiredFaces));
300   PetscCallMMG_NONSTANDARD(PMMG_Get_vertices(parmesh, verticesNew, verTagsNew, corners, requiredVer));
301   PetscCallMMG_NONSTANDARD(PMMG_Get_tetrahedra(parmesh, cellsNew, cellTagsNew, requiredCells));
302   PetscCallMMG_NONSTANDARD(PMMG_Get_triangles(parmesh, facesNew, faceTagsNew, requiredFaces));
303   PetscCall(PetscMalloc2(numVerticesNew, &owners, numVerticesNew, &gv_new));
304   PetscCallMMG_NONSTANDARD(PMMG_Set_iparameter(parmesh, PMMG_IPARAM_globalNum, 1));
305   PetscCallMMG_NONSTANDARD(PMMG_Get_verticesGloNum(parmesh, gv_new, owners));
306   for (i = 0; i < dim * numFacesNew; ++i) facesNew[i] -= 1;
307   for (i = 0; i < (dim + 1) * numCellsNew; ++i) cellsNew[i] = gv_new[cellsNew[i] - 1] - 1;
308   for (i = 0, numVerticesNewLoc = 0; i < numVerticesNew; ++i) {
309     if (owners[i] == rank) numVerticesNewLoc++;
310   }
311   PetscCall(PetscMalloc2(numVerticesNewLoc * dim, &verticesNewLoc, numVerticesNew, &verticesNewSorted));
312   for (i = 0, c = 0; i < numVerticesNew; i++) {
313     if (owners[i] == rank) {
314       for (j = 0; j < dim; ++j) verticesNewLoc[dim * c + j] = verticesNew[dim * i + j];
315       c++;
316     }
317   }
318 
319   /* Reorder for consistency with DMPlex */
320   for (i = 0; i < numCellsNew; ++i) PetscCall(DMPlexInvertCell(DM_POLYTOPE_TETRAHEDRON, &cellsNew[4 * i]));
321 
322   /* Create new plex */
323   PetscCall(DMPlexCreateFromCellListParallelPetsc(comm, dim, numCellsNew, numVerticesNewLoc, PETSC_DECIDE, numCornersNew, PETSC_TRUE, cellsNew, dim, verticesNewLoc, NULL, &verticesNewSorted, dmNew));
324   PetscCallMMG_NONSTANDARD(PMMG_Free_all(PMMG_ARG_start, PMMG_ARG_ppParMesh, &parmesh, PMMG_ARG_end));
325   PetscCall(PetscFree4(verticesNew, verTagsNew, corners, requiredVer));
326 
327   /* Get adapted mesh information */
328   PetscCall(DMPlexGetHeightStratum(*dmNew, 0, &cStart, &cEnd));
329   PetscCall(DMPlexGetHeightStratum(*dmNew, 1, &fStart, &fEnd));
330   PetscCall(DMPlexGetDepthStratum(*dmNew, 0, &vStart, &vEnd));
331 
332   /* Rebuild boundary label */
333   PetscCall(DMCreateLabel(*dmNew, flg ? bdName : bdLabelName));
334   PetscCall(DMGetLabel(*dmNew, flg ? bdName : bdLabelName, &bdLabelNew));
335   for (i = 0; i < numFacesNew; i++) {
336     PetscBool       hasTag = PETSC_FALSE;
337     PetscInt        numCoveredPoints, numFaces = 0, facePoints[3];
338     const PetscInt *coveredPoints = NULL;
339 
340     for (j = 0; j < dim; ++j) {
341       lv = facesNew[i * dim + j];
342       gv = gv_new[lv] - 1;
343       PetscCall(PetscFindInt(gv, numVerticesNew, verticesNewSorted, &lv));
344       facePoints[j] = lv + vStart;
345     }
346     PetscCall(DMPlexGetFullJoin(*dmNew, dim, facePoints, &numCoveredPoints, &coveredPoints));
347     for (j = 0; j < numCoveredPoints; ++j) {
348       if (coveredPoints[j] >= fStart && coveredPoints[j] < fEnd) {
349         numFaces++;
350         f = j;
351       }
352     }
353     PetscCheck(numFaces == 1, comm, PETSC_ERR_ARG_OUTOFRANGE, "%" PetscInt_FMT " vertices cannot define more than 1 facet (%" PetscInt_FMT ")", dim, numFaces);
354     PetscCall(DMLabelHasStratum(bdLabel, faceTagsNew[i], &hasTag));
355     if (hasTag) PetscCall(DMLabelSetValue(bdLabelNew, coveredPoints[f], faceTagsNew[i]));
356     PetscCall(DMPlexRestoreJoin(*dmNew, dim, facePoints, &numCoveredPoints, &coveredPoints));
357   }
358   PetscCall(PetscFree4(facesNew, faceTagsNew, ridges, requiredFaces));
359   PetscCall(PetscFree2(owners, gv_new));
360   PetscCall(PetscFree2(verticesNewLoc, verticesNewSorted));
361   if (flg) PetscCall(DMLabelDestroy(&bdLabel));
362 
363   /* Rebuild cell labels */
364   PetscCall(DMCreateLabel(*dmNew, rgLabel ? rgLabelName : rgName));
365   PetscCall(DMGetLabel(*dmNew, rgLabel ? rgLabelName : rgName, &rgLabelNew));
366   for (c = cStart; c < cEnd; ++c) PetscCall(DMLabelSetValue(rgLabelNew, c, cellTagsNew[c - cStart]));
367   PetscCall(PetscFree3(cellsNew, cellTagsNew, requiredCells));
368 
369   PetscFunctionReturn(0);
370 }
371