xref: /petsc/src/dm/impls/plex/plexsubmesh.c (revision 5f80ce2ab25dff0f4601e710601cbbcecf323266)
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;
10*5f80ce2aSJacob Faibussowitsch   CHKERRQ(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;
29*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetCellTypeLabel(dm, &ctLabel));
30412e9a14SMatthew G. Knepley   switch (dim) {
31*5f80ce2aSJacob Faibussowitsch     case 1: CHKERRQ(DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_POINT_PRISM_TENSOR, cStart, cEnd));break;
32*5f80ce2aSJacob Faibussowitsch     case 2: CHKERRQ(DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_SEG_PRISM_TENSOR, cStart, cEnd));break;
33412e9a14SMatthew G. Knepley     case 3:
34*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_TRI_PRISM_TENSOR, cStart, cEnd));
35*5f80ce2aSJacob Faibussowitsch       if (*cStart < 0) CHKERRQ(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;
47*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetHeightStratum(dm, cellHeight+1, &fStart, &fEnd));
4830560a7bSMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {
4930560a7bSMatthew G. Knepley     PetscInt supportSize;
5030560a7bSMatthew G. Knepley 
51*5f80ce2aSJacob Faibussowitsch     CHKERRQ(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 
57*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetTransitiveClosure(dm, f, PETSC_TRUE, &clSize, &closure));
58e752be1aSMatthew G. Knepley         for (cl = 0; cl < clSize*2; cl += 2) {
59*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelGetValue(label, closure[cl], &cval));
60e752be1aSMatthew G. Knepley           if (cval < 0) continue;
61*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelSetValue(label, f, cval));
62e752be1aSMatthew G. Knepley           break;
63e752be1aSMatthew G. Knepley         }
64*5f80ce2aSJacob Faibussowitsch         if (cl == clSize*2) CHKERRQ(DMLabelSetValue(label, f, 1));
65*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexRestoreTransitiveClosure(dm, f, PETSC_TRUE, &clSize, &closure));
66e752be1aSMatthew G. Knepley       } else {
67*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMLabelSetValue(label, f, val));
68e752be1aSMatthew G. Knepley       }
69e752be1aSMatthew G. Knepley     }
7030560a7bSMatthew G. Knepley   }
7130560a7bSMatthew G. Knepley   PetscFunctionReturn(0);
7230560a7bSMatthew G. Knepley }
7330560a7bSMatthew G. Knepley 
74cd0c2139SMatthew G Knepley /*@
75cd0c2139SMatthew G Knepley   DMPlexMarkBoundaryFaces - Mark all faces on the boundary
76cd0c2139SMatthew G Knepley 
77cd0c2139SMatthew G Knepley   Not Collective
78cd0c2139SMatthew G Knepley 
79d8d19677SJose E. Roman   Input Parameters:
80e752be1aSMatthew G. Knepley + dm - The original DM
81e752be1aSMatthew G. Knepley - val - The marker value, or PETSC_DETERMINE to use some value in the closure (or 1 if none are found)
82cd0c2139SMatthew G Knepley 
83cd0c2139SMatthew G Knepley   Output Parameter:
84e752be1aSMatthew G. Knepley . label - The DMLabel marking boundary faces with the given value
85cd0c2139SMatthew G Knepley 
86cd0c2139SMatthew G Knepley   Level: developer
87cd0c2139SMatthew G Knepley 
88c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMCreateLabel()
8909f723d9SJed Brown @*/
90e752be1aSMatthew G. Knepley PetscErrorCode DMPlexMarkBoundaryFaces(DM dm, PetscInt val, DMLabel label)
91cd0c2139SMatthew G Knepley {
92827c4036SVaclav Hapla   DMPlexInterpolatedFlag  flg;
93cd0c2139SMatthew G Knepley 
94cd0c2139SMatthew G Knepley   PetscFunctionBegin;
95827c4036SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
96*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexIsInterpolated(dm, &flg));
972c71b3e2SJacob Faibussowitsch   PetscCheckFalse(flg != DMPLEX_INTERPOLATED_FULL,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not fully interpolated on this rank");
98*5f80ce2aSJacob Faibussowitsch   CHKERRQ(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;
110*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelGetNumValues(label, &numValues));
111*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelGetValueIS(label, &valueIS));
112*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetHeightStratum(dm,0,&cStart,&cEnd));
113*5f80ce2aSJacob Faibussowitsch   CHKERRQ(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 
119*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetStratumSize(label, values[v], &numPoints));
120*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetStratumIS(label, values[v], &pointIS));
121*5f80ce2aSJacob Faibussowitsch     CHKERRQ(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       }
130*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure));
131b0bf5782SToby Isaac       for (c = 0; c < closureSize*2; c += 2) {
132*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMLabelSetValue(label, closure[c], values[v]));
133b0bf5782SToby Isaac       }
134*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexRestoreTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure));
135b0bf5782SToby Isaac     }
136*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISRestoreIndices(pointIS, &points));
137*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&pointIS));
138b0bf5782SToby Isaac   }
139*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISRestoreIndices(valueIS, &values));
140*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&valueIS));
141*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetPointSF(dm, &sfPoint));
142*5f80ce2aSJacob Faibussowitsch   CHKERRQ(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 */
150*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGather(label, sfPoint, &lblLeaves));
151*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetValueIS(lblLeaves, &valueIS));
152*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetLocalSize(valueIS, &numValues));
153*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetIndices(valueIS, &values));
15426279d81SSanderA     for (v = 0; v < numValues; ++v) {
15526279d81SSanderA       const PetscInt value = values[v];
15626279d81SSanderA 
157*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelGetStratumIS(lblLeaves, value, &pointIS));
158*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelInsertIS(label, pointIS, value));
159*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISDestroy(&pointIS));
16026279d81SSanderA     }
161*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISRestoreIndices(valueIS, &values));
162*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&valueIS));
163*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelDestroy(&lblLeaves));
16426279d81SSanderA     /* Push point contributions from roots into remote leaves */
165*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelDistribute(label, sfPoint, &lblRoots));
166*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetValueIS(lblRoots, &valueIS));
167*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetLocalSize(valueIS, &numValues));
168*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetIndices(valueIS, &values));
16926279d81SSanderA     for (v = 0; v < numValues; ++v) {
17026279d81SSanderA       const PetscInt value = values[v];
17126279d81SSanderA 
172*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelGetStratumIS(lblRoots, value, &pointIS));
173*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelInsertIS(label, pointIS, value));
174*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISDestroy(&pointIS));
17526279d81SSanderA     }
176*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISRestoreIndices(valueIS, &values));
177*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&valueIS));
178*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelDestroy(&lblRoots));
17926279d81SSanderA   }
180b0bf5782SToby Isaac   PetscFunctionReturn(0);
181b0bf5782SToby Isaac }
182b0bf5782SToby Isaac 
1832be2b188SMatthew G Knepley /*@
1842be2b188SMatthew G Knepley   DMPlexLabelComplete - Starting with a label marking points on a surface, we add the transitive closure to the surface
1852be2b188SMatthew G Knepley 
1862be2b188SMatthew G Knepley   Input Parameters:
1872be2b188SMatthew G Knepley + dm - The DM
1882be2b188SMatthew G Knepley - label - A DMLabel marking the surface points
1892be2b188SMatthew G Knepley 
1902be2b188SMatthew G Knepley   Output Parameter:
1912be2b188SMatthew G Knepley . label - A DMLabel marking all surface points in the transitive closure
1922be2b188SMatthew G Knepley 
1932be2b188SMatthew G Knepley   Level: developer
1942be2b188SMatthew G Knepley 
1952be2b188SMatthew G Knepley .seealso: DMPlexLabelCohesiveComplete()
1962be2b188SMatthew G Knepley @*/
1972be2b188SMatthew G Knepley PetscErrorCode DMPlexLabelComplete(DM dm, DMLabel label)
1982be2b188SMatthew G Knepley {
1992be2b188SMatthew G Knepley   PetscFunctionBegin;
200*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexLabelComplete_Internal(dm, label, PETSC_TRUE));
2012be2b188SMatthew G Knepley   PetscFunctionReturn(0);
2022be2b188SMatthew G Knepley }
2032be2b188SMatthew G Knepley 
2046cf0e42fSMatthew G. Knepley /*@
205a6e0b375SMatthew G. Knepley   DMPlexLabelAddCells - Starting with a label marking points on a surface, we add a cell for each point
2066cf0e42fSMatthew G. Knepley 
2076cf0e42fSMatthew G. Knepley   Input Parameters:
2086cf0e42fSMatthew G. Knepley + dm - The DM
2096cf0e42fSMatthew G. Knepley - label - A DMLabel marking the surface points
2106cf0e42fSMatthew G. Knepley 
2116cf0e42fSMatthew G. Knepley   Output Parameter:
2126cf0e42fSMatthew G. Knepley . label - A DMLabel incorporating cells
2136cf0e42fSMatthew G. Knepley 
2146cf0e42fSMatthew G. Knepley   Level: developer
2156cf0e42fSMatthew G. Knepley 
2166cf0e42fSMatthew G. Knepley   Note: The cells allow FEM boundary conditions to be applied using the cell geometry
2176cf0e42fSMatthew G. Knepley 
218a6e0b375SMatthew G. Knepley .seealso: DMPlexLabelAddFaceCells(), DMPlexLabelComplete(), DMPlexLabelCohesiveComplete()
2196cf0e42fSMatthew G. Knepley @*/
2206cf0e42fSMatthew G. Knepley PetscErrorCode DMPlexLabelAddCells(DM dm, DMLabel label)
2216cf0e42fSMatthew G. Knepley {
2226cf0e42fSMatthew G. Knepley   IS              valueIS;
2236cf0e42fSMatthew G. Knepley   const PetscInt *values;
224485ad865SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd;
2256cf0e42fSMatthew G. Knepley 
2266cf0e42fSMatthew G. Knepley   PetscFunctionBegin;
227*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
228*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelGetNumValues(label, &numValues));
229*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelGetValueIS(label, &valueIS));
230*5f80ce2aSJacob Faibussowitsch   CHKERRQ(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 
236*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetStratumSize(label, values[v], &numPoints));
237*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetStratumIS(label, values[v], &pointIS));
238*5f80ce2aSJacob Faibussowitsch     CHKERRQ(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 
243*5f80ce2aSJacob Faibussowitsch       CHKERRQ(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];
246*5f80ce2aSJacob Faibussowitsch         if ((cell >= cStart) && (cell < cEnd)) {CHKERRQ(DMLabelSetValue(label, cell, values[v])); break;}
24722eabd52SMatthew G. Knepley       }
248*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure));
2496cf0e42fSMatthew G. Knepley     }
250*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISRestoreIndices(pointIS, &points));
251*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&pointIS));
2526cf0e42fSMatthew G. Knepley   }
253*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISRestoreIndices(valueIS, &values));
254*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&valueIS));
2556cf0e42fSMatthew G. Knepley   PetscFunctionReturn(0);
2566cf0e42fSMatthew G. Knepley }
2576cf0e42fSMatthew G. Knepley 
258f402d5e4SToby Isaac /*@
259a6e0b375SMatthew G. Knepley   DMPlexLabelAddFaceCells - Starting with a label marking faces on a surface, we add a cell for each face
260a6e0b375SMatthew G. Knepley 
261a6e0b375SMatthew G. Knepley   Input Parameters:
262a6e0b375SMatthew G. Knepley + dm - The DM
263a6e0b375SMatthew G. Knepley - label - A DMLabel marking the surface points
264a6e0b375SMatthew G. Knepley 
265a6e0b375SMatthew G. Knepley   Output Parameter:
266a6e0b375SMatthew G. Knepley . label - A DMLabel incorporating cells
267a6e0b375SMatthew G. Knepley 
268a6e0b375SMatthew G. Knepley   Level: developer
269a6e0b375SMatthew G. Knepley 
270a6e0b375SMatthew G. Knepley   Note: The cells allow FEM boundary conditions to be applied using the cell geometry
271a6e0b375SMatthew G. Knepley 
272a6e0b375SMatthew G. Knepley .seealso: DMPlexLabelAddCells(), DMPlexLabelComplete(), DMPlexLabelCohesiveComplete()
273a6e0b375SMatthew G. Knepley @*/
274a6e0b375SMatthew G. Knepley PetscErrorCode DMPlexLabelAddFaceCells(DM dm, DMLabel label)
275a6e0b375SMatthew G. Knepley {
276a6e0b375SMatthew G. Knepley   IS              valueIS;
277a6e0b375SMatthew G. Knepley   const PetscInt *values;
278a6e0b375SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd, fStart, fEnd;
279a6e0b375SMatthew G. Knepley 
280a6e0b375SMatthew G. Knepley   PetscFunctionBegin;
281*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
282*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
283*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelGetNumValues(label, &numValues));
284*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelGetValueIS(label, &valueIS));
285*5f80ce2aSJacob Faibussowitsch   CHKERRQ(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 
291*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetStratumSize(label, values[v], &numPoints));
292*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetStratumIS(label, values[v], &pointIS));
293*5f80ce2aSJacob Faibussowitsch     CHKERRQ(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;
300*5f80ce2aSJacob Faibussowitsch       CHKERRQ(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];
303*5f80ce2aSJacob Faibussowitsch         if ((cell >= cStart) && (cell < cEnd)) {CHKERRQ(DMLabelSetValue(label, cell, values[v])); break;}
304a6e0b375SMatthew G. Knepley       }
305*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexRestoreTransitiveClosure(dm, face, PETSC_FALSE, &closureSize, &closure));
306a6e0b375SMatthew G. Knepley     }
307*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISRestoreIndices(pointIS, &points));
308*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&pointIS));
309a6e0b375SMatthew G. Knepley   }
310*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISRestoreIndices(valueIS, &values));
311*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&valueIS));
312a6e0b375SMatthew G. Knepley   PetscFunctionReturn(0);
313a6e0b375SMatthew G. Knepley }
314a6e0b375SMatthew G. Knepley 
315a6e0b375SMatthew G. Knepley /*@
316f402d5e4SToby Isaac   DMPlexLabelClearCells - Remove cells from a label
317f402d5e4SToby Isaac 
318f402d5e4SToby Isaac   Input Parameters:
319f402d5e4SToby Isaac + dm - The DM
320f402d5e4SToby Isaac - label - A DMLabel marking surface points and their adjacent cells
321f402d5e4SToby Isaac 
322f402d5e4SToby Isaac   Output Parameter:
323f402d5e4SToby Isaac . label - A DMLabel without cells
324f402d5e4SToby Isaac 
325f402d5e4SToby Isaac   Level: developer
326f402d5e4SToby Isaac 
327a6e0b375SMatthew G. Knepley   Note: This undoes DMPlexLabelAddCells() or DMPlexLabelAddFaceCells()
328f402d5e4SToby Isaac 
329f402d5e4SToby Isaac .seealso: DMPlexLabelComplete(), DMPlexLabelCohesiveComplete(), DMPlexLabelAddCells()
330f402d5e4SToby Isaac @*/
331f402d5e4SToby Isaac PetscErrorCode DMPlexLabelClearCells(DM dm, DMLabel label)
332f402d5e4SToby Isaac {
333f402d5e4SToby Isaac   IS              valueIS;
334f402d5e4SToby Isaac   const PetscInt *values;
335485ad865SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd;
336f402d5e4SToby Isaac 
337f402d5e4SToby Isaac   PetscFunctionBegin;
338*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
339*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelGetNumValues(label, &numValues));
340*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelGetValueIS(label, &valueIS));
341*5f80ce2aSJacob Faibussowitsch   CHKERRQ(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 
347*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetStratumSize(label, values[v], &numPoints));
348*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetStratumIS(label, values[v], &pointIS));
349*5f80ce2aSJacob Faibussowitsch     CHKERRQ(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) {
354*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMLabelClearValue(label,point,values[v]));
355f402d5e4SToby Isaac       }
356f402d5e4SToby Isaac     }
357*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISRestoreIndices(pointIS, &points));
358*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&pointIS));
359f402d5e4SToby Isaac   }
360*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISRestoreIndices(valueIS, &values));
361*5f80ce2aSJacob Faibussowitsch   CHKERRQ(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;
436*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetDepth(dm, &depth));
437cd0c2139SMatthew G Knepley   if (depth < 0) PetscFunctionReturn(0);
438cd0c2139SMatthew G Knepley   /* Step 1: Expand chart */
439*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetChart(dm, &pStart, &pEnd));
4400974a383SToby Isaac   pEnd = DMPlexShiftPoint_Internal(pEnd,depth,depthShift);
441*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexSetChart(dmNew, pStart, pEnd));
442*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMCreateLabel(dmNew,"depth"));
443*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetDepthLabel(dmNew,&depthLabel));
444*5f80ce2aSJacob Faibussowitsch   CHKERRQ(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 
450*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetDepthStratum(dm, d, &pStart, &pEnd));
451fa8e8ae5SToby Isaac     pStartNew = DMPlexShiftPoint_Internal(pStart, depth, depthShift);
452fa8e8ae5SToby Isaac     pEndNew = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
453*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISCreateStride(PETSC_COMM_SELF, pEndNew - pStartNew, pStartNew, 1, &pIS));
454*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelSetStratumIS(depthLabel, d, pIS));
455*5f80ce2aSJacob Faibussowitsch     CHKERRQ(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 
461*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetConeSize(dm, p, &size));
462*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexSetConeSize(dmNew, newp, size));
463*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetSupportSize(dm, p, &size));
464*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexSetSupportSize(dmNew, newp, size));
465*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetCellType(dm, p, &ct));
466*5f80ce2aSJacob Faibussowitsch       CHKERRQ(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;
478*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetDepth(dm, &depth));
479cd0c2139SMatthew G Knepley   if (depth < 0) PetscFunctionReturn(0);
480*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
481*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetMaxSizes(dmNew, &maxConeSizeNew, &maxSupportSizeNew));
482*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(PetscMax(PetscMax(maxConeSize, maxSupportSize), PetscMax(maxConeSizeNew, maxSupportSizeNew)),&newpoints));
483cd0c2139SMatthew G Knepley   /* Step 5: Set cones and supports */
484*5f80ce2aSJacob Faibussowitsch   CHKERRQ(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 
489*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetConeSize(dm, p, &size));
490*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetCone(dm, p, &points));
491*5f80ce2aSJacob Faibussowitsch     CHKERRQ(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     }
495*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexSetCone(dmNew, newp, newpoints));
496*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexSetConeOrientation(dmNew, newp, orientations));
497*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetSupportSize(dm, p, &size));
498*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetSupportSize(dmNew, newp, &sizeNew));
499*5f80ce2aSJacob Faibussowitsch     CHKERRQ(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;
504*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexSetSupport(dmNew, newp, newpoints));
505cd0c2139SMatthew G Knepley   }
506*5f80ce2aSJacob Faibussowitsch   CHKERRQ(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;
520*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinateDim(dm, &dim));
521*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetCoordinateDim(dmNew, dim));
522*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetDepth(dm, &depth));
523cd0c2139SMatthew G Knepley   /* Step 8: Convert coordinates */
524*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
525*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
526*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetDepthStratum(dmNew, 0, &vStartNew, &vEndNew));
527*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetHeightStratum(dmNew, 0, &cStartNew, &cEndNew));
528*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinateSection(dm, &coordSection));
529*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &newCoordSection));
530*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetNumFields(newCoordSection, 1));
531*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetFieldComponents(newCoordSection, 0, dim));
532*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetChart(coordSection, &sStart, &sEnd));
533f2b8cce1SMatthew G. Knepley   hasCells = sStart == cStart ? PETSC_TRUE : PETSC_FALSE;
534*5f80ce2aSJacob Faibussowitsch   CHKERRQ(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 
539*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetDof(coordSection, c, &dof));
540*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionSetDof(newCoordSection, cNew, dof));
541*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionSetFieldDof(newCoordSection, cNew, 0, dof));
542f2b8cce1SMatthew G. Knepley     }
543f2b8cce1SMatthew G. Knepley   }
544cd0c2139SMatthew G Knepley   for (v = vStartNew; v < vEndNew; ++v) {
545*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetDof(newCoordSection, v, dim));
546*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetFieldDof(newCoordSection, v, 0, dim));
547cd0c2139SMatthew G Knepley   }
548*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionSetUp(newCoordSection));
549*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetCoordinateSection(dmNew, PETSC_DETERMINE, newCoordSection));
550*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSectionGetStorageSize(newCoordSection, &coordSize));
551*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecCreate(PETSC_COMM_SELF, &newCoordinates));
552*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectSetName((PetscObject) newCoordinates, "coordinates"));
553*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecSetSizes(newCoordinates, coordSize, PETSC_DETERMINE));
554*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecSetBlockSize(newCoordinates, dim));
555*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecSetType(newCoordinates,VECSTANDARD));
556*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetCoordinatesLocal(dmNew, newCoordinates));
557*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinatesLocal(dm, &coordinates));
558*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetArray(coordinates, &coords));
559*5f80ce2aSJacob Faibussowitsch   CHKERRQ(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 
564*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetDof(coordSection, c, &dof));
565*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetOffset(coordSection, c, &off));
566*5f80ce2aSJacob Faibussowitsch       CHKERRQ(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 
573*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(coordSection, v, &dof));
574*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetOffset(coordSection, v, &off));
575*5f80ce2aSJacob Faibussowitsch     CHKERRQ(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   }
578*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecRestoreArray(coordinates, &coords));
579*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecRestoreArray(newCoordinates, &newCoords));
580*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&newCoordinates));
581*5f80ce2aSJacob Faibussowitsch   CHKERRQ(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;
594*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetDepth(dm, &depth));
595*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetChart(dm, &pStart, &pEnd));
596*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSFGetGraph(sf, &numRoots, &numLeaves, &localPoints, &remotePoints));
5976e21efdcSToby Isaac   totShift = DMPlexShiftPoint_Internal(pEnd, depth, depthShift) - pEnd;
598cd0c2139SMatthew G Knepley   if (numRoots >= 0) {
599*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc2(numRoots, &newLocation, pEnd-pStart, &newRemoteLocation));
6000e33faafSMatthew G. Knepley     for (l = 0; l < numRoots; ++l) newLocation[l] = DMPlexShiftPoint_Internal(l, depth, depthShift);
601*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSFBcastBegin(sf, MPIU_INT, newLocation, newRemoteLocation, MPI_REPLACE));
602*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSFBcastEnd(sf, MPIU_INT, newLocation, newRemoteLocation, MPI_REPLACE));
603*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1(numLeaves, &glocalPoints));
604*5f80ce2aSJacob Faibussowitsch     CHKERRQ(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     }
610*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree2(newLocation, newRemoteLocation));
611*5f80ce2aSJacob Faibussowitsch     CHKERRQ(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 */
623*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetPointSF(dm, &sfPoint));
624*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetPointSF(dmNew, &sfPointNew));
625*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexShiftSF_Single(dm, depthShift, sfPoint, sfPointNew));
6260e33faafSMatthew G. Knepley   /* Step 9b: Convert naturalSF */
627*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetUseNatural(dm, &useNatural));
6280e33faafSMatthew G. Knepley   if (useNatural) {
6290e33faafSMatthew G. Knepley     PetscSF sfNat, sfNatNew;
6300e33faafSMatthew G. Knepley 
631*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetUseNatural(dmNew, useNatural));
632*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetNaturalSF(dm, &sfNat));
633*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetNaturalSF(dmNew, &sfNatNew));
634*5f80ce2aSJacob Faibussowitsch     CHKERRQ(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;
644*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetDepth(dm, &depth));
645cd0c2139SMatthew G Knepley   /* Step 10: Convert labels */
646*5f80ce2aSJacob Faibussowitsch   CHKERRQ(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 
655*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetLabelName(dm, l, &lname));
656*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcmp(lname, "depth", &isDepth));
657cd0c2139SMatthew G Knepley     if (isDepth) continue;
658*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcmp(lname, "dim", &isDim));
659fa8e8ae5SToby Isaac     if (isDim) continue;
660*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMCreateLabel(dmNew, lname));
661*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetLabel(dm, lname, &label));
662*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetLabel(dmNew, lname, &newlabel));
663*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetDefaultValue(label,&val));
664*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelSetDefaultValue(newlabel,val));
665*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetValueIS(label, &valueIS));
666*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetLocalSize(valueIS, &numValues));
667*5f80ce2aSJacob Faibussowitsch     CHKERRQ(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 
673*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelGetStratumIS(label, values[val], &pointIS));
674*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISGetLocalSize(pointIS, &numPoints));
675*5f80ce2aSJacob Faibussowitsch       CHKERRQ(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 
679*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMLabelSetValue(newlabel, newpoint, values[val]));
680cd0c2139SMatthew G Knepley       }
681*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISRestoreIndices(pointIS, &points));
682*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISDestroy(&pointIS));
683cd0c2139SMatthew G Knepley     }
684*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISRestoreIndices(valueIS, &values));
685*5f80ce2aSJacob Faibussowitsch     CHKERRQ(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) */
701*5f80ce2aSJacob Faibussowitsch   CHKERRMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
702*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetPointSF(dm, &sfPoint));
703*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetVTKCellHeight(dmNew, &cellHeight));
704*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd));
705*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSFGetGraph(sfPoint, NULL, &numLeaves, &leafLocal, &leafRemote));
706*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMCreateLabel(dmNew, "vtk"));
707*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetLabel(dmNew, "vtk", &vtkLabel));
708d56405f8SMatthew G. Knepley   if (createGhostLabel) {
709*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMCreateLabel(dmNew, "ghost"));
710*5f80ce2aSJacob Faibussowitsch     CHKERRQ(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) {
714*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelSetValue(vtkLabel, c, 1));
715cd0c2139SMatthew G Knepley     }
716cd0c2139SMatthew G Knepley     if (leafLocal[l] >= cEnd) break;
717cd0c2139SMatthew G Knepley     if (leafRemote[l].rank == rank) {
718*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelSetValue(vtkLabel, c, 1));
719d56405f8SMatthew G. Knepley     } else if (ghostLabel) {
720*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelSetValue(ghostLabel, c, 2));
721cd0c2139SMatthew G Knepley     }
722cd0c2139SMatthew G Knepley   }
723cd0c2139SMatthew G Knepley   for (; c < cEnd; ++c) {
724*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelSetValue(vtkLabel, c, 1));
725cd0c2139SMatthew G Knepley   }
726d56405f8SMatthew G. Knepley   if (ghostLabel) {
727*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetHeightStratum(dmNew, 1, &fStart, &fEnd));
728cd0c2139SMatthew G Knepley     for (f = fStart; f < fEnd; ++f) {
729cd0c2139SMatthew G Knepley       PetscInt numCells;
730cd0c2139SMatthew G Knepley 
731*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetSupportSize(dmNew, f, &numCells));
732cd0c2139SMatthew G Knepley       if (numCells < 2) {
733*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMLabelSetValue(ghostLabel, f, 1));
734cd0c2139SMatthew G Knepley       } else {
735cd0c2139SMatthew G Knepley         const PetscInt *cells = NULL;
736cd0c2139SMatthew G Knepley         PetscInt        vA, vB;
737cd0c2139SMatthew G Knepley 
738*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetSupport(dmNew, f, &cells));
739*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMLabelGetValue(vtkLabel, cells[0], &vA));
740*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMLabelGetValue(vtkLabel, cells[1], &vB));
741*5f80ce2aSJacob Faibussowitsch         if (vA != 1 && vB != 1) CHKERRQ(DMLabelSetValue(ghostLabel, f, 1));
742cd0c2139SMatthew G Knepley       }
743cd0c2139SMatthew G Knepley     }
744d56405f8SMatthew G. Knepley   }
745cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
746cd0c2139SMatthew G Knepley }
747cd0c2139SMatthew G Knepley 
748ca04dac2SToby Isaac static PetscErrorCode DMPlexShiftTree_Internal(DM dm, PetscInt depthShift[], DM dmNew)
749ca04dac2SToby Isaac {
750ca04dac2SToby Isaac   DM             refTree;
751ca04dac2SToby Isaac   PetscSection   pSec;
752ca04dac2SToby Isaac   PetscInt       *parents, *childIDs;
753ca04dac2SToby Isaac 
754ca04dac2SToby Isaac   PetscFunctionBegin;
755*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetReferenceTree(dm,&refTree));
756*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexSetReferenceTree(dmNew,refTree));
757*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetTree(dm,&pSec,&parents,&childIDs,NULL,NULL));
758ca04dac2SToby Isaac   if (pSec) {
7592582d50cSToby Isaac     PetscInt p, pStart, pEnd, *parentsShifted, pStartShifted, pEndShifted, depth;
760fb4630b5SToby Isaac     PetscInt *childIDsShifted;
761ca04dac2SToby Isaac     PetscSection pSecShifted;
762ca04dac2SToby Isaac 
763*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetChart(pSec,&pStart,&pEnd));
764*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetDepth(dm,&depth));
7652582d50cSToby Isaac     pStartShifted = DMPlexShiftPoint_Internal(pStart,depth,depthShift);
7662582d50cSToby Isaac     pEndShifted   = DMPlexShiftPoint_Internal(pEnd,depth,depthShift);
767*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc2(pEndShifted - pStartShifted,&parentsShifted,pEndShifted-pStartShifted,&childIDsShifted));
768*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionCreate(PetscObjectComm((PetscObject)dmNew),&pSecShifted));
769*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetChart(pSecShifted,pStartShifted,pEndShifted));
770ca04dac2SToby Isaac     for (p = pStartShifted; p < pEndShifted; p++) {
771fb4630b5SToby Isaac       /* start off assuming no children */
772*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionSetDof(pSecShifted,p,0));
773fb4630b5SToby Isaac     }
774fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
775fb4630b5SToby Isaac       PetscInt dof;
776fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p,depth,depthShift);
777ca04dac2SToby Isaac 
778*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetDof(pSec,p,&dof));
779*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionSetDof(pSecShifted,pNew,dof));
780ca04dac2SToby Isaac     }
781*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetUp(pSecShifted));
782fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
783fb4630b5SToby Isaac       PetscInt dof;
784fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p,depth,depthShift);
785fb4630b5SToby Isaac 
786*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetDof(pSec,p,&dof));
787fb4630b5SToby Isaac       if (dof) {
788fb4630b5SToby Isaac         PetscInt off, offNew;
789fb4630b5SToby Isaac 
790*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetOffset(pSec,p,&off));
791*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetOffset(pSecShifted,pNew,&offNew));
792fb4630b5SToby Isaac         parentsShifted[offNew] = DMPlexShiftPoint_Internal(parents[off],depth,depthShift);
793fb4630b5SToby Isaac         childIDsShifted[offNew] = childIDs[off];
794fb4630b5SToby Isaac       }
795fb4630b5SToby Isaac     }
796*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexSetTree(dmNew,pSecShifted,parentsShifted,childIDsShifted));
797*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFree2(parentsShifted,childIDsShifted));
798*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionDestroy(&pSecShifted));
799ca04dac2SToby Isaac   }
800ca04dac2SToby Isaac   PetscFunctionReturn(0);
801ca04dac2SToby Isaac }
802ca04dac2SToby Isaac 
803cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexConstructGhostCells_Internal(DM dm, DMLabel label, PetscInt *numGhostCells, DM gdm)
804cd0c2139SMatthew G Knepley {
805da97024aSMatthew G. Knepley   PetscSF               sf;
806cd0c2139SMatthew G Knepley   IS                    valueIS;
807da97024aSMatthew G. Knepley   const PetscInt       *values, *leaves;
808cd0c2139SMatthew G Knepley   PetscInt             *depthShift;
8092582d50cSToby Isaac   PetscInt              d, depth = 0, nleaves, loc, Ng, numFS, fs, fStart, fEnd, ghostCell, cEnd, c;
81090b157c4SStefano Zampini   PetscBool             isper;
81190b157c4SStefano Zampini   const PetscReal      *maxCell, *L;
81290b157c4SStefano Zampini   const DMBoundaryType *bd;
813cd0c2139SMatthew G Knepley 
814cd0c2139SMatthew G Knepley   PetscFunctionBegin;
815*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetPointSF(dm, &sf));
816*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL));
817da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
818*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
819cd0c2139SMatthew G Knepley   /* Count ghost cells */
820*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelGetValueIS(label, &valueIS));
821*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetLocalSize(valueIS, &numFS));
822*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetIndices(valueIS, &values));
8234a6cfa73SMatthew G. Knepley   Ng   = 0;
824cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
82546c796b9SMatthew G. Knepley     IS              faceIS;
82646c796b9SMatthew G. Knepley     const PetscInt *faces;
82746c796b9SMatthew G. Knepley     PetscInt        numFaces, f, numBdFaces = 0;
828cd0c2139SMatthew G Knepley 
829*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetStratumIS(label, values[fs], &faceIS));
830*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetLocalSize(faceIS, &numFaces));
831*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetIndices(faceIS, &faces));
83246c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
833ca04dac2SToby Isaac       PetscInt numChildren;
834ca04dac2SToby Isaac 
835*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFindInt(faces[f], nleaves, leaves, &loc));
836*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL));
837ca04dac2SToby Isaac       /* non-local and ancestors points don't get to register ghosts */
838ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
83946c796b9SMatthew G. Knepley       if ((faces[f] >= fStart) && (faces[f] < fEnd)) ++numBdFaces;
84046c796b9SMatthew G. Knepley     }
8414a6cfa73SMatthew G. Knepley     Ng += numBdFaces;
842*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISRestoreIndices(faceIS, &faces));
843*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&faceIS));
844cd0c2139SMatthew G Knepley   }
845*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetDepth(dm, &depth));
846*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(2*(depth+1), &depthShift));
8472582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
84859eef20bSToby Isaac     PetscInt dEnd;
8492582d50cSToby Isaac 
850*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetDepthStratum(dm,d,NULL,&dEnd));
85159eef20bSToby Isaac     depthShift[2*d]   = dEnd;
8522582d50cSToby Isaac     depthShift[2*d+1] = 0;
8532582d50cSToby Isaac   }
8542582d50cSToby Isaac   if (depth >= 0) depthShift[2*depth+1] = Ng;
855*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexShiftPointSetUp_Internal(depth,depthShift));
856*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexShiftSizes_Internal(dm, depthShift, gdm));
857cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
858*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetHeightStratum(dm, 0, NULL, &cEnd));
8594a6cfa73SMatthew G. Knepley   for (c = cEnd; c < cEnd + Ng; ++c) {
860*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexSetConeSize(gdm, c, 1));
861cd0c2139SMatthew G Knepley   }
862cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
863cd0c2139SMatthew G Knepley     IS              faceIS;
864cd0c2139SMatthew G Knepley     const PetscInt *faces;
865cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
866cd0c2139SMatthew G Knepley 
867*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetStratumIS(label, values[fs], &faceIS));
868*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetLocalSize(faceIS, &numFaces));
869*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetIndices(faceIS, &faces));
870cd0c2139SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
871ca04dac2SToby Isaac       PetscInt size, numChildren;
872cd0c2139SMatthew G Knepley 
873*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFindInt(faces[f], nleaves, leaves, &loc));
874*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL));
875ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
87646c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
877*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetSupportSize(dm, faces[f], &size));
8782c71b3e2SJacob Faibussowitsch       PetscCheckFalse(size != 1,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM has boundary face %d with %d support cells", faces[f], size);
879*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexSetSupportSize(gdm, faces[f] + Ng, 2));
880cd0c2139SMatthew G Knepley     }
881*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISRestoreIndices(faceIS, &faces));
882*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&faceIS));
883cd0c2139SMatthew G Knepley   }
884cd0c2139SMatthew G Knepley   /* Step 4: Setup ghosted DM */
885*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetUp(gdm));
886*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexShiftPoints_Internal(dm, depthShift, gdm));
887cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
888cd0c2139SMatthew G Knepley   ghostCell = cEnd;
889cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
890cd0c2139SMatthew G Knepley     IS              faceIS;
891cd0c2139SMatthew G Knepley     const PetscInt *faces;
892cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
893cd0c2139SMatthew G Knepley 
894*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetStratumIS(label, values[fs], &faceIS));
895*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetLocalSize(faceIS, &numFaces));
896*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetIndices(faceIS, &faces));
89746c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
898ca04dac2SToby Isaac       PetscInt newFace = faces[f] + Ng, numChildren;
899cd0c2139SMatthew G Knepley 
900*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFindInt(faces[f], nleaves, leaves, &loc));
901*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL));
902ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
90346c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
904*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexSetCone(gdm, ghostCell, &newFace));
905*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexInsertSupport(gdm, newFace, 1, ghostCell));
90646c796b9SMatthew G. Knepley       ++ghostCell;
907cd0c2139SMatthew G Knepley     }
908*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISRestoreIndices(faceIS, &faces));
909*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&faceIS));
910cd0c2139SMatthew G Knepley   }
911*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISRestoreIndices(valueIS, &values));
912*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&valueIS));
913*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexShiftCoordinates_Internal(dm, depthShift, gdm));
914*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexShiftSF_Internal(dm, depthShift, gdm));
915*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexShiftLabels_Internal(dm, depthShift, gdm));
916*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexCreateVTKLabel_Internal(dm, PETSC_TRUE, gdm));
917*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexShiftTree_Internal(dm, depthShift, gdm));
918*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(depthShift));
919412e9a14SMatthew G. Knepley   for (c = cEnd; c < cEnd + Ng; ++c) {
920*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexSetCellType(gdm, c, DM_POLYTOPE_FV_GHOST));
921412e9a14SMatthew G. Knepley   }
922966c7b3fSMatthew G. Knepley   /* Step 7: Periodicity */
923*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd));
924*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetPeriodicity(gdm, isper, maxCell,  L,  bd));
9254a6cfa73SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
926cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
927cd0c2139SMatthew G Knepley }
928cd0c2139SMatthew G Knepley 
929cd0c2139SMatthew G Knepley /*@C
930cd0c2139SMatthew G Knepley   DMPlexConstructGhostCells - Construct ghost cells which connect to every boundary face
931cd0c2139SMatthew G Knepley 
932cd0c2139SMatthew G Knepley   Collective on dm
933cd0c2139SMatthew G Knepley 
934cd0c2139SMatthew G Knepley   Input Parameters:
935cd0c2139SMatthew G Knepley + dm - The original DM
936cd0c2139SMatthew G Knepley - labelName - The label specifying the boundary faces, or "Face Sets" if this is NULL
937cd0c2139SMatthew G Knepley 
938cd0c2139SMatthew G Knepley   Output Parameters:
939cd0c2139SMatthew G Knepley + numGhostCells - The number of ghost cells added to the DM
940cd0c2139SMatthew G Knepley - dmGhosted - The new DM
941cd0c2139SMatthew G Knepley 
942cd0c2139SMatthew G Knepley   Note: If no label exists of that name, one will be created marking all boundary faces
943cd0c2139SMatthew G Knepley 
944cd0c2139SMatthew G Knepley   Level: developer
945cd0c2139SMatthew G Knepley 
946cd0c2139SMatthew G Knepley .seealso: DMCreate()
94731266bc0SMatthew G. Knepley @*/
948cd0c2139SMatthew G Knepley PetscErrorCode DMPlexConstructGhostCells(DM dm, const char labelName[], PetscInt *numGhostCells, DM *dmGhosted)
949cd0c2139SMatthew G Knepley {
950cd0c2139SMatthew G Knepley   DM             gdm;
951cd0c2139SMatthew G Knepley   DMLabel        label;
952cd0c2139SMatthew G Knepley   const char    *name = labelName ? labelName : "Face Sets";
953412e9a14SMatthew G. Knepley   PetscInt       dim, Ng = 0;
954b0441da4SMatthew G. Knepley   PetscBool      useCone, useClosure;
955cd0c2139SMatthew G Knepley 
956cd0c2139SMatthew G Knepley   PetscFunctionBegin;
957cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9584a6cfa73SMatthew G. Knepley   if (numGhostCells) PetscValidPointer(numGhostCells, 3);
959cd0c2139SMatthew G Knepley   PetscValidPointer(dmGhosted, 4);
960*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMCreate(PetscObjectComm((PetscObject)dm), &gdm));
961*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetType(gdm, DMPLEX));
962*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetDimension(dm, &dim));
963*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetDimension(gdm, dim));
964*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetBasicAdjacency(dm, &useCone, &useClosure));
965*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetBasicAdjacency(gdm, useCone,  useClosure));
966*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetLabel(dm, name, &label));
967cd0c2139SMatthew G Knepley   if (!label) {
968cd0c2139SMatthew G Knepley     /* Get label for boundary faces */
969*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMCreateLabel(dm, name));
970*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetLabel(dm, name, &label));
971*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexMarkBoundaryFaces(dm, 1, label));
972cd0c2139SMatthew G Knepley   }
973*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexConstructGhostCells_Internal(dm, label, &Ng, gdm));
974*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMCopyDisc(dm, gdm));
975*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexCopy_Internal(dm, PETSC_TRUE, gdm));
976cad26855SMatthew G. Knepley   gdm->setfromoptionscalled = dm->setfromoptionscalled;
977d80ece95SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
978cd0c2139SMatthew G Knepley   *dmGhosted = gdm;
979cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
980cd0c2139SMatthew G Knepley }
981cd0c2139SMatthew G Knepley 
982607ab7a9SMatthew G. Knepley /*
983faedd622SMatthew G. Knepley   We are adding three kinds of points here:
984607ab7a9SMatthew G. Knepley     Replicated:     Copies of points which exist in the mesh, such as vertices identified across a fault
985faedd622SMatthew G. Knepley     Non-replicated: Points which exist on the fault, but are not replicated
986607ab7a9SMatthew G. Knepley     Hybrid:         Entirely new points, such as cohesive cells
987a6ae58d1SMatthew G. Knepley 
988a6ae58d1SMatthew G. Knepley   When creating subsequent cohesive cells, we shift the old hybrid cells to the end of the numbering at
989a6ae58d1SMatthew G. Knepley   each depth so that the new split/hybrid points can be inserted as a block.
990607ab7a9SMatthew G. Knepley */
9917db7e0a7SMatthew G. Knepley static PetscErrorCode DMPlexConstructCohesiveCells_Internal(DM dm, DMLabel label, DMLabel splitLabel, DM sdm)
992cd0c2139SMatthew G Knepley {
993cd0c2139SMatthew G Knepley   MPI_Comm         comm;
994607ab7a9SMatthew G. Knepley   IS               valueIS;
995607ab7a9SMatthew G. Knepley   PetscInt         numSP = 0;          /* The number of depths for which we have replicated points */
996607ab7a9SMatthew G. Knepley   const PetscInt  *values;             /* List of depths for which we have replicated points */
99718c5995bSMatthew G. Knepley   IS              *splitIS;
99818c5995bSMatthew G. Knepley   IS              *unsplitIS;
999607ab7a9SMatthew G. Knepley   PetscInt        *numSplitPoints;     /* The number of replicated points at each depth */
100018c5995bSMatthew G. Knepley   PetscInt        *numUnsplitPoints;   /* The number of non-replicated points at each depth which still give rise to hybrid points */
100136dbac82SMatthew G. Knepley   PetscInt        *numHybridPoints;    /* The number of new hybrid points at each depth */
100236dbac82SMatthew G. Knepley   PetscInt        *numHybridPointsOld; /* The number of existing hybrid points at each depth */
1003607ab7a9SMatthew G. Knepley   const PetscInt **splitPoints;        /* Replicated points for each depth */
100418c5995bSMatthew G. Knepley   const PetscInt **unsplitPoints;      /* Non-replicated points for each depth */
1005cd0c2139SMatthew G Knepley   PetscSection     coordSection;
1006cd0c2139SMatthew G Knepley   Vec              coordinates;
1007cd0c2139SMatthew G Knepley   PetscScalar     *coords;
1008a6ae58d1SMatthew G. Knepley   PetscInt        *depthMax;           /* The first hybrid point at each depth in the original mesh */
1009a6ae58d1SMatthew G. Knepley   PetscInt        *depthEnd;           /* The point limit at each depth in the original mesh */
1010607ab7a9SMatthew G. Knepley   PetscInt        *depthShift;         /* Number of replicated+hybrid points at each depth */
1011607ab7a9SMatthew G. Knepley   PetscInt        *pMaxNew;            /* The first replicated point at each depth in the new mesh, hybrids come after this */
1012607ab7a9SMatthew G. Knepley   PetscInt        *coneNew, *coneONew, *supportNew;
101318c5995bSMatthew G. Knepley   PetscInt         shift = 100, shift2 = 200, depth = 0, dep, dim, d, sp, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, numLabels, vStart, vEnd, pEnd, p, v;
1014cd0c2139SMatthew G Knepley 
1015cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1016*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectGetComm((PetscObject)dm,&comm));
1017*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetDimension(dm, &dim));
1018*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetDepth(dm, &depth));
1019*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
1020412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
1021*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMCreateLabel(sdm, "celltype"));
1022cd0c2139SMatthew G Knepley   /* Count split points and add cohesive cells */
1023*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize));
1024*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc5(depth+1,&depthMax,depth+1,&depthEnd,2*(depth+1),&depthShift,depth+1,&pMaxNew,depth+1,&numHybridPointsOld));
1025*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc7(depth+1,&splitIS,depth+1,&unsplitIS,depth+1,&numSplitPoints,depth+1,&numUnsplitPoints,depth+1,&numHybridPoints,depth+1,&splitPoints,depth+1,&unsplitPoints));
1026607ab7a9SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
1027*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetDepthStratum(dm, d, NULL, &pMaxNew[d]));
1028*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetTensorPrismBounds_Internal(dm, d, &depthMax[d], NULL));
1029a6ae58d1SMatthew G. Knepley     depthEnd[d]           = pMaxNew[d];
1030a6ae58d1SMatthew G. Knepley     depthMax[d]           = depthMax[d] < 0 ? depthEnd[d] : depthMax[d];
1031607ab7a9SMatthew G. Knepley     numSplitPoints[d]     = 0;
103218c5995bSMatthew G. Knepley     numUnsplitPoints[d]   = 0;
1033607ab7a9SMatthew G. Knepley     numHybridPoints[d]    = 0;
1034a6ae58d1SMatthew G. Knepley     numHybridPointsOld[d] = depthMax[d] < 0 ? 0 : depthEnd[d] - depthMax[d];
1035607ab7a9SMatthew G. Knepley     splitPoints[d]        = NULL;
103618c5995bSMatthew G. Knepley     unsplitPoints[d]      = NULL;
103718c5995bSMatthew G. Knepley     splitIS[d]            = NULL;
103818c5995bSMatthew G. Knepley     unsplitIS[d]          = NULL;
103959eef20bSToby Isaac     /* we are shifting the existing hybrid points with the stratum behind them, so
104059eef20bSToby Isaac      * the split comes at the end of the normal points, i.e., at depthMax[d] */
104159eef20bSToby Isaac     depthShift[2*d]       = depthMax[d];
104259eef20bSToby Isaac     depthShift[2*d+1]     = 0;
1043607ab7a9SMatthew G. Knepley   }
1044cd0c2139SMatthew G Knepley   if (label) {
1045*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetValueIS(label, &valueIS));
1046*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetLocalSize(valueIS, &numSP));
1047*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetIndices(valueIS, &values));
1048cd0c2139SMatthew G Knepley   }
1049cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1050cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1051cd0c2139SMatthew G Knepley 
1052cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
1053*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetStratumIS(label, dep, &splitIS[dep]));
105418c5995bSMatthew G. Knepley     if (splitIS[dep]) {
1055*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISGetLocalSize(splitIS[dep], &numSplitPoints[dep]));
1056*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISGetIndices(splitIS[dep], &splitPoints[dep]));
105718c5995bSMatthew G. Knepley     }
1058*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetStratumIS(label, shift2+dep, &unsplitIS[dep]));
105918c5995bSMatthew G. Knepley     if (unsplitIS[dep]) {
1060*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISGetLocalSize(unsplitIS[dep], &numUnsplitPoints[dep]));
1061*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISGetIndices(unsplitIS[dep], &unsplitPoints[dep]));
1062cd0c2139SMatthew G Knepley     }
1063cd0c2139SMatthew G Knepley   }
1064607ab7a9SMatthew G. Knepley   /* Calculate number of hybrid points */
106518c5995bSMatthew 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   */
10662582d50cSToby Isaac   for (d = 0; d <= depth; ++d) depthShift[2*d+1]      = numSplitPoints[d] + numHybridPoints[d];
1067*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexShiftPointSetUp_Internal(depth,depthShift));
106859eef20bSToby Isaac   /* the end of the points in this stratum that come before the new points:
106959eef20bSToby Isaac    * shifting pMaxNew[d] gets the new start of the next stratum, then count back the old hybrid points and the newly
107059eef20bSToby Isaac    * added points */
10712582d50cSToby Isaac   for (d = 0; d <= depth; ++d) pMaxNew[d]             = DMPlexShiftPoint_Internal(pMaxNew[d],depth,depthShift) - (numHybridPointsOld[d] + numSplitPoints[d] + numHybridPoints[d]);
1072*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexShiftSizes_Internal(dm, depthShift, sdm));
1073cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
1074cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1075cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1076cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
10772582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
10784c367dbcSMatthew G. Knepley       const PetscInt  splitp = p    + pMaxNew[dep];
1079cd0c2139SMatthew G Knepley       const PetscInt *support;
1080394c2f0fSMatthew G. Knepley       DMPolytopeType  ct;
10814c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, qn, qp, e;
1082cd0c2139SMatthew G Knepley 
1083*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetConeSize(dm, oldp, &coneSize));
1084*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexSetConeSize(sdm, splitp, coneSize));
1085*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetSupportSize(dm, oldp, &supportSize));
1086*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexSetSupportSize(sdm, splitp, supportSize));
1087*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetCellType(dm, oldp, &ct));
1088*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexSetCellType(sdm, splitp, ct));
1089cd0c2139SMatthew G Knepley       if (dep == depth-1) {
10904c367dbcSMatthew G. Knepley         const PetscInt hybcell = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
10914c367dbcSMatthew G. Knepley 
1092cd0c2139SMatthew G Knepley         /* Add cohesive cells, they are prisms */
1093*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetConeSize(sdm, hybcell, 2 + coneSize));
1094412e9a14SMatthew G. Knepley         switch (coneSize) {
1095*5f80ce2aSJacob Faibussowitsch           case 2: CHKERRQ(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_SEG_PRISM_TENSOR));break;
1096*5f80ce2aSJacob Faibussowitsch           case 3: CHKERRQ(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_TRI_PRISM_TENSOR));break;
1097*5f80ce2aSJacob Faibussowitsch           case 4: CHKERRQ(DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_QUAD_PRISM_TENSOR));break;
1098412e9a14SMatthew G. Knepley         }
1099cd0c2139SMatthew G Knepley       } else if (dep == 0) {
11004c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1101cd0c2139SMatthew G Knepley 
1102*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetSupport(dm, oldp, &support));
11034c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1104cd0c2139SMatthew G Knepley           PetscInt val;
1105cd0c2139SMatthew G Knepley 
1106*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelGetValue(label, support[e], &val));
11074c367dbcSMatthew G. Knepley           if (val == 1) ++qf;
11084c367dbcSMatthew G. Knepley           if ((val == 1) || (val ==  (shift + 1))) ++qn;
11094c367dbcSMatthew G. Knepley           if ((val == 1) || (val == -(shift + 1))) ++qp;
1110cd0c2139SMatthew G Knepley         }
11114c367dbcSMatthew G. Knepley         /* Split old vertex: Edges into original vertex and new cohesive edge */
1112*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetSupportSize(sdm, newp, qn+1));
11134c367dbcSMatthew G. Knepley         /* Split new vertex: Edges into split vertex and new cohesive edge */
1114*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetSupportSize(sdm, splitp, qp+1));
11154c367dbcSMatthew G. Knepley         /* Add hybrid edge */
1116*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetConeSize(sdm, hybedge, 2));
1117*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetSupportSize(sdm, hybedge, qf));
1118*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR));
1119cd0c2139SMatthew G Knepley       } else if (dep == dim-2) {
11204c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
11214c367dbcSMatthew G. Knepley 
1122*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetSupport(dm, oldp, &support));
11234c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1124cd0c2139SMatthew G Knepley           PetscInt val;
1125cd0c2139SMatthew G Knepley 
1126*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelGetValue(label, support[e], &val));
11274c367dbcSMatthew G. Knepley           if (val == dim-1) ++qf;
11284c367dbcSMatthew G. Knepley           if ((val == dim-1) || (val ==  (shift + dim-1))) ++qn;
11294c367dbcSMatthew G. Knepley           if ((val == dim-1) || (val == -(shift + dim-1))) ++qp;
1130cd0c2139SMatthew G Knepley         }
11314c367dbcSMatthew G. Knepley         /* Split old edge: Faces into original edge and cohesive face (positive side?) */
1132*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetSupportSize(sdm, newp, qn+1));
11334c367dbcSMatthew G. Knepley         /* Split new edge: Faces into split edge and cohesive face (negative side?) */
1134*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetSupportSize(sdm, splitp, qp+1));
11354c367dbcSMatthew G. Knepley         /* Add hybrid face */
1136*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetConeSize(sdm, hybface, 4));
1137*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetSupportSize(sdm, hybface, qf));
1138*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR));
1139cd0c2139SMatthew G Knepley       }
1140cd0c2139SMatthew G Knepley     }
1141cd0c2139SMatthew G Knepley   }
114218c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
114318c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
114418c5995bSMatthew G. Knepley       const PetscInt  oldp   = unsplitPoints[dep][p];
11452582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
114618c5995bSMatthew G. Knepley       const PetscInt *support;
1147da1dd7e4SMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, e, s;
114818c5995bSMatthew G. Knepley 
1149*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetConeSize(dm, oldp, &coneSize));
1150*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetSupportSize(dm, oldp, &supportSize));
1151*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetSupport(dm, oldp, &support));
115218c5995bSMatthew G. Knepley       if (dep == 0) {
115318c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
115418c5995bSMatthew G. Knepley 
115539254ff6SMatthew G. Knepley         /* Unsplit vertex: Edges into original vertex, split edges, and new cohesive edge twice */
115639254ff6SMatthew G. Knepley         for (s = 0, qf = 0; s < supportSize; ++s, ++qf) {
1157*5f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscFindInt(support[s], numSplitPoints[dep+1], splitPoints[dep+1], &e));
115839254ff6SMatthew G. Knepley           if (e >= 0) ++qf;
115939254ff6SMatthew G. Knepley         }
1160*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetSupportSize(sdm, newp, qf+2));
116118c5995bSMatthew G. Knepley         /* Add hybrid edge */
1162*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetConeSize(sdm, hybedge, 2));
1163e1757548SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1164e1757548SMatthew G. Knepley           PetscInt val;
1165e1757548SMatthew G. Knepley 
1166*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelGetValue(label, support[e], &val));
1167e1757548SMatthew G. Knepley           /* Split and unsplit edges produce hybrid faces */
1168da1dd7e4SMatthew G. Knepley           if (val == 1) ++qf;
1169da1dd7e4SMatthew G. Knepley           if (val == (shift2 + 1)) ++qf;
1170e1757548SMatthew G. Knepley         }
1171*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetSupportSize(sdm, hybedge, qf));
1172*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR));
117318c5995bSMatthew G. Knepley       } else if (dep == dim-2) {
117418c5995bSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
1175cd0c2139SMatthew G Knepley         PetscInt       val;
1176cd0c2139SMatthew G Knepley 
1177da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1178*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelGetValue(label, support[e], &val));
1179da1dd7e4SMatthew G. Knepley           if (val == dim-1) qf += 2;
1180da1dd7e4SMatthew G. Knepley           else              ++qf;
1181cd0c2139SMatthew G Knepley         }
118218c5995bSMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and cohesive face twice */
1183*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetSupportSize(sdm, newp, qf+2));
118418c5995bSMatthew G. Knepley         /* Add hybrid face */
1185da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1186*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelGetValue(label, support[e], &val));
1187da1dd7e4SMatthew G. Knepley           if (val == dim-1) ++qf;
1188da1dd7e4SMatthew G. Knepley         }
1189*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetConeSize(sdm, hybface, 4));
1190*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetSupportSize(sdm, hybface, qf));
1191*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR));
1192cd0c2139SMatthew G Knepley       }
1193cd0c2139SMatthew G Knepley     }
1194cd0c2139SMatthew G Knepley   }
1195cd0c2139SMatthew G Knepley   /* Step 4: Setup split DM */
1196*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetUp(sdm));
1197*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexShiftPoints_Internal(dm, depthShift, sdm));
1198*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetMaxSizes(sdm, &maxConeSizeNew, &maxSupportSizeNew));
1199*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc3(PetscMax(maxConeSize, maxConeSizeNew)*3,&coneNew,PetscMax(maxConeSize, maxConeSizeNew)*3,&coneONew,PetscMax(maxSupportSize, maxSupportSizeNew),&supportNew));
1200cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
1201cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1202cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1203cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
12042582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
12054c367dbcSMatthew G. Knepley       const PetscInt  splitp = p    + pMaxNew[dep];
1206cd0c2139SMatthew G Knepley       const PetscInt *cone, *support, *ornt;
1207b5a892a1SMatthew G. Knepley       DMPolytopeType  ct;
12084c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, q, qf, qn, qp, v, e, s;
1209cd0c2139SMatthew G Knepley 
1210*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetCellType(dm, oldp, &ct));
1211*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetConeSize(dm, oldp, &coneSize));
1212*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetCone(dm, oldp, &cone));
1213*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetConeOrientation(dm, oldp, &ornt));
1214*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetSupportSize(dm, oldp, &supportSize));
1215*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetSupport(dm, oldp, &support));
1216cd0c2139SMatthew G Knepley       if (dep == depth-1) {
121796a07cd0SMatthew G. Knepley         PetscBool       hasUnsplit = PETSC_FALSE;
12184c367dbcSMatthew G. Knepley         const PetscInt  hybcell    = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1219cd0c2139SMatthew G Knepley         const PetscInt *supportF;
1220cd0c2139SMatthew G Knepley 
1221cd0c2139SMatthew G Knepley         /* Split face:       copy in old face to new face to start */
1222*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetSupport(sdm, newp,  &supportF));
1223*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetSupport(sdm, splitp, supportF));
1224cd0c2139SMatthew G Knepley         /* Split old face:   old vertices/edges in cone so no change */
1225cd0c2139SMatthew G Knepley         /* Split new face:   new vertices/edges in cone */
1226cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
1227*5f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v));
122818c5995bSMatthew G. Knepley           if (v < 0) {
1229*5f80ce2aSJacob Faibussowitsch             CHKERRQ(PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v));
12302c71b3e2SJacob Faibussowitsch             PetscCheckFalse(v < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %d in split or unsplit points of depth %d", cone[q], dep-1);
12312582d50cSToby Isaac             coneNew[2+q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
123296a07cd0SMatthew G. Knepley             hasUnsplit   = PETSC_TRUE;
123318c5995bSMatthew G. Knepley           } else {
12344c367dbcSMatthew G. Knepley             coneNew[2+q] = v + pMaxNew[dep-1];
1235163235baSMatthew G. Knepley             if (dep > 1) {
1236163235baSMatthew G. Knepley               const PetscInt *econe;
1237163235baSMatthew G. Knepley               PetscInt        econeSize, r, vs, vu;
1238163235baSMatthew G. Knepley 
1239*5f80ce2aSJacob Faibussowitsch               CHKERRQ(DMPlexGetConeSize(dm, cone[q], &econeSize));
1240*5f80ce2aSJacob Faibussowitsch               CHKERRQ(DMPlexGetCone(dm, cone[q], &econe));
1241163235baSMatthew G. Knepley               for (r = 0; r < econeSize; ++r) {
1242*5f80ce2aSJacob Faibussowitsch                 CHKERRQ(PetscFindInt(econe[r], numSplitPoints[dep-2],   splitPoints[dep-2],   &vs));
1243*5f80ce2aSJacob Faibussowitsch                 CHKERRQ(PetscFindInt(econe[r], numUnsplitPoints[dep-2], unsplitPoints[dep-2], &vu));
1244163235baSMatthew G. Knepley                 if (vs >= 0) continue;
12452c71b3e2SJacob Faibussowitsch                 PetscCheckFalse(vu < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %d in split or unsplit points of depth %d", econe[r], dep-2);
1246163235baSMatthew G. Knepley                 hasUnsplit   = PETSC_TRUE;
1247163235baSMatthew G. Knepley               }
1248163235baSMatthew G. Knepley             }
1249cd0c2139SMatthew G Knepley           }
1250cd0c2139SMatthew G Knepley         }
1251*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetCone(sdm, splitp, &coneNew[2]));
1252*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetConeOrientation(sdm, splitp, ornt));
1253e537020bSMatthew G. Knepley         /* Face support */
1254cd0c2139SMatthew G Knepley         for (s = 0; s < supportSize; ++s) {
1255cd0c2139SMatthew G Knepley           PetscInt val;
1256cd0c2139SMatthew G Knepley 
1257*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelGetValue(label, support[s], &val));
1258cd0c2139SMatthew G Knepley           if (val < 0) {
1259cd0c2139SMatthew G Knepley             /* Split old face:   Replace negative side cell with cohesive cell */
1260*5f80ce2aSJacob Faibussowitsch              CHKERRQ(DMPlexInsertSupport(sdm, newp, s, hybcell));
1261cd0c2139SMatthew G Knepley           } else {
1262cd0c2139SMatthew G Knepley             /* Split new face:   Replace positive side cell with cohesive cell */
1263*5f80ce2aSJacob Faibussowitsch             CHKERRQ(DMPlexInsertSupport(sdm, splitp, s, hybcell));
1264e537020bSMatthew G. Knepley             /* Get orientation for cohesive face */
1265e537020bSMatthew G. Knepley             {
1266e537020bSMatthew G. Knepley               const PetscInt *ncone, *nconeO;
1267e537020bSMatthew G. Knepley               PetscInt        nconeSize, nc;
1268e537020bSMatthew G. Knepley 
1269*5f80ce2aSJacob Faibussowitsch               CHKERRQ(DMPlexGetConeSize(dm, support[s], &nconeSize));
1270*5f80ce2aSJacob Faibussowitsch               CHKERRQ(DMPlexGetCone(dm, support[s], &ncone));
1271*5f80ce2aSJacob Faibussowitsch               CHKERRQ(DMPlexGetConeOrientation(dm, support[s], &nconeO));
1272e537020bSMatthew G. Knepley               for (nc = 0; nc < nconeSize; ++nc) {
1273e537020bSMatthew G. Knepley                 if (ncone[nc] == oldp) {
1274e537020bSMatthew G. Knepley                   coneONew[0] = nconeO[nc];
1275e537020bSMatthew G. Knepley                   break;
1276cd0c2139SMatthew G Knepley                 }
1277cd0c2139SMatthew G Knepley               }
12782c71b3e2SJacob Faibussowitsch               PetscCheckFalse(nc >= nconeSize,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate face %d in neighboring cell %d", oldp, support[s]);
1279e537020bSMatthew G. Knepley             }
1280e537020bSMatthew G. Knepley           }
1281e537020bSMatthew G. Knepley         }
12824c367dbcSMatthew G. Knepley         /* Cohesive cell:    Old and new split face, then new cohesive faces */
1283b5a892a1SMatthew G. Knepley         const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, coneONew[0]);
1284b5a892a1SMatthew G. Knepley 
1285fd4b9f15SMatthew G. Knepley         coneNew[0]  = newp;   /* Extracted negative side orientation above */
12864c367dbcSMatthew G. Knepley         coneNew[1]  = splitp;
12874c367dbcSMatthew G. Knepley         coneONew[1] = coneONew[0];
1288e537020bSMatthew G. Knepley         for (q = 0; q < coneSize; ++q) {
1289412e9a14SMatthew G. Knepley           /* Hybrid faces must follow order from oriented end face */
1290b5a892a1SMatthew G. Knepley           const PetscInt qa = arr[q*2+0];
1291b5a892a1SMatthew G. Knepley           const PetscInt qo = arr[q*2+1];
1292b5a892a1SMatthew G. Knepley           DMPolytopeType ft = dep == 2 ? DM_POLYTOPE_SEGMENT : DM_POLYTOPE_POINT;
1293412e9a14SMatthew G. Knepley 
1294*5f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscFindInt(cone[qa], numSplitPoints[dep-1], splitPoints[dep-1], &v));
129518c5995bSMatthew G. Knepley           if (v < 0) {
1296*5f80ce2aSJacob Faibussowitsch             CHKERRQ(PetscFindInt(cone[qa], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v));
129718c5995bSMatthew G. Knepley             coneNew[2+q]  = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
129818c5995bSMatthew G. Knepley           } else {
129918c5995bSMatthew G. Knepley             coneNew[2+q]  = v + pMaxNew[dep] + numSplitPoints[dep];
130018c5995bSMatthew G. Knepley           }
1301b5a892a1SMatthew G. Knepley           coneONew[2+q] = DMPolytopeTypeComposeOrientation(ft, qo, ornt[qa]);
1302e537020bSMatthew G. Knepley         }
1303*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetCone(sdm, hybcell, coneNew));
1304*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetConeOrientation(sdm, hybcell, coneONew));
130596a07cd0SMatthew G. Knepley         /* Label the hybrid cells on the boundary of the split */
1306*5f80ce2aSJacob Faibussowitsch         if (hasUnsplit) CHKERRQ(DMLabelSetValue(label, -hybcell, dim));
1307cd0c2139SMatthew G Knepley       } else if (dep == 0) {
13084c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1309cd0c2139SMatthew G Knepley 
1310cd0c2139SMatthew G Knepley         /* Split old vertex: Edges in old split faces and new cohesive edge */
13114c367dbcSMatthew G. Knepley         for (e = 0, qn = 0; e < supportSize; ++e) {
1312cd0c2139SMatthew G Knepley           PetscInt val;
1313cd0c2139SMatthew G Knepley 
1314*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelGetValue(label, support[e], &val));
1315cd0c2139SMatthew G Knepley           if ((val == 1) || (val == (shift + 1))) {
13162582d50cSToby Isaac             supportNew[qn++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1317cd0c2139SMatthew G Knepley           }
1318cd0c2139SMatthew G Knepley         }
13194c367dbcSMatthew G. Knepley         supportNew[qn] = hybedge;
1320*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetSupport(sdm, newp, supportNew));
1321cd0c2139SMatthew G Knepley         /* Split new vertex: Edges in new split faces and new cohesive edge */
13224c367dbcSMatthew G. Knepley         for (e = 0, qp = 0; e < supportSize; ++e) {
1323cd0c2139SMatthew G Knepley           PetscInt val, edge;
1324cd0c2139SMatthew G Knepley 
1325*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelGetValue(label, support[e], &val));
1326cd0c2139SMatthew G Knepley           if (val == 1) {
1327*5f80ce2aSJacob Faibussowitsch             CHKERRQ(PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge));
13282c71b3e2SJacob Faibussowitsch             PetscCheckFalse(edge < 0,comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
13294c367dbcSMatthew G. Knepley             supportNew[qp++] = edge + pMaxNew[dep+1];
1330cd0c2139SMatthew G Knepley           } else if (val == -(shift + 1)) {
13312582d50cSToby Isaac             supportNew[qp++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1332cd0c2139SMatthew G Knepley           }
1333cd0c2139SMatthew G Knepley         }
13344c367dbcSMatthew G. Knepley         supportNew[qp] = hybedge;
1335*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetSupport(sdm, splitp, supportNew));
13364c367dbcSMatthew G. Knepley         /* Hybrid edge:    Old and new split vertex */
1337cd0c2139SMatthew G Knepley         coneNew[0] = newp;
1338cd0c2139SMatthew G Knepley         coneNew[1] = splitp;
1339*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetCone(sdm, hybedge, coneNew));
13404c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
13414c367dbcSMatthew G. Knepley           PetscInt val, edge;
13424c367dbcSMatthew G. Knepley 
1343*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelGetValue(label, support[e], &val));
13444c367dbcSMatthew G. Knepley           if (val == 1) {
1345*5f80ce2aSJacob Faibussowitsch             CHKERRQ(PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge));
13462c71b3e2SJacob Faibussowitsch             PetscCheckFalse(edge < 0,comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
13474c367dbcSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2];
13484c367dbcSMatthew G. Knepley           }
13494c367dbcSMatthew G. Knepley         }
1350*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetSupport(sdm, hybedge, supportNew));
1351cd0c2139SMatthew G Knepley       } else if (dep == dim-2) {
13524c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
13534c367dbcSMatthew G. Knepley 
1354cd0c2139SMatthew G Knepley         /* Split old edge:   old vertices in cone so no change */
1355cd0c2139SMatthew G Knepley         /* Split new edge:   new vertices in cone */
1356cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
1357*5f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v));
1358e1757548SMatthew G. Knepley           if (v < 0) {
1359*5f80ce2aSJacob Faibussowitsch             CHKERRQ(PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v));
13602c71b3e2SJacob Faibussowitsch             PetscCheckFalse(v < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %d in split or unsplit points of depth %d", cone[q], dep-1);
13612582d50cSToby Isaac             coneNew[q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
1362e1757548SMatthew G. Knepley           } else {
13634c367dbcSMatthew G. Knepley             coneNew[q] = v + pMaxNew[dep-1];
1364cd0c2139SMatthew G Knepley           }
1365e1757548SMatthew G. Knepley         }
1366*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetCone(sdm, splitp, coneNew));
1367cd0c2139SMatthew G Knepley         /* Split old edge: Faces in positive side cells and old split faces */
1368cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1369cd0c2139SMatthew G Knepley           PetscInt val;
1370cd0c2139SMatthew G Knepley 
1371*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelGetValue(label, support[e], &val));
13724c367dbcSMatthew G. Knepley           if (val == dim-1) {
13732582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
13744c367dbcSMatthew G. Knepley           } else if (val == (shift + dim-1)) {
13752582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1376cd0c2139SMatthew G Knepley           }
1377cd0c2139SMatthew G Knepley         }
1378b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1379*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetSupport(sdm, newp, supportNew));
1380cd0c2139SMatthew G Knepley         /* Split new edge: Faces in negative side cells and new split faces */
1381cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1382cd0c2139SMatthew G Knepley           PetscInt val, face;
1383cd0c2139SMatthew G Knepley 
1384*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelGetValue(label, support[e], &val));
1385cd0c2139SMatthew G Knepley           if (val == dim-1) {
1386*5f80ce2aSJacob Faibussowitsch             CHKERRQ(PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &face));
13872c71b3e2SJacob Faibussowitsch             PetscCheckFalse(face < 0,comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[e]);
13884c367dbcSMatthew G. Knepley             supportNew[q++] = face + pMaxNew[dep+1];
1389cd0c2139SMatthew G Knepley           } else if (val == -(shift + dim-1)) {
13902582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1391cd0c2139SMatthew G Knepley           }
1392cd0c2139SMatthew G Knepley         }
1393b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1394*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetSupport(sdm, splitp, supportNew));
13954c367dbcSMatthew G. Knepley         /* Hybrid face */
13964c367dbcSMatthew G. Knepley         coneNew[0] = newp;
13974c367dbcSMatthew G. Knepley         coneNew[1] = splitp;
13984c367dbcSMatthew G. Knepley         for (v = 0; v < coneSize; ++v) {
13994c367dbcSMatthew G. Knepley           PetscInt vertex;
1400*5f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscFindInt(cone[v], numSplitPoints[dep-1], splitPoints[dep-1], &vertex));
1401e1757548SMatthew G. Knepley           if (vertex < 0) {
1402*5f80ce2aSJacob Faibussowitsch             CHKERRQ(PetscFindInt(cone[v], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &vertex));
14032c71b3e2SJacob Faibussowitsch             PetscCheckFalse(vertex < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %d in split or unsplit points of depth %d", cone[v], dep-1);
1404e1757548SMatthew G. Knepley             coneNew[2+v] = vertex + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1405e1757548SMatthew G. Knepley           } else {
14064c367dbcSMatthew G. Knepley             coneNew[2+v] = vertex + pMaxNew[dep] + numSplitPoints[dep];
14074c367dbcSMatthew G. Knepley           }
1408e1757548SMatthew G. Knepley         }
1409*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetCone(sdm, hybface, coneNew));
14104c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
14114c367dbcSMatthew G. Knepley           PetscInt val, face;
14124c367dbcSMatthew G. Knepley 
1413*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelGetValue(label, support[e], &val));
14144c367dbcSMatthew G. Knepley           if (val == dim-1) {
1415*5f80ce2aSJacob Faibussowitsch             CHKERRQ(PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &face));
14162c71b3e2SJacob Faibussowitsch             PetscCheckFalse(face < 0,comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[e]);
14174c367dbcSMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+2] + numSplitPoints[dep+2];
14184c367dbcSMatthew G. Knepley           }
14194c367dbcSMatthew G. Knepley         }
1420*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetSupport(sdm, hybface, supportNew));
1421cd0c2139SMatthew G Knepley       }
1422cd0c2139SMatthew G Knepley     }
1423cd0c2139SMatthew G Knepley   }
142418c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
142518c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
142618c5995bSMatthew G. Knepley       const PetscInt  oldp   = unsplitPoints[dep][p];
14272582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
1428b5a892a1SMatthew G. Knepley       const PetscInt *cone, *support;
1429e1757548SMatthew G. Knepley       PetscInt        coneSize, supportSize, supportSizeNew, q, qf, e, f, s;
143018c5995bSMatthew G. Knepley 
1431*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetConeSize(dm, oldp, &coneSize));
1432*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetCone(dm, oldp, &cone));
1433*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetSupportSize(dm, oldp, &supportSize));
1434*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetSupport(dm, oldp, &support));
143518c5995bSMatthew G. Knepley       if (dep == 0) {
143618c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
143718c5995bSMatthew G. Knepley 
143818c5995bSMatthew G. Knepley         /* Unsplit vertex */
1439*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetSupportSize(sdm, newp, &supportSizeNew));
144018c5995bSMatthew G. Knepley         for (s = 0, q = 0; s < supportSize; ++s) {
14412582d50cSToby Isaac           supportNew[q++] = DMPlexShiftPoint_Internal(support[s], depth, depthShift) /*support[s] + depthOffset[dep+1]*/;
1442*5f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscFindInt(support[s], numSplitPoints[dep+1], splitPoints[dep+1], &e));
144318c5995bSMatthew G. Knepley           if (e >= 0) {
144418c5995bSMatthew G. Knepley             supportNew[q++] = e + pMaxNew[dep+1];
144518c5995bSMatthew G. Knepley           }
144618c5995bSMatthew G. Knepley         }
144718c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
144818c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
14492c71b3e2SJacob Faibussowitsch         PetscCheckFalse(q != supportSizeNew,comm, PETSC_ERR_ARG_WRONG, "Support size %d != %d for vertex %d", q, supportSizeNew, newp);
1450*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetSupport(sdm, newp, supportNew));
145118c5995bSMatthew G. Knepley         /* Hybrid edge */
145218c5995bSMatthew G. Knepley         coneNew[0] = newp;
145318c5995bSMatthew G. Knepley         coneNew[1] = newp;
1454*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetCone(sdm, hybedge, coneNew));
145518c5995bSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
145618c5995bSMatthew G. Knepley           PetscInt val, edge;
145718c5995bSMatthew G. Knepley 
1458*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelGetValue(label, support[e], &val));
145918c5995bSMatthew G. Knepley           if (val == 1) {
1460*5f80ce2aSJacob Faibussowitsch             CHKERRQ(PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge));
14612c71b3e2SJacob Faibussowitsch             PetscCheckFalse(edge < 0,comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
146218c5995bSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2];
1463e1757548SMatthew G. Knepley           } else if  (val ==  (shift2 + 1)) {
1464*5f80ce2aSJacob Faibussowitsch             CHKERRQ(PetscFindInt(support[e], numUnsplitPoints[dep+1], unsplitPoints[dep+1], &edge));
14652c71b3e2SJacob Faibussowitsch             PetscCheckFalse(edge < 0,comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a unsplit edge", support[e]);
1466e1757548SMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2] + numSplitPoints[dep+1];
146718c5995bSMatthew G. Knepley           }
146818c5995bSMatthew G. Knepley         }
1469*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetSupport(sdm, hybedge, supportNew));
1470e1757548SMatthew G. Knepley       } else if (dep == dim-2) {
1471e1757548SMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
1472e1757548SMatthew G. Knepley 
1473da1dd7e4SMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and hybrid face twice */
1474e1757548SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1475e1757548SMatthew G. Knepley           PetscInt val, face;
1476e1757548SMatthew G. Knepley 
1477*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelGetValue(label, support[f], &val));
1478e1757548SMatthew G. Knepley           if (val == dim-1) {
1479*5f80ce2aSJacob Faibussowitsch             CHKERRQ(PetscFindInt(support[f], numSplitPoints[dep+1], splitPoints[dep+1], &face));
14802c71b3e2SJacob Faibussowitsch             PetscCheckFalse(face < 0,comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[f]);
14812582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1482e1757548SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+1];
1483e1757548SMatthew G. Knepley           } else {
14842582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1485e1757548SMatthew G. Knepley           }
1486e1757548SMatthew G. Knepley         }
1487e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
1488e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
1489*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetSupportSize(sdm, newp, &supportSizeNew));
14902c71b3e2SJacob Faibussowitsch         PetscCheckFalse(qf != supportSizeNew,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Support size for unsplit edge %d is %d != %d", newp, qf, supportSizeNew);
1491*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetSupport(sdm, newp, supportNew));
1492e1757548SMatthew G. Knepley         /* Add hybrid face */
1493e1757548SMatthew G. Knepley         coneNew[0] = newp;
1494212cc919SMatthew G. Knepley         coneNew[1] = newp;
1495*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscFindInt(cone[0], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v));
14962c71b3e2SJacob Faibussowitsch         PetscCheckFalse(v < 0,comm, PETSC_ERR_ARG_WRONG, "Vertex %d is not an unsplit vertex", cone[0]);
1497212cc919SMatthew G. Knepley         coneNew[2] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1498*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscFindInt(cone[1], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v));
14992c71b3e2SJacob Faibussowitsch         PetscCheckFalse(v < 0,comm, PETSC_ERR_ARG_WRONG, "Vertex %d is not an unsplit vertex", cone[1]);
1500e1757548SMatthew G. Knepley         coneNew[3] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1501*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetCone(sdm, hybface, coneNew));
1502da1dd7e4SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1503da1dd7e4SMatthew G. Knepley           PetscInt val, face;
1504da1dd7e4SMatthew G. Knepley 
1505*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelGetValue(label, support[f], &val));
1506da1dd7e4SMatthew G. Knepley           if (val == dim-1) {
1507*5f80ce2aSJacob Faibussowitsch             CHKERRQ(PetscFindInt(support[f], numSplitPoints[dep+1], splitPoints[dep+1], &face));
1508da1dd7e4SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+2] + numSplitPoints[dep+2];
1509da1dd7e4SMatthew G. Knepley           }
1510da1dd7e4SMatthew G. Knepley         }
1511*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetSupportSize(sdm, hybface, &supportSizeNew));
15122c71b3e2SJacob Faibussowitsch         PetscCheckFalse(qf != supportSizeNew,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Support size for hybrid face %d is %d != %d", hybface, qf, supportSizeNew);
1513*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetSupport(sdm, hybface, supportNew));
1514cd0c2139SMatthew G Knepley       }
1515cd0c2139SMatthew G Knepley     }
1516cd0c2139SMatthew G Knepley   }
1517cd0c2139SMatthew G Knepley   /* Step 6b: Replace split points in negative side cones */
1518cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1519cd0c2139SMatthew G Knepley     PetscInt        dep = values[sp];
1520cd0c2139SMatthew G Knepley     IS              pIS;
1521cd0c2139SMatthew G Knepley     PetscInt        numPoints;
1522cd0c2139SMatthew G Knepley     const PetscInt *points;
1523cd0c2139SMatthew G Knepley 
1524cd0c2139SMatthew G Knepley     if (dep >= 0) continue;
1525*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetStratumIS(label, dep, &pIS));
1526cd0c2139SMatthew G Knepley     if (!pIS) continue;
1527cd0c2139SMatthew G Knepley     dep  = -dep - shift;
1528*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetLocalSize(pIS, &numPoints));
1529*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetIndices(pIS, &points));
1530cd0c2139SMatthew G Knepley     for (p = 0; p < numPoints; ++p) {
1531cd0c2139SMatthew G Knepley       const PetscInt  oldp = points[p];
15322582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*depthOffset[dep] + oldp*/;
1533cd0c2139SMatthew G Knepley       const PetscInt *cone;
1534cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
153550cf782dSMatthew G. Knepley       /* PetscBool       replaced = PETSC_FALSE; */
1536cd0c2139SMatthew G Knepley 
1537cd0c2139SMatthew G Knepley       /* Negative edge: replace split vertex */
1538cd0c2139SMatthew G Knepley       /* Negative cell: replace split face */
1539*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetConeSize(sdm, newp, &coneSize));
1540*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetCone(sdm, newp, &cone));
1541cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
1542e38fbfedSToby Isaac         const PetscInt coldp = DMPlexShiftPointInverse_Internal(cone[c],depth,depthShift);
1543cd0c2139SMatthew G Knepley         PetscInt       csplitp, cp, val;
1544cd0c2139SMatthew G Knepley 
1545*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMLabelGetValue(label, coldp, &val));
1546cd0c2139SMatthew G Knepley         if (val == dep-1) {
1547*5f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscFindInt(coldp, numSplitPoints[dep-1], splitPoints[dep-1], &cp));
15482c71b3e2SJacob Faibussowitsch           PetscCheckFalse(cp < 0,comm, PETSC_ERR_ARG_WRONG, "Point %d is not a split point of dimension %d", oldp, dep-1);
1549cd0c2139SMatthew G Knepley           csplitp  = pMaxNew[dep-1] + cp;
1550*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMPlexInsertCone(sdm, newp, c, csplitp));
155150cf782dSMatthew G. Knepley           /* replaced = PETSC_TRUE; */
1552cd0c2139SMatthew G Knepley         }
1553cd0c2139SMatthew G Knepley       }
15544a189a86SMatthew G. Knepley       /* Cells with only a vertex or edge on the submesh have no replacement */
15552c71b3e2SJacob Faibussowitsch       /* PetscCheckFalse(!replaced,comm, PETSC_ERR_ARG_WRONG, "The cone of point %d does not contain split points", oldp); */
1556cd0c2139SMatthew G Knepley     }
1557*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISRestoreIndices(pIS, &points));
1558*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&pIS));
1559cd0c2139SMatthew G Knepley   }
1560fa8e8ae5SToby Isaac   /* Step 7: Coordinates */
1561*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexShiftCoordinates_Internal(dm, depthShift, sdm));
1562*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinateSection(sdm, &coordSection));
1563*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinatesLocal(sdm, &coordinates));
1564*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetArray(coordinates, &coords));
1565cd0c2139SMatthew G Knepley   for (v = 0; v < (numSplitPoints ? numSplitPoints[0] : 0); ++v) {
15662582d50cSToby Isaac     const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[0][v], depth, depthShift) /*depthOffset[0] + splitPoints[0][v]*/;
1567cd0c2139SMatthew G Knepley     const PetscInt splitp = pMaxNew[0] + v;
1568cd0c2139SMatthew G Knepley     PetscInt       dof, off, soff, d;
1569cd0c2139SMatthew G Knepley 
1570*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetDof(coordSection, newp, &dof));
1571*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetOffset(coordSection, newp, &off));
1572*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetOffset(coordSection, splitp, &soff));
1573cd0c2139SMatthew G Knepley     for (d = 0; d < dof; ++d) coords[soff+d] = coords[off+d];
1574cd0c2139SMatthew G Knepley   }
1575*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecRestoreArray(coordinates, &coords));
1576fa8e8ae5SToby Isaac   /* Step 8: SF, if I can figure this out we can split the mesh in parallel */
1577*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexShiftSF_Internal(dm, depthShift, sdm));
1578fa8e8ae5SToby Isaac   /* Step 9: Labels */
1579*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexShiftLabels_Internal(dm, depthShift, sdm));
1580*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexCreateVTKLabel_Internal(dm, PETSC_FALSE, sdm));
1581*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetNumLabels(sdm, &numLabels));
1582cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1583cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
15842582d50cSToby Isaac       const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[dep][p], depth, depthShift) /*depthOffset[dep] + splitPoints[dep][p]*/;
1585cd0c2139SMatthew G Knepley       const PetscInt splitp = pMaxNew[dep] + p;
1586cd0c2139SMatthew G Knepley       PetscInt       l;
1587cd0c2139SMatthew G Knepley 
15887db7e0a7SMatthew G. Knepley       if (splitLabel) {
15897db7e0a7SMatthew G. Knepley         const PetscInt val = 100 + dep;
15907db7e0a7SMatthew G. Knepley 
1591*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMLabelSetValue(splitLabel, newp,    val));
1592*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMLabelSetValue(splitLabel, splitp, -val));
15937db7e0a7SMatthew G. Knepley       }
1594cd0c2139SMatthew G Knepley       for (l = 0; l < numLabels; ++l) {
1595cd0c2139SMatthew G Knepley         DMLabel     mlabel;
1596cd0c2139SMatthew G Knepley         const char *lname;
1597cd0c2139SMatthew G Knepley         PetscInt    val;
15989a356370SMatthew G. Knepley         PetscBool   isDepth;
1599cd0c2139SMatthew G Knepley 
1600*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMGetLabelName(sdm, l, &lname));
1601*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscStrcmp(lname, "depth", &isDepth));
16029a356370SMatthew G. Knepley         if (isDepth) continue;
1603*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMGetLabel(sdm, lname, &mlabel));
1604*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMLabelGetValue(mlabel, newp, &val));
1605cd0c2139SMatthew G Knepley         if (val >= 0) {
1606*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelSetValue(mlabel, splitp, val));
1607cd0c2139SMatthew G Knepley         }
1608cd0c2139SMatthew G Knepley       }
1609cd0c2139SMatthew G Knepley     }
1610cd0c2139SMatthew G Knepley   }
1611cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1612cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1613cd0c2139SMatthew G Knepley 
1614cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
1615*5f80ce2aSJacob Faibussowitsch     if (splitIS[dep]) CHKERRQ(ISRestoreIndices(splitIS[dep], &splitPoints[dep]));
1616*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&splitIS[dep]));
1617*5f80ce2aSJacob Faibussowitsch     if (unsplitIS[dep]) CHKERRQ(ISRestoreIndices(unsplitIS[dep], &unsplitPoints[dep]));
1618*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&unsplitIS[dep]));
1619cd0c2139SMatthew G Knepley   }
1620cd0c2139SMatthew G Knepley   if (label) {
1621*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISRestoreIndices(valueIS, &values));
1622*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&valueIS));
1623cd0c2139SMatthew G Knepley   }
16240d4d4d06SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
1625*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetDepthStratum(sdm, d, NULL, &pEnd));
162636dbac82SMatthew G. Knepley     pMaxNew[d] = pEnd - numHybridPoints[d] - numHybridPointsOld[d];
16270d4d4d06SMatthew G. Knepley   }
1628*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree3(coneNew, coneONew, supportNew));
1629*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree5(depthMax, depthEnd, depthShift, pMaxNew, numHybridPointsOld));
1630*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree7(splitIS, unsplitIS, numSplitPoints, numUnsplitPoints, numHybridPoints, splitPoints, unsplitPoints));
1631cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1632cd0c2139SMatthew G Knepley }
1633cd0c2139SMatthew G Knepley 
1634cd0c2139SMatthew G Knepley /*@C
1635cd0c2139SMatthew G Knepley   DMPlexConstructCohesiveCells - Construct cohesive cells which split the face along an internal interface
1636cd0c2139SMatthew G Knepley 
1637cd0c2139SMatthew G Knepley   Collective on dm
1638cd0c2139SMatthew G Knepley 
1639cd0c2139SMatthew G Knepley   Input Parameters:
1640cd0c2139SMatthew G Knepley + dm - The original DM
164153156dfcSMatthew G. Knepley - label - The label specifying the boundary faces (this could be auto-generated)
1642cd0c2139SMatthew G Knepley 
1643cd0c2139SMatthew G Knepley   Output Parameters:
16447db7e0a7SMatthew G. Knepley + splitLabel - The label containing the split points, or NULL if no output is desired
1645cd0c2139SMatthew G Knepley - dmSplit - The new DM
1646cd0c2139SMatthew G Knepley 
1647cd0c2139SMatthew G Knepley   Level: developer
1648cd0c2139SMatthew G Knepley 
16492be2b188SMatthew G Knepley .seealso: DMCreate(), DMPlexLabelCohesiveComplete()
1650cd0c2139SMatthew G Knepley @*/
16517db7e0a7SMatthew G. Knepley PetscErrorCode DMPlexConstructCohesiveCells(DM dm, DMLabel label, DMLabel splitLabel, DM *dmSplit)
1652cd0c2139SMatthew G Knepley {
1653cd0c2139SMatthew G Knepley   DM             sdm;
1654cd0c2139SMatthew G Knepley   PetscInt       dim;
1655cd0c2139SMatthew G Knepley 
1656cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1657cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1658064a246eSJacob Faibussowitsch   PetscValidPointer(dmSplit, 4);
1659*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMCreate(PetscObjectComm((PetscObject)dm), &sdm));
1660*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetType(sdm, DMPLEX));
1661*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetDimension(dm, &dim));
1662*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetDimension(sdm, dim));
1663cd0c2139SMatthew G Knepley   switch (dim) {
1664cd0c2139SMatthew G Knepley   case 2:
1665cd0c2139SMatthew G Knepley   case 3:
1666*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexConstructCohesiveCells_Internal(dm, label, splitLabel, sdm));
1667cd0c2139SMatthew G Knepley     break;
1668cd0c2139SMatthew G Knepley   default:
166998921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct cohesive cells for dimension %d", dim);
1670cd0c2139SMatthew G Knepley   }
1671cd0c2139SMatthew G Knepley   *dmSplit = sdm;
1672cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1673cd0c2139SMatthew G Knepley }
1674cd0c2139SMatthew G Knepley 
16750f66a230SMatthew G. Knepley /* Returns the side of the surface for a given cell with a face on the surface */
16760f66a230SMatthew G. Knepley static PetscErrorCode GetSurfaceSide_Static(DM dm, DM subdm, PetscInt numSubpoints, const PetscInt *subpoints, PetscInt cell, PetscInt face, PetscBool *pos)
16770f66a230SMatthew G. Knepley {
16780f66a230SMatthew G. Knepley   const PetscInt *cone, *ornt;
16790f66a230SMatthew G. Knepley   PetscInt        dim, coneSize, c;
16800f66a230SMatthew G. Knepley 
16810f66a230SMatthew G. Knepley   PetscFunctionBegin;
16820f66a230SMatthew G. Knepley   *pos = PETSC_TRUE;
1683*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetDimension(dm, &dim));
1684*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetConeSize(dm, cell, &coneSize));
1685*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetCone(dm, cell, &cone));
1686*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetConeOrientation(dm, cell, &ornt));
16870f66a230SMatthew G. Knepley   for (c = 0; c < coneSize; ++c) {
16880f66a230SMatthew G. Knepley     if (cone[c] == face) {
16890f66a230SMatthew G. Knepley       PetscInt o = ornt[c];
16900f66a230SMatthew G. Knepley 
16910f66a230SMatthew G. Knepley       if (subdm) {
16920f66a230SMatthew G. Knepley         const PetscInt *subcone, *subornt;
16930f66a230SMatthew G. Knepley         PetscInt        subpoint, subface, subconeSize, sc;
16940f66a230SMatthew G. Knepley 
1695*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscFindInt(cell, numSubpoints, subpoints, &subpoint));
1696*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscFindInt(face, numSubpoints, subpoints, &subface));
1697*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetConeSize(subdm, subpoint, &subconeSize));
1698*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetCone(subdm, subpoint, &subcone));
1699*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetConeOrientation(subdm, subpoint, &subornt));
17000f66a230SMatthew G. Knepley         for (sc = 0; sc < subconeSize; ++sc) {
17010f66a230SMatthew G. Knepley           if (subcone[sc] == subface) {
17020f66a230SMatthew G. Knepley             o = subornt[0];
17030f66a230SMatthew G. Knepley             break;
17040f66a230SMatthew G. Knepley           }
17050f66a230SMatthew G. Knepley         }
17062c71b3e2SJacob Faibussowitsch         PetscCheckFalse(sc >= subconeSize,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find subpoint %d (%d) in cone for subpoint %d (%d)", subface, face, subpoint, cell);
17070f66a230SMatthew G. Knepley       }
17080f66a230SMatthew G. Knepley       if (o >= 0) *pos = PETSC_TRUE;
17090f66a230SMatthew G. Knepley       else        *pos = PETSC_FALSE;
17100f66a230SMatthew G. Knepley       break;
17110f66a230SMatthew G. Knepley     }
17120f66a230SMatthew G. Knepley   }
17132c71b3e2SJacob Faibussowitsch   PetscCheckFalse(c == coneSize,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Cell %d in split face %d support does not have it in the cone", cell, face);
17140f66a230SMatthew G. Knepley   PetscFunctionReturn(0);
17150f66a230SMatthew G. Knepley }
17160f66a230SMatthew G. Knepley 
1717cd0c2139SMatthew G Knepley /*@
17180f66a230SMatthew G. Knepley   DMPlexLabelCohesiveComplete - Starting with a label marking points on an internal surface, we add all other mesh pieces
1719cd0c2139SMatthew G Knepley   to complete the surface
1720cd0c2139SMatthew G Knepley 
1721cd0c2139SMatthew G Knepley   Input Parameters:
1722cd0c2139SMatthew G Knepley + dm     - The DM
17230f66a230SMatthew G. Knepley . label  - A DMLabel marking the surface
17240f66a230SMatthew G. Knepley . blabel - A DMLabel marking the vertices on the boundary which will not be duplicated, or NULL to find them automatically
1725bb55d314SMatthew G. Knepley . flip   - Flag to flip the submesh normal and replace points on the other side
172647946fd8SMatthew G. Knepley - subdm  - The subDM associated with the label, or NULL
1727cd0c2139SMatthew G Knepley 
1728cd0c2139SMatthew G Knepley   Output Parameter:
1729cd0c2139SMatthew G Knepley . label - A DMLabel marking all surface points
1730cd0c2139SMatthew G Knepley 
17310f66a230SMatthew G. Knepley   Note: The vertices in blabel are called "unsplit" in the terminology from hybrid cell creation.
17320f66a230SMatthew G. Knepley 
1733cd0c2139SMatthew G Knepley   Level: developer
1734cd0c2139SMatthew G Knepley 
17352be2b188SMatthew G Knepley .seealso: DMPlexConstructCohesiveCells(), DMPlexLabelComplete()
1736cd0c2139SMatthew G Knepley @*/
17370f66a230SMatthew G. Knepley PetscErrorCode DMPlexLabelCohesiveComplete(DM dm, DMLabel label, DMLabel blabel, PetscBool flip, DM subdm)
1738cd0c2139SMatthew G Knepley {
1739d90583fdSMatthew G. Knepley   DMLabel         depthLabel;
17408630cb1aSMatthew G. Knepley   IS              dimIS, subpointIS = NULL, facePosIS, faceNegIS, crossEdgeIS = NULL;
174147946fd8SMatthew G. Knepley   const PetscInt *points, *subpoints;
1742bb55d314SMatthew G. Knepley   const PetscInt  rev   = flip ? -1 : 1;
1743412e9a14SMatthew G. Knepley   PetscInt        shift = 100, shift2 = 200, dim, depth, dep, cStart, cEnd, vStart, vEnd, numPoints, numSubpoints, p, val;
1744cd0c2139SMatthew G Knepley 
1745cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1746*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetDepth(dm, &depth));
1747*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetDimension(dm, &dim));
1748*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetDepthLabel(dm, &depthLabel));
174947946fd8SMatthew G. Knepley   if (subdm) {
1750*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetSubpointIS(subdm, &subpointIS));
175147946fd8SMatthew G. Knepley     if (subpointIS) {
1752*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISGetLocalSize(subpointIS, &numSubpoints));
1753*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISGetIndices(subpointIS, &subpoints));
175447946fd8SMatthew G. Knepley     }
175547946fd8SMatthew G. Knepley   }
1756d7c8f101SMatthew G. Knepley   /* Mark cell on the fault, and its faces which touch the fault: cell orientation for face gives the side of the fault */
1757*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelGetStratumIS(label, dim-1, &dimIS));
175897d8846cSMatthew Knepley   if (!dimIS) PetscFunctionReturn(0);
1759*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetLocalSize(dimIS, &numPoints));
1760*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetIndices(dimIS, &points));
1761d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault faces */
1762cd0c2139SMatthew G Knepley     const PetscInt *support;
1763cd0c2139SMatthew G Knepley     PetscInt        supportSize, s;
1764cd0c2139SMatthew G Knepley 
1765*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetSupportSize(dm, points[p], &supportSize));
1766c4419245SMatthew G. Knepley #if 0
1767c4419245SMatthew G. Knepley     if (supportSize != 2) {
1768c4419245SMatthew G. Knepley       const PetscInt *lp;
1769c4419245SMatthew G. Knepley       PetscInt        Nlp, pind;
1770c4419245SMatthew G. Knepley 
1771c4419245SMatthew G. Knepley       /* Check that for a cell with a single support face, that face is in the SF */
1772c4419245SMatthew G. Knepley       /*   THis check only works for the remote side. We would need root side information */
1773*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSFGetGraph(dm->sf, NULL, &Nlp, &lp, NULL));
1774*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFindInt(points[p], Nlp, lp, &pind));
17752c71b3e2SJacob Faibussowitsch       PetscCheckFalse(pind < 0,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Split face %d has %d != 2 supports, and the face is not shared with another process", points[p], supportSize);
1776c4419245SMatthew G. Knepley     }
1777c4419245SMatthew G. Knepley #endif
1778*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetSupport(dm, points[p], &support));
1779cd0c2139SMatthew G Knepley     for (s = 0; s < supportSize; ++s) {
17800f66a230SMatthew G. Knepley       const PetscInt *cone;
1781cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
17820f66a230SMatthew G. Knepley       PetscBool       pos;
1783cd0c2139SMatthew G Knepley 
1784*5f80ce2aSJacob Faibussowitsch       CHKERRQ(GetSurfaceSide_Static(dm, subdm, numSubpoints, subpoints, support[s], points[p], &pos));
1785*5f80ce2aSJacob Faibussowitsch       if (pos) CHKERRQ(DMLabelSetValue(label, support[s],  rev*(shift+dim)));
1786*5f80ce2aSJacob Faibussowitsch       else     CHKERRQ(DMLabelSetValue(label, support[s], -rev*(shift+dim)));
17870f66a230SMatthew G. Knepley       if (rev < 0) pos = !pos ? PETSC_TRUE : PETSC_FALSE;
17880f66a230SMatthew G. Knepley       /* Put faces touching the fault in the label */
1789*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetConeSize(dm, support[s], &coneSize));
1790*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetCone(dm, support[s], &cone));
1791cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
1792cd0c2139SMatthew G Knepley         const PetscInt point = cone[c];
1793cd0c2139SMatthew G Knepley 
1794*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMLabelGetValue(label, point, &val));
1795cd0c2139SMatthew G Knepley         if (val == -1) {
1796cd0c2139SMatthew G Knepley           PetscInt *closure = NULL;
1797cd0c2139SMatthew G Knepley           PetscInt  closureSize, cl;
1798cd0c2139SMatthew G Knepley 
1799*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
1800cd0c2139SMatthew G Knepley           for (cl = 0; cl < closureSize*2; cl += 2) {
1801cd0c2139SMatthew G Knepley             const PetscInt clp  = closure[cl];
1802a0541d8aSMatthew G. Knepley             PetscInt       bval = -1;
1803cd0c2139SMatthew G Knepley 
1804*5f80ce2aSJacob Faibussowitsch             CHKERRQ(DMLabelGetValue(label, clp, &val));
1805*5f80ce2aSJacob Faibussowitsch             if (blabel) CHKERRQ(DMLabelGetValue(blabel, clp, &bval));
1806a0541d8aSMatthew G. Knepley             if ((val >= 0) && (val < dim-1) && (bval < 0)) {
1807*5f80ce2aSJacob Faibussowitsch               CHKERRQ(DMLabelSetValue(label, point, pos == PETSC_TRUE ? shift+dim-1 : -(shift+dim-1)));
1808cd0c2139SMatthew G Knepley               break;
1809cd0c2139SMatthew G Knepley             }
1810cd0c2139SMatthew G Knepley           }
1811*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
1812cd0c2139SMatthew G Knepley         }
1813cd0c2139SMatthew G Knepley       }
1814cd0c2139SMatthew G Knepley     }
1815cd0c2139SMatthew G Knepley   }
1816*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISRestoreIndices(dimIS, &points));
1817*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&dimIS));
1818*5f80ce2aSJacob Faibussowitsch   if (subpointIS) CHKERRQ(ISRestoreIndices(subpointIS, &subpoints));
1819a0541d8aSMatthew G. Knepley   /* Mark boundary points as unsplit */
182086200784SMatthew G. Knepley   if (blabel) {
1821*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetStratumIS(blabel, 1, &dimIS));
1822*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetLocalSize(dimIS, &numPoints));
1823*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetIndices(dimIS, &points));
1824a0541d8aSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
1825a0541d8aSMatthew G. Knepley       const PetscInt point = points[p];
1826a0541d8aSMatthew G. Knepley       PetscInt       val, bval;
1827a0541d8aSMatthew G. Knepley 
1828*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelGetValue(blabel, point, &bval));
1829a0541d8aSMatthew G. Knepley       if (bval >= 0) {
1830*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMLabelGetValue(label, point, &val));
1831f7019248SMatthew G. Knepley         if ((val < 0) || (val > dim)) {
1832f7019248SMatthew G. Knepley           /* This could be a point added from splitting a vertex on an adjacent fault, otherwise its just wrong */
1833*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelClearValue(blabel, point, bval));
1834f7019248SMatthew G. Knepley         }
1835f7019248SMatthew G. Knepley       }
1836f7019248SMatthew G. Knepley     }
1837f7019248SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
1838f7019248SMatthew G. Knepley       const PetscInt point = points[p];
1839f7019248SMatthew G. Knepley       PetscInt       val, bval;
1840f7019248SMatthew G. Knepley 
1841*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelGetValue(blabel, point, &bval));
1842f7019248SMatthew G. Knepley       if (bval >= 0) {
184386200784SMatthew G. Knepley         const PetscInt *cone,    *support;
184486200784SMatthew G. Knepley         PetscInt        coneSize, supportSize, s, valA, valB, valE;
184586200784SMatthew G. Knepley 
1846a0541d8aSMatthew G. Knepley         /* Mark as unsplit */
1847*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMLabelGetValue(label, point, &val));
18482c71b3e2SJacob Faibussowitsch         PetscCheckFalse((val < 0) || (val > dim),PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %d has label value %d, should be part of the fault", point, val);
1849*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMLabelClearValue(label, point, val));
1850*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMLabelSetValue(label, point, shift2+val));
18512c06a818SMatthew G. Knepley         /* Check for cross-edge
18522c06a818SMatthew G. Knepley              A cross-edge has endpoints which are both on the boundary of the surface, but the edge itself is not. */
185386200784SMatthew G. Knepley         if (val != 0) continue;
1854*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetSupport(dm, point, &support));
1855*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetSupportSize(dm, point, &supportSize));
185686200784SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
1857*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMPlexGetCone(dm, support[s], &cone));
1858*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMPlexGetConeSize(dm, support[s], &coneSize));
18592c71b3e2SJacob Faibussowitsch           PetscCheckFalse(coneSize != 2,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Edge %D has %D vertices != 2", support[s], coneSize);
1860*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelGetValue(blabel, cone[0], &valA));
1861*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelGetValue(blabel, cone[1], &valB));
1862*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelGetValue(blabel, support[s], &valE));
1863*5f80ce2aSJacob Faibussowitsch           if ((valE < 0) && (valA >= 0) && (valB >= 0) && (cone[0] != cone[1])) CHKERRQ(DMLabelSetValue(blabel, support[s], 2));
186486200784SMatthew G. Knepley         }
1865a0541d8aSMatthew G. Knepley       }
1866a0541d8aSMatthew G. Knepley     }
1867*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISRestoreIndices(dimIS, &points));
1868*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&dimIS));
1869a0541d8aSMatthew G. Knepley   }
1870cd0c2139SMatthew G Knepley   /* Search for other cells/faces/edges connected to the fault by a vertex */
1871*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
1872*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd));
1873*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelGetStratumIS(label, 0, &dimIS));
18747d2fefb6SMatthew G. Knepley   /* TODO Why are we including cross edges here? Shouldn't they be in the star of boundary vertices? */
1875*5f80ce2aSJacob Faibussowitsch   if (blabel) CHKERRQ(DMLabelGetStratumIS(blabel, 2, &crossEdgeIS));
187686200784SMatthew G. Knepley   if (dimIS && crossEdgeIS) {
187786200784SMatthew G. Knepley     IS vertIS = dimIS;
187886200784SMatthew G. Knepley 
1879*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISExpand(vertIS, crossEdgeIS, &dimIS));
1880*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&crossEdgeIS));
1881*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&vertIS));
188286200784SMatthew G. Knepley   }
1883dc86033cSMatthew G. Knepley   if (!dimIS) {
1884dc86033cSMatthew G. Knepley     PetscFunctionReturn(0);
1885dc86033cSMatthew G. Knepley   }
1886*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetLocalSize(dimIS, &numPoints));
1887*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetIndices(dimIS, &points));
1888d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault vertices */
1889cd0c2139SMatthew G Knepley     PetscInt *star = NULL;
189086200784SMatthew G. Knepley     PetscInt  starSize, s;
1891cd0c2139SMatthew G Knepley     PetscInt  again = 1;  /* 0: Finished 1: Keep iterating after a change 2: No change */
1892cd0c2139SMatthew G Knepley 
1893d4622b2cSMatthew G. Knepley     /* All points connected to the fault are inside a cell, so at the top level we will only check cells */
1894*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &starSize, &star));
1895cd0c2139SMatthew G Knepley     while (again) {
18962c71b3e2SJacob Faibussowitsch       PetscCheckFalse(again > 1,PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Could not classify all cells connected to the fault");
1897cd0c2139SMatthew G Knepley       again = 0;
1898cd0c2139SMatthew G Knepley       for (s = 0; s < starSize*2; s += 2) {
1899cd0c2139SMatthew G Knepley         const PetscInt  point = star[s];
1900cd0c2139SMatthew G Knepley         const PetscInt *cone;
1901cd0c2139SMatthew G Knepley         PetscInt        coneSize, c;
1902cd0c2139SMatthew G Knepley 
1903412e9a14SMatthew G. Knepley         if ((point < cStart) || (point >= cEnd)) continue;
1904*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMLabelGetValue(label, point, &val));
1905cd0c2139SMatthew G Knepley         if (val != -1) continue;
1906d4622b2cSMatthew G. Knepley         again = again == 1 ? 1 : 2;
1907*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetConeSize(dm, point, &coneSize));
1908*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetCone(dm, point, &cone));
1909cd0c2139SMatthew G Knepley         for (c = 0; c < coneSize; ++c) {
1910*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelGetValue(label, cone[c], &val));
1911cd0c2139SMatthew G Knepley           if (val != -1) {
1912d7c8f101SMatthew G. Knepley             const PetscInt *ccone;
1913166d9d0cSMatthew G. Knepley             PetscInt        cconeSize, cc, side;
1914d7c8f101SMatthew G. Knepley 
19152c71b3e2SJacob Faibussowitsch             PetscCheckFalse(PetscAbs(val) < shift,PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %d on cell %d has an invalid label %d", cone[c], point, val);
191637a6de01SMatthew G. Knepley             if (val > 0) side =  1;
191737a6de01SMatthew G. Knepley             else         side = -1;
1918*5f80ce2aSJacob Faibussowitsch             CHKERRQ(DMLabelSetValue(label, point, side*(shift+dim)));
1919d7c8f101SMatthew G. Knepley             /* Mark cell faces which touch the fault */
1920*5f80ce2aSJacob Faibussowitsch             CHKERRQ(DMPlexGetConeSize(dm, point, &cconeSize));
1921*5f80ce2aSJacob Faibussowitsch             CHKERRQ(DMPlexGetCone(dm, point, &ccone));
1922d7c8f101SMatthew G. Knepley             for (cc = 0; cc < cconeSize; ++cc) {
1923d7c8f101SMatthew G. Knepley               PetscInt *closure = NULL;
1924d7c8f101SMatthew G. Knepley               PetscInt  closureSize, cl;
1925d7c8f101SMatthew G. Knepley 
1926*5f80ce2aSJacob Faibussowitsch               CHKERRQ(DMLabelGetValue(label, ccone[cc], &val));
1927166d9d0cSMatthew G. Knepley               if (val != -1) continue;
1928*5f80ce2aSJacob Faibussowitsch               CHKERRQ(DMPlexGetTransitiveClosure(dm, ccone[cc], PETSC_TRUE, &closureSize, &closure));
1929d4622b2cSMatthew G. Knepley               for (cl = 0; cl < closureSize*2; cl += 2) {
1930d7c8f101SMatthew G. Knepley                 const PetscInt clp = closure[cl];
1931d7c8f101SMatthew G. Knepley 
1932*5f80ce2aSJacob Faibussowitsch                 CHKERRQ(DMLabelGetValue(label, clp, &val));
1933d7c8f101SMatthew G. Knepley                 if (val == -1) continue;
1934*5f80ce2aSJacob Faibussowitsch                 CHKERRQ(DMLabelSetValue(label, ccone[cc], side*(shift+dim-1)));
193537a6de01SMatthew G. Knepley                 break;
193637a6de01SMatthew G. Knepley               }
1937*5f80ce2aSJacob Faibussowitsch               CHKERRQ(DMPlexRestoreTransitiveClosure(dm, ccone[cc], PETSC_TRUE, &closureSize, &closure));
1938cd0c2139SMatthew G Knepley             }
1939cd0c2139SMatthew G Knepley             again = 1;
1940cd0c2139SMatthew G Knepley             break;
1941cd0c2139SMatthew G Knepley           }
1942cd0c2139SMatthew G Knepley         }
1943cd0c2139SMatthew G Knepley       }
1944cd0c2139SMatthew G Knepley     }
1945cd0c2139SMatthew G Knepley     /* Classify the rest by cell membership */
1946cd0c2139SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
1947cd0c2139SMatthew G Knepley       const PetscInt point = star[s];
1948cd0c2139SMatthew G Knepley 
1949*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelGetValue(label, point, &val));
1950cd0c2139SMatthew G Knepley       if (val == -1) {
1951cd0c2139SMatthew G Knepley         PetscInt      *sstar = NULL;
1952cd0c2139SMatthew G Knepley         PetscInt       sstarSize, ss;
1953412e9a14SMatthew G. Knepley         PetscBool      marked = PETSC_FALSE, isHybrid;
1954cd0c2139SMatthew G Knepley 
1955*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &sstarSize, &sstar));
1956cd0c2139SMatthew G Knepley         for (ss = 0; ss < sstarSize*2; ss += 2) {
1957cd0c2139SMatthew G Knepley           const PetscInt spoint = sstar[ss];
1958cd0c2139SMatthew G Knepley 
1959412e9a14SMatthew G. Knepley           if ((spoint < cStart) || (spoint >= cEnd)) continue;
1960*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelGetValue(label, spoint, &val));
19612c71b3e2SJacob Faibussowitsch           PetscCheckFalse(val == -1,PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %d in star of %d does not have a valid label", spoint, point);
1962*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelGetValue(depthLabel, point, &dep));
1963cd0c2139SMatthew G Knepley           if (val > 0) {
1964*5f80ce2aSJacob Faibussowitsch             CHKERRQ(DMLabelSetValue(label, point,   shift+dep));
1965cd0c2139SMatthew G Knepley           } else {
1966*5f80ce2aSJacob Faibussowitsch             CHKERRQ(DMLabelSetValue(label, point, -(shift+dep)));
1967cd0c2139SMatthew G Knepley           }
1968cd0c2139SMatthew G Knepley           marked = PETSC_TRUE;
1969cd0c2139SMatthew G Knepley           break;
1970cd0c2139SMatthew G Knepley         }
1971*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &sstarSize, &sstar));
1972*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexCellIsHybrid_Internal(dm, point, &isHybrid));
19732c71b3e2SJacob Faibussowitsch         PetscCheckFalse(!isHybrid && !marked,PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Point %d could not be classified", point);
1974cd0c2139SMatthew G Knepley       }
1975cd0c2139SMatthew G Knepley     }
1976*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &starSize, &star));
1977cd0c2139SMatthew G Knepley   }
1978*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISRestoreIndices(dimIS, &points));
1979*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&dimIS));
19807d2fefb6SMatthew G. Knepley   /* If any faces touching the fault divide cells on either side, split them
19817d2fefb6SMatthew G. Knepley        This only occurs without a surface boundary */
1982*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelGetStratumIS(label,   shift+dim-1,  &facePosIS));
1983*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelGetStratumIS(label, -(shift+dim-1), &faceNegIS));
1984*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISExpand(facePosIS, faceNegIS, &dimIS));
1985*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&facePosIS));
1986*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&faceNegIS));
1987*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetLocalSize(dimIS, &numPoints));
1988*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetIndices(dimIS, &points));
198918c5995bSMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
199018c5995bSMatthew G. Knepley     const PetscInt  point = points[p];
199118c5995bSMatthew G. Knepley     const PetscInt *support;
199218c5995bSMatthew G. Knepley     PetscInt        supportSize, valA, valB;
199318c5995bSMatthew G. Knepley 
1994*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetSupportSize(dm, point, &supportSize));
199518c5995bSMatthew G. Knepley     if (supportSize != 2) continue;
1996*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetSupport(dm, point, &support));
1997*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetValue(label, support[0], &valA));
1998*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetValue(label, support[1], &valB));
199918c5995bSMatthew G. Knepley     if ((valA == -1) || (valB == -1)) continue;
200018c5995bSMatthew G. Knepley     if (valA*valB > 0) continue;
200118c5995bSMatthew G. Knepley     /* Split the face */
2002*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetValue(label, point, &valA));
2003*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelClearValue(label, point, valA));
2004*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelSetValue(label, point, dim-1));
2005e1757548SMatthew G. Knepley     /* Label its closure:
2006e1757548SMatthew G. Knepley       unmarked: label as unsplit
2007e1757548SMatthew G. Knepley       incident: relabel as split
2008e1757548SMatthew G. Knepley       split:    do nothing
2009e1757548SMatthew G. Knepley     */
201018c5995bSMatthew G. Knepley     {
201118c5995bSMatthew G. Knepley       PetscInt *closure = NULL;
201218c5995bSMatthew G. Knepley       PetscInt  closureSize, cl;
201318c5995bSMatthew G. Knepley 
2014*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
201518c5995bSMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
2016*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMLabelGetValue(label, closure[cl], &valA));
20178771c1d3SMatthew G. Knepley         if (valA == -1) { /* Mark as unsplit */
2018*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelGetValue(depthLabel, closure[cl], &dep));
2019*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelSetValue(label, closure[cl], shift2+dep));
20208771c1d3SMatthew G. Knepley         } else if (((valA >= shift) && (valA < shift2)) || ((valA <= -shift) && (valA > -shift2))) {
2021*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelGetValue(depthLabel, closure[cl], &dep));
2022*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelClearValue(label, closure[cl], valA));
2023*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelSetValue(label, closure[cl], dep));
2024e1757548SMatthew G. Knepley         }
202518c5995bSMatthew G. Knepley       }
2026*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure));
202718c5995bSMatthew G. Knepley     }
202818c5995bSMatthew G. Knepley   }
2029*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISRestoreIndices(dimIS, &points));
2030*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&dimIS));
2031cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
2032cd0c2139SMatthew G Knepley }
2033cd0c2139SMatthew G Knepley 
2034720e594eSMatthew G. Knepley /* Check that no cell have all vertices on the fault */
2035720e594eSMatthew G. Knepley PetscErrorCode DMPlexCheckValidSubmesh_Private(DM dm, DMLabel label, DM subdm)
2036720e594eSMatthew G. Knepley {
2037720e594eSMatthew G. Knepley   IS              subpointIS;
2038720e594eSMatthew G. Knepley   const PetscInt *dmpoints;
2039720e594eSMatthew G. Knepley   PetscInt        defaultValue, cStart, cEnd, c, vStart, vEnd;
2040720e594eSMatthew G. Knepley 
2041720e594eSMatthew G. Knepley   PetscFunctionBegin;
2042720e594eSMatthew G. Knepley   if (!label) PetscFunctionReturn(0);
2043*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelGetDefaultValue(label, &defaultValue));
2044*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetSubpointIS(subdm, &subpointIS));
2045720e594eSMatthew G. Knepley   if (!subpointIS) PetscFunctionReturn(0);
2046*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetHeightStratum(subdm, 0, &cStart, &cEnd));
2047*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
2048*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISGetIndices(subpointIS, &dmpoints));
2049720e594eSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2050720e594eSMatthew G. Knepley     PetscBool invalidCell = PETSC_TRUE;
2051720e594eSMatthew G. Knepley     PetscInt *closure     = NULL;
2052720e594eSMatthew G. Knepley     PetscInt  closureSize, cl;
2053720e594eSMatthew G. Knepley 
2054*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure));
2055720e594eSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
2056720e594eSMatthew G. Knepley       PetscInt value = 0;
2057720e594eSMatthew G. Knepley 
2058720e594eSMatthew G. Knepley       if ((closure[cl] < vStart) || (closure[cl] >= vEnd)) continue;
2059*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelGetValue(label, closure[cl], &value));
2060720e594eSMatthew G. Knepley       if (value == defaultValue) {invalidCell = PETSC_FALSE; break;}
2061720e594eSMatthew G. Knepley     }
2062*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexRestoreTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure));
2063720e594eSMatthew G. Knepley     if (invalidCell) {
2064*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISRestoreIndices(subpointIS, &dmpoints));
2065*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISDestroy(&subpointIS));
2066*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMDestroy(&subdm));
206798921bdaSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ambiguous submesh. Cell %D has all of its vertices on the submesh.", dmpoints[c]);
2068720e594eSMatthew G. Knepley     }
2069720e594eSMatthew G. Knepley   }
2070*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISRestoreIndices(subpointIS, &dmpoints));
2071720e594eSMatthew G. Knepley   PetscFunctionReturn(0);
2072720e594eSMatthew G. Knepley }
2073720e594eSMatthew G. Knepley 
2074c08575a3SMatthew G. Knepley /*@
20753cf72582SMatthew G. Knepley   DMPlexCreateHybridMesh - Create a mesh with hybrid cells along an internal interface
20763cf72582SMatthew G. Knepley 
20773cf72582SMatthew G. Knepley   Collective on dm
20783cf72582SMatthew G. Knepley 
20793cf72582SMatthew G. Knepley   Input Parameters:
20803cf72582SMatthew G. Knepley + dm - The original DM
2081720e594eSMatthew G. Knepley . label - The label specifying the interface vertices
2082720e594eSMatthew G. Knepley - bdlabel - The optional label specifying the interface boundary vertices
20833cf72582SMatthew G. Knepley 
20843cf72582SMatthew G. Knepley   Output Parameters:
20857db7e0a7SMatthew G. Knepley + hybridLabel - The label fully marking the interface, or NULL if no output is desired
20867db7e0a7SMatthew G. Knepley . splitLabel - The label containing the split points, or NULL if no output is desired
2087720e594eSMatthew G. Knepley . dmInterface - The new interface DM, or NULL
2088720e594eSMatthew G. Knepley - dmHybrid - The new DM with cohesive cells
20893cf72582SMatthew G. Knepley 
20906eccb800SMatthew Knepley   Note: The hybridLabel indicates what parts of the original mesh impinged on the on division surface. For points
20916eccb800SMatthew Knepley   directly on the division surface, they are labeled with their dimension, so an edge 7 on the division surface would be
20926eccb800SMatthew Knepley   7 (1) in hybridLabel. For points that impinge from the positive side, they are labeled with 100+dim, so an edge 6 with
20936eccb800SMatthew 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
20946eccb800SMatthew Knepley   surface also hits vertex 3, it would be 9 (-101) in hybridLabel.
20956eccb800SMatthew Knepley 
20966eccb800SMatthew Knepley   The splitLabel indicates what points in the new hybrid mesh were the result of splitting points in the original
20976eccb800SMatthew Knepley   mesh. The label value is +=100+dim for each point. For example, if two edges 10 and 14 in the hybrid resulting from
20986eccb800SMatthew Knepley   splitting an edge in the original mesh, you would have 10 (101) and 14 (-101) in the splitLabel.
20996eccb800SMatthew Knepley 
21006eccb800SMatthew Knepley   The dmInterface is a DM built from the original division surface. It has a label which can be retrieved using
21016eccb800SMatthew Knepley   DMPlexGetSubpointMap() which maps each point back to the point in the surface of the original mesh.
21026eccb800SMatthew Knepley 
21033cf72582SMatthew G. Knepley   Level: developer
21043cf72582SMatthew G. Knepley 
21056eccb800SMatthew Knepley .seealso: DMPlexConstructCohesiveCells(), DMPlexLabelCohesiveComplete(), DMPlexGetSubpointMap(), DMCreate()
21063cf72582SMatthew G. Knepley @*/
21077db7e0a7SMatthew G. Knepley PetscErrorCode DMPlexCreateHybridMesh(DM dm, DMLabel label, DMLabel bdlabel, DMLabel *hybridLabel, DMLabel *splitLabel, DM *dmInterface, DM *dmHybrid)
21083cf72582SMatthew G. Knepley {
21093cf72582SMatthew G. Knepley   DM             idm;
21107db7e0a7SMatthew G. Knepley   DMLabel        subpointMap, hlabel, slabel = NULL;
21113cf72582SMatthew G. Knepley   PetscInt       dim;
21123cf72582SMatthew G. Knepley 
21133cf72582SMatthew G. Knepley   PetscFunctionBegin;
21143cf72582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2115720e594eSMatthew G. Knepley   if (bdlabel) PetscValidPointer(bdlabel, 3);
2116720e594eSMatthew G. Knepley   if (hybridLabel) PetscValidPointer(hybridLabel, 4);
21177db7e0a7SMatthew G. Knepley   if (splitLabel)  PetscValidPointer(splitLabel, 5);
21187db7e0a7SMatthew G. Knepley   if (dmInterface) PetscValidPointer(dmInterface, 6);
21197db7e0a7SMatthew G. Knepley   PetscValidPointer(dmHybrid, 7);
2120*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetDimension(dm, &dim));
2121*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexCreateSubmesh(dm, label, 1, PETSC_FALSE, &idm));
2122*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexCheckValidSubmesh_Private(dm, label, idm));
2123*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexOrient(idm));
2124*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetSubpointMap(idm, &subpointMap));
2125*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelDuplicate(subpointMap, &hlabel));
2126*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelClearStratum(hlabel, dim));
21277db7e0a7SMatthew G. Knepley   if (splitLabel) {
21287db7e0a7SMatthew G. Knepley     const char *name;
21297db7e0a7SMatthew G. Knepley     char        sname[PETSC_MAX_PATH_LEN];
21307db7e0a7SMatthew G. Knepley 
2131*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectGetName((PetscObject) hlabel, &name));
2132*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrncpy(sname, name, PETSC_MAX_PATH_LEN));
2133*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcat(sname, " split"));
2134*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelCreate(PETSC_COMM_SELF, sname, &slabel));
21357db7e0a7SMatthew G. Knepley   }
2136*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexLabelCohesiveComplete(dm, hlabel, bdlabel, PETSC_FALSE, idm));
2137720e594eSMatthew G. Knepley   if (dmInterface) {*dmInterface = idm;}
2138*5f80ce2aSJacob Faibussowitsch   else             CHKERRQ(DMDestroy(&idm));
2139*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexConstructCohesiveCells(dm, hlabel, slabel, dmHybrid));
2140*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexCopy_Internal(dm, PETSC_TRUE, *dmHybrid));
21413cf72582SMatthew G. Knepley   if (hybridLabel) *hybridLabel = hlabel;
2142*5f80ce2aSJacob Faibussowitsch   else             CHKERRQ(DMLabelDestroy(&hlabel));
21437db7e0a7SMatthew G. Knepley   if (splitLabel)  *splitLabel  = slabel;
21444a7ee7d0SMatthew G. Knepley   {
21454a7ee7d0SMatthew G. Knepley     DM      cdm;
21464a7ee7d0SMatthew G. Knepley     DMLabel ctLabel;
21474a7ee7d0SMatthew G. Knepley 
21484a7ee7d0SMatthew G. Knepley     /* We need to somehow share the celltype label with the coordinate dm */
2149*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetCoordinateDM(*dmHybrid, &cdm));
2150*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetCellTypeLabel(*dmHybrid, &ctLabel));
2151*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetLabel(cdm, ctLabel));
21524a7ee7d0SMatthew G. Knepley   }
2153cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
2154cd0c2139SMatthew G Knepley }
2155cd0c2139SMatthew G Knepley 
2156efa14ee0SMatthew G Knepley /* Here we need the explicit assumption that:
2157efa14ee0SMatthew G Knepley 
2158efa14ee0SMatthew G Knepley      For any marked cell, the marked vertices constitute a single face
2159efa14ee0SMatthew G Knepley */
2160830e53efSMatthew G. Knepley static PetscErrorCode DMPlexMarkSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, DM subdm)
2161efa14ee0SMatthew G Knepley {
2162fed694aaSMatthew G. Knepley   IS               subvertexIS = NULL;
2163efa14ee0SMatthew G Knepley   const PetscInt  *subvertices;
2164412e9a14SMatthew G. Knepley   PetscInt        *pStart, *pEnd, pSize;
2165efa14ee0SMatthew G Knepley   PetscInt         depth, dim, d, numSubVerticesInitial = 0, v;
2166efa14ee0SMatthew G Knepley 
2167efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2168efa14ee0SMatthew G Knepley   *numFaces = 0;
2169efa14ee0SMatthew G Knepley   *nFV      = 0;
2170*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetDepth(dm, &depth));
2171*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetDimension(dm, &dim));
217277d178adSMatthew G. Knepley   pSize = PetscMax(depth, dim) + 1;
2173*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc2(pSize, &pStart, pSize, &pEnd));
2174efa14ee0SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
2175*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetSimplexOrBoxCells(dm, depth-d, &pStart[d], &pEnd[d]));
2176efa14ee0SMatthew G Knepley   }
2177efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
2178*5f80ce2aSJacob Faibussowitsch   if (vertexLabel) CHKERRQ(DMLabelGetStratumIS(vertexLabel, value, &subvertexIS));
2179efa14ee0SMatthew G Knepley   if (subvertexIS) {
2180*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetSize(subvertexIS, &numSubVerticesInitial));
2181*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetIndices(subvertexIS, &subvertices));
2182efa14ee0SMatthew G Knepley   }
2183efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2184efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
21850298fd71SBarry Smith     PetscInt      *star   = NULL;
2186efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numCells = 0, c;
2187efa14ee0SMatthew G Knepley 
2188*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2189efa14ee0SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
2190efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2191efa14ee0SMatthew G Knepley       if ((point >= pStart[depth]) && (point < pEnd[depth])) star[numCells++] = point;
2192efa14ee0SMatthew G Knepley     }
2193efa14ee0SMatthew G Knepley     for (c = 0; c < numCells; ++c) {
2194efa14ee0SMatthew G Knepley       const PetscInt cell    = star[c];
21950298fd71SBarry Smith       PetscInt      *closure = NULL;
2196efa14ee0SMatthew G Knepley       PetscInt       closureSize, cl;
2197efa14ee0SMatthew G Knepley       PetscInt       cellLoc, numCorners = 0, faceSize = 0;
2198efa14ee0SMatthew G Knepley 
2199*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelGetValue(subpointMap, cell, &cellLoc));
220065560c7fSMatthew G Knepley       if (cellLoc == 2) continue;
22012c71b3e2SJacob Faibussowitsch       PetscCheckFalse(cellLoc >= 0,PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %d has dimension %d in the surface label", cell, cellLoc);
2202*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
2203efa14ee0SMatthew G Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
2204efa14ee0SMatthew G Knepley         const PetscInt point = closure[cl];
2205efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2206efa14ee0SMatthew G Knepley 
2207efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
2208efa14ee0SMatthew G Knepley           ++numCorners;
2209*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelGetValue(vertexLabel, point, &vertexLoc));
2210830e53efSMatthew G. Knepley           if (vertexLoc == value) closure[faceSize++] = point;
2211efa14ee0SMatthew G Knepley         }
2212efa14ee0SMatthew G Knepley       }
2213*5f80ce2aSJacob Faibussowitsch       if (!(*nFV)) CHKERRQ(DMPlexGetNumFaceVertices(dm, dim, numCorners, nFV));
22142c71b3e2SJacob Faibussowitsch       PetscCheckFalse(faceSize > *nFV,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %d of an element on the surface", faceSize);
2215efa14ee0SMatthew G Knepley       if (faceSize == *nFV) {
2216007baee2SMatthew G. Knepley         const PetscInt *cells = NULL;
2217007baee2SMatthew G. Knepley         PetscInt        numCells, nc;
2218007baee2SMatthew G. Knepley 
2219efa14ee0SMatthew G Knepley         ++(*numFaces);
2220efa14ee0SMatthew G Knepley         for (cl = 0; cl < faceSize; ++cl) {
2221*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelSetValue(subpointMap, closure[cl], 0));
2222efa14ee0SMatthew G Knepley         }
2223*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetJoin(dm, faceSize, closure, &numCells, &cells));
2224007baee2SMatthew G. Knepley         for (nc = 0; nc < numCells; ++nc) {
2225*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelSetValue(subpointMap, cells[nc], 2));
2226007baee2SMatthew G. Knepley         }
2227*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexRestoreJoin(dm, faceSize, closure, &numCells, &cells));
2228efa14ee0SMatthew G Knepley       }
2229*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
2230efa14ee0SMatthew G Knepley     }
2231*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2232efa14ee0SMatthew G Knepley   }
2233efa14ee0SMatthew G Knepley   if (subvertexIS) {
2234*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISRestoreIndices(subvertexIS, &subvertices));
2235efa14ee0SMatthew G Knepley   }
2236*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&subvertexIS));
2237*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree2(pStart, pEnd));
2238efa14ee0SMatthew G Knepley   PetscFunctionReturn(0);
2239efa14ee0SMatthew G Knepley }
2240efa14ee0SMatthew G Knepley 
2241158acfadSMatthew G. Knepley static PetscErrorCode DMPlexMarkSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DMLabel subpointMap, DM subdm)
2242efa14ee0SMatthew G Knepley {
224334b4c39eSMatthew G. Knepley   IS               subvertexIS = NULL;
2244efa14ee0SMatthew G Knepley   const PetscInt  *subvertices;
2245412e9a14SMatthew G. Knepley   PetscInt        *pStart, *pEnd;
2246efa14ee0SMatthew G Knepley   PetscInt         dim, d, numSubVerticesInitial = 0, v;
2247efa14ee0SMatthew G Knepley 
2248efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2249*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetDimension(dm, &dim));
2250*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc2(dim+1, &pStart, dim+1, &pEnd));
2251efa14ee0SMatthew G Knepley   for (d = 0; d <= dim; ++d) {
2252*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetSimplexOrBoxCells(dm, dim-d, &pStart[d], &pEnd[d]));
2253efa14ee0SMatthew G Knepley   }
2254efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
225534b4c39eSMatthew G. Knepley   if (vertexLabel) {
2256*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetStratumIS(vertexLabel, value, &subvertexIS));
2257efa14ee0SMatthew G Knepley     if (subvertexIS) {
2258*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISGetSize(subvertexIS, &numSubVerticesInitial));
2259*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISGetIndices(subvertexIS, &subvertices));
2260efa14ee0SMatthew G Knepley     }
226134b4c39eSMatthew G. Knepley   }
2262efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2263efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
22640298fd71SBarry Smith     PetscInt      *star   = NULL;
2265efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numFaces = 0, f;
2266efa14ee0SMatthew G Knepley 
2267*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2268efa14ee0SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
2269efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2270158acfadSMatthew G. Knepley       PetscInt       faceLoc;
2271158acfadSMatthew G. Knepley 
2272158acfadSMatthew G. Knepley       if ((point >= pStart[dim-1]) && (point < pEnd[dim-1])) {
2273158acfadSMatthew G. Knepley         if (markedFaces) {
2274*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelGetValue(vertexLabel, point, &faceLoc));
2275158acfadSMatthew G. Knepley           if (faceLoc < 0) continue;
2276158acfadSMatthew G. Knepley         }
2277158acfadSMatthew G. Knepley         star[numFaces++] = point;
2278158acfadSMatthew G. Knepley       }
2279efa14ee0SMatthew G Knepley     }
2280efa14ee0SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
2281efa14ee0SMatthew G Knepley       const PetscInt face    = star[f];
22820298fd71SBarry Smith       PetscInt      *closure = NULL;
2283efa14ee0SMatthew G Knepley       PetscInt       closureSize, c;
2284efa14ee0SMatthew G Knepley       PetscInt       faceLoc;
2285efa14ee0SMatthew G Knepley 
2286*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelGetValue(subpointMap, face, &faceLoc));
2287efa14ee0SMatthew G Knepley       if (faceLoc == dim-1) continue;
22882c71b3e2SJacob Faibussowitsch       PetscCheckFalse(faceLoc >= 0,PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %d has dimension %d in the surface label", face, faceLoc);
2289*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure));
2290efa14ee0SMatthew G Knepley       for (c = 0; c < closureSize*2; c += 2) {
2291efa14ee0SMatthew G Knepley         const PetscInt point = closure[c];
2292efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2293efa14ee0SMatthew G Knepley 
2294efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
2295*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelGetValue(vertexLabel, point, &vertexLoc));
2296830e53efSMatthew G. Knepley           if (vertexLoc != value) break;
2297efa14ee0SMatthew G Knepley         }
2298efa14ee0SMatthew G Knepley       }
2299efa14ee0SMatthew G Knepley       if (c == closureSize*2) {
2300efa14ee0SMatthew G Knepley         const PetscInt *support;
2301efa14ee0SMatthew G Knepley         PetscInt        supportSize, s;
2302efa14ee0SMatthew G Knepley 
2303efa14ee0SMatthew G Knepley         for (c = 0; c < closureSize*2; c += 2) {
2304efa14ee0SMatthew G Knepley           const PetscInt point = closure[c];
2305efa14ee0SMatthew G Knepley 
2306efa14ee0SMatthew G Knepley           for (d = 0; d < dim; ++d) {
2307efa14ee0SMatthew G Knepley             if ((point >= pStart[d]) && (point < pEnd[d])) {
2308*5f80ce2aSJacob Faibussowitsch               CHKERRQ(DMLabelSetValue(subpointMap, point, d));
2309efa14ee0SMatthew G Knepley               break;
2310efa14ee0SMatthew G Knepley             }
2311efa14ee0SMatthew G Knepley           }
2312efa14ee0SMatthew G Knepley         }
2313*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetSupportSize(dm, face, &supportSize));
2314*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetSupport(dm, face, &support));
2315efa14ee0SMatthew G Knepley         for (s = 0; s < supportSize; ++s) {
2316*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelSetValue(subpointMap, support[s], dim));
2317efa14ee0SMatthew G Knepley         }
2318efa14ee0SMatthew G Knepley       }
2319*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexRestoreTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure));
2320efa14ee0SMatthew G Knepley     }
2321*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star));
2322efa14ee0SMatthew G Knepley   }
2323*5f80ce2aSJacob Faibussowitsch   if (subvertexIS) CHKERRQ(ISRestoreIndices(subvertexIS, &subvertices));
2324*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&subvertexIS));
2325*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree2(pStart, pEnd));
2326efa14ee0SMatthew G Knepley   PetscFunctionReturn(0);
2327efa14ee0SMatthew G Knepley }
2328efa14ee0SMatthew G Knepley 
232927c04023SMatthew 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)
2330766ab985SMatthew G. Knepley {
233127c04023SMatthew G. Knepley   DMLabel         label = NULL;
2332766ab985SMatthew G. Knepley   const PetscInt *cone;
23339fc93327SToby Isaac   PetscInt        dim, cMax, cEnd, c, subc = 0, p, coneSize = -1;
2334766ab985SMatthew G. Knepley 
2335812bfc34SJed Brown   PetscFunctionBegin;
2336c0ed958bSJed Brown   *numFaces = 0;
2337c0ed958bSJed Brown   *nFV = 0;
2338*5f80ce2aSJacob Faibussowitsch   if (labelname) CHKERRQ(DMGetLabel(dm, labelname, &label));
2339fed694aaSMatthew G. Knepley   *subCells = NULL;
2340*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetDimension(dm, &dim));
2341*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd));
2342766ab985SMatthew G. Knepley   if (cMax < 0) PetscFunctionReturn(0);
234327c04023SMatthew G. Knepley   if (label) {
234427c04023SMatthew G. Knepley     for (c = cMax; c < cEnd; ++c) {
234527c04023SMatthew G. Knepley       PetscInt val;
234627c04023SMatthew G. Knepley 
2347*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelGetValue(label, c, &val));
234827c04023SMatthew G. Knepley       if (val == value) {
234927c04023SMatthew G. Knepley         ++(*numFaces);
2350*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetConeSize(dm, c, &coneSize));
235127c04023SMatthew G. Knepley       }
235227c04023SMatthew G. Knepley     }
235327c04023SMatthew G. Knepley   } else {
2354766ab985SMatthew G. Knepley     *numFaces = cEnd - cMax;
2355*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetConeSize(dm, cMax, &coneSize));
235627c04023SMatthew G. Knepley   }
2357*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc1(*numFaces *2, subCells));
23589fc93327SToby Isaac   if (!(*numFaces)) PetscFunctionReturn(0);
23599fc93327SToby Isaac   *nFV = hasLagrange ? coneSize/3 : coneSize/2;
2360766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2361766ab985SMatthew G. Knepley     const PetscInt *cells;
2362766ab985SMatthew G. Knepley     PetscInt        numCells;
2363766ab985SMatthew G. Knepley 
236427c04023SMatthew G. Knepley     if (label) {
236527c04023SMatthew G. Knepley       PetscInt val;
236627c04023SMatthew G. Knepley 
2367*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelGetValue(label, c, &val));
236827c04023SMatthew G. Knepley       if (val != value) continue;
236927c04023SMatthew G. Knepley     }
2370*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetCone(dm, c, &cone));
2371766ab985SMatthew G. Knepley     for (p = 0; p < *nFV; ++p) {
2372*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelSetValue(subpointMap, cone[p], 0));
2373766ab985SMatthew G. Knepley     }
2374766ab985SMatthew G. Knepley     /* Negative face */
2375*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetJoin(dm, *nFV, cone, &numCells, &cells));
237627234c99SMatthew G. Knepley     /* Not true in parallel
23772c71b3e2SJacob Faibussowitsch     PetscCheckFalse(numCells != 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
2378766ab985SMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
2379*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelSetValue(subpointMap, cells[p], 2));
238027234c99SMatthew G. Knepley       (*subCells)[subc++] = cells[p];
2381766ab985SMatthew G. Knepley     }
2382*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexRestoreJoin(dm, *nFV, cone, &numCells, &cells));
2383766ab985SMatthew G. Knepley     /* Positive face is not included */
2384766ab985SMatthew G. Knepley   }
2385766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
2386766ab985SMatthew G. Knepley }
2387766ab985SMatthew G. Knepley 
23883982b651SMatthew G. Knepley static PetscErrorCode DMPlexMarkCohesiveSubmesh_Interpolated(DM dm, DMLabel label, PetscInt value, DMLabel subpointMap, DM subdm)
2389766ab985SMatthew G. Knepley {
2390766ab985SMatthew G. Knepley   PetscInt      *pStart, *pEnd;
2391766ab985SMatthew G. Knepley   PetscInt       dim, cMax, cEnd, c, d;
2392766ab985SMatthew G. Knepley 
2393812bfc34SJed Brown   PetscFunctionBegin;
2394*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetDimension(dm, &dim));
2395*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd));
2396766ab985SMatthew G. Knepley   if (cMax < 0) PetscFunctionReturn(0);
2397*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc2(dim+1,&pStart,dim+1,&pEnd));
2398*5f80ce2aSJacob Faibussowitsch   for (d = 0; d <= dim; ++d) CHKERRQ(DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]));
2399766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2400766ab985SMatthew G. Knepley     const PetscInt *cone;
2401766ab985SMatthew G. Knepley     PetscInt       *closure = NULL;
2402b3154360SMatthew G. Knepley     PetscInt        fconeSize, coneSize, closureSize, cl, val;
2403766ab985SMatthew G. Knepley 
240427c04023SMatthew G. Knepley     if (label) {
2405*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelGetValue(label, c, &val));
240627c04023SMatthew G. Knepley       if (val != value) continue;
240727c04023SMatthew G. Knepley     }
2408*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetConeSize(dm, c, &coneSize));
2409*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetCone(dm, c, &cone));
2410*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetConeSize(dm, cone[0], &fconeSize));
24112c71b3e2SJacob Faibussowitsch     PetscCheckFalse(coneSize != (fconeSize ? fconeSize : 1) + 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells");
2412b3154360SMatthew G. Knepley     /* Negative face */
2413*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure));
2414766ab985SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
2415766ab985SMatthew G. Knepley       const PetscInt point = closure[cl];
2416766ab985SMatthew G. Knepley 
2417766ab985SMatthew G. Knepley       for (d = 0; d <= dim; ++d) {
2418766ab985SMatthew G. Knepley         if ((point >= pStart[d]) && (point < pEnd[d])) {
2419*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelSetValue(subpointMap, point, d));
2420766ab985SMatthew G. Knepley           break;
2421766ab985SMatthew G. Knepley         }
2422766ab985SMatthew G. Knepley       }
2423766ab985SMatthew G. Knepley     }
2424*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure));
2425766ab985SMatthew G. Knepley     /* Cells -- positive face is not included */
2426766ab985SMatthew G. Knepley     for (cl = 0; cl < 1; ++cl) {
2427766ab985SMatthew G. Knepley       const PetscInt *support;
2428766ab985SMatthew G. Knepley       PetscInt        supportSize, s;
2429766ab985SMatthew G. Knepley 
2430*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetSupportSize(dm, cone[cl], &supportSize));
24312c71b3e2SJacob Faibussowitsch       /* PetscCheckFalse(supportSize != 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive faces should separate two cells"); */
2432*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetSupport(dm, cone[cl], &support));
2433766ab985SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
2434*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMLabelSetValue(subpointMap, support[s], dim));
2435766ab985SMatthew G. Knepley       }
2436766ab985SMatthew G. Knepley     }
2437766ab985SMatthew G. Knepley   }
2438*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree2(pStart, pEnd));
2439766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
2440766ab985SMatthew G. Knepley }
2441766ab985SMatthew G. Knepley 
2442c08575a3SMatthew G. Knepley static PetscErrorCode DMPlexGetFaceOrientation(DM dm, PetscInt cell, PetscInt numCorners, PetscInt indices[], PetscInt oppositeVertex, PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2443e6ccafaeSMatthew G Knepley {
244482f516ccSBarry Smith   MPI_Comm       comm;
2445e6ccafaeSMatthew G Knepley   PetscBool      posOrient = PETSC_FALSE;
2446e6ccafaeSMatthew G Knepley   const PetscInt debug     = 0;
2447e6ccafaeSMatthew G Knepley   PetscInt       cellDim, faceSize, f;
2448e6ccafaeSMatthew G Knepley 
244982f516ccSBarry Smith   PetscFunctionBegin;
2450*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectGetComm((PetscObject)dm,&comm));
2451*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetDimension(dm, &cellDim));
2452*5f80ce2aSJacob Faibussowitsch   if (debug) CHKERRQ(PetscPrintf(comm, "cellDim: %d numCorners: %d\n", cellDim, numCorners));
2453e6ccafaeSMatthew G Knepley 
2454ddeab2a6SMatthew G. Knepley   if (cellDim == 1 && numCorners == 2) {
2455ddeab2a6SMatthew G. Knepley     /* Triangle */
2456e6ccafaeSMatthew G Knepley     faceSize  = numCorners-1;
2457e6ccafaeSMatthew G Knepley     posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2458ddeab2a6SMatthew G. Knepley   } else if (cellDim == 2 && numCorners == 3) {
2459ddeab2a6SMatthew G. Knepley     /* Triangle */
2460ddeab2a6SMatthew G. Knepley     faceSize  = numCorners-1;
2461ddeab2a6SMatthew G. Knepley     posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2462ddeab2a6SMatthew G. Knepley   } else if (cellDim == 3 && numCorners == 4) {
2463ddeab2a6SMatthew G. Knepley     /* Tetrahedron */
2464ddeab2a6SMatthew G. Knepley     faceSize  = numCorners-1;
2465ddeab2a6SMatthew G. Knepley     posOrient = (oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2466e6ccafaeSMatthew G Knepley   } else if (cellDim == 1 && numCorners == 3) {
2467e6ccafaeSMatthew G Knepley     /* Quadratic line */
2468e6ccafaeSMatthew G Knepley     faceSize  = 1;
2469e6ccafaeSMatthew G Knepley     posOrient = PETSC_TRUE;
2470e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 4) {
2471e6ccafaeSMatthew G Knepley     /* Quads */
2472e6ccafaeSMatthew G Knepley     faceSize = 2;
2473e6ccafaeSMatthew G Knepley     if ((indices[1] > indices[0]) && (indices[1] - indices[0] == 1)) {
2474e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2475e6ccafaeSMatthew G Knepley     } else if ((indices[0] == 3) && (indices[1] == 0)) {
2476e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2477e6ccafaeSMatthew G Knepley     } else {
2478e6ccafaeSMatthew G Knepley       if (((indices[0] > indices[1]) && (indices[0] - indices[1] == 1)) || ((indices[0] == 0) && (indices[1] == 3))) {
2479e6ccafaeSMatthew G Knepley         posOrient = PETSC_FALSE;
2480e6ccafaeSMatthew G Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossedge");
2481e6ccafaeSMatthew G Knepley     }
2482e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 6) {
2483e6ccafaeSMatthew G Knepley     /* Quadratic triangle (I hate this) */
2484e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2485e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTri = 3;
2486e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2487e6ccafaeSMatthew G Knepley     PetscBool      found                    = PETSC_FALSE;
2488e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTriSorted[9] = {
2489e6ccafaeSMatthew G Knepley       0, 3,  4, /* bottom */
2490e6ccafaeSMatthew G Knepley       1, 4,  5, /* right */
2491e6ccafaeSMatthew G Knepley       2, 3,  5, /* left */
2492e6ccafaeSMatthew G Knepley     };
2493e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTri[9] = {
2494e6ccafaeSMatthew G Knepley       0, 3,  4, /* bottom */
2495e6ccafaeSMatthew G Knepley       1, 4,  5, /* right */
2496e6ccafaeSMatthew G Knepley       2, 5,  3, /* left */
2497e6ccafaeSMatthew G Knepley     };
2498e6ccafaeSMatthew G Knepley 
2499e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTri; ++i) sortedIndices[i] = indices[i];
2500*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSortInt(faceSizeTri, sortedIndices));
2501e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 3; ++iFace) {
2502e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeTri;
2503e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2504e6ccafaeSMatthew G Knepley 
2505e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesTriSorted[ii+0]) &&
2506e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesTriSorted[ii+1])) {
2507e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTri; ++fVertex) {
2508e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTri; ++cVertex) {
2509e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTri[ii+fVertex]) {
2510e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2511e6ccafaeSMatthew G Knepley               break;
2512e6ccafaeSMatthew G Knepley             }
2513e6ccafaeSMatthew G Knepley           }
2514e6ccafaeSMatthew G Knepley         }
2515e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2516e6ccafaeSMatthew G Knepley         break;
2517e6ccafaeSMatthew G Knepley       }
2518e6ccafaeSMatthew G Knepley     }
25192c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!found,comm, PETSC_ERR_ARG_WRONG, "Invalid tri crossface");
2520e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2521e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2522e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 9) {
2523e6ccafaeSMatthew G Knepley     /* Quadratic quad (I hate this) */
2524e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2525e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuad = 3;
2526e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2527e6ccafaeSMatthew G Knepley     PetscBool      found                      = PETSC_FALSE;
2528e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadSorted[12] = {
2529e6ccafaeSMatthew G Knepley       0, 1,  4, /* bottom */
2530e6ccafaeSMatthew G Knepley       1, 2,  5, /* right */
2531e6ccafaeSMatthew G Knepley       2, 3,  6, /* top */
2532e6ccafaeSMatthew G Knepley       0, 3,  7, /* left */
2533e6ccafaeSMatthew G Knepley     };
2534e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuad[12] = {
2535e6ccafaeSMatthew G Knepley       0, 1,  4, /* bottom */
2536e6ccafaeSMatthew G Knepley       1, 2,  5, /* right */
2537e6ccafaeSMatthew G Knepley       2, 3,  6, /* top */
2538e6ccafaeSMatthew G Knepley       3, 0,  7, /* left */
2539e6ccafaeSMatthew G Knepley     };
2540e6ccafaeSMatthew G Knepley 
2541e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuad; ++i) sortedIndices[i] = indices[i];
2542*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSortInt(faceSizeQuad, sortedIndices));
2543e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2544e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeQuad;
2545e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2546e6ccafaeSMatthew G Knepley 
2547e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesQuadSorted[ii+0]) &&
2548e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesQuadSorted[ii+1])) {
2549e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuad; ++fVertex) {
2550e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuad; ++cVertex) {
2551e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuad[ii+fVertex]) {
2552e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2553e6ccafaeSMatthew G Knepley               break;
2554e6ccafaeSMatthew G Knepley             }
2555e6ccafaeSMatthew G Knepley           }
2556e6ccafaeSMatthew G Knepley         }
2557e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2558e6ccafaeSMatthew G Knepley         break;
2559e6ccafaeSMatthew G Knepley       }
2560e6ccafaeSMatthew G Knepley     }
25612c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!found,comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossface");
2562e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2563e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2564e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 8) {
2565e6ccafaeSMatthew G Knepley     /* Hexes
2566e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2567e6ccafaeSMatthew G Knepley        pointing up at the second.
2568e6ccafaeSMatthew G Knepley 
2569e6ccafaeSMatthew G Knepley           7---6
2570e6ccafaeSMatthew G Knepley          /|  /|
2571e6ccafaeSMatthew G Knepley         4---5 |
2572ddeab2a6SMatthew G. Knepley         | 1-|-2
2573e6ccafaeSMatthew G Knepley         |/  |/
2574ddeab2a6SMatthew G. Knepley         0---3
2575e6ccafaeSMatthew G Knepley 
2576e6ccafaeSMatthew G Knepley         Faces are determined by the first 4 vertices (corners of faces) */
2577e6ccafaeSMatthew G Knepley     const PetscInt faceSizeHex = 4;
2578e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[4], i, iFace;
2579e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2580e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHexSorted[24] = {
2581e6ccafaeSMatthew G Knepley       0, 1, 2, 3,  /* bottom */
2582e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  /* top */
2583ddeab2a6SMatthew G. Knepley       0, 3, 4, 5,  /* front */
2584ddeab2a6SMatthew G. Knepley       2, 3, 5, 6,  /* right */
2585ddeab2a6SMatthew G. Knepley       1, 2, 6, 7,  /* back */
2586ddeab2a6SMatthew G. Knepley       0, 1, 4, 7,  /* left */
2587e6ccafaeSMatthew G Knepley     };
2588e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHex[24] = {
2589ddeab2a6SMatthew G. Knepley       1, 2, 3, 0,  /* bottom */
2590e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  /* top */
2591ddeab2a6SMatthew G. Knepley       0, 3, 5, 4,  /* front */
2592ddeab2a6SMatthew G. Knepley       3, 2, 6, 5,  /* right */
2593ddeab2a6SMatthew G. Knepley       2, 1, 7, 6,  /* back */
2594ddeab2a6SMatthew G. Knepley       1, 0, 4, 7,  /* left */
2595e6ccafaeSMatthew G Knepley     };
2596e6ccafaeSMatthew G Knepley 
2597e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeHex; ++i) sortedIndices[i] = indices[i];
2598*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSortInt(faceSizeHex, sortedIndices));
2599e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2600e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeHex;
2601e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2602e6ccafaeSMatthew G Knepley 
2603e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesHexSorted[ii+0]) &&
2604e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesHexSorted[ii+1]) &&
2605e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesHexSorted[ii+2]) &&
2606e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesHexSorted[ii+3])) {
2607e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeHex; ++fVertex) {
2608e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeHex; ++cVertex) {
2609e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesHex[ii+fVertex]) {
2610e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2611e6ccafaeSMatthew G Knepley               break;
2612e6ccafaeSMatthew G Knepley             }
2613e6ccafaeSMatthew G Knepley           }
2614e6ccafaeSMatthew G Knepley         }
2615e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2616e6ccafaeSMatthew G Knepley         break;
2617e6ccafaeSMatthew G Knepley       }
2618e6ccafaeSMatthew G Knepley     }
26192c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!found,comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2620e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2621e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2622e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 10) {
2623e6ccafaeSMatthew G Knepley     /* Quadratic tet */
2624e6ccafaeSMatthew G Knepley     /* Faces are determined by the first 3 vertices (corners of faces) */
2625e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTet = 6;
2626e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[6], i, iFace;
2627e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2628e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTetSorted[24] = {
2629e6ccafaeSMatthew G Knepley       0, 1, 2,  6, 7, 8, /* bottom */
2630e6ccafaeSMatthew G Knepley       0, 3, 4,  6, 7, 9,  /* front */
2631e6ccafaeSMatthew G Knepley       1, 4, 5,  7, 8, 9,  /* right */
2632e6ccafaeSMatthew G Knepley       2, 3, 5,  6, 8, 9,  /* left */
2633e6ccafaeSMatthew G Knepley     };
2634e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTet[24] = {
2635e6ccafaeSMatthew G Knepley       0, 1, 2,  6, 7, 8, /* bottom */
2636e6ccafaeSMatthew G Knepley       0, 4, 3,  6, 7, 9,  /* front */
2637e6ccafaeSMatthew G Knepley       1, 5, 4,  7, 8, 9,  /* right */
2638e6ccafaeSMatthew G Knepley       2, 3, 5,  8, 6, 9,  /* left */
2639e6ccafaeSMatthew G Knepley     };
2640e6ccafaeSMatthew G Knepley 
2641e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTet; ++i) sortedIndices[i] = indices[i];
2642*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSortInt(faceSizeTet, sortedIndices));
2643e6ccafaeSMatthew G Knepley     for (iFace=0; iFace < 4; ++iFace) {
2644e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeTet;
2645e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2646e6ccafaeSMatthew G Knepley 
2647e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesTetSorted[ii+0]) &&
2648e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesTetSorted[ii+1]) &&
2649e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesTetSorted[ii+2]) &&
2650e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesTetSorted[ii+3])) {
2651e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTet; ++fVertex) {
2652e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTet; ++cVertex) {
2653e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTet[ii+fVertex]) {
2654e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2655e6ccafaeSMatthew G Knepley               break;
2656e6ccafaeSMatthew G Knepley             }
2657e6ccafaeSMatthew G Knepley           }
2658e6ccafaeSMatthew G Knepley         }
2659e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2660e6ccafaeSMatthew G Knepley         break;
2661e6ccafaeSMatthew G Knepley       }
2662e6ccafaeSMatthew G Knepley     }
26632c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!found,comm, PETSC_ERR_ARG_WRONG, "Invalid tet crossface");
2664e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2665e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2666e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 27) {
2667e6ccafaeSMatthew G Knepley     /* Quadratic hexes (I hate this)
2668e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2669e6ccafaeSMatthew G Knepley        pointing up at the second.
2670e6ccafaeSMatthew G Knepley 
2671e6ccafaeSMatthew G Knepley          7---6
2672e6ccafaeSMatthew G Knepley         /|  /|
2673e6ccafaeSMatthew G Knepley        4---5 |
2674e6ccafaeSMatthew G Knepley        | 3-|-2
2675e6ccafaeSMatthew G Knepley        |/  |/
2676e6ccafaeSMatthew G Knepley        0---1
2677e6ccafaeSMatthew G Knepley 
2678e6ccafaeSMatthew G Knepley        Faces are determined by the first 4 vertices (corners of faces) */
2679e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuadHex = 9;
2680e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[9], i, iFace;
2681e6ccafaeSMatthew G Knepley     PetscBool      found                         = PETSC_FALSE;
2682e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHexSorted[54] = {
2683e6ccafaeSMatthew G Knepley       0, 1, 2, 3,  8, 9, 10, 11,  24, /* bottom */
2684e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  12, 13, 14, 15,  25, /* top */
2685e6ccafaeSMatthew G Knepley       0, 1, 4, 5,  8, 12, 16, 17,  22, /* front */
2686e6ccafaeSMatthew G Knepley       1, 2, 5, 6,  9, 13, 17, 18,  21, /* right */
2687e6ccafaeSMatthew G Knepley       2, 3, 6, 7,  10, 14, 18, 19,  23, /* back */
2688e6ccafaeSMatthew G Knepley       0, 3, 4, 7,  11, 15, 16, 19,  20, /* left */
2689e6ccafaeSMatthew G Knepley     };
2690e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHex[54] = {
2691e6ccafaeSMatthew G Knepley       3, 2, 1, 0,  10, 9, 8, 11,  24, /* bottom */
2692e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  12, 13, 14, 15,  25, /* top */
2693e6ccafaeSMatthew G Knepley       0, 1, 5, 4,  8, 17, 12, 16,  22, /* front */
2694e6ccafaeSMatthew G Knepley       1, 2, 6, 5,  9, 18, 13, 17,  21, /* right */
2695e6ccafaeSMatthew G Knepley       2, 3, 7, 6,  10, 19, 14, 18,  23, /* back */
2696e6ccafaeSMatthew G Knepley       3, 0, 4, 7,  11, 16, 15, 19,  20 /* left */
2697e6ccafaeSMatthew G Knepley     };
2698e6ccafaeSMatthew G Knepley 
2699e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuadHex; ++i) sortedIndices[i] = indices[i];
2700*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSortInt(faceSizeQuadHex, sortedIndices));
2701e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2702e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeQuadHex;
2703e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2704e6ccafaeSMatthew G Knepley 
2705e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesQuadHexSorted[ii+0]) &&
2706e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesQuadHexSorted[ii+1]) &&
2707e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesQuadHexSorted[ii+2]) &&
2708e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesQuadHexSorted[ii+3])) {
2709e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuadHex; ++fVertex) {
2710e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuadHex; ++cVertex) {
2711e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuadHex[ii+fVertex]) {
2712e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2713e6ccafaeSMatthew G Knepley               break;
2714e6ccafaeSMatthew G Knepley             }
2715e6ccafaeSMatthew G Knepley           }
2716e6ccafaeSMatthew G Knepley         }
2717e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2718e6ccafaeSMatthew G Knepley         break;
2719e6ccafaeSMatthew G Knepley       }
2720e6ccafaeSMatthew G Knepley     }
27212c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!found,comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2722e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2723e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2724e6ccafaeSMatthew G Knepley   } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Unknown cell type for faceOrientation().");
2725e6ccafaeSMatthew G Knepley   if (!posOrient) {
2726*5f80ce2aSJacob Faibussowitsch     if (debug) CHKERRQ(PetscPrintf(comm, "  Reversing initial face orientation\n"));
2727e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[faceSize-1 - f];
2728e6ccafaeSMatthew G Knepley   } else {
2729*5f80ce2aSJacob Faibussowitsch     if (debug) CHKERRQ(PetscPrintf(comm, "  Keeping initial face orientation\n"));
2730e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[f];
2731e6ccafaeSMatthew G Knepley   }
2732e6ccafaeSMatthew G Knepley   if (posOriented) *posOriented = posOrient;
2733e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2734e6ccafaeSMatthew G Knepley }
2735e6ccafaeSMatthew G Knepley 
2736c08575a3SMatthew G. Knepley /*@
2737c08575a3SMatthew G. Knepley   DMPlexGetOrientedFace - Given a cell and a face, as a set of vertices, return the oriented face, as a set of vertices,
2738c08575a3SMatthew G. Knepley   in faceVertices. The orientation is such that the face normal points out of the cell
2739c08575a3SMatthew G. Knepley 
2740c08575a3SMatthew G. Knepley   Not collective
2741c08575a3SMatthew G. Knepley 
2742c08575a3SMatthew G. Knepley   Input Parameters:
2743c08575a3SMatthew G. Knepley + dm           - The original mesh
2744c08575a3SMatthew G. Knepley . cell         - The cell mesh point
2745c08575a3SMatthew G. Knepley . faceSize     - The number of vertices on the face
2746c08575a3SMatthew G. Knepley . face         - The face vertices
2747c08575a3SMatthew G. Knepley . numCorners   - The number of vertices on the cell
2748c08575a3SMatthew G. Knepley . indices      - Local numbering of face vertices in cell cone
2749c08575a3SMatthew G. Knepley - origVertices - Original face vertices
2750c08575a3SMatthew G. Knepley 
2751d8d19677SJose E. Roman   Output Parameters:
2752c08575a3SMatthew G. Knepley + faceVertices - The face vertices properly oriented
2753c08575a3SMatthew G. Knepley - posOriented  - PETSC_TRUE if the face was oriented with outward normal
2754c08575a3SMatthew G. Knepley 
2755c08575a3SMatthew G. Knepley   Level: developer
2756c08575a3SMatthew G. Knepley 
2757c08575a3SMatthew G. Knepley .seealso: DMPlexGetCone()
2758c08575a3SMatthew G. Knepley @*/
2759e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexGetOrientedFace(DM dm, PetscInt cell, PetscInt faceSize, const PetscInt face[], PetscInt numCorners, PetscInt indices[], PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2760e6ccafaeSMatthew G Knepley {
27610298fd71SBarry Smith   const PetscInt *cone = NULL;
2762e6ccafaeSMatthew G Knepley   PetscInt        coneSize, v, f, v2;
2763e6ccafaeSMatthew G Knepley   PetscInt        oppositeVertex = -1;
2764e6ccafaeSMatthew G Knepley 
2765e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
2766*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetConeSize(dm, cell, &coneSize));
2767*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetCone(dm, cell, &cone));
2768e6ccafaeSMatthew G Knepley   for (v = 0, v2 = 0; v < coneSize; ++v) {
2769e6ccafaeSMatthew G Knepley     PetscBool found = PETSC_FALSE;
2770e6ccafaeSMatthew G Knepley 
2771e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) {
2772e6ccafaeSMatthew G Knepley       if (face[f] == cone[v]) {
2773e6ccafaeSMatthew G Knepley         found = PETSC_TRUE; break;
2774e6ccafaeSMatthew G Knepley       }
2775e6ccafaeSMatthew G Knepley     }
2776e6ccafaeSMatthew G Knepley     if (found) {
2777e6ccafaeSMatthew G Knepley       indices[v2]      = v;
2778e6ccafaeSMatthew G Knepley       origVertices[v2] = cone[v];
2779e6ccafaeSMatthew G Knepley       ++v2;
2780e6ccafaeSMatthew G Knepley     } else {
2781e6ccafaeSMatthew G Knepley       oppositeVertex = v;
2782e6ccafaeSMatthew G Knepley     }
2783e6ccafaeSMatthew G Knepley   }
2784*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetFaceOrientation(dm, cell, numCorners, indices, oppositeVertex, origVertices, faceVertices, posOriented));
2785e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2786e6ccafaeSMatthew G Knepley }
2787e6ccafaeSMatthew G Knepley 
2788e6ccafaeSMatthew G Knepley /*
2789cd0c2139SMatthew G Knepley   DMPlexInsertFace_Internal - Puts a face into the mesh
2790e6ccafaeSMatthew G Knepley 
2791e6ccafaeSMatthew G Knepley   Not collective
2792e6ccafaeSMatthew G Knepley 
2793e6ccafaeSMatthew G Knepley   Input Parameters:
2794e6ccafaeSMatthew G Knepley   + dm              - The DMPlex
2795e6ccafaeSMatthew G Knepley   . numFaceVertex   - The number of vertices in the face
2796e6ccafaeSMatthew G Knepley   . faceVertices    - The vertices in the face for dm
2797e6ccafaeSMatthew G Knepley   . subfaceVertices - The vertices in the face for subdm
2798e6ccafaeSMatthew G Knepley   . numCorners      - The number of vertices in the cell
2799e6ccafaeSMatthew G Knepley   . cell            - A cell in dm containing the face
2800e6ccafaeSMatthew G Knepley   . subcell         - A cell in subdm containing the face
2801e6ccafaeSMatthew G Knepley   . firstFace       - First face in the mesh
2802e6ccafaeSMatthew G Knepley   - newFacePoint    - Next face in the mesh
2803e6ccafaeSMatthew G Knepley 
2804e6ccafaeSMatthew G Knepley   Output Parameters:
2805e6ccafaeSMatthew G Knepley   . newFacePoint - Contains next face point number on input, updated on output
2806e6ccafaeSMatthew G Knepley 
2807e6ccafaeSMatthew G Knepley   Level: developer
2808e6ccafaeSMatthew G Knepley */
2809cd0c2139SMatthew 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)
2810e6ccafaeSMatthew G Knepley {
281182f516ccSBarry Smith   MPI_Comm        comm;
2812e6ccafaeSMatthew G Knepley   DM_Plex        *submesh = (DM_Plex*) subdm->data;
2813e6ccafaeSMatthew G Knepley   const PetscInt *faces;
2814e6ccafaeSMatthew G Knepley   PetscInt        numFaces, coneSize;
2815e6ccafaeSMatthew G Knepley 
2816e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
2817*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectGetComm((PetscObject)dm,&comm));
2818*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetConeSize(subdm, subcell, &coneSize));
28192c71b3e2SJacob Faibussowitsch   PetscCheckFalse(coneSize != 1,comm, PETSC_ERR_ARG_OUTOFRANGE, "Cone size of cell %d is %d != 1", cell, coneSize);
2820e6ccafaeSMatthew G Knepley #if 0
2821e6ccafaeSMatthew G Knepley   /* Cannot use this because support() has not been constructed yet */
2822*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces));
2823e6ccafaeSMatthew G Knepley #else
2824e6ccafaeSMatthew G Knepley   {
2825e6ccafaeSMatthew G Knepley     PetscInt f;
2826e6ccafaeSMatthew G Knepley 
2827e6ccafaeSMatthew G Knepley     numFaces = 0;
2828*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetWorkArray(subdm, 1, MPIU_INT, (void **) &faces));
2829e6ccafaeSMatthew G Knepley     for (f = firstFace; f < *newFacePoint; ++f) {
2830e6ccafaeSMatthew G Knepley       PetscInt dof, off, d;
2831e6ccafaeSMatthew G Knepley 
2832*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetDof(submesh->coneSection, f, &dof));
2833*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetOffset(submesh->coneSection, f, &off));
2834e6ccafaeSMatthew G Knepley       /* Yes, I know this is quadratic, but I expect the sizes to be <5 */
2835e6ccafaeSMatthew G Knepley       for (d = 0; d < dof; ++d) {
2836e6ccafaeSMatthew G Knepley         const PetscInt p = submesh->cones[off+d];
2837e6ccafaeSMatthew G Knepley         PetscInt       v;
2838e6ccafaeSMatthew G Knepley 
2839e6ccafaeSMatthew G Knepley         for (v = 0; v < numFaceVertices; ++v) {
2840e6ccafaeSMatthew G Knepley           if (subfaceVertices[v] == p) break;
2841e6ccafaeSMatthew G Knepley         }
2842e6ccafaeSMatthew G Knepley         if (v == numFaceVertices) break;
2843e6ccafaeSMatthew G Knepley       }
2844e6ccafaeSMatthew G Knepley       if (d == dof) {
2845e6ccafaeSMatthew G Knepley         numFaces               = 1;
2846e6ccafaeSMatthew G Knepley         ((PetscInt*) faces)[0] = f;
2847e6ccafaeSMatthew G Knepley       }
2848e6ccafaeSMatthew G Knepley     }
2849e6ccafaeSMatthew G Knepley   }
2850e6ccafaeSMatthew G Knepley #endif
28512c71b3e2SJacob Faibussowitsch   PetscCheckFalse(numFaces > 1,comm, PETSC_ERR_ARG_WRONG, "Vertex set had %d faces, not one", numFaces);
2852e6ccafaeSMatthew G Knepley   else if (numFaces == 1) {
2853e6ccafaeSMatthew G Knepley     /* Add the other cell neighbor for this face */
2854*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexSetCone(subdm, subcell, faces));
2855e6ccafaeSMatthew G Knepley   } else {
2856e6ccafaeSMatthew G Knepley     PetscInt *indices, *origVertices, *orientedVertices, *orientedSubVertices, v, ov;
2857e6ccafaeSMatthew G Knepley     PetscBool posOriented;
2858e6ccafaeSMatthew G Knepley 
2859*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices));
2860e6ccafaeSMatthew G Knepley     origVertices        = &orientedVertices[numFaceVertices];
2861e6ccafaeSMatthew G Knepley     indices             = &orientedVertices[numFaceVertices*2];
2862e6ccafaeSMatthew G Knepley     orientedSubVertices = &orientedVertices[numFaceVertices*3];
2863*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetOrientedFace(dm, cell, numFaceVertices, faceVertices, numCorners, indices, origVertices, orientedVertices, &posOriented));
2864e6ccafaeSMatthew G Knepley     /* TODO: I know that routine should return a permutation, not the indices */
2865e6ccafaeSMatthew G Knepley     for (v = 0; v < numFaceVertices; ++v) {
2866e6ccafaeSMatthew G Knepley       const PetscInt vertex = faceVertices[v], subvertex = subfaceVertices[v];
2867e6ccafaeSMatthew G Knepley       for (ov = 0; ov < numFaceVertices; ++ov) {
2868e6ccafaeSMatthew G Knepley         if (orientedVertices[ov] == vertex) {
2869e6ccafaeSMatthew G Knepley           orientedSubVertices[ov] = subvertex;
2870e6ccafaeSMatthew G Knepley           break;
2871e6ccafaeSMatthew G Knepley         }
2872e6ccafaeSMatthew G Knepley       }
28732c71b3e2SJacob Faibussowitsch       PetscCheckFalse(ov == numFaceVertices,comm, PETSC_ERR_PLIB, "Could not find face vertex %d in orientated set", vertex);
2874e6ccafaeSMatthew G Knepley     }
2875*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexSetCone(subdm, *newFacePoint, orientedSubVertices));
2876*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexSetCone(subdm, subcell, newFacePoint));
2877*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMRestoreWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices));
2878e6ccafaeSMatthew G Knepley     ++(*newFacePoint);
2879e6ccafaeSMatthew G Knepley   }
2880ef07cca7SMatthew G. Knepley #if 0
2881*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexRestoreJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces));
2882ef07cca7SMatthew G. Knepley #else
2883*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMRestoreWorkArray(subdm, 1, MPIU_INT, (void **) &faces));
2884ef07cca7SMatthew G. Knepley #endif
2885e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2886e6ccafaeSMatthew G Knepley }
2887e6ccafaeSMatthew G Knepley 
288853156dfcSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm)
2889e6ccafaeSMatthew G Knepley {
289082f516ccSBarry Smith   MPI_Comm        comm;
289153156dfcSMatthew G. Knepley   DMLabel         subpointMap;
2892efa14ee0SMatthew G Knepley   IS              subvertexIS,  subcellIS;
2893efa14ee0SMatthew G Knepley   const PetscInt *subVertices, *subCells;
2894efa14ee0SMatthew G Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells;
2895fed694aaSMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces = 0, firstSubFace, newFacePoint, nFV = 0;
2896efa14ee0SMatthew G Knepley   PetscInt        vStart, vEnd, c, f;
2897e6ccafaeSMatthew G Knepley 
2898e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
2899*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectGetComm((PetscObject)dm,&comm));
2900efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
2901*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
2902*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexSetSubpointMap(subdm, subpointMap));
2903*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelDestroy(&subpointMap));
2904*5f80ce2aSJacob Faibussowitsch   if (vertexLabel) CHKERRQ(DMPlexMarkSubmesh_Uninterpolated(dm, vertexLabel, value, subpointMap, &numSubFaces, &nFV, subdm));
2905efa14ee0SMatthew G Knepley   /* Setup chart */
2906*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelGetStratumSize(subpointMap, 0, &numSubVertices));
2907*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelGetStratumSize(subpointMap, 2, &numSubCells));
2908*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices));
2909*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexSetVTKCellHeight(subdm, 1));
2910e6ccafaeSMatthew G Knepley   /* Set cone sizes */
2911e6ccafaeSMatthew G Knepley   firstSubVertex = numSubCells;
2912efa14ee0SMatthew G Knepley   firstSubFace   = numSubCells+numSubVertices;
2913e6ccafaeSMatthew G Knepley   newFacePoint   = firstSubFace;
2914*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelGetStratumIS(subpointMap, 0, &subvertexIS));
2915*5f80ce2aSJacob Faibussowitsch   if (subvertexIS) CHKERRQ(ISGetIndices(subvertexIS, &subVertices));
2916*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelGetStratumIS(subpointMap, 2, &subcellIS));
2917*5f80ce2aSJacob Faibussowitsch   if (subcellIS) CHKERRQ(ISGetIndices(subcellIS, &subCells));
2918e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
2919*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexSetConeSize(subdm, c, 1));
2920e6ccafaeSMatthew G Knepley   }
2921e6ccafaeSMatthew G Knepley   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
2922*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexSetConeSize(subdm, f, nFV));
2923e6ccafaeSMatthew G Knepley   }
2924*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetUp(subdm));
2925e6ccafaeSMatthew G Knepley   /* Create face cones */
2926*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
2927*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
2928*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface));
2929e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
2930e6ccafaeSMatthew G Knepley     const PetscInt cell    = subCells[c];
2931efa14ee0SMatthew G Knepley     const PetscInt subcell = c;
29320298fd71SBarry Smith     PetscInt      *closure = NULL;
2933efa14ee0SMatthew G Knepley     PetscInt       closureSize, cl, numCorners = 0, faceSize = 0;
2934e6ccafaeSMatthew G Knepley 
2935*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
2936efa14ee0SMatthew G Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
2937efa14ee0SMatthew G Knepley       const PetscInt point = closure[cl];
2938e6ccafaeSMatthew G Knepley       PetscInt       subVertex;
2939e6ccafaeSMatthew G Knepley 
2940efa14ee0SMatthew G Knepley       if ((point >= vStart) && (point < vEnd)) {
2941efa14ee0SMatthew G Knepley         ++numCorners;
2942*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscFindInt(point, numSubVertices, subVertices, &subVertex));
2943efa14ee0SMatthew G Knepley         if (subVertex >= 0) {
2944efa14ee0SMatthew G Knepley           closure[faceSize] = point;
294565560c7fSMatthew G Knepley           subface[faceSize] = firstSubVertex+subVertex;
2946e6ccafaeSMatthew G Knepley           ++faceSize;
2947e6ccafaeSMatthew G Knepley         }
2948e6ccafaeSMatthew G Knepley       }
2949e6ccafaeSMatthew G Knepley     }
29502c71b3e2SJacob Faibussowitsch     PetscCheckFalse(faceSize > nFV,comm, PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %d of an element on the surface", faceSize);
2951efa14ee0SMatthew G Knepley     if (faceSize == nFV) {
2952*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexInsertFace_Internal(dm, subdm, faceSize, closure, subface, numCorners, cell, subcell, firstSubFace, &newFacePoint));
2953e6ccafaeSMatthew G Knepley     }
2954*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure));
2955e6ccafaeSMatthew G Knepley   }
2956*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface));
2957*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexSymmetrize(subdm));
2958*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexStratify(subdm));
2959e6ccafaeSMatthew G Knepley   /* Build coordinates */
2960efa14ee0SMatthew G Knepley   {
2961efa14ee0SMatthew G Knepley     PetscSection coordSection, subCoordSection;
2962efa14ee0SMatthew G Knepley     Vec          coordinates, subCoordinates;
2963efa14ee0SMatthew G Knepley     PetscScalar *coords, *subCoords;
2964285d324eSMatthew G. Knepley     PetscInt     numComp, coordSize, v;
296524640c55SToby Isaac     const char  *name;
2966efa14ee0SMatthew G Knepley 
2967*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetCoordinateSection(dm, &coordSection));
2968*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetCoordinatesLocal(dm, &coordinates));
2969*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetCoordinateSection(subdm, &subCoordSection));
2970*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetNumFields(subCoordSection, 1));
2971*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
2972*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
2973*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices));
2974efa14ee0SMatthew G Knepley     for (v = 0; v < numSubVertices; ++v) {
2975efa14ee0SMatthew G Knepley       const PetscInt vertex    = subVertices[v];
2976efa14ee0SMatthew G Knepley       const PetscInt subvertex = firstSubVertex+v;
2977efa14ee0SMatthew G Knepley       PetscInt       dof;
2978efa14ee0SMatthew G Knepley 
2979*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetDof(coordSection, vertex, &dof));
2980*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionSetDof(subCoordSection, subvertex, dof));
2981*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
2982e6ccafaeSMatthew G Knepley     }
2983*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetUp(subCoordSection));
2984*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetStorageSize(subCoordSection, &coordSize));
2985*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecCreate(PETSC_COMM_SELF, &subCoordinates));
2986*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectGetName((PetscObject)coordinates,&name));
2987*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectSetName((PetscObject)subCoordinates,name));
2988*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
2989*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecSetType(subCoordinates,VECSTANDARD));
2990830e53efSMatthew G. Knepley     if (coordSize) {
2991*5f80ce2aSJacob Faibussowitsch       CHKERRQ(VecGetArray(coordinates,    &coords));
2992*5f80ce2aSJacob Faibussowitsch       CHKERRQ(VecGetArray(subCoordinates, &subCoords));
2993efa14ee0SMatthew G Knepley       for (v = 0; v < numSubVertices; ++v) {
2994efa14ee0SMatthew G Knepley         const PetscInt vertex    = subVertices[v];
2995efa14ee0SMatthew G Knepley         const PetscInt subvertex = firstSubVertex+v;
2996efa14ee0SMatthew G Knepley         PetscInt       dof, off, sdof, soff, d;
2997e6ccafaeSMatthew G Knepley 
2998*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetDof(coordSection, vertex, &dof));
2999*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetOffset(coordSection, vertex, &off));
3000*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
3001*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
30022c71b3e2SJacob Faibussowitsch         PetscCheckFalse(dof != sdof,comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
3003e6ccafaeSMatthew G Knepley         for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3004e6ccafaeSMatthew G Knepley       }
3005*5f80ce2aSJacob Faibussowitsch       CHKERRQ(VecRestoreArray(coordinates,    &coords));
3006*5f80ce2aSJacob Faibussowitsch       CHKERRQ(VecRestoreArray(subCoordinates, &subCoords));
30073b399e24SMatthew G. Knepley     }
3008*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetCoordinatesLocal(subdm, subCoordinates));
3009*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecDestroy(&subCoordinates));
3010e6ccafaeSMatthew G Knepley   }
3011efa14ee0SMatthew G Knepley   /* Cleanup */
3012*5f80ce2aSJacob Faibussowitsch   if (subvertexIS) CHKERRQ(ISRestoreIndices(subvertexIS, &subVertices));
3013*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&subvertexIS));
3014*5f80ce2aSJacob Faibussowitsch   if (subcellIS) CHKERRQ(ISRestoreIndices(subcellIS, &subCells));
3015*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&subcellIS));
3016e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3017e6ccafaeSMatthew G Knepley }
3018e6ccafaeSMatthew G Knepley 
30199fbee547SJacob Faibussowitsch static inline PetscInt DMPlexFilterPoint_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[])
30203982b651SMatthew G. Knepley {
30213982b651SMatthew G. Knepley   PetscInt       subPoint;
30223982b651SMatthew G. Knepley   PetscErrorCode ierr;
30233982b651SMatthew G. Knepley 
30243982b651SMatthew G. Knepley   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint); if (ierr < 0) return ierr;
30253982b651SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint+subPoint;
30263982b651SMatthew G. Knepley }
30273982b651SMatthew G. Knepley 
3028212103e5SMatthew Knepley static PetscErrorCode DMPlexFilterLabels_Internal(DM dm, const PetscInt numSubPoints[], const PetscInt *subpoints[], const PetscInt firstSubPoint[], DM subdm)
3029212103e5SMatthew Knepley {
3030212103e5SMatthew Knepley   PetscInt       Nl, l, d;
3031212103e5SMatthew Knepley 
3032212103e5SMatthew Knepley   PetscFunctionBegin;
3033*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetNumLabels(dm, &Nl));
3034212103e5SMatthew Knepley   for (l = 0; l < Nl; ++l) {
3035212103e5SMatthew Knepley     DMLabel         label, newlabel;
3036212103e5SMatthew Knepley     const char     *lname;
3037d56405f8SMatthew G. Knepley     PetscBool       isDepth, isDim, isCelltype, isVTK;
3038212103e5SMatthew Knepley     IS              valueIS;
3039212103e5SMatthew Knepley     const PetscInt *values;
3040212103e5SMatthew Knepley     PetscInt        Nv, v;
3041212103e5SMatthew Knepley 
3042*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetLabelName(dm, l, &lname));
3043*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcmp(lname, "depth", &isDepth));
3044*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcmp(lname, "dim", &isDim));
3045*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcmp(lname, "celltype", &isCelltype));
3046*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscStrcmp(lname, "vtk", &isVTK));
3047d56405f8SMatthew G. Knepley     if (isDepth || isDim || isCelltype || isVTK) continue;
3048*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMCreateLabel(subdm, lname));
3049*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetLabel(dm, lname, &label));
3050*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetLabel(subdm, lname, &newlabel));
3051*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetDefaultValue(label, &v));
3052*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelSetDefaultValue(newlabel, v));
3053*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetValueIS(label, &valueIS));
3054*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetLocalSize(valueIS, &Nv));
3055*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetIndices(valueIS, &values));
3056212103e5SMatthew Knepley     for (v = 0; v < Nv; ++v) {
3057212103e5SMatthew Knepley       IS              pointIS;
3058212103e5SMatthew Knepley       const PetscInt *points;
3059212103e5SMatthew Knepley       PetscInt        Np, p;
3060212103e5SMatthew Knepley 
3061*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelGetStratumIS(label, values[v], &pointIS));
3062*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISGetLocalSize(pointIS, &Np));
3063*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISGetIndices(pointIS, &points));
3064212103e5SMatthew Knepley       for (p = 0; p < Np; ++p) {
3065212103e5SMatthew Knepley         const PetscInt point = points[p];
3066212103e5SMatthew Knepley         PetscInt       subp;
3067212103e5SMatthew Knepley 
3068*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetPointDepth(dm, point, &d));
3069212103e5SMatthew Knepley         subp = DMPlexFilterPoint_Internal(point, firstSubPoint[d], numSubPoints[d], subpoints[d]);
3070*5f80ce2aSJacob Faibussowitsch         if (subp >= 0) CHKERRQ(DMLabelSetValue(newlabel, subp, values[v]));
3071212103e5SMatthew Knepley       }
3072*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISRestoreIndices(pointIS, &points));
3073*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISDestroy(&pointIS));
3074212103e5SMatthew Knepley     }
3075*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISRestoreIndices(valueIS, &values));
3076*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&valueIS));
3077212103e5SMatthew Knepley   }
3078212103e5SMatthew Knepley   PetscFunctionReturn(0);
3079212103e5SMatthew Knepley }
3080212103e5SMatthew Knepley 
3081158acfadSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmeshGeneric_Interpolated(DM dm, DMLabel label, PetscInt value, PetscBool markedFaces, PetscBool isCohesive, PetscInt cellHeight, DM subdm)
3082e6ccafaeSMatthew G Knepley {
308382f516ccSBarry Smith   MPI_Comm         comm;
308453156dfcSMatthew G. Knepley   DMLabel          subpointMap;
3085efa14ee0SMatthew G Knepley   IS              *subpointIS;
3086efa14ee0SMatthew G Knepley   const PetscInt **subpoints;
30873982b651SMatthew G. Knepley   PetscInt        *numSubPoints, *firstSubPoint, *coneNew, *orntNew;
3088412e9a14SMatthew G. Knepley   PetscInt         totSubPoints = 0, maxConeSize, dim, p, d, v;
30890d366550SMatthew G. Knepley   PetscMPIInt      rank;
3090e6ccafaeSMatthew G Knepley 
3091e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3092*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectGetComm((PetscObject)dm,&comm));
3093*5f80ce2aSJacob Faibussowitsch   CHKERRMPI(MPI_Comm_rank(comm, &rank));
3094efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
3095*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
3096*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexSetSubpointMap(subdm, subpointMap));
3097bec263e5SMatthew G. Knepley   if (cellHeight) {
3098*5f80ce2aSJacob Faibussowitsch     if (isCohesive) CHKERRQ(DMPlexMarkCohesiveSubmesh_Interpolated(dm, label, value, subpointMap, subdm));
3099*5f80ce2aSJacob Faibussowitsch     else            CHKERRQ(DMPlexMarkSubmesh_Interpolated(dm, label, value, markedFaces, subpointMap, subdm));
3100bec263e5SMatthew G. Knepley   } else {
3101bec263e5SMatthew G. Knepley     DMLabel         depth;
3102bec263e5SMatthew G. Knepley     IS              pointIS;
3103bec263e5SMatthew G. Knepley     const PetscInt *points;
3104b85c8bf9SMatthew G. Knepley     PetscInt        numPoints=0;
3105bec263e5SMatthew G. Knepley 
3106*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetDepthLabel(dm, &depth));
3107*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetStratumIS(label, value, &pointIS));
3108b85c8bf9SMatthew G. Knepley     if (pointIS) {
3109*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISGetIndices(pointIS, &points));
3110*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISGetLocalSize(pointIS, &numPoints));
3111b85c8bf9SMatthew G. Knepley     }
3112bec263e5SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
3113bec263e5SMatthew G. Knepley       PetscInt *closure = NULL;
3114bec263e5SMatthew G. Knepley       PetscInt  closureSize, c, pdim;
3115bec263e5SMatthew G. Knepley 
3116*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure));
3117bec263e5SMatthew G. Knepley       for (c = 0; c < closureSize*2; c += 2) {
3118*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMLabelGetValue(depth, closure[c], &pdim));
3119*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMLabelSetValue(subpointMap, closure[c], pdim));
3120bec263e5SMatthew G. Knepley       }
3121*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure));
3122bec263e5SMatthew G. Knepley     }
3123*5f80ce2aSJacob Faibussowitsch     if (pointIS) CHKERRQ(ISRestoreIndices(pointIS, &points));
3124*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&pointIS));
3125bec263e5SMatthew G. Knepley   }
3126efa14ee0SMatthew G Knepley   /* Setup chart */
3127*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetDimension(dm, &dim));
3128*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc4(dim+1,&numSubPoints,dim+1,&firstSubPoint,dim+1,&subpointIS,dim+1,&subpoints));
3129e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
3130*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d]));
3131e6ccafaeSMatthew G Knepley     totSubPoints += numSubPoints[d];
3132e6ccafaeSMatthew G Knepley   }
3133*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexSetChart(subdm, 0, totSubPoints));
3134*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexSetVTKCellHeight(subdm, cellHeight));
3135e6ccafaeSMatthew G Knepley   /* Set cone sizes */
3136e6ccafaeSMatthew G Knepley   firstSubPoint[dim] = 0;
3137e6ccafaeSMatthew G Knepley   firstSubPoint[0]   = firstSubPoint[dim] + numSubPoints[dim];
3138e6ccafaeSMatthew G Knepley   if (dim > 1) {firstSubPoint[dim-1] = firstSubPoint[0]     + numSubPoints[0];}
3139e6ccafaeSMatthew G Knepley   if (dim > 2) {firstSubPoint[dim-2] = firstSubPoint[dim-1] + numSubPoints[dim-1];}
3140e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
3141*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMLabelGetStratumIS(subpointMap, d, &subpointIS[d]));
3142*5f80ce2aSJacob Faibussowitsch     if (subpointIS[d]) CHKERRQ(ISGetIndices(subpointIS[d], &subpoints[d]));
3143e6ccafaeSMatthew G Knepley   }
3144412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
3145*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMCreateLabel(subdm, "celltype"));
3146e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
3147e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3148e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3149e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
3150e6ccafaeSMatthew G Knepley       const PetscInt *cone;
3151e6ccafaeSMatthew G Knepley       PetscInt        coneSize, coneSizeNew, c, val;
3152412e9a14SMatthew G. Knepley       DMPolytopeType  ct;
3153e6ccafaeSMatthew G Knepley 
3154*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetConeSize(dm, point, &coneSize));
3155*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexSetConeSize(subdm, subpoint, coneSize));
3156*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetCellType(dm, point, &ct));
3157*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexSetCellType(subdm, subpoint, ct));
3158bec263e5SMatthew G. Knepley       if (cellHeight && (d == dim)) {
3159*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetCone(dm, point, &cone));
3160e6ccafaeSMatthew G Knepley         for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
3161*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMLabelGetValue(subpointMap, cone[c], &val));
3162e6ccafaeSMatthew G Knepley           if (val >= 0) coneSizeNew++;
3163e6ccafaeSMatthew G Knepley         }
3164*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetConeSize(subdm, subpoint, coneSizeNew));
3165*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexSetCellType(subdm, subpoint, DM_POLYTOPE_FV_GHOST));
3166e6ccafaeSMatthew G Knepley       }
3167e6ccafaeSMatthew G Knepley     }
3168e6ccafaeSMatthew G Knepley   }
3169*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelDestroy(&subpointMap));
3170*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetUp(subdm));
3171e6ccafaeSMatthew G Knepley   /* Set cones */
3172*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
3173*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscMalloc2(maxConeSize,&coneNew,maxConeSize,&orntNew));
3174e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
3175e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3176e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3177e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
31780e49e2e2SMatthew G. Knepley       const PetscInt *cone, *ornt;
31790d366550SMatthew G. Knepley       PetscInt        coneSize, subconeSize, coneSizeNew, c, subc, fornt = 0;
3180e6ccafaeSMatthew G Knepley 
31810d366550SMatthew G. Knepley       if (d == dim-1) {
31820d366550SMatthew G. Knepley         const PetscInt *support, *cone, *ornt;
31830d366550SMatthew G. Knepley         PetscInt        supportSize, coneSize, s, subc;
31840d366550SMatthew G. Knepley 
3185*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetSupport(dm, point, &support));
3186*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMPlexGetSupportSize(dm, point, &supportSize));
31870d366550SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
3188412e9a14SMatthew G. Knepley           PetscBool isHybrid;
3189412e9a14SMatthew G. Knepley 
3190*5f80ce2aSJacob Faibussowitsch           CHKERRQ(DMPlexCellIsHybrid_Internal(dm, support[s], &isHybrid));
3191412e9a14SMatthew G. Knepley           if (!isHybrid) continue;
3192*5f80ce2aSJacob Faibussowitsch           CHKERRQ(PetscFindInt(support[s], numSubPoints[d+1], subpoints[d+1], &subc));
31930d366550SMatthew G. Knepley           if (subc >= 0) {
31940d366550SMatthew G. Knepley             const PetscInt ccell = subpoints[d+1][subc];
31950d366550SMatthew G. Knepley 
3196*5f80ce2aSJacob Faibussowitsch             CHKERRQ(DMPlexGetCone(dm, ccell, &cone));
3197*5f80ce2aSJacob Faibussowitsch             CHKERRQ(DMPlexGetConeSize(dm, ccell, &coneSize));
3198*5f80ce2aSJacob Faibussowitsch             CHKERRQ(DMPlexGetConeOrientation(dm, ccell, &ornt));
31990d366550SMatthew G. Knepley             for (c = 0; c < coneSize; ++c) {
32000d366550SMatthew G. Knepley               if (cone[c] == point) {
32010d366550SMatthew G. Knepley                 fornt = ornt[c];
32020d366550SMatthew G. Knepley                 break;
32030d366550SMatthew G. Knepley               }
32040d366550SMatthew G. Knepley             }
32050d366550SMatthew G. Knepley             break;
32060d366550SMatthew G. Knepley           }
32070d366550SMatthew G. Knepley         }
32080d366550SMatthew G. Knepley       }
3209*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetConeSize(dm, point, &coneSize));
3210*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetConeSize(subdm, subpoint, &subconeSize));
3211*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetCone(dm, point, &cone));
3212*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetConeOrientation(dm, point, &ornt));
3213e6ccafaeSMatthew G Knepley       for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
3214*5f80ce2aSJacob Faibussowitsch         CHKERRQ(PetscFindInt(cone[c], numSubPoints[d-1], subpoints[d-1], &subc));
321501a2673eSMatthew G. Knepley         if (subc >= 0) {
321601a2673eSMatthew G. Knepley           coneNew[coneSizeNew] = firstSubPoint[d-1] + subc;
32173982b651SMatthew G. Knepley           orntNew[coneSizeNew] = ornt[c];
321801a2673eSMatthew G. Knepley           ++coneSizeNew;
321901a2673eSMatthew G. Knepley         }
3220e6ccafaeSMatthew G Knepley       }
32212c71b3e2SJacob Faibussowitsch       PetscCheckFalse(coneSizeNew != subconeSize,comm, PETSC_ERR_PLIB, "Number of cone points located %d does not match subcone size %d", coneSizeNew, subconeSize);
3222*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexSetCone(subdm, subpoint, coneNew));
3223*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexSetConeOrientation(subdm, subpoint, orntNew));
3224*5f80ce2aSJacob Faibussowitsch       if (fornt < 0) CHKERRQ(DMPlexOrientPoint(subdm, subpoint, fornt));
3225e6ccafaeSMatthew G Knepley     }
3226e6ccafaeSMatthew G Knepley   }
3227*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree2(coneNew,orntNew));
3228*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexSymmetrize(subdm));
3229*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexStratify(subdm));
3230e6ccafaeSMatthew G Knepley   /* Build coordinates */
3231e6ccafaeSMatthew G Knepley   {
3232e6ccafaeSMatthew G Knepley     PetscSection coordSection, subCoordSection;
3233e6ccafaeSMatthew G Knepley     Vec          coordinates, subCoordinates;
3234e6ccafaeSMatthew G Knepley     PetscScalar *coords, *subCoords;
3235c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize;
323624640c55SToby Isaac     const char  *name;
3237e6ccafaeSMatthew G Knepley 
3238*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetCoordinateDim(dm, &cdim));
3239*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetCoordinateSection(dm, &coordSection));
3240*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetCoordinatesLocal(dm, &coordinates));
3241*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetCoordinateSection(subdm, &subCoordSection));
3242*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetNumFields(subCoordSection, 1));
3243*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
3244*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
3245*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetChart(subCoordSection, firstSubPoint[0], firstSubPoint[0]+numSubPoints[0]));
3246e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3247e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3248e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0]+v;
3249e6ccafaeSMatthew G Knepley       PetscInt       dof;
3250e6ccafaeSMatthew G Knepley 
3251*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetDof(coordSection, vertex, &dof));
3252*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionSetDof(subCoordSection, subvertex, dof));
3253*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3254e6ccafaeSMatthew G Knepley     }
3255*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetUp(subCoordSection));
3256*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetStorageSize(subCoordSection, &coordSize));
3257*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecCreate(PETSC_COMM_SELF, &subCoordinates));
3258*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectGetName((PetscObject)coordinates,&name));
3259*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectSetName((PetscObject)subCoordinates,name));
3260*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
3261*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecSetBlockSize(subCoordinates, cdim));
3262*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecSetType(subCoordinates,VECSTANDARD));
3263*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecGetArray(coordinates,    &coords));
3264*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecGetArray(subCoordinates, &subCoords));
3265e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3266e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3267e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0]+v;
3268e6ccafaeSMatthew G Knepley       PetscInt dof, off, sdof, soff, d;
3269e6ccafaeSMatthew G Knepley 
3270*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetDof(coordSection, vertex, &dof));
3271*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetOffset(coordSection, vertex, &off));
3272*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
3273*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
32742c71b3e2SJacob Faibussowitsch       PetscCheckFalse(dof != sdof,comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
3275efa14ee0SMatthew G Knepley       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3276e6ccafaeSMatthew G Knepley     }
3277*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecRestoreArray(coordinates,    &coords));
3278*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecRestoreArray(subCoordinates, &subCoords));
3279*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetCoordinatesLocal(subdm, subCoordinates));
3280*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecDestroy(&subCoordinates));
3281e6ccafaeSMatthew G Knepley   }
32823982b651SMatthew G. Knepley   /* Build SF: We need this complexity because subpoints might not be selected on the owning process */
32833982b651SMatthew G. Knepley   {
32843982b651SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
32853982b651SMatthew G. Knepley     IS                 subpIS;
32863982b651SMatthew G. Knepley     const PetscSFNode *remotePoints;
32873982b651SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
32883982b651SMatthew G. Knepley     const PetscInt    *localPoints, *subpoints;
32893982b651SMatthew G. Knepley     PetscInt          *slocalPoints;
32903982b651SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubpoints = 0, numSubroots, numSubleaves = 0, l, sl, ll, pStart, pEnd, p;
32913982b651SMatthew G. Knepley     PetscMPIInt        rank;
32923982b651SMatthew G. Knepley 
3293*5f80ce2aSJacob Faibussowitsch     CHKERRMPI(MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank));
3294*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetPointSF(dm, &sfPoint));
3295*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetPointSF(subdm, &sfPointSub));
3296*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetChart(dm, &pStart, &pEnd));
3297*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetChart(subdm, NULL, &numSubroots));
3298*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetSubpointIS(subdm, &subpIS));
32993982b651SMatthew G. Knepley     if (subpIS) {
3300*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISGetIndices(subpIS, &subpoints));
3301*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISGetLocalSize(subpIS, &numSubpoints));
33023982b651SMatthew G. Knepley     }
3303*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints));
33043982b651SMatthew G. Knepley     if (numRoots >= 0) {
3305*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscMalloc2(pEnd-pStart,&newLocalPoints,numRoots,&newOwners));
33063982b651SMatthew G. Knepley       for (p = 0; p < pEnd-pStart; ++p) {
33073982b651SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
33083982b651SMatthew G. Knepley         newLocalPoints[p].index = -2;
33093982b651SMatthew G. Knepley       }
33103982b651SMatthew G. Knepley       /* Set subleaves */
33113982b651SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
33123982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
33133982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(point, 0, numSubpoints, subpoints);
33143982b651SMatthew G. Knepley 
33153982b651SMatthew G. Knepley         if (subpoint < 0) continue;
33163982b651SMatthew G. Knepley         newLocalPoints[point-pStart].rank  = rank;
33173982b651SMatthew G. Knepley         newLocalPoints[point-pStart].index = subpoint;
33183982b651SMatthew G. Knepley         ++numSubleaves;
33193982b651SMatthew G. Knepley       }
33203982b651SMatthew G. Knepley       /* Must put in owned subpoints */
33213982b651SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
33223982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(p, 0, numSubpoints, subpoints);
33233982b651SMatthew G. Knepley 
33243982b651SMatthew G. Knepley         if (subpoint < 0) {
33253982b651SMatthew G. Knepley           newOwners[p-pStart].rank  = -3;
33263982b651SMatthew G. Knepley           newOwners[p-pStart].index = -3;
33273982b651SMatthew G. Knepley         } else {
33283982b651SMatthew G. Knepley           newOwners[p-pStart].rank  = rank;
33293982b651SMatthew G. Knepley           newOwners[p-pStart].index = subpoint;
33303982b651SMatthew G. Knepley         }
33313982b651SMatthew G. Knepley       }
3332*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
3333*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
3334*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints,MPI_REPLACE));
3335*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints,MPI_REPLACE));
3336*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscMalloc1(numSubleaves, &slocalPoints));
3337*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscMalloc1(numSubleaves, &sremotePoints));
33383982b651SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
33393982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
33403982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(point, 0, numSubpoints, subpoints);
33413982b651SMatthew G. Knepley 
33423982b651SMatthew G. Knepley         if (subpoint < 0) continue;
33433982b651SMatthew G. Knepley         if (newLocalPoints[point].rank == rank) {++ll; continue;}
33443982b651SMatthew G. Knepley         slocalPoints[sl]        = subpoint;
33453982b651SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
33463982b651SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
33472c71b3e2SJacob Faibussowitsch         PetscCheckFalse(sremotePoints[sl].rank  < 0,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %d", point);
33482c71b3e2SJacob Faibussowitsch         PetscCheckFalse(sremotePoints[sl].index < 0,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %d", point);
33493982b651SMatthew G. Knepley         ++sl;
33503982b651SMatthew G. Knepley       }
33512c71b3e2SJacob Faibussowitsch       PetscCheckFalse(sl + ll != numSubleaves,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %d + %d != %d", sl, ll, numSubleaves);
3352*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFree2(newLocalPoints,newOwners));
3353*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSFSetGraph(sfPointSub, numSubroots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER));
33543982b651SMatthew G. Knepley     }
33553982b651SMatthew G. Knepley     if (subpIS) {
3356*5f80ce2aSJacob Faibussowitsch       CHKERRQ(ISRestoreIndices(subpIS, &subpoints));
33573982b651SMatthew G. Knepley     }
33583982b651SMatthew G. Knepley   }
3359212103e5SMatthew Knepley   /* Filter labels */
3360*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexFilterLabels_Internal(dm, numSubPoints, subpoints, firstSubPoint, subdm));
3361efa14ee0SMatthew G Knepley   /* Cleanup */
3362e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
3363*5f80ce2aSJacob Faibussowitsch     if (subpointIS[d]) CHKERRQ(ISRestoreIndices(subpointIS[d], &subpoints[d]));
3364*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISDestroy(&subpointIS[d]));
3365e6ccafaeSMatthew G Knepley   }
3366*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree4(numSubPoints,firstSubPoint,subpointIS,subpoints));
3367e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3368e6ccafaeSMatthew G Knepley }
3369e6ccafaeSMatthew G Knepley 
3370158acfadSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM subdm)
33713982b651SMatthew G. Knepley {
33723982b651SMatthew G. Knepley   PetscFunctionBegin;
3373*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexCreateSubmeshGeneric_Interpolated(dm, vertexLabel, value, markedFaces, PETSC_FALSE, 1, subdm));
33743982b651SMatthew G. Knepley   PetscFunctionReturn(0);
33753982b651SMatthew G. Knepley }
33763982b651SMatthew G. Knepley 
3377d0fa310fSMatthew G. Knepley /*@
3378e6ccafaeSMatthew G Knepley   DMPlexCreateSubmesh - Extract a hypersurface from the mesh using vertices defined by a label
3379e6ccafaeSMatthew G Knepley 
3380e6ccafaeSMatthew G Knepley   Input Parameters:
3381e6ccafaeSMatthew G Knepley + dm           - The original mesh
3382158acfadSMatthew G. Knepley . vertexLabel  - The DMLabel marking points contained in the surface
3383158acfadSMatthew G. Knepley . value        - The label value to use
3384158acfadSMatthew G. Knepley - markedFaces  - PETSC_TRUE if surface faces are marked in addition to vertices, PETSC_FALSE if only vertices are marked
3385e6ccafaeSMatthew G Knepley 
3386e6ccafaeSMatthew G Knepley   Output Parameter:
3387e6ccafaeSMatthew G Knepley . subdm - The surface mesh
3388e6ccafaeSMatthew G Knepley 
3389e6ccafaeSMatthew G Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3390e6ccafaeSMatthew G Knepley 
3391e6ccafaeSMatthew G Knepley   Level: developer
3392e6ccafaeSMatthew G Knepley 
3393c58f1c22SToby Isaac .seealso: DMPlexGetSubpointMap(), DMGetLabel(), DMLabelSetValue()
3394830e53efSMatthew G. Knepley @*/
3395158acfadSMatthew G. Knepley PetscErrorCode DMPlexCreateSubmesh(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM *subdm)
3396e6ccafaeSMatthew G Knepley {
3397827c4036SVaclav Hapla   DMPlexInterpolatedFlag interpolated;
3398827c4036SVaclav Hapla   PetscInt       dim, cdim;
3399e6ccafaeSMatthew G Knepley 
3400e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3401e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3402064a246eSJacob Faibussowitsch   PetscValidPointer(subdm, 5);
3403*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetDimension(dm, &dim));
3404*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
3405*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetType(*subdm, DMPLEX));
3406*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetDimension(*subdm, dim-1));
3407*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinateDim(dm, &cdim));
3408*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetCoordinateDim(*subdm, cdim));
3409*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexIsInterpolated(dm, &interpolated));
34102c71b3e2SJacob Faibussowitsch   PetscCheckFalse(interpolated == DMPLEX_INTERPOLATED_PARTIAL,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not for partially interpolated meshes");
3411827c4036SVaclav Hapla   if (interpolated) {
3412*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexCreateSubmesh_Interpolated(dm, vertexLabel, value, markedFaces, *subdm));
3413e6ccafaeSMatthew G Knepley   } else {
3414*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexCreateSubmesh_Uninterpolated(dm, vertexLabel, value, *subdm));
3415e6ccafaeSMatthew G Knepley   }
3416*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexCopy_Internal(dm, PETSC_TRUE, *subdm));
3417e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3418e6ccafaeSMatthew G Knepley }
3419e6ccafaeSMatthew G Knepley 
342027c04023SMatthew G. Knepley static PetscErrorCode DMPlexCreateCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM subdm)
3421766ab985SMatthew G. Knepley {
3422766ab985SMatthew G. Knepley   MPI_Comm        comm;
3423766ab985SMatthew G. Knepley   DMLabel         subpointMap;
3424766ab985SMatthew G. Knepley   IS              subvertexIS;
3425766ab985SMatthew G. Knepley   const PetscInt *subVertices;
3426fed694aaSMatthew G. Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells, *subCells = NULL;
3427766ab985SMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces, firstSubFace, newFacePoint, nFV;
3428412e9a14SMatthew G. Knepley   PetscInt        c, f;
3429766ab985SMatthew G. Knepley 
3430766ab985SMatthew G. Knepley   PetscFunctionBegin;
3431*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectGetComm((PetscObject)dm, &comm));
3432766ab985SMatthew G. Knepley   /* Create subpointMap which marks the submesh */
3433*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap));
3434*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexSetSubpointMap(subdm, subpointMap));
3435*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelDestroy(&subpointMap));
3436*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexMarkCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, subpointMap, &numSubFaces, &nFV, &subCells, subdm));
3437766ab985SMatthew G. Knepley   /* Setup chart */
3438*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelGetStratumSize(subpointMap, 0, &numSubVertices));
3439*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelGetStratumSize(subpointMap, 2, &numSubCells));
3440*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices));
3441*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexSetVTKCellHeight(subdm, 1));
3442766ab985SMatthew G. Knepley   /* Set cone sizes */
3443766ab985SMatthew G. Knepley   firstSubVertex = numSubCells;
3444766ab985SMatthew G. Knepley   firstSubFace   = numSubCells+numSubVertices;
3445766ab985SMatthew G. Knepley   newFacePoint   = firstSubFace;
3446*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelGetStratumIS(subpointMap, 0, &subvertexIS));
3447*5f80ce2aSJacob Faibussowitsch   if (subvertexIS) CHKERRQ(ISGetIndices(subvertexIS, &subVertices));
3448766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3449*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexSetConeSize(subdm, c, 1));
3450766ab985SMatthew G. Knepley   }
3451766ab985SMatthew G. Knepley   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
3452*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexSetConeSize(subdm, f, nFV));
3453766ab985SMatthew G. Knepley   }
3454*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetUp(subdm));
3455766ab985SMatthew G. Knepley   /* Create face cones */
3456*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetMaxSizes(dm, &maxConeSize, NULL));
3457*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface));
3458766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3459766ab985SMatthew G. Knepley     const PetscInt  cell    = subCells[c];
3460766ab985SMatthew G. Knepley     const PetscInt  subcell = c;
346187feddfdSMatthew G. Knepley     const PetscInt *cone, *cells;
3462412e9a14SMatthew G. Knepley     PetscBool       isHybrid;
346387feddfdSMatthew G. Knepley     PetscInt        numCells, subVertex, p, v;
3464766ab985SMatthew G. Knepley 
3465*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexCellIsHybrid_Internal(dm, cell, &isHybrid));
3466412e9a14SMatthew G. Knepley     if (!isHybrid) continue;
3467*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetCone(dm, cell, &cone));
346887feddfdSMatthew G. Knepley     for (v = 0; v < nFV; ++v) {
3469*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFindInt(cone[v], numSubVertices, subVertices, &subVertex));
347087feddfdSMatthew G. Knepley       subface[v] = firstSubVertex+subVertex;
347187feddfdSMatthew G. Knepley     }
3472*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexSetCone(subdm, newFacePoint, subface));
3473*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexSetCone(subdm, subcell, &newFacePoint));
3474*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetJoin(dm, nFV, cone, &numCells, &cells));
347527234c99SMatthew G. Knepley     /* Not true in parallel
34762c71b3e2SJacob Faibussowitsch     PetscCheckFalse(numCells != 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
347787feddfdSMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
347887feddfdSMatthew G. Knepley       PetscInt  negsubcell;
3479412e9a14SMatthew G. Knepley       PetscBool isHybrid;
3480766ab985SMatthew G. Knepley 
3481*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexCellIsHybrid_Internal(dm, cells[p], &isHybrid));
3482412e9a14SMatthew G. Knepley       if (isHybrid) continue;
348387feddfdSMatthew G. Knepley       /* I know this is a crap search */
348487feddfdSMatthew G. Knepley       for (negsubcell = 0; negsubcell < numSubCells; ++negsubcell) {
348587feddfdSMatthew G. Knepley         if (subCells[negsubcell] == cells[p]) break;
3486766ab985SMatthew G. Knepley       }
34872c71b3e2SJacob Faibussowitsch       PetscCheckFalse(negsubcell == numSubCells,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find negative face neighbor for cohesive cell %d", cell);
3488*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexSetCone(subdm, negsubcell, &newFacePoint));
3489766ab985SMatthew G. Knepley     }
3490*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexRestoreJoin(dm, nFV, cone, &numCells, &cells));
349187feddfdSMatthew G. Knepley     ++newFacePoint;
3492766ab985SMatthew G. Knepley   }
3493*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface));
3494*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexSymmetrize(subdm));
3495*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexStratify(subdm));
3496766ab985SMatthew G. Knepley   /* Build coordinates */
3497766ab985SMatthew G. Knepley   {
3498766ab985SMatthew G. Knepley     PetscSection coordSection, subCoordSection;
3499766ab985SMatthew G. Knepley     Vec          coordinates, subCoordinates;
3500766ab985SMatthew G. Knepley     PetscScalar *coords, *subCoords;
3501c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize, v;
350224640c55SToby Isaac     const char  *name;
3503766ab985SMatthew G. Knepley 
3504*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetCoordinateDim(dm, &cdim));
3505*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetCoordinateSection(dm, &coordSection));
3506*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetCoordinatesLocal(dm, &coordinates));
3507*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetCoordinateSection(subdm, &subCoordSection));
3508*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetNumFields(subCoordSection, 1));
3509*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetFieldComponents(coordSection, 0, &numComp));
3510*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetFieldComponents(subCoordSection, 0, numComp));
3511*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices));
3512766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3513766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3514766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex+v;
3515766ab985SMatthew G. Knepley       PetscInt       dof;
3516766ab985SMatthew G. Knepley 
3517*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetDof(coordSection, vertex, &dof));
3518*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionSetDof(subCoordSection, subvertex, dof));
3519*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof));
3520766ab985SMatthew G. Knepley     }
3521*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionSetUp(subCoordSection));
3522*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSectionGetStorageSize(subCoordSection, &coordSize));
3523*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecCreate(PETSC_COMM_SELF, &subCoordinates));
3524*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectGetName((PetscObject)coordinates,&name));
3525*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectSetName((PetscObject)subCoordinates,name));
3526*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE));
3527*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecSetBlockSize(subCoordinates, cdim));
3528*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecSetType(subCoordinates,VECSTANDARD));
3529*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecGetArray(coordinates,    &coords));
3530*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecGetArray(subCoordinates, &subCoords));
3531766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3532766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3533766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex+v;
3534766ab985SMatthew G. Knepley       PetscInt       dof, off, sdof, soff, d;
3535766ab985SMatthew G. Knepley 
3536*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetDof(coordSection, vertex, &dof));
3537*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetOffset(coordSection, vertex, &off));
3538*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetDof(subCoordSection, subvertex, &sdof));
3539*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSectionGetOffset(subCoordSection, subvertex, &soff));
35402c71b3e2SJacob Faibussowitsch       PetscCheckFalse(dof != sdof,comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
3541766ab985SMatthew G. Knepley       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3542766ab985SMatthew G. Knepley     }
3543*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecRestoreArray(coordinates,    &coords));
3544*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecRestoreArray(subCoordinates, &subCoords));
3545*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMSetCoordinatesLocal(subdm, subCoordinates));
3546*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecDestroy(&subCoordinates));
3547766ab985SMatthew G. Knepley   }
3548aca35d17SMatthew G. Knepley   /* Build SF */
3549aca35d17SMatthew G. Knepley   CHKMEMQ;
3550aca35d17SMatthew G. Knepley   {
3551aca35d17SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
3552aca35d17SMatthew G. Knepley     const PetscSFNode *remotePoints;
3553bdcf2095SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
3554aca35d17SMatthew G. Knepley     const PetscInt    *localPoints;
3555bdcf2095SMatthew G. Knepley     PetscInt          *slocalPoints;
355649c26ae4SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubRoots = numSubCells+numSubFaces+numSubVertices, numSubLeaves = 0, l, sl, ll, pStart, pEnd, p, vStart, vEnd;
3557bdcf2095SMatthew G. Knepley     PetscMPIInt        rank;
3558aca35d17SMatthew G. Knepley 
3559*5f80ce2aSJacob Faibussowitsch     CHKERRMPI(MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank));
3560*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetPointSF(dm, &sfPoint));
3561*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetPointSF(subdm, &sfPointSub));
3562*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetChart(dm, &pStart, &pEnd));
3563*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
3564*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints));
3565aca35d17SMatthew G. Knepley     if (numRoots >= 0) {
3566aca35d17SMatthew G. Knepley       /* Only vertices should be shared */
3567*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscMalloc2(pEnd-pStart,&newLocalPoints,numRoots,&newOwners));
3568bdcf2095SMatthew G. Knepley       for (p = 0; p < pEnd-pStart; ++p) {
3569bdcf2095SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
3570bdcf2095SMatthew G. Knepley         newLocalPoints[p].index = -2;
3571bdcf2095SMatthew G. Knepley       }
35729e0823b2SMatthew G. Knepley       /* Set subleaves */
3573aca35d17SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
3574aca35d17SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3575bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3576aca35d17SMatthew G. Knepley 
35772c71b3e2SJacob Faibussowitsch         PetscCheckFalse((point < vStart) && (point >= vEnd),PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Should not be mapping anything but vertices, %d", point);
3578bdcf2095SMatthew G. Knepley         if (subPoint < 0) continue;
3579bdcf2095SMatthew G. Knepley         newLocalPoints[point-pStart].rank  = rank;
3580bdcf2095SMatthew G. Knepley         newLocalPoints[point-pStart].index = subPoint;
3581bdcf2095SMatthew G. Knepley         ++numSubLeaves;
3582aca35d17SMatthew G. Knepley       }
35839e0823b2SMatthew G. Knepley       /* Must put in owned subpoints */
35849e0823b2SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
35859e0823b2SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(p, firstSubVertex, numSubVertices, subVertices);
35869e0823b2SMatthew G. Knepley 
35879e0823b2SMatthew G. Knepley         if (subPoint < 0) {
35889e0823b2SMatthew G. Knepley           newOwners[p-pStart].rank  = -3;
35899e0823b2SMatthew G. Knepley           newOwners[p-pStart].index = -3;
35909e0823b2SMatthew G. Knepley         } else {
35919e0823b2SMatthew G. Knepley           newOwners[p-pStart].rank  = rank;
35929e0823b2SMatthew G. Knepley           newOwners[p-pStart].index = subPoint;
35939e0823b2SMatthew G. Knepley         }
3594bdcf2095SMatthew G. Knepley       }
3595*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
3596*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC));
3597*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints,MPI_REPLACE));
3598*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints,MPI_REPLACE));
3599*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscMalloc1(numSubLeaves,    &slocalPoints));
3600*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscMalloc1(numSubLeaves, &sremotePoints));
360149c26ae4SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
3602bdcf2095SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3603bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3604aca35d17SMatthew G. Knepley 
3605aca35d17SMatthew G. Knepley         if (subPoint < 0) continue;
360649c26ae4SMatthew G. Knepley         if (newLocalPoints[point].rank == rank) {++ll; continue;}
3607aca35d17SMatthew G. Knepley         slocalPoints[sl]        = subPoint;
3608bdcf2095SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
3609bdcf2095SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
36102c71b3e2SJacob Faibussowitsch         PetscCheckFalse(sremotePoints[sl].rank  < 0,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %d", point);
36112c71b3e2SJacob Faibussowitsch         PetscCheckFalse(sremotePoints[sl].index < 0,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %d", point);
3612aca35d17SMatthew G. Knepley         ++sl;
3613aca35d17SMatthew G. Knepley       }
3614*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscFree2(newLocalPoints,newOwners));
36152c71b3e2SJacob Faibussowitsch       PetscCheckFalse(sl + ll != numSubLeaves,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %d + %d != %d", sl, ll, numSubLeaves);
3616*5f80ce2aSJacob Faibussowitsch       CHKERRQ(PetscSFSetGraph(sfPointSub, numSubRoots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER));
3617aca35d17SMatthew G. Knepley     }
3618aca35d17SMatthew G. Knepley   }
3619aca35d17SMatthew G. Knepley   CHKMEMQ;
3620766ab985SMatthew G. Knepley   /* Cleanup */
3621*5f80ce2aSJacob Faibussowitsch   if (subvertexIS) CHKERRQ(ISRestoreIndices(subvertexIS, &subVertices));
3622*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ISDestroy(&subvertexIS));
3623*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscFree(subCells));
3624766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
3625766ab985SMatthew G. Knepley }
3626766ab985SMatthew G. Knepley 
36273982b651SMatthew G. Knepley static PetscErrorCode DMPlexCreateCohesiveSubmesh_Interpolated(DM dm, const char labelname[], PetscInt value, DM subdm)
3628766ab985SMatthew G. Knepley {
36293982b651SMatthew G. Knepley   DMLabel        label = NULL;
3630766ab985SMatthew G. Knepley 
3631766ab985SMatthew G. Knepley   PetscFunctionBegin;
3632*5f80ce2aSJacob Faibussowitsch   if (labelname) CHKERRQ(DMGetLabel(dm, labelname, &label));
3633*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexCreateSubmeshGeneric_Interpolated(dm, label, value, PETSC_FALSE, PETSC_TRUE, 1, subdm));
3634766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
3635766ab985SMatthew G. Knepley }
3636766ab985SMatthew G. Knepley 
3637c08575a3SMatthew G. Knepley /*@C
36387d2fefb6SMatthew 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.
3639766ab985SMatthew G. Knepley 
3640766ab985SMatthew G. Knepley   Input Parameters:
3641766ab985SMatthew G. Knepley + dm          - The original mesh
364227c04023SMatthew G. Knepley . hasLagrange - The mesh has Lagrange unknowns in the cohesive cells
36437afc1a8bSJed Brown . label       - A label name, or NULL
364427c04023SMatthew G. Knepley - value  - A label value
3645766ab985SMatthew G. Knepley 
3646766ab985SMatthew G. Knepley   Output Parameter:
3647766ab985SMatthew G. Knepley . subdm - The surface mesh
3648766ab985SMatthew G. Knepley 
3649766ab985SMatthew G. Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3650766ab985SMatthew G. Knepley 
3651766ab985SMatthew G. Knepley   Level: developer
3652766ab985SMatthew G. Knepley 
3653766ab985SMatthew G. Knepley .seealso: DMPlexGetSubpointMap(), DMPlexCreateSubmesh()
3654c08575a3SMatthew G. Knepley @*/
365527c04023SMatthew G. Knepley PetscErrorCode DMPlexCreateCohesiveSubmesh(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM *subdm)
3656766ab985SMatthew G. Knepley {
3657c0e8cf5fSMatthew G. Knepley   PetscInt       dim, cdim, depth;
3658766ab985SMatthew G. Knepley 
3659766ab985SMatthew G. Knepley   PetscFunctionBegin;
3660766ab985SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
366127c04023SMatthew G. Knepley   PetscValidPointer(subdm, 5);
3662*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetDimension(dm, &dim));
3663*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetDepth(dm, &depth));
3664*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMCreate(PetscObjectComm((PetscObject)dm), subdm));
3665*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetType(*subdm, DMPLEX));
3666*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetDimension(*subdm, dim-1));
3667*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetCoordinateDim(dm, &cdim));
3668*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetCoordinateDim(*subdm, cdim));
3669766ab985SMatthew G. Knepley   if (depth == dim) {
3670*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexCreateCohesiveSubmesh_Interpolated(dm, label, value, *subdm));
3671766ab985SMatthew G. Knepley   } else {
3672*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexCreateCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, *subdm));
3673e6ccafaeSMatthew G Knepley   }
3674*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexCopy_Internal(dm, PETSC_TRUE, *subdm));
3675e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3676e6ccafaeSMatthew G Knepley }
3677e6ccafaeSMatthew G Knepley 
3678bec263e5SMatthew G. Knepley /*@
3679bec263e5SMatthew G. Knepley   DMPlexFilter - Extract a subset of mesh cells defined by a label as a separate mesh
3680bec263e5SMatthew G. Knepley 
3681bec263e5SMatthew G. Knepley   Input Parameters:
3682bec263e5SMatthew G. Knepley + dm        - The original mesh
3683bec263e5SMatthew G. Knepley . cellLabel - The DMLabel marking cells contained in the new mesh
3684bec263e5SMatthew G. Knepley - value     - The label value to use
3685bec263e5SMatthew G. Knepley 
3686bec263e5SMatthew G. Knepley   Output Parameter:
3687bec263e5SMatthew G. Knepley . subdm - The new mesh
3688bec263e5SMatthew G. Knepley 
3689bec263e5SMatthew G. Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3690bec263e5SMatthew G. Knepley 
3691bec263e5SMatthew G. Knepley   Level: developer
3692bec263e5SMatthew G. Knepley 
3693c58f1c22SToby Isaac .seealso: DMPlexGetSubpointMap(), DMGetLabel(), DMLabelSetValue()
3694bec263e5SMatthew G. Knepley @*/
3695bec263e5SMatthew G. Knepley PetscErrorCode DMPlexFilter(DM dm, DMLabel cellLabel, PetscInt value, DM *subdm)
3696bec263e5SMatthew G. Knepley {
3697bec263e5SMatthew G. Knepley   PetscInt       dim;
3698bec263e5SMatthew G. Knepley 
3699bec263e5SMatthew G. Knepley   PetscFunctionBegin;
3700bec263e5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3701064a246eSJacob Faibussowitsch   PetscValidPointer(subdm, 4);
3702*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMGetDimension(dm, &dim));
3703*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMCreate(PetscObjectComm((PetscObject) dm), subdm));
3704*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetType(*subdm, DMPLEX));
3705*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetDimension(*subdm, dim));
3706bec263e5SMatthew G. Knepley   /* Extract submesh in place, could be empty on some procs, could have inconsistency if procs do not both extract a shared cell */
3707*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexCreateSubmeshGeneric_Interpolated(dm, cellLabel, value, PETSC_FALSE, PETSC_FALSE, 0, *subdm));
3708*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexCopy_Internal(dm, PETSC_TRUE, *subdm));
3709bec263e5SMatthew G. Knepley   PetscFunctionReturn(0);
3710bec263e5SMatthew G. Knepley }
3711bec263e5SMatthew G. Knepley 
371264beef6dSMatthew G. Knepley /*@
371364beef6dSMatthew G. Knepley   DMPlexGetSubpointMap - Returns a DMLabel with point dimension as values
371464beef6dSMatthew G. Knepley 
371564beef6dSMatthew G. Knepley   Input Parameter:
371664beef6dSMatthew G. Knepley . dm - The submesh DM
371764beef6dSMatthew G. Knepley 
371864beef6dSMatthew G. Knepley   Output Parameter:
371964beef6dSMatthew G. Knepley . subpointMap - The DMLabel of all the points from the original mesh in this submesh, or NULL if this is not a submesh
372064beef6dSMatthew G. Knepley 
372164beef6dSMatthew G. Knepley   Level: developer
372264beef6dSMatthew G. Knepley 
372397d8846cSMatthew Knepley .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointIS()
372464beef6dSMatthew G. Knepley @*/
3725e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexGetSubpointMap(DM dm, DMLabel *subpointMap)
3726e6ccafaeSMatthew G Knepley {
3727e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3728e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3729e6ccafaeSMatthew G Knepley   PetscValidPointer(subpointMap, 2);
373065663942SMatthew G. Knepley   *subpointMap = ((DM_Plex*) dm->data)->subpointMap;
3731e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3732e6ccafaeSMatthew G Knepley }
3733e6ccafaeSMatthew G Knepley 
3734c08575a3SMatthew G. Knepley /*@
3735c08575a3SMatthew G. Knepley   DMPlexSetSubpointMap - Sets the DMLabel with point dimension as values
3736c08575a3SMatthew G. Knepley 
3737c08575a3SMatthew G. Knepley   Input Parameters:
3738c08575a3SMatthew G. Knepley + dm - The submesh DM
3739c08575a3SMatthew G. Knepley - subpointMap - The DMLabel of all the points from the original mesh in this submesh
3740c08575a3SMatthew G. Knepley 
3741c08575a3SMatthew G. Knepley   Note: Should normally not be called by the user, since it is set in DMPlexCreateSubmesh()
3742c08575a3SMatthew G. Knepley 
3743c08575a3SMatthew G. Knepley   Level: developer
3744c08575a3SMatthew G. Knepley 
374597d8846cSMatthew Knepley .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointIS()
3746c08575a3SMatthew G. Knepley @*/
3747e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexSetSubpointMap(DM dm, DMLabel subpointMap)
3748e6ccafaeSMatthew G Knepley {
3749e6ccafaeSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
3750285d324eSMatthew G. Knepley   DMLabel        tmp;
3751e6ccafaeSMatthew G Knepley 
3752e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3753e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3754285d324eSMatthew G. Knepley   tmp  = mesh->subpointMap;
3755e6ccafaeSMatthew G Knepley   mesh->subpointMap = subpointMap;
3756*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectReference((PetscObject) mesh->subpointMap));
3757*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMLabelDestroy(&tmp));
3758e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3759e6ccafaeSMatthew G Knepley }
3760e6ccafaeSMatthew G Knepley 
376197d8846cSMatthew Knepley static PetscErrorCode DMPlexCreateSubpointIS_Internal(DM dm, IS *subpointIS)
376297d8846cSMatthew Knepley {
376397d8846cSMatthew Knepley   DMLabel        spmap;
376497d8846cSMatthew Knepley   PetscInt       depth, d;
376597d8846cSMatthew Knepley 
376697d8846cSMatthew Knepley   PetscFunctionBegin;
3767*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetSubpointMap(dm, &spmap));
3768*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetDepth(dm, &depth));
376997d8846cSMatthew Knepley   if (spmap && depth >= 0) {
377097d8846cSMatthew Knepley     DM_Plex  *mesh = (DM_Plex *) dm->data;
377197d8846cSMatthew Knepley     PetscInt *points, *depths;
377297d8846cSMatthew Knepley     PetscInt  pStart, pEnd, p, off;
377397d8846cSMatthew Knepley 
3774*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetChart(dm, &pStart, &pEnd));
37752c71b3e2SJacob Faibussowitsch     PetscCheckFalse(pStart,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Submeshes must start the point numbering at 0, not %d", pStart);
3776*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscMalloc1(pEnd, &points));
3777*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMGetWorkArray(dm, depth+1, MPIU_INT, &depths));
377897d8846cSMatthew Knepley     depths[0] = depth;
377997d8846cSMatthew Knepley     depths[1] = 0;
378097d8846cSMatthew Knepley     for (d = 2; d <= depth; ++d) {depths[d] = depth+1 - d;}
378197d8846cSMatthew Knepley     for (d = 0, off = 0; d <= depth; ++d) {
378297d8846cSMatthew Knepley       const PetscInt dep = depths[d];
378397d8846cSMatthew Knepley       PetscInt       depStart, depEnd, n;
378497d8846cSMatthew Knepley 
3785*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMPlexGetDepthStratum(dm, dep, &depStart, &depEnd));
3786*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMLabelGetStratumSize(spmap, dep, &n));
378797d8846cSMatthew Knepley       if (((d < 2) && (depth > 1)) || (d == 1)) { /* Only check vertices and cells for now since the map is broken for others */
37882c71b3e2SJacob Faibussowitsch         PetscCheckFalse(n != depEnd-depStart,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %d at depth %d should be %d", n, dep, depEnd-depStart);
378997d8846cSMatthew Knepley       } else {
379097d8846cSMatthew Knepley         if (!n) {
379197d8846cSMatthew Knepley           if (d == 0) {
379297d8846cSMatthew Knepley             /* Missing cells */
379397d8846cSMatthew Knepley             for (p = 0; p < depEnd-depStart; ++p, ++off) points[off] = -1;
379497d8846cSMatthew Knepley           } else {
379597d8846cSMatthew Knepley             /* Missing faces */
379697d8846cSMatthew Knepley             for (p = 0; p < depEnd-depStart; ++p, ++off) points[off] = PETSC_MAX_INT;
379797d8846cSMatthew Knepley           }
379897d8846cSMatthew Knepley         }
379997d8846cSMatthew Knepley       }
380097d8846cSMatthew Knepley       if (n) {
380197d8846cSMatthew Knepley         IS              is;
380297d8846cSMatthew Knepley         const PetscInt *opoints;
380397d8846cSMatthew Knepley 
3804*5f80ce2aSJacob Faibussowitsch         CHKERRQ(DMLabelGetStratumIS(spmap, dep, &is));
3805*5f80ce2aSJacob Faibussowitsch         CHKERRQ(ISGetIndices(is, &opoints));
380697d8846cSMatthew Knepley         for (p = 0; p < n; ++p, ++off) points[off] = opoints[p];
3807*5f80ce2aSJacob Faibussowitsch         CHKERRQ(ISRestoreIndices(is, &opoints));
3808*5f80ce2aSJacob Faibussowitsch         CHKERRQ(ISDestroy(&is));
380997d8846cSMatthew Knepley       }
381097d8846cSMatthew Knepley     }
3811*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMRestoreWorkArray(dm, depth+1, MPIU_INT, &depths));
38122c71b3e2SJacob Faibussowitsch     PetscCheckFalse(off != pEnd,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %d should be %d", off, pEnd);
3813*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISCreateGeneral(PETSC_COMM_SELF, pEnd, points, PETSC_OWN_POINTER, subpointIS));
3814*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscObjectStateGet((PetscObject) spmap, &mesh->subpointState));
381597d8846cSMatthew Knepley   }
381697d8846cSMatthew Knepley   PetscFunctionReturn(0);
381797d8846cSMatthew Knepley }
381897d8846cSMatthew Knepley 
381964beef6dSMatthew G. Knepley /*@
382097d8846cSMatthew Knepley   DMPlexGetSubpointIS - Returns an IS covering the entire subdm chart with the original points as data
3821e6ccafaeSMatthew G Knepley 
3822e6ccafaeSMatthew G Knepley   Input Parameter:
3823e6ccafaeSMatthew G Knepley . dm - The submesh DM
3824e6ccafaeSMatthew G Knepley 
3825e6ccafaeSMatthew G Knepley   Output Parameter:
38260298fd71SBarry Smith . subpointIS - The IS of all the points from the original mesh in this submesh, or NULL if this is not a submesh
3827e6ccafaeSMatthew G Knepley 
38283982b651SMatthew G. Knepley   Note: This IS is guaranteed to be sorted by the construction of the submesh
382964beef6dSMatthew G. Knepley 
383064beef6dSMatthew G. Knepley   Level: developer
383164beef6dSMatthew G. Knepley 
383264beef6dSMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointMap()
383364beef6dSMatthew G. Knepley @*/
383497d8846cSMatthew Knepley PetscErrorCode DMPlexGetSubpointIS(DM dm, IS *subpointIS)
3835e6ccafaeSMatthew G Knepley {
383697d8846cSMatthew Knepley   DM_Plex         *mesh = (DM_Plex *) dm->data;
383797d8846cSMatthew Knepley   DMLabel          spmap;
383897d8846cSMatthew Knepley   PetscObjectState state;
3839e6ccafaeSMatthew G Knepley 
3840e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3841e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3842e6ccafaeSMatthew G Knepley   PetscValidPointer(subpointIS, 2);
3843*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetSubpointMap(dm, &spmap));
3844*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectStateGet((PetscObject) spmap, &state));
3845*5f80ce2aSJacob Faibussowitsch   if (state != mesh->subpointState || !mesh->subpointIS) CHKERRQ(DMPlexCreateSubpointIS_Internal(dm, &mesh->subpointIS));
384697d8846cSMatthew Knepley   *subpointIS = mesh->subpointIS;
3847e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3848e6ccafaeSMatthew G Knepley }
3849559a1558SMatthew G. Knepley 
3850559a1558SMatthew G. Knepley /*@
3851a6e0b375SMatthew G. Knepley   DMGetEnclosureRelation - Get the relationship between dmA and dmB
3852559a1558SMatthew G. Knepley 
3853559a1558SMatthew G. Knepley   Input Parameters:
3854a6e0b375SMatthew G. Knepley + dmA - The first DM
3855a6e0b375SMatthew G. Knepley - dmB - The second DM
3856559a1558SMatthew G. Knepley 
3857559a1558SMatthew G. Knepley   Output Parameter:
3858a6e0b375SMatthew G. Knepley . rel - The relation of dmA to dmB
3859559a1558SMatthew G. Knepley 
3860a6e0b375SMatthew G. Knepley   Level: intermediate
3861559a1558SMatthew G. Knepley 
3862446c23c1SPierre Jolivet .seealso: DMGetEnclosurePoint()
3863559a1558SMatthew G. Knepley @*/
3864a6e0b375SMatthew G. Knepley PetscErrorCode DMGetEnclosureRelation(DM dmA, DM dmB, DMEnclosureType *rel)
3865559a1558SMatthew G. Knepley {
3866a6e0b375SMatthew G. Knepley   DM             plexA, plexB, sdm;
3867559a1558SMatthew G. Knepley   DMLabel        spmap;
3868a6e0b375SMatthew G. Knepley   PetscInt       pStartA, pEndA, pStartB, pEndB, NpA, NpB;
3869559a1558SMatthew G. Knepley 
387044171101SMatthew G. Knepley   PetscFunctionBegin;
3871a6e0b375SMatthew G. Knepley   PetscValidPointer(rel, 3);
3872a6e0b375SMatthew G. Knepley   *rel = DM_ENC_NONE;
3873a6e0b375SMatthew G. Knepley   if (!dmA || !dmB) PetscFunctionReturn(0);
3874a6e0b375SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
3875064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
3876a6e0b375SMatthew G. Knepley   if (dmA == dmB) {*rel = DM_ENC_EQUALITY; PetscFunctionReturn(0);}
3877*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMConvert(dmA, DMPLEX, &plexA));
3878*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMConvert(dmB, DMPLEX, &plexB));
3879*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetChart(plexA, &pStartA, &pEndA));
3880*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetChart(plexB, &pStartB, &pEndB));
3881a6e0b375SMatthew G. Knepley   /* Assumption 1: subDMs have smaller charts than the DMs that they originate from
3882a6e0b375SMatthew G. Knepley     - The degenerate case of a subdomain which includes all of the domain on some process can be treated as equality */
3883a6e0b375SMatthew G. Knepley   if ((pStartA == pStartB) && (pEndA == pEndB)) {
3884a6e0b375SMatthew G. Knepley     *rel = DM_ENC_EQUALITY;
3885a6e0b375SMatthew G. Knepley     goto end;
3886559a1558SMatthew G. Knepley   }
3887a6e0b375SMatthew G. Knepley   NpA = pEndA - pStartA;
3888a6e0b375SMatthew G. Knepley   NpB = pEndB - pStartB;
3889a6e0b375SMatthew G. Knepley   if (NpA == NpB) goto end;
3890a6e0b375SMatthew G. Knepley   sdm = NpA > NpB ? plexB : plexA; /* The other is the original, enclosing dm */
3891*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMPlexGetSubpointMap(sdm, &spmap));
3892a6e0b375SMatthew G. Knepley   if (!spmap) goto end;
3893a6e0b375SMatthew G. Knepley   /* TODO Check the space mapped to by subpointMap is same size as dm */
3894a6e0b375SMatthew G. Knepley   if (NpA > NpB) {
3895a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUPERMESH;
3896a6e0b375SMatthew G. Knepley   } else {
3897a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUBMESH;
3898a6e0b375SMatthew G. Knepley   }
3899a6e0b375SMatthew G. Knepley   end:
3900*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDestroy(&plexA));
3901*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDestroy(&plexB));
3902559a1558SMatthew G. Knepley   PetscFunctionReturn(0);
3903559a1558SMatthew G. Knepley }
390444171101SMatthew G. Knepley 
390544171101SMatthew G. Knepley /*@
3906a6e0b375SMatthew G. Knepley   DMGetEnclosurePoint - Get the point pA in dmA which corresponds to the point pB in dmB
390744171101SMatthew G. Knepley 
390844171101SMatthew G. Knepley   Input Parameters:
3909a6e0b375SMatthew G. Knepley + dmA   - The first DM
3910a6e0b375SMatthew G. Knepley . dmB   - The second DM
3911a6e0b375SMatthew G. Knepley . etype - The type of enclosure relation that dmA has to dmB
3912a6e0b375SMatthew G. Knepley - pB    - A point of dmB
391344171101SMatthew G. Knepley 
391444171101SMatthew G. Knepley   Output Parameter:
3915a6e0b375SMatthew G. Knepley . pA    - The corresponding point of dmA
391644171101SMatthew G. Knepley 
3917a6e0b375SMatthew G. Knepley   Level: intermediate
391844171101SMatthew G. Knepley 
3919a6e0b375SMatthew G. Knepley .seealso: DMGetEnclosureRelation()
392044171101SMatthew G. Knepley @*/
3921a6e0b375SMatthew G. Knepley PetscErrorCode DMGetEnclosurePoint(DM dmA, DM dmB, DMEnclosureType etype, PetscInt pB, PetscInt *pA)
392244171101SMatthew G. Knepley {
3923a6e0b375SMatthew G. Knepley   DM              sdm;
3924a6e0b375SMatthew G. Knepley   IS              subpointIS;
3925a6e0b375SMatthew G. Knepley   const PetscInt *subpoints;
3926a6e0b375SMatthew G. Knepley   PetscInt        numSubpoints;
392744171101SMatthew G. Knepley 
392844171101SMatthew G. Knepley   PetscFunctionBegin;
3929a6e0b375SMatthew G. Knepley   /* TODO Cache the IS, making it look like an index */
3930a6e0b375SMatthew G. Knepley   switch (etype) {
3931a6e0b375SMatthew G. Knepley     case DM_ENC_SUPERMESH:
3932a6e0b375SMatthew G. Knepley     sdm  = dmB;
3933*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetSubpointIS(sdm, &subpointIS));
3934*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetIndices(subpointIS, &subpoints));
3935a6e0b375SMatthew G. Knepley     *pA  = subpoints[pB];
3936*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISRestoreIndices(subpointIS, &subpoints));
3937a6e0b375SMatthew G. Knepley     break;
3938a6e0b375SMatthew G. Knepley     case DM_ENC_SUBMESH:
3939a6e0b375SMatthew G. Knepley     sdm  = dmA;
3940*5f80ce2aSJacob Faibussowitsch     CHKERRQ(DMPlexGetSubpointIS(sdm, &subpointIS));
3941*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetLocalSize(subpointIS, &numSubpoints));
3942*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISGetIndices(subpointIS, &subpoints));
3943*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscFindInt(pB, numSubpoints, subpoints, pA));
3944a6e0b375SMatthew G. Knepley     if (*pA < 0) {
3945*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMViewFromOptions(dmA, NULL, "-dm_enc_A_view"));
3946*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMViewFromOptions(dmB, NULL, "-dm_enc_B_view"));
394798921bdaSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %d not found in submesh", pB);
3948a6e0b375SMatthew G. Knepley     }
3949*5f80ce2aSJacob Faibussowitsch     CHKERRQ(ISRestoreIndices(subpointIS, &subpoints));
3950a6e0b375SMatthew G. Knepley     break;
3951a6e0b375SMatthew G. Knepley     case DM_ENC_EQUALITY:
3952a6e0b375SMatthew G. Knepley     case DM_ENC_NONE:
3953a6e0b375SMatthew G. Knepley     *pA = pB;break;
3954a6e0b375SMatthew G. Knepley     case DM_ENC_UNKNOWN:
3955a6e0b375SMatthew G. Knepley     {
3956a6e0b375SMatthew G. Knepley       DMEnclosureType enc;
395744171101SMatthew G. Knepley 
3958*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMGetEnclosureRelation(dmA, dmB, &enc));
3959*5f80ce2aSJacob Faibussowitsch       CHKERRQ(DMGetEnclosurePoint(dmA, dmB, enc, pB, pA));
3960a6e0b375SMatthew G. Knepley     }
3961a6e0b375SMatthew G. Knepley     break;
396298921bdaSJacob Faibussowitsch     default: SETERRQ(PetscObjectComm((PetscObject) dmA), PETSC_ERR_ARG_OUTOFRANGE, "Invalid enclosure type %d", (int) etype);
396344171101SMatthew G. Knepley   }
396444171101SMatthew G. Knepley   PetscFunctionReturn(0);
396544171101SMatthew G. Knepley }
3966