xref: /petsc/src/dm/impls/plex/plexsubmesh.c (revision b6dfa3397e453ab95bfee1f8c36819653a7e8b85)
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 
5412e9a14SMatthew G. Knepley static PetscErrorCode DMPlexCellIsHybrid_Internal(DM dm, PetscInt p, PetscBool *isHybrid)
6412e9a14SMatthew G. Knepley {
7412e9a14SMatthew G. Knepley   DMPolytopeType ct;
8412e9a14SMatthew G. Knepley 
9412e9a14SMatthew G. Knepley   PetscFunctionBegin;
109566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellType(dm, p, &ct));
11412e9a14SMatthew G. Knepley   switch (ct) {
12412e9a14SMatthew G. Knepley     case DM_POLYTOPE_POINT_PRISM_TENSOR:
13412e9a14SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
14412e9a14SMatthew G. Knepley     case DM_POLYTOPE_TRI_PRISM_TENSOR:
15412e9a14SMatthew G. Knepley     case DM_POLYTOPE_QUAD_PRISM_TENSOR:
16412e9a14SMatthew G. Knepley       *isHybrid = PETSC_TRUE;
17412e9a14SMatthew G. Knepley     default: *isHybrid = PETSC_FALSE;
18412e9a14SMatthew G. Knepley   }
19412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
20412e9a14SMatthew G. Knepley }
21412e9a14SMatthew G. Knepley 
22412e9a14SMatthew G. Knepley static PetscErrorCode DMPlexGetTensorPrismBounds_Internal(DM dm, PetscInt dim, PetscInt *cStart, PetscInt *cEnd)
23412e9a14SMatthew G. Knepley {
24412e9a14SMatthew G. Knepley   DMLabel        ctLabel;
25412e9a14SMatthew G. Knepley 
26412e9a14SMatthew G. Knepley   PetscFunctionBegin;
27412e9a14SMatthew G. Knepley   if (cStart) *cStart = -1;
28412e9a14SMatthew G. Knepley   if (cEnd)   *cEnd   = -1;
299566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCellTypeLabel(dm, &ctLabel));
30412e9a14SMatthew G. Knepley   switch (dim) {
319566063dSJacob Faibussowitsch     case 1: PetscCall(DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_POINT_PRISM_TENSOR, cStart, cEnd));break;
329566063dSJacob Faibussowitsch     case 2: PetscCall(DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_SEG_PRISM_TENSOR, cStart, cEnd));break;
33412e9a14SMatthew G. Knepley     case 3:
349566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_TRI_PRISM_TENSOR, cStart, cEnd));
359566063dSJacob Faibussowitsch       if (*cStart < 0) PetscCall(DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_QUAD_PRISM_TENSOR, cStart, cEnd));
36412e9a14SMatthew G. Knepley       break;
37412e9a14SMatthew G. Knepley     default: PetscFunctionReturn(0);
38412e9a14SMatthew G. Knepley   }
39412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
40412e9a14SMatthew G. Knepley }
41412e9a14SMatthew G. Knepley 
42e752be1aSMatthew G. Knepley static PetscErrorCode DMPlexMarkBoundaryFaces_Internal(DM dm, PetscInt val, PetscInt cellHeight, DMLabel label)
4330560a7bSMatthew G. Knepley {
4430560a7bSMatthew G. Knepley   PetscInt       fStart, fEnd, f;
4530560a7bSMatthew G. Knepley 
4630560a7bSMatthew G. Knepley   PetscFunctionBegin;
479566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, cellHeight+1, &fStart, &fEnd));
4830560a7bSMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {
4930560a7bSMatthew G. Knepley     PetscInt supportSize;
5030560a7bSMatthew G. Knepley 
519566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, f, &supportSize));
52e752be1aSMatthew G. Knepley     if (supportSize == 1) {
53e752be1aSMatthew G. Knepley       if (val < 0) {
54e752be1aSMatthew G. Knepley         PetscInt *closure = NULL;
55e752be1aSMatthew G. Knepley         PetscInt  clSize, cl, cval;
56e752be1aSMatthew G. Knepley 
579566063dSJacob Faibussowitsch         PetscCall(DMPlexGetTransitiveClosure(dm, f, PETSC_TRUE, &clSize, &closure));
58e752be1aSMatthew G. Knepley         for (cl = 0; cl < clSize*2; cl += 2) {
599566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, closure[cl], &cval));
60e752be1aSMatthew G. Knepley           if (cval < 0) continue;
619566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(label, f, cval));
62e752be1aSMatthew G. Knepley           break;
63e752be1aSMatthew G. Knepley         }
649566063dSJacob Faibussowitsch         if (cl == clSize*2) PetscCall(DMLabelSetValue(label, f, 1));
659566063dSJacob Faibussowitsch         PetscCall(DMPlexRestoreTransitiveClosure(dm, f, PETSC_TRUE, &clSize, &closure));
66e752be1aSMatthew G. Knepley       } else {
679566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(label, f, val));
68e752be1aSMatthew G. Knepley       }
69e752be1aSMatthew G. Knepley     }
7030560a7bSMatthew G. Knepley   }
7130560a7bSMatthew G. Knepley   PetscFunctionReturn(0);
7230560a7bSMatthew G. Knepley }
7330560a7bSMatthew G. Knepley 
74cd0c2139SMatthew G Knepley /*@
75cd0c2139SMatthew G Knepley   DMPlexMarkBoundaryFaces - Mark all faces on the boundary
76cd0c2139SMatthew G Knepley 
77cd0c2139SMatthew G Knepley   Not Collective
78cd0c2139SMatthew G Knepley 
79d8d19677SJose E. Roman   Input Parameters:
80e752be1aSMatthew G. Knepley + dm - The original DM
81e752be1aSMatthew G. Knepley - val - The marker value, or PETSC_DETERMINE to use some value in the closure (or 1 if none are found)
82cd0c2139SMatthew G Knepley 
83cd0c2139SMatthew G Knepley   Output Parameter:
84e752be1aSMatthew G. Knepley . label - The DMLabel marking boundary faces with the given value
85cd0c2139SMatthew G Knepley 
86cd0c2139SMatthew G Knepley   Level: developer
87cd0c2139SMatthew G Knepley 
88c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMCreateLabel()
8909f723d9SJed Brown @*/
90e752be1aSMatthew G. Knepley PetscErrorCode DMPlexMarkBoundaryFaces(DM dm, PetscInt val, DMLabel label)
91cd0c2139SMatthew G Knepley {
92827c4036SVaclav Hapla   DMPlexInterpolatedFlag  flg;
93cd0c2139SMatthew G Knepley 
94cd0c2139SMatthew G Knepley   PetscFunctionBegin;
95827c4036SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
969566063dSJacob Faibussowitsch   PetscCall(DMPlexIsInterpolated(dm, &flg));
9708401ef6SPierre Jolivet   PetscCheck(flg == DMPLEX_INTERPOLATED_FULL,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not fully interpolated on this rank");
989566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkBoundaryFaces_Internal(dm, val, 0, label));
99cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
100cd0c2139SMatthew G Knepley }
101cd0c2139SMatthew G Knepley 
102c08575a3SMatthew G. Knepley static PetscErrorCode DMPlexLabelComplete_Internal(DM dm, DMLabel label, PetscBool completeCells)
103b0bf5782SToby Isaac {
104b0bf5782SToby Isaac   IS              valueIS;
105ac51f24eSSander Arens   PetscSF         sfPoint;
106b0bf5782SToby Isaac   const PetscInt *values;
107ac51f24eSSander Arens   PetscInt        numValues, v, cStart, cEnd, nroots;
108b0bf5782SToby Isaac 
109b0bf5782SToby Isaac   PetscFunctionBegin;
1109566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
1119566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
1129566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm,0,&cStart,&cEnd));
1139566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
114b0bf5782SToby Isaac   for (v = 0; v < numValues; ++v) {
115b0bf5782SToby Isaac     IS              pointIS;
116b0bf5782SToby Isaac     const PetscInt *points;
117b0bf5782SToby Isaac     PetscInt        numPoints, p;
118b0bf5782SToby Isaac 
1199566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
1209566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
1219566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
122b0bf5782SToby Isaac     for (p = 0; p < numPoints; ++p) {
123b0bf5782SToby Isaac       PetscInt  q = points[p];
124b0bf5782SToby Isaac       PetscInt *closure = NULL;
125b0bf5782SToby Isaac       PetscInt  closureSize, c;
126b0bf5782SToby Isaac 
127b0bf5782SToby Isaac       if (cStart <= q && q < cEnd && !completeCells) { /* skip cells */
128b0bf5782SToby Isaac         continue;
129b0bf5782SToby Isaac       }
1309566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure));
131b0bf5782SToby Isaac       for (c = 0; c < closureSize*2; c += 2) {
1329566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(label, closure[c], values[v]));
133b0bf5782SToby Isaac       }
1349566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure));
135b0bf5782SToby Isaac     }
1369566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
1379566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
138b0bf5782SToby Isaac   }
1399566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
1409566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
1419566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
1429566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL));
143ac51f24eSSander Arens   if (nroots >= 0) {
14426279d81SSanderA     DMLabel         lblRoots, lblLeaves;
14526279d81SSanderA     IS              valueIS, pointIS;
14626279d81SSanderA     const PetscInt *values;
14726279d81SSanderA     PetscInt        numValues, v;
14826279d81SSanderA 
14926279d81SSanderA     /* Pull point contributions from remote leaves into local roots */
1509566063dSJacob Faibussowitsch     PetscCall(DMLabelGather(label, sfPoint, &lblLeaves));
1519566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(lblLeaves, &valueIS));
1529566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numValues));
1539566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
15426279d81SSanderA     for (v = 0; v < numValues; ++v) {
15526279d81SSanderA       const PetscInt value = values[v];
15626279d81SSanderA 
1579566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(lblLeaves, value, &pointIS));
1589566063dSJacob Faibussowitsch       PetscCall(DMLabelInsertIS(label, pointIS, value));
1599566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
16026279d81SSanderA     }
1619566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
1629566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
1639566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&lblLeaves));
16426279d81SSanderA     /* Push point contributions from roots into remote leaves */
1659566063dSJacob Faibussowitsch     PetscCall(DMLabelDistribute(label, sfPoint, &lblRoots));
1669566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(lblRoots, &valueIS));
1679566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numValues));
1689566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
16926279d81SSanderA     for (v = 0; v < numValues; ++v) {
17026279d81SSanderA       const PetscInt value = values[v];
17126279d81SSanderA 
1729566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(lblRoots, value, &pointIS));
1739566063dSJacob Faibussowitsch       PetscCall(DMLabelInsertIS(label, pointIS, value));
1749566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
17526279d81SSanderA     }
1769566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
1779566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
1789566063dSJacob Faibussowitsch     PetscCall(DMLabelDestroy(&lblRoots));
17926279d81SSanderA   }
180b0bf5782SToby Isaac   PetscFunctionReturn(0);
181b0bf5782SToby Isaac }
182b0bf5782SToby Isaac 
1832be2b188SMatthew G Knepley /*@
1842be2b188SMatthew G Knepley   DMPlexLabelComplete - Starting with a label marking points on a surface, we add the transitive closure to the surface
1852be2b188SMatthew G Knepley 
1862be2b188SMatthew G Knepley   Input Parameters:
1872be2b188SMatthew G Knepley + dm - The DM
1882be2b188SMatthew G Knepley - label - A DMLabel marking the surface points
1892be2b188SMatthew G Knepley 
1902be2b188SMatthew G Knepley   Output Parameter:
1912be2b188SMatthew G Knepley . label - A DMLabel marking all surface points in the transitive closure
1922be2b188SMatthew G Knepley 
1932be2b188SMatthew G Knepley   Level: developer
1942be2b188SMatthew G Knepley 
1952be2b188SMatthew G Knepley .seealso: DMPlexLabelCohesiveComplete()
1962be2b188SMatthew G Knepley @*/
1972be2b188SMatthew G Knepley PetscErrorCode DMPlexLabelComplete(DM dm, DMLabel label)
1982be2b188SMatthew G Knepley {
1992be2b188SMatthew G Knepley   PetscFunctionBegin;
2009566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete_Internal(dm, label, PETSC_TRUE));
2012be2b188SMatthew G Knepley   PetscFunctionReturn(0);
2022be2b188SMatthew G Knepley }
2032be2b188SMatthew G Knepley 
2046cf0e42fSMatthew G. Knepley /*@
205a6e0b375SMatthew G. Knepley   DMPlexLabelAddCells - Starting with a label marking points on a surface, we add a cell for each point
2066cf0e42fSMatthew G. Knepley 
2076cf0e42fSMatthew G. Knepley   Input Parameters:
2086cf0e42fSMatthew G. Knepley + dm - The DM
2096cf0e42fSMatthew G. Knepley - label - A DMLabel marking the surface points
2106cf0e42fSMatthew G. Knepley 
2116cf0e42fSMatthew G. Knepley   Output Parameter:
2126cf0e42fSMatthew G. Knepley . label - A DMLabel incorporating cells
2136cf0e42fSMatthew G. Knepley 
2146cf0e42fSMatthew G. Knepley   Level: developer
2156cf0e42fSMatthew G. Knepley 
2166cf0e42fSMatthew G. Knepley   Note: The cells allow FEM boundary conditions to be applied using the cell geometry
2176cf0e42fSMatthew G. Knepley 
218a6e0b375SMatthew G. Knepley .seealso: DMPlexLabelAddFaceCells(), DMPlexLabelComplete(), DMPlexLabelCohesiveComplete()
2196cf0e42fSMatthew G. Knepley @*/
2206cf0e42fSMatthew G. Knepley PetscErrorCode DMPlexLabelAddCells(DM dm, DMLabel label)
2216cf0e42fSMatthew G. Knepley {
2226cf0e42fSMatthew G. Knepley   IS              valueIS;
2236cf0e42fSMatthew G. Knepley   const PetscInt *values;
224485ad865SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd;
2256cf0e42fSMatthew G. Knepley 
2266cf0e42fSMatthew G. Knepley   PetscFunctionBegin;
2279566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
2289566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
2299566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
2309566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
2316cf0e42fSMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
2326cf0e42fSMatthew G. Knepley     IS              pointIS;
2336cf0e42fSMatthew G. Knepley     const PetscInt *points;
2346cf0e42fSMatthew G. Knepley     PetscInt        numPoints, p;
2356cf0e42fSMatthew G. Knepley 
2369566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
2379566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
2389566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
2396cf0e42fSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2406cf0e42fSMatthew G. Knepley       PetscInt *closure = NULL;
241a6e0b375SMatthew G. Knepley       PetscInt  closureSize, cl;
2426cf0e42fSMatthew G. Knepley 
2439566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure));
24422eabd52SMatthew G. Knepley       for (cl = closureSize-1; cl > 0; --cl) {
245a6e0b375SMatthew G. Knepley         const PetscInt cell = closure[cl*2];
2469566063dSJacob Faibussowitsch         if ((cell >= cStart) && (cell < cEnd)) {PetscCall(DMLabelSetValue(label, cell, values[v])); break;}
24722eabd52SMatthew G. Knepley       }
2489566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure));
2496cf0e42fSMatthew G. Knepley     }
2509566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
2519566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
2526cf0e42fSMatthew G. Knepley   }
2539566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
2549566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
2556cf0e42fSMatthew G. Knepley   PetscFunctionReturn(0);
2566cf0e42fSMatthew G. Knepley }
2576cf0e42fSMatthew G. Knepley 
258f402d5e4SToby Isaac /*@
259a6e0b375SMatthew G. Knepley   DMPlexLabelAddFaceCells - Starting with a label marking faces on a surface, we add a cell for each face
260a6e0b375SMatthew G. Knepley 
261a6e0b375SMatthew G. Knepley   Input Parameters:
262a6e0b375SMatthew G. Knepley + dm - The DM
263a6e0b375SMatthew G. Knepley - label - A DMLabel marking the surface points
264a6e0b375SMatthew G. Knepley 
265a6e0b375SMatthew G. Knepley   Output Parameter:
266a6e0b375SMatthew G. Knepley . label - A DMLabel incorporating cells
267a6e0b375SMatthew G. Knepley 
268a6e0b375SMatthew G. Knepley   Level: developer
269a6e0b375SMatthew G. Knepley 
270a6e0b375SMatthew G. Knepley   Note: The cells allow FEM boundary conditions to be applied using the cell geometry
271a6e0b375SMatthew G. Knepley 
272a6e0b375SMatthew G. Knepley .seealso: DMPlexLabelAddCells(), DMPlexLabelComplete(), DMPlexLabelCohesiveComplete()
273a6e0b375SMatthew G. Knepley @*/
274a6e0b375SMatthew G. Knepley PetscErrorCode DMPlexLabelAddFaceCells(DM dm, DMLabel label)
275a6e0b375SMatthew G. Knepley {
276a6e0b375SMatthew G. Knepley   IS              valueIS;
277a6e0b375SMatthew G. Knepley   const PetscInt *values;
278a6e0b375SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd, fStart, fEnd;
279a6e0b375SMatthew G. Knepley 
280a6e0b375SMatthew G. Knepley   PetscFunctionBegin;
2819566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
2829566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
2839566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
2849566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
2859566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
286a6e0b375SMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
287a6e0b375SMatthew G. Knepley     IS              pointIS;
288a6e0b375SMatthew G. Knepley     const PetscInt *points;
289a6e0b375SMatthew G. Knepley     PetscInt        numPoints, p;
290a6e0b375SMatthew G. Knepley 
2919566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
2929566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
2939566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
294a6e0b375SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
295a6e0b375SMatthew G. Knepley       const PetscInt face = points[p];
296a6e0b375SMatthew G. Knepley       PetscInt      *closure = NULL;
297a6e0b375SMatthew G. Knepley       PetscInt       closureSize, cl;
298a6e0b375SMatthew G. Knepley 
299a6e0b375SMatthew G. Knepley       if ((face < fStart) || (face >= fEnd)) continue;
3009566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, face, PETSC_FALSE, &closureSize, &closure));
301a6e0b375SMatthew G. Knepley       for (cl = closureSize-1; cl > 0; --cl) {
302a6e0b375SMatthew G. Knepley         const PetscInt cell = closure[cl*2];
3039566063dSJacob Faibussowitsch         if ((cell >= cStart) && (cell < cEnd)) {PetscCall(DMLabelSetValue(label, cell, values[v])); break;}
304a6e0b375SMatthew G. Knepley       }
3059566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, face, PETSC_FALSE, &closureSize, &closure));
306a6e0b375SMatthew G. Knepley     }
3079566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
3089566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
309a6e0b375SMatthew G. Knepley   }
3109566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
3119566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
312a6e0b375SMatthew G. Knepley   PetscFunctionReturn(0);
313a6e0b375SMatthew G. Knepley }
314a6e0b375SMatthew G. Knepley 
315a6e0b375SMatthew G. Knepley /*@
316f402d5e4SToby Isaac   DMPlexLabelClearCells - Remove cells from a label
317f402d5e4SToby Isaac 
318f402d5e4SToby Isaac   Input Parameters:
319f402d5e4SToby Isaac + dm - The DM
320f402d5e4SToby Isaac - label - A DMLabel marking surface points and their adjacent cells
321f402d5e4SToby Isaac 
322f402d5e4SToby Isaac   Output Parameter:
323f402d5e4SToby Isaac . label - A DMLabel without cells
324f402d5e4SToby Isaac 
325f402d5e4SToby Isaac   Level: developer
326f402d5e4SToby Isaac 
327a6e0b375SMatthew G. Knepley   Note: This undoes DMPlexLabelAddCells() or DMPlexLabelAddFaceCells()
328f402d5e4SToby Isaac 
329f402d5e4SToby Isaac .seealso: DMPlexLabelComplete(), DMPlexLabelCohesiveComplete(), DMPlexLabelAddCells()
330f402d5e4SToby Isaac @*/
331f402d5e4SToby Isaac PetscErrorCode DMPlexLabelClearCells(DM dm, DMLabel label)
332f402d5e4SToby Isaac {
333f402d5e4SToby Isaac   IS              valueIS;
334f402d5e4SToby Isaac   const PetscInt *values;
335485ad865SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd;
336f402d5e4SToby Isaac 
337f402d5e4SToby Isaac   PetscFunctionBegin;
3389566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
3399566063dSJacob Faibussowitsch   PetscCall(DMLabelGetNumValues(label, &numValues));
3409566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
3419566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
342f402d5e4SToby Isaac   for (v = 0; v < numValues; ++v) {
343f402d5e4SToby Isaac     IS              pointIS;
344f402d5e4SToby Isaac     const PetscInt *points;
345f402d5e4SToby Isaac     PetscInt        numPoints, p;
346f402d5e4SToby Isaac 
3479566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(label, values[v], &numPoints));
3489566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
3499566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pointIS, &points));
350f402d5e4SToby Isaac     for (p = 0; p < numPoints; ++p) {
351f402d5e4SToby Isaac       PetscInt point = points[p];
352f402d5e4SToby Isaac 
353f402d5e4SToby Isaac       if (point >= cStart && point < cEnd) {
3549566063dSJacob Faibussowitsch         PetscCall(DMLabelClearValue(label,point,values[v]));
355f402d5e4SToby Isaac       }
356f402d5e4SToby Isaac     }
3579566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pointIS, &points));
3589566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
359f402d5e4SToby Isaac   }
3609566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
3619566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
362f402d5e4SToby Isaac   PetscFunctionReturn(0);
363f402d5e4SToby Isaac }
364f402d5e4SToby Isaac 
36559eef20bSToby Isaac /* take (oldEnd, added) pairs, ordered by height and convert them to (oldstart, newstart) pairs, ordered by ascending
36659eef20bSToby Isaac  * index (skipping first, which is (0,0)) */
3679fbee547SJacob Faibussowitsch static inline PetscErrorCode DMPlexShiftPointSetUp_Internal(PetscInt depth, PetscInt depthShift[])
368cd0c2139SMatthew G Knepley {
3692582d50cSToby Isaac   PetscInt d, off = 0;
3702582d50cSToby Isaac 
3712582d50cSToby Isaac   PetscFunctionBegin;
37259eef20bSToby Isaac   /* sort by (oldend): yes this is an O(n^2) sort, we expect depth <= 3 */
3730974a383SToby Isaac   for (d = 0; d < depth; d++) {
3742582d50cSToby Isaac     PetscInt firstd = d;
3750974a383SToby Isaac     PetscInt firstStart = depthShift[2*d];
3762582d50cSToby Isaac     PetscInt e;
3772582d50cSToby Isaac 
3782582d50cSToby Isaac     for (e = d+1; e <= depth; e++) {
3792582d50cSToby Isaac       if (depthShift[2*e] < firstStart) {
3802582d50cSToby Isaac         firstd = e;
3812582d50cSToby Isaac         firstStart = depthShift[2*d];
3822582d50cSToby Isaac       }
3832582d50cSToby Isaac     }
3842582d50cSToby Isaac     if (firstd != d) {
3852582d50cSToby Isaac       PetscInt swap[2];
3862582d50cSToby Isaac 
3872582d50cSToby Isaac       e = firstd;
3882582d50cSToby Isaac       swap[0] = depthShift[2*d];
3892582d50cSToby Isaac       swap[1] = depthShift[2*d+1];
3902582d50cSToby Isaac       depthShift[2*d]   = depthShift[2*e];
3912582d50cSToby Isaac       depthShift[2*d+1] = depthShift[2*e+1];
3922582d50cSToby Isaac       depthShift[2*e]   = swap[0];
3932582d50cSToby Isaac       depthShift[2*e+1] = swap[1];
3942582d50cSToby Isaac     }
3952582d50cSToby Isaac   }
3962582d50cSToby Isaac   /* convert (oldstart, added) to (oldstart, newstart) */
3970974a383SToby Isaac   for (d = 0; d <= depth; d++) {
3982582d50cSToby Isaac     off += depthShift[2*d+1];
39959eef20bSToby Isaac     depthShift[2*d+1] = depthShift[2*d] + off;
4002582d50cSToby Isaac   }
4012582d50cSToby Isaac   PetscFunctionReturn(0);
4022582d50cSToby Isaac }
4032582d50cSToby Isaac 
4042582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
4059fbee547SJacob Faibussowitsch static inline PetscInt DMPlexShiftPoint_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
4062582d50cSToby Isaac {
4072582d50cSToby Isaac   PetscInt d;
4082582d50cSToby Isaac   PetscInt newOff = 0;
4092582d50cSToby Isaac 
4102582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
4112582d50cSToby Isaac     if (p < depthShift[2*d]) return p + newOff;
4122582d50cSToby Isaac     else newOff = depthShift[2*d+1] - depthShift[2*d];
4132582d50cSToby Isaac   }
4140974a383SToby Isaac   return p + newOff;
4152582d50cSToby Isaac }
4162582d50cSToby Isaac 
4172582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
4189fbee547SJacob Faibussowitsch static inline PetscInt DMPlexShiftPointInverse_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
4192582d50cSToby Isaac {
4202582d50cSToby Isaac   PetscInt d;
4212582d50cSToby Isaac   PetscInt newOff = 0;
4222582d50cSToby Isaac 
4232582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
4242582d50cSToby Isaac     if (p < depthShift[2*d+1]) return p + newOff;
4252582d50cSToby Isaac     else newOff = depthShift[2*d] - depthShift[2*d+1];
4262582d50cSToby Isaac   }
4270974a383SToby Isaac   return p + newOff;
428cd0c2139SMatthew G Knepley }
429cd0c2139SMatthew G Knepley 
430cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftSizes_Internal(DM dm, PetscInt depthShift[], DM dmNew)
431cd0c2139SMatthew G Knepley {
432cd0c2139SMatthew G Knepley   PetscInt       depth = 0, d, pStart, pEnd, p;
433fa8e8ae5SToby Isaac   DMLabel        depthLabel;
434cd0c2139SMatthew G Knepley 
435cd0c2139SMatthew G Knepley   PetscFunctionBegin;
4369566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
437cd0c2139SMatthew G Knepley   if (depth < 0) PetscFunctionReturn(0);
438cd0c2139SMatthew G Knepley   /* Step 1: Expand chart */
4399566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
4400974a383SToby Isaac   pEnd = DMPlexShiftPoint_Internal(pEnd,depth,depthShift);
4419566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dmNew, pStart, pEnd));
4429566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dmNew,"depth"));
4439566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthLabel(dmNew,&depthLabel));
4449566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dmNew, "celltype"));
445cd0c2139SMatthew G Knepley   /* Step 2: Set cone and support sizes */
446cd0c2139SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
447fa8e8ae5SToby Isaac     PetscInt pStartNew, pEndNew;
448fa8e8ae5SToby Isaac     IS pIS;
449fa8e8ae5SToby Isaac 
4509566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd));
451fa8e8ae5SToby Isaac     pStartNew = DMPlexShiftPoint_Internal(pStart, depth, depthShift);
452fa8e8ae5SToby Isaac     pEndNew = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
4539566063dSJacob Faibussowitsch     PetscCall(ISCreateStride(PETSC_COMM_SELF, pEndNew - pStartNew, pStartNew, 1, &pIS));
4549566063dSJacob Faibussowitsch     PetscCall(DMLabelSetStratumIS(depthLabel, d, pIS));
4559566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pIS));
456cd0c2139SMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
4572582d50cSToby Isaac       PetscInt       newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
458cd0c2139SMatthew G Knepley       PetscInt       size;
459412e9a14SMatthew G. Knepley       DMPolytopeType ct;
460cd0c2139SMatthew G Knepley 
4619566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, p, &size));
4629566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(dmNew, newp, size));
4639566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, p, &size));
4649566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(dmNew, newp, size));
4659566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, p, &ct));
4669566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(dmNew, newp, ct));
467cd0c2139SMatthew G Knepley     }
468cd0c2139SMatthew G Knepley   }
469cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
470cd0c2139SMatthew G Knepley }
471cd0c2139SMatthew G Knepley 
472cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftPoints_Internal(DM dm, PetscInt depthShift[], DM dmNew)
473cd0c2139SMatthew G Knepley {
4742582d50cSToby Isaac   PetscInt      *newpoints;
4752582d50cSToby Isaac   PetscInt       depth = 0, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, pStart, pEnd, p;
476cd0c2139SMatthew G Knepley 
477cd0c2139SMatthew G Knepley   PetscFunctionBegin;
4789566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
479cd0c2139SMatthew G Knepley   if (depth < 0) PetscFunctionReturn(0);
4809566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
4819566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dmNew, &maxConeSizeNew, &maxSupportSizeNew));
4829566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(PetscMax(PetscMax(maxConeSize, maxSupportSize), PetscMax(maxConeSizeNew, maxSupportSizeNew)),&newpoints));
483cd0c2139SMatthew G Knepley   /* Step 5: Set cones and supports */
4849566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
485cd0c2139SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
486cd0c2139SMatthew G Knepley     const PetscInt *points = NULL, *orientations = NULL;
4872582d50cSToby Isaac     PetscInt        size,sizeNew, i, newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
488cd0c2139SMatthew G Knepley 
4899566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, p, &size));
4909566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, p, &points));
4919566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeOrientation(dm, p, &orientations));
492cd0c2139SMatthew G Knepley     for (i = 0; i < size; ++i) {
4932582d50cSToby Isaac       newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
494cd0c2139SMatthew G Knepley     }
4959566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(dmNew, newp, newpoints));
4969566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeOrientation(dmNew, newp, orientations));
4979566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, p, &size));
4989566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dmNew, newp, &sizeNew));
4999566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, p, &points));
500cd0c2139SMatthew G Knepley     for (i = 0; i < size; ++i) {
5012582d50cSToby Isaac       newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
502cd0c2139SMatthew G Knepley     }
503dcbb62e8SMatthew G. Knepley     for (i = size; i < sizeNew; ++i) newpoints[i] = 0;
5049566063dSJacob Faibussowitsch     PetscCall(DMPlexSetSupport(dmNew, newp, newpoints));
505cd0c2139SMatthew G Knepley   }
5069566063dSJacob Faibussowitsch   PetscCall(PetscFree(newpoints));
507cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
508cd0c2139SMatthew G Knepley }
509cd0c2139SMatthew G Knepley 
510cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftCoordinates_Internal(DM dm, PetscInt depthShift[], DM dmNew)
511cd0c2139SMatthew G Knepley {
512cd0c2139SMatthew G Knepley   PetscSection   coordSection, newCoordSection;
513cd0c2139SMatthew G Knepley   Vec            coordinates, newCoordinates;
514cd0c2139SMatthew G Knepley   PetscScalar   *coords, *newCoords;
515f2b8cce1SMatthew G. Knepley   PetscInt       coordSize, sStart, sEnd;
516f2b8cce1SMatthew G. Knepley   PetscInt       dim, depth = 0, cStart, cEnd, cStartNew, cEndNew, c, vStart, vEnd, vStartNew, vEndNew, v;
517f2b8cce1SMatthew G. Knepley   PetscBool      hasCells;
518cd0c2139SMatthew G Knepley 
519cd0c2139SMatthew G Knepley   PetscFunctionBegin;
5209566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dim));
5219566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dmNew, dim));
5229566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
523cd0c2139SMatthew G Knepley   /* Step 8: Convert coordinates */
5249566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
5259566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
5269566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dmNew, 0, &vStartNew, &vEndNew));
5279566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dmNew, 0, &cStartNew, &cEndNew));
5289566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
5299566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &newCoordSection));
5309566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(newCoordSection, 1));
5319566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(newCoordSection, 0, dim));
5329566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetChart(coordSection, &sStart, &sEnd));
533f2b8cce1SMatthew G. Knepley   hasCells = sStart == cStart ? PETSC_TRUE : PETSC_FALSE;
5349566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(newCoordSection, hasCells ? cStartNew : vStartNew, vEndNew));
535f2b8cce1SMatthew G. Knepley   if (hasCells) {
536f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
537f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof;
538f2b8cce1SMatthew G. Knepley 
5399566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, c, &dof));
5409566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(newCoordSection, cNew, dof));
5419566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(newCoordSection, cNew, 0, dof));
542f2b8cce1SMatthew G. Knepley     }
543f2b8cce1SMatthew G. Knepley   }
544cd0c2139SMatthew G Knepley   for (v = vStartNew; v < vEndNew; ++v) {
5459566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(newCoordSection, v, dim));
5469566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(newCoordSection, v, 0, dim));
547cd0c2139SMatthew G Knepley   }
5489566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(newCoordSection));
5499566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateSection(dmNew, PETSC_DETERMINE, newCoordSection));
5509566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(newCoordSection, &coordSize));
5519566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &newCoordinates));
5529566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject) newCoordinates, "coordinates"));
5539566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(newCoordinates, coordSize, PETSC_DETERMINE));
5549566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(newCoordinates, dim));
5559566063dSJacob Faibussowitsch   PetscCall(VecSetType(newCoordinates,VECSTANDARD));
5569566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmNew, newCoordinates));
5579566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
5589566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
5599566063dSJacob Faibussowitsch   PetscCall(VecGetArray(newCoordinates, &newCoords));
560f2b8cce1SMatthew G. Knepley   if (hasCells) {
561f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
562f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof, off, noff, d;
563f2b8cce1SMatthew G. Knepley 
5649566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, c, &dof));
5659566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, c, &off));
5669566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(newCoordSection, cNew, &noff));
567f2b8cce1SMatthew G. Knepley       for (d = 0; d < dof; ++d) newCoords[noff+d] = coords[off+d];
568f2b8cce1SMatthew G. Knepley     }
569f2b8cce1SMatthew G. Knepley   }
570cd0c2139SMatthew G Knepley   for (v = vStart; v < vEnd; ++v) {
571cd0c2139SMatthew G Knepley     PetscInt dof, off, noff, d;
572cd0c2139SMatthew G Knepley 
5739566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, v, &dof));
5749566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, v, &off));
5759566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(newCoordSection, DMPlexShiftPoint_Internal(v, depth, depthShift), &noff));
576f2b8cce1SMatthew G. Knepley     for (d = 0; d < dof; ++d) newCoords[noff+d] = coords[off+d];
577cd0c2139SMatthew G Knepley   }
5789566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
5799566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(newCoordinates, &newCoords));
5809566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&newCoordinates));
5819566063dSJacob Faibussowitsch   PetscCall(PetscSectionDestroy(&newCoordSection));
582cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
583cd0c2139SMatthew G Knepley }
584cd0c2139SMatthew G Knepley 
5850e33faafSMatthew G. Knepley static PetscErrorCode DMPlexShiftSF_Single(DM dm, PetscInt depthShift[], PetscSF sf, PetscSF sfNew)
586cd0c2139SMatthew G Knepley {
587cd0c2139SMatthew G Knepley   const PetscSFNode *remotePoints;
588cd0c2139SMatthew G Knepley   PetscSFNode       *gremotePoints;
589cd0c2139SMatthew G Knepley   const PetscInt    *localPoints;
590cd0c2139SMatthew G Knepley   PetscInt          *glocalPoints, *newLocation, *newRemoteLocation;
5910e33faafSMatthew G. Knepley   PetscInt           numRoots, numLeaves, l, pStart, pEnd, depth = 0, totShift = 0;
592cd0c2139SMatthew G Knepley 
593cd0c2139SMatthew G Knepley   PetscFunctionBegin;
5949566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
5959566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
5969566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sf, &numRoots, &numLeaves, &localPoints, &remotePoints));
5976e21efdcSToby Isaac   totShift = DMPlexShiftPoint_Internal(pEnd, depth, depthShift) - pEnd;
598cd0c2139SMatthew G Knepley   if (numRoots >= 0) {
5999566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(numRoots, &newLocation, pEnd-pStart, &newRemoteLocation));
6000e33faafSMatthew G. Knepley     for (l = 0; l < numRoots; ++l) newLocation[l] = DMPlexShiftPoint_Internal(l, depth, depthShift);
6019566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sf, MPIU_INT, newLocation, newRemoteLocation, MPI_REPLACE));
6029566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sf, MPIU_INT, newLocation, newRemoteLocation, MPI_REPLACE));
6039566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numLeaves, &glocalPoints));
6049566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numLeaves, &gremotePoints));
605cd0c2139SMatthew G Knepley     for (l = 0; l < numLeaves; ++l) {
6062582d50cSToby Isaac       glocalPoints[l]        = DMPlexShiftPoint_Internal(localPoints[l], depth, depthShift);
607cd0c2139SMatthew G Knepley       gremotePoints[l].rank  = remotePoints[l].rank;
608cd0c2139SMatthew G Knepley       gremotePoints[l].index = newRemoteLocation[localPoints[l]];
609cd0c2139SMatthew G Knepley     }
6109566063dSJacob Faibussowitsch     PetscCall(PetscFree2(newLocation, newRemoteLocation));
6119566063dSJacob Faibussowitsch     PetscCall(PetscSFSetGraph(sfNew, numRoots + totShift, numLeaves, glocalPoints, PETSC_OWN_POINTER, gremotePoints, PETSC_OWN_POINTER));
6120e33faafSMatthew G. Knepley   }
6130e33faafSMatthew G. Knepley   PetscFunctionReturn(0);
6140e33faafSMatthew G. Knepley }
6150e33faafSMatthew G. Knepley 
6160e33faafSMatthew G. Knepley static PetscErrorCode DMPlexShiftSF_Internal(DM dm, PetscInt depthShift[], DM dmNew)
6170e33faafSMatthew G. Knepley {
6180e33faafSMatthew G. Knepley   PetscSF        sfPoint, sfPointNew;
6190e33faafSMatthew G. Knepley   PetscBool      useNatural;
6200e33faafSMatthew G. Knepley 
6210e33faafSMatthew G. Knepley   PetscFunctionBegin;
6220e33faafSMatthew G. Knepley   /* Step 9: Convert pointSF */
6239566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
6249566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmNew, &sfPointNew));
6259566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Single(dm, depthShift, sfPoint, sfPointNew));
6260e33faafSMatthew G. Knepley   /* Step 9b: Convert naturalSF */
6279566063dSJacob Faibussowitsch   PetscCall(DMGetUseNatural(dm, &useNatural));
6280e33faafSMatthew G. Knepley   if (useNatural) {
6290e33faafSMatthew G. Knepley     PetscSF sfNat, sfNatNew;
6300e33faafSMatthew G. Knepley 
6319566063dSJacob Faibussowitsch     PetscCall(DMSetUseNatural(dmNew, useNatural));
6329566063dSJacob Faibussowitsch     PetscCall(DMGetNaturalSF(dm, &sfNat));
6339566063dSJacob Faibussowitsch     PetscCall(DMGetNaturalSF(dmNew, &sfNatNew));
6349566063dSJacob Faibussowitsch     PetscCall(DMPlexShiftSF_Single(dm, depthShift, sfNat, sfNatNew));
635cd0c2139SMatthew G Knepley   }
636cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
637cd0c2139SMatthew G Knepley }
638cd0c2139SMatthew G Knepley 
639cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftLabels_Internal(DM dm, PetscInt depthShift[], DM dmNew)
640cd0c2139SMatthew G Knepley {
641d56405f8SMatthew G. Knepley   PetscInt           depth = 0, numLabels, l;
642cd0c2139SMatthew G Knepley 
643cd0c2139SMatthew G Knepley   PetscFunctionBegin;
6449566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
645cd0c2139SMatthew G Knepley   /* Step 10: Convert labels */
6469566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &numLabels));
647cd0c2139SMatthew G Knepley   for (l = 0; l < numLabels; ++l) {
648cd0c2139SMatthew G Knepley     DMLabel         label, newlabel;
649cd0c2139SMatthew G Knepley     const char     *lname;
650fa8e8ae5SToby Isaac     PetscBool       isDepth, isDim;
651cd0c2139SMatthew G Knepley     IS              valueIS;
652cd0c2139SMatthew G Knepley     const PetscInt *values;
653cd0c2139SMatthew G Knepley     PetscInt        numValues, val;
654cd0c2139SMatthew G Knepley 
6559566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &lname));
6569566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
657cd0c2139SMatthew G Knepley     if (isDepth) continue;
6589566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "dim", &isDim));
659fa8e8ae5SToby Isaac     if (isDim) continue;
6609566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dmNew, lname));
6619566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, lname, &label));
6629566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmNew, lname, &newlabel));
6639566063dSJacob Faibussowitsch     PetscCall(DMLabelGetDefaultValue(label,&val));
6649566063dSJacob Faibussowitsch     PetscCall(DMLabelSetDefaultValue(newlabel,val));
6659566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
6669566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numValues));
6679566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
668cd0c2139SMatthew G Knepley     for (val = 0; val < numValues; ++val) {
669cd0c2139SMatthew G Knepley       IS              pointIS;
670cd0c2139SMatthew G Knepley       const PetscInt *points;
671cd0c2139SMatthew G Knepley       PetscInt        numPoints, p;
672cd0c2139SMatthew G Knepley 
6739566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, values[val], &pointIS));
6749566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &numPoints));
6759566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
676cd0c2139SMatthew G Knepley       for (p = 0; p < numPoints; ++p) {
6772582d50cSToby Isaac         const PetscInt newpoint = DMPlexShiftPoint_Internal(points[p], depth, depthShift);
678cd0c2139SMatthew G Knepley 
6799566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(newlabel, newpoint, values[val]));
680cd0c2139SMatthew G Knepley       }
6819566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(pointIS, &points));
6829566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
683cd0c2139SMatthew G Knepley     }
6849566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
6859566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
686cd0c2139SMatthew G Knepley   }
687d56405f8SMatthew G. Knepley   PetscFunctionReturn(0);
688d56405f8SMatthew G. Knepley }
689d56405f8SMatthew G. Knepley 
690d56405f8SMatthew G. Knepley static PetscErrorCode DMPlexCreateVTKLabel_Internal(DM dm, PetscBool createGhostLabel, DM dmNew)
691d56405f8SMatthew G. Knepley {
692d56405f8SMatthew G. Knepley   PetscSF            sfPoint;
693d56405f8SMatthew G. Knepley   DMLabel            vtkLabel, ghostLabel = NULL;
694d56405f8SMatthew G. Knepley   const PetscSFNode *leafRemote;
695d56405f8SMatthew G. Knepley   const PetscInt    *leafLocal;
696d56405f8SMatthew G. Knepley   PetscInt           cellHeight, cStart, cEnd, c, fStart, fEnd, f, numLeaves, l;
697d56405f8SMatthew G. Knepley   PetscMPIInt        rank;
698d56405f8SMatthew G. Knepley 
699d56405f8SMatthew G. Knepley   PetscFunctionBegin;
700cd0c2139SMatthew G Knepley    /* Step 11: Make label for output (vtk) and to mark ghost points (ghost) */
7019566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
7029566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sfPoint));
7039566063dSJacob Faibussowitsch   PetscCall(DMPlexGetVTKCellHeight(dmNew, &cellHeight));
7049566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd));
7059566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfPoint, NULL, &numLeaves, &leafLocal, &leafRemote));
7069566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dmNew, "vtk"));
7079566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dmNew, "vtk", &vtkLabel));
708d56405f8SMatthew G. Knepley   if (createGhostLabel) {
7099566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dmNew, "ghost"));
7109566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dmNew, "ghost", &ghostLabel));
711d56405f8SMatthew G. Knepley   }
712cd0c2139SMatthew G Knepley   for (l = 0, c = cStart; l < numLeaves && c < cEnd; ++l, ++c) {
713cd0c2139SMatthew G Knepley     for (; c < leafLocal[l] && c < cEnd; ++c) {
7149566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(vtkLabel, c, 1));
715cd0c2139SMatthew G Knepley     }
716cd0c2139SMatthew G Knepley     if (leafLocal[l] >= cEnd) break;
717cd0c2139SMatthew G Knepley     if (leafRemote[l].rank == rank) {
7189566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(vtkLabel, c, 1));
719d56405f8SMatthew G. Knepley     } else if (ghostLabel) {
7209566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(ghostLabel, c, 2));
721cd0c2139SMatthew G Knepley     }
722cd0c2139SMatthew G Knepley   }
723cd0c2139SMatthew G Knepley   for (; c < cEnd; ++c) {
7249566063dSJacob Faibussowitsch     PetscCall(DMLabelSetValue(vtkLabel, c, 1));
725cd0c2139SMatthew G Knepley   }
726d56405f8SMatthew G. Knepley   if (ghostLabel) {
7279566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dmNew, 1, &fStart, &fEnd));
728cd0c2139SMatthew G Knepley     for (f = fStart; f < fEnd; ++f) {
729cd0c2139SMatthew G Knepley       PetscInt numCells;
730cd0c2139SMatthew G Knepley 
7319566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dmNew, f, &numCells));
732cd0c2139SMatthew G Knepley       if (numCells < 2) {
7339566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(ghostLabel, f, 1));
734cd0c2139SMatthew G Knepley       } else {
735cd0c2139SMatthew G Knepley         const PetscInt *cells = NULL;
736cd0c2139SMatthew G Knepley         PetscInt        vA, vB;
737cd0c2139SMatthew G Knepley 
7389566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dmNew, f, &cells));
7399566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(vtkLabel, cells[0], &vA));
7409566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(vtkLabel, cells[1], &vB));
7419566063dSJacob Faibussowitsch         if (vA != 1 && vB != 1) PetscCall(DMLabelSetValue(ghostLabel, f, 1));
742cd0c2139SMatthew G Knepley       }
743cd0c2139SMatthew G Knepley     }
744d56405f8SMatthew G. Knepley   }
745cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
746cd0c2139SMatthew G Knepley }
747cd0c2139SMatthew G Knepley 
748ca04dac2SToby Isaac static PetscErrorCode DMPlexShiftTree_Internal(DM dm, PetscInt depthShift[], DM dmNew)
749ca04dac2SToby Isaac {
750ca04dac2SToby Isaac   DM             refTree;
751ca04dac2SToby Isaac   PetscSection   pSec;
752ca04dac2SToby Isaac   PetscInt       *parents, *childIDs;
753ca04dac2SToby Isaac 
754ca04dac2SToby Isaac   PetscFunctionBegin;
7559566063dSJacob Faibussowitsch   PetscCall(DMPlexGetReferenceTree(dm,&refTree));
7569566063dSJacob Faibussowitsch   PetscCall(DMPlexSetReferenceTree(dmNew,refTree));
7579566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTree(dm,&pSec,&parents,&childIDs,NULL,NULL));
758ca04dac2SToby Isaac   if (pSec) {
7592582d50cSToby Isaac     PetscInt p, pStart, pEnd, *parentsShifted, pStartShifted, pEndShifted, depth;
760fb4630b5SToby Isaac     PetscInt *childIDsShifted;
761ca04dac2SToby Isaac     PetscSection pSecShifted;
762ca04dac2SToby Isaac 
7639566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(pSec,&pStart,&pEnd));
7649566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(dm,&depth));
7652582d50cSToby Isaac     pStartShifted = DMPlexShiftPoint_Internal(pStart,depth,depthShift);
7662582d50cSToby Isaac     pEndShifted   = DMPlexShiftPoint_Internal(pEnd,depth,depthShift);
7679566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(pEndShifted - pStartShifted,&parentsShifted,pEndShifted-pStartShifted,&childIDsShifted));
7689566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dmNew),&pSecShifted));
7699566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(pSecShifted,pStartShifted,pEndShifted));
770ca04dac2SToby Isaac     for (p = pStartShifted; p < pEndShifted; p++) {
771fb4630b5SToby Isaac       /* start off assuming no children */
7729566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(pSecShifted,p,0));
773fb4630b5SToby Isaac     }
774fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
775fb4630b5SToby Isaac       PetscInt dof;
776fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p,depth,depthShift);
777ca04dac2SToby Isaac 
7789566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(pSec,p,&dof));
7799566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(pSecShifted,pNew,dof));
780ca04dac2SToby Isaac     }
7819566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(pSecShifted));
782fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
783fb4630b5SToby Isaac       PetscInt dof;
784fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p,depth,depthShift);
785fb4630b5SToby Isaac 
7869566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(pSec,p,&dof));
787fb4630b5SToby Isaac       if (dof) {
788fb4630b5SToby Isaac         PetscInt off, offNew;
789fb4630b5SToby Isaac 
7909566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(pSec,p,&off));
7919566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(pSecShifted,pNew,&offNew));
792fb4630b5SToby Isaac         parentsShifted[offNew] = DMPlexShiftPoint_Internal(parents[off],depth,depthShift);
793fb4630b5SToby Isaac         childIDsShifted[offNew] = childIDs[off];
794fb4630b5SToby Isaac       }
795fb4630b5SToby Isaac     }
7969566063dSJacob Faibussowitsch     PetscCall(DMPlexSetTree(dmNew,pSecShifted,parentsShifted,childIDsShifted));
7979566063dSJacob Faibussowitsch     PetscCall(PetscFree2(parentsShifted,childIDsShifted));
7989566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&pSecShifted));
799ca04dac2SToby Isaac   }
800ca04dac2SToby Isaac   PetscFunctionReturn(0);
801ca04dac2SToby Isaac }
802ca04dac2SToby Isaac 
803cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexConstructGhostCells_Internal(DM dm, DMLabel label, PetscInt *numGhostCells, DM gdm)
804cd0c2139SMatthew G Knepley {
805da97024aSMatthew G. Knepley   PetscSF               sf;
806cd0c2139SMatthew G Knepley   IS                    valueIS;
807da97024aSMatthew G. Knepley   const PetscInt       *values, *leaves;
808cd0c2139SMatthew G Knepley   PetscInt             *depthShift;
8092582d50cSToby Isaac   PetscInt              d, depth = 0, nleaves, loc, Ng, numFS, fs, fStart, fEnd, ghostCell, cEnd, c;
81090b157c4SStefano Zampini   PetscBool             isper;
81190b157c4SStefano Zampini   const PetscReal      *maxCell, *L;
81290b157c4SStefano Zampini   const DMBoundaryType *bd;
813cd0c2139SMatthew G Knepley 
814cd0c2139SMatthew G Knepley   PetscFunctionBegin;
8159566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
8169566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL));
817da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
8189566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
819cd0c2139SMatthew G Knepley   /* Count ghost cells */
8209566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
8219566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(valueIS, &numFS));
8229566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
8234a6cfa73SMatthew G. Knepley   Ng   = 0;
824cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
82546c796b9SMatthew G. Knepley     IS              faceIS;
82646c796b9SMatthew G. Knepley     const PetscInt *faces;
82746c796b9SMatthew G. Knepley     PetscInt        numFaces, f, numBdFaces = 0;
828cd0c2139SMatthew G Knepley 
8299566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
8309566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
8319566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
83246c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
833ca04dac2SToby Isaac       PetscInt numChildren;
834ca04dac2SToby Isaac 
8359566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
8369566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL));
837ca04dac2SToby Isaac       /* non-local and ancestors points don't get to register ghosts */
838ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
83946c796b9SMatthew G. Knepley       if ((faces[f] >= fStart) && (faces[f] < fEnd)) ++numBdFaces;
84046c796b9SMatthew G. Knepley     }
8414a6cfa73SMatthew G. Knepley     Ng += numBdFaces;
8429566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
8439566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
844cd0c2139SMatthew G Knepley   }
8459566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
8469566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(2*(depth+1), &depthShift));
8472582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
84859eef20bSToby Isaac     PetscInt dEnd;
8492582d50cSToby Isaac 
8509566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm,d,NULL,&dEnd));
85159eef20bSToby Isaac     depthShift[2*d]   = dEnd;
8522582d50cSToby Isaac     depthShift[2*d+1] = 0;
8532582d50cSToby Isaac   }
8542582d50cSToby Isaac   if (depth >= 0) depthShift[2*depth+1] = Ng;
8559566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPointSetUp_Internal(depth,depthShift));
8569566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSizes_Internal(dm, depthShift, gdm));
857cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
8589566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, NULL, &cEnd));
8594a6cfa73SMatthew G. Knepley   for (c = cEnd; c < cEnd + Ng; ++c) {
8609566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeSize(gdm, c, 1));
861cd0c2139SMatthew G Knepley   }
862cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
863cd0c2139SMatthew G Knepley     IS              faceIS;
864cd0c2139SMatthew G Knepley     const PetscInt *faces;
865cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
866cd0c2139SMatthew G Knepley 
8679566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
8689566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
8699566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
870cd0c2139SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
871ca04dac2SToby Isaac       PetscInt size, numChildren;
872cd0c2139SMatthew G Knepley 
8739566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
8749566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL));
875ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
87646c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
8779566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, faces[f], &size));
87808401ef6SPierre Jolivet       PetscCheck(size == 1,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM has boundary face %d with %d support cells", faces[f], size);
8799566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(gdm, faces[f] + Ng, 2));
880cd0c2139SMatthew G Knepley     }
8819566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
8829566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
883cd0c2139SMatthew G Knepley   }
884cd0c2139SMatthew G Knepley   /* Step 4: Setup ghosted DM */
8859566063dSJacob Faibussowitsch   PetscCall(DMSetUp(gdm));
8869566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPoints_Internal(dm, depthShift, gdm));
887cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
888cd0c2139SMatthew G Knepley   ghostCell = cEnd;
889cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
890cd0c2139SMatthew G Knepley     IS              faceIS;
891cd0c2139SMatthew G Knepley     const PetscInt *faces;
892cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
893cd0c2139SMatthew G Knepley 
8949566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
8959566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
8969566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
89746c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
898ca04dac2SToby Isaac       PetscInt newFace = faces[f] + Ng, numChildren;
899cd0c2139SMatthew G Knepley 
9009566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
9019566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL));
902ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
90346c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
9049566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(gdm, ghostCell, &newFace));
9059566063dSJacob Faibussowitsch       PetscCall(DMPlexInsertSupport(gdm, newFace, 1, ghostCell));
90646c796b9SMatthew G. Knepley       ++ghostCell;
907cd0c2139SMatthew G Knepley     }
9089566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
9099566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
910cd0c2139SMatthew G Knepley   }
9119566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
9129566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
9139566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftCoordinates_Internal(dm, depthShift, gdm));
9149566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Internal(dm, depthShift, gdm));
9159566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftLabels_Internal(dm, depthShift, gdm));
9169566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateVTKLabel_Internal(dm, PETSC_TRUE, gdm));
9179566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftTree_Internal(dm, depthShift, gdm));
9189566063dSJacob Faibussowitsch   PetscCall(PetscFree(depthShift));
919412e9a14SMatthew G. Knepley   for (c = cEnd; c < cEnd + Ng; ++c) {
9209566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCellType(gdm, c, DM_POLYTOPE_FV_GHOST));
921412e9a14SMatthew G. Knepley   }
922966c7b3fSMatthew G. Knepley   /* Step 7: Periodicity */
9239566063dSJacob Faibussowitsch   PetscCall(DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd));
9249566063dSJacob Faibussowitsch   PetscCall(DMSetPeriodicity(gdm, isper, maxCell,  L,  bd));
9254a6cfa73SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
926cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
927cd0c2139SMatthew G Knepley }
928cd0c2139SMatthew G Knepley 
929cd0c2139SMatthew G Knepley /*@C
930cd0c2139SMatthew G Knepley   DMPlexConstructGhostCells - Construct ghost cells which connect to every boundary face
931cd0c2139SMatthew G Knepley 
932cd0c2139SMatthew G Knepley   Collective on dm
933cd0c2139SMatthew G Knepley 
934cd0c2139SMatthew G Knepley   Input Parameters:
935cd0c2139SMatthew G Knepley + dm - The original DM
936cd0c2139SMatthew G Knepley - labelName - The label specifying the boundary faces, or "Face Sets" if this is NULL
937cd0c2139SMatthew G Knepley 
938cd0c2139SMatthew G Knepley   Output Parameters:
939cd0c2139SMatthew G Knepley + numGhostCells - The number of ghost cells added to the DM
940cd0c2139SMatthew G Knepley - dmGhosted - The new DM
941cd0c2139SMatthew G Knepley 
942cd0c2139SMatthew G Knepley   Note: If no label exists of that name, one will be created marking all boundary faces
943cd0c2139SMatthew G Knepley 
944cd0c2139SMatthew G Knepley   Level: developer
945cd0c2139SMatthew G Knepley 
946cd0c2139SMatthew G Knepley .seealso: DMCreate()
94731266bc0SMatthew G. Knepley @*/
948cd0c2139SMatthew G Knepley PetscErrorCode DMPlexConstructGhostCells(DM dm, const char labelName[], PetscInt *numGhostCells, DM *dmGhosted)
949cd0c2139SMatthew G Knepley {
950cd0c2139SMatthew G Knepley   DM             gdm;
951cd0c2139SMatthew G Knepley   DMLabel        label;
952cd0c2139SMatthew G Knepley   const char    *name = labelName ? labelName : "Face Sets";
953412e9a14SMatthew G. Knepley   PetscInt       dim, Ng = 0;
954b0441da4SMatthew G. Knepley   PetscBool      useCone, useClosure;
955cd0c2139SMatthew G Knepley 
956cd0c2139SMatthew G Knepley   PetscFunctionBegin;
957cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
958dadcf809SJacob Faibussowitsch   if (numGhostCells) PetscValidIntPointer(numGhostCells, 3);
959cd0c2139SMatthew G Knepley   PetscValidPointer(dmGhosted, 4);
9609566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &gdm));
9619566063dSJacob Faibussowitsch   PetscCall(DMSetType(gdm, DMPLEX));
9629566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
9639566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(gdm, dim));
9649566063dSJacob Faibussowitsch   PetscCall(DMGetBasicAdjacency(dm, &useCone, &useClosure));
9659566063dSJacob Faibussowitsch   PetscCall(DMSetBasicAdjacency(gdm, useCone,  useClosure));
9669566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
967cd0c2139SMatthew G Knepley   if (!label) {
968cd0c2139SMatthew G Knepley     /* Get label for boundary faces */
9699566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
9709566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
9719566063dSJacob Faibussowitsch     PetscCall(DMPlexMarkBoundaryFaces(dm, 1, label));
972cd0c2139SMatthew G Knepley   }
9739566063dSJacob Faibussowitsch   PetscCall(DMPlexConstructGhostCells_Internal(dm, label, &Ng, gdm));
9749566063dSJacob Faibussowitsch   PetscCall(DMCopyDisc(dm, gdm));
9759566063dSJacob Faibussowitsch   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, gdm));
976cad26855SMatthew G. Knepley   gdm->setfromoptionscalled = dm->setfromoptionscalled;
977d80ece95SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
978cd0c2139SMatthew G Knepley   *dmGhosted = gdm;
979cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
980cd0c2139SMatthew G Knepley }
981cd0c2139SMatthew G Knepley 
982607ab7a9SMatthew G. Knepley /*
983faedd622SMatthew G. Knepley   We are adding three kinds of points here:
984607ab7a9SMatthew G. Knepley     Replicated:     Copies of points which exist in the mesh, such as vertices identified across a fault
985faedd622SMatthew G. Knepley     Non-replicated: Points which exist on the fault, but are not replicated
986*b6dfa339SMatthew 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
987607ab7a9SMatthew G. Knepley     Hybrid:         Entirely new points, such as cohesive cells
988a6ae58d1SMatthew G. Knepley 
989a6ae58d1SMatthew G. Knepley   When creating subsequent cohesive cells, we shift the old hybrid cells to the end of the numbering at
990a6ae58d1SMatthew G. Knepley   each depth so that the new split/hybrid points can be inserted as a block.
991607ab7a9SMatthew G. Knepley */
9927db7e0a7SMatthew G. Knepley static PetscErrorCode DMPlexConstructCohesiveCells_Internal(DM dm, DMLabel label, DMLabel splitLabel, DM sdm)
993cd0c2139SMatthew G Knepley {
994cd0c2139SMatthew G Knepley   MPI_Comm         comm;
995607ab7a9SMatthew G. Knepley   IS               valueIS;
996607ab7a9SMatthew G. Knepley   PetscInt         numSP = 0;          /* The number of depths for which we have replicated points */
997607ab7a9SMatthew G. Knepley   const PetscInt  *values;             /* List of depths for which we have replicated points */
99818c5995bSMatthew G. Knepley   IS              *splitIS;
99918c5995bSMatthew G. Knepley   IS              *unsplitIS;
1000*b6dfa339SMatthew G. Knepley   IS               ghostIS;
1001607ab7a9SMatthew G. Knepley   PetscInt        *numSplitPoints;     /* The number of replicated points at each depth */
100218c5995bSMatthew G. Knepley   PetscInt        *numUnsplitPoints;   /* The number of non-replicated points at each depth which still give rise to hybrid points */
100336dbac82SMatthew G. Knepley   PetscInt        *numHybridPoints;    /* The number of new hybrid points at each depth */
100436dbac82SMatthew G. Knepley   PetscInt        *numHybridPointsOld; /* The number of existing hybrid points at each depth */
1005*b6dfa339SMatthew G. Knepley   PetscInt         numGhostPoints;     /* The number of unowned, shared fault faces */
1006607ab7a9SMatthew G. Knepley   const PetscInt **splitPoints;        /* Replicated points for each depth */
100718c5995bSMatthew G. Knepley   const PetscInt **unsplitPoints;      /* Non-replicated points for each depth */
1008*b6dfa339SMatthew G. Knepley   const PetscInt  *ghostPoints;        /* Ghost fault faces */
1009cd0c2139SMatthew G Knepley   PetscSection     coordSection;
1010cd0c2139SMatthew G Knepley   Vec              coordinates;
1011cd0c2139SMatthew G Knepley   PetscScalar     *coords;
1012a6ae58d1SMatthew G. Knepley   PetscInt        *depthMax;           /* The first hybrid point at each depth in the original mesh */
1013a6ae58d1SMatthew G. Knepley   PetscInt        *depthEnd;           /* The point limit at each depth in the original mesh */
1014607ab7a9SMatthew G. Knepley   PetscInt        *depthShift;         /* Number of replicated+hybrid points at each depth */
1015607ab7a9SMatthew G. Knepley   PetscInt        *pMaxNew;            /* The first replicated point at each depth in the new mesh, hybrids come after this */
1016607ab7a9SMatthew G. Knepley   PetscInt        *coneNew, *coneONew, *supportNew;
101718c5995bSMatthew G. Knepley   PetscInt         shift = 100, shift2 = 200, depth = 0, dep, dim, d, sp, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, numLabels, vStart, vEnd, pEnd, p, v;
1018cd0c2139SMatthew G Knepley 
1019cd0c2139SMatthew G Knepley   PetscFunctionBegin;
10209566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm,&comm));
10219566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
10229566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
10239566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
1024412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
10259566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(sdm, "celltype"));
1026cd0c2139SMatthew G Knepley   /* Count split points and add cohesive cells */
10279566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
10289566063dSJacob Faibussowitsch   PetscCall(PetscMalloc5(depth+1,&depthMax,depth+1,&depthEnd,2*(depth+1),&depthShift,depth+1,&pMaxNew,depth+1,&numHybridPointsOld));
10299566063dSJacob Faibussowitsch   PetscCall(PetscMalloc7(depth+1,&splitIS,depth+1,&unsplitIS,depth+1,&numSplitPoints,depth+1,&numUnsplitPoints,depth+1,&numHybridPoints,depth+1,&splitPoints,depth+1,&unsplitPoints));
1030607ab7a9SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
10319566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, NULL, &pMaxNew[d]));
10329566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, d, &depthMax[d], NULL));
1033a6ae58d1SMatthew G. Knepley     depthEnd[d]           = pMaxNew[d];
1034a6ae58d1SMatthew G. Knepley     depthMax[d]           = depthMax[d] < 0 ? depthEnd[d] : depthMax[d];
1035607ab7a9SMatthew G. Knepley     numSplitPoints[d]     = 0;
103618c5995bSMatthew G. Knepley     numUnsplitPoints[d]   = 0;
1037607ab7a9SMatthew G. Knepley     numHybridPoints[d]    = 0;
1038a6ae58d1SMatthew G. Knepley     numHybridPointsOld[d] = depthMax[d] < 0 ? 0 : depthEnd[d] - depthMax[d];
1039607ab7a9SMatthew G. Knepley     splitPoints[d]        = NULL;
104018c5995bSMatthew G. Knepley     unsplitPoints[d]      = NULL;
104118c5995bSMatthew G. Knepley     splitIS[d]            = NULL;
104218c5995bSMatthew G. Knepley     unsplitIS[d]          = NULL;
104359eef20bSToby Isaac     /* we are shifting the existing hybrid points with the stratum behind them, so
104459eef20bSToby Isaac      * the split comes at the end of the normal points, i.e., at depthMax[d] */
104559eef20bSToby Isaac     depthShift[2*d]       = depthMax[d];
104659eef20bSToby Isaac     depthShift[2*d+1]     = 0;
1047607ab7a9SMatthew G. Knepley   }
1048*b6dfa339SMatthew G. Knepley   numGhostPoints = 0;
1049*b6dfa339SMatthew G. Knepley   ghostPoints    = NULL;
1050cd0c2139SMatthew G Knepley   if (label) {
10519566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
10529566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numSP));
10539566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
1054cd0c2139SMatthew G Knepley   }
1055cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1056cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1057cd0c2139SMatthew G Knepley 
1058cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
10599566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, dep, &splitIS[dep]));
106018c5995bSMatthew G. Knepley     if (splitIS[dep]) {
10619566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(splitIS[dep], &numSplitPoints[dep]));
10629566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(splitIS[dep], &splitPoints[dep]));
106318c5995bSMatthew G. Knepley     }
10649566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, shift2+dep, &unsplitIS[dep]));
106518c5995bSMatthew G. Knepley     if (unsplitIS[dep]) {
10669566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(unsplitIS[dep], &numUnsplitPoints[dep]));
10679566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(unsplitIS[dep], &unsplitPoints[dep]));
1068cd0c2139SMatthew G Knepley     }
1069cd0c2139SMatthew G Knepley   }
1070*b6dfa339SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, shift2+dim-1, &ghostIS));
1071*b6dfa339SMatthew G. Knepley   if (ghostIS) {
1072*b6dfa339SMatthew G. Knepley     PetscCall(ISGetLocalSize(ghostIS, &numGhostPoints));
1073*b6dfa339SMatthew G. Knepley     PetscCall(ISGetIndices(ghostIS, &ghostPoints));
1074*b6dfa339SMatthew G. Knepley   }
1075607ab7a9SMatthew G. Knepley   /* Calculate number of hybrid points */
107618c5995bSMatthew 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   */
10772582d50cSToby Isaac   for (d = 0; d <= depth; ++d) depthShift[2*d+1]      = numSplitPoints[d] + numHybridPoints[d];
10789566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPointSetUp_Internal(depth,depthShift));
107959eef20bSToby Isaac   /* the end of the points in this stratum that come before the new points:
108059eef20bSToby Isaac    * shifting pMaxNew[d] gets the new start of the next stratum, then count back the old hybrid points and the newly
108159eef20bSToby Isaac    * added points */
10822582d50cSToby Isaac   for (d = 0; d <= depth; ++d) pMaxNew[d]             = DMPlexShiftPoint_Internal(pMaxNew[d],depth,depthShift) - (numHybridPointsOld[d] + numSplitPoints[d] + numHybridPoints[d]);
10839566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSizes_Internal(dm, depthShift, sdm));
1084cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
1085cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1086cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1087cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
10882582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
10894c367dbcSMatthew G. Knepley       const PetscInt  splitp = p    + pMaxNew[dep];
1090cd0c2139SMatthew G Knepley       const PetscInt *support;
1091394c2f0fSMatthew G. Knepley       DMPolytopeType  ct;
10924c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, qn, qp, e;
1093cd0c2139SMatthew G Knepley 
10949566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
10959566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(sdm, splitp, coneSize));
10969566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
10979566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(sdm, splitp, supportSize));
10989566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, oldp, &ct));
10999566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(sdm, splitp, ct));
1100cd0c2139SMatthew G Knepley       if (dep == depth-1) {
11014c367dbcSMatthew G. Knepley         const PetscInt hybcell = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
11024c367dbcSMatthew G. Knepley 
1103cd0c2139SMatthew G Knepley         /* Add cohesive cells, they are prisms */
11049566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybcell, 2 + coneSize));
1105412e9a14SMatthew G. Knepley         switch (coneSize) {
11069566063dSJacob Faibussowitsch           case 2: PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_SEG_PRISM_TENSOR));break;
11079566063dSJacob Faibussowitsch           case 3: PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_TRI_PRISM_TENSOR));break;
11089566063dSJacob Faibussowitsch           case 4: PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_QUAD_PRISM_TENSOR));break;
1109412e9a14SMatthew G. Knepley         }
1110*b6dfa339SMatthew G. Knepley         /* Shared fault faces with only one support cell now have two with the cohesive cell */
1111*b6dfa339SMatthew G. Knepley         /*   TODO Check thaat oldp has rootdegree == 1 */
1112*b6dfa339SMatthew G. Knepley         if (supportSize == 1) {
1113*b6dfa339SMatthew G. Knepley           const PetscInt *support;
1114*b6dfa339SMatthew G. Knepley           PetscInt        val;
1115*b6dfa339SMatthew G. Knepley 
1116*b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetSupport(dm, oldp, &support));
1117*b6dfa339SMatthew G. Knepley           PetscCall(DMLabelGetValue(label, support[0], &val));
1118*b6dfa339SMatthew G. Knepley           if (val < 0) PetscCall(DMPlexSetSupportSize(sdm, splitp, 2));
1119*b6dfa339SMatthew G. Knepley           else         PetscCall(DMPlexSetSupportSize(sdm, newp,   2));
1120*b6dfa339SMatthew G. Knepley         }
1121cd0c2139SMatthew G Knepley       } else if (dep == 0) {
11224c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1123cd0c2139SMatthew G Knepley 
11249566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, oldp, &support));
11254c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1126cd0c2139SMatthew G Knepley           PetscInt val;
1127cd0c2139SMatthew G Knepley 
11289566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
11294c367dbcSMatthew G. Knepley           if (val == 1) ++qf;
11304c367dbcSMatthew G. Knepley           if ((val == 1) || (val ==  (shift + 1))) ++qn;
11314c367dbcSMatthew G. Knepley           if ((val == 1) || (val == -(shift + 1))) ++qp;
1132cd0c2139SMatthew G Knepley         }
11334c367dbcSMatthew G. Knepley         /* Split old vertex: Edges into original vertex and new cohesive edge */
11349566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qn+1));
11354c367dbcSMatthew G. Knepley         /* Split new vertex: Edges into split vertex and new cohesive edge */
11369566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, splitp, qp+1));
11374c367dbcSMatthew G. Knepley         /* Add hybrid edge */
11389566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybedge, 2));
11399566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybedge, qf));
11409566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR));
1141cd0c2139SMatthew G Knepley       } else if (dep == dim-2) {
11424c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
11434c367dbcSMatthew G. Knepley 
11449566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, oldp, &support));
11454c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1146cd0c2139SMatthew G Knepley           PetscInt val;
1147cd0c2139SMatthew G Knepley 
11489566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
11494c367dbcSMatthew G. Knepley           if (val == dim-1) ++qf;
11504c367dbcSMatthew G. Knepley           if ((val == dim-1) || (val ==  (shift + dim-1))) ++qn;
11514c367dbcSMatthew G. Knepley           if ((val == dim-1) || (val == -(shift + dim-1))) ++qp;
1152cd0c2139SMatthew G Knepley         }
11534c367dbcSMatthew G. Knepley         /* Split old edge: Faces into original edge and cohesive face (positive side?) */
11549566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qn+1));
11554c367dbcSMatthew G. Knepley         /* Split new edge: Faces into split edge and cohesive face (negative side?) */
11569566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, splitp, qp+1));
11574c367dbcSMatthew G. Knepley         /* Add hybrid face */
11589566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybface, 4));
11599566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybface, qf));
11609566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR));
1161cd0c2139SMatthew G Knepley       }
1162cd0c2139SMatthew G Knepley     }
1163cd0c2139SMatthew G Knepley   }
116418c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
116518c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
116618c5995bSMatthew G. Knepley       const PetscInt  oldp   = unsplitPoints[dep][p];
11672582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
116818c5995bSMatthew G. Knepley       const PetscInt *support;
1169da1dd7e4SMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, e, s;
117018c5995bSMatthew G. Knepley 
11719566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
11729566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
11739566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
117418c5995bSMatthew G. Knepley       if (dep == 0) {
117518c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
117618c5995bSMatthew G. Knepley 
117739254ff6SMatthew G. Knepley         /* Unsplit vertex: Edges into original vertex, split edges, and new cohesive edge twice */
117839254ff6SMatthew G. Knepley         for (s = 0, qf = 0; s < supportSize; ++s, ++qf) {
11799566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSplitPoints[dep+1], splitPoints[dep+1], &e));
118039254ff6SMatthew G. Knepley           if (e >= 0) ++qf;
118139254ff6SMatthew G. Knepley         }
11829566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qf+2));
118318c5995bSMatthew G. Knepley         /* Add hybrid edge */
11849566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybedge, 2));
1185e1757548SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1186e1757548SMatthew G. Knepley           PetscInt val;
1187e1757548SMatthew G. Knepley 
11889566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1189e1757548SMatthew G. Knepley           /* Split and unsplit edges produce hybrid faces */
1190da1dd7e4SMatthew G. Knepley           if (val == 1) ++qf;
1191da1dd7e4SMatthew G. Knepley           if (val == (shift2 + 1)) ++qf;
1192e1757548SMatthew G. Knepley         }
11939566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybedge, qf));
11949566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR));
119518c5995bSMatthew G. Knepley       } else if (dep == dim-2) {
119618c5995bSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
1197cd0c2139SMatthew G Knepley         PetscInt       val;
1198cd0c2139SMatthew G Knepley 
1199da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
12009566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1201da1dd7e4SMatthew G. Knepley           if (val == dim-1) qf += 2;
1202da1dd7e4SMatthew G. Knepley           else              ++qf;
1203cd0c2139SMatthew G Knepley         }
120418c5995bSMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and cohesive face twice */
12059566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qf+2));
120618c5995bSMatthew G. Knepley         /* Add hybrid face */
1207da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
12089566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1209da1dd7e4SMatthew G. Knepley           if (val == dim-1) ++qf;
1210da1dd7e4SMatthew G. Knepley         }
12119566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybface, 4));
12129566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybface, qf));
12139566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR));
1214cd0c2139SMatthew G Knepley       }
1215cd0c2139SMatthew G Knepley     }
1216cd0c2139SMatthew G Knepley   }
1217cd0c2139SMatthew G Knepley   /* Step 4: Setup split DM */
12189566063dSJacob Faibussowitsch   PetscCall(DMSetUp(sdm));
12199566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPoints_Internal(dm, depthShift, sdm));
12209566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(sdm, &maxConeSizeNew, &maxSupportSizeNew));
12219566063dSJacob Faibussowitsch   PetscCall(PetscMalloc3(PetscMax(maxConeSize, maxConeSizeNew)*3,&coneNew,PetscMax(maxConeSize, maxConeSizeNew)*3,&coneONew,PetscMax(maxSupportSize, maxSupportSizeNew),&supportNew));
1222cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
1223cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1224cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1225cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
12262582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
12274c367dbcSMatthew G. Knepley       const PetscInt  splitp = p    + pMaxNew[dep];
1228cd0c2139SMatthew G Knepley       const PetscInt *cone, *support, *ornt;
1229b5a892a1SMatthew G. Knepley       DMPolytopeType  ct;
12304c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, q, qf, qn, qp, v, e, s;
1231cd0c2139SMatthew G Knepley 
12329566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, oldp, &ct));
12339566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
12349566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, oldp, &cone));
12359566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeOrientation(dm, oldp, &ornt));
12369566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
12379566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
1238cd0c2139SMatthew G Knepley       if (dep == depth-1) {
123996a07cd0SMatthew G. Knepley         PetscBool       hasUnsplit = PETSC_FALSE;
12404c367dbcSMatthew G. Knepley         const PetscInt  hybcell    = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1241cd0c2139SMatthew G Knepley         const PetscInt *supportF;
1242cd0c2139SMatthew G Knepley 
1243*b6dfa339SMatthew G. Knepley         coneONew[0] = coneONew[1] = -1000;
1244cd0c2139SMatthew G Knepley         /* Split face:       copy in old face to new face to start */
12459566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(sdm, newp,  &supportF));
12469566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportF));
1247cd0c2139SMatthew G Knepley         /* Split old face:   old vertices/edges in cone so no change */
1248cd0c2139SMatthew G Knepley         /* Split new face:   new vertices/edges in cone */
1249cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
12509566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v));
125118c5995bSMatthew G. Knepley           if (v < 0) {
12529566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v));
125308401ef6SPierre Jolivet             PetscCheck(v >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %d in split or unsplit points of depth %d", cone[q], dep-1);
12542582d50cSToby Isaac             coneNew[2+q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
125596a07cd0SMatthew G. Knepley             hasUnsplit   = PETSC_TRUE;
125618c5995bSMatthew G. Knepley           } else {
12574c367dbcSMatthew G. Knepley             coneNew[2+q] = v + pMaxNew[dep-1];
1258163235baSMatthew G. Knepley             if (dep > 1) {
1259163235baSMatthew G. Knepley               const PetscInt *econe;
1260163235baSMatthew G. Knepley               PetscInt        econeSize, r, vs, vu;
1261163235baSMatthew G. Knepley 
12629566063dSJacob Faibussowitsch               PetscCall(DMPlexGetConeSize(dm, cone[q], &econeSize));
12639566063dSJacob Faibussowitsch               PetscCall(DMPlexGetCone(dm, cone[q], &econe));
1264163235baSMatthew G. Knepley               for (r = 0; r < econeSize; ++r) {
12659566063dSJacob Faibussowitsch                 PetscCall(PetscFindInt(econe[r], numSplitPoints[dep-2],   splitPoints[dep-2],   &vs));
12669566063dSJacob Faibussowitsch                 PetscCall(PetscFindInt(econe[r], numUnsplitPoints[dep-2], unsplitPoints[dep-2], &vu));
1267163235baSMatthew G. Knepley                 if (vs >= 0) continue;
126808401ef6SPierre Jolivet                 PetscCheck(vu >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %d in split or unsplit points of depth %d", econe[r], dep-2);
1269163235baSMatthew G. Knepley                 hasUnsplit   = PETSC_TRUE;
1270163235baSMatthew G. Knepley               }
1271163235baSMatthew G. Knepley             }
1272cd0c2139SMatthew G Knepley           }
1273cd0c2139SMatthew G Knepley         }
12749566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, splitp, &coneNew[2]));
12759566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(sdm, splitp, ornt));
1276e537020bSMatthew G. Knepley         /* Face support */
1277*b6dfa339SMatthew G. Knepley         PetscInt vals[2];
1278cd0c2139SMatthew G Knepley 
1279*b6dfa339SMatthew G. Knepley         PetscCall(DMLabelGetValue(label, support[0], &vals[0]));
1280*b6dfa339SMatthew G. Knepley         if (supportSize > 1) PetscCall(DMLabelGetValue(label, support[1], &vals[1]));
1281*b6dfa339SMatthew G. Knepley         else                 vals[1] = -vals[0];
1282*b6dfa339SMatthew 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]);
1283*b6dfa339SMatthew G. Knepley 
1284*b6dfa339SMatthew G. Knepley         for (s = 0; s < 2; ++s) {
1285*b6dfa339SMatthew G. Knepley           if (s >= supportSize) {
1286*b6dfa339SMatthew G. Knepley             if (vals[s] < 0) {
1287*b6dfa339SMatthew G. Knepley               /* Ghost old face:   Replace negative side cell with cohesive cell */
1288*b6dfa339SMatthew G. Knepley               PetscCall(DMPlexInsertSupport(sdm, newp,   0, hybcell));
1289*b6dfa339SMatthew G. Knepley             } else {
1290*b6dfa339SMatthew G. Knepley               /* Ghost new face:   Replace positive side cell with cohesive cell */
1291*b6dfa339SMatthew G. Knepley               PetscCall(DMPlexInsertSupport(sdm, splitp, 0, hybcell));
1292*b6dfa339SMatthew G. Knepley             }
1293*b6dfa339SMatthew G. Knepley           } else {
1294*b6dfa339SMatthew G. Knepley             if (vals[s] < 0) {
1295cd0c2139SMatthew G Knepley               /* Split old face:   Replace negative side cell with cohesive cell */
12969566063dSJacob Faibussowitsch               PetscCall(DMPlexInsertSupport(sdm, newp,   s, hybcell));
1297cd0c2139SMatthew G Knepley             } else {
1298cd0c2139SMatthew G Knepley               /* Split new face:   Replace positive side cell with cohesive cell */
12999566063dSJacob Faibussowitsch               PetscCall(DMPlexInsertSupport(sdm, splitp, s, hybcell));
1300*b6dfa339SMatthew G. Knepley             }
1301*b6dfa339SMatthew G. Knepley           }
1302*b6dfa339SMatthew G. Knepley         }
1303*b6dfa339SMatthew G. Knepley         /* Get orientation for cohesive face using the positive side cell */
1304e537020bSMatthew G. Knepley         {
1305e537020bSMatthew G. Knepley           const PetscInt *ncone, *nconeO;
1306*b6dfa339SMatthew G. Knepley           PetscInt        nconeSize, nc, ocell;
1307*b6dfa339SMatthew G. Knepley           PetscBool       flip;
1308e537020bSMatthew G. Knepley 
1309*b6dfa339SMatthew G. Knepley           if (supportSize > 1) {ocell = vals[0] < 0 ? support[1] : support[0];}
1310*b6dfa339SMatthew G. Knepley           else                 {ocell = support[0]; flip = vals[0] < 0 ? PETSC_TRUE : PETSC_FALSE;}
1311*b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetConeSize(dm, ocell, &nconeSize));
1312*b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetCone(dm, ocell, &ncone));
13139566063dSJacob Faibussowitsch           PetscCall(DMPlexGetConeOrientation(dm, support[s], &nconeO));
1314e537020bSMatthew G. Knepley           for (nc = 0; nc < nconeSize; ++nc) {
1315e537020bSMatthew G. Knepley             if (ncone[nc] == oldp) {
1316*b6dfa339SMatthew G. Knepley               coneONew[0] = flip ? -(nconeO[nc]+1) : nconeO[nc];
1317e537020bSMatthew G. Knepley               break;
1318cd0c2139SMatthew G Knepley             }
1319cd0c2139SMatthew G Knepley           }
1320*b6dfa339SMatthew 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);
1321e537020bSMatthew G. Knepley         }
13224c367dbcSMatthew G. Knepley         /* Cohesive cell:    Old and new split face, then new cohesive faces */
1323*b6dfa339SMatthew G. Knepley         {
1324*b6dfa339SMatthew G. Knepley           const PetscInt No = DMPolytopeTypeGetNumArrangments(ct)/2;
1325*b6dfa339SMatthew G. Knepley           PetscCheck((coneONew[0] >= -No) && (coneONew[0] < No), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid %s orientation %" PetscInt_FMT, DMPolytopeTypes[ct], coneONew[0]);
1326*b6dfa339SMatthew G. Knepley         }
1327b5a892a1SMatthew G. Knepley         const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, coneONew[0]);
1328b5a892a1SMatthew G. Knepley 
1329fd4b9f15SMatthew G. Knepley         coneNew[0]  = newp;   /* Extracted negative side orientation above */
13304c367dbcSMatthew G. Knepley         coneNew[1]  = splitp;
13314c367dbcSMatthew G. Knepley         coneONew[1] = coneONew[0];
1332e537020bSMatthew G. Knepley         for (q = 0; q < coneSize; ++q) {
1333412e9a14SMatthew G. Knepley           /* Hybrid faces must follow order from oriented end face */
1334b5a892a1SMatthew G. Knepley           const PetscInt qa = arr[q*2+0];
1335b5a892a1SMatthew G. Knepley           const PetscInt qo = arr[q*2+1];
1336b5a892a1SMatthew G. Knepley           DMPolytopeType ft = dep == 2 ? DM_POLYTOPE_SEGMENT : DM_POLYTOPE_POINT;
1337412e9a14SMatthew G. Knepley 
13389566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[qa], numSplitPoints[dep-1], splitPoints[dep-1], &v));
133918c5995bSMatthew G. Knepley           if (v < 0) {
13409566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[qa], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v));
134118c5995bSMatthew G. Knepley             coneNew[2+q]  = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
134218c5995bSMatthew G. Knepley           } else {
134318c5995bSMatthew G. Knepley             coneNew[2+q]  = v + pMaxNew[dep] + numSplitPoints[dep];
134418c5995bSMatthew G. Knepley           }
1345b5a892a1SMatthew G. Knepley           coneONew[2+q] = DMPolytopeTypeComposeOrientation(ft, qo, ornt[qa]);
1346e537020bSMatthew G. Knepley         }
13479566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybcell, coneNew));
13489566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(sdm, hybcell, coneONew));
134996a07cd0SMatthew G. Knepley         /* Label the hybrid cells on the boundary of the split */
13509566063dSJacob Faibussowitsch         if (hasUnsplit) PetscCall(DMLabelSetValue(label, -hybcell, dim));
1351cd0c2139SMatthew G Knepley       } else if (dep == 0) {
13524c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1353cd0c2139SMatthew G Knepley 
1354cd0c2139SMatthew G Knepley         /* Split old vertex: Edges in old split faces and new cohesive edge */
13554c367dbcSMatthew G. Knepley         for (e = 0, qn = 0; e < supportSize; ++e) {
1356cd0c2139SMatthew G Knepley           PetscInt val;
1357cd0c2139SMatthew G Knepley 
13589566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1359cd0c2139SMatthew G Knepley           if ((val == 1) || (val == (shift + 1))) {
13602582d50cSToby Isaac             supportNew[qn++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1361cd0c2139SMatthew G Knepley           }
1362cd0c2139SMatthew G Knepley         }
13634c367dbcSMatthew G. Knepley         supportNew[qn] = hybedge;
13649566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1365cd0c2139SMatthew G Knepley         /* Split new vertex: Edges in new split faces and new cohesive edge */
13664c367dbcSMatthew G. Knepley         for (e = 0, qp = 0; e < supportSize; ++e) {
1367cd0c2139SMatthew G Knepley           PetscInt val, edge;
1368cd0c2139SMatthew G Knepley 
13699566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1370cd0c2139SMatthew G Knepley           if (val == 1) {
13719566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge));
137208401ef6SPierre Jolivet             PetscCheck(edge >= 0,comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
13734c367dbcSMatthew G. Knepley             supportNew[qp++] = edge + pMaxNew[dep+1];
1374cd0c2139SMatthew G Knepley           } else if (val == -(shift + 1)) {
13752582d50cSToby Isaac             supportNew[qp++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1376cd0c2139SMatthew G Knepley           }
1377cd0c2139SMatthew G Knepley         }
13784c367dbcSMatthew G. Knepley         supportNew[qp] = hybedge;
13799566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportNew));
13804c367dbcSMatthew G. Knepley         /* Hybrid edge:    Old and new split vertex */
1381cd0c2139SMatthew G Knepley         coneNew[0] = newp;
1382cd0c2139SMatthew G Knepley         coneNew[1] = splitp;
13839566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybedge, coneNew));
13844c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
13854c367dbcSMatthew G. Knepley           PetscInt val, edge;
13864c367dbcSMatthew G. Knepley 
13879566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
13884c367dbcSMatthew G. Knepley           if (val == 1) {
13899566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge));
139008401ef6SPierre Jolivet             PetscCheck(edge >= 0,comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
13914c367dbcSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2];
13924c367dbcSMatthew G. Knepley           }
13934c367dbcSMatthew G. Knepley         }
13949566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybedge, supportNew));
1395cd0c2139SMatthew G Knepley       } else if (dep == dim-2) {
13964c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
13974c367dbcSMatthew G. Knepley 
1398cd0c2139SMatthew G Knepley         /* Split old edge:   old vertices in cone so no change */
1399cd0c2139SMatthew G Knepley         /* Split new edge:   new vertices in cone */
1400cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
14019566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v));
1402e1757548SMatthew G. Knepley           if (v < 0) {
14039566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v));
140408401ef6SPierre Jolivet             PetscCheck(v >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %d in split or unsplit points of depth %d", cone[q], dep-1);
14052582d50cSToby Isaac             coneNew[q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
1406e1757548SMatthew G. Knepley           } else {
14074c367dbcSMatthew G. Knepley             coneNew[q] = v + pMaxNew[dep-1];
1408cd0c2139SMatthew G Knepley           }
1409e1757548SMatthew G. Knepley         }
14109566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, splitp, coneNew));
1411cd0c2139SMatthew G Knepley         /* Split old edge: Faces in positive side cells and old split faces */
1412cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1413cd0c2139SMatthew G Knepley           PetscInt val;
1414cd0c2139SMatthew G Knepley 
14159566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
14164c367dbcSMatthew G. Knepley           if (val == dim-1) {
14172582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
14184c367dbcSMatthew G. Knepley           } else if (val == (shift + dim-1)) {
14192582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1420cd0c2139SMatthew G Knepley           }
1421cd0c2139SMatthew G Knepley         }
1422b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
14239566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1424cd0c2139SMatthew G Knepley         /* Split new edge: Faces in negative side cells and new split faces */
1425cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1426cd0c2139SMatthew G Knepley           PetscInt val, face;
1427cd0c2139SMatthew G Knepley 
14289566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1429cd0c2139SMatthew G Knepley           if (val == dim-1) {
14309566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &face));
143108401ef6SPierre Jolivet             PetscCheck(face >= 0,comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[e]);
14324c367dbcSMatthew G. Knepley             supportNew[q++] = face + pMaxNew[dep+1];
1433cd0c2139SMatthew G Knepley           } else if (val == -(shift + dim-1)) {
14342582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1435cd0c2139SMatthew G Knepley           }
1436cd0c2139SMatthew G Knepley         }
1437b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
14389566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportNew));
14394c367dbcSMatthew G. Knepley         /* Hybrid face */
14404c367dbcSMatthew G. Knepley         coneNew[0] = newp;
14414c367dbcSMatthew G. Knepley         coneNew[1] = splitp;
14424c367dbcSMatthew G. Knepley         for (v = 0; v < coneSize; ++v) {
14434c367dbcSMatthew G. Knepley           PetscInt vertex;
14449566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[v], numSplitPoints[dep-1], splitPoints[dep-1], &vertex));
1445e1757548SMatthew G. Knepley           if (vertex < 0) {
14469566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[v], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &vertex));
144708401ef6SPierre Jolivet             PetscCheck(vertex >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %d in split or unsplit points of depth %d", cone[v], dep-1);
1448e1757548SMatthew G. Knepley             coneNew[2+v] = vertex + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1449e1757548SMatthew G. Knepley           } else {
14504c367dbcSMatthew G. Knepley             coneNew[2+v] = vertex + pMaxNew[dep] + numSplitPoints[dep];
14514c367dbcSMatthew G. Knepley           }
1452e1757548SMatthew G. Knepley         }
14539566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybface, coneNew));
14544c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
14554c367dbcSMatthew G. Knepley           PetscInt val, face;
14564c367dbcSMatthew G. Knepley 
14579566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
14584c367dbcSMatthew G. Knepley           if (val == dim-1) {
14599566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &face));
146008401ef6SPierre Jolivet             PetscCheck(face >= 0,comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[e]);
14614c367dbcSMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+2] + numSplitPoints[dep+2];
14624c367dbcSMatthew G. Knepley           }
14634c367dbcSMatthew G. Knepley         }
14649566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybface, supportNew));
1465cd0c2139SMatthew G Knepley       }
1466cd0c2139SMatthew G Knepley     }
1467cd0c2139SMatthew G Knepley   }
146818c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
146918c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
147018c5995bSMatthew G. Knepley       const PetscInt  oldp   = unsplitPoints[dep][p];
14712582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
1472b5a892a1SMatthew G. Knepley       const PetscInt *cone, *support;
1473e1757548SMatthew G. Knepley       PetscInt        coneSize, supportSize, supportSizeNew, q, qf, e, f, s;
147418c5995bSMatthew G. Knepley 
14759566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
14769566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, oldp, &cone));
14779566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
14789566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
147918c5995bSMatthew G. Knepley       if (dep == 0) {
148018c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
148118c5995bSMatthew G. Knepley 
148218c5995bSMatthew G. Knepley         /* Unsplit vertex */
14839566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, newp, &supportSizeNew));
148418c5995bSMatthew G. Knepley         for (s = 0, q = 0; s < supportSize; ++s) {
14852582d50cSToby Isaac           supportNew[q++] = DMPlexShiftPoint_Internal(support[s], depth, depthShift) /*support[s] + depthOffset[dep+1]*/;
14869566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSplitPoints[dep+1], splitPoints[dep+1], &e));
148718c5995bSMatthew G. Knepley           if (e >= 0) {
148818c5995bSMatthew G. Knepley             supportNew[q++] = e + pMaxNew[dep+1];
148918c5995bSMatthew G. Knepley           }
149018c5995bSMatthew G. Knepley         }
149118c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
149218c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
149308401ef6SPierre Jolivet         PetscCheck(q == supportSizeNew,comm, PETSC_ERR_ARG_WRONG, "Support size %d != %d for vertex %d", q, supportSizeNew, newp);
14949566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
149518c5995bSMatthew G. Knepley         /* Hybrid edge */
149618c5995bSMatthew G. Knepley         coneNew[0] = newp;
149718c5995bSMatthew G. Knepley         coneNew[1] = newp;
14989566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybedge, coneNew));
149918c5995bSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
150018c5995bSMatthew G. Knepley           PetscInt val, edge;
150118c5995bSMatthew G. Knepley 
15029566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
150318c5995bSMatthew G. Knepley           if (val == 1) {
15049566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge));
150508401ef6SPierre Jolivet             PetscCheck(edge >= 0,comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
150618c5995bSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2];
1507e1757548SMatthew G. Knepley           } else if  (val ==  (shift2 + 1)) {
15089566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numUnsplitPoints[dep+1], unsplitPoints[dep+1], &edge));
150908401ef6SPierre Jolivet             PetscCheck(edge >= 0,comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a unsplit edge", support[e]);
1510e1757548SMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2] + numSplitPoints[dep+1];
151118c5995bSMatthew G. Knepley           }
151218c5995bSMatthew G. Knepley         }
15139566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybedge, supportNew));
1514e1757548SMatthew G. Knepley       } else if (dep == dim-2) {
1515e1757548SMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
1516e1757548SMatthew G. Knepley 
1517da1dd7e4SMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and hybrid face twice */
1518e1757548SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1519e1757548SMatthew G. Knepley           PetscInt val, face;
1520e1757548SMatthew G. Knepley 
15219566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[f], &val));
1522e1757548SMatthew G. Knepley           if (val == dim-1) {
15239566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[f], numSplitPoints[dep+1], splitPoints[dep+1], &face));
152408401ef6SPierre Jolivet             PetscCheck(face >= 0,comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[f]);
15252582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1526e1757548SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+1];
1527e1757548SMatthew G. Knepley           } else {
15282582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1529e1757548SMatthew G. Knepley           }
1530e1757548SMatthew G. Knepley         }
1531e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
1532e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
15339566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, newp, &supportSizeNew));
153408401ef6SPierre Jolivet         PetscCheck(qf == supportSizeNew,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Support size for unsplit edge %d is %d != %d", newp, qf, supportSizeNew);
15359566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1536e1757548SMatthew G. Knepley         /* Add hybrid face */
1537e1757548SMatthew G. Knepley         coneNew[0] = newp;
1538212cc919SMatthew G. Knepley         coneNew[1] = newp;
15399566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[0], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v));
154008401ef6SPierre Jolivet         PetscCheck(v >= 0,comm, PETSC_ERR_ARG_WRONG, "Vertex %d is not an unsplit vertex", cone[0]);
1541212cc919SMatthew G. Knepley         coneNew[2] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
15429566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[1], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v));
154308401ef6SPierre Jolivet         PetscCheck(v >= 0,comm, PETSC_ERR_ARG_WRONG, "Vertex %d is not an unsplit vertex", cone[1]);
1544e1757548SMatthew G. Knepley         coneNew[3] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
15459566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybface, coneNew));
1546da1dd7e4SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1547da1dd7e4SMatthew G. Knepley           PetscInt val, face;
1548da1dd7e4SMatthew G. Knepley 
15499566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[f], &val));
1550da1dd7e4SMatthew G. Knepley           if (val == dim-1) {
15519566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[f], numSplitPoints[dep+1], splitPoints[dep+1], &face));
1552da1dd7e4SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+2] + numSplitPoints[dep+2];
1553da1dd7e4SMatthew G. Knepley           }
1554da1dd7e4SMatthew G. Knepley         }
15559566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, hybface, &supportSizeNew));
155608401ef6SPierre Jolivet         PetscCheck(qf == supportSizeNew,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Support size for hybrid face %d is %d != %d", hybface, qf, supportSizeNew);
15579566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybface, supportNew));
1558cd0c2139SMatthew G Knepley       }
1559cd0c2139SMatthew G Knepley     }
1560cd0c2139SMatthew G Knepley   }
1561cd0c2139SMatthew G Knepley   /* Step 6b: Replace split points in negative side cones */
1562cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1563cd0c2139SMatthew G Knepley     PetscInt        dep = values[sp];
1564cd0c2139SMatthew G Knepley     IS              pIS;
1565cd0c2139SMatthew G Knepley     PetscInt        numPoints;
1566cd0c2139SMatthew G Knepley     const PetscInt *points;
1567cd0c2139SMatthew G Knepley 
1568cd0c2139SMatthew G Knepley     if (dep >= 0) continue;
15699566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, dep, &pIS));
1570cd0c2139SMatthew G Knepley     if (!pIS) continue;
1571cd0c2139SMatthew G Knepley     dep  = -dep - shift;
15729566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(pIS, &numPoints));
15739566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pIS, &points));
1574cd0c2139SMatthew G Knepley     for (p = 0; p < numPoints; ++p) {
1575cd0c2139SMatthew G Knepley       const PetscInt  oldp = points[p];
15762582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*depthOffset[dep] + oldp*/;
1577cd0c2139SMatthew G Knepley       const PetscInt *cone;
1578cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
157950cf782dSMatthew G. Knepley       /* PetscBool       replaced = PETSC_FALSE; */
1580cd0c2139SMatthew G Knepley 
1581cd0c2139SMatthew G Knepley       /* Negative edge: replace split vertex */
1582cd0c2139SMatthew G Knepley       /* Negative cell: replace split face */
15839566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(sdm, newp, &coneSize));
15849566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(sdm, newp, &cone));
1585cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
1586e38fbfedSToby Isaac         const PetscInt coldp = DMPlexShiftPointInverse_Internal(cone[c],depth,depthShift);
1587cd0c2139SMatthew G Knepley         PetscInt       csplitp, cp, val;
1588cd0c2139SMatthew G Knepley 
15899566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, coldp, &val));
1590cd0c2139SMatthew G Knepley         if (val == dep-1) {
15919566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(coldp, numSplitPoints[dep-1], splitPoints[dep-1], &cp));
159208401ef6SPierre Jolivet           PetscCheck(cp >= 0,comm, PETSC_ERR_ARG_WRONG, "Point %d is not a split point of dimension %d", oldp, dep-1);
1593cd0c2139SMatthew G Knepley           csplitp  = pMaxNew[dep-1] + cp;
15949566063dSJacob Faibussowitsch           PetscCall(DMPlexInsertCone(sdm, newp, c, csplitp));
159550cf782dSMatthew G. Knepley           /* replaced = PETSC_TRUE; */
1596cd0c2139SMatthew G Knepley         }
1597cd0c2139SMatthew G Knepley       }
15984a189a86SMatthew G. Knepley       /* Cells with only a vertex or edge on the submesh have no replacement */
159928b400f6SJacob Faibussowitsch       /* PetscCheck(replaced,comm, PETSC_ERR_ARG_WRONG, "The cone of point %d does not contain split points", oldp); */
1600cd0c2139SMatthew G Knepley     }
16019566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pIS, &points));
16029566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pIS));
1603cd0c2139SMatthew G Knepley   }
1604fa8e8ae5SToby Isaac   /* Step 7: Coordinates */
16059566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftCoordinates_Internal(dm, depthShift, sdm));
16069566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(sdm, &coordSection));
16079566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(sdm, &coordinates));
16089566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
1609cd0c2139SMatthew G Knepley   for (v = 0; v < (numSplitPoints ? numSplitPoints[0] : 0); ++v) {
16102582d50cSToby Isaac     const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[0][v], depth, depthShift) /*depthOffset[0] + splitPoints[0][v]*/;
1611cd0c2139SMatthew G Knepley     const PetscInt splitp = pMaxNew[0] + v;
1612cd0c2139SMatthew G Knepley     PetscInt       dof, off, soff, d;
1613cd0c2139SMatthew G Knepley 
16149566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, newp, &dof));
16159566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, newp, &off));
16169566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, splitp, &soff));
1617cd0c2139SMatthew G Knepley     for (d = 0; d < dof; ++d) coords[soff+d] = coords[off+d];
1618cd0c2139SMatthew G Knepley   }
16199566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
1620fa8e8ae5SToby Isaac   /* Step 8: SF, if I can figure this out we can split the mesh in parallel */
16219566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Internal(dm, depthShift, sdm));
1622*b6dfa339SMatthew G. Knepley   /*   TODO We need to associate the ghost points with the correct replica */
1623fa8e8ae5SToby Isaac   /* Step 9: Labels */
16249566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftLabels_Internal(dm, depthShift, sdm));
16259566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateVTKLabel_Internal(dm, PETSC_FALSE, sdm));
16269566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(sdm, &numLabels));
1627cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1628cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
16292582d50cSToby Isaac       const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[dep][p], depth, depthShift) /*depthOffset[dep] + splitPoints[dep][p]*/;
1630cd0c2139SMatthew G Knepley       const PetscInt splitp = pMaxNew[dep] + p;
1631cd0c2139SMatthew G Knepley       PetscInt       l;
1632cd0c2139SMatthew G Knepley 
16337db7e0a7SMatthew G. Knepley       if (splitLabel) {
16347db7e0a7SMatthew G. Knepley         const PetscInt val = 100 + dep;
16357db7e0a7SMatthew G. Knepley 
16369566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(splitLabel, newp,    val));
16379566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(splitLabel, splitp, -val));
16387db7e0a7SMatthew G. Knepley       }
1639cd0c2139SMatthew G Knepley       for (l = 0; l < numLabels; ++l) {
1640cd0c2139SMatthew G Knepley         DMLabel     mlabel;
1641cd0c2139SMatthew G Knepley         const char *lname;
1642cd0c2139SMatthew G Knepley         PetscInt    val;
16439a356370SMatthew G. Knepley         PetscBool   isDepth;
1644cd0c2139SMatthew G Knepley 
16459566063dSJacob Faibussowitsch         PetscCall(DMGetLabelName(sdm, l, &lname));
16469566063dSJacob Faibussowitsch         PetscCall(PetscStrcmp(lname, "depth", &isDepth));
16479a356370SMatthew G. Knepley         if (isDepth) continue;
16489566063dSJacob Faibussowitsch         PetscCall(DMGetLabel(sdm, lname, &mlabel));
16499566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(mlabel, newp, &val));
1650cd0c2139SMatthew G Knepley         if (val >= 0) {
16519566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(mlabel, splitp, val));
1652cd0c2139SMatthew G Knepley         }
1653cd0c2139SMatthew G Knepley       }
1654cd0c2139SMatthew G Knepley     }
1655cd0c2139SMatthew G Knepley   }
1656cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1657cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1658cd0c2139SMatthew G Knepley 
1659cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
16609566063dSJacob Faibussowitsch     if (splitIS[dep]) PetscCall(ISRestoreIndices(splitIS[dep], &splitPoints[dep]));
16619566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&splitIS[dep]));
16629566063dSJacob Faibussowitsch     if (unsplitIS[dep]) PetscCall(ISRestoreIndices(unsplitIS[dep], &unsplitPoints[dep]));
16639566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&unsplitIS[dep]));
1664cd0c2139SMatthew G Knepley   }
1665*b6dfa339SMatthew G. Knepley   if (ghostIS) PetscCall(ISRestoreIndices(ghostIS, &ghostPoints));
1666*b6dfa339SMatthew G. Knepley   PetscCall(ISDestroy(&ghostIS));
1667cd0c2139SMatthew G Knepley   if (label) {
16689566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
16699566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
1670cd0c2139SMatthew G Knepley   }
16710d4d4d06SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
16729566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(sdm, d, NULL, &pEnd));
167336dbac82SMatthew G. Knepley     pMaxNew[d] = pEnd - numHybridPoints[d] - numHybridPointsOld[d];
16740d4d4d06SMatthew G. Knepley   }
16759566063dSJacob Faibussowitsch   PetscCall(PetscFree3(coneNew, coneONew, supportNew));
16769566063dSJacob Faibussowitsch   PetscCall(PetscFree5(depthMax, depthEnd, depthShift, pMaxNew, numHybridPointsOld));
16779566063dSJacob Faibussowitsch   PetscCall(PetscFree7(splitIS, unsplitIS, numSplitPoints, numUnsplitPoints, numHybridPoints, splitPoints, unsplitPoints));
1678cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1679cd0c2139SMatthew G Knepley }
1680cd0c2139SMatthew G Knepley 
1681cd0c2139SMatthew G Knepley /*@C
1682cd0c2139SMatthew G Knepley   DMPlexConstructCohesiveCells - Construct cohesive cells which split the face along an internal interface
1683cd0c2139SMatthew G Knepley 
1684cd0c2139SMatthew G Knepley   Collective on dm
1685cd0c2139SMatthew G Knepley 
1686cd0c2139SMatthew G Knepley   Input Parameters:
1687cd0c2139SMatthew G Knepley + dm - The original DM
168853156dfcSMatthew G. Knepley - label - The label specifying the boundary faces (this could be auto-generated)
1689cd0c2139SMatthew G Knepley 
1690cd0c2139SMatthew G Knepley   Output Parameters:
16917db7e0a7SMatthew G. Knepley + splitLabel - The label containing the split points, or NULL if no output is desired
1692cd0c2139SMatthew G Knepley - dmSplit - The new DM
1693cd0c2139SMatthew G Knepley 
1694cd0c2139SMatthew G Knepley   Level: developer
1695cd0c2139SMatthew G Knepley 
16962be2b188SMatthew G Knepley .seealso: DMCreate(), DMPlexLabelCohesiveComplete()
1697cd0c2139SMatthew G Knepley @*/
16987db7e0a7SMatthew G. Knepley PetscErrorCode DMPlexConstructCohesiveCells(DM dm, DMLabel label, DMLabel splitLabel, DM *dmSplit)
1699cd0c2139SMatthew G Knepley {
1700cd0c2139SMatthew G Knepley   DM             sdm;
1701cd0c2139SMatthew G Knepley   PetscInt       dim;
1702cd0c2139SMatthew G Knepley 
1703cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1704cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1705064a246eSJacob Faibussowitsch   PetscValidPointer(dmSplit, 4);
17069566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &sdm));
17079566063dSJacob Faibussowitsch   PetscCall(DMSetType(sdm, DMPLEX));
17089566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
17099566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(sdm, dim));
1710cd0c2139SMatthew G Knepley   switch (dim) {
1711cd0c2139SMatthew G Knepley   case 2:
1712cd0c2139SMatthew G Knepley   case 3:
17139566063dSJacob Faibussowitsch     PetscCall(DMPlexConstructCohesiveCells_Internal(dm, label, splitLabel, sdm));
1714cd0c2139SMatthew G Knepley     break;
1715cd0c2139SMatthew G Knepley   default:
171698921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct cohesive cells for dimension %d", dim);
1717cd0c2139SMatthew G Knepley   }
1718cd0c2139SMatthew G Knepley   *dmSplit = sdm;
1719cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1720cd0c2139SMatthew G Knepley }
1721cd0c2139SMatthew G Knepley 
17220f66a230SMatthew G. Knepley /* Returns the side of the surface for a given cell with a face on the surface */
17230f66a230SMatthew G. Knepley static PetscErrorCode GetSurfaceSide_Static(DM dm, DM subdm, PetscInt numSubpoints, const PetscInt *subpoints, PetscInt cell, PetscInt face, PetscBool *pos)
17240f66a230SMatthew G. Knepley {
17250f66a230SMatthew G. Knepley   const PetscInt *cone, *ornt;
17260f66a230SMatthew G. Knepley   PetscInt        dim, coneSize, c;
17270f66a230SMatthew G. Knepley 
17280f66a230SMatthew G. Knepley   PetscFunctionBegin;
17290f66a230SMatthew G. Knepley   *pos = PETSC_TRUE;
17309566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
17319566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
17329566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCone(dm, cell, &cone));
17339566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeOrientation(dm, cell, &ornt));
17340f66a230SMatthew G. Knepley   for (c = 0; c < coneSize; ++c) {
17350f66a230SMatthew G. Knepley     if (cone[c] == face) {
17360f66a230SMatthew G. Knepley       PetscInt o = ornt[c];
17370f66a230SMatthew G. Knepley 
17380f66a230SMatthew G. Knepley       if (subdm) {
17390f66a230SMatthew G. Knepley         const PetscInt *subcone, *subornt;
17400f66a230SMatthew G. Knepley         PetscInt        subpoint, subface, subconeSize, sc;
17410f66a230SMatthew G. Knepley 
17429566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cell, numSubpoints, subpoints, &subpoint));
17439566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(face, numSubpoints, subpoints, &subface));
17449566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeSize(subdm, subpoint, &subconeSize));
17459566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCone(subdm, subpoint, &subcone));
17469566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeOrientation(subdm, subpoint, &subornt));
17470f66a230SMatthew G. Knepley         for (sc = 0; sc < subconeSize; ++sc) {
17480f66a230SMatthew G. Knepley           if (subcone[sc] == subface) {
17490f66a230SMatthew G. Knepley             o = subornt[0];
17500f66a230SMatthew G. Knepley             break;
17510f66a230SMatthew G. Knepley           }
17520f66a230SMatthew G. Knepley         }
175308401ef6SPierre Jolivet         PetscCheck(sc < subconeSize,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find subpoint %d (%d) in cone for subpoint %d (%d)", subface, face, subpoint, cell);
17540f66a230SMatthew G. Knepley       }
17550f66a230SMatthew G. Knepley       if (o >= 0) *pos = PETSC_TRUE;
17560f66a230SMatthew G. Knepley       else        *pos = PETSC_FALSE;
17570f66a230SMatthew G. Knepley       break;
17580f66a230SMatthew G. Knepley     }
17590f66a230SMatthew G. Knepley   }
176008401ef6SPierre Jolivet   PetscCheck(c != coneSize,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Cell %d in split face %d support does not have it in the cone", cell, face);
17610f66a230SMatthew G. Knepley   PetscFunctionReturn(0);
17620f66a230SMatthew G. Knepley }
17630f66a230SMatthew G. Knepley 
1764cd0c2139SMatthew G Knepley /*@
17650f66a230SMatthew G. Knepley   DMPlexLabelCohesiveComplete - Starting with a label marking points on an internal surface, we add all other mesh pieces
1766cd0c2139SMatthew G Knepley   to complete the surface
1767cd0c2139SMatthew G Knepley 
1768cd0c2139SMatthew G Knepley   Input Parameters:
1769cd0c2139SMatthew G Knepley + dm     - The DM
17700f66a230SMatthew G. Knepley . label  - A DMLabel marking the surface
17710f66a230SMatthew G. Knepley . blabel - A DMLabel marking the vertices on the boundary which will not be duplicated, or NULL to find them automatically
1772bb55d314SMatthew G. Knepley . flip   - Flag to flip the submesh normal and replace points on the other side
177347946fd8SMatthew G. Knepley - subdm  - The subDM associated with the label, or NULL
1774cd0c2139SMatthew G Knepley 
1775cd0c2139SMatthew G Knepley   Output Parameter:
1776cd0c2139SMatthew G Knepley . label - A DMLabel marking all surface points
1777cd0c2139SMatthew G Knepley 
17780f66a230SMatthew G. Knepley   Note: The vertices in blabel are called "unsplit" in the terminology from hybrid cell creation.
17790f66a230SMatthew G. Knepley 
1780cd0c2139SMatthew G Knepley   Level: developer
1781cd0c2139SMatthew G Knepley 
17822be2b188SMatthew G Knepley .seealso: DMPlexConstructCohesiveCells(), DMPlexLabelComplete()
1783cd0c2139SMatthew G Knepley @*/
17840f66a230SMatthew G. Knepley PetscErrorCode DMPlexLabelCohesiveComplete(DM dm, DMLabel label, DMLabel blabel, PetscBool flip, DM subdm)
1785cd0c2139SMatthew G Knepley {
1786d90583fdSMatthew G. Knepley   DMLabel         depthLabel;
17878630cb1aSMatthew G. Knepley   IS              dimIS, subpointIS = NULL, facePosIS, faceNegIS, crossEdgeIS = NULL;
178847946fd8SMatthew G. Knepley   const PetscInt *points, *subpoints;
1789bb55d314SMatthew G. Knepley   const PetscInt  rev   = flip ? -1 : 1;
1790*b6dfa339SMatthew G. Knepley   PetscInt        shift = 100, shift2 = 200, shift3 = 300, dim, depth, dep, cStart, cEnd, vStart, vEnd, numPoints, numSubpoints, p, val;
1791cd0c2139SMatthew G Knepley 
1792cd0c2139SMatthew G Knepley   PetscFunctionBegin;
17939566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
17949566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
17959566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
179647946fd8SMatthew G. Knepley   if (subdm) {
17979566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(subdm, &subpointIS));
179847946fd8SMatthew G. Knepley     if (subpointIS) {
17999566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(subpointIS, &numSubpoints));
18009566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subpointIS, &subpoints));
180147946fd8SMatthew G. Knepley     }
180247946fd8SMatthew G. Knepley   }
1803d7c8f101SMatthew G. Knepley   /* Mark cell on the fault, and its faces which touch the fault: cell orientation for face gives the side of the fault */
18049566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, dim-1, &dimIS));
180597d8846cSMatthew Knepley   if (!dimIS) PetscFunctionReturn(0);
18069566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(dimIS, &numPoints));
18079566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(dimIS, &points));
1808d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault faces */
1809cd0c2139SMatthew G Knepley     const PetscInt *support;
1810cd0c2139SMatthew G Knepley     PetscInt        supportSize, s;
1811cd0c2139SMatthew G Knepley 
18129566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, points[p], &supportSize));
1813c4419245SMatthew G. Knepley #if 0
1814c4419245SMatthew G. Knepley     if (supportSize != 2) {
1815c4419245SMatthew G. Knepley       const PetscInt *lp;
1816c4419245SMatthew G. Knepley       PetscInt        Nlp, pind;
1817c4419245SMatthew G. Knepley 
1818c4419245SMatthew G. Knepley       /* Check that for a cell with a single support face, that face is in the SF */
1819c4419245SMatthew G. Knepley       /*   THis check only works for the remote side. We would need root side information */
18209566063dSJacob Faibussowitsch       PetscCall(PetscSFGetGraph(dm->sf, NULL, &Nlp, &lp, NULL));
18219566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(points[p], Nlp, lp, &pind));
182208401ef6SPierre Jolivet       PetscCheck(pind >= 0,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Split face %d has %d != 2 supports, and the face is not shared with another process", points[p], supportSize);
1823c4419245SMatthew G. Knepley     }
1824c4419245SMatthew G. Knepley #endif
18259566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, points[p], &support));
1826cd0c2139SMatthew G Knepley     for (s = 0; s < supportSize; ++s) {
18270f66a230SMatthew G. Knepley       const PetscInt *cone;
1828cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
18290f66a230SMatthew G. Knepley       PetscBool       pos;
1830cd0c2139SMatthew G Knepley 
18319566063dSJacob Faibussowitsch       PetscCall(GetSurfaceSide_Static(dm, subdm, numSubpoints, subpoints, support[s], points[p], &pos));
18329566063dSJacob Faibussowitsch       if (pos) PetscCall(DMLabelSetValue(label, support[s],  rev*(shift+dim)));
18339566063dSJacob Faibussowitsch       else     PetscCall(DMLabelSetValue(label, support[s], -rev*(shift+dim)));
18340f66a230SMatthew G. Knepley       if (rev < 0) pos = !pos ? PETSC_TRUE : PETSC_FALSE;
18350f66a230SMatthew G. Knepley       /* Put faces touching the fault in the label */
18369566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, support[s], &coneSize));
18379566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, support[s], &cone));
1838cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
1839cd0c2139SMatthew G Knepley         const PetscInt point = cone[c];
1840cd0c2139SMatthew G Knepley 
18419566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
1842cd0c2139SMatthew G Knepley         if (val == -1) {
1843cd0c2139SMatthew G Knepley           PetscInt *closure = NULL;
1844cd0c2139SMatthew G Knepley           PetscInt  closureSize, cl;
1845cd0c2139SMatthew G Knepley 
18469566063dSJacob Faibussowitsch           PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
1847cd0c2139SMatthew G Knepley           for (cl = 0; cl < closureSize*2; cl += 2) {
1848cd0c2139SMatthew G Knepley             const PetscInt clp  = closure[cl];
1849a0541d8aSMatthew G. Knepley             PetscInt       bval = -1;
1850cd0c2139SMatthew G Knepley 
18519566063dSJacob Faibussowitsch             PetscCall(DMLabelGetValue(label, clp, &val));
18529566063dSJacob Faibussowitsch             if (blabel) PetscCall(DMLabelGetValue(blabel, clp, &bval));
1853a0541d8aSMatthew G. Knepley             if ((val >= 0) && (val < dim-1) && (bval < 0)) {
18549566063dSJacob Faibussowitsch               PetscCall(DMLabelSetValue(label, point, pos == PETSC_TRUE ? shift+dim-1 : -(shift+dim-1)));
1855cd0c2139SMatthew G Knepley               break;
1856cd0c2139SMatthew G Knepley             }
1857cd0c2139SMatthew G Knepley           }
18589566063dSJacob Faibussowitsch           PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
1859cd0c2139SMatthew G Knepley         }
1860cd0c2139SMatthew G Knepley       }
1861cd0c2139SMatthew G Knepley     }
1862cd0c2139SMatthew G Knepley   }
1863a0541d8aSMatthew G. Knepley   /* Mark boundary points as unsplit */
186486200784SMatthew G. Knepley   if (blabel) {
18659566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(blabel, 1, &dimIS));
18669566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(dimIS, &numPoints));
18679566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(dimIS, &points));
1868a0541d8aSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
1869a0541d8aSMatthew G. Knepley       const PetscInt point = points[p];
1870a0541d8aSMatthew G. Knepley       PetscInt       val, bval;
1871a0541d8aSMatthew G. Knepley 
18729566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(blabel, point, &bval));
1873a0541d8aSMatthew G. Knepley       if (bval >= 0) {
18749566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
1875f7019248SMatthew G. Knepley         if ((val < 0) || (val > dim)) {
1876f7019248SMatthew G. Knepley           /* This could be a point added from splitting a vertex on an adjacent fault, otherwise its just wrong */
18779566063dSJacob Faibussowitsch           PetscCall(DMLabelClearValue(blabel, point, bval));
1878f7019248SMatthew G. Knepley         }
1879f7019248SMatthew G. Knepley       }
1880f7019248SMatthew G. Knepley     }
1881f7019248SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
1882f7019248SMatthew G. Knepley       const PetscInt point = points[p];
1883f7019248SMatthew G. Knepley       PetscInt       val, bval;
1884f7019248SMatthew G. Knepley 
18859566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(blabel, point, &bval));
1886f7019248SMatthew G. Knepley       if (bval >= 0) {
188786200784SMatthew G. Knepley         const PetscInt *cone,    *support;
188886200784SMatthew G. Knepley         PetscInt        coneSize, supportSize, s, valA, valB, valE;
188986200784SMatthew G. Knepley 
1890a0541d8aSMatthew G. Knepley         /* Mark as unsplit */
18919566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
189208401ef6SPierre Jolivet         PetscCheck(!(val < 0) && !(val > dim),PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %d has label value %d, should be part of the fault", point, val);
18939566063dSJacob Faibussowitsch         PetscCall(DMLabelClearValue(label, point, val));
18949566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(label, point, shift2+val));
18952c06a818SMatthew G. Knepley         /* Check for cross-edge
18962c06a818SMatthew G. Knepley              A cross-edge has endpoints which are both on the boundary of the surface, but the edge itself is not. */
189786200784SMatthew G. Knepley         if (val != 0) continue;
18989566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, point, &support));
18999566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
190086200784SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
19019566063dSJacob Faibussowitsch           PetscCall(DMPlexGetCone(dm, support[s], &cone));
19029566063dSJacob Faibussowitsch           PetscCall(DMPlexGetConeSize(dm, support[s], &coneSize));
190308401ef6SPierre Jolivet           PetscCheck(coneSize == 2,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Edge %D has %D vertices != 2", support[s], coneSize);
19049566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, cone[0], &valA));
19059566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, cone[1], &valB));
19069566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, support[s], &valE));
19079566063dSJacob Faibussowitsch           if ((valE < 0) && (valA >= 0) && (valB >= 0) && (cone[0] != cone[1])) PetscCall(DMLabelSetValue(blabel, support[s], 2));
190886200784SMatthew G. Knepley         }
1909a0541d8aSMatthew G. Knepley       }
1910a0541d8aSMatthew G. Knepley     }
19119566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(dimIS, &points));
19129566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&dimIS));
1913a0541d8aSMatthew G. Knepley   }
1914*b6dfa339SMatthew G. Knepley   /* Mark ghost fault cells */
1915*b6dfa339SMatthew G. Knepley   {
1916*b6dfa339SMatthew G. Knepley     PetscSF         sf;
1917*b6dfa339SMatthew G. Knepley     const PetscInt *leaves;
1918*b6dfa339SMatthew G. Knepley     PetscInt         Nl, l;
1919*b6dfa339SMatthew G. Knepley 
1920*b6dfa339SMatthew G. Knepley     PetscCall(DMGetPointSF(dm, &sf));
1921*b6dfa339SMatthew G. Knepley     PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL));
1922*b6dfa339SMatthew G. Knepley     if (Nl > 0) {
1923*b6dfa339SMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
1924*b6dfa339SMatthew G. Knepley         const PetscInt point = points[p];
1925*b6dfa339SMatthew G. Knepley         PetscInt       val;
1926*b6dfa339SMatthew G. Knepley 
1927*b6dfa339SMatthew G. Knepley         PetscCall(PetscFindInt(point, Nl, leaves, &l));
1928*b6dfa339SMatthew G. Knepley         if (l >= 0) {
1929*b6dfa339SMatthew G. Knepley           PetscInt *closure = NULL;
1930*b6dfa339SMatthew G. Knepley           PetscInt  closureSize, cl;
1931*b6dfa339SMatthew G. Knepley 
1932*b6dfa339SMatthew G. Knepley           PetscCall(DMLabelGetValue(label, point, &val));
1933*b6dfa339SMatthew 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);
1934*b6dfa339SMatthew G. Knepley           PetscCall(DMLabelClearValue(label, point, val));
1935*b6dfa339SMatthew G. Knepley           PetscCall(DMLabelSetValue(label, point, shift3+val));
1936*b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
1937*b6dfa339SMatthew G. Knepley           for (cl = 0; cl < closureSize*2; cl += 2) {
1938*b6dfa339SMatthew G. Knepley             const PetscInt clp  = closure[cl];
1939*b6dfa339SMatthew G. Knepley 
1940*b6dfa339SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, clp, &val));
1941*b6dfa339SMatthew 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);
1942*b6dfa339SMatthew G. Knepley             PetscCall(DMLabelClearValue(label, clp, val));
1943*b6dfa339SMatthew G. Knepley             PetscCall(DMLabelSetValue(label, clp, shift3+val));
1944*b6dfa339SMatthew G. Knepley           }
1945*b6dfa339SMatthew G. Knepley           PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
1946*b6dfa339SMatthew G. Knepley         }
1947*b6dfa339SMatthew G. Knepley       }
1948*b6dfa339SMatthew G. Knepley     }
1949*b6dfa339SMatthew G. Knepley   }
1950*b6dfa339SMatthew G. Knepley   PetscCall(ISRestoreIndices(dimIS, &points));
1951*b6dfa339SMatthew G. Knepley   PetscCall(ISDestroy(&dimIS));
1952*b6dfa339SMatthew G. Knepley   if (subpointIS) PetscCall(ISRestoreIndices(subpointIS, &subpoints));
1953cd0c2139SMatthew G Knepley   /* Search for other cells/faces/edges connected to the fault by a vertex */
19549566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
19559566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
19569566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, 0, &dimIS));
19577d2fefb6SMatthew G. Knepley   /* TODO Why are we including cross edges here? Shouldn't they be in the star of boundary vertices? */
19589566063dSJacob Faibussowitsch   if (blabel) PetscCall(DMLabelGetStratumIS(blabel, 2, &crossEdgeIS));
195986200784SMatthew G. Knepley   if (dimIS && crossEdgeIS) {
196086200784SMatthew G. Knepley     IS vertIS = dimIS;
196186200784SMatthew G. Knepley 
19629566063dSJacob Faibussowitsch     PetscCall(ISExpand(vertIS, crossEdgeIS, &dimIS));
19639566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&crossEdgeIS));
19649566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&vertIS));
196586200784SMatthew G. Knepley   }
1966dc86033cSMatthew G. Knepley   if (!dimIS) {
1967dc86033cSMatthew G. Knepley     PetscFunctionReturn(0);
1968dc86033cSMatthew G. Knepley   }
19699566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(dimIS, &numPoints));
19709566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(dimIS, &points));
1971d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault vertices */
1972cd0c2139SMatthew G Knepley     PetscInt *star = NULL;
197386200784SMatthew G. Knepley     PetscInt  starSize, s;
1974cd0c2139SMatthew G Knepley     PetscInt  again = 1;  /* 0: Finished 1: Keep iterating after a change 2: No change */
1975cd0c2139SMatthew G Knepley 
1976d4622b2cSMatthew G. Knepley     /* All points connected to the fault are inside a cell, so at the top level we will only check cells */
19779566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &starSize, &star));
1978cd0c2139SMatthew G Knepley     while (again) {
197908401ef6SPierre Jolivet       PetscCheck(again <= 1,PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Could not classify all cells connected to the fault");
1980cd0c2139SMatthew G Knepley       again = 0;
1981cd0c2139SMatthew G Knepley       for (s = 0; s < starSize*2; s += 2) {
1982cd0c2139SMatthew G Knepley         const PetscInt  point = star[s];
1983cd0c2139SMatthew G Knepley         const PetscInt *cone;
1984cd0c2139SMatthew G Knepley         PetscInt        coneSize, c;
1985cd0c2139SMatthew G Knepley 
1986412e9a14SMatthew G. Knepley         if ((point < cStart) || (point >= cEnd)) continue;
19879566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
1988cd0c2139SMatthew G Knepley         if (val != -1) continue;
1989d4622b2cSMatthew G. Knepley         again = again == 1 ? 1 : 2;
19909566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeSize(dm, point, &coneSize));
19919566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCone(dm, point, &cone));
1992cd0c2139SMatthew G Knepley         for (c = 0; c < coneSize; ++c) {
19939566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, cone[c], &val));
1994cd0c2139SMatthew G Knepley           if (val != -1) {
1995d7c8f101SMatthew G. Knepley             const PetscInt *ccone;
1996166d9d0cSMatthew G. Knepley             PetscInt        cconeSize, cc, side;
1997d7c8f101SMatthew G. Knepley 
199808401ef6SPierre Jolivet             PetscCheck(PetscAbs(val) >= shift,PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %d on cell %d has an invalid label %d", cone[c], point, val);
199937a6de01SMatthew G. Knepley             if (val > 0) side =  1;
200037a6de01SMatthew G. Knepley             else         side = -1;
20019566063dSJacob Faibussowitsch             PetscCall(DMLabelSetValue(label, point, side*(shift+dim)));
2002d7c8f101SMatthew G. Knepley             /* Mark cell faces which touch the fault */
20039566063dSJacob Faibussowitsch             PetscCall(DMPlexGetConeSize(dm, point, &cconeSize));
20049566063dSJacob Faibussowitsch             PetscCall(DMPlexGetCone(dm, point, &ccone));
2005d7c8f101SMatthew G. Knepley             for (cc = 0; cc < cconeSize; ++cc) {
2006d7c8f101SMatthew G. Knepley               PetscInt *closure = NULL;
2007d7c8f101SMatthew G. Knepley               PetscInt  closureSize, cl;
2008d7c8f101SMatthew G. Knepley 
20099566063dSJacob Faibussowitsch               PetscCall(DMLabelGetValue(label, ccone[cc], &val));
2010166d9d0cSMatthew G. Knepley               if (val != -1) continue;
20119566063dSJacob Faibussowitsch               PetscCall(DMPlexGetTransitiveClosure(dm, ccone[cc], PETSC_TRUE, &closureSize, &closure));
2012d4622b2cSMatthew G. Knepley               for (cl = 0; cl < closureSize*2; cl += 2) {
2013d7c8f101SMatthew G. Knepley                 const PetscInt clp = closure[cl];
2014d7c8f101SMatthew G. Knepley 
20159566063dSJacob Faibussowitsch                 PetscCall(DMLabelGetValue(label, clp, &val));
2016d7c8f101SMatthew G. Knepley                 if (val == -1) continue;
20179566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(label, ccone[cc], side*(shift+dim-1)));
201837a6de01SMatthew G. Knepley                 break;
201937a6de01SMatthew G. Knepley               }
20209566063dSJacob Faibussowitsch               PetscCall(DMPlexRestoreTransitiveClosure(dm, ccone[cc], PETSC_TRUE, &closureSize, &closure));
2021cd0c2139SMatthew G Knepley             }
2022cd0c2139SMatthew G Knepley             again = 1;
2023cd0c2139SMatthew G Knepley             break;
2024cd0c2139SMatthew G Knepley           }
2025cd0c2139SMatthew G Knepley         }
2026cd0c2139SMatthew G Knepley       }
2027cd0c2139SMatthew G Knepley     }
2028cd0c2139SMatthew G Knepley     /* Classify the rest by cell membership */
2029cd0c2139SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
2030cd0c2139SMatthew G Knepley       const PetscInt point = star[s];
2031cd0c2139SMatthew G Knepley 
20329566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, point, &val));
2033cd0c2139SMatthew G Knepley       if (val == -1) {
2034cd0c2139SMatthew G Knepley         PetscInt      *sstar = NULL;
2035cd0c2139SMatthew G Knepley         PetscInt       sstarSize, ss;
2036412e9a14SMatthew G. Knepley         PetscBool      marked = PETSC_FALSE, isHybrid;
2037cd0c2139SMatthew G Knepley 
20389566063dSJacob Faibussowitsch         PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &sstarSize, &sstar));
2039cd0c2139SMatthew G Knepley         for (ss = 0; ss < sstarSize*2; ss += 2) {
2040cd0c2139SMatthew G Knepley           const PetscInt spoint = sstar[ss];
2041cd0c2139SMatthew G Knepley 
2042412e9a14SMatthew G. Knepley           if ((spoint < cStart) || (spoint >= cEnd)) continue;
20439566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, spoint, &val));
204408401ef6SPierre Jolivet           PetscCheck(val != -1,PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %d in star of %d does not have a valid label", spoint, point);
20459566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(depthLabel, point, &dep));
2046cd0c2139SMatthew G Knepley           if (val > 0) {
20479566063dSJacob Faibussowitsch             PetscCall(DMLabelSetValue(label, point,   shift+dep));
2048cd0c2139SMatthew G Knepley           } else {
20499566063dSJacob Faibussowitsch             PetscCall(DMLabelSetValue(label, point, -(shift+dep)));
2050cd0c2139SMatthew G Knepley           }
2051cd0c2139SMatthew G Knepley           marked = PETSC_TRUE;
2052cd0c2139SMatthew G Knepley           break;
2053cd0c2139SMatthew G Knepley         }
20549566063dSJacob Faibussowitsch         PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &sstarSize, &sstar));
20559566063dSJacob Faibussowitsch         PetscCall(DMPlexCellIsHybrid_Internal(dm, point, &isHybrid));
205608401ef6SPierre Jolivet         PetscCheck(isHybrid || marked,PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Point %d could not be classified", point);
2057cd0c2139SMatthew G Knepley       }
2058cd0c2139SMatthew G Knepley     }
20599566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &starSize, &star));
2060cd0c2139SMatthew G Knepley   }
20619566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(dimIS, &points));
20629566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&dimIS));
20637d2fefb6SMatthew G. Knepley   /* If any faces touching the fault divide cells on either side, split them
20647d2fefb6SMatthew G. Knepley        This only occurs without a surface boundary */
20659566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label,   shift+dim-1,  &facePosIS));
20669566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, -(shift+dim-1), &faceNegIS));
20679566063dSJacob Faibussowitsch   PetscCall(ISExpand(facePosIS, faceNegIS, &dimIS));
20689566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&facePosIS));
20699566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&faceNegIS));
20709566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(dimIS, &numPoints));
20719566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(dimIS, &points));
207218c5995bSMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
207318c5995bSMatthew G. Knepley     const PetscInt  point = points[p];
207418c5995bSMatthew G. Knepley     const PetscInt *support;
207518c5995bSMatthew G. Knepley     PetscInt        supportSize, valA, valB;
207618c5995bSMatthew G. Knepley 
20779566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
207818c5995bSMatthew G. Knepley     if (supportSize != 2) continue;
20799566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, point, &support));
20809566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValue(label, support[0], &valA));
20819566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValue(label, support[1], &valB));
208218c5995bSMatthew G. Knepley     if ((valA == -1) || (valB == -1)) continue;
208318c5995bSMatthew G. Knepley     if (valA*valB > 0) continue;
208418c5995bSMatthew G. Knepley     /* Split the face */
20859566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValue(label, point, &valA));
20869566063dSJacob Faibussowitsch     PetscCall(DMLabelClearValue(label, point, valA));
20879566063dSJacob Faibussowitsch     PetscCall(DMLabelSetValue(label, point, dim-1));
2088e1757548SMatthew G. Knepley     /* Label its closure:
2089e1757548SMatthew G. Knepley       unmarked: label as unsplit
2090e1757548SMatthew G. Knepley       incident: relabel as split
2091e1757548SMatthew G. Knepley       split:    do nothing
2092e1757548SMatthew G. Knepley     */
209318c5995bSMatthew G. Knepley     {
209418c5995bSMatthew G. Knepley       PetscInt *closure = NULL;
209518c5995bSMatthew G. Knepley       PetscInt  closureSize, cl;
209618c5995bSMatthew G. Knepley 
20979566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
209818c5995bSMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
20999566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, closure[cl], &valA));
21008771c1d3SMatthew G. Knepley         if (valA == -1) { /* Mark as unsplit */
21019566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(depthLabel, closure[cl], &dep));
21029566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(label, closure[cl], shift2+dep));
21038771c1d3SMatthew G. Knepley         } else if (((valA >= shift) && (valA < shift2)) || ((valA <= -shift) && (valA > -shift2))) {
21049566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(depthLabel, closure[cl], &dep));
21059566063dSJacob Faibussowitsch           PetscCall(DMLabelClearValue(label, closure[cl], valA));
21069566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(label, closure[cl], dep));
2107e1757548SMatthew G. Knepley         }
210818c5995bSMatthew G. Knepley       }
21099566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
211018c5995bSMatthew G. Knepley     }
211118c5995bSMatthew G. Knepley   }
21129566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(dimIS, &points));
21139566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&dimIS));
2114cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
2115cd0c2139SMatthew G Knepley }
2116cd0c2139SMatthew G Knepley 
2117720e594eSMatthew G. Knepley /* Check that no cell have all vertices on the fault */
2118720e594eSMatthew G. Knepley PetscErrorCode DMPlexCheckValidSubmesh_Private(DM dm, DMLabel label, DM subdm)
2119720e594eSMatthew G. Knepley {
2120720e594eSMatthew G. Knepley   IS              subpointIS;
2121720e594eSMatthew G. Knepley   const PetscInt *dmpoints;
2122720e594eSMatthew G. Knepley   PetscInt        defaultValue, cStart, cEnd, c, vStart, vEnd;
2123720e594eSMatthew G. Knepley 
2124720e594eSMatthew G. Knepley   PetscFunctionBegin;
2125720e594eSMatthew G. Knepley   if (!label) PetscFunctionReturn(0);
21269566063dSJacob Faibussowitsch   PetscCall(DMLabelGetDefaultValue(label, &defaultValue));
21279566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointIS(subdm, &subpointIS));
2128720e594eSMatthew G. Knepley   if (!subpointIS) PetscFunctionReturn(0);
21299566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(subdm, 0, &cStart, &cEnd));
21309566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
21319566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(subpointIS, &dmpoints));
2132720e594eSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2133720e594eSMatthew G. Knepley     PetscBool invalidCell = PETSC_TRUE;
2134720e594eSMatthew G. Knepley     PetscInt *closure     = NULL;
2135720e594eSMatthew G. Knepley     PetscInt  closureSize, cl;
2136720e594eSMatthew G. Knepley 
21379566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure));
2138720e594eSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
2139720e594eSMatthew G. Knepley       PetscInt value = 0;
2140720e594eSMatthew G. Knepley 
2141720e594eSMatthew G. Knepley       if ((closure[cl] < vStart) || (closure[cl] >= vEnd)) continue;
21429566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, closure[cl], &value));
2143720e594eSMatthew G. Knepley       if (value == defaultValue) {invalidCell = PETSC_FALSE; break;}
2144720e594eSMatthew G. Knepley     }
21459566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure));
2146720e594eSMatthew G. Knepley     if (invalidCell) {
21479566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(subpointIS, &dmpoints));
21489566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&subpointIS));
21499566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&subdm));
215098921bdaSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ambiguous submesh. Cell %D has all of its vertices on the submesh.", dmpoints[c]);
2151720e594eSMatthew G. Knepley     }
2152720e594eSMatthew G. Knepley   }
21539566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(subpointIS, &dmpoints));
2154720e594eSMatthew G. Knepley   PetscFunctionReturn(0);
2155720e594eSMatthew G. Knepley }
2156720e594eSMatthew G. Knepley 
2157c08575a3SMatthew G. Knepley /*@
21583cf72582SMatthew G. Knepley   DMPlexCreateHybridMesh - Create a mesh with hybrid cells along an internal interface
21593cf72582SMatthew G. Knepley 
21603cf72582SMatthew G. Knepley   Collective on dm
21613cf72582SMatthew G. Knepley 
21623cf72582SMatthew G. Knepley   Input Parameters:
21633cf72582SMatthew G. Knepley + dm - The original DM
2164720e594eSMatthew G. Knepley . label - The label specifying the interface vertices
2165720e594eSMatthew G. Knepley - bdlabel - The optional label specifying the interface boundary vertices
21663cf72582SMatthew G. Knepley 
21673cf72582SMatthew G. Knepley   Output Parameters:
21687db7e0a7SMatthew G. Knepley + hybridLabel - The label fully marking the interface, or NULL if no output is desired
21697db7e0a7SMatthew G. Knepley . splitLabel - The label containing the split points, or NULL if no output is desired
2170720e594eSMatthew G. Knepley . dmInterface - The new interface DM, or NULL
2171720e594eSMatthew G. Knepley - dmHybrid - The new DM with cohesive cells
21723cf72582SMatthew G. Knepley 
21736eccb800SMatthew Knepley   Note: The hybridLabel indicates what parts of the original mesh impinged on the on division surface. For points
21746eccb800SMatthew Knepley   directly on the division surface, they are labeled with their dimension, so an edge 7 on the division surface would be
21756eccb800SMatthew Knepley   7 (1) in hybridLabel. For points that impinge from the positive side, they are labeled with 100+dim, so an edge 6 with
21766eccb800SMatthew 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
21776eccb800SMatthew Knepley   surface also hits vertex 3, it would be 9 (-101) in hybridLabel.
21786eccb800SMatthew Knepley 
21796eccb800SMatthew Knepley   The splitLabel indicates what points in the new hybrid mesh were the result of splitting points in the original
21806eccb800SMatthew Knepley   mesh. The label value is +=100+dim for each point. For example, if two edges 10 and 14 in the hybrid resulting from
21816eccb800SMatthew Knepley   splitting an edge in the original mesh, you would have 10 (101) and 14 (-101) in the splitLabel.
21826eccb800SMatthew Knepley 
21836eccb800SMatthew Knepley   The dmInterface is a DM built from the original division surface. It has a label which can be retrieved using
21846eccb800SMatthew Knepley   DMPlexGetSubpointMap() which maps each point back to the point in the surface of the original mesh.
21856eccb800SMatthew Knepley 
21863cf72582SMatthew G. Knepley   Level: developer
21873cf72582SMatthew G. Knepley 
21886eccb800SMatthew Knepley .seealso: DMPlexConstructCohesiveCells(), DMPlexLabelCohesiveComplete(), DMPlexGetSubpointMap(), DMCreate()
21893cf72582SMatthew G. Knepley @*/
21907db7e0a7SMatthew G. Knepley PetscErrorCode DMPlexCreateHybridMesh(DM dm, DMLabel label, DMLabel bdlabel, DMLabel *hybridLabel, DMLabel *splitLabel, DM *dmInterface, DM *dmHybrid)
21913cf72582SMatthew G. Knepley {
21923cf72582SMatthew G. Knepley   DM             idm;
21937db7e0a7SMatthew G. Knepley   DMLabel        subpointMap, hlabel, slabel = NULL;
21943cf72582SMatthew G. Knepley   PetscInt       dim;
21953cf72582SMatthew G. Knepley 
21963cf72582SMatthew G. Knepley   PetscFunctionBegin;
21973cf72582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2198720e594eSMatthew G. Knepley   if (bdlabel) PetscValidPointer(bdlabel, 3);
2199720e594eSMatthew G. Knepley   if (hybridLabel) PetscValidPointer(hybridLabel, 4);
22007db7e0a7SMatthew G. Knepley   if (splitLabel)  PetscValidPointer(splitLabel, 5);
22017db7e0a7SMatthew G. Knepley   if (dmInterface) PetscValidPointer(dmInterface, 6);
22027db7e0a7SMatthew G. Knepley   PetscValidPointer(dmHybrid, 7);
22039566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
22049566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmesh(dm, label, 1, PETSC_FALSE, &idm));
22059566063dSJacob Faibussowitsch   PetscCall(DMPlexCheckValidSubmesh_Private(dm, label, idm));
22069566063dSJacob Faibussowitsch   PetscCall(DMPlexOrient(idm));
22079566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(idm, &subpointMap));
22089566063dSJacob Faibussowitsch   PetscCall(DMLabelDuplicate(subpointMap, &hlabel));
22099566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(hlabel, dim));
22107db7e0a7SMatthew G. Knepley   if (splitLabel) {
22117db7e0a7SMatthew G. Knepley     const char *name;
22127db7e0a7SMatthew G. Knepley     char        sname[PETSC_MAX_PATH_LEN];
22137db7e0a7SMatthew G. Knepley 
22149566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) hlabel, &name));
22159566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(sname, name, PETSC_MAX_PATH_LEN));
22169566063dSJacob Faibussowitsch     PetscCall(PetscStrcat(sname, " split"));
22179566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, sname, &slabel));
22187db7e0a7SMatthew G. Knepley   }
22199566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelCohesiveComplete(dm, hlabel, bdlabel, PETSC_FALSE, idm));
2220720e594eSMatthew G. Knepley   if (dmInterface) {*dmInterface = idm;}
22219566063dSJacob Faibussowitsch   else             PetscCall(DMDestroy(&idm));
22229566063dSJacob Faibussowitsch   PetscCall(DMPlexConstructCohesiveCells(dm, hlabel, slabel, dmHybrid));
22239566063dSJacob Faibussowitsch   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, *dmHybrid));
22243cf72582SMatthew G. Knepley   if (hybridLabel) *hybridLabel = hlabel;
22259566063dSJacob Faibussowitsch   else             PetscCall(DMLabelDestroy(&hlabel));
22267db7e0a7SMatthew G. Knepley   if (splitLabel)  *splitLabel  = slabel;
22274a7ee7d0SMatthew G. Knepley   {
22284a7ee7d0SMatthew G. Knepley     DM      cdm;
22294a7ee7d0SMatthew G. Knepley     DMLabel ctLabel;
22304a7ee7d0SMatthew G. Knepley 
22314a7ee7d0SMatthew G. Knepley     /* We need to somehow share the celltype label with the coordinate dm */
22329566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(*dmHybrid, &cdm));
22339566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellTypeLabel(*dmHybrid, &ctLabel));
22349566063dSJacob Faibussowitsch     PetscCall(DMSetLabel(cdm, ctLabel));
22354a7ee7d0SMatthew G. Knepley   }
2236cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
2237cd0c2139SMatthew G Knepley }
2238cd0c2139SMatthew G Knepley 
2239efa14ee0SMatthew G Knepley /* Here we need the explicit assumption that:
2240efa14ee0SMatthew G Knepley 
2241efa14ee0SMatthew G Knepley      For any marked cell, the marked vertices constitute a single face
2242efa14ee0SMatthew G Knepley */
2243830e53efSMatthew G. Knepley static PetscErrorCode DMPlexMarkSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, DM subdm)
2244efa14ee0SMatthew G Knepley {
2245fed694aaSMatthew G. Knepley   IS               subvertexIS = NULL;
2246efa14ee0SMatthew G Knepley   const PetscInt  *subvertices;
2247412e9a14SMatthew G. Knepley   PetscInt        *pStart, *pEnd, pSize;
2248efa14ee0SMatthew G Knepley   PetscInt         depth, dim, d, numSubVerticesInitial = 0, v;
2249efa14ee0SMatthew G Knepley 
2250efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2251efa14ee0SMatthew G Knepley   *numFaces = 0;
2252efa14ee0SMatthew G Knepley   *nFV      = 0;
22539566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
22549566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
225577d178adSMatthew G. Knepley   pSize = PetscMax(depth, dim) + 1;
22569566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(pSize, &pStart, pSize, &pEnd));
2257efa14ee0SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
22589566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSimplexOrBoxCells(dm, depth-d, &pStart[d], &pEnd[d]));
2259efa14ee0SMatthew G Knepley   }
2260efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
22619566063dSJacob Faibussowitsch   if (vertexLabel) PetscCall(DMLabelGetStratumIS(vertexLabel, value, &subvertexIS));
2262efa14ee0SMatthew G Knepley   if (subvertexIS) {
22639566063dSJacob Faibussowitsch     PetscCall(ISGetSize(subvertexIS, &numSubVerticesInitial));
22649566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subvertexIS, &subvertices));
2265efa14ee0SMatthew G Knepley   }
2266efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2267efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
22680298fd71SBarry Smith     PetscInt      *star   = NULL;
2269efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numCells = 0, c;
2270efa14ee0SMatthew G Knepley 
22719566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2272efa14ee0SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
2273efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2274efa14ee0SMatthew G Knepley       if ((point >= pStart[depth]) && (point < pEnd[depth])) star[numCells++] = point;
2275efa14ee0SMatthew G Knepley     }
2276efa14ee0SMatthew G Knepley     for (c = 0; c < numCells; ++c) {
2277efa14ee0SMatthew G Knepley       const PetscInt cell    = star[c];
22780298fd71SBarry Smith       PetscInt      *closure = NULL;
2279efa14ee0SMatthew G Knepley       PetscInt       closureSize, cl;
2280efa14ee0SMatthew G Knepley       PetscInt       cellLoc, numCorners = 0, faceSize = 0;
2281efa14ee0SMatthew G Knepley 
22829566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(subpointMap, cell, &cellLoc));
228365560c7fSMatthew G Knepley       if (cellLoc == 2) continue;
228408401ef6SPierre Jolivet       PetscCheck(cellLoc < 0,PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %d has dimension %d in the surface label", cell, cellLoc);
22859566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
2286efa14ee0SMatthew G Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
2287efa14ee0SMatthew G Knepley         const PetscInt point = closure[cl];
2288efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2289efa14ee0SMatthew G Knepley 
2290efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
2291efa14ee0SMatthew G Knepley           ++numCorners;
22929566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &vertexLoc));
2293830e53efSMatthew G. Knepley           if (vertexLoc == value) closure[faceSize++] = point;
2294efa14ee0SMatthew G Knepley         }
2295efa14ee0SMatthew G Knepley       }
22969566063dSJacob Faibussowitsch       if (!(*nFV)) PetscCall(DMPlexGetNumFaceVertices(dm, dim, numCorners, nFV));
229708401ef6SPierre Jolivet       PetscCheck(faceSize <= *nFV,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %d of an element on the surface", faceSize);
2298efa14ee0SMatthew G Knepley       if (faceSize == *nFV) {
2299007baee2SMatthew G. Knepley         const PetscInt *cells = NULL;
2300007baee2SMatthew G. Knepley         PetscInt        numCells, nc;
2301007baee2SMatthew G. Knepley 
2302efa14ee0SMatthew G Knepley         ++(*numFaces);
2303efa14ee0SMatthew G Knepley         for (cl = 0; cl < faceSize; ++cl) {
23049566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(subpointMap, closure[cl], 0));
2305efa14ee0SMatthew G Knepley         }
23069566063dSJacob Faibussowitsch         PetscCall(DMPlexGetJoin(dm, faceSize, closure, &numCells, &cells));
2307007baee2SMatthew G. Knepley         for (nc = 0; nc < numCells; ++nc) {
23089566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(subpointMap, cells[nc], 2));
2309007baee2SMatthew G. Knepley         }
23109566063dSJacob Faibussowitsch         PetscCall(DMPlexRestoreJoin(dm, faceSize, closure, &numCells, &cells));
2311efa14ee0SMatthew G Knepley       }
23129566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
2313efa14ee0SMatthew G Knepley     }
23149566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2315efa14ee0SMatthew G Knepley   }
2316efa14ee0SMatthew G Knepley   if (subvertexIS) {
23179566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(subvertexIS, &subvertices));
2318efa14ee0SMatthew G Knepley   }
23199566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
23209566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
2321efa14ee0SMatthew G Knepley   PetscFunctionReturn(0);
2322efa14ee0SMatthew G Knepley }
2323efa14ee0SMatthew G Knepley 
2324158acfadSMatthew G. Knepley static PetscErrorCode DMPlexMarkSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DMLabel subpointMap, DM subdm)
2325efa14ee0SMatthew G Knepley {
232634b4c39eSMatthew G. Knepley   IS               subvertexIS = NULL;
2327efa14ee0SMatthew G Knepley   const PetscInt  *subvertices;
2328412e9a14SMatthew G. Knepley   PetscInt        *pStart, *pEnd;
2329efa14ee0SMatthew G Knepley   PetscInt         dim, d, numSubVerticesInitial = 0, v;
2330efa14ee0SMatthew G Knepley 
2331efa14ee0SMatthew G Knepley   PetscFunctionBegin;
23329566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
23339566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(dim+1, &pStart, dim+1, &pEnd));
2334efa14ee0SMatthew G Knepley   for (d = 0; d <= dim; ++d) {
23359566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSimplexOrBoxCells(dm, dim-d, &pStart[d], &pEnd[d]));
2336efa14ee0SMatthew G Knepley   }
2337efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
233834b4c39eSMatthew G. Knepley   if (vertexLabel) {
23399566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(vertexLabel, value, &subvertexIS));
2340efa14ee0SMatthew G Knepley     if (subvertexIS) {
23419566063dSJacob Faibussowitsch       PetscCall(ISGetSize(subvertexIS, &numSubVerticesInitial));
23429566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subvertexIS, &subvertices));
2343efa14ee0SMatthew G Knepley     }
234434b4c39eSMatthew G. Knepley   }
2345efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2346efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
23470298fd71SBarry Smith     PetscInt      *star   = NULL;
2348efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numFaces = 0, f;
2349efa14ee0SMatthew G Knepley 
23509566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2351efa14ee0SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
2352efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2353158acfadSMatthew G. Knepley       PetscInt       faceLoc;
2354158acfadSMatthew G. Knepley 
2355158acfadSMatthew G. Knepley       if ((point >= pStart[dim-1]) && (point < pEnd[dim-1])) {
2356158acfadSMatthew G. Knepley         if (markedFaces) {
23579566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &faceLoc));
2358158acfadSMatthew G. Knepley           if (faceLoc < 0) continue;
2359158acfadSMatthew G. Knepley         }
2360158acfadSMatthew G. Knepley         star[numFaces++] = point;
2361158acfadSMatthew G. Knepley       }
2362efa14ee0SMatthew G Knepley     }
2363efa14ee0SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
2364efa14ee0SMatthew G Knepley       const PetscInt face    = star[f];
23650298fd71SBarry Smith       PetscInt      *closure = NULL;
2366efa14ee0SMatthew G Knepley       PetscInt       closureSize, c;
2367efa14ee0SMatthew G Knepley       PetscInt       faceLoc;
2368efa14ee0SMatthew G Knepley 
23699566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(subpointMap, face, &faceLoc));
2370efa14ee0SMatthew G Knepley       if (faceLoc == dim-1) continue;
237108401ef6SPierre Jolivet       PetscCheck(faceLoc < 0,PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %d has dimension %d in the surface label", face, faceLoc);
23729566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure));
2373efa14ee0SMatthew G Knepley       for (c = 0; c < closureSize*2; c += 2) {
2374efa14ee0SMatthew G Knepley         const PetscInt point = closure[c];
2375efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2376efa14ee0SMatthew G Knepley 
2377efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
23789566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &vertexLoc));
2379830e53efSMatthew G. Knepley           if (vertexLoc != value) break;
2380efa14ee0SMatthew G Knepley         }
2381efa14ee0SMatthew G Knepley       }
2382efa14ee0SMatthew G Knepley       if (c == closureSize*2) {
2383efa14ee0SMatthew G Knepley         const PetscInt *support;
2384efa14ee0SMatthew G Knepley         PetscInt        supportSize, s;
2385efa14ee0SMatthew G Knepley 
2386efa14ee0SMatthew G Knepley         for (c = 0; c < closureSize*2; c += 2) {
2387efa14ee0SMatthew G Knepley           const PetscInt point = closure[c];
2388efa14ee0SMatthew G Knepley 
2389efa14ee0SMatthew G Knepley           for (d = 0; d < dim; ++d) {
2390efa14ee0SMatthew G Knepley             if ((point >= pStart[d]) && (point < pEnd[d])) {
23919566063dSJacob Faibussowitsch               PetscCall(DMLabelSetValue(subpointMap, point, d));
2392efa14ee0SMatthew G Knepley               break;
2393efa14ee0SMatthew G Knepley             }
2394efa14ee0SMatthew G Knepley           }
2395efa14ee0SMatthew G Knepley         }
23969566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, face, &supportSize));
23979566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, face, &support));
2398efa14ee0SMatthew G Knepley         for (s = 0; s < supportSize; ++s) {
23999566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(subpointMap, support[s], dim));
2400efa14ee0SMatthew G Knepley         }
2401efa14ee0SMatthew G Knepley       }
24029566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure));
2403efa14ee0SMatthew G Knepley     }
24049566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2405efa14ee0SMatthew G Knepley   }
24069566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subvertices));
24079566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
24089566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
2409efa14ee0SMatthew G Knepley   PetscFunctionReturn(0);
2410efa14ee0SMatthew G Knepley }
2411efa14ee0SMatthew G Knepley 
241227c04023SMatthew G. Knepley static PetscErrorCode DMPlexMarkCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char labelname[], PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, PetscInt *subCells[], DM subdm)
2413766ab985SMatthew G. Knepley {
241427c04023SMatthew G. Knepley   DMLabel         label = NULL;
2415766ab985SMatthew G. Knepley   const PetscInt *cone;
24169fc93327SToby Isaac   PetscInt        dim, cMax, cEnd, c, subc = 0, p, coneSize = -1;
2417766ab985SMatthew G. Knepley 
2418812bfc34SJed Brown   PetscFunctionBegin;
2419c0ed958bSJed Brown   *numFaces = 0;
2420c0ed958bSJed Brown   *nFV = 0;
24219566063dSJacob Faibussowitsch   if (labelname) PetscCall(DMGetLabel(dm, labelname, &label));
2422fed694aaSMatthew G. Knepley   *subCells = NULL;
24239566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
24249566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd));
2425766ab985SMatthew G. Knepley   if (cMax < 0) PetscFunctionReturn(0);
242627c04023SMatthew G. Knepley   if (label) {
242727c04023SMatthew G. Knepley     for (c = cMax; c < cEnd; ++c) {
242827c04023SMatthew G. Knepley       PetscInt val;
242927c04023SMatthew G. Knepley 
24309566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
243127c04023SMatthew G. Knepley       if (val == value) {
243227c04023SMatthew G. Knepley         ++(*numFaces);
24339566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
243427c04023SMatthew G. Knepley       }
243527c04023SMatthew G. Knepley     }
243627c04023SMatthew G. Knepley   } else {
2437766ab985SMatthew G. Knepley     *numFaces = cEnd - cMax;
24389566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cMax, &coneSize));
243927c04023SMatthew G. Knepley   }
24409566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(*numFaces *2, subCells));
24419fc93327SToby Isaac   if (!(*numFaces)) PetscFunctionReturn(0);
24429fc93327SToby Isaac   *nFV = hasLagrange ? coneSize/3 : coneSize/2;
2443766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2444766ab985SMatthew G. Knepley     const PetscInt *cells;
2445766ab985SMatthew G. Knepley     PetscInt        numCells;
2446766ab985SMatthew G. Knepley 
244727c04023SMatthew G. Knepley     if (label) {
244827c04023SMatthew G. Knepley       PetscInt val;
244927c04023SMatthew G. Knepley 
24509566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
245127c04023SMatthew G. Knepley       if (val != value) continue;
245227c04023SMatthew G. Knepley     }
24539566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, c, &cone));
2454766ab985SMatthew G. Knepley     for (p = 0; p < *nFV; ++p) {
24559566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(subpointMap, cone[p], 0));
2456766ab985SMatthew G. Knepley     }
2457766ab985SMatthew G. Knepley     /* Negative face */
24589566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, *nFV, cone, &numCells, &cells));
245927234c99SMatthew G. Knepley     /* Not true in parallel
246008401ef6SPierre Jolivet     PetscCheck(numCells == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
2461766ab985SMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
24629566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(subpointMap, cells[p], 2));
246327234c99SMatthew G. Knepley       (*subCells)[subc++] = cells[p];
2464766ab985SMatthew G. Knepley     }
24659566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, *nFV, cone, &numCells, &cells));
2466766ab985SMatthew G. Knepley     /* Positive face is not included */
2467766ab985SMatthew G. Knepley   }
2468766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
2469766ab985SMatthew G. Knepley }
2470766ab985SMatthew G. Knepley 
24713982b651SMatthew G. Knepley static PetscErrorCode DMPlexMarkCohesiveSubmesh_Interpolated(DM dm, DMLabel label, PetscInt value, DMLabel subpointMap, DM subdm)
2472766ab985SMatthew G. Knepley {
2473766ab985SMatthew G. Knepley   PetscInt      *pStart, *pEnd;
2474766ab985SMatthew G. Knepley   PetscInt       dim, cMax, cEnd, c, d;
2475766ab985SMatthew G. Knepley 
2476812bfc34SJed Brown   PetscFunctionBegin;
24779566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
24789566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd));
2479766ab985SMatthew G. Knepley   if (cMax < 0) PetscFunctionReturn(0);
24809566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(dim+1,&pStart,dim+1,&pEnd));
24819566063dSJacob Faibussowitsch   for (d = 0; d <= dim; ++d) PetscCall(DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]));
2482766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2483766ab985SMatthew G. Knepley     const PetscInt *cone;
2484766ab985SMatthew G. Knepley     PetscInt       *closure = NULL;
2485b3154360SMatthew G. Knepley     PetscInt        fconeSize, coneSize, closureSize, cl, val;
2486766ab985SMatthew G. Knepley 
248727c04023SMatthew G. Knepley     if (label) {
24889566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
248927c04023SMatthew G. Knepley       if (val != value) continue;
249027c04023SMatthew G. Knepley     }
24919566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
24929566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, c, &cone));
24939566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cone[0], &fconeSize));
24942c71b3e2SJacob Faibussowitsch     PetscCheckFalse(coneSize != (fconeSize ? fconeSize : 1) + 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells");
2495b3154360SMatthew G. Knepley     /* Negative face */
24969566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure));
2497766ab985SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
2498766ab985SMatthew G. Knepley       const PetscInt point = closure[cl];
2499766ab985SMatthew G. Knepley 
2500766ab985SMatthew G. Knepley       for (d = 0; d <= dim; ++d) {
2501766ab985SMatthew G. Knepley         if ((point >= pStart[d]) && (point < pEnd[d])) {
25029566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(subpointMap, point, d));
2503766ab985SMatthew G. Knepley           break;
2504766ab985SMatthew G. Knepley         }
2505766ab985SMatthew G. Knepley       }
2506766ab985SMatthew G. Knepley     }
25079566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure));
2508766ab985SMatthew G. Knepley     /* Cells -- positive face is not included */
2509766ab985SMatthew G. Knepley     for (cl = 0; cl < 1; ++cl) {
2510766ab985SMatthew G. Knepley       const PetscInt *support;
2511766ab985SMatthew G. Knepley       PetscInt        supportSize, s;
2512766ab985SMatthew G. Knepley 
25139566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, cone[cl], &supportSize));
251408401ef6SPierre Jolivet       /* PetscCheck(supportSize == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive faces should separate two cells"); */
25159566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, cone[cl], &support));
2516766ab985SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
25179566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(subpointMap, support[s], dim));
2518766ab985SMatthew G. Knepley       }
2519766ab985SMatthew G. Knepley     }
2520766ab985SMatthew G. Knepley   }
25219566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
2522766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
2523766ab985SMatthew G. Knepley }
2524766ab985SMatthew G. Knepley 
2525c08575a3SMatthew G. Knepley static PetscErrorCode DMPlexGetFaceOrientation(DM dm, PetscInt cell, PetscInt numCorners, PetscInt indices[], PetscInt oppositeVertex, PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2526e6ccafaeSMatthew G Knepley {
252782f516ccSBarry Smith   MPI_Comm       comm;
2528e6ccafaeSMatthew G Knepley   PetscBool      posOrient = PETSC_FALSE;
2529e6ccafaeSMatthew G Knepley   const PetscInt debug     = 0;
2530e6ccafaeSMatthew G Knepley   PetscInt       cellDim, faceSize, f;
2531e6ccafaeSMatthew G Knepley 
253282f516ccSBarry Smith   PetscFunctionBegin;
25339566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm,&comm));
25349566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &cellDim));
25359566063dSJacob Faibussowitsch   if (debug) PetscCall(PetscPrintf(comm, "cellDim: %d numCorners: %d\n", cellDim, numCorners));
2536e6ccafaeSMatthew G Knepley 
2537ddeab2a6SMatthew G. Knepley   if (cellDim == 1 && numCorners == 2) {
2538ddeab2a6SMatthew G. Knepley     /* Triangle */
2539e6ccafaeSMatthew G Knepley     faceSize  = numCorners-1;
2540e6ccafaeSMatthew G Knepley     posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2541ddeab2a6SMatthew G. Knepley   } else if (cellDim == 2 && numCorners == 3) {
2542ddeab2a6SMatthew G. Knepley     /* Triangle */
2543ddeab2a6SMatthew G. Knepley     faceSize  = numCorners-1;
2544ddeab2a6SMatthew G. Knepley     posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2545ddeab2a6SMatthew G. Knepley   } else if (cellDim == 3 && numCorners == 4) {
2546ddeab2a6SMatthew G. Knepley     /* Tetrahedron */
2547ddeab2a6SMatthew G. Knepley     faceSize  = numCorners-1;
2548ddeab2a6SMatthew G. Knepley     posOrient = (oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2549e6ccafaeSMatthew G Knepley   } else if (cellDim == 1 && numCorners == 3) {
2550e6ccafaeSMatthew G Knepley     /* Quadratic line */
2551e6ccafaeSMatthew G Knepley     faceSize  = 1;
2552e6ccafaeSMatthew G Knepley     posOrient = PETSC_TRUE;
2553e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 4) {
2554e6ccafaeSMatthew G Knepley     /* Quads */
2555e6ccafaeSMatthew G Knepley     faceSize = 2;
2556e6ccafaeSMatthew G Knepley     if ((indices[1] > indices[0]) && (indices[1] - indices[0] == 1)) {
2557e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2558e6ccafaeSMatthew G Knepley     } else if ((indices[0] == 3) && (indices[1] == 0)) {
2559e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2560e6ccafaeSMatthew G Knepley     } else {
2561e6ccafaeSMatthew G Knepley       if (((indices[0] > indices[1]) && (indices[0] - indices[1] == 1)) || ((indices[0] == 0) && (indices[1] == 3))) {
2562e6ccafaeSMatthew G Knepley         posOrient = PETSC_FALSE;
2563e6ccafaeSMatthew G Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossedge");
2564e6ccafaeSMatthew G Knepley     }
2565e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 6) {
2566e6ccafaeSMatthew G Knepley     /* Quadratic triangle (I hate this) */
2567e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2568e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTri = 3;
2569e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2570e6ccafaeSMatthew G Knepley     PetscBool      found                    = PETSC_FALSE;
2571e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTriSorted[9] = {
2572e6ccafaeSMatthew G Knepley       0, 3,  4, /* bottom */
2573e6ccafaeSMatthew G Knepley       1, 4,  5, /* right */
2574e6ccafaeSMatthew G Knepley       2, 3,  5, /* left */
2575e6ccafaeSMatthew G Knepley     };
2576e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTri[9] = {
2577e6ccafaeSMatthew G Knepley       0, 3,  4, /* bottom */
2578e6ccafaeSMatthew G Knepley       1, 4,  5, /* right */
2579e6ccafaeSMatthew G Knepley       2, 5,  3, /* left */
2580e6ccafaeSMatthew G Knepley     };
2581e6ccafaeSMatthew G Knepley 
2582e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTri; ++i) sortedIndices[i] = indices[i];
25839566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeTri, sortedIndices));
2584e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 3; ++iFace) {
2585e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeTri;
2586e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2587e6ccafaeSMatthew G Knepley 
2588e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesTriSorted[ii+0]) &&
2589e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesTriSorted[ii+1])) {
2590e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTri; ++fVertex) {
2591e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTri; ++cVertex) {
2592e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTri[ii+fVertex]) {
2593e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2594e6ccafaeSMatthew G Knepley               break;
2595e6ccafaeSMatthew G Knepley             }
2596e6ccafaeSMatthew G Knepley           }
2597e6ccafaeSMatthew G Knepley         }
2598e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2599e6ccafaeSMatthew G Knepley         break;
2600e6ccafaeSMatthew G Knepley       }
2601e6ccafaeSMatthew G Knepley     }
260228b400f6SJacob Faibussowitsch     PetscCheck(found,comm, PETSC_ERR_ARG_WRONG, "Invalid tri crossface");
2603e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2604e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2605e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 9) {
2606e6ccafaeSMatthew G Knepley     /* Quadratic quad (I hate this) */
2607e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2608e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuad = 3;
2609e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2610e6ccafaeSMatthew G Knepley     PetscBool      found                      = PETSC_FALSE;
2611e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadSorted[12] = {
2612e6ccafaeSMatthew G Knepley       0, 1,  4, /* bottom */
2613e6ccafaeSMatthew G Knepley       1, 2,  5, /* right */
2614e6ccafaeSMatthew G Knepley       2, 3,  6, /* top */
2615e6ccafaeSMatthew G Knepley       0, 3,  7, /* left */
2616e6ccafaeSMatthew G Knepley     };
2617e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuad[12] = {
2618e6ccafaeSMatthew G Knepley       0, 1,  4, /* bottom */
2619e6ccafaeSMatthew G Knepley       1, 2,  5, /* right */
2620e6ccafaeSMatthew G Knepley       2, 3,  6, /* top */
2621e6ccafaeSMatthew G Knepley       3, 0,  7, /* left */
2622e6ccafaeSMatthew G Knepley     };
2623e6ccafaeSMatthew G Knepley 
2624e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuad; ++i) sortedIndices[i] = indices[i];
26259566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeQuad, sortedIndices));
2626e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2627e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeQuad;
2628e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2629e6ccafaeSMatthew G Knepley 
2630e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesQuadSorted[ii+0]) &&
2631e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesQuadSorted[ii+1])) {
2632e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuad; ++fVertex) {
2633e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuad; ++cVertex) {
2634e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuad[ii+fVertex]) {
2635e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2636e6ccafaeSMatthew G Knepley               break;
2637e6ccafaeSMatthew G Knepley             }
2638e6ccafaeSMatthew G Knepley           }
2639e6ccafaeSMatthew G Knepley         }
2640e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2641e6ccafaeSMatthew G Knepley         break;
2642e6ccafaeSMatthew G Knepley       }
2643e6ccafaeSMatthew G Knepley     }
264428b400f6SJacob Faibussowitsch     PetscCheck(found,comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossface");
2645e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2646e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2647e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 8) {
2648e6ccafaeSMatthew G Knepley     /* Hexes
2649e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2650e6ccafaeSMatthew G Knepley        pointing up at the second.
2651e6ccafaeSMatthew G Knepley 
2652e6ccafaeSMatthew G Knepley           7---6
2653e6ccafaeSMatthew G Knepley          /|  /|
2654e6ccafaeSMatthew G Knepley         4---5 |
2655ddeab2a6SMatthew G. Knepley         | 1-|-2
2656e6ccafaeSMatthew G Knepley         |/  |/
2657ddeab2a6SMatthew G. Knepley         0---3
2658e6ccafaeSMatthew G Knepley 
2659e6ccafaeSMatthew G Knepley         Faces are determined by the first 4 vertices (corners of faces) */
2660e6ccafaeSMatthew G Knepley     const PetscInt faceSizeHex = 4;
2661e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[4], i, iFace;
2662e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2663e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHexSorted[24] = {
2664e6ccafaeSMatthew G Knepley       0, 1, 2, 3,  /* bottom */
2665e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  /* top */
2666ddeab2a6SMatthew G. Knepley       0, 3, 4, 5,  /* front */
2667ddeab2a6SMatthew G. Knepley       2, 3, 5, 6,  /* right */
2668ddeab2a6SMatthew G. Knepley       1, 2, 6, 7,  /* back */
2669ddeab2a6SMatthew G. Knepley       0, 1, 4, 7,  /* left */
2670e6ccafaeSMatthew G Knepley     };
2671e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHex[24] = {
2672ddeab2a6SMatthew G. Knepley       1, 2, 3, 0,  /* bottom */
2673e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  /* top */
2674ddeab2a6SMatthew G. Knepley       0, 3, 5, 4,  /* front */
2675ddeab2a6SMatthew G. Knepley       3, 2, 6, 5,  /* right */
2676ddeab2a6SMatthew G. Knepley       2, 1, 7, 6,  /* back */
2677ddeab2a6SMatthew G. Knepley       1, 0, 4, 7,  /* left */
2678e6ccafaeSMatthew G Knepley     };
2679e6ccafaeSMatthew G Knepley 
2680e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeHex; ++i) sortedIndices[i] = indices[i];
26819566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeHex, sortedIndices));
2682e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2683e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeHex;
2684e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2685e6ccafaeSMatthew G Knepley 
2686e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesHexSorted[ii+0]) &&
2687e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesHexSorted[ii+1]) &&
2688e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesHexSorted[ii+2]) &&
2689e6ccafaeSMatthew G Knepley           (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 
2730e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesTetSorted[ii+0]) &&
2731e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesTetSorted[ii+1]) &&
2732e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesTetSorted[ii+2]) &&
2733e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesTetSorted[ii+3])) {
2734e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTet; ++fVertex) {
2735e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTet; ++cVertex) {
2736e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTet[ii+fVertex]) {
2737e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2738e6ccafaeSMatthew G Knepley               break;
2739e6ccafaeSMatthew G Knepley             }
2740e6ccafaeSMatthew G Knepley           }
2741e6ccafaeSMatthew G Knepley         }
2742e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2743e6ccafaeSMatthew G Knepley         break;
2744e6ccafaeSMatthew G Knepley       }
2745e6ccafaeSMatthew G Knepley     }
274628b400f6SJacob Faibussowitsch     PetscCheck(found,comm, PETSC_ERR_ARG_WRONG, "Invalid tet crossface");
2747e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2748e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2749e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 27) {
2750e6ccafaeSMatthew G Knepley     /* Quadratic hexes (I hate this)
2751e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2752e6ccafaeSMatthew G Knepley        pointing up at the second.
2753e6ccafaeSMatthew G Knepley 
2754e6ccafaeSMatthew G Knepley          7---6
2755e6ccafaeSMatthew G Knepley         /|  /|
2756e6ccafaeSMatthew G Knepley        4---5 |
2757e6ccafaeSMatthew G Knepley        | 3-|-2
2758e6ccafaeSMatthew G Knepley        |/  |/
2759e6ccafaeSMatthew G Knepley        0---1
2760e6ccafaeSMatthew G Knepley 
2761e6ccafaeSMatthew G Knepley        Faces are determined by the first 4 vertices (corners of faces) */
2762e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuadHex = 9;
2763e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[9], i, iFace;
2764e6ccafaeSMatthew G Knepley     PetscBool      found                         = PETSC_FALSE;
2765e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHexSorted[54] = {
2766e6ccafaeSMatthew G Knepley       0, 1, 2, 3,  8, 9, 10, 11,  24, /* bottom */
2767e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  12, 13, 14, 15,  25, /* top */
2768e6ccafaeSMatthew G Knepley       0, 1, 4, 5,  8, 12, 16, 17,  22, /* front */
2769e6ccafaeSMatthew G Knepley       1, 2, 5, 6,  9, 13, 17, 18,  21, /* right */
2770e6ccafaeSMatthew G Knepley       2, 3, 6, 7,  10, 14, 18, 19,  23, /* back */
2771e6ccafaeSMatthew G Knepley       0, 3, 4, 7,  11, 15, 16, 19,  20, /* left */
2772e6ccafaeSMatthew G Knepley     };
2773e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHex[54] = {
2774e6ccafaeSMatthew G Knepley       3, 2, 1, 0,  10, 9, 8, 11,  24, /* bottom */
2775e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  12, 13, 14, 15,  25, /* top */
2776e6ccafaeSMatthew G Knepley       0, 1, 5, 4,  8, 17, 12, 16,  22, /* front */
2777e6ccafaeSMatthew G Knepley       1, 2, 6, 5,  9, 18, 13, 17,  21, /* right */
2778e6ccafaeSMatthew G Knepley       2, 3, 7, 6,  10, 19, 14, 18,  23, /* back */
2779e6ccafaeSMatthew G Knepley       3, 0, 4, 7,  11, 16, 15, 19,  20 /* left */
2780e6ccafaeSMatthew G Knepley     };
2781e6ccafaeSMatthew G Knepley 
2782e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuadHex; ++i) sortedIndices[i] = indices[i];
27839566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeQuadHex, sortedIndices));
2784e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2785e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeQuadHex;
2786e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2787e6ccafaeSMatthew G Knepley 
2788e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesQuadHexSorted[ii+0]) &&
2789e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesQuadHexSorted[ii+1]) &&
2790e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesQuadHexSorted[ii+2]) &&
2791e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesQuadHexSorted[ii+3])) {
2792e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuadHex; ++fVertex) {
2793e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuadHex; ++cVertex) {
2794e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuadHex[ii+fVertex]) {
2795e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2796e6ccafaeSMatthew G Knepley               break;
2797e6ccafaeSMatthew G Knepley             }
2798e6ccafaeSMatthew G Knepley           }
2799e6ccafaeSMatthew G Knepley         }
2800e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2801e6ccafaeSMatthew G Knepley         break;
2802e6ccafaeSMatthew G Knepley       }
2803e6ccafaeSMatthew G Knepley     }
280428b400f6SJacob Faibussowitsch     PetscCheck(found,comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2805e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2806e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2807e6ccafaeSMatthew G Knepley   } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Unknown cell type for faceOrientation().");
2808e6ccafaeSMatthew G Knepley   if (!posOrient) {
28099566063dSJacob Faibussowitsch     if (debug) PetscCall(PetscPrintf(comm, "  Reversing initial face orientation\n"));
2810e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[faceSize-1 - f];
2811e6ccafaeSMatthew G Knepley   } else {
28129566063dSJacob Faibussowitsch     if (debug) PetscCall(PetscPrintf(comm, "  Keeping initial face orientation\n"));
2813e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[f];
2814e6ccafaeSMatthew G Knepley   }
2815e6ccafaeSMatthew G Knepley   if (posOriented) *posOriented = posOrient;
2816e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2817e6ccafaeSMatthew G Knepley }
2818e6ccafaeSMatthew G Knepley 
2819c08575a3SMatthew G. Knepley /*@
2820c08575a3SMatthew G. Knepley   DMPlexGetOrientedFace - Given a cell and a face, as a set of vertices, return the oriented face, as a set of vertices,
2821c08575a3SMatthew G. Knepley   in faceVertices. The orientation is such that the face normal points out of the cell
2822c08575a3SMatthew G. Knepley 
2823c08575a3SMatthew G. Knepley   Not collective
2824c08575a3SMatthew G. Knepley 
2825c08575a3SMatthew G. Knepley   Input Parameters:
2826c08575a3SMatthew G. Knepley + dm           - The original mesh
2827c08575a3SMatthew G. Knepley . cell         - The cell mesh point
2828c08575a3SMatthew G. Knepley . faceSize     - The number of vertices on the face
2829c08575a3SMatthew G. Knepley . face         - The face vertices
2830c08575a3SMatthew G. Knepley . numCorners   - The number of vertices on the cell
2831c08575a3SMatthew G. Knepley . indices      - Local numbering of face vertices in cell cone
2832c08575a3SMatthew G. Knepley - origVertices - Original face vertices
2833c08575a3SMatthew G. Knepley 
2834d8d19677SJose E. Roman   Output Parameters:
2835c08575a3SMatthew G. Knepley + faceVertices - The face vertices properly oriented
2836c08575a3SMatthew G. Knepley - posOriented  - PETSC_TRUE if the face was oriented with outward normal
2837c08575a3SMatthew G. Knepley 
2838c08575a3SMatthew G. Knepley   Level: developer
2839c08575a3SMatthew G. Knepley 
2840c08575a3SMatthew G. Knepley .seealso: DMPlexGetCone()
2841c08575a3SMatthew G. Knepley @*/
2842e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexGetOrientedFace(DM dm, PetscInt cell, PetscInt faceSize, const PetscInt face[], PetscInt numCorners, PetscInt indices[], PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2843e6ccafaeSMatthew G Knepley {
28440298fd71SBarry Smith   const PetscInt *cone = NULL;
2845e6ccafaeSMatthew G Knepley   PetscInt        coneSize, v, f, v2;
2846e6ccafaeSMatthew G Knepley   PetscInt        oppositeVertex = -1;
2847e6ccafaeSMatthew G Knepley 
2848e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
28499566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
28509566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCone(dm, cell, &cone));
2851e6ccafaeSMatthew G Knepley   for (v = 0, v2 = 0; v < coneSize; ++v) {
2852e6ccafaeSMatthew G Knepley     PetscBool found = PETSC_FALSE;
2853e6ccafaeSMatthew G Knepley 
2854e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) {
2855e6ccafaeSMatthew G Knepley       if (face[f] == cone[v]) {
2856e6ccafaeSMatthew G Knepley         found = PETSC_TRUE; break;
2857e6ccafaeSMatthew G Knepley       }
2858e6ccafaeSMatthew G Knepley     }
2859e6ccafaeSMatthew G Knepley     if (found) {
2860e6ccafaeSMatthew G Knepley       indices[v2]      = v;
2861e6ccafaeSMatthew G Knepley       origVertices[v2] = cone[v];
2862e6ccafaeSMatthew G Knepley       ++v2;
2863e6ccafaeSMatthew G Knepley     } else {
2864e6ccafaeSMatthew G Knepley       oppositeVertex = v;
2865e6ccafaeSMatthew G Knepley     }
2866e6ccafaeSMatthew G Knepley   }
28679566063dSJacob Faibussowitsch   PetscCall(DMPlexGetFaceOrientation(dm, cell, numCorners, indices, oppositeVertex, origVertices, faceVertices, posOriented));
2868e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2869e6ccafaeSMatthew G Knepley }
2870e6ccafaeSMatthew G Knepley 
2871e6ccafaeSMatthew G Knepley /*
2872cd0c2139SMatthew G Knepley   DMPlexInsertFace_Internal - Puts a face into the mesh
2873e6ccafaeSMatthew G Knepley 
2874e6ccafaeSMatthew G Knepley   Not collective
2875e6ccafaeSMatthew G Knepley 
2876e6ccafaeSMatthew G Knepley   Input Parameters:
2877e6ccafaeSMatthew G Knepley   + dm              - The DMPlex
2878e6ccafaeSMatthew G Knepley   . numFaceVertex   - The number of vertices in the face
2879e6ccafaeSMatthew G Knepley   . faceVertices    - The vertices in the face for dm
2880e6ccafaeSMatthew G Knepley   . subfaceVertices - The vertices in the face for subdm
2881e6ccafaeSMatthew G Knepley   . numCorners      - The number of vertices in the cell
2882e6ccafaeSMatthew G Knepley   . cell            - A cell in dm containing the face
2883e6ccafaeSMatthew G Knepley   . subcell         - A cell in subdm containing the face
2884e6ccafaeSMatthew G Knepley   . firstFace       - First face in the mesh
2885e6ccafaeSMatthew G Knepley   - newFacePoint    - Next face in the mesh
2886e6ccafaeSMatthew G Knepley 
2887e6ccafaeSMatthew G Knepley   Output Parameters:
2888e6ccafaeSMatthew G Knepley   . newFacePoint - Contains next face point number on input, updated on output
2889e6ccafaeSMatthew G Knepley 
2890e6ccafaeSMatthew G Knepley   Level: developer
2891e6ccafaeSMatthew G Knepley */
2892cd0c2139SMatthew G Knepley 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)
2893e6ccafaeSMatthew G Knepley {
289482f516ccSBarry Smith   MPI_Comm        comm;
2895e6ccafaeSMatthew G Knepley   DM_Plex        *submesh = (DM_Plex*) subdm->data;
2896e6ccafaeSMatthew G Knepley   const PetscInt *faces;
2897e6ccafaeSMatthew G Knepley   PetscInt        numFaces, coneSize;
2898e6ccafaeSMatthew G Knepley 
2899e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
29009566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm,&comm));
29019566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(subdm, subcell, &coneSize));
290208401ef6SPierre Jolivet   PetscCheck(coneSize == 1,comm, PETSC_ERR_ARG_OUTOFRANGE, "Cone size of cell %d is %d != 1", cell, coneSize);
2903e6ccafaeSMatthew G Knepley #if 0
2904e6ccafaeSMatthew G Knepley   /* Cannot use this because support() has not been constructed yet */
29059566063dSJacob Faibussowitsch   PetscCall(DMPlexGetJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces));
2906e6ccafaeSMatthew G Knepley #else
2907e6ccafaeSMatthew G Knepley   {
2908e6ccafaeSMatthew G Knepley     PetscInt f;
2909e6ccafaeSMatthew G Knepley 
2910e6ccafaeSMatthew G Knepley     numFaces = 0;
29119566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(subdm, 1, MPIU_INT, (void **) &faces));
2912e6ccafaeSMatthew G Knepley     for (f = firstFace; f < *newFacePoint; ++f) {
2913e6ccafaeSMatthew G Knepley       PetscInt dof, off, d;
2914e6ccafaeSMatthew G Knepley 
29159566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(submesh->coneSection, f, &dof));
29169566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(submesh->coneSection, f, &off));
2917e6ccafaeSMatthew G Knepley       /* Yes, I know this is quadratic, but I expect the sizes to be <5 */
2918e6ccafaeSMatthew G Knepley       for (d = 0; d < dof; ++d) {
2919e6ccafaeSMatthew G Knepley         const PetscInt p = submesh->cones[off+d];
2920e6ccafaeSMatthew G Knepley         PetscInt       v;
2921e6ccafaeSMatthew G Knepley 
2922e6ccafaeSMatthew G Knepley         for (v = 0; v < numFaceVertices; ++v) {
2923e6ccafaeSMatthew G Knepley           if (subfaceVertices[v] == p) break;
2924e6ccafaeSMatthew G Knepley         }
2925e6ccafaeSMatthew G Knepley         if (v == numFaceVertices) break;
2926e6ccafaeSMatthew G Knepley       }
2927e6ccafaeSMatthew G Knepley       if (d == dof) {
2928e6ccafaeSMatthew G Knepley         numFaces               = 1;
2929e6ccafaeSMatthew G Knepley         ((PetscInt*) faces)[0] = f;
2930e6ccafaeSMatthew G Knepley       }
2931e6ccafaeSMatthew G Knepley     }
2932e6ccafaeSMatthew G Knepley   }
2933e6ccafaeSMatthew G Knepley #endif
293408401ef6SPierre Jolivet   PetscCheck(numFaces <= 1,comm, PETSC_ERR_ARG_WRONG, "Vertex set had %d faces, not one", numFaces);
2935e6ccafaeSMatthew G Knepley   else if (numFaces == 1) {
2936e6ccafaeSMatthew G Knepley     /* Add the other cell neighbor for this face */
29379566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, faces));
2938e6ccafaeSMatthew G Knepley   } else {
2939e6ccafaeSMatthew G Knepley     PetscInt *indices, *origVertices, *orientedVertices, *orientedSubVertices, v, ov;
2940e6ccafaeSMatthew G Knepley     PetscBool posOriented;
2941e6ccafaeSMatthew G Knepley 
29429566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices));
2943e6ccafaeSMatthew G Knepley     origVertices        = &orientedVertices[numFaceVertices];
2944e6ccafaeSMatthew G Knepley     indices             = &orientedVertices[numFaceVertices*2];
2945e6ccafaeSMatthew G Knepley     orientedSubVertices = &orientedVertices[numFaceVertices*3];
29469566063dSJacob Faibussowitsch     PetscCall(DMPlexGetOrientedFace(dm, cell, numFaceVertices, faceVertices, numCorners, indices, origVertices, orientedVertices, &posOriented));
2947e6ccafaeSMatthew G Knepley     /* TODO: I know that routine should return a permutation, not the indices */
2948e6ccafaeSMatthew G Knepley     for (v = 0; v < numFaceVertices; ++v) {
2949e6ccafaeSMatthew G Knepley       const PetscInt vertex = faceVertices[v], subvertex = subfaceVertices[v];
2950e6ccafaeSMatthew G Knepley       for (ov = 0; ov < numFaceVertices; ++ov) {
2951e6ccafaeSMatthew G Knepley         if (orientedVertices[ov] == vertex) {
2952e6ccafaeSMatthew G Knepley           orientedSubVertices[ov] = subvertex;
2953e6ccafaeSMatthew G Knepley           break;
2954e6ccafaeSMatthew G Knepley         }
2955e6ccafaeSMatthew G Knepley       }
295608401ef6SPierre Jolivet       PetscCheck(ov != numFaceVertices,comm, PETSC_ERR_PLIB, "Could not find face vertex %d in orientated set", vertex);
2957e6ccafaeSMatthew G Knepley     }
29589566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, *newFacePoint, orientedSubVertices));
29599566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, newFacePoint));
29609566063dSJacob Faibussowitsch     PetscCall(DMRestoreWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices));
2961e6ccafaeSMatthew G Knepley     ++(*newFacePoint);
2962e6ccafaeSMatthew G Knepley   }
2963ef07cca7SMatthew G. Knepley #if 0
29649566063dSJacob Faibussowitsch   PetscCall(DMPlexRestoreJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces));
2965ef07cca7SMatthew G. Knepley #else
29669566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, 1, MPIU_INT, (void **) &faces));
2967ef07cca7SMatthew G. Knepley #endif
2968e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2969e6ccafaeSMatthew G Knepley }
2970e6ccafaeSMatthew G Knepley 
297153156dfcSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm)
2972e6ccafaeSMatthew G Knepley {
297382f516ccSBarry Smith   MPI_Comm        comm;
297453156dfcSMatthew G. Knepley   DMLabel         subpointMap;
2975efa14ee0SMatthew G Knepley   IS              subvertexIS,  subcellIS;
2976efa14ee0SMatthew G Knepley   const PetscInt *subVertices, *subCells;
2977efa14ee0SMatthew G Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells;
2978fed694aaSMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces = 0, firstSubFace, newFacePoint, nFV = 0;
2979efa14ee0SMatthew G Knepley   PetscInt        vStart, vEnd, c, f;
2980e6ccafaeSMatthew G Knepley 
2981e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
29829566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm,&comm));
2983efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
29849566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
29859566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
29869566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&subpointMap));
29879566063dSJacob Faibussowitsch   if (vertexLabel) PetscCall(DMPlexMarkSubmesh_Uninterpolated(dm, vertexLabel, value, subpointMap, &numSubFaces, &nFV, subdm));
2988efa14ee0SMatthew G Knepley   /* Setup chart */
29899566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 0, &numSubVertices));
29909566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 2, &numSubCells));
29919566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices));
29929566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, 1));
2993e6ccafaeSMatthew G Knepley   /* Set cone sizes */
2994e6ccafaeSMatthew G Knepley   firstSubVertex = numSubCells;
2995efa14ee0SMatthew G Knepley   firstSubFace   = numSubCells+numSubVertices;
2996e6ccafaeSMatthew G Knepley   newFacePoint   = firstSubFace;
29979566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 0, &subvertexIS));
29989566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISGetIndices(subvertexIS, &subVertices));
29999566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 2, &subcellIS));
30009566063dSJacob Faibussowitsch   if (subcellIS) PetscCall(ISGetIndices(subcellIS, &subCells));
3001e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
30029566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeSize(subdm, c, 1));
3003e6ccafaeSMatthew G Knepley   }
3004e6ccafaeSMatthew G Knepley   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
30059566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeSize(subdm, f, nFV));
3006e6ccafaeSMatthew G Knepley   }
30079566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3008e6ccafaeSMatthew G Knepley   /* Create face cones */
30099566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
30109566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
30119566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface));
3012e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
3013e6ccafaeSMatthew G Knepley     const PetscInt cell    = subCells[c];
3014efa14ee0SMatthew G Knepley     const PetscInt subcell = c;
30150298fd71SBarry Smith     PetscInt      *closure = NULL;
3016efa14ee0SMatthew G Knepley     PetscInt       closureSize, cl, numCorners = 0, faceSize = 0;
3017e6ccafaeSMatthew G Knepley 
30189566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
3019efa14ee0SMatthew G Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
3020efa14ee0SMatthew G Knepley       const PetscInt point = closure[cl];
3021e6ccafaeSMatthew G Knepley       PetscInt       subVertex;
3022e6ccafaeSMatthew G Knepley 
3023efa14ee0SMatthew G Knepley       if ((point >= vStart) && (point < vEnd)) {
3024efa14ee0SMatthew G Knepley         ++numCorners;
30259566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(point, numSubVertices, subVertices, &subVertex));
3026efa14ee0SMatthew G Knepley         if (subVertex >= 0) {
3027efa14ee0SMatthew G Knepley           closure[faceSize] = point;
302865560c7fSMatthew G Knepley           subface[faceSize] = firstSubVertex+subVertex;
3029e6ccafaeSMatthew G Knepley           ++faceSize;
3030e6ccafaeSMatthew G Knepley         }
3031e6ccafaeSMatthew G Knepley       }
3032e6ccafaeSMatthew G Knepley     }
303308401ef6SPierre Jolivet     PetscCheck(faceSize <= nFV,comm, PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %d of an element on the surface", faceSize);
3034efa14ee0SMatthew G Knepley     if (faceSize == nFV) {
30359566063dSJacob Faibussowitsch       PetscCall(DMPlexInsertFace_Internal(dm, subdm, faceSize, closure, subface, numCorners, cell, subcell, firstSubFace, &newFacePoint));
3036e6ccafaeSMatthew G Knepley     }
30379566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
3038e6ccafaeSMatthew G Knepley   }
30399566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface));
30409566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
30419566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3042e6ccafaeSMatthew G Knepley   /* Build coordinates */
3043efa14ee0SMatthew G Knepley   {
3044efa14ee0SMatthew G Knepley     PetscSection coordSection, subCoordSection;
3045efa14ee0SMatthew G Knepley     Vec          coordinates, subCoordinates;
3046efa14ee0SMatthew G Knepley     PetscScalar *coords, *subCoords;
3047285d324eSMatthew G. Knepley     PetscInt     numComp, coordSize, v;
304824640c55SToby Isaac     const char  *name;
3049efa14ee0SMatthew G Knepley 
30509566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
30519566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
30529566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
30539566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
30549566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
30559566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
30569566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices));
3057efa14ee0SMatthew G Knepley     for (v = 0; v < numSubVertices; ++v) {
3058efa14ee0SMatthew G Knepley       const PetscInt vertex    = subVertices[v];
3059efa14ee0SMatthew G Knepley       const PetscInt subvertex = firstSubVertex+v;
3060efa14ee0SMatthew G Knepley       PetscInt       dof;
3061efa14ee0SMatthew G Knepley 
30629566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
30639566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
30649566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3065e6ccafaeSMatthew G Knepley     }
30669566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
30679566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
30689566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
30699566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates,&name));
30709566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates,name));
30719566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
30729566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates,VECSTANDARD));
3073830e53efSMatthew G. Knepley     if (coordSize) {
30749566063dSJacob Faibussowitsch       PetscCall(VecGetArray(coordinates,    &coords));
30759566063dSJacob Faibussowitsch       PetscCall(VecGetArray(subCoordinates, &subCoords));
3076efa14ee0SMatthew G Knepley       for (v = 0; v < numSubVertices; ++v) {
3077efa14ee0SMatthew G Knepley         const PetscInt vertex    = subVertices[v];
3078efa14ee0SMatthew G Knepley         const PetscInt subvertex = firstSubVertex+v;
3079efa14ee0SMatthew G Knepley         PetscInt       dof, off, sdof, soff, d;
3080e6ccafaeSMatthew G Knepley 
30819566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
30829566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
30839566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
30849566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
308508401ef6SPierre Jolivet         PetscCheck(dof == sdof,comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
3086e6ccafaeSMatthew G Knepley         for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3087e6ccafaeSMatthew G Knepley       }
30889566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(coordinates,    &coords));
30899566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(subCoordinates, &subCoords));
30903b399e24SMatthew G. Knepley     }
30919566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
30929566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3093e6ccafaeSMatthew G Knepley   }
3094efa14ee0SMatthew G Knepley   /* Cleanup */
30959566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subVertices));
30969566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
30979566063dSJacob Faibussowitsch   if (subcellIS) PetscCall(ISRestoreIndices(subcellIS, &subCells));
30989566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subcellIS));
3099e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3100e6ccafaeSMatthew G Knepley }
3101e6ccafaeSMatthew G Knepley 
3102d0609cedSBarry Smith /* TODO: Fix this to properly propogate up error conditions it may find */
31039fbee547SJacob Faibussowitsch static inline PetscInt DMPlexFilterPoint_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[])
31043982b651SMatthew G. Knepley {
31053982b651SMatthew G. Knepley   PetscInt       subPoint;
31063982b651SMatthew G. Knepley   PetscErrorCode ierr;
31073982b651SMatthew G. Knepley 
3108d0609cedSBarry Smith   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint); if (ierr) return -1;
31093982b651SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint+subPoint;
31103982b651SMatthew G. Knepley }
31113982b651SMatthew G. Knepley 
3112212103e5SMatthew Knepley static PetscErrorCode DMPlexFilterLabels_Internal(DM dm, const PetscInt numSubPoints[], const PetscInt *subpoints[], const PetscInt firstSubPoint[], DM subdm)
3113212103e5SMatthew Knepley {
3114212103e5SMatthew Knepley   PetscInt       Nl, l, d;
3115212103e5SMatthew Knepley 
3116212103e5SMatthew Knepley   PetscFunctionBegin;
31179566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
3118212103e5SMatthew Knepley   for (l = 0; l < Nl; ++l) {
3119212103e5SMatthew Knepley     DMLabel         label, newlabel;
3120212103e5SMatthew Knepley     const char     *lname;
3121d56405f8SMatthew G. Knepley     PetscBool       isDepth, isDim, isCelltype, isVTK;
3122212103e5SMatthew Knepley     IS              valueIS;
3123212103e5SMatthew Knepley     const PetscInt *values;
3124212103e5SMatthew Knepley     PetscInt        Nv, v;
3125212103e5SMatthew Knepley 
31269566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &lname));
31279566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
31289566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "dim", &isDim));
31299566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "celltype", &isCelltype));
31309566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "vtk", &isVTK));
3131d56405f8SMatthew G. Knepley     if (isDepth || isDim || isCelltype || isVTK) continue;
31329566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(subdm, lname));
31339566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, lname, &label));
31349566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(subdm, lname, &newlabel));
31359566063dSJacob Faibussowitsch     PetscCall(DMLabelGetDefaultValue(label, &v));
31369566063dSJacob Faibussowitsch     PetscCall(DMLabelSetDefaultValue(newlabel, v));
31379566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
31389566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &Nv));
31399566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
3140212103e5SMatthew Knepley     for (v = 0; v < Nv; ++v) {
3141212103e5SMatthew Knepley       IS              pointIS;
3142212103e5SMatthew Knepley       const PetscInt *points;
3143212103e5SMatthew Knepley       PetscInt        Np, p;
3144212103e5SMatthew Knepley 
31459566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
31469566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &Np));
31479566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
3148212103e5SMatthew Knepley       for (p = 0; p < Np; ++p) {
3149212103e5SMatthew Knepley         const PetscInt point = points[p];
3150212103e5SMatthew Knepley         PetscInt       subp;
3151212103e5SMatthew Knepley 
31529566063dSJacob Faibussowitsch         PetscCall(DMPlexGetPointDepth(dm, point, &d));
3153212103e5SMatthew Knepley         subp = DMPlexFilterPoint_Internal(point, firstSubPoint[d], numSubPoints[d], subpoints[d]);
31549566063dSJacob Faibussowitsch         if (subp >= 0) PetscCall(DMLabelSetValue(newlabel, subp, values[v]));
3155212103e5SMatthew Knepley       }
31569566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(pointIS, &points));
31579566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
3158212103e5SMatthew Knepley     }
31599566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
31609566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
3161212103e5SMatthew Knepley   }
3162212103e5SMatthew Knepley   PetscFunctionReturn(0);
3163212103e5SMatthew Knepley }
3164212103e5SMatthew Knepley 
3165158acfadSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmeshGeneric_Interpolated(DM dm, DMLabel label, PetscInt value, PetscBool markedFaces, PetscBool isCohesive, PetscInt cellHeight, DM subdm)
3166e6ccafaeSMatthew G Knepley {
316782f516ccSBarry Smith   MPI_Comm         comm;
316853156dfcSMatthew G. Knepley   DMLabel          subpointMap;
3169efa14ee0SMatthew G Knepley   IS              *subpointIS;
3170efa14ee0SMatthew G Knepley   const PetscInt **subpoints;
31713982b651SMatthew G. Knepley   PetscInt        *numSubPoints, *firstSubPoint, *coneNew, *orntNew;
3172412e9a14SMatthew G. Knepley   PetscInt         totSubPoints = 0, maxConeSize, dim, p, d, v;
31730d366550SMatthew G. Knepley   PetscMPIInt      rank;
3174e6ccafaeSMatthew G Knepley 
3175e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
31769566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm,&comm));
31779566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
3178efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
31799566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
31809566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
3181bec263e5SMatthew G. Knepley   if (cellHeight) {
31829566063dSJacob Faibussowitsch     if (isCohesive) PetscCall(DMPlexMarkCohesiveSubmesh_Interpolated(dm, label, value, subpointMap, subdm));
31839566063dSJacob Faibussowitsch     else            PetscCall(DMPlexMarkSubmesh_Interpolated(dm, label, value, markedFaces, subpointMap, subdm));
3184bec263e5SMatthew G. Knepley   } else {
3185bec263e5SMatthew G. Knepley     DMLabel         depth;
3186bec263e5SMatthew G. Knepley     IS              pointIS;
3187bec263e5SMatthew G. Knepley     const PetscInt *points;
3188b85c8bf9SMatthew G. Knepley     PetscInt        numPoints=0;
3189bec263e5SMatthew G. Knepley 
31909566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthLabel(dm, &depth));
31919566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, value, &pointIS));
3192b85c8bf9SMatthew G. Knepley     if (pointIS) {
31939566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
31949566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &numPoints));
3195b85c8bf9SMatthew G. Knepley     }
3196bec263e5SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
3197bec263e5SMatthew G. Knepley       PetscInt *closure = NULL;
3198bec263e5SMatthew G. Knepley       PetscInt  closureSize, c, pdim;
3199bec263e5SMatthew G. Knepley 
32009566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure));
3201bec263e5SMatthew G. Knepley       for (c = 0; c < closureSize*2; c += 2) {
32029566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(depth, closure[c], &pdim));
32039566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(subpointMap, closure[c], pdim));
3204bec263e5SMatthew G. Knepley       }
32059566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure));
3206bec263e5SMatthew G. Knepley     }
32079566063dSJacob Faibussowitsch     if (pointIS) PetscCall(ISRestoreIndices(pointIS, &points));
32089566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
3209bec263e5SMatthew G. Knepley   }
3210efa14ee0SMatthew G Knepley   /* Setup chart */
32119566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
32129566063dSJacob Faibussowitsch   PetscCall(PetscMalloc4(dim+1,&numSubPoints,dim+1,&firstSubPoint,dim+1,&subpointIS,dim+1,&subpoints));
3213e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
32149566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d]));
3215e6ccafaeSMatthew G Knepley     totSubPoints += numSubPoints[d];
3216e6ccafaeSMatthew G Knepley   }
32179566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, totSubPoints));
32189566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, cellHeight));
3219e6ccafaeSMatthew G Knepley   /* Set cone sizes */
3220e6ccafaeSMatthew G Knepley   firstSubPoint[dim] = 0;
3221e6ccafaeSMatthew G Knepley   firstSubPoint[0]   = firstSubPoint[dim] + numSubPoints[dim];
3222e6ccafaeSMatthew G Knepley   if (dim > 1) {firstSubPoint[dim-1] = firstSubPoint[0]     + numSubPoints[0];}
3223e6ccafaeSMatthew G Knepley   if (dim > 2) {firstSubPoint[dim-2] = firstSubPoint[dim-1] + numSubPoints[dim-1];}
3224e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
32259566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(subpointMap, d, &subpointIS[d]));
32269566063dSJacob Faibussowitsch     if (subpointIS[d]) PetscCall(ISGetIndices(subpointIS[d], &subpoints[d]));
3227e6ccafaeSMatthew G Knepley   }
3228412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
32299566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(subdm, "celltype"));
3230e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
3231e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3232e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3233e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
3234e6ccafaeSMatthew G Knepley       const PetscInt *cone;
323515100a53SVaclav Hapla       PetscInt        coneSize;
3236e6ccafaeSMatthew G Knepley 
32379566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, point, &coneSize));
3238bec263e5SMatthew G. Knepley       if (cellHeight && (d == dim)) {
323915100a53SVaclav Hapla         PetscInt coneSizeNew, c, val;
324015100a53SVaclav Hapla 
32419566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCone(dm, point, &cone));
3242e6ccafaeSMatthew G Knepley         for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
32439566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(subpointMap, cone[c], &val));
3244e6ccafaeSMatthew G Knepley           if (val >= 0) coneSizeNew++;
3245e6ccafaeSMatthew G Knepley         }
32469566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(subdm, subpoint, coneSizeNew));
32479566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(subdm, subpoint, DM_POLYTOPE_FV_GHOST));
324815100a53SVaclav Hapla       } else {
324915100a53SVaclav Hapla         DMPolytopeType  ct;
325015100a53SVaclav Hapla 
325115100a53SVaclav Hapla         PetscCall(DMPlexSetConeSize(subdm, subpoint, coneSize));
325215100a53SVaclav Hapla         PetscCall(DMPlexGetCellType(dm, point, &ct));
325315100a53SVaclav Hapla         PetscCall(DMPlexSetCellType(subdm, subpoint, ct));
3254e6ccafaeSMatthew G Knepley       }
3255e6ccafaeSMatthew G Knepley     }
3256e6ccafaeSMatthew G Knepley   }
32579566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&subpointMap));
32589566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3259e6ccafaeSMatthew G Knepley   /* Set cones */
32609566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
32619566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(maxConeSize,&coneNew,maxConeSize,&orntNew));
3262e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
3263e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3264e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3265e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
32660e49e2e2SMatthew G. Knepley       const PetscInt *cone, *ornt;
32670d366550SMatthew G. Knepley       PetscInt        coneSize, subconeSize, coneSizeNew, c, subc, fornt = 0;
3268e6ccafaeSMatthew G Knepley 
32690d366550SMatthew G. Knepley       if (d == dim-1) {
32700d366550SMatthew G. Knepley         const PetscInt *support, *cone, *ornt;
32710d366550SMatthew G. Knepley         PetscInt        supportSize, coneSize, s, subc;
32720d366550SMatthew G. Knepley 
32739566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, point, &support));
32749566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
32750d366550SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
3276412e9a14SMatthew G. Knepley           PetscBool isHybrid;
3277412e9a14SMatthew G. Knepley 
32789566063dSJacob Faibussowitsch           PetscCall(DMPlexCellIsHybrid_Internal(dm, support[s], &isHybrid));
3279412e9a14SMatthew G. Knepley           if (!isHybrid) continue;
32809566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSubPoints[d+1], subpoints[d+1], &subc));
32810d366550SMatthew G. Knepley           if (subc >= 0) {
32820d366550SMatthew G. Knepley             const PetscInt ccell = subpoints[d+1][subc];
32830d366550SMatthew G. Knepley 
32849566063dSJacob Faibussowitsch             PetscCall(DMPlexGetCone(dm, ccell, &cone));
32859566063dSJacob Faibussowitsch             PetscCall(DMPlexGetConeSize(dm, ccell, &coneSize));
32869566063dSJacob Faibussowitsch             PetscCall(DMPlexGetConeOrientation(dm, ccell, &ornt));
32870d366550SMatthew G. Knepley             for (c = 0; c < coneSize; ++c) {
32880d366550SMatthew G. Knepley               if (cone[c] == point) {
32890d366550SMatthew G. Knepley                 fornt = ornt[c];
32900d366550SMatthew G. Knepley                 break;
32910d366550SMatthew G. Knepley               }
32920d366550SMatthew G. Knepley             }
32930d366550SMatthew G. Knepley             break;
32940d366550SMatthew G. Knepley           }
32950d366550SMatthew G. Knepley         }
32960d366550SMatthew G. Knepley       }
32979566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, point, &coneSize));
32989566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(subdm, subpoint, &subconeSize));
32999566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, point, &cone));
33009566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeOrientation(dm, point, &ornt));
3301e6ccafaeSMatthew G Knepley       for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
33029566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[c], numSubPoints[d-1], subpoints[d-1], &subc));
330301a2673eSMatthew G. Knepley         if (subc >= 0) {
330401a2673eSMatthew G. Knepley           coneNew[coneSizeNew] = firstSubPoint[d-1] + subc;
33053982b651SMatthew G. Knepley           orntNew[coneSizeNew] = ornt[c];
330601a2673eSMatthew G. Knepley           ++coneSizeNew;
330701a2673eSMatthew G. Knepley         }
3308e6ccafaeSMatthew G Knepley       }
330908401ef6SPierre Jolivet       PetscCheck(coneSizeNew == subconeSize,comm, PETSC_ERR_PLIB, "Number of cone points located %d does not match subcone size %d", coneSizeNew, subconeSize);
33109566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(subdm, subpoint, coneNew));
33119566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeOrientation(subdm, subpoint, orntNew));
33129566063dSJacob Faibussowitsch       if (fornt < 0) PetscCall(DMPlexOrientPoint(subdm, subpoint, fornt));
3313e6ccafaeSMatthew G Knepley     }
3314e6ccafaeSMatthew G Knepley   }
33159566063dSJacob Faibussowitsch   PetscCall(PetscFree2(coneNew,orntNew));
33169566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
33179566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3318e6ccafaeSMatthew G Knepley   /* Build coordinates */
3319e6ccafaeSMatthew G Knepley   {
3320e6ccafaeSMatthew G Knepley     PetscSection coordSection, subCoordSection;
3321e6ccafaeSMatthew G Knepley     Vec          coordinates, subCoordinates;
3322e6ccafaeSMatthew G Knepley     PetscScalar *coords, *subCoords;
3323c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize;
332424640c55SToby Isaac     const char  *name;
3325e6ccafaeSMatthew G Knepley 
33269566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDim(dm, &cdim));
33279566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
33289566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
33299566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
33309566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
33319566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
33329566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
33339566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubPoint[0], firstSubPoint[0]+numSubPoints[0]));
3334e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3335e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3336e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0]+v;
3337e6ccafaeSMatthew G Knepley       PetscInt       dof;
3338e6ccafaeSMatthew G Knepley 
33399566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
33409566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
33419566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3342e6ccafaeSMatthew G Knepley     }
33439566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
33449566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
33459566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
33469566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates,&name));
33479566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates,name));
33489566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
33499566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(subCoordinates, cdim));
33509566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates,VECSTANDARD));
33519566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates,    &coords));
33529566063dSJacob Faibussowitsch     PetscCall(VecGetArray(subCoordinates, &subCoords));
3353e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3354e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3355e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0]+v;
3356e6ccafaeSMatthew G Knepley       PetscInt dof, off, sdof, soff, d;
3357e6ccafaeSMatthew G Knepley 
33589566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
33599566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
33609566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
33619566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
336208401ef6SPierre Jolivet       PetscCheck(dof == sdof,comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
3363efa14ee0SMatthew G Knepley       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3364e6ccafaeSMatthew G Knepley     }
33659566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates,    &coords));
33669566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(subCoordinates, &subCoords));
33679566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
33689566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3369e6ccafaeSMatthew G Knepley   }
33703982b651SMatthew G. Knepley   /* Build SF: We need this complexity because subpoints might not be selected on the owning process */
33713982b651SMatthew G. Knepley   {
33723982b651SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
33733982b651SMatthew G. Knepley     IS                 subpIS;
33743982b651SMatthew G. Knepley     const PetscSFNode *remotePoints;
33753982b651SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
33763982b651SMatthew G. Knepley     const PetscInt    *localPoints, *subpoints;
33773982b651SMatthew G. Knepley     PetscInt          *slocalPoints;
33783982b651SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubpoints = 0, numSubroots, numSubleaves = 0, l, sl, ll, pStart, pEnd, p;
33793982b651SMatthew G. Knepley     PetscMPIInt        rank;
33803982b651SMatthew G. Knepley 
33819566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank));
33829566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm, &sfPoint));
33839566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(subdm, &sfPointSub));
33849566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
33859566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(subdm, NULL, &numSubroots));
33869566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(subdm, &subpIS));
33873982b651SMatthew G. Knepley     if (subpIS) {
33889566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subpIS, &subpoints));
33899566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(subpIS, &numSubpoints));
33903982b651SMatthew G. Knepley     }
33919566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints));
33923982b651SMatthew G. Knepley     if (numRoots >= 0) {
33939566063dSJacob Faibussowitsch       PetscCall(PetscMalloc2(pEnd-pStart,&newLocalPoints,numRoots,&newOwners));
33943982b651SMatthew G. Knepley       for (p = 0; p < pEnd-pStart; ++p) {
33953982b651SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
33963982b651SMatthew G. Knepley         newLocalPoints[p].index = -2;
33973982b651SMatthew G. Knepley       }
33983982b651SMatthew G. Knepley       /* Set subleaves */
33993982b651SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
34003982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
34013982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(point, 0, numSubpoints, subpoints);
34023982b651SMatthew G. Knepley 
34033982b651SMatthew G. Knepley         if (subpoint < 0) continue;
34043982b651SMatthew G. Knepley         newLocalPoints[point-pStart].rank  = rank;
34053982b651SMatthew G. Knepley         newLocalPoints[point-pStart].index = subpoint;
34063982b651SMatthew G. Knepley         ++numSubleaves;
34073982b651SMatthew G. Knepley       }
34083982b651SMatthew G. Knepley       /* Must put in owned subpoints */
34093982b651SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
34103982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(p, 0, numSubpoints, subpoints);
34113982b651SMatthew G. Knepley 
34123982b651SMatthew G. Knepley         if (subpoint < 0) {
34133982b651SMatthew G. Knepley           newOwners[p-pStart].rank  = -3;
34143982b651SMatthew G. Knepley           newOwners[p-pStart].index = -3;
34153982b651SMatthew G. Knepley         } else {
34163982b651SMatthew G. Knepley           newOwners[p-pStart].rank  = rank;
34173982b651SMatthew G. Knepley           newOwners[p-pStart].index = subpoint;
34183982b651SMatthew G. Knepley         }
34193982b651SMatthew G. Knepley       }
34209566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
34219566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
34229566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints,MPI_REPLACE));
34239566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints,MPI_REPLACE));
34249566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubleaves, &slocalPoints));
34259566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubleaves, &sremotePoints));
34263982b651SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
34273982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
34283982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(point, 0, numSubpoints, subpoints);
34293982b651SMatthew G. Knepley 
34303982b651SMatthew G. Knepley         if (subpoint < 0) continue;
34313982b651SMatthew G. Knepley         if (newLocalPoints[point].rank == rank) {++ll; continue;}
34323982b651SMatthew G. Knepley         slocalPoints[sl]        = subpoint;
34333982b651SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
34343982b651SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
343508401ef6SPierre Jolivet         PetscCheck(sremotePoints[sl].rank  >= 0,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %d", point);
343608401ef6SPierre Jolivet         PetscCheck(sremotePoints[sl].index >= 0,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %d", point);
34373982b651SMatthew G. Knepley         ++sl;
34383982b651SMatthew G. Knepley       }
34392c71b3e2SJacob Faibussowitsch       PetscCheckFalse(sl + ll != numSubleaves,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %d + %d != %d", sl, ll, numSubleaves);
34409566063dSJacob Faibussowitsch       PetscCall(PetscFree2(newLocalPoints,newOwners));
34419566063dSJacob Faibussowitsch       PetscCall(PetscSFSetGraph(sfPointSub, numSubroots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER));
34423982b651SMatthew G. Knepley     }
34433982b651SMatthew G. Knepley     if (subpIS) {
34449566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(subpIS, &subpoints));
34453982b651SMatthew G. Knepley     }
34463982b651SMatthew G. Knepley   }
3447212103e5SMatthew Knepley   /* Filter labels */
34489566063dSJacob Faibussowitsch   PetscCall(DMPlexFilterLabels_Internal(dm, numSubPoints, subpoints, firstSubPoint, subdm));
3449efa14ee0SMatthew G Knepley   /* Cleanup */
3450e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
34519566063dSJacob Faibussowitsch     if (subpointIS[d]) PetscCall(ISRestoreIndices(subpointIS[d], &subpoints[d]));
34529566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&subpointIS[d]));
3453e6ccafaeSMatthew G Knepley   }
34549566063dSJacob Faibussowitsch   PetscCall(PetscFree4(numSubPoints,firstSubPoint,subpointIS,subpoints));
3455e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3456e6ccafaeSMatthew G Knepley }
3457e6ccafaeSMatthew G Knepley 
3458158acfadSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM subdm)
34593982b651SMatthew G. Knepley {
34603982b651SMatthew G. Knepley   PetscFunctionBegin;
34619566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, vertexLabel, value, markedFaces, PETSC_FALSE, 1, subdm));
34623982b651SMatthew G. Knepley   PetscFunctionReturn(0);
34633982b651SMatthew G. Knepley }
34643982b651SMatthew G. Knepley 
3465d0fa310fSMatthew G. Knepley /*@
3466e6ccafaeSMatthew G Knepley   DMPlexCreateSubmesh - Extract a hypersurface from the mesh using vertices defined by a label
3467e6ccafaeSMatthew G Knepley 
3468e6ccafaeSMatthew G Knepley   Input Parameters:
3469e6ccafaeSMatthew G Knepley + dm           - The original mesh
3470158acfadSMatthew G. Knepley . vertexLabel  - The DMLabel marking points contained in the surface
3471158acfadSMatthew G. Knepley . value        - The label value to use
3472158acfadSMatthew G. Knepley - markedFaces  - PETSC_TRUE if surface faces are marked in addition to vertices, PETSC_FALSE if only vertices are marked
3473e6ccafaeSMatthew G Knepley 
3474e6ccafaeSMatthew G Knepley   Output Parameter:
3475e6ccafaeSMatthew G Knepley . subdm - The surface mesh
3476e6ccafaeSMatthew G Knepley 
3477e6ccafaeSMatthew G Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3478e6ccafaeSMatthew G Knepley 
3479e6ccafaeSMatthew G Knepley   Level: developer
3480e6ccafaeSMatthew G Knepley 
3481c58f1c22SToby Isaac .seealso: DMPlexGetSubpointMap(), DMGetLabel(), DMLabelSetValue()
3482830e53efSMatthew G. Knepley @*/
3483158acfadSMatthew G. Knepley PetscErrorCode DMPlexCreateSubmesh(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM *subdm)
3484e6ccafaeSMatthew G Knepley {
3485827c4036SVaclav Hapla   DMPlexInterpolatedFlag interpolated;
3486827c4036SVaclav Hapla   PetscInt       dim, cdim;
3487e6ccafaeSMatthew G Knepley 
3488e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3489e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3490064a246eSJacob Faibussowitsch   PetscValidPointer(subdm, 5);
34919566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
34929566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
34939566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
34949566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*subdm, dim-1));
34959566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
34969566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*subdm, cdim));
34979566063dSJacob Faibussowitsch   PetscCall(DMPlexIsInterpolated(dm, &interpolated));
349808401ef6SPierre Jolivet   PetscCheck(interpolated != DMPLEX_INTERPOLATED_PARTIAL,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not for partially interpolated meshes");
3499827c4036SVaclav Hapla   if (interpolated) {
35009566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateSubmesh_Interpolated(dm, vertexLabel, value, markedFaces, *subdm));
3501e6ccafaeSMatthew G Knepley   } else {
35029566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateSubmesh_Uninterpolated(dm, vertexLabel, value, *subdm));
3503e6ccafaeSMatthew G Knepley   }
35049566063dSJacob Faibussowitsch   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, *subdm));
3505e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3506e6ccafaeSMatthew G Knepley }
3507e6ccafaeSMatthew G Knepley 
350827c04023SMatthew G. Knepley static PetscErrorCode DMPlexCreateCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM subdm)
3509766ab985SMatthew G. Knepley {
3510766ab985SMatthew G. Knepley   MPI_Comm        comm;
3511766ab985SMatthew G. Knepley   DMLabel         subpointMap;
3512766ab985SMatthew G. Knepley   IS              subvertexIS;
3513766ab985SMatthew G. Knepley   const PetscInt *subVertices;
3514fed694aaSMatthew G. Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells, *subCells = NULL;
3515766ab985SMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces, firstSubFace, newFacePoint, nFV;
3516412e9a14SMatthew G. Knepley   PetscInt        c, f;
3517766ab985SMatthew G. Knepley 
3518766ab985SMatthew G. Knepley   PetscFunctionBegin;
35199566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
3520766ab985SMatthew G. Knepley   /* Create subpointMap which marks the submesh */
35219566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
35229566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
35239566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&subpointMap));
35249566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, subpointMap, &numSubFaces, &nFV, &subCells, subdm));
3525766ab985SMatthew G. Knepley   /* Setup chart */
35269566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 0, &numSubVertices));
35279566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 2, &numSubCells));
35289566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices));
35299566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, 1));
3530766ab985SMatthew G. Knepley   /* Set cone sizes */
3531766ab985SMatthew G. Knepley   firstSubVertex = numSubCells;
3532766ab985SMatthew G. Knepley   firstSubFace   = numSubCells+numSubVertices;
3533766ab985SMatthew G. Knepley   newFacePoint   = firstSubFace;
35349566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 0, &subvertexIS));
35359566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISGetIndices(subvertexIS, &subVertices));
3536766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
35379566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeSize(subdm, c, 1));
3538766ab985SMatthew G. Knepley   }
3539766ab985SMatthew G. Knepley   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
35409566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeSize(subdm, f, nFV));
3541766ab985SMatthew G. Knepley   }
35429566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3543766ab985SMatthew G. Knepley   /* Create face cones */
35449566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
35459566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface));
3546766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3547766ab985SMatthew G. Knepley     const PetscInt  cell    = subCells[c];
3548766ab985SMatthew G. Knepley     const PetscInt  subcell = c;
354987feddfdSMatthew G. Knepley     const PetscInt *cone, *cells;
3550412e9a14SMatthew G. Knepley     PetscBool       isHybrid;
355187feddfdSMatthew G. Knepley     PetscInt        numCells, subVertex, p, v;
3552766ab985SMatthew G. Knepley 
35539566063dSJacob Faibussowitsch     PetscCall(DMPlexCellIsHybrid_Internal(dm, cell, &isHybrid));
3554412e9a14SMatthew G. Knepley     if (!isHybrid) continue;
35559566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, cell, &cone));
355687feddfdSMatthew G. Knepley     for (v = 0; v < nFV; ++v) {
35579566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(cone[v], numSubVertices, subVertices, &subVertex));
355887feddfdSMatthew G. Knepley       subface[v] = firstSubVertex+subVertex;
355987feddfdSMatthew G. Knepley     }
35609566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, newFacePoint, subface));
35619566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, &newFacePoint));
35629566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, nFV, cone, &numCells, &cells));
356327234c99SMatthew G. Knepley     /* Not true in parallel
356408401ef6SPierre Jolivet     PetscCheck(numCells == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
356587feddfdSMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
356687feddfdSMatthew G. Knepley       PetscInt  negsubcell;
3567412e9a14SMatthew G. Knepley       PetscBool isHybrid;
3568766ab985SMatthew G. Knepley 
35699566063dSJacob Faibussowitsch       PetscCall(DMPlexCellIsHybrid_Internal(dm, cells[p], &isHybrid));
3570412e9a14SMatthew G. Knepley       if (isHybrid) continue;
357187feddfdSMatthew G. Knepley       /* I know this is a crap search */
357287feddfdSMatthew G. Knepley       for (negsubcell = 0; negsubcell < numSubCells; ++negsubcell) {
357387feddfdSMatthew G. Knepley         if (subCells[negsubcell] == cells[p]) break;
3574766ab985SMatthew G. Knepley       }
357508401ef6SPierre Jolivet       PetscCheck(negsubcell != numSubCells,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find negative face neighbor for cohesive cell %d", cell);
35769566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(subdm, negsubcell, &newFacePoint));
3577766ab985SMatthew G. Knepley     }
35789566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, nFV, cone, &numCells, &cells));
357987feddfdSMatthew G. Knepley     ++newFacePoint;
3580766ab985SMatthew G. Knepley   }
35819566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface));
35829566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
35839566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3584766ab985SMatthew G. Knepley   /* Build coordinates */
3585766ab985SMatthew G. Knepley   {
3586766ab985SMatthew G. Knepley     PetscSection coordSection, subCoordSection;
3587766ab985SMatthew G. Knepley     Vec          coordinates, subCoordinates;
3588766ab985SMatthew G. Knepley     PetscScalar *coords, *subCoords;
3589c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize, v;
359024640c55SToby Isaac     const char  *name;
3591766ab985SMatthew G. Knepley 
35929566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDim(dm, &cdim));
35939566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
35949566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
35959566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
35969566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
35979566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
35989566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
35999566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices));
3600766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3601766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3602766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex+v;
3603766ab985SMatthew G. Knepley       PetscInt       dof;
3604766ab985SMatthew G. Knepley 
36059566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
36069566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
36079566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3608766ab985SMatthew G. Knepley     }
36099566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
36109566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
36119566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
36129566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates,&name));
36139566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates,name));
36149566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
36159566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(subCoordinates, cdim));
36169566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates,VECSTANDARD));
36179566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates,    &coords));
36189566063dSJacob Faibussowitsch     PetscCall(VecGetArray(subCoordinates, &subCoords));
3619766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3620766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3621766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex+v;
3622766ab985SMatthew G. Knepley       PetscInt       dof, off, sdof, soff, d;
3623766ab985SMatthew G. Knepley 
36249566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
36259566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
36269566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
36279566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
362808401ef6SPierre Jolivet       PetscCheck(dof == sdof,comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
3629766ab985SMatthew G. Knepley       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3630766ab985SMatthew G. Knepley     }
36319566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates,    &coords));
36329566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(subCoordinates, &subCoords));
36339566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
36349566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3635766ab985SMatthew G. Knepley   }
3636aca35d17SMatthew G. Knepley   /* Build SF */
3637aca35d17SMatthew G. Knepley   CHKMEMQ;
3638aca35d17SMatthew G. Knepley   {
3639aca35d17SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
3640aca35d17SMatthew G. Knepley     const PetscSFNode *remotePoints;
3641bdcf2095SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
3642aca35d17SMatthew G. Knepley     const PetscInt    *localPoints;
3643bdcf2095SMatthew G. Knepley     PetscInt          *slocalPoints;
364449c26ae4SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubRoots = numSubCells+numSubFaces+numSubVertices, numSubLeaves = 0, l, sl, ll, pStart, pEnd, p, vStart, vEnd;
3645bdcf2095SMatthew G. Knepley     PetscMPIInt        rank;
3646aca35d17SMatthew G. Knepley 
36479566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank));
36489566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm, &sfPoint));
36499566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(subdm, &sfPointSub));
36509566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
36519566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
36529566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints));
3653aca35d17SMatthew G. Knepley     if (numRoots >= 0) {
3654aca35d17SMatthew G. Knepley       /* Only vertices should be shared */
36559566063dSJacob Faibussowitsch       PetscCall(PetscMalloc2(pEnd-pStart,&newLocalPoints,numRoots,&newOwners));
3656bdcf2095SMatthew G. Knepley       for (p = 0; p < pEnd-pStart; ++p) {
3657bdcf2095SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
3658bdcf2095SMatthew G. Knepley         newLocalPoints[p].index = -2;
3659bdcf2095SMatthew G. Knepley       }
36609e0823b2SMatthew G. Knepley       /* Set subleaves */
3661aca35d17SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
3662aca35d17SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3663bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3664aca35d17SMatthew G. Knepley 
366508401ef6SPierre Jolivet         PetscCheck(!(point < vStart) || !(point >= vEnd),PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Should not be mapping anything but vertices, %d", point);
3666bdcf2095SMatthew G. Knepley         if (subPoint < 0) continue;
3667bdcf2095SMatthew G. Knepley         newLocalPoints[point-pStart].rank  = rank;
3668bdcf2095SMatthew G. Knepley         newLocalPoints[point-pStart].index = subPoint;
3669bdcf2095SMatthew G. Knepley         ++numSubLeaves;
3670aca35d17SMatthew G. Knepley       }
36719e0823b2SMatthew G. Knepley       /* Must put in owned subpoints */
36729e0823b2SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
36739e0823b2SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(p, firstSubVertex, numSubVertices, subVertices);
36749e0823b2SMatthew G. Knepley 
36759e0823b2SMatthew G. Knepley         if (subPoint < 0) {
36769e0823b2SMatthew G. Knepley           newOwners[p-pStart].rank  = -3;
36779e0823b2SMatthew G. Knepley           newOwners[p-pStart].index = -3;
36789e0823b2SMatthew G. Knepley         } else {
36799e0823b2SMatthew G. Knepley           newOwners[p-pStart].rank  = rank;
36809e0823b2SMatthew G. Knepley           newOwners[p-pStart].index = subPoint;
36819e0823b2SMatthew G. Knepley         }
3682bdcf2095SMatthew G. Knepley       }
36839566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
36849566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
36859566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints,MPI_REPLACE));
36869566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints,MPI_REPLACE));
36879566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubLeaves,    &slocalPoints));
36889566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubLeaves, &sremotePoints));
368949c26ae4SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
3690bdcf2095SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3691bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3692aca35d17SMatthew G. Knepley 
3693aca35d17SMatthew G. Knepley         if (subPoint < 0) continue;
369449c26ae4SMatthew G. Knepley         if (newLocalPoints[point].rank == rank) {++ll; continue;}
3695aca35d17SMatthew G. Knepley         slocalPoints[sl]        = subPoint;
3696bdcf2095SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
3697bdcf2095SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
369808401ef6SPierre Jolivet         PetscCheck(sremotePoints[sl].rank  >= 0,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %d", point);
369908401ef6SPierre Jolivet         PetscCheck(sremotePoints[sl].index >= 0,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %d", point);
3700aca35d17SMatthew G. Knepley         ++sl;
3701aca35d17SMatthew G. Knepley       }
37029566063dSJacob Faibussowitsch       PetscCall(PetscFree2(newLocalPoints,newOwners));
37032c71b3e2SJacob Faibussowitsch       PetscCheckFalse(sl + ll != numSubLeaves,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %d + %d != %d", sl, ll, numSubLeaves);
37049566063dSJacob Faibussowitsch       PetscCall(PetscSFSetGraph(sfPointSub, numSubRoots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER));
3705aca35d17SMatthew G. Knepley     }
3706aca35d17SMatthew G. Knepley   }
3707aca35d17SMatthew G. Knepley   CHKMEMQ;
3708766ab985SMatthew G. Knepley   /* Cleanup */
37099566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subVertices));
37109566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
37119566063dSJacob Faibussowitsch   PetscCall(PetscFree(subCells));
3712766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
3713766ab985SMatthew G. Knepley }
3714766ab985SMatthew G. Knepley 
37153982b651SMatthew G. Knepley static PetscErrorCode DMPlexCreateCohesiveSubmesh_Interpolated(DM dm, const char labelname[], PetscInt value, DM subdm)
3716766ab985SMatthew G. Knepley {
37173982b651SMatthew G. Knepley   DMLabel        label = NULL;
3718766ab985SMatthew G. Knepley 
3719766ab985SMatthew G. Knepley   PetscFunctionBegin;
37209566063dSJacob Faibussowitsch   if (labelname) PetscCall(DMGetLabel(dm, labelname, &label));
37219566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, label, value, PETSC_FALSE, PETSC_TRUE, 1, subdm));
3722766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
3723766ab985SMatthew G. Knepley }
3724766ab985SMatthew G. Knepley 
3725c08575a3SMatthew G. Knepley /*@C
37267d2fefb6SMatthew 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.
3727766ab985SMatthew G. Knepley 
3728766ab985SMatthew G. Knepley   Input Parameters:
3729766ab985SMatthew G. Knepley + dm          - The original mesh
373027c04023SMatthew G. Knepley . hasLagrange - The mesh has Lagrange unknowns in the cohesive cells
37317afc1a8bSJed Brown . label       - A label name, or NULL
373227c04023SMatthew G. Knepley - value  - A label value
3733766ab985SMatthew G. Knepley 
3734766ab985SMatthew G. Knepley   Output Parameter:
3735766ab985SMatthew G. Knepley . subdm - The surface mesh
3736766ab985SMatthew G. Knepley 
3737766ab985SMatthew G. Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3738766ab985SMatthew G. Knepley 
3739766ab985SMatthew G. Knepley   Level: developer
3740766ab985SMatthew G. Knepley 
3741766ab985SMatthew G. Knepley .seealso: DMPlexGetSubpointMap(), DMPlexCreateSubmesh()
3742c08575a3SMatthew G. Knepley @*/
374327c04023SMatthew G. Knepley PetscErrorCode DMPlexCreateCohesiveSubmesh(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM *subdm)
3744766ab985SMatthew G. Knepley {
3745c0e8cf5fSMatthew G. Knepley   PetscInt       dim, cdim, depth;
3746766ab985SMatthew G. Knepley 
3747766ab985SMatthew G. Knepley   PetscFunctionBegin;
3748766ab985SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
374927c04023SMatthew G. Knepley   PetscValidPointer(subdm, 5);
37509566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
37519566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
37529566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
37539566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
37549566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*subdm, dim-1));
37559566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
37569566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*subdm, cdim));
3757766ab985SMatthew G. Knepley   if (depth == dim) {
37589566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCohesiveSubmesh_Interpolated(dm, label, value, *subdm));
3759766ab985SMatthew G. Knepley   } else {
37609566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, *subdm));
3761e6ccafaeSMatthew G Knepley   }
37629566063dSJacob Faibussowitsch   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, *subdm));
3763e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3764e6ccafaeSMatthew G Knepley }
3765e6ccafaeSMatthew G Knepley 
3766bec263e5SMatthew G. Knepley /*@
3767bec263e5SMatthew G. Knepley   DMPlexFilter - Extract a subset of mesh cells defined by a label as a separate mesh
3768bec263e5SMatthew G. Knepley 
3769bec263e5SMatthew G. Knepley   Input Parameters:
3770bec263e5SMatthew G. Knepley + dm        - The original mesh
3771bec263e5SMatthew G. Knepley . cellLabel - The DMLabel marking cells contained in the new mesh
3772bec263e5SMatthew G. Knepley - value     - The label value to use
3773bec263e5SMatthew G. Knepley 
3774bec263e5SMatthew G. Knepley   Output Parameter:
3775bec263e5SMatthew G. Knepley . subdm - The new mesh
3776bec263e5SMatthew G. Knepley 
3777bec263e5SMatthew G. Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3778bec263e5SMatthew G. Knepley 
3779bec263e5SMatthew G. Knepley   Level: developer
3780bec263e5SMatthew G. Knepley 
3781c58f1c22SToby Isaac .seealso: DMPlexGetSubpointMap(), DMGetLabel(), DMLabelSetValue()
3782bec263e5SMatthew G. Knepley @*/
3783bec263e5SMatthew G. Knepley PetscErrorCode DMPlexFilter(DM dm, DMLabel cellLabel, PetscInt value, DM *subdm)
3784bec263e5SMatthew G. Knepley {
3785bec263e5SMatthew G. Knepley   PetscInt       dim;
3786bec263e5SMatthew G. Knepley 
3787bec263e5SMatthew G. Knepley   PetscFunctionBegin;
3788bec263e5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3789064a246eSJacob Faibussowitsch   PetscValidPointer(subdm, 4);
37909566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
37919566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject) dm), subdm));
37929566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
37939566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*subdm, dim));
3794bec263e5SMatthew G. Knepley   /* Extract submesh in place, could be empty on some procs, could have inconsistency if procs do not both extract a shared cell */
37959566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, cellLabel, value, PETSC_FALSE, PETSC_FALSE, 0, *subdm));
37969566063dSJacob Faibussowitsch   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, *subdm));
3797bec263e5SMatthew G. Knepley   PetscFunctionReturn(0);
3798bec263e5SMatthew G. Knepley }
3799bec263e5SMatthew G. Knepley 
380064beef6dSMatthew G. Knepley /*@
380164beef6dSMatthew G. Knepley   DMPlexGetSubpointMap - Returns a DMLabel with point dimension as values
380264beef6dSMatthew G. Knepley 
380364beef6dSMatthew G. Knepley   Input Parameter:
380464beef6dSMatthew G. Knepley . dm - The submesh DM
380564beef6dSMatthew G. Knepley 
380664beef6dSMatthew G. Knepley   Output Parameter:
380764beef6dSMatthew G. Knepley . subpointMap - The DMLabel of all the points from the original mesh in this submesh, or NULL if this is not a submesh
380864beef6dSMatthew G. Knepley 
380964beef6dSMatthew G. Knepley   Level: developer
381064beef6dSMatthew G. Knepley 
381197d8846cSMatthew Knepley .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointIS()
381264beef6dSMatthew G. Knepley @*/
3813e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexGetSubpointMap(DM dm, DMLabel *subpointMap)
3814e6ccafaeSMatthew G Knepley {
3815e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3816e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3817e6ccafaeSMatthew G Knepley   PetscValidPointer(subpointMap, 2);
381865663942SMatthew G. Knepley   *subpointMap = ((DM_Plex*) dm->data)->subpointMap;
3819e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3820e6ccafaeSMatthew G Knepley }
3821e6ccafaeSMatthew G Knepley 
3822c08575a3SMatthew G. Knepley /*@
3823c08575a3SMatthew G. Knepley   DMPlexSetSubpointMap - Sets the DMLabel with point dimension as values
3824c08575a3SMatthew G. Knepley 
3825c08575a3SMatthew G. Knepley   Input Parameters:
3826c08575a3SMatthew G. Knepley + dm - The submesh DM
3827c08575a3SMatthew G. Knepley - subpointMap - The DMLabel of all the points from the original mesh in this submesh
3828c08575a3SMatthew G. Knepley 
3829c08575a3SMatthew G. Knepley   Note: Should normally not be called by the user, since it is set in DMPlexCreateSubmesh()
3830c08575a3SMatthew G. Knepley 
3831c08575a3SMatthew G. Knepley   Level: developer
3832c08575a3SMatthew G. Knepley 
383397d8846cSMatthew Knepley .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointIS()
3834c08575a3SMatthew G. Knepley @*/
3835e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexSetSubpointMap(DM dm, DMLabel subpointMap)
3836e6ccafaeSMatthew G Knepley {
3837e6ccafaeSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
3838285d324eSMatthew G. Knepley   DMLabel        tmp;
3839e6ccafaeSMatthew G Knepley 
3840e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3841e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3842285d324eSMatthew G. Knepley   tmp  = mesh->subpointMap;
3843e6ccafaeSMatthew G Knepley   mesh->subpointMap = subpointMap;
38449566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) mesh->subpointMap));
38459566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&tmp));
3846e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3847e6ccafaeSMatthew G Knepley }
3848e6ccafaeSMatthew G Knepley 
384997d8846cSMatthew Knepley static PetscErrorCode DMPlexCreateSubpointIS_Internal(DM dm, IS *subpointIS)
385097d8846cSMatthew Knepley {
385197d8846cSMatthew Knepley   DMLabel        spmap;
385297d8846cSMatthew Knepley   PetscInt       depth, d;
385397d8846cSMatthew Knepley 
385497d8846cSMatthew Knepley   PetscFunctionBegin;
38559566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(dm, &spmap));
38569566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
385797d8846cSMatthew Knepley   if (spmap && depth >= 0) {
385897d8846cSMatthew Knepley     DM_Plex  *mesh = (DM_Plex *) dm->data;
385997d8846cSMatthew Knepley     PetscInt *points, *depths;
386097d8846cSMatthew Knepley     PetscInt  pStart, pEnd, p, off;
386197d8846cSMatthew Knepley 
38629566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
386328b400f6SJacob Faibussowitsch     PetscCheck(!pStart,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Submeshes must start the point numbering at 0, not %d", pStart);
38649566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(pEnd, &points));
38659566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(dm, depth+1, MPIU_INT, &depths));
386697d8846cSMatthew Knepley     depths[0] = depth;
386797d8846cSMatthew Knepley     depths[1] = 0;
386897d8846cSMatthew Knepley     for (d = 2; d <= depth; ++d) {depths[d] = depth+1 - d;}
386997d8846cSMatthew Knepley     for (d = 0, off = 0; d <= depth; ++d) {
387097d8846cSMatthew Knepley       const PetscInt dep = depths[d];
387197d8846cSMatthew Knepley       PetscInt       depStart, depEnd, n;
387297d8846cSMatthew Knepley 
38739566063dSJacob Faibussowitsch       PetscCall(DMPlexGetDepthStratum(dm, dep, &depStart, &depEnd));
38749566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumSize(spmap, dep, &n));
387597d8846cSMatthew Knepley       if (((d < 2) && (depth > 1)) || (d == 1)) { /* Only check vertices and cells for now since the map is broken for others */
387608401ef6SPierre Jolivet         PetscCheck(n == depEnd-depStart,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %d at depth %d should be %d", n, dep, depEnd-depStart);
387797d8846cSMatthew Knepley       } else {
387897d8846cSMatthew Knepley         if (!n) {
387997d8846cSMatthew Knepley           if (d == 0) {
388097d8846cSMatthew Knepley             /* Missing cells */
388197d8846cSMatthew Knepley             for (p = 0; p < depEnd-depStart; ++p, ++off) points[off] = -1;
388297d8846cSMatthew Knepley           } else {
388397d8846cSMatthew Knepley             /* Missing faces */
388497d8846cSMatthew Knepley             for (p = 0; p < depEnd-depStart; ++p, ++off) points[off] = PETSC_MAX_INT;
388597d8846cSMatthew Knepley           }
388697d8846cSMatthew Knepley         }
388797d8846cSMatthew Knepley       }
388897d8846cSMatthew Knepley       if (n) {
388997d8846cSMatthew Knepley         IS              is;
389097d8846cSMatthew Knepley         const PetscInt *opoints;
389197d8846cSMatthew Knepley 
38929566063dSJacob Faibussowitsch         PetscCall(DMLabelGetStratumIS(spmap, dep, &is));
38939566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(is, &opoints));
389497d8846cSMatthew Knepley         for (p = 0; p < n; ++p, ++off) points[off] = opoints[p];
38959566063dSJacob Faibussowitsch         PetscCall(ISRestoreIndices(is, &opoints));
38969566063dSJacob Faibussowitsch         PetscCall(ISDestroy(&is));
389797d8846cSMatthew Knepley       }
389897d8846cSMatthew Knepley     }
38999566063dSJacob Faibussowitsch     PetscCall(DMRestoreWorkArray(dm, depth+1, MPIU_INT, &depths));
390008401ef6SPierre Jolivet     PetscCheck(off == pEnd,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %d should be %d", off, pEnd);
39019566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, pEnd, points, PETSC_OWN_POINTER, subpointIS));
39029566063dSJacob Faibussowitsch     PetscCall(PetscObjectStateGet((PetscObject) spmap, &mesh->subpointState));
390397d8846cSMatthew Knepley   }
390497d8846cSMatthew Knepley   PetscFunctionReturn(0);
390597d8846cSMatthew Knepley }
390697d8846cSMatthew Knepley 
390764beef6dSMatthew G. Knepley /*@
390897d8846cSMatthew Knepley   DMPlexGetSubpointIS - Returns an IS covering the entire subdm chart with the original points as data
3909e6ccafaeSMatthew G Knepley 
3910e6ccafaeSMatthew G Knepley   Input Parameter:
3911e6ccafaeSMatthew G Knepley . dm - The submesh DM
3912e6ccafaeSMatthew G Knepley 
3913e6ccafaeSMatthew G Knepley   Output Parameter:
39140298fd71SBarry Smith . subpointIS - The IS of all the points from the original mesh in this submesh, or NULL if this is not a submesh
3915e6ccafaeSMatthew G Knepley 
39163982b651SMatthew G. Knepley   Note: This IS is guaranteed to be sorted by the construction of the submesh
391764beef6dSMatthew G. Knepley 
391864beef6dSMatthew G. Knepley   Level: developer
391964beef6dSMatthew G. Knepley 
392064beef6dSMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointMap()
392164beef6dSMatthew G. Knepley @*/
392297d8846cSMatthew Knepley PetscErrorCode DMPlexGetSubpointIS(DM dm, IS *subpointIS)
3923e6ccafaeSMatthew G Knepley {
392497d8846cSMatthew Knepley   DM_Plex         *mesh = (DM_Plex *) dm->data;
392597d8846cSMatthew Knepley   DMLabel          spmap;
392697d8846cSMatthew Knepley   PetscObjectState state;
3927e6ccafaeSMatthew G Knepley 
3928e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3929e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3930e6ccafaeSMatthew G Knepley   PetscValidPointer(subpointIS, 2);
39319566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(dm, &spmap));
39329566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateGet((PetscObject) spmap, &state));
39339566063dSJacob Faibussowitsch   if (state != mesh->subpointState || !mesh->subpointIS) PetscCall(DMPlexCreateSubpointIS_Internal(dm, &mesh->subpointIS));
393497d8846cSMatthew Knepley   *subpointIS = mesh->subpointIS;
3935e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3936e6ccafaeSMatthew G Knepley }
3937559a1558SMatthew G. Knepley 
3938559a1558SMatthew G. Knepley /*@
3939a6e0b375SMatthew G. Knepley   DMGetEnclosureRelation - Get the relationship between dmA and dmB
3940559a1558SMatthew G. Knepley 
3941559a1558SMatthew G. Knepley   Input Parameters:
3942a6e0b375SMatthew G. Knepley + dmA - The first DM
3943a6e0b375SMatthew G. Knepley - dmB - The second DM
3944559a1558SMatthew G. Knepley 
3945559a1558SMatthew G. Knepley   Output Parameter:
3946a6e0b375SMatthew G. Knepley . rel - The relation of dmA to dmB
3947559a1558SMatthew G. Knepley 
3948a6e0b375SMatthew G. Knepley   Level: intermediate
3949559a1558SMatthew G. Knepley 
3950446c23c1SPierre Jolivet .seealso: DMGetEnclosurePoint()
3951559a1558SMatthew G. Knepley @*/
3952a6e0b375SMatthew G. Knepley PetscErrorCode DMGetEnclosureRelation(DM dmA, DM dmB, DMEnclosureType *rel)
3953559a1558SMatthew G. Knepley {
3954a6e0b375SMatthew G. Knepley   DM             plexA, plexB, sdm;
3955559a1558SMatthew G. Knepley   DMLabel        spmap;
3956a6e0b375SMatthew G. Knepley   PetscInt       pStartA, pEndA, pStartB, pEndB, NpA, NpB;
3957559a1558SMatthew G. Knepley 
395844171101SMatthew G. Knepley   PetscFunctionBegin;
3959a6e0b375SMatthew G. Knepley   PetscValidPointer(rel, 3);
3960a6e0b375SMatthew G. Knepley   *rel = DM_ENC_NONE;
3961a6e0b375SMatthew G. Knepley   if (!dmA || !dmB) PetscFunctionReturn(0);
3962a6e0b375SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
3963064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
3964a6e0b375SMatthew G. Knepley   if (dmA == dmB) {*rel = DM_ENC_EQUALITY; PetscFunctionReturn(0);}
39659566063dSJacob Faibussowitsch   PetscCall(DMConvert(dmA, DMPLEX, &plexA));
39669566063dSJacob Faibussowitsch   PetscCall(DMConvert(dmB, DMPLEX, &plexB));
39679566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(plexA, &pStartA, &pEndA));
39689566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(plexB, &pStartB, &pEndB));
3969a6e0b375SMatthew G. Knepley   /* Assumption 1: subDMs have smaller charts than the DMs that they originate from
3970a6e0b375SMatthew G. Knepley     - The degenerate case of a subdomain which includes all of the domain on some process can be treated as equality */
3971a6e0b375SMatthew G. Knepley   if ((pStartA == pStartB) && (pEndA == pEndB)) {
3972a6e0b375SMatthew G. Knepley     *rel = DM_ENC_EQUALITY;
3973a6e0b375SMatthew G. Knepley     goto end;
3974559a1558SMatthew G. Knepley   }
3975a6e0b375SMatthew G. Knepley   NpA = pEndA - pStartA;
3976a6e0b375SMatthew G. Knepley   NpB = pEndB - pStartB;
3977a6e0b375SMatthew G. Knepley   if (NpA == NpB) goto end;
3978a6e0b375SMatthew G. Knepley   sdm = NpA > NpB ? plexB : plexA; /* The other is the original, enclosing dm */
39799566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(sdm, &spmap));
3980a6e0b375SMatthew G. Knepley   if (!spmap) goto end;
3981a6e0b375SMatthew G. Knepley   /* TODO Check the space mapped to by subpointMap is same size as dm */
3982a6e0b375SMatthew G. Knepley   if (NpA > NpB) {
3983a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUPERMESH;
3984a6e0b375SMatthew G. Knepley   } else {
3985a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUBMESH;
3986a6e0b375SMatthew G. Knepley   }
3987a6e0b375SMatthew G. Knepley   end:
39889566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plexA));
39899566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plexB));
3990559a1558SMatthew G. Knepley   PetscFunctionReturn(0);
3991559a1558SMatthew G. Knepley }
399244171101SMatthew G. Knepley 
399344171101SMatthew G. Knepley /*@
3994a6e0b375SMatthew G. Knepley   DMGetEnclosurePoint - Get the point pA in dmA which corresponds to the point pB in dmB
399544171101SMatthew G. Knepley 
399644171101SMatthew G. Knepley   Input Parameters:
3997a6e0b375SMatthew G. Knepley + dmA   - The first DM
3998a6e0b375SMatthew G. Knepley . dmB   - The second DM
3999a6e0b375SMatthew G. Knepley . etype - The type of enclosure relation that dmA has to dmB
4000a6e0b375SMatthew G. Knepley - pB    - A point of dmB
400144171101SMatthew G. Knepley 
400244171101SMatthew G. Knepley   Output Parameter:
4003a6e0b375SMatthew G. Knepley . pA    - The corresponding point of dmA
400444171101SMatthew G. Knepley 
4005a6e0b375SMatthew G. Knepley   Level: intermediate
400644171101SMatthew G. Knepley 
4007a6e0b375SMatthew G. Knepley .seealso: DMGetEnclosureRelation()
400844171101SMatthew G. Knepley @*/
4009a6e0b375SMatthew G. Knepley PetscErrorCode DMGetEnclosurePoint(DM dmA, DM dmB, DMEnclosureType etype, PetscInt pB, PetscInt *pA)
401044171101SMatthew G. Knepley {
4011a6e0b375SMatthew G. Knepley   DM              sdm;
4012a6e0b375SMatthew G. Knepley   IS              subpointIS;
4013a6e0b375SMatthew G. Knepley   const PetscInt *subpoints;
4014a6e0b375SMatthew G. Knepley   PetscInt        numSubpoints;
401544171101SMatthew G. Knepley 
401644171101SMatthew G. Knepley   PetscFunctionBegin;
4017a6e0b375SMatthew G. Knepley   /* TODO Cache the IS, making it look like an index */
4018a6e0b375SMatthew G. Knepley   switch (etype) {
4019a6e0b375SMatthew G. Knepley     case DM_ENC_SUPERMESH:
4020a6e0b375SMatthew G. Knepley     sdm  = dmB;
40219566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(sdm, &subpointIS));
40229566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subpointIS, &subpoints));
4023a6e0b375SMatthew G. Knepley     *pA  = subpoints[pB];
40249566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(subpointIS, &subpoints));
4025a6e0b375SMatthew G. Knepley     break;
4026a6e0b375SMatthew G. Knepley     case DM_ENC_SUBMESH:
4027a6e0b375SMatthew G. Knepley     sdm  = dmA;
40289566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(sdm, &subpointIS));
40299566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(subpointIS, &numSubpoints));
40309566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subpointIS, &subpoints));
40319566063dSJacob Faibussowitsch     PetscCall(PetscFindInt(pB, numSubpoints, subpoints, pA));
4032a6e0b375SMatthew G. Knepley     if (*pA < 0) {
40339566063dSJacob Faibussowitsch       PetscCall(DMViewFromOptions(dmA, NULL, "-dm_enc_A_view"));
40349566063dSJacob Faibussowitsch       PetscCall(DMViewFromOptions(dmB, NULL, "-dm_enc_B_view"));
403598921bdaSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %d not found in submesh", pB);
4036a6e0b375SMatthew G. Knepley     }
40379566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(subpointIS, &subpoints));
4038a6e0b375SMatthew G. Knepley     break;
4039a6e0b375SMatthew G. Knepley     case DM_ENC_EQUALITY:
4040a6e0b375SMatthew G. Knepley     case DM_ENC_NONE:
4041a6e0b375SMatthew G. Knepley     *pA = pB;break;
4042a6e0b375SMatthew G. Knepley     case DM_ENC_UNKNOWN:
4043a6e0b375SMatthew G. Knepley     {
4044a6e0b375SMatthew G. Knepley       DMEnclosureType enc;
404544171101SMatthew G. Knepley 
40469566063dSJacob Faibussowitsch       PetscCall(DMGetEnclosureRelation(dmA, dmB, &enc));
40479566063dSJacob Faibussowitsch       PetscCall(DMGetEnclosurePoint(dmA, dmB, enc, pB, pA));
4048a6e0b375SMatthew G. Knepley     }
4049a6e0b375SMatthew G. Knepley     break;
405098921bdaSJacob Faibussowitsch     default: SETERRQ(PetscObjectComm((PetscObject) dmA), PETSC_ERR_ARG_OUTOFRANGE, "Invalid enclosure type %d", (int) etype);
405144171101SMatthew G. Knepley   }
405244171101SMatthew G. Knepley   PetscFunctionReturn(0);
405344171101SMatthew G. Knepley }
4054