xref: /petsc/src/dm/impls/plex/plexsubmesh.c (revision dd05d8109ffd4bf5d135d4e8a3a4f4291851a735)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>  /*I      "petscdmplex.h"    I*/
2c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h> /*I      "petscdmlabel.h"   I*/
30c312b8eSJed Brown #include <petscsf.h>
4e6ccafaeSMatthew G Knepley 
59371c9d4SSatish Balay static PetscErrorCode DMPlexCellIsHybrid_Internal(DM dm, PetscInt p, PetscBool *isHybrid) {
6412e9a14SMatthew G. Knepley   DMPolytopeType ct;
7412e9a14SMatthew G. Knepley 
8412e9a14SMatthew G. Knepley   PetscFunctionBegin;
99566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, p, &ct));
10412e9a14SMatthew G. Knepley   switch (ct) {
11412e9a14SMatthew G. Knepley   case DM_POLYTOPE_POINT_PRISM_TENSOR:
12412e9a14SMatthew G. Knepley   case DM_POLYTOPE_SEG_PRISM_TENSOR:
13412e9a14SMatthew G. Knepley   case DM_POLYTOPE_TRI_PRISM_TENSOR:
149371c9d4SSatish Balay   case DM_POLYTOPE_QUAD_PRISM_TENSOR: *isHybrid = PETSC_TRUE;
15412e9a14SMatthew G. Knepley   default: *isHybrid = PETSC_FALSE;
16412e9a14SMatthew G. Knepley   }
17412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
18412e9a14SMatthew G. Knepley }
19412e9a14SMatthew G. Knepley 
209371c9d4SSatish Balay static PetscErrorCode DMPlexGetTensorPrismBounds_Internal(DM dm, PetscInt dim, PetscInt *cStart, PetscInt *cEnd) {
21412e9a14SMatthew G. Knepley   DMLabel ctLabel;
22412e9a14SMatthew G. Knepley 
23412e9a14SMatthew G. Knepley   PetscFunctionBegin;
24412e9a14SMatthew G. Knepley   if (cStart) *cStart = -1;
25412e9a14SMatthew G. Knepley   if (cEnd) *cEnd = -1;
269566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellTypeLabel(dm, &ctLabel));
27412e9a14SMatthew G. Knepley   switch (dim) {
289566063dSJacob Faibussowitsch   case 1: PetscCall(DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_POINT_PRISM_TENSOR, cStart, cEnd)); break;
299566063dSJacob Faibussowitsch   case 2: PetscCall(DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_SEG_PRISM_TENSOR, cStart, cEnd)); break;
30412e9a14SMatthew G. Knepley   case 3:
319566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_TRI_PRISM_TENSOR, cStart, cEnd));
329566063dSJacob Faibussowitsch     if (*cStart < 0) PetscCall(DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_QUAD_PRISM_TENSOR, cStart, cEnd));
33412e9a14SMatthew G. Knepley     break;
34412e9a14SMatthew G. Knepley   default: PetscFunctionReturn(0);
35412e9a14SMatthew G. Knepley   }
36412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
37412e9a14SMatthew G. Knepley }
38412e9a14SMatthew G. Knepley 
399371c9d4SSatish Balay static PetscErrorCode DMPlexMarkBoundaryFaces_Internal(DM dm, PetscInt val, PetscInt cellHeight, DMLabel label) {
4030560a7bSMatthew G. Knepley   PetscInt fStart, fEnd, f;
4130560a7bSMatthew G. Knepley 
4230560a7bSMatthew G. Knepley   PetscFunctionBegin;
439566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, cellHeight + 1, &fStart, &fEnd));
4430560a7bSMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {
4530560a7bSMatthew G. Knepley     PetscInt supportSize;
4630560a7bSMatthew G. Knepley 
479566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, f, &supportSize));
48e752be1aSMatthew G. Knepley     if (supportSize == 1) {
49e752be1aSMatthew G. Knepley       if (val < 0) {
50e752be1aSMatthew G. Knepley         PetscInt *closure = NULL;
51e752be1aSMatthew G. Knepley         PetscInt  clSize, cl, cval;
52e752be1aSMatthew G. Knepley 
539566063dSJacob Faibussowitsch         PetscCall(DMPlexGetTransitiveClosure(dm, f, PETSC_TRUE, &clSize, &closure));
54e752be1aSMatthew G. Knepley         for (cl = 0; cl < clSize * 2; cl += 2) {
559566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, closure[cl], &cval));
56e752be1aSMatthew G. Knepley           if (cval < 0) continue;
579566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(label, f, cval));
58e752be1aSMatthew G. Knepley           break;
59e752be1aSMatthew G. Knepley         }
609566063dSJacob Faibussowitsch         if (cl == clSize * 2) PetscCall(DMLabelSetValue(label, f, 1));
619566063dSJacob Faibussowitsch         PetscCall(DMPlexRestoreTransitiveClosure(dm, f, PETSC_TRUE, &clSize, &closure));
62e752be1aSMatthew G. Knepley       } else {
639566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(label, f, val));
64e752be1aSMatthew G. Knepley       }
65e752be1aSMatthew G. Knepley     }
6630560a7bSMatthew G. Knepley   }
6730560a7bSMatthew G. Knepley   PetscFunctionReturn(0);
6830560a7bSMatthew G. Knepley }
6930560a7bSMatthew G. Knepley 
70cd0c2139SMatthew G Knepley /*@
71cd0c2139SMatthew G Knepley   DMPlexMarkBoundaryFaces - Mark all faces on the boundary
72cd0c2139SMatthew G Knepley 
73cd0c2139SMatthew G Knepley   Not Collective
74cd0c2139SMatthew G Knepley 
75d8d19677SJose E. Roman   Input Parameters:
76e752be1aSMatthew G. Knepley + dm - The original DM
77e752be1aSMatthew G. Knepley - val - The marker value, or PETSC_DETERMINE to use some value in the closure (or 1 if none are found)
78cd0c2139SMatthew G Knepley 
79cd0c2139SMatthew G Knepley   Output Parameter:
80e752be1aSMatthew G. Knepley . label - The DMLabel marking boundary faces with the given value
81cd0c2139SMatthew G Knepley 
82cd0c2139SMatthew G Knepley   Level: developer
83cd0c2139SMatthew G Knepley 
84db781477SPatrick Sanan .seealso: `DMLabelCreate()`, `DMCreateLabel()`
8509f723d9SJed Brown @*/
869371c9d4SSatish Balay PetscErrorCode DMPlexMarkBoundaryFaces(DM dm, PetscInt val, DMLabel label) {
87827c4036SVaclav Hapla   DMPlexInterpolatedFlag flg;
88cd0c2139SMatthew G Knepley 
89cd0c2139SMatthew G Knepley   PetscFunctionBegin;
90827c4036SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
919566063dSJacob Faibussowitsch   PetscCall(DMPlexIsInterpolated(dm, &flg));
9208401ef6SPierre Jolivet   PetscCheck(flg == DMPLEX_INTERPOLATED_FULL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not fully interpolated on this rank");
939566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkBoundaryFaces_Internal(dm, val, 0, label));
94cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
95cd0c2139SMatthew G Knepley }
96cd0c2139SMatthew G Knepley 
979371c9d4SSatish Balay static PetscErrorCode DMPlexLabelComplete_Internal(DM dm, DMLabel label, PetscBool completeCells) {
98b0bf5782SToby Isaac   IS              valueIS;
99ac51f24eSSander Arens   PetscSF         sfPoint;
100b0bf5782SToby Isaac   const PetscInt *values;
101ac51f24eSSander Arens   PetscInt        numValues, v, cStart, cEnd, nroots;
102b0bf5782SToby Isaac 
103b0bf5782SToby Isaac   PetscFunctionBegin;
1049566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
1059566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
1069566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
1079566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
108b0bf5782SToby Isaac   for (v = 0; v < numValues; ++v) {
109b0bf5782SToby Isaac     IS              pointIS;
110b0bf5782SToby Isaac     const PetscInt *points;
111b0bf5782SToby Isaac     PetscInt        numPoints, p;
112b0bf5782SToby Isaac 
1139566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
1149566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
1159566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
116b0bf5782SToby Isaac     for (p = 0; p < numPoints; ++p) {
117b0bf5782SToby Isaac       PetscInt  q       = points[p];
118b0bf5782SToby Isaac       PetscInt *closure = NULL;
119b0bf5782SToby Isaac       PetscInt  closureSize, c;
120b0bf5782SToby Isaac 
121b0bf5782SToby Isaac       if (cStart <= q && q < cEnd && !completeCells) { /* skip cells */
122b0bf5782SToby Isaac         continue;
123b0bf5782SToby Isaac       }
1249566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure));
12548a46eb9SPierre Jolivet       for (c = 0; c < closureSize * 2; c += 2) PetscCall(DMLabelSetValue(label, closure[c], values[v]));
1269566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure));
127b0bf5782SToby Isaac     }
1289566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
1299566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
130b0bf5782SToby Isaac   }
1319566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
1329566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
1339566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
1349566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL));
135ac51f24eSSander Arens   if (nroots >= 0) {
13626279d81SSanderA     DMLabel         lblRoots, lblLeaves;
13726279d81SSanderA     IS              valueIS, pointIS;
13826279d81SSanderA     const PetscInt *values;
13926279d81SSanderA     PetscInt        numValues, v;
14026279d81SSanderA 
14126279d81SSanderA     /* Pull point contributions from remote leaves into local roots */
1429566063dSJacob Faibussowitsch     PetscCall(DMLabelGather(label, sfPoint, &lblLeaves));
1439566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(lblLeaves, &valueIS));
1449566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numValues));
1459566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
14626279d81SSanderA     for (v = 0; v < numValues; ++v) {
14726279d81SSanderA       const PetscInt value = values[v];
14826279d81SSanderA 
1499566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(lblLeaves, value, &pointIS));
1509566063dSJacob Faibussowitsch       PetscCall(DMLabelInsertIS(label, pointIS, value));
1519566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
15226279d81SSanderA     }
1539566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
1549566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
1559566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&lblLeaves));
15626279d81SSanderA     /* Push point contributions from roots into remote leaves */
1579566063dSJacob Faibussowitsch     PetscCall(DMLabelDistribute(label, sfPoint, &lblRoots));
1589566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(lblRoots, &valueIS));
1599566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numValues));
1609566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
16126279d81SSanderA     for (v = 0; v < numValues; ++v) {
16226279d81SSanderA       const PetscInt value = values[v];
16326279d81SSanderA 
1649566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(lblRoots, value, &pointIS));
1659566063dSJacob Faibussowitsch       PetscCall(DMLabelInsertIS(label, pointIS, value));
1669566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
16726279d81SSanderA     }
1689566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
1699566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
1709566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&lblRoots));
17126279d81SSanderA   }
172b0bf5782SToby Isaac   PetscFunctionReturn(0);
173b0bf5782SToby Isaac }
174b0bf5782SToby Isaac 
1752be2b188SMatthew G Knepley /*@
1762be2b188SMatthew G Knepley   DMPlexLabelComplete - Starting with a label marking points on a surface, we add the transitive closure to the surface
1772be2b188SMatthew G Knepley 
1782be2b188SMatthew G Knepley   Input Parameters:
1792be2b188SMatthew G Knepley + dm - The DM
1802be2b188SMatthew G Knepley - label - A DMLabel marking the surface points
1812be2b188SMatthew G Knepley 
1822be2b188SMatthew G Knepley   Output Parameter:
1832be2b188SMatthew G Knepley . label - A DMLabel marking all surface points in the transitive closure
1842be2b188SMatthew G Knepley 
1852be2b188SMatthew G Knepley   Level: developer
1862be2b188SMatthew G Knepley 
187db781477SPatrick Sanan .seealso: `DMPlexLabelCohesiveComplete()`
1882be2b188SMatthew G Knepley @*/
1899371c9d4SSatish Balay PetscErrorCode DMPlexLabelComplete(DM dm, DMLabel label) {
1902be2b188SMatthew G Knepley   PetscFunctionBegin;
1919566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete_Internal(dm, label, PETSC_TRUE));
1922be2b188SMatthew G Knepley   PetscFunctionReturn(0);
1932be2b188SMatthew G Knepley }
1942be2b188SMatthew G Knepley 
1956cf0e42fSMatthew G. Knepley /*@
196a6e0b375SMatthew G. Knepley   DMPlexLabelAddCells - Starting with a label marking points on a surface, we add a cell for each point
1976cf0e42fSMatthew G. Knepley 
1986cf0e42fSMatthew G. Knepley   Input Parameters:
1996cf0e42fSMatthew G. Knepley + dm - The DM
2006cf0e42fSMatthew G. Knepley - label - A DMLabel marking the surface points
2016cf0e42fSMatthew G. Knepley 
2026cf0e42fSMatthew G. Knepley   Output Parameter:
2036cf0e42fSMatthew G. Knepley . label - A DMLabel incorporating cells
2046cf0e42fSMatthew G. Knepley 
2056cf0e42fSMatthew G. Knepley   Level: developer
2066cf0e42fSMatthew G. Knepley 
2076cf0e42fSMatthew G. Knepley   Note: The cells allow FEM boundary conditions to be applied using the cell geometry
2086cf0e42fSMatthew G. Knepley 
209db781477SPatrick Sanan .seealso: `DMPlexLabelAddFaceCells()`, `DMPlexLabelComplete()`, `DMPlexLabelCohesiveComplete()`
2106cf0e42fSMatthew G. Knepley @*/
2119371c9d4SSatish Balay PetscErrorCode DMPlexLabelAddCells(DM dm, DMLabel label) {
2126cf0e42fSMatthew G. Knepley   IS              valueIS;
2136cf0e42fSMatthew G. Knepley   const PetscInt *values;
214485ad865SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd;
2156cf0e42fSMatthew G. Knepley 
2166cf0e42fSMatthew G. Knepley   PetscFunctionBegin;
2179566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
2189566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
2199566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
2209566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
2216cf0e42fSMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
2226cf0e42fSMatthew G. Knepley     IS              pointIS;
2236cf0e42fSMatthew G. Knepley     const PetscInt *points;
2246cf0e42fSMatthew G. Knepley     PetscInt        numPoints, p;
2256cf0e42fSMatthew G. Knepley 
2269566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
2279566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
2289566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
2296cf0e42fSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2306cf0e42fSMatthew G. Knepley       PetscInt *closure = NULL;
231a6e0b375SMatthew G. Knepley       PetscInt  closureSize, cl;
2326cf0e42fSMatthew G. Knepley 
2339566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure));
23422eabd52SMatthew G. Knepley       for (cl = closureSize - 1; cl > 0; --cl) {
235a6e0b375SMatthew G. Knepley         const PetscInt cell = closure[cl * 2];
2369371c9d4SSatish Balay         if ((cell >= cStart) && (cell < cEnd)) {
2379371c9d4SSatish Balay           PetscCall(DMLabelSetValue(label, cell, values[v]));
2389371c9d4SSatish Balay           break;
2399371c9d4SSatish Balay         }
24022eabd52SMatthew G. Knepley       }
2419566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure));
2426cf0e42fSMatthew G. Knepley     }
2439566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
2449566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
2456cf0e42fSMatthew G. Knepley   }
2469566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
2479566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
2486cf0e42fSMatthew G. Knepley   PetscFunctionReturn(0);
2496cf0e42fSMatthew G. Knepley }
2506cf0e42fSMatthew G. Knepley 
251f402d5e4SToby Isaac /*@
252a6e0b375SMatthew G. Knepley   DMPlexLabelAddFaceCells - Starting with a label marking faces on a surface, we add a cell for each face
253a6e0b375SMatthew G. Knepley 
254a6e0b375SMatthew G. Knepley   Input Parameters:
255a6e0b375SMatthew G. Knepley + dm - The DM
256a6e0b375SMatthew G. Knepley - label - A DMLabel marking the surface points
257a6e0b375SMatthew G. Knepley 
258a6e0b375SMatthew G. Knepley   Output Parameter:
259a6e0b375SMatthew G. Knepley . label - A DMLabel incorporating cells
260a6e0b375SMatthew G. Knepley 
261a6e0b375SMatthew G. Knepley   Level: developer
262a6e0b375SMatthew G. Knepley 
263a6e0b375SMatthew G. Knepley   Note: The cells allow FEM boundary conditions to be applied using the cell geometry
264a6e0b375SMatthew G. Knepley 
265db781477SPatrick Sanan .seealso: `DMPlexLabelAddCells()`, `DMPlexLabelComplete()`, `DMPlexLabelCohesiveComplete()`
266a6e0b375SMatthew G. Knepley @*/
2679371c9d4SSatish Balay PetscErrorCode DMPlexLabelAddFaceCells(DM dm, DMLabel label) {
268a6e0b375SMatthew G. Knepley   IS              valueIS;
269a6e0b375SMatthew G. Knepley   const PetscInt *values;
270a6e0b375SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd, fStart, fEnd;
271a6e0b375SMatthew G. Knepley 
272a6e0b375SMatthew G. Knepley   PetscFunctionBegin;
2739566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
2749566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
2759566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
2769566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
2779566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
278a6e0b375SMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
279a6e0b375SMatthew G. Knepley     IS              pointIS;
280a6e0b375SMatthew G. Knepley     const PetscInt *points;
281a6e0b375SMatthew G. Knepley     PetscInt        numPoints, p;
282a6e0b375SMatthew G. Knepley 
2839566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
2849566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
2859566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
286a6e0b375SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
287a6e0b375SMatthew G. Knepley       const PetscInt face    = points[p];
288a6e0b375SMatthew G. Knepley       PetscInt      *closure = NULL;
289a6e0b375SMatthew G. Knepley       PetscInt       closureSize, cl;
290a6e0b375SMatthew G. Knepley 
291a6e0b375SMatthew G. Knepley       if ((face < fStart) || (face >= fEnd)) continue;
2929566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, face, PETSC_FALSE, &closureSize, &closure));
293a6e0b375SMatthew G. Knepley       for (cl = closureSize - 1; cl > 0; --cl) {
294a6e0b375SMatthew G. Knepley         const PetscInt cell = closure[cl * 2];
2959371c9d4SSatish Balay         if ((cell >= cStart) && (cell < cEnd)) {
2969371c9d4SSatish Balay           PetscCall(DMLabelSetValue(label, cell, values[v]));
2979371c9d4SSatish Balay           break;
2989371c9d4SSatish Balay         }
299a6e0b375SMatthew G. Knepley       }
3009566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, face, PETSC_FALSE, &closureSize, &closure));
301a6e0b375SMatthew G. Knepley     }
3029566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
3039566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
304a6e0b375SMatthew G. Knepley   }
3059566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
3069566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
307a6e0b375SMatthew G. Knepley   PetscFunctionReturn(0);
308a6e0b375SMatthew G. Knepley }
309a6e0b375SMatthew G. Knepley 
310a6e0b375SMatthew G. Knepley /*@
311f402d5e4SToby Isaac   DMPlexLabelClearCells - Remove cells from a label
312f402d5e4SToby Isaac 
313f402d5e4SToby Isaac   Input Parameters:
314f402d5e4SToby Isaac + dm - The DM
315f402d5e4SToby Isaac - label - A DMLabel marking surface points and their adjacent cells
316f402d5e4SToby Isaac 
317f402d5e4SToby Isaac   Output Parameter:
318f402d5e4SToby Isaac . label - A DMLabel without cells
319f402d5e4SToby Isaac 
320f402d5e4SToby Isaac   Level: developer
321f402d5e4SToby Isaac 
322a6e0b375SMatthew G. Knepley   Note: This undoes DMPlexLabelAddCells() or DMPlexLabelAddFaceCells()
323f402d5e4SToby Isaac 
324db781477SPatrick Sanan .seealso: `DMPlexLabelComplete()`, `DMPlexLabelCohesiveComplete()`, `DMPlexLabelAddCells()`
325f402d5e4SToby Isaac @*/
3269371c9d4SSatish Balay PetscErrorCode DMPlexLabelClearCells(DM dm, DMLabel label) {
327f402d5e4SToby Isaac   IS              valueIS;
328f402d5e4SToby Isaac   const PetscInt *values;
329485ad865SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd;
330f402d5e4SToby Isaac 
331f402d5e4SToby Isaac   PetscFunctionBegin;
3329566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
3339566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
3349566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
3359566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
336f402d5e4SToby Isaac   for (v = 0; v < numValues; ++v) {
337f402d5e4SToby Isaac     IS              pointIS;
338f402d5e4SToby Isaac     const PetscInt *points;
339f402d5e4SToby Isaac     PetscInt        numPoints, p;
340f402d5e4SToby Isaac 
3419566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
3429566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
3439566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
344f402d5e4SToby Isaac     for (p = 0; p < numPoints; ++p) {
345f402d5e4SToby Isaac       PetscInt point = points[p];
346f402d5e4SToby Isaac 
34748a46eb9SPierre Jolivet       if (point >= cStart && point < cEnd) PetscCall(DMLabelClearValue(label, point, values[v]));
348f402d5e4SToby Isaac     }
3499566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
3509566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
351f402d5e4SToby Isaac   }
3529566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
3539566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
354f402d5e4SToby Isaac   PetscFunctionReturn(0);
355f402d5e4SToby Isaac }
356f402d5e4SToby Isaac 
35759eef20bSToby Isaac /* take (oldEnd, added) pairs, ordered by height and convert them to (oldstart, newstart) pairs, ordered by ascending
35859eef20bSToby Isaac  * index (skipping first, which is (0,0)) */
3599371c9d4SSatish Balay static inline PetscErrorCode DMPlexShiftPointSetUp_Internal(PetscInt depth, PetscInt depthShift[]) {
3602582d50cSToby Isaac   PetscInt d, off = 0;
3612582d50cSToby Isaac 
3622582d50cSToby Isaac   PetscFunctionBegin;
36359eef20bSToby Isaac   /* sort by (oldend): yes this is an O(n^2) sort, we expect depth <= 3 */
3640974a383SToby Isaac   for (d = 0; d < depth; d++) {
3652582d50cSToby Isaac     PetscInt firstd     = d;
3660974a383SToby Isaac     PetscInt firstStart = depthShift[2 * d];
3672582d50cSToby Isaac     PetscInt e;
3682582d50cSToby Isaac 
3692582d50cSToby Isaac     for (e = d + 1; e <= depth; e++) {
3702582d50cSToby Isaac       if (depthShift[2 * e] < firstStart) {
3712582d50cSToby Isaac         firstd     = e;
3722582d50cSToby Isaac         firstStart = depthShift[2 * d];
3732582d50cSToby Isaac       }
3742582d50cSToby Isaac     }
3752582d50cSToby Isaac     if (firstd != d) {
3762582d50cSToby Isaac       PetscInt swap[2];
3772582d50cSToby Isaac 
3782582d50cSToby Isaac       e                     = firstd;
3792582d50cSToby Isaac       swap[0]               = depthShift[2 * d];
3802582d50cSToby Isaac       swap[1]               = depthShift[2 * d + 1];
3812582d50cSToby Isaac       depthShift[2 * d]     = depthShift[2 * e];
3822582d50cSToby Isaac       depthShift[2 * d + 1] = depthShift[2 * e + 1];
3832582d50cSToby Isaac       depthShift[2 * e]     = swap[0];
3842582d50cSToby Isaac       depthShift[2 * e + 1] = swap[1];
3852582d50cSToby Isaac     }
3862582d50cSToby Isaac   }
3872582d50cSToby Isaac   /* convert (oldstart, added) to (oldstart, newstart) */
3880974a383SToby Isaac   for (d = 0; d <= depth; d++) {
3892582d50cSToby Isaac     off += depthShift[2 * d + 1];
39059eef20bSToby Isaac     depthShift[2 * d + 1] = depthShift[2 * d] + off;
3912582d50cSToby Isaac   }
3922582d50cSToby Isaac   PetscFunctionReturn(0);
3932582d50cSToby Isaac }
3942582d50cSToby Isaac 
3952582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
3969371c9d4SSatish Balay static inline PetscInt DMPlexShiftPoint_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[]) {
3972582d50cSToby Isaac   PetscInt d;
3982582d50cSToby Isaac   PetscInt newOff = 0;
3992582d50cSToby Isaac 
4002582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
4012582d50cSToby Isaac     if (p < depthShift[2 * d]) return p + newOff;
4022582d50cSToby Isaac     else newOff = depthShift[2 * d + 1] - depthShift[2 * d];
4032582d50cSToby Isaac   }
4040974a383SToby Isaac   return p + newOff;
4052582d50cSToby Isaac }
4062582d50cSToby Isaac 
4072582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
4089371c9d4SSatish Balay static inline PetscInt DMPlexShiftPointInverse_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[]) {
4092582d50cSToby Isaac   PetscInt d;
4102582d50cSToby Isaac   PetscInt newOff = 0;
4112582d50cSToby Isaac 
4122582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
4132582d50cSToby Isaac     if (p < depthShift[2 * d + 1]) return p + newOff;
4142582d50cSToby Isaac     else newOff = depthShift[2 * d] - depthShift[2 * d + 1];
4152582d50cSToby Isaac   }
4160974a383SToby Isaac   return p + newOff;
417cd0c2139SMatthew G Knepley }
418cd0c2139SMatthew G Knepley 
4199371c9d4SSatish Balay static PetscErrorCode DMPlexShiftSizes_Internal(DM dm, PetscInt depthShift[], DM dmNew) {
420cd0c2139SMatthew G Knepley   PetscInt depth = 0, d, pStart, pEnd, p;
421fa8e8ae5SToby Isaac   DMLabel  depthLabel;
422cd0c2139SMatthew G Knepley 
423cd0c2139SMatthew G Knepley   PetscFunctionBegin;
4249566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
425cd0c2139SMatthew G Knepley   if (depth < 0) PetscFunctionReturn(0);
426cd0c2139SMatthew G Knepley   /* Step 1: Expand chart */
4279566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
4280974a383SToby Isaac   pEnd = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
4299566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dmNew, pStart, pEnd));
4309566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dmNew, "depth"));
4319566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthLabel(dmNew, &depthLabel));
4329566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dmNew, "celltype"));
433cd0c2139SMatthew G Knepley   /* Step 2: Set cone and support sizes */
434cd0c2139SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
435fa8e8ae5SToby Isaac     PetscInt pStartNew, pEndNew;
436fa8e8ae5SToby Isaac     IS       pIS;
437fa8e8ae5SToby Isaac 
4389566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd));
439fa8e8ae5SToby Isaac     pStartNew = DMPlexShiftPoint_Internal(pStart, depth, depthShift);
440fa8e8ae5SToby Isaac     pEndNew   = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
4419566063dSJacob Faibussowitsch     PetscCall(ISCreateStride(PETSC_COMM_SELF, pEndNew - pStartNew, pStartNew, 1, &pIS));
4429566063dSJacob Faibussowitsch     PetscCall(DMLabelSetStratumIS(depthLabel, d, pIS));
4439566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pIS));
444cd0c2139SMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
4452582d50cSToby Isaac       PetscInt       newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
446cd0c2139SMatthew G Knepley       PetscInt       size;
447412e9a14SMatthew G. Knepley       DMPolytopeType ct;
448cd0c2139SMatthew G Knepley 
4499566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, p, &size));
4509566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(dmNew, newp, size));
4519566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, p, &size));
4529566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(dmNew, newp, size));
4539566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, p, &ct));
4549566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(dmNew, newp, ct));
455cd0c2139SMatthew G Knepley     }
456cd0c2139SMatthew G Knepley   }
457cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
458cd0c2139SMatthew G Knepley }
459cd0c2139SMatthew G Knepley 
4609371c9d4SSatish Balay static PetscErrorCode DMPlexShiftPoints_Internal(DM dm, PetscInt depthShift[], DM dmNew) {
4612582d50cSToby Isaac   PetscInt *newpoints;
4622582d50cSToby Isaac   PetscInt  depth = 0, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, pStart, pEnd, p;
463cd0c2139SMatthew G Knepley 
464cd0c2139SMatthew G Knepley   PetscFunctionBegin;
4659566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
466cd0c2139SMatthew G Knepley   if (depth < 0) PetscFunctionReturn(0);
4679566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
4689566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dmNew, &maxConeSizeNew, &maxSupportSizeNew));
4699566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(PetscMax(PetscMax(maxConeSize, maxSupportSize), PetscMax(maxConeSizeNew, maxSupportSizeNew)), &newpoints));
470cd0c2139SMatthew G Knepley   /* Step 5: Set cones and supports */
4719566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
472cd0c2139SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
473cd0c2139SMatthew G Knepley     const PetscInt *points = NULL, *orientations = NULL;
4742582d50cSToby Isaac     PetscInt        size, sizeNew, i, newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
475cd0c2139SMatthew G Knepley 
4769566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, p, &size));
4779566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, p, &points));
4789566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeOrientation(dm, p, &orientations));
479ad540459SPierre Jolivet     for (i = 0; i < size; ++i) newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
4809566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(dmNew, newp, newpoints));
4819566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeOrientation(dmNew, newp, orientations));
4829566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, p, &size));
4839566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dmNew, newp, &sizeNew));
4849566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, p, &points));
485ad540459SPierre Jolivet     for (i = 0; i < size; ++i) newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
486dcbb62e8SMatthew G. Knepley     for (i = size; i < sizeNew; ++i) newpoints[i] = 0;
4879566063dSJacob Faibussowitsch     PetscCall(DMPlexSetSupport(dmNew, newp, newpoints));
488cd0c2139SMatthew G Knepley   }
4899566063dSJacob Faibussowitsch   PetscCall(PetscFree(newpoints));
490cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
491cd0c2139SMatthew G Knepley }
492cd0c2139SMatthew G Knepley 
4939371c9d4SSatish Balay static PetscErrorCode DMPlexShiftCoordinates_Internal(DM dm, PetscInt depthShift[], DM dmNew) {
494cd0c2139SMatthew G Knepley   PetscSection coordSection, newCoordSection;
495cd0c2139SMatthew G Knepley   Vec          coordinates, newCoordinates;
496cd0c2139SMatthew G Knepley   PetscScalar *coords, *newCoords;
497f2b8cce1SMatthew G. Knepley   PetscInt     coordSize, sStart, sEnd;
498f2b8cce1SMatthew G. Knepley   PetscInt     dim, depth = 0, cStart, cEnd, cStartNew, cEndNew, c, vStart, vEnd, vStartNew, vEndNew, v;
499f2b8cce1SMatthew G. Knepley   PetscBool    hasCells;
500cd0c2139SMatthew G Knepley 
501cd0c2139SMatthew G Knepley   PetscFunctionBegin;
5029566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dim));
5039566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dmNew, dim));
5049566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
505cd0c2139SMatthew G Knepley   /* Step 8: Convert coordinates */
5069566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
5079566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
5089566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dmNew, 0, &vStartNew, &vEndNew));
5099566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dmNew, 0, &cStartNew, &cEndNew));
5109566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
5119566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &newCoordSection));
5129566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(newCoordSection, 1));
5139566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(newCoordSection, 0, dim));
5149566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(coordSection, &sStart, &sEnd));
515f2b8cce1SMatthew G. Knepley   hasCells = sStart == cStart ? PETSC_TRUE : PETSC_FALSE;
5169566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(newCoordSection, hasCells ? cStartNew : vStartNew, vEndNew));
517f2b8cce1SMatthew G. Knepley   if (hasCells) {
518f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
519f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof;
520f2b8cce1SMatthew G. Knepley 
5219566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, c, &dof));
5229566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(newCoordSection, cNew, dof));
5239566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(newCoordSection, cNew, 0, dof));
524f2b8cce1SMatthew G. Knepley     }
525f2b8cce1SMatthew G. Knepley   }
526cd0c2139SMatthew G Knepley   for (v = vStartNew; v < vEndNew; ++v) {
5279566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(newCoordSection, v, dim));
5289566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(newCoordSection, v, 0, dim));
529cd0c2139SMatthew G Knepley   }
5309566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(newCoordSection));
5319566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateSection(dmNew, PETSC_DETERMINE, newCoordSection));
5329566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(newCoordSection, &coordSize));
5339566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &newCoordinates));
5349566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)newCoordinates, "coordinates"));
5359566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(newCoordinates, coordSize, PETSC_DETERMINE));
5369566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(newCoordinates, dim));
5379566063dSJacob Faibussowitsch   PetscCall(VecSetType(newCoordinates, VECSTANDARD));
5389566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmNew, newCoordinates));
5399566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
5409566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
5419566063dSJacob Faibussowitsch   PetscCall(VecGetArray(newCoordinates, &newCoords));
542f2b8cce1SMatthew G. Knepley   if (hasCells) {
543f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
544f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof, off, noff, d;
545f2b8cce1SMatthew G. Knepley 
5469566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, c, &dof));
5479566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, c, &off));
5489566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(newCoordSection, cNew, &noff));
549f2b8cce1SMatthew G. Knepley       for (d = 0; d < dof; ++d) newCoords[noff + d] = coords[off + d];
550f2b8cce1SMatthew G. Knepley     }
551f2b8cce1SMatthew G. Knepley   }
552cd0c2139SMatthew G Knepley   for (v = vStart; v < vEnd; ++v) {
553cd0c2139SMatthew G Knepley     PetscInt dof, off, noff, d;
554cd0c2139SMatthew G Knepley 
5559566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, v, &dof));
5569566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, v, &off));
5579566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(newCoordSection, DMPlexShiftPoint_Internal(v, depth, depthShift), &noff));
558f2b8cce1SMatthew G. Knepley     for (d = 0; d < dof; ++d) newCoords[noff + d] = coords[off + d];
559cd0c2139SMatthew G Knepley   }
5609566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
5619566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(newCoordinates, &newCoords));
5629566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&newCoordinates));
5639566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&newCoordSection));
564cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
565cd0c2139SMatthew G Knepley }
566cd0c2139SMatthew G Knepley 
5679371c9d4SSatish Balay static PetscErrorCode DMPlexShiftSF_Single(DM dm, PetscInt depthShift[], PetscSF sf, PetscSF sfNew) {
568cd0c2139SMatthew G Knepley   const PetscSFNode *remotePoints;
569cd0c2139SMatthew G Knepley   PetscSFNode       *gremotePoints;
570cd0c2139SMatthew G Knepley   const PetscInt    *localPoints;
571cd0c2139SMatthew G Knepley   PetscInt          *glocalPoints, *newLocation, *newRemoteLocation;
5720e33faafSMatthew G. Knepley   PetscInt           numRoots, numLeaves, l, pStart, pEnd, depth = 0, totShift = 0;
573cd0c2139SMatthew G Knepley 
574cd0c2139SMatthew G Knepley   PetscFunctionBegin;
5759566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
5769566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
5779566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sf, &numRoots, &numLeaves, &localPoints, &remotePoints));
5786e21efdcSToby Isaac   totShift = DMPlexShiftPoint_Internal(pEnd, depth, depthShift) - pEnd;
579cd0c2139SMatthew G Knepley   if (numRoots >= 0) {
5809566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(numRoots, &newLocation, pEnd - pStart, &newRemoteLocation));
5810e33faafSMatthew G. Knepley     for (l = 0; l < numRoots; ++l) newLocation[l] = DMPlexShiftPoint_Internal(l, depth, depthShift);
5829566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, newLocation, newRemoteLocation, MPI_REPLACE));
5839566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, newLocation, newRemoteLocation, MPI_REPLACE));
5849566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numLeaves, &glocalPoints));
5859566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numLeaves, &gremotePoints));
586cd0c2139SMatthew G Knepley     for (l = 0; l < numLeaves; ++l) {
5872582d50cSToby Isaac       glocalPoints[l]        = DMPlexShiftPoint_Internal(localPoints[l], depth, depthShift);
588cd0c2139SMatthew G Knepley       gremotePoints[l].rank  = remotePoints[l].rank;
589cd0c2139SMatthew G Knepley       gremotePoints[l].index = newRemoteLocation[localPoints[l]];
590cd0c2139SMatthew G Knepley     }
5919566063dSJacob Faibussowitsch     PetscCall(PetscFree2(newLocation, newRemoteLocation));
5929566063dSJacob Faibussowitsch     PetscCall(PetscSFSetGraph(sfNew, numRoots + totShift, numLeaves, glocalPoints, PETSC_OWN_POINTER, gremotePoints, PETSC_OWN_POINTER));
5930e33faafSMatthew G. Knepley   }
5940e33faafSMatthew G. Knepley   PetscFunctionReturn(0);
5950e33faafSMatthew G. Knepley }
5960e33faafSMatthew G. Knepley 
5979371c9d4SSatish Balay static PetscErrorCode DMPlexShiftSF_Internal(DM dm, PetscInt depthShift[], DM dmNew) {
5980e33faafSMatthew G. Knepley   PetscSF   sfPoint, sfPointNew;
5990e33faafSMatthew G. Knepley   PetscBool useNatural;
6000e33faafSMatthew G. Knepley 
6010e33faafSMatthew G. Knepley   PetscFunctionBegin;
6020e33faafSMatthew G. Knepley   /* Step 9: Convert pointSF */
6039566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
6049566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmNew, &sfPointNew));
6059566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Single(dm, depthShift, sfPoint, sfPointNew));
6060e33faafSMatthew G. Knepley   /* Step 9b: Convert naturalSF */
6079566063dSJacob Faibussowitsch   PetscCall(DMGetUseNatural(dm, &useNatural));
6080e33faafSMatthew G. Knepley   if (useNatural) {
6090e33faafSMatthew G. Knepley     PetscSF sfNat, sfNatNew;
6100e33faafSMatthew G. Knepley 
6119566063dSJacob Faibussowitsch     PetscCall(DMSetUseNatural(dmNew, useNatural));
6129566063dSJacob Faibussowitsch     PetscCall(DMGetNaturalSF(dm, &sfNat));
6139566063dSJacob Faibussowitsch     PetscCall(DMGetNaturalSF(dmNew, &sfNatNew));
6149566063dSJacob Faibussowitsch     PetscCall(DMPlexShiftSF_Single(dm, depthShift, sfNat, sfNatNew));
615cd0c2139SMatthew G Knepley   }
616cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
617cd0c2139SMatthew G Knepley }
618cd0c2139SMatthew G Knepley 
6199371c9d4SSatish Balay static PetscErrorCode DMPlexShiftLabels_Internal(DM dm, PetscInt depthShift[], DM dmNew) {
620d56405f8SMatthew G. Knepley   PetscInt depth = 0, numLabels, l;
621cd0c2139SMatthew G Knepley 
622cd0c2139SMatthew G Knepley   PetscFunctionBegin;
6239566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
624cd0c2139SMatthew G Knepley   /* Step 10: Convert labels */
6259566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &numLabels));
626cd0c2139SMatthew G Knepley   for (l = 0; l < numLabels; ++l) {
627cd0c2139SMatthew G Knepley     DMLabel         label, newlabel;
628cd0c2139SMatthew G Knepley     const char     *lname;
629fa8e8ae5SToby Isaac     PetscBool       isDepth, isDim;
630cd0c2139SMatthew G Knepley     IS              valueIS;
631cd0c2139SMatthew G Knepley     const PetscInt *values;
632cd0c2139SMatthew G Knepley     PetscInt        numValues, val;
633cd0c2139SMatthew G Knepley 
6349566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &lname));
6359566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
636cd0c2139SMatthew G Knepley     if (isDepth) continue;
6379566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "dim", &isDim));
638fa8e8ae5SToby Isaac     if (isDim) continue;
6399566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dmNew, lname));
6409566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, lname, &label));
6419566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmNew, lname, &newlabel));
6429566063dSJacob Faibussowitsch     PetscCall(DMLabelGetDefaultValue(label, &val));
6439566063dSJacob Faibussowitsch     PetscCall(DMLabelSetDefaultValue(newlabel, val));
6449566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
6459566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numValues));
6469566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
647cd0c2139SMatthew G Knepley     for (val = 0; val < numValues; ++val) {
648cd0c2139SMatthew G Knepley       IS              pointIS;
649cd0c2139SMatthew G Knepley       const PetscInt *points;
650cd0c2139SMatthew G Knepley       PetscInt        numPoints, p;
651cd0c2139SMatthew G Knepley 
6529566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, values[val], &pointIS));
6539566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &numPoints));
6549566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
655cd0c2139SMatthew G Knepley       for (p = 0; p < numPoints; ++p) {
6562582d50cSToby Isaac         const PetscInt newpoint = DMPlexShiftPoint_Internal(points[p], depth, depthShift);
657cd0c2139SMatthew G Knepley 
6589566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(newlabel, newpoint, values[val]));
659cd0c2139SMatthew G Knepley       }
6609566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(pointIS, &points));
6619566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
662cd0c2139SMatthew G Knepley     }
6639566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
6649566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
665cd0c2139SMatthew G Knepley   }
666d56405f8SMatthew G. Knepley   PetscFunctionReturn(0);
667d56405f8SMatthew G. Knepley }
668d56405f8SMatthew G. Knepley 
6699371c9d4SSatish Balay static PetscErrorCode DMPlexCreateVTKLabel_Internal(DM dm, PetscBool createGhostLabel, DM dmNew) {
670d56405f8SMatthew G. Knepley   PetscSF            sfPoint;
671d56405f8SMatthew G. Knepley   DMLabel            vtkLabel, ghostLabel = NULL;
672d56405f8SMatthew G. Knepley   const PetscSFNode *leafRemote;
673d56405f8SMatthew G. Knepley   const PetscInt    *leafLocal;
674d56405f8SMatthew G. Knepley   PetscInt           cellHeight, cStart, cEnd, c, fStart, fEnd, f, numLeaves, l;
675d56405f8SMatthew G. Knepley   PetscMPIInt        rank;
676d56405f8SMatthew G. Knepley 
677d56405f8SMatthew G. Knepley   PetscFunctionBegin;
678cd0c2139SMatthew G Knepley   /* Step 11: Make label for output (vtk) and to mark ghost points (ghost) */
6799566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
6809566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
6819566063dSJacob Faibussowitsch   PetscCall(DMPlexGetVTKCellHeight(dmNew, &cellHeight));
6829566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd));
6839566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfPoint, NULL, &numLeaves, &leafLocal, &leafRemote));
6849566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dmNew, "vtk"));
6859566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dmNew, "vtk", &vtkLabel));
686d56405f8SMatthew G. Knepley   if (createGhostLabel) {
6879566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dmNew, "ghost"));
6889566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmNew, "ghost", &ghostLabel));
689d56405f8SMatthew G. Knepley   }
690cd0c2139SMatthew G Knepley   for (l = 0, c = cStart; l < numLeaves && c < cEnd; ++l, ++c) {
69148a46eb9SPierre Jolivet     for (; c < leafLocal[l] && c < cEnd; ++c) PetscCall(DMLabelSetValue(vtkLabel, c, 1));
692cd0c2139SMatthew G Knepley     if (leafLocal[l] >= cEnd) break;
693cd0c2139SMatthew G Knepley     if (leafRemote[l].rank == rank) {
6949566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(vtkLabel, c, 1));
6951baa6e33SBarry Smith     } else if (ghostLabel) PetscCall(DMLabelSetValue(ghostLabel, c, 2));
696cd0c2139SMatthew G Knepley   }
69748a46eb9SPierre Jolivet   for (; c < cEnd; ++c) PetscCall(DMLabelSetValue(vtkLabel, c, 1));
698d56405f8SMatthew G. Knepley   if (ghostLabel) {
6999566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dmNew, 1, &fStart, &fEnd));
700cd0c2139SMatthew G Knepley     for (f = fStart; f < fEnd; ++f) {
701cd0c2139SMatthew G Knepley       PetscInt numCells;
702cd0c2139SMatthew G Knepley 
7039566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dmNew, f, &numCells));
704cd0c2139SMatthew G Knepley       if (numCells < 2) {
7059566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(ghostLabel, f, 1));
706cd0c2139SMatthew G Knepley       } else {
707cd0c2139SMatthew G Knepley         const PetscInt *cells = NULL;
708cd0c2139SMatthew G Knepley         PetscInt        vA, vB;
709cd0c2139SMatthew G Knepley 
7109566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dmNew, f, &cells));
7119566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(vtkLabel, cells[0], &vA));
7129566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(vtkLabel, cells[1], &vB));
7139566063dSJacob Faibussowitsch         if (vA != 1 && vB != 1) PetscCall(DMLabelSetValue(ghostLabel, f, 1));
714cd0c2139SMatthew G Knepley       }
715cd0c2139SMatthew G Knepley     }
716d56405f8SMatthew G. Knepley   }
717cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
718cd0c2139SMatthew G Knepley }
719cd0c2139SMatthew G Knepley 
7209371c9d4SSatish Balay static PetscErrorCode DMPlexShiftTree_Internal(DM dm, PetscInt depthShift[], DM dmNew) {
721ca04dac2SToby Isaac   DM           refTree;
722ca04dac2SToby Isaac   PetscSection pSec;
723ca04dac2SToby Isaac   PetscInt    *parents, *childIDs;
724ca04dac2SToby Isaac 
725ca04dac2SToby Isaac   PetscFunctionBegin;
7269566063dSJacob Faibussowitsch   PetscCall(DMPlexGetReferenceTree(dm, &refTree));
7279566063dSJacob Faibussowitsch   PetscCall(DMPlexSetReferenceTree(dmNew, refTree));
7289566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTree(dm, &pSec, &parents, &childIDs, NULL, NULL));
729ca04dac2SToby Isaac   if (pSec) {
7302582d50cSToby Isaac     PetscInt     p, pStart, pEnd, *parentsShifted, pStartShifted, pEndShifted, depth;
731fb4630b5SToby Isaac     PetscInt    *childIDsShifted;
732ca04dac2SToby Isaac     PetscSection pSecShifted;
733ca04dac2SToby Isaac 
7349566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(pSec, &pStart, &pEnd));
7359566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(dm, &depth));
7362582d50cSToby Isaac     pStartShifted = DMPlexShiftPoint_Internal(pStart, depth, depthShift);
7372582d50cSToby Isaac     pEndShifted   = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
7389566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(pEndShifted - pStartShifted, &parentsShifted, pEndShifted - pStartShifted, &childIDsShifted));
7399566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dmNew), &pSecShifted));
7409566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(pSecShifted, pStartShifted, pEndShifted));
741ca04dac2SToby Isaac     for (p = pStartShifted; p < pEndShifted; p++) {
742fb4630b5SToby Isaac       /* start off assuming no children */
7439566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(pSecShifted, p, 0));
744fb4630b5SToby Isaac     }
745fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
746fb4630b5SToby Isaac       PetscInt dof;
747fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p, depth, depthShift);
748ca04dac2SToby Isaac 
7499566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(pSec, p, &dof));
7509566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(pSecShifted, pNew, dof));
751ca04dac2SToby Isaac     }
7529566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(pSecShifted));
753fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
754fb4630b5SToby Isaac       PetscInt dof;
755fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p, depth, depthShift);
756fb4630b5SToby Isaac 
7579566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(pSec, p, &dof));
758fb4630b5SToby Isaac       if (dof) {
759fb4630b5SToby Isaac         PetscInt off, offNew;
760fb4630b5SToby Isaac 
7619566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(pSec, p, &off));
7629566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(pSecShifted, pNew, &offNew));
763fb4630b5SToby Isaac         parentsShifted[offNew]  = DMPlexShiftPoint_Internal(parents[off], depth, depthShift);
764fb4630b5SToby Isaac         childIDsShifted[offNew] = childIDs[off];
765fb4630b5SToby Isaac       }
766fb4630b5SToby Isaac     }
7679566063dSJacob Faibussowitsch     PetscCall(DMPlexSetTree(dmNew, pSecShifted, parentsShifted, childIDsShifted));
7689566063dSJacob Faibussowitsch     PetscCall(PetscFree2(parentsShifted, childIDsShifted));
7699566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&pSecShifted));
770ca04dac2SToby Isaac   }
771ca04dac2SToby Isaac   PetscFunctionReturn(0);
772ca04dac2SToby Isaac }
773ca04dac2SToby Isaac 
7749371c9d4SSatish Balay static PetscErrorCode DMPlexConstructGhostCells_Internal(DM dm, DMLabel label, PetscInt *numGhostCells, DM gdm) {
775da97024aSMatthew G. Knepley   PetscSF          sf;
776cd0c2139SMatthew G Knepley   IS               valueIS;
777da97024aSMatthew G. Knepley   const PetscInt  *values, *leaves;
778cd0c2139SMatthew G Knepley   PetscInt        *depthShift;
7792582d50cSToby Isaac   PetscInt         d, depth = 0, nleaves, loc, Ng, numFS, fs, fStart, fEnd, ghostCell, cEnd, c;
7804fb89dddSMatthew G. Knepley   const PetscReal *maxCell, *Lstart, *L;
781cd0c2139SMatthew G Knepley 
782cd0c2139SMatthew G Knepley   PetscFunctionBegin;
7839566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
7849566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL));
785da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
7869566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
787cd0c2139SMatthew G Knepley   /* Count ghost cells */
7889566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
7899566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(valueIS, &numFS));
7909566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
7914a6cfa73SMatthew G. Knepley   Ng = 0;
792cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
79346c796b9SMatthew G. Knepley     IS              faceIS;
79446c796b9SMatthew G. Knepley     const PetscInt *faces;
79546c796b9SMatthew G. Knepley     PetscInt        numFaces, f, numBdFaces = 0;
796cd0c2139SMatthew G Knepley 
7979566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
7989566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
7999566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
80046c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
801ca04dac2SToby Isaac       PetscInt numChildren;
802ca04dac2SToby Isaac 
8039566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
8049566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm, faces[f], &numChildren, NULL));
805ca04dac2SToby Isaac       /* non-local and ancestors points don't get to register ghosts */
806ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
80746c796b9SMatthew G. Knepley       if ((faces[f] >= fStart) && (faces[f] < fEnd)) ++numBdFaces;
80846c796b9SMatthew G. Knepley     }
8094a6cfa73SMatthew G. Knepley     Ng += numBdFaces;
8109566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
8119566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
812cd0c2139SMatthew G Knepley   }
8139566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
8149566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(2 * (depth + 1), &depthShift));
8152582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
81659eef20bSToby Isaac     PetscInt dEnd;
8172582d50cSToby Isaac 
8189566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, NULL, &dEnd));
81959eef20bSToby Isaac     depthShift[2 * d]     = dEnd;
8202582d50cSToby Isaac     depthShift[2 * d + 1] = 0;
8212582d50cSToby Isaac   }
8222582d50cSToby Isaac   if (depth >= 0) depthShift[2 * depth + 1] = Ng;
8239566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPointSetUp_Internal(depth, depthShift));
8249566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSizes_Internal(dm, depthShift, gdm));
825cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
8269566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, NULL, &cEnd));
82748a46eb9SPierre Jolivet   for (c = cEnd; c < cEnd + Ng; ++c) PetscCall(DMPlexSetConeSize(gdm, c, 1));
828cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
829cd0c2139SMatthew G Knepley     IS              faceIS;
830cd0c2139SMatthew G Knepley     const PetscInt *faces;
831cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
832cd0c2139SMatthew G Knepley 
8339566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
8349566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
8359566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
836cd0c2139SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
837ca04dac2SToby Isaac       PetscInt size, numChildren;
838cd0c2139SMatthew G Knepley 
8399566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
8409566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm, faces[f], &numChildren, NULL));
841ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
84246c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
8439566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, faces[f], &size));
84463a3b9bcSJacob Faibussowitsch       PetscCheck(size == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM has boundary face %" PetscInt_FMT " with %" PetscInt_FMT " support cells", faces[f], size);
8459566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(gdm, faces[f] + Ng, 2));
846cd0c2139SMatthew G Knepley     }
8479566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
8489566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
849cd0c2139SMatthew G Knepley   }
850cd0c2139SMatthew G Knepley   /* Step 4: Setup ghosted DM */
8519566063dSJacob Faibussowitsch   PetscCall(DMSetUp(gdm));
8529566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPoints_Internal(dm, depthShift, gdm));
853cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
854cd0c2139SMatthew G Knepley   ghostCell = cEnd;
855cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
856cd0c2139SMatthew G Knepley     IS              faceIS;
857cd0c2139SMatthew G Knepley     const PetscInt *faces;
858cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
859cd0c2139SMatthew G Knepley 
8609566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
8619566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
8629566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
86346c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
864ca04dac2SToby Isaac       PetscInt newFace = faces[f] + Ng, numChildren;
865cd0c2139SMatthew G Knepley 
8669566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
8679566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm, faces[f], &numChildren, NULL));
868ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
86946c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
8709566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(gdm, ghostCell, &newFace));
8719566063dSJacob Faibussowitsch       PetscCall(DMPlexInsertSupport(gdm, newFace, 1, ghostCell));
87246c796b9SMatthew G. Knepley       ++ghostCell;
873cd0c2139SMatthew G Knepley     }
8749566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
8759566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
876cd0c2139SMatthew G Knepley   }
8779566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
8789566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
8799566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftCoordinates_Internal(dm, depthShift, gdm));
8809566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Internal(dm, depthShift, gdm));
8819566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftLabels_Internal(dm, depthShift, gdm));
8829566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateVTKLabel_Internal(dm, PETSC_TRUE, gdm));
8839566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftTree_Internal(dm, depthShift, gdm));
8849566063dSJacob Faibussowitsch   PetscCall(PetscFree(depthShift));
88548a46eb9SPierre Jolivet   for (c = cEnd; c < cEnd + Ng; ++c) PetscCall(DMPlexSetCellType(gdm, c, DM_POLYTOPE_FV_GHOST));
886966c7b3fSMatthew G. Knepley   /* Step 7: Periodicity */
8874fb89dddSMatthew G. Knepley   PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
8884fb89dddSMatthew G. Knepley   PetscCall(DMSetPeriodicity(gdm, maxCell, Lstart, L));
8894a6cfa73SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
890cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
891cd0c2139SMatthew G Knepley }
892cd0c2139SMatthew G Knepley 
893cd0c2139SMatthew G Knepley /*@C
894cd0c2139SMatthew G Knepley   DMPlexConstructGhostCells - Construct ghost cells which connect to every boundary face
895cd0c2139SMatthew G Knepley 
896cd0c2139SMatthew G Knepley   Collective on dm
897cd0c2139SMatthew G Knepley 
898cd0c2139SMatthew G Knepley   Input Parameters:
899cd0c2139SMatthew G Knepley + dm - The original DM
900cd0c2139SMatthew G Knepley - labelName - The label specifying the boundary faces, or "Face Sets" if this is NULL
901cd0c2139SMatthew G Knepley 
902cd0c2139SMatthew G Knepley   Output Parameters:
903cd0c2139SMatthew G Knepley + numGhostCells - The number of ghost cells added to the DM
904cd0c2139SMatthew G Knepley - dmGhosted - The new DM
905cd0c2139SMatthew G Knepley 
906cd0c2139SMatthew G Knepley   Note: If no label exists of that name, one will be created marking all boundary faces
907cd0c2139SMatthew G Knepley 
908cd0c2139SMatthew G Knepley   Level: developer
909cd0c2139SMatthew G Knepley 
910db781477SPatrick Sanan .seealso: `DMCreate()`
91131266bc0SMatthew G. Knepley @*/
9129371c9d4SSatish Balay PetscErrorCode DMPlexConstructGhostCells(DM dm, const char labelName[], PetscInt *numGhostCells, DM *dmGhosted) {
913cd0c2139SMatthew G Knepley   DM          gdm;
914cd0c2139SMatthew G Knepley   DMLabel     label;
915cd0c2139SMatthew G Knepley   const char *name = labelName ? labelName : "Face Sets";
916412e9a14SMatthew G. Knepley   PetscInt    dim, Ng = 0;
917b0441da4SMatthew G. Knepley   PetscBool   useCone, useClosure;
918cd0c2139SMatthew G Knepley 
919cd0c2139SMatthew G Knepley   PetscFunctionBegin;
920cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
921dadcf809SJacob Faibussowitsch   if (numGhostCells) PetscValidIntPointer(numGhostCells, 3);
922cd0c2139SMatthew G Knepley   PetscValidPointer(dmGhosted, 4);
9239566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &gdm));
9249566063dSJacob Faibussowitsch   PetscCall(DMSetType(gdm, DMPLEX));
9259566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
9269566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(gdm, dim));
9279566063dSJacob Faibussowitsch   PetscCall(DMGetBasicAdjacency(dm, &useCone, &useClosure));
9289566063dSJacob Faibussowitsch   PetscCall(DMSetBasicAdjacency(gdm, useCone, useClosure));
9299566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
930cd0c2139SMatthew G Knepley   if (!label) {
931cd0c2139SMatthew G Knepley     /* Get label for boundary faces */
9329566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
9339566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
9349566063dSJacob Faibussowitsch     PetscCall(DMPlexMarkBoundaryFaces(dm, 1, label));
935cd0c2139SMatthew G Knepley   }
9369566063dSJacob Faibussowitsch   PetscCall(DMPlexConstructGhostCells_Internal(dm, label, &Ng, gdm));
9379566063dSJacob Faibussowitsch   PetscCall(DMCopyDisc(dm, gdm));
9385de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, gdm));
939cad26855SMatthew G. Knepley   gdm->setfromoptionscalled = dm->setfromoptionscalled;
940d80ece95SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
941cd0c2139SMatthew G Knepley   *dmGhosted = gdm;
942cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
943cd0c2139SMatthew G Knepley }
944cd0c2139SMatthew G Knepley 
9459371c9d4SSatish Balay static PetscErrorCode DivideCells_Private(DM dm, DMLabel label, DMPlexPointQueue queue) {
946accc9626SMatthew G. Knepley   PetscInt dim, d, shift = 100, *pStart, *pEnd;
947accc9626SMatthew G. Knepley 
948accc9626SMatthew G. Knepley   PetscFunctionBegin;
949accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
950accc9626SMatthew G. Knepley   PetscCall(PetscMalloc2(dim, &pStart, dim, &pEnd));
95148a46eb9SPierre Jolivet   for (d = 0; d < dim; ++d) PetscCall(DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]));
952accc9626SMatthew G. Knepley   while (!DMPlexPointQueueEmpty(queue)) {
953accc9626SMatthew G. Knepley     PetscInt  cell    = -1;
954accc9626SMatthew G. Knepley     PetscInt *closure = NULL;
955accc9626SMatthew G. Knepley     PetscInt  closureSize, cl, cval;
956accc9626SMatthew G. Knepley 
957accc9626SMatthew G. Knepley     PetscCall(DMPlexPointQueueDequeue(queue, &cell));
958accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, cell, &cval));
959accc9626SMatthew G. Knepley     PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
960accc9626SMatthew G. Knepley     /* Mark points in the cell closure that touch the fault */
961accc9626SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
962accc9626SMatthew G. Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
963accc9626SMatthew G. Knepley         const PetscInt clp = closure[cl];
964accc9626SMatthew G. Knepley         PetscInt       clval;
965accc9626SMatthew G. Knepley 
966accc9626SMatthew G. Knepley         if ((clp < pStart[d]) || (clp >= pEnd[d])) continue;
967accc9626SMatthew G. Knepley         PetscCall(DMLabelGetValue(label, clp, &clval));
968accc9626SMatthew G. Knepley         if (clval == -1) {
969accc9626SMatthew G. Knepley           const PetscInt *cone;
970accc9626SMatthew G. Knepley           PetscInt        coneSize, c;
971accc9626SMatthew G. Knepley 
972accc9626SMatthew G. Knepley           /* If a cone point touches the fault, then this point touches the fault */
973accc9626SMatthew G. Knepley           PetscCall(DMPlexGetCone(dm, clp, &cone));
974accc9626SMatthew G. Knepley           PetscCall(DMPlexGetConeSize(dm, clp, &coneSize));
975accc9626SMatthew G. Knepley           for (c = 0; c < coneSize; ++c) {
976accc9626SMatthew G. Knepley             PetscInt cpval;
977accc9626SMatthew G. Knepley 
978accc9626SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, cone[c], &cpval));
979accc9626SMatthew G. Knepley             if (cpval != -1) {
980accc9626SMatthew G. Knepley               PetscInt dep;
981accc9626SMatthew G. Knepley 
982accc9626SMatthew G. Knepley               PetscCall(DMPlexGetPointDepth(dm, clp, &dep));
983accc9626SMatthew G. Knepley               clval = cval < 0 ? -(shift + dep) : shift + dep;
984accc9626SMatthew G. Knepley               PetscCall(DMLabelSetValue(label, clp, clval));
985accc9626SMatthew G. Knepley               break;
986accc9626SMatthew G. Knepley             }
987accc9626SMatthew G. Knepley           }
988accc9626SMatthew G. Knepley         }
989accc9626SMatthew G. Knepley         /* Mark neighbor cells through marked faces (these cells must also touch the fault) */
990accc9626SMatthew G. Knepley         if (d == dim - 1 && clval != -1) {
991accc9626SMatthew G. Knepley           const PetscInt *support;
992accc9626SMatthew G. Knepley           PetscInt        supportSize, s, nval;
993accc9626SMatthew G. Knepley 
994accc9626SMatthew G. Knepley           PetscCall(DMPlexGetSupport(dm, clp, &support));
995accc9626SMatthew G. Knepley           PetscCall(DMPlexGetSupportSize(dm, clp, &supportSize));
996accc9626SMatthew G. Knepley           for (s = 0; s < supportSize; ++s) {
997accc9626SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, support[s], &nval));
998accc9626SMatthew G. Knepley             if (nval == -1) {
999accc9626SMatthew G. Knepley               PetscCall(DMLabelSetValue(label, support[s], clval < 0 ? clval - 1 : clval + 1));
1000accc9626SMatthew G. Knepley               PetscCall(DMPlexPointQueueEnqueue(queue, support[s]));
1001accc9626SMatthew G. Knepley             }
1002accc9626SMatthew G. Knepley           }
1003accc9626SMatthew G. Knepley         }
1004accc9626SMatthew G. Knepley       }
1005accc9626SMatthew G. Knepley     }
1006accc9626SMatthew G. Knepley     PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
1007accc9626SMatthew G. Knepley   }
1008accc9626SMatthew G. Knepley   PetscCall(PetscFree2(pStart, pEnd));
1009accc9626SMatthew G. Knepley   PetscFunctionReturn(0);
1010accc9626SMatthew G. Knepley }
1011accc9626SMatthew G. Knepley 
1012accc9626SMatthew G. Knepley typedef struct {
1013accc9626SMatthew G. Knepley   DM               dm;
1014accc9626SMatthew G. Knepley   DMPlexPointQueue queue;
1015accc9626SMatthew G. Knepley } PointDivision;
1016accc9626SMatthew G. Knepley 
10179371c9d4SSatish Balay static PetscErrorCode divideCell(DMLabel label, PetscInt p, PetscInt val, void *ctx) {
1018accc9626SMatthew G. Knepley   PointDivision  *div  = (PointDivision *)ctx;
1019accc9626SMatthew G. Knepley   PetscInt        cval = val < 0 ? val - 1 : val + 1;
1020accc9626SMatthew G. Knepley   const PetscInt *support;
1021accc9626SMatthew G. Knepley   PetscInt        supportSize, s;
1022accc9626SMatthew G. Knepley 
1023accc9626SMatthew G. Knepley   PetscFunctionBegin;
1024accc9626SMatthew G. Knepley   PetscCall(DMPlexGetSupport(div->dm, p, &support));
1025accc9626SMatthew G. Knepley   PetscCall(DMPlexGetSupportSize(div->dm, p, &supportSize));
1026accc9626SMatthew G. Knepley   for (s = 0; s < supportSize; ++s) {
1027accc9626SMatthew G. Knepley     PetscCall(DMLabelSetValue(label, support[s], cval));
1028accc9626SMatthew G. Knepley     PetscCall(DMPlexPointQueueEnqueue(div->queue, support[s]));
1029accc9626SMatthew G. Knepley   }
1030accc9626SMatthew G. Knepley   PetscFunctionReturn(0);
1031accc9626SMatthew G. Knepley }
1032accc9626SMatthew G. Knepley 
1033accc9626SMatthew G. Knepley /* Mark cells by label propagation */
10349371c9d4SSatish Balay static PetscErrorCode DMPlexLabelFaultHalo(DM dm, DMLabel faultLabel) {
1035accc9626SMatthew G. Knepley   DMPlexPointQueue queue = NULL;
1036accc9626SMatthew G. Knepley   PointDivision    div;
1037accc9626SMatthew G. Knepley   PetscSF          pointSF;
1038accc9626SMatthew G. Knepley   IS               pointIS;
1039accc9626SMatthew G. Knepley   const PetscInt  *points;
1040accc9626SMatthew G. Knepley   PetscBool        empty;
1041accc9626SMatthew G. Knepley   PetscInt         dim, shift = 100, n, i;
1042accc9626SMatthew G. Knepley 
1043accc9626SMatthew G. Knepley   PetscFunctionBegin;
1044accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
1045accc9626SMatthew G. Knepley   PetscCall(DMPlexPointQueueCreate(1024, &queue));
1046accc9626SMatthew G. Knepley   div.dm    = dm;
1047accc9626SMatthew G. Knepley   div.queue = queue;
1048accc9626SMatthew G. Knepley   /* Enqueue cells on fault */
1049accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(faultLabel, shift + dim, &pointIS));
1050accc9626SMatthew G. Knepley   if (pointIS) {
1051accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(pointIS, &n));
1052accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(pointIS, &points));
105348a46eb9SPierre Jolivet     for (i = 0; i < n; ++i) PetscCall(DMPlexPointQueueEnqueue(queue, points[i]));
1054accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(pointIS, &points));
1055accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&pointIS));
1056accc9626SMatthew G. Knepley   }
1057accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(faultLabel, -(shift + dim), &pointIS));
1058accc9626SMatthew G. Knepley   if (pointIS) {
1059accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(pointIS, &n));
1060accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(pointIS, &points));
106148a46eb9SPierre Jolivet     for (i = 0; i < n; ++i) PetscCall(DMPlexPointQueueEnqueue(queue, points[i]));
1062accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(pointIS, &points));
1063accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&pointIS));
1064accc9626SMatthew G. Knepley   }
1065accc9626SMatthew G. Knepley 
1066accc9626SMatthew G. Knepley   PetscCall(DMGetPointSF(dm, &pointSF));
1067accc9626SMatthew G. Knepley   PetscCall(DMLabelPropagateBegin(faultLabel, pointSF));
1068accc9626SMatthew G. Knepley   /* While edge queue is not empty: */
1069accc9626SMatthew G. Knepley   PetscCall(DMPlexPointQueueEmptyCollective((PetscObject)dm, queue, &empty));
1070accc9626SMatthew G. Knepley   while (!empty) {
1071accc9626SMatthew G. Knepley     PetscCall(DivideCells_Private(dm, faultLabel, queue));
1072accc9626SMatthew G. Knepley     PetscCall(DMLabelPropagatePush(faultLabel, pointSF, divideCell, &div));
1073accc9626SMatthew G. Knepley     PetscCall(DMPlexPointQueueEmptyCollective((PetscObject)dm, queue, &empty));
1074accc9626SMatthew G. Knepley   }
1075accc9626SMatthew G. Knepley   PetscCall(DMLabelPropagateEnd(faultLabel, pointSF));
1076accc9626SMatthew G. Knepley   PetscCall(DMPlexPointQueueDestroy(&queue));
1077accc9626SMatthew G. Knepley   PetscFunctionReturn(0);
1078accc9626SMatthew G. Knepley }
1079accc9626SMatthew G. Knepley 
1080607ab7a9SMatthew G. Knepley /*
1081faedd622SMatthew G. Knepley   We are adding three kinds of points here:
1082607ab7a9SMatthew G. Knepley     Replicated:     Copies of points which exist in the mesh, such as vertices identified across a fault
1083faedd622SMatthew G. Knepley     Non-replicated: Points which exist on the fault, but are not replicated
1084b6dfa339SMatthew G. Knepley     Ghost:          These are shared fault faces which are not owned by this process. These do not produce hybrid cells and do not replicate
1085607ab7a9SMatthew G. Knepley     Hybrid:         Entirely new points, such as cohesive cells
1086a6ae58d1SMatthew G. Knepley 
1087a6ae58d1SMatthew G. Knepley   When creating subsequent cohesive cells, we shift the old hybrid cells to the end of the numbering at
1088a6ae58d1SMatthew G. Knepley   each depth so that the new split/hybrid points can be inserted as a block.
1089607ab7a9SMatthew G. Knepley */
10909371c9d4SSatish Balay static PetscErrorCode DMPlexConstructCohesiveCells_Internal(DM dm, DMLabel label, DMLabel splitLabel, DM sdm) {
1091cd0c2139SMatthew G Knepley   MPI_Comm         comm;
1092607ab7a9SMatthew G. Knepley   IS               valueIS;
1093607ab7a9SMatthew G. Knepley   PetscInt         numSP = 0; /* The number of depths for which we have replicated points */
1094607ab7a9SMatthew G. Knepley   const PetscInt  *values;    /* List of depths for which we have replicated points */
109518c5995bSMatthew G. Knepley   IS              *splitIS;
109618c5995bSMatthew G. Knepley   IS              *unsplitIS;
1097b6dfa339SMatthew G. Knepley   IS               ghostIS;
1098607ab7a9SMatthew G. Knepley   PetscInt        *numSplitPoints;     /* The number of replicated points at each depth */
109918c5995bSMatthew G. Knepley   PetscInt        *numUnsplitPoints;   /* The number of non-replicated points at each depth which still give rise to hybrid points */
110036dbac82SMatthew G. Knepley   PetscInt        *numHybridPoints;    /* The number of new hybrid points at each depth */
110136dbac82SMatthew G. Knepley   PetscInt        *numHybridPointsOld; /* The number of existing hybrid points at each depth */
1102b6dfa339SMatthew G. Knepley   PetscInt         numGhostPoints;     /* The number of unowned, shared fault faces */
1103607ab7a9SMatthew G. Knepley   const PetscInt **splitPoints;        /* Replicated points for each depth */
110418c5995bSMatthew G. Knepley   const PetscInt **unsplitPoints;      /* Non-replicated points for each depth */
1105b6dfa339SMatthew G. Knepley   const PetscInt  *ghostPoints;        /* Ghost fault faces */
1106cd0c2139SMatthew G Knepley   PetscSection     coordSection;
1107cd0c2139SMatthew G Knepley   Vec              coordinates;
1108cd0c2139SMatthew G Knepley   PetscScalar     *coords;
1109a6ae58d1SMatthew G. Knepley   PetscInt        *depthMax;   /* The first hybrid point at each depth in the original mesh */
1110a6ae58d1SMatthew G. Knepley   PetscInt        *depthEnd;   /* The point limit at each depth in the original mesh */
1111607ab7a9SMatthew G. Knepley   PetscInt        *depthShift; /* Number of replicated+hybrid points at each depth */
1112607ab7a9SMatthew G. Knepley   PetscInt        *pMaxNew;    /* The first replicated point at each depth in the new mesh, hybrids come after this */
1113607ab7a9SMatthew G. Knepley   PetscInt        *coneNew, *coneONew, *supportNew;
111418c5995bSMatthew G. Knepley   PetscInt         shift = 100, shift2 = 200, depth = 0, dep, dim, d, sp, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, numLabels, vStart, vEnd, pEnd, p, v;
1115cd0c2139SMatthew G Knepley 
1116cd0c2139SMatthew G Knepley   PetscFunctionBegin;
11179566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
11189566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
11199566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
11209566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
1121412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
11229566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(sdm, "celltype"));
1123cd0c2139SMatthew G Knepley   /* Count split points and add cohesive cells */
11249566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
11259566063dSJacob Faibussowitsch   PetscCall(PetscMalloc5(depth + 1, &depthMax, depth + 1, &depthEnd, 2 * (depth + 1), &depthShift, depth + 1, &pMaxNew, depth + 1, &numHybridPointsOld));
11269566063dSJacob Faibussowitsch   PetscCall(PetscMalloc7(depth + 1, &splitIS, depth + 1, &unsplitIS, depth + 1, &numSplitPoints, depth + 1, &numUnsplitPoints, depth + 1, &numHybridPoints, depth + 1, &splitPoints, depth + 1, &unsplitPoints));
1127607ab7a9SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
11289566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, NULL, &pMaxNew[d]));
11299566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, d, &depthMax[d], NULL));
1130a6ae58d1SMatthew G. Knepley     depthEnd[d]           = pMaxNew[d];
1131a6ae58d1SMatthew G. Knepley     depthMax[d]           = depthMax[d] < 0 ? depthEnd[d] : depthMax[d];
1132607ab7a9SMatthew G. Knepley     numSplitPoints[d]     = 0;
113318c5995bSMatthew G. Knepley     numUnsplitPoints[d]   = 0;
1134607ab7a9SMatthew G. Knepley     numHybridPoints[d]    = 0;
1135a6ae58d1SMatthew G. Knepley     numHybridPointsOld[d] = depthMax[d] < 0 ? 0 : depthEnd[d] - depthMax[d];
1136607ab7a9SMatthew G. Knepley     splitPoints[d]        = NULL;
113718c5995bSMatthew G. Knepley     unsplitPoints[d]      = NULL;
113818c5995bSMatthew G. Knepley     splitIS[d]            = NULL;
113918c5995bSMatthew G. Knepley     unsplitIS[d]          = NULL;
114059eef20bSToby Isaac     /* we are shifting the existing hybrid points with the stratum behind them, so
114159eef20bSToby Isaac      * the split comes at the end of the normal points, i.e., at depthMax[d] */
114259eef20bSToby Isaac     depthShift[2 * d]     = depthMax[d];
114359eef20bSToby Isaac     depthShift[2 * d + 1] = 0;
1144607ab7a9SMatthew G. Knepley   }
1145b6dfa339SMatthew G. Knepley   numGhostPoints = 0;
1146b6dfa339SMatthew G. Knepley   ghostPoints    = NULL;
1147cd0c2139SMatthew G Knepley   if (label) {
11489566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
11499566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numSP));
11509566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
1151cd0c2139SMatthew G Knepley   }
1152cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1153cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1154cd0c2139SMatthew G Knepley 
1155cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
11569566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, dep, &splitIS[dep]));
115718c5995bSMatthew G. Knepley     if (splitIS[dep]) {
11589566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(splitIS[dep], &numSplitPoints[dep]));
11599566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(splitIS[dep], &splitPoints[dep]));
116018c5995bSMatthew G. Knepley     }
11619566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, shift2 + dep, &unsplitIS[dep]));
116218c5995bSMatthew G. Knepley     if (unsplitIS[dep]) {
11639566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(unsplitIS[dep], &numUnsplitPoints[dep]));
11649566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(unsplitIS[dep], &unsplitPoints[dep]));
1165cd0c2139SMatthew G Knepley     }
1166cd0c2139SMatthew G Knepley   }
1167b6dfa339SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, shift2 + dim - 1, &ghostIS));
1168b6dfa339SMatthew G. Knepley   if (ghostIS) {
1169b6dfa339SMatthew G. Knepley     PetscCall(ISGetLocalSize(ghostIS, &numGhostPoints));
1170b6dfa339SMatthew G. Knepley     PetscCall(ISGetIndices(ghostIS, &ghostPoints));
1171b6dfa339SMatthew G. Knepley   }
1172607ab7a9SMatthew G. Knepley   /* Calculate number of hybrid points */
117318c5995bSMatthew G. Knepley   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   */
11742582d50cSToby Isaac   for (d = 0; d <= depth; ++d) depthShift[2 * d + 1] = numSplitPoints[d] + numHybridPoints[d];
11759566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPointSetUp_Internal(depth, depthShift));
117659eef20bSToby Isaac   /* the end of the points in this stratum that come before the new points:
117759eef20bSToby Isaac    * shifting pMaxNew[d] gets the new start of the next stratum, then count back the old hybrid points and the newly
117859eef20bSToby Isaac    * added points */
11792582d50cSToby Isaac   for (d = 0; d <= depth; ++d) pMaxNew[d] = DMPlexShiftPoint_Internal(pMaxNew[d], depth, depthShift) - (numHybridPointsOld[d] + numSplitPoints[d] + numHybridPoints[d]);
11809566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSizes_Internal(dm, depthShift, sdm));
1181cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
1182cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1183cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1184cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
11852582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
11864c367dbcSMatthew G. Knepley       const PetscInt  splitp = p + pMaxNew[dep];
1187cd0c2139SMatthew G Knepley       const PetscInt *support;
1188394c2f0fSMatthew G. Knepley       DMPolytopeType  ct;
11894c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, qn, qp, e;
1190cd0c2139SMatthew G Knepley 
11919566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
11929566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(sdm, splitp, coneSize));
11939566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
11949566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(sdm, splitp, supportSize));
11959566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, oldp, &ct));
11969566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(sdm, splitp, ct));
1197cd0c2139SMatthew G Knepley       if (dep == depth - 1) {
11984c367dbcSMatthew G. Knepley         const PetscInt hybcell = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
11994c367dbcSMatthew G. Knepley 
1200cd0c2139SMatthew G Knepley         /* Add cohesive cells, they are prisms */
12019566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybcell, 2 + coneSize));
1202412e9a14SMatthew G. Knepley         switch (coneSize) {
12039566063dSJacob Faibussowitsch         case 2: PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_SEG_PRISM_TENSOR)); break;
12049566063dSJacob Faibussowitsch         case 3: PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_TRI_PRISM_TENSOR)); break;
12059566063dSJacob Faibussowitsch         case 4: PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_QUAD_PRISM_TENSOR)); break;
1206412e9a14SMatthew G. Knepley         }
1207b6dfa339SMatthew G. Knepley         /* Shared fault faces with only one support cell now have two with the cohesive cell */
1208b6dfa339SMatthew G. Knepley         /*   TODO Check thaat oldp has rootdegree == 1 */
1209b6dfa339SMatthew G. Knepley         if (supportSize == 1) {
1210b6dfa339SMatthew G. Knepley           const PetscInt *support;
1211b6dfa339SMatthew G. Knepley           PetscInt        val;
1212b6dfa339SMatthew G. Knepley 
1213b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetSupport(dm, oldp, &support));
1214b6dfa339SMatthew G. Knepley           PetscCall(DMLabelGetValue(label, support[0], &val));
1215b6dfa339SMatthew G. Knepley           if (val < 0) PetscCall(DMPlexSetSupportSize(sdm, splitp, 2));
1216b6dfa339SMatthew G. Knepley           else PetscCall(DMPlexSetSupportSize(sdm, newp, 2));
1217b6dfa339SMatthew G. Knepley         }
1218cd0c2139SMatthew G Knepley       } else if (dep == 0) {
12194c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
1220cd0c2139SMatthew G Knepley 
12219566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, oldp, &support));
12224c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1223cd0c2139SMatthew G Knepley           PetscInt val;
1224cd0c2139SMatthew G Knepley 
12259566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
12264c367dbcSMatthew G. Knepley           if (val == 1) ++qf;
12274c367dbcSMatthew G. Knepley           if ((val == 1) || (val == (shift + 1))) ++qn;
12284c367dbcSMatthew G. Knepley           if ((val == 1) || (val == -(shift + 1))) ++qp;
1229cd0c2139SMatthew G Knepley         }
12304c367dbcSMatthew G. Knepley         /* Split old vertex: Edges into original vertex and new cohesive edge */
12319566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qn + 1));
12324c367dbcSMatthew G. Knepley         /* Split new vertex: Edges into split vertex and new cohesive edge */
12339566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, splitp, qp + 1));
12344c367dbcSMatthew G. Knepley         /* Add hybrid edge */
12359566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybedge, 2));
12369566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybedge, qf));
12379566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR));
1238cd0c2139SMatthew G Knepley       } else if (dep == dim - 2) {
12394c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
12404c367dbcSMatthew G. Knepley 
12419566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, oldp, &support));
12424c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1243cd0c2139SMatthew G Knepley           PetscInt val;
1244cd0c2139SMatthew G Knepley 
12459566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
12464c367dbcSMatthew G. Knepley           if (val == dim - 1) ++qf;
12474c367dbcSMatthew G. Knepley           if ((val == dim - 1) || (val == (shift + dim - 1))) ++qn;
12484c367dbcSMatthew G. Knepley           if ((val == dim - 1) || (val == -(shift + dim - 1))) ++qp;
1249cd0c2139SMatthew G Knepley         }
12504c367dbcSMatthew G. Knepley         /* Split old edge: Faces into original edge and cohesive face (positive side?) */
12519566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qn + 1));
12524c367dbcSMatthew G. Knepley         /* Split new edge: Faces into split edge and cohesive face (negative side?) */
12539566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, splitp, qp + 1));
12544c367dbcSMatthew G. Knepley         /* Add hybrid face */
12559566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybface, 4));
12569566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybface, qf));
12579566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR));
1258cd0c2139SMatthew G Knepley       }
1259cd0c2139SMatthew G Knepley     }
1260cd0c2139SMatthew G Knepley   }
126118c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
126218c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
126318c5995bSMatthew G. Knepley       const PetscInt  oldp = unsplitPoints[dep][p];
12642582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
126518c5995bSMatthew G. Knepley       const PetscInt *support;
1266da1dd7e4SMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, e, s;
126718c5995bSMatthew G. Knepley 
12689566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
12699566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
12709566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
127118c5995bSMatthew G. Knepley       if (dep == 0) {
127218c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
127318c5995bSMatthew G. Knepley 
127439254ff6SMatthew G. Knepley         /* Unsplit vertex: Edges into original vertex, split edges, and new cohesive edge twice */
127539254ff6SMatthew G. Knepley         for (s = 0, qf = 0; s < supportSize; ++s, ++qf) {
12769566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSplitPoints[dep + 1], splitPoints[dep + 1], &e));
127739254ff6SMatthew G. Knepley           if (e >= 0) ++qf;
127839254ff6SMatthew G. Knepley         }
12799566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qf + 2));
128018c5995bSMatthew G. Knepley         /* Add hybrid edge */
12819566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybedge, 2));
1282e1757548SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1283e1757548SMatthew G. Knepley           PetscInt val;
1284e1757548SMatthew G. Knepley 
12859566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1286e1757548SMatthew G. Knepley           /* Split and unsplit edges produce hybrid faces */
1287da1dd7e4SMatthew G. Knepley           if (val == 1) ++qf;
1288da1dd7e4SMatthew G. Knepley           if (val == (shift2 + 1)) ++qf;
1289e1757548SMatthew G. Knepley         }
12909566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybedge, qf));
12919566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR));
129218c5995bSMatthew G. Knepley       } else if (dep == dim - 2) {
129318c5995bSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
1294cd0c2139SMatthew G Knepley         PetscInt       val;
1295cd0c2139SMatthew G Knepley 
1296da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
12979566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1298da1dd7e4SMatthew G. Knepley           if (val == dim - 1) qf += 2;
1299da1dd7e4SMatthew G. Knepley           else ++qf;
1300cd0c2139SMatthew G Knepley         }
130118c5995bSMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and cohesive face twice */
13029566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qf + 2));
130318c5995bSMatthew G. Knepley         /* Add hybrid face */
1304da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
13059566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1306da1dd7e4SMatthew G. Knepley           if (val == dim - 1) ++qf;
1307da1dd7e4SMatthew G. Knepley         }
13089566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybface, 4));
13099566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybface, qf));
13109566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR));
1311cd0c2139SMatthew G Knepley       }
1312cd0c2139SMatthew G Knepley     }
1313cd0c2139SMatthew G Knepley   }
1314cd0c2139SMatthew G Knepley   /* Step 4: Setup split DM */
13159566063dSJacob Faibussowitsch   PetscCall(DMSetUp(sdm));
13169566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPoints_Internal(dm, depthShift, sdm));
13179566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(sdm, &maxConeSizeNew, &maxSupportSizeNew));
13189566063dSJacob Faibussowitsch   PetscCall(PetscMalloc3(PetscMax(maxConeSize, maxConeSizeNew) * 3, &coneNew, PetscMax(maxConeSize, maxConeSizeNew) * 3, &coneONew, PetscMax(maxSupportSize, maxSupportSizeNew), &supportNew));
1319cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
1320cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1321cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1322cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
13232582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
13244c367dbcSMatthew G. Knepley       const PetscInt  splitp = p + pMaxNew[dep];
1325cd0c2139SMatthew G Knepley       const PetscInt *cone, *support, *ornt;
1326b5a892a1SMatthew G. Knepley       DMPolytopeType  ct;
13274c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, q, qf, qn, qp, v, e, s;
1328cd0c2139SMatthew G Knepley 
13299566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, oldp, &ct));
13309566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
13319566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, oldp, &cone));
13329566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeOrientation(dm, oldp, &ornt));
13339566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
13349566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
1335cd0c2139SMatthew G Knepley       if (dep == depth - 1) {
133696a07cd0SMatthew G. Knepley         PetscBool       hasUnsplit = PETSC_FALSE;
13374c367dbcSMatthew G. Knepley         const PetscInt  hybcell    = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
1338cd0c2139SMatthew G Knepley         const PetscInt *supportF;
1339cd0c2139SMatthew G Knepley 
1340b6dfa339SMatthew G. Knepley         coneONew[0] = coneONew[1] = -1000;
1341cd0c2139SMatthew G Knepley         /* Split face:       copy in old face to new face to start */
13429566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(sdm, newp, &supportF));
13439566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportF));
1344cd0c2139SMatthew G Knepley         /* Split old face:   old vertices/edges in cone so no change */
1345cd0c2139SMatthew G Knepley         /* Split new face:   new vertices/edges in cone */
1346cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
13479566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[q], numSplitPoints[dep - 1], splitPoints[dep - 1], &v));
134818c5995bSMatthew G. Knepley           if (v < 0) {
13499566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[q], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
135063a3b9bcSJacob Faibussowitsch             PetscCheck(v >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %" PetscInt_FMT " in split or unsplit points of depth %" PetscInt_FMT, cone[q], dep - 1);
13512582d50cSToby Isaac             coneNew[2 + q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
135296a07cd0SMatthew G. Knepley             hasUnsplit     = PETSC_TRUE;
135318c5995bSMatthew G. Knepley           } else {
13544c367dbcSMatthew G. Knepley             coneNew[2 + q] = v + pMaxNew[dep - 1];
1355163235baSMatthew G. Knepley             if (dep > 1) {
1356163235baSMatthew G. Knepley               const PetscInt *econe;
1357163235baSMatthew G. Knepley               PetscInt        econeSize, r, vs, vu;
1358163235baSMatthew G. Knepley 
13599566063dSJacob Faibussowitsch               PetscCall(DMPlexGetConeSize(dm, cone[q], &econeSize));
13609566063dSJacob Faibussowitsch               PetscCall(DMPlexGetCone(dm, cone[q], &econe));
1361163235baSMatthew G. Knepley               for (r = 0; r < econeSize; ++r) {
13629566063dSJacob Faibussowitsch                 PetscCall(PetscFindInt(econe[r], numSplitPoints[dep - 2], splitPoints[dep - 2], &vs));
13639566063dSJacob Faibussowitsch                 PetscCall(PetscFindInt(econe[r], numUnsplitPoints[dep - 2], unsplitPoints[dep - 2], &vu));
1364163235baSMatthew G. Knepley                 if (vs >= 0) continue;
136563a3b9bcSJacob Faibussowitsch                 PetscCheck(vu >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %" PetscInt_FMT " in split or unsplit points of depth %" PetscInt_FMT, econe[r], dep - 2);
1366163235baSMatthew G. Knepley                 hasUnsplit = PETSC_TRUE;
1367163235baSMatthew G. Knepley               }
1368163235baSMatthew G. Knepley             }
1369cd0c2139SMatthew G Knepley           }
1370cd0c2139SMatthew G Knepley         }
13719566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, splitp, &coneNew[2]));
13729566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(sdm, splitp, ornt));
1373e537020bSMatthew G. Knepley         /* Face support */
1374b6dfa339SMatthew G. Knepley         PetscInt vals[2];
1375cd0c2139SMatthew G Knepley 
1376b6dfa339SMatthew G. Knepley         PetscCall(DMLabelGetValue(label, support[0], &vals[0]));
1377b6dfa339SMatthew G. Knepley         if (supportSize > 1) PetscCall(DMLabelGetValue(label, support[1], &vals[1]));
1378b6dfa339SMatthew G. Knepley         else vals[1] = -vals[0];
1379b6dfa339SMatthew G. Knepley         PetscCheck(vals[0] * vals[1] < 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid support labels %" PetscInt_FMT " %" PetscInt_FMT, vals[0], vals[1]);
1380b6dfa339SMatthew G. Knepley 
1381b6dfa339SMatthew G. Knepley         for (s = 0; s < 2; ++s) {
1382b6dfa339SMatthew G. Knepley           if (s >= supportSize) {
1383b6dfa339SMatthew G. Knepley             if (vals[s] < 0) {
1384b6dfa339SMatthew G. Knepley               /* Ghost old face:   Replace negative side cell with cohesive cell */
1385accc9626SMatthew G. Knepley               PetscCall(DMPlexInsertSupport(sdm, newp, s, hybcell));
1386b6dfa339SMatthew G. Knepley             } else {
1387b6dfa339SMatthew G. Knepley               /* Ghost new face:   Replace positive side cell with cohesive cell */
1388accc9626SMatthew G. Knepley               PetscCall(DMPlexInsertSupport(sdm, splitp, s, hybcell));
1389b6dfa339SMatthew G. Knepley             }
1390b6dfa339SMatthew G. Knepley           } else {
1391b6dfa339SMatthew G. Knepley             if (vals[s] < 0) {
1392cd0c2139SMatthew G Knepley               /* Split old face:   Replace negative side cell with cohesive cell */
13939566063dSJacob Faibussowitsch               PetscCall(DMPlexInsertSupport(sdm, newp, s, hybcell));
1394cd0c2139SMatthew G Knepley             } else {
1395cd0c2139SMatthew G Knepley               /* Split new face:   Replace positive side cell with cohesive cell */
13969566063dSJacob Faibussowitsch               PetscCall(DMPlexInsertSupport(sdm, splitp, s, hybcell));
1397b6dfa339SMatthew G. Knepley             }
1398b6dfa339SMatthew G. Knepley           }
1399b6dfa339SMatthew G. Knepley         }
1400b6dfa339SMatthew G. Knepley         /* Get orientation for cohesive face using the positive side cell */
1401e537020bSMatthew G. Knepley         {
1402e537020bSMatthew G. Knepley           const PetscInt *ncone, *nconeO;
1403b6dfa339SMatthew G. Knepley           PetscInt        nconeSize, nc, ocell;
1404accc9626SMatthew G. Knepley           PetscBool       flip = PETSC_FALSE;
1405e537020bSMatthew G. Knepley 
14069371c9d4SSatish Balay           if (supportSize > 1) {
14079371c9d4SSatish Balay             ocell = vals[0] < 0 ? support[1] : support[0];
14089371c9d4SSatish Balay           } else {
14099371c9d4SSatish Balay             ocell = support[0];
14109371c9d4SSatish Balay             flip  = vals[0] < 0 ? PETSC_TRUE : PETSC_FALSE;
14119371c9d4SSatish Balay           }
1412b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetConeSize(dm, ocell, &nconeSize));
1413b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetCone(dm, ocell, &ncone));
1414e1a13daeSMatthew G. Knepley           PetscCall(DMPlexGetConeOrientation(dm, ocell, &nconeO));
1415e537020bSMatthew G. Knepley           for (nc = 0; nc < nconeSize; ++nc) {
1416e537020bSMatthew G. Knepley             if (ncone[nc] == oldp) {
1417b6dfa339SMatthew G. Knepley               coneONew[0] = flip ? -(nconeO[nc] + 1) : nconeO[nc];
1418e537020bSMatthew G. Knepley               break;
1419cd0c2139SMatthew G Knepley             }
1420cd0c2139SMatthew G Knepley           }
1421b6dfa339SMatthew G. Knepley           PetscCheck(nc < nconeSize, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate face %" PetscInt_FMT " in neighboring cell %" PetscInt_FMT, oldp, ocell);
1422e537020bSMatthew G. Knepley         }
14234c367dbcSMatthew G. Knepley         /* Cohesive cell:    Old and new split face, then new cohesive faces */
1424b6dfa339SMatthew G. Knepley         {
1425b6dfa339SMatthew G. Knepley           const PetscInt No = DMPolytopeTypeGetNumArrangments(ct) / 2;
1426b6dfa339SMatthew G. Knepley           PetscCheck((coneONew[0] >= -No) && (coneONew[0] < No), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid %s orientation %" PetscInt_FMT, DMPolytopeTypes[ct], coneONew[0]);
1427b6dfa339SMatthew G. Knepley         }
1428b5a892a1SMatthew G. Knepley         const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, coneONew[0]);
1429b5a892a1SMatthew G. Knepley 
1430fd4b9f15SMatthew G. Knepley         coneNew[0]  = newp; /* Extracted negative side orientation above */
14314c367dbcSMatthew G. Knepley         coneNew[1]  = splitp;
14324c367dbcSMatthew G. Knepley         coneONew[1] = coneONew[0];
1433e537020bSMatthew G. Knepley         for (q = 0; q < coneSize; ++q) {
1434412e9a14SMatthew G. Knepley           /* Hybrid faces must follow order from oriented end face */
1435b5a892a1SMatthew G. Knepley           const PetscInt qa = arr[q * 2 + 0];
1436b5a892a1SMatthew G. Knepley           const PetscInt qo = arr[q * 2 + 1];
1437b5a892a1SMatthew G. Knepley           DMPolytopeType ft = dep == 2 ? DM_POLYTOPE_SEGMENT : DM_POLYTOPE_POINT;
1438412e9a14SMatthew G. Knepley 
14399566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[qa], numSplitPoints[dep - 1], splitPoints[dep - 1], &v));
144018c5995bSMatthew G. Knepley           if (v < 0) {
14419566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[qa], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
144218c5995bSMatthew G. Knepley             coneNew[2 + q] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
144318c5995bSMatthew G. Knepley           } else {
144418c5995bSMatthew G. Knepley             coneNew[2 + q] = v + pMaxNew[dep] + numSplitPoints[dep];
144518c5995bSMatthew G. Knepley           }
1446b5a892a1SMatthew G. Knepley           coneONew[2 + q] = DMPolytopeTypeComposeOrientation(ft, qo, ornt[qa]);
1447e537020bSMatthew G. Knepley         }
14489566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybcell, coneNew));
14499566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(sdm, hybcell, coneONew));
145096a07cd0SMatthew G. Knepley         /* Label the hybrid cells on the boundary of the split */
14519566063dSJacob Faibussowitsch         if (hasUnsplit) PetscCall(DMLabelSetValue(label, -hybcell, dim));
1452cd0c2139SMatthew G Knepley       } else if (dep == 0) {
14534c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
1454cd0c2139SMatthew G Knepley 
1455cd0c2139SMatthew G Knepley         /* Split old vertex: Edges in old split faces and new cohesive edge */
14564c367dbcSMatthew G. Knepley         for (e = 0, qn = 0; e < supportSize; ++e) {
1457cd0c2139SMatthew G Knepley           PetscInt val;
1458cd0c2139SMatthew G Knepley 
14599566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1460ad540459SPierre Jolivet           if ((val == 1) || (val == (shift + 1))) supportNew[qn++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1461cd0c2139SMatthew G Knepley         }
14624c367dbcSMatthew G. Knepley         supportNew[qn] = hybedge;
14639566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1464cd0c2139SMatthew G Knepley         /* Split new vertex: Edges in new split faces and new cohesive edge */
14654c367dbcSMatthew G. Knepley         for (e = 0, qp = 0; e < supportSize; ++e) {
1466cd0c2139SMatthew G Knepley           PetscInt val, edge;
1467cd0c2139SMatthew G Knepley 
14689566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1469cd0c2139SMatthew G Knepley           if (val == 1) {
14709566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &edge));
147163a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]);
14724c367dbcSMatthew G. Knepley             supportNew[qp++] = edge + pMaxNew[dep + 1];
1473cd0c2139SMatthew G Knepley           } else if (val == -(shift + 1)) {
14742582d50cSToby Isaac             supportNew[qp++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1475cd0c2139SMatthew G Knepley           }
1476cd0c2139SMatthew G Knepley         }
14774c367dbcSMatthew G. Knepley         supportNew[qp] = hybedge;
14789566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportNew));
14794c367dbcSMatthew G. Knepley         /* Hybrid edge:    Old and new split vertex */
1480cd0c2139SMatthew G Knepley         coneNew[0] = newp;
1481cd0c2139SMatthew G Knepley         coneNew[1] = splitp;
14829566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybedge, coneNew));
14834c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
14844c367dbcSMatthew G. Knepley           PetscInt val, edge;
14854c367dbcSMatthew G. Knepley 
14869566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
14874c367dbcSMatthew G. Knepley           if (val == 1) {
14889566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &edge));
148963a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]);
14904c367dbcSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
14914c367dbcSMatthew G. Knepley           }
14924c367dbcSMatthew G. Knepley         }
14939566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybedge, supportNew));
1494cd0c2139SMatthew G Knepley       } else if (dep == dim - 2) {
14954c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
14964c367dbcSMatthew G. Knepley 
1497cd0c2139SMatthew G Knepley         /* Split old edge:   old vertices in cone so no change */
1498cd0c2139SMatthew G Knepley         /* Split new edge:   new vertices in cone */
1499cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
15009566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[q], numSplitPoints[dep - 1], splitPoints[dep - 1], &v));
1501e1757548SMatthew G. Knepley           if (v < 0) {
15029566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[q], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
150363a3b9bcSJacob Faibussowitsch             PetscCheck(v >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %" PetscInt_FMT " in split or unsplit points of depth %" PetscInt_FMT, cone[q], dep - 1);
15042582d50cSToby Isaac             coneNew[q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
1505e1757548SMatthew G. Knepley           } else {
15064c367dbcSMatthew G. Knepley             coneNew[q] = v + pMaxNew[dep - 1];
1507cd0c2139SMatthew G Knepley           }
1508e1757548SMatthew G. Knepley         }
15099566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, splitp, coneNew));
1510cd0c2139SMatthew G Knepley         /* Split old edge: Faces in positive side cells and old split faces */
1511cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1512cd0c2139SMatthew G Knepley           PetscInt val;
1513cd0c2139SMatthew G Knepley 
15149566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
15154c367dbcSMatthew G. Knepley           if (val == dim - 1) {
15162582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
15174c367dbcSMatthew G. Knepley           } else if (val == (shift + dim - 1)) {
15182582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1519cd0c2139SMatthew G Knepley           }
1520cd0c2139SMatthew G Knepley         }
1521b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
15229566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1523cd0c2139SMatthew G Knepley         /* Split new edge: Faces in negative side cells and new split faces */
1524cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1525cd0c2139SMatthew G Knepley           PetscInt val, face;
1526cd0c2139SMatthew G Knepley 
15279566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1528cd0c2139SMatthew G Knepley           if (val == dim - 1) {
15299566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
153063a3b9bcSJacob Faibussowitsch             PetscCheck(face >= 0, comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[e]);
15314c367dbcSMatthew G. Knepley             supportNew[q++] = face + pMaxNew[dep + 1];
1532cd0c2139SMatthew G Knepley           } else if (val == -(shift + dim - 1)) {
15332582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1534cd0c2139SMatthew G Knepley           }
1535cd0c2139SMatthew G Knepley         }
1536b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1];
15379566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportNew));
15384c367dbcSMatthew G. Knepley         /* Hybrid face */
15394c367dbcSMatthew G. Knepley         coneNew[0] = newp;
15404c367dbcSMatthew G. Knepley         coneNew[1] = splitp;
15414c367dbcSMatthew G. Knepley         for (v = 0; v < coneSize; ++v) {
15424c367dbcSMatthew G. Knepley           PetscInt vertex;
15439566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[v], numSplitPoints[dep - 1], splitPoints[dep - 1], &vertex));
1544e1757548SMatthew G. Knepley           if (vertex < 0) {
15459566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[v], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &vertex));
154663a3b9bcSJacob Faibussowitsch             PetscCheck(vertex >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %" PetscInt_FMT " in split or unsplit points of depth %" PetscInt_FMT, cone[v], dep - 1);
1547e1757548SMatthew G. Knepley             coneNew[2 + v] = vertex + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
1548e1757548SMatthew G. Knepley           } else {
15494c367dbcSMatthew G. Knepley             coneNew[2 + v] = vertex + pMaxNew[dep] + numSplitPoints[dep];
15504c367dbcSMatthew G. Knepley           }
1551e1757548SMatthew G. Knepley         }
15529566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybface, coneNew));
15534c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
15544c367dbcSMatthew G. Knepley           PetscInt val, face;
15554c367dbcSMatthew G. Knepley 
15569566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
15574c367dbcSMatthew G. Knepley           if (val == dim - 1) {
15589566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
155963a3b9bcSJacob Faibussowitsch             PetscCheck(face >= 0, comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[e]);
15604c367dbcSMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
15614c367dbcSMatthew G. Knepley           }
15624c367dbcSMatthew G. Knepley         }
15639566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybface, supportNew));
1564cd0c2139SMatthew G Knepley       }
1565cd0c2139SMatthew G Knepley     }
1566cd0c2139SMatthew G Knepley   }
156718c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
156818c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
156918c5995bSMatthew G. Knepley       const PetscInt  oldp = unsplitPoints[dep][p];
15702582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
1571b5a892a1SMatthew G. Knepley       const PetscInt *cone, *support;
1572e1757548SMatthew G. Knepley       PetscInt        coneSize, supportSize, supportSizeNew, q, qf, e, f, s;
157318c5995bSMatthew G. Knepley 
15749566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
15759566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, oldp, &cone));
15769566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
15779566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
157818c5995bSMatthew G. Knepley       if (dep == 0) {
157918c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
158018c5995bSMatthew G. Knepley 
158118c5995bSMatthew G. Knepley         /* Unsplit vertex */
15829566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, newp, &supportSizeNew));
158318c5995bSMatthew G. Knepley         for (s = 0, q = 0; s < supportSize; ++s) {
15842582d50cSToby Isaac           supportNew[q++] = DMPlexShiftPoint_Internal(support[s], depth, depthShift) /*support[s] + depthOffset[dep+1]*/;
15859566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSplitPoints[dep + 1], splitPoints[dep + 1], &e));
1586ad540459SPierre Jolivet           if (e >= 0) supportNew[q++] = e + pMaxNew[dep + 1];
158718c5995bSMatthew G. Knepley         }
158818c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
158918c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
159063a3b9bcSJacob Faibussowitsch         PetscCheck(q == supportSizeNew, comm, PETSC_ERR_ARG_WRONG, "Support size %" PetscInt_FMT " != %" PetscInt_FMT " for vertex %" PetscInt_FMT, q, supportSizeNew, newp);
15919566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
159218c5995bSMatthew G. Knepley         /* Hybrid edge */
159318c5995bSMatthew G. Knepley         coneNew[0] = newp;
159418c5995bSMatthew G. Knepley         coneNew[1] = newp;
15959566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybedge, coneNew));
159618c5995bSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
159718c5995bSMatthew G. Knepley           PetscInt val, edge;
159818c5995bSMatthew G. Knepley 
15999566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
160018c5995bSMatthew G. Knepley           if (val == 1) {
16019566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep + 1], splitPoints[dep + 1], &edge));
160263a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]);
160318c5995bSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
1604e1757548SMatthew G. Knepley           } else if (val == (shift2 + 1)) {
16059566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numUnsplitPoints[dep + 1], unsplitPoints[dep + 1], &edge));
160663a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0, comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a unsplit edge", support[e]);
1607e1757548SMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep + 2] + numSplitPoints[dep + 2] + numSplitPoints[dep + 1];
160818c5995bSMatthew G. Knepley           }
160918c5995bSMatthew G. Knepley         }
16109566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybedge, supportNew));
1611e1757548SMatthew G. Knepley       } else if (dep == dim - 2) {
1612e1757548SMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep + 1] + numSplitPoints[dep + 1] + numSplitPoints[dep];
1613e1757548SMatthew G. Knepley 
1614da1dd7e4SMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and hybrid face twice */
1615e1757548SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1616e1757548SMatthew G. Knepley           PetscInt val, face;
1617e1757548SMatthew G. Knepley 
16189566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[f], &val));
1619e1757548SMatthew G. Knepley           if (val == dim - 1) {
16209566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[f], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
162163a3b9bcSJacob Faibussowitsch             PetscCheck(face >= 0, comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[f]);
16222582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1623e1757548SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep + 1];
1624e1757548SMatthew G. Knepley           } else {
16252582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1626e1757548SMatthew G. Knepley           }
1627e1757548SMatthew G. Knepley         }
1628e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
1629e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
16309566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, newp, &supportSizeNew));
163163a3b9bcSJacob Faibussowitsch         PetscCheck(qf == supportSizeNew, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Support size for unsplit edge %" PetscInt_FMT " is %" PetscInt_FMT " != %" PetscInt_FMT, newp, qf, supportSizeNew);
16329566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1633e1757548SMatthew G. Knepley         /* Add hybrid face */
1634e1757548SMatthew G. Knepley         coneNew[0] = newp;
1635212cc919SMatthew G. Knepley         coneNew[1] = newp;
16369566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[0], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
163763a3b9bcSJacob Faibussowitsch         PetscCheck(v >= 0, comm, PETSC_ERR_ARG_WRONG, "Vertex %" PetscInt_FMT " is not an unsplit vertex", cone[0]);
1638212cc919SMatthew G. Knepley         coneNew[2] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
16399566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[1], numUnsplitPoints[dep - 1], unsplitPoints[dep - 1], &v));
164063a3b9bcSJacob Faibussowitsch         PetscCheck(v >= 0, comm, PETSC_ERR_ARG_WRONG, "Vertex %" PetscInt_FMT " is not an unsplit vertex", cone[1]);
1641e1757548SMatthew G. Knepley         coneNew[3] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep - 1];
16429566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybface, coneNew));
1643da1dd7e4SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1644da1dd7e4SMatthew G. Knepley           PetscInt val, face;
1645da1dd7e4SMatthew G. Knepley 
16469566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[f], &val));
1647da1dd7e4SMatthew G. Knepley           if (val == dim - 1) {
16489566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[f], numSplitPoints[dep + 1], splitPoints[dep + 1], &face));
1649da1dd7e4SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep + 2] + numSplitPoints[dep + 2];
1650da1dd7e4SMatthew G. Knepley           }
1651da1dd7e4SMatthew G. Knepley         }
16529566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, hybface, &supportSizeNew));
165363a3b9bcSJacob Faibussowitsch         PetscCheck(qf == supportSizeNew, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Support size for hybrid face %" PetscInt_FMT " is %" PetscInt_FMT " != %" PetscInt_FMT, hybface, qf, supportSizeNew);
16549566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybface, supportNew));
1655cd0c2139SMatthew G Knepley       }
1656cd0c2139SMatthew G Knepley     }
1657cd0c2139SMatthew G Knepley   }
1658cd0c2139SMatthew G Knepley   /* Step 6b: Replace split points in negative side cones */
1659cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1660cd0c2139SMatthew G Knepley     PetscInt        dep = values[sp];
1661cd0c2139SMatthew G Knepley     IS              pIS;
1662cd0c2139SMatthew G Knepley     PetscInt        numPoints;
1663cd0c2139SMatthew G Knepley     const PetscInt *points;
1664cd0c2139SMatthew G Knepley 
1665cd0c2139SMatthew G Knepley     if (dep >= 0) continue;
16669566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, dep, &pIS));
1667cd0c2139SMatthew G Knepley     if (!pIS) continue;
1668cd0c2139SMatthew G Knepley     dep = -dep - shift;
16699566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(pIS, &numPoints));
16709566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pIS, &points));
1671cd0c2139SMatthew G Knepley     for (p = 0; p < numPoints; ++p) {
1672cd0c2139SMatthew G Knepley       const PetscInt  oldp = points[p];
16732582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*depthOffset[dep] + oldp*/;
1674cd0c2139SMatthew G Knepley       const PetscInt *cone;
1675cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
167650cf782dSMatthew G. Knepley       /* PetscBool       replaced = PETSC_FALSE; */
1677cd0c2139SMatthew G Knepley 
1678cd0c2139SMatthew G Knepley       /* Negative edge: replace split vertex */
1679cd0c2139SMatthew G Knepley       /* Negative cell: replace split face */
16809566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(sdm, newp, &coneSize));
16819566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(sdm, newp, &cone));
1682cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
1683e38fbfedSToby Isaac         const PetscInt coldp = DMPlexShiftPointInverse_Internal(cone[c], depth, depthShift);
1684cd0c2139SMatthew G Knepley         PetscInt       csplitp, cp, val;
1685cd0c2139SMatthew G Knepley 
16869566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, coldp, &val));
1687cd0c2139SMatthew G Knepley         if (val == dep - 1) {
16889566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(coldp, numSplitPoints[dep - 1], splitPoints[dep - 1], &cp));
168963a3b9bcSJacob Faibussowitsch           PetscCheck(cp >= 0, comm, PETSC_ERR_ARG_WRONG, "Point %" PetscInt_FMT " is not a split point of dimension %" PetscInt_FMT, oldp, dep - 1);
1690cd0c2139SMatthew G Knepley           csplitp = pMaxNew[dep - 1] + cp;
16919566063dSJacob Faibussowitsch           PetscCall(DMPlexInsertCone(sdm, newp, c, csplitp));
169250cf782dSMatthew G. Knepley           /* replaced = PETSC_TRUE; */
1693cd0c2139SMatthew G Knepley         }
1694cd0c2139SMatthew G Knepley       }
16954a189a86SMatthew G. Knepley       /* Cells with only a vertex or edge on the submesh have no replacement */
169628b400f6SJacob Faibussowitsch       /* PetscCheck(replaced,comm, PETSC_ERR_ARG_WRONG, "The cone of point %d does not contain split points", oldp); */
1697cd0c2139SMatthew G Knepley     }
16989566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pIS, &points));
16999566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pIS));
1700cd0c2139SMatthew G Knepley   }
1701fa8e8ae5SToby Isaac   /* Step 7: Coordinates */
17029566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftCoordinates_Internal(dm, depthShift, sdm));
17039566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(sdm, &coordSection));
17049566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(sdm, &coordinates));
17059566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
1706cd0c2139SMatthew G Knepley   for (v = 0; v < (numSplitPoints ? numSplitPoints[0] : 0); ++v) {
17072582d50cSToby Isaac     const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[0][v], depth, depthShift) /*depthOffset[0] + splitPoints[0][v]*/;
1708cd0c2139SMatthew G Knepley     const PetscInt splitp = pMaxNew[0] + v;
1709cd0c2139SMatthew G Knepley     PetscInt       dof, off, soff, d;
1710cd0c2139SMatthew G Knepley 
17119566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, newp, &dof));
17129566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, newp, &off));
17139566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, splitp, &soff));
1714cd0c2139SMatthew G Knepley     for (d = 0; d < dof; ++d) coords[soff + d] = coords[off + d];
1715cd0c2139SMatthew G Knepley   }
17169566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
1717fa8e8ae5SToby Isaac   /* Step 8: SF, if I can figure this out we can split the mesh in parallel */
17189566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Internal(dm, depthShift, sdm));
1719b6dfa339SMatthew G. Knepley   /*   TODO We need to associate the ghost points with the correct replica */
1720fa8e8ae5SToby Isaac   /* Step 9: Labels */
17219566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftLabels_Internal(dm, depthShift, sdm));
17229566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateVTKLabel_Internal(dm, PETSC_FALSE, sdm));
17239566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(sdm, &numLabels));
1724cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1725cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
17262582d50cSToby Isaac       const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[dep][p], depth, depthShift) /*depthOffset[dep] + splitPoints[dep][p]*/;
1727cd0c2139SMatthew G Knepley       const PetscInt splitp = pMaxNew[dep] + p;
1728cd0c2139SMatthew G Knepley       PetscInt       l;
1729cd0c2139SMatthew G Knepley 
17307db7e0a7SMatthew G. Knepley       if (splitLabel) {
17317db7e0a7SMatthew G. Knepley         const PetscInt val = 100 + dep;
17327db7e0a7SMatthew G. Knepley 
17339566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(splitLabel, newp, val));
17349566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(splitLabel, splitp, -val));
17357db7e0a7SMatthew G. Knepley       }
1736cd0c2139SMatthew G Knepley       for (l = 0; l < numLabels; ++l) {
1737cd0c2139SMatthew G Knepley         DMLabel     mlabel;
1738cd0c2139SMatthew G Knepley         const char *lname;
1739cd0c2139SMatthew G Knepley         PetscInt    val;
17409a356370SMatthew G. Knepley         PetscBool   isDepth;
1741cd0c2139SMatthew G Knepley 
17429566063dSJacob Faibussowitsch         PetscCall(DMGetLabelName(sdm, l, &lname));
17439566063dSJacob Faibussowitsch         PetscCall(PetscStrcmp(lname, "depth", &isDepth));
17449a356370SMatthew G. Knepley         if (isDepth) continue;
17459566063dSJacob Faibussowitsch         PetscCall(DMGetLabel(sdm, lname, &mlabel));
17469566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(mlabel, newp, &val));
174748a46eb9SPierre Jolivet         if (val >= 0) PetscCall(DMLabelSetValue(mlabel, splitp, val));
1748cd0c2139SMatthew G Knepley       }
1749cd0c2139SMatthew G Knepley     }
1750cd0c2139SMatthew G Knepley   }
1751cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1752cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1753cd0c2139SMatthew G Knepley 
1754cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
17559566063dSJacob Faibussowitsch     if (splitIS[dep]) PetscCall(ISRestoreIndices(splitIS[dep], &splitPoints[dep]));
17569566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&splitIS[dep]));
17579566063dSJacob Faibussowitsch     if (unsplitIS[dep]) PetscCall(ISRestoreIndices(unsplitIS[dep], &unsplitPoints[dep]));
17589566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&unsplitIS[dep]));
1759cd0c2139SMatthew G Knepley   }
1760b6dfa339SMatthew G. Knepley   if (ghostIS) PetscCall(ISRestoreIndices(ghostIS, &ghostPoints));
1761b6dfa339SMatthew G. Knepley   PetscCall(ISDestroy(&ghostIS));
1762cd0c2139SMatthew G Knepley   if (label) {
17639566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
17649566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
1765cd0c2139SMatthew G Knepley   }
17660d4d4d06SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
17679566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(sdm, d, NULL, &pEnd));
176836dbac82SMatthew G. Knepley     pMaxNew[d] = pEnd - numHybridPoints[d] - numHybridPointsOld[d];
17690d4d4d06SMatthew G. Knepley   }
17709566063dSJacob Faibussowitsch   PetscCall(PetscFree3(coneNew, coneONew, supportNew));
17719566063dSJacob Faibussowitsch   PetscCall(PetscFree5(depthMax, depthEnd, depthShift, pMaxNew, numHybridPointsOld));
17729566063dSJacob Faibussowitsch   PetscCall(PetscFree7(splitIS, unsplitIS, numSplitPoints, numUnsplitPoints, numHybridPoints, splitPoints, unsplitPoints));
1773cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1774cd0c2139SMatthew G Knepley }
1775cd0c2139SMatthew G Knepley 
1776cd0c2139SMatthew G Knepley /*@C
1777cd0c2139SMatthew G Knepley   DMPlexConstructCohesiveCells - Construct cohesive cells which split the face along an internal interface
1778cd0c2139SMatthew G Knepley 
1779cd0c2139SMatthew G Knepley   Collective on dm
1780cd0c2139SMatthew G Knepley 
1781cd0c2139SMatthew G Knepley   Input Parameters:
1782cd0c2139SMatthew G Knepley + dm - The original DM
178353156dfcSMatthew G. Knepley - label - The label specifying the boundary faces (this could be auto-generated)
1784cd0c2139SMatthew G Knepley 
1785cd0c2139SMatthew G Knepley   Output Parameters:
17867db7e0a7SMatthew G. Knepley + splitLabel - The label containing the split points, or NULL if no output is desired
1787cd0c2139SMatthew G Knepley - dmSplit - The new DM
1788cd0c2139SMatthew G Knepley 
1789cd0c2139SMatthew G Knepley   Level: developer
1790cd0c2139SMatthew G Knepley 
1791db781477SPatrick Sanan .seealso: `DMCreate()`, `DMPlexLabelCohesiveComplete()`
1792cd0c2139SMatthew G Knepley @*/
17939371c9d4SSatish Balay PetscErrorCode DMPlexConstructCohesiveCells(DM dm, DMLabel label, DMLabel splitLabel, DM *dmSplit) {
1794cd0c2139SMatthew G Knepley   DM       sdm;
1795cd0c2139SMatthew G Knepley   PetscInt dim;
1796cd0c2139SMatthew G Knepley 
1797cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1798cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1799064a246eSJacob Faibussowitsch   PetscValidPointer(dmSplit, 4);
18009566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &sdm));
18019566063dSJacob Faibussowitsch   PetscCall(DMSetType(sdm, DMPLEX));
18029566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
18039566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(sdm, dim));
1804cd0c2139SMatthew G Knepley   switch (dim) {
1805cd0c2139SMatthew G Knepley   case 2:
18069371c9d4SSatish Balay   case 3: PetscCall(DMPlexConstructCohesiveCells_Internal(dm, label, splitLabel, sdm)); break;
18079371c9d4SSatish Balay   default: SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct cohesive cells for dimension %" PetscInt_FMT, dim);
1808cd0c2139SMatthew G Knepley   }
1809cd0c2139SMatthew G Knepley   *dmSplit = sdm;
1810cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1811cd0c2139SMatthew G Knepley }
1812cd0c2139SMatthew G Knepley 
18130f66a230SMatthew G. Knepley /* Returns the side of the surface for a given cell with a face on the surface */
18149371c9d4SSatish Balay static PetscErrorCode GetSurfaceSide_Static(DM dm, DM subdm, PetscInt numSubpoints, const PetscInt *subpoints, PetscInt cell, PetscInt face, PetscBool *pos) {
18150f66a230SMatthew G. Knepley   const PetscInt *cone, *ornt;
18160f66a230SMatthew G. Knepley   PetscInt        dim, coneSize, c;
18170f66a230SMatthew G. Knepley 
18180f66a230SMatthew G. Knepley   PetscFunctionBegin;
18190f66a230SMatthew G. Knepley   *pos = PETSC_TRUE;
18209566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
18219566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
18229566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCone(dm, cell, &cone));
18239566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeOrientation(dm, cell, &ornt));
18240f66a230SMatthew G. Knepley   for (c = 0; c < coneSize; ++c) {
18250f66a230SMatthew G. Knepley     if (cone[c] == face) {
18260f66a230SMatthew G. Knepley       PetscInt o = ornt[c];
18270f66a230SMatthew G. Knepley 
18280f66a230SMatthew G. Knepley       if (subdm) {
18290f66a230SMatthew G. Knepley         const PetscInt *subcone, *subornt;
18300f66a230SMatthew G. Knepley         PetscInt        subpoint, subface, subconeSize, sc;
18310f66a230SMatthew G. Knepley 
18329566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cell, numSubpoints, subpoints, &subpoint));
18339566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(face, numSubpoints, subpoints, &subface));
18349566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeSize(subdm, subpoint, &subconeSize));
18359566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCone(subdm, subpoint, &subcone));
18369566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeOrientation(subdm, subpoint, &subornt));
18370f66a230SMatthew G. Knepley         for (sc = 0; sc < subconeSize; ++sc) {
18380f66a230SMatthew G. Knepley           if (subcone[sc] == subface) {
18390f66a230SMatthew G. Knepley             o = subornt[0];
18400f66a230SMatthew G. Knepley             break;
18410f66a230SMatthew G. Knepley           }
18420f66a230SMatthew G. Knepley         }
184363a3b9bcSJacob Faibussowitsch         PetscCheck(sc < subconeSize, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find subpoint %" PetscInt_FMT " (%" PetscInt_FMT ") in cone for subpoint %" PetscInt_FMT " (%" PetscInt_FMT ")", subface, face, subpoint, cell);
18440f66a230SMatthew G. Knepley       }
18450f66a230SMatthew G. Knepley       if (o >= 0) *pos = PETSC_TRUE;
18460f66a230SMatthew G. Knepley       else *pos = PETSC_FALSE;
18470f66a230SMatthew G. Knepley       break;
18480f66a230SMatthew G. Knepley     }
18490f66a230SMatthew G. Knepley   }
185063a3b9bcSJacob Faibussowitsch   PetscCheck(c != coneSize, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Cell %" PetscInt_FMT " in split face %" PetscInt_FMT " support does not have it in the cone", cell, face);
18510f66a230SMatthew G. Knepley   PetscFunctionReturn(0);
18520f66a230SMatthew G. Knepley }
18530f66a230SMatthew G. Knepley 
18549371c9d4SSatish Balay static PetscErrorCode CheckFaultEdge_Private(DM dm, DMLabel label) {
1855accc9626SMatthew G. Knepley   IS              facePosIS, faceNegIS, dimIS;
1856accc9626SMatthew G. Knepley   const PetscInt *points;
1857accc9626SMatthew G. Knepley   PetscInt        dim, numPoints, p, shift = 100, shift2 = 200;
1858accc9626SMatthew G. Knepley 
1859accc9626SMatthew G. Knepley   PetscFunctionBegin;
1860accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
1861accc9626SMatthew G. Knepley   /* If any faces touching the fault divide cells on either side, split them */
1862accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, shift + dim - 1, &facePosIS));
1863accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, -(shift + dim - 1), &faceNegIS));
1864accc9626SMatthew G. Knepley   if (!facePosIS || !faceNegIS) {
1865accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&facePosIS));
1866accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&faceNegIS));
1867accc9626SMatthew G. Knepley     PetscFunctionReturn(0);
1868accc9626SMatthew G. Knepley   }
1869accc9626SMatthew G. Knepley   PetscCall(ISExpand(facePosIS, faceNegIS, &dimIS));
1870accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&facePosIS));
1871accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&faceNegIS));
1872accc9626SMatthew G. Knepley   PetscCall(ISGetLocalSize(dimIS, &numPoints));
1873accc9626SMatthew G. Knepley   PetscCall(ISGetIndices(dimIS, &points));
1874accc9626SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
1875accc9626SMatthew G. Knepley     const PetscInt  point = points[p];
1876accc9626SMatthew G. Knepley     const PetscInt *support;
1877accc9626SMatthew G. Knepley     PetscInt        supportSize, valA, valB;
1878accc9626SMatthew G. Knepley 
1879accc9626SMatthew G. Knepley     PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
1880accc9626SMatthew G. Knepley     if (supportSize != 2) continue;
1881accc9626SMatthew G. Knepley     PetscCall(DMPlexGetSupport(dm, point, &support));
1882accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, support[0], &valA));
1883accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, support[1], &valB));
1884accc9626SMatthew G. Knepley     if ((valA == -1) || (valB == -1)) continue;
1885accc9626SMatthew G. Knepley     if (valA * valB > 0) continue;
1886e1a13daeSMatthew G. Knepley     /* Check that this face is not incident on only unsplit faces, meaning has at least one split face */
1887e1a13daeSMatthew G. Knepley     {
1888e1a13daeSMatthew G. Knepley       PetscInt *closure = NULL;
1889e1a13daeSMatthew G. Knepley       PetscBool split   = PETSC_FALSE;
1890e1a13daeSMatthew G. Knepley       PetscInt  closureSize, cl;
1891e1a13daeSMatthew G. Knepley 
1892e1a13daeSMatthew G. Knepley       PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
1893e1a13daeSMatthew G. Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
1894e1a13daeSMatthew G. Knepley         PetscCall(DMLabelGetValue(label, closure[cl], &valA));
18959371c9d4SSatish Balay         if ((valA >= 0) && (valA <= dim)) {
18969371c9d4SSatish Balay           split = PETSC_TRUE;
18979371c9d4SSatish Balay           break;
18989371c9d4SSatish Balay         }
1899e1a13daeSMatthew G. Knepley       }
1900e1a13daeSMatthew G. Knepley       PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
1901e1a13daeSMatthew G. Knepley       if (!split) continue;
1902e1a13daeSMatthew G. Knepley     }
1903accc9626SMatthew G. Knepley     /* Split the face */
1904accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, point, &valA));
1905accc9626SMatthew G. Knepley     PetscCall(DMLabelClearValue(label, point, valA));
1906accc9626SMatthew G. Knepley     PetscCall(DMLabelSetValue(label, point, dim - 1));
1907accc9626SMatthew G. Knepley     /* Label its closure:
1908accc9626SMatthew G. Knepley       unmarked: label as unsplit
1909accc9626SMatthew G. Knepley       incident: relabel as split
1910accc9626SMatthew G. Knepley       split:    do nothing
1911accc9626SMatthew G. Knepley     */
1912accc9626SMatthew G. Knepley     {
1913accc9626SMatthew G. Knepley       PetscInt *closure = NULL;
1914accc9626SMatthew G. Knepley       PetscInt  closureSize, cl, dep;
1915accc9626SMatthew G. Knepley 
1916accc9626SMatthew G. Knepley       PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
1917accc9626SMatthew G. Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
1918accc9626SMatthew G. Knepley         PetscCall(DMLabelGetValue(label, closure[cl], &valA));
1919accc9626SMatthew G. Knepley         if (valA == -1) { /* Mark as unsplit */
1920accc9626SMatthew G. Knepley           PetscCall(DMPlexGetPointDepth(dm, closure[cl], &dep));
1921accc9626SMatthew G. Knepley           PetscCall(DMLabelSetValue(label, closure[cl], shift2 + dep));
1922accc9626SMatthew G. Knepley         } else if (((valA >= shift) && (valA < shift2)) || ((valA <= -shift) && (valA > -shift2))) {
1923accc9626SMatthew G. Knepley           PetscCall(DMPlexGetPointDepth(dm, closure[cl], &dep));
1924accc9626SMatthew G. Knepley           PetscCall(DMLabelClearValue(label, closure[cl], valA));
1925accc9626SMatthew G. Knepley           PetscCall(DMLabelSetValue(label, closure[cl], dep));
1926accc9626SMatthew G. Knepley         }
1927accc9626SMatthew G. Knepley       }
1928accc9626SMatthew G. Knepley       PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
1929accc9626SMatthew G. Knepley     }
1930accc9626SMatthew G. Knepley   }
1931accc9626SMatthew G. Knepley   PetscCall(ISRestoreIndices(dimIS, &points));
1932accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&dimIS));
1933accc9626SMatthew G. Knepley   PetscFunctionReturn(0);
1934accc9626SMatthew G. Knepley }
1935accc9626SMatthew G. Knepley 
1936cd0c2139SMatthew G Knepley /*@
19370f66a230SMatthew G. Knepley   DMPlexLabelCohesiveComplete - Starting with a label marking points on an internal surface, we add all other mesh pieces
1938cd0c2139SMatthew G Knepley   to complete the surface
1939cd0c2139SMatthew G Knepley 
1940cd0c2139SMatthew G Knepley   Input Parameters:
1941cd0c2139SMatthew G Knepley + dm     - The DM
19420f66a230SMatthew G. Knepley . label  - A DMLabel marking the surface
19430f66a230SMatthew G. Knepley . blabel - A DMLabel marking the vertices on the boundary which will not be duplicated, or NULL to find them automatically
1944caf9e14dSMatthew G. Knepley . bvalue - Value of DMLabel marking the vertices on the boundary
1945bb55d314SMatthew G. Knepley . flip   - Flag to flip the submesh normal and replace points on the other side
194647946fd8SMatthew G. Knepley - subdm  - The subDM associated with the label, or NULL
1947cd0c2139SMatthew G Knepley 
1948cd0c2139SMatthew G Knepley   Output Parameter:
1949cd0c2139SMatthew G Knepley . label - A DMLabel marking all surface points
1950cd0c2139SMatthew G Knepley 
19510f66a230SMatthew G. Knepley   Note: The vertices in blabel are called "unsplit" in the terminology from hybrid cell creation.
19520f66a230SMatthew G. Knepley 
1953cd0c2139SMatthew G Knepley   Level: developer
1954cd0c2139SMatthew G Knepley 
1955db781477SPatrick Sanan .seealso: `DMPlexConstructCohesiveCells()`, `DMPlexLabelComplete()`
1956cd0c2139SMatthew G Knepley @*/
19579371c9d4SSatish Balay PetscErrorCode DMPlexLabelCohesiveComplete(DM dm, DMLabel label, DMLabel blabel, PetscInt bvalue, PetscBool flip, DM subdm) {
1958d90583fdSMatthew G. Knepley   DMLabel         depthLabel;
1959accc9626SMatthew G. Knepley   IS              dimIS, subpointIS = NULL;
196047946fd8SMatthew G. Knepley   const PetscInt *points, *subpoints;
1961bb55d314SMatthew G. Knepley   const PetscInt  rev   = flip ? -1 : 1;
1962accc9626SMatthew G. Knepley   PetscInt        shift = 100, shift2 = 200, shift3 = 300, dim, depth, numPoints, numSubpoints, p, val;
1963cd0c2139SMatthew G Knepley 
1964cd0c2139SMatthew G Knepley   PetscFunctionBegin;
19659566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
19669566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
19679566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
196847946fd8SMatthew G. Knepley   if (subdm) {
19699566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(subdm, &subpointIS));
197047946fd8SMatthew G. Knepley     if (subpointIS) {
19719566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(subpointIS, &numSubpoints));
19729566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subpointIS, &subpoints));
197347946fd8SMatthew G. Knepley     }
197447946fd8SMatthew G. Knepley   }
1975d7c8f101SMatthew G. Knepley   /* Mark cell on the fault, and its faces which touch the fault: cell orientation for face gives the side of the fault */
19769566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, dim - 1, &dimIS));
1977accc9626SMatthew G. Knepley   if (!dimIS) goto divide;
19789566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(dimIS, &numPoints));
19799566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(dimIS, &points));
1980d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault faces */
1981cd0c2139SMatthew G Knepley     const PetscInt *support;
1982cd0c2139SMatthew G Knepley     PetscInt        supportSize, s;
1983cd0c2139SMatthew G Knepley 
19849566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, points[p], &supportSize));
1985c4419245SMatthew G. Knepley #if 0
1986c4419245SMatthew G. Knepley     if (supportSize != 2) {
1987c4419245SMatthew G. Knepley       const PetscInt *lp;
1988c4419245SMatthew G. Knepley       PetscInt        Nlp, pind;
1989c4419245SMatthew G. Knepley 
1990c4419245SMatthew G. Knepley       /* Check that for a cell with a single support face, that face is in the SF */
1991c4419245SMatthew G. Knepley       /*   THis check only works for the remote side. We would need root side information */
19929566063dSJacob Faibussowitsch       PetscCall(PetscSFGetGraph(dm->sf, NULL, &Nlp, &lp, NULL));
19939566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(points[p], Nlp, lp, &pind));
199463a3b9bcSJacob Faibussowitsch       PetscCheck(pind >= 0,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Split face %" PetscInt_FMT " has %" PetscInt_FMT " != 2 supports, and the face is not shared with another process", points[p], supportSize);
1995c4419245SMatthew G. Knepley     }
1996c4419245SMatthew G. Knepley #endif
19979566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, points[p], &support));
1998cd0c2139SMatthew G Knepley     for (s = 0; s < supportSize; ++s) {
19990f66a230SMatthew G. Knepley       const PetscInt *cone;
2000cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
20010f66a230SMatthew G. Knepley       PetscBool       pos;
2002cd0c2139SMatthew G Knepley 
20039566063dSJacob Faibussowitsch       PetscCall(GetSurfaceSide_Static(dm, subdm, numSubpoints, subpoints, support[s], points[p], &pos));
20049566063dSJacob Faibussowitsch       if (pos) PetscCall(DMLabelSetValue(label, support[s], rev * (shift + dim)));
20059566063dSJacob Faibussowitsch       else PetscCall(DMLabelSetValue(label, support[s], -rev * (shift + dim)));
20060f66a230SMatthew G. Knepley       if (rev < 0) pos = !pos ? PETSC_TRUE : PETSC_FALSE;
20070f66a230SMatthew G. Knepley       /* Put faces touching the fault in the label */
20089566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, support[s], &coneSize));
20099566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, support[s], &cone));
2010cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
2011cd0c2139SMatthew G Knepley         const PetscInt point = cone[c];
2012cd0c2139SMatthew G Knepley 
20139566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2014cd0c2139SMatthew G Knepley         if (val == -1) {
2015cd0c2139SMatthew G Knepley           PetscInt *closure = NULL;
2016cd0c2139SMatthew G Knepley           PetscInt  closureSize, cl;
2017cd0c2139SMatthew G Knepley 
20189566063dSJacob Faibussowitsch           PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2019cd0c2139SMatthew G Knepley           for (cl = 0; cl < closureSize * 2; cl += 2) {
2020cd0c2139SMatthew G Knepley             const PetscInt clp  = closure[cl];
2021a0541d8aSMatthew G. Knepley             PetscInt       bval = -1;
2022cd0c2139SMatthew G Knepley 
20239566063dSJacob Faibussowitsch             PetscCall(DMLabelGetValue(label, clp, &val));
20249566063dSJacob Faibussowitsch             if (blabel) PetscCall(DMLabelGetValue(blabel, clp, &bval));
2025a0541d8aSMatthew G. Knepley             if ((val >= 0) && (val < dim - 1) && (bval < 0)) {
20269566063dSJacob Faibussowitsch               PetscCall(DMLabelSetValue(label, point, pos == PETSC_TRUE ? shift + dim - 1 : -(shift + dim - 1)));
2027cd0c2139SMatthew G Knepley               break;
2028cd0c2139SMatthew G Knepley             }
2029cd0c2139SMatthew G Knepley           }
20309566063dSJacob Faibussowitsch           PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2031cd0c2139SMatthew G Knepley         }
2032cd0c2139SMatthew G Knepley       }
2033cd0c2139SMatthew G Knepley     }
2034cd0c2139SMatthew G Knepley   }
2035accc9626SMatthew G. Knepley   PetscCall(ISRestoreIndices(dimIS, &points));
2036accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&dimIS));
2037a0541d8aSMatthew G. Knepley   /* Mark boundary points as unsplit */
203886200784SMatthew G. Knepley   if (blabel) {
2039accc9626SMatthew G. Knepley     IS bdIS;
2040accc9626SMatthew G. Knepley 
2041caf9e14dSMatthew G. Knepley     PetscCall(DMLabelGetStratumIS(blabel, bvalue, &bdIS));
2042accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(bdIS, &numPoints));
2043accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(bdIS, &points));
2044a0541d8aSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2045a0541d8aSMatthew G. Knepley       const PetscInt point = points[p];
2046a0541d8aSMatthew G. Knepley       PetscInt       val, bval;
2047a0541d8aSMatthew G. Knepley 
20489566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(blabel, point, &bval));
2049a0541d8aSMatthew G. Knepley       if (bval >= 0) {
20509566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2051f7019248SMatthew G. Knepley         if ((val < 0) || (val > dim)) {
2052f7019248SMatthew G. Knepley           /* This could be a point added from splitting a vertex on an adjacent fault, otherwise its just wrong */
20539566063dSJacob Faibussowitsch           PetscCall(DMLabelClearValue(blabel, point, bval));
2054f7019248SMatthew G. Knepley         }
2055f7019248SMatthew G. Knepley       }
2056f7019248SMatthew G. Knepley     }
2057f7019248SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2058f7019248SMatthew G. Knepley       const PetscInt point = points[p];
2059f7019248SMatthew G. Knepley       PetscInt       val, bval;
2060f7019248SMatthew G. Knepley 
20619566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(blabel, point, &bval));
2062f7019248SMatthew G. Knepley       if (bval >= 0) {
206386200784SMatthew G. Knepley         const PetscInt *cone, *support;
206486200784SMatthew G. Knepley         PetscInt        coneSize, supportSize, s, valA, valB, valE;
206586200784SMatthew G. Knepley 
2066a0541d8aSMatthew G. Knepley         /* Mark as unsplit */
20679566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2068e1a13daeSMatthew G. Knepley         PetscCheck(val >= 0 && val <= dim, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %" PetscInt_FMT " has label value %" PetscInt_FMT ", should be part of the fault", point, val);
20699566063dSJacob Faibussowitsch         PetscCall(DMLabelClearValue(label, point, val));
20709566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(label, point, shift2 + val));
20712c06a818SMatthew G. Knepley         /* Check for cross-edge
20722c06a818SMatthew G. Knepley              A cross-edge has endpoints which are both on the boundary of the surface, but the edge itself is not. */
207386200784SMatthew G. Knepley         if (val != 0) continue;
20749566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, point, &support));
20759566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
207686200784SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
20779566063dSJacob Faibussowitsch           PetscCall(DMPlexGetCone(dm, support[s], &cone));
20789566063dSJacob Faibussowitsch           PetscCall(DMPlexGetConeSize(dm, support[s], &coneSize));
207963a3b9bcSJacob Faibussowitsch           PetscCheck(coneSize == 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Edge %" PetscInt_FMT " has %" PetscInt_FMT " vertices != 2", support[s], coneSize);
20809566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, cone[0], &valA));
20819566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, cone[1], &valB));
20829566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, support[s], &valE));
20839566063dSJacob Faibussowitsch           if ((valE < 0) && (valA >= 0) && (valB >= 0) && (cone[0] != cone[1])) PetscCall(DMLabelSetValue(blabel, support[s], 2));
208486200784SMatthew G. Knepley         }
2085a0541d8aSMatthew G. Knepley       }
2086a0541d8aSMatthew G. Knepley     }
2087accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(bdIS, &points));
2088accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&bdIS));
2089a0541d8aSMatthew G. Knepley   }
2090b6dfa339SMatthew G. Knepley   /* Mark ghost fault cells */
2091b6dfa339SMatthew G. Knepley   {
2092b6dfa339SMatthew G. Knepley     PetscSF         sf;
2093b6dfa339SMatthew G. Knepley     const PetscInt *leaves;
2094b6dfa339SMatthew G. Knepley     PetscInt        Nl, l;
2095b6dfa339SMatthew G. Knepley 
2096b6dfa339SMatthew G. Knepley     PetscCall(DMGetPointSF(dm, &sf));
2097b6dfa339SMatthew G. Knepley     PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL));
2098accc9626SMatthew G. Knepley     PetscCall(DMLabelGetStratumIS(label, dim - 1, &dimIS));
2099accc9626SMatthew G. Knepley     if (!dimIS) goto divide;
2100accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(dimIS, &numPoints));
2101accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(dimIS, &points));
2102b6dfa339SMatthew G. Knepley     if (Nl > 0) {
2103b6dfa339SMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
2104b6dfa339SMatthew G. Knepley         const PetscInt point = points[p];
2105b6dfa339SMatthew G. Knepley         PetscInt       val;
2106b6dfa339SMatthew G. Knepley 
2107b6dfa339SMatthew G. Knepley         PetscCall(PetscFindInt(point, Nl, leaves, &l));
2108b6dfa339SMatthew G. Knepley         if (l >= 0) {
2109b6dfa339SMatthew G. Knepley           PetscInt *closure = NULL;
2110b6dfa339SMatthew G. Knepley           PetscInt  closureSize, cl;
2111b6dfa339SMatthew G. Knepley 
2112b6dfa339SMatthew G. Knepley           PetscCall(DMLabelGetValue(label, point, &val));
2113b6dfa339SMatthew G. Knepley           PetscCheck((val == dim - 1) || (val == shift2 + dim - 1), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %" PetscInt_FMT " has label value %" PetscInt_FMT ", should be a fault face", point, val);
2114b6dfa339SMatthew G. Knepley           PetscCall(DMLabelClearValue(label, point, val));
2115b6dfa339SMatthew G. Knepley           PetscCall(DMLabelSetValue(label, point, shift3 + val));
2116b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2117accc9626SMatthew G. Knepley           for (cl = 2; cl < closureSize * 2; cl += 2) {
2118b6dfa339SMatthew G. Knepley             const PetscInt clp = closure[cl];
2119b6dfa339SMatthew G. Knepley 
2120b6dfa339SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, clp, &val));
2121b6dfa339SMatthew G. Knepley             PetscCheck(val != -1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %" PetscInt_FMT " is missing from label, but is in the closure of a fault face", point);
2122b6dfa339SMatthew G. Knepley             PetscCall(DMLabelClearValue(label, clp, val));
2123b6dfa339SMatthew G. Knepley             PetscCall(DMLabelSetValue(label, clp, shift3 + val));
2124b6dfa339SMatthew G. Knepley           }
2125b6dfa339SMatthew G. Knepley           PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2126b6dfa339SMatthew G. Knepley         }
2127b6dfa339SMatthew G. Knepley       }
2128b6dfa339SMatthew G. Knepley     }
2129b6dfa339SMatthew G. Knepley     PetscCall(ISRestoreIndices(dimIS, &points));
2130b6dfa339SMatthew G. Knepley     PetscCall(ISDestroy(&dimIS));
2131accc9626SMatthew G. Knepley   }
2132accc9626SMatthew G. Knepley divide:
2133b6dfa339SMatthew G. Knepley   if (subpointIS) PetscCall(ISRestoreIndices(subpointIS, &subpoints));
2134accc9626SMatthew G. Knepley   PetscCall(DMPlexLabelFaultHalo(dm, label));
2135accc9626SMatthew G. Knepley   PetscCall(CheckFaultEdge_Private(dm, label));
2136cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
2137cd0c2139SMatthew G Knepley }
2138cd0c2139SMatthew G Knepley 
2139720e594eSMatthew G. Knepley /* Check that no cell have all vertices on the fault */
21409371c9d4SSatish Balay PetscErrorCode DMPlexCheckValidSubmesh_Private(DM dm, DMLabel label, DM subdm) {
2141720e594eSMatthew G. Knepley   IS              subpointIS;
2142720e594eSMatthew G. Knepley   const PetscInt *dmpoints;
2143720e594eSMatthew G. Knepley   PetscInt        defaultValue, cStart, cEnd, c, vStart, vEnd;
2144720e594eSMatthew G. Knepley 
2145720e594eSMatthew G. Knepley   PetscFunctionBegin;
2146720e594eSMatthew G. Knepley   if (!label) PetscFunctionReturn(0);
21479566063dSJacob Faibussowitsch   PetscCall(DMLabelGetDefaultValue(label, &defaultValue));
21489566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointIS(subdm, &subpointIS));
2149720e594eSMatthew G. Knepley   if (!subpointIS) PetscFunctionReturn(0);
21509566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(subdm, 0, &cStart, &cEnd));
21519566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
21529566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(subpointIS, &dmpoints));
2153720e594eSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2154720e594eSMatthew G. Knepley     PetscBool invalidCell = PETSC_TRUE;
2155720e594eSMatthew G. Knepley     PetscInt *closure     = NULL;
2156720e594eSMatthew G. Knepley     PetscInt  closureSize, cl;
2157720e594eSMatthew G. Knepley 
21589566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure));
2159720e594eSMatthew G. Knepley     for (cl = 0; cl < closureSize * 2; cl += 2) {
2160720e594eSMatthew G. Knepley       PetscInt value = 0;
2161720e594eSMatthew G. Knepley 
2162720e594eSMatthew G. Knepley       if ((closure[cl] < vStart) || (closure[cl] >= vEnd)) continue;
21639566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, closure[cl], &value));
21649371c9d4SSatish Balay       if (value == defaultValue) {
21659371c9d4SSatish Balay         invalidCell = PETSC_FALSE;
21669371c9d4SSatish Balay         break;
21679371c9d4SSatish Balay       }
2168720e594eSMatthew G. Knepley     }
21699566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure));
2170720e594eSMatthew G. Knepley     if (invalidCell) {
21719566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(subpointIS, &dmpoints));
21729566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&subpointIS));
21739566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&subdm));
217463a3b9bcSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ambiguous submesh. Cell %" PetscInt_FMT " has all of its vertices on the submesh.", dmpoints[c]);
2175720e594eSMatthew G. Knepley     }
2176720e594eSMatthew G. Knepley   }
21779566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(subpointIS, &dmpoints));
2178720e594eSMatthew G. Knepley   PetscFunctionReturn(0);
2179720e594eSMatthew G. Knepley }
2180720e594eSMatthew G. Knepley 
2181c08575a3SMatthew G. Knepley /*@
21823cf72582SMatthew G. Knepley   DMPlexCreateHybridMesh - Create a mesh with hybrid cells along an internal interface
21833cf72582SMatthew G. Knepley 
21843cf72582SMatthew G. Knepley   Collective on dm
21853cf72582SMatthew G. Knepley 
21863cf72582SMatthew G. Knepley   Input Parameters:
21873cf72582SMatthew G. Knepley + dm - The original DM
2188720e594eSMatthew G. Knepley . label - The label specifying the interface vertices
2189caf9e14dSMatthew G. Knepley . bdlabel - The optional label specifying the interface boundary vertices
2190caf9e14dSMatthew G. Knepley - bdvalue - Value of optional label specifying the interface boundary vertices
21913cf72582SMatthew G. Knepley 
21923cf72582SMatthew G. Knepley   Output Parameters:
21937db7e0a7SMatthew G. Knepley + hybridLabel - The label fully marking the interface, or NULL if no output is desired
21947db7e0a7SMatthew G. Knepley . splitLabel - The label containing the split points, or NULL if no output is desired
2195720e594eSMatthew G. Knepley . dmInterface - The new interface DM, or NULL
2196720e594eSMatthew G. Knepley - dmHybrid - The new DM with cohesive cells
21973cf72582SMatthew G. Knepley 
21986eccb800SMatthew Knepley   Note: The hybridLabel indicates what parts of the original mesh impinged on the on division surface. For points
21996eccb800SMatthew Knepley   directly on the division surface, they are labeled with their dimension, so an edge 7 on the division surface would be
22006eccb800SMatthew Knepley   7 (1) in hybridLabel. For points that impinge from the positive side, they are labeled with 100+dim, so an edge 6 with
22016eccb800SMatthew Knepley   one vertex 3 on the surface would be 6 (101) and 3 (0) in hybridLabel. If an edge 9 from the negative side of the
22026eccb800SMatthew Knepley   surface also hits vertex 3, it would be 9 (-101) in hybridLabel.
22036eccb800SMatthew Knepley 
22046eccb800SMatthew Knepley   The splitLabel indicates what points in the new hybrid mesh were the result of splitting points in the original
22056eccb800SMatthew Knepley   mesh. The label value is +=100+dim for each point. For example, if two edges 10 and 14 in the hybrid resulting from
22066eccb800SMatthew Knepley   splitting an edge in the original mesh, you would have 10 (101) and 14 (-101) in the splitLabel.
22076eccb800SMatthew Knepley 
22086eccb800SMatthew Knepley   The dmInterface is a DM built from the original division surface. It has a label which can be retrieved using
22096eccb800SMatthew Knepley   DMPlexGetSubpointMap() which maps each point back to the point in the surface of the original mesh.
22106eccb800SMatthew Knepley 
22113cf72582SMatthew G. Knepley   Level: developer
22123cf72582SMatthew G. Knepley 
2213db781477SPatrick Sanan .seealso: `DMPlexConstructCohesiveCells()`, `DMPlexLabelCohesiveComplete()`, `DMPlexGetSubpointMap()`, `DMCreate()`
22143cf72582SMatthew G. Knepley @*/
22159371c9d4SSatish Balay PetscErrorCode DMPlexCreateHybridMesh(DM dm, DMLabel label, DMLabel bdlabel, PetscInt bdvalue, DMLabel *hybridLabel, DMLabel *splitLabel, DM *dmInterface, DM *dmHybrid) {
22163cf72582SMatthew G. Knepley   DM       idm;
22177db7e0a7SMatthew G. Knepley   DMLabel  subpointMap, hlabel, slabel = NULL;
22183cf72582SMatthew G. Knepley   PetscInt dim;
22193cf72582SMatthew G. Knepley 
22203cf72582SMatthew G. Knepley   PetscFunctionBegin;
22213cf72582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
222292ef8ba2SVaclav Hapla   if (label) PetscValidPointer(label, 2);
2223720e594eSMatthew G. Knepley   if (bdlabel) PetscValidPointer(bdlabel, 3);
222492ef8ba2SVaclav Hapla   if (hybridLabel) PetscValidPointer(hybridLabel, 5);
222592ef8ba2SVaclav Hapla   if (splitLabel) PetscValidPointer(splitLabel, 6);
222692ef8ba2SVaclav Hapla   if (dmInterface) PetscValidPointer(dmInterface, 7);
222792ef8ba2SVaclav Hapla   PetscValidPointer(dmHybrid, 8);
22289566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
22299566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmesh(dm, label, 1, PETSC_FALSE, &idm));
22309566063dSJacob Faibussowitsch   PetscCall(DMPlexCheckValidSubmesh_Private(dm, label, idm));
22319566063dSJacob Faibussowitsch   PetscCall(DMPlexOrient(idm));
22329566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(idm, &subpointMap));
22339566063dSJacob Faibussowitsch   PetscCall(DMLabelDuplicate(subpointMap, &hlabel));
22349566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(hlabel, dim));
22357db7e0a7SMatthew G. Knepley   if (splitLabel) {
22367db7e0a7SMatthew G. Knepley     const char *name;
22377db7e0a7SMatthew G. Knepley     char        sname[PETSC_MAX_PATH_LEN];
22387db7e0a7SMatthew G. Knepley 
22399566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)hlabel, &name));
22409566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(sname, name, PETSC_MAX_PATH_LEN));
22419566063dSJacob Faibussowitsch     PetscCall(PetscStrcat(sname, " split"));
22429566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, sname, &slabel));
22437db7e0a7SMatthew G. Knepley   }
2244caf9e14dSMatthew G. Knepley   PetscCall(DMPlexLabelCohesiveComplete(dm, hlabel, bdlabel, bdvalue, PETSC_FALSE, idm));
22459371c9d4SSatish Balay   if (dmInterface) {
22469371c9d4SSatish Balay     *dmInterface = idm;
22479371c9d4SSatish Balay   } else PetscCall(DMDestroy(&idm));
22489566063dSJacob Faibussowitsch   PetscCall(DMPlexConstructCohesiveCells(dm, hlabel, slabel, dmHybrid));
22495de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *dmHybrid));
22503cf72582SMatthew G. Knepley   if (hybridLabel) *hybridLabel = hlabel;
22519566063dSJacob Faibussowitsch   else PetscCall(DMLabelDestroy(&hlabel));
22527db7e0a7SMatthew G. Knepley   if (splitLabel) *splitLabel = slabel;
22534a7ee7d0SMatthew G. Knepley   {
22544a7ee7d0SMatthew G. Knepley     DM      cdm;
22554a7ee7d0SMatthew G. Knepley     DMLabel ctLabel;
22564a7ee7d0SMatthew G. Knepley 
22574a7ee7d0SMatthew G. Knepley     /* We need to somehow share the celltype label with the coordinate dm */
22589566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(*dmHybrid, &cdm));
22599566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellTypeLabel(*dmHybrid, &ctLabel));
22609566063dSJacob Faibussowitsch     PetscCall(DMSetLabel(cdm, ctLabel));
22614a7ee7d0SMatthew G. Knepley   }
2262cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
2263cd0c2139SMatthew G Knepley }
2264cd0c2139SMatthew G Knepley 
2265efa14ee0SMatthew G Knepley /* Here we need the explicit assumption that:
2266efa14ee0SMatthew G Knepley 
2267efa14ee0SMatthew G Knepley      For any marked cell, the marked vertices constitute a single face
2268efa14ee0SMatthew G Knepley */
22699371c9d4SSatish Balay static PetscErrorCode DMPlexMarkSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, DM subdm) {
2270fed694aaSMatthew G. Knepley   IS              subvertexIS = NULL;
2271efa14ee0SMatthew G Knepley   const PetscInt *subvertices;
2272412e9a14SMatthew G. Knepley   PetscInt       *pStart, *pEnd, pSize;
2273efa14ee0SMatthew G Knepley   PetscInt        depth, dim, d, numSubVerticesInitial = 0, v;
2274efa14ee0SMatthew G Knepley 
2275efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2276efa14ee0SMatthew G Knepley   *numFaces = 0;
2277efa14ee0SMatthew G Knepley   *nFV      = 0;
22789566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
22799566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
228077d178adSMatthew G. Knepley   pSize = PetscMax(depth, dim) + 1;
22819566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(pSize, &pStart, pSize, &pEnd));
228248a46eb9SPierre Jolivet   for (d = 0; d <= depth; ++d) PetscCall(DMPlexGetSimplexOrBoxCells(dm, depth - d, &pStart[d], &pEnd[d]));
2283efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
22849566063dSJacob Faibussowitsch   if (vertexLabel) PetscCall(DMLabelGetStratumIS(vertexLabel, value, &subvertexIS));
2285efa14ee0SMatthew G Knepley   if (subvertexIS) {
22869566063dSJacob Faibussowitsch     PetscCall(ISGetSize(subvertexIS, &numSubVerticesInitial));
22879566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subvertexIS, &subvertices));
2288efa14ee0SMatthew G Knepley   }
2289efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2290efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
22910298fd71SBarry Smith     PetscInt      *star   = NULL;
2292efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numCells = 0, c;
2293efa14ee0SMatthew G Knepley 
22949566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2295efa14ee0SMatthew G Knepley     for (s = 0; s < starSize * 2; s += 2) {
2296efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2297efa14ee0SMatthew G Knepley       if ((point >= pStart[depth]) && (point < pEnd[depth])) star[numCells++] = point;
2298efa14ee0SMatthew G Knepley     }
2299efa14ee0SMatthew G Knepley     for (c = 0; c < numCells; ++c) {
2300efa14ee0SMatthew G Knepley       const PetscInt cell    = star[c];
23010298fd71SBarry Smith       PetscInt      *closure = NULL;
2302efa14ee0SMatthew G Knepley       PetscInt       closureSize, cl;
2303efa14ee0SMatthew G Knepley       PetscInt       cellLoc, numCorners = 0, faceSize = 0;
2304efa14ee0SMatthew G Knepley 
23059566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(subpointMap, cell, &cellLoc));
230665560c7fSMatthew G Knepley       if (cellLoc == 2) continue;
230763a3b9bcSJacob Faibussowitsch       PetscCheck(cellLoc < 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %" PetscInt_FMT " has dimension %" PetscInt_FMT " in the surface label", cell, cellLoc);
23089566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
2309efa14ee0SMatthew G Knepley       for (cl = 0; cl < closureSize * 2; cl += 2) {
2310efa14ee0SMatthew G Knepley         const PetscInt point = closure[cl];
2311efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2312efa14ee0SMatthew G Knepley 
2313efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
2314efa14ee0SMatthew G Knepley           ++numCorners;
23159566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &vertexLoc));
2316830e53efSMatthew G. Knepley           if (vertexLoc == value) closure[faceSize++] = point;
2317efa14ee0SMatthew G Knepley         }
2318efa14ee0SMatthew G Knepley       }
23199566063dSJacob Faibussowitsch       if (!(*nFV)) PetscCall(DMPlexGetNumFaceVertices(dm, dim, numCorners, nFV));
232063a3b9bcSJacob Faibussowitsch       PetscCheck(faceSize <= *nFV, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %" PetscInt_FMT " of an element on the surface", faceSize);
2321efa14ee0SMatthew G Knepley       if (faceSize == *nFV) {
2322007baee2SMatthew G. Knepley         const PetscInt *cells = NULL;
2323007baee2SMatthew G. Knepley         PetscInt        numCells, nc;
2324007baee2SMatthew G. Knepley 
2325efa14ee0SMatthew G Knepley         ++(*numFaces);
232648a46eb9SPierre Jolivet         for (cl = 0; cl < faceSize; ++cl) PetscCall(DMLabelSetValue(subpointMap, closure[cl], 0));
23279566063dSJacob Faibussowitsch         PetscCall(DMPlexGetJoin(dm, faceSize, closure, &numCells, &cells));
232848a46eb9SPierre Jolivet         for (nc = 0; nc < numCells; ++nc) PetscCall(DMLabelSetValue(subpointMap, cells[nc], 2));
23299566063dSJacob Faibussowitsch         PetscCall(DMPlexRestoreJoin(dm, faceSize, closure, &numCells, &cells));
2330efa14ee0SMatthew G Knepley       }
23319566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
2332efa14ee0SMatthew G Knepley     }
23339566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2334efa14ee0SMatthew G Knepley   }
233548a46eb9SPierre Jolivet   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subvertices));
23369566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
23379566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
2338efa14ee0SMatthew G Knepley   PetscFunctionReturn(0);
2339efa14ee0SMatthew G Knepley }
2340efa14ee0SMatthew G Knepley 
23419371c9d4SSatish Balay static PetscErrorCode DMPlexMarkSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DMLabel subpointMap, DM subdm) {
234234b4c39eSMatthew G. Knepley   IS              subvertexIS = NULL;
2343efa14ee0SMatthew G Knepley   const PetscInt *subvertices;
2344412e9a14SMatthew G. Knepley   PetscInt       *pStart, *pEnd;
2345efa14ee0SMatthew G Knepley   PetscInt        dim, d, numSubVerticesInitial = 0, v;
2346efa14ee0SMatthew G Knepley 
2347efa14ee0SMatthew G Knepley   PetscFunctionBegin;
23489566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
23499566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(dim + 1, &pStart, dim + 1, &pEnd));
235048a46eb9SPierre Jolivet   for (d = 0; d <= dim; ++d) PetscCall(DMPlexGetSimplexOrBoxCells(dm, dim - d, &pStart[d], &pEnd[d]));
2351efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
235234b4c39eSMatthew G. Knepley   if (vertexLabel) {
23539566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(vertexLabel, value, &subvertexIS));
2354efa14ee0SMatthew G Knepley     if (subvertexIS) {
23559566063dSJacob Faibussowitsch       PetscCall(ISGetSize(subvertexIS, &numSubVerticesInitial));
23569566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subvertexIS, &subvertices));
2357efa14ee0SMatthew G Knepley     }
235834b4c39eSMatthew G. Knepley   }
2359efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2360efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
23610298fd71SBarry Smith     PetscInt      *star   = NULL;
2362efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numFaces = 0, f;
2363efa14ee0SMatthew G Knepley 
23649566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2365efa14ee0SMatthew G Knepley     for (s = 0; s < starSize * 2; s += 2) {
2366efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2367158acfadSMatthew G. Knepley       PetscInt       faceLoc;
2368158acfadSMatthew G. Knepley 
2369158acfadSMatthew G. Knepley       if ((point >= pStart[dim - 1]) && (point < pEnd[dim - 1])) {
2370158acfadSMatthew G. Knepley         if (markedFaces) {
23719566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &faceLoc));
2372158acfadSMatthew G. Knepley           if (faceLoc < 0) continue;
2373158acfadSMatthew G. Knepley         }
2374158acfadSMatthew G. Knepley         star[numFaces++] = point;
2375158acfadSMatthew G. Knepley       }
2376efa14ee0SMatthew G Knepley     }
2377efa14ee0SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
2378efa14ee0SMatthew G Knepley       const PetscInt face    = star[f];
23790298fd71SBarry Smith       PetscInt      *closure = NULL;
2380efa14ee0SMatthew G Knepley       PetscInt       closureSize, c;
2381efa14ee0SMatthew G Knepley       PetscInt       faceLoc;
2382efa14ee0SMatthew G Knepley 
23839566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(subpointMap, face, &faceLoc));
2384efa14ee0SMatthew G Knepley       if (faceLoc == dim - 1) continue;
238563a3b9bcSJacob Faibussowitsch       PetscCheck(faceLoc < 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %" PetscInt_FMT " has dimension %" PetscInt_FMT " in the surface label", face, faceLoc);
23869566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure));
2387efa14ee0SMatthew G Knepley       for (c = 0; c < closureSize * 2; c += 2) {
2388efa14ee0SMatthew G Knepley         const PetscInt point = closure[c];
2389efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2390efa14ee0SMatthew G Knepley 
2391efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
23929566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &vertexLoc));
2393830e53efSMatthew G. Knepley           if (vertexLoc != value) break;
2394efa14ee0SMatthew G Knepley         }
2395efa14ee0SMatthew G Knepley       }
2396efa14ee0SMatthew G Knepley       if (c == closureSize * 2) {
2397efa14ee0SMatthew G Knepley         const PetscInt *support;
2398efa14ee0SMatthew G Knepley         PetscInt        supportSize, s;
2399efa14ee0SMatthew G Knepley 
2400efa14ee0SMatthew G Knepley         for (c = 0; c < closureSize * 2; c += 2) {
2401efa14ee0SMatthew G Knepley           const PetscInt point = closure[c];
2402efa14ee0SMatthew G Knepley 
2403efa14ee0SMatthew G Knepley           for (d = 0; d < dim; ++d) {
2404efa14ee0SMatthew G Knepley             if ((point >= pStart[d]) && (point < pEnd[d])) {
24059566063dSJacob Faibussowitsch               PetscCall(DMLabelSetValue(subpointMap, point, d));
2406efa14ee0SMatthew G Knepley               break;
2407efa14ee0SMatthew G Knepley             }
2408efa14ee0SMatthew G Knepley           }
2409efa14ee0SMatthew G Knepley         }
24109566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, face, &supportSize));
24119566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, face, &support));
241248a46eb9SPierre Jolivet         for (s = 0; s < supportSize; ++s) PetscCall(DMLabelSetValue(subpointMap, support[s], dim));
2413efa14ee0SMatthew G Knepley       }
24149566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure));
2415efa14ee0SMatthew G Knepley     }
24169566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2417efa14ee0SMatthew G Knepley   }
24189566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subvertices));
24199566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
24209566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
2421efa14ee0SMatthew G Knepley   PetscFunctionReturn(0);
2422efa14ee0SMatthew G Knepley }
2423efa14ee0SMatthew G Knepley 
24249371c9d4SSatish Balay static PetscErrorCode DMPlexMarkCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char labelname[], PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, PetscInt *subCells[], DM subdm) {
242527c04023SMatthew G. Knepley   DMLabel         label = NULL;
2426766ab985SMatthew G. Knepley   const PetscInt *cone;
24279fc93327SToby Isaac   PetscInt        dim, cMax, cEnd, c, subc = 0, p, coneSize = -1;
2428766ab985SMatthew G. Knepley 
2429812bfc34SJed Brown   PetscFunctionBegin;
2430c0ed958bSJed Brown   *numFaces = 0;
2431c0ed958bSJed Brown   *nFV      = 0;
24329566063dSJacob Faibussowitsch   if (labelname) PetscCall(DMGetLabel(dm, labelname, &label));
2433fed694aaSMatthew G. Knepley   *subCells = NULL;
24349566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
24359566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd));
2436766ab985SMatthew G. Knepley   if (cMax < 0) PetscFunctionReturn(0);
243727c04023SMatthew G. Knepley   if (label) {
243827c04023SMatthew G. Knepley     for (c = cMax; c < cEnd; ++c) {
243927c04023SMatthew G. Knepley       PetscInt val;
244027c04023SMatthew G. Knepley 
24419566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
244227c04023SMatthew G. Knepley       if (val == value) {
244327c04023SMatthew G. Knepley         ++(*numFaces);
24449566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
244527c04023SMatthew G. Knepley       }
244627c04023SMatthew G. Knepley     }
244727c04023SMatthew G. Knepley   } else {
2448766ab985SMatthew G. Knepley     *numFaces = cEnd - cMax;
24499566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cMax, &coneSize));
245027c04023SMatthew G. Knepley   }
24519566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(*numFaces * 2, subCells));
24529fc93327SToby Isaac   if (!(*numFaces)) PetscFunctionReturn(0);
24539fc93327SToby Isaac   *nFV = hasLagrange ? coneSize / 3 : coneSize / 2;
2454766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2455766ab985SMatthew G. Knepley     const PetscInt *cells;
2456766ab985SMatthew G. Knepley     PetscInt        numCells;
2457766ab985SMatthew G. Knepley 
245827c04023SMatthew G. Knepley     if (label) {
245927c04023SMatthew G. Knepley       PetscInt val;
246027c04023SMatthew G. Knepley 
24619566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
246227c04023SMatthew G. Knepley       if (val != value) continue;
246327c04023SMatthew G. Knepley     }
24649566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, c, &cone));
246548a46eb9SPierre Jolivet     for (p = 0; p < *nFV; ++p) PetscCall(DMLabelSetValue(subpointMap, cone[p], 0));
2466766ab985SMatthew G. Knepley     /* Negative face */
24679566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, *nFV, cone, &numCells, &cells));
246827234c99SMatthew G. Knepley     /* Not true in parallel
246908401ef6SPierre Jolivet     PetscCheck(numCells == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
2470766ab985SMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
24719566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(subpointMap, cells[p], 2));
247227234c99SMatthew G. Knepley       (*subCells)[subc++] = cells[p];
2473766ab985SMatthew G. Knepley     }
24749566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, *nFV, cone, &numCells, &cells));
2475766ab985SMatthew G. Knepley     /* Positive face is not included */
2476766ab985SMatthew G. Knepley   }
2477766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
2478766ab985SMatthew G. Knepley }
2479766ab985SMatthew G. Knepley 
24809371c9d4SSatish Balay static PetscErrorCode DMPlexMarkCohesiveSubmesh_Interpolated(DM dm, DMLabel label, PetscInt value, DMLabel subpointMap, DM subdm) {
2481766ab985SMatthew G. Knepley   PetscInt *pStart, *pEnd;
2482766ab985SMatthew G. Knepley   PetscInt  dim, cMax, cEnd, c, d;
2483766ab985SMatthew G. Knepley 
2484812bfc34SJed Brown   PetscFunctionBegin;
24859566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
24869566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd));
2487766ab985SMatthew G. Knepley   if (cMax < 0) PetscFunctionReturn(0);
24889566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(dim + 1, &pStart, dim + 1, &pEnd));
24899566063dSJacob Faibussowitsch   for (d = 0; d <= dim; ++d) PetscCall(DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]));
2490766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2491766ab985SMatthew G. Knepley     const PetscInt *cone;
2492766ab985SMatthew G. Knepley     PetscInt       *closure = NULL;
2493b3154360SMatthew G. Knepley     PetscInt        fconeSize, coneSize, closureSize, cl, val;
2494766ab985SMatthew G. Knepley 
249527c04023SMatthew G. Knepley     if (label) {
24969566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
249727c04023SMatthew G. Knepley       if (val != value) continue;
249827c04023SMatthew G. Knepley     }
24999566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
25009566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, c, &cone));
25019566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cone[0], &fconeSize));
25021dca8a05SBarry Smith     PetscCheck(coneSize == (fconeSize ? fconeSize : 1) + 2, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells");
2503b3154360SMatthew G. Knepley     /* Negative face */
25049566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure));
2505766ab985SMatthew G. Knepley     for (cl = 0; cl < closureSize * 2; cl += 2) {
2506766ab985SMatthew G. Knepley       const PetscInt point = closure[cl];
2507766ab985SMatthew G. Knepley 
2508766ab985SMatthew G. Knepley       for (d = 0; d <= dim; ++d) {
2509766ab985SMatthew G. Knepley         if ((point >= pStart[d]) && (point < pEnd[d])) {
25109566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(subpointMap, point, d));
2511766ab985SMatthew G. Knepley           break;
2512766ab985SMatthew G. Knepley         }
2513766ab985SMatthew G. Knepley       }
2514766ab985SMatthew G. Knepley     }
25159566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure));
2516766ab985SMatthew G. Knepley     /* Cells -- positive face is not included */
2517766ab985SMatthew G. Knepley     for (cl = 0; cl < 1; ++cl) {
2518766ab985SMatthew G. Knepley       const PetscInt *support;
2519766ab985SMatthew G. Knepley       PetscInt        supportSize, s;
2520766ab985SMatthew G. Knepley 
25219566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, cone[cl], &supportSize));
252208401ef6SPierre Jolivet       /* PetscCheck(supportSize == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive faces should separate two cells"); */
25239566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, cone[cl], &support));
252448a46eb9SPierre Jolivet       for (s = 0; s < supportSize; ++s) PetscCall(DMLabelSetValue(subpointMap, support[s], dim));
2525766ab985SMatthew G. Knepley     }
2526766ab985SMatthew G. Knepley   }
25279566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
2528766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
2529766ab985SMatthew G. Knepley }
2530766ab985SMatthew G. Knepley 
25319371c9d4SSatish Balay static PetscErrorCode DMPlexGetFaceOrientation(DM dm, PetscInt cell, PetscInt numCorners, PetscInt indices[], PetscInt oppositeVertex, PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented) {
253282f516ccSBarry Smith   MPI_Comm       comm;
2533e6ccafaeSMatthew G Knepley   PetscBool      posOrient = PETSC_FALSE;
2534e6ccafaeSMatthew G Knepley   const PetscInt debug     = 0;
2535e6ccafaeSMatthew G Knepley   PetscInt       cellDim, faceSize, f;
2536e6ccafaeSMatthew G Knepley 
253782f516ccSBarry Smith   PetscFunctionBegin;
25389566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
25399566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &cellDim));
254063a3b9bcSJacob Faibussowitsch   if (debug) PetscCall(PetscPrintf(comm, "cellDim: %" PetscInt_FMT " numCorners: %" PetscInt_FMT "\n", cellDim, numCorners));
2541e6ccafaeSMatthew G Knepley 
2542ddeab2a6SMatthew G. Knepley   if (cellDim == 1 && numCorners == 2) {
2543ddeab2a6SMatthew G. Knepley     /* Triangle */
2544e6ccafaeSMatthew G Knepley     faceSize  = numCorners - 1;
2545e6ccafaeSMatthew G Knepley     posOrient = !(oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE;
2546ddeab2a6SMatthew G. Knepley   } else if (cellDim == 2 && numCorners == 3) {
2547ddeab2a6SMatthew G. Knepley     /* Triangle */
2548ddeab2a6SMatthew G. Knepley     faceSize  = numCorners - 1;
2549ddeab2a6SMatthew G. Knepley     posOrient = !(oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE;
2550ddeab2a6SMatthew G. Knepley   } else if (cellDim == 3 && numCorners == 4) {
2551ddeab2a6SMatthew G. Knepley     /* Tetrahedron */
2552ddeab2a6SMatthew G. Knepley     faceSize  = numCorners - 1;
2553ddeab2a6SMatthew G. Knepley     posOrient = (oppositeVertex % 2) ? PETSC_TRUE : PETSC_FALSE;
2554e6ccafaeSMatthew G Knepley   } else if (cellDim == 1 && numCorners == 3) {
2555e6ccafaeSMatthew G Knepley     /* Quadratic line */
2556e6ccafaeSMatthew G Knepley     faceSize  = 1;
2557e6ccafaeSMatthew G Knepley     posOrient = PETSC_TRUE;
2558e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 4) {
2559e6ccafaeSMatthew G Knepley     /* Quads */
2560e6ccafaeSMatthew G Knepley     faceSize = 2;
2561e6ccafaeSMatthew G Knepley     if ((indices[1] > indices[0]) && (indices[1] - indices[0] == 1)) {
2562e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2563e6ccafaeSMatthew G Knepley     } else if ((indices[0] == 3) && (indices[1] == 0)) {
2564e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2565e6ccafaeSMatthew G Knepley     } else {
2566e6ccafaeSMatthew G Knepley       if (((indices[0] > indices[1]) && (indices[0] - indices[1] == 1)) || ((indices[0] == 0) && (indices[1] == 3))) {
2567e6ccafaeSMatthew G Knepley         posOrient = PETSC_FALSE;
2568e6ccafaeSMatthew G Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossedge");
2569e6ccafaeSMatthew G Knepley     }
2570e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 6) {
2571e6ccafaeSMatthew G Knepley     /* Quadratic triangle (I hate this) */
2572e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2573e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTri = 3;
2574e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2575e6ccafaeSMatthew G Knepley     PetscBool      found                    = PETSC_FALSE;
2576e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTriSorted[9] = {
2577e6ccafaeSMatthew G Knepley             0, 3, 4, /* bottom */
2578e6ccafaeSMatthew G Knepley             1, 4, 5, /* right */
2579e6ccafaeSMatthew G Knepley             2, 3, 5, /* left */
2580e6ccafaeSMatthew G Knepley     };
2581e6ccafaeSMatthew G Knepley     PetscInt faceVerticesTri[9] = {
2582e6ccafaeSMatthew G Knepley       0, 3, 4, /* bottom */
2583e6ccafaeSMatthew G Knepley       1, 4, 5, /* right */
2584e6ccafaeSMatthew G Knepley       2, 5, 3, /* left */
2585e6ccafaeSMatthew G Knepley     };
2586e6ccafaeSMatthew G Knepley 
2587e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTri; ++i) sortedIndices[i] = indices[i];
25889566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeTri, sortedIndices));
2589e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 3; ++iFace) {
2590e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeTri;
2591e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2592e6ccafaeSMatthew G Knepley 
25939371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesTriSorted[ii + 0]) && (sortedIndices[1] == faceVerticesTriSorted[ii + 1])) {
2594e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTri; ++fVertex) {
2595e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTri; ++cVertex) {
2596e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTri[ii + fVertex]) {
2597e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2598e6ccafaeSMatthew G Knepley               break;
2599e6ccafaeSMatthew G Knepley             }
2600e6ccafaeSMatthew G Knepley           }
2601e6ccafaeSMatthew G Knepley         }
2602e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2603e6ccafaeSMatthew G Knepley         break;
2604e6ccafaeSMatthew G Knepley       }
2605e6ccafaeSMatthew G Knepley     }
260628b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid tri crossface");
2607e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2608e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2609e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 9) {
2610e6ccafaeSMatthew G Knepley     /* Quadratic quad (I hate this) */
2611e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2612e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuad = 3;
2613e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2614e6ccafaeSMatthew G Knepley     PetscBool      found                      = PETSC_FALSE;
2615e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadSorted[12] = {
2616e6ccafaeSMatthew G Knepley             0, 1, 4, /* bottom */
2617e6ccafaeSMatthew G Knepley             1, 2, 5, /* right */
2618e6ccafaeSMatthew G Knepley             2, 3, 6, /* top */
2619e6ccafaeSMatthew G Knepley             0, 3, 7, /* left */
2620e6ccafaeSMatthew G Knepley     };
2621e6ccafaeSMatthew G Knepley     PetscInt faceVerticesQuad[12] = {
2622e6ccafaeSMatthew G Knepley       0, 1, 4, /* bottom */
2623e6ccafaeSMatthew G Knepley       1, 2, 5, /* right */
2624e6ccafaeSMatthew G Knepley       2, 3, 6, /* top */
2625e6ccafaeSMatthew G Knepley       3, 0, 7, /* left */
2626e6ccafaeSMatthew G Knepley     };
2627e6ccafaeSMatthew G Knepley 
2628e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuad; ++i) sortedIndices[i] = indices[i];
26299566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeQuad, sortedIndices));
2630e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2631e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeQuad;
2632e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2633e6ccafaeSMatthew G Knepley 
26349371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesQuadSorted[ii + 0]) && (sortedIndices[1] == faceVerticesQuadSorted[ii + 1])) {
2635e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuad; ++fVertex) {
2636e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuad; ++cVertex) {
2637e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuad[ii + fVertex]) {
2638e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2639e6ccafaeSMatthew G Knepley               break;
2640e6ccafaeSMatthew G Knepley             }
2641e6ccafaeSMatthew G Knepley           }
2642e6ccafaeSMatthew G Knepley         }
2643e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2644e6ccafaeSMatthew G Knepley         break;
2645e6ccafaeSMatthew G Knepley       }
2646e6ccafaeSMatthew G Knepley     }
264728b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossface");
2648e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2649e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2650e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 8) {
2651e6ccafaeSMatthew G Knepley     /* Hexes
2652e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2653e6ccafaeSMatthew G Knepley        pointing up at the second.
2654e6ccafaeSMatthew G Knepley 
2655e6ccafaeSMatthew G Knepley           7---6
2656e6ccafaeSMatthew G Knepley          /|  /|
2657e6ccafaeSMatthew G Knepley         4---5 |
2658ddeab2a6SMatthew G. Knepley         | 1-|-2
2659e6ccafaeSMatthew G Knepley         |/  |/
2660ddeab2a6SMatthew G. Knepley         0---3
2661e6ccafaeSMatthew G Knepley 
2662e6ccafaeSMatthew G Knepley         Faces are determined by the first 4 vertices (corners of faces) */
2663e6ccafaeSMatthew G Knepley     const PetscInt faceSizeHex = 4;
2664e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[4], i, iFace;
2665e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2666e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHexSorted[24] = {
2667e6ccafaeSMatthew G Knepley             0, 1, 2, 3, /* bottom */
2668e6ccafaeSMatthew G Knepley             4, 5, 6, 7, /* top */
2669ddeab2a6SMatthew G. Knepley             0, 3, 4, 5, /* front */
2670ddeab2a6SMatthew G. Knepley             2, 3, 5, 6, /* right */
2671ddeab2a6SMatthew G. Knepley             1, 2, 6, 7, /* back */
2672ddeab2a6SMatthew G. Knepley             0, 1, 4, 7, /* left */
2673e6ccafaeSMatthew G Knepley     };
2674e6ccafaeSMatthew G Knepley     PetscInt faceVerticesHex[24] = {
2675ddeab2a6SMatthew G. Knepley       1, 2, 3, 0, /* bottom */
2676e6ccafaeSMatthew G Knepley       4, 5, 6, 7, /* top */
2677ddeab2a6SMatthew G. Knepley       0, 3, 5, 4, /* front */
2678ddeab2a6SMatthew G. Knepley       3, 2, 6, 5, /* right */
2679ddeab2a6SMatthew G. Knepley       2, 1, 7, 6, /* back */
2680ddeab2a6SMatthew G. Knepley       1, 0, 4, 7, /* left */
2681e6ccafaeSMatthew G Knepley     };
2682e6ccafaeSMatthew G Knepley 
2683e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeHex; ++i) sortedIndices[i] = indices[i];
26849566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeHex, sortedIndices));
2685e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2686e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeHex;
2687e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2688e6ccafaeSMatthew G Knepley 
26899371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesHexSorted[ii + 0]) && (sortedIndices[1] == faceVerticesHexSorted[ii + 1]) && (sortedIndices[2] == faceVerticesHexSorted[ii + 2]) && (sortedIndices[3] == faceVerticesHexSorted[ii + 3])) {
2690e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeHex; ++fVertex) {
2691e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeHex; ++cVertex) {
2692e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesHex[ii + fVertex]) {
2693e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2694e6ccafaeSMatthew G Knepley               break;
2695e6ccafaeSMatthew G Knepley             }
2696e6ccafaeSMatthew G Knepley           }
2697e6ccafaeSMatthew G Knepley         }
2698e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2699e6ccafaeSMatthew G Knepley         break;
2700e6ccafaeSMatthew G Knepley       }
2701e6ccafaeSMatthew G Knepley     }
270228b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2703e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2704e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2705e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 10) {
2706e6ccafaeSMatthew G Knepley     /* Quadratic tet */
2707e6ccafaeSMatthew G Knepley     /* Faces are determined by the first 3 vertices (corners of faces) */
2708e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTet = 6;
2709e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[6], i, iFace;
2710e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2711e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTetSorted[24] = {
2712e6ccafaeSMatthew G Knepley             0, 1, 2, 6, 7, 8, /* bottom */
2713e6ccafaeSMatthew G Knepley             0, 3, 4, 6, 7, 9, /* front */
2714e6ccafaeSMatthew G Knepley             1, 4, 5, 7, 8, 9, /* right */
2715e6ccafaeSMatthew G Knepley             2, 3, 5, 6, 8, 9, /* left */
2716e6ccafaeSMatthew G Knepley     };
2717e6ccafaeSMatthew G Knepley     PetscInt faceVerticesTet[24] = {
2718e6ccafaeSMatthew G Knepley       0, 1, 2, 6, 7, 8, /* bottom */
2719e6ccafaeSMatthew G Knepley       0, 4, 3, 6, 7, 9, /* front */
2720e6ccafaeSMatthew G Knepley       1, 5, 4, 7, 8, 9, /* right */
2721e6ccafaeSMatthew G Knepley       2, 3, 5, 8, 6, 9, /* left */
2722e6ccafaeSMatthew G Knepley     };
2723e6ccafaeSMatthew G Knepley 
2724e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTet; ++i) sortedIndices[i] = indices[i];
27259566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeTet, sortedIndices));
2726e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2727e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeTet;
2728e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2729e6ccafaeSMatthew G Knepley 
27309371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesTetSorted[ii + 0]) && (sortedIndices[1] == faceVerticesTetSorted[ii + 1]) && (sortedIndices[2] == faceVerticesTetSorted[ii + 2]) && (sortedIndices[3] == faceVerticesTetSorted[ii + 3])) {
2731e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTet; ++fVertex) {
2732e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTet; ++cVertex) {
2733e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTet[ii + fVertex]) {
2734e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2735e6ccafaeSMatthew G Knepley               break;
2736e6ccafaeSMatthew G Knepley             }
2737e6ccafaeSMatthew G Knepley           }
2738e6ccafaeSMatthew G Knepley         }
2739e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2740e6ccafaeSMatthew G Knepley         break;
2741e6ccafaeSMatthew G Knepley       }
2742e6ccafaeSMatthew G Knepley     }
274328b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid tet crossface");
2744e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2745e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2746e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 27) {
2747e6ccafaeSMatthew G Knepley     /* Quadratic hexes (I hate this)
2748e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2749e6ccafaeSMatthew G Knepley        pointing up at the second.
2750e6ccafaeSMatthew G Knepley 
2751e6ccafaeSMatthew G Knepley          7---6
2752e6ccafaeSMatthew G Knepley         /|  /|
2753e6ccafaeSMatthew G Knepley        4---5 |
2754e6ccafaeSMatthew G Knepley        | 3-|-2
2755e6ccafaeSMatthew G Knepley        |/  |/
2756e6ccafaeSMatthew G Knepley        0---1
2757e6ccafaeSMatthew G Knepley 
2758e6ccafaeSMatthew G Knepley        Faces are determined by the first 4 vertices (corners of faces) */
2759e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuadHex = 9;
2760e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[9], i, iFace;
2761e6ccafaeSMatthew G Knepley     PetscBool      found                         = PETSC_FALSE;
2762e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHexSorted[54] = {
2763e6ccafaeSMatthew G Knepley             0, 1, 2, 3, 8,  9,  10, 11, 24, /* bottom */
2764e6ccafaeSMatthew G Knepley             4, 5, 6, 7, 12, 13, 14, 15, 25, /* top */
2765e6ccafaeSMatthew G Knepley             0, 1, 4, 5, 8,  12, 16, 17, 22, /* front */
2766e6ccafaeSMatthew G Knepley             1, 2, 5, 6, 9,  13, 17, 18, 21, /* right */
2767e6ccafaeSMatthew G Knepley             2, 3, 6, 7, 10, 14, 18, 19, 23, /* back */
2768e6ccafaeSMatthew G Knepley             0, 3, 4, 7, 11, 15, 16, 19, 20, /* left */
2769e6ccafaeSMatthew G Knepley     };
2770e6ccafaeSMatthew G Knepley     PetscInt faceVerticesQuadHex[54] = {
2771e6ccafaeSMatthew G Knepley       3, 2, 1, 0, 10, 9,  8,  11, 24, /* bottom */
2772e6ccafaeSMatthew G Knepley       4, 5, 6, 7, 12, 13, 14, 15, 25, /* top */
2773e6ccafaeSMatthew G Knepley       0, 1, 5, 4, 8,  17, 12, 16, 22, /* front */
2774e6ccafaeSMatthew G Knepley       1, 2, 6, 5, 9,  18, 13, 17, 21, /* right */
2775e6ccafaeSMatthew G Knepley       2, 3, 7, 6, 10, 19, 14, 18, 23, /* back */
2776e6ccafaeSMatthew G Knepley       3, 0, 4, 7, 11, 16, 15, 19, 20  /* left */
2777e6ccafaeSMatthew G Knepley     };
2778e6ccafaeSMatthew G Knepley 
2779e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuadHex; ++i) sortedIndices[i] = indices[i];
27809566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeQuadHex, sortedIndices));
2781e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2782e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace * faceSizeQuadHex;
2783e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2784e6ccafaeSMatthew G Knepley 
27859371c9d4SSatish Balay       if ((sortedIndices[0] == faceVerticesQuadHexSorted[ii + 0]) && (sortedIndices[1] == faceVerticesQuadHexSorted[ii + 1]) && (sortedIndices[2] == faceVerticesQuadHexSorted[ii + 2]) && (sortedIndices[3] == faceVerticesQuadHexSorted[ii + 3])) {
2786e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuadHex; ++fVertex) {
2787e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuadHex; ++cVertex) {
2788e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuadHex[ii + fVertex]) {
2789e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2790e6ccafaeSMatthew G Knepley               break;
2791e6ccafaeSMatthew G Knepley             }
2792e6ccafaeSMatthew G Knepley           }
2793e6ccafaeSMatthew G Knepley         }
2794e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2795e6ccafaeSMatthew G Knepley         break;
2796e6ccafaeSMatthew G Knepley       }
2797e6ccafaeSMatthew G Knepley     }
279828b400f6SJacob Faibussowitsch     PetscCheck(found, comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2799e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2800e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2801e6ccafaeSMatthew G Knepley   } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Unknown cell type for faceOrientation().");
2802e6ccafaeSMatthew G Knepley   if (!posOrient) {
28039566063dSJacob Faibussowitsch     if (debug) PetscCall(PetscPrintf(comm, "  Reversing initial face orientation\n"));
2804e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[faceSize - 1 - f];
2805e6ccafaeSMatthew G Knepley   } else {
28069566063dSJacob Faibussowitsch     if (debug) PetscCall(PetscPrintf(comm, "  Keeping initial face orientation\n"));
2807e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[f];
2808e6ccafaeSMatthew G Knepley   }
2809e6ccafaeSMatthew G Knepley   if (posOriented) *posOriented = posOrient;
2810e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2811e6ccafaeSMatthew G Knepley }
2812e6ccafaeSMatthew G Knepley 
2813c08575a3SMatthew G. Knepley /*@
2814c08575a3SMatthew G. Knepley   DMPlexGetOrientedFace - Given a cell and a face, as a set of vertices, return the oriented face, as a set of vertices,
2815c08575a3SMatthew G. Knepley   in faceVertices. The orientation is such that the face normal points out of the cell
2816c08575a3SMatthew G. Knepley 
2817c08575a3SMatthew G. Knepley   Not collective
2818c08575a3SMatthew G. Knepley 
2819c08575a3SMatthew G. Knepley   Input Parameters:
2820c08575a3SMatthew G. Knepley + dm           - The original mesh
2821c08575a3SMatthew G. Knepley . cell         - The cell mesh point
2822c08575a3SMatthew G. Knepley . faceSize     - The number of vertices on the face
2823c08575a3SMatthew G. Knepley . face         - The face vertices
2824c08575a3SMatthew G. Knepley . numCorners   - The number of vertices on the cell
2825c08575a3SMatthew G. Knepley . indices      - Local numbering of face vertices in cell cone
2826c08575a3SMatthew G. Knepley - origVertices - Original face vertices
2827c08575a3SMatthew G. Knepley 
2828d8d19677SJose E. Roman   Output Parameters:
2829c08575a3SMatthew G. Knepley + faceVertices - The face vertices properly oriented
2830c08575a3SMatthew G. Knepley - posOriented  - PETSC_TRUE if the face was oriented with outward normal
2831c08575a3SMatthew G. Knepley 
2832c08575a3SMatthew G. Knepley   Level: developer
2833c08575a3SMatthew G. Knepley 
2834db781477SPatrick Sanan .seealso: `DMPlexGetCone()`
2835c08575a3SMatthew G. Knepley @*/
28369371c9d4SSatish Balay PetscErrorCode DMPlexGetOrientedFace(DM dm, PetscInt cell, PetscInt faceSize, const PetscInt face[], PetscInt numCorners, PetscInt indices[], PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented) {
28370298fd71SBarry Smith   const PetscInt *cone = NULL;
2838e6ccafaeSMatthew G Knepley   PetscInt        coneSize, v, f, v2;
2839e6ccafaeSMatthew G Knepley   PetscInt        oppositeVertex = -1;
2840e6ccafaeSMatthew G Knepley 
2841e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
28429566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
28439566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCone(dm, cell, &cone));
2844e6ccafaeSMatthew G Knepley   for (v = 0, v2 = 0; v < coneSize; ++v) {
2845e6ccafaeSMatthew G Knepley     PetscBool found = PETSC_FALSE;
2846e6ccafaeSMatthew G Knepley 
2847e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) {
2848e6ccafaeSMatthew G Knepley       if (face[f] == cone[v]) {
28499371c9d4SSatish Balay         found = PETSC_TRUE;
28509371c9d4SSatish Balay         break;
2851e6ccafaeSMatthew G Knepley       }
2852e6ccafaeSMatthew G Knepley     }
2853e6ccafaeSMatthew G Knepley     if (found) {
2854e6ccafaeSMatthew G Knepley       indices[v2]      = v;
2855e6ccafaeSMatthew G Knepley       origVertices[v2] = cone[v];
2856e6ccafaeSMatthew G Knepley       ++v2;
2857e6ccafaeSMatthew G Knepley     } else {
2858e6ccafaeSMatthew G Knepley       oppositeVertex = v;
2859e6ccafaeSMatthew G Knepley     }
2860e6ccafaeSMatthew G Knepley   }
28619566063dSJacob Faibussowitsch   PetscCall(DMPlexGetFaceOrientation(dm, cell, numCorners, indices, oppositeVertex, origVertices, faceVertices, posOriented));
2862e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2863e6ccafaeSMatthew G Knepley }
2864e6ccafaeSMatthew G Knepley 
2865e6ccafaeSMatthew G Knepley /*
2866cd0c2139SMatthew G Knepley   DMPlexInsertFace_Internal - Puts a face into the mesh
2867e6ccafaeSMatthew G Knepley 
2868e6ccafaeSMatthew G Knepley   Not collective
2869e6ccafaeSMatthew G Knepley 
2870e6ccafaeSMatthew G Knepley   Input Parameters:
2871e6ccafaeSMatthew G Knepley   + dm              - The DMPlex
2872e6ccafaeSMatthew G Knepley   . numFaceVertex   - The number of vertices in the face
2873e6ccafaeSMatthew G Knepley   . faceVertices    - The vertices in the face for dm
2874e6ccafaeSMatthew G Knepley   . subfaceVertices - The vertices in the face for subdm
2875e6ccafaeSMatthew G Knepley   . numCorners      - The number of vertices in the cell
2876e6ccafaeSMatthew G Knepley   . cell            - A cell in dm containing the face
2877e6ccafaeSMatthew G Knepley   . subcell         - A cell in subdm containing the face
2878e6ccafaeSMatthew G Knepley   . firstFace       - First face in the mesh
2879e6ccafaeSMatthew G Knepley   - newFacePoint    - Next face in the mesh
2880e6ccafaeSMatthew G Knepley 
2881e6ccafaeSMatthew G Knepley   Output Parameters:
2882e6ccafaeSMatthew G Knepley   . newFacePoint - Contains next face point number on input, updated on output
2883e6ccafaeSMatthew G Knepley 
2884e6ccafaeSMatthew G Knepley   Level: developer
2885e6ccafaeSMatthew G Knepley */
28869371c9d4SSatish Balay 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) {
288782f516ccSBarry Smith   MPI_Comm        comm;
2888e6ccafaeSMatthew G Knepley   DM_Plex        *submesh = (DM_Plex *)subdm->data;
2889e6ccafaeSMatthew G Knepley   const PetscInt *faces;
2890e6ccafaeSMatthew G Knepley   PetscInt        numFaces, coneSize;
2891e6ccafaeSMatthew G Knepley 
2892e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
28939566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
28949566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(subdm, subcell, &coneSize));
289563a3b9bcSJacob Faibussowitsch   PetscCheck(coneSize == 1, comm, PETSC_ERR_ARG_OUTOFRANGE, "Cone size of cell %" PetscInt_FMT " is %" PetscInt_FMT " != 1", cell, coneSize);
2896e6ccafaeSMatthew G Knepley #if 0
2897e6ccafaeSMatthew G Knepley   /* Cannot use this because support() has not been constructed yet */
28989566063dSJacob Faibussowitsch   PetscCall(DMPlexGetJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces));
2899e6ccafaeSMatthew G Knepley #else
2900e6ccafaeSMatthew G Knepley   {
2901e6ccafaeSMatthew G Knepley     PetscInt f;
2902e6ccafaeSMatthew G Knepley 
2903e6ccafaeSMatthew G Knepley     numFaces = 0;
29049566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(subdm, 1, MPIU_INT, (void **)&faces));
2905e6ccafaeSMatthew G Knepley     for (f = firstFace; f < *newFacePoint; ++f) {
2906e6ccafaeSMatthew G Knepley       PetscInt dof, off, d;
2907e6ccafaeSMatthew G Knepley 
29089566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(submesh->coneSection, f, &dof));
29099566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(submesh->coneSection, f, &off));
2910e6ccafaeSMatthew G Knepley       /* Yes, I know this is quadratic, but I expect the sizes to be <5 */
2911e6ccafaeSMatthew G Knepley       for (d = 0; d < dof; ++d) {
2912e6ccafaeSMatthew G Knepley         const PetscInt p = submesh->cones[off + d];
2913e6ccafaeSMatthew G Knepley         PetscInt       v;
2914e6ccafaeSMatthew G Knepley 
2915e6ccafaeSMatthew G Knepley         for (v = 0; v < numFaceVertices; ++v) {
2916e6ccafaeSMatthew G Knepley           if (subfaceVertices[v] == p) break;
2917e6ccafaeSMatthew G Knepley         }
2918e6ccafaeSMatthew G Knepley         if (v == numFaceVertices) break;
2919e6ccafaeSMatthew G Knepley       }
2920e6ccafaeSMatthew G Knepley       if (d == dof) {
2921e6ccafaeSMatthew G Knepley         numFaces               = 1;
2922e6ccafaeSMatthew G Knepley         ((PetscInt *)faces)[0] = f;
2923e6ccafaeSMatthew G Knepley       }
2924e6ccafaeSMatthew G Knepley     }
2925e6ccafaeSMatthew G Knepley   }
2926e6ccafaeSMatthew G Knepley #endif
292763a3b9bcSJacob Faibussowitsch   PetscCheck(numFaces <= 1, comm, PETSC_ERR_ARG_WRONG, "Vertex set had %" PetscInt_FMT " faces, not one", numFaces);
2928f7d195e4SLawrence Mitchell   if (numFaces == 1) {
2929e6ccafaeSMatthew G Knepley     /* Add the other cell neighbor for this face */
29309566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, faces));
2931e6ccafaeSMatthew G Knepley   } else {
2932e6ccafaeSMatthew G Knepley     PetscInt *indices, *origVertices, *orientedVertices, *orientedSubVertices, v, ov;
2933e6ccafaeSMatthew G Knepley     PetscBool posOriented;
2934e6ccafaeSMatthew G Knepley 
29359566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(subdm, 4 * numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices));
2936e6ccafaeSMatthew G Knepley     origVertices        = &orientedVertices[numFaceVertices];
2937e6ccafaeSMatthew G Knepley     indices             = &orientedVertices[numFaceVertices * 2];
2938e6ccafaeSMatthew G Knepley     orientedSubVertices = &orientedVertices[numFaceVertices * 3];
29399566063dSJacob Faibussowitsch     PetscCall(DMPlexGetOrientedFace(dm, cell, numFaceVertices, faceVertices, numCorners, indices, origVertices, orientedVertices, &posOriented));
2940e6ccafaeSMatthew G Knepley     /* TODO: I know that routine should return a permutation, not the indices */
2941e6ccafaeSMatthew G Knepley     for (v = 0; v < numFaceVertices; ++v) {
2942e6ccafaeSMatthew G Knepley       const PetscInt vertex = faceVertices[v], subvertex = subfaceVertices[v];
2943e6ccafaeSMatthew G Knepley       for (ov = 0; ov < numFaceVertices; ++ov) {
2944e6ccafaeSMatthew G Knepley         if (orientedVertices[ov] == vertex) {
2945e6ccafaeSMatthew G Knepley           orientedSubVertices[ov] = subvertex;
2946e6ccafaeSMatthew G Knepley           break;
2947e6ccafaeSMatthew G Knepley         }
2948e6ccafaeSMatthew G Knepley       }
294963a3b9bcSJacob Faibussowitsch       PetscCheck(ov != numFaceVertices, comm, PETSC_ERR_PLIB, "Could not find face vertex %" PetscInt_FMT " in orientated set", vertex);
2950e6ccafaeSMatthew G Knepley     }
29519566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, *newFacePoint, orientedSubVertices));
29529566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, newFacePoint));
29539566063dSJacob Faibussowitsch     PetscCall(DMRestoreWorkArray(subdm, 4 * numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices));
2954e6ccafaeSMatthew G Knepley     ++(*newFacePoint);
2955e6ccafaeSMatthew G Knepley   }
2956ef07cca7SMatthew G. Knepley #if 0
29579566063dSJacob Faibussowitsch   PetscCall(DMPlexRestoreJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces));
2958ef07cca7SMatthew G. Knepley #else
29599566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, 1, MPIU_INT, (void **)&faces));
2960ef07cca7SMatthew G. Knepley #endif
2961e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2962e6ccafaeSMatthew G Knepley }
2963e6ccafaeSMatthew G Knepley 
29649371c9d4SSatish Balay static PetscErrorCode DMPlexCreateSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm) {
296582f516ccSBarry Smith   MPI_Comm        comm;
296653156dfcSMatthew G. Knepley   DMLabel         subpointMap;
2967efa14ee0SMatthew G Knepley   IS              subvertexIS, subcellIS;
2968efa14ee0SMatthew G Knepley   const PetscInt *subVertices, *subCells;
2969efa14ee0SMatthew G Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells;
2970fed694aaSMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces = 0, firstSubFace, newFacePoint, nFV = 0;
2971efa14ee0SMatthew G Knepley   PetscInt        vStart, vEnd, c, f;
2972e6ccafaeSMatthew G Knepley 
2973e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
29749566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
2975efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
29769566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
29779566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
29789566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&subpointMap));
29799566063dSJacob Faibussowitsch   if (vertexLabel) PetscCall(DMPlexMarkSubmesh_Uninterpolated(dm, vertexLabel, value, subpointMap, &numSubFaces, &nFV, subdm));
2980efa14ee0SMatthew G Knepley   /* Setup chart */
29819566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 0, &numSubVertices));
29829566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 2, &numSubCells));
29839566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, numSubCells + numSubFaces + numSubVertices));
29849566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, 1));
2985e6ccafaeSMatthew G Knepley   /* Set cone sizes */
2986e6ccafaeSMatthew G Knepley   firstSubVertex = numSubCells;
2987efa14ee0SMatthew G Knepley   firstSubFace   = numSubCells + numSubVertices;
2988e6ccafaeSMatthew G Knepley   newFacePoint   = firstSubFace;
29899566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 0, &subvertexIS));
29909566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISGetIndices(subvertexIS, &subVertices));
29919566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 2, &subcellIS));
29929566063dSJacob Faibussowitsch   if (subcellIS) PetscCall(ISGetIndices(subcellIS, &subCells));
299348a46eb9SPierre Jolivet   for (c = 0; c < numSubCells; ++c) PetscCall(DMPlexSetConeSize(subdm, c, 1));
299448a46eb9SPierre Jolivet   for (f = firstSubFace; f < firstSubFace + numSubFaces; ++f) PetscCall(DMPlexSetConeSize(subdm, f, nFV));
29959566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
2996e6ccafaeSMatthew G Knepley   /* Create face cones */
29979566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
29989566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
29999566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
3000e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
3001e6ccafaeSMatthew G Knepley     const PetscInt cell    = subCells[c];
3002efa14ee0SMatthew G Knepley     const PetscInt subcell = c;
30030298fd71SBarry Smith     PetscInt      *closure = NULL;
3004efa14ee0SMatthew G Knepley     PetscInt       closureSize, cl, numCorners = 0, faceSize = 0;
3005e6ccafaeSMatthew G Knepley 
30069566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
3007efa14ee0SMatthew G Knepley     for (cl = 0; cl < closureSize * 2; cl += 2) {
3008efa14ee0SMatthew G Knepley       const PetscInt point = closure[cl];
3009e6ccafaeSMatthew G Knepley       PetscInt       subVertex;
3010e6ccafaeSMatthew G Knepley 
3011efa14ee0SMatthew G Knepley       if ((point >= vStart) && (point < vEnd)) {
3012efa14ee0SMatthew G Knepley         ++numCorners;
30139566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(point, numSubVertices, subVertices, &subVertex));
3014efa14ee0SMatthew G Knepley         if (subVertex >= 0) {
3015efa14ee0SMatthew G Knepley           closure[faceSize] = point;
301665560c7fSMatthew G Knepley           subface[faceSize] = firstSubVertex + subVertex;
3017e6ccafaeSMatthew G Knepley           ++faceSize;
3018e6ccafaeSMatthew G Knepley         }
3019e6ccafaeSMatthew G Knepley       }
3020e6ccafaeSMatthew G Knepley     }
302163a3b9bcSJacob Faibussowitsch     PetscCheck(faceSize <= nFV, comm, PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %" PetscInt_FMT " of an element on the surface", faceSize);
302248a46eb9SPierre Jolivet     if (faceSize == nFV) PetscCall(DMPlexInsertFace_Internal(dm, subdm, faceSize, closure, subface, numCorners, cell, subcell, firstSubFace, &newFacePoint));
30239566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
3024e6ccafaeSMatthew G Knepley   }
30259566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
30269566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
30279566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3028e6ccafaeSMatthew G Knepley   /* Build coordinates */
3029efa14ee0SMatthew G Knepley   {
3030efa14ee0SMatthew G Knepley     PetscSection coordSection, subCoordSection;
3031efa14ee0SMatthew G Knepley     Vec          coordinates, subCoordinates;
3032efa14ee0SMatthew G Knepley     PetscScalar *coords, *subCoords;
3033285d324eSMatthew G. Knepley     PetscInt     numComp, coordSize, v;
303424640c55SToby Isaac     const char  *name;
3035efa14ee0SMatthew G Knepley 
30369566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
30379566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
30389566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
30399566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
30409566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
30419566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
30429566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex + numSubVertices));
3043efa14ee0SMatthew G Knepley     for (v = 0; v < numSubVertices; ++v) {
3044efa14ee0SMatthew G Knepley       const PetscInt vertex    = subVertices[v];
3045efa14ee0SMatthew G Knepley       const PetscInt subvertex = firstSubVertex + v;
3046efa14ee0SMatthew G Knepley       PetscInt       dof;
3047efa14ee0SMatthew G Knepley 
30489566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
30499566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
30509566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3051e6ccafaeSMatthew G Knepley     }
30529566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
30539566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
30549566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
30559566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
30569566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name));
30579566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
30589566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates, VECSTANDARD));
3059830e53efSMatthew G. Knepley     if (coordSize) {
30609566063dSJacob Faibussowitsch       PetscCall(VecGetArray(coordinates, &coords));
30619566063dSJacob Faibussowitsch       PetscCall(VecGetArray(subCoordinates, &subCoords));
3062efa14ee0SMatthew G Knepley       for (v = 0; v < numSubVertices; ++v) {
3063efa14ee0SMatthew G Knepley         const PetscInt vertex    = subVertices[v];
3064efa14ee0SMatthew G Knepley         const PetscInt subvertex = firstSubVertex + v;
3065efa14ee0SMatthew G Knepley         PetscInt       dof, off, sdof, soff, d;
3066e6ccafaeSMatthew G Knepley 
30679566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
30689566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
30699566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
30709566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
307163a3b9bcSJacob Faibussowitsch         PetscCheck(dof == sdof, comm, PETSC_ERR_PLIB, "Coordinate dimension %" PetscInt_FMT " on subvertex %" PetscInt_FMT ", vertex %" PetscInt_FMT " should be %" PetscInt_FMT, sdof, subvertex, vertex, dof);
3072e6ccafaeSMatthew G Knepley         for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d];
3073e6ccafaeSMatthew G Knepley       }
30749566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(coordinates, &coords));
30759566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(subCoordinates, &subCoords));
30763b399e24SMatthew G. Knepley     }
30779566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
30789566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3079e6ccafaeSMatthew G Knepley   }
3080efa14ee0SMatthew G Knepley   /* Cleanup */
30819566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subVertices));
30829566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
30839566063dSJacob Faibussowitsch   if (subcellIS) PetscCall(ISRestoreIndices(subcellIS, &subCells));
30849566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subcellIS));
3085e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3086e6ccafaeSMatthew G Knepley }
3087e6ccafaeSMatthew G Knepley 
3088d0609cedSBarry Smith /* TODO: Fix this to properly propogate up error conditions it may find */
30899371c9d4SSatish Balay static inline PetscInt DMPlexFilterPoint_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[]) {
30903982b651SMatthew G. Knepley   PetscInt       subPoint;
30913982b651SMatthew G. Knepley   PetscErrorCode ierr;
30923982b651SMatthew G. Knepley 
30939371c9d4SSatish Balay   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint);
30949371c9d4SSatish Balay   if (ierr) return -1;
30953982b651SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint + subPoint;
30963982b651SMatthew G. Knepley }
30973982b651SMatthew G. Knepley 
3098*dd05d810SMatthew G. Knepley /* TODO: Fix this to properly propogate up error conditions it may find */
3099*dd05d810SMatthew G. Knepley static inline PetscInt DMPlexFilterPointPerm_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[], const PetscInt subIndices[]) {
3100*dd05d810SMatthew G. Knepley   PetscInt       subPoint;
3101*dd05d810SMatthew G. Knepley   PetscErrorCode ierr;
3102*dd05d810SMatthew G. Knepley 
3103*dd05d810SMatthew G. Knepley   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint);
3104*dd05d810SMatthew G. Knepley   if (ierr) return -1;
3105*dd05d810SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint + (subIndices ? subIndices[subPoint] : subPoint);
3106*dd05d810SMatthew G. Knepley }
3107*dd05d810SMatthew G. Knepley 
31089371c9d4SSatish Balay static PetscErrorCode DMPlexFilterLabels_Internal(DM dm, const PetscInt numSubPoints[], const PetscInt *subpoints[], const PetscInt firstSubPoint[], DM subdm) {
3109695799ffSMatthew G. Knepley   DMLabel  depthLabel;
3110212103e5SMatthew Knepley   PetscInt Nl, l, d;
3111212103e5SMatthew Knepley 
3112212103e5SMatthew Knepley   PetscFunctionBegin;
3113695799ffSMatthew G. Knepley   // Reset depth label for fast lookup
3114695799ffSMatthew G. Knepley   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
3115695799ffSMatthew G. Knepley   PetscCall(DMLabelMakeAllInvalid_Internal(depthLabel));
31169566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
3117212103e5SMatthew Knepley   for (l = 0; l < Nl; ++l) {
3118212103e5SMatthew Knepley     DMLabel         label, newlabel;
3119212103e5SMatthew Knepley     const char     *lname;
3120d56405f8SMatthew G. Knepley     PetscBool       isDepth, isDim, isCelltype, isVTK;
3121212103e5SMatthew Knepley     IS              valueIS;
3122212103e5SMatthew Knepley     const PetscInt *values;
3123212103e5SMatthew Knepley     PetscInt        Nv, v;
3124212103e5SMatthew Knepley 
31259566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &lname));
31269566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
31279566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "dim", &isDim));
31289566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "celltype", &isCelltype));
31299566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "vtk", &isVTK));
3130d56405f8SMatthew G. Knepley     if (isDepth || isDim || isCelltype || isVTK) continue;
31319566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(subdm, lname));
31329566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, lname, &label));
31339566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(subdm, lname, &newlabel));
31349566063dSJacob Faibussowitsch     PetscCall(DMLabelGetDefaultValue(label, &v));
31359566063dSJacob Faibussowitsch     PetscCall(DMLabelSetDefaultValue(newlabel, v));
31369566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
31379566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &Nv));
31389566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
3139212103e5SMatthew Knepley     for (v = 0; v < Nv; ++v) {
3140212103e5SMatthew Knepley       IS              pointIS;
3141212103e5SMatthew Knepley       const PetscInt *points;
3142212103e5SMatthew Knepley       PetscInt        Np, p;
3143212103e5SMatthew Knepley 
31449566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
31459566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &Np));
31469566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
3147212103e5SMatthew Knepley       for (p = 0; p < Np; ++p) {
3148212103e5SMatthew Knepley         const PetscInt point = points[p];
3149212103e5SMatthew Knepley         PetscInt       subp;
3150212103e5SMatthew Knepley 
31519566063dSJacob Faibussowitsch         PetscCall(DMPlexGetPointDepth(dm, point, &d));
3152212103e5SMatthew Knepley         subp = DMPlexFilterPoint_Internal(point, firstSubPoint[d], numSubPoints[d], subpoints[d]);
31539566063dSJacob Faibussowitsch         if (subp >= 0) PetscCall(DMLabelSetValue(newlabel, subp, values[v]));
3154212103e5SMatthew Knepley       }
31559566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(pointIS, &points));
31569566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
3157212103e5SMatthew Knepley     }
31589566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
31599566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
3160212103e5SMatthew Knepley   }
3161212103e5SMatthew Knepley   PetscFunctionReturn(0);
3162212103e5SMatthew Knepley }
3163212103e5SMatthew Knepley 
31649371c9d4SSatish Balay static PetscErrorCode DMPlexCreateSubmeshGeneric_Interpolated(DM dm, DMLabel label, PetscInt value, PetscBool markedFaces, PetscBool isCohesive, PetscInt cellHeight, DM subdm) {
316582f516ccSBarry Smith   MPI_Comm         comm;
316653156dfcSMatthew G. Knepley   DMLabel          subpointMap;
3167efa14ee0SMatthew G Knepley   IS              *subpointIS;
3168efa14ee0SMatthew G Knepley   const PetscInt **subpoints;
31693982b651SMatthew G. Knepley   PetscInt        *numSubPoints, *firstSubPoint, *coneNew, *orntNew;
3170*dd05d810SMatthew G. Knepley   PetscInt         totSubPoints = 0, maxConeSize, dim, sdim, cdim, p, d, v;
31710d366550SMatthew G. Knepley   PetscMPIInt      rank;
3172e6ccafaeSMatthew G Knepley 
3173e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
31749566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
31759566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
3176efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
31779566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
31789566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
3179bec263e5SMatthew G. Knepley   if (cellHeight) {
31809566063dSJacob Faibussowitsch     if (isCohesive) PetscCall(DMPlexMarkCohesiveSubmesh_Interpolated(dm, label, value, subpointMap, subdm));
31819566063dSJacob Faibussowitsch     else PetscCall(DMPlexMarkSubmesh_Interpolated(dm, label, value, markedFaces, subpointMap, subdm));
3182bec263e5SMatthew G. Knepley   } else {
3183bec263e5SMatthew G. Knepley     DMLabel         depth;
3184bec263e5SMatthew G. Knepley     IS              pointIS;
3185bec263e5SMatthew G. Knepley     const PetscInt *points;
3186b85c8bf9SMatthew G. Knepley     PetscInt        numPoints = 0;
3187bec263e5SMatthew G. Knepley 
31889566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthLabel(dm, &depth));
31899566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, value, &pointIS));
3190b85c8bf9SMatthew G. Knepley     if (pointIS) {
31919566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
31929566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &numPoints));
3193b85c8bf9SMatthew G. Knepley     }
3194bec263e5SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
3195bec263e5SMatthew G. Knepley       PetscInt *closure = NULL;
3196bec263e5SMatthew G. Knepley       PetscInt  closureSize, c, pdim;
3197bec263e5SMatthew G. Knepley 
31989566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure));
3199bec263e5SMatthew G. Knepley       for (c = 0; c < closureSize * 2; c += 2) {
32009566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(depth, closure[c], &pdim));
32019566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(subpointMap, closure[c], pdim));
3202bec263e5SMatthew G. Knepley       }
32039566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure));
3204bec263e5SMatthew G. Knepley     }
32059566063dSJacob Faibussowitsch     if (pointIS) PetscCall(ISRestoreIndices(pointIS, &points));
32069566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
3207bec263e5SMatthew G. Knepley   }
3208efa14ee0SMatthew G Knepley   /* Setup chart */
32099566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
3210*dd05d810SMatthew G. Knepley   PetscCall(DMGetCoordinateDim(dm, &cdim));
32119566063dSJacob Faibussowitsch   PetscCall(PetscMalloc4(dim + 1, &numSubPoints, dim + 1, &firstSubPoint, dim + 1, &subpointIS, dim + 1, &subpoints));
3212e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
32139566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d]));
3214e6ccafaeSMatthew G Knepley     totSubPoints += numSubPoints[d];
3215e6ccafaeSMatthew G Knepley   }
3216*dd05d810SMatthew G. Knepley   // Determine submesh dimension
3217*dd05d810SMatthew G. Knepley   PetscCall(DMGetDimension(subdm, &sdim));
3218*dd05d810SMatthew G. Knepley   if (sdim > 0) {
3219*dd05d810SMatthew G. Knepley     // Calling function knows what dimension to use, and we include neighboring cells as well
3220*dd05d810SMatthew G. Knepley     sdim = dim;
3221*dd05d810SMatthew G. Knepley   } else {
3222*dd05d810SMatthew G. Knepley     // We reset the subdimension based on what is being selected
3223*dd05d810SMatthew G. Knepley     PetscInt lsdim;
3224*dd05d810SMatthew G. Knepley     for (lsdim = dim; lsdim >= 0; --lsdim)
3225*dd05d810SMatthew G. Knepley       if (numSubPoints[lsdim]) break;
3226*dd05d810SMatthew G. Knepley     PetscCall(MPI_Allreduce(&lsdim, &sdim, 1, MPIU_INT, MPIU_MAX, comm));
3227*dd05d810SMatthew G. Knepley     PetscCall(DMSetDimension(subdm, sdim));
3228*dd05d810SMatthew G. Knepley     PetscCall(DMSetCoordinateDim(subdm, cdim));
3229*dd05d810SMatthew G. Knepley   }
32309566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, totSubPoints));
32319566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, cellHeight));
3232e6ccafaeSMatthew G Knepley   /* Set cone sizes */
3233*dd05d810SMatthew G. Knepley   firstSubPoint[sdim] = 0;
3234*dd05d810SMatthew G. Knepley   firstSubPoint[0]    = firstSubPoint[sdim] + numSubPoints[sdim];
3235*dd05d810SMatthew G. Knepley   if (sdim > 1) firstSubPoint[sdim - 1] = firstSubPoint[0] + numSubPoints[0];
3236*dd05d810SMatthew G. Knepley   if (sdim > 2) firstSubPoint[sdim - 2] = firstSubPoint[sdim - 1] + numSubPoints[sdim - 1];
3237*dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
32389566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(subpointMap, d, &subpointIS[d]));
32399566063dSJacob Faibussowitsch     if (subpointIS[d]) PetscCall(ISGetIndices(subpointIS[d], &subpoints[d]));
3240e6ccafaeSMatthew G Knepley   }
3241412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
32429566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(subdm, "celltype"));
3243*dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
3244e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3245e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3246e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
3247e6ccafaeSMatthew G Knepley       const PetscInt *cone;
324815100a53SVaclav Hapla       PetscInt        coneSize;
3249e6ccafaeSMatthew G Knepley 
32509566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, point, &coneSize));
3251*dd05d810SMatthew G. Knepley       if (cellHeight && (d == sdim)) {
325215100a53SVaclav Hapla         PetscInt coneSizeNew, c, val;
325315100a53SVaclav Hapla 
32549566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCone(dm, point, &cone));
3255e6ccafaeSMatthew G Knepley         for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
32569566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(subpointMap, cone[c], &val));
3257e6ccafaeSMatthew G Knepley           if (val >= 0) coneSizeNew++;
3258e6ccafaeSMatthew G Knepley         }
32599566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(subdm, subpoint, coneSizeNew));
32609566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(subdm, subpoint, DM_POLYTOPE_FV_GHOST));
326115100a53SVaclav Hapla       } else {
326215100a53SVaclav Hapla         DMPolytopeType ct;
326315100a53SVaclav Hapla 
326415100a53SVaclav Hapla         PetscCall(DMPlexSetConeSize(subdm, subpoint, coneSize));
326515100a53SVaclav Hapla         PetscCall(DMPlexGetCellType(dm, point, &ct));
326615100a53SVaclav Hapla         PetscCall(DMPlexSetCellType(subdm, subpoint, ct));
3267e6ccafaeSMatthew G Knepley       }
3268e6ccafaeSMatthew G Knepley     }
3269e6ccafaeSMatthew G Knepley   }
32709566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&subpointMap));
32719566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3272e6ccafaeSMatthew G Knepley   /* Set cones */
32739566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
32749566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(maxConeSize, &coneNew, maxConeSize, &orntNew));
3275*dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
3276e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3277e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3278e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
32790e49e2e2SMatthew G. Knepley       const PetscInt *cone, *ornt;
32800d366550SMatthew G. Knepley       PetscInt        coneSize, subconeSize, coneSizeNew, c, subc, fornt = 0;
3281e6ccafaeSMatthew G Knepley 
3282*dd05d810SMatthew G. Knepley       if (d == sdim - 1) {
32830d366550SMatthew G. Knepley         const PetscInt *support, *cone, *ornt;
32840d366550SMatthew G. Knepley         PetscInt        supportSize, coneSize, s, subc;
32850d366550SMatthew G. Knepley 
32869566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, point, &support));
32879566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
32880d366550SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
3289412e9a14SMatthew G. Knepley           PetscBool isHybrid;
3290412e9a14SMatthew G. Knepley 
32919566063dSJacob Faibussowitsch           PetscCall(DMPlexCellIsHybrid_Internal(dm, support[s], &isHybrid));
3292412e9a14SMatthew G. Knepley           if (!isHybrid) continue;
32939566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSubPoints[d + 1], subpoints[d + 1], &subc));
32940d366550SMatthew G. Knepley           if (subc >= 0) {
32950d366550SMatthew G. Knepley             const PetscInt ccell = subpoints[d + 1][subc];
32960d366550SMatthew G. Knepley 
32979566063dSJacob Faibussowitsch             PetscCall(DMPlexGetCone(dm, ccell, &cone));
32989566063dSJacob Faibussowitsch             PetscCall(DMPlexGetConeSize(dm, ccell, &coneSize));
32999566063dSJacob Faibussowitsch             PetscCall(DMPlexGetConeOrientation(dm, ccell, &ornt));
33000d366550SMatthew G. Knepley             for (c = 0; c < coneSize; ++c) {
33010d366550SMatthew G. Knepley               if (cone[c] == point) {
33020d366550SMatthew G. Knepley                 fornt = ornt[c];
33030d366550SMatthew G. Knepley                 break;
33040d366550SMatthew G. Knepley               }
33050d366550SMatthew G. Knepley             }
33060d366550SMatthew G. Knepley             break;
33070d366550SMatthew G. Knepley           }
33080d366550SMatthew G. Knepley         }
33090d366550SMatthew G. Knepley       }
33109566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, point, &coneSize));
33119566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(subdm, subpoint, &subconeSize));
33129566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, point, &cone));
33139566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeOrientation(dm, point, &ornt));
3314e6ccafaeSMatthew G Knepley       for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
33159566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[c], numSubPoints[d - 1], subpoints[d - 1], &subc));
331601a2673eSMatthew G. Knepley         if (subc >= 0) {
331701a2673eSMatthew G. Knepley           coneNew[coneSizeNew] = firstSubPoint[d - 1] + subc;
33183982b651SMatthew G. Knepley           orntNew[coneSizeNew] = ornt[c];
331901a2673eSMatthew G. Knepley           ++coneSizeNew;
332001a2673eSMatthew G. Knepley         }
3321e6ccafaeSMatthew G Knepley       }
332263a3b9bcSJacob Faibussowitsch       PetscCheck(coneSizeNew == subconeSize, comm, PETSC_ERR_PLIB, "Number of cone points located %" PetscInt_FMT " does not match subcone size %" PetscInt_FMT, coneSizeNew, subconeSize);
33239566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(subdm, subpoint, coneNew));
33249566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeOrientation(subdm, subpoint, orntNew));
33259566063dSJacob Faibussowitsch       if (fornt < 0) PetscCall(DMPlexOrientPoint(subdm, subpoint, fornt));
3326e6ccafaeSMatthew G Knepley     }
3327e6ccafaeSMatthew G Knepley   }
33289566063dSJacob Faibussowitsch   PetscCall(PetscFree2(coneNew, orntNew));
33299566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
33309566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3331e6ccafaeSMatthew G Knepley   /* Build coordinates */
3332e6ccafaeSMatthew G Knepley   {
3333e6ccafaeSMatthew G Knepley     PetscSection coordSection, subCoordSection;
3334e6ccafaeSMatthew G Knepley     Vec          coordinates, subCoordinates;
3335e6ccafaeSMatthew G Knepley     PetscScalar *coords, *subCoords;
3336c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize;
333724640c55SToby Isaac     const char  *name;
3338e6ccafaeSMatthew G Knepley 
33399566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDim(dm, &cdim));
33409566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
33419566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
33429566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
33439566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
33449566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
33459566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
33469566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubPoint[0], firstSubPoint[0] + numSubPoints[0]));
3347e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3348e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3349e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0] + v;
3350e6ccafaeSMatthew G Knepley       PetscInt       dof;
3351e6ccafaeSMatthew G Knepley 
33529566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
33539566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
33549566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3355e6ccafaeSMatthew G Knepley     }
33569566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
33579566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
33589566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
33599566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
33609566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name));
33619566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
33629566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(subCoordinates, cdim));
33639566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates, VECSTANDARD));
33649566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
33659566063dSJacob Faibussowitsch     PetscCall(VecGetArray(subCoordinates, &subCoords));
3366e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3367e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3368e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0] + v;
3369e6ccafaeSMatthew G Knepley       PetscInt       dof, off, sdof, soff, d;
3370e6ccafaeSMatthew G Knepley 
33719566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
33729566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
33739566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
33749566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
337563a3b9bcSJacob Faibussowitsch       PetscCheck(dof == sdof, comm, PETSC_ERR_PLIB, "Coordinate dimension %" PetscInt_FMT " on subvertex %" PetscInt_FMT ", vertex %" PetscInt_FMT " should be %" PetscInt_FMT, sdof, subvertex, vertex, dof);
3376efa14ee0SMatthew G Knepley       for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d];
3377e6ccafaeSMatthew G Knepley     }
33789566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
33799566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(subCoordinates, &subCoords));
33809566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
33819566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3382e6ccafaeSMatthew G Knepley   }
33833982b651SMatthew G. Knepley   /* Build SF: We need this complexity because subpoints might not be selected on the owning process */
33843982b651SMatthew G. Knepley   {
33853982b651SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
33863982b651SMatthew G. Knepley     IS                 subpIS;
33873982b651SMatthew G. Knepley     const PetscSFNode *remotePoints;
33885033f954SMatthew G. Knepley     PetscSFNode       *sremotePoints = NULL, *newLocalPoints = NULL, *newOwners = NULL;
33895033f954SMatthew G. Knepley     const PetscInt    *localPoints, *subpoints, *rootdegree;
3390*dd05d810SMatthew G. Knepley     PetscInt          *slocalPoints = NULL, *sortedPoints = NULL, *sortedIndices = NULL;
33915033f954SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubpoints = 0, numSubroots, numSubleaves = 0, l, sl = 0, ll = 0, pStart, pEnd, p;
33925033f954SMatthew G. Knepley     PetscMPIInt        rank, size;
33933982b651SMatthew G. Knepley 
33949566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
33955033f954SMatthew G. Knepley     PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)dm), &size));
33969566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm, &sfPoint));
33979566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(subdm, &sfPointSub));
33989566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
33999566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(subdm, NULL, &numSubroots));
34009566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(subdm, &subpIS));
34013982b651SMatthew G. Knepley     if (subpIS) {
3402*dd05d810SMatthew G. Knepley       PetscBool sorted = PETSC_TRUE;
3403*dd05d810SMatthew G. Knepley 
34049566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subpIS, &subpoints));
34059566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(subpIS, &numSubpoints));
3406*dd05d810SMatthew G. Knepley       for (p = 1; p < numSubpoints; ++p) sorted = sorted && (subpoints[p] >= subpoints[p - 1]) ? PETSC_TRUE : PETSC_FALSE;
3407*dd05d810SMatthew G. Knepley       if (!sorted) {
3408*dd05d810SMatthew G. Knepley         PetscCall(PetscMalloc2(numSubpoints, &sortedPoints, numSubpoints, &sortedIndices));
3409*dd05d810SMatthew G. Knepley         for (p = 0; p < numSubpoints; ++p) sortedIndices[p] = p;
3410*dd05d810SMatthew G. Knepley         PetscCall(PetscArraycpy(sortedPoints, subpoints, numSubpoints));
3411*dd05d810SMatthew G. Knepley         PetscCall(PetscSortIntWithArray(numSubpoints, sortedPoints, sortedIndices));
3412*dd05d810SMatthew G. Knepley       }
34133982b651SMatthew G. Knepley     }
34149566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints));
34153982b651SMatthew G. Knepley     if (numRoots >= 0) {
34165033f954SMatthew G. Knepley       PetscCall(PetscSFComputeDegreeBegin(sfPoint, &rootdegree));
34175033f954SMatthew G. Knepley       PetscCall(PetscSFComputeDegreeEnd(sfPoint, &rootdegree));
34189566063dSJacob Faibussowitsch       PetscCall(PetscMalloc2(pEnd - pStart, &newLocalPoints, numRoots, &newOwners));
34193982b651SMatthew G. Knepley       for (p = 0; p < pEnd - pStart; ++p) {
34203982b651SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
34213982b651SMatthew G. Knepley         newLocalPoints[p].index = -2;
34223982b651SMatthew G. Knepley       }
34233982b651SMatthew G. Knepley       /* Set subleaves */
34243982b651SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
34253982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3426*dd05d810SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPointPerm_Internal(point, 0, numSubpoints, sortedPoints ? sortedPoints : subpoints, sortedIndices);
34273982b651SMatthew G. Knepley 
34283982b651SMatthew G. Knepley         if (subpoint < 0) continue;
34293982b651SMatthew G. Knepley         newLocalPoints[point - pStart].rank  = rank;
34303982b651SMatthew G. Knepley         newLocalPoints[point - pStart].index = subpoint;
34313982b651SMatthew G. Knepley         ++numSubleaves;
34323982b651SMatthew G. Knepley       }
34333982b651SMatthew G. Knepley       /* Must put in owned subpoints */
34343982b651SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
34353982b651SMatthew G. Knepley         newOwners[p - pStart].rank  = -3;
34363982b651SMatthew G. Knepley         newOwners[p - pStart].index = -3;
34373982b651SMatthew G. Knepley       }
3438d9384f27SMatthew G. Knepley       for (p = 0; p < numSubpoints; ++p) {
34395033f954SMatthew G. Knepley         /* Hold on to currently owned points */
34405033f954SMatthew G. Knepley         if (rootdegree[subpoints[p] - pStart]) newOwners[subpoints[p] - pStart].rank = rank + size;
34415033f954SMatthew G. Knepley         else newOwners[subpoints[p] - pStart].rank = rank;
3442d9384f27SMatthew G. Knepley         newOwners[subpoints[p] - pStart].index = p;
34433982b651SMatthew G. Knepley       }
34449566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
34459566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
34469371c9d4SSatish Balay       for (p = pStart; p < pEnd; ++p)
34479371c9d4SSatish Balay         if (newOwners[p - pStart].rank >= size) newOwners[p - pStart].rank -= size;
34489566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE));
34499566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE));
34509566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubleaves, &slocalPoints));
34519566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubleaves, &sremotePoints));
34525033f954SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
34533982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3454*dd05d810SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPointPerm_Internal(point, 0, numSubpoints, sortedPoints ? sortedPoints : subpoints, sortedIndices);
34553982b651SMatthew G. Knepley 
34563982b651SMatthew G. Knepley         if (subpoint < 0) continue;
34579371c9d4SSatish Balay         if (newLocalPoints[point].rank == rank) {
34589371c9d4SSatish Balay           ++ll;
34599371c9d4SSatish Balay           continue;
34609371c9d4SSatish Balay         }
34613982b651SMatthew G. Knepley         slocalPoints[sl]        = subpoint;
34623982b651SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
34633982b651SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
346463a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].rank >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %" PetscInt_FMT, point);
346563a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].index >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %" PetscInt_FMT, point);
34663982b651SMatthew G. Knepley         ++sl;
34673982b651SMatthew G. Knepley       }
34681dca8a05SBarry Smith       PetscCheck(sl + ll == numSubleaves, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %" PetscInt_FMT " + %" PetscInt_FMT " != %" PetscInt_FMT, sl, ll, numSubleaves);
34699566063dSJacob Faibussowitsch       PetscCall(PetscFree2(newLocalPoints, newOwners));
34709566063dSJacob Faibussowitsch       PetscCall(PetscSFSetGraph(sfPointSub, numSubroots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER));
34713982b651SMatthew G. Knepley     }
347248a46eb9SPierre Jolivet     if (subpIS) PetscCall(ISRestoreIndices(subpIS, &subpoints));
3473*dd05d810SMatthew G. Knepley     PetscCall(PetscFree2(sortedPoints, sortedIndices));
34743982b651SMatthew G. Knepley   }
3475212103e5SMatthew Knepley   /* Filter labels */
34769566063dSJacob Faibussowitsch   PetscCall(DMPlexFilterLabels_Internal(dm, numSubPoints, subpoints, firstSubPoint, subdm));
3477efa14ee0SMatthew G Knepley   /* Cleanup */
3478*dd05d810SMatthew G. Knepley   for (d = 0; d <= sdim; ++d) {
34799566063dSJacob Faibussowitsch     if (subpointIS[d]) PetscCall(ISRestoreIndices(subpointIS[d], &subpoints[d]));
34809566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&subpointIS[d]));
3481e6ccafaeSMatthew G Knepley   }
34829566063dSJacob Faibussowitsch   PetscCall(PetscFree4(numSubPoints, firstSubPoint, subpointIS, subpoints));
3483e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3484e6ccafaeSMatthew G Knepley }
3485e6ccafaeSMatthew G Knepley 
34869371c9d4SSatish Balay static PetscErrorCode DMPlexCreateSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM subdm) {
34873982b651SMatthew G. Knepley   PetscFunctionBegin;
34889566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, vertexLabel, value, markedFaces, PETSC_FALSE, 1, subdm));
34893982b651SMatthew G. Knepley   PetscFunctionReturn(0);
34903982b651SMatthew G. Knepley }
34913982b651SMatthew G. Knepley 
3492d0fa310fSMatthew G. Knepley /*@
3493e6ccafaeSMatthew G Knepley   DMPlexCreateSubmesh - Extract a hypersurface from the mesh using vertices defined by a label
3494e6ccafaeSMatthew G Knepley 
3495e6ccafaeSMatthew G Knepley   Input Parameters:
3496e6ccafaeSMatthew G Knepley + dm           - The original mesh
3497158acfadSMatthew G. Knepley . vertexLabel  - The DMLabel marking points contained in the surface
3498158acfadSMatthew G. Knepley . value        - The label value to use
3499158acfadSMatthew G. Knepley - markedFaces  - PETSC_TRUE if surface faces are marked in addition to vertices, PETSC_FALSE if only vertices are marked
3500e6ccafaeSMatthew G Knepley 
3501e6ccafaeSMatthew G Knepley   Output Parameter:
3502e6ccafaeSMatthew G Knepley . subdm - The surface mesh
3503e6ccafaeSMatthew G Knepley 
3504e6ccafaeSMatthew G Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3505e6ccafaeSMatthew G Knepley 
3506e6ccafaeSMatthew G Knepley   Level: developer
3507e6ccafaeSMatthew G Knepley 
3508db781477SPatrick Sanan .seealso: `DMPlexGetSubpointMap()`, `DMGetLabel()`, `DMLabelSetValue()`
3509830e53efSMatthew G. Knepley @*/
35109371c9d4SSatish Balay PetscErrorCode DMPlexCreateSubmesh(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM *subdm) {
3511827c4036SVaclav Hapla   DMPlexInterpolatedFlag interpolated;
3512827c4036SVaclav Hapla   PetscInt               dim, cdim;
3513e6ccafaeSMatthew G Knepley 
3514e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3515e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3516064a246eSJacob Faibussowitsch   PetscValidPointer(subdm, 5);
35179566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
35189566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
35199566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
35209566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*subdm, dim - 1));
35219566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
35229566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*subdm, cdim));
35239566063dSJacob Faibussowitsch   PetscCall(DMPlexIsInterpolated(dm, &interpolated));
352408401ef6SPierre Jolivet   PetscCheck(interpolated != DMPLEX_INTERPOLATED_PARTIAL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not for partially interpolated meshes");
3525827c4036SVaclav Hapla   if (interpolated) {
35269566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateSubmesh_Interpolated(dm, vertexLabel, value, markedFaces, *subdm));
3527e6ccafaeSMatthew G Knepley   } else {
35289566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateSubmesh_Uninterpolated(dm, vertexLabel, value, *subdm));
3529e6ccafaeSMatthew G Knepley   }
35305de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
3531e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3532e6ccafaeSMatthew G Knepley }
3533e6ccafaeSMatthew G Knepley 
35349371c9d4SSatish Balay static PetscErrorCode DMPlexCreateCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM subdm) {
3535766ab985SMatthew G. Knepley   MPI_Comm        comm;
3536766ab985SMatthew G. Knepley   DMLabel         subpointMap;
3537766ab985SMatthew G. Knepley   IS              subvertexIS;
3538766ab985SMatthew G. Knepley   const PetscInt *subVertices;
3539fed694aaSMatthew G. Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells, *subCells = NULL;
3540766ab985SMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces, firstSubFace, newFacePoint, nFV;
3541412e9a14SMatthew G. Knepley   PetscInt        c, f;
3542766ab985SMatthew G. Knepley 
3543766ab985SMatthew G. Knepley   PetscFunctionBegin;
35449566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
3545766ab985SMatthew G. Knepley   /* Create subpointMap which marks the submesh */
35469566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
35479566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
35489566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&subpointMap));
35499566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, subpointMap, &numSubFaces, &nFV, &subCells, subdm));
3550766ab985SMatthew G. Knepley   /* Setup chart */
35519566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 0, &numSubVertices));
35529566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 2, &numSubCells));
35539566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, numSubCells + numSubFaces + numSubVertices));
35549566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, 1));
3555766ab985SMatthew G. Knepley   /* Set cone sizes */
3556766ab985SMatthew G. Knepley   firstSubVertex = numSubCells;
3557766ab985SMatthew G. Knepley   firstSubFace   = numSubCells + numSubVertices;
3558766ab985SMatthew G. Knepley   newFacePoint   = firstSubFace;
35599566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 0, &subvertexIS));
35609566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISGetIndices(subvertexIS, &subVertices));
356148a46eb9SPierre Jolivet   for (c = 0; c < numSubCells; ++c) PetscCall(DMPlexSetConeSize(subdm, c, 1));
356248a46eb9SPierre Jolivet   for (f = firstSubFace; f < firstSubFace + numSubFaces; ++f) PetscCall(DMPlexSetConeSize(subdm, f, nFV));
35639566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3564766ab985SMatthew G. Knepley   /* Create face cones */
35659566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
35669566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
3567766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3568766ab985SMatthew G. Knepley     const PetscInt  cell    = subCells[c];
3569766ab985SMatthew G. Knepley     const PetscInt  subcell = c;
357087feddfdSMatthew G. Knepley     const PetscInt *cone, *cells;
3571412e9a14SMatthew G. Knepley     PetscBool       isHybrid;
357287feddfdSMatthew G. Knepley     PetscInt        numCells, subVertex, p, v;
3573766ab985SMatthew G. Knepley 
35749566063dSJacob Faibussowitsch     PetscCall(DMPlexCellIsHybrid_Internal(dm, cell, &isHybrid));
3575412e9a14SMatthew G. Knepley     if (!isHybrid) continue;
35769566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, cell, &cone));
357787feddfdSMatthew G. Knepley     for (v = 0; v < nFV; ++v) {
35789566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(cone[v], numSubVertices, subVertices, &subVertex));
357987feddfdSMatthew G. Knepley       subface[v] = firstSubVertex + subVertex;
358087feddfdSMatthew G. Knepley     }
35819566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, newFacePoint, subface));
35829566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, &newFacePoint));
35839566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, nFV, cone, &numCells, &cells));
358427234c99SMatthew G. Knepley     /* Not true in parallel
358508401ef6SPierre Jolivet     PetscCheck(numCells == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
358687feddfdSMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
358787feddfdSMatthew G. Knepley       PetscInt  negsubcell;
3588412e9a14SMatthew G. Knepley       PetscBool isHybrid;
3589766ab985SMatthew G. Knepley 
35909566063dSJacob Faibussowitsch       PetscCall(DMPlexCellIsHybrid_Internal(dm, cells[p], &isHybrid));
3591412e9a14SMatthew G. Knepley       if (isHybrid) continue;
359287feddfdSMatthew G. Knepley       /* I know this is a crap search */
359387feddfdSMatthew G. Knepley       for (negsubcell = 0; negsubcell < numSubCells; ++negsubcell) {
359487feddfdSMatthew G. Knepley         if (subCells[negsubcell] == cells[p]) break;
3595766ab985SMatthew G. Knepley       }
359663a3b9bcSJacob Faibussowitsch       PetscCheck(negsubcell != numSubCells, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find negative face neighbor for cohesive cell %" PetscInt_FMT, cell);
35979566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(subdm, negsubcell, &newFacePoint));
3598766ab985SMatthew G. Knepley     }
35999566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, nFV, cone, &numCells, &cells));
360087feddfdSMatthew G. Knepley     ++newFacePoint;
3601766ab985SMatthew G. Knepley   }
36029566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void **)&subface));
36039566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
36049566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3605766ab985SMatthew G. Knepley   /* Build coordinates */
3606766ab985SMatthew G. Knepley   {
3607766ab985SMatthew G. Knepley     PetscSection coordSection, subCoordSection;
3608766ab985SMatthew G. Knepley     Vec          coordinates, subCoordinates;
3609766ab985SMatthew G. Knepley     PetscScalar *coords, *subCoords;
3610c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize, v;
361124640c55SToby Isaac     const char  *name;
3612766ab985SMatthew G. Knepley 
36139566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDim(dm, &cdim));
36149566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
36159566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
36169566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
36179566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
36189566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
36199566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
36209566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex + numSubVertices));
3621766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3622766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3623766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex + v;
3624766ab985SMatthew G. Knepley       PetscInt       dof;
3625766ab985SMatthew G. Knepley 
36269566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
36279566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
36289566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3629766ab985SMatthew G. Knepley     }
36309566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
36319566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
36329566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
36339566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates, &name));
36349566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates, name));
36359566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
36369566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(subCoordinates, cdim));
36379566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates, VECSTANDARD));
36389566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
36399566063dSJacob Faibussowitsch     PetscCall(VecGetArray(subCoordinates, &subCoords));
3640766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3641766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3642766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex + v;
3643766ab985SMatthew G. Knepley       PetscInt       dof, off, sdof, soff, d;
3644766ab985SMatthew G. Knepley 
36459566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
36469566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
36479566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
36489566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
364963a3b9bcSJacob Faibussowitsch       PetscCheck(dof == sdof, comm, PETSC_ERR_PLIB, "Coordinate dimension %" PetscInt_FMT " on subvertex %" PetscInt_FMT ", vertex %" PetscInt_FMT " should be %" PetscInt_FMT, sdof, subvertex, vertex, dof);
3650766ab985SMatthew G. Knepley       for (d = 0; d < dof; ++d) subCoords[soff + d] = coords[off + d];
3651766ab985SMatthew G. Knepley     }
36529566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
36539566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(subCoordinates, &subCoords));
36549566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
36559566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3656766ab985SMatthew G. Knepley   }
3657aca35d17SMatthew G. Knepley   /* Build SF */
3658aca35d17SMatthew G. Knepley   CHKMEMQ;
3659aca35d17SMatthew G. Knepley   {
3660aca35d17SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
3661aca35d17SMatthew G. Knepley     const PetscSFNode *remotePoints;
3662bdcf2095SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
3663aca35d17SMatthew G. Knepley     const PetscInt    *localPoints;
3664bdcf2095SMatthew G. Knepley     PetscInt          *slocalPoints;
366549c26ae4SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubRoots = numSubCells + numSubFaces + numSubVertices, numSubLeaves = 0, l, sl, ll, pStart, pEnd, p, vStart, vEnd;
3666bdcf2095SMatthew G. Knepley     PetscMPIInt        rank;
3667aca35d17SMatthew G. Knepley 
36689566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
36699566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm, &sfPoint));
36709566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(subdm, &sfPointSub));
36719566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
36729566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
36739566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints));
3674aca35d17SMatthew G. Knepley     if (numRoots >= 0) {
3675aca35d17SMatthew G. Knepley       /* Only vertices should be shared */
36769566063dSJacob Faibussowitsch       PetscCall(PetscMalloc2(pEnd - pStart, &newLocalPoints, numRoots, &newOwners));
3677bdcf2095SMatthew G. Knepley       for (p = 0; p < pEnd - pStart; ++p) {
3678bdcf2095SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
3679bdcf2095SMatthew G. Knepley         newLocalPoints[p].index = -2;
3680bdcf2095SMatthew G. Knepley       }
36819e0823b2SMatthew G. Knepley       /* Set subleaves */
3682aca35d17SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
3683aca35d17SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3684bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3685aca35d17SMatthew G. Knepley 
368663a3b9bcSJacob Faibussowitsch         PetscCheck(!(point < vStart) || !(point >= vEnd), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Should not be mapping anything but vertices, %" PetscInt_FMT, point);
3687bdcf2095SMatthew G. Knepley         if (subPoint < 0) continue;
3688bdcf2095SMatthew G. Knepley         newLocalPoints[point - pStart].rank  = rank;
3689bdcf2095SMatthew G. Knepley         newLocalPoints[point - pStart].index = subPoint;
3690bdcf2095SMatthew G. Knepley         ++numSubLeaves;
3691aca35d17SMatthew G. Knepley       }
36929e0823b2SMatthew G. Knepley       /* Must put in owned subpoints */
36939e0823b2SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
36949e0823b2SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(p, firstSubVertex, numSubVertices, subVertices);
36959e0823b2SMatthew G. Knepley 
36969e0823b2SMatthew G. Knepley         if (subPoint < 0) {
36979e0823b2SMatthew G. Knepley           newOwners[p - pStart].rank  = -3;
36989e0823b2SMatthew G. Knepley           newOwners[p - pStart].index = -3;
36999e0823b2SMatthew G. Knepley         } else {
37009e0823b2SMatthew G. Knepley           newOwners[p - pStart].rank  = rank;
37019e0823b2SMatthew G. Knepley           newOwners[p - pStart].index = subPoint;
37029e0823b2SMatthew G. Knepley         }
3703bdcf2095SMatthew G. Knepley       }
37049566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
37059566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
37069566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE));
37079566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints, MPI_REPLACE));
37089566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubLeaves, &slocalPoints));
37099566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubLeaves, &sremotePoints));
371049c26ae4SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
3711bdcf2095SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3712bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3713aca35d17SMatthew G. Knepley 
3714aca35d17SMatthew G. Knepley         if (subPoint < 0) continue;
37159371c9d4SSatish Balay         if (newLocalPoints[point].rank == rank) {
37169371c9d4SSatish Balay           ++ll;
37179371c9d4SSatish Balay           continue;
37189371c9d4SSatish Balay         }
3719aca35d17SMatthew G. Knepley         slocalPoints[sl]        = subPoint;
3720bdcf2095SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
3721bdcf2095SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
372263a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].rank >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %" PetscInt_FMT, point);
372363a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].index >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %" PetscInt_FMT, point);
3724aca35d17SMatthew G. Knepley         ++sl;
3725aca35d17SMatthew G. Knepley       }
37269566063dSJacob Faibussowitsch       PetscCall(PetscFree2(newLocalPoints, newOwners));
37271dca8a05SBarry Smith       PetscCheck(sl + ll == numSubLeaves, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %" PetscInt_FMT " + %" PetscInt_FMT " != %" PetscInt_FMT, sl, ll, numSubLeaves);
37289566063dSJacob Faibussowitsch       PetscCall(PetscSFSetGraph(sfPointSub, numSubRoots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER));
3729aca35d17SMatthew G. Knepley     }
3730aca35d17SMatthew G. Knepley   }
3731aca35d17SMatthew G. Knepley   CHKMEMQ;
3732766ab985SMatthew G. Knepley   /* Cleanup */
37339566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subVertices));
37349566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
37359566063dSJacob Faibussowitsch   PetscCall(PetscFree(subCells));
3736766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
3737766ab985SMatthew G. Knepley }
3738766ab985SMatthew G. Knepley 
37399371c9d4SSatish Balay static PetscErrorCode DMPlexCreateCohesiveSubmesh_Interpolated(DM dm, const char labelname[], PetscInt value, DM subdm) {
37403982b651SMatthew G. Knepley   DMLabel label = NULL;
3741766ab985SMatthew G. Knepley 
3742766ab985SMatthew G. Knepley   PetscFunctionBegin;
37439566063dSJacob Faibussowitsch   if (labelname) PetscCall(DMGetLabel(dm, labelname, &label));
37449566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, label, value, PETSC_FALSE, PETSC_TRUE, 1, subdm));
3745766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
3746766ab985SMatthew G. Knepley }
3747766ab985SMatthew G. Knepley 
3748c08575a3SMatthew G. Knepley /*@C
37497d2fefb6SMatthew G. Knepley   DMPlexCreateCohesiveSubmesh - Extract from a mesh with cohesive cells the hypersurface defined by one face of the cells. Optionally, a Label can be given to restrict the cells.
3750766ab985SMatthew G. Knepley 
3751766ab985SMatthew G. Knepley   Input Parameters:
3752766ab985SMatthew G. Knepley + dm          - The original mesh
375327c04023SMatthew G. Knepley . hasLagrange - The mesh has Lagrange unknowns in the cohesive cells
37547afc1a8bSJed Brown . label       - A label name, or NULL
375527c04023SMatthew G. Knepley - value  - A label value
3756766ab985SMatthew G. Knepley 
3757766ab985SMatthew G. Knepley   Output Parameter:
3758766ab985SMatthew G. Knepley . subdm - The surface mesh
3759766ab985SMatthew G. Knepley 
3760766ab985SMatthew G. Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3761766ab985SMatthew G. Knepley 
3762766ab985SMatthew G. Knepley   Level: developer
3763766ab985SMatthew G. Knepley 
3764db781477SPatrick Sanan .seealso: `DMPlexGetSubpointMap()`, `DMPlexCreateSubmesh()`
3765c08575a3SMatthew G. Knepley @*/
37669371c9d4SSatish Balay PetscErrorCode DMPlexCreateCohesiveSubmesh(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM *subdm) {
3767c0e8cf5fSMatthew G. Knepley   PetscInt dim, cdim, depth;
3768766ab985SMatthew G. Knepley 
3769766ab985SMatthew G. Knepley   PetscFunctionBegin;
3770766ab985SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
377127c04023SMatthew G. Knepley   PetscValidPointer(subdm, 5);
37729566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
37739566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
37749566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
37759566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
37769566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*subdm, dim - 1));
37779566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
37789566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*subdm, cdim));
3779766ab985SMatthew G. Knepley   if (depth == dim) {
37809566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCohesiveSubmesh_Interpolated(dm, label, value, *subdm));
3781766ab985SMatthew G. Knepley   } else {
37829566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, *subdm));
3783e6ccafaeSMatthew G Knepley   }
37845de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
3785e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3786e6ccafaeSMatthew G Knepley }
3787e6ccafaeSMatthew G Knepley 
3788bec263e5SMatthew G. Knepley /*@
3789bec263e5SMatthew G. Knepley   DMPlexFilter - Extract a subset of mesh cells defined by a label as a separate mesh
3790bec263e5SMatthew G. Knepley 
3791bec263e5SMatthew G. Knepley   Input Parameters:
3792bec263e5SMatthew G. Knepley + dm        - The original mesh
3793bec263e5SMatthew G. Knepley . cellLabel - The DMLabel marking cells contained in the new mesh
3794bec263e5SMatthew G. Knepley - value     - The label value to use
3795bec263e5SMatthew G. Knepley 
3796bec263e5SMatthew G. Knepley   Output Parameter:
3797bec263e5SMatthew G. Knepley . subdm - The new mesh
3798bec263e5SMatthew G. Knepley 
3799*dd05d810SMatthew G. Knepley   Notes:
3800*dd05d810SMatthew G. Knepley   This function produces a `DMLabel` mapping original points in the submesh to their depth. This can be obtained using `DMPlexGetSubpointMap()`.
3801bec263e5SMatthew G. Knepley 
3802bec263e5SMatthew G. Knepley   Level: developer
3803bec263e5SMatthew G. Knepley 
3804*dd05d810SMatthew G. Knepley .seealso: `DMPlexGetSubpointMap()`, `DMGetLabel()`, `DMLabelSetValue()`, `DMPlexCreateSubmesh()`
3805bec263e5SMatthew G. Knepley @*/
38069371c9d4SSatish Balay PetscErrorCode DMPlexFilter(DM dm, DMLabel cellLabel, PetscInt value, DM *subdm) {
3807bec263e5SMatthew G. Knepley   PetscInt dim;
3808bec263e5SMatthew G. Knepley 
3809bec263e5SMatthew G. Knepley   PetscFunctionBegin;
3810bec263e5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3811064a246eSJacob Faibussowitsch   PetscValidPointer(subdm, 4);
38129566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
38139566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
38149566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
3815bec263e5SMatthew G. Knepley   /* Extract submesh in place, could be empty on some procs, could have inconsistency if procs do not both extract a shared cell */
38169566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, cellLabel, value, PETSC_FALSE, PETSC_FALSE, 0, *subdm));
38175de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
3818bec263e5SMatthew G. Knepley   PetscFunctionReturn(0);
3819bec263e5SMatthew G. Knepley }
3820bec263e5SMatthew G. Knepley 
382164beef6dSMatthew G. Knepley /*@
382264beef6dSMatthew G. Knepley   DMPlexGetSubpointMap - Returns a DMLabel with point dimension as values
382364beef6dSMatthew G. Knepley 
382464beef6dSMatthew G. Knepley   Input Parameter:
382564beef6dSMatthew G. Knepley . dm - The submesh DM
382664beef6dSMatthew G. Knepley 
382764beef6dSMatthew G. Knepley   Output Parameter:
382864beef6dSMatthew G. Knepley . subpointMap - The DMLabel of all the points from the original mesh in this submesh, or NULL if this is not a submesh
382964beef6dSMatthew G. Knepley 
383064beef6dSMatthew G. Knepley   Level: developer
383164beef6dSMatthew G. Knepley 
3832db781477SPatrick Sanan .seealso: `DMPlexCreateSubmesh()`, `DMPlexGetSubpointIS()`
383364beef6dSMatthew G. Knepley @*/
38349371c9d4SSatish Balay PetscErrorCode DMPlexGetSubpointMap(DM dm, DMLabel *subpointMap) {
3835e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3836e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3837e6ccafaeSMatthew G Knepley   PetscValidPointer(subpointMap, 2);
383865663942SMatthew G. Knepley   *subpointMap = ((DM_Plex *)dm->data)->subpointMap;
3839e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3840e6ccafaeSMatthew G Knepley }
3841e6ccafaeSMatthew G Knepley 
3842c08575a3SMatthew G. Knepley /*@
3843c08575a3SMatthew G. Knepley   DMPlexSetSubpointMap - Sets the DMLabel with point dimension as values
3844c08575a3SMatthew G. Knepley 
3845c08575a3SMatthew G. Knepley   Input Parameters:
3846c08575a3SMatthew G. Knepley + dm - The submesh DM
3847c08575a3SMatthew G. Knepley - subpointMap - The DMLabel of all the points from the original mesh in this submesh
3848c08575a3SMatthew G. Knepley 
3849c08575a3SMatthew G. Knepley   Note: Should normally not be called by the user, since it is set in DMPlexCreateSubmesh()
3850c08575a3SMatthew G. Knepley 
3851c08575a3SMatthew G. Knepley   Level: developer
3852c08575a3SMatthew G. Knepley 
3853db781477SPatrick Sanan .seealso: `DMPlexCreateSubmesh()`, `DMPlexGetSubpointIS()`
3854c08575a3SMatthew G. Knepley @*/
38559371c9d4SSatish Balay PetscErrorCode DMPlexSetSubpointMap(DM dm, DMLabel subpointMap) {
3856e6ccafaeSMatthew G Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
3857285d324eSMatthew G. Knepley   DMLabel  tmp;
3858e6ccafaeSMatthew G Knepley 
3859e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3860e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3861285d324eSMatthew G. Knepley   tmp               = mesh->subpointMap;
3862e6ccafaeSMatthew G Knepley   mesh->subpointMap = subpointMap;
38639566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)mesh->subpointMap));
38649566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&tmp));
3865e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3866e6ccafaeSMatthew G Knepley }
3867e6ccafaeSMatthew G Knepley 
38689371c9d4SSatish Balay static PetscErrorCode DMPlexCreateSubpointIS_Internal(DM dm, IS *subpointIS) {
386997d8846cSMatthew Knepley   DMLabel  spmap;
387097d8846cSMatthew Knepley   PetscInt depth, d;
387197d8846cSMatthew Knepley 
387297d8846cSMatthew Knepley   PetscFunctionBegin;
38739566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(dm, &spmap));
38749566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
387597d8846cSMatthew Knepley   if (spmap && depth >= 0) {
387697d8846cSMatthew Knepley     DM_Plex  *mesh = (DM_Plex *)dm->data;
387797d8846cSMatthew Knepley     PetscInt *points, *depths;
387897d8846cSMatthew Knepley     PetscInt  pStart, pEnd, p, off;
387997d8846cSMatthew Knepley 
38809566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
388163a3b9bcSJacob Faibussowitsch     PetscCheck(!pStart, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Submeshes must start the point numbering at 0, not %" PetscInt_FMT, pStart);
38829566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(pEnd, &points));
38839566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(dm, depth + 1, MPIU_INT, &depths));
388497d8846cSMatthew Knepley     depths[0] = depth;
388597d8846cSMatthew Knepley     depths[1] = 0;
3886ad540459SPierre Jolivet     for (d = 2; d <= depth; ++d) depths[d] = depth + 1 - d;
388797d8846cSMatthew Knepley     for (d = 0, off = 0; d <= depth; ++d) {
388897d8846cSMatthew Knepley       const PetscInt dep = depths[d];
388997d8846cSMatthew Knepley       PetscInt       depStart, depEnd, n;
389097d8846cSMatthew Knepley 
38919566063dSJacob Faibussowitsch       PetscCall(DMPlexGetDepthStratum(dm, dep, &depStart, &depEnd));
38929566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumSize(spmap, dep, &n));
389397d8846cSMatthew Knepley       if (((d < 2) && (depth > 1)) || (d == 1)) { /* Only check vertices and cells for now since the map is broken for others */
389463a3b9bcSJacob Faibussowitsch         PetscCheck(n == depEnd - depStart, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %" PetscInt_FMT " at depth %" PetscInt_FMT " should be %" PetscInt_FMT, n, dep, depEnd - depStart);
389597d8846cSMatthew Knepley       } else {
389697d8846cSMatthew Knepley         if (!n) {
389797d8846cSMatthew Knepley           if (d == 0) {
389897d8846cSMatthew Knepley             /* Missing cells */
389997d8846cSMatthew Knepley             for (p = 0; p < depEnd - depStart; ++p, ++off) points[off] = -1;
390097d8846cSMatthew Knepley           } else {
390197d8846cSMatthew Knepley             /* Missing faces */
390297d8846cSMatthew Knepley             for (p = 0; p < depEnd - depStart; ++p, ++off) points[off] = PETSC_MAX_INT;
390397d8846cSMatthew Knepley           }
390497d8846cSMatthew Knepley         }
390597d8846cSMatthew Knepley       }
390697d8846cSMatthew Knepley       if (n) {
390797d8846cSMatthew Knepley         IS              is;
390897d8846cSMatthew Knepley         const PetscInt *opoints;
390997d8846cSMatthew Knepley 
39109566063dSJacob Faibussowitsch         PetscCall(DMLabelGetStratumIS(spmap, dep, &is));
39119566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(is, &opoints));
391297d8846cSMatthew Knepley         for (p = 0; p < n; ++p, ++off) points[off] = opoints[p];
39139566063dSJacob Faibussowitsch         PetscCall(ISRestoreIndices(is, &opoints));
39149566063dSJacob Faibussowitsch         PetscCall(ISDestroy(&is));
391597d8846cSMatthew Knepley       }
391697d8846cSMatthew Knepley     }
39179566063dSJacob Faibussowitsch     PetscCall(DMRestoreWorkArray(dm, depth + 1, MPIU_INT, &depths));
391863a3b9bcSJacob Faibussowitsch     PetscCheck(off == pEnd, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %" PetscInt_FMT " should be %" PetscInt_FMT, off, pEnd);
39199566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, pEnd, points, PETSC_OWN_POINTER, subpointIS));
39209566063dSJacob Faibussowitsch     PetscCall(PetscObjectStateGet((PetscObject)spmap, &mesh->subpointState));
392197d8846cSMatthew Knepley   }
392297d8846cSMatthew Knepley   PetscFunctionReturn(0);
392397d8846cSMatthew Knepley }
392497d8846cSMatthew Knepley 
392564beef6dSMatthew G. Knepley /*@
392697d8846cSMatthew Knepley   DMPlexGetSubpointIS - Returns an IS covering the entire subdm chart with the original points as data
3927e6ccafaeSMatthew G Knepley 
3928e6ccafaeSMatthew G Knepley   Input Parameter:
3929e6ccafaeSMatthew G Knepley . dm - The submesh DM
3930e6ccafaeSMatthew G Knepley 
3931e6ccafaeSMatthew G Knepley   Output Parameter:
39320298fd71SBarry Smith . subpointIS - The IS of all the points from the original mesh in this submesh, or NULL if this is not a submesh
3933e6ccafaeSMatthew G Knepley 
39343982b651SMatthew G. Knepley   Note: This IS is guaranteed to be sorted by the construction of the submesh
393564beef6dSMatthew G. Knepley 
393664beef6dSMatthew G. Knepley   Level: developer
393764beef6dSMatthew G. Knepley 
3938db781477SPatrick Sanan .seealso: `DMPlexCreateSubmesh()`, `DMPlexGetSubpointMap()`
393964beef6dSMatthew G. Knepley @*/
39409371c9d4SSatish Balay PetscErrorCode DMPlexGetSubpointIS(DM dm, IS *subpointIS) {
394197d8846cSMatthew Knepley   DM_Plex         *mesh = (DM_Plex *)dm->data;
394297d8846cSMatthew Knepley   DMLabel          spmap;
394397d8846cSMatthew Knepley   PetscObjectState state;
3944e6ccafaeSMatthew G Knepley 
3945e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3946e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3947e6ccafaeSMatthew G Knepley   PetscValidPointer(subpointIS, 2);
39489566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(dm, &spmap));
39499566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateGet((PetscObject)spmap, &state));
39509566063dSJacob Faibussowitsch   if (state != mesh->subpointState || !mesh->subpointIS) PetscCall(DMPlexCreateSubpointIS_Internal(dm, &mesh->subpointIS));
395197d8846cSMatthew Knepley   *subpointIS = mesh->subpointIS;
3952e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3953e6ccafaeSMatthew G Knepley }
3954559a1558SMatthew G. Knepley 
3955559a1558SMatthew G. Knepley /*@
3956a6e0b375SMatthew G. Knepley   DMGetEnclosureRelation - Get the relationship between dmA and dmB
3957559a1558SMatthew G. Knepley 
3958559a1558SMatthew G. Knepley   Input Parameters:
3959a6e0b375SMatthew G. Knepley + dmA - The first DM
3960a6e0b375SMatthew G. Knepley - dmB - The second DM
3961559a1558SMatthew G. Knepley 
3962559a1558SMatthew G. Knepley   Output Parameter:
3963a6e0b375SMatthew G. Knepley . rel - The relation of dmA to dmB
3964559a1558SMatthew G. Knepley 
3965a6e0b375SMatthew G. Knepley   Level: intermediate
3966559a1558SMatthew G. Knepley 
3967db781477SPatrick Sanan .seealso: `DMGetEnclosurePoint()`
3968559a1558SMatthew G. Knepley @*/
39699371c9d4SSatish Balay PetscErrorCode DMGetEnclosureRelation(DM dmA, DM dmB, DMEnclosureType *rel) {
3970a6e0b375SMatthew G. Knepley   DM       plexA, plexB, sdm;
3971559a1558SMatthew G. Knepley   DMLabel  spmap;
3972a6e0b375SMatthew G. Knepley   PetscInt pStartA, pEndA, pStartB, pEndB, NpA, NpB;
3973559a1558SMatthew G. Knepley 
397444171101SMatthew G. Knepley   PetscFunctionBegin;
3975a6e0b375SMatthew G. Knepley   PetscValidPointer(rel, 3);
3976a6e0b375SMatthew G. Knepley   *rel = DM_ENC_NONE;
3977a6e0b375SMatthew G. Knepley   if (!dmA || !dmB) PetscFunctionReturn(0);
3978a6e0b375SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
3979064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
39809371c9d4SSatish Balay   if (dmA == dmB) {
39819371c9d4SSatish Balay     *rel = DM_ENC_EQUALITY;
39829371c9d4SSatish Balay     PetscFunctionReturn(0);
39839371c9d4SSatish Balay   }
39849566063dSJacob Faibussowitsch   PetscCall(DMConvert(dmA, DMPLEX, &plexA));
39859566063dSJacob Faibussowitsch   PetscCall(DMConvert(dmB, DMPLEX, &plexB));
39869566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(plexA, &pStartA, &pEndA));
39879566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(plexB, &pStartB, &pEndB));
3988a6e0b375SMatthew G. Knepley   /* Assumption 1: subDMs have smaller charts than the DMs that they originate from
3989a6e0b375SMatthew G. Knepley     - The degenerate case of a subdomain which includes all of the domain on some process can be treated as equality */
3990a6e0b375SMatthew G. Knepley   if ((pStartA == pStartB) && (pEndA == pEndB)) {
3991a6e0b375SMatthew G. Knepley     *rel = DM_ENC_EQUALITY;
3992a6e0b375SMatthew G. Knepley     goto end;
3993559a1558SMatthew G. Knepley   }
3994a6e0b375SMatthew G. Knepley   NpA = pEndA - pStartA;
3995a6e0b375SMatthew G. Knepley   NpB = pEndB - pStartB;
3996a6e0b375SMatthew G. Knepley   if (NpA == NpB) goto end;
3997a6e0b375SMatthew G. Knepley   sdm = NpA > NpB ? plexB : plexA; /* The other is the original, enclosing dm */
39989566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(sdm, &spmap));
3999a6e0b375SMatthew G. Knepley   if (!spmap) goto end;
4000a6e0b375SMatthew G. Knepley   /* TODO Check the space mapped to by subpointMap is same size as dm */
4001a6e0b375SMatthew G. Knepley   if (NpA > NpB) {
4002a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUPERMESH;
4003a6e0b375SMatthew G. Knepley   } else {
4004a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUBMESH;
4005a6e0b375SMatthew G. Knepley   }
4006a6e0b375SMatthew G. Knepley end:
40079566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plexA));
40089566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plexB));
4009559a1558SMatthew G. Knepley   PetscFunctionReturn(0);
4010559a1558SMatthew G. Knepley }
401144171101SMatthew G. Knepley 
401244171101SMatthew G. Knepley /*@
4013a6e0b375SMatthew G. Knepley   DMGetEnclosurePoint - Get the point pA in dmA which corresponds to the point pB in dmB
401444171101SMatthew G. Knepley 
401544171101SMatthew G. Knepley   Input Parameters:
4016a6e0b375SMatthew G. Knepley + dmA   - The first DM
4017a6e0b375SMatthew G. Knepley . dmB   - The second DM
4018a6e0b375SMatthew G. Knepley . etype - The type of enclosure relation that dmA has to dmB
4019a6e0b375SMatthew G. Knepley - pB    - A point of dmB
402044171101SMatthew G. Knepley 
402144171101SMatthew G. Knepley   Output Parameter:
4022a6e0b375SMatthew G. Knepley . pA    - The corresponding point of dmA
402344171101SMatthew G. Knepley 
4024a6e0b375SMatthew G. Knepley   Level: intermediate
402544171101SMatthew G. Knepley 
4026db781477SPatrick Sanan .seealso: `DMGetEnclosureRelation()`
402744171101SMatthew G. Knepley @*/
40289371c9d4SSatish Balay PetscErrorCode DMGetEnclosurePoint(DM dmA, DM dmB, DMEnclosureType etype, PetscInt pB, PetscInt *pA) {
4029a6e0b375SMatthew G. Knepley   DM              sdm;
4030a6e0b375SMatthew G. Knepley   IS              subpointIS;
4031a6e0b375SMatthew G. Knepley   const PetscInt *subpoints;
4032a6e0b375SMatthew G. Knepley   PetscInt        numSubpoints;
403344171101SMatthew G. Knepley 
403444171101SMatthew G. Knepley   PetscFunctionBegin;
4035a6e0b375SMatthew G. Knepley   /* TODO Cache the IS, making it look like an index */
4036a6e0b375SMatthew G. Knepley   switch (etype) {
4037a6e0b375SMatthew G. Knepley   case DM_ENC_SUPERMESH:
4038a6e0b375SMatthew G. Knepley     sdm = dmB;
40399566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(sdm, &subpointIS));
40409566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subpointIS, &subpoints));
4041a6e0b375SMatthew G. Knepley     *pA = subpoints[pB];
40429566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(subpointIS, &subpoints));
4043a6e0b375SMatthew G. Knepley     break;
4044a6e0b375SMatthew G. Knepley   case DM_ENC_SUBMESH:
4045a6e0b375SMatthew G. Knepley     sdm = dmA;
40469566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(sdm, &subpointIS));
40479566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(subpointIS, &numSubpoints));
40489566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subpointIS, &subpoints));
40499566063dSJacob Faibussowitsch     PetscCall(PetscFindInt(pB, numSubpoints, subpoints, pA));
4050a6e0b375SMatthew G. Knepley     if (*pA < 0) {
40519566063dSJacob Faibussowitsch       PetscCall(DMViewFromOptions(dmA, NULL, "-dm_enc_A_view"));
40529566063dSJacob Faibussowitsch       PetscCall(DMViewFromOptions(dmB, NULL, "-dm_enc_B_view"));
405363a3b9bcSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %" PetscInt_FMT " not found in submesh", pB);
4054a6e0b375SMatthew G. Knepley     }
40559566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(subpointIS, &subpoints));
4056a6e0b375SMatthew G. Knepley     break;
4057a6e0b375SMatthew G. Knepley   case DM_ENC_EQUALITY:
40589371c9d4SSatish Balay   case DM_ENC_NONE: *pA = pB; break;
40599371c9d4SSatish Balay   case DM_ENC_UNKNOWN: {
4060a6e0b375SMatthew G. Knepley     DMEnclosureType enc;
406144171101SMatthew G. Knepley 
40629566063dSJacob Faibussowitsch     PetscCall(DMGetEnclosureRelation(dmA, dmB, &enc));
40639566063dSJacob Faibussowitsch     PetscCall(DMGetEnclosurePoint(dmA, dmB, enc, pB, pA));
40649371c9d4SSatish Balay   } break;
406598921bdaSJacob Faibussowitsch   default: SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_ARG_OUTOFRANGE, "Invalid enclosure type %d", (int)etype);
406644171101SMatthew G. Knepley   }
406744171101SMatthew G. Knepley   PetscFunctionReturn(0);
406844171101SMatthew G. Knepley }
4069