xref: /petsc/src/dm/impls/plex/plexsubmesh.c (revision 4fb89dddf56594b92bdd2ca7e24874fafe134f45)
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 
88db781477SPatrick Sanan .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 
195db781477SPatrick Sanan .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 
218db781477SPatrick Sanan .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 
272db781477SPatrick Sanan .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 
329db781477SPatrick Sanan .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));
7191baa6e33SBarry Smith     } else if (ghostLabel) PetscCall(DMLabelSetValue(ghostLabel, c, 2));
720cd0c2139SMatthew G Knepley   }
721cd0c2139SMatthew G Knepley   for (; c < cEnd; ++c) {
7229566063dSJacob Faibussowitsch     PetscCall(DMLabelSetValue(vtkLabel, c, 1));
723cd0c2139SMatthew G Knepley   }
724d56405f8SMatthew G. Knepley   if (ghostLabel) {
7259566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dmNew, 1, &fStart, &fEnd));
726cd0c2139SMatthew G Knepley     for (f = fStart; f < fEnd; ++f) {
727cd0c2139SMatthew G Knepley       PetscInt numCells;
728cd0c2139SMatthew G Knepley 
7299566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dmNew, f, &numCells));
730cd0c2139SMatthew G Knepley       if (numCells < 2) {
7319566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(ghostLabel, f, 1));
732cd0c2139SMatthew G Knepley       } else {
733cd0c2139SMatthew G Knepley         const PetscInt *cells = NULL;
734cd0c2139SMatthew G Knepley         PetscInt        vA, vB;
735cd0c2139SMatthew G Knepley 
7369566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dmNew, f, &cells));
7379566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(vtkLabel, cells[0], &vA));
7389566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(vtkLabel, cells[1], &vB));
7399566063dSJacob Faibussowitsch         if (vA != 1 && vB != 1) PetscCall(DMLabelSetValue(ghostLabel, f, 1));
740cd0c2139SMatthew G Knepley       }
741cd0c2139SMatthew G Knepley     }
742d56405f8SMatthew G. Knepley   }
743cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
744cd0c2139SMatthew G Knepley }
745cd0c2139SMatthew G Knepley 
746ca04dac2SToby Isaac static PetscErrorCode DMPlexShiftTree_Internal(DM dm, PetscInt depthShift[], DM dmNew)
747ca04dac2SToby Isaac {
748ca04dac2SToby Isaac   DM             refTree;
749ca04dac2SToby Isaac   PetscSection   pSec;
750ca04dac2SToby Isaac   PetscInt       *parents, *childIDs;
751ca04dac2SToby Isaac 
752ca04dac2SToby Isaac   PetscFunctionBegin;
7539566063dSJacob Faibussowitsch   PetscCall(DMPlexGetReferenceTree(dm,&refTree));
7549566063dSJacob Faibussowitsch   PetscCall(DMPlexSetReferenceTree(dmNew,refTree));
7559566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTree(dm,&pSec,&parents,&childIDs,NULL,NULL));
756ca04dac2SToby Isaac   if (pSec) {
7572582d50cSToby Isaac     PetscInt p, pStart, pEnd, *parentsShifted, pStartShifted, pEndShifted, depth;
758fb4630b5SToby Isaac     PetscInt *childIDsShifted;
759ca04dac2SToby Isaac     PetscSection pSecShifted;
760ca04dac2SToby Isaac 
7619566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetChart(pSec,&pStart,&pEnd));
7629566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepth(dm,&depth));
7632582d50cSToby Isaac     pStartShifted = DMPlexShiftPoint_Internal(pStart,depth,depthShift);
7642582d50cSToby Isaac     pEndShifted   = DMPlexShiftPoint_Internal(pEnd,depth,depthShift);
7659566063dSJacob Faibussowitsch     PetscCall(PetscMalloc2(pEndShifted - pStartShifted,&parentsShifted,pEndShifted-pStartShifted,&childIDsShifted));
7669566063dSJacob Faibussowitsch     PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dmNew),&pSecShifted));
7679566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(pSecShifted,pStartShifted,pEndShifted));
768ca04dac2SToby Isaac     for (p = pStartShifted; p < pEndShifted; p++) {
769fb4630b5SToby Isaac       /* start off assuming no children */
7709566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(pSecShifted,p,0));
771fb4630b5SToby Isaac     }
772fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
773fb4630b5SToby Isaac       PetscInt dof;
774fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p,depth,depthShift);
775ca04dac2SToby Isaac 
7769566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(pSec,p,&dof));
7779566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(pSecShifted,pNew,dof));
778ca04dac2SToby Isaac     }
7799566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(pSecShifted));
780fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
781fb4630b5SToby Isaac       PetscInt dof;
782fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p,depth,depthShift);
783fb4630b5SToby Isaac 
7849566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(pSec,p,&dof));
785fb4630b5SToby Isaac       if (dof) {
786fb4630b5SToby Isaac         PetscInt off, offNew;
787fb4630b5SToby Isaac 
7889566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(pSec,p,&off));
7899566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(pSecShifted,pNew,&offNew));
790fb4630b5SToby Isaac         parentsShifted[offNew] = DMPlexShiftPoint_Internal(parents[off],depth,depthShift);
791fb4630b5SToby Isaac         childIDsShifted[offNew] = childIDs[off];
792fb4630b5SToby Isaac       }
793fb4630b5SToby Isaac     }
7949566063dSJacob Faibussowitsch     PetscCall(DMPlexSetTree(dmNew,pSecShifted,parentsShifted,childIDsShifted));
7959566063dSJacob Faibussowitsch     PetscCall(PetscFree2(parentsShifted,childIDsShifted));
7969566063dSJacob Faibussowitsch     PetscCall(PetscSectionDestroy(&pSecShifted));
797ca04dac2SToby Isaac   }
798ca04dac2SToby Isaac   PetscFunctionReturn(0);
799ca04dac2SToby Isaac }
800ca04dac2SToby Isaac 
801cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexConstructGhostCells_Internal(DM dm, DMLabel label, PetscInt *numGhostCells, DM gdm)
802cd0c2139SMatthew G Knepley {
803da97024aSMatthew G. Knepley   PetscSF          sf;
804cd0c2139SMatthew G Knepley   IS               valueIS;
805da97024aSMatthew G. Knepley   const PetscInt  *values, *leaves;
806cd0c2139SMatthew G Knepley   PetscInt        *depthShift;
8072582d50cSToby Isaac   PetscInt         d, depth = 0, nleaves, loc, Ng, numFS, fs, fStart, fEnd, ghostCell, cEnd, c;
808*4fb89dddSMatthew G. Knepley   const PetscReal *maxCell, *Lstart, *L;
809cd0c2139SMatthew G Knepley 
810cd0c2139SMatthew G Knepley   PetscFunctionBegin;
8119566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
8129566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL));
813da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
8149566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
815cd0c2139SMatthew G Knepley   /* Count ghost cells */
8169566063dSJacob Faibussowitsch   PetscCall(DMLabelGetValueIS(label, &valueIS));
8179566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(valueIS, &numFS));
8189566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(valueIS, &values));
8194a6cfa73SMatthew G. Knepley   Ng   = 0;
820cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
82146c796b9SMatthew G. Knepley     IS              faceIS;
82246c796b9SMatthew G. Knepley     const PetscInt *faces;
82346c796b9SMatthew G. Knepley     PetscInt        numFaces, f, numBdFaces = 0;
824cd0c2139SMatthew G Knepley 
8259566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
8269566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
8279566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
82846c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
829ca04dac2SToby Isaac       PetscInt numChildren;
830ca04dac2SToby Isaac 
8319566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
8329566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL));
833ca04dac2SToby Isaac       /* non-local and ancestors points don't get to register ghosts */
834ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
83546c796b9SMatthew G. Knepley       if ((faces[f] >= fStart) && (faces[f] < fEnd)) ++numBdFaces;
83646c796b9SMatthew G. Knepley     }
8374a6cfa73SMatthew G. Knepley     Ng += numBdFaces;
8389566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
8399566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
840cd0c2139SMatthew G Knepley   }
8419566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
8429566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(2*(depth+1), &depthShift));
8432582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
84459eef20bSToby Isaac     PetscInt dEnd;
8452582d50cSToby Isaac 
8469566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm,d,NULL,&dEnd));
84759eef20bSToby Isaac     depthShift[2*d]   = dEnd;
8482582d50cSToby Isaac     depthShift[2*d+1] = 0;
8492582d50cSToby Isaac   }
8502582d50cSToby Isaac   if (depth >= 0) depthShift[2*depth+1] = Ng;
8519566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPointSetUp_Internal(depth,depthShift));
8529566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSizes_Internal(dm, depthShift, gdm));
853cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
8549566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(dm, 0, NULL, &cEnd));
8554a6cfa73SMatthew G. Knepley   for (c = cEnd; c < cEnd + Ng; ++c) {
8569566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeSize(gdm, c, 1));
857cd0c2139SMatthew G Knepley   }
858cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
859cd0c2139SMatthew G Knepley     IS              faceIS;
860cd0c2139SMatthew G Knepley     const PetscInt *faces;
861cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
862cd0c2139SMatthew G Knepley 
8639566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
8649566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
8659566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
866cd0c2139SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
867ca04dac2SToby Isaac       PetscInt size, numChildren;
868cd0c2139SMatthew G Knepley 
8699566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
8709566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL));
871ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
87246c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
8739566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, faces[f], &size));
87463a3b9bcSJacob Faibussowitsch       PetscCheck(size == 1,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM has boundary face %" PetscInt_FMT " with %" PetscInt_FMT " support cells", faces[f], size);
8759566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(gdm, faces[f] + Ng, 2));
876cd0c2139SMatthew G Knepley     }
8779566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
8789566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
879cd0c2139SMatthew G Knepley   }
880cd0c2139SMatthew G Knepley   /* Step 4: Setup ghosted DM */
8819566063dSJacob Faibussowitsch   PetscCall(DMSetUp(gdm));
8829566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPoints_Internal(dm, depthShift, gdm));
883cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
884cd0c2139SMatthew G Knepley   ghostCell = cEnd;
885cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
886cd0c2139SMatthew G Knepley     IS              faceIS;
887cd0c2139SMatthew G Knepley     const PetscInt *faces;
888cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
889cd0c2139SMatthew G Knepley 
8909566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, values[fs], &faceIS));
8919566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(faceIS, &numFaces));
8929566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(faceIS, &faces));
89346c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
894ca04dac2SToby Isaac       PetscInt newFace = faces[f] + Ng, numChildren;
895cd0c2139SMatthew G Knepley 
8969566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(faces[f], nleaves, leaves, &loc));
8979566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL));
898ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
89946c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
9009566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(gdm, ghostCell, &newFace));
9019566063dSJacob Faibussowitsch       PetscCall(DMPlexInsertSupport(gdm, newFace, 1, ghostCell));
90246c796b9SMatthew G. Knepley       ++ghostCell;
903cd0c2139SMatthew G Knepley     }
9049566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(faceIS, &faces));
9059566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&faceIS));
906cd0c2139SMatthew G Knepley   }
9079566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(valueIS, &values));
9089566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&valueIS));
9099566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftCoordinates_Internal(dm, depthShift, gdm));
9109566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Internal(dm, depthShift, gdm));
9119566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftLabels_Internal(dm, depthShift, gdm));
9129566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateVTKLabel_Internal(dm, PETSC_TRUE, gdm));
9139566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftTree_Internal(dm, depthShift, gdm));
9149566063dSJacob Faibussowitsch   PetscCall(PetscFree(depthShift));
915412e9a14SMatthew G. Knepley   for (c = cEnd; c < cEnd + Ng; ++c) {
9169566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCellType(gdm, c, DM_POLYTOPE_FV_GHOST));
917412e9a14SMatthew G. Knepley   }
918966c7b3fSMatthew G. Knepley   /* Step 7: Periodicity */
919*4fb89dddSMatthew G. Knepley   PetscCall(DMGetPeriodicity(dm, &maxCell, &Lstart, &L));
920*4fb89dddSMatthew G. Knepley   PetscCall(DMSetPeriodicity(gdm, maxCell,  Lstart,  L));
9214a6cfa73SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
922cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
923cd0c2139SMatthew G Knepley }
924cd0c2139SMatthew G Knepley 
925cd0c2139SMatthew G Knepley /*@C
926cd0c2139SMatthew G Knepley   DMPlexConstructGhostCells - Construct ghost cells which connect to every boundary face
927cd0c2139SMatthew G Knepley 
928cd0c2139SMatthew G Knepley   Collective on dm
929cd0c2139SMatthew G Knepley 
930cd0c2139SMatthew G Knepley   Input Parameters:
931cd0c2139SMatthew G Knepley + dm - The original DM
932cd0c2139SMatthew G Knepley - labelName - The label specifying the boundary faces, or "Face Sets" if this is NULL
933cd0c2139SMatthew G Knepley 
934cd0c2139SMatthew G Knepley   Output Parameters:
935cd0c2139SMatthew G Knepley + numGhostCells - The number of ghost cells added to the DM
936cd0c2139SMatthew G Knepley - dmGhosted - The new DM
937cd0c2139SMatthew G Knepley 
938cd0c2139SMatthew G Knepley   Note: If no label exists of that name, one will be created marking all boundary faces
939cd0c2139SMatthew G Knepley 
940cd0c2139SMatthew G Knepley   Level: developer
941cd0c2139SMatthew G Knepley 
942db781477SPatrick Sanan .seealso: `DMCreate()`
94331266bc0SMatthew G. Knepley @*/
944cd0c2139SMatthew G Knepley PetscErrorCode DMPlexConstructGhostCells(DM dm, const char labelName[], PetscInt *numGhostCells, DM *dmGhosted)
945cd0c2139SMatthew G Knepley {
946cd0c2139SMatthew G Knepley   DM             gdm;
947cd0c2139SMatthew G Knepley   DMLabel        label;
948cd0c2139SMatthew G Knepley   const char    *name = labelName ? labelName : "Face Sets";
949412e9a14SMatthew G. Knepley   PetscInt       dim, Ng = 0;
950b0441da4SMatthew G. Knepley   PetscBool      useCone, useClosure;
951cd0c2139SMatthew G Knepley 
952cd0c2139SMatthew G Knepley   PetscFunctionBegin;
953cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
954dadcf809SJacob Faibussowitsch   if (numGhostCells) PetscValidIntPointer(numGhostCells, 3);
955cd0c2139SMatthew G Knepley   PetscValidPointer(dmGhosted, 4);
9569566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &gdm));
9579566063dSJacob Faibussowitsch   PetscCall(DMSetType(gdm, DMPLEX));
9589566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
9599566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(gdm, dim));
9609566063dSJacob Faibussowitsch   PetscCall(DMGetBasicAdjacency(dm, &useCone, &useClosure));
9619566063dSJacob Faibussowitsch   PetscCall(DMSetBasicAdjacency(gdm, useCone,  useClosure));
9629566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
963cd0c2139SMatthew G Knepley   if (!label) {
964cd0c2139SMatthew G Knepley     /* Get label for boundary faces */
9659566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(dm, name));
9669566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, name, &label));
9679566063dSJacob Faibussowitsch     PetscCall(DMPlexMarkBoundaryFaces(dm, 1, label));
968cd0c2139SMatthew G Knepley   }
9699566063dSJacob Faibussowitsch   PetscCall(DMPlexConstructGhostCells_Internal(dm, label, &Ng, gdm));
9709566063dSJacob Faibussowitsch   PetscCall(DMCopyDisc(dm, gdm));
9715de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, gdm));
972cad26855SMatthew G. Knepley   gdm->setfromoptionscalled = dm->setfromoptionscalled;
973d80ece95SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
974cd0c2139SMatthew G Knepley   *dmGhosted = gdm;
975cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
976cd0c2139SMatthew G Knepley }
977cd0c2139SMatthew G Knepley 
978accc9626SMatthew G. Knepley static PetscErrorCode DivideCells_Private(DM dm, DMLabel label, DMPlexPointQueue queue)
979accc9626SMatthew G. Knepley {
980accc9626SMatthew G. Knepley   PetscInt dim, d, shift = 100, *pStart, *pEnd;
981accc9626SMatthew G. Knepley 
982accc9626SMatthew G. Knepley   PetscFunctionBegin;
983accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
984accc9626SMatthew G. Knepley   PetscCall(PetscMalloc2(dim, &pStart, dim, &pEnd));
985accc9626SMatthew G. Knepley   for (d = 0; d < dim; ++d) {
986accc9626SMatthew G. Knepley     PetscCall(DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]));
987accc9626SMatthew G. Knepley   }
988accc9626SMatthew G. Knepley   while (!DMPlexPointQueueEmpty(queue)) {
989accc9626SMatthew G. Knepley     PetscInt  cell    = -1;
990accc9626SMatthew G. Knepley     PetscInt *closure = NULL;
991accc9626SMatthew G. Knepley     PetscInt  closureSize, cl, cval;
992accc9626SMatthew G. Knepley 
993accc9626SMatthew G. Knepley     PetscCall(DMPlexPointQueueDequeue(queue, &cell));
994accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, cell, &cval));
995accc9626SMatthew G. Knepley     PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
996accc9626SMatthew G. Knepley     /* Mark points in the cell closure that touch the fault */
997accc9626SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
998accc9626SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
999accc9626SMatthew G. Knepley         const PetscInt clp = closure[cl];
1000accc9626SMatthew G. Knepley         PetscInt       clval;
1001accc9626SMatthew G. Knepley 
1002accc9626SMatthew G. Knepley         if ((clp < pStart[d]) || (clp >= pEnd[d])) continue;
1003accc9626SMatthew G. Knepley         PetscCall(DMLabelGetValue(label, clp, &clval));
1004accc9626SMatthew G. Knepley         if (clval == -1) {
1005accc9626SMatthew G. Knepley           const PetscInt *cone;
1006accc9626SMatthew G. Knepley           PetscInt        coneSize, c;
1007accc9626SMatthew G. Knepley 
1008accc9626SMatthew G. Knepley           /* If a cone point touches the fault, then this point touches the fault */
1009accc9626SMatthew G. Knepley           PetscCall(DMPlexGetCone(dm, clp, &cone));
1010accc9626SMatthew G. Knepley           PetscCall(DMPlexGetConeSize(dm, clp, &coneSize));
1011accc9626SMatthew G. Knepley           for (c = 0; c < coneSize; ++c) {
1012accc9626SMatthew G. Knepley             PetscInt cpval;
1013accc9626SMatthew G. Knepley 
1014accc9626SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, cone[c], &cpval));
1015accc9626SMatthew G. Knepley             if (cpval != -1) {
1016accc9626SMatthew G. Knepley               PetscInt dep;
1017accc9626SMatthew G. Knepley 
1018accc9626SMatthew G. Knepley               PetscCall(DMPlexGetPointDepth(dm, clp, &dep));
1019accc9626SMatthew G. Knepley               clval = cval < 0 ? -(shift+dep) : shift+dep;
1020accc9626SMatthew G. Knepley               PetscCall(DMLabelSetValue(label, clp, clval));
1021accc9626SMatthew G. Knepley               break;
1022accc9626SMatthew G. Knepley             }
1023accc9626SMatthew G. Knepley           }
1024accc9626SMatthew G. Knepley         }
1025accc9626SMatthew G. Knepley         /* Mark neighbor cells through marked faces (these cells must also touch the fault) */
1026accc9626SMatthew G. Knepley         if (d == dim-1 && clval != -1) {
1027accc9626SMatthew G. Knepley           const PetscInt *support;
1028accc9626SMatthew G. Knepley           PetscInt        supportSize, s, nval;
1029accc9626SMatthew G. Knepley 
1030accc9626SMatthew G. Knepley           PetscCall(DMPlexGetSupport(dm, clp, &support));
1031accc9626SMatthew G. Knepley           PetscCall(DMPlexGetSupportSize(dm, clp, &supportSize));
1032accc9626SMatthew G. Knepley           for (s = 0; s < supportSize; ++s) {
1033accc9626SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, support[s], &nval));
1034accc9626SMatthew G. Knepley             if (nval == -1) {
1035accc9626SMatthew G. Knepley               PetscCall(DMLabelSetValue(label, support[s], clval < 0 ? clval-1 : clval+1));
1036accc9626SMatthew G. Knepley               PetscCall(DMPlexPointQueueEnqueue(queue, support[s]));
1037accc9626SMatthew G. Knepley             }
1038accc9626SMatthew G. Knepley           }
1039accc9626SMatthew G. Knepley         }
1040accc9626SMatthew G. Knepley       }
1041accc9626SMatthew G. Knepley     }
1042accc9626SMatthew G. Knepley     PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
1043accc9626SMatthew G. Knepley   }
1044accc9626SMatthew G. Knepley   PetscCall(PetscFree2(pStart, pEnd));
1045accc9626SMatthew G. Knepley   PetscFunctionReturn(0);
1046accc9626SMatthew G. Knepley }
1047accc9626SMatthew G. Knepley 
1048accc9626SMatthew G. Knepley typedef struct {
1049accc9626SMatthew G. Knepley   DM               dm;
1050accc9626SMatthew G. Knepley   DMPlexPointQueue queue;
1051accc9626SMatthew G. Knepley } PointDivision;
1052accc9626SMatthew G. Knepley 
1053accc9626SMatthew G. Knepley static PetscErrorCode divideCell(DMLabel label, PetscInt p, PetscInt val, void *ctx)
1054accc9626SMatthew G. Knepley {
1055accc9626SMatthew G. Knepley   PointDivision  *div  = (PointDivision *) ctx;
1056accc9626SMatthew G. Knepley   PetscInt        cval = val < 0 ? val-1 : val+1;
1057accc9626SMatthew G. Knepley   const PetscInt *support;
1058accc9626SMatthew G. Knepley   PetscInt        supportSize, s;
1059accc9626SMatthew G. Knepley 
1060accc9626SMatthew G. Knepley   PetscFunctionBegin;
1061accc9626SMatthew G. Knepley   PetscCall(DMPlexGetSupport(div->dm, p, &support));
1062accc9626SMatthew G. Knepley   PetscCall(DMPlexGetSupportSize(div->dm, p, &supportSize));
1063accc9626SMatthew G. Knepley   for (s = 0; s < supportSize; ++s) {
1064accc9626SMatthew G. Knepley     PetscCall(DMLabelSetValue(label, support[s], cval));
1065accc9626SMatthew G. Knepley     PetscCall(DMPlexPointQueueEnqueue(div->queue, support[s]));
1066accc9626SMatthew G. Knepley   }
1067accc9626SMatthew G. Knepley   PetscFunctionReturn(0);
1068accc9626SMatthew G. Knepley }
1069accc9626SMatthew G. Knepley 
1070accc9626SMatthew G. Knepley /* Mark cells by label propagation */
1071accc9626SMatthew G. Knepley static PetscErrorCode DMPlexLabelFaultHalo(DM dm, DMLabel faultLabel)
1072accc9626SMatthew G. Knepley {
1073accc9626SMatthew G. Knepley   DMPlexPointQueue queue = NULL;
1074accc9626SMatthew G. Knepley   PointDivision    div;
1075accc9626SMatthew G. Knepley   PetscSF          pointSF;
1076accc9626SMatthew G. Knepley   IS               pointIS;
1077accc9626SMatthew G. Knepley   const PetscInt  *points;
1078accc9626SMatthew G. Knepley   PetscBool        empty;
1079accc9626SMatthew G. Knepley   PetscInt         dim, shift = 100, n, i;
1080accc9626SMatthew G. Knepley 
1081accc9626SMatthew G. Knepley   PetscFunctionBegin;
1082accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
1083accc9626SMatthew G. Knepley   PetscCall(DMPlexPointQueueCreate(1024, &queue));
1084accc9626SMatthew G. Knepley   div.dm    = dm;
1085accc9626SMatthew G. Knepley   div.queue = queue;
1086accc9626SMatthew G. Knepley   /* Enqueue cells on fault */
1087accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(faultLabel, shift+dim, &pointIS));
1088accc9626SMatthew G. Knepley   if (pointIS) {
1089accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(pointIS, &n));
1090accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(pointIS, &points));
1091accc9626SMatthew G. Knepley     for (i = 0; i < n; ++i) {PetscCall(DMPlexPointQueueEnqueue(queue, points[i]));}
1092accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(pointIS, &points));
1093accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&pointIS));
1094accc9626SMatthew G. Knepley   }
1095accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(faultLabel, -(shift+dim), &pointIS));
1096accc9626SMatthew G. Knepley   if (pointIS) {
1097accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(pointIS, &n));
1098accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(pointIS, &points));
1099accc9626SMatthew G. Knepley     for (i = 0; i < n; ++i) {PetscCall(DMPlexPointQueueEnqueue(queue, points[i]));}
1100accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(pointIS, &points));
1101accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&pointIS));
1102accc9626SMatthew G. Knepley   }
1103accc9626SMatthew G. Knepley 
1104accc9626SMatthew G. Knepley   PetscCall(DMGetPointSF(dm, &pointSF));
1105accc9626SMatthew G. Knepley   PetscCall(DMLabelPropagateBegin(faultLabel, pointSF));
1106accc9626SMatthew G. Knepley   /* While edge queue is not empty: */
1107accc9626SMatthew G. Knepley   PetscCall(DMPlexPointQueueEmptyCollective((PetscObject) dm, queue, &empty));
1108accc9626SMatthew G. Knepley   while (!empty) {
1109accc9626SMatthew G. Knepley     PetscCall(DivideCells_Private(dm, faultLabel, queue));
1110accc9626SMatthew G. Knepley     PetscCall(DMLabelPropagatePush(faultLabel, pointSF, divideCell, &div));
1111accc9626SMatthew G. Knepley     PetscCall(DMPlexPointQueueEmptyCollective((PetscObject) dm, queue, &empty));
1112accc9626SMatthew G. Knepley   }
1113accc9626SMatthew G. Knepley   PetscCall(DMLabelPropagateEnd(faultLabel, pointSF));
1114accc9626SMatthew G. Knepley   PetscCall(DMPlexPointQueueDestroy(&queue));
1115accc9626SMatthew G. Knepley   PetscFunctionReturn(0);
1116accc9626SMatthew G. Knepley }
1117accc9626SMatthew G. Knepley 
1118607ab7a9SMatthew G. Knepley /*
1119faedd622SMatthew G. Knepley   We are adding three kinds of points here:
1120607ab7a9SMatthew G. Knepley     Replicated:     Copies of points which exist in the mesh, such as vertices identified across a fault
1121faedd622SMatthew G. Knepley     Non-replicated: Points which exist on the fault, but are not replicated
1122b6dfa339SMatthew 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
1123607ab7a9SMatthew G. Knepley     Hybrid:         Entirely new points, such as cohesive cells
1124a6ae58d1SMatthew G. Knepley 
1125a6ae58d1SMatthew G. Knepley   When creating subsequent cohesive cells, we shift the old hybrid cells to the end of the numbering at
1126a6ae58d1SMatthew G. Knepley   each depth so that the new split/hybrid points can be inserted as a block.
1127607ab7a9SMatthew G. Knepley */
11287db7e0a7SMatthew G. Knepley static PetscErrorCode DMPlexConstructCohesiveCells_Internal(DM dm, DMLabel label, DMLabel splitLabel, DM sdm)
1129cd0c2139SMatthew G Knepley {
1130cd0c2139SMatthew G Knepley   MPI_Comm         comm;
1131607ab7a9SMatthew G. Knepley   IS               valueIS;
1132607ab7a9SMatthew G. Knepley   PetscInt         numSP = 0;          /* The number of depths for which we have replicated points */
1133607ab7a9SMatthew G. Knepley   const PetscInt  *values;             /* List of depths for which we have replicated points */
113418c5995bSMatthew G. Knepley   IS              *splitIS;
113518c5995bSMatthew G. Knepley   IS              *unsplitIS;
1136b6dfa339SMatthew G. Knepley   IS               ghostIS;
1137607ab7a9SMatthew G. Knepley   PetscInt        *numSplitPoints;     /* The number of replicated points at each depth */
113818c5995bSMatthew G. Knepley   PetscInt        *numUnsplitPoints;   /* The number of non-replicated points at each depth which still give rise to hybrid points */
113936dbac82SMatthew G. Knepley   PetscInt        *numHybridPoints;    /* The number of new hybrid points at each depth */
114036dbac82SMatthew G. Knepley   PetscInt        *numHybridPointsOld; /* The number of existing hybrid points at each depth */
1141b6dfa339SMatthew G. Knepley   PetscInt         numGhostPoints;     /* The number of unowned, shared fault faces */
1142607ab7a9SMatthew G. Knepley   const PetscInt **splitPoints;        /* Replicated points for each depth */
114318c5995bSMatthew G. Knepley   const PetscInt **unsplitPoints;      /* Non-replicated points for each depth */
1144b6dfa339SMatthew G. Knepley   const PetscInt  *ghostPoints;        /* Ghost fault faces */
1145cd0c2139SMatthew G Knepley   PetscSection     coordSection;
1146cd0c2139SMatthew G Knepley   Vec              coordinates;
1147cd0c2139SMatthew G Knepley   PetscScalar     *coords;
1148a6ae58d1SMatthew G. Knepley   PetscInt        *depthMax;           /* The first hybrid point at each depth in the original mesh */
1149a6ae58d1SMatthew G. Knepley   PetscInt        *depthEnd;           /* The point limit at each depth in the original mesh */
1150607ab7a9SMatthew G. Knepley   PetscInt        *depthShift;         /* Number of replicated+hybrid points at each depth */
1151607ab7a9SMatthew G. Knepley   PetscInt        *pMaxNew;            /* The first replicated point at each depth in the new mesh, hybrids come after this */
1152607ab7a9SMatthew G. Knepley   PetscInt        *coneNew, *coneONew, *supportNew;
115318c5995bSMatthew G. Knepley   PetscInt         shift = 100, shift2 = 200, depth = 0, dep, dim, d, sp, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, numLabels, vStart, vEnd, pEnd, p, v;
1154cd0c2139SMatthew G Knepley 
1155cd0c2139SMatthew G Knepley   PetscFunctionBegin;
11569566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm,&comm));
11579566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
11589566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
11599566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
1160412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
11619566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(sdm, "celltype"));
1162cd0c2139SMatthew G Knepley   /* Count split points and add cohesive cells */
11639566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
11649566063dSJacob Faibussowitsch   PetscCall(PetscMalloc5(depth+1,&depthMax,depth+1,&depthEnd,2*(depth+1),&depthShift,depth+1,&pMaxNew,depth+1,&numHybridPointsOld));
11659566063dSJacob Faibussowitsch   PetscCall(PetscMalloc7(depth+1,&splitIS,depth+1,&unsplitIS,depth+1,&numSplitPoints,depth+1,&numUnsplitPoints,depth+1,&numHybridPoints,depth+1,&splitPoints,depth+1,&unsplitPoints));
1166607ab7a9SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
11679566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, d, NULL, &pMaxNew[d]));
11689566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, d, &depthMax[d], NULL));
1169a6ae58d1SMatthew G. Knepley     depthEnd[d]           = pMaxNew[d];
1170a6ae58d1SMatthew G. Knepley     depthMax[d]           = depthMax[d] < 0 ? depthEnd[d] : depthMax[d];
1171607ab7a9SMatthew G. Knepley     numSplitPoints[d]     = 0;
117218c5995bSMatthew G. Knepley     numUnsplitPoints[d]   = 0;
1173607ab7a9SMatthew G. Knepley     numHybridPoints[d]    = 0;
1174a6ae58d1SMatthew G. Knepley     numHybridPointsOld[d] = depthMax[d] < 0 ? 0 : depthEnd[d] - depthMax[d];
1175607ab7a9SMatthew G. Knepley     splitPoints[d]        = NULL;
117618c5995bSMatthew G. Knepley     unsplitPoints[d]      = NULL;
117718c5995bSMatthew G. Knepley     splitIS[d]            = NULL;
117818c5995bSMatthew G. Knepley     unsplitIS[d]          = NULL;
117959eef20bSToby Isaac     /* we are shifting the existing hybrid points with the stratum behind them, so
118059eef20bSToby Isaac      * the split comes at the end of the normal points, i.e., at depthMax[d] */
118159eef20bSToby Isaac     depthShift[2*d]       = depthMax[d];
118259eef20bSToby Isaac     depthShift[2*d+1]     = 0;
1183607ab7a9SMatthew G. Knepley   }
1184b6dfa339SMatthew G. Knepley   numGhostPoints = 0;
1185b6dfa339SMatthew G. Knepley   ghostPoints    = NULL;
1186cd0c2139SMatthew G Knepley   if (label) {
11879566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
11889566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &numSP));
11899566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
1190cd0c2139SMatthew G Knepley   }
1191cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1192cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1193cd0c2139SMatthew G Knepley 
1194cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
11959566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, dep, &splitIS[dep]));
119618c5995bSMatthew G. Knepley     if (splitIS[dep]) {
11979566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(splitIS[dep], &numSplitPoints[dep]));
11989566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(splitIS[dep], &splitPoints[dep]));
119918c5995bSMatthew G. Knepley     }
12009566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, shift2+dep, &unsplitIS[dep]));
120118c5995bSMatthew G. Knepley     if (unsplitIS[dep]) {
12029566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(unsplitIS[dep], &numUnsplitPoints[dep]));
12039566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(unsplitIS[dep], &unsplitPoints[dep]));
1204cd0c2139SMatthew G Knepley     }
1205cd0c2139SMatthew G Knepley   }
1206b6dfa339SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, shift2+dim-1, &ghostIS));
1207b6dfa339SMatthew G. Knepley   if (ghostIS) {
1208b6dfa339SMatthew G. Knepley     PetscCall(ISGetLocalSize(ghostIS, &numGhostPoints));
1209b6dfa339SMatthew G. Knepley     PetscCall(ISGetIndices(ghostIS, &ghostPoints));
1210b6dfa339SMatthew G. Knepley   }
1211607ab7a9SMatthew G. Knepley   /* Calculate number of hybrid points */
121218c5995bSMatthew 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   */
12132582d50cSToby Isaac   for (d = 0; d <= depth; ++d) depthShift[2*d+1]      = numSplitPoints[d] + numHybridPoints[d];
12149566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPointSetUp_Internal(depth,depthShift));
121559eef20bSToby Isaac   /* the end of the points in this stratum that come before the new points:
121659eef20bSToby Isaac    * shifting pMaxNew[d] gets the new start of the next stratum, then count back the old hybrid points and the newly
121759eef20bSToby Isaac    * added points */
12182582d50cSToby Isaac   for (d = 0; d <= depth; ++d) pMaxNew[d]             = DMPlexShiftPoint_Internal(pMaxNew[d],depth,depthShift) - (numHybridPointsOld[d] + numSplitPoints[d] + numHybridPoints[d]);
12199566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSizes_Internal(dm, depthShift, sdm));
1220cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
1221cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1222cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1223cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
12242582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
12254c367dbcSMatthew G. Knepley       const PetscInt  splitp = p    + pMaxNew[dep];
1226cd0c2139SMatthew G Knepley       const PetscInt *support;
1227394c2f0fSMatthew G. Knepley       DMPolytopeType  ct;
12284c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, qn, qp, e;
1229cd0c2139SMatthew G Knepley 
12309566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
12319566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(sdm, splitp, coneSize));
12329566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
12339566063dSJacob Faibussowitsch       PetscCall(DMPlexSetSupportSize(sdm, splitp, supportSize));
12349566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, oldp, &ct));
12359566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(sdm, splitp, ct));
1236cd0c2139SMatthew G Knepley       if (dep == depth-1) {
12374c367dbcSMatthew G. Knepley         const PetscInt hybcell = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
12384c367dbcSMatthew G. Knepley 
1239cd0c2139SMatthew G Knepley         /* Add cohesive cells, they are prisms */
12409566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybcell, 2 + coneSize));
1241412e9a14SMatthew G. Knepley         switch (coneSize) {
12429566063dSJacob Faibussowitsch           case 2: PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_SEG_PRISM_TENSOR));break;
12439566063dSJacob Faibussowitsch           case 3: PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_TRI_PRISM_TENSOR));break;
12449566063dSJacob Faibussowitsch           case 4: PetscCall(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_QUAD_PRISM_TENSOR));break;
1245412e9a14SMatthew G. Knepley         }
1246b6dfa339SMatthew G. Knepley         /* Shared fault faces with only one support cell now have two with the cohesive cell */
1247b6dfa339SMatthew G. Knepley         /*   TODO Check thaat oldp has rootdegree == 1 */
1248b6dfa339SMatthew G. Knepley         if (supportSize == 1) {
1249b6dfa339SMatthew G. Knepley           const PetscInt *support;
1250b6dfa339SMatthew G. Knepley           PetscInt        val;
1251b6dfa339SMatthew G. Knepley 
1252b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetSupport(dm, oldp, &support));
1253b6dfa339SMatthew G. Knepley           PetscCall(DMLabelGetValue(label, support[0], &val));
1254b6dfa339SMatthew G. Knepley           if (val < 0) PetscCall(DMPlexSetSupportSize(sdm, splitp, 2));
1255b6dfa339SMatthew G. Knepley           else         PetscCall(DMPlexSetSupportSize(sdm, newp,   2));
1256b6dfa339SMatthew G. Knepley         }
1257cd0c2139SMatthew G Knepley       } else if (dep == 0) {
12584c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1259cd0c2139SMatthew G Knepley 
12609566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, oldp, &support));
12614c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1262cd0c2139SMatthew G Knepley           PetscInt val;
1263cd0c2139SMatthew G Knepley 
12649566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
12654c367dbcSMatthew G. Knepley           if (val == 1) ++qf;
12664c367dbcSMatthew G. Knepley           if ((val == 1) || (val ==  (shift + 1))) ++qn;
12674c367dbcSMatthew G. Knepley           if ((val == 1) || (val == -(shift + 1))) ++qp;
1268cd0c2139SMatthew G Knepley         }
12694c367dbcSMatthew G. Knepley         /* Split old vertex: Edges into original vertex and new cohesive edge */
12709566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qn+1));
12714c367dbcSMatthew G. Knepley         /* Split new vertex: Edges into split vertex and new cohesive edge */
12729566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, splitp, qp+1));
12734c367dbcSMatthew G. Knepley         /* Add hybrid edge */
12749566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybedge, 2));
12759566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybedge, qf));
12769566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR));
1277cd0c2139SMatthew G Knepley       } else if (dep == dim-2) {
12784c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
12794c367dbcSMatthew G. Knepley 
12809566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, oldp, &support));
12814c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1282cd0c2139SMatthew G Knepley           PetscInt val;
1283cd0c2139SMatthew G Knepley 
12849566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
12854c367dbcSMatthew G. Knepley           if (val == dim-1) ++qf;
12864c367dbcSMatthew G. Knepley           if ((val == dim-1) || (val ==  (shift + dim-1))) ++qn;
12874c367dbcSMatthew G. Knepley           if ((val == dim-1) || (val == -(shift + dim-1))) ++qp;
1288cd0c2139SMatthew G Knepley         }
12894c367dbcSMatthew G. Knepley         /* Split old edge: Faces into original edge and cohesive face (positive side?) */
12909566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qn+1));
12914c367dbcSMatthew G. Knepley         /* Split new edge: Faces into split edge and cohesive face (negative side?) */
12929566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, splitp, qp+1));
12934c367dbcSMatthew G. Knepley         /* Add hybrid face */
12949566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybface, 4));
12959566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybface, qf));
12969566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR));
1297cd0c2139SMatthew G Knepley       }
1298cd0c2139SMatthew G Knepley     }
1299cd0c2139SMatthew G Knepley   }
130018c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
130118c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
130218c5995bSMatthew G. Knepley       const PetscInt  oldp   = unsplitPoints[dep][p];
13032582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
130418c5995bSMatthew G. Knepley       const PetscInt *support;
1305da1dd7e4SMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, e, s;
130618c5995bSMatthew G. Knepley 
13079566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
13089566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
13099566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
131018c5995bSMatthew G. Knepley       if (dep == 0) {
131118c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
131218c5995bSMatthew G. Knepley 
131339254ff6SMatthew G. Knepley         /* Unsplit vertex: Edges into original vertex, split edges, and new cohesive edge twice */
131439254ff6SMatthew G. Knepley         for (s = 0, qf = 0; s < supportSize; ++s, ++qf) {
13159566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSplitPoints[dep+1], splitPoints[dep+1], &e));
131639254ff6SMatthew G. Knepley           if (e >= 0) ++qf;
131739254ff6SMatthew G. Knepley         }
13189566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qf+2));
131918c5995bSMatthew G. Knepley         /* Add hybrid edge */
13209566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybedge, 2));
1321e1757548SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1322e1757548SMatthew G. Knepley           PetscInt val;
1323e1757548SMatthew G. Knepley 
13249566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1325e1757548SMatthew G. Knepley           /* Split and unsplit edges produce hybrid faces */
1326da1dd7e4SMatthew G. Knepley           if (val == 1) ++qf;
1327da1dd7e4SMatthew G. Knepley           if (val == (shift2 + 1)) ++qf;
1328e1757548SMatthew G. Knepley         }
13299566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybedge, qf));
13309566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR));
133118c5995bSMatthew G. Knepley       } else if (dep == dim-2) {
133218c5995bSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
1333cd0c2139SMatthew G Knepley         PetscInt       val;
1334cd0c2139SMatthew G Knepley 
1335da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
13369566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1337da1dd7e4SMatthew G. Knepley           if (val == dim-1) qf += 2;
1338da1dd7e4SMatthew G. Knepley           else              ++qf;
1339cd0c2139SMatthew G Knepley         }
134018c5995bSMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and cohesive face twice */
13419566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, newp, qf+2));
134218c5995bSMatthew G. Knepley         /* Add hybrid face */
1343da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
13449566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1345da1dd7e4SMatthew G. Knepley           if (val == dim-1) ++qf;
1346da1dd7e4SMatthew G. Knepley         }
13479566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(sdm, hybface, 4));
13489566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupportSize(sdm, hybface, qf));
13499566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR));
1350cd0c2139SMatthew G Knepley       }
1351cd0c2139SMatthew G Knepley     }
1352cd0c2139SMatthew G Knepley   }
1353cd0c2139SMatthew G Knepley   /* Step 4: Setup split DM */
13549566063dSJacob Faibussowitsch   PetscCall(DMSetUp(sdm));
13559566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftPoints_Internal(dm, depthShift, sdm));
13569566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(sdm, &maxConeSizeNew, &maxSupportSizeNew));
13579566063dSJacob Faibussowitsch   PetscCall(PetscMalloc3(PetscMax(maxConeSize, maxConeSizeNew)*3,&coneNew,PetscMax(maxConeSize, maxConeSizeNew)*3,&coneONew,PetscMax(maxSupportSize, maxSupportSizeNew),&supportNew));
1358cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
1359cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1360cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1361cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
13622582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
13634c367dbcSMatthew G. Knepley       const PetscInt  splitp = p    + pMaxNew[dep];
1364cd0c2139SMatthew G Knepley       const PetscInt *cone, *support, *ornt;
1365b5a892a1SMatthew G. Knepley       DMPolytopeType  ct;
13664c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, q, qf, qn, qp, v, e, s;
1367cd0c2139SMatthew G Knepley 
13689566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCellType(dm, oldp, &ct));
13699566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
13709566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, oldp, &cone));
13719566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeOrientation(dm, oldp, &ornt));
13729566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
13739566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
1374cd0c2139SMatthew G Knepley       if (dep == depth-1) {
137596a07cd0SMatthew G. Knepley         PetscBool       hasUnsplit = PETSC_FALSE;
13764c367dbcSMatthew G. Knepley         const PetscInt  hybcell    = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1377cd0c2139SMatthew G Knepley         const PetscInt *supportF;
1378cd0c2139SMatthew G Knepley 
1379b6dfa339SMatthew G. Knepley         coneONew[0] = coneONew[1] = -1000;
1380cd0c2139SMatthew G Knepley         /* Split face:       copy in old face to new face to start */
13819566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(sdm, newp,  &supportF));
13829566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportF));
1383cd0c2139SMatthew G Knepley         /* Split old face:   old vertices/edges in cone so no change */
1384cd0c2139SMatthew G Knepley         /* Split new face:   new vertices/edges in cone */
1385cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
13869566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v));
138718c5995bSMatthew G. Knepley           if (v < 0) {
13889566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v));
138963a3b9bcSJacob Faibussowitsch             PetscCheck(v >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %" PetscInt_FMT " in split or unsplit points of depth %" PetscInt_FMT, cone[q], dep-1);
13902582d50cSToby Isaac             coneNew[2+q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
139196a07cd0SMatthew G. Knepley             hasUnsplit   = PETSC_TRUE;
139218c5995bSMatthew G. Knepley           } else {
13934c367dbcSMatthew G. Knepley             coneNew[2+q] = v + pMaxNew[dep-1];
1394163235baSMatthew G. Knepley             if (dep > 1) {
1395163235baSMatthew G. Knepley               const PetscInt *econe;
1396163235baSMatthew G. Knepley               PetscInt        econeSize, r, vs, vu;
1397163235baSMatthew G. Knepley 
13989566063dSJacob Faibussowitsch               PetscCall(DMPlexGetConeSize(dm, cone[q], &econeSize));
13999566063dSJacob Faibussowitsch               PetscCall(DMPlexGetCone(dm, cone[q], &econe));
1400163235baSMatthew G. Knepley               for (r = 0; r < econeSize; ++r) {
14019566063dSJacob Faibussowitsch                 PetscCall(PetscFindInt(econe[r], numSplitPoints[dep-2],   splitPoints[dep-2],   &vs));
14029566063dSJacob Faibussowitsch                 PetscCall(PetscFindInt(econe[r], numUnsplitPoints[dep-2], unsplitPoints[dep-2], &vu));
1403163235baSMatthew G. Knepley                 if (vs >= 0) continue;
140463a3b9bcSJacob Faibussowitsch                 PetscCheck(vu >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %" PetscInt_FMT " in split or unsplit points of depth %" PetscInt_FMT, econe[r], dep-2);
1405163235baSMatthew G. Knepley                 hasUnsplit   = PETSC_TRUE;
1406163235baSMatthew G. Knepley               }
1407163235baSMatthew G. Knepley             }
1408cd0c2139SMatthew G Knepley           }
1409cd0c2139SMatthew G Knepley         }
14109566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, splitp, &coneNew[2]));
14119566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(sdm, splitp, ornt));
1412e537020bSMatthew G. Knepley         /* Face support */
1413b6dfa339SMatthew G. Knepley         PetscInt vals[2];
1414cd0c2139SMatthew G Knepley 
1415b6dfa339SMatthew G. Knepley         PetscCall(DMLabelGetValue(label, support[0], &vals[0]));
1416b6dfa339SMatthew G. Knepley         if (supportSize > 1) PetscCall(DMLabelGetValue(label, support[1], &vals[1]));
1417b6dfa339SMatthew G. Knepley         else                 vals[1] = -vals[0];
1418b6dfa339SMatthew 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]);
1419b6dfa339SMatthew G. Knepley 
1420b6dfa339SMatthew G. Knepley         for (s = 0; s < 2; ++s) {
1421b6dfa339SMatthew G. Knepley           if (s >= supportSize) {
1422b6dfa339SMatthew G. Knepley             if (vals[s] < 0) {
1423b6dfa339SMatthew G. Knepley               /* Ghost old face:   Replace negative side cell with cohesive cell */
1424accc9626SMatthew G. Knepley               PetscCall(DMPlexInsertSupport(sdm, newp,   s, hybcell));
1425b6dfa339SMatthew G. Knepley             } else {
1426b6dfa339SMatthew G. Knepley               /* Ghost new face:   Replace positive side cell with cohesive cell */
1427accc9626SMatthew G. Knepley               PetscCall(DMPlexInsertSupport(sdm, splitp, s, hybcell));
1428b6dfa339SMatthew G. Knepley             }
1429b6dfa339SMatthew G. Knepley           } else {
1430b6dfa339SMatthew G. Knepley             if (vals[s] < 0) {
1431cd0c2139SMatthew G Knepley               /* Split old face:   Replace negative side cell with cohesive cell */
14329566063dSJacob Faibussowitsch               PetscCall(DMPlexInsertSupport(sdm, newp,   s, hybcell));
1433cd0c2139SMatthew G Knepley             } else {
1434cd0c2139SMatthew G Knepley               /* Split new face:   Replace positive side cell with cohesive cell */
14359566063dSJacob Faibussowitsch               PetscCall(DMPlexInsertSupport(sdm, splitp, s, hybcell));
1436b6dfa339SMatthew G. Knepley             }
1437b6dfa339SMatthew G. Knepley           }
1438b6dfa339SMatthew G. Knepley         }
1439b6dfa339SMatthew G. Knepley         /* Get orientation for cohesive face using the positive side cell */
1440e537020bSMatthew G. Knepley         {
1441e537020bSMatthew G. Knepley           const PetscInt *ncone, *nconeO;
1442b6dfa339SMatthew G. Knepley           PetscInt        nconeSize, nc, ocell;
1443accc9626SMatthew G. Knepley           PetscBool       flip = PETSC_FALSE;
1444e537020bSMatthew G. Knepley 
1445b6dfa339SMatthew G. Knepley           if (supportSize > 1) {ocell = vals[0] < 0 ? support[1] : support[0];}
1446b6dfa339SMatthew G. Knepley           else                 {ocell = support[0]; flip = vals[0] < 0 ? PETSC_TRUE : PETSC_FALSE;}
1447b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetConeSize(dm, ocell, &nconeSize));
1448b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetCone(dm, ocell, &ncone));
1449e1a13daeSMatthew G. Knepley           PetscCall(DMPlexGetConeOrientation(dm, ocell, &nconeO));
1450e537020bSMatthew G. Knepley           for (nc = 0; nc < nconeSize; ++nc) {
1451e537020bSMatthew G. Knepley             if (ncone[nc] == oldp) {
1452b6dfa339SMatthew G. Knepley               coneONew[0] = flip ? -(nconeO[nc]+1) : nconeO[nc];
1453e537020bSMatthew G. Knepley               break;
1454cd0c2139SMatthew G Knepley             }
1455cd0c2139SMatthew G Knepley           }
1456b6dfa339SMatthew 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);
1457e537020bSMatthew G. Knepley         }
14584c367dbcSMatthew G. Knepley         /* Cohesive cell:    Old and new split face, then new cohesive faces */
1459b6dfa339SMatthew G. Knepley         {
1460b6dfa339SMatthew G. Knepley           const PetscInt No = DMPolytopeTypeGetNumArrangments(ct)/2;
1461b6dfa339SMatthew G. Knepley           PetscCheck((coneONew[0] >= -No) && (coneONew[0] < No), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid %s orientation %" PetscInt_FMT, DMPolytopeTypes[ct], coneONew[0]);
1462b6dfa339SMatthew G. Knepley         }
1463b5a892a1SMatthew G. Knepley         const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, coneONew[0]);
1464b5a892a1SMatthew G. Knepley 
1465fd4b9f15SMatthew G. Knepley         coneNew[0]  = newp;   /* Extracted negative side orientation above */
14664c367dbcSMatthew G. Knepley         coneNew[1]  = splitp;
14674c367dbcSMatthew G. Knepley         coneONew[1] = coneONew[0];
1468e537020bSMatthew G. Knepley         for (q = 0; q < coneSize; ++q) {
1469412e9a14SMatthew G. Knepley           /* Hybrid faces must follow order from oriented end face */
1470b5a892a1SMatthew G. Knepley           const PetscInt qa = arr[q*2+0];
1471b5a892a1SMatthew G. Knepley           const PetscInt qo = arr[q*2+1];
1472b5a892a1SMatthew G. Knepley           DMPolytopeType ft = dep == 2 ? DM_POLYTOPE_SEGMENT : DM_POLYTOPE_POINT;
1473412e9a14SMatthew G. Knepley 
14749566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[qa], numSplitPoints[dep-1], splitPoints[dep-1], &v));
147518c5995bSMatthew G. Knepley           if (v < 0) {
14769566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[qa], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v));
147718c5995bSMatthew G. Knepley             coneNew[2+q]  = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
147818c5995bSMatthew G. Knepley           } else {
147918c5995bSMatthew G. Knepley             coneNew[2+q]  = v + pMaxNew[dep] + numSplitPoints[dep];
148018c5995bSMatthew G. Knepley           }
1481b5a892a1SMatthew G. Knepley           coneONew[2+q] = DMPolytopeTypeComposeOrientation(ft, qo, ornt[qa]);
1482e537020bSMatthew G. Knepley         }
14839566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybcell, coneNew));
14849566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(sdm, hybcell, coneONew));
148596a07cd0SMatthew G. Knepley         /* Label the hybrid cells on the boundary of the split */
14869566063dSJacob Faibussowitsch         if (hasUnsplit) PetscCall(DMLabelSetValue(label, -hybcell, dim));
1487cd0c2139SMatthew G Knepley       } else if (dep == 0) {
14884c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1489cd0c2139SMatthew G Knepley 
1490cd0c2139SMatthew G Knepley         /* Split old vertex: Edges in old split faces and new cohesive edge */
14914c367dbcSMatthew G. Knepley         for (e = 0, qn = 0; e < supportSize; ++e) {
1492cd0c2139SMatthew G Knepley           PetscInt val;
1493cd0c2139SMatthew G Knepley 
14949566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1495cd0c2139SMatthew G Knepley           if ((val == 1) || (val == (shift + 1))) {
14962582d50cSToby Isaac             supportNew[qn++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1497cd0c2139SMatthew G Knepley           }
1498cd0c2139SMatthew G Knepley         }
14994c367dbcSMatthew G. Knepley         supportNew[qn] = hybedge;
15009566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1501cd0c2139SMatthew G Knepley         /* Split new vertex: Edges in new split faces and new cohesive edge */
15024c367dbcSMatthew G. Knepley         for (e = 0, qp = 0; e < supportSize; ++e) {
1503cd0c2139SMatthew G Knepley           PetscInt val, edge;
1504cd0c2139SMatthew G Knepley 
15059566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1506cd0c2139SMatthew G Knepley           if (val == 1) {
15079566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge));
150863a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0,comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]);
15094c367dbcSMatthew G. Knepley             supportNew[qp++] = edge + pMaxNew[dep+1];
1510cd0c2139SMatthew G Knepley           } else if (val == -(shift + 1)) {
15112582d50cSToby Isaac             supportNew[qp++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1512cd0c2139SMatthew G Knepley           }
1513cd0c2139SMatthew G Knepley         }
15144c367dbcSMatthew G. Knepley         supportNew[qp] = hybedge;
15159566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportNew));
15164c367dbcSMatthew G. Knepley         /* Hybrid edge:    Old and new split vertex */
1517cd0c2139SMatthew G Knepley         coneNew[0] = newp;
1518cd0c2139SMatthew G Knepley         coneNew[1] = splitp;
15199566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybedge, coneNew));
15204c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
15214c367dbcSMatthew G. Knepley           PetscInt val, edge;
15224c367dbcSMatthew G. Knepley 
15239566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
15244c367dbcSMatthew G. Knepley           if (val == 1) {
15259566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge));
152663a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0,comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]);
15274c367dbcSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2];
15284c367dbcSMatthew G. Knepley           }
15294c367dbcSMatthew G. Knepley         }
15309566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybedge, supportNew));
1531cd0c2139SMatthew G Knepley       } else if (dep == dim-2) {
15324c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
15334c367dbcSMatthew G. Knepley 
1534cd0c2139SMatthew G Knepley         /* Split old edge:   old vertices in cone so no change */
1535cd0c2139SMatthew G Knepley         /* Split new edge:   new vertices in cone */
1536cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
15379566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v));
1538e1757548SMatthew G. Knepley           if (v < 0) {
15399566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v));
154063a3b9bcSJacob Faibussowitsch             PetscCheck(v >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %" PetscInt_FMT " in split or unsplit points of depth %" PetscInt_FMT, cone[q], dep-1);
15412582d50cSToby Isaac             coneNew[q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
1542e1757548SMatthew G. Knepley           } else {
15434c367dbcSMatthew G. Knepley             coneNew[q] = v + pMaxNew[dep-1];
1544cd0c2139SMatthew G Knepley           }
1545e1757548SMatthew G. Knepley         }
15469566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, splitp, coneNew));
1547cd0c2139SMatthew G Knepley         /* Split old edge: Faces in positive side cells and old split faces */
1548cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1549cd0c2139SMatthew G Knepley           PetscInt val;
1550cd0c2139SMatthew G Knepley 
15519566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
15524c367dbcSMatthew G. Knepley           if (val == dim-1) {
15532582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
15544c367dbcSMatthew G. Knepley           } else if (val == (shift + dim-1)) {
15552582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1556cd0c2139SMatthew G Knepley           }
1557cd0c2139SMatthew G Knepley         }
1558b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
15599566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1560cd0c2139SMatthew G Knepley         /* Split new edge: Faces in negative side cells and new split faces */
1561cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1562cd0c2139SMatthew G Knepley           PetscInt val, face;
1563cd0c2139SMatthew G Knepley 
15649566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
1565cd0c2139SMatthew G Knepley           if (val == dim-1) {
15669566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &face));
156763a3b9bcSJacob Faibussowitsch             PetscCheck(face >= 0,comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[e]);
15684c367dbcSMatthew G. Knepley             supportNew[q++] = face + pMaxNew[dep+1];
1569cd0c2139SMatthew G Knepley           } else if (val == -(shift + dim-1)) {
15702582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1571cd0c2139SMatthew G Knepley           }
1572cd0c2139SMatthew G Knepley         }
1573b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
15749566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, splitp, supportNew));
15754c367dbcSMatthew G. Knepley         /* Hybrid face */
15764c367dbcSMatthew G. Knepley         coneNew[0] = newp;
15774c367dbcSMatthew G. Knepley         coneNew[1] = splitp;
15784c367dbcSMatthew G. Knepley         for (v = 0; v < coneSize; ++v) {
15794c367dbcSMatthew G. Knepley           PetscInt vertex;
15809566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(cone[v], numSplitPoints[dep-1], splitPoints[dep-1], &vertex));
1581e1757548SMatthew G. Knepley           if (vertex < 0) {
15829566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(cone[v], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &vertex));
158363a3b9bcSJacob Faibussowitsch             PetscCheck(vertex >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %" PetscInt_FMT " in split or unsplit points of depth %" PetscInt_FMT, cone[v], dep-1);
1584e1757548SMatthew G. Knepley             coneNew[2+v] = vertex + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1585e1757548SMatthew G. Knepley           } else {
15864c367dbcSMatthew G. Knepley             coneNew[2+v] = vertex + pMaxNew[dep] + numSplitPoints[dep];
15874c367dbcSMatthew G. Knepley           }
1588e1757548SMatthew G. Knepley         }
15899566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybface, coneNew));
15904c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
15914c367dbcSMatthew G. Knepley           PetscInt val, face;
15924c367dbcSMatthew G. Knepley 
15939566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
15944c367dbcSMatthew G. Knepley           if (val == dim-1) {
15959566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &face));
159663a3b9bcSJacob Faibussowitsch             PetscCheck(face >= 0,comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[e]);
15974c367dbcSMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+2] + numSplitPoints[dep+2];
15984c367dbcSMatthew G. Knepley           }
15994c367dbcSMatthew G. Knepley         }
16009566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybface, supportNew));
1601cd0c2139SMatthew G Knepley       }
1602cd0c2139SMatthew G Knepley     }
1603cd0c2139SMatthew G Knepley   }
160418c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
160518c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
160618c5995bSMatthew G. Knepley       const PetscInt  oldp   = unsplitPoints[dep][p];
16072582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
1608b5a892a1SMatthew G. Knepley       const PetscInt *cone, *support;
1609e1757548SMatthew G. Knepley       PetscInt        coneSize, supportSize, supportSizeNew, q, qf, e, f, s;
161018c5995bSMatthew G. Knepley 
16119566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, oldp, &coneSize));
16129566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, oldp, &cone));
16139566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, oldp, &supportSize));
16149566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, oldp, &support));
161518c5995bSMatthew G. Knepley       if (dep == 0) {
161618c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
161718c5995bSMatthew G. Knepley 
161818c5995bSMatthew G. Knepley         /* Unsplit vertex */
16199566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, newp, &supportSizeNew));
162018c5995bSMatthew G. Knepley         for (s = 0, q = 0; s < supportSize; ++s) {
16212582d50cSToby Isaac           supportNew[q++] = DMPlexShiftPoint_Internal(support[s], depth, depthShift) /*support[s] + depthOffset[dep+1]*/;
16229566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSplitPoints[dep+1], splitPoints[dep+1], &e));
162318c5995bSMatthew G. Knepley           if (e >= 0) {
162418c5995bSMatthew G. Knepley             supportNew[q++] = e + pMaxNew[dep+1];
162518c5995bSMatthew G. Knepley           }
162618c5995bSMatthew G. Knepley         }
162718c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
162818c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
162963a3b9bcSJacob Faibussowitsch         PetscCheck(q == supportSizeNew,comm, PETSC_ERR_ARG_WRONG, "Support size %" PetscInt_FMT " != %" PetscInt_FMT " for vertex %" PetscInt_FMT, q, supportSizeNew, newp);
16309566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
163118c5995bSMatthew G. Knepley         /* Hybrid edge */
163218c5995bSMatthew G. Knepley         coneNew[0] = newp;
163318c5995bSMatthew G. Knepley         coneNew[1] = newp;
16349566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybedge, coneNew));
163518c5995bSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
163618c5995bSMatthew G. Knepley           PetscInt val, edge;
163718c5995bSMatthew G. Knepley 
16389566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[e], &val));
163918c5995bSMatthew G. Knepley           if (val == 1) {
16409566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge));
164163a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0,comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a split edge", support[e]);
164218c5995bSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2];
1643e1757548SMatthew G. Knepley           } else if  (val ==  (shift2 + 1)) {
16449566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[e], numUnsplitPoints[dep+1], unsplitPoints[dep+1], &edge));
164563a3b9bcSJacob Faibussowitsch             PetscCheck(edge >= 0,comm, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " is not a unsplit edge", support[e]);
1646e1757548SMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2] + numSplitPoints[dep+1];
164718c5995bSMatthew G. Knepley           }
164818c5995bSMatthew G. Knepley         }
16499566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybedge, supportNew));
1650e1757548SMatthew G. Knepley       } else if (dep == dim-2) {
1651e1757548SMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
1652e1757548SMatthew G. Knepley 
1653da1dd7e4SMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and hybrid face twice */
1654e1757548SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1655e1757548SMatthew G. Knepley           PetscInt val, face;
1656e1757548SMatthew G. Knepley 
16579566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[f], &val));
1658e1757548SMatthew G. Knepley           if (val == dim-1) {
16599566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[f], numSplitPoints[dep+1], splitPoints[dep+1], &face));
166063a3b9bcSJacob Faibussowitsch             PetscCheck(face >= 0,comm, PETSC_ERR_ARG_WRONG, "Face %" PetscInt_FMT " is not a split face", support[f]);
16612582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1662e1757548SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+1];
1663e1757548SMatthew G. Knepley           } else {
16642582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1665e1757548SMatthew G. Knepley           }
1666e1757548SMatthew G. Knepley         }
1667e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
1668e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
16699566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, newp, &supportSizeNew));
167063a3b9bcSJacob Faibussowitsch         PetscCheck(qf == supportSizeNew,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Support size for unsplit edge %" PetscInt_FMT " is %" PetscInt_FMT " != %" PetscInt_FMT, newp, qf, supportSizeNew);
16719566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, newp, supportNew));
1672e1757548SMatthew G. Knepley         /* Add hybrid face */
1673e1757548SMatthew G. Knepley         coneNew[0] = newp;
1674212cc919SMatthew G. Knepley         coneNew[1] = newp;
16759566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[0], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v));
167663a3b9bcSJacob Faibussowitsch         PetscCheck(v >= 0,comm, PETSC_ERR_ARG_WRONG, "Vertex %" PetscInt_FMT " is not an unsplit vertex", cone[0]);
1677212cc919SMatthew G. Knepley         coneNew[2] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
16789566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[1], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v));
167963a3b9bcSJacob Faibussowitsch         PetscCheck(v >= 0,comm, PETSC_ERR_ARG_WRONG, "Vertex %" PetscInt_FMT " is not an unsplit vertex", cone[1]);
1680e1757548SMatthew G. Knepley         coneNew[3] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
16819566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(sdm, hybface, coneNew));
1682da1dd7e4SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1683da1dd7e4SMatthew G. Knepley           PetscInt val, face;
1684da1dd7e4SMatthew G. Knepley 
16859566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(label, support[f], &val));
1686da1dd7e4SMatthew G. Knepley           if (val == dim-1) {
16879566063dSJacob Faibussowitsch             PetscCall(PetscFindInt(support[f], numSplitPoints[dep+1], splitPoints[dep+1], &face));
1688da1dd7e4SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+2] + numSplitPoints[dep+2];
1689da1dd7e4SMatthew G. Knepley           }
1690da1dd7e4SMatthew G. Knepley         }
16919566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(sdm, hybface, &supportSizeNew));
169263a3b9bcSJacob Faibussowitsch         PetscCheck(qf == supportSizeNew,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Support size for hybrid face %" PetscInt_FMT " is %" PetscInt_FMT " != %" PetscInt_FMT, hybface, qf, supportSizeNew);
16939566063dSJacob Faibussowitsch         PetscCall(DMPlexSetSupport(sdm, hybface, supportNew));
1694cd0c2139SMatthew G Knepley       }
1695cd0c2139SMatthew G Knepley     }
1696cd0c2139SMatthew G Knepley   }
1697cd0c2139SMatthew G Knepley   /* Step 6b: Replace split points in negative side cones */
1698cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1699cd0c2139SMatthew G Knepley     PetscInt        dep = values[sp];
1700cd0c2139SMatthew G Knepley     IS              pIS;
1701cd0c2139SMatthew G Knepley     PetscInt        numPoints;
1702cd0c2139SMatthew G Knepley     const PetscInt *points;
1703cd0c2139SMatthew G Knepley 
1704cd0c2139SMatthew G Knepley     if (dep >= 0) continue;
17059566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, dep, &pIS));
1706cd0c2139SMatthew G Knepley     if (!pIS) continue;
1707cd0c2139SMatthew G Knepley     dep  = -dep - shift;
17089566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(pIS, &numPoints));
17099566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(pIS, &points));
1710cd0c2139SMatthew G Knepley     for (p = 0; p < numPoints; ++p) {
1711cd0c2139SMatthew G Knepley       const PetscInt  oldp = points[p];
17122582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*depthOffset[dep] + oldp*/;
1713cd0c2139SMatthew G Knepley       const PetscInt *cone;
1714cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
171550cf782dSMatthew G. Knepley       /* PetscBool       replaced = PETSC_FALSE; */
1716cd0c2139SMatthew G Knepley 
1717cd0c2139SMatthew G Knepley       /* Negative edge: replace split vertex */
1718cd0c2139SMatthew G Knepley       /* Negative cell: replace split face */
17199566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(sdm, newp, &coneSize));
17209566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(sdm, newp, &cone));
1721cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
1722e38fbfedSToby Isaac         const PetscInt coldp = DMPlexShiftPointInverse_Internal(cone[c],depth,depthShift);
1723cd0c2139SMatthew G Knepley         PetscInt       csplitp, cp, val;
1724cd0c2139SMatthew G Knepley 
17259566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, coldp, &val));
1726cd0c2139SMatthew G Knepley         if (val == dep-1) {
17279566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(coldp, numSplitPoints[dep-1], splitPoints[dep-1], &cp));
172863a3b9bcSJacob Faibussowitsch           PetscCheck(cp >= 0,comm, PETSC_ERR_ARG_WRONG, "Point %" PetscInt_FMT " is not a split point of dimension %" PetscInt_FMT, oldp, dep-1);
1729cd0c2139SMatthew G Knepley           csplitp  = pMaxNew[dep-1] + cp;
17309566063dSJacob Faibussowitsch           PetscCall(DMPlexInsertCone(sdm, newp, c, csplitp));
173150cf782dSMatthew G. Knepley           /* replaced = PETSC_TRUE; */
1732cd0c2139SMatthew G Knepley         }
1733cd0c2139SMatthew G Knepley       }
17344a189a86SMatthew G. Knepley       /* Cells with only a vertex or edge on the submesh have no replacement */
173528b400f6SJacob Faibussowitsch       /* PetscCheck(replaced,comm, PETSC_ERR_ARG_WRONG, "The cone of point %d does not contain split points", oldp); */
1736cd0c2139SMatthew G Knepley     }
17379566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(pIS, &points));
17389566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pIS));
1739cd0c2139SMatthew G Knepley   }
1740fa8e8ae5SToby Isaac   /* Step 7: Coordinates */
17419566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftCoordinates_Internal(dm, depthShift, sdm));
17429566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(sdm, &coordSection));
17439566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(sdm, &coordinates));
17449566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
1745cd0c2139SMatthew G Knepley   for (v = 0; v < (numSplitPoints ? numSplitPoints[0] : 0); ++v) {
17462582d50cSToby Isaac     const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[0][v], depth, depthShift) /*depthOffset[0] + splitPoints[0][v]*/;
1747cd0c2139SMatthew G Knepley     const PetscInt splitp = pMaxNew[0] + v;
1748cd0c2139SMatthew G Knepley     PetscInt       dof, off, soff, d;
1749cd0c2139SMatthew G Knepley 
17509566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetDof(coordSection, newp, &dof));
17519566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, newp, &off));
17529566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetOffset(coordSection, splitp, &soff));
1753cd0c2139SMatthew G Knepley     for (d = 0; d < dof; ++d) coords[soff+d] = coords[off+d];
1754cd0c2139SMatthew G Knepley   }
17559566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
1756fa8e8ae5SToby Isaac   /* Step 8: SF, if I can figure this out we can split the mesh in parallel */
17579566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftSF_Internal(dm, depthShift, sdm));
1758b6dfa339SMatthew G. Knepley   /*   TODO We need to associate the ghost points with the correct replica */
1759fa8e8ae5SToby Isaac   /* Step 9: Labels */
17609566063dSJacob Faibussowitsch   PetscCall(DMPlexShiftLabels_Internal(dm, depthShift, sdm));
17619566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateVTKLabel_Internal(dm, PETSC_FALSE, sdm));
17629566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(sdm, &numLabels));
1763cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1764cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
17652582d50cSToby Isaac       const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[dep][p], depth, depthShift) /*depthOffset[dep] + splitPoints[dep][p]*/;
1766cd0c2139SMatthew G Knepley       const PetscInt splitp = pMaxNew[dep] + p;
1767cd0c2139SMatthew G Knepley       PetscInt       l;
1768cd0c2139SMatthew G Knepley 
17697db7e0a7SMatthew G. Knepley       if (splitLabel) {
17707db7e0a7SMatthew G. Knepley         const PetscInt val = 100 + dep;
17717db7e0a7SMatthew G. Knepley 
17729566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(splitLabel, newp,    val));
17739566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(splitLabel, splitp, -val));
17747db7e0a7SMatthew G. Knepley       }
1775cd0c2139SMatthew G Knepley       for (l = 0; l < numLabels; ++l) {
1776cd0c2139SMatthew G Knepley         DMLabel     mlabel;
1777cd0c2139SMatthew G Knepley         const char *lname;
1778cd0c2139SMatthew G Knepley         PetscInt    val;
17799a356370SMatthew G. Knepley         PetscBool   isDepth;
1780cd0c2139SMatthew G Knepley 
17819566063dSJacob Faibussowitsch         PetscCall(DMGetLabelName(sdm, l, &lname));
17829566063dSJacob Faibussowitsch         PetscCall(PetscStrcmp(lname, "depth", &isDepth));
17839a356370SMatthew G. Knepley         if (isDepth) continue;
17849566063dSJacob Faibussowitsch         PetscCall(DMGetLabel(sdm, lname, &mlabel));
17859566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(mlabel, newp, &val));
1786cd0c2139SMatthew G Knepley         if (val >= 0) {
17879566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(mlabel, splitp, val));
1788cd0c2139SMatthew G Knepley         }
1789cd0c2139SMatthew G Knepley       }
1790cd0c2139SMatthew G Knepley     }
1791cd0c2139SMatthew G Knepley   }
1792cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1793cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1794cd0c2139SMatthew G Knepley 
1795cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
17969566063dSJacob Faibussowitsch     if (splitIS[dep]) PetscCall(ISRestoreIndices(splitIS[dep], &splitPoints[dep]));
17979566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&splitIS[dep]));
17989566063dSJacob Faibussowitsch     if (unsplitIS[dep]) PetscCall(ISRestoreIndices(unsplitIS[dep], &unsplitPoints[dep]));
17999566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&unsplitIS[dep]));
1800cd0c2139SMatthew G Knepley   }
1801b6dfa339SMatthew G. Knepley   if (ghostIS) PetscCall(ISRestoreIndices(ghostIS, &ghostPoints));
1802b6dfa339SMatthew G. Knepley   PetscCall(ISDestroy(&ghostIS));
1803cd0c2139SMatthew G Knepley   if (label) {
18049566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
18059566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
1806cd0c2139SMatthew G Knepley   }
18070d4d4d06SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
18089566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(sdm, d, NULL, &pEnd));
180936dbac82SMatthew G. Knepley     pMaxNew[d] = pEnd - numHybridPoints[d] - numHybridPointsOld[d];
18100d4d4d06SMatthew G. Knepley   }
18119566063dSJacob Faibussowitsch   PetscCall(PetscFree3(coneNew, coneONew, supportNew));
18129566063dSJacob Faibussowitsch   PetscCall(PetscFree5(depthMax, depthEnd, depthShift, pMaxNew, numHybridPointsOld));
18139566063dSJacob Faibussowitsch   PetscCall(PetscFree7(splitIS, unsplitIS, numSplitPoints, numUnsplitPoints, numHybridPoints, splitPoints, unsplitPoints));
1814cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1815cd0c2139SMatthew G Knepley }
1816cd0c2139SMatthew G Knepley 
1817cd0c2139SMatthew G Knepley /*@C
1818cd0c2139SMatthew G Knepley   DMPlexConstructCohesiveCells - Construct cohesive cells which split the face along an internal interface
1819cd0c2139SMatthew G Knepley 
1820cd0c2139SMatthew G Knepley   Collective on dm
1821cd0c2139SMatthew G Knepley 
1822cd0c2139SMatthew G Knepley   Input Parameters:
1823cd0c2139SMatthew G Knepley + dm - The original DM
182453156dfcSMatthew G. Knepley - label - The label specifying the boundary faces (this could be auto-generated)
1825cd0c2139SMatthew G Knepley 
1826cd0c2139SMatthew G Knepley   Output Parameters:
18277db7e0a7SMatthew G. Knepley + splitLabel - The label containing the split points, or NULL if no output is desired
1828cd0c2139SMatthew G Knepley - dmSplit - The new DM
1829cd0c2139SMatthew G Knepley 
1830cd0c2139SMatthew G Knepley   Level: developer
1831cd0c2139SMatthew G Knepley 
1832db781477SPatrick Sanan .seealso: `DMCreate()`, `DMPlexLabelCohesiveComplete()`
1833cd0c2139SMatthew G Knepley @*/
18347db7e0a7SMatthew G. Knepley PetscErrorCode DMPlexConstructCohesiveCells(DM dm, DMLabel label, DMLabel splitLabel, DM *dmSplit)
1835cd0c2139SMatthew G Knepley {
1836cd0c2139SMatthew G Knepley   DM             sdm;
1837cd0c2139SMatthew G Knepley   PetscInt       dim;
1838cd0c2139SMatthew G Knepley 
1839cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1840cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1841064a246eSJacob Faibussowitsch   PetscValidPointer(dmSplit, 4);
18429566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &sdm));
18439566063dSJacob Faibussowitsch   PetscCall(DMSetType(sdm, DMPLEX));
18449566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
18459566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(sdm, dim));
1846cd0c2139SMatthew G Knepley   switch (dim) {
1847cd0c2139SMatthew G Knepley   case 2:
1848cd0c2139SMatthew G Knepley   case 3:
18499566063dSJacob Faibussowitsch     PetscCall(DMPlexConstructCohesiveCells_Internal(dm, label, splitLabel, sdm));
1850cd0c2139SMatthew G Knepley     break;
1851cd0c2139SMatthew G Knepley   default:
185263a3b9bcSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct cohesive cells for dimension %" PetscInt_FMT, dim);
1853cd0c2139SMatthew G Knepley   }
1854cd0c2139SMatthew G Knepley   *dmSplit = sdm;
1855cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1856cd0c2139SMatthew G Knepley }
1857cd0c2139SMatthew G Knepley 
18580f66a230SMatthew G. Knepley /* Returns the side of the surface for a given cell with a face on the surface */
18590f66a230SMatthew G. Knepley static PetscErrorCode GetSurfaceSide_Static(DM dm, DM subdm, PetscInt numSubpoints, const PetscInt *subpoints, PetscInt cell, PetscInt face, PetscBool *pos)
18600f66a230SMatthew G. Knepley {
18610f66a230SMatthew G. Knepley   const PetscInt *cone, *ornt;
18620f66a230SMatthew G. Knepley   PetscInt        dim, coneSize, c;
18630f66a230SMatthew G. Knepley 
18640f66a230SMatthew G. Knepley   PetscFunctionBegin;
18650f66a230SMatthew G. Knepley   *pos = PETSC_TRUE;
18669566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
18679566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
18689566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCone(dm, cell, &cone));
18699566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeOrientation(dm, cell, &ornt));
18700f66a230SMatthew G. Knepley   for (c = 0; c < coneSize; ++c) {
18710f66a230SMatthew G. Knepley     if (cone[c] == face) {
18720f66a230SMatthew G. Knepley       PetscInt o = ornt[c];
18730f66a230SMatthew G. Knepley 
18740f66a230SMatthew G. Knepley       if (subdm) {
18750f66a230SMatthew G. Knepley         const PetscInt *subcone, *subornt;
18760f66a230SMatthew G. Knepley         PetscInt        subpoint, subface, subconeSize, sc;
18770f66a230SMatthew G. Knepley 
18789566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cell, numSubpoints, subpoints, &subpoint));
18799566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(face, numSubpoints, subpoints, &subface));
18809566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeSize(subdm, subpoint, &subconeSize));
18819566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCone(subdm, subpoint, &subcone));
18829566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeOrientation(subdm, subpoint, &subornt));
18830f66a230SMatthew G. Knepley         for (sc = 0; sc < subconeSize; ++sc) {
18840f66a230SMatthew G. Knepley           if (subcone[sc] == subface) {
18850f66a230SMatthew G. Knepley             o = subornt[0];
18860f66a230SMatthew G. Knepley             break;
18870f66a230SMatthew G. Knepley           }
18880f66a230SMatthew G. Knepley         }
188963a3b9bcSJacob Faibussowitsch         PetscCheck(sc < subconeSize,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find subpoint %" PetscInt_FMT " (%" PetscInt_FMT ") in cone for subpoint %" PetscInt_FMT " (%" PetscInt_FMT ")", subface, face, subpoint, cell);
18900f66a230SMatthew G. Knepley       }
18910f66a230SMatthew G. Knepley       if (o >= 0) *pos = PETSC_TRUE;
18920f66a230SMatthew G. Knepley       else        *pos = PETSC_FALSE;
18930f66a230SMatthew G. Knepley       break;
18940f66a230SMatthew G. Knepley     }
18950f66a230SMatthew G. Knepley   }
189663a3b9bcSJacob Faibussowitsch   PetscCheck(c != coneSize,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Cell %" PetscInt_FMT " in split face %" PetscInt_FMT " support does not have it in the cone", cell, face);
18970f66a230SMatthew G. Knepley   PetscFunctionReturn(0);
18980f66a230SMatthew G. Knepley }
18990f66a230SMatthew G. Knepley 
1900accc9626SMatthew G. Knepley static PetscErrorCode CheckFaultEdge_Private(DM dm, DMLabel label)
1901accc9626SMatthew G. Knepley {
1902accc9626SMatthew G. Knepley   IS              facePosIS, faceNegIS, dimIS;
1903accc9626SMatthew G. Knepley   const PetscInt *points;
1904accc9626SMatthew G. Knepley   PetscInt        dim, numPoints, p, shift = 100, shift2 = 200;
1905accc9626SMatthew G. Knepley 
1906accc9626SMatthew G. Knepley   PetscFunctionBegin;
1907accc9626SMatthew G. Knepley   PetscCall(DMGetDimension(dm, &dim));
1908accc9626SMatthew G. Knepley   /* If any faces touching the fault divide cells on either side, split them */
1909accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label,   shift+dim-1,  &facePosIS));
1910accc9626SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(label, -(shift+dim-1), &faceNegIS));
1911accc9626SMatthew G. Knepley   if (!facePosIS || !faceNegIS) {
1912accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&facePosIS));
1913accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&faceNegIS));
1914accc9626SMatthew G. Knepley     PetscFunctionReturn(0);
1915accc9626SMatthew G. Knepley   }
1916accc9626SMatthew G. Knepley   PetscCall(ISExpand(facePosIS, faceNegIS, &dimIS));
1917accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&facePosIS));
1918accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&faceNegIS));
1919accc9626SMatthew G. Knepley   PetscCall(ISGetLocalSize(dimIS, &numPoints));
1920accc9626SMatthew G. Knepley   PetscCall(ISGetIndices(dimIS, &points));
1921accc9626SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
1922accc9626SMatthew G. Knepley     const PetscInt  point = points[p];
1923accc9626SMatthew G. Knepley     const PetscInt *support;
1924accc9626SMatthew G. Knepley     PetscInt        supportSize, valA, valB;
1925accc9626SMatthew G. Knepley 
1926accc9626SMatthew G. Knepley     PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
1927accc9626SMatthew G. Knepley     if (supportSize != 2) continue;
1928accc9626SMatthew G. Knepley     PetscCall(DMPlexGetSupport(dm, point, &support));
1929accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, support[0], &valA));
1930accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, support[1], &valB));
1931accc9626SMatthew G. Knepley     if ((valA == -1) || (valB == -1)) continue;
1932accc9626SMatthew G. Knepley     if (valA*valB > 0) continue;
1933e1a13daeSMatthew G. Knepley     /* Check that this face is not incident on only unsplit faces, meaning has at least one split face */
1934e1a13daeSMatthew G. Knepley     {
1935e1a13daeSMatthew G. Knepley       PetscInt *closure = NULL;
1936e1a13daeSMatthew G. Knepley       PetscBool split   = PETSC_FALSE;
1937e1a13daeSMatthew G. Knepley       PetscInt  closureSize, cl;
1938e1a13daeSMatthew G. Knepley 
1939e1a13daeSMatthew G. Knepley       PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
1940e1a13daeSMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
1941e1a13daeSMatthew G. Knepley         PetscCall(DMLabelGetValue(label, closure[cl], &valA));
1942e1a13daeSMatthew G. Knepley         if ((valA >= 0) && (valA <= dim)) {split = PETSC_TRUE; break;}
1943e1a13daeSMatthew G. Knepley       }
1944e1a13daeSMatthew G. Knepley       PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
1945e1a13daeSMatthew G. Knepley       if (!split) continue;
1946e1a13daeSMatthew G. Knepley     }
1947accc9626SMatthew G. Knepley     /* Split the face */
1948accc9626SMatthew G. Knepley     PetscCall(DMLabelGetValue(label, point, &valA));
1949accc9626SMatthew G. Knepley     PetscCall(DMLabelClearValue(label, point, valA));
1950accc9626SMatthew G. Knepley     PetscCall(DMLabelSetValue(label, point, dim-1));
1951accc9626SMatthew G. Knepley     /* Label its closure:
1952accc9626SMatthew G. Knepley       unmarked: label as unsplit
1953accc9626SMatthew G. Knepley       incident: relabel as split
1954accc9626SMatthew G. Knepley       split:    do nothing
1955accc9626SMatthew G. Knepley     */
1956accc9626SMatthew G. Knepley     {
1957accc9626SMatthew G. Knepley       PetscInt *closure = NULL;
1958accc9626SMatthew G. Knepley       PetscInt  closureSize, cl, dep;
1959accc9626SMatthew G. Knepley 
1960accc9626SMatthew G. Knepley       PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
1961accc9626SMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
1962accc9626SMatthew G. Knepley         PetscCall(DMLabelGetValue(label, closure[cl], &valA));
1963accc9626SMatthew G. Knepley         if (valA == -1) { /* Mark as unsplit */
1964accc9626SMatthew G. Knepley           PetscCall(DMPlexGetPointDepth(dm, closure[cl], &dep));
1965accc9626SMatthew G. Knepley           PetscCall(DMLabelSetValue(label, closure[cl], shift2+dep));
1966accc9626SMatthew G. Knepley         } else if (((valA >= shift) && (valA < shift2)) || ((valA <= -shift) && (valA > -shift2))) {
1967accc9626SMatthew G. Knepley           PetscCall(DMPlexGetPointDepth(dm, closure[cl], &dep));
1968accc9626SMatthew G. Knepley           PetscCall(DMLabelClearValue(label, closure[cl], valA));
1969accc9626SMatthew G. Knepley           PetscCall(DMLabelSetValue(label, closure[cl], dep));
1970accc9626SMatthew G. Knepley         }
1971accc9626SMatthew G. Knepley       }
1972accc9626SMatthew G. Knepley       PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
1973accc9626SMatthew G. Knepley     }
1974accc9626SMatthew G. Knepley   }
1975accc9626SMatthew G. Knepley   PetscCall(ISRestoreIndices(dimIS, &points));
1976accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&dimIS));
1977accc9626SMatthew G. Knepley   PetscFunctionReturn(0);
1978accc9626SMatthew G. Knepley }
1979accc9626SMatthew G. Knepley 
1980cd0c2139SMatthew G Knepley /*@
19810f66a230SMatthew G. Knepley   DMPlexLabelCohesiveComplete - Starting with a label marking points on an internal surface, we add all other mesh pieces
1982cd0c2139SMatthew G Knepley   to complete the surface
1983cd0c2139SMatthew G Knepley 
1984cd0c2139SMatthew G Knepley   Input Parameters:
1985cd0c2139SMatthew G Knepley + dm     - The DM
19860f66a230SMatthew G. Knepley . label  - A DMLabel marking the surface
19870f66a230SMatthew G. Knepley . blabel - A DMLabel marking the vertices on the boundary which will not be duplicated, or NULL to find them automatically
1988caf9e14dSMatthew G. Knepley . bvalue - Value of DMLabel marking the vertices on the boundary
1989bb55d314SMatthew G. Knepley . flip   - Flag to flip the submesh normal and replace points on the other side
199047946fd8SMatthew G. Knepley - subdm  - The subDM associated with the label, or NULL
1991cd0c2139SMatthew G Knepley 
1992cd0c2139SMatthew G Knepley   Output Parameter:
1993cd0c2139SMatthew G Knepley . label - A DMLabel marking all surface points
1994cd0c2139SMatthew G Knepley 
19950f66a230SMatthew G. Knepley   Note: The vertices in blabel are called "unsplit" in the terminology from hybrid cell creation.
19960f66a230SMatthew G. Knepley 
1997cd0c2139SMatthew G Knepley   Level: developer
1998cd0c2139SMatthew G Knepley 
1999db781477SPatrick Sanan .seealso: `DMPlexConstructCohesiveCells()`, `DMPlexLabelComplete()`
2000cd0c2139SMatthew G Knepley @*/
2001caf9e14dSMatthew G. Knepley PetscErrorCode DMPlexLabelCohesiveComplete(DM dm, DMLabel label, DMLabel blabel, PetscInt bvalue, PetscBool flip, DM subdm)
2002cd0c2139SMatthew G Knepley {
2003d90583fdSMatthew G. Knepley   DMLabel         depthLabel;
2004accc9626SMatthew G. Knepley   IS              dimIS, subpointIS = NULL;
200547946fd8SMatthew G. Knepley   const PetscInt *points, *subpoints;
2006bb55d314SMatthew G. Knepley   const PetscInt  rev   = flip ? -1 : 1;
2007accc9626SMatthew G. Knepley   PetscInt        shift = 100, shift2 = 200, shift3 = 300, dim, depth, numPoints, numSubpoints, p, val;
2008cd0c2139SMatthew G Knepley 
2009cd0c2139SMatthew G Knepley   PetscFunctionBegin;
20109566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
20119566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
20129566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthLabel(dm, &depthLabel));
201347946fd8SMatthew G. Knepley   if (subdm) {
20149566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(subdm, &subpointIS));
201547946fd8SMatthew G. Knepley     if (subpointIS) {
20169566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(subpointIS, &numSubpoints));
20179566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subpointIS, &subpoints));
201847946fd8SMatthew G. Knepley     }
201947946fd8SMatthew G. Knepley   }
2020d7c8f101SMatthew G. Knepley   /* Mark cell on the fault, and its faces which touch the fault: cell orientation for face gives the side of the fault */
20219566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(label, dim-1, &dimIS));
2022accc9626SMatthew G. Knepley   if (!dimIS) goto divide;
20239566063dSJacob Faibussowitsch   PetscCall(ISGetLocalSize(dimIS, &numPoints));
20249566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(dimIS, &points));
2025d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault faces */
2026cd0c2139SMatthew G Knepley     const PetscInt *support;
2027cd0c2139SMatthew G Knepley     PetscInt        supportSize, s;
2028cd0c2139SMatthew G Knepley 
20299566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupportSize(dm, points[p], &supportSize));
2030c4419245SMatthew G. Knepley #if 0
2031c4419245SMatthew G. Knepley     if (supportSize != 2) {
2032c4419245SMatthew G. Knepley       const PetscInt *lp;
2033c4419245SMatthew G. Knepley       PetscInt        Nlp, pind;
2034c4419245SMatthew G. Knepley 
2035c4419245SMatthew G. Knepley       /* Check that for a cell with a single support face, that face is in the SF */
2036c4419245SMatthew G. Knepley       /*   THis check only works for the remote side. We would need root side information */
20379566063dSJacob Faibussowitsch       PetscCall(PetscSFGetGraph(dm->sf, NULL, &Nlp, &lp, NULL));
20389566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(points[p], Nlp, lp, &pind));
203963a3b9bcSJacob Faibussowitsch       PetscCheck(pind >= 0,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Split face %" PetscInt_FMT " has %" PetscInt_FMT " != 2 supports, and the face is not shared with another process", points[p], supportSize);
2040c4419245SMatthew G. Knepley     }
2041c4419245SMatthew G. Knepley #endif
20429566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSupport(dm, points[p], &support));
2043cd0c2139SMatthew G Knepley     for (s = 0; s < supportSize; ++s) {
20440f66a230SMatthew G. Knepley       const PetscInt *cone;
2045cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
20460f66a230SMatthew G. Knepley       PetscBool       pos;
2047cd0c2139SMatthew G Knepley 
20489566063dSJacob Faibussowitsch       PetscCall(GetSurfaceSide_Static(dm, subdm, numSubpoints, subpoints, support[s], points[p], &pos));
20499566063dSJacob Faibussowitsch       if (pos) PetscCall(DMLabelSetValue(label, support[s],  rev*(shift+dim)));
20509566063dSJacob Faibussowitsch       else     PetscCall(DMLabelSetValue(label, support[s], -rev*(shift+dim)));
20510f66a230SMatthew G. Knepley       if (rev < 0) pos = !pos ? PETSC_TRUE : PETSC_FALSE;
20520f66a230SMatthew G. Knepley       /* Put faces touching the fault in the label */
20539566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, support[s], &coneSize));
20549566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, support[s], &cone));
2055cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
2056cd0c2139SMatthew G Knepley         const PetscInt point = cone[c];
2057cd0c2139SMatthew G Knepley 
20589566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2059cd0c2139SMatthew G Knepley         if (val == -1) {
2060cd0c2139SMatthew G Knepley           PetscInt *closure = NULL;
2061cd0c2139SMatthew G Knepley           PetscInt  closureSize, cl;
2062cd0c2139SMatthew G Knepley 
20639566063dSJacob Faibussowitsch           PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2064cd0c2139SMatthew G Knepley           for (cl = 0; cl < closureSize*2; cl += 2) {
2065cd0c2139SMatthew G Knepley             const PetscInt clp  = closure[cl];
2066a0541d8aSMatthew G. Knepley             PetscInt       bval = -1;
2067cd0c2139SMatthew G Knepley 
20689566063dSJacob Faibussowitsch             PetscCall(DMLabelGetValue(label, clp, &val));
20699566063dSJacob Faibussowitsch             if (blabel) PetscCall(DMLabelGetValue(blabel, clp, &bval));
2070a0541d8aSMatthew G. Knepley             if ((val >= 0) && (val < dim-1) && (bval < 0)) {
20719566063dSJacob Faibussowitsch               PetscCall(DMLabelSetValue(label, point, pos == PETSC_TRUE ? shift+dim-1 : -(shift+dim-1)));
2072cd0c2139SMatthew G Knepley               break;
2073cd0c2139SMatthew G Knepley             }
2074cd0c2139SMatthew G Knepley           }
20759566063dSJacob Faibussowitsch           PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2076cd0c2139SMatthew G Knepley         }
2077cd0c2139SMatthew G Knepley       }
2078cd0c2139SMatthew G Knepley     }
2079cd0c2139SMatthew G Knepley   }
2080accc9626SMatthew G. Knepley   PetscCall(ISRestoreIndices(dimIS, &points));
2081accc9626SMatthew G. Knepley   PetscCall(ISDestroy(&dimIS));
2082a0541d8aSMatthew G. Knepley   /* Mark boundary points as unsplit */
208386200784SMatthew G. Knepley   if (blabel) {
2084accc9626SMatthew G. Knepley     IS bdIS;
2085accc9626SMatthew G. Knepley 
2086caf9e14dSMatthew G. Knepley     PetscCall(DMLabelGetStratumIS(blabel, bvalue, &bdIS));
2087accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(bdIS, &numPoints));
2088accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(bdIS, &points));
2089a0541d8aSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2090a0541d8aSMatthew G. Knepley       const PetscInt point = points[p];
2091a0541d8aSMatthew G. Knepley       PetscInt       val, bval;
2092a0541d8aSMatthew G. Knepley 
20939566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(blabel, point, &bval));
2094a0541d8aSMatthew G. Knepley       if (bval >= 0) {
20959566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2096f7019248SMatthew G. Knepley         if ((val < 0) || (val > dim)) {
2097f7019248SMatthew G. Knepley           /* This could be a point added from splitting a vertex on an adjacent fault, otherwise its just wrong */
20989566063dSJacob Faibussowitsch           PetscCall(DMLabelClearValue(blabel, point, bval));
2099f7019248SMatthew G. Knepley         }
2100f7019248SMatthew G. Knepley       }
2101f7019248SMatthew G. Knepley     }
2102f7019248SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2103f7019248SMatthew G. Knepley       const PetscInt point = points[p];
2104f7019248SMatthew G. Knepley       PetscInt       val, bval;
2105f7019248SMatthew G. Knepley 
21069566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(blabel, point, &bval));
2107f7019248SMatthew G. Knepley       if (bval >= 0) {
210886200784SMatthew G. Knepley         const PetscInt *cone,    *support;
210986200784SMatthew G. Knepley         PetscInt        coneSize, supportSize, s, valA, valB, valE;
211086200784SMatthew G. Knepley 
2111a0541d8aSMatthew G. Knepley         /* Mark as unsplit */
21129566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(label, point, &val));
2113e1a13daeSMatthew G. Knepley         PetscCheck(val >= 0 && val <= dim, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %" PetscInt_FMT " has label value %" PetscInt_FMT ", should be part of the fault", point, val);
21149566063dSJacob Faibussowitsch         PetscCall(DMLabelClearValue(label, point, val));
21159566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(label, point, shift2+val));
21162c06a818SMatthew G. Knepley         /* Check for cross-edge
21172c06a818SMatthew G. Knepley              A cross-edge has endpoints which are both on the boundary of the surface, but the edge itself is not. */
211886200784SMatthew G. Knepley         if (val != 0) continue;
21199566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, point, &support));
21209566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
212186200784SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
21229566063dSJacob Faibussowitsch           PetscCall(DMPlexGetCone(dm, support[s], &cone));
21239566063dSJacob Faibussowitsch           PetscCall(DMPlexGetConeSize(dm, support[s], &coneSize));
212463a3b9bcSJacob Faibussowitsch           PetscCheck(coneSize == 2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Edge %" PetscInt_FMT " has %" PetscInt_FMT " vertices != 2", support[s], coneSize);
21259566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, cone[0], &valA));
21269566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, cone[1], &valB));
21279566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(blabel, support[s], &valE));
21289566063dSJacob Faibussowitsch           if ((valE < 0) && (valA >= 0) && (valB >= 0) && (cone[0] != cone[1])) PetscCall(DMLabelSetValue(blabel, support[s], 2));
212986200784SMatthew G. Knepley         }
2130a0541d8aSMatthew G. Knepley       }
2131a0541d8aSMatthew G. Knepley     }
2132accc9626SMatthew G. Knepley     PetscCall(ISRestoreIndices(bdIS, &points));
2133accc9626SMatthew G. Knepley     PetscCall(ISDestroy(&bdIS));
2134a0541d8aSMatthew G. Knepley   }
2135b6dfa339SMatthew G. Knepley   /* Mark ghost fault cells */
2136b6dfa339SMatthew G. Knepley   {
2137b6dfa339SMatthew G. Knepley     PetscSF         sf;
2138b6dfa339SMatthew G. Knepley     const PetscInt *leaves;
2139b6dfa339SMatthew G. Knepley     PetscInt         Nl, l;
2140b6dfa339SMatthew G. Knepley 
2141b6dfa339SMatthew G. Knepley     PetscCall(DMGetPointSF(dm, &sf));
2142b6dfa339SMatthew G. Knepley     PetscCall(PetscSFGetGraph(sf, NULL, &Nl, &leaves, NULL));
2143accc9626SMatthew G. Knepley     PetscCall(DMLabelGetStratumIS(label, dim-1, &dimIS));
2144accc9626SMatthew G. Knepley     if (!dimIS) goto divide;
2145accc9626SMatthew G. Knepley     PetscCall(ISGetLocalSize(dimIS, &numPoints));
2146accc9626SMatthew G. Knepley     PetscCall(ISGetIndices(dimIS, &points));
2147b6dfa339SMatthew G. Knepley     if (Nl > 0) {
2148b6dfa339SMatthew G. Knepley       for (p = 0; p < numPoints; ++p) {
2149b6dfa339SMatthew G. Knepley         const PetscInt point = points[p];
2150b6dfa339SMatthew G. Knepley         PetscInt       val;
2151b6dfa339SMatthew G. Knepley 
2152b6dfa339SMatthew G. Knepley         PetscCall(PetscFindInt(point, Nl, leaves, &l));
2153b6dfa339SMatthew G. Knepley         if (l >= 0) {
2154b6dfa339SMatthew G. Knepley           PetscInt *closure = NULL;
2155b6dfa339SMatthew G. Knepley           PetscInt  closureSize, cl;
2156b6dfa339SMatthew G. Knepley 
2157b6dfa339SMatthew G. Knepley           PetscCall(DMLabelGetValue(label, point, &val));
2158b6dfa339SMatthew 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);
2159b6dfa339SMatthew G. Knepley           PetscCall(DMLabelClearValue(label, point, val));
2160b6dfa339SMatthew G. Knepley           PetscCall(DMLabelSetValue(label, point, shift3+val));
2161b6dfa339SMatthew G. Knepley           PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2162accc9626SMatthew G. Knepley           for (cl = 2; cl < closureSize*2; cl += 2) {
2163b6dfa339SMatthew G. Knepley             const PetscInt clp  = closure[cl];
2164b6dfa339SMatthew G. Knepley 
2165b6dfa339SMatthew G. Knepley             PetscCall(DMLabelGetValue(label, clp, &val));
2166b6dfa339SMatthew 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);
2167b6dfa339SMatthew G. Knepley             PetscCall(DMLabelClearValue(label, clp, val));
2168b6dfa339SMatthew G. Knepley             PetscCall(DMLabelSetValue(label, clp, shift3+val));
2169b6dfa339SMatthew G. Knepley           }
2170b6dfa339SMatthew G. Knepley           PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
2171b6dfa339SMatthew G. Knepley         }
2172b6dfa339SMatthew G. Knepley       }
2173b6dfa339SMatthew G. Knepley     }
2174b6dfa339SMatthew G. Knepley     PetscCall(ISRestoreIndices(dimIS, &points));
2175b6dfa339SMatthew G. Knepley     PetscCall(ISDestroy(&dimIS));
2176accc9626SMatthew G. Knepley   }
2177accc9626SMatthew G. Knepley   divide:
2178b6dfa339SMatthew G. Knepley   if (subpointIS) PetscCall(ISRestoreIndices(subpointIS, &subpoints));
2179accc9626SMatthew G. Knepley   PetscCall(DMPlexLabelFaultHalo(dm, label));
2180accc9626SMatthew G. Knepley   PetscCall(CheckFaultEdge_Private(dm, label));
2181cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
2182cd0c2139SMatthew G Knepley }
2183cd0c2139SMatthew G Knepley 
2184720e594eSMatthew G. Knepley /* Check that no cell have all vertices on the fault */
2185720e594eSMatthew G. Knepley PetscErrorCode DMPlexCheckValidSubmesh_Private(DM dm, DMLabel label, DM subdm)
2186720e594eSMatthew G. Knepley {
2187720e594eSMatthew G. Knepley   IS              subpointIS;
2188720e594eSMatthew G. Knepley   const PetscInt *dmpoints;
2189720e594eSMatthew G. Knepley   PetscInt        defaultValue, cStart, cEnd, c, vStart, vEnd;
2190720e594eSMatthew G. Knepley 
2191720e594eSMatthew G. Knepley   PetscFunctionBegin;
2192720e594eSMatthew G. Knepley   if (!label) PetscFunctionReturn(0);
21939566063dSJacob Faibussowitsch   PetscCall(DMLabelGetDefaultValue(label, &defaultValue));
21949566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointIS(subdm, &subpointIS));
2195720e594eSMatthew G. Knepley   if (!subpointIS) PetscFunctionReturn(0);
21969566063dSJacob Faibussowitsch   PetscCall(DMPlexGetHeightStratum(subdm, 0, &cStart, &cEnd));
21979566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
21989566063dSJacob Faibussowitsch   PetscCall(ISGetIndices(subpointIS, &dmpoints));
2199720e594eSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2200720e594eSMatthew G. Knepley     PetscBool invalidCell = PETSC_TRUE;
2201720e594eSMatthew G. Knepley     PetscInt *closure     = NULL;
2202720e594eSMatthew G. Knepley     PetscInt  closureSize, cl;
2203720e594eSMatthew G. Knepley 
22049566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure));
2205720e594eSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
2206720e594eSMatthew G. Knepley       PetscInt value = 0;
2207720e594eSMatthew G. Knepley 
2208720e594eSMatthew G. Knepley       if ((closure[cl] < vStart) || (closure[cl] >= vEnd)) continue;
22099566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, closure[cl], &value));
2210720e594eSMatthew G. Knepley       if (value == defaultValue) {invalidCell = PETSC_FALSE; break;}
2211720e594eSMatthew G. Knepley     }
22129566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure));
2213720e594eSMatthew G. Knepley     if (invalidCell) {
22149566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(subpointIS, &dmpoints));
22159566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&subpointIS));
22169566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&subdm));
221763a3b9bcSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ambiguous submesh. Cell %" PetscInt_FMT " has all of its vertices on the submesh.", dmpoints[c]);
2218720e594eSMatthew G. Knepley     }
2219720e594eSMatthew G. Knepley   }
22209566063dSJacob Faibussowitsch   PetscCall(ISRestoreIndices(subpointIS, &dmpoints));
2221720e594eSMatthew G. Knepley   PetscFunctionReturn(0);
2222720e594eSMatthew G. Knepley }
2223720e594eSMatthew G. Knepley 
2224c08575a3SMatthew G. Knepley /*@
22253cf72582SMatthew G. Knepley   DMPlexCreateHybridMesh - Create a mesh with hybrid cells along an internal interface
22263cf72582SMatthew G. Knepley 
22273cf72582SMatthew G. Knepley   Collective on dm
22283cf72582SMatthew G. Knepley 
22293cf72582SMatthew G. Knepley   Input Parameters:
22303cf72582SMatthew G. Knepley + dm - The original DM
2231720e594eSMatthew G. Knepley . label - The label specifying the interface vertices
2232caf9e14dSMatthew G. Knepley . bdlabel - The optional label specifying the interface boundary vertices
2233caf9e14dSMatthew G. Knepley - bdvalue - Value of optional label specifying the interface boundary vertices
22343cf72582SMatthew G. Knepley 
22353cf72582SMatthew G. Knepley   Output Parameters:
22367db7e0a7SMatthew G. Knepley + hybridLabel - The label fully marking the interface, or NULL if no output is desired
22377db7e0a7SMatthew G. Knepley . splitLabel - The label containing the split points, or NULL if no output is desired
2238720e594eSMatthew G. Knepley . dmInterface - The new interface DM, or NULL
2239720e594eSMatthew G. Knepley - dmHybrid - The new DM with cohesive cells
22403cf72582SMatthew G. Knepley 
22416eccb800SMatthew Knepley   Note: The hybridLabel indicates what parts of the original mesh impinged on the on division surface. For points
22426eccb800SMatthew Knepley   directly on the division surface, they are labeled with their dimension, so an edge 7 on the division surface would be
22436eccb800SMatthew Knepley   7 (1) in hybridLabel. For points that impinge from the positive side, they are labeled with 100+dim, so an edge 6 with
22446eccb800SMatthew 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
22456eccb800SMatthew Knepley   surface also hits vertex 3, it would be 9 (-101) in hybridLabel.
22466eccb800SMatthew Knepley 
22476eccb800SMatthew Knepley   The splitLabel indicates what points in the new hybrid mesh were the result of splitting points in the original
22486eccb800SMatthew Knepley   mesh. The label value is +=100+dim for each point. For example, if two edges 10 and 14 in the hybrid resulting from
22496eccb800SMatthew Knepley   splitting an edge in the original mesh, you would have 10 (101) and 14 (-101) in the splitLabel.
22506eccb800SMatthew Knepley 
22516eccb800SMatthew Knepley   The dmInterface is a DM built from the original division surface. It has a label which can be retrieved using
22526eccb800SMatthew Knepley   DMPlexGetSubpointMap() which maps each point back to the point in the surface of the original mesh.
22536eccb800SMatthew Knepley 
22543cf72582SMatthew G. Knepley   Level: developer
22553cf72582SMatthew G. Knepley 
2256db781477SPatrick Sanan .seealso: `DMPlexConstructCohesiveCells()`, `DMPlexLabelCohesiveComplete()`, `DMPlexGetSubpointMap()`, `DMCreate()`
22573cf72582SMatthew G. Knepley @*/
2258caf9e14dSMatthew G. Knepley PetscErrorCode DMPlexCreateHybridMesh(DM dm, DMLabel label, DMLabel bdlabel, PetscInt bdvalue, DMLabel *hybridLabel, DMLabel *splitLabel, DM *dmInterface, DM *dmHybrid)
22593cf72582SMatthew G. Knepley {
22603cf72582SMatthew G. Knepley   DM             idm;
22617db7e0a7SMatthew G. Knepley   DMLabel        subpointMap, hlabel, slabel = NULL;
22623cf72582SMatthew G. Knepley   PetscInt       dim;
22633cf72582SMatthew G. Knepley 
22643cf72582SMatthew G. Knepley   PetscFunctionBegin;
22653cf72582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
226692ef8ba2SVaclav Hapla   if (label) PetscValidPointer(label, 2);
2267720e594eSMatthew G. Knepley   if (bdlabel) PetscValidPointer(bdlabel, 3);
226892ef8ba2SVaclav Hapla   if (hybridLabel) PetscValidPointer(hybridLabel, 5);
226992ef8ba2SVaclav Hapla   if (splitLabel)  PetscValidPointer(splitLabel, 6);
227092ef8ba2SVaclav Hapla   if (dmInterface) PetscValidPointer(dmInterface, 7);
227192ef8ba2SVaclav Hapla   PetscValidPointer(dmHybrid, 8);
22729566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
22739566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmesh(dm, label, 1, PETSC_FALSE, &idm));
22749566063dSJacob Faibussowitsch   PetscCall(DMPlexCheckValidSubmesh_Private(dm, label, idm));
22759566063dSJacob Faibussowitsch   PetscCall(DMPlexOrient(idm));
22769566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(idm, &subpointMap));
22779566063dSJacob Faibussowitsch   PetscCall(DMLabelDuplicate(subpointMap, &hlabel));
22789566063dSJacob Faibussowitsch   PetscCall(DMLabelClearStratum(hlabel, dim));
22797db7e0a7SMatthew G. Knepley   if (splitLabel) {
22807db7e0a7SMatthew G. Knepley     const char *name;
22817db7e0a7SMatthew G. Knepley     char        sname[PETSC_MAX_PATH_LEN];
22827db7e0a7SMatthew G. Knepley 
22839566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) hlabel, &name));
22849566063dSJacob Faibussowitsch     PetscCall(PetscStrncpy(sname, name, PETSC_MAX_PATH_LEN));
22859566063dSJacob Faibussowitsch     PetscCall(PetscStrcat(sname, " split"));
22869566063dSJacob Faibussowitsch     PetscCall(DMLabelCreate(PETSC_COMM_SELF, sname, &slabel));
22877db7e0a7SMatthew G. Knepley   }
2288caf9e14dSMatthew G. Knepley   PetscCall(DMPlexLabelCohesiveComplete(dm, hlabel, bdlabel, bdvalue, PETSC_FALSE, idm));
2289720e594eSMatthew G. Knepley   if (dmInterface) {*dmInterface = idm;}
22909566063dSJacob Faibussowitsch   else             PetscCall(DMDestroy(&idm));
22919566063dSJacob Faibussowitsch   PetscCall(DMPlexConstructCohesiveCells(dm, hlabel, slabel, dmHybrid));
22925de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *dmHybrid));
22933cf72582SMatthew G. Knepley   if (hybridLabel) *hybridLabel = hlabel;
22949566063dSJacob Faibussowitsch   else             PetscCall(DMLabelDestroy(&hlabel));
22957db7e0a7SMatthew G. Knepley   if (splitLabel)  *splitLabel  = slabel;
22964a7ee7d0SMatthew G. Knepley   {
22974a7ee7d0SMatthew G. Knepley     DM      cdm;
22984a7ee7d0SMatthew G. Knepley     DMLabel ctLabel;
22994a7ee7d0SMatthew G. Knepley 
23004a7ee7d0SMatthew G. Knepley     /* We need to somehow share the celltype label with the coordinate dm */
23019566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(*dmHybrid, &cdm));
23029566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCellTypeLabel(*dmHybrid, &ctLabel));
23039566063dSJacob Faibussowitsch     PetscCall(DMSetLabel(cdm, ctLabel));
23044a7ee7d0SMatthew G. Knepley   }
2305cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
2306cd0c2139SMatthew G Knepley }
2307cd0c2139SMatthew G Knepley 
2308efa14ee0SMatthew G Knepley /* Here we need the explicit assumption that:
2309efa14ee0SMatthew G Knepley 
2310efa14ee0SMatthew G Knepley      For any marked cell, the marked vertices constitute a single face
2311efa14ee0SMatthew G Knepley */
2312830e53efSMatthew G. Knepley static PetscErrorCode DMPlexMarkSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, DM subdm)
2313efa14ee0SMatthew G Knepley {
2314fed694aaSMatthew G. Knepley   IS               subvertexIS = NULL;
2315efa14ee0SMatthew G Knepley   const PetscInt  *subvertices;
2316412e9a14SMatthew G. Knepley   PetscInt        *pStart, *pEnd, pSize;
2317efa14ee0SMatthew G Knepley   PetscInt         depth, dim, d, numSubVerticesInitial = 0, v;
2318efa14ee0SMatthew G Knepley 
2319efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2320efa14ee0SMatthew G Knepley   *numFaces = 0;
2321efa14ee0SMatthew G Knepley   *nFV      = 0;
23229566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
23239566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
232477d178adSMatthew G. Knepley   pSize = PetscMax(depth, dim) + 1;
23259566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(pSize, &pStart, pSize, &pEnd));
2326efa14ee0SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
23279566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSimplexOrBoxCells(dm, depth-d, &pStart[d], &pEnd[d]));
2328efa14ee0SMatthew G Knepley   }
2329efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
23309566063dSJacob Faibussowitsch   if (vertexLabel) PetscCall(DMLabelGetStratumIS(vertexLabel, value, &subvertexIS));
2331efa14ee0SMatthew G Knepley   if (subvertexIS) {
23329566063dSJacob Faibussowitsch     PetscCall(ISGetSize(subvertexIS, &numSubVerticesInitial));
23339566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subvertexIS, &subvertices));
2334efa14ee0SMatthew G Knepley   }
2335efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2336efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
23370298fd71SBarry Smith     PetscInt      *star   = NULL;
2338efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numCells = 0, c;
2339efa14ee0SMatthew G Knepley 
23409566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2341efa14ee0SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
2342efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2343efa14ee0SMatthew G Knepley       if ((point >= pStart[depth]) && (point < pEnd[depth])) star[numCells++] = point;
2344efa14ee0SMatthew G Knepley     }
2345efa14ee0SMatthew G Knepley     for (c = 0; c < numCells; ++c) {
2346efa14ee0SMatthew G Knepley       const PetscInt cell    = star[c];
23470298fd71SBarry Smith       PetscInt      *closure = NULL;
2348efa14ee0SMatthew G Knepley       PetscInt       closureSize, cl;
2349efa14ee0SMatthew G Knepley       PetscInt       cellLoc, numCorners = 0, faceSize = 0;
2350efa14ee0SMatthew G Knepley 
23519566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(subpointMap, cell, &cellLoc));
235265560c7fSMatthew G Knepley       if (cellLoc == 2) continue;
235363a3b9bcSJacob Faibussowitsch       PetscCheck(cellLoc < 0,PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %" PetscInt_FMT " has dimension %" PetscInt_FMT " in the surface label", cell, cellLoc);
23549566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
2355efa14ee0SMatthew G Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
2356efa14ee0SMatthew G Knepley         const PetscInt point = closure[cl];
2357efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2358efa14ee0SMatthew G Knepley 
2359efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
2360efa14ee0SMatthew G Knepley           ++numCorners;
23619566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &vertexLoc));
2362830e53efSMatthew G. Knepley           if (vertexLoc == value) closure[faceSize++] = point;
2363efa14ee0SMatthew G Knepley         }
2364efa14ee0SMatthew G Knepley       }
23659566063dSJacob Faibussowitsch       if (!(*nFV)) PetscCall(DMPlexGetNumFaceVertices(dm, dim, numCorners, nFV));
236663a3b9bcSJacob Faibussowitsch       PetscCheck(faceSize <= *nFV,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %" PetscInt_FMT " of an element on the surface", faceSize);
2367efa14ee0SMatthew G Knepley       if (faceSize == *nFV) {
2368007baee2SMatthew G. Knepley         const PetscInt *cells = NULL;
2369007baee2SMatthew G. Knepley         PetscInt        numCells, nc;
2370007baee2SMatthew G. Knepley 
2371efa14ee0SMatthew G Knepley         ++(*numFaces);
2372efa14ee0SMatthew G Knepley         for (cl = 0; cl < faceSize; ++cl) {
23739566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(subpointMap, closure[cl], 0));
2374efa14ee0SMatthew G Knepley         }
23759566063dSJacob Faibussowitsch         PetscCall(DMPlexGetJoin(dm, faceSize, closure, &numCells, &cells));
2376007baee2SMatthew G. Knepley         for (nc = 0; nc < numCells; ++nc) {
23779566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(subpointMap, cells[nc], 2));
2378007baee2SMatthew G. Knepley         }
23799566063dSJacob Faibussowitsch         PetscCall(DMPlexRestoreJoin(dm, faceSize, closure, &numCells, &cells));
2380efa14ee0SMatthew G Knepley       }
23819566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
2382efa14ee0SMatthew G Knepley     }
23839566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2384efa14ee0SMatthew G Knepley   }
2385efa14ee0SMatthew G Knepley   if (subvertexIS) {
23869566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(subvertexIS, &subvertices));
2387efa14ee0SMatthew G Knepley   }
23889566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
23899566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
2390efa14ee0SMatthew G Knepley   PetscFunctionReturn(0);
2391efa14ee0SMatthew G Knepley }
2392efa14ee0SMatthew G Knepley 
2393158acfadSMatthew G. Knepley static PetscErrorCode DMPlexMarkSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DMLabel subpointMap, DM subdm)
2394efa14ee0SMatthew G Knepley {
239534b4c39eSMatthew G. Knepley   IS               subvertexIS = NULL;
2396efa14ee0SMatthew G Knepley   const PetscInt  *subvertices;
2397412e9a14SMatthew G. Knepley   PetscInt        *pStart, *pEnd;
2398efa14ee0SMatthew G Knepley   PetscInt         dim, d, numSubVerticesInitial = 0, v;
2399efa14ee0SMatthew G Knepley 
2400efa14ee0SMatthew G Knepley   PetscFunctionBegin;
24019566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
24029566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(dim+1, &pStart, dim+1, &pEnd));
2403efa14ee0SMatthew G Knepley   for (d = 0; d <= dim; ++d) {
24049566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSimplexOrBoxCells(dm, dim-d, &pStart[d], &pEnd[d]));
2405efa14ee0SMatthew G Knepley   }
2406efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
240734b4c39eSMatthew G. Knepley   if (vertexLabel) {
24089566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(vertexLabel, value, &subvertexIS));
2409efa14ee0SMatthew G Knepley     if (subvertexIS) {
24109566063dSJacob Faibussowitsch       PetscCall(ISGetSize(subvertexIS, &numSubVerticesInitial));
24119566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subvertexIS, &subvertices));
2412efa14ee0SMatthew G Knepley     }
241334b4c39eSMatthew G. Knepley   }
2414efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2415efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
24160298fd71SBarry Smith     PetscInt      *star   = NULL;
2417efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numFaces = 0, f;
2418efa14ee0SMatthew G Knepley 
24199566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2420efa14ee0SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
2421efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2422158acfadSMatthew G. Knepley       PetscInt       faceLoc;
2423158acfadSMatthew G. Knepley 
2424158acfadSMatthew G. Knepley       if ((point >= pStart[dim-1]) && (point < pEnd[dim-1])) {
2425158acfadSMatthew G. Knepley         if (markedFaces) {
24269566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &faceLoc));
2427158acfadSMatthew G. Knepley           if (faceLoc < 0) continue;
2428158acfadSMatthew G. Knepley         }
2429158acfadSMatthew G. Knepley         star[numFaces++] = point;
2430158acfadSMatthew G. Knepley       }
2431efa14ee0SMatthew G Knepley     }
2432efa14ee0SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
2433efa14ee0SMatthew G Knepley       const PetscInt face    = star[f];
24340298fd71SBarry Smith       PetscInt      *closure = NULL;
2435efa14ee0SMatthew G Knepley       PetscInt       closureSize, c;
2436efa14ee0SMatthew G Knepley       PetscInt       faceLoc;
2437efa14ee0SMatthew G Knepley 
24389566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(subpointMap, face, &faceLoc));
2439efa14ee0SMatthew G Knepley       if (faceLoc == dim-1) continue;
244063a3b9bcSJacob Faibussowitsch       PetscCheck(faceLoc < 0,PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %" PetscInt_FMT " has dimension %" PetscInt_FMT " in the surface label", face, faceLoc);
24419566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure));
2442efa14ee0SMatthew G Knepley       for (c = 0; c < closureSize*2; c += 2) {
2443efa14ee0SMatthew G Knepley         const PetscInt point = closure[c];
2444efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2445efa14ee0SMatthew G Knepley 
2446efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
24479566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(vertexLabel, point, &vertexLoc));
2448830e53efSMatthew G. Knepley           if (vertexLoc != value) break;
2449efa14ee0SMatthew G Knepley         }
2450efa14ee0SMatthew G Knepley       }
2451efa14ee0SMatthew G Knepley       if (c == closureSize*2) {
2452efa14ee0SMatthew G Knepley         const PetscInt *support;
2453efa14ee0SMatthew G Knepley         PetscInt        supportSize, s;
2454efa14ee0SMatthew G Knepley 
2455efa14ee0SMatthew G Knepley         for (c = 0; c < closureSize*2; c += 2) {
2456efa14ee0SMatthew G Knepley           const PetscInt point = closure[c];
2457efa14ee0SMatthew G Knepley 
2458efa14ee0SMatthew G Knepley           for (d = 0; d < dim; ++d) {
2459efa14ee0SMatthew G Knepley             if ((point >= pStart[d]) && (point < pEnd[d])) {
24609566063dSJacob Faibussowitsch               PetscCall(DMLabelSetValue(subpointMap, point, d));
2461efa14ee0SMatthew G Knepley               break;
2462efa14ee0SMatthew G Knepley             }
2463efa14ee0SMatthew G Knepley           }
2464efa14ee0SMatthew G Knepley         }
24659566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, face, &supportSize));
24669566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, face, &support));
2467efa14ee0SMatthew G Knepley         for (s = 0; s < supportSize; ++s) {
24689566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(subpointMap, support[s], dim));
2469efa14ee0SMatthew G Knepley         }
2470efa14ee0SMatthew G Knepley       }
24719566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure));
2472efa14ee0SMatthew G Knepley     }
24739566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2474efa14ee0SMatthew G Knepley   }
24759566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subvertices));
24769566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
24779566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
2478efa14ee0SMatthew G Knepley   PetscFunctionReturn(0);
2479efa14ee0SMatthew G Knepley }
2480efa14ee0SMatthew G Knepley 
248127c04023SMatthew 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)
2482766ab985SMatthew G. Knepley {
248327c04023SMatthew G. Knepley   DMLabel         label = NULL;
2484766ab985SMatthew G. Knepley   const PetscInt *cone;
24859fc93327SToby Isaac   PetscInt        dim, cMax, cEnd, c, subc = 0, p, coneSize = -1;
2486766ab985SMatthew G. Knepley 
2487812bfc34SJed Brown   PetscFunctionBegin;
2488c0ed958bSJed Brown   *numFaces = 0;
2489c0ed958bSJed Brown   *nFV = 0;
24909566063dSJacob Faibussowitsch   if (labelname) PetscCall(DMGetLabel(dm, labelname, &label));
2491fed694aaSMatthew G. Knepley   *subCells = NULL;
24929566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
24939566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd));
2494766ab985SMatthew G. Knepley   if (cMax < 0) PetscFunctionReturn(0);
249527c04023SMatthew G. Knepley   if (label) {
249627c04023SMatthew G. Knepley     for (c = cMax; c < cEnd; ++c) {
249727c04023SMatthew G. Knepley       PetscInt val;
249827c04023SMatthew G. Knepley 
24999566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
250027c04023SMatthew G. Knepley       if (val == value) {
250127c04023SMatthew G. Knepley         ++(*numFaces);
25029566063dSJacob Faibussowitsch         PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
250327c04023SMatthew G. Knepley       }
250427c04023SMatthew G. Knepley     }
250527c04023SMatthew G. Knepley   } else {
2506766ab985SMatthew G. Knepley     *numFaces = cEnd - cMax;
25079566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cMax, &coneSize));
250827c04023SMatthew G. Knepley   }
25099566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(*numFaces *2, subCells));
25109fc93327SToby Isaac   if (!(*numFaces)) PetscFunctionReturn(0);
25119fc93327SToby Isaac   *nFV = hasLagrange ? coneSize/3 : coneSize/2;
2512766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2513766ab985SMatthew G. Knepley     const PetscInt *cells;
2514766ab985SMatthew G. Knepley     PetscInt        numCells;
2515766ab985SMatthew G. Knepley 
251627c04023SMatthew G. Knepley     if (label) {
251727c04023SMatthew G. Knepley       PetscInt val;
251827c04023SMatthew G. Knepley 
25199566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
252027c04023SMatthew G. Knepley       if (val != value) continue;
252127c04023SMatthew G. Knepley     }
25229566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, c, &cone));
2523766ab985SMatthew G. Knepley     for (p = 0; p < *nFV; ++p) {
25249566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(subpointMap, cone[p], 0));
2525766ab985SMatthew G. Knepley     }
2526766ab985SMatthew G. Knepley     /* Negative face */
25279566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, *nFV, cone, &numCells, &cells));
252827234c99SMatthew G. Knepley     /* Not true in parallel
252908401ef6SPierre Jolivet     PetscCheck(numCells == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
2530766ab985SMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
25319566063dSJacob Faibussowitsch       PetscCall(DMLabelSetValue(subpointMap, cells[p], 2));
253227234c99SMatthew G. Knepley       (*subCells)[subc++] = cells[p];
2533766ab985SMatthew G. Knepley     }
25349566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, *nFV, cone, &numCells, &cells));
2535766ab985SMatthew G. Knepley     /* Positive face is not included */
2536766ab985SMatthew G. Knepley   }
2537766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
2538766ab985SMatthew G. Knepley }
2539766ab985SMatthew G. Knepley 
25403982b651SMatthew G. Knepley static PetscErrorCode DMPlexMarkCohesiveSubmesh_Interpolated(DM dm, DMLabel label, PetscInt value, DMLabel subpointMap, DM subdm)
2541766ab985SMatthew G. Knepley {
2542766ab985SMatthew G. Knepley   PetscInt      *pStart, *pEnd;
2543766ab985SMatthew G. Knepley   PetscInt       dim, cMax, cEnd, c, d;
2544766ab985SMatthew G. Knepley 
2545812bfc34SJed Brown   PetscFunctionBegin;
25469566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
25479566063dSJacob Faibussowitsch   PetscCall(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd));
2548766ab985SMatthew G. Knepley   if (cMax < 0) PetscFunctionReturn(0);
25499566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(dim+1,&pStart,dim+1,&pEnd));
25509566063dSJacob Faibussowitsch   for (d = 0; d <= dim; ++d) PetscCall(DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]));
2551766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2552766ab985SMatthew G. Knepley     const PetscInt *cone;
2553766ab985SMatthew G. Knepley     PetscInt       *closure = NULL;
2554b3154360SMatthew G. Knepley     PetscInt        fconeSize, coneSize, closureSize, cl, val;
2555766ab985SMatthew G. Knepley 
255627c04023SMatthew G. Knepley     if (label) {
25579566063dSJacob Faibussowitsch       PetscCall(DMLabelGetValue(label, c, &val));
255827c04023SMatthew G. Knepley       if (val != value) continue;
255927c04023SMatthew G. Knepley     }
25609566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, c, &coneSize));
25619566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, c, &cone));
25629566063dSJacob Faibussowitsch     PetscCall(DMPlexGetConeSize(dm, cone[0], &fconeSize));
25631dca8a05SBarry Smith     PetscCheck(coneSize == (fconeSize ? fconeSize : 1) + 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells");
2564b3154360SMatthew G. Knepley     /* Negative face */
25659566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure));
2566766ab985SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
2567766ab985SMatthew G. Knepley       const PetscInt point = closure[cl];
2568766ab985SMatthew G. Knepley 
2569766ab985SMatthew G. Knepley       for (d = 0; d <= dim; ++d) {
2570766ab985SMatthew G. Knepley         if ((point >= pStart[d]) && (point < pEnd[d])) {
25719566063dSJacob Faibussowitsch           PetscCall(DMLabelSetValue(subpointMap, point, d));
2572766ab985SMatthew G. Knepley           break;
2573766ab985SMatthew G. Knepley         }
2574766ab985SMatthew G. Knepley       }
2575766ab985SMatthew G. Knepley     }
25769566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure));
2577766ab985SMatthew G. Knepley     /* Cells -- positive face is not included */
2578766ab985SMatthew G. Knepley     for (cl = 0; cl < 1; ++cl) {
2579766ab985SMatthew G. Knepley       const PetscInt *support;
2580766ab985SMatthew G. Knepley       PetscInt        supportSize, s;
2581766ab985SMatthew G. Knepley 
25829566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupportSize(dm, cone[cl], &supportSize));
258308401ef6SPierre Jolivet       /* PetscCheck(supportSize == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive faces should separate two cells"); */
25849566063dSJacob Faibussowitsch       PetscCall(DMPlexGetSupport(dm, cone[cl], &support));
2585766ab985SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
25869566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(subpointMap, support[s], dim));
2587766ab985SMatthew G. Knepley       }
2588766ab985SMatthew G. Knepley     }
2589766ab985SMatthew G. Knepley   }
25909566063dSJacob Faibussowitsch   PetscCall(PetscFree2(pStart, pEnd));
2591766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
2592766ab985SMatthew G. Knepley }
2593766ab985SMatthew G. Knepley 
2594c08575a3SMatthew G. Knepley static PetscErrorCode DMPlexGetFaceOrientation(DM dm, PetscInt cell, PetscInt numCorners, PetscInt indices[], PetscInt oppositeVertex, PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2595e6ccafaeSMatthew G Knepley {
259682f516ccSBarry Smith   MPI_Comm       comm;
2597e6ccafaeSMatthew G Knepley   PetscBool      posOrient = PETSC_FALSE;
2598e6ccafaeSMatthew G Knepley   const PetscInt debug     = 0;
2599e6ccafaeSMatthew G Knepley   PetscInt       cellDim, faceSize, f;
2600e6ccafaeSMatthew G Knepley 
260182f516ccSBarry Smith   PetscFunctionBegin;
26029566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm,&comm));
26039566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &cellDim));
260463a3b9bcSJacob Faibussowitsch   if (debug) PetscCall(PetscPrintf(comm, "cellDim: %" PetscInt_FMT " numCorners: %" PetscInt_FMT "\n", cellDim, numCorners));
2605e6ccafaeSMatthew G Knepley 
2606ddeab2a6SMatthew G. Knepley   if (cellDim == 1 && numCorners == 2) {
2607ddeab2a6SMatthew G. Knepley     /* Triangle */
2608e6ccafaeSMatthew G Knepley     faceSize  = numCorners-1;
2609e6ccafaeSMatthew G Knepley     posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2610ddeab2a6SMatthew G. Knepley   } else if (cellDim == 2 && numCorners == 3) {
2611ddeab2a6SMatthew G. Knepley     /* Triangle */
2612ddeab2a6SMatthew G. Knepley     faceSize  = numCorners-1;
2613ddeab2a6SMatthew G. Knepley     posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2614ddeab2a6SMatthew G. Knepley   } else if (cellDim == 3 && numCorners == 4) {
2615ddeab2a6SMatthew G. Knepley     /* Tetrahedron */
2616ddeab2a6SMatthew G. Knepley     faceSize  = numCorners-1;
2617ddeab2a6SMatthew G. Knepley     posOrient = (oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2618e6ccafaeSMatthew G Knepley   } else if (cellDim == 1 && numCorners == 3) {
2619e6ccafaeSMatthew G Knepley     /* Quadratic line */
2620e6ccafaeSMatthew G Knepley     faceSize  = 1;
2621e6ccafaeSMatthew G Knepley     posOrient = PETSC_TRUE;
2622e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 4) {
2623e6ccafaeSMatthew G Knepley     /* Quads */
2624e6ccafaeSMatthew G Knepley     faceSize = 2;
2625e6ccafaeSMatthew G Knepley     if ((indices[1] > indices[0]) && (indices[1] - indices[0] == 1)) {
2626e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2627e6ccafaeSMatthew G Knepley     } else if ((indices[0] == 3) && (indices[1] == 0)) {
2628e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2629e6ccafaeSMatthew G Knepley     } else {
2630e6ccafaeSMatthew G Knepley       if (((indices[0] > indices[1]) && (indices[0] - indices[1] == 1)) || ((indices[0] == 0) && (indices[1] == 3))) {
2631e6ccafaeSMatthew G Knepley         posOrient = PETSC_FALSE;
2632e6ccafaeSMatthew G Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossedge");
2633e6ccafaeSMatthew G Knepley     }
2634e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 6) {
2635e6ccafaeSMatthew G Knepley     /* Quadratic triangle (I hate this) */
2636e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2637e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTri = 3;
2638e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2639e6ccafaeSMatthew G Knepley     PetscBool      found                    = PETSC_FALSE;
2640e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTriSorted[9] = {
2641e6ccafaeSMatthew G Knepley       0, 3,  4, /* bottom */
2642e6ccafaeSMatthew G Knepley       1, 4,  5, /* right */
2643e6ccafaeSMatthew G Knepley       2, 3,  5, /* left */
2644e6ccafaeSMatthew G Knepley     };
2645e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTri[9] = {
2646e6ccafaeSMatthew G Knepley       0, 3,  4, /* bottom */
2647e6ccafaeSMatthew G Knepley       1, 4,  5, /* right */
2648e6ccafaeSMatthew G Knepley       2, 5,  3, /* left */
2649e6ccafaeSMatthew G Knepley     };
2650e6ccafaeSMatthew G Knepley 
2651e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTri; ++i) sortedIndices[i] = indices[i];
26529566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeTri, sortedIndices));
2653e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 3; ++iFace) {
2654e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeTri;
2655e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2656e6ccafaeSMatthew G Knepley 
2657e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesTriSorted[ii+0]) &&
2658e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesTriSorted[ii+1])) {
2659e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTri; ++fVertex) {
2660e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTri; ++cVertex) {
2661e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTri[ii+fVertex]) {
2662e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2663e6ccafaeSMatthew G Knepley               break;
2664e6ccafaeSMatthew G Knepley             }
2665e6ccafaeSMatthew G Knepley           }
2666e6ccafaeSMatthew G Knepley         }
2667e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2668e6ccafaeSMatthew G Knepley         break;
2669e6ccafaeSMatthew G Knepley       }
2670e6ccafaeSMatthew G Knepley     }
267128b400f6SJacob Faibussowitsch     PetscCheck(found,comm, PETSC_ERR_ARG_WRONG, "Invalid tri crossface");
2672e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2673e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2674e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 9) {
2675e6ccafaeSMatthew G Knepley     /* Quadratic quad (I hate this) */
2676e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2677e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuad = 3;
2678e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2679e6ccafaeSMatthew G Knepley     PetscBool      found                      = PETSC_FALSE;
2680e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadSorted[12] = {
2681e6ccafaeSMatthew G Knepley       0, 1,  4, /* bottom */
2682e6ccafaeSMatthew G Knepley       1, 2,  5, /* right */
2683e6ccafaeSMatthew G Knepley       2, 3,  6, /* top */
2684e6ccafaeSMatthew G Knepley       0, 3,  7, /* left */
2685e6ccafaeSMatthew G Knepley     };
2686e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuad[12] = {
2687e6ccafaeSMatthew G Knepley       0, 1,  4, /* bottom */
2688e6ccafaeSMatthew G Knepley       1, 2,  5, /* right */
2689e6ccafaeSMatthew G Knepley       2, 3,  6, /* top */
2690e6ccafaeSMatthew G Knepley       3, 0,  7, /* left */
2691e6ccafaeSMatthew G Knepley     };
2692e6ccafaeSMatthew G Knepley 
2693e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuad; ++i) sortedIndices[i] = indices[i];
26949566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeQuad, sortedIndices));
2695e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2696e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeQuad;
2697e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2698e6ccafaeSMatthew G Knepley 
2699e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesQuadSorted[ii+0]) &&
2700e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesQuadSorted[ii+1])) {
2701e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuad; ++fVertex) {
2702e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuad; ++cVertex) {
2703e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuad[ii+fVertex]) {
2704e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2705e6ccafaeSMatthew G Knepley               break;
2706e6ccafaeSMatthew G Knepley             }
2707e6ccafaeSMatthew G Knepley           }
2708e6ccafaeSMatthew G Knepley         }
2709e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2710e6ccafaeSMatthew G Knepley         break;
2711e6ccafaeSMatthew G Knepley       }
2712e6ccafaeSMatthew G Knepley     }
271328b400f6SJacob Faibussowitsch     PetscCheck(found,comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossface");
2714e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2715e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2716e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 8) {
2717e6ccafaeSMatthew G Knepley     /* Hexes
2718e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2719e6ccafaeSMatthew G Knepley        pointing up at the second.
2720e6ccafaeSMatthew G Knepley 
2721e6ccafaeSMatthew G Knepley           7---6
2722e6ccafaeSMatthew G Knepley          /|  /|
2723e6ccafaeSMatthew G Knepley         4---5 |
2724ddeab2a6SMatthew G. Knepley         | 1-|-2
2725e6ccafaeSMatthew G Knepley         |/  |/
2726ddeab2a6SMatthew G. Knepley         0---3
2727e6ccafaeSMatthew G Knepley 
2728e6ccafaeSMatthew G Knepley         Faces are determined by the first 4 vertices (corners of faces) */
2729e6ccafaeSMatthew G Knepley     const PetscInt faceSizeHex = 4;
2730e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[4], i, iFace;
2731e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2732e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHexSorted[24] = {
2733e6ccafaeSMatthew G Knepley       0, 1, 2, 3,  /* bottom */
2734e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  /* top */
2735ddeab2a6SMatthew G. Knepley       0, 3, 4, 5,  /* front */
2736ddeab2a6SMatthew G. Knepley       2, 3, 5, 6,  /* right */
2737ddeab2a6SMatthew G. Knepley       1, 2, 6, 7,  /* back */
2738ddeab2a6SMatthew G. Knepley       0, 1, 4, 7,  /* left */
2739e6ccafaeSMatthew G Knepley     };
2740e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHex[24] = {
2741ddeab2a6SMatthew G. Knepley       1, 2, 3, 0,  /* bottom */
2742e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  /* top */
2743ddeab2a6SMatthew G. Knepley       0, 3, 5, 4,  /* front */
2744ddeab2a6SMatthew G. Knepley       3, 2, 6, 5,  /* right */
2745ddeab2a6SMatthew G. Knepley       2, 1, 7, 6,  /* back */
2746ddeab2a6SMatthew G. Knepley       1, 0, 4, 7,  /* left */
2747e6ccafaeSMatthew G Knepley     };
2748e6ccafaeSMatthew G Knepley 
2749e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeHex; ++i) sortedIndices[i] = indices[i];
27509566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeHex, sortedIndices));
2751e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2752e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeHex;
2753e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2754e6ccafaeSMatthew G Knepley 
2755e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesHexSorted[ii+0]) &&
2756e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesHexSorted[ii+1]) &&
2757e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesHexSorted[ii+2]) &&
2758e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesHexSorted[ii+3])) {
2759e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeHex; ++fVertex) {
2760e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeHex; ++cVertex) {
2761e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesHex[ii+fVertex]) {
2762e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2763e6ccafaeSMatthew G Knepley               break;
2764e6ccafaeSMatthew G Knepley             }
2765e6ccafaeSMatthew G Knepley           }
2766e6ccafaeSMatthew G Knepley         }
2767e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2768e6ccafaeSMatthew G Knepley         break;
2769e6ccafaeSMatthew G Knepley       }
2770e6ccafaeSMatthew G Knepley     }
277128b400f6SJacob Faibussowitsch     PetscCheck(found,comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2772e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2773e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2774e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 10) {
2775e6ccafaeSMatthew G Knepley     /* Quadratic tet */
2776e6ccafaeSMatthew G Knepley     /* Faces are determined by the first 3 vertices (corners of faces) */
2777e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTet = 6;
2778e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[6], i, iFace;
2779e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2780e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTetSorted[24] = {
2781e6ccafaeSMatthew G Knepley       0, 1, 2,  6, 7, 8, /* bottom */
2782e6ccafaeSMatthew G Knepley       0, 3, 4,  6, 7, 9,  /* front */
2783e6ccafaeSMatthew G Knepley       1, 4, 5,  7, 8, 9,  /* right */
2784e6ccafaeSMatthew G Knepley       2, 3, 5,  6, 8, 9,  /* left */
2785e6ccafaeSMatthew G Knepley     };
2786e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTet[24] = {
2787e6ccafaeSMatthew G Knepley       0, 1, 2,  6, 7, 8, /* bottom */
2788e6ccafaeSMatthew G Knepley       0, 4, 3,  6, 7, 9,  /* front */
2789e6ccafaeSMatthew G Knepley       1, 5, 4,  7, 8, 9,  /* right */
2790e6ccafaeSMatthew G Knepley       2, 3, 5,  8, 6, 9,  /* left */
2791e6ccafaeSMatthew G Knepley     };
2792e6ccafaeSMatthew G Knepley 
2793e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTet; ++i) sortedIndices[i] = indices[i];
27949566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeTet, sortedIndices));
2795e6ccafaeSMatthew G Knepley     for (iFace=0; iFace < 4; ++iFace) {
2796e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeTet;
2797e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2798e6ccafaeSMatthew G Knepley 
2799e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesTetSorted[ii+0]) &&
2800e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesTetSorted[ii+1]) &&
2801e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesTetSorted[ii+2]) &&
2802e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesTetSorted[ii+3])) {
2803e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTet; ++fVertex) {
2804e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTet; ++cVertex) {
2805e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTet[ii+fVertex]) {
2806e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2807e6ccafaeSMatthew G Knepley               break;
2808e6ccafaeSMatthew G Knepley             }
2809e6ccafaeSMatthew G Knepley           }
2810e6ccafaeSMatthew G Knepley         }
2811e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2812e6ccafaeSMatthew G Knepley         break;
2813e6ccafaeSMatthew G Knepley       }
2814e6ccafaeSMatthew G Knepley     }
281528b400f6SJacob Faibussowitsch     PetscCheck(found,comm, PETSC_ERR_ARG_WRONG, "Invalid tet crossface");
2816e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2817e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2818e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 27) {
2819e6ccafaeSMatthew G Knepley     /* Quadratic hexes (I hate this)
2820e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2821e6ccafaeSMatthew G Knepley        pointing up at the second.
2822e6ccafaeSMatthew G Knepley 
2823e6ccafaeSMatthew G Knepley          7---6
2824e6ccafaeSMatthew G Knepley         /|  /|
2825e6ccafaeSMatthew G Knepley        4---5 |
2826e6ccafaeSMatthew G Knepley        | 3-|-2
2827e6ccafaeSMatthew G Knepley        |/  |/
2828e6ccafaeSMatthew G Knepley        0---1
2829e6ccafaeSMatthew G Knepley 
2830e6ccafaeSMatthew G Knepley        Faces are determined by the first 4 vertices (corners of faces) */
2831e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuadHex = 9;
2832e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[9], i, iFace;
2833e6ccafaeSMatthew G Knepley     PetscBool      found                         = PETSC_FALSE;
2834e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHexSorted[54] = {
2835e6ccafaeSMatthew G Knepley       0, 1, 2, 3,  8, 9, 10, 11,  24, /* bottom */
2836e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  12, 13, 14, 15,  25, /* top */
2837e6ccafaeSMatthew G Knepley       0, 1, 4, 5,  8, 12, 16, 17,  22, /* front */
2838e6ccafaeSMatthew G Knepley       1, 2, 5, 6,  9, 13, 17, 18,  21, /* right */
2839e6ccafaeSMatthew G Knepley       2, 3, 6, 7,  10, 14, 18, 19,  23, /* back */
2840e6ccafaeSMatthew G Knepley       0, 3, 4, 7,  11, 15, 16, 19,  20, /* left */
2841e6ccafaeSMatthew G Knepley     };
2842e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHex[54] = {
2843e6ccafaeSMatthew G Knepley       3, 2, 1, 0,  10, 9, 8, 11,  24, /* bottom */
2844e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  12, 13, 14, 15,  25, /* top */
2845e6ccafaeSMatthew G Knepley       0, 1, 5, 4,  8, 17, 12, 16,  22, /* front */
2846e6ccafaeSMatthew G Knepley       1, 2, 6, 5,  9, 18, 13, 17,  21, /* right */
2847e6ccafaeSMatthew G Knepley       2, 3, 7, 6,  10, 19, 14, 18,  23, /* back */
2848e6ccafaeSMatthew G Knepley       3, 0, 4, 7,  11, 16, 15, 19,  20 /* left */
2849e6ccafaeSMatthew G Knepley     };
2850e6ccafaeSMatthew G Knepley 
2851e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuadHex; ++i) sortedIndices[i] = indices[i];
28529566063dSJacob Faibussowitsch     PetscCall(PetscSortInt(faceSizeQuadHex, sortedIndices));
2853e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2854e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeQuadHex;
2855e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2856e6ccafaeSMatthew G Knepley 
2857e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesQuadHexSorted[ii+0]) &&
2858e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesQuadHexSorted[ii+1]) &&
2859e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesQuadHexSorted[ii+2]) &&
2860e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesQuadHexSorted[ii+3])) {
2861e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuadHex; ++fVertex) {
2862e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuadHex; ++cVertex) {
2863e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuadHex[ii+fVertex]) {
2864e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2865e6ccafaeSMatthew G Knepley               break;
2866e6ccafaeSMatthew G Knepley             }
2867e6ccafaeSMatthew G Knepley           }
2868e6ccafaeSMatthew G Knepley         }
2869e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2870e6ccafaeSMatthew G Knepley         break;
2871e6ccafaeSMatthew G Knepley       }
2872e6ccafaeSMatthew G Knepley     }
287328b400f6SJacob Faibussowitsch     PetscCheck(found,comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2874e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2875e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2876e6ccafaeSMatthew G Knepley   } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Unknown cell type for faceOrientation().");
2877e6ccafaeSMatthew G Knepley   if (!posOrient) {
28789566063dSJacob Faibussowitsch     if (debug) PetscCall(PetscPrintf(comm, "  Reversing initial face orientation\n"));
2879e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[faceSize-1 - f];
2880e6ccafaeSMatthew G Knepley   } else {
28819566063dSJacob Faibussowitsch     if (debug) PetscCall(PetscPrintf(comm, "  Keeping initial face orientation\n"));
2882e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[f];
2883e6ccafaeSMatthew G Knepley   }
2884e6ccafaeSMatthew G Knepley   if (posOriented) *posOriented = posOrient;
2885e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2886e6ccafaeSMatthew G Knepley }
2887e6ccafaeSMatthew G Knepley 
2888c08575a3SMatthew G. Knepley /*@
2889c08575a3SMatthew G. Knepley   DMPlexGetOrientedFace - Given a cell and a face, as a set of vertices, return the oriented face, as a set of vertices,
2890c08575a3SMatthew G. Knepley   in faceVertices. The orientation is such that the face normal points out of the cell
2891c08575a3SMatthew G. Knepley 
2892c08575a3SMatthew G. Knepley   Not collective
2893c08575a3SMatthew G. Knepley 
2894c08575a3SMatthew G. Knepley   Input Parameters:
2895c08575a3SMatthew G. Knepley + dm           - The original mesh
2896c08575a3SMatthew G. Knepley . cell         - The cell mesh point
2897c08575a3SMatthew G. Knepley . faceSize     - The number of vertices on the face
2898c08575a3SMatthew G. Knepley . face         - The face vertices
2899c08575a3SMatthew G. Knepley . numCorners   - The number of vertices on the cell
2900c08575a3SMatthew G. Knepley . indices      - Local numbering of face vertices in cell cone
2901c08575a3SMatthew G. Knepley - origVertices - Original face vertices
2902c08575a3SMatthew G. Knepley 
2903d8d19677SJose E. Roman   Output Parameters:
2904c08575a3SMatthew G. Knepley + faceVertices - The face vertices properly oriented
2905c08575a3SMatthew G. Knepley - posOriented  - PETSC_TRUE if the face was oriented with outward normal
2906c08575a3SMatthew G. Knepley 
2907c08575a3SMatthew G. Knepley   Level: developer
2908c08575a3SMatthew G. Knepley 
2909db781477SPatrick Sanan .seealso: `DMPlexGetCone()`
2910c08575a3SMatthew G. Knepley @*/
2911e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexGetOrientedFace(DM dm, PetscInt cell, PetscInt faceSize, const PetscInt face[], PetscInt numCorners, PetscInt indices[], PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2912e6ccafaeSMatthew G Knepley {
29130298fd71SBarry Smith   const PetscInt *cone = NULL;
2914e6ccafaeSMatthew G Knepley   PetscInt        coneSize, v, f, v2;
2915e6ccafaeSMatthew G Knepley   PetscInt        oppositeVertex = -1;
2916e6ccafaeSMatthew G Knepley 
2917e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
29189566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(dm, cell, &coneSize));
29199566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCone(dm, cell, &cone));
2920e6ccafaeSMatthew G Knepley   for (v = 0, v2 = 0; v < coneSize; ++v) {
2921e6ccafaeSMatthew G Knepley     PetscBool found = PETSC_FALSE;
2922e6ccafaeSMatthew G Knepley 
2923e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) {
2924e6ccafaeSMatthew G Knepley       if (face[f] == cone[v]) {
2925e6ccafaeSMatthew G Knepley         found = PETSC_TRUE; break;
2926e6ccafaeSMatthew G Knepley       }
2927e6ccafaeSMatthew G Knepley     }
2928e6ccafaeSMatthew G Knepley     if (found) {
2929e6ccafaeSMatthew G Knepley       indices[v2]      = v;
2930e6ccafaeSMatthew G Knepley       origVertices[v2] = cone[v];
2931e6ccafaeSMatthew G Knepley       ++v2;
2932e6ccafaeSMatthew G Knepley     } else {
2933e6ccafaeSMatthew G Knepley       oppositeVertex = v;
2934e6ccafaeSMatthew G Knepley     }
2935e6ccafaeSMatthew G Knepley   }
29369566063dSJacob Faibussowitsch   PetscCall(DMPlexGetFaceOrientation(dm, cell, numCorners, indices, oppositeVertex, origVertices, faceVertices, posOriented));
2937e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2938e6ccafaeSMatthew G Knepley }
2939e6ccafaeSMatthew G Knepley 
2940e6ccafaeSMatthew G Knepley /*
2941cd0c2139SMatthew G Knepley   DMPlexInsertFace_Internal - Puts a face into the mesh
2942e6ccafaeSMatthew G Knepley 
2943e6ccafaeSMatthew G Knepley   Not collective
2944e6ccafaeSMatthew G Knepley 
2945e6ccafaeSMatthew G Knepley   Input Parameters:
2946e6ccafaeSMatthew G Knepley   + dm              - The DMPlex
2947e6ccafaeSMatthew G Knepley   . numFaceVertex   - The number of vertices in the face
2948e6ccafaeSMatthew G Knepley   . faceVertices    - The vertices in the face for dm
2949e6ccafaeSMatthew G Knepley   . subfaceVertices - The vertices in the face for subdm
2950e6ccafaeSMatthew G Knepley   . numCorners      - The number of vertices in the cell
2951e6ccafaeSMatthew G Knepley   . cell            - A cell in dm containing the face
2952e6ccafaeSMatthew G Knepley   . subcell         - A cell in subdm containing the face
2953e6ccafaeSMatthew G Knepley   . firstFace       - First face in the mesh
2954e6ccafaeSMatthew G Knepley   - newFacePoint    - Next face in the mesh
2955e6ccafaeSMatthew G Knepley 
2956e6ccafaeSMatthew G Knepley   Output Parameters:
2957e6ccafaeSMatthew G Knepley   . newFacePoint - Contains next face point number on input, updated on output
2958e6ccafaeSMatthew G Knepley 
2959e6ccafaeSMatthew G Knepley   Level: developer
2960e6ccafaeSMatthew G Knepley */
2961cd0c2139SMatthew 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)
2962e6ccafaeSMatthew G Knepley {
296382f516ccSBarry Smith   MPI_Comm        comm;
2964e6ccafaeSMatthew G Knepley   DM_Plex        *submesh = (DM_Plex*) subdm->data;
2965e6ccafaeSMatthew G Knepley   const PetscInt *faces;
2966e6ccafaeSMatthew G Knepley   PetscInt        numFaces, coneSize;
2967e6ccafaeSMatthew G Knepley 
2968e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
29699566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm,&comm));
29709566063dSJacob Faibussowitsch   PetscCall(DMPlexGetConeSize(subdm, subcell, &coneSize));
297163a3b9bcSJacob Faibussowitsch   PetscCheck(coneSize == 1,comm, PETSC_ERR_ARG_OUTOFRANGE, "Cone size of cell %" PetscInt_FMT " is %" PetscInt_FMT " != 1", cell, coneSize);
2972e6ccafaeSMatthew G Knepley #if 0
2973e6ccafaeSMatthew G Knepley   /* Cannot use this because support() has not been constructed yet */
29749566063dSJacob Faibussowitsch   PetscCall(DMPlexGetJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces));
2975e6ccafaeSMatthew G Knepley #else
2976e6ccafaeSMatthew G Knepley   {
2977e6ccafaeSMatthew G Knepley     PetscInt f;
2978e6ccafaeSMatthew G Knepley 
2979e6ccafaeSMatthew G Knepley     numFaces = 0;
29809566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(subdm, 1, MPIU_INT, (void **) &faces));
2981e6ccafaeSMatthew G Knepley     for (f = firstFace; f < *newFacePoint; ++f) {
2982e6ccafaeSMatthew G Knepley       PetscInt dof, off, d;
2983e6ccafaeSMatthew G Knepley 
29849566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(submesh->coneSection, f, &dof));
29859566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(submesh->coneSection, f, &off));
2986e6ccafaeSMatthew G Knepley       /* Yes, I know this is quadratic, but I expect the sizes to be <5 */
2987e6ccafaeSMatthew G Knepley       for (d = 0; d < dof; ++d) {
2988e6ccafaeSMatthew G Knepley         const PetscInt p = submesh->cones[off+d];
2989e6ccafaeSMatthew G Knepley         PetscInt       v;
2990e6ccafaeSMatthew G Knepley 
2991e6ccafaeSMatthew G Knepley         for (v = 0; v < numFaceVertices; ++v) {
2992e6ccafaeSMatthew G Knepley           if (subfaceVertices[v] == p) break;
2993e6ccafaeSMatthew G Knepley         }
2994e6ccafaeSMatthew G Knepley         if (v == numFaceVertices) break;
2995e6ccafaeSMatthew G Knepley       }
2996e6ccafaeSMatthew G Knepley       if (d == dof) {
2997e6ccafaeSMatthew G Knepley         numFaces               = 1;
2998e6ccafaeSMatthew G Knepley         ((PetscInt*) faces)[0] = f;
2999e6ccafaeSMatthew G Knepley       }
3000e6ccafaeSMatthew G Knepley     }
3001e6ccafaeSMatthew G Knepley   }
3002e6ccafaeSMatthew G Knepley #endif
300363a3b9bcSJacob Faibussowitsch   PetscCheck(numFaces <= 1,comm, PETSC_ERR_ARG_WRONG, "Vertex set had %" PetscInt_FMT " faces, not one", numFaces);
3004f7d195e4SLawrence Mitchell   if (numFaces == 1) {
3005e6ccafaeSMatthew G Knepley     /* Add the other cell neighbor for this face */
30069566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, faces));
3007e6ccafaeSMatthew G Knepley   } else {
3008e6ccafaeSMatthew G Knepley     PetscInt *indices, *origVertices, *orientedVertices, *orientedSubVertices, v, ov;
3009e6ccafaeSMatthew G Knepley     PetscBool posOriented;
3010e6ccafaeSMatthew G Knepley 
30119566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices));
3012e6ccafaeSMatthew G Knepley     origVertices        = &orientedVertices[numFaceVertices];
3013e6ccafaeSMatthew G Knepley     indices             = &orientedVertices[numFaceVertices*2];
3014e6ccafaeSMatthew G Knepley     orientedSubVertices = &orientedVertices[numFaceVertices*3];
30159566063dSJacob Faibussowitsch     PetscCall(DMPlexGetOrientedFace(dm, cell, numFaceVertices, faceVertices, numCorners, indices, origVertices, orientedVertices, &posOriented));
3016e6ccafaeSMatthew G Knepley     /* TODO: I know that routine should return a permutation, not the indices */
3017e6ccafaeSMatthew G Knepley     for (v = 0; v < numFaceVertices; ++v) {
3018e6ccafaeSMatthew G Knepley       const PetscInt vertex = faceVertices[v], subvertex = subfaceVertices[v];
3019e6ccafaeSMatthew G Knepley       for (ov = 0; ov < numFaceVertices; ++ov) {
3020e6ccafaeSMatthew G Knepley         if (orientedVertices[ov] == vertex) {
3021e6ccafaeSMatthew G Knepley           orientedSubVertices[ov] = subvertex;
3022e6ccafaeSMatthew G Knepley           break;
3023e6ccafaeSMatthew G Knepley         }
3024e6ccafaeSMatthew G Knepley       }
302563a3b9bcSJacob Faibussowitsch       PetscCheck(ov != numFaceVertices,comm, PETSC_ERR_PLIB, "Could not find face vertex %" PetscInt_FMT " in orientated set", vertex);
3026e6ccafaeSMatthew G Knepley     }
30279566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, *newFacePoint, orientedSubVertices));
30289566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, newFacePoint));
30299566063dSJacob Faibussowitsch     PetscCall(DMRestoreWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices));
3030e6ccafaeSMatthew G Knepley     ++(*newFacePoint);
3031e6ccafaeSMatthew G Knepley   }
3032ef07cca7SMatthew G. Knepley #if 0
30339566063dSJacob Faibussowitsch   PetscCall(DMPlexRestoreJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces));
3034ef07cca7SMatthew G. Knepley #else
30359566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, 1, MPIU_INT, (void **) &faces));
3036ef07cca7SMatthew G. Knepley #endif
3037e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3038e6ccafaeSMatthew G Knepley }
3039e6ccafaeSMatthew G Knepley 
304053156dfcSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm)
3041e6ccafaeSMatthew G Knepley {
304282f516ccSBarry Smith   MPI_Comm        comm;
304353156dfcSMatthew G. Knepley   DMLabel         subpointMap;
3044efa14ee0SMatthew G Knepley   IS              subvertexIS,  subcellIS;
3045efa14ee0SMatthew G Knepley   const PetscInt *subVertices, *subCells;
3046efa14ee0SMatthew G Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells;
3047fed694aaSMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces = 0, firstSubFace, newFacePoint, nFV = 0;
3048efa14ee0SMatthew G Knepley   PetscInt        vStart, vEnd, c, f;
3049e6ccafaeSMatthew G Knepley 
3050e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
30519566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm,&comm));
3052efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
30539566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
30549566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
30559566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&subpointMap));
30569566063dSJacob Faibussowitsch   if (vertexLabel) PetscCall(DMPlexMarkSubmesh_Uninterpolated(dm, vertexLabel, value, subpointMap, &numSubFaces, &nFV, subdm));
3057efa14ee0SMatthew G Knepley   /* Setup chart */
30589566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 0, &numSubVertices));
30599566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 2, &numSubCells));
30609566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices));
30619566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, 1));
3062e6ccafaeSMatthew G Knepley   /* Set cone sizes */
3063e6ccafaeSMatthew G Knepley   firstSubVertex = numSubCells;
3064efa14ee0SMatthew G Knepley   firstSubFace   = numSubCells+numSubVertices;
3065e6ccafaeSMatthew G Knepley   newFacePoint   = firstSubFace;
30669566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 0, &subvertexIS));
30679566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISGetIndices(subvertexIS, &subVertices));
30689566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 2, &subcellIS));
30699566063dSJacob Faibussowitsch   if (subcellIS) PetscCall(ISGetIndices(subcellIS, &subCells));
3070e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
30719566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeSize(subdm, c, 1));
3072e6ccafaeSMatthew G Knepley   }
3073e6ccafaeSMatthew G Knepley   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
30749566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeSize(subdm, f, nFV));
3075e6ccafaeSMatthew G Knepley   }
30769566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3077e6ccafaeSMatthew G Knepley   /* Create face cones */
30789566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
30799566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
30809566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface));
3081e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
3082e6ccafaeSMatthew G Knepley     const PetscInt cell    = subCells[c];
3083efa14ee0SMatthew G Knepley     const PetscInt subcell = c;
30840298fd71SBarry Smith     PetscInt      *closure = NULL;
3085efa14ee0SMatthew G Knepley     PetscInt       closureSize, cl, numCorners = 0, faceSize = 0;
3086e6ccafaeSMatthew G Knepley 
30879566063dSJacob Faibussowitsch     PetscCall(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
3088efa14ee0SMatthew G Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
3089efa14ee0SMatthew G Knepley       const PetscInt point = closure[cl];
3090e6ccafaeSMatthew G Knepley       PetscInt       subVertex;
3091e6ccafaeSMatthew G Knepley 
3092efa14ee0SMatthew G Knepley       if ((point >= vStart) && (point < vEnd)) {
3093efa14ee0SMatthew G Knepley         ++numCorners;
30949566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(point, numSubVertices, subVertices, &subVertex));
3095efa14ee0SMatthew G Knepley         if (subVertex >= 0) {
3096efa14ee0SMatthew G Knepley           closure[faceSize] = point;
309765560c7fSMatthew G Knepley           subface[faceSize] = firstSubVertex+subVertex;
3098e6ccafaeSMatthew G Knepley           ++faceSize;
3099e6ccafaeSMatthew G Knepley         }
3100e6ccafaeSMatthew G Knepley       }
3101e6ccafaeSMatthew G Knepley     }
310263a3b9bcSJacob Faibussowitsch     PetscCheck(faceSize <= nFV,comm, PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %" PetscInt_FMT " of an element on the surface", faceSize);
3103efa14ee0SMatthew G Knepley     if (faceSize == nFV) {
31049566063dSJacob Faibussowitsch       PetscCall(DMPlexInsertFace_Internal(dm, subdm, faceSize, closure, subface, numCorners, cell, subcell, firstSubFace, &newFacePoint));
3105e6ccafaeSMatthew G Knepley     }
31069566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
3107e6ccafaeSMatthew G Knepley   }
31089566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface));
31099566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
31109566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3111e6ccafaeSMatthew G Knepley   /* Build coordinates */
3112efa14ee0SMatthew G Knepley   {
3113efa14ee0SMatthew G Knepley     PetscSection coordSection, subCoordSection;
3114efa14ee0SMatthew G Knepley     Vec          coordinates, subCoordinates;
3115efa14ee0SMatthew G Knepley     PetscScalar *coords, *subCoords;
3116285d324eSMatthew G. Knepley     PetscInt     numComp, coordSize, v;
311724640c55SToby Isaac     const char  *name;
3118efa14ee0SMatthew G Knepley 
31199566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
31209566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
31219566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
31229566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
31239566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
31249566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
31259566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices));
3126efa14ee0SMatthew G Knepley     for (v = 0; v < numSubVertices; ++v) {
3127efa14ee0SMatthew G Knepley       const PetscInt vertex    = subVertices[v];
3128efa14ee0SMatthew G Knepley       const PetscInt subvertex = firstSubVertex+v;
3129efa14ee0SMatthew G Knepley       PetscInt       dof;
3130efa14ee0SMatthew G Knepley 
31319566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
31329566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
31339566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3134e6ccafaeSMatthew G Knepley     }
31359566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
31369566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
31379566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
31389566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates,&name));
31399566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates,name));
31409566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
31419566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates,VECSTANDARD));
3142830e53efSMatthew G. Knepley     if (coordSize) {
31439566063dSJacob Faibussowitsch       PetscCall(VecGetArray(coordinates,    &coords));
31449566063dSJacob Faibussowitsch       PetscCall(VecGetArray(subCoordinates, &subCoords));
3145efa14ee0SMatthew G Knepley       for (v = 0; v < numSubVertices; ++v) {
3146efa14ee0SMatthew G Knepley         const PetscInt vertex    = subVertices[v];
3147efa14ee0SMatthew G Knepley         const PetscInt subvertex = firstSubVertex+v;
3148efa14ee0SMatthew G Knepley         PetscInt       dof, off, sdof, soff, d;
3149e6ccafaeSMatthew G Knepley 
31509566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
31519566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
31529566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
31539566063dSJacob Faibussowitsch         PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
315463a3b9bcSJacob Faibussowitsch         PetscCheck(dof == sdof,comm, PETSC_ERR_PLIB, "Coordinate dimension %" PetscInt_FMT " on subvertex %" PetscInt_FMT ", vertex %" PetscInt_FMT " should be %" PetscInt_FMT, sdof, subvertex, vertex, dof);
3155e6ccafaeSMatthew G Knepley         for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3156e6ccafaeSMatthew G Knepley       }
31579566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(coordinates,    &coords));
31589566063dSJacob Faibussowitsch       PetscCall(VecRestoreArray(subCoordinates, &subCoords));
31593b399e24SMatthew G. Knepley     }
31609566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
31619566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3162e6ccafaeSMatthew G Knepley   }
3163efa14ee0SMatthew G Knepley   /* Cleanup */
31649566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subVertices));
31659566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
31669566063dSJacob Faibussowitsch   if (subcellIS) PetscCall(ISRestoreIndices(subcellIS, &subCells));
31679566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subcellIS));
3168e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3169e6ccafaeSMatthew G Knepley }
3170e6ccafaeSMatthew G Knepley 
3171d0609cedSBarry Smith /* TODO: Fix this to properly propogate up error conditions it may find */
31729fbee547SJacob Faibussowitsch static inline PetscInt DMPlexFilterPoint_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[])
31733982b651SMatthew G. Knepley {
31743982b651SMatthew G. Knepley   PetscInt       subPoint;
31753982b651SMatthew G. Knepley   PetscErrorCode ierr;
31763982b651SMatthew G. Knepley 
3177d0609cedSBarry Smith   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint); if (ierr) return -1;
31783982b651SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint+subPoint;
31793982b651SMatthew G. Knepley }
31803982b651SMatthew G. Knepley 
3181212103e5SMatthew Knepley static PetscErrorCode DMPlexFilterLabels_Internal(DM dm, const PetscInt numSubPoints[], const PetscInt *subpoints[], const PetscInt firstSubPoint[], DM subdm)
3182212103e5SMatthew Knepley {
3183212103e5SMatthew Knepley   PetscInt       Nl, l, d;
3184212103e5SMatthew Knepley 
3185212103e5SMatthew Knepley   PetscFunctionBegin;
31869566063dSJacob Faibussowitsch   PetscCall(DMGetNumLabels(dm, &Nl));
3187212103e5SMatthew Knepley   for (l = 0; l < Nl; ++l) {
3188212103e5SMatthew Knepley     DMLabel         label, newlabel;
3189212103e5SMatthew Knepley     const char     *lname;
3190d56405f8SMatthew G. Knepley     PetscBool       isDepth, isDim, isCelltype, isVTK;
3191212103e5SMatthew Knepley     IS              valueIS;
3192212103e5SMatthew Knepley     const PetscInt *values;
3193212103e5SMatthew Knepley     PetscInt        Nv, v;
3194212103e5SMatthew Knepley 
31959566063dSJacob Faibussowitsch     PetscCall(DMGetLabelName(dm, l, &lname));
31969566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
31979566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "dim", &isDim));
31989566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "celltype", &isCelltype));
31999566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(lname, "vtk", &isVTK));
3200d56405f8SMatthew G. Knepley     if (isDepth || isDim || isCelltype || isVTK) continue;
32019566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(subdm, lname));
32029566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(dm, lname, &label));
32039566063dSJacob Faibussowitsch     PetscCall(DMGetLabel(subdm, lname, &newlabel));
32049566063dSJacob Faibussowitsch     PetscCall(DMLabelGetDefaultValue(label, &v));
32059566063dSJacob Faibussowitsch     PetscCall(DMLabelSetDefaultValue(newlabel, v));
32069566063dSJacob Faibussowitsch     PetscCall(DMLabelGetValueIS(label, &valueIS));
32079566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(valueIS, &Nv));
32089566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(valueIS, &values));
3209212103e5SMatthew Knepley     for (v = 0; v < Nv; ++v) {
3210212103e5SMatthew Knepley       IS              pointIS;
3211212103e5SMatthew Knepley       const PetscInt *points;
3212212103e5SMatthew Knepley       PetscInt        Np, p;
3213212103e5SMatthew Knepley 
32149566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumIS(label, values[v], &pointIS));
32159566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &Np));
32169566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
3217212103e5SMatthew Knepley       for (p = 0; p < Np; ++p) {
3218212103e5SMatthew Knepley         const PetscInt point = points[p];
3219212103e5SMatthew Knepley         PetscInt       subp;
3220212103e5SMatthew Knepley 
32219566063dSJacob Faibussowitsch         PetscCall(DMPlexGetPointDepth(dm, point, &d));
3222212103e5SMatthew Knepley         subp = DMPlexFilterPoint_Internal(point, firstSubPoint[d], numSubPoints[d], subpoints[d]);
32239566063dSJacob Faibussowitsch         if (subp >= 0) PetscCall(DMLabelSetValue(newlabel, subp, values[v]));
3224212103e5SMatthew Knepley       }
32259566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(pointIS, &points));
32269566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&pointIS));
3227212103e5SMatthew Knepley     }
32289566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(valueIS, &values));
32299566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&valueIS));
3230212103e5SMatthew Knepley   }
3231212103e5SMatthew Knepley   PetscFunctionReturn(0);
3232212103e5SMatthew Knepley }
3233212103e5SMatthew Knepley 
3234158acfadSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmeshGeneric_Interpolated(DM dm, DMLabel label, PetscInt value, PetscBool markedFaces, PetscBool isCohesive, PetscInt cellHeight, DM subdm)
3235e6ccafaeSMatthew G Knepley {
323682f516ccSBarry Smith   MPI_Comm         comm;
323753156dfcSMatthew G. Knepley   DMLabel          subpointMap;
3238efa14ee0SMatthew G Knepley   IS              *subpointIS;
3239efa14ee0SMatthew G Knepley   const PetscInt **subpoints;
32403982b651SMatthew G. Knepley   PetscInt        *numSubPoints, *firstSubPoint, *coneNew, *orntNew;
3241412e9a14SMatthew G. Knepley   PetscInt         totSubPoints = 0, maxConeSize, dim, p, d, v;
32420d366550SMatthew G. Knepley   PetscMPIInt      rank;
3243e6ccafaeSMatthew G Knepley 
3244e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
32459566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm,&comm));
32469566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
3247efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
32489566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
32499566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
3250bec263e5SMatthew G. Knepley   if (cellHeight) {
32519566063dSJacob Faibussowitsch     if (isCohesive) PetscCall(DMPlexMarkCohesiveSubmesh_Interpolated(dm, label, value, subpointMap, subdm));
32529566063dSJacob Faibussowitsch     else            PetscCall(DMPlexMarkSubmesh_Interpolated(dm, label, value, markedFaces, subpointMap, subdm));
3253bec263e5SMatthew G. Knepley   } else {
3254bec263e5SMatthew G. Knepley     DMLabel         depth;
3255bec263e5SMatthew G. Knepley     IS              pointIS;
3256bec263e5SMatthew G. Knepley     const PetscInt *points;
3257b85c8bf9SMatthew G. Knepley     PetscInt        numPoints=0;
3258bec263e5SMatthew G. Knepley 
32599566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthLabel(dm, &depth));
32609566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(label, value, &pointIS));
3261b85c8bf9SMatthew G. Knepley     if (pointIS) {
32629566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(pointIS, &points));
32639566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(pointIS, &numPoints));
3264b85c8bf9SMatthew G. Knepley     }
3265bec263e5SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
3266bec263e5SMatthew G. Knepley       PetscInt *closure = NULL;
3267bec263e5SMatthew G. Knepley       PetscInt  closureSize, c, pdim;
3268bec263e5SMatthew G. Knepley 
32699566063dSJacob Faibussowitsch       PetscCall(DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure));
3270bec263e5SMatthew G. Knepley       for (c = 0; c < closureSize*2; c += 2) {
32719566063dSJacob Faibussowitsch         PetscCall(DMLabelGetValue(depth, closure[c], &pdim));
32729566063dSJacob Faibussowitsch         PetscCall(DMLabelSetValue(subpointMap, closure[c], pdim));
3273bec263e5SMatthew G. Knepley       }
32749566063dSJacob Faibussowitsch       PetscCall(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure));
3275bec263e5SMatthew G. Knepley     }
32769566063dSJacob Faibussowitsch     if (pointIS) PetscCall(ISRestoreIndices(pointIS, &points));
32779566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&pointIS));
3278bec263e5SMatthew G. Knepley   }
3279efa14ee0SMatthew G Knepley   /* Setup chart */
32809566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
32819566063dSJacob Faibussowitsch   PetscCall(PetscMalloc4(dim+1,&numSubPoints,dim+1,&firstSubPoint,dim+1,&subpointIS,dim+1,&subpoints));
3282e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
32839566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d]));
3284e6ccafaeSMatthew G Knepley     totSubPoints += numSubPoints[d];
3285e6ccafaeSMatthew G Knepley   }
32869566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, totSubPoints));
32879566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, cellHeight));
3288e6ccafaeSMatthew G Knepley   /* Set cone sizes */
3289e6ccafaeSMatthew G Knepley   firstSubPoint[dim] = 0;
3290e6ccafaeSMatthew G Knepley   firstSubPoint[0]   = firstSubPoint[dim] + numSubPoints[dim];
3291e6ccafaeSMatthew G Knepley   if (dim > 1) {firstSubPoint[dim-1] = firstSubPoint[0]     + numSubPoints[0];}
3292e6ccafaeSMatthew G Knepley   if (dim > 2) {firstSubPoint[dim-2] = firstSubPoint[dim-1] + numSubPoints[dim-1];}
3293e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
32949566063dSJacob Faibussowitsch     PetscCall(DMLabelGetStratumIS(subpointMap, d, &subpointIS[d]));
32959566063dSJacob Faibussowitsch     if (subpointIS[d]) PetscCall(ISGetIndices(subpointIS[d], &subpoints[d]));
3296e6ccafaeSMatthew G Knepley   }
3297412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
32989566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(subdm, "celltype"));
3299e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
3300e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3301e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3302e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
3303e6ccafaeSMatthew G Knepley       const PetscInt *cone;
330415100a53SVaclav Hapla       PetscInt        coneSize;
3305e6ccafaeSMatthew G Knepley 
33069566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, point, &coneSize));
3307bec263e5SMatthew G. Knepley       if (cellHeight && (d == dim)) {
330815100a53SVaclav Hapla         PetscInt coneSizeNew, c, val;
330915100a53SVaclav Hapla 
33109566063dSJacob Faibussowitsch         PetscCall(DMPlexGetCone(dm, point, &cone));
3311e6ccafaeSMatthew G Knepley         for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
33129566063dSJacob Faibussowitsch           PetscCall(DMLabelGetValue(subpointMap, cone[c], &val));
3313e6ccafaeSMatthew G Knepley           if (val >= 0) coneSizeNew++;
3314e6ccafaeSMatthew G Knepley         }
33159566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(subdm, subpoint, coneSizeNew));
33169566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCellType(subdm, subpoint, DM_POLYTOPE_FV_GHOST));
331715100a53SVaclav Hapla       } else {
331815100a53SVaclav Hapla         DMPolytopeType  ct;
331915100a53SVaclav Hapla 
332015100a53SVaclav Hapla         PetscCall(DMPlexSetConeSize(subdm, subpoint, coneSize));
332115100a53SVaclav Hapla         PetscCall(DMPlexGetCellType(dm, point, &ct));
332215100a53SVaclav Hapla         PetscCall(DMPlexSetCellType(subdm, subpoint, ct));
3323e6ccafaeSMatthew G Knepley       }
3324e6ccafaeSMatthew G Knepley     }
3325e6ccafaeSMatthew G Knepley   }
33269566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&subpointMap));
33279566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3328e6ccafaeSMatthew G Knepley   /* Set cones */
33299566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
33309566063dSJacob Faibussowitsch   PetscCall(PetscMalloc2(maxConeSize,&coneNew,maxConeSize,&orntNew));
3331e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
3332e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3333e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3334e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
33350e49e2e2SMatthew G. Knepley       const PetscInt *cone, *ornt;
33360d366550SMatthew G. Knepley       PetscInt        coneSize, subconeSize, coneSizeNew, c, subc, fornt = 0;
3337e6ccafaeSMatthew G Knepley 
33380d366550SMatthew G. Knepley       if (d == dim-1) {
33390d366550SMatthew G. Knepley         const PetscInt *support, *cone, *ornt;
33400d366550SMatthew G. Knepley         PetscInt        supportSize, coneSize, s, subc;
33410d366550SMatthew G. Knepley 
33429566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupport(dm, point, &support));
33439566063dSJacob Faibussowitsch         PetscCall(DMPlexGetSupportSize(dm, point, &supportSize));
33440d366550SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
3345412e9a14SMatthew G. Knepley           PetscBool isHybrid;
3346412e9a14SMatthew G. Knepley 
33479566063dSJacob Faibussowitsch           PetscCall(DMPlexCellIsHybrid_Internal(dm, support[s], &isHybrid));
3348412e9a14SMatthew G. Knepley           if (!isHybrid) continue;
33499566063dSJacob Faibussowitsch           PetscCall(PetscFindInt(support[s], numSubPoints[d+1], subpoints[d+1], &subc));
33500d366550SMatthew G. Knepley           if (subc >= 0) {
33510d366550SMatthew G. Knepley             const PetscInt ccell = subpoints[d+1][subc];
33520d366550SMatthew G. Knepley 
33539566063dSJacob Faibussowitsch             PetscCall(DMPlexGetCone(dm, ccell, &cone));
33549566063dSJacob Faibussowitsch             PetscCall(DMPlexGetConeSize(dm, ccell, &coneSize));
33559566063dSJacob Faibussowitsch             PetscCall(DMPlexGetConeOrientation(dm, ccell, &ornt));
33560d366550SMatthew G. Knepley             for (c = 0; c < coneSize; ++c) {
33570d366550SMatthew G. Knepley               if (cone[c] == point) {
33580d366550SMatthew G. Knepley                 fornt = ornt[c];
33590d366550SMatthew G. Knepley                 break;
33600d366550SMatthew G. Knepley               }
33610d366550SMatthew G. Knepley             }
33620d366550SMatthew G. Knepley             break;
33630d366550SMatthew G. Knepley           }
33640d366550SMatthew G. Knepley         }
33650d366550SMatthew G. Knepley       }
33669566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(dm, point, &coneSize));
33679566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeSize(subdm, subpoint, &subconeSize));
33689566063dSJacob Faibussowitsch       PetscCall(DMPlexGetCone(dm, point, &cone));
33699566063dSJacob Faibussowitsch       PetscCall(DMPlexGetConeOrientation(dm, point, &ornt));
3370e6ccafaeSMatthew G Knepley       for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
33719566063dSJacob Faibussowitsch         PetscCall(PetscFindInt(cone[c], numSubPoints[d-1], subpoints[d-1], &subc));
337201a2673eSMatthew G. Knepley         if (subc >= 0) {
337301a2673eSMatthew G. Knepley           coneNew[coneSizeNew] = firstSubPoint[d-1] + subc;
33743982b651SMatthew G. Knepley           orntNew[coneSizeNew] = ornt[c];
337501a2673eSMatthew G. Knepley           ++coneSizeNew;
337601a2673eSMatthew G. Knepley         }
3377e6ccafaeSMatthew G Knepley       }
337863a3b9bcSJacob Faibussowitsch       PetscCheck(coneSizeNew == subconeSize,comm, PETSC_ERR_PLIB, "Number of cone points located %" PetscInt_FMT " does not match subcone size %" PetscInt_FMT, coneSizeNew, subconeSize);
33799566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(subdm, subpoint, coneNew));
33809566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeOrientation(subdm, subpoint, orntNew));
33819566063dSJacob Faibussowitsch       if (fornt < 0) PetscCall(DMPlexOrientPoint(subdm, subpoint, fornt));
3382e6ccafaeSMatthew G Knepley     }
3383e6ccafaeSMatthew G Knepley   }
33849566063dSJacob Faibussowitsch   PetscCall(PetscFree2(coneNew,orntNew));
33859566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
33869566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3387e6ccafaeSMatthew G Knepley   /* Build coordinates */
3388e6ccafaeSMatthew G Knepley   {
3389e6ccafaeSMatthew G Knepley     PetscSection coordSection, subCoordSection;
3390e6ccafaeSMatthew G Knepley     Vec          coordinates, subCoordinates;
3391e6ccafaeSMatthew G Knepley     PetscScalar *coords, *subCoords;
3392c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize;
339324640c55SToby Isaac     const char  *name;
3394e6ccafaeSMatthew G Knepley 
33959566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDim(dm, &cdim));
33969566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
33979566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
33989566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
33999566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
34009566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
34019566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
34029566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubPoint[0], firstSubPoint[0]+numSubPoints[0]));
3403e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3404e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3405e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0]+v;
3406e6ccafaeSMatthew G Knepley       PetscInt       dof;
3407e6ccafaeSMatthew G Knepley 
34089566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
34099566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
34109566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3411e6ccafaeSMatthew G Knepley     }
34129566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
34139566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
34149566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
34159566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates,&name));
34169566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates,name));
34179566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
34189566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(subCoordinates, cdim));
34199566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates,VECSTANDARD));
34209566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates,    &coords));
34219566063dSJacob Faibussowitsch     PetscCall(VecGetArray(subCoordinates, &subCoords));
3422e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3423e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3424e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0]+v;
3425e6ccafaeSMatthew G Knepley       PetscInt dof, off, sdof, soff, d;
3426e6ccafaeSMatthew G Knepley 
34279566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
34289566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
34299566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
34309566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
343163a3b9bcSJacob Faibussowitsch       PetscCheck(dof == sdof,comm, PETSC_ERR_PLIB, "Coordinate dimension %" PetscInt_FMT " on subvertex %" PetscInt_FMT ", vertex %" PetscInt_FMT " should be %" PetscInt_FMT, sdof, subvertex, vertex, dof);
3432efa14ee0SMatthew G Knepley       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3433e6ccafaeSMatthew G Knepley     }
34349566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates,    &coords));
34359566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(subCoordinates, &subCoords));
34369566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
34379566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3438e6ccafaeSMatthew G Knepley   }
34393982b651SMatthew G. Knepley   /* Build SF: We need this complexity because subpoints might not be selected on the owning process */
34403982b651SMatthew G. Knepley   {
34413982b651SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
34423982b651SMatthew G. Knepley     IS                 subpIS;
34433982b651SMatthew G. Knepley     const PetscSFNode *remotePoints;
34443982b651SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
34453982b651SMatthew G. Knepley     const PetscInt    *localPoints, *subpoints;
34463982b651SMatthew G. Knepley     PetscInt          *slocalPoints;
34473982b651SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubpoints = 0, numSubroots, numSubleaves = 0, l, sl, ll, pStart, pEnd, p;
34483982b651SMatthew G. Knepley     PetscMPIInt        rank;
34493982b651SMatthew G. Knepley 
34509566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank));
34519566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm, &sfPoint));
34529566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(subdm, &sfPointSub));
34539566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
34549566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(subdm, NULL, &numSubroots));
34559566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(subdm, &subpIS));
34563982b651SMatthew G. Knepley     if (subpIS) {
34579566063dSJacob Faibussowitsch       PetscCall(ISGetIndices(subpIS, &subpoints));
34589566063dSJacob Faibussowitsch       PetscCall(ISGetLocalSize(subpIS, &numSubpoints));
34593982b651SMatthew G. Knepley     }
34609566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints));
34613982b651SMatthew G. Knepley     if (numRoots >= 0) {
34629566063dSJacob Faibussowitsch       PetscCall(PetscMalloc2(pEnd-pStart,&newLocalPoints,numRoots,&newOwners));
34633982b651SMatthew G. Knepley       for (p = 0; p < pEnd-pStart; ++p) {
34643982b651SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
34653982b651SMatthew G. Knepley         newLocalPoints[p].index = -2;
34663982b651SMatthew G. Knepley       }
34673982b651SMatthew G. Knepley       /* Set subleaves */
34683982b651SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
34693982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
34703982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(point, 0, numSubpoints, subpoints);
34713982b651SMatthew G. Knepley 
34723982b651SMatthew G. Knepley         if (subpoint < 0) continue;
34733982b651SMatthew G. Knepley         newLocalPoints[point-pStart].rank  = rank;
34743982b651SMatthew G. Knepley         newLocalPoints[point-pStart].index = subpoint;
34753982b651SMatthew G. Knepley         ++numSubleaves;
34763982b651SMatthew G. Knepley       }
34773982b651SMatthew G. Knepley       /* Must put in owned subpoints */
34783982b651SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
34793982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(p, 0, numSubpoints, subpoints);
34803982b651SMatthew G. Knepley 
34813982b651SMatthew G. Knepley         if (subpoint < 0) {
34823982b651SMatthew G. Knepley           newOwners[p-pStart].rank  = -3;
34833982b651SMatthew G. Knepley           newOwners[p-pStart].index = -3;
34843982b651SMatthew G. Knepley         } else {
34853982b651SMatthew G. Knepley           newOwners[p-pStart].rank  = rank;
34863982b651SMatthew G. Knepley           newOwners[p-pStart].index = subpoint;
34873982b651SMatthew G. Knepley         }
34883982b651SMatthew G. Knepley       }
34899566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
34909566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
34919566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints,MPI_REPLACE));
34929566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints,MPI_REPLACE));
34939566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubleaves, &slocalPoints));
34949566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubleaves, &sremotePoints));
34953982b651SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
34963982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
34973982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(point, 0, numSubpoints, subpoints);
34983982b651SMatthew G. Knepley 
34993982b651SMatthew G. Knepley         if (subpoint < 0) continue;
35003982b651SMatthew G. Knepley         if (newLocalPoints[point].rank == rank) {++ll; continue;}
35013982b651SMatthew G. Knepley         slocalPoints[sl]        = subpoint;
35023982b651SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
35033982b651SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
350463a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].rank  >= 0,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %" PetscInt_FMT, point);
350563a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].index >= 0,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %" PetscInt_FMT, point);
35063982b651SMatthew G. Knepley         ++sl;
35073982b651SMatthew G. Knepley       }
35081dca8a05SBarry Smith       PetscCheck(sl + ll == numSubleaves,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %" PetscInt_FMT " + %" PetscInt_FMT " != %" PetscInt_FMT, sl, ll, numSubleaves);
35099566063dSJacob Faibussowitsch       PetscCall(PetscFree2(newLocalPoints,newOwners));
35109566063dSJacob Faibussowitsch       PetscCall(PetscSFSetGraph(sfPointSub, numSubroots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER));
35113982b651SMatthew G. Knepley     }
35123982b651SMatthew G. Knepley     if (subpIS) {
35139566063dSJacob Faibussowitsch       PetscCall(ISRestoreIndices(subpIS, &subpoints));
35143982b651SMatthew G. Knepley     }
35153982b651SMatthew G. Knepley   }
3516212103e5SMatthew Knepley   /* Filter labels */
35179566063dSJacob Faibussowitsch   PetscCall(DMPlexFilterLabels_Internal(dm, numSubPoints, subpoints, firstSubPoint, subdm));
3518efa14ee0SMatthew G Knepley   /* Cleanup */
3519e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
35209566063dSJacob Faibussowitsch     if (subpointIS[d]) PetscCall(ISRestoreIndices(subpointIS[d], &subpoints[d]));
35219566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&subpointIS[d]));
3522e6ccafaeSMatthew G Knepley   }
35239566063dSJacob Faibussowitsch   PetscCall(PetscFree4(numSubPoints,firstSubPoint,subpointIS,subpoints));
3524e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3525e6ccafaeSMatthew G Knepley }
3526e6ccafaeSMatthew G Knepley 
3527158acfadSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM subdm)
35283982b651SMatthew G. Knepley {
35293982b651SMatthew G. Knepley   PetscFunctionBegin;
35309566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, vertexLabel, value, markedFaces, PETSC_FALSE, 1, subdm));
35313982b651SMatthew G. Knepley   PetscFunctionReturn(0);
35323982b651SMatthew G. Knepley }
35333982b651SMatthew G. Knepley 
3534d0fa310fSMatthew G. Knepley /*@
3535e6ccafaeSMatthew G Knepley   DMPlexCreateSubmesh - Extract a hypersurface from the mesh using vertices defined by a label
3536e6ccafaeSMatthew G Knepley 
3537e6ccafaeSMatthew G Knepley   Input Parameters:
3538e6ccafaeSMatthew G Knepley + dm           - The original mesh
3539158acfadSMatthew G. Knepley . vertexLabel  - The DMLabel marking points contained in the surface
3540158acfadSMatthew G. Knepley . value        - The label value to use
3541158acfadSMatthew G. Knepley - markedFaces  - PETSC_TRUE if surface faces are marked in addition to vertices, PETSC_FALSE if only vertices are marked
3542e6ccafaeSMatthew G Knepley 
3543e6ccafaeSMatthew G Knepley   Output Parameter:
3544e6ccafaeSMatthew G Knepley . subdm - The surface mesh
3545e6ccafaeSMatthew G Knepley 
3546e6ccafaeSMatthew G Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3547e6ccafaeSMatthew G Knepley 
3548e6ccafaeSMatthew G Knepley   Level: developer
3549e6ccafaeSMatthew G Knepley 
3550db781477SPatrick Sanan .seealso: `DMPlexGetSubpointMap()`, `DMGetLabel()`, `DMLabelSetValue()`
3551830e53efSMatthew G. Knepley @*/
3552158acfadSMatthew G. Knepley PetscErrorCode DMPlexCreateSubmesh(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM *subdm)
3553e6ccafaeSMatthew G Knepley {
3554827c4036SVaclav Hapla   DMPlexInterpolatedFlag interpolated;
3555827c4036SVaclav Hapla   PetscInt       dim, cdim;
3556e6ccafaeSMatthew G Knepley 
3557e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3558e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3559064a246eSJacob Faibussowitsch   PetscValidPointer(subdm, 5);
35609566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
35619566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
35629566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
35639566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*subdm, dim-1));
35649566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
35659566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*subdm, cdim));
35669566063dSJacob Faibussowitsch   PetscCall(DMPlexIsInterpolated(dm, &interpolated));
356708401ef6SPierre Jolivet   PetscCheck(interpolated != DMPLEX_INTERPOLATED_PARTIAL,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not for partially interpolated meshes");
3568827c4036SVaclav Hapla   if (interpolated) {
35699566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateSubmesh_Interpolated(dm, vertexLabel, value, markedFaces, *subdm));
3570e6ccafaeSMatthew G Knepley   } else {
35719566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateSubmesh_Uninterpolated(dm, vertexLabel, value, *subdm));
3572e6ccafaeSMatthew G Knepley   }
35735de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
3574e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3575e6ccafaeSMatthew G Knepley }
3576e6ccafaeSMatthew G Knepley 
357727c04023SMatthew G. Knepley static PetscErrorCode DMPlexCreateCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM subdm)
3578766ab985SMatthew G. Knepley {
3579766ab985SMatthew G. Knepley   MPI_Comm        comm;
3580766ab985SMatthew G. Knepley   DMLabel         subpointMap;
3581766ab985SMatthew G. Knepley   IS              subvertexIS;
3582766ab985SMatthew G. Knepley   const PetscInt *subVertices;
3583fed694aaSMatthew G. Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells, *subCells = NULL;
3584766ab985SMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces, firstSubFace, newFacePoint, nFV;
3585412e9a14SMatthew G. Knepley   PetscInt        c, f;
3586766ab985SMatthew G. Knepley 
3587766ab985SMatthew G. Knepley   PetscFunctionBegin;
35889566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
3589766ab985SMatthew G. Knepley   /* Create subpointMap which marks the submesh */
35909566063dSJacob Faibussowitsch   PetscCall(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
35919566063dSJacob Faibussowitsch   PetscCall(DMPlexSetSubpointMap(subdm, subpointMap));
35929566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&subpointMap));
35939566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, subpointMap, &numSubFaces, &nFV, &subCells, subdm));
3594766ab985SMatthew G. Knepley   /* Setup chart */
35959566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 0, &numSubVertices));
35969566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumSize(subpointMap, 2, &numSubCells));
35979566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices));
35989566063dSJacob Faibussowitsch   PetscCall(DMPlexSetVTKCellHeight(subdm, 1));
3599766ab985SMatthew G. Knepley   /* Set cone sizes */
3600766ab985SMatthew G. Knepley   firstSubVertex = numSubCells;
3601766ab985SMatthew G. Knepley   firstSubFace   = numSubCells+numSubVertices;
3602766ab985SMatthew G. Knepley   newFacePoint   = firstSubFace;
36039566063dSJacob Faibussowitsch   PetscCall(DMLabelGetStratumIS(subpointMap, 0, &subvertexIS));
36049566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISGetIndices(subvertexIS, &subVertices));
3605766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
36069566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeSize(subdm, c, 1));
3607766ab985SMatthew G. Knepley   }
3608766ab985SMatthew G. Knepley   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
36099566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeSize(subdm, f, nFV));
3610766ab985SMatthew G. Knepley   }
36119566063dSJacob Faibussowitsch   PetscCall(DMSetUp(subdm));
3612766ab985SMatthew G. Knepley   /* Create face cones */
36139566063dSJacob Faibussowitsch   PetscCall(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
36149566063dSJacob Faibussowitsch   PetscCall(DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface));
3615766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3616766ab985SMatthew G. Knepley     const PetscInt  cell    = subCells[c];
3617766ab985SMatthew G. Knepley     const PetscInt  subcell = c;
361887feddfdSMatthew G. Knepley     const PetscInt *cone, *cells;
3619412e9a14SMatthew G. Knepley     PetscBool       isHybrid;
362087feddfdSMatthew G. Knepley     PetscInt        numCells, subVertex, p, v;
3621766ab985SMatthew G. Knepley 
36229566063dSJacob Faibussowitsch     PetscCall(DMPlexCellIsHybrid_Internal(dm, cell, &isHybrid));
3623412e9a14SMatthew G. Knepley     if (!isHybrid) continue;
36249566063dSJacob Faibussowitsch     PetscCall(DMPlexGetCone(dm, cell, &cone));
362587feddfdSMatthew G. Knepley     for (v = 0; v < nFV; ++v) {
36269566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(cone[v], numSubVertices, subVertices, &subVertex));
362787feddfdSMatthew G. Knepley       subface[v] = firstSubVertex+subVertex;
362887feddfdSMatthew G. Knepley     }
36299566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, newFacePoint, subface));
36309566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(subdm, subcell, &newFacePoint));
36319566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, nFV, cone, &numCells, &cells));
363227234c99SMatthew G. Knepley     /* Not true in parallel
363308401ef6SPierre Jolivet     PetscCheck(numCells == 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
363487feddfdSMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
363587feddfdSMatthew G. Knepley       PetscInt  negsubcell;
3636412e9a14SMatthew G. Knepley       PetscBool isHybrid;
3637766ab985SMatthew G. Knepley 
36389566063dSJacob Faibussowitsch       PetscCall(DMPlexCellIsHybrid_Internal(dm, cells[p], &isHybrid));
3639412e9a14SMatthew G. Knepley       if (isHybrid) continue;
364087feddfdSMatthew G. Knepley       /* I know this is a crap search */
364187feddfdSMatthew G. Knepley       for (negsubcell = 0; negsubcell < numSubCells; ++negsubcell) {
364287feddfdSMatthew G. Knepley         if (subCells[negsubcell] == cells[p]) break;
3643766ab985SMatthew G. Knepley       }
364463a3b9bcSJacob Faibussowitsch       PetscCheck(negsubcell != numSubCells,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find negative face neighbor for cohesive cell %" PetscInt_FMT, cell);
36459566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(subdm, negsubcell, &newFacePoint));
3646766ab985SMatthew G. Knepley     }
36479566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, nFV, cone, &numCells, &cells));
364887feddfdSMatthew G. Knepley     ++newFacePoint;
3649766ab985SMatthew G. Knepley   }
36509566063dSJacob Faibussowitsch   PetscCall(DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface));
36519566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(subdm));
36529566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(subdm));
3653766ab985SMatthew G. Knepley   /* Build coordinates */
3654766ab985SMatthew G. Knepley   {
3655766ab985SMatthew G. Knepley     PetscSection coordSection, subCoordSection;
3656766ab985SMatthew G. Knepley     Vec          coordinates, subCoordinates;
3657766ab985SMatthew G. Knepley     PetscScalar *coords, *subCoords;
3658c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize, v;
365924640c55SToby Isaac     const char  *name;
3660766ab985SMatthew G. Knepley 
36619566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDim(dm, &cdim));
36629566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
36639566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
36649566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(subdm, &subCoordSection));
36659566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(subCoordSection, 1));
36669566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
36679566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
36689566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices));
3669766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3670766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3671766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex+v;
3672766ab985SMatthew G. Knepley       PetscInt       dof;
3673766ab985SMatthew G. Knepley 
36749566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
36759566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(subCoordSection, subvertex, dof));
36769566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3677766ab985SMatthew G. Knepley     }
36789566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(subCoordSection));
36799566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(subCoordSection, &coordSize));
36809566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &subCoordinates));
36819566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)coordinates,&name));
36829566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)subCoordinates,name));
36839566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
36849566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(subCoordinates, cdim));
36859566063dSJacob Faibussowitsch     PetscCall(VecSetType(subCoordinates,VECSTANDARD));
36869566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates,    &coords));
36879566063dSJacob Faibussowitsch     PetscCall(VecGetArray(subCoordinates, &subCoords));
3688766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3689766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3690766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex+v;
3691766ab985SMatthew G. Knepley       PetscInt       dof, off, sdof, soff, d;
3692766ab985SMatthew G. Knepley 
36939566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(coordSection, vertex, &dof));
36949566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, vertex, &off));
36959566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
36969566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
369763a3b9bcSJacob Faibussowitsch       PetscCheck(dof == sdof,comm, PETSC_ERR_PLIB, "Coordinate dimension %" PetscInt_FMT " on subvertex %" PetscInt_FMT ", vertex %" PetscInt_FMT " should be %" PetscInt_FMT, sdof, subvertex, vertex, dof);
3698766ab985SMatthew G. Knepley       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3699766ab985SMatthew G. Knepley     }
37009566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates,    &coords));
37019566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(subCoordinates, &subCoords));
37029566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(subdm, subCoordinates));
37039566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&subCoordinates));
3704766ab985SMatthew G. Knepley   }
3705aca35d17SMatthew G. Knepley   /* Build SF */
3706aca35d17SMatthew G. Knepley   CHKMEMQ;
3707aca35d17SMatthew G. Knepley   {
3708aca35d17SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
3709aca35d17SMatthew G. Knepley     const PetscSFNode *remotePoints;
3710bdcf2095SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
3711aca35d17SMatthew G. Knepley     const PetscInt    *localPoints;
3712bdcf2095SMatthew G. Knepley     PetscInt          *slocalPoints;
371349c26ae4SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubRoots = numSubCells+numSubFaces+numSubVertices, numSubLeaves = 0, l, sl, ll, pStart, pEnd, p, vStart, vEnd;
3714bdcf2095SMatthew G. Knepley     PetscMPIInt        rank;
3715aca35d17SMatthew G. Knepley 
37169566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank));
37179566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(dm, &sfPoint));
37189566063dSJacob Faibussowitsch     PetscCall(DMGetPointSF(subdm, &sfPointSub));
37199566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
37209566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
37219566063dSJacob Faibussowitsch     PetscCall(PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints));
3722aca35d17SMatthew G. Knepley     if (numRoots >= 0) {
3723aca35d17SMatthew G. Knepley       /* Only vertices should be shared */
37249566063dSJacob Faibussowitsch       PetscCall(PetscMalloc2(pEnd-pStart,&newLocalPoints,numRoots,&newOwners));
3725bdcf2095SMatthew G. Knepley       for (p = 0; p < pEnd-pStart; ++p) {
3726bdcf2095SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
3727bdcf2095SMatthew G. Knepley         newLocalPoints[p].index = -2;
3728bdcf2095SMatthew G. Knepley       }
37299e0823b2SMatthew G. Knepley       /* Set subleaves */
3730aca35d17SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
3731aca35d17SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3732bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3733aca35d17SMatthew G. Knepley 
373463a3b9bcSJacob Faibussowitsch         PetscCheck(!(point < vStart) || !(point >= vEnd),PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Should not be mapping anything but vertices, %" PetscInt_FMT, point);
3735bdcf2095SMatthew G. Knepley         if (subPoint < 0) continue;
3736bdcf2095SMatthew G. Knepley         newLocalPoints[point-pStart].rank  = rank;
3737bdcf2095SMatthew G. Knepley         newLocalPoints[point-pStart].index = subPoint;
3738bdcf2095SMatthew G. Knepley         ++numSubLeaves;
3739aca35d17SMatthew G. Knepley       }
37409e0823b2SMatthew G. Knepley       /* Must put in owned subpoints */
37419e0823b2SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
37429e0823b2SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(p, firstSubVertex, numSubVertices, subVertices);
37439e0823b2SMatthew G. Knepley 
37449e0823b2SMatthew G. Knepley         if (subPoint < 0) {
37459e0823b2SMatthew G. Knepley           newOwners[p-pStart].rank  = -3;
37469e0823b2SMatthew G. Knepley           newOwners[p-pStart].index = -3;
37479e0823b2SMatthew G. Knepley         } else {
37489e0823b2SMatthew G. Knepley           newOwners[p-pStart].rank  = rank;
37499e0823b2SMatthew G. Knepley           newOwners[p-pStart].index = subPoint;
37509e0823b2SMatthew G. Knepley         }
3751bdcf2095SMatthew G. Knepley       }
37529566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
37539566063dSJacob Faibussowitsch       PetscCall(PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
37549566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints,MPI_REPLACE));
37559566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints,MPI_REPLACE));
37569566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubLeaves,    &slocalPoints));
37579566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numSubLeaves, &sremotePoints));
375849c26ae4SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
3759bdcf2095SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3760bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3761aca35d17SMatthew G. Knepley 
3762aca35d17SMatthew G. Knepley         if (subPoint < 0) continue;
376349c26ae4SMatthew G. Knepley         if (newLocalPoints[point].rank == rank) {++ll; continue;}
3764aca35d17SMatthew G. Knepley         slocalPoints[sl]        = subPoint;
3765bdcf2095SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
3766bdcf2095SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
376763a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].rank  >= 0,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %" PetscInt_FMT, point);
376863a3b9bcSJacob Faibussowitsch         PetscCheck(sremotePoints[sl].index >= 0,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %" PetscInt_FMT, point);
3769aca35d17SMatthew G. Knepley         ++sl;
3770aca35d17SMatthew G. Knepley       }
37719566063dSJacob Faibussowitsch       PetscCall(PetscFree2(newLocalPoints,newOwners));
37721dca8a05SBarry Smith       PetscCheck(sl + ll == numSubLeaves,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %" PetscInt_FMT " + %" PetscInt_FMT " != %" PetscInt_FMT, sl, ll, numSubLeaves);
37739566063dSJacob Faibussowitsch       PetscCall(PetscSFSetGraph(sfPointSub, numSubRoots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER));
3774aca35d17SMatthew G. Knepley     }
3775aca35d17SMatthew G. Knepley   }
3776aca35d17SMatthew G. Knepley   CHKMEMQ;
3777766ab985SMatthew G. Knepley   /* Cleanup */
37789566063dSJacob Faibussowitsch   if (subvertexIS) PetscCall(ISRestoreIndices(subvertexIS, &subVertices));
37799566063dSJacob Faibussowitsch   PetscCall(ISDestroy(&subvertexIS));
37809566063dSJacob Faibussowitsch   PetscCall(PetscFree(subCells));
3781766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
3782766ab985SMatthew G. Knepley }
3783766ab985SMatthew G. Knepley 
37843982b651SMatthew G. Knepley static PetscErrorCode DMPlexCreateCohesiveSubmesh_Interpolated(DM dm, const char labelname[], PetscInt value, DM subdm)
3785766ab985SMatthew G. Knepley {
37863982b651SMatthew G. Knepley   DMLabel        label = NULL;
3787766ab985SMatthew G. Knepley 
3788766ab985SMatthew G. Knepley   PetscFunctionBegin;
37899566063dSJacob Faibussowitsch   if (labelname) PetscCall(DMGetLabel(dm, labelname, &label));
37909566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, label, value, PETSC_FALSE, PETSC_TRUE, 1, subdm));
3791766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
3792766ab985SMatthew G. Knepley }
3793766ab985SMatthew G. Knepley 
3794c08575a3SMatthew G. Knepley /*@C
37957d2fefb6SMatthew 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.
3796766ab985SMatthew G. Knepley 
3797766ab985SMatthew G. Knepley   Input Parameters:
3798766ab985SMatthew G. Knepley + dm          - The original mesh
379927c04023SMatthew G. Knepley . hasLagrange - The mesh has Lagrange unknowns in the cohesive cells
38007afc1a8bSJed Brown . label       - A label name, or NULL
380127c04023SMatthew G. Knepley - value  - A label value
3802766ab985SMatthew G. Knepley 
3803766ab985SMatthew G. Knepley   Output Parameter:
3804766ab985SMatthew G. Knepley . subdm - The surface mesh
3805766ab985SMatthew G. Knepley 
3806766ab985SMatthew G. Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3807766ab985SMatthew G. Knepley 
3808766ab985SMatthew G. Knepley   Level: developer
3809766ab985SMatthew G. Knepley 
3810db781477SPatrick Sanan .seealso: `DMPlexGetSubpointMap()`, `DMPlexCreateSubmesh()`
3811c08575a3SMatthew G. Knepley @*/
381227c04023SMatthew G. Knepley PetscErrorCode DMPlexCreateCohesiveSubmesh(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM *subdm)
3813766ab985SMatthew G. Knepley {
3814c0e8cf5fSMatthew G. Knepley   PetscInt       dim, cdim, depth;
3815766ab985SMatthew G. Knepley 
3816766ab985SMatthew G. Knepley   PetscFunctionBegin;
3817766ab985SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
381827c04023SMatthew G. Knepley   PetscValidPointer(subdm, 5);
38199566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
38209566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
38219566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
38229566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
38239566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*subdm, dim-1));
38249566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &cdim));
38259566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*subdm, cdim));
3826766ab985SMatthew G. Knepley   if (depth == dim) {
38279566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCohesiveSubmesh_Interpolated(dm, label, value, *subdm));
3828766ab985SMatthew G. Knepley   } else {
38299566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, *subdm));
3830e6ccafaeSMatthew G Knepley   }
38315de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
3832e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3833e6ccafaeSMatthew G Knepley }
3834e6ccafaeSMatthew G Knepley 
3835bec263e5SMatthew G. Knepley /*@
3836bec263e5SMatthew G. Knepley   DMPlexFilter - Extract a subset of mesh cells defined by a label as a separate mesh
3837bec263e5SMatthew G. Knepley 
3838bec263e5SMatthew G. Knepley   Input Parameters:
3839bec263e5SMatthew G. Knepley + dm        - The original mesh
3840bec263e5SMatthew G. Knepley . cellLabel - The DMLabel marking cells contained in the new mesh
3841bec263e5SMatthew G. Knepley - value     - The label value to use
3842bec263e5SMatthew G. Knepley 
3843bec263e5SMatthew G. Knepley   Output Parameter:
3844bec263e5SMatthew G. Knepley . subdm - The new mesh
3845bec263e5SMatthew G. Knepley 
3846bec263e5SMatthew G. Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3847bec263e5SMatthew G. Knepley 
3848bec263e5SMatthew G. Knepley   Level: developer
3849bec263e5SMatthew G. Knepley 
3850db781477SPatrick Sanan .seealso: `DMPlexGetSubpointMap()`, `DMGetLabel()`, `DMLabelSetValue()`
3851bec263e5SMatthew G. Knepley @*/
3852bec263e5SMatthew G. Knepley PetscErrorCode DMPlexFilter(DM dm, DMLabel cellLabel, PetscInt value, DM *subdm)
3853bec263e5SMatthew G. Knepley {
3854bec263e5SMatthew G. Knepley   PetscInt       dim;
3855bec263e5SMatthew G. Knepley 
3856bec263e5SMatthew G. Knepley   PetscFunctionBegin;
3857bec263e5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3858064a246eSJacob Faibussowitsch   PetscValidPointer(subdm, 4);
38599566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
38609566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject) dm), subdm));
38619566063dSJacob Faibussowitsch   PetscCall(DMSetType(*subdm, DMPLEX));
38629566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*subdm, dim));
3863bec263e5SMatthew G. Knepley   /* Extract submesh in place, could be empty on some procs, could have inconsistency if procs do not both extract a shared cell */
38649566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSubmeshGeneric_Interpolated(dm, cellLabel, value, PETSC_FALSE, PETSC_FALSE, 0, *subdm));
38655de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_TRUE, *subdm));
3866bec263e5SMatthew G. Knepley   PetscFunctionReturn(0);
3867bec263e5SMatthew G. Knepley }
3868bec263e5SMatthew G. Knepley 
386964beef6dSMatthew G. Knepley /*@
387064beef6dSMatthew G. Knepley   DMPlexGetSubpointMap - Returns a DMLabel with point dimension as values
387164beef6dSMatthew G. Knepley 
387264beef6dSMatthew G. Knepley   Input Parameter:
387364beef6dSMatthew G. Knepley . dm - The submesh DM
387464beef6dSMatthew G. Knepley 
387564beef6dSMatthew G. Knepley   Output Parameter:
387664beef6dSMatthew G. Knepley . subpointMap - The DMLabel of all the points from the original mesh in this submesh, or NULL if this is not a submesh
387764beef6dSMatthew G. Knepley 
387864beef6dSMatthew G. Knepley   Level: developer
387964beef6dSMatthew G. Knepley 
3880db781477SPatrick Sanan .seealso: `DMPlexCreateSubmesh()`, `DMPlexGetSubpointIS()`
388164beef6dSMatthew G. Knepley @*/
3882e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexGetSubpointMap(DM dm, DMLabel *subpointMap)
3883e6ccafaeSMatthew G Knepley {
3884e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3885e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3886e6ccafaeSMatthew G Knepley   PetscValidPointer(subpointMap, 2);
388765663942SMatthew G. Knepley   *subpointMap = ((DM_Plex*) dm->data)->subpointMap;
3888e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3889e6ccafaeSMatthew G Knepley }
3890e6ccafaeSMatthew G Knepley 
3891c08575a3SMatthew G. Knepley /*@
3892c08575a3SMatthew G. Knepley   DMPlexSetSubpointMap - Sets the DMLabel with point dimension as values
3893c08575a3SMatthew G. Knepley 
3894c08575a3SMatthew G. Knepley   Input Parameters:
3895c08575a3SMatthew G. Knepley + dm - The submesh DM
3896c08575a3SMatthew G. Knepley - subpointMap - The DMLabel of all the points from the original mesh in this submesh
3897c08575a3SMatthew G. Knepley 
3898c08575a3SMatthew G. Knepley   Note: Should normally not be called by the user, since it is set in DMPlexCreateSubmesh()
3899c08575a3SMatthew G. Knepley 
3900c08575a3SMatthew G. Knepley   Level: developer
3901c08575a3SMatthew G. Knepley 
3902db781477SPatrick Sanan .seealso: `DMPlexCreateSubmesh()`, `DMPlexGetSubpointIS()`
3903c08575a3SMatthew G. Knepley @*/
3904e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexSetSubpointMap(DM dm, DMLabel subpointMap)
3905e6ccafaeSMatthew G Knepley {
3906e6ccafaeSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
3907285d324eSMatthew G. Knepley   DMLabel        tmp;
3908e6ccafaeSMatthew G Knepley 
3909e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3910e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3911285d324eSMatthew G. Knepley   tmp  = mesh->subpointMap;
3912e6ccafaeSMatthew G Knepley   mesh->subpointMap = subpointMap;
39139566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) mesh->subpointMap));
39149566063dSJacob Faibussowitsch   PetscCall(DMLabelDestroy(&tmp));
3915e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3916e6ccafaeSMatthew G Knepley }
3917e6ccafaeSMatthew G Knepley 
391897d8846cSMatthew Knepley static PetscErrorCode DMPlexCreateSubpointIS_Internal(DM dm, IS *subpointIS)
391997d8846cSMatthew Knepley {
392097d8846cSMatthew Knepley   DMLabel        spmap;
392197d8846cSMatthew Knepley   PetscInt       depth, d;
392297d8846cSMatthew Knepley 
392397d8846cSMatthew Knepley   PetscFunctionBegin;
39249566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(dm, &spmap));
39259566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
392697d8846cSMatthew Knepley   if (spmap && depth >= 0) {
392797d8846cSMatthew Knepley     DM_Plex  *mesh = (DM_Plex *) dm->data;
392897d8846cSMatthew Knepley     PetscInt *points, *depths;
392997d8846cSMatthew Knepley     PetscInt  pStart, pEnd, p, off;
393097d8846cSMatthew Knepley 
39319566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
393263a3b9bcSJacob Faibussowitsch     PetscCheck(!pStart,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Submeshes must start the point numbering at 0, not %" PetscInt_FMT, pStart);
39339566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(pEnd, &points));
39349566063dSJacob Faibussowitsch     PetscCall(DMGetWorkArray(dm, depth+1, MPIU_INT, &depths));
393597d8846cSMatthew Knepley     depths[0] = depth;
393697d8846cSMatthew Knepley     depths[1] = 0;
393797d8846cSMatthew Knepley     for (d = 2; d <= depth; ++d) {depths[d] = depth+1 - d;}
393897d8846cSMatthew Knepley     for (d = 0, off = 0; d <= depth; ++d) {
393997d8846cSMatthew Knepley       const PetscInt dep = depths[d];
394097d8846cSMatthew Knepley       PetscInt       depStart, depEnd, n;
394197d8846cSMatthew Knepley 
39429566063dSJacob Faibussowitsch       PetscCall(DMPlexGetDepthStratum(dm, dep, &depStart, &depEnd));
39439566063dSJacob Faibussowitsch       PetscCall(DMLabelGetStratumSize(spmap, dep, &n));
394497d8846cSMatthew Knepley       if (((d < 2) && (depth > 1)) || (d == 1)) { /* Only check vertices and cells for now since the map is broken for others */
394563a3b9bcSJacob Faibussowitsch         PetscCheck(n == depEnd-depStart,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %" PetscInt_FMT " at depth %" PetscInt_FMT " should be %" PetscInt_FMT, n, dep, depEnd-depStart);
394697d8846cSMatthew Knepley       } else {
394797d8846cSMatthew Knepley         if (!n) {
394897d8846cSMatthew Knepley           if (d == 0) {
394997d8846cSMatthew Knepley             /* Missing cells */
395097d8846cSMatthew Knepley             for (p = 0; p < depEnd-depStart; ++p, ++off) points[off] = -1;
395197d8846cSMatthew Knepley           } else {
395297d8846cSMatthew Knepley             /* Missing faces */
395397d8846cSMatthew Knepley             for (p = 0; p < depEnd-depStart; ++p, ++off) points[off] = PETSC_MAX_INT;
395497d8846cSMatthew Knepley           }
395597d8846cSMatthew Knepley         }
395697d8846cSMatthew Knepley       }
395797d8846cSMatthew Knepley       if (n) {
395897d8846cSMatthew Knepley         IS              is;
395997d8846cSMatthew Knepley         const PetscInt *opoints;
396097d8846cSMatthew Knepley 
39619566063dSJacob Faibussowitsch         PetscCall(DMLabelGetStratumIS(spmap, dep, &is));
39629566063dSJacob Faibussowitsch         PetscCall(ISGetIndices(is, &opoints));
396397d8846cSMatthew Knepley         for (p = 0; p < n; ++p, ++off) points[off] = opoints[p];
39649566063dSJacob Faibussowitsch         PetscCall(ISRestoreIndices(is, &opoints));
39659566063dSJacob Faibussowitsch         PetscCall(ISDestroy(&is));
396697d8846cSMatthew Knepley       }
396797d8846cSMatthew Knepley     }
39689566063dSJacob Faibussowitsch     PetscCall(DMRestoreWorkArray(dm, depth+1, MPIU_INT, &depths));
396963a3b9bcSJacob Faibussowitsch     PetscCheck(off == pEnd,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %" PetscInt_FMT " should be %" PetscInt_FMT, off, pEnd);
39709566063dSJacob Faibussowitsch     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, pEnd, points, PETSC_OWN_POINTER, subpointIS));
39719566063dSJacob Faibussowitsch     PetscCall(PetscObjectStateGet((PetscObject) spmap, &mesh->subpointState));
397297d8846cSMatthew Knepley   }
397397d8846cSMatthew Knepley   PetscFunctionReturn(0);
397497d8846cSMatthew Knepley }
397597d8846cSMatthew Knepley 
397664beef6dSMatthew G. Knepley /*@
397797d8846cSMatthew Knepley   DMPlexGetSubpointIS - Returns an IS covering the entire subdm chart with the original points as data
3978e6ccafaeSMatthew G Knepley 
3979e6ccafaeSMatthew G Knepley   Input Parameter:
3980e6ccafaeSMatthew G Knepley . dm - The submesh DM
3981e6ccafaeSMatthew G Knepley 
3982e6ccafaeSMatthew G Knepley   Output Parameter:
39830298fd71SBarry Smith . subpointIS - The IS of all the points from the original mesh in this submesh, or NULL if this is not a submesh
3984e6ccafaeSMatthew G Knepley 
39853982b651SMatthew G. Knepley   Note: This IS is guaranteed to be sorted by the construction of the submesh
398664beef6dSMatthew G. Knepley 
398764beef6dSMatthew G. Knepley   Level: developer
398864beef6dSMatthew G. Knepley 
3989db781477SPatrick Sanan .seealso: `DMPlexCreateSubmesh()`, `DMPlexGetSubpointMap()`
399064beef6dSMatthew G. Knepley @*/
399197d8846cSMatthew Knepley PetscErrorCode DMPlexGetSubpointIS(DM dm, IS *subpointIS)
3992e6ccafaeSMatthew G Knepley {
399397d8846cSMatthew Knepley   DM_Plex         *mesh = (DM_Plex *) dm->data;
399497d8846cSMatthew Knepley   DMLabel          spmap;
399597d8846cSMatthew Knepley   PetscObjectState state;
3996e6ccafaeSMatthew G Knepley 
3997e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3998e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3999e6ccafaeSMatthew G Knepley   PetscValidPointer(subpointIS, 2);
40009566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(dm, &spmap));
40019566063dSJacob Faibussowitsch   PetscCall(PetscObjectStateGet((PetscObject) spmap, &state));
40029566063dSJacob Faibussowitsch   if (state != mesh->subpointState || !mesh->subpointIS) PetscCall(DMPlexCreateSubpointIS_Internal(dm, &mesh->subpointIS));
400397d8846cSMatthew Knepley   *subpointIS = mesh->subpointIS;
4004e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
4005e6ccafaeSMatthew G Knepley }
4006559a1558SMatthew G. Knepley 
4007559a1558SMatthew G. Knepley /*@
4008a6e0b375SMatthew G. Knepley   DMGetEnclosureRelation - Get the relationship between dmA and dmB
4009559a1558SMatthew G. Knepley 
4010559a1558SMatthew G. Knepley   Input Parameters:
4011a6e0b375SMatthew G. Knepley + dmA - The first DM
4012a6e0b375SMatthew G. Knepley - dmB - The second DM
4013559a1558SMatthew G. Knepley 
4014559a1558SMatthew G. Knepley   Output Parameter:
4015a6e0b375SMatthew G. Knepley . rel - The relation of dmA to dmB
4016559a1558SMatthew G. Knepley 
4017a6e0b375SMatthew G. Knepley   Level: intermediate
4018559a1558SMatthew G. Knepley 
4019db781477SPatrick Sanan .seealso: `DMGetEnclosurePoint()`
4020559a1558SMatthew G. Knepley @*/
4021a6e0b375SMatthew G. Knepley PetscErrorCode DMGetEnclosureRelation(DM dmA, DM dmB, DMEnclosureType *rel)
4022559a1558SMatthew G. Knepley {
4023a6e0b375SMatthew G. Knepley   DM             plexA, plexB, sdm;
4024559a1558SMatthew G. Knepley   DMLabel        spmap;
4025a6e0b375SMatthew G. Knepley   PetscInt       pStartA, pEndA, pStartB, pEndB, NpA, NpB;
4026559a1558SMatthew G. Knepley 
402744171101SMatthew G. Knepley   PetscFunctionBegin;
4028a6e0b375SMatthew G. Knepley   PetscValidPointer(rel, 3);
4029a6e0b375SMatthew G. Knepley   *rel = DM_ENC_NONE;
4030a6e0b375SMatthew G. Knepley   if (!dmA || !dmB) PetscFunctionReturn(0);
4031a6e0b375SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
4032064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
4033a6e0b375SMatthew G. Knepley   if (dmA == dmB) {*rel = DM_ENC_EQUALITY; PetscFunctionReturn(0);}
40349566063dSJacob Faibussowitsch   PetscCall(DMConvert(dmA, DMPLEX, &plexA));
40359566063dSJacob Faibussowitsch   PetscCall(DMConvert(dmB, DMPLEX, &plexB));
40369566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(plexA, &pStartA, &pEndA));
40379566063dSJacob Faibussowitsch   PetscCall(DMPlexGetChart(plexB, &pStartB, &pEndB));
4038a6e0b375SMatthew G. Knepley   /* Assumption 1: subDMs have smaller charts than the DMs that they originate from
4039a6e0b375SMatthew G. Knepley     - The degenerate case of a subdomain which includes all of the domain on some process can be treated as equality */
4040a6e0b375SMatthew G. Knepley   if ((pStartA == pStartB) && (pEndA == pEndB)) {
4041a6e0b375SMatthew G. Knepley     *rel = DM_ENC_EQUALITY;
4042a6e0b375SMatthew G. Knepley     goto end;
4043559a1558SMatthew G. Knepley   }
4044a6e0b375SMatthew G. Knepley   NpA = pEndA - pStartA;
4045a6e0b375SMatthew G. Knepley   NpB = pEndB - pStartB;
4046a6e0b375SMatthew G. Knepley   if (NpA == NpB) goto end;
4047a6e0b375SMatthew G. Knepley   sdm = NpA > NpB ? plexB : plexA; /* The other is the original, enclosing dm */
40489566063dSJacob Faibussowitsch   PetscCall(DMPlexGetSubpointMap(sdm, &spmap));
4049a6e0b375SMatthew G. Knepley   if (!spmap) goto end;
4050a6e0b375SMatthew G. Knepley   /* TODO Check the space mapped to by subpointMap is same size as dm */
4051a6e0b375SMatthew G. Knepley   if (NpA > NpB) {
4052a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUPERMESH;
4053a6e0b375SMatthew G. Knepley   } else {
4054a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUBMESH;
4055a6e0b375SMatthew G. Knepley   }
4056a6e0b375SMatthew G. Knepley   end:
40579566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plexA));
40589566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plexB));
4059559a1558SMatthew G. Knepley   PetscFunctionReturn(0);
4060559a1558SMatthew G. Knepley }
406144171101SMatthew G. Knepley 
406244171101SMatthew G. Knepley /*@
4063a6e0b375SMatthew G. Knepley   DMGetEnclosurePoint - Get the point pA in dmA which corresponds to the point pB in dmB
406444171101SMatthew G. Knepley 
406544171101SMatthew G. Knepley   Input Parameters:
4066a6e0b375SMatthew G. Knepley + dmA   - The first DM
4067a6e0b375SMatthew G. Knepley . dmB   - The second DM
4068a6e0b375SMatthew G. Knepley . etype - The type of enclosure relation that dmA has to dmB
4069a6e0b375SMatthew G. Knepley - pB    - A point of dmB
407044171101SMatthew G. Knepley 
407144171101SMatthew G. Knepley   Output Parameter:
4072a6e0b375SMatthew G. Knepley . pA    - The corresponding point of dmA
407344171101SMatthew G. Knepley 
4074a6e0b375SMatthew G. Knepley   Level: intermediate
407544171101SMatthew G. Knepley 
4076db781477SPatrick Sanan .seealso: `DMGetEnclosureRelation()`
407744171101SMatthew G. Knepley @*/
4078a6e0b375SMatthew G. Knepley PetscErrorCode DMGetEnclosurePoint(DM dmA, DM dmB, DMEnclosureType etype, PetscInt pB, PetscInt *pA)
407944171101SMatthew G. Knepley {
4080a6e0b375SMatthew G. Knepley   DM              sdm;
4081a6e0b375SMatthew G. Knepley   IS              subpointIS;
4082a6e0b375SMatthew G. Knepley   const PetscInt *subpoints;
4083a6e0b375SMatthew G. Knepley   PetscInt        numSubpoints;
408444171101SMatthew G. Knepley 
408544171101SMatthew G. Knepley   PetscFunctionBegin;
4086a6e0b375SMatthew G. Knepley   /* TODO Cache the IS, making it look like an index */
4087a6e0b375SMatthew G. Knepley   switch (etype) {
4088a6e0b375SMatthew G. Knepley     case DM_ENC_SUPERMESH:
4089a6e0b375SMatthew G. Knepley     sdm  = dmB;
40909566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(sdm, &subpointIS));
40919566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subpointIS, &subpoints));
4092a6e0b375SMatthew G. Knepley     *pA  = subpoints[pB];
40939566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(subpointIS, &subpoints));
4094a6e0b375SMatthew G. Knepley     break;
4095a6e0b375SMatthew G. Knepley     case DM_ENC_SUBMESH:
4096a6e0b375SMatthew G. Knepley     sdm  = dmA;
40979566063dSJacob Faibussowitsch     PetscCall(DMPlexGetSubpointIS(sdm, &subpointIS));
40989566063dSJacob Faibussowitsch     PetscCall(ISGetLocalSize(subpointIS, &numSubpoints));
40999566063dSJacob Faibussowitsch     PetscCall(ISGetIndices(subpointIS, &subpoints));
41009566063dSJacob Faibussowitsch     PetscCall(PetscFindInt(pB, numSubpoints, subpoints, pA));
4101a6e0b375SMatthew G. Knepley     if (*pA < 0) {
41029566063dSJacob Faibussowitsch       PetscCall(DMViewFromOptions(dmA, NULL, "-dm_enc_A_view"));
41039566063dSJacob Faibussowitsch       PetscCall(DMViewFromOptions(dmB, NULL, "-dm_enc_B_view"));
410463a3b9bcSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %" PetscInt_FMT " not found in submesh", pB);
4105a6e0b375SMatthew G. Knepley     }
41069566063dSJacob Faibussowitsch     PetscCall(ISRestoreIndices(subpointIS, &subpoints));
4107a6e0b375SMatthew G. Knepley     break;
4108a6e0b375SMatthew G. Knepley     case DM_ENC_EQUALITY:
4109a6e0b375SMatthew G. Knepley     case DM_ENC_NONE:
4110a6e0b375SMatthew G. Knepley     *pA = pB;break;
4111a6e0b375SMatthew G. Knepley     case DM_ENC_UNKNOWN:
4112a6e0b375SMatthew G. Knepley     {
4113a6e0b375SMatthew G. Knepley       DMEnclosureType enc;
411444171101SMatthew G. Knepley 
41159566063dSJacob Faibussowitsch       PetscCall(DMGetEnclosureRelation(dmA, dmB, &enc));
41169566063dSJacob Faibussowitsch       PetscCall(DMGetEnclosurePoint(dmA, dmB, enc, pB, pA));
4117a6e0b375SMatthew G. Knepley     }
4118a6e0b375SMatthew G. Knepley     break;
411998921bdaSJacob Faibussowitsch     default: SETERRQ(PetscObjectComm((PetscObject) dmA), PETSC_ERR_ARG_OUTOFRANGE, "Invalid enclosure type %d", (int) etype);
412044171101SMatthew G. Knepley   }
412144171101SMatthew G. Knepley   PetscFunctionReturn(0);
412244171101SMatthew G. Knepley }
4123