xref: /petsc/src/dm/impls/plex/plexsubmesh.c (revision 412e9a14abbdcfab8bb1cbfb40875fcde8c4ce26)
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 
5*412e9a14SMatthew G. Knepley static PetscErrorCode DMPlexCellIsHybrid_Internal(DM dm, PetscInt p, PetscBool *isHybrid)
6*412e9a14SMatthew G. Knepley {
7*412e9a14SMatthew G. Knepley   DMPolytopeType ct;
8*412e9a14SMatthew G. Knepley   PetscErrorCode ierr;
9*412e9a14SMatthew G. Knepley 
10*412e9a14SMatthew G. Knepley   PetscFunctionBegin;
11*412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, p, &ct);CHKERRQ(ierr);
12*412e9a14SMatthew G. Knepley   switch (ct) {
13*412e9a14SMatthew G. Knepley     case DM_POLYTOPE_POINT_PRISM_TENSOR:
14*412e9a14SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
15*412e9a14SMatthew G. Knepley     case DM_POLYTOPE_TRI_PRISM_TENSOR:
16*412e9a14SMatthew G. Knepley     case DM_POLYTOPE_QUAD_PRISM_TENSOR:
17*412e9a14SMatthew G. Knepley       *isHybrid = PETSC_TRUE;
18*412e9a14SMatthew G. Knepley     default: *isHybrid = PETSC_FALSE;
19*412e9a14SMatthew G. Knepley   }
20*412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
21*412e9a14SMatthew G. Knepley }
22*412e9a14SMatthew G. Knepley 
23*412e9a14SMatthew G. Knepley static PetscErrorCode DMPlexGetTensorPrismBounds_Internal(DM dm, PetscInt dim, PetscInt *cStart, PetscInt *cEnd)
24*412e9a14SMatthew G. Knepley {
25*412e9a14SMatthew G. Knepley   DMLabel        ctLabel;
26*412e9a14SMatthew G. Knepley   PetscErrorCode ierr;
27*412e9a14SMatthew G. Knepley 
28*412e9a14SMatthew G. Knepley   PetscFunctionBegin;
29*412e9a14SMatthew G. Knepley   if (cStart) *cStart = -1;
30*412e9a14SMatthew G. Knepley   if (cEnd)   *cEnd   = -1;
31*412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &ctLabel);CHKERRQ(ierr);
32*412e9a14SMatthew G. Knepley   switch (dim) {
33*412e9a14SMatthew G. Knepley     case 1: ierr = DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_POINT_PRISM_TENSOR, cStart, cEnd);CHKERRQ(ierr);break;
34*412e9a14SMatthew G. Knepley     case 2: ierr = DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_SEG_PRISM_TENSOR, cStart, cEnd);CHKERRQ(ierr);break;
35*412e9a14SMatthew G. Knepley     case 3:
36*412e9a14SMatthew G. Knepley       ierr = DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_TRI_PRISM_TENSOR, cStart, cEnd);CHKERRQ(ierr);
37*412e9a14SMatthew G. Knepley       if (*cStart < 0) {ierr = DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_QUAD_PRISM_TENSOR, cStart, cEnd);CHKERRQ(ierr);}
38*412e9a14SMatthew G. Knepley       break;
39*412e9a14SMatthew G. Knepley     default: PetscFunctionReturn(0);
40*412e9a14SMatthew G. Knepley   }
41*412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
42*412e9a14SMatthew G. Knepley }
43*412e9a14SMatthew G. Knepley 
44e752be1aSMatthew G. Knepley static PetscErrorCode DMPlexMarkBoundaryFaces_Internal(DM dm, PetscInt val, PetscInt cellHeight, DMLabel label)
4530560a7bSMatthew G. Knepley {
4630560a7bSMatthew G. Knepley   PetscInt       fStart, fEnd, f;
4730560a7bSMatthew G. Knepley   PetscErrorCode ierr;
4830560a7bSMatthew G. Knepley 
4930560a7bSMatthew G. Knepley   PetscFunctionBegin;
5030560a7bSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight+1, &fStart, &fEnd);CHKERRQ(ierr);
5130560a7bSMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {
5230560a7bSMatthew G. Knepley     PetscInt supportSize;
5330560a7bSMatthew G. Knepley 
5430560a7bSMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, f, &supportSize);CHKERRQ(ierr);
55e752be1aSMatthew G. Knepley     if (supportSize == 1) {
56e752be1aSMatthew G. Knepley       if (val < 0) {
57e752be1aSMatthew G. Knepley         PetscInt *closure = NULL;
58e752be1aSMatthew G. Knepley         PetscInt  clSize, cl, cval;
59e752be1aSMatthew G. Knepley 
60e752be1aSMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure(dm, f, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
61e752be1aSMatthew G. Knepley         for (cl = 0; cl < clSize*2; cl += 2) {
62e752be1aSMatthew G. Knepley           ierr = DMLabelGetValue(label, closure[cl], &cval);CHKERRQ(ierr);
63e752be1aSMatthew G. Knepley           if (cval < 0) continue;
64e752be1aSMatthew G. Knepley           ierr = DMLabelSetValue(label, f, cval);CHKERRQ(ierr);
65e752be1aSMatthew G. Knepley           break;
66e752be1aSMatthew G. Knepley         }
67e752be1aSMatthew G. Knepley         if (cl == clSize*2) {ierr = DMLabelSetValue(label, f, 1);CHKERRQ(ierr);}
68e752be1aSMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, f, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
69e752be1aSMatthew G. Knepley       } else {
70e752be1aSMatthew G. Knepley         ierr = DMLabelSetValue(label, f, val);CHKERRQ(ierr);
71e752be1aSMatthew G. Knepley       }
72e752be1aSMatthew G. Knepley     }
7330560a7bSMatthew G. Knepley   }
7430560a7bSMatthew G. Knepley   PetscFunctionReturn(0);
7530560a7bSMatthew G. Knepley }
7630560a7bSMatthew G. Knepley 
77cd0c2139SMatthew G Knepley /*@
78cd0c2139SMatthew G Knepley   DMPlexMarkBoundaryFaces - Mark all faces on the boundary
79cd0c2139SMatthew G Knepley 
80cd0c2139SMatthew G Knepley   Not Collective
81cd0c2139SMatthew G Knepley 
82cd0c2139SMatthew G Knepley   Input Parameter:
83e752be1aSMatthew G. Knepley + dm - The original DM
84e752be1aSMatthew G. Knepley - val - The marker value, or PETSC_DETERMINE to use some value in the closure (or 1 if none are found)
85cd0c2139SMatthew G Knepley 
86cd0c2139SMatthew G Knepley   Output Parameter:
87e752be1aSMatthew G. Knepley . label - The DMLabel marking boundary faces with the given value
88cd0c2139SMatthew G Knepley 
89cd0c2139SMatthew G Knepley   Level: developer
90cd0c2139SMatthew G Knepley 
91c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMCreateLabel()
9209f723d9SJed Brown @*/
93e752be1aSMatthew G. Knepley PetscErrorCode DMPlexMarkBoundaryFaces(DM dm, PetscInt val, DMLabel label)
94cd0c2139SMatthew G Knepley {
95827c4036SVaclav Hapla   DMPlexInterpolatedFlag  flg;
96cd0c2139SMatthew G Knepley   PetscErrorCode          ierr;
97cd0c2139SMatthew G Knepley 
98cd0c2139SMatthew G Knepley   PetscFunctionBegin;
99827c4036SVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
100827c4036SVaclav Hapla   ierr = DMPlexIsInterpolated(dm, &flg);CHKERRQ(ierr);
101827c4036SVaclav Hapla   if (flg != DMPLEX_INTERPOLATED_FULL) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not fully interpolated on this rank");
102e752be1aSMatthew G. Knepley   ierr = DMPlexMarkBoundaryFaces_Internal(dm, val, 0, label);CHKERRQ(ierr);
103cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
104cd0c2139SMatthew G Knepley }
105cd0c2139SMatthew G Knepley 
106c08575a3SMatthew G. Knepley static PetscErrorCode DMPlexLabelComplete_Internal(DM dm, DMLabel label, PetscBool completeCells)
107b0bf5782SToby Isaac {
108b0bf5782SToby Isaac   IS              valueIS;
109ac51f24eSSander Arens   PetscSF         sfPoint;
110b0bf5782SToby Isaac   const PetscInt *values;
111ac51f24eSSander Arens   PetscInt        numValues, v, cStart, cEnd, nroots;
112b0bf5782SToby Isaac   PetscErrorCode  ierr;
113b0bf5782SToby Isaac 
114b0bf5782SToby Isaac   PetscFunctionBegin;
115b0bf5782SToby Isaac   ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
116b0bf5782SToby Isaac   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
117b0bf5782SToby Isaac   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
118b0bf5782SToby Isaac   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
119b0bf5782SToby Isaac   for (v = 0; v < numValues; ++v) {
120b0bf5782SToby Isaac     IS              pointIS;
121b0bf5782SToby Isaac     const PetscInt *points;
122b0bf5782SToby Isaac     PetscInt        numPoints, p;
123b0bf5782SToby Isaac 
124b0bf5782SToby Isaac     ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr);
125b0bf5782SToby Isaac     ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
126b0bf5782SToby Isaac     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
127b0bf5782SToby Isaac     for (p = 0; p < numPoints; ++p) {
128b0bf5782SToby Isaac       PetscInt  q = points[p];
129b0bf5782SToby Isaac       PetscInt *closure = NULL;
130b0bf5782SToby Isaac       PetscInt  closureSize, c;
131b0bf5782SToby Isaac 
132b0bf5782SToby Isaac       if (cStart <= q && q < cEnd && !completeCells) { /* skip cells */
133b0bf5782SToby Isaac         continue;
134b0bf5782SToby Isaac       }
135b0bf5782SToby Isaac       ierr = DMPlexGetTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
136b0bf5782SToby Isaac       for (c = 0; c < closureSize*2; c += 2) {
137b0bf5782SToby Isaac         ierr = DMLabelSetValue(label, closure[c], values[v]);CHKERRQ(ierr);
138b0bf5782SToby Isaac       }
139b0bf5782SToby Isaac       ierr = DMPlexRestoreTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
140b0bf5782SToby Isaac     }
141b0bf5782SToby Isaac     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
142b0bf5782SToby Isaac     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
143b0bf5782SToby Isaac   }
144b0bf5782SToby Isaac   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
145b0bf5782SToby Isaac   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
146ac51f24eSSander Arens   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
147ac51f24eSSander Arens   ierr = PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
148ac51f24eSSander Arens   if (nroots >= 0) {
14926279d81SSanderA     DMLabel         lblRoots, lblLeaves;
15026279d81SSanderA     IS              valueIS, pointIS;
15126279d81SSanderA     const PetscInt *values;
15226279d81SSanderA     PetscInt        numValues, v;
15326279d81SSanderA     PetscErrorCode  ierr;
15426279d81SSanderA 
15526279d81SSanderA     ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
15626279d81SSanderA     /* Pull point contributions from remote leaves into local roots */
15726279d81SSanderA     ierr = DMLabelGather(label, sfPoint, &lblLeaves);CHKERRQ(ierr);
15826279d81SSanderA     ierr = DMLabelGetValueIS(lblLeaves, &valueIS);CHKERRQ(ierr);
15926279d81SSanderA     ierr = ISGetLocalSize(valueIS, &numValues);CHKERRQ(ierr);
16026279d81SSanderA     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
16126279d81SSanderA     for (v = 0; v < numValues; ++v) {
16226279d81SSanderA       const PetscInt value = values[v];
16326279d81SSanderA 
16426279d81SSanderA       ierr = DMLabelGetStratumIS(lblLeaves, value, &pointIS);CHKERRQ(ierr);
16526279d81SSanderA       ierr = DMLabelInsertIS(label, pointIS, value);CHKERRQ(ierr);
16626279d81SSanderA       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
16726279d81SSanderA     }
16826279d81SSanderA     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
16926279d81SSanderA     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
17026279d81SSanderA     ierr = DMLabelDestroy(&lblLeaves);CHKERRQ(ierr);
17126279d81SSanderA     /* Push point contributions from roots into remote leaves */
17226279d81SSanderA     ierr = DMLabelDistribute(label, sfPoint, &lblRoots);CHKERRQ(ierr);
17326279d81SSanderA     ierr = DMLabelGetValueIS(lblRoots, &valueIS);CHKERRQ(ierr);
17426279d81SSanderA     ierr = ISGetLocalSize(valueIS, &numValues);CHKERRQ(ierr);
17526279d81SSanderA     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
17626279d81SSanderA     for (v = 0; v < numValues; ++v) {
17726279d81SSanderA       const PetscInt value = values[v];
17826279d81SSanderA 
17926279d81SSanderA       ierr = DMLabelGetStratumIS(lblRoots, value, &pointIS);CHKERRQ(ierr);
18026279d81SSanderA       ierr = DMLabelInsertIS(label, pointIS, value);CHKERRQ(ierr);
18126279d81SSanderA       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
18226279d81SSanderA     }
18326279d81SSanderA     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
18426279d81SSanderA     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
18526279d81SSanderA     ierr = DMLabelDestroy(&lblRoots);CHKERRQ(ierr);
18626279d81SSanderA   }
187b0bf5782SToby Isaac   PetscFunctionReturn(0);
188b0bf5782SToby Isaac }
189b0bf5782SToby Isaac 
1902be2b188SMatthew G Knepley /*@
1912be2b188SMatthew G Knepley   DMPlexLabelComplete - Starting with a label marking points on a surface, we add the transitive closure to the surface
1922be2b188SMatthew G Knepley 
1932be2b188SMatthew G Knepley   Input Parameters:
1942be2b188SMatthew G Knepley + dm - The DM
1952be2b188SMatthew G Knepley - label - A DMLabel marking the surface points
1962be2b188SMatthew G Knepley 
1972be2b188SMatthew G Knepley   Output Parameter:
1982be2b188SMatthew G Knepley . label - A DMLabel marking all surface points in the transitive closure
1992be2b188SMatthew G Knepley 
2002be2b188SMatthew G Knepley   Level: developer
2012be2b188SMatthew G Knepley 
2022be2b188SMatthew G Knepley .seealso: DMPlexLabelCohesiveComplete()
2032be2b188SMatthew G Knepley @*/
2042be2b188SMatthew G Knepley PetscErrorCode DMPlexLabelComplete(DM dm, DMLabel label)
2052be2b188SMatthew G Knepley {
2062be2b188SMatthew G Knepley   PetscErrorCode ierr;
2072be2b188SMatthew G Knepley 
2082be2b188SMatthew G Knepley   PetscFunctionBegin;
209b0bf5782SToby Isaac   ierr = DMPlexLabelComplete_Internal(dm, label, PETSC_TRUE);CHKERRQ(ierr);
2102be2b188SMatthew G Knepley   PetscFunctionReturn(0);
2112be2b188SMatthew G Knepley }
2122be2b188SMatthew G Knepley 
2136cf0e42fSMatthew G. Knepley /*@
214a6e0b375SMatthew G. Knepley   DMPlexLabelAddCells - Starting with a label marking points on a surface, we add a cell for each point
2156cf0e42fSMatthew G. Knepley 
2166cf0e42fSMatthew G. Knepley   Input Parameters:
2176cf0e42fSMatthew G. Knepley + dm - The DM
2186cf0e42fSMatthew G. Knepley - label - A DMLabel marking the surface points
2196cf0e42fSMatthew G. Knepley 
2206cf0e42fSMatthew G. Knepley   Output Parameter:
2216cf0e42fSMatthew G. Knepley . label - A DMLabel incorporating cells
2226cf0e42fSMatthew G. Knepley 
2236cf0e42fSMatthew G. Knepley   Level: developer
2246cf0e42fSMatthew G. Knepley 
2256cf0e42fSMatthew G. Knepley   Note: The cells allow FEM boundary conditions to be applied using the cell geometry
2266cf0e42fSMatthew G. Knepley 
227a6e0b375SMatthew G. Knepley .seealso: DMPlexLabelAddFaceCells(), DMPlexLabelComplete(), DMPlexLabelCohesiveComplete()
2286cf0e42fSMatthew G. Knepley @*/
2296cf0e42fSMatthew G. Knepley PetscErrorCode DMPlexLabelAddCells(DM dm, DMLabel label)
2306cf0e42fSMatthew G. Knepley {
2316cf0e42fSMatthew G. Knepley   IS              valueIS;
2326cf0e42fSMatthew G. Knepley   const PetscInt *values;
233485ad865SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd;
2346cf0e42fSMatthew G. Knepley   PetscErrorCode  ierr;
2356cf0e42fSMatthew G. Knepley 
2366cf0e42fSMatthew G. Knepley   PetscFunctionBegin;
237*412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2386cf0e42fSMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
2396cf0e42fSMatthew G. Knepley   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
2406cf0e42fSMatthew G. Knepley   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
2416cf0e42fSMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
2426cf0e42fSMatthew G. Knepley     IS              pointIS;
2436cf0e42fSMatthew G. Knepley     const PetscInt *points;
2446cf0e42fSMatthew G. Knepley     PetscInt        numPoints, p;
2456cf0e42fSMatthew G. Knepley 
2466cf0e42fSMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr);
2476cf0e42fSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
2486cf0e42fSMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
2496cf0e42fSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2506cf0e42fSMatthew G. Knepley       PetscInt *closure = NULL;
251a6e0b375SMatthew G. Knepley       PetscInt  closureSize, cl;
2526cf0e42fSMatthew G. Knepley 
2536cf0e42fSMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure);CHKERRQ(ierr);
25422eabd52SMatthew G. Knepley       for (cl = closureSize-1; cl > 0; --cl) {
255a6e0b375SMatthew G. Knepley         const PetscInt cell = closure[cl*2];
256a6e0b375SMatthew G. Knepley         if ((cell >= cStart) && (cell < cEnd)) {ierr = DMLabelSetValue(label, cell, values[v]);CHKERRQ(ierr); break;}
25722eabd52SMatthew G. Knepley       }
2586cf0e42fSMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure);CHKERRQ(ierr);
2596cf0e42fSMatthew G. Knepley     }
2606cf0e42fSMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
2616cf0e42fSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
2626cf0e42fSMatthew G. Knepley   }
2636cf0e42fSMatthew G. Knepley   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
2646cf0e42fSMatthew G. Knepley   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
2656cf0e42fSMatthew G. Knepley   PetscFunctionReturn(0);
2666cf0e42fSMatthew G. Knepley }
2676cf0e42fSMatthew G. Knepley 
268f402d5e4SToby Isaac /*@
269a6e0b375SMatthew G. Knepley   DMPlexLabelAddFaceCells - Starting with a label marking faces on a surface, we add a cell for each face
270a6e0b375SMatthew G. Knepley 
271a6e0b375SMatthew G. Knepley   Input Parameters:
272a6e0b375SMatthew G. Knepley + dm - The DM
273a6e0b375SMatthew G. Knepley - label - A DMLabel marking the surface points
274a6e0b375SMatthew G. Knepley 
275a6e0b375SMatthew G. Knepley   Output Parameter:
276a6e0b375SMatthew G. Knepley . label - A DMLabel incorporating cells
277a6e0b375SMatthew G. Knepley 
278a6e0b375SMatthew G. Knepley   Level: developer
279a6e0b375SMatthew G. Knepley 
280a6e0b375SMatthew G. Knepley   Note: The cells allow FEM boundary conditions to be applied using the cell geometry
281a6e0b375SMatthew G. Knepley 
282a6e0b375SMatthew G. Knepley .seealso: DMPlexLabelAddCells(), DMPlexLabelComplete(), DMPlexLabelCohesiveComplete()
283a6e0b375SMatthew G. Knepley @*/
284a6e0b375SMatthew G. Knepley PetscErrorCode DMPlexLabelAddFaceCells(DM dm, DMLabel label)
285a6e0b375SMatthew G. Knepley {
286a6e0b375SMatthew G. Knepley   IS              valueIS;
287a6e0b375SMatthew G. Knepley   const PetscInt *values;
288a6e0b375SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd, fStart, fEnd;
289a6e0b375SMatthew G. Knepley   PetscErrorCode  ierr;
290a6e0b375SMatthew G. Knepley 
291a6e0b375SMatthew G. Knepley   PetscFunctionBegin;
292*412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
293a6e0b375SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
294a6e0b375SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
295a6e0b375SMatthew G. Knepley   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
296a6e0b375SMatthew G. Knepley   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
297a6e0b375SMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
298a6e0b375SMatthew G. Knepley     IS              pointIS;
299a6e0b375SMatthew G. Knepley     const PetscInt *points;
300a6e0b375SMatthew G. Knepley     PetscInt        numPoints, p;
301a6e0b375SMatthew G. Knepley 
302a6e0b375SMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr);
303a6e0b375SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
304a6e0b375SMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
305a6e0b375SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
306a6e0b375SMatthew G. Knepley       const PetscInt face = points[p];
307a6e0b375SMatthew G. Knepley       PetscInt      *closure = NULL;
308a6e0b375SMatthew G. Knepley       PetscInt       closureSize, cl;
309a6e0b375SMatthew G. Knepley 
310a6e0b375SMatthew G. Knepley       if ((face < fStart) || (face >= fEnd)) continue;
311a6e0b375SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, face, PETSC_FALSE, &closureSize, &closure);CHKERRQ(ierr);
312a6e0b375SMatthew G. Knepley       for (cl = closureSize-1; cl > 0; --cl) {
313a6e0b375SMatthew G. Knepley         const PetscInt cell = closure[cl*2];
314a6e0b375SMatthew G. Knepley         if ((cell >= cStart) && (cell < cEnd)) {ierr = DMLabelSetValue(label, cell, values[v]);CHKERRQ(ierr); break;}
315a6e0b375SMatthew G. Knepley       }
316a6e0b375SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, face, PETSC_FALSE, &closureSize, &closure);CHKERRQ(ierr);
317a6e0b375SMatthew G. Knepley     }
318a6e0b375SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
319a6e0b375SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
320a6e0b375SMatthew G. Knepley   }
321a6e0b375SMatthew G. Knepley   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
322a6e0b375SMatthew G. Knepley   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
323a6e0b375SMatthew G. Knepley   PetscFunctionReturn(0);
324a6e0b375SMatthew G. Knepley }
325a6e0b375SMatthew G. Knepley 
326a6e0b375SMatthew G. Knepley /*@
327f402d5e4SToby Isaac   DMPlexLabelClearCells - Remove cells from a label
328f402d5e4SToby Isaac 
329f402d5e4SToby Isaac   Input Parameters:
330f402d5e4SToby Isaac + dm - The DM
331f402d5e4SToby Isaac - label - A DMLabel marking surface points and their adjacent cells
332f402d5e4SToby Isaac 
333f402d5e4SToby Isaac   Output Parameter:
334f402d5e4SToby Isaac . label - A DMLabel without cells
335f402d5e4SToby Isaac 
336f402d5e4SToby Isaac   Level: developer
337f402d5e4SToby Isaac 
338a6e0b375SMatthew G. Knepley   Note: This undoes DMPlexLabelAddCells() or DMPlexLabelAddFaceCells()
339f402d5e4SToby Isaac 
340f402d5e4SToby Isaac .seealso: DMPlexLabelComplete(), DMPlexLabelCohesiveComplete(), DMPlexLabelAddCells()
341f402d5e4SToby Isaac @*/
342f402d5e4SToby Isaac PetscErrorCode DMPlexLabelClearCells(DM dm, DMLabel label)
343f402d5e4SToby Isaac {
344f402d5e4SToby Isaac   IS              valueIS;
345f402d5e4SToby Isaac   const PetscInt *values;
346485ad865SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd;
347f402d5e4SToby Isaac   PetscErrorCode  ierr;
348f402d5e4SToby Isaac 
349f402d5e4SToby Isaac   PetscFunctionBegin;
350*412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
351f402d5e4SToby Isaac   ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
352f402d5e4SToby Isaac   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
353f402d5e4SToby Isaac   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
354f402d5e4SToby Isaac   for (v = 0; v < numValues; ++v) {
355f402d5e4SToby Isaac     IS              pointIS;
356f402d5e4SToby Isaac     const PetscInt *points;
357f402d5e4SToby Isaac     PetscInt        numPoints, p;
358f402d5e4SToby Isaac 
359f402d5e4SToby Isaac     ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr);
360f402d5e4SToby Isaac     ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
361f402d5e4SToby Isaac     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
362f402d5e4SToby Isaac     for (p = 0; p < numPoints; ++p) {
363f402d5e4SToby Isaac       PetscInt point = points[p];
364f402d5e4SToby Isaac 
365f402d5e4SToby Isaac       if (point >= cStart && point < cEnd) {
366f402d5e4SToby Isaac         ierr = DMLabelClearValue(label,point,values[v]);CHKERRQ(ierr);
367f402d5e4SToby Isaac       }
368f402d5e4SToby Isaac     }
369f402d5e4SToby Isaac     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
370f402d5e4SToby Isaac     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
371f402d5e4SToby Isaac   }
372f402d5e4SToby Isaac   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
373f402d5e4SToby Isaac   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
374f402d5e4SToby Isaac   PetscFunctionReturn(0);
375f402d5e4SToby Isaac }
376f402d5e4SToby Isaac 
37759eef20bSToby Isaac /* take (oldEnd, added) pairs, ordered by height and convert them to (oldstart, newstart) pairs, ordered by ascending
37859eef20bSToby Isaac  * index (skipping first, which is (0,0)) */
3792582d50cSToby Isaac PETSC_STATIC_INLINE PetscErrorCode DMPlexShiftPointSetUp_Internal(PetscInt depth, PetscInt depthShift[])
380cd0c2139SMatthew G Knepley {
3812582d50cSToby Isaac   PetscInt d, off = 0;
3822582d50cSToby Isaac 
3832582d50cSToby Isaac   PetscFunctionBegin;
38459eef20bSToby Isaac   /* sort by (oldend): yes this is an O(n^2) sort, we expect depth <= 3 */
3850974a383SToby Isaac   for (d = 0; d < depth; d++) {
3862582d50cSToby Isaac     PetscInt firstd = d;
3870974a383SToby Isaac     PetscInt firstStart = depthShift[2*d];
3882582d50cSToby Isaac     PetscInt e;
3892582d50cSToby Isaac 
3902582d50cSToby Isaac     for (e = d+1; e <= depth; e++) {
3912582d50cSToby Isaac       if (depthShift[2*e] < firstStart) {
3922582d50cSToby Isaac         firstd = e;
3932582d50cSToby Isaac         firstStart = depthShift[2*d];
3942582d50cSToby Isaac       }
3952582d50cSToby Isaac     }
3962582d50cSToby Isaac     if (firstd != d) {
3972582d50cSToby Isaac       PetscInt swap[2];
3982582d50cSToby Isaac 
3992582d50cSToby Isaac       e = firstd;
4002582d50cSToby Isaac       swap[0] = depthShift[2*d];
4012582d50cSToby Isaac       swap[1] = depthShift[2*d+1];
4022582d50cSToby Isaac       depthShift[2*d]   = depthShift[2*e];
4032582d50cSToby Isaac       depthShift[2*d+1] = depthShift[2*e+1];
4042582d50cSToby Isaac       depthShift[2*e]   = swap[0];
4052582d50cSToby Isaac       depthShift[2*e+1] = swap[1];
4062582d50cSToby Isaac     }
4072582d50cSToby Isaac   }
4082582d50cSToby Isaac   /* convert (oldstart, added) to (oldstart, newstart) */
4090974a383SToby Isaac   for (d = 0; d <= depth; d++) {
4102582d50cSToby Isaac     off += depthShift[2*d+1];
41159eef20bSToby Isaac     depthShift[2*d+1] = depthShift[2*d] + off;
4122582d50cSToby Isaac   }
4132582d50cSToby Isaac   PetscFunctionReturn(0);
4142582d50cSToby Isaac }
4152582d50cSToby Isaac 
4162582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
4172582d50cSToby Isaac PETSC_STATIC_INLINE PetscInt DMPlexShiftPoint_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
4182582d50cSToby Isaac {
4192582d50cSToby Isaac   PetscInt d;
4202582d50cSToby Isaac   PetscInt newOff = 0;
4212582d50cSToby Isaac 
4222582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
4232582d50cSToby Isaac     if (p < depthShift[2*d]) return p + newOff;
4242582d50cSToby Isaac     else newOff = depthShift[2*d+1] - depthShift[2*d];
4252582d50cSToby Isaac   }
4260974a383SToby Isaac   return p + newOff;
4272582d50cSToby Isaac }
4282582d50cSToby Isaac 
4292582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
4302582d50cSToby Isaac PETSC_STATIC_INLINE PetscInt DMPlexShiftPointInverse_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
4312582d50cSToby Isaac {
4322582d50cSToby Isaac   PetscInt d;
4332582d50cSToby Isaac   PetscInt newOff = 0;
4342582d50cSToby Isaac 
4352582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
4362582d50cSToby Isaac     if (p < depthShift[2*d+1]) return p + newOff;
4372582d50cSToby Isaac     else newOff = depthShift[2*d] - depthShift[2*d+1];
4382582d50cSToby Isaac   }
4390974a383SToby Isaac   return p + newOff;
440cd0c2139SMatthew G Knepley }
441cd0c2139SMatthew G Knepley 
442cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftSizes_Internal(DM dm, PetscInt depthShift[], DM dmNew)
443cd0c2139SMatthew G Knepley {
444cd0c2139SMatthew G Knepley   PetscInt       depth = 0, d, pStart, pEnd, p;
445fa8e8ae5SToby Isaac   DMLabel        depthLabel;
446cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
447cd0c2139SMatthew G Knepley 
448cd0c2139SMatthew G Knepley   PetscFunctionBegin;
449cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
450cd0c2139SMatthew G Knepley   if (depth < 0) PetscFunctionReturn(0);
451cd0c2139SMatthew G Knepley   /* Step 1: Expand chart */
452cd0c2139SMatthew G Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
4530974a383SToby Isaac   pEnd = DMPlexShiftPoint_Internal(pEnd,depth,depthShift);
454cd0c2139SMatthew G Knepley   ierr = DMPlexSetChart(dmNew, pStart, pEnd);CHKERRQ(ierr);
455fa8e8ae5SToby Isaac   ierr = DMCreateLabel(dmNew,"depth");CHKERRQ(ierr);
456fa8e8ae5SToby Isaac   ierr = DMPlexGetDepthLabel(dmNew,&depthLabel);CHKERRQ(ierr);
457*412e9a14SMatthew G. Knepley   ierr = DMCreateLabel(dmNew, "celltype");CHKERRQ(ierr);
458cd0c2139SMatthew G Knepley   /* Step 2: Set cone and support sizes */
459cd0c2139SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
460fa8e8ae5SToby Isaac     PetscInt pStartNew, pEndNew;
461fa8e8ae5SToby Isaac     IS pIS;
462fa8e8ae5SToby Isaac 
463cd0c2139SMatthew G Knepley     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
464fa8e8ae5SToby Isaac     pStartNew = DMPlexShiftPoint_Internal(pStart, depth, depthShift);
465fa8e8ae5SToby Isaac     pEndNew = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
466fa8e8ae5SToby Isaac     ierr = ISCreateStride(PETSC_COMM_SELF, pEndNew - pStartNew, pStartNew, 1, &pIS);CHKERRQ(ierr);
467fa8e8ae5SToby Isaac     ierr = DMLabelSetStratumIS(depthLabel, d, pIS);CHKERRQ(ierr);
468fa8e8ae5SToby Isaac     ierr = ISDestroy(&pIS);CHKERRQ(ierr);
469cd0c2139SMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
4702582d50cSToby Isaac       PetscInt       newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
471cd0c2139SMatthew G Knepley       PetscInt       size;
472*412e9a14SMatthew G. Knepley       DMPolytopeType ct;
473cd0c2139SMatthew G Knepley 
474cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, p, &size);CHKERRQ(ierr);
475cd0c2139SMatthew G Knepley       ierr = DMPlexSetConeSize(dmNew, newp, size);CHKERRQ(ierr);
476cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, p, &size);CHKERRQ(ierr);
477cd0c2139SMatthew G Knepley       ierr = DMPlexSetSupportSize(dmNew, newp, size);CHKERRQ(ierr);
478*412e9a14SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, p, &ct);CHKERRQ(ierr);
479*412e9a14SMatthew G. Knepley       ierr = DMPlexSetCellType(dmNew, newp, ct);CHKERRQ(ierr);
480cd0c2139SMatthew G Knepley     }
481cd0c2139SMatthew G Knepley   }
482cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
483cd0c2139SMatthew G Knepley }
484cd0c2139SMatthew G Knepley 
485cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftPoints_Internal(DM dm, PetscInt depthShift[], DM dmNew)
486cd0c2139SMatthew G Knepley {
4872582d50cSToby Isaac   PetscInt      *newpoints;
4882582d50cSToby Isaac   PetscInt       depth = 0, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, pStart, pEnd, p;
489cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
490cd0c2139SMatthew G Knepley 
491cd0c2139SMatthew G Knepley   PetscFunctionBegin;
492cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
493cd0c2139SMatthew G Knepley   if (depth < 0) PetscFunctionReturn(0);
494cd0c2139SMatthew G Knepley   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
495dcbb62e8SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dmNew, &maxConeSizeNew, &maxSupportSizeNew);CHKERRQ(ierr);
4962582d50cSToby Isaac   ierr = PetscMalloc1(PetscMax(PetscMax(maxConeSize, maxSupportSize), PetscMax(maxConeSizeNew, maxSupportSizeNew)),&newpoints);CHKERRQ(ierr);
497cd0c2139SMatthew G Knepley   /* Step 5: Set cones and supports */
498cd0c2139SMatthew G Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
499cd0c2139SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
500cd0c2139SMatthew G Knepley     const PetscInt *points = NULL, *orientations = NULL;
5012582d50cSToby Isaac     PetscInt        size,sizeNew, i, newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
502cd0c2139SMatthew G Knepley 
503cd0c2139SMatthew G Knepley     ierr = DMPlexGetConeSize(dm, p, &size);CHKERRQ(ierr);
504cd0c2139SMatthew G Knepley     ierr = DMPlexGetCone(dm, p, &points);CHKERRQ(ierr);
505cd0c2139SMatthew G Knepley     ierr = DMPlexGetConeOrientation(dm, p, &orientations);CHKERRQ(ierr);
506cd0c2139SMatthew G Knepley     for (i = 0; i < size; ++i) {
5072582d50cSToby Isaac       newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
508cd0c2139SMatthew G Knepley     }
509cd0c2139SMatthew G Knepley     ierr = DMPlexSetCone(dmNew, newp, newpoints);CHKERRQ(ierr);
510cd0c2139SMatthew G Knepley     ierr = DMPlexSetConeOrientation(dmNew, newp, orientations);CHKERRQ(ierr);
511cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupportSize(dm, p, &size);CHKERRQ(ierr);
512dcbb62e8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmNew, newp, &sizeNew);CHKERRQ(ierr);
513cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupport(dm, p, &points);CHKERRQ(ierr);
514cd0c2139SMatthew G Knepley     for (i = 0; i < size; ++i) {
5152582d50cSToby Isaac       newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
516cd0c2139SMatthew G Knepley     }
517dcbb62e8SMatthew G. Knepley     for (i = size; i < sizeNew; ++i) newpoints[i] = 0;
518cd0c2139SMatthew G Knepley     ierr = DMPlexSetSupport(dmNew, newp, newpoints);CHKERRQ(ierr);
519cd0c2139SMatthew G Knepley   }
5202582d50cSToby Isaac   ierr = PetscFree(newpoints);CHKERRQ(ierr);
521cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
522cd0c2139SMatthew G Knepley }
523cd0c2139SMatthew G Knepley 
524cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftCoordinates_Internal(DM dm, PetscInt depthShift[], DM dmNew)
525cd0c2139SMatthew G Knepley {
526cd0c2139SMatthew G Knepley   PetscSection   coordSection, newCoordSection;
527cd0c2139SMatthew G Knepley   Vec            coordinates, newCoordinates;
528cd0c2139SMatthew G Knepley   PetscScalar   *coords, *newCoords;
529f2b8cce1SMatthew G. Knepley   PetscInt       coordSize, sStart, sEnd;
530f2b8cce1SMatthew G. Knepley   PetscInt       dim, depth = 0, cStart, cEnd, cStartNew, cEndNew, c, vStart, vEnd, vStartNew, vEndNew, v;
531f2b8cce1SMatthew G. Knepley   PetscBool      hasCells;
532cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
533cd0c2139SMatthew G Knepley 
534cd0c2139SMatthew G Knepley   PetscFunctionBegin;
535f2b8cce1SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
536c0e8cf5fSMatthew G. Knepley   ierr = DMSetCoordinateDim(dmNew, dim);CHKERRQ(ierr);
537cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
538cd0c2139SMatthew G Knepley   /* Step 8: Convert coordinates */
539cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
540f2b8cce1SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
541cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepthStratum(dmNew, 0, &vStartNew, &vEndNew);CHKERRQ(ierr);
542f2b8cce1SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmNew, 0, &cStartNew, &cEndNew);CHKERRQ(ierr);
54369d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
544cd0c2139SMatthew G Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &newCoordSection);CHKERRQ(ierr);
545cd0c2139SMatthew G Knepley   ierr = PetscSectionSetNumFields(newCoordSection, 1);CHKERRQ(ierr);
546cd0c2139SMatthew G Knepley   ierr = PetscSectionSetFieldComponents(newCoordSection, 0, dim);CHKERRQ(ierr);
547f2b8cce1SMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
548f2b8cce1SMatthew G. Knepley   hasCells = sStart == cStart ? PETSC_TRUE : PETSC_FALSE;
549f2b8cce1SMatthew G. Knepley   ierr = PetscSectionSetChart(newCoordSection, hasCells ? cStartNew : vStartNew, vEndNew);CHKERRQ(ierr);
550f2b8cce1SMatthew G. Knepley   if (hasCells) {
551f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
552f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof;
553f2b8cce1SMatthew G. Knepley 
554f2b8cce1SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
555f2b8cce1SMatthew G. Knepley       ierr = PetscSectionSetDof(newCoordSection, cNew, dof);CHKERRQ(ierr);
556f2b8cce1SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(newCoordSection, cNew, 0, dof);CHKERRQ(ierr);
557f2b8cce1SMatthew G. Knepley     }
558f2b8cce1SMatthew G. Knepley   }
559cd0c2139SMatthew G Knepley   for (v = vStartNew; v < vEndNew; ++v) {
560cd0c2139SMatthew G Knepley     ierr = PetscSectionSetDof(newCoordSection, v, dim);CHKERRQ(ierr);
561cd0c2139SMatthew G Knepley     ierr = PetscSectionSetFieldDof(newCoordSection, v, 0, dim);CHKERRQ(ierr);
562cd0c2139SMatthew G Knepley   }
563cd0c2139SMatthew G Knepley   ierr = PetscSectionSetUp(newCoordSection);CHKERRQ(ierr);
56446e270d4SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmNew, PETSC_DETERMINE, newCoordSection);CHKERRQ(ierr);
565cd0c2139SMatthew G Knepley   ierr = PetscSectionGetStorageSize(newCoordSection, &coordSize);CHKERRQ(ierr);
5668b9ced59SLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &newCoordinates);CHKERRQ(ierr);
567cd0c2139SMatthew G Knepley   ierr = PetscObjectSetName((PetscObject) newCoordinates, "coordinates");CHKERRQ(ierr);
568cd0c2139SMatthew G Knepley   ierr = VecSetSizes(newCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
5694e90ef8eSMatthew G. Knepley   ierr = VecSetBlockSize(newCoordinates, dim);CHKERRQ(ierr);
5702eb5907fSJed Brown   ierr = VecSetType(newCoordinates,VECSTANDARD);CHKERRQ(ierr);
571cd0c2139SMatthew G Knepley   ierr = DMSetCoordinatesLocal(dmNew, newCoordinates);CHKERRQ(ierr);
572cd0c2139SMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
573cd0c2139SMatthew G Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
574cd0c2139SMatthew G Knepley   ierr = VecGetArray(newCoordinates, &newCoords);CHKERRQ(ierr);
575f2b8cce1SMatthew G. Knepley   if (hasCells) {
576f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
577f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof, off, noff, d;
578f2b8cce1SMatthew G. Knepley 
579f2b8cce1SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
580f2b8cce1SMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, c, &off);CHKERRQ(ierr);
581f2b8cce1SMatthew G. Knepley       ierr = PetscSectionGetOffset(newCoordSection, cNew, &noff);CHKERRQ(ierr);
582f2b8cce1SMatthew G. Knepley       for (d = 0; d < dof; ++d) newCoords[noff+d] = coords[off+d];
583f2b8cce1SMatthew G. Knepley     }
584f2b8cce1SMatthew G. Knepley   }
585cd0c2139SMatthew G Knepley   for (v = vStart; v < vEnd; ++v) {
586cd0c2139SMatthew G Knepley     PetscInt dof, off, noff, d;
587cd0c2139SMatthew G Knepley 
588cd0c2139SMatthew G Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
589cd0c2139SMatthew G Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
5902582d50cSToby Isaac     ierr = PetscSectionGetOffset(newCoordSection, DMPlexShiftPoint_Internal(v, depth, depthShift), &noff);CHKERRQ(ierr);
591f2b8cce1SMatthew G. Knepley     for (d = 0; d < dof; ++d) newCoords[noff+d] = coords[off+d];
592cd0c2139SMatthew G Knepley   }
593cd0c2139SMatthew G Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
594cd0c2139SMatthew G Knepley   ierr = VecRestoreArray(newCoordinates, &newCoords);CHKERRQ(ierr);
595cd0c2139SMatthew G Knepley   ierr = VecDestroy(&newCoordinates);CHKERRQ(ierr);
596cd0c2139SMatthew G Knepley   ierr = PetscSectionDestroy(&newCoordSection);CHKERRQ(ierr);
597cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
598cd0c2139SMatthew G Knepley }
599cd0c2139SMatthew G Knepley 
600cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftSF_Internal(DM dm, PetscInt depthShift[], DM dmNew)
601cd0c2139SMatthew G Knepley {
6022582d50cSToby Isaac   PetscInt           depth = 0;
603cd0c2139SMatthew G Knepley   PetscSF            sfPoint, sfPointNew;
604cd0c2139SMatthew G Knepley   const PetscSFNode *remotePoints;
605cd0c2139SMatthew G Knepley   PetscSFNode       *gremotePoints;
606cd0c2139SMatthew G Knepley   const PetscInt    *localPoints;
607cd0c2139SMatthew G Knepley   PetscInt          *glocalPoints, *newLocation, *newRemoteLocation;
608cd0c2139SMatthew G Knepley   PetscInt           numRoots, numLeaves, l, pStart, pEnd, totShift = 0;
609cd0c2139SMatthew G Knepley   PetscErrorCode     ierr;
610cd0c2139SMatthew G Knepley 
611cd0c2139SMatthew G Knepley   PetscFunctionBegin;
612cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
613cd0c2139SMatthew G Knepley   /* Step 9: Convert pointSF */
614cd0c2139SMatthew G Knepley   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
615cd0c2139SMatthew G Knepley   ierr = DMGetPointSF(dmNew, &sfPointNew);CHKERRQ(ierr);
616cd0c2139SMatthew G Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
617cd0c2139SMatthew G Knepley   ierr = PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr);
6186e21efdcSToby Isaac   totShift = DMPlexShiftPoint_Internal(pEnd,depth,depthShift) - pEnd;
619cd0c2139SMatthew G Knepley   if (numRoots >= 0) {
620dcca6d9dSJed Brown     ierr = PetscMalloc2(numRoots,&newLocation,pEnd-pStart,&newRemoteLocation);CHKERRQ(ierr);
6212582d50cSToby Isaac     for (l=0; l<numRoots; l++) newLocation[l] = DMPlexShiftPoint_Internal(l, depth, depthShift);
622cd0c2139SMatthew G Knepley     ierr = PetscSFBcastBegin(sfPoint, MPIU_INT, newLocation, newRemoteLocation);CHKERRQ(ierr);
623cd0c2139SMatthew G Knepley     ierr = PetscSFBcastEnd(sfPoint, MPIU_INT, newLocation, newRemoteLocation);CHKERRQ(ierr);
624785e854fSJed Brown     ierr = PetscMalloc1(numLeaves,    &glocalPoints);CHKERRQ(ierr);
625785e854fSJed Brown     ierr = PetscMalloc1(numLeaves, &gremotePoints);CHKERRQ(ierr);
626cd0c2139SMatthew G Knepley     for (l = 0; l < numLeaves; ++l) {
6272582d50cSToby Isaac       glocalPoints[l]        = DMPlexShiftPoint_Internal(localPoints[l], depth, depthShift);
628cd0c2139SMatthew G Knepley       gremotePoints[l].rank  = remotePoints[l].rank;
629cd0c2139SMatthew G Knepley       gremotePoints[l].index = newRemoteLocation[localPoints[l]];
630cd0c2139SMatthew G Knepley     }
631cd0c2139SMatthew G Knepley     ierr = PetscFree2(newLocation,newRemoteLocation);CHKERRQ(ierr);
632cd0c2139SMatthew G Knepley     ierr = PetscSFSetGraph(sfPointNew, numRoots + totShift, numLeaves, glocalPoints, PETSC_OWN_POINTER, gremotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
633cd0c2139SMatthew G Knepley   }
634cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
635cd0c2139SMatthew G Knepley }
636cd0c2139SMatthew G Knepley 
637cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftLabels_Internal(DM dm, PetscInt depthShift[], DM dmNew)
638cd0c2139SMatthew G Knepley {
639cd0c2139SMatthew G Knepley   PetscSF            sfPoint;
640cd0c2139SMatthew G Knepley   DMLabel            vtkLabel, ghostLabel;
641cd0c2139SMatthew G Knepley   const PetscSFNode *leafRemote;
642cd0c2139SMatthew G Knepley   const PetscInt    *leafLocal;
6432582d50cSToby Isaac   PetscInt           depth = 0, numLeaves, numLabels, l, cStart, cEnd, c, fStart, fEnd, f;
644cd0c2139SMatthew G Knepley   PetscMPIInt        rank;
645cd0c2139SMatthew G Knepley   PetscErrorCode     ierr;
646cd0c2139SMatthew G Knepley 
647cd0c2139SMatthew G Knepley   PetscFunctionBegin;
648cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
649cd0c2139SMatthew G Knepley   /* Step 10: Convert labels */
650c58f1c22SToby Isaac   ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
651cd0c2139SMatthew G Knepley   for (l = 0; l < numLabels; ++l) {
652cd0c2139SMatthew G Knepley     DMLabel         label, newlabel;
653cd0c2139SMatthew G Knepley     const char     *lname;
654fa8e8ae5SToby Isaac     PetscBool       isDepth, isDim;
655cd0c2139SMatthew G Knepley     IS              valueIS;
656cd0c2139SMatthew G Knepley     const PetscInt *values;
657cd0c2139SMatthew G Knepley     PetscInt        numValues, val;
658cd0c2139SMatthew G Knepley 
659c58f1c22SToby Isaac     ierr = DMGetLabelName(dm, l, &lname);CHKERRQ(ierr);
660cd0c2139SMatthew G Knepley     ierr = PetscStrcmp(lname, "depth", &isDepth);CHKERRQ(ierr);
661cd0c2139SMatthew G Knepley     if (isDepth) continue;
662fa8e8ae5SToby Isaac     ierr = PetscStrcmp(lname, "dim", &isDim);CHKERRQ(ierr);
663fa8e8ae5SToby Isaac     if (isDim) continue;
664c58f1c22SToby Isaac     ierr = DMCreateLabel(dmNew, lname);CHKERRQ(ierr);
665c58f1c22SToby Isaac     ierr = DMGetLabel(dm, lname, &label);CHKERRQ(ierr);
666c58f1c22SToby Isaac     ierr = DMGetLabel(dmNew, lname, &newlabel);CHKERRQ(ierr);
66764a6a1a4SToby Isaac     ierr = DMLabelGetDefaultValue(label,&val);CHKERRQ(ierr);
66864a6a1a4SToby Isaac     ierr = DMLabelSetDefaultValue(newlabel,val);CHKERRQ(ierr);
669cd0c2139SMatthew G Knepley     ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
670cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(valueIS, &numValues);CHKERRQ(ierr);
671cd0c2139SMatthew G Knepley     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
672cd0c2139SMatthew G Knepley     for (val = 0; val < numValues; ++val) {
673cd0c2139SMatthew G Knepley       IS              pointIS;
674cd0c2139SMatthew G Knepley       const PetscInt *points;
675cd0c2139SMatthew G Knepley       PetscInt        numPoints, p;
676cd0c2139SMatthew G Knepley 
677cd0c2139SMatthew G Knepley       ierr = DMLabelGetStratumIS(label, values[val], &pointIS);CHKERRQ(ierr);
678cd0c2139SMatthew G Knepley       ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
679cd0c2139SMatthew G Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
680cd0c2139SMatthew G Knepley       for (p = 0; p < numPoints; ++p) {
6812582d50cSToby Isaac         const PetscInt newpoint = DMPlexShiftPoint_Internal(points[p], depth, depthShift);
682cd0c2139SMatthew G Knepley 
683cd0c2139SMatthew G Knepley         ierr = DMLabelSetValue(newlabel, newpoint, values[val]);CHKERRQ(ierr);
684cd0c2139SMatthew G Knepley       }
685cd0c2139SMatthew G Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
686cd0c2139SMatthew G Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
687cd0c2139SMatthew G Knepley     }
688cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
689cd0c2139SMatthew G Knepley     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
690cd0c2139SMatthew G Knepley   }
691cd0c2139SMatthew G Knepley   /* Step 11: Make label for output (vtk) and to mark ghost points (ghost) */
692cd0c2139SMatthew G Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
693cd0c2139SMatthew G Knepley   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
694cd0c2139SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
695cd0c2139SMatthew G Knepley   ierr = PetscSFGetGraph(sfPoint, NULL, &numLeaves, &leafLocal, &leafRemote);CHKERRQ(ierr);
696c58f1c22SToby Isaac   ierr = DMCreateLabel(dmNew, "vtk");CHKERRQ(ierr);
697c58f1c22SToby Isaac   ierr = DMCreateLabel(dmNew, "ghost");CHKERRQ(ierr);
698c58f1c22SToby Isaac   ierr = DMGetLabel(dmNew, "vtk", &vtkLabel);CHKERRQ(ierr);
699c58f1c22SToby Isaac   ierr = DMGetLabel(dmNew, "ghost", &ghostLabel);CHKERRQ(ierr);
700cd0c2139SMatthew G Knepley   for (l = 0, c = cStart; l < numLeaves && c < cEnd; ++l, ++c) {
701cd0c2139SMatthew G Knepley     for (; c < leafLocal[l] && c < cEnd; ++c) {
702cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(vtkLabel, c, 1);CHKERRQ(ierr);
703cd0c2139SMatthew G Knepley     }
704cd0c2139SMatthew G Knepley     if (leafLocal[l] >= cEnd) break;
705cd0c2139SMatthew G Knepley     if (leafRemote[l].rank == rank) {
706cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(vtkLabel, c, 1);CHKERRQ(ierr);
707cd0c2139SMatthew G Knepley     } else {
708cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(ghostLabel, c, 2);CHKERRQ(ierr);
709cd0c2139SMatthew G Knepley     }
710cd0c2139SMatthew G Knepley   }
711cd0c2139SMatthew G Knepley   for (; c < cEnd; ++c) {
712cd0c2139SMatthew G Knepley     ierr = DMLabelSetValue(vtkLabel, c, 1);CHKERRQ(ierr);
713cd0c2139SMatthew G Knepley   }
714cd0c2139SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dmNew, 1, &fStart, &fEnd);CHKERRQ(ierr);
715cd0c2139SMatthew G Knepley   for (f = fStart; f < fEnd; ++f) {
716cd0c2139SMatthew G Knepley     PetscInt numCells;
717cd0c2139SMatthew G Knepley 
718cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupportSize(dmNew, f, &numCells);CHKERRQ(ierr);
719cd0c2139SMatthew G Knepley     if (numCells < 2) {
720cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(ghostLabel, f, 1);CHKERRQ(ierr);
721cd0c2139SMatthew G Knepley     } else {
722cd0c2139SMatthew G Knepley       const PetscInt *cells = NULL;
723cd0c2139SMatthew G Knepley       PetscInt        vA, vB;
724cd0c2139SMatthew G Knepley 
725cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupport(dmNew, f, &cells);CHKERRQ(ierr);
726cd0c2139SMatthew G Knepley       ierr = DMLabelGetValue(vtkLabel, cells[0], &vA);CHKERRQ(ierr);
727cd0c2139SMatthew G Knepley       ierr = DMLabelGetValue(vtkLabel, cells[1], &vB);CHKERRQ(ierr);
72890664b96SToby Isaac       if (vA != 1 && vB != 1) {ierr = DMLabelSetValue(ghostLabel, f, 1);CHKERRQ(ierr);}
729cd0c2139SMatthew G Knepley     }
730cd0c2139SMatthew G Knepley   }
731cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
732cd0c2139SMatthew G Knepley }
733cd0c2139SMatthew G Knepley 
734ca04dac2SToby Isaac static PetscErrorCode DMPlexShiftTree_Internal(DM dm, PetscInt depthShift[], DM dmNew)
735ca04dac2SToby Isaac {
736ca04dac2SToby Isaac   DM             refTree;
737ca04dac2SToby Isaac   PetscSection   pSec;
738ca04dac2SToby Isaac   PetscInt       *parents, *childIDs;
739ca04dac2SToby Isaac   PetscErrorCode ierr;
740ca04dac2SToby Isaac 
741ca04dac2SToby Isaac   PetscFunctionBegin;
742ca04dac2SToby Isaac   ierr = DMPlexGetReferenceTree(dm,&refTree);CHKERRQ(ierr);
743ca04dac2SToby Isaac   ierr = DMPlexSetReferenceTree(dmNew,refTree);CHKERRQ(ierr);
744ca04dac2SToby Isaac   ierr = DMPlexGetTree(dm,&pSec,&parents,&childIDs,NULL,NULL);CHKERRQ(ierr);
745ca04dac2SToby Isaac   if (pSec) {
7462582d50cSToby Isaac     PetscInt p, pStart, pEnd, *parentsShifted, pStartShifted, pEndShifted, depth;
747fb4630b5SToby Isaac     PetscInt *childIDsShifted;
748ca04dac2SToby Isaac     PetscSection pSecShifted;
749ca04dac2SToby Isaac 
750ca04dac2SToby Isaac     ierr = PetscSectionGetChart(pSec,&pStart,&pEnd);CHKERRQ(ierr);
751ca04dac2SToby Isaac     ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
7522582d50cSToby Isaac     pStartShifted = DMPlexShiftPoint_Internal(pStart,depth,depthShift);
7532582d50cSToby Isaac     pEndShifted   = DMPlexShiftPoint_Internal(pEnd,depth,depthShift);
754fb4630b5SToby Isaac     ierr = PetscMalloc2(pEndShifted - pStartShifted,&parentsShifted,pEndShifted-pStartShifted,&childIDsShifted);CHKERRQ(ierr);
755ca04dac2SToby Isaac     ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dmNew),&pSecShifted);CHKERRQ(ierr);
756ca04dac2SToby Isaac     ierr = PetscSectionSetChart(pSecShifted,pStartShifted,pEndShifted);CHKERRQ(ierr);
757ca04dac2SToby Isaac     for (p = pStartShifted; p < pEndShifted; p++) {
758fb4630b5SToby Isaac       /* start off assuming no children */
759fb4630b5SToby Isaac       ierr = PetscSectionSetDof(pSecShifted,p,0);CHKERRQ(ierr);
760fb4630b5SToby Isaac     }
761fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
762fb4630b5SToby Isaac       PetscInt dof;
763fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p,depth,depthShift);
764ca04dac2SToby Isaac 
765ca04dac2SToby Isaac       ierr = PetscSectionGetDof(pSec,p,&dof);CHKERRQ(ierr);
766fb4630b5SToby Isaac       ierr = PetscSectionSetDof(pSecShifted,pNew,dof);CHKERRQ(ierr);
767ca04dac2SToby Isaac     }
768ca04dac2SToby Isaac     ierr = PetscSectionSetUp(pSecShifted);CHKERRQ(ierr);
769fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
770fb4630b5SToby Isaac       PetscInt dof;
771fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p,depth,depthShift);
772fb4630b5SToby Isaac 
773fb4630b5SToby Isaac       ierr = PetscSectionGetDof(pSec,p,&dof);CHKERRQ(ierr);
774fb4630b5SToby Isaac       if (dof) {
775fb4630b5SToby Isaac         PetscInt off, offNew;
776fb4630b5SToby Isaac 
777fb4630b5SToby Isaac         ierr = PetscSectionGetOffset(pSec,p,&off);CHKERRQ(ierr);
778fb4630b5SToby Isaac         ierr = PetscSectionGetOffset(pSecShifted,pNew,&offNew);CHKERRQ(ierr);
779fb4630b5SToby Isaac         parentsShifted[offNew] = DMPlexShiftPoint_Internal(parents[off],depth,depthShift);
780fb4630b5SToby Isaac         childIDsShifted[offNew] = childIDs[off];
781fb4630b5SToby Isaac       }
782fb4630b5SToby Isaac     }
783fb4630b5SToby Isaac     ierr = DMPlexSetTree(dmNew,pSecShifted,parentsShifted,childIDsShifted);CHKERRQ(ierr);
784fb4630b5SToby Isaac     ierr = PetscFree2(parentsShifted,childIDsShifted);CHKERRQ(ierr);
785e6885bbbSToby Isaac     ierr = PetscSectionDestroy(&pSecShifted);CHKERRQ(ierr);
786ca04dac2SToby Isaac   }
787ca04dac2SToby Isaac   PetscFunctionReturn(0);
788ca04dac2SToby Isaac }
789ca04dac2SToby Isaac 
790cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexConstructGhostCells_Internal(DM dm, DMLabel label, PetscInt *numGhostCells, DM gdm)
791cd0c2139SMatthew G Knepley {
792da97024aSMatthew G. Knepley   PetscSF               sf;
793cd0c2139SMatthew G Knepley   IS                    valueIS;
794da97024aSMatthew G. Knepley   const PetscInt       *values, *leaves;
795cd0c2139SMatthew G Knepley   PetscInt             *depthShift;
7962582d50cSToby Isaac   PetscInt              d, depth = 0, nleaves, loc, Ng, numFS, fs, fStart, fEnd, ghostCell, cEnd, c;
79790b157c4SStefano Zampini   PetscBool             isper;
79890b157c4SStefano Zampini   const PetscReal      *maxCell, *L;
79990b157c4SStefano Zampini   const DMBoundaryType *bd;
800cd0c2139SMatthew G Knepley   PetscErrorCode        ierr;
801cd0c2139SMatthew G Knepley 
802cd0c2139SMatthew G Knepley   PetscFunctionBegin;
803da97024aSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
804da97024aSMatthew G. Knepley   ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
805da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
80646c796b9SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
807cd0c2139SMatthew G Knepley   /* Count ghost cells */
808cd0c2139SMatthew G Knepley   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
809cd0c2139SMatthew G Knepley   ierr = ISGetLocalSize(valueIS, &numFS);CHKERRQ(ierr);
810cd0c2139SMatthew G Knepley   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
8114a6cfa73SMatthew G. Knepley   Ng   = 0;
812cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
81346c796b9SMatthew G. Knepley     IS              faceIS;
81446c796b9SMatthew G. Knepley     const PetscInt *faces;
81546c796b9SMatthew G. Knepley     PetscInt        numFaces, f, numBdFaces = 0;
816cd0c2139SMatthew G Knepley 
81746c796b9SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, values[fs], &faceIS);CHKERRQ(ierr);
81846c796b9SMatthew G. Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
81946c796b9SMatthew G. Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
82046c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
821ca04dac2SToby Isaac       PetscInt numChildren;
822ca04dac2SToby Isaac 
823da97024aSMatthew G. Knepley       ierr = PetscFindInt(faces[f], nleaves, leaves, &loc);CHKERRQ(ierr);
824ca04dac2SToby Isaac       ierr = DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL);CHKERRQ(ierr);
825ca04dac2SToby Isaac       /* non-local and ancestors points don't get to register ghosts */
826ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
82746c796b9SMatthew G. Knepley       if ((faces[f] >= fStart) && (faces[f] < fEnd)) ++numBdFaces;
82846c796b9SMatthew G. Knepley     }
8294a6cfa73SMatthew G. Knepley     Ng += numBdFaces;
830ba2698f1SMatthew G. Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
83146c796b9SMatthew G. Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
832cd0c2139SMatthew G Knepley   }
833cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
8342582d50cSToby Isaac   ierr = PetscMalloc1(2*(depth+1), &depthShift);CHKERRQ(ierr);
8352582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
83659eef20bSToby Isaac     PetscInt dEnd;
8372582d50cSToby Isaac 
8380974a383SToby Isaac     ierr = DMPlexGetDepthStratum(dm,d,NULL,&dEnd);CHKERRQ(ierr);
83959eef20bSToby Isaac     depthShift[2*d]   = dEnd;
8402582d50cSToby Isaac     depthShift[2*d+1] = 0;
8412582d50cSToby Isaac   }
8422582d50cSToby Isaac   if (depth >= 0) depthShift[2*depth+1] = Ng;
8432582d50cSToby Isaac   ierr = DMPlexShiftPointSetUp_Internal(depth,depthShift);CHKERRQ(ierr);
844cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSizes_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
845cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
846cd0c2139SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
8474a6cfa73SMatthew G. Knepley   for (c = cEnd; c < cEnd + Ng; ++c) {
848cd0c2139SMatthew G Knepley     ierr = DMPlexSetConeSize(gdm, c, 1);CHKERRQ(ierr);
849cd0c2139SMatthew G Knepley   }
850cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
851cd0c2139SMatthew G Knepley     IS              faceIS;
852cd0c2139SMatthew G Knepley     const PetscInt *faces;
853cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
854cd0c2139SMatthew G Knepley 
855cd0c2139SMatthew G Knepley     ierr = DMLabelGetStratumIS(label, values[fs], &faceIS);CHKERRQ(ierr);
856cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
857cd0c2139SMatthew G Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
858cd0c2139SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
859ca04dac2SToby Isaac       PetscInt size, numChildren;
860cd0c2139SMatthew G Knepley 
861da97024aSMatthew G. Knepley       ierr = PetscFindInt(faces[f], nleaves, leaves, &loc);CHKERRQ(ierr);
862ca04dac2SToby Isaac       ierr = DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL);CHKERRQ(ierr);
863ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
86446c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
865cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, faces[f], &size);CHKERRQ(ierr);
8664553fd90SToby Isaac       if (size != 1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM has boundary face %d with %d support cells", faces[f], size);
8674a6cfa73SMatthew G. Knepley       ierr = DMPlexSetSupportSize(gdm, faces[f] + Ng, 2);CHKERRQ(ierr);
868cd0c2139SMatthew G Knepley     }
869cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
870cd0c2139SMatthew G Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
871cd0c2139SMatthew G Knepley   }
872cd0c2139SMatthew G Knepley   /* Step 4: Setup ghosted DM */
873cd0c2139SMatthew G Knepley   ierr = DMSetUp(gdm);CHKERRQ(ierr);
874cd0c2139SMatthew G Knepley   ierr = DMPlexShiftPoints_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
875cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
876cd0c2139SMatthew G Knepley   ghostCell = cEnd;
877cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
878cd0c2139SMatthew G Knepley     IS              faceIS;
879cd0c2139SMatthew G Knepley     const PetscInt *faces;
880cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
881cd0c2139SMatthew G Knepley 
882cd0c2139SMatthew G Knepley     ierr = DMLabelGetStratumIS(label, values[fs], &faceIS);CHKERRQ(ierr);
883cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
884cd0c2139SMatthew G Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
88546c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
886ca04dac2SToby Isaac       PetscInt newFace = faces[f] + Ng, numChildren;
887cd0c2139SMatthew G Knepley 
888da97024aSMatthew G. Knepley       ierr = PetscFindInt(faces[f], nleaves, leaves, &loc);CHKERRQ(ierr);
889ca04dac2SToby Isaac       ierr = DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL);CHKERRQ(ierr);
890ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
89146c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
892cd0c2139SMatthew G Knepley       ierr = DMPlexSetCone(gdm, ghostCell, &newFace);CHKERRQ(ierr);
893cd0c2139SMatthew G Knepley       ierr = DMPlexInsertSupport(gdm, newFace, 1, ghostCell);CHKERRQ(ierr);
89446c796b9SMatthew G. Knepley       ++ghostCell;
895cd0c2139SMatthew G Knepley     }
896cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
897cd0c2139SMatthew G Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
898cd0c2139SMatthew G Knepley   }
899cd0c2139SMatthew G Knepley   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
900cd0c2139SMatthew G Knepley   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
901cd0c2139SMatthew G Knepley   ierr = DMPlexShiftCoordinates_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
902cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSF_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
903cd0c2139SMatthew G Knepley   ierr = DMPlexShiftLabels_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
904ca04dac2SToby Isaac   ierr = DMPlexShiftTree_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
905cd0c2139SMatthew G Knepley   ierr = PetscFree(depthShift);CHKERRQ(ierr);
906*412e9a14SMatthew G. Knepley   for (c = cEnd; c < cEnd + Ng; ++c) {
907*412e9a14SMatthew G. Knepley     ierr = DMPlexSetCellType(gdm, c, DM_POLYTOPE_FV_GHOST);CHKERRQ(ierr);
908*412e9a14SMatthew G. Knepley   }
909966c7b3fSMatthew G. Knepley   /* Step 7: Periodicity */
91090b157c4SStefano Zampini   ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
91190b157c4SStefano Zampini   ierr = DMSetPeriodicity(gdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
9124a6cfa73SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
913cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
914cd0c2139SMatthew G Knepley }
915cd0c2139SMatthew G Knepley 
916cd0c2139SMatthew G Knepley /*@C
917cd0c2139SMatthew G Knepley   DMPlexConstructGhostCells - Construct ghost cells which connect to every boundary face
918cd0c2139SMatthew G Knepley 
919cd0c2139SMatthew G Knepley   Collective on dm
920cd0c2139SMatthew G Knepley 
921cd0c2139SMatthew G Knepley   Input Parameters:
922cd0c2139SMatthew G Knepley + dm - The original DM
923cd0c2139SMatthew G Knepley - labelName - The label specifying the boundary faces, or "Face Sets" if this is NULL
924cd0c2139SMatthew G Knepley 
925cd0c2139SMatthew G Knepley   Output Parameters:
926cd0c2139SMatthew G Knepley + numGhostCells - The number of ghost cells added to the DM
927cd0c2139SMatthew G Knepley - dmGhosted - The new DM
928cd0c2139SMatthew G Knepley 
929cd0c2139SMatthew G Knepley   Note: If no label exists of that name, one will be created marking all boundary faces
930cd0c2139SMatthew G Knepley 
931cd0c2139SMatthew G Knepley   Level: developer
932cd0c2139SMatthew G Knepley 
933cd0c2139SMatthew G Knepley .seealso: DMCreate()
93431266bc0SMatthew G. Knepley @*/
935cd0c2139SMatthew G Knepley PetscErrorCode DMPlexConstructGhostCells(DM dm, const char labelName[], PetscInt *numGhostCells, DM *dmGhosted)
936cd0c2139SMatthew G Knepley {
937cd0c2139SMatthew G Knepley   DM             gdm;
938cd0c2139SMatthew G Knepley   DMLabel        label;
939cd0c2139SMatthew G Knepley   const char    *name = labelName ? labelName : "Face Sets";
940*412e9a14SMatthew G. Knepley   PetscInt       dim, Ng = 0;
941b0441da4SMatthew G. Knepley   PetscBool      useCone, useClosure;
942cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
943cd0c2139SMatthew G Knepley 
944cd0c2139SMatthew G Knepley   PetscFunctionBegin;
945cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9464a6cfa73SMatthew G. Knepley   if (numGhostCells) PetscValidPointer(numGhostCells, 3);
947cd0c2139SMatthew G Knepley   PetscValidPointer(dmGhosted, 4);
948cd0c2139SMatthew G Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), &gdm);CHKERRQ(ierr);
949cd0c2139SMatthew G Knepley   ierr = DMSetType(gdm, DMPLEX);CHKERRQ(ierr);
950c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
951c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(gdm, dim);CHKERRQ(ierr);
952b0441da4SMatthew G. Knepley   ierr = DMGetBasicAdjacency(dm, &useCone, &useClosure);CHKERRQ(ierr);
953b0441da4SMatthew G. Knepley   ierr = DMSetBasicAdjacency(gdm, useCone,  useClosure);CHKERRQ(ierr);
954c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
955cd0c2139SMatthew G Knepley   if (!label) {
956cd0c2139SMatthew G Knepley     /* Get label for boundary faces */
957c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
958c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
959e752be1aSMatthew G. Knepley     ierr = DMPlexMarkBoundaryFaces(dm, 1, label);CHKERRQ(ierr);
960cd0c2139SMatthew G Knepley   }
961d80ece95SMatthew G. Knepley   ierr = DMPlexConstructGhostCells_Internal(dm, label, &Ng, gdm);CHKERRQ(ierr);
962a6ba4734SToby Isaac   ierr = DMCopyBoundary(dm, gdm);CHKERRQ(ierr);
963b0441da4SMatthew G. Knepley   ierr = DMCopyDisc(dm, gdm);CHKERRQ(ierr);
964cad26855SMatthew G. Knepley   gdm->setfromoptionscalled = dm->setfromoptionscalled;
965d80ece95SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
966cd0c2139SMatthew G Knepley   *dmGhosted = gdm;
967cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
968cd0c2139SMatthew G Knepley }
969cd0c2139SMatthew G Knepley 
970607ab7a9SMatthew G. Knepley /*
971faedd622SMatthew G. Knepley   We are adding three kinds of points here:
972607ab7a9SMatthew G. Knepley     Replicated:     Copies of points which exist in the mesh, such as vertices identified across a fault
973faedd622SMatthew G. Knepley     Non-replicated: Points which exist on the fault, but are not replicated
974607ab7a9SMatthew G. Knepley     Hybrid:         Entirely new points, such as cohesive cells
975a6ae58d1SMatthew G. Knepley 
976a6ae58d1SMatthew G. Knepley   When creating subsequent cohesive cells, we shift the old hybrid cells to the end of the numbering at
977a6ae58d1SMatthew G. Knepley   each depth so that the new split/hybrid points can be inserted as a block.
978607ab7a9SMatthew G. Knepley */
9797db7e0a7SMatthew G. Knepley static PetscErrorCode DMPlexConstructCohesiveCells_Internal(DM dm, DMLabel label, DMLabel splitLabel, DM sdm)
980cd0c2139SMatthew G Knepley {
981cd0c2139SMatthew G Knepley   MPI_Comm         comm;
982607ab7a9SMatthew G. Knepley   IS               valueIS;
983607ab7a9SMatthew G. Knepley   PetscInt         numSP = 0;       /* The number of depths for which we have replicated points */
984607ab7a9SMatthew G. Knepley   const PetscInt  *values;          /* List of depths for which we have replicated points */
98518c5995bSMatthew G. Knepley   IS              *splitIS;
98618c5995bSMatthew G. Knepley   IS              *unsplitIS;
987607ab7a9SMatthew G. Knepley   PetscInt        *numSplitPoints;     /* The number of replicated points at each depth */
98818c5995bSMatthew G. Knepley   PetscInt        *numUnsplitPoints;   /* The number of non-replicated points at each depth which still give rise to hybrid points */
98936dbac82SMatthew G. Knepley   PetscInt        *numHybridPoints;    /* The number of new hybrid points at each depth */
99036dbac82SMatthew G. Knepley   PetscInt        *numHybridPointsOld; /* The number of existing hybrid points at each depth */
991607ab7a9SMatthew G. Knepley   const PetscInt **splitPoints;        /* Replicated points for each depth */
99218c5995bSMatthew G. Knepley   const PetscInt **unsplitPoints;      /* Non-replicated points for each depth */
993cd0c2139SMatthew G Knepley   PetscSection     coordSection;
994cd0c2139SMatthew G Knepley   Vec              coordinates;
995cd0c2139SMatthew G Knepley   PetscScalar     *coords;
996a6ae58d1SMatthew G. Knepley   PetscInt        *depthMax;           /* The first hybrid point at each depth in the original mesh */
997a6ae58d1SMatthew G. Knepley   PetscInt        *depthEnd;           /* The point limit at each depth in the original mesh */
998607ab7a9SMatthew G. Knepley   PetscInt        *depthShift;         /* Number of replicated+hybrid points at each depth */
999607ab7a9SMatthew G. Knepley   PetscInt        *pMaxNew;            /* The first replicated point at each depth in the new mesh, hybrids come after this */
1000607ab7a9SMatthew G. Knepley   PetscInt        *coneNew, *coneONew, *supportNew;
100118c5995bSMatthew G. Knepley   PetscInt         shift = 100, shift2 = 200, depth = 0, dep, dim, d, sp, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, numLabels, vStart, vEnd, pEnd, p, v;
1002cd0c2139SMatthew G Knepley   PetscErrorCode   ierr;
1003cd0c2139SMatthew G Knepley 
1004cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1005cd0c2139SMatthew G Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
1006c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1007607ab7a9SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1008fd4b9f15SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1009*412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
1010*412e9a14SMatthew G. Knepley   ierr = DMCreateLabel(sdm, "celltype");CHKERRQ(ierr);
1011cd0c2139SMatthew G Knepley   /* Count split points and add cohesive cells */
1012607ab7a9SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
10132582d50cSToby Isaac   ierr = PetscMalloc5(depth+1,&depthMax,depth+1,&depthEnd,2*(depth+1),&depthShift,depth+1,&pMaxNew,depth+1,&numHybridPointsOld);CHKERRQ(ierr);
1014dcca6d9dSJed Brown   ierr = PetscMalloc7(depth+1,&splitIS,depth+1,&unsplitIS,depth+1,&numSplitPoints,depth+1,&numUnsplitPoints,depth+1,&numHybridPoints,depth+1,&splitPoints,depth+1,&unsplitPoints);CHKERRQ(ierr);
1015607ab7a9SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
1016607ab7a9SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, d, NULL, &pMaxNew[d]);CHKERRQ(ierr);
1017*412e9a14SMatthew G. Knepley     ierr = DMPlexGetTensorPrismBounds_Internal(dm, d, &depthMax[d], NULL);CHKERRQ(ierr);
1018a6ae58d1SMatthew G. Knepley     depthEnd[d]           = pMaxNew[d];
1019a6ae58d1SMatthew G. Knepley     depthMax[d]           = depthMax[d] < 0 ? depthEnd[d] : depthMax[d];
1020607ab7a9SMatthew G. Knepley     numSplitPoints[d]     = 0;
102118c5995bSMatthew G. Knepley     numUnsplitPoints[d]   = 0;
1022607ab7a9SMatthew G. Knepley     numHybridPoints[d]    = 0;
1023a6ae58d1SMatthew G. Knepley     numHybridPointsOld[d] = depthMax[d] < 0 ? 0 : depthEnd[d] - depthMax[d];
1024607ab7a9SMatthew G. Knepley     splitPoints[d]        = NULL;
102518c5995bSMatthew G. Knepley     unsplitPoints[d]      = NULL;
102618c5995bSMatthew G. Knepley     splitIS[d]            = NULL;
102718c5995bSMatthew G. Knepley     unsplitIS[d]          = NULL;
102859eef20bSToby Isaac     /* we are shifting the existing hybrid points with the stratum behind them, so
102959eef20bSToby Isaac      * the split comes at the end of the normal points, i.e., at depthMax[d] */
103059eef20bSToby Isaac     depthShift[2*d]       = depthMax[d];
103159eef20bSToby Isaac     depthShift[2*d+1]     = 0;
1032607ab7a9SMatthew G. Knepley   }
1033cd0c2139SMatthew G Knepley   if (label) {
1034cd0c2139SMatthew G Knepley     ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
1035cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(valueIS, &numSP);CHKERRQ(ierr);
1036cd0c2139SMatthew G Knepley     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
1037cd0c2139SMatthew G Knepley   }
1038cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1039cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1040cd0c2139SMatthew G Knepley 
1041cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
104218c5995bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, dep, &splitIS[dep]);CHKERRQ(ierr);
104318c5995bSMatthew G. Knepley     if (splitIS[dep]) {
104418c5995bSMatthew G. Knepley       ierr = ISGetLocalSize(splitIS[dep], &numSplitPoints[dep]);CHKERRQ(ierr);
104518c5995bSMatthew G. Knepley       ierr = ISGetIndices(splitIS[dep], &splitPoints[dep]);CHKERRQ(ierr);
104618c5995bSMatthew G. Knepley     }
104718c5995bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, shift2+dep, &unsplitIS[dep]);CHKERRQ(ierr);
104818c5995bSMatthew G. Knepley     if (unsplitIS[dep]) {
104918c5995bSMatthew G. Knepley       ierr = ISGetLocalSize(unsplitIS[dep], &numUnsplitPoints[dep]);CHKERRQ(ierr);
105018c5995bSMatthew G. Knepley       ierr = ISGetIndices(unsplitIS[dep], &unsplitPoints[dep]);CHKERRQ(ierr);
1051cd0c2139SMatthew G Knepley     }
1052cd0c2139SMatthew G Knepley   }
1053607ab7a9SMatthew G. Knepley   /* Calculate number of hybrid points */
105418c5995bSMatthew 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   */
10552582d50cSToby Isaac   for (d = 0; d <= depth; ++d) depthShift[2*d+1]      = numSplitPoints[d] + numHybridPoints[d];
10563d3eaba7SBarry Smith   ierr = DMPlexShiftPointSetUp_Internal(depth,depthShift);CHKERRQ(ierr);
105759eef20bSToby Isaac   /* the end of the points in this stratum that come before the new points:
105859eef20bSToby Isaac    * shifting pMaxNew[d] gets the new start of the next stratum, then count back the old hybrid points and the newly
105959eef20bSToby Isaac    * added points */
10602582d50cSToby Isaac   for (d = 0; d <= depth; ++d) pMaxNew[d]             = DMPlexShiftPoint_Internal(pMaxNew[d],depth,depthShift) - (numHybridPointsOld[d] + numSplitPoints[d] + numHybridPoints[d]);
1061cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSizes_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
1062cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
1063cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1064cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1065cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
10662582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
10674c367dbcSMatthew G. Knepley       const PetscInt  splitp = p    + pMaxNew[dep];
1068cd0c2139SMatthew G Knepley       const PetscInt *support;
10694c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, qn, qp, e;
1070cd0c2139SMatthew G Knepley 
1071cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
1072cd0c2139SMatthew G Knepley       ierr = DMPlexSetConeSize(sdm, splitp, coneSize);CHKERRQ(ierr);
1073cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
1074cd0c2139SMatthew G Knepley       ierr = DMPlexSetSupportSize(sdm, splitp, supportSize);CHKERRQ(ierr);
1075cd0c2139SMatthew G Knepley       if (dep == depth-1) {
10764c367dbcSMatthew G. Knepley         const PetscInt hybcell = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
10774c367dbcSMatthew G. Knepley 
1078cd0c2139SMatthew G Knepley         /* Add cohesive cells, they are prisms */
10794c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybcell, 2 + coneSize);CHKERRQ(ierr);
1080*412e9a14SMatthew G. Knepley         switch (coneSize) {
1081*412e9a14SMatthew G. Knepley           case 2: ierr = DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_SEG_PRISM_TENSOR);CHKERRQ(ierr);break;
1082*412e9a14SMatthew G. Knepley           case 3: ierr = DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_TRI_PRISM_TENSOR);CHKERRQ(ierr);break;
1083*412e9a14SMatthew G. Knepley           case 4: ierr = DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_QUAD_PRISM_TENSOR);CHKERRQ(ierr);break;
1084*412e9a14SMatthew G. Knepley         }
1085cd0c2139SMatthew G Knepley       } else if (dep == 0) {
10864c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1087cd0c2139SMatthew G Knepley 
1088cd0c2139SMatthew G Knepley         ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
10894c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1090cd0c2139SMatthew G Knepley           PetscInt val;
1091cd0c2139SMatthew G Knepley 
1092cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
10934c367dbcSMatthew G. Knepley           if (val == 1) ++qf;
10944c367dbcSMatthew G. Knepley           if ((val == 1) || (val ==  (shift + 1))) ++qn;
10954c367dbcSMatthew G. Knepley           if ((val == 1) || (val == -(shift + 1))) ++qp;
1096cd0c2139SMatthew G Knepley         }
10974c367dbcSMatthew G. Knepley         /* Split old vertex: Edges into original vertex and new cohesive edge */
10984c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qn+1);CHKERRQ(ierr);
10994c367dbcSMatthew G. Knepley         /* Split new vertex: Edges into split vertex and new cohesive edge */
11004c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, splitp, qp+1);CHKERRQ(ierr);
11014c367dbcSMatthew G. Knepley         /* Add hybrid edge */
11024c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybedge, 2);CHKERRQ(ierr);
11034c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybedge, qf);CHKERRQ(ierr);
1104*412e9a14SMatthew G. Knepley         ierr = DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR);CHKERRQ(ierr);
1105cd0c2139SMatthew G Knepley       } else if (dep == dim-2) {
11064c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
11074c367dbcSMatthew G. Knepley 
1108cd0c2139SMatthew G Knepley         ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
11094c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1110cd0c2139SMatthew G Knepley           PetscInt val;
1111cd0c2139SMatthew G Knepley 
1112cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
11134c367dbcSMatthew G. Knepley           if (val == dim-1) ++qf;
11144c367dbcSMatthew G. Knepley           if ((val == dim-1) || (val ==  (shift + dim-1))) ++qn;
11154c367dbcSMatthew G. Knepley           if ((val == dim-1) || (val == -(shift + dim-1))) ++qp;
1116cd0c2139SMatthew G Knepley         }
11174c367dbcSMatthew G. Knepley         /* Split old edge: Faces into original edge and cohesive face (positive side?) */
11184c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qn+1);CHKERRQ(ierr);
11194c367dbcSMatthew G. Knepley         /* Split new edge: Faces into split edge and cohesive face (negative side?) */
11204c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, splitp, qp+1);CHKERRQ(ierr);
11214c367dbcSMatthew G. Knepley         /* Add hybrid face */
11224c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybface, 4);CHKERRQ(ierr);
11234c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybface, qf);CHKERRQ(ierr);
1124*412e9a14SMatthew G. Knepley         ierr = DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR);CHKERRQ(ierr);
1125cd0c2139SMatthew G Knepley       }
1126cd0c2139SMatthew G Knepley     }
1127cd0c2139SMatthew G Knepley   }
112818c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
112918c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
113018c5995bSMatthew G. Knepley       const PetscInt  oldp   = unsplitPoints[dep][p];
11312582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
113218c5995bSMatthew G. Knepley       const PetscInt *support;
1133da1dd7e4SMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, e, s;
113418c5995bSMatthew G. Knepley 
113518c5995bSMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
113618c5995bSMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
113739254ff6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
113818c5995bSMatthew G. Knepley       if (dep == 0) {
113918c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
114018c5995bSMatthew G. Knepley 
114139254ff6SMatthew G. Knepley         /* Unsplit vertex: Edges into original vertex, split edges, and new cohesive edge twice */
114239254ff6SMatthew G. Knepley         for (s = 0, qf = 0; s < supportSize; ++s, ++qf) {
114339254ff6SMatthew G. Knepley           ierr = PetscFindInt(support[s], numSplitPoints[dep+1], splitPoints[dep+1], &e);CHKERRQ(ierr);
114439254ff6SMatthew G. Knepley           if (e >= 0) ++qf;
114539254ff6SMatthew G. Knepley         }
114639254ff6SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qf+2);CHKERRQ(ierr);
114718c5995bSMatthew G. Knepley         /* Add hybrid edge */
114818c5995bSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybedge, 2);CHKERRQ(ierr);
1149e1757548SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1150e1757548SMatthew G. Knepley           PetscInt val;
1151e1757548SMatthew G. Knepley 
1152e1757548SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1153e1757548SMatthew G. Knepley           /* Split and unsplit edges produce hybrid faces */
1154da1dd7e4SMatthew G. Knepley           if (val == 1) ++qf;
1155da1dd7e4SMatthew G. Knepley           if (val == (shift2 + 1)) ++qf;
1156e1757548SMatthew G. Knepley         }
1157e1757548SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybedge, qf);CHKERRQ(ierr);
1158*412e9a14SMatthew G. Knepley         ierr = DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR);CHKERRQ(ierr);
115918c5995bSMatthew G. Knepley       } else if (dep == dim-2) {
116018c5995bSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
1161cd0c2139SMatthew G Knepley         PetscInt       val;
1162cd0c2139SMatthew G Knepley 
1163da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1164cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1165da1dd7e4SMatthew G. Knepley           if (val == dim-1) qf += 2;
1166da1dd7e4SMatthew G. Knepley           else              ++qf;
1167cd0c2139SMatthew G Knepley         }
116818c5995bSMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and cohesive face twice */
1169da1dd7e4SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qf+2);CHKERRQ(ierr);
117018c5995bSMatthew G. Knepley         /* Add hybrid face */
1171da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1172da1dd7e4SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1173da1dd7e4SMatthew G. Knepley           if (val == dim-1) ++qf;
1174da1dd7e4SMatthew G. Knepley         }
117518c5995bSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybface, 4);CHKERRQ(ierr);
1176da1dd7e4SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybface, qf);CHKERRQ(ierr);
1177*412e9a14SMatthew G. Knepley         ierr = DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR);CHKERRQ(ierr);
1178cd0c2139SMatthew G Knepley       }
1179cd0c2139SMatthew G Knepley     }
1180cd0c2139SMatthew G Knepley   }
1181cd0c2139SMatthew G Knepley   /* Step 4: Setup split DM */
1182cd0c2139SMatthew G Knepley   ierr = DMSetUp(sdm);CHKERRQ(ierr);
1183cd0c2139SMatthew G Knepley   ierr = DMPlexShiftPoints_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
1184dcbb62e8SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(sdm, &maxConeSizeNew, &maxSupportSizeNew);CHKERRQ(ierr);
1185dcca6d9dSJed Brown   ierr = PetscMalloc3(PetscMax(maxConeSize, maxConeSizeNew)*3,&coneNew,PetscMax(maxConeSize, maxConeSizeNew)*3,&coneONew,PetscMax(maxSupportSize, maxSupportSizeNew),&supportNew);CHKERRQ(ierr);
1186cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
1187cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1188cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1189cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
11902582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
11914c367dbcSMatthew G. Knepley       const PetscInt  splitp = p    + pMaxNew[dep];
1192cd0c2139SMatthew G Knepley       const PetscInt *cone, *support, *ornt;
11934c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, q, qf, qn, qp, v, e, s;
1194cd0c2139SMatthew G Knepley 
1195cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
1196cd0c2139SMatthew G Knepley       ierr = DMPlexGetCone(dm, oldp, &cone);CHKERRQ(ierr);
1197cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeOrientation(dm, oldp, &ornt);CHKERRQ(ierr);
1198cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
1199cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
1200cd0c2139SMatthew G Knepley       if (dep == depth-1) {
120196a07cd0SMatthew G. Knepley         PetscBool       hasUnsplit = PETSC_FALSE;
12024c367dbcSMatthew G. Knepley         const PetscInt  hybcell    = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1203cd0c2139SMatthew G Knepley         const PetscInt *supportF;
1204cd0c2139SMatthew G Knepley 
1205cd0c2139SMatthew G Knepley         /* Split face:       copy in old face to new face to start */
1206cd0c2139SMatthew G Knepley         ierr = DMPlexGetSupport(sdm, newp,  &supportF);CHKERRQ(ierr);
1207cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, splitp, supportF);CHKERRQ(ierr);
1208cd0c2139SMatthew G Knepley         /* Split old face:   old vertices/edges in cone so no change */
1209cd0c2139SMatthew G Knepley         /* Split new face:   new vertices/edges in cone */
1210cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
12114c367dbcSMatthew G. Knepley           ierr = PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);CHKERRQ(ierr);
121218c5995bSMatthew G. Knepley           if (v < 0) {
121318c5995bSMatthew G. Knepley             ierr = PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
121418c5995bSMatthew G. Knepley             if (v < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %d in split or unsplit points of depth %d", cone[q], dep-1);
12152582d50cSToby Isaac             coneNew[2+q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
121696a07cd0SMatthew G. Knepley             hasUnsplit   = PETSC_TRUE;
121718c5995bSMatthew G. Knepley           } else {
12184c367dbcSMatthew G. Knepley             coneNew[2+q] = v + pMaxNew[dep-1];
1219163235baSMatthew G. Knepley             if (dep > 1) {
1220163235baSMatthew G. Knepley               const PetscInt *econe;
1221163235baSMatthew G. Knepley               PetscInt        econeSize, r, vs, vu;
1222163235baSMatthew G. Knepley 
1223163235baSMatthew G. Knepley               ierr = DMPlexGetConeSize(dm, cone[q], &econeSize);CHKERRQ(ierr);
1224163235baSMatthew G. Knepley               ierr = DMPlexGetCone(dm, cone[q], &econe);CHKERRQ(ierr);
1225163235baSMatthew G. Knepley               for (r = 0; r < econeSize; ++r) {
1226163235baSMatthew G. Knepley                 ierr = PetscFindInt(econe[r], numSplitPoints[dep-2],   splitPoints[dep-2],   &vs);CHKERRQ(ierr);
1227163235baSMatthew G. Knepley                 ierr = PetscFindInt(econe[r], numUnsplitPoints[dep-2], unsplitPoints[dep-2], &vu);CHKERRQ(ierr);
1228163235baSMatthew G. Knepley                 if (vs >= 0) continue;
1229163235baSMatthew G. Knepley                 if (vu < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %d in split or unsplit points of depth %d", econe[r], dep-2);
1230163235baSMatthew G. Knepley                 hasUnsplit   = PETSC_TRUE;
1231163235baSMatthew G. Knepley               }
1232163235baSMatthew G. Knepley             }
1233cd0c2139SMatthew G Knepley           }
1234cd0c2139SMatthew G Knepley         }
1235cd0c2139SMatthew G Knepley         ierr = DMPlexSetCone(sdm, splitp, &coneNew[2]);CHKERRQ(ierr);
1236cd0c2139SMatthew G Knepley         ierr = DMPlexSetConeOrientation(sdm, splitp, ornt);CHKERRQ(ierr);
1237e537020bSMatthew G. Knepley         /* Face support */
1238cd0c2139SMatthew G Knepley         for (s = 0; s < supportSize; ++s) {
1239cd0c2139SMatthew G Knepley           PetscInt val;
1240cd0c2139SMatthew G Knepley 
1241cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[s], &val);CHKERRQ(ierr);
1242cd0c2139SMatthew G Knepley           if (val < 0) {
1243cd0c2139SMatthew G Knepley             /* Split old face:   Replace negative side cell with cohesive cell */
12444c367dbcSMatthew G. Knepley              ierr = DMPlexInsertSupport(sdm, newp, s, hybcell);CHKERRQ(ierr);
1245cd0c2139SMatthew G Knepley           } else {
1246cd0c2139SMatthew G Knepley             /* Split new face:   Replace positive side cell with cohesive cell */
12474c367dbcSMatthew G. Knepley             ierr = DMPlexInsertSupport(sdm, splitp, s, hybcell);CHKERRQ(ierr);
1248e537020bSMatthew G. Knepley             /* Get orientation for cohesive face */
1249e537020bSMatthew G. Knepley             {
1250e537020bSMatthew G. Knepley               const PetscInt *ncone, *nconeO;
1251e537020bSMatthew G. Knepley               PetscInt        nconeSize, nc;
1252e537020bSMatthew G. Knepley 
1253e537020bSMatthew G. Knepley               ierr = DMPlexGetConeSize(dm, support[s], &nconeSize);CHKERRQ(ierr);
1254e537020bSMatthew G. Knepley               ierr = DMPlexGetCone(dm, support[s], &ncone);CHKERRQ(ierr);
1255e537020bSMatthew G. Knepley               ierr = DMPlexGetConeOrientation(dm, support[s], &nconeO);CHKERRQ(ierr);
1256e537020bSMatthew G. Knepley               for (nc = 0; nc < nconeSize; ++nc) {
1257e537020bSMatthew G. Knepley                 if (ncone[nc] == oldp) {
1258e537020bSMatthew G. Knepley                   coneONew[0] = nconeO[nc];
1259e537020bSMatthew G. Knepley                   break;
1260cd0c2139SMatthew G Knepley                 }
1261cd0c2139SMatthew G Knepley               }
1262e537020bSMatthew G. Knepley               if (nc >= nconeSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate face %d in neighboring cell %d", oldp, support[s]);
1263e537020bSMatthew G. Knepley             }
1264e537020bSMatthew G. Knepley           }
1265e537020bSMatthew G. Knepley         }
12664c367dbcSMatthew G. Knepley         /* Cohesive cell:    Old and new split face, then new cohesive faces */
1267fd4b9f15SMatthew G. Knepley         coneNew[0]  = newp;   /* Extracted negative side orientation above */
12684c367dbcSMatthew G. Knepley         coneNew[1]  = splitp;
12694c367dbcSMatthew G. Knepley         coneONew[1] = coneONew[0];
1270e537020bSMatthew G. Knepley         for (q = 0; q < coneSize; ++q) {
1271*412e9a14SMatthew G. Knepley           /* Hybrid faces must follow order from oriented end face */
1272*412e9a14SMatthew G. Knepley           const PetscInt o  = coneONew[0];
1273*412e9a14SMatthew G. Knepley           const PetscInt qo = o < 0 ? (-(o+1)+coneSize-q)%coneSize : (q+o)%coneSize;
1274*412e9a14SMatthew G. Knepley 
1275*412e9a14SMatthew G. Knepley           ierr = PetscFindInt(cone[qo], numSplitPoints[dep-1], splitPoints[dep-1], &v);CHKERRQ(ierr);
127618c5995bSMatthew G. Knepley           if (v < 0) {
1277*412e9a14SMatthew G. Knepley             ierr = PetscFindInt(cone[qo], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
127818c5995bSMatthew G. Knepley             coneNew[2+q]  = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
127918c5995bSMatthew G. Knepley           } else {
128018c5995bSMatthew G. Knepley             coneNew[2+q]  = v + pMaxNew[dep] + numSplitPoints[dep];
128118c5995bSMatthew G. Knepley           }
1282*412e9a14SMatthew G. Knepley           coneONew[2+q] = ((o < 0) + (ornt[qo] < 0))%2 ? -1 : 0;
1283e537020bSMatthew G. Knepley         }
12844c367dbcSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybcell, coneNew);CHKERRQ(ierr);
12854c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeOrientation(sdm, hybcell, coneONew);CHKERRQ(ierr);
128696a07cd0SMatthew G. Knepley         /* Label the hybrid cells on the boundary of the split */
128796a07cd0SMatthew G. Knepley         if (hasUnsplit) {ierr = DMLabelSetValue(label, -hybcell, dim);CHKERRQ(ierr);}
1288cd0c2139SMatthew G Knepley       } else if (dep == 0) {
12894c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1290cd0c2139SMatthew G Knepley 
1291cd0c2139SMatthew G Knepley         /* Split old vertex: Edges in old split faces and new cohesive edge */
12924c367dbcSMatthew G. Knepley         for (e = 0, qn = 0; e < supportSize; ++e) {
1293cd0c2139SMatthew G Knepley           PetscInt val;
1294cd0c2139SMatthew G Knepley 
1295cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1296cd0c2139SMatthew G Knepley           if ((val == 1) || (val == (shift + 1))) {
12972582d50cSToby Isaac             supportNew[qn++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1298cd0c2139SMatthew G Knepley           }
1299cd0c2139SMatthew G Knepley         }
13004c367dbcSMatthew G. Knepley         supportNew[qn] = hybedge;
1301cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
1302cd0c2139SMatthew G Knepley         /* Split new vertex: Edges in new split faces and new cohesive edge */
13034c367dbcSMatthew G. Knepley         for (e = 0, qp = 0; e < supportSize; ++e) {
1304cd0c2139SMatthew G Knepley           PetscInt val, edge;
1305cd0c2139SMatthew G Knepley 
1306cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1307cd0c2139SMatthew G Knepley           if (val == 1) {
13084c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);CHKERRQ(ierr);
1309cd0c2139SMatthew G Knepley             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
13104c367dbcSMatthew G. Knepley             supportNew[qp++] = edge + pMaxNew[dep+1];
1311cd0c2139SMatthew G Knepley           } else if (val == -(shift + 1)) {
13122582d50cSToby Isaac             supportNew[qp++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1313cd0c2139SMatthew G Knepley           }
1314cd0c2139SMatthew G Knepley         }
13154c367dbcSMatthew G. Knepley         supportNew[qp] = hybedge;
1316cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, splitp, supportNew);CHKERRQ(ierr);
13174c367dbcSMatthew G. Knepley         /* Hybrid edge:    Old and new split vertex */
1318cd0c2139SMatthew G Knepley         coneNew[0] = newp;
1319cd0c2139SMatthew G Knepley         coneNew[1] = splitp;
13204c367dbcSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybedge, coneNew);CHKERRQ(ierr);
13214c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
13224c367dbcSMatthew G. Knepley           PetscInt val, edge;
13234c367dbcSMatthew G. Knepley 
13244c367dbcSMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
13254c367dbcSMatthew G. Knepley           if (val == 1) {
13264c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);CHKERRQ(ierr);
13274c367dbcSMatthew G. Knepley             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
13284c367dbcSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2];
13294c367dbcSMatthew G. Knepley           }
13304c367dbcSMatthew G. Knepley         }
13314c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybedge, supportNew);CHKERRQ(ierr);
1332cd0c2139SMatthew G Knepley       } else if (dep == dim-2) {
13334c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
13344c367dbcSMatthew G. Knepley 
1335cd0c2139SMatthew G Knepley         /* Split old edge:   old vertices in cone so no change */
1336cd0c2139SMatthew G Knepley         /* Split new edge:   new vertices in cone */
1337cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
13384c367dbcSMatthew G. Knepley           ierr = PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);CHKERRQ(ierr);
1339e1757548SMatthew G. Knepley           if (v < 0) {
1340e1757548SMatthew G. Knepley             ierr = PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
1341e1757548SMatthew G. Knepley             if (v < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %d in split or unsplit points of depth %d", cone[q], dep-1);
13422582d50cSToby Isaac             coneNew[q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
1343e1757548SMatthew G. Knepley           } else {
13444c367dbcSMatthew G. Knepley             coneNew[q] = v + pMaxNew[dep-1];
1345cd0c2139SMatthew G Knepley           }
1346e1757548SMatthew G. Knepley         }
1347cd0c2139SMatthew G Knepley         ierr = DMPlexSetCone(sdm, splitp, coneNew);CHKERRQ(ierr);
1348cd0c2139SMatthew G Knepley         /* Split old edge: Faces in positive side cells and old split faces */
1349cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1350cd0c2139SMatthew G Knepley           PetscInt val;
1351cd0c2139SMatthew G Knepley 
1352cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
13534c367dbcSMatthew G. Knepley           if (val == dim-1) {
13542582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
13554c367dbcSMatthew G. Knepley           } else if (val == (shift + dim-1)) {
13562582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1357cd0c2139SMatthew G Knepley           }
1358cd0c2139SMatthew G Knepley         }
1359b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1360cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
1361cd0c2139SMatthew G Knepley         /* Split new edge: Faces in negative side cells and new split faces */
1362cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1363cd0c2139SMatthew G Knepley           PetscInt val, face;
1364cd0c2139SMatthew G Knepley 
1365cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1366cd0c2139SMatthew G Knepley           if (val == dim-1) {
13674c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
1368cd0c2139SMatthew G Knepley             if (face < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[e]);
13694c367dbcSMatthew G. Knepley             supportNew[q++] = face + pMaxNew[dep+1];
1370cd0c2139SMatthew G Knepley           } else if (val == -(shift + dim-1)) {
13712582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1372cd0c2139SMatthew G Knepley           }
1373cd0c2139SMatthew G Knepley         }
1374b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1375cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, splitp, supportNew);CHKERRQ(ierr);
13764c367dbcSMatthew G. Knepley         /* Hybrid face */
13774c367dbcSMatthew G. Knepley         coneNew[0] = newp;
13784c367dbcSMatthew G. Knepley         coneNew[1] = splitp;
13794c367dbcSMatthew G. Knepley         for (v = 0; v < coneSize; ++v) {
13804c367dbcSMatthew G. Knepley           PetscInt vertex;
13814c367dbcSMatthew G. Knepley           ierr = PetscFindInt(cone[v], numSplitPoints[dep-1], splitPoints[dep-1], &vertex);CHKERRQ(ierr);
1382e1757548SMatthew G. Knepley           if (vertex < 0) {
1383e1757548SMatthew G. Knepley             ierr = PetscFindInt(cone[v], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &vertex);CHKERRQ(ierr);
1384e1757548SMatthew G. Knepley             if (vertex < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %d in split or unsplit points of depth %d", cone[v], dep-1);
1385e1757548SMatthew G. Knepley             coneNew[2+v] = vertex + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1386e1757548SMatthew G. Knepley           } else {
13874c367dbcSMatthew G. Knepley             coneNew[2+v] = vertex + pMaxNew[dep] + numSplitPoints[dep];
13884c367dbcSMatthew G. Knepley           }
1389e1757548SMatthew G. Knepley         }
13904c367dbcSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybface, coneNew);CHKERRQ(ierr);
13914c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
13924c367dbcSMatthew G. Knepley           PetscInt val, face;
13934c367dbcSMatthew G. Knepley 
13944c367dbcSMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
13954c367dbcSMatthew G. Knepley           if (val == dim-1) {
13964c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
13974c367dbcSMatthew G. Knepley             if (face < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[e]);
13984c367dbcSMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+2] + numSplitPoints[dep+2];
13994c367dbcSMatthew G. Knepley           }
14004c367dbcSMatthew G. Knepley         }
14014c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybface, supportNew);CHKERRQ(ierr);
1402cd0c2139SMatthew G Knepley       }
1403cd0c2139SMatthew G Knepley     }
1404cd0c2139SMatthew G Knepley   }
140518c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
140618c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
140718c5995bSMatthew G. Knepley       const PetscInt  oldp   = unsplitPoints[dep][p];
14082582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
140918c5995bSMatthew G. Knepley       const PetscInt *cone, *support, *ornt;
1410e1757548SMatthew G. Knepley       PetscInt        coneSize, supportSize, supportSizeNew, q, qf, e, f, s;
141118c5995bSMatthew G. Knepley 
141218c5995bSMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
141318c5995bSMatthew G. Knepley       ierr = DMPlexGetCone(dm, oldp, &cone);CHKERRQ(ierr);
141418c5995bSMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, oldp, &ornt);CHKERRQ(ierr);
141518c5995bSMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
141618c5995bSMatthew G. Knepley       ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
141718c5995bSMatthew G. Knepley       if (dep == 0) {
141818c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
141918c5995bSMatthew G. Knepley 
142018c5995bSMatthew G. Knepley         /* Unsplit vertex */
142118c5995bSMatthew G. Knepley         ierr = DMPlexGetSupportSize(sdm, newp, &supportSizeNew);CHKERRQ(ierr);
142218c5995bSMatthew G. Knepley         for (s = 0, q = 0; s < supportSize; ++s) {
14232582d50cSToby Isaac           supportNew[q++] = DMPlexShiftPoint_Internal(support[s], depth, depthShift) /*support[s] + depthOffset[dep+1]*/;
142418c5995bSMatthew G. Knepley           ierr = PetscFindInt(support[s], numSplitPoints[dep+1], splitPoints[dep+1], &e);CHKERRQ(ierr);
142518c5995bSMatthew G. Knepley           if (e >= 0) {
142618c5995bSMatthew G. Knepley             supportNew[q++] = e + pMaxNew[dep+1];
142718c5995bSMatthew G. Knepley           }
142818c5995bSMatthew G. Knepley         }
142918c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
143018c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
143118c5995bSMatthew G. Knepley         if (q != supportSizeNew) SETERRQ3(comm, PETSC_ERR_ARG_WRONG, "Support size %d != %d for vertex %d", q, supportSizeNew, newp);
143218c5995bSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
143318c5995bSMatthew G. Knepley         /* Hybrid edge */
143418c5995bSMatthew G. Knepley         coneNew[0] = newp;
143518c5995bSMatthew G. Knepley         coneNew[1] = newp;
143618c5995bSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybedge, coneNew);CHKERRQ(ierr);
143718c5995bSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
143818c5995bSMatthew G. Knepley           PetscInt val, edge;
143918c5995bSMatthew G. Knepley 
144018c5995bSMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
144118c5995bSMatthew G. Knepley           if (val == 1) {
144218c5995bSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);CHKERRQ(ierr);
144318c5995bSMatthew G. Knepley             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
144418c5995bSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2];
1445e1757548SMatthew G. Knepley           } else if  (val ==  (shift2 + 1)) {
1446e1757548SMatthew G. Knepley             ierr = PetscFindInt(support[e], numUnsplitPoints[dep+1], unsplitPoints[dep+1], &edge);CHKERRQ(ierr);
1447e1757548SMatthew G. Knepley             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a unsplit edge", support[e]);
1448e1757548SMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2] + numSplitPoints[dep+1];
144918c5995bSMatthew G. Knepley           }
145018c5995bSMatthew G. Knepley         }
145118c5995bSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybedge, supportNew);CHKERRQ(ierr);
1452e1757548SMatthew G. Knepley       } else if (dep == dim-2) {
1453e1757548SMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
1454e1757548SMatthew G. Knepley 
1455da1dd7e4SMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and hybrid face twice */
1456e1757548SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1457e1757548SMatthew G. Knepley           PetscInt val, face;
1458e1757548SMatthew G. Knepley 
1459e1757548SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[f], &val);CHKERRQ(ierr);
1460e1757548SMatthew G. Knepley           if (val == dim-1) {
1461e1757548SMatthew G. Knepley             ierr = PetscFindInt(support[f], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
1462e1757548SMatthew G. Knepley             if (face < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[f]);
14632582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1464e1757548SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+1];
1465e1757548SMatthew G. Knepley           } else {
14662582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1467e1757548SMatthew G. Knepley           }
1468e1757548SMatthew G. Knepley         }
1469e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
1470e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
1471da1dd7e4SMatthew G. Knepley         ierr = DMPlexGetSupportSize(sdm, newp, &supportSizeNew);CHKERRQ(ierr);
1472da1dd7e4SMatthew G. Knepley         if (qf != supportSizeNew) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Support size for unsplit edge %d is %d != %d\n", newp, qf, supportSizeNew);
1473e1757548SMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
1474e1757548SMatthew G. Knepley         /* Add hybrid face */
1475e1757548SMatthew G. Knepley         coneNew[0] = newp;
1476212cc919SMatthew G. Knepley         coneNew[1] = newp;
1477e1757548SMatthew G. Knepley         ierr = PetscFindInt(cone[0], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
1478e1757548SMatthew G. Knepley         if (v < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Vertex %d is not an unsplit vertex", cone[0]);
1479212cc919SMatthew G. Knepley         coneNew[2] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1480e1757548SMatthew G. Knepley         ierr = PetscFindInt(cone[1], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
1481e1757548SMatthew G. Knepley         if (v < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Vertex %d is not an unsplit vertex", cone[1]);
1482e1757548SMatthew G. Knepley         coneNew[3] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1483e1757548SMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybface, coneNew);CHKERRQ(ierr);
1484da1dd7e4SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1485da1dd7e4SMatthew G. Knepley           PetscInt val, face;
1486da1dd7e4SMatthew G. Knepley 
1487da1dd7e4SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[f], &val);CHKERRQ(ierr);
1488da1dd7e4SMatthew G. Knepley           if (val == dim-1) {
1489da1dd7e4SMatthew G. Knepley             ierr = PetscFindInt(support[f], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
1490da1dd7e4SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+2] + numSplitPoints[dep+2];
1491da1dd7e4SMatthew G. Knepley           }
1492da1dd7e4SMatthew G. Knepley         }
1493da1dd7e4SMatthew G. Knepley         ierr = DMPlexGetSupportSize(sdm, hybface, &supportSizeNew);CHKERRQ(ierr);
1494da1dd7e4SMatthew G. Knepley         if (qf != supportSizeNew) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Support size for hybrid face %d is %d != %d\n", hybface, qf, supportSizeNew);
1495e1757548SMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybface, supportNew);CHKERRQ(ierr);
1496cd0c2139SMatthew G Knepley       }
1497cd0c2139SMatthew G Knepley     }
1498cd0c2139SMatthew G Knepley   }
1499cd0c2139SMatthew G Knepley   /* Step 6b: Replace split points in negative side cones */
1500cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1501cd0c2139SMatthew G Knepley     PetscInt        dep = values[sp];
1502cd0c2139SMatthew G Knepley     IS              pIS;
1503cd0c2139SMatthew G Knepley     PetscInt        numPoints;
1504cd0c2139SMatthew G Knepley     const PetscInt *points;
1505cd0c2139SMatthew G Knepley 
1506cd0c2139SMatthew G Knepley     if (dep >= 0) continue;
1507cd0c2139SMatthew G Knepley     ierr = DMLabelGetStratumIS(label, dep, &pIS);CHKERRQ(ierr);
1508cd0c2139SMatthew G Knepley     if (!pIS) continue;
1509cd0c2139SMatthew G Knepley     dep  = -dep - shift;
1510cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(pIS, &numPoints);CHKERRQ(ierr);
1511cd0c2139SMatthew G Knepley     ierr = ISGetIndices(pIS, &points);CHKERRQ(ierr);
1512cd0c2139SMatthew G Knepley     for (p = 0; p < numPoints; ++p) {
1513cd0c2139SMatthew G Knepley       const PetscInt  oldp = points[p];
15142582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*depthOffset[dep] + oldp*/;
1515cd0c2139SMatthew G Knepley       const PetscInt *cone;
1516cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
151750cf782dSMatthew G. Knepley       /* PetscBool       replaced = PETSC_FALSE; */
1518cd0c2139SMatthew G Knepley 
1519cd0c2139SMatthew G Knepley       /* Negative edge: replace split vertex */
1520cd0c2139SMatthew G Knepley       /* Negative cell: replace split face */
1521cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(sdm, newp, &coneSize);CHKERRQ(ierr);
1522cd0c2139SMatthew G Knepley       ierr = DMPlexGetCone(sdm, newp, &cone);CHKERRQ(ierr);
1523cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
1524e38fbfedSToby Isaac         const PetscInt coldp = DMPlexShiftPointInverse_Internal(cone[c],depth,depthShift);
1525cd0c2139SMatthew G Knepley         PetscInt       csplitp, cp, val;
1526cd0c2139SMatthew G Knepley 
1527cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(label, coldp, &val);CHKERRQ(ierr);
1528cd0c2139SMatthew G Knepley         if (val == dep-1) {
1529cd0c2139SMatthew G Knepley           ierr = PetscFindInt(coldp, numSplitPoints[dep-1], splitPoints[dep-1], &cp);CHKERRQ(ierr);
1530cd0c2139SMatthew G Knepley           if (cp < 0) SETERRQ2(comm, PETSC_ERR_ARG_WRONG, "Point %d is not a split point of dimension %d", oldp, dep-1);
1531cd0c2139SMatthew G Knepley           csplitp  = pMaxNew[dep-1] + cp;
1532cd0c2139SMatthew G Knepley           ierr     = DMPlexInsertCone(sdm, newp, c, csplitp);CHKERRQ(ierr);
153350cf782dSMatthew G. Knepley           /* replaced = PETSC_TRUE; */
1534cd0c2139SMatthew G Knepley         }
1535cd0c2139SMatthew G Knepley       }
15364a189a86SMatthew G. Knepley       /* Cells with only a vertex or edge on the submesh have no replacement */
15374a189a86SMatthew G. Knepley       /* if (!replaced) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "The cone of point %d does not contain split points", oldp); */
1538cd0c2139SMatthew G Knepley     }
1539cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(pIS, &points);CHKERRQ(ierr);
1540cd0c2139SMatthew G Knepley     ierr = ISDestroy(&pIS);CHKERRQ(ierr);
1541cd0c2139SMatthew G Knepley   }
1542fa8e8ae5SToby Isaac   /* Step 7: Coordinates */
1543cd0c2139SMatthew G Knepley   ierr = DMPlexShiftCoordinates_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
154469d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(sdm, &coordSection);CHKERRQ(ierr);
1545cd0c2139SMatthew G Knepley   ierr = DMGetCoordinatesLocal(sdm, &coordinates);CHKERRQ(ierr);
1546cd0c2139SMatthew G Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
1547cd0c2139SMatthew G Knepley   for (v = 0; v < (numSplitPoints ? numSplitPoints[0] : 0); ++v) {
15482582d50cSToby Isaac     const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[0][v], depth, depthShift) /*depthOffset[0] + splitPoints[0][v]*/;
1549cd0c2139SMatthew G Knepley     const PetscInt splitp = pMaxNew[0] + v;
1550cd0c2139SMatthew G Knepley     PetscInt       dof, off, soff, d;
1551cd0c2139SMatthew G Knepley 
1552cd0c2139SMatthew G Knepley     ierr = PetscSectionGetDof(coordSection, newp, &dof);CHKERRQ(ierr);
1553cd0c2139SMatthew G Knepley     ierr = PetscSectionGetOffset(coordSection, newp, &off);CHKERRQ(ierr);
1554cd0c2139SMatthew G Knepley     ierr = PetscSectionGetOffset(coordSection, splitp, &soff);CHKERRQ(ierr);
1555cd0c2139SMatthew G Knepley     for (d = 0; d < dof; ++d) coords[soff+d] = coords[off+d];
1556cd0c2139SMatthew G Knepley   }
1557cd0c2139SMatthew G Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
1558fa8e8ae5SToby Isaac   /* Step 8: SF, if I can figure this out we can split the mesh in parallel */
1559cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSF_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
1560fa8e8ae5SToby Isaac   /* Step 9: Labels */
1561cd0c2139SMatthew G Knepley   ierr = DMPlexShiftLabels_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
1562c58f1c22SToby Isaac   ierr = DMGetNumLabels(sdm, &numLabels);CHKERRQ(ierr);
1563cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1564cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
15652582d50cSToby Isaac       const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[dep][p], depth, depthShift) /*depthOffset[dep] + splitPoints[dep][p]*/;
1566cd0c2139SMatthew G Knepley       const PetscInt splitp = pMaxNew[dep] + p;
1567cd0c2139SMatthew G Knepley       PetscInt       l;
1568cd0c2139SMatthew G Knepley 
15697db7e0a7SMatthew G. Knepley       if (splitLabel) {
15707db7e0a7SMatthew G. Knepley         const PetscInt val = 100 + dep;
15717db7e0a7SMatthew G. Knepley 
15727db7e0a7SMatthew G. Knepley         ierr = DMLabelSetValue(splitLabel, newp,    val);CHKERRQ(ierr);
15737db7e0a7SMatthew G. Knepley         ierr = DMLabelSetValue(splitLabel, splitp, -val);CHKERRQ(ierr);
15747db7e0a7SMatthew G. Knepley       }
1575cd0c2139SMatthew G Knepley       for (l = 0; l < numLabels; ++l) {
1576cd0c2139SMatthew G Knepley         DMLabel     mlabel;
1577cd0c2139SMatthew G Knepley         const char *lname;
1578cd0c2139SMatthew G Knepley         PetscInt    val;
15799a356370SMatthew G. Knepley         PetscBool   isDepth;
1580cd0c2139SMatthew G Knepley 
1581c58f1c22SToby Isaac         ierr = DMGetLabelName(sdm, l, &lname);CHKERRQ(ierr);
15829a356370SMatthew G. Knepley         ierr = PetscStrcmp(lname, "depth", &isDepth);CHKERRQ(ierr);
15839a356370SMatthew G. Knepley         if (isDepth) continue;
1584c58f1c22SToby Isaac         ierr = DMGetLabel(sdm, lname, &mlabel);CHKERRQ(ierr);
1585cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(mlabel, newp, &val);CHKERRQ(ierr);
1586cd0c2139SMatthew G Knepley         if (val >= 0) {
1587cd0c2139SMatthew G Knepley           ierr = DMLabelSetValue(mlabel, splitp, val);CHKERRQ(ierr);
1588cd0c2139SMatthew G Knepley         }
1589cd0c2139SMatthew G Knepley       }
1590cd0c2139SMatthew G Knepley     }
1591cd0c2139SMatthew G Knepley   }
1592cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1593cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1594cd0c2139SMatthew G Knepley 
1595cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
159618c5995bSMatthew G. Knepley     if (splitIS[dep]) {ierr = ISRestoreIndices(splitIS[dep], &splitPoints[dep]);CHKERRQ(ierr);}
159718c5995bSMatthew G. Knepley     ierr = ISDestroy(&splitIS[dep]);CHKERRQ(ierr);
159818c5995bSMatthew G. Knepley     if (unsplitIS[dep]) {ierr = ISRestoreIndices(unsplitIS[dep], &unsplitPoints[dep]);CHKERRQ(ierr);}
159918c5995bSMatthew G. Knepley     ierr = ISDestroy(&unsplitIS[dep]);CHKERRQ(ierr);
1600cd0c2139SMatthew G Knepley   }
1601cd0c2139SMatthew G Knepley   if (label) {
1602cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
1603cd0c2139SMatthew G Knepley     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
1604cd0c2139SMatthew G Knepley   }
16050d4d4d06SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
16060d4d4d06SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(sdm, d, NULL, &pEnd);CHKERRQ(ierr);
160736dbac82SMatthew G. Knepley     pMaxNew[d] = pEnd - numHybridPoints[d] - numHybridPointsOld[d];
16080d4d4d06SMatthew G. Knepley   }
1609dcbb62e8SMatthew G. Knepley   ierr = PetscFree3(coneNew, coneONew, supportNew);CHKERRQ(ierr);
16102582d50cSToby Isaac   ierr = PetscFree5(depthMax, depthEnd, depthShift, pMaxNew, numHybridPointsOld);CHKERRQ(ierr);
161118c5995bSMatthew G. Knepley   ierr = PetscFree7(splitIS, unsplitIS, numSplitPoints, numUnsplitPoints, numHybridPoints, splitPoints, unsplitPoints);CHKERRQ(ierr);
1612cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1613cd0c2139SMatthew G Knepley }
1614cd0c2139SMatthew G Knepley 
1615cd0c2139SMatthew G Knepley /*@C
1616cd0c2139SMatthew G Knepley   DMPlexConstructCohesiveCells - Construct cohesive cells which split the face along an internal interface
1617cd0c2139SMatthew G Knepley 
1618cd0c2139SMatthew G Knepley   Collective on dm
1619cd0c2139SMatthew G Knepley 
1620cd0c2139SMatthew G Knepley   Input Parameters:
1621cd0c2139SMatthew G Knepley + dm - The original DM
162253156dfcSMatthew G. Knepley - label - The label specifying the boundary faces (this could be auto-generated)
1623cd0c2139SMatthew G Knepley 
1624cd0c2139SMatthew G Knepley   Output Parameters:
16257db7e0a7SMatthew G. Knepley + splitLabel - The label containing the split points, or NULL if no output is desired
1626cd0c2139SMatthew G Knepley - dmSplit - The new DM
1627cd0c2139SMatthew G Knepley 
1628cd0c2139SMatthew G Knepley   Level: developer
1629cd0c2139SMatthew G Knepley 
16302be2b188SMatthew G Knepley .seealso: DMCreate(), DMPlexLabelCohesiveComplete()
1631cd0c2139SMatthew G Knepley @*/
16327db7e0a7SMatthew G. Knepley PetscErrorCode DMPlexConstructCohesiveCells(DM dm, DMLabel label, DMLabel splitLabel, DM *dmSplit)
1633cd0c2139SMatthew G Knepley {
1634cd0c2139SMatthew G Knepley   DM             sdm;
1635cd0c2139SMatthew G Knepley   PetscInt       dim;
1636cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
1637cd0c2139SMatthew G Knepley 
1638cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1639cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
164053156dfcSMatthew G. Knepley   PetscValidPointer(dmSplit, 3);
1641cd0c2139SMatthew G Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), &sdm);CHKERRQ(ierr);
1642cd0c2139SMatthew G Knepley   ierr = DMSetType(sdm, DMPLEX);CHKERRQ(ierr);
1643c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1644c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(sdm, dim);CHKERRQ(ierr);
1645cd0c2139SMatthew G Knepley   switch (dim) {
1646cd0c2139SMatthew G Knepley   case 2:
1647cd0c2139SMatthew G Knepley   case 3:
16487db7e0a7SMatthew G. Knepley     ierr = DMPlexConstructCohesiveCells_Internal(dm, label, splitLabel, sdm);CHKERRQ(ierr);
1649cd0c2139SMatthew G Knepley     break;
1650cd0c2139SMatthew G Knepley   default:
1651cd0c2139SMatthew G Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct cohesive cells for dimension %d", dim);
1652cd0c2139SMatthew G Knepley   }
1653cd0c2139SMatthew G Knepley   *dmSplit = sdm;
1654cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1655cd0c2139SMatthew G Knepley }
1656cd0c2139SMatthew G Knepley 
16570f66a230SMatthew G. Knepley /* Returns the side of the surface for a given cell with a face on the surface */
16580f66a230SMatthew G. Knepley static PetscErrorCode GetSurfaceSide_Static(DM dm, DM subdm, PetscInt numSubpoints, const PetscInt *subpoints, PetscInt cell, PetscInt face, PetscBool *pos)
16590f66a230SMatthew G. Knepley {
16600f66a230SMatthew G. Knepley   const PetscInt *cone, *ornt;
16610f66a230SMatthew G. Knepley   PetscInt        dim, coneSize, c;
16620f66a230SMatthew G. Knepley   PetscErrorCode  ierr;
16630f66a230SMatthew G. Knepley 
16640f66a230SMatthew G. Knepley   PetscFunctionBegin;
16650f66a230SMatthew G. Knepley   *pos = PETSC_TRUE;
1666c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
16670f66a230SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
16680f66a230SMatthew G. Knepley   ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
16690f66a230SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, cell, &ornt);CHKERRQ(ierr);
16700f66a230SMatthew G. Knepley   for (c = 0; c < coneSize; ++c) {
16710f66a230SMatthew G. Knepley     if (cone[c] == face) {
16720f66a230SMatthew G. Knepley       PetscInt o = ornt[c];
16730f66a230SMatthew G. Knepley 
16740f66a230SMatthew G. Knepley       if (subdm) {
16750f66a230SMatthew G. Knepley         const PetscInt *subcone, *subornt;
16760f66a230SMatthew G. Knepley         PetscInt        subpoint, subface, subconeSize, sc;
16770f66a230SMatthew G. Knepley 
16780f66a230SMatthew G. Knepley         ierr = PetscFindInt(cell, numSubpoints, subpoints, &subpoint);CHKERRQ(ierr);
16790f66a230SMatthew G. Knepley         ierr = PetscFindInt(face, numSubpoints, subpoints, &subface);CHKERRQ(ierr);
16800f66a230SMatthew G. Knepley         ierr = DMPlexGetConeSize(subdm, subpoint, &subconeSize);CHKERRQ(ierr);
16810f66a230SMatthew G. Knepley         ierr = DMPlexGetCone(subdm, subpoint, &subcone);CHKERRQ(ierr);
16820f66a230SMatthew G. Knepley         ierr = DMPlexGetConeOrientation(subdm, subpoint, &subornt);CHKERRQ(ierr);
16830f66a230SMatthew G. Knepley         for (sc = 0; sc < subconeSize; ++sc) {
16840f66a230SMatthew G. Knepley           if (subcone[sc] == subface) {
16850f66a230SMatthew G. Knepley             o = subornt[0];
16860f66a230SMatthew G. Knepley             break;
16870f66a230SMatthew G. Knepley           }
16880f66a230SMatthew G. Knepley         }
16890f66a230SMatthew G. Knepley         if (sc >= subconeSize) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find subpoint %d (%d) in cone for subpoint %d (%d)", subface, face, subpoint, cell);
16900f66a230SMatthew G. Knepley       }
16910f66a230SMatthew G. Knepley       if (o >= 0) *pos = PETSC_TRUE;
16920f66a230SMatthew G. Knepley       else        *pos = PETSC_FALSE;
16930f66a230SMatthew G. Knepley       break;
16940f66a230SMatthew G. Knepley     }
16950f66a230SMatthew G. Knepley   }
16960f66a230SMatthew G. Knepley   if (c == coneSize) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Cell %d in split face %d support does not have it in the cone", cell, face);
16970f66a230SMatthew G. Knepley   PetscFunctionReturn(0);
16980f66a230SMatthew G. Knepley }
16990f66a230SMatthew G. Knepley 
1700cd0c2139SMatthew G Knepley /*@
17010f66a230SMatthew G. Knepley   DMPlexLabelCohesiveComplete - Starting with a label marking points on an internal surface, we add all other mesh pieces
1702cd0c2139SMatthew G Knepley   to complete the surface
1703cd0c2139SMatthew G Knepley 
1704cd0c2139SMatthew G Knepley   Input Parameters:
1705cd0c2139SMatthew G Knepley + dm     - The DM
17060f66a230SMatthew G. Knepley . label  - A DMLabel marking the surface
17070f66a230SMatthew G. Knepley . blabel - A DMLabel marking the vertices on the boundary which will not be duplicated, or NULL to find them automatically
1708bb55d314SMatthew G. Knepley . flip   - Flag to flip the submesh normal and replace points on the other side
170947946fd8SMatthew G. Knepley - subdm  - The subDM associated with the label, or NULL
1710cd0c2139SMatthew G Knepley 
1711cd0c2139SMatthew G Knepley   Output Parameter:
1712cd0c2139SMatthew G Knepley . label - A DMLabel marking all surface points
1713cd0c2139SMatthew G Knepley 
17140f66a230SMatthew G. Knepley   Note: The vertices in blabel are called "unsplit" in the terminology from hybrid cell creation.
17150f66a230SMatthew G. Knepley 
1716cd0c2139SMatthew G Knepley   Level: developer
1717cd0c2139SMatthew G Knepley 
17182be2b188SMatthew G Knepley .seealso: DMPlexConstructCohesiveCells(), DMPlexLabelComplete()
1719cd0c2139SMatthew G Knepley @*/
17200f66a230SMatthew G. Knepley PetscErrorCode DMPlexLabelCohesiveComplete(DM dm, DMLabel label, DMLabel blabel, PetscBool flip, DM subdm)
1721cd0c2139SMatthew G Knepley {
1722d90583fdSMatthew G. Knepley   DMLabel         depthLabel;
17238630cb1aSMatthew G. Knepley   IS              dimIS, subpointIS = NULL, facePosIS, faceNegIS, crossEdgeIS = NULL;
172447946fd8SMatthew G. Knepley   const PetscInt *points, *subpoints;
1725bb55d314SMatthew G. Knepley   const PetscInt  rev   = flip ? -1 : 1;
1726*412e9a14SMatthew G. Knepley   PetscInt        shift = 100, shift2 = 200, dim, depth, dep, cStart, cEnd, vStart, vEnd, numPoints, numSubpoints, p, val;
1727cd0c2139SMatthew G Knepley   PetscErrorCode  ierr;
1728cd0c2139SMatthew G Knepley 
1729cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1730370472baSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1731370472baSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1732d90583fdSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
173347946fd8SMatthew G. Knepley   if (subdm) {
173447946fd8SMatthew G. Knepley     ierr = DMPlexCreateSubpointIS(subdm, &subpointIS);CHKERRQ(ierr);
173547946fd8SMatthew G. Knepley     if (subpointIS) {
173647946fd8SMatthew G. Knepley       ierr = ISGetLocalSize(subpointIS, &numSubpoints);CHKERRQ(ierr);
173747946fd8SMatthew G. Knepley       ierr = ISGetIndices(subpointIS, &subpoints);CHKERRQ(ierr);
173847946fd8SMatthew G. Knepley     }
173947946fd8SMatthew G. Knepley   }
1740d7c8f101SMatthew G. Knepley   /* Mark cell on the fault, and its faces which touch the fault: cell orientation for face gives the side of the fault */
1741cd0c2139SMatthew G Knepley   ierr = DMLabelGetStratumIS(label, dim-1, &dimIS);CHKERRQ(ierr);
1742dc86033cSMatthew G. Knepley   if (!dimIS) {
17438630cb1aSMatthew G. Knepley     ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
1744dc86033cSMatthew G. Knepley     PetscFunctionReturn(0);
1745dc86033cSMatthew G. Knepley   }
1746cd0c2139SMatthew G Knepley   ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
1747cd0c2139SMatthew G Knepley   ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
1748d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault faces */
1749cd0c2139SMatthew G Knepley     const PetscInt *support;
1750cd0c2139SMatthew G Knepley     PetscInt        supportSize, s;
1751cd0c2139SMatthew G Knepley 
1752cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupportSize(dm, points[p], &supportSize);CHKERRQ(ierr);
1753c4419245SMatthew G. Knepley #if 0
1754c4419245SMatthew G. Knepley     if (supportSize != 2) {
1755c4419245SMatthew G. Knepley       const PetscInt *lp;
1756c4419245SMatthew G. Knepley       PetscInt        Nlp, pind;
1757c4419245SMatthew G. Knepley 
1758c4419245SMatthew G. Knepley       /* Check that for a cell with a single support face, that face is in the SF */
1759c4419245SMatthew G. Knepley       /*   THis check only works for the remote side. We would need root side information */
1760c4419245SMatthew G. Knepley       ierr = PetscSFGetGraph(dm->sf, NULL, &Nlp, &lp, NULL);CHKERRQ(ierr);
1761c4419245SMatthew G. Knepley       ierr = PetscFindInt(points[p], Nlp, lp, &pind);CHKERRQ(ierr);
1762c4419245SMatthew G. Knepley       if (pind < 0) SETERRQ2(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);
1763c4419245SMatthew G. Knepley     }
1764c4419245SMatthew G. Knepley #endif
1765cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupport(dm, points[p], &support);CHKERRQ(ierr);
1766cd0c2139SMatthew G Knepley     for (s = 0; s < supportSize; ++s) {
17670f66a230SMatthew G. Knepley       const PetscInt *cone;
1768cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
17690f66a230SMatthew G. Knepley       PetscBool       pos;
1770cd0c2139SMatthew G Knepley 
17710f66a230SMatthew G. Knepley       ierr = GetSurfaceSide_Static(dm, subdm, numSubpoints, subpoints, support[s], points[p], &pos);CHKERRQ(ierr);
17720f66a230SMatthew G. Knepley       if (pos) {ierr = DMLabelSetValue(label, support[s],  rev*(shift+dim));CHKERRQ(ierr);}
17730f66a230SMatthew G. Knepley       else     {ierr = DMLabelSetValue(label, support[s], -rev*(shift+dim));CHKERRQ(ierr);}
17740f66a230SMatthew G. Knepley       if (rev < 0) pos = !pos ? PETSC_TRUE : PETSC_FALSE;
17750f66a230SMatthew G. Knepley       /* Put faces touching the fault in the label */
1776cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
1777cd0c2139SMatthew G Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
1778cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
1779cd0c2139SMatthew G Knepley         const PetscInt point = cone[c];
1780cd0c2139SMatthew G Knepley 
1781cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1782cd0c2139SMatthew G Knepley         if (val == -1) {
1783cd0c2139SMatthew G Knepley           PetscInt *closure = NULL;
1784cd0c2139SMatthew G Knepley           PetscInt  closureSize, cl;
1785cd0c2139SMatthew G Knepley 
1786cd0c2139SMatthew G Knepley           ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1787cd0c2139SMatthew G Knepley           for (cl = 0; cl < closureSize*2; cl += 2) {
1788cd0c2139SMatthew G Knepley             const PetscInt clp  = closure[cl];
1789a0541d8aSMatthew G. Knepley             PetscInt       bval = -1;
1790cd0c2139SMatthew G Knepley 
1791cd0c2139SMatthew G Knepley             ierr = DMLabelGetValue(label, clp, &val);CHKERRQ(ierr);
1792a0541d8aSMatthew G. Knepley             if (blabel) {ierr = DMLabelGetValue(blabel, clp, &bval);CHKERRQ(ierr);}
1793a0541d8aSMatthew G. Knepley             if ((val >= 0) && (val < dim-1) && (bval < 0)) {
1794cd0c2139SMatthew G Knepley               ierr = DMLabelSetValue(label, point, pos == PETSC_TRUE ? shift+dim-1 : -(shift+dim-1));CHKERRQ(ierr);
1795cd0c2139SMatthew G Knepley               break;
1796cd0c2139SMatthew G Knepley             }
1797cd0c2139SMatthew G Knepley           }
1798cd0c2139SMatthew G Knepley           ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1799cd0c2139SMatthew G Knepley         }
1800cd0c2139SMatthew G Knepley       }
1801cd0c2139SMatthew G Knepley     }
1802cd0c2139SMatthew G Knepley   }
18030f66a230SMatthew G. Knepley   ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
18040f66a230SMatthew G. Knepley   ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
180547946fd8SMatthew G. Knepley   if (subpointIS) {ierr = ISRestoreIndices(subpointIS, &subpoints);CHKERRQ(ierr);}
180647946fd8SMatthew G. Knepley   ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
1807a0541d8aSMatthew G. Knepley   /* Mark boundary points as unsplit */
180886200784SMatthew G. Knepley   if (blabel) {
1809a0541d8aSMatthew G. Knepley     ierr = DMLabelGetStratumIS(blabel, 1, &dimIS);CHKERRQ(ierr);
1810a0541d8aSMatthew G. Knepley     ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
1811a0541d8aSMatthew G. Knepley     ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
1812a0541d8aSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
1813a0541d8aSMatthew G. Knepley       const PetscInt point = points[p];
1814a0541d8aSMatthew G. Knepley       PetscInt       val, bval;
1815a0541d8aSMatthew G. Knepley 
1816a0541d8aSMatthew G. Knepley       ierr = DMLabelGetValue(blabel, point, &bval);CHKERRQ(ierr);
1817a0541d8aSMatthew G. Knepley       if (bval >= 0) {
1818f7019248SMatthew G. Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1819f7019248SMatthew G. Knepley         if ((val < 0) || (val > dim)) {
1820f7019248SMatthew G. Knepley           /* This could be a point added from splitting a vertex on an adjacent fault, otherwise its just wrong */
1821f7019248SMatthew G. Knepley           ierr = DMLabelClearValue(blabel, point, bval);CHKERRQ(ierr);
1822f7019248SMatthew G. Knepley         }
1823f7019248SMatthew G. Knepley       }
1824f7019248SMatthew G. Knepley     }
1825f7019248SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
1826f7019248SMatthew G. Knepley       const PetscInt point = points[p];
1827f7019248SMatthew G. Knepley       PetscInt       val, bval;
1828f7019248SMatthew G. Knepley 
1829f7019248SMatthew G. Knepley       ierr = DMLabelGetValue(blabel, point, &bval);CHKERRQ(ierr);
1830f7019248SMatthew G. Knepley       if (bval >= 0) {
183186200784SMatthew G. Knepley         const PetscInt *cone,    *support;
183286200784SMatthew G. Knepley         PetscInt        coneSize, supportSize, s, valA, valB, valE;
183386200784SMatthew G. Knepley 
1834a0541d8aSMatthew G. Knepley         /* Mark as unsplit */
1835a0541d8aSMatthew G. Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1836a0541d8aSMatthew G. Knepley         if ((val < 0) || (val > dim)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %d has label value %d, should be part of the fault", point, val);
1837a0541d8aSMatthew G. Knepley         ierr = DMLabelClearValue(label, point, val);CHKERRQ(ierr);
1838a0541d8aSMatthew G. Knepley         ierr = DMLabelSetValue(label, point, shift2+val);CHKERRQ(ierr);
18392c06a818SMatthew G. Knepley         /* Check for cross-edge
18402c06a818SMatthew G. Knepley              A cross-edge has endpoints which are both on the boundary of the surface, but the edge itself is not. */
184186200784SMatthew G. Knepley         if (val != 0) continue;
184286200784SMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
184386200784SMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
184486200784SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
184586200784SMatthew G. Knepley           ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
184686200784SMatthew G. Knepley           ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
184786200784SMatthew G. Knepley           if (coneSize != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Edge %D has %D vertices != 2", support[s], coneSize);
184886200784SMatthew G. Knepley           ierr = DMLabelGetValue(blabel, cone[0], &valA);CHKERRQ(ierr);
184986200784SMatthew G. Knepley           ierr = DMLabelGetValue(blabel, cone[1], &valB);CHKERRQ(ierr);
185086200784SMatthew G. Knepley           ierr = DMLabelGetValue(blabel, support[s], &valE);CHKERRQ(ierr);
185109816f77SMatthew G. Knepley           if ((valE < 0) && (valA >= 0) && (valB >= 0) && (cone[0] != cone[1])) {ierr = DMLabelSetValue(blabel, support[s], 2);CHKERRQ(ierr);}
185286200784SMatthew G. Knepley         }
1853a0541d8aSMatthew G. Knepley       }
1854a0541d8aSMatthew G. Knepley     }
1855a0541d8aSMatthew G. Knepley     ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
1856a0541d8aSMatthew G. Knepley     ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
1857a0541d8aSMatthew G. Knepley   }
1858cd0c2139SMatthew G Knepley   /* Search for other cells/faces/edges connected to the fault by a vertex */
1859c4d480a2SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1860*412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1861cd0c2139SMatthew G Knepley   ierr = DMLabelGetStratumIS(label, 0, &dimIS);CHKERRQ(ierr);
186286200784SMatthew G. Knepley   if (blabel) {ierr = DMLabelGetStratumIS(blabel, 2, &crossEdgeIS);CHKERRQ(ierr);}
186386200784SMatthew G. Knepley   if (dimIS && crossEdgeIS) {
186486200784SMatthew G. Knepley     IS vertIS = dimIS;
186586200784SMatthew G. Knepley 
186686200784SMatthew G. Knepley     ierr = ISExpand(vertIS, crossEdgeIS, &dimIS);CHKERRQ(ierr);
186786200784SMatthew G. Knepley     ierr = ISDestroy(&crossEdgeIS);CHKERRQ(ierr);
186886200784SMatthew G. Knepley     ierr = ISDestroy(&vertIS);CHKERRQ(ierr);
186986200784SMatthew G. Knepley   }
1870dc86033cSMatthew G. Knepley   if (!dimIS) {
1871dc86033cSMatthew G. Knepley     PetscFunctionReturn(0);
1872dc86033cSMatthew G. Knepley   }
1873cd0c2139SMatthew G Knepley   ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
1874cd0c2139SMatthew G Knepley   ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
1875d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault vertices */
1876cd0c2139SMatthew G Knepley     PetscInt *star = NULL;
187786200784SMatthew G. Knepley     PetscInt  starSize, s;
1878cd0c2139SMatthew G Knepley     PetscInt  again = 1;  /* 0: Finished 1: Keep iterating after a change 2: No change */
1879cd0c2139SMatthew G Knepley 
1880d4622b2cSMatthew G. Knepley     /* All points connected to the fault are inside a cell, so at the top level we will only check cells */
1881cd0c2139SMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
1882cd0c2139SMatthew G Knepley     while (again) {
1883cd0c2139SMatthew G Knepley       if (again > 1) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Could not classify all cells connected to the fault");
1884cd0c2139SMatthew G Knepley       again = 0;
1885cd0c2139SMatthew G Knepley       for (s = 0; s < starSize*2; s += 2) {
1886cd0c2139SMatthew G Knepley         const PetscInt  point = star[s];
1887cd0c2139SMatthew G Knepley         const PetscInt *cone;
1888cd0c2139SMatthew G Knepley         PetscInt        coneSize, c;
1889cd0c2139SMatthew G Knepley 
1890*412e9a14SMatthew G. Knepley         if ((point < cStart) || (point >= cEnd)) continue;
1891cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1892cd0c2139SMatthew G Knepley         if (val != -1) continue;
1893d4622b2cSMatthew G. Knepley         again = again == 1 ? 1 : 2;
1894cd0c2139SMatthew G Knepley         ierr  = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
1895cd0c2139SMatthew G Knepley         ierr  = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
1896cd0c2139SMatthew G Knepley         for (c = 0; c < coneSize; ++c) {
1897cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, cone[c], &val);CHKERRQ(ierr);
1898cd0c2139SMatthew G Knepley           if (val != -1) {
1899d7c8f101SMatthew G. Knepley             const PetscInt *ccone;
1900166d9d0cSMatthew G. Knepley             PetscInt        cconeSize, cc, side;
1901d7c8f101SMatthew G. Knepley 
1902db8edaafSMatthew G. Knepley             if (PetscAbs(val) < shift) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %d on cell %d has an invalid label %d", cone[c], point, val);
190337a6de01SMatthew G. Knepley             if (val > 0) side =  1;
190437a6de01SMatthew G. Knepley             else         side = -1;
190537a6de01SMatthew G. Knepley             ierr = DMLabelSetValue(label, point, side*(shift+dim));CHKERRQ(ierr);
1906d7c8f101SMatthew G. Knepley             /* Mark cell faces which touch the fault */
1907d7c8f101SMatthew G. Knepley             ierr = DMPlexGetConeSize(dm, point, &cconeSize);CHKERRQ(ierr);
1908d7c8f101SMatthew G. Knepley             ierr = DMPlexGetCone(dm, point, &ccone);CHKERRQ(ierr);
1909d7c8f101SMatthew G. Knepley             for (cc = 0; cc < cconeSize; ++cc) {
1910d7c8f101SMatthew G. Knepley               PetscInt *closure = NULL;
1911d7c8f101SMatthew G. Knepley               PetscInt  closureSize, cl;
1912d7c8f101SMatthew G. Knepley 
1913166d9d0cSMatthew G. Knepley               ierr = DMLabelGetValue(label, ccone[cc], &val);CHKERRQ(ierr);
1914166d9d0cSMatthew G. Knepley               if (val != -1) continue;
1915d7c8f101SMatthew G. Knepley               ierr = DMPlexGetTransitiveClosure(dm, ccone[cc], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1916d4622b2cSMatthew G. Knepley               for (cl = 0; cl < closureSize*2; cl += 2) {
1917d7c8f101SMatthew G. Knepley                 const PetscInt clp = closure[cl];
1918d7c8f101SMatthew G. Knepley 
1919d7c8f101SMatthew G. Knepley                 ierr = DMLabelGetValue(label, clp, &val);CHKERRQ(ierr);
1920d7c8f101SMatthew G. Knepley                 if (val == -1) continue;
1921d7c8f101SMatthew G. Knepley                 ierr = DMLabelSetValue(label, ccone[cc], side*(shift+dim-1));CHKERRQ(ierr);
192237a6de01SMatthew G. Knepley                 break;
192337a6de01SMatthew G. Knepley               }
1924d7c8f101SMatthew G. Knepley               ierr = DMPlexRestoreTransitiveClosure(dm, ccone[cc], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1925cd0c2139SMatthew G Knepley             }
1926cd0c2139SMatthew G Knepley             again = 1;
1927cd0c2139SMatthew G Knepley             break;
1928cd0c2139SMatthew G Knepley           }
1929cd0c2139SMatthew G Knepley         }
1930cd0c2139SMatthew G Knepley       }
1931cd0c2139SMatthew G Knepley     }
1932cd0c2139SMatthew G Knepley     /* Classify the rest by cell membership */
1933cd0c2139SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
1934cd0c2139SMatthew G Knepley       const PetscInt point = star[s];
1935cd0c2139SMatthew G Knepley 
1936cd0c2139SMatthew G Knepley       ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1937cd0c2139SMatthew G Knepley       if (val == -1) {
1938cd0c2139SMatthew G Knepley         PetscInt      *sstar = NULL;
1939cd0c2139SMatthew G Knepley         PetscInt       sstarSize, ss;
1940*412e9a14SMatthew G. Knepley         PetscBool      marked = PETSC_FALSE, isHybrid;
1941cd0c2139SMatthew G Knepley 
1942cd0c2139SMatthew G Knepley         ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &sstarSize, &sstar);CHKERRQ(ierr);
1943cd0c2139SMatthew G Knepley         for (ss = 0; ss < sstarSize*2; ss += 2) {
1944cd0c2139SMatthew G Knepley           const PetscInt spoint = sstar[ss];
1945cd0c2139SMatthew G Knepley 
1946*412e9a14SMatthew G. Knepley           if ((spoint < cStart) || (spoint >= cEnd)) continue;
1947cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, spoint, &val);CHKERRQ(ierr);
1948cd0c2139SMatthew G Knepley           if (val == -1) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %d in star of %d does not have a valid label", spoint, point);
1949d90583fdSMatthew G. Knepley           ierr = DMLabelGetValue(depthLabel, point, &dep);CHKERRQ(ierr);
1950cd0c2139SMatthew G Knepley           if (val > 0) {
1951cd0c2139SMatthew G Knepley             ierr = DMLabelSetValue(label, point,   shift+dep);CHKERRQ(ierr);
1952cd0c2139SMatthew G Knepley           } else {
1953cd0c2139SMatthew G Knepley             ierr = DMLabelSetValue(label, point, -(shift+dep));CHKERRQ(ierr);
1954cd0c2139SMatthew G Knepley           }
1955cd0c2139SMatthew G Knepley           marked = PETSC_TRUE;
1956cd0c2139SMatthew G Knepley           break;
1957cd0c2139SMatthew G Knepley         }
1958cd0c2139SMatthew G Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &sstarSize, &sstar);CHKERRQ(ierr);
1959*412e9a14SMatthew G. Knepley         ierr = DMPlexCellIsHybrid_Internal(dm, point, &isHybrid);CHKERRQ(ierr);
1960*412e9a14SMatthew G. Knepley         if (!isHybrid && !marked) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Point %d could not be classified", point);
1961cd0c2139SMatthew G Knepley       }
1962cd0c2139SMatthew G Knepley     }
1963cd0c2139SMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
1964cd0c2139SMatthew G Knepley   }
1965cd0c2139SMatthew G Knepley   ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
1966cd0c2139SMatthew G Knepley   ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
196718c5995bSMatthew G. Knepley   /* If any faces touching the fault divide cells on either side, split them */
196818c5995bSMatthew G. Knepley   ierr = DMLabelGetStratumIS(label,   shift+dim-1,  &facePosIS);CHKERRQ(ierr);
196918c5995bSMatthew G. Knepley   ierr = DMLabelGetStratumIS(label, -(shift+dim-1), &faceNegIS);CHKERRQ(ierr);
197018c5995bSMatthew G. Knepley   ierr = ISExpand(facePosIS, faceNegIS, &dimIS);CHKERRQ(ierr);
197118c5995bSMatthew G. Knepley   ierr = ISDestroy(&facePosIS);CHKERRQ(ierr);
197218c5995bSMatthew G. Knepley   ierr = ISDestroy(&faceNegIS);CHKERRQ(ierr);
197318c5995bSMatthew G. Knepley   ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
197418c5995bSMatthew G. Knepley   ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
197518c5995bSMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
197618c5995bSMatthew G. Knepley     const PetscInt  point = points[p];
197718c5995bSMatthew G. Knepley     const PetscInt *support;
197818c5995bSMatthew G. Knepley     PetscInt        supportSize, valA, valB;
197918c5995bSMatthew G. Knepley 
198018c5995bSMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
198118c5995bSMatthew G. Knepley     if (supportSize != 2) continue;
198218c5995bSMatthew G. Knepley     ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
198318c5995bSMatthew G. Knepley     ierr = DMLabelGetValue(label, support[0], &valA);CHKERRQ(ierr);
198418c5995bSMatthew G. Knepley     ierr = DMLabelGetValue(label, support[1], &valB);CHKERRQ(ierr);
198518c5995bSMatthew G. Knepley     if ((valA == -1) || (valB == -1)) continue;
198618c5995bSMatthew G. Knepley     if (valA*valB > 0) continue;
198718c5995bSMatthew G. Knepley     /* Split the face */
198818c5995bSMatthew G. Knepley     ierr = DMLabelGetValue(label, point, &valA);CHKERRQ(ierr);
198918c5995bSMatthew G. Knepley     ierr = DMLabelClearValue(label, point, valA);CHKERRQ(ierr);
199018c5995bSMatthew G. Knepley     ierr = DMLabelSetValue(label, point, dim-1);CHKERRQ(ierr);
1991e1757548SMatthew G. Knepley     /* Label its closure:
1992e1757548SMatthew G. Knepley       unmarked: label as unsplit
1993e1757548SMatthew G. Knepley       incident: relabel as split
1994e1757548SMatthew G. Knepley       split:    do nothing
1995e1757548SMatthew G. Knepley     */
199618c5995bSMatthew G. Knepley     {
199718c5995bSMatthew G. Knepley       PetscInt *closure = NULL;
199818c5995bSMatthew G. Knepley       PetscInt  closureSize, cl;
199918c5995bSMatthew G. Knepley 
200018c5995bSMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
200118c5995bSMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
200218c5995bSMatthew G. Knepley         ierr = DMLabelGetValue(label, closure[cl], &valA);CHKERRQ(ierr);
20038771c1d3SMatthew G. Knepley         if (valA == -1) { /* Mark as unsplit */
200418c5995bSMatthew G. Knepley           ierr = DMLabelGetValue(depthLabel, closure[cl], &dep);CHKERRQ(ierr);
200518c5995bSMatthew G. Knepley           ierr = DMLabelSetValue(label, closure[cl], shift2+dep);CHKERRQ(ierr);
20068771c1d3SMatthew G. Knepley         } else if (((valA >= shift) && (valA < shift2)) || ((valA <= -shift) && (valA > -shift2))) {
2007e1757548SMatthew G. Knepley           ierr = DMLabelGetValue(depthLabel, closure[cl], &dep);CHKERRQ(ierr);
2008e1757548SMatthew G. Knepley           ierr = DMLabelClearValue(label, closure[cl], valA);CHKERRQ(ierr);
2009e1757548SMatthew G. Knepley           ierr = DMLabelSetValue(label, closure[cl], dep);CHKERRQ(ierr);
2010e1757548SMatthew G. Knepley         }
201118c5995bSMatthew G. Knepley       }
201218c5995bSMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
201318c5995bSMatthew G. Knepley     }
201418c5995bSMatthew G. Knepley   }
201518c5995bSMatthew G. Knepley   ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
201618c5995bSMatthew G. Knepley   ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
2017cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
2018cd0c2139SMatthew G Knepley }
2019cd0c2139SMatthew G Knepley 
2020720e594eSMatthew G. Knepley /* Check that no cell have all vertices on the fault */
2021720e594eSMatthew G. Knepley PetscErrorCode DMPlexCheckValidSubmesh_Private(DM dm, DMLabel label, DM subdm)
2022720e594eSMatthew G. Knepley {
2023720e594eSMatthew G. Knepley   IS              subpointIS;
2024720e594eSMatthew G. Knepley   const PetscInt *dmpoints;
2025720e594eSMatthew G. Knepley   PetscInt        defaultValue, cStart, cEnd, c, vStart, vEnd;
2026720e594eSMatthew G. Knepley   PetscErrorCode  ierr;
2027720e594eSMatthew G. Knepley 
2028720e594eSMatthew G. Knepley   PetscFunctionBegin;
2029720e594eSMatthew G. Knepley   if (!label) PetscFunctionReturn(0);
2030720e594eSMatthew G. Knepley   ierr = DMLabelGetDefaultValue(label, &defaultValue);CHKERRQ(ierr);
2031720e594eSMatthew G. Knepley   ierr = DMPlexCreateSubpointIS(subdm, &subpointIS);CHKERRQ(ierr);
2032720e594eSMatthew G. Knepley   if (!subpointIS) PetscFunctionReturn(0);
2033720e594eSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(subdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2034720e594eSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
2035720e594eSMatthew G. Knepley   ierr = ISGetIndices(subpointIS, &dmpoints);CHKERRQ(ierr);
2036720e594eSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2037720e594eSMatthew G. Knepley     PetscBool invalidCell = PETSC_TRUE;
2038720e594eSMatthew G. Knepley     PetscInt *closure     = NULL;
2039720e594eSMatthew G. Knepley     PetscInt  closureSize, cl;
2040720e594eSMatthew G. Knepley 
2041720e594eSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2042720e594eSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
2043720e594eSMatthew G. Knepley       PetscInt value = 0;
2044720e594eSMatthew G. Knepley 
2045720e594eSMatthew G. Knepley       if ((closure[cl] < vStart) || (closure[cl] >= vEnd)) continue;
2046720e594eSMatthew G. Knepley       ierr = DMLabelGetValue(label, closure[cl], &value);CHKERRQ(ierr);
2047720e594eSMatthew G. Knepley       if (value == defaultValue) {invalidCell = PETSC_FALSE; break;}
2048720e594eSMatthew G. Knepley     }
2049720e594eSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2050720e594eSMatthew G. Knepley     if (invalidCell) {
2051720e594eSMatthew G. Knepley       ierr = ISRestoreIndices(subpointIS, &dmpoints);CHKERRQ(ierr);
2052720e594eSMatthew G. Knepley       ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
2053720e594eSMatthew G. Knepley       ierr = DMDestroy(&subdm);CHKERRQ(ierr);
2054720e594eSMatthew G. Knepley       SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ambiguous submesh. Cell %D has all of its vertices on the submesh.", dmpoints[c]);
2055720e594eSMatthew G. Knepley     }
2056720e594eSMatthew G. Knepley   }
2057720e594eSMatthew G. Knepley   ierr = ISRestoreIndices(subpointIS, &dmpoints);CHKERRQ(ierr);
2058720e594eSMatthew G. Knepley   ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
2059720e594eSMatthew G. Knepley   PetscFunctionReturn(0);
2060720e594eSMatthew G. Knepley }
2061720e594eSMatthew G. Knepley 
2062720e594eSMatthew G. Knepley 
2063c08575a3SMatthew G. Knepley /*@
20643cf72582SMatthew G. Knepley   DMPlexCreateHybridMesh - Create a mesh with hybrid cells along an internal interface
20653cf72582SMatthew G. Knepley 
20663cf72582SMatthew G. Knepley   Collective on dm
20673cf72582SMatthew G. Knepley 
20683cf72582SMatthew G. Knepley   Input Parameters:
20693cf72582SMatthew G. Knepley + dm - The original DM
2070720e594eSMatthew G. Knepley . label - The label specifying the interface vertices
2071720e594eSMatthew G. Knepley - bdlabel - The optional label specifying the interface boundary vertices
20723cf72582SMatthew G. Knepley 
20733cf72582SMatthew G. Knepley   Output Parameters:
20747db7e0a7SMatthew G. Knepley + hybridLabel - The label fully marking the interface, or NULL if no output is desired
20757db7e0a7SMatthew G. Knepley . splitLabel - The label containing the split points, or NULL if no output is desired
2076720e594eSMatthew G. Knepley . dmInterface - The new interface DM, or NULL
2077720e594eSMatthew G. Knepley - dmHybrid - The new DM with cohesive cells
20783cf72582SMatthew G. Knepley 
20796eccb800SMatthew Knepley   Note: The hybridLabel indicates what parts of the original mesh impinged on the on division surface. For points
20806eccb800SMatthew Knepley   directly on the division surface, they are labeled with their dimension, so an edge 7 on the division surface would be
20816eccb800SMatthew Knepley   7 (1) in hybridLabel. For points that impinge from the positive side, they are labeled with 100+dim, so an edge 6 with
20826eccb800SMatthew 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
20836eccb800SMatthew Knepley   surface also hits vertex 3, it would be 9 (-101) in hybridLabel.
20846eccb800SMatthew Knepley 
20856eccb800SMatthew Knepley   The splitLabel indicates what points in the new hybrid mesh were the result of splitting points in the original
20866eccb800SMatthew Knepley   mesh. The label value is +=100+dim for each point. For example, if two edges 10 and 14 in the hybrid resulting from
20876eccb800SMatthew Knepley   splitting an edge in the original mesh, you would have 10 (101) and 14 (-101) in the splitLabel.
20886eccb800SMatthew Knepley 
20896eccb800SMatthew Knepley   The dmInterface is a DM built from the original division surface. It has a label which can be retrieved using
20906eccb800SMatthew Knepley   DMPlexGetSubpointMap() which maps each point back to the point in the surface of the original mesh.
20916eccb800SMatthew Knepley 
20923cf72582SMatthew G. Knepley   Level: developer
20933cf72582SMatthew G. Knepley 
20946eccb800SMatthew Knepley .seealso: DMPlexConstructCohesiveCells(), DMPlexLabelCohesiveComplete(), DMPlexGetSubpointMap(), DMCreate()
20953cf72582SMatthew G. Knepley @*/
20967db7e0a7SMatthew G. Knepley PetscErrorCode DMPlexCreateHybridMesh(DM dm, DMLabel label, DMLabel bdlabel, DMLabel *hybridLabel, DMLabel *splitLabel, DM *dmInterface, DM *dmHybrid)
20973cf72582SMatthew G. Knepley {
20983cf72582SMatthew G. Knepley   DM             idm;
20997db7e0a7SMatthew G. Knepley   DMLabel        subpointMap, hlabel, slabel = NULL;
21003cf72582SMatthew G. Knepley   PetscInt       dim;
21013cf72582SMatthew G. Knepley   PetscErrorCode ierr;
21023cf72582SMatthew G. Knepley 
21033cf72582SMatthew G. Knepley   PetscFunctionBegin;
21043cf72582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2105720e594eSMatthew G. Knepley   if (bdlabel) PetscValidPointer(bdlabel, 3);
2106720e594eSMatthew G. Knepley   if (hybridLabel) PetscValidPointer(hybridLabel, 4);
21077db7e0a7SMatthew G. Knepley   if (splitLabel)  PetscValidPointer(splitLabel, 5);
21087db7e0a7SMatthew G. Knepley   if (dmInterface) PetscValidPointer(dmInterface, 6);
21097db7e0a7SMatthew G. Knepley   PetscValidPointer(dmHybrid, 7);
2110c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2111158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmesh(dm, label, 1, PETSC_FALSE, &idm);CHKERRQ(ierr);
2112720e594eSMatthew G. Knepley   ierr = DMPlexCheckValidSubmesh_Private(dm, label, idm);CHKERRQ(ierr);
21133cf72582SMatthew G. Knepley   ierr = DMPlexOrient(idm);CHKERRQ(ierr);
21143cf72582SMatthew G. Knepley   ierr = DMPlexGetSubpointMap(idm, &subpointMap);CHKERRQ(ierr);
21153cf72582SMatthew G. Knepley   ierr = DMLabelDuplicate(subpointMap, &hlabel);CHKERRQ(ierr);
21163cf72582SMatthew G. Knepley   ierr = DMLabelClearStratum(hlabel, dim);CHKERRQ(ierr);
21177db7e0a7SMatthew G. Knepley   if (splitLabel) {
21187db7e0a7SMatthew G. Knepley     const char *name;
21197db7e0a7SMatthew G. Knepley     char        sname[PETSC_MAX_PATH_LEN];
21207db7e0a7SMatthew G. Knepley 
2121d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) hlabel, &name);CHKERRQ(ierr);
21227db7e0a7SMatthew G. Knepley     ierr = PetscStrncpy(sname, name, PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
21237db7e0a7SMatthew G. Knepley     ierr = PetscStrcat(sname, " split");CHKERRQ(ierr);
2124d67d17b1SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, sname, &slabel);CHKERRQ(ierr);
21257db7e0a7SMatthew G. Knepley   }
2126720e594eSMatthew G. Knepley   ierr = DMPlexLabelCohesiveComplete(dm, hlabel, bdlabel, PETSC_FALSE, idm);CHKERRQ(ierr);
2127720e594eSMatthew G. Knepley   if (dmInterface) {*dmInterface = idm;}
2128720e594eSMatthew G. Knepley   else             {ierr = DMDestroy(&idm);CHKERRQ(ierr);}
21297db7e0a7SMatthew G. Knepley   ierr = DMPlexConstructCohesiveCells(dm, hlabel, slabel, dmHybrid);CHKERRQ(ierr);
21303cf72582SMatthew G. Knepley   if (hybridLabel) *hybridLabel = hlabel;
213153aa2e23SMatthew G. Knepley   else             {ierr = DMLabelDestroy(&hlabel);CHKERRQ(ierr);}
21327db7e0a7SMatthew G. Knepley   if (splitLabel)  *splitLabel  = slabel;
2133cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
2134cd0c2139SMatthew G Knepley }
2135cd0c2139SMatthew G Knepley 
2136efa14ee0SMatthew G Knepley /* Here we need the explicit assumption that:
2137efa14ee0SMatthew G Knepley 
2138efa14ee0SMatthew G Knepley      For any marked cell, the marked vertices constitute a single face
2139efa14ee0SMatthew G Knepley */
2140830e53efSMatthew G. Knepley static PetscErrorCode DMPlexMarkSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, DM subdm)
2141efa14ee0SMatthew G Knepley {
2142fed694aaSMatthew G. Knepley   IS               subvertexIS = NULL;
2143efa14ee0SMatthew G Knepley   const PetscInt  *subvertices;
2144*412e9a14SMatthew G. Knepley   PetscInt        *pStart, *pEnd, pSize;
2145efa14ee0SMatthew G Knepley   PetscInt         depth, dim, d, numSubVerticesInitial = 0, v;
2146efa14ee0SMatthew G Knepley   PetscErrorCode   ierr;
2147efa14ee0SMatthew G Knepley 
2148efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2149efa14ee0SMatthew G Knepley   *numFaces = 0;
2150efa14ee0SMatthew G Knepley   *nFV      = 0;
2151efa14ee0SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2152c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
215377d178adSMatthew G. Knepley   pSize = PetscMax(depth, dim) + 1;
2154*412e9a14SMatthew G. Knepley   ierr = PetscMalloc2(pSize, &pStart, pSize, &pEnd);CHKERRQ(ierr);
2155efa14ee0SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
2156*412e9a14SMatthew G. Knepley     ierr = DMPlexGetSimplexOrBoxCells(dm, depth-d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);
2157efa14ee0SMatthew G Knepley   }
2158efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
2159fed694aaSMatthew G. Knepley   if (vertexLabel) {ierr = DMLabelGetStratumIS(vertexLabel, value, &subvertexIS);CHKERRQ(ierr);}
2160efa14ee0SMatthew G Knepley   if (subvertexIS) {
2161efa14ee0SMatthew G Knepley     ierr = ISGetSize(subvertexIS, &numSubVerticesInitial);CHKERRQ(ierr);
2162efa14ee0SMatthew G Knepley     ierr = ISGetIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
2163efa14ee0SMatthew G Knepley   }
2164efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2165efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
21660298fd71SBarry Smith     PetscInt      *star   = NULL;
2167efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numCells = 0, c;
2168efa14ee0SMatthew G Knepley 
2169efa14ee0SMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2170efa14ee0SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
2171efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2172efa14ee0SMatthew G Knepley       if ((point >= pStart[depth]) && (point < pEnd[depth])) star[numCells++] = point;
2173efa14ee0SMatthew G Knepley     }
2174efa14ee0SMatthew G Knepley     for (c = 0; c < numCells; ++c) {
2175efa14ee0SMatthew G Knepley       const PetscInt cell    = star[c];
21760298fd71SBarry Smith       PetscInt      *closure = NULL;
2177efa14ee0SMatthew G Knepley       PetscInt       closureSize, cl;
2178efa14ee0SMatthew G Knepley       PetscInt       cellLoc, numCorners = 0, faceSize = 0;
2179efa14ee0SMatthew G Knepley 
2180efa14ee0SMatthew G Knepley       ierr = DMLabelGetValue(subpointMap, cell, &cellLoc);CHKERRQ(ierr);
218165560c7fSMatthew G Knepley       if (cellLoc == 2) continue;
218282f516ccSBarry Smith       if (cellLoc >= 0) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %d has dimension %d in the surface label", cell, cellLoc);
2183efa14ee0SMatthew G Knepley       ierr = DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2184efa14ee0SMatthew G Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
2185efa14ee0SMatthew G Knepley         const PetscInt point = closure[cl];
2186efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2187efa14ee0SMatthew G Knepley 
2188efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
2189efa14ee0SMatthew G Knepley           ++numCorners;
2190efa14ee0SMatthew G Knepley           ierr = DMLabelGetValue(vertexLabel, point, &vertexLoc);CHKERRQ(ierr);
2191830e53efSMatthew G. Knepley           if (vertexLoc == value) closure[faceSize++] = point;
2192efa14ee0SMatthew G Knepley         }
2193efa14ee0SMatthew G Knepley       }
219418ad9376SMatthew G. Knepley       if (!(*nFV)) {ierr = DMPlexGetNumFaceVertices(dm, dim, numCorners, nFV);CHKERRQ(ierr);}
219582f516ccSBarry Smith       if (faceSize > *nFV) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %d of an element on the surface", faceSize);
2196efa14ee0SMatthew G Knepley       if (faceSize == *nFV) {
2197007baee2SMatthew G. Knepley         const PetscInt *cells = NULL;
2198007baee2SMatthew G. Knepley         PetscInt        numCells, nc;
2199007baee2SMatthew G. Knepley 
2200efa14ee0SMatthew G Knepley         ++(*numFaces);
2201efa14ee0SMatthew G Knepley         for (cl = 0; cl < faceSize; ++cl) {
2202efa14ee0SMatthew G Knepley           ierr = DMLabelSetValue(subpointMap, closure[cl], 0);CHKERRQ(ierr);
2203efa14ee0SMatthew G Knepley         }
2204007baee2SMatthew G. Knepley         ierr = DMPlexGetJoin(dm, faceSize, closure, &numCells, &cells);CHKERRQ(ierr);
2205007baee2SMatthew G. Knepley         for (nc = 0; nc < numCells; ++nc) {
2206007baee2SMatthew G. Knepley           ierr = DMLabelSetValue(subpointMap, cells[nc], 2);CHKERRQ(ierr);
2207007baee2SMatthew G. Knepley         }
2208007baee2SMatthew G. Knepley         ierr = DMPlexRestoreJoin(dm, faceSize, closure, &numCells, &cells);CHKERRQ(ierr);
2209efa14ee0SMatthew G Knepley       }
2210efa14ee0SMatthew G Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2211efa14ee0SMatthew G Knepley     }
2212efa14ee0SMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2213efa14ee0SMatthew G Knepley   }
2214efa14ee0SMatthew G Knepley   if (subvertexIS) {
2215efa14ee0SMatthew G Knepley     ierr = ISRestoreIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
2216efa14ee0SMatthew G Knepley   }
2217efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
2218*412e9a14SMatthew G. Knepley   ierr = PetscFree2(pStart, pEnd);CHKERRQ(ierr);
2219efa14ee0SMatthew G Knepley   PetscFunctionReturn(0);
2220efa14ee0SMatthew G Knepley }
2221efa14ee0SMatthew G Knepley 
2222158acfadSMatthew G. Knepley static PetscErrorCode DMPlexMarkSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DMLabel subpointMap, DM subdm)
2223efa14ee0SMatthew G Knepley {
222434b4c39eSMatthew G. Knepley   IS               subvertexIS = NULL;
2225efa14ee0SMatthew G Knepley   const PetscInt  *subvertices;
2226*412e9a14SMatthew G. Knepley   PetscInt        *pStart, *pEnd;
2227efa14ee0SMatthew G Knepley   PetscInt         dim, d, numSubVerticesInitial = 0, v;
2228efa14ee0SMatthew G Knepley   PetscErrorCode   ierr;
2229efa14ee0SMatthew G Knepley 
2230efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2231c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2232*412e9a14SMatthew G. Knepley   ierr = PetscMalloc2(dim+1, &pStart, dim+1, &pEnd);CHKERRQ(ierr);
2233efa14ee0SMatthew G Knepley   for (d = 0; d <= dim; ++d) {
2234*412e9a14SMatthew G. Knepley     ierr = DMPlexGetSimplexOrBoxCells(dm, dim-d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);
2235efa14ee0SMatthew G Knepley   }
2236efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
223734b4c39eSMatthew G. Knepley   if (vertexLabel) {
2238830e53efSMatthew G. Knepley     ierr = DMLabelGetStratumIS(vertexLabel, value, &subvertexIS);CHKERRQ(ierr);
2239efa14ee0SMatthew G Knepley     if (subvertexIS) {
2240efa14ee0SMatthew G Knepley       ierr = ISGetSize(subvertexIS, &numSubVerticesInitial);CHKERRQ(ierr);
2241efa14ee0SMatthew G Knepley       ierr = ISGetIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
2242efa14ee0SMatthew G Knepley     }
224334b4c39eSMatthew G. Knepley   }
2244efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2245efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
22460298fd71SBarry Smith     PetscInt      *star   = NULL;
2247efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numFaces = 0, f;
2248efa14ee0SMatthew G Knepley 
2249efa14ee0SMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2250efa14ee0SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
2251efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2252158acfadSMatthew G. Knepley       PetscInt       faceLoc;
2253158acfadSMatthew G. Knepley 
2254158acfadSMatthew G. Knepley       if ((point >= pStart[dim-1]) && (point < pEnd[dim-1])) {
2255158acfadSMatthew G. Knepley         if (markedFaces) {
2256158acfadSMatthew G. Knepley           ierr = DMLabelGetValue(vertexLabel, point, &faceLoc);CHKERRQ(ierr);
2257158acfadSMatthew G. Knepley           if (faceLoc < 0) continue;
2258158acfadSMatthew G. Knepley         }
2259158acfadSMatthew G. Knepley         star[numFaces++] = point;
2260158acfadSMatthew G. Knepley       }
2261efa14ee0SMatthew G Knepley     }
2262efa14ee0SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
2263efa14ee0SMatthew G Knepley       const PetscInt face    = star[f];
22640298fd71SBarry Smith       PetscInt      *closure = NULL;
2265efa14ee0SMatthew G Knepley       PetscInt       closureSize, c;
2266efa14ee0SMatthew G Knepley       PetscInt       faceLoc;
2267efa14ee0SMatthew G Knepley 
2268efa14ee0SMatthew G Knepley       ierr = DMLabelGetValue(subpointMap, face, &faceLoc);CHKERRQ(ierr);
2269efa14ee0SMatthew G Knepley       if (faceLoc == dim-1) continue;
227082f516ccSBarry Smith       if (faceLoc >= 0) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %d has dimension %d in the surface label", face, faceLoc);
2271efa14ee0SMatthew G Knepley       ierr = DMPlexGetTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2272efa14ee0SMatthew G Knepley       for (c = 0; c < closureSize*2; c += 2) {
2273efa14ee0SMatthew G Knepley         const PetscInt point = closure[c];
2274efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2275efa14ee0SMatthew G Knepley 
2276efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
2277efa14ee0SMatthew G Knepley           ierr = DMLabelGetValue(vertexLabel, point, &vertexLoc);CHKERRQ(ierr);
2278830e53efSMatthew G. Knepley           if (vertexLoc != value) break;
2279efa14ee0SMatthew G Knepley         }
2280efa14ee0SMatthew G Knepley       }
2281efa14ee0SMatthew G Knepley       if (c == closureSize*2) {
2282efa14ee0SMatthew G Knepley         const PetscInt *support;
2283efa14ee0SMatthew G Knepley         PetscInt        supportSize, s;
2284efa14ee0SMatthew G Knepley 
2285efa14ee0SMatthew G Knepley         for (c = 0; c < closureSize*2; c += 2) {
2286efa14ee0SMatthew G Knepley           const PetscInt point = closure[c];
2287efa14ee0SMatthew G Knepley 
2288efa14ee0SMatthew G Knepley           for (d = 0; d < dim; ++d) {
2289efa14ee0SMatthew G Knepley             if ((point >= pStart[d]) && (point < pEnd[d])) {
2290efa14ee0SMatthew G Knepley               ierr = DMLabelSetValue(subpointMap, point, d);CHKERRQ(ierr);
2291efa14ee0SMatthew G Knepley               break;
2292efa14ee0SMatthew G Knepley             }
2293efa14ee0SMatthew G Knepley           }
2294efa14ee0SMatthew G Knepley         }
2295efa14ee0SMatthew G Knepley         ierr = DMPlexGetSupportSize(dm, face, &supportSize);CHKERRQ(ierr);
2296efa14ee0SMatthew G Knepley         ierr = DMPlexGetSupport(dm, face, &support);CHKERRQ(ierr);
2297efa14ee0SMatthew G Knepley         for (s = 0; s < supportSize; ++s) {
2298efa14ee0SMatthew G Knepley           ierr = DMLabelSetValue(subpointMap, support[s], dim);CHKERRQ(ierr);
2299efa14ee0SMatthew G Knepley         }
2300efa14ee0SMatthew G Knepley       }
2301efa14ee0SMatthew G Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2302efa14ee0SMatthew G Knepley     }
2303efa14ee0SMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2304efa14ee0SMatthew G Knepley   }
230534b4c39eSMatthew G. Knepley   if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subvertices);CHKERRQ(ierr);}
2306efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
2307*412e9a14SMatthew G. Knepley   ierr = PetscFree2(pStart, pEnd);CHKERRQ(ierr);
2308efa14ee0SMatthew G Knepley   PetscFunctionReturn(0);
2309efa14ee0SMatthew G Knepley }
2310efa14ee0SMatthew G Knepley 
231127c04023SMatthew 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)
2312766ab985SMatthew G. Knepley {
231327c04023SMatthew G. Knepley   DMLabel         label = NULL;
2314766ab985SMatthew G. Knepley   const PetscInt *cone;
23159fc93327SToby Isaac   PetscInt        dim, cMax, cEnd, c, subc = 0, p, coneSize = -1;
2316766ab985SMatthew G. Knepley   PetscErrorCode  ierr;
2317766ab985SMatthew G. Knepley 
2318812bfc34SJed Brown   PetscFunctionBegin;
2319c0ed958bSJed Brown   *numFaces = 0;
2320c0ed958bSJed Brown   *nFV = 0;
2321c58f1c22SToby Isaac   if (labelname) {ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);}
2322fed694aaSMatthew G. Knepley   *subCells = NULL;
2323c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2324*412e9a14SMatthew G. Knepley   ierr = DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd);CHKERRQ(ierr);
2325766ab985SMatthew G. Knepley   if (cMax < 0) PetscFunctionReturn(0);
232627c04023SMatthew G. Knepley   if (label) {
232727c04023SMatthew G. Knepley     for (c = cMax; c < cEnd; ++c) {
232827c04023SMatthew G. Knepley       PetscInt val;
232927c04023SMatthew G. Knepley 
233027c04023SMatthew G. Knepley       ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr);
233127c04023SMatthew G. Knepley       if (val == value) {
233227c04023SMatthew G. Knepley         ++(*numFaces);
233327c04023SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
233427c04023SMatthew G. Knepley       }
233527c04023SMatthew G. Knepley     }
233627c04023SMatthew G. Knepley   } else {
2337766ab985SMatthew G. Knepley     *numFaces = cEnd - cMax;
233827c04023SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cMax, &coneSize);CHKERRQ(ierr);
233927c04023SMatthew G. Knepley   }
2340785e854fSJed Brown   ierr = PetscMalloc1(*numFaces *2, subCells);CHKERRQ(ierr);
23419fc93327SToby Isaac   if (!(*numFaces)) PetscFunctionReturn(0);
23429fc93327SToby Isaac   *nFV = hasLagrange ? coneSize/3 : coneSize/2;
2343766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2344766ab985SMatthew G. Knepley     const PetscInt *cells;
2345766ab985SMatthew G. Knepley     PetscInt        numCells;
2346766ab985SMatthew G. Knepley 
234727c04023SMatthew G. Knepley     if (label) {
234827c04023SMatthew G. Knepley       PetscInt val;
234927c04023SMatthew G. Knepley 
235027c04023SMatthew G. Knepley       ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr);
235127c04023SMatthew G. Knepley       if (val != value) continue;
235227c04023SMatthew G. Knepley     }
2353766ab985SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
2354766ab985SMatthew G. Knepley     for (p = 0; p < *nFV; ++p) {
2355766ab985SMatthew G. Knepley       ierr = DMLabelSetValue(subpointMap, cone[p], 0);CHKERRQ(ierr);
2356766ab985SMatthew G. Knepley     }
2357766ab985SMatthew G. Knepley     /* Negative face */
2358766ab985SMatthew G. Knepley     ierr = DMPlexGetJoin(dm, *nFV, cone, &numCells, &cells);CHKERRQ(ierr);
235927234c99SMatthew G. Knepley     /* Not true in parallel
236027234c99SMatthew G. Knepley     if (numCells != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
2361766ab985SMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
2362766ab985SMatthew G. Knepley       ierr = DMLabelSetValue(subpointMap, cells[p], 2);CHKERRQ(ierr);
236327234c99SMatthew G. Knepley       (*subCells)[subc++] = cells[p];
2364766ab985SMatthew G. Knepley     }
2365766ab985SMatthew G. Knepley     ierr = DMPlexRestoreJoin(dm, *nFV, cone, &numCells, &cells);CHKERRQ(ierr);
2366766ab985SMatthew G. Knepley     /* Positive face is not included */
2367766ab985SMatthew G. Knepley   }
2368766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
2369766ab985SMatthew G. Knepley }
2370766ab985SMatthew G. Knepley 
23713982b651SMatthew G. Knepley static PetscErrorCode DMPlexMarkCohesiveSubmesh_Interpolated(DM dm, DMLabel label, PetscInt value, DMLabel subpointMap, DM subdm)
2372766ab985SMatthew G. Knepley {
2373766ab985SMatthew G. Knepley   PetscInt      *pStart, *pEnd;
2374766ab985SMatthew G. Knepley   PetscInt       dim, cMax, cEnd, c, d;
2375766ab985SMatthew G. Knepley   PetscErrorCode ierr;
2376766ab985SMatthew G. Knepley 
2377812bfc34SJed Brown   PetscFunctionBegin;
2378c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2379*412e9a14SMatthew G. Knepley   ierr = DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd);CHKERRQ(ierr);
2380766ab985SMatthew G. Knepley   if (cMax < 0) PetscFunctionReturn(0);
2381dcca6d9dSJed Brown   ierr = PetscMalloc2(dim+1,&pStart,dim+1,&pEnd);CHKERRQ(ierr);
2382b3154360SMatthew G. Knepley   for (d = 0; d <= dim; ++d) {ierr = DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);}
2383766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2384766ab985SMatthew G. Knepley     const PetscInt *cone;
2385766ab985SMatthew G. Knepley     PetscInt       *closure = NULL;
2386b3154360SMatthew G. Knepley     PetscInt        fconeSize, coneSize, closureSize, cl, val;
2387766ab985SMatthew G. Knepley 
238827c04023SMatthew G. Knepley     if (label) {
238927c04023SMatthew G. Knepley       ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr);
239027c04023SMatthew G. Knepley       if (val != value) continue;
239127c04023SMatthew G. Knepley     }
2392766ab985SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
2393766ab985SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
2394b3154360SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cone[0], &fconeSize);CHKERRQ(ierr);
2395b3154360SMatthew G. Knepley     if (coneSize != (fconeSize ? fconeSize : 1) + 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells");
2396b3154360SMatthew G. Knepley     /* Negative face */
2397766ab985SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2398766ab985SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
2399766ab985SMatthew G. Knepley       const PetscInt point = closure[cl];
2400766ab985SMatthew G. Knepley 
2401766ab985SMatthew G. Knepley       for (d = 0; d <= dim; ++d) {
2402766ab985SMatthew G. Knepley         if ((point >= pStart[d]) && (point < pEnd[d])) {
2403766ab985SMatthew G. Knepley           ierr = DMLabelSetValue(subpointMap, point, d);CHKERRQ(ierr);
2404766ab985SMatthew G. Knepley           break;
2405766ab985SMatthew G. Knepley         }
2406766ab985SMatthew G. Knepley       }
2407766ab985SMatthew G. Knepley     }
2408766ab985SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2409766ab985SMatthew G. Knepley     /* Cells -- positive face is not included */
2410766ab985SMatthew G. Knepley     for (cl = 0; cl < 1; ++cl) {
2411766ab985SMatthew G. Knepley       const PetscInt *support;
2412766ab985SMatthew G. Knepley       PetscInt        supportSize, s;
2413766ab985SMatthew G. Knepley 
2414766ab985SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[cl], &supportSize);CHKERRQ(ierr);
2415711de394SMatthew G. Knepley       /* if (supportSize != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive faces should separate two cells"); */
2416766ab985SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[cl], &support);CHKERRQ(ierr);
2417766ab985SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
2418766ab985SMatthew G. Knepley         ierr = DMLabelSetValue(subpointMap, support[s], dim);CHKERRQ(ierr);
2419766ab985SMatthew G. Knepley       }
2420766ab985SMatthew G. Knepley     }
2421766ab985SMatthew G. Knepley   }
2422766ab985SMatthew G. Knepley   ierr = PetscFree2(pStart, pEnd);CHKERRQ(ierr);
2423766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
2424766ab985SMatthew G. Knepley }
2425766ab985SMatthew G. Knepley 
2426c08575a3SMatthew G. Knepley static PetscErrorCode DMPlexGetFaceOrientation(DM dm, PetscInt cell, PetscInt numCorners, PetscInt indices[], PetscInt oppositeVertex, PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2427e6ccafaeSMatthew G Knepley {
242882f516ccSBarry Smith   MPI_Comm       comm;
2429e6ccafaeSMatthew G Knepley   PetscBool      posOrient = PETSC_FALSE;
2430e6ccafaeSMatthew G Knepley   const PetscInt debug     = 0;
2431e6ccafaeSMatthew G Knepley   PetscInt       cellDim, faceSize, f;
2432e6ccafaeSMatthew G Knepley   PetscErrorCode ierr;
2433e6ccafaeSMatthew G Knepley 
243482f516ccSBarry Smith   PetscFunctionBegin;
243582f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2436c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &cellDim);CHKERRQ(ierr);
2437367003a6SStefano Zampini   if (debug) {ierr = PetscPrintf(comm, "cellDim: %d numCorners: %d\n", cellDim, numCorners);CHKERRQ(ierr);}
2438e6ccafaeSMatthew G Knepley 
2439ddeab2a6SMatthew G. Knepley   if (cellDim == 1 && numCorners == 2) {
2440ddeab2a6SMatthew G. Knepley     /* Triangle */
2441e6ccafaeSMatthew G Knepley     faceSize  = numCorners-1;
2442e6ccafaeSMatthew G Knepley     posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2443ddeab2a6SMatthew G. Knepley   } else if (cellDim == 2 && numCorners == 3) {
2444ddeab2a6SMatthew G. Knepley     /* Triangle */
2445ddeab2a6SMatthew G. Knepley     faceSize  = numCorners-1;
2446ddeab2a6SMatthew G. Knepley     posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2447ddeab2a6SMatthew G. Knepley   } else if (cellDim == 3 && numCorners == 4) {
2448ddeab2a6SMatthew G. Knepley     /* Tetrahedron */
2449ddeab2a6SMatthew G. Knepley     faceSize  = numCorners-1;
2450ddeab2a6SMatthew G. Knepley     posOrient = (oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2451e6ccafaeSMatthew G Knepley   } else if (cellDim == 1 && numCorners == 3) {
2452e6ccafaeSMatthew G Knepley     /* Quadratic line */
2453e6ccafaeSMatthew G Knepley     faceSize  = 1;
2454e6ccafaeSMatthew G Knepley     posOrient = PETSC_TRUE;
2455e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 4) {
2456e6ccafaeSMatthew G Knepley     /* Quads */
2457e6ccafaeSMatthew G Knepley     faceSize = 2;
2458e6ccafaeSMatthew G Knepley     if ((indices[1] > indices[0]) && (indices[1] - indices[0] == 1)) {
2459e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2460e6ccafaeSMatthew G Knepley     } else if ((indices[0] == 3) && (indices[1] == 0)) {
2461e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2462e6ccafaeSMatthew G Knepley     } else {
2463e6ccafaeSMatthew G Knepley       if (((indices[0] > indices[1]) && (indices[0] - indices[1] == 1)) || ((indices[0] == 0) && (indices[1] == 3))) {
2464e6ccafaeSMatthew G Knepley         posOrient = PETSC_FALSE;
2465e6ccafaeSMatthew G Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossedge");
2466e6ccafaeSMatthew G Knepley     }
2467e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 6) {
2468e6ccafaeSMatthew G Knepley     /* Quadratic triangle (I hate this) */
2469e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2470e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTri = 3;
2471e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2472e6ccafaeSMatthew G Knepley     PetscBool      found                    = PETSC_FALSE;
2473e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTriSorted[9] = {
2474e6ccafaeSMatthew G Knepley       0, 3,  4, /* bottom */
2475e6ccafaeSMatthew G Knepley       1, 4,  5, /* right */
2476e6ccafaeSMatthew G Knepley       2, 3,  5, /* left */
2477e6ccafaeSMatthew G Knepley     };
2478e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTri[9] = {
2479e6ccafaeSMatthew G Knepley       0, 3,  4, /* bottom */
2480e6ccafaeSMatthew G Knepley       1, 4,  5, /* right */
2481e6ccafaeSMatthew G Knepley       2, 5,  3, /* left */
2482e6ccafaeSMatthew G Knepley     };
2483e6ccafaeSMatthew G Knepley 
2484e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTri; ++i) sortedIndices[i] = indices[i];
2485e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeTri, sortedIndices);CHKERRQ(ierr);
2486e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 3; ++iFace) {
2487e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeTri;
2488e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2489e6ccafaeSMatthew G Knepley 
2490e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesTriSorted[ii+0]) &&
2491e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesTriSorted[ii+1])) {
2492e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTri; ++fVertex) {
2493e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTri; ++cVertex) {
2494e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTri[ii+fVertex]) {
2495e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2496e6ccafaeSMatthew G Knepley               break;
2497e6ccafaeSMatthew G Knepley             }
2498e6ccafaeSMatthew G Knepley           }
2499e6ccafaeSMatthew G Knepley         }
2500e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2501e6ccafaeSMatthew G Knepley         break;
2502e6ccafaeSMatthew G Knepley       }
2503e6ccafaeSMatthew G Knepley     }
2504e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid tri crossface");
2505e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2506e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2507e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 9) {
2508e6ccafaeSMatthew G Knepley     /* Quadratic quad (I hate this) */
2509e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2510e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuad = 3;
2511e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2512e6ccafaeSMatthew G Knepley     PetscBool      found                      = PETSC_FALSE;
2513e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadSorted[12] = {
2514e6ccafaeSMatthew G Knepley       0, 1,  4, /* bottom */
2515e6ccafaeSMatthew G Knepley       1, 2,  5, /* right */
2516e6ccafaeSMatthew G Knepley       2, 3,  6, /* top */
2517e6ccafaeSMatthew G Knepley       0, 3,  7, /* left */
2518e6ccafaeSMatthew G Knepley     };
2519e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuad[12] = {
2520e6ccafaeSMatthew G Knepley       0, 1,  4, /* bottom */
2521e6ccafaeSMatthew G Knepley       1, 2,  5, /* right */
2522e6ccafaeSMatthew G Knepley       2, 3,  6, /* top */
2523e6ccafaeSMatthew G Knepley       3, 0,  7, /* left */
2524e6ccafaeSMatthew G Knepley     };
2525e6ccafaeSMatthew G Knepley 
2526e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuad; ++i) sortedIndices[i] = indices[i];
2527e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeQuad, sortedIndices);CHKERRQ(ierr);
2528e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2529e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeQuad;
2530e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2531e6ccafaeSMatthew G Knepley 
2532e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesQuadSorted[ii+0]) &&
2533e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesQuadSorted[ii+1])) {
2534e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuad; ++fVertex) {
2535e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuad; ++cVertex) {
2536e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuad[ii+fVertex]) {
2537e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2538e6ccafaeSMatthew G Knepley               break;
2539e6ccafaeSMatthew G Knepley             }
2540e6ccafaeSMatthew G Knepley           }
2541e6ccafaeSMatthew G Knepley         }
2542e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2543e6ccafaeSMatthew G Knepley         break;
2544e6ccafaeSMatthew G Knepley       }
2545e6ccafaeSMatthew G Knepley     }
2546e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossface");
2547e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2548e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2549e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 8) {
2550e6ccafaeSMatthew G Knepley     /* Hexes
2551e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2552e6ccafaeSMatthew G Knepley        pointing up at the second.
2553e6ccafaeSMatthew G Knepley 
2554e6ccafaeSMatthew G Knepley           7---6
2555e6ccafaeSMatthew G Knepley          /|  /|
2556e6ccafaeSMatthew G Knepley         4---5 |
2557ddeab2a6SMatthew G. Knepley         | 1-|-2
2558e6ccafaeSMatthew G Knepley         |/  |/
2559ddeab2a6SMatthew G. Knepley         0---3
2560e6ccafaeSMatthew G Knepley 
2561e6ccafaeSMatthew G Knepley         Faces are determined by the first 4 vertices (corners of faces) */
2562e6ccafaeSMatthew G Knepley     const PetscInt faceSizeHex = 4;
2563e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[4], i, iFace;
2564e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2565e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHexSorted[24] = {
2566e6ccafaeSMatthew G Knepley       0, 1, 2, 3,  /* bottom */
2567e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  /* top */
2568ddeab2a6SMatthew G. Knepley       0, 3, 4, 5,  /* front */
2569ddeab2a6SMatthew G. Knepley       2, 3, 5, 6,  /* right */
2570ddeab2a6SMatthew G. Knepley       1, 2, 6, 7,  /* back */
2571ddeab2a6SMatthew G. Knepley       0, 1, 4, 7,  /* left */
2572e6ccafaeSMatthew G Knepley     };
2573e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHex[24] = {
2574ddeab2a6SMatthew G. Knepley       1, 2, 3, 0,  /* bottom */
2575e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  /* top */
2576ddeab2a6SMatthew G. Knepley       0, 3, 5, 4,  /* front */
2577ddeab2a6SMatthew G. Knepley       3, 2, 6, 5,  /* right */
2578ddeab2a6SMatthew G. Knepley       2, 1, 7, 6,  /* back */
2579ddeab2a6SMatthew G. Knepley       1, 0, 4, 7,  /* left */
2580e6ccafaeSMatthew G Knepley     };
2581e6ccafaeSMatthew G Knepley 
2582e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeHex; ++i) sortedIndices[i] = indices[i];
2583e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeHex, sortedIndices);CHKERRQ(ierr);
2584e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2585e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeHex;
2586e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2587e6ccafaeSMatthew G Knepley 
2588e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesHexSorted[ii+0]) &&
2589e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesHexSorted[ii+1]) &&
2590e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesHexSorted[ii+2]) &&
2591e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesHexSorted[ii+3])) {
2592e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeHex; ++fVertex) {
2593e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeHex; ++cVertex) {
2594e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesHex[ii+fVertex]) {
2595e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2596e6ccafaeSMatthew G Knepley               break;
2597e6ccafaeSMatthew G Knepley             }
2598e6ccafaeSMatthew G Knepley           }
2599e6ccafaeSMatthew G Knepley         }
2600e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2601e6ccafaeSMatthew G Knepley         break;
2602e6ccafaeSMatthew G Knepley       }
2603e6ccafaeSMatthew G Knepley     }
2604e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2605e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2606e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2607e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 10) {
2608e6ccafaeSMatthew G Knepley     /* Quadratic tet */
2609e6ccafaeSMatthew G Knepley     /* Faces are determined by the first 3 vertices (corners of faces) */
2610e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTet = 6;
2611e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[6], i, iFace;
2612e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2613e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTetSorted[24] = {
2614e6ccafaeSMatthew G Knepley       0, 1, 2,  6, 7, 8, /* bottom */
2615e6ccafaeSMatthew G Knepley       0, 3, 4,  6, 7, 9,  /* front */
2616e6ccafaeSMatthew G Knepley       1, 4, 5,  7, 8, 9,  /* right */
2617e6ccafaeSMatthew G Knepley       2, 3, 5,  6, 8, 9,  /* left */
2618e6ccafaeSMatthew G Knepley     };
2619e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTet[24] = {
2620e6ccafaeSMatthew G Knepley       0, 1, 2,  6, 7, 8, /* bottom */
2621e6ccafaeSMatthew G Knepley       0, 4, 3,  6, 7, 9,  /* front */
2622e6ccafaeSMatthew G Knepley       1, 5, 4,  7, 8, 9,  /* right */
2623e6ccafaeSMatthew G Knepley       2, 3, 5,  8, 6, 9,  /* left */
2624e6ccafaeSMatthew G Knepley     };
2625e6ccafaeSMatthew G Knepley 
2626e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTet; ++i) sortedIndices[i] = indices[i];
2627e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeTet, sortedIndices);CHKERRQ(ierr);
2628e6ccafaeSMatthew G Knepley     for (iFace=0; iFace < 4; ++iFace) {
2629e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeTet;
2630e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2631e6ccafaeSMatthew G Knepley 
2632e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesTetSorted[ii+0]) &&
2633e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesTetSorted[ii+1]) &&
2634e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesTetSorted[ii+2]) &&
2635e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesTetSorted[ii+3])) {
2636e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTet; ++fVertex) {
2637e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTet; ++cVertex) {
2638e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTet[ii+fVertex]) {
2639e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2640e6ccafaeSMatthew G Knepley               break;
2641e6ccafaeSMatthew G Knepley             }
2642e6ccafaeSMatthew G Knepley           }
2643e6ccafaeSMatthew G Knepley         }
2644e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2645e6ccafaeSMatthew G Knepley         break;
2646e6ccafaeSMatthew G Knepley       }
2647e6ccafaeSMatthew G Knepley     }
2648e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid tet crossface");
2649e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2650e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2651e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 27) {
2652e6ccafaeSMatthew G Knepley     /* Quadratic hexes (I hate this)
2653e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2654e6ccafaeSMatthew G Knepley        pointing up at the second.
2655e6ccafaeSMatthew G Knepley 
2656e6ccafaeSMatthew G Knepley          7---6
2657e6ccafaeSMatthew G Knepley         /|  /|
2658e6ccafaeSMatthew G Knepley        4---5 |
2659e6ccafaeSMatthew G Knepley        | 3-|-2
2660e6ccafaeSMatthew G Knepley        |/  |/
2661e6ccafaeSMatthew G Knepley        0---1
2662e6ccafaeSMatthew G Knepley 
2663e6ccafaeSMatthew G Knepley        Faces are determined by the first 4 vertices (corners of faces) */
2664e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuadHex = 9;
2665e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[9], i, iFace;
2666e6ccafaeSMatthew G Knepley     PetscBool      found                         = PETSC_FALSE;
2667e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHexSorted[54] = {
2668e6ccafaeSMatthew G Knepley       0, 1, 2, 3,  8, 9, 10, 11,  24, /* bottom */
2669e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  12, 13, 14, 15,  25, /* top */
2670e6ccafaeSMatthew G Knepley       0, 1, 4, 5,  8, 12, 16, 17,  22, /* front */
2671e6ccafaeSMatthew G Knepley       1, 2, 5, 6,  9, 13, 17, 18,  21, /* right */
2672e6ccafaeSMatthew G Knepley       2, 3, 6, 7,  10, 14, 18, 19,  23, /* back */
2673e6ccafaeSMatthew G Knepley       0, 3, 4, 7,  11, 15, 16, 19,  20, /* left */
2674e6ccafaeSMatthew G Knepley     };
2675e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHex[54] = {
2676e6ccafaeSMatthew G Knepley       3, 2, 1, 0,  10, 9, 8, 11,  24, /* bottom */
2677e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  12, 13, 14, 15,  25, /* top */
2678e6ccafaeSMatthew G Knepley       0, 1, 5, 4,  8, 17, 12, 16,  22, /* front */
2679e6ccafaeSMatthew G Knepley       1, 2, 6, 5,  9, 18, 13, 17,  21, /* right */
2680e6ccafaeSMatthew G Knepley       2, 3, 7, 6,  10, 19, 14, 18,  23, /* back */
2681e6ccafaeSMatthew G Knepley       3, 0, 4, 7,  11, 16, 15, 19,  20 /* left */
2682e6ccafaeSMatthew G Knepley     };
2683e6ccafaeSMatthew G Knepley 
2684e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuadHex; ++i) sortedIndices[i] = indices[i];
2685e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeQuadHex, sortedIndices);CHKERRQ(ierr);
2686e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2687e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeQuadHex;
2688e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2689e6ccafaeSMatthew G Knepley 
2690e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesQuadHexSorted[ii+0]) &&
2691e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesQuadHexSorted[ii+1]) &&
2692e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesQuadHexSorted[ii+2]) &&
2693e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesQuadHexSorted[ii+3])) {
2694e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuadHex; ++fVertex) {
2695e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuadHex; ++cVertex) {
2696e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuadHex[ii+fVertex]) {
2697e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2698e6ccafaeSMatthew G Knepley               break;
2699e6ccafaeSMatthew G Knepley             }
2700e6ccafaeSMatthew G Knepley           }
2701e6ccafaeSMatthew G Knepley         }
2702e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2703e6ccafaeSMatthew G Knepley         break;
2704e6ccafaeSMatthew G Knepley       }
2705e6ccafaeSMatthew G Knepley     }
2706e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2707e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2708e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2709e6ccafaeSMatthew G Knepley   } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Unknown cell type for faceOrientation().");
2710e6ccafaeSMatthew G Knepley   if (!posOrient) {
2711e6ccafaeSMatthew G Knepley     if (debug) {ierr = PetscPrintf(comm, "  Reversing initial face orientation\n");CHKERRQ(ierr);}
2712e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[faceSize-1 - f];
2713e6ccafaeSMatthew G Knepley   } else {
2714e6ccafaeSMatthew G Knepley     if (debug) {ierr = PetscPrintf(comm, "  Keeping initial face orientation\n");CHKERRQ(ierr);}
2715e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[f];
2716e6ccafaeSMatthew G Knepley   }
2717e6ccafaeSMatthew G Knepley   if (posOriented) *posOriented = posOrient;
2718e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2719e6ccafaeSMatthew G Knepley }
2720e6ccafaeSMatthew G Knepley 
2721c08575a3SMatthew G. Knepley /*@
2722c08575a3SMatthew G. Knepley   DMPlexGetOrientedFace - Given a cell and a face, as a set of vertices, return the oriented face, as a set of vertices,
2723c08575a3SMatthew G. Knepley   in faceVertices. The orientation is such that the face normal points out of the cell
2724c08575a3SMatthew G. Knepley 
2725c08575a3SMatthew G. Knepley   Not collective
2726c08575a3SMatthew G. Knepley 
2727c08575a3SMatthew G. Knepley   Input Parameters:
2728c08575a3SMatthew G. Knepley + dm           - The original mesh
2729c08575a3SMatthew G. Knepley . cell         - The cell mesh point
2730c08575a3SMatthew G. Knepley . faceSize     - The number of vertices on the face
2731c08575a3SMatthew G. Knepley . face         - The face vertices
2732c08575a3SMatthew G. Knepley . numCorners   - The number of vertices on the cell
2733c08575a3SMatthew G. Knepley . indices      - Local numbering of face vertices in cell cone
2734c08575a3SMatthew G. Knepley - origVertices - Original face vertices
2735c08575a3SMatthew G. Knepley 
2736c08575a3SMatthew G. Knepley   Output Parameter:
2737c08575a3SMatthew G. Knepley + faceVertices - The face vertices properly oriented
2738c08575a3SMatthew G. Knepley - posOriented  - PETSC_TRUE if the face was oriented with outward normal
2739c08575a3SMatthew G. Knepley 
2740c08575a3SMatthew G. Knepley   Level: developer
2741c08575a3SMatthew G. Knepley 
2742c08575a3SMatthew G. Knepley .seealso: DMPlexGetCone()
2743c08575a3SMatthew G. Knepley @*/
2744e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexGetOrientedFace(DM dm, PetscInt cell, PetscInt faceSize, const PetscInt face[], PetscInt numCorners, PetscInt indices[], PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2745e6ccafaeSMatthew G Knepley {
27460298fd71SBarry Smith   const PetscInt *cone = NULL;
2747e6ccafaeSMatthew G Knepley   PetscInt        coneSize, v, f, v2;
2748e6ccafaeSMatthew G Knepley   PetscInt        oppositeVertex = -1;
2749e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
2750e6ccafaeSMatthew G Knepley 
2751e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
2752e6ccafaeSMatthew G Knepley   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
2753e6ccafaeSMatthew G Knepley   ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
2754e6ccafaeSMatthew G Knepley   for (v = 0, v2 = 0; v < coneSize; ++v) {
2755e6ccafaeSMatthew G Knepley     PetscBool found = PETSC_FALSE;
2756e6ccafaeSMatthew G Knepley 
2757e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) {
2758e6ccafaeSMatthew G Knepley       if (face[f] == cone[v]) {
2759e6ccafaeSMatthew G Knepley         found = PETSC_TRUE; break;
2760e6ccafaeSMatthew G Knepley       }
2761e6ccafaeSMatthew G Knepley     }
2762e6ccafaeSMatthew G Knepley     if (found) {
2763e6ccafaeSMatthew G Knepley       indices[v2]      = v;
2764e6ccafaeSMatthew G Knepley       origVertices[v2] = cone[v];
2765e6ccafaeSMatthew G Knepley       ++v2;
2766e6ccafaeSMatthew G Knepley     } else {
2767e6ccafaeSMatthew G Knepley       oppositeVertex = v;
2768e6ccafaeSMatthew G Knepley     }
2769e6ccafaeSMatthew G Knepley   }
2770e6ccafaeSMatthew G Knepley   ierr = DMPlexGetFaceOrientation(dm, cell, numCorners, indices, oppositeVertex, origVertices, faceVertices, posOriented);CHKERRQ(ierr);
2771e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2772e6ccafaeSMatthew G Knepley }
2773e6ccafaeSMatthew G Knepley 
2774e6ccafaeSMatthew G Knepley /*
2775cd0c2139SMatthew G Knepley   DMPlexInsertFace_Internal - Puts a face into the mesh
2776e6ccafaeSMatthew G Knepley 
2777e6ccafaeSMatthew G Knepley   Not collective
2778e6ccafaeSMatthew G Knepley 
2779e6ccafaeSMatthew G Knepley   Input Parameters:
2780e6ccafaeSMatthew G Knepley   + dm              - The DMPlex
2781e6ccafaeSMatthew G Knepley   . numFaceVertex   - The number of vertices in the face
2782e6ccafaeSMatthew G Knepley   . faceVertices    - The vertices in the face for dm
2783e6ccafaeSMatthew G Knepley   . subfaceVertices - The vertices in the face for subdm
2784e6ccafaeSMatthew G Knepley   . numCorners      - The number of vertices in the cell
2785e6ccafaeSMatthew G Knepley   . cell            - A cell in dm containing the face
2786e6ccafaeSMatthew G Knepley   . subcell         - A cell in subdm containing the face
2787e6ccafaeSMatthew G Knepley   . firstFace       - First face in the mesh
2788e6ccafaeSMatthew G Knepley   - newFacePoint    - Next face in the mesh
2789e6ccafaeSMatthew G Knepley 
2790e6ccafaeSMatthew G Knepley   Output Parameters:
2791e6ccafaeSMatthew G Knepley   . newFacePoint - Contains next face point number on input, updated on output
2792e6ccafaeSMatthew G Knepley 
2793e6ccafaeSMatthew G Knepley   Level: developer
2794e6ccafaeSMatthew G Knepley */
2795cd0c2139SMatthew 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)
2796e6ccafaeSMatthew G Knepley {
279782f516ccSBarry Smith   MPI_Comm        comm;
2798e6ccafaeSMatthew G Knepley   DM_Plex        *submesh = (DM_Plex*) subdm->data;
2799e6ccafaeSMatthew G Knepley   const PetscInt *faces;
2800e6ccafaeSMatthew G Knepley   PetscInt        numFaces, coneSize;
2801e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
2802e6ccafaeSMatthew G Knepley 
2803e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
280482f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2805e6ccafaeSMatthew G Knepley   ierr = DMPlexGetConeSize(subdm, subcell, &coneSize);CHKERRQ(ierr);
2806e6ccafaeSMatthew G Knepley   if (coneSize != 1) SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cone size of cell %d is %d != 1", cell, coneSize);
2807e6ccafaeSMatthew G Knepley #if 0
2808e6ccafaeSMatthew G Knepley   /* Cannot use this because support() has not been constructed yet */
2809e6ccafaeSMatthew G Knepley   ierr = DMPlexGetJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces);CHKERRQ(ierr);
2810e6ccafaeSMatthew G Knepley #else
2811e6ccafaeSMatthew G Knepley   {
2812e6ccafaeSMatthew G Knepley     PetscInt f;
2813e6ccafaeSMatthew G Knepley 
2814e6ccafaeSMatthew G Knepley     numFaces = 0;
281569291d52SBarry Smith     ierr     = DMGetWorkArray(subdm, 1, MPIU_INT, (void **) &faces);CHKERRQ(ierr);
2816e6ccafaeSMatthew G Knepley     for (f = firstFace; f < *newFacePoint; ++f) {
2817e6ccafaeSMatthew G Knepley       PetscInt dof, off, d;
2818e6ccafaeSMatthew G Knepley 
2819e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(submesh->coneSection, f, &dof);CHKERRQ(ierr);
2820e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetOffset(submesh->coneSection, f, &off);CHKERRQ(ierr);
2821e6ccafaeSMatthew G Knepley       /* Yes, I know this is quadratic, but I expect the sizes to be <5 */
2822e6ccafaeSMatthew G Knepley       for (d = 0; d < dof; ++d) {
2823e6ccafaeSMatthew G Knepley         const PetscInt p = submesh->cones[off+d];
2824e6ccafaeSMatthew G Knepley         PetscInt       v;
2825e6ccafaeSMatthew G Knepley 
2826e6ccafaeSMatthew G Knepley         for (v = 0; v < numFaceVertices; ++v) {
2827e6ccafaeSMatthew G Knepley           if (subfaceVertices[v] == p) break;
2828e6ccafaeSMatthew G Knepley         }
2829e6ccafaeSMatthew G Knepley         if (v == numFaceVertices) break;
2830e6ccafaeSMatthew G Knepley       }
2831e6ccafaeSMatthew G Knepley       if (d == dof) {
2832e6ccafaeSMatthew G Knepley         numFaces               = 1;
2833e6ccafaeSMatthew G Knepley         ((PetscInt*) faces)[0] = f;
2834e6ccafaeSMatthew G Knepley       }
2835e6ccafaeSMatthew G Knepley     }
2836e6ccafaeSMatthew G Knepley   }
2837e6ccafaeSMatthew G Knepley #endif
2838e6ccafaeSMatthew G Knepley   if (numFaces > 1) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Vertex set had %d faces, not one", numFaces);
2839e6ccafaeSMatthew G Knepley   else if (numFaces == 1) {
2840e6ccafaeSMatthew G Knepley     /* Add the other cell neighbor for this face */
2841766ab985SMatthew G. Knepley     ierr = DMPlexSetCone(subdm, subcell, faces);CHKERRQ(ierr);
2842e6ccafaeSMatthew G Knepley   } else {
2843e6ccafaeSMatthew G Knepley     PetscInt *indices, *origVertices, *orientedVertices, *orientedSubVertices, v, ov;
2844e6ccafaeSMatthew G Knepley     PetscBool posOriented;
2845e6ccafaeSMatthew G Knepley 
284669291d52SBarry Smith     ierr                = DMGetWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices);CHKERRQ(ierr);
2847e6ccafaeSMatthew G Knepley     origVertices        = &orientedVertices[numFaceVertices];
2848e6ccafaeSMatthew G Knepley     indices             = &orientedVertices[numFaceVertices*2];
2849e6ccafaeSMatthew G Knepley     orientedSubVertices = &orientedVertices[numFaceVertices*3];
2850e6ccafaeSMatthew G Knepley     ierr                = DMPlexGetOrientedFace(dm, cell, numFaceVertices, faceVertices, numCorners, indices, origVertices, orientedVertices, &posOriented);CHKERRQ(ierr);
2851e6ccafaeSMatthew G Knepley     /* TODO: I know that routine should return a permutation, not the indices */
2852e6ccafaeSMatthew G Knepley     for (v = 0; v < numFaceVertices; ++v) {
2853e6ccafaeSMatthew G Knepley       const PetscInt vertex = faceVertices[v], subvertex = subfaceVertices[v];
2854e6ccafaeSMatthew G Knepley       for (ov = 0; ov < numFaceVertices; ++ov) {
2855e6ccafaeSMatthew G Knepley         if (orientedVertices[ov] == vertex) {
2856e6ccafaeSMatthew G Knepley           orientedSubVertices[ov] = subvertex;
2857e6ccafaeSMatthew G Knepley           break;
2858e6ccafaeSMatthew G Knepley         }
2859e6ccafaeSMatthew G Knepley       }
2860e6ccafaeSMatthew G Knepley       if (ov == numFaceVertices) SETERRQ1(comm, PETSC_ERR_PLIB, "Could not find face vertex %d in orientated set", vertex);
2861e6ccafaeSMatthew G Knepley     }
2862e6ccafaeSMatthew G Knepley     ierr = DMPlexSetCone(subdm, *newFacePoint, orientedSubVertices);CHKERRQ(ierr);
2863e6ccafaeSMatthew G Knepley     ierr = DMPlexSetCone(subdm, subcell, newFacePoint);CHKERRQ(ierr);
286469291d52SBarry Smith     ierr = DMRestoreWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices);CHKERRQ(ierr);
2865e6ccafaeSMatthew G Knepley     ++(*newFacePoint);
2866e6ccafaeSMatthew G Knepley   }
2867ef07cca7SMatthew G. Knepley #if 0
2868e6ccafaeSMatthew G Knepley   ierr = DMPlexRestoreJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces);CHKERRQ(ierr);
2869ef07cca7SMatthew G. Knepley #else
287069291d52SBarry Smith   ierr = DMRestoreWorkArray(subdm, 1, MPIU_INT, (void **) &faces);CHKERRQ(ierr);
2871ef07cca7SMatthew G. Knepley #endif
2872e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2873e6ccafaeSMatthew G Knepley }
2874e6ccafaeSMatthew G Knepley 
287553156dfcSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm)
2876e6ccafaeSMatthew G Knepley {
287782f516ccSBarry Smith   MPI_Comm        comm;
287853156dfcSMatthew G. Knepley   DMLabel         subpointMap;
2879efa14ee0SMatthew G Knepley   IS              subvertexIS,  subcellIS;
2880efa14ee0SMatthew G Knepley   const PetscInt *subVertices, *subCells;
2881efa14ee0SMatthew G Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells;
2882fed694aaSMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces = 0, firstSubFace, newFacePoint, nFV = 0;
2883efa14ee0SMatthew G Knepley   PetscInt        vStart, vEnd, c, f;
2884e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
2885e6ccafaeSMatthew G Knepley 
2886e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
288782f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2888efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
2889d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap);CHKERRQ(ierr);
2890efa14ee0SMatthew G Knepley   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
2891efa14ee0SMatthew G Knepley   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
289253156dfcSMatthew G. Knepley   if (vertexLabel) {ierr = DMPlexMarkSubmesh_Uninterpolated(dm, vertexLabel, value, subpointMap, &numSubFaces, &nFV, subdm);CHKERRQ(ierr);}
2893efa14ee0SMatthew G Knepley   /* Setup chart */
2894efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumSize(subpointMap, 0, &numSubVertices);CHKERRQ(ierr);
2895efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumSize(subpointMap, 2, &numSubCells);CHKERRQ(ierr);
2896efa14ee0SMatthew G Knepley   ierr = DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices);CHKERRQ(ierr);
2897efa14ee0SMatthew G Knepley   ierr = DMPlexSetVTKCellHeight(subdm, 1);CHKERRQ(ierr);
2898e6ccafaeSMatthew G Knepley   /* Set cone sizes */
2899e6ccafaeSMatthew G Knepley   firstSubVertex = numSubCells;
2900efa14ee0SMatthew G Knepley   firstSubFace   = numSubCells+numSubVertices;
2901e6ccafaeSMatthew G Knepley   newFacePoint   = firstSubFace;
2902efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumIS(subpointMap, 0, &subvertexIS);CHKERRQ(ierr);
2903efa14ee0SMatthew G Knepley   if (subvertexIS) {ierr = ISGetIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
2904efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumIS(subpointMap, 2, &subcellIS);CHKERRQ(ierr);
2905efa14ee0SMatthew G Knepley   if (subcellIS) {ierr = ISGetIndices(subcellIS, &subCells);CHKERRQ(ierr);}
2906e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
2907e6ccafaeSMatthew G Knepley     ierr = DMPlexSetConeSize(subdm, c, 1);CHKERRQ(ierr);
2908e6ccafaeSMatthew G Knepley   }
2909e6ccafaeSMatthew G Knepley   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
2910e6ccafaeSMatthew G Knepley     ierr = DMPlexSetConeSize(subdm, f, nFV);CHKERRQ(ierr);
2911e6ccafaeSMatthew G Knepley   }
2912e6ccafaeSMatthew G Knepley   ierr = DMSetUp(subdm);CHKERRQ(ierr);
2913e6ccafaeSMatthew G Knepley   /* Create face cones */
2914efa14ee0SMatthew G Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
29150298fd71SBarry Smith   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
291669291d52SBarry Smith   ierr = DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
2917e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
2918e6ccafaeSMatthew G Knepley     const PetscInt cell    = subCells[c];
2919efa14ee0SMatthew G Knepley     const PetscInt subcell = c;
29200298fd71SBarry Smith     PetscInt      *closure = NULL;
2921efa14ee0SMatthew G Knepley     PetscInt       closureSize, cl, numCorners = 0, faceSize = 0;
2922e6ccafaeSMatthew G Knepley 
2923e6ccafaeSMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2924efa14ee0SMatthew G Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
2925efa14ee0SMatthew G Knepley       const PetscInt point = closure[cl];
2926e6ccafaeSMatthew G Knepley       PetscInt       subVertex;
2927e6ccafaeSMatthew G Knepley 
2928efa14ee0SMatthew G Knepley       if ((point >= vStart) && (point < vEnd)) {
2929efa14ee0SMatthew G Knepley         ++numCorners;
2930efa14ee0SMatthew G Knepley         ierr = PetscFindInt(point, numSubVertices, subVertices, &subVertex);CHKERRQ(ierr);
2931efa14ee0SMatthew G Knepley         if (subVertex >= 0) {
2932efa14ee0SMatthew G Knepley           closure[faceSize] = point;
293365560c7fSMatthew G Knepley           subface[faceSize] = firstSubVertex+subVertex;
2934e6ccafaeSMatthew G Knepley           ++faceSize;
2935e6ccafaeSMatthew G Knepley         }
2936e6ccafaeSMatthew G Knepley       }
2937e6ccafaeSMatthew G Knepley     }
2938efa14ee0SMatthew G Knepley     if (faceSize > nFV) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %d of an element on the surface", faceSize);
2939efa14ee0SMatthew G Knepley     if (faceSize == nFV) {
2940cd0c2139SMatthew G Knepley       ierr = DMPlexInsertFace_Internal(dm, subdm, faceSize, closure, subface, numCorners, cell, subcell, firstSubFace, &newFacePoint);CHKERRQ(ierr);
2941e6ccafaeSMatthew G Knepley     }
294265560c7fSMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2943e6ccafaeSMatthew G Knepley   }
294469291d52SBarry Smith   ierr = DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
2945e6ccafaeSMatthew G Knepley   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
2946e6ccafaeSMatthew G Knepley   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
2947e6ccafaeSMatthew G Knepley   /* Build coordinates */
2948efa14ee0SMatthew G Knepley   {
2949efa14ee0SMatthew G Knepley     PetscSection coordSection, subCoordSection;
2950efa14ee0SMatthew G Knepley     Vec          coordinates, subCoordinates;
2951efa14ee0SMatthew G Knepley     PetscScalar *coords, *subCoords;
2952285d324eSMatthew G. Knepley     PetscInt     numComp, coordSize, v;
295324640c55SToby Isaac     const char  *name;
2954efa14ee0SMatthew G Knepley 
295569d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
2956e6ccafaeSMatthew G Knepley     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
295769d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
2958285d324eSMatthew G. Knepley     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
2959285d324eSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
2960285d324eSMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
2961efa14ee0SMatthew G Knepley     ierr = PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices);CHKERRQ(ierr);
2962efa14ee0SMatthew G Knepley     for (v = 0; v < numSubVertices; ++v) {
2963efa14ee0SMatthew G Knepley       const PetscInt vertex    = subVertices[v];
2964efa14ee0SMatthew G Knepley       const PetscInt subvertex = firstSubVertex+v;
2965efa14ee0SMatthew G Knepley       PetscInt       dof;
2966efa14ee0SMatthew G Knepley 
2967efa14ee0SMatthew G Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
2968efa14ee0SMatthew G Knepley       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
2969285d324eSMatthew G. Knepley       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
2970e6ccafaeSMatthew G Knepley     }
2971e6ccafaeSMatthew G Knepley     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
2972e6ccafaeSMatthew G Knepley     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
29738b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &subCoordinates);CHKERRQ(ierr);
297424640c55SToby Isaac     ierr = PetscObjectGetName((PetscObject)coordinates,&name);CHKERRQ(ierr);
297524640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)subCoordinates,name);CHKERRQ(ierr);
2976e6ccafaeSMatthew G Knepley     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
29772eb5907fSJed Brown     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
2978830e53efSMatthew G. Knepley     if (coordSize) {
2979e6ccafaeSMatthew G Knepley       ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
2980e6ccafaeSMatthew G Knepley       ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
2981efa14ee0SMatthew G Knepley       for (v = 0; v < numSubVertices; ++v) {
2982efa14ee0SMatthew G Knepley         const PetscInt vertex    = subVertices[v];
2983efa14ee0SMatthew G Knepley         const PetscInt subvertex = firstSubVertex+v;
2984efa14ee0SMatthew G Knepley         PetscInt       dof, off, sdof, soff, d;
2985e6ccafaeSMatthew G Knepley 
2986e6ccafaeSMatthew G Knepley         ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
2987e6ccafaeSMatthew G Knepley         ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
2988efa14ee0SMatthew G Knepley         ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
2989efa14ee0SMatthew G Knepley         ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
2990efa14ee0SMatthew G Knepley         if (dof != sdof) SETERRQ4(comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
2991e6ccafaeSMatthew G Knepley         for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
2992e6ccafaeSMatthew G Knepley       }
2993e6ccafaeSMatthew G Knepley       ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
2994e6ccafaeSMatthew G Knepley       ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
29953b399e24SMatthew G. Knepley     }
2996e6ccafaeSMatthew G Knepley     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
2997e6ccafaeSMatthew G Knepley     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
2998e6ccafaeSMatthew G Knepley   }
2999efa14ee0SMatthew G Knepley   /* Cleanup */
3000efa14ee0SMatthew G Knepley   if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
3001efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
3002efa14ee0SMatthew G Knepley   if (subcellIS) {ierr = ISRestoreIndices(subcellIS, &subCells);CHKERRQ(ierr);}
3003efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subcellIS);CHKERRQ(ierr);
3004e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3005e6ccafaeSMatthew G Knepley }
3006e6ccafaeSMatthew G Knepley 
30073982b651SMatthew G. Knepley PETSC_STATIC_INLINE PetscInt DMPlexFilterPoint_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[])
30083982b651SMatthew G. Knepley {
30093982b651SMatthew G. Knepley   PetscInt       subPoint;
30103982b651SMatthew G. Knepley   PetscErrorCode ierr;
30113982b651SMatthew G. Knepley 
30123982b651SMatthew G. Knepley   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint); if (ierr < 0) return ierr;
30133982b651SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint+subPoint;
30143982b651SMatthew G. Knepley }
30153982b651SMatthew G. Knepley 
3016158acfadSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmeshGeneric_Interpolated(DM dm, DMLabel label, PetscInt value, PetscBool markedFaces, PetscBool isCohesive, PetscInt cellHeight, DM subdm)
3017e6ccafaeSMatthew G Knepley {
301882f516ccSBarry Smith   MPI_Comm         comm;
301953156dfcSMatthew G. Knepley   DMLabel          subpointMap;
3020efa14ee0SMatthew G Knepley   IS              *subpointIS;
3021efa14ee0SMatthew G Knepley   const PetscInt **subpoints;
30223982b651SMatthew G. Knepley   PetscInt        *numSubPoints, *firstSubPoint, *coneNew, *orntNew;
3023*412e9a14SMatthew G. Knepley   PetscInt         totSubPoints = 0, maxConeSize, dim, p, d, v;
30240d366550SMatthew G. Knepley   PetscMPIInt      rank;
3025e6ccafaeSMatthew G Knepley   PetscErrorCode   ierr;
3026e6ccafaeSMatthew G Knepley 
3027e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
302882f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
30290d366550SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3030efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
3031d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap);CHKERRQ(ierr);
3032efa14ee0SMatthew G Knepley   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
3033bec263e5SMatthew G. Knepley   if (cellHeight) {
30343982b651SMatthew G. Knepley     if (isCohesive) {ierr = DMPlexMarkCohesiveSubmesh_Interpolated(dm, label, value, subpointMap, subdm);CHKERRQ(ierr);}
3035158acfadSMatthew G. Knepley     else            {ierr = DMPlexMarkSubmesh_Interpolated(dm, label, value, markedFaces, subpointMap, subdm);CHKERRQ(ierr);}
3036bec263e5SMatthew G. Knepley   } else {
3037bec263e5SMatthew G. Knepley     DMLabel         depth;
3038bec263e5SMatthew G. Knepley     IS              pointIS;
3039bec263e5SMatthew G. Knepley     const PetscInt *points;
3040bec263e5SMatthew G. Knepley     PetscInt        numPoints;
3041bec263e5SMatthew G. Knepley 
3042bec263e5SMatthew G. Knepley     ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
3043bec263e5SMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, value, &numPoints);CHKERRQ(ierr);
3044bec263e5SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, value, &pointIS);CHKERRQ(ierr);
3045bec263e5SMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
3046bec263e5SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
3047bec263e5SMatthew G. Knepley       PetscInt *closure = NULL;
3048bec263e5SMatthew G. Knepley       PetscInt  closureSize, c, pdim;
3049bec263e5SMatthew G. Knepley 
3050bec263e5SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
3051bec263e5SMatthew G. Knepley       for (c = 0; c < closureSize*2; c += 2) {
3052bec263e5SMatthew G. Knepley         ierr = DMLabelGetValue(depth, closure[c], &pdim);CHKERRQ(ierr);
3053bec263e5SMatthew G. Knepley         ierr = DMLabelSetValue(subpointMap, closure[c], pdim);CHKERRQ(ierr);
3054bec263e5SMatthew G. Knepley       }
3055bec263e5SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
3056bec263e5SMatthew G. Knepley     }
3057bec263e5SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
3058bec263e5SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
3059bec263e5SMatthew G. Knepley   }
3060efa14ee0SMatthew G Knepley   /* Setup chart */
3061c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3062dcca6d9dSJed Brown   ierr = PetscMalloc4(dim+1,&numSubPoints,dim+1,&firstSubPoint,dim+1,&subpointIS,dim+1,&subpoints);CHKERRQ(ierr);
3063e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
3064e6ccafaeSMatthew G Knepley     ierr = DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d]);CHKERRQ(ierr);
3065e6ccafaeSMatthew G Knepley     totSubPoints += numSubPoints[d];
3066e6ccafaeSMatthew G Knepley   }
3067e6ccafaeSMatthew G Knepley   ierr = DMPlexSetChart(subdm, 0, totSubPoints);CHKERRQ(ierr);
3068bec263e5SMatthew G. Knepley   ierr = DMPlexSetVTKCellHeight(subdm, cellHeight);CHKERRQ(ierr);
3069e6ccafaeSMatthew G Knepley   /* Set cone sizes */
3070e6ccafaeSMatthew G Knepley   firstSubPoint[dim] = 0;
3071e6ccafaeSMatthew G Knepley   firstSubPoint[0]   = firstSubPoint[dim] + numSubPoints[dim];
3072e6ccafaeSMatthew G Knepley   if (dim > 1) {firstSubPoint[dim-1] = firstSubPoint[0]     + numSubPoints[0];}
3073e6ccafaeSMatthew G Knepley   if (dim > 2) {firstSubPoint[dim-2] = firstSubPoint[dim-1] + numSubPoints[dim-1];}
3074e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
3075e6ccafaeSMatthew G Knepley     ierr = DMLabelGetStratumIS(subpointMap, d, &subpointIS[d]);CHKERRQ(ierr);
30760ae02aa4SMatthew G. Knepley     if (subpointIS[d]) {ierr = ISGetIndices(subpointIS[d], &subpoints[d]);CHKERRQ(ierr);}
3077e6ccafaeSMatthew G Knepley   }
3078*412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
3079*412e9a14SMatthew G. Knepley   ierr = DMCreateLabel(subdm, "celltype");CHKERRQ(ierr);
3080e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
3081e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3082e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3083e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
3084e6ccafaeSMatthew G Knepley       const PetscInt *cone;
3085e6ccafaeSMatthew G Knepley       PetscInt        coneSize, coneSizeNew, c, val;
3086*412e9a14SMatthew G. Knepley       DMPolytopeType  ct;
3087e6ccafaeSMatthew G Knepley 
3088e6ccafaeSMatthew G Knepley       ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
3089e6ccafaeSMatthew G Knepley       ierr = DMPlexSetConeSize(subdm, subpoint, coneSize);CHKERRQ(ierr);
3090*412e9a14SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, point, &ct);CHKERRQ(ierr);
3091*412e9a14SMatthew G. Knepley       ierr = DMPlexSetCellType(subdm, subpoint, ct);CHKERRQ(ierr);
3092bec263e5SMatthew G. Knepley       if (cellHeight && (d == dim)) {
3093e6ccafaeSMatthew G Knepley         ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
3094e6ccafaeSMatthew G Knepley         for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
3095e6ccafaeSMatthew G Knepley           ierr = DMLabelGetValue(subpointMap, cone[c], &val);CHKERRQ(ierr);
3096e6ccafaeSMatthew G Knepley           if (val >= 0) coneSizeNew++;
3097e6ccafaeSMatthew G Knepley         }
3098e6ccafaeSMatthew G Knepley         ierr = DMPlexSetConeSize(subdm, subpoint, coneSizeNew);CHKERRQ(ierr);
3099*412e9a14SMatthew G. Knepley         ierr = DMPlexSetCellType(subdm, subpoint, DM_POLYTOPE_FV_GHOST);CHKERRQ(ierr);
3100e6ccafaeSMatthew G Knepley       }
3101e6ccafaeSMatthew G Knepley     }
3102e6ccafaeSMatthew G Knepley   }
3103d67d17b1SMatthew G. Knepley   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
3104e6ccafaeSMatthew G Knepley   ierr = DMSetUp(subdm);CHKERRQ(ierr);
3105e6ccafaeSMatthew G Knepley   /* Set cones */
31060298fd71SBarry Smith   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
31073982b651SMatthew G. Knepley   ierr = PetscMalloc2(maxConeSize,&coneNew,maxConeSize,&orntNew);CHKERRQ(ierr);
3108e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
3109e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3110e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3111e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
31120e49e2e2SMatthew G. Knepley       const PetscInt *cone, *ornt;
31130d366550SMatthew G. Knepley       PetscInt        coneSize, subconeSize, coneSizeNew, c, subc, fornt = 0;
3114e6ccafaeSMatthew G Knepley 
31150d366550SMatthew G. Knepley       if (d == dim-1) {
31160d366550SMatthew G. Knepley         const PetscInt *support, *cone, *ornt;
31170d366550SMatthew G. Knepley         PetscInt        supportSize, coneSize, s, subc;
31180d366550SMatthew G. Knepley 
31190d366550SMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
31200d366550SMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
31210d366550SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
3122*412e9a14SMatthew G. Knepley           PetscBool isHybrid;
3123*412e9a14SMatthew G. Knepley 
3124*412e9a14SMatthew G. Knepley           ierr = DMPlexCellIsHybrid_Internal(dm, support[s], &isHybrid);CHKERRQ(ierr);
3125*412e9a14SMatthew G. Knepley           if (!isHybrid) continue;
31260d366550SMatthew G. Knepley           ierr = PetscFindInt(support[s], numSubPoints[d+1], subpoints[d+1], &subc);CHKERRQ(ierr);
31270d366550SMatthew G. Knepley           if (subc >= 0) {
31280d366550SMatthew G. Knepley             const PetscInt ccell = subpoints[d+1][subc];
31290d366550SMatthew G. Knepley 
31307f79407eSBarry Smith             ierr = DMPlexGetCone(dm, ccell, &cone);CHKERRQ(ierr);
31317f79407eSBarry Smith             ierr = DMPlexGetConeSize(dm, ccell, &coneSize);CHKERRQ(ierr);
31327f79407eSBarry Smith             ierr = DMPlexGetConeOrientation(dm, ccell, &ornt);CHKERRQ(ierr);
31330d366550SMatthew G. Knepley             for (c = 0; c < coneSize; ++c) {
31340d366550SMatthew G. Knepley               if (cone[c] == point) {
31350d366550SMatthew G. Knepley                 fornt = ornt[c];
31360d366550SMatthew G. Knepley                 break;
31370d366550SMatthew G. Knepley               }
31380d366550SMatthew G. Knepley             }
31390d366550SMatthew G. Knepley             break;
31400d366550SMatthew G. Knepley           }
31410d366550SMatthew G. Knepley         }
31420d366550SMatthew G. Knepley       }
3143e6ccafaeSMatthew G Knepley       ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
3144e6ccafaeSMatthew G Knepley       ierr = DMPlexGetConeSize(subdm, subpoint, &subconeSize);CHKERRQ(ierr);
3145e6ccafaeSMatthew G Knepley       ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
31460e49e2e2SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, point, &ornt);CHKERRQ(ierr);
3147e6ccafaeSMatthew G Knepley       for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
3148e6ccafaeSMatthew G Knepley         ierr = PetscFindInt(cone[c], numSubPoints[d-1], subpoints[d-1], &subc);CHKERRQ(ierr);
314901a2673eSMatthew G. Knepley         if (subc >= 0) {
315001a2673eSMatthew G. Knepley           coneNew[coneSizeNew] = firstSubPoint[d-1] + subc;
31513982b651SMatthew G. Knepley           orntNew[coneSizeNew] = ornt[c];
315201a2673eSMatthew G. Knepley           ++coneSizeNew;
315301a2673eSMatthew G. Knepley         }
3154e6ccafaeSMatthew G Knepley       }
3155e6ccafaeSMatthew G Knepley       if (coneSizeNew != subconeSize) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of cone points located %d does not match subcone size %d", coneSizeNew, subconeSize);
31560d366550SMatthew G. Knepley       if (fornt < 0) {
31570d366550SMatthew G. Knepley         /* This should be replaced by a call to DMPlexReverseCell() */
31580d366550SMatthew G. Knepley #if 0
3159a129c400SMatthew G. Knepley         ierr = DMPlexReverseCell(subdm, subpoint);CHKERRQ(ierr);
3160a129c400SMatthew G. Knepley #else
3161c0b40891SMatthew G. Knepley         for (c = 0; c < coneSizeNew/2 + coneSizeNew%2; ++c) {
3162a129c400SMatthew G. Knepley           PetscInt faceSize, tmp;
31630d366550SMatthew G. Knepley 
31640d366550SMatthew G. Knepley           tmp        = coneNew[c];
31650d366550SMatthew G. Knepley           coneNew[c] = coneNew[coneSizeNew-1-c];
31660d366550SMatthew G. Knepley           coneNew[coneSizeNew-1-c] = tmp;
3167a129c400SMatthew G. Knepley           ierr = DMPlexGetConeSize(dm, cone[c], &faceSize);CHKERRQ(ierr);
3168a129c400SMatthew G. Knepley           tmp        = orntNew[c] >= 0 ? -(faceSize-orntNew[c]) : faceSize+orntNew[c];
3169a129c400SMatthew G. Knepley           orntNew[c] = orntNew[coneSizeNew-1-c] >= 0 ? -(faceSize-orntNew[coneSizeNew-1-c]) : faceSize+orntNew[coneSizeNew-1-c];
31700d366550SMatthew G. Knepley           orntNew[coneSizeNew-1-c] = tmp;
31710d366550SMatthew G. Knepley         }
31720d366550SMatthew G. Knepley       }
3173e6ccafaeSMatthew G Knepley       ierr = DMPlexSetCone(subdm, subpoint, coneNew);CHKERRQ(ierr);
31743982b651SMatthew G. Knepley       ierr = DMPlexSetConeOrientation(subdm, subpoint, orntNew);CHKERRQ(ierr);
3175a129c400SMatthew G. Knepley #endif
3176e6ccafaeSMatthew G Knepley     }
3177e6ccafaeSMatthew G Knepley   }
31783982b651SMatthew G. Knepley   ierr = PetscFree2(coneNew,orntNew);CHKERRQ(ierr);
3179e6ccafaeSMatthew G Knepley   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
3180e6ccafaeSMatthew G Knepley   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
3181e6ccafaeSMatthew G Knepley   /* Build coordinates */
3182e6ccafaeSMatthew G Knepley   {
3183e6ccafaeSMatthew G Knepley     PetscSection coordSection, subCoordSection;
3184e6ccafaeSMatthew G Knepley     Vec          coordinates, subCoordinates;
3185e6ccafaeSMatthew G Knepley     PetscScalar *coords, *subCoords;
3186c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize;
318724640c55SToby Isaac     const char  *name;
3188e6ccafaeSMatthew G Knepley 
3189c0e8cf5fSMatthew G. Knepley     ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
319069d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
3191e6ccafaeSMatthew G Knepley     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
319269d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
3193285d324eSMatthew G. Knepley     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
3194285d324eSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
3195285d324eSMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
3196e6ccafaeSMatthew G Knepley     ierr = PetscSectionSetChart(subCoordSection, firstSubPoint[0], firstSubPoint[0]+numSubPoints[0]);CHKERRQ(ierr);
3197e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3198e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3199e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0]+v;
3200e6ccafaeSMatthew G Knepley       PetscInt       dof;
3201e6ccafaeSMatthew G Knepley 
3202e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3203e6ccafaeSMatthew G Knepley       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
3204285d324eSMatthew G. Knepley       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
3205e6ccafaeSMatthew G Knepley     }
3206e6ccafaeSMatthew G Knepley     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
3207e6ccafaeSMatthew G Knepley     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
32088b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &subCoordinates);CHKERRQ(ierr);
320924640c55SToby Isaac     ierr = PetscObjectGetName((PetscObject)coordinates,&name);CHKERRQ(ierr);
321024640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)subCoordinates,name);CHKERRQ(ierr);
3211e6ccafaeSMatthew G Knepley     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
3212c0e8cf5fSMatthew G. Knepley     ierr = VecSetBlockSize(subCoordinates, cdim);CHKERRQ(ierr);
32132eb5907fSJed Brown     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
3214e6ccafaeSMatthew G Knepley     ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
3215e6ccafaeSMatthew G Knepley     ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3216e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3217e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3218e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0]+v;
3219e6ccafaeSMatthew G Knepley       PetscInt dof, off, sdof, soff, d;
3220e6ccafaeSMatthew G Knepley 
3221e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3222e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
3223e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
3224e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
3225e6ccafaeSMatthew G Knepley       if (dof != sdof) SETERRQ4(comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
3226efa14ee0SMatthew G Knepley       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3227e6ccafaeSMatthew G Knepley     }
3228e6ccafaeSMatthew G Knepley     ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
3229e6ccafaeSMatthew G Knepley     ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3230e6ccafaeSMatthew G Knepley     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
3231e6ccafaeSMatthew G Knepley     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
3232e6ccafaeSMatthew G Knepley   }
32333982b651SMatthew G. Knepley   /* Build SF: We need this complexity because subpoints might not be selected on the owning process */
32343982b651SMatthew G. Knepley   {
32353982b651SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
32363982b651SMatthew G. Knepley     IS                 subpIS;
32373982b651SMatthew G. Knepley     const PetscSFNode *remotePoints;
32383982b651SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
32393982b651SMatthew G. Knepley     const PetscInt    *localPoints, *subpoints;
32403982b651SMatthew G. Knepley     PetscInt          *slocalPoints;
32413982b651SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubpoints = 0, numSubroots, numSubleaves = 0, l, sl, ll, pStart, pEnd, p;
32423982b651SMatthew G. Knepley     PetscMPIInt        rank;
32433982b651SMatthew G. Knepley 
32443982b651SMatthew G. Knepley     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
32453982b651SMatthew G. Knepley     ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
32463982b651SMatthew G. Knepley     ierr = DMGetPointSF(subdm, &sfPointSub);CHKERRQ(ierr);
32473982b651SMatthew G. Knepley     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
32483982b651SMatthew G. Knepley     ierr = DMPlexGetChart(subdm, NULL, &numSubroots);CHKERRQ(ierr);
32493982b651SMatthew G. Knepley     ierr = DMPlexCreateSubpointIS(subdm, &subpIS);CHKERRQ(ierr);
32503982b651SMatthew G. Knepley     if (subpIS) {
32513982b651SMatthew G. Knepley       ierr = ISGetIndices(subpIS, &subpoints);CHKERRQ(ierr);
32523982b651SMatthew G. Knepley       ierr = ISGetLocalSize(subpIS, &numSubpoints);CHKERRQ(ierr);
32533982b651SMatthew G. Knepley     }
32543982b651SMatthew G. Knepley     ierr = PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr);
32553982b651SMatthew G. Knepley     if (numRoots >= 0) {
32563982b651SMatthew G. Knepley       ierr = PetscMalloc2(pEnd-pStart,&newLocalPoints,numRoots,&newOwners);CHKERRQ(ierr);
32573982b651SMatthew G. Knepley       for (p = 0; p < pEnd-pStart; ++p) {
32583982b651SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
32593982b651SMatthew G. Knepley         newLocalPoints[p].index = -2;
32603982b651SMatthew G. Knepley       }
32613982b651SMatthew G. Knepley       /* Set subleaves */
32623982b651SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
32633982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
32643982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(point, 0, numSubpoints, subpoints);
32653982b651SMatthew G. Knepley 
32663982b651SMatthew G. Knepley         if (subpoint < 0) continue;
32673982b651SMatthew G. Knepley         newLocalPoints[point-pStart].rank  = rank;
32683982b651SMatthew G. Knepley         newLocalPoints[point-pStart].index = subpoint;
32693982b651SMatthew G. Knepley         ++numSubleaves;
32703982b651SMatthew G. Knepley       }
32713982b651SMatthew G. Knepley       /* Must put in owned subpoints */
32723982b651SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
32733982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(p, 0, numSubpoints, subpoints);
32743982b651SMatthew G. Knepley 
32753982b651SMatthew G. Knepley         if (subpoint < 0) {
32763982b651SMatthew G. Knepley           newOwners[p-pStart].rank  = -3;
32773982b651SMatthew G. Knepley           newOwners[p-pStart].index = -3;
32783982b651SMatthew G. Knepley         } else {
32793982b651SMatthew G. Knepley           newOwners[p-pStart].rank  = rank;
32803982b651SMatthew G. Knepley           newOwners[p-pStart].index = subpoint;
32813982b651SMatthew G. Knepley         }
32823982b651SMatthew G. Knepley       }
32833982b651SMatthew G. Knepley       ierr = PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
32843982b651SMatthew G. Knepley       ierr = PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
32853982b651SMatthew G. Knepley       ierr = PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr);
32863982b651SMatthew G. Knepley       ierr = PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr);
32873982b651SMatthew G. Knepley       ierr = PetscMalloc1(numSubleaves, &slocalPoints);CHKERRQ(ierr);
32883982b651SMatthew G. Knepley       ierr = PetscMalloc1(numSubleaves, &sremotePoints);CHKERRQ(ierr);
32893982b651SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
32903982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
32913982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(point, 0, numSubpoints, subpoints);
32923982b651SMatthew G. Knepley 
32933982b651SMatthew G. Knepley         if (subpoint < 0) continue;
32943982b651SMatthew G. Knepley         if (newLocalPoints[point].rank == rank) {++ll; continue;}
32953982b651SMatthew G. Knepley         slocalPoints[sl]        = subpoint;
32963982b651SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
32973982b651SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
32983982b651SMatthew G. Knepley         if (sremotePoints[sl].rank  < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %d", point);
32993982b651SMatthew G. Knepley         if (sremotePoints[sl].index < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %d", point);
33003982b651SMatthew G. Knepley         ++sl;
33013982b651SMatthew G. Knepley       }
33023982b651SMatthew G. Knepley       if (sl + ll != numSubleaves) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %d + %d != %d", sl, ll, numSubleaves);
33033982b651SMatthew G. Knepley       ierr = PetscFree2(newLocalPoints,newOwners);CHKERRQ(ierr);
33043982b651SMatthew G. Knepley       ierr = PetscSFSetGraph(sfPointSub, numSubroots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
33053982b651SMatthew G. Knepley     }
33063982b651SMatthew G. Knepley     if (subpIS) {
33073982b651SMatthew G. Knepley       ierr = ISRestoreIndices(subpIS, &subpoints);CHKERRQ(ierr);
33083982b651SMatthew G. Knepley       ierr = ISDestroy(&subpIS);CHKERRQ(ierr);
33093982b651SMatthew G. Knepley     }
33103982b651SMatthew G. Knepley   }
3311efa14ee0SMatthew G Knepley   /* Cleanup */
3312e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
33130ae02aa4SMatthew G. Knepley     if (subpointIS[d]) {ierr = ISRestoreIndices(subpointIS[d], &subpoints[d]);CHKERRQ(ierr);}
3314e6ccafaeSMatthew G Knepley     ierr = ISDestroy(&subpointIS[d]);CHKERRQ(ierr);
3315e6ccafaeSMatthew G Knepley   }
3316efa14ee0SMatthew G Knepley   ierr = PetscFree4(numSubPoints,firstSubPoint,subpointIS,subpoints);CHKERRQ(ierr);
3317e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3318e6ccafaeSMatthew G Knepley }
3319e6ccafaeSMatthew G Knepley 
3320158acfadSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM subdm)
33213982b651SMatthew G. Knepley {
33223982b651SMatthew G. Knepley   PetscErrorCode ierr;
33233982b651SMatthew G. Knepley 
33243982b651SMatthew G. Knepley   PetscFunctionBegin;
3325158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmeshGeneric_Interpolated(dm, vertexLabel, value, markedFaces, PETSC_FALSE, 1, subdm);CHKERRQ(ierr);
33263982b651SMatthew G. Knepley   PetscFunctionReturn(0);
33273982b651SMatthew G. Knepley }
33283982b651SMatthew G. Knepley 
3329d0fa310fSMatthew G. Knepley /*@
3330e6ccafaeSMatthew G Knepley   DMPlexCreateSubmesh - Extract a hypersurface from the mesh using vertices defined by a label
3331e6ccafaeSMatthew G Knepley 
3332e6ccafaeSMatthew G Knepley   Input Parameters:
3333e6ccafaeSMatthew G Knepley + dm           - The original mesh
3334158acfadSMatthew G. Knepley . vertexLabel  - The DMLabel marking points contained in the surface
3335158acfadSMatthew G. Knepley . value        - The label value to use
3336158acfadSMatthew G. Knepley - markedFaces  - PETSC_TRUE if surface faces are marked in addition to vertices, PETSC_FALSE if only vertices are marked
3337e6ccafaeSMatthew G Knepley 
3338e6ccafaeSMatthew G Knepley   Output Parameter:
3339e6ccafaeSMatthew G Knepley . subdm - The surface mesh
3340e6ccafaeSMatthew G Knepley 
3341e6ccafaeSMatthew G Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3342e6ccafaeSMatthew G Knepley 
3343e6ccafaeSMatthew G Knepley   Level: developer
3344e6ccafaeSMatthew G Knepley 
3345c58f1c22SToby Isaac .seealso: DMPlexGetSubpointMap(), DMGetLabel(), DMLabelSetValue()
3346830e53efSMatthew G. Knepley @*/
3347158acfadSMatthew G. Knepley PetscErrorCode DMPlexCreateSubmesh(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM *subdm)
3348e6ccafaeSMatthew G Knepley {
3349827c4036SVaclav Hapla   DMPlexInterpolatedFlag interpolated;
3350827c4036SVaclav Hapla   PetscInt       dim, cdim;
3351e6ccafaeSMatthew G Knepley   PetscErrorCode ierr;
3352e6ccafaeSMatthew G Knepley 
3353e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3354e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3355766ab985SMatthew G. Knepley   PetscValidPointer(subdm, 3);
3356c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
335782f516ccSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)dm), subdm);CHKERRQ(ierr);
3358e6ccafaeSMatthew G Knepley   ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr);
3359c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(*subdm, dim-1);CHKERRQ(ierr);
3360dd2763adSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3361dd2763adSMatthew G. Knepley   ierr = DMSetCoordinateDim(*subdm, cdim);CHKERRQ(ierr);
3362827c4036SVaclav Hapla   ierr = DMPlexIsInterpolated(dm, &interpolated);CHKERRQ(ierr);
3363827c4036SVaclav Hapla   if (interpolated == DMPLEX_INTERPOLATED_PARTIAL) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not for partially interpolated meshes");
3364827c4036SVaclav Hapla   if (interpolated) {
3365158acfadSMatthew G. Knepley     ierr = DMPlexCreateSubmesh_Interpolated(dm, vertexLabel, value, markedFaces, *subdm);CHKERRQ(ierr);
3366e6ccafaeSMatthew G Knepley   } else {
3367830e53efSMatthew G. Knepley     ierr = DMPlexCreateSubmesh_Uninterpolated(dm, vertexLabel, value, *subdm);CHKERRQ(ierr);
3368e6ccafaeSMatthew G Knepley   }
3369e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3370e6ccafaeSMatthew G Knepley }
3371e6ccafaeSMatthew G Knepley 
337227c04023SMatthew G. Knepley static PetscErrorCode DMPlexCreateCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM subdm)
3373766ab985SMatthew G. Knepley {
3374766ab985SMatthew G. Knepley   MPI_Comm        comm;
3375766ab985SMatthew G. Knepley   DMLabel         subpointMap;
3376766ab985SMatthew G. Knepley   IS              subvertexIS;
3377766ab985SMatthew G. Knepley   const PetscInt *subVertices;
3378fed694aaSMatthew G. Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells, *subCells = NULL;
3379766ab985SMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces, firstSubFace, newFacePoint, nFV;
3380*412e9a14SMatthew G. Knepley   PetscInt        c, f;
3381766ab985SMatthew G. Knepley   PetscErrorCode  ierr;
3382766ab985SMatthew G. Knepley 
3383766ab985SMatthew G. Knepley   PetscFunctionBegin;
3384766ab985SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
3385766ab985SMatthew G. Knepley   /* Create subpointMap which marks the submesh */
3386d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap);CHKERRQ(ierr);
3387766ab985SMatthew G. Knepley   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
3388766ab985SMatthew G. Knepley   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
338927c04023SMatthew G. Knepley   ierr = DMPlexMarkCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, subpointMap, &numSubFaces, &nFV, &subCells, subdm);CHKERRQ(ierr);
3390766ab985SMatthew G. Knepley   /* Setup chart */
3391766ab985SMatthew G. Knepley   ierr = DMLabelGetStratumSize(subpointMap, 0, &numSubVertices);CHKERRQ(ierr);
3392766ab985SMatthew G. Knepley   ierr = DMLabelGetStratumSize(subpointMap, 2, &numSubCells);CHKERRQ(ierr);
3393766ab985SMatthew G. Knepley   ierr = DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices);CHKERRQ(ierr);
3394766ab985SMatthew G. Knepley   ierr = DMPlexSetVTKCellHeight(subdm, 1);CHKERRQ(ierr);
3395766ab985SMatthew G. Knepley   /* Set cone sizes */
3396766ab985SMatthew G. Knepley   firstSubVertex = numSubCells;
3397766ab985SMatthew G. Knepley   firstSubFace   = numSubCells+numSubVertices;
3398766ab985SMatthew G. Knepley   newFacePoint   = firstSubFace;
3399766ab985SMatthew G. Knepley   ierr = DMLabelGetStratumIS(subpointMap, 0, &subvertexIS);CHKERRQ(ierr);
3400766ab985SMatthew G. Knepley   if (subvertexIS) {ierr = ISGetIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
3401766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3402766ab985SMatthew G. Knepley     ierr = DMPlexSetConeSize(subdm, c, 1);CHKERRQ(ierr);
3403766ab985SMatthew G. Knepley   }
3404766ab985SMatthew G. Knepley   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
3405766ab985SMatthew G. Knepley     ierr = DMPlexSetConeSize(subdm, f, nFV);CHKERRQ(ierr);
3406766ab985SMatthew G. Knepley   }
3407766ab985SMatthew G. Knepley   ierr = DMSetUp(subdm);CHKERRQ(ierr);
3408766ab985SMatthew G. Knepley   /* Create face cones */
3409766ab985SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
341069291d52SBarry Smith   ierr = DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
3411766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3412766ab985SMatthew G. Knepley     const PetscInt  cell    = subCells[c];
3413766ab985SMatthew G. Knepley     const PetscInt  subcell = c;
341487feddfdSMatthew G. Knepley     const PetscInt *cone, *cells;
3415*412e9a14SMatthew G. Knepley     PetscBool       isHybrid;
341687feddfdSMatthew G. Knepley     PetscInt        numCells, subVertex, p, v;
3417766ab985SMatthew G. Knepley 
3418*412e9a14SMatthew G. Knepley     ierr = DMPlexCellIsHybrid_Internal(dm, cell, &isHybrid);CHKERRQ(ierr);
3419*412e9a14SMatthew G. Knepley     if (!isHybrid) continue;
342087feddfdSMatthew G. Knepley     ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
342187feddfdSMatthew G. Knepley     for (v = 0; v < nFV; ++v) {
342287feddfdSMatthew G. Knepley       ierr = PetscFindInt(cone[v], numSubVertices, subVertices, &subVertex);CHKERRQ(ierr);
342387feddfdSMatthew G. Knepley       subface[v] = firstSubVertex+subVertex;
342487feddfdSMatthew G. Knepley     }
342587feddfdSMatthew G. Knepley     ierr = DMPlexSetCone(subdm, newFacePoint, subface);CHKERRQ(ierr);
342687feddfdSMatthew G. Knepley     ierr = DMPlexSetCone(subdm, subcell, &newFacePoint);CHKERRQ(ierr);
342787feddfdSMatthew G. Knepley     ierr = DMPlexGetJoin(dm, nFV, cone, &numCells, &cells);CHKERRQ(ierr);
342827234c99SMatthew G. Knepley     /* Not true in parallel
342927234c99SMatthew G. Knepley     if (numCells != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
343087feddfdSMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
343187feddfdSMatthew G. Knepley       PetscInt  negsubcell;
3432*412e9a14SMatthew G. Knepley       PetscBool isHybrid;
3433766ab985SMatthew G. Knepley 
3434*412e9a14SMatthew G. Knepley       ierr = DMPlexCellIsHybrid_Internal(dm, cells[p], &isHybrid);CHKERRQ(ierr);
3435*412e9a14SMatthew G. Knepley       if (isHybrid) continue;
343687feddfdSMatthew G. Knepley       /* I know this is a crap search */
343787feddfdSMatthew G. Knepley       for (negsubcell = 0; negsubcell < numSubCells; ++negsubcell) {
343887feddfdSMatthew G. Knepley         if (subCells[negsubcell] == cells[p]) break;
3439766ab985SMatthew G. Knepley       }
344087feddfdSMatthew G. Knepley       if (negsubcell == numSubCells) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find negative face neighbor for cohesive cell %d", cell);
344187feddfdSMatthew G. Knepley       ierr = DMPlexSetCone(subdm, negsubcell, &newFacePoint);CHKERRQ(ierr);
3442766ab985SMatthew G. Knepley     }
344387feddfdSMatthew G. Knepley     ierr = DMPlexRestoreJoin(dm, nFV, cone, &numCells, &cells);CHKERRQ(ierr);
344487feddfdSMatthew G. Knepley     ++newFacePoint;
3445766ab985SMatthew G. Knepley   }
344669291d52SBarry Smith   ierr = DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
3447766ab985SMatthew G. Knepley   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
3448766ab985SMatthew G. Knepley   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
3449766ab985SMatthew G. Knepley   /* Build coordinates */
3450766ab985SMatthew G. Knepley   {
3451766ab985SMatthew G. Knepley     PetscSection coordSection, subCoordSection;
3452766ab985SMatthew G. Knepley     Vec          coordinates, subCoordinates;
3453766ab985SMatthew G. Knepley     PetscScalar *coords, *subCoords;
3454c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize, v;
345524640c55SToby Isaac     const char  *name;
3456766ab985SMatthew G. Knepley 
3457c0e8cf5fSMatthew G. Knepley     ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
345869d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
3459766ab985SMatthew G. Knepley     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
346069d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
3461285d324eSMatthew G. Knepley     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
3462285d324eSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
3463285d324eSMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
3464766ab985SMatthew G. Knepley     ierr = PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices);CHKERRQ(ierr);
3465766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3466766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3467766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex+v;
3468766ab985SMatthew G. Knepley       PetscInt       dof;
3469766ab985SMatthew G. Knepley 
3470766ab985SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3471766ab985SMatthew G. Knepley       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
3472285d324eSMatthew G. Knepley       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
3473766ab985SMatthew G. Knepley     }
3474766ab985SMatthew G. Knepley     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
3475766ab985SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
34768b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &subCoordinates);CHKERRQ(ierr);
347724640c55SToby Isaac     ierr = PetscObjectGetName((PetscObject)coordinates,&name);CHKERRQ(ierr);
347824640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)subCoordinates,name);CHKERRQ(ierr);
3479766ab985SMatthew G. Knepley     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
3480c0e8cf5fSMatthew G. Knepley     ierr = VecSetBlockSize(subCoordinates, cdim);CHKERRQ(ierr);
34812eb5907fSJed Brown     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
3482766ab985SMatthew G. Knepley     ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
3483766ab985SMatthew G. Knepley     ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3484766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3485766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3486766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex+v;
3487766ab985SMatthew G. Knepley       PetscInt       dof, off, sdof, soff, d;
3488766ab985SMatthew G. Knepley 
3489766ab985SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3490766ab985SMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
3491766ab985SMatthew G. Knepley       ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
3492766ab985SMatthew G. Knepley       ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
3493766ab985SMatthew G. Knepley       if (dof != sdof) SETERRQ4(comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
3494766ab985SMatthew G. Knepley       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3495766ab985SMatthew G. Knepley     }
3496766ab985SMatthew G. Knepley     ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
3497766ab985SMatthew G. Knepley     ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3498766ab985SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
3499766ab985SMatthew G. Knepley     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
3500766ab985SMatthew G. Knepley   }
3501aca35d17SMatthew G. Knepley   /* Build SF */
3502aca35d17SMatthew G. Knepley   CHKMEMQ;
3503aca35d17SMatthew G. Knepley   {
3504aca35d17SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
3505aca35d17SMatthew G. Knepley     const PetscSFNode *remotePoints;
3506bdcf2095SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
3507aca35d17SMatthew G. Knepley     const PetscInt    *localPoints;
3508bdcf2095SMatthew G. Knepley     PetscInt          *slocalPoints;
350949c26ae4SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubRoots = numSubCells+numSubFaces+numSubVertices, numSubLeaves = 0, l, sl, ll, pStart, pEnd, p, vStart, vEnd;
3510bdcf2095SMatthew G. Knepley     PetscMPIInt        rank;
3511aca35d17SMatthew G. Knepley 
3512bdcf2095SMatthew G. Knepley     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
3513aca35d17SMatthew G. Knepley     ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
3514aca35d17SMatthew G. Knepley     ierr = DMGetPointSF(subdm, &sfPointSub);CHKERRQ(ierr);
3515aca35d17SMatthew G. Knepley     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3516aca35d17SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
3517aca35d17SMatthew G. Knepley     ierr = PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr);
3518aca35d17SMatthew G. Knepley     if (numRoots >= 0) {
3519aca35d17SMatthew G. Knepley       /* Only vertices should be shared */
3520dcca6d9dSJed Brown       ierr = PetscMalloc2(pEnd-pStart,&newLocalPoints,numRoots,&newOwners);CHKERRQ(ierr);
3521bdcf2095SMatthew G. Knepley       for (p = 0; p < pEnd-pStart; ++p) {
3522bdcf2095SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
3523bdcf2095SMatthew G. Knepley         newLocalPoints[p].index = -2;
3524bdcf2095SMatthew G. Knepley       }
35259e0823b2SMatthew G. Knepley       /* Set subleaves */
3526aca35d17SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
3527aca35d17SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3528bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3529aca35d17SMatthew G. Knepley 
3530aca35d17SMatthew G. Knepley         if ((point < vStart) && (point >= vEnd)) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Should not be mapping anything but vertices, %d", point);
3531bdcf2095SMatthew G. Knepley         if (subPoint < 0) continue;
3532bdcf2095SMatthew G. Knepley         newLocalPoints[point-pStart].rank  = rank;
3533bdcf2095SMatthew G. Knepley         newLocalPoints[point-pStart].index = subPoint;
3534bdcf2095SMatthew G. Knepley         ++numSubLeaves;
3535aca35d17SMatthew G. Knepley       }
35369e0823b2SMatthew G. Knepley       /* Must put in owned subpoints */
35379e0823b2SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
35389e0823b2SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(p, firstSubVertex, numSubVertices, subVertices);
35399e0823b2SMatthew G. Knepley 
35409e0823b2SMatthew G. Knepley         if (subPoint < 0) {
35419e0823b2SMatthew G. Knepley           newOwners[p-pStart].rank  = -3;
35429e0823b2SMatthew G. Knepley           newOwners[p-pStart].index = -3;
35439e0823b2SMatthew G. Knepley         } else {
35449e0823b2SMatthew G. Knepley           newOwners[p-pStart].rank  = rank;
35459e0823b2SMatthew G. Knepley           newOwners[p-pStart].index = subPoint;
35469e0823b2SMatthew G. Knepley         }
3547bdcf2095SMatthew G. Knepley       }
3548bdcf2095SMatthew G. Knepley       ierr = PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
3549bdcf2095SMatthew G. Knepley       ierr = PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
3550bdcf2095SMatthew G. Knepley       ierr = PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr);
3551bdcf2095SMatthew G. Knepley       ierr = PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr);
3552785e854fSJed Brown       ierr = PetscMalloc1(numSubLeaves,    &slocalPoints);CHKERRQ(ierr);
3553785e854fSJed Brown       ierr = PetscMalloc1(numSubLeaves, &sremotePoints);CHKERRQ(ierr);
355449c26ae4SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
3555bdcf2095SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3556bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3557aca35d17SMatthew G. Knepley 
3558aca35d17SMatthew G. Knepley         if (subPoint < 0) continue;
355949c26ae4SMatthew G. Knepley         if (newLocalPoints[point].rank == rank) {++ll; continue;}
3560aca35d17SMatthew G. Knepley         slocalPoints[sl]        = subPoint;
3561bdcf2095SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
3562bdcf2095SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
3563bdcf2095SMatthew G. Knepley         if (sremotePoints[sl].rank  < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %d", point);
3564bdcf2095SMatthew G. Knepley         if (sremotePoints[sl].index < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %d", point);
3565aca35d17SMatthew G. Knepley         ++sl;
3566aca35d17SMatthew G. Knepley       }
3567bdcf2095SMatthew G. Knepley       ierr = PetscFree2(newLocalPoints,newOwners);CHKERRQ(ierr);
356849c26ae4SMatthew G. Knepley       if (sl + ll != numSubLeaves) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %d + %d != %d", sl, ll, numSubLeaves);
356949c26ae4SMatthew G. Knepley       ierr = PetscSFSetGraph(sfPointSub, numSubRoots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
3570aca35d17SMatthew G. Knepley     }
3571aca35d17SMatthew G. Knepley   }
3572aca35d17SMatthew G. Knepley   CHKMEMQ;
3573766ab985SMatthew G. Knepley   /* Cleanup */
3574766ab985SMatthew G. Knepley   if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
3575766ab985SMatthew G. Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
3576766ab985SMatthew G. Knepley   ierr = PetscFree(subCells);CHKERRQ(ierr);
3577766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
3578766ab985SMatthew G. Knepley }
3579766ab985SMatthew G. Knepley 
35803982b651SMatthew G. Knepley static PetscErrorCode DMPlexCreateCohesiveSubmesh_Interpolated(DM dm, const char labelname[], PetscInt value, DM subdm)
3581766ab985SMatthew G. Knepley {
35823982b651SMatthew G. Knepley   DMLabel        label = NULL;
3583766ab985SMatthew G. Knepley   PetscErrorCode ierr;
3584766ab985SMatthew G. Knepley 
3585766ab985SMatthew G. Knepley   PetscFunctionBegin;
3586c58f1c22SToby Isaac   if (labelname) {ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);}
3587158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmeshGeneric_Interpolated(dm, label, value, PETSC_FALSE, PETSC_TRUE, 1, subdm);CHKERRQ(ierr);
3588766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
3589766ab985SMatthew G. Knepley }
3590766ab985SMatthew G. Knepley 
3591c08575a3SMatthew G. Knepley /*@C
359227c04023SMatthew G. Knepley   DMPlexCreateCohesiveSubmesh - Extract from a mesh with cohesive cells the hypersurface defined by one face of the cells. Optionally, a Label an be given to restrict the cells.
3593766ab985SMatthew G. Knepley 
3594766ab985SMatthew G. Knepley   Input Parameters:
3595766ab985SMatthew G. Knepley + dm          - The original mesh
359627c04023SMatthew G. Knepley . hasLagrange - The mesh has Lagrange unknowns in the cohesive cells
35977afc1a8bSJed Brown . label       - A label name, or NULL
359827c04023SMatthew G. Knepley - value  - A label value
3599766ab985SMatthew G. Knepley 
3600766ab985SMatthew G. Knepley   Output Parameter:
3601766ab985SMatthew G. Knepley . subdm - The surface mesh
3602766ab985SMatthew G. Knepley 
3603766ab985SMatthew G. Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3604766ab985SMatthew G. Knepley 
3605766ab985SMatthew G. Knepley   Level: developer
3606766ab985SMatthew G. Knepley 
3607766ab985SMatthew G. Knepley .seealso: DMPlexGetSubpointMap(), DMPlexCreateSubmesh()
3608c08575a3SMatthew G. Knepley @*/
360927c04023SMatthew G. Knepley PetscErrorCode DMPlexCreateCohesiveSubmesh(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM *subdm)
3610766ab985SMatthew G. Knepley {
3611c0e8cf5fSMatthew G. Knepley   PetscInt       dim, cdim, depth;
3612766ab985SMatthew G. Knepley   PetscErrorCode ierr;
3613766ab985SMatthew G. Knepley 
3614766ab985SMatthew G. Knepley   PetscFunctionBegin;
3615766ab985SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
361627c04023SMatthew G. Knepley   PetscValidPointer(subdm, 5);
3617c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3618766ab985SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3619766ab985SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), subdm);CHKERRQ(ierr);
3620766ab985SMatthew G. Knepley   ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr);
3621c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(*subdm, dim-1);CHKERRQ(ierr);
3622c0e8cf5fSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3623c0e8cf5fSMatthew G. Knepley   ierr = DMSetCoordinateDim(*subdm, cdim);CHKERRQ(ierr);
3624766ab985SMatthew G. Knepley   if (depth == dim) {
3625b3154360SMatthew G. Knepley     ierr = DMPlexCreateCohesiveSubmesh_Interpolated(dm, label, value, *subdm);CHKERRQ(ierr);
3626766ab985SMatthew G. Knepley   } else {
362727c04023SMatthew G. Knepley     ierr = DMPlexCreateCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, *subdm);CHKERRQ(ierr);
3628e6ccafaeSMatthew G Knepley   }
3629e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3630e6ccafaeSMatthew G Knepley }
3631e6ccafaeSMatthew G Knepley 
3632bec263e5SMatthew G. Knepley /*@
3633bec263e5SMatthew G. Knepley   DMPlexFilter - Extract a subset of mesh cells defined by a label as a separate mesh
3634bec263e5SMatthew G. Knepley 
3635bec263e5SMatthew G. Knepley   Input Parameters:
3636bec263e5SMatthew G. Knepley + dm        - The original mesh
3637bec263e5SMatthew G. Knepley . cellLabel - The DMLabel marking cells contained in the new mesh
3638bec263e5SMatthew G. Knepley - value     - The label value to use
3639bec263e5SMatthew G. Knepley 
3640bec263e5SMatthew G. Knepley   Output Parameter:
3641bec263e5SMatthew G. Knepley . subdm - The new mesh
3642bec263e5SMatthew G. Knepley 
3643bec263e5SMatthew G. Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3644bec263e5SMatthew G. Knepley 
3645bec263e5SMatthew G. Knepley   Level: developer
3646bec263e5SMatthew G. Knepley 
3647c58f1c22SToby Isaac .seealso: DMPlexGetSubpointMap(), DMGetLabel(), DMLabelSetValue()
3648bec263e5SMatthew G. Knepley @*/
3649bec263e5SMatthew G. Knepley PetscErrorCode DMPlexFilter(DM dm, DMLabel cellLabel, PetscInt value, DM *subdm)
3650bec263e5SMatthew G. Knepley {
3651bec263e5SMatthew G. Knepley   PetscInt       dim;
3652bec263e5SMatthew G. Knepley   PetscErrorCode ierr;
3653bec263e5SMatthew G. Knepley 
3654bec263e5SMatthew G. Knepley   PetscFunctionBegin;
3655bec263e5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3656bec263e5SMatthew G. Knepley   PetscValidPointer(subdm, 3);
3657bec263e5SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3658bec263e5SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), subdm);CHKERRQ(ierr);
3659bec263e5SMatthew G. Knepley   ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr);
3660bec263e5SMatthew G. Knepley   ierr = DMSetDimension(*subdm, dim);CHKERRQ(ierr);
3661bec263e5SMatthew G. Knepley   /* Extract submesh in place, could be empty on some procs, could have inconsistency if procs do not both extract a shared cell */
3662158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmeshGeneric_Interpolated(dm, cellLabel, value, PETSC_FALSE, PETSC_FALSE, 0, *subdm);CHKERRQ(ierr);
3663bec263e5SMatthew G. Knepley   PetscFunctionReturn(0);
3664bec263e5SMatthew G. Knepley }
3665bec263e5SMatthew G. Knepley 
366664beef6dSMatthew G. Knepley /*@
366764beef6dSMatthew G. Knepley   DMPlexGetSubpointMap - Returns a DMLabel with point dimension as values
366864beef6dSMatthew G. Knepley 
366964beef6dSMatthew G. Knepley   Input Parameter:
367064beef6dSMatthew G. Knepley . dm - The submesh DM
367164beef6dSMatthew G. Knepley 
367264beef6dSMatthew G. Knepley   Output Parameter:
367364beef6dSMatthew G. Knepley . subpointMap - The DMLabel of all the points from the original mesh in this submesh, or NULL if this is not a submesh
367464beef6dSMatthew G. Knepley 
367564beef6dSMatthew G. Knepley   Level: developer
367664beef6dSMatthew G. Knepley 
367764beef6dSMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexCreateSubpointIS()
367864beef6dSMatthew G. Knepley @*/
3679e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexGetSubpointMap(DM dm, DMLabel *subpointMap)
3680e6ccafaeSMatthew G Knepley {
3681e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3682e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3683e6ccafaeSMatthew G Knepley   PetscValidPointer(subpointMap, 2);
368465663942SMatthew G. Knepley   *subpointMap = ((DM_Plex*) dm->data)->subpointMap;
3685e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3686e6ccafaeSMatthew G Knepley }
3687e6ccafaeSMatthew G Knepley 
3688c08575a3SMatthew G. Knepley /*@
3689c08575a3SMatthew G. Knepley   DMPlexSetSubpointMap - Sets the DMLabel with point dimension as values
3690c08575a3SMatthew G. Knepley 
3691c08575a3SMatthew G. Knepley   Input Parameters:
3692c08575a3SMatthew G. Knepley + dm - The submesh DM
3693c08575a3SMatthew G. Knepley - subpointMap - The DMLabel of all the points from the original mesh in this submesh
3694c08575a3SMatthew G. Knepley 
3695c08575a3SMatthew G. Knepley   Note: Should normally not be called by the user, since it is set in DMPlexCreateSubmesh()
3696c08575a3SMatthew G. Knepley 
3697c08575a3SMatthew G. Knepley   Level: developer
3698c08575a3SMatthew G. Knepley 
3699c08575a3SMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexCreateSubpointIS()
3700c08575a3SMatthew G. Knepley @*/
3701e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexSetSubpointMap(DM dm, DMLabel subpointMap)
3702e6ccafaeSMatthew G Knepley {
3703e6ccafaeSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
3704285d324eSMatthew G. Knepley   DMLabel        tmp;
3705e6ccafaeSMatthew G Knepley   PetscErrorCode ierr;
3706e6ccafaeSMatthew G Knepley 
3707e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3708e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3709285d324eSMatthew G. Knepley   tmp  = mesh->subpointMap;
3710e6ccafaeSMatthew G Knepley   mesh->subpointMap = subpointMap;
3711d67d17b1SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) mesh->subpointMap);CHKERRQ(ierr);
3712285d324eSMatthew G. Knepley   ierr = DMLabelDestroy(&tmp);CHKERRQ(ierr);
3713e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3714e6ccafaeSMatthew G Knepley }
3715e6ccafaeSMatthew G Knepley 
371664beef6dSMatthew G. Knepley /*@
3717e6ccafaeSMatthew G Knepley   DMPlexCreateSubpointIS - Creates an IS covering the entire subdm chart with the original points as data
3718e6ccafaeSMatthew G Knepley 
3719e6ccafaeSMatthew G Knepley   Input Parameter:
3720e6ccafaeSMatthew G Knepley . dm - The submesh DM
3721e6ccafaeSMatthew G Knepley 
3722e6ccafaeSMatthew G Knepley   Output Parameter:
37230298fd71SBarry Smith . subpointIS - The IS of all the points from the original mesh in this submesh, or NULL if this is not a submesh
3724e6ccafaeSMatthew G Knepley 
37253982b651SMatthew G. Knepley   Note: This IS is guaranteed to be sorted by the construction of the submesh
372664beef6dSMatthew G. Knepley 
372764beef6dSMatthew G. Knepley   Level: developer
372864beef6dSMatthew G. Knepley 
372964beef6dSMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointMap()
373064beef6dSMatthew G. Knepley @*/
3731e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexCreateSubpointIS(DM dm, IS *subpointIS)
3732e6ccafaeSMatthew G Knepley {
373382f516ccSBarry Smith   MPI_Comm        comm;
3734e6ccafaeSMatthew G Knepley   DMLabel         subpointMap;
3735e6ccafaeSMatthew G Knepley   IS              is;
3736e6ccafaeSMatthew G Knepley   const PetscInt *opoints;
3737e6ccafaeSMatthew G Knepley   PetscInt       *points, *depths;
3738e6ccafaeSMatthew G Knepley   PetscInt        depth, depStart, depEnd, d, pStart, pEnd, p, n, off;
3739e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
3740e6ccafaeSMatthew G Knepley 
3741e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3742e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3743e6ccafaeSMatthew G Knepley   PetscValidPointer(subpointIS, 2);
374482f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
37450298fd71SBarry Smith   *subpointIS = NULL;
3746e6ccafaeSMatthew G Knepley   ierr = DMPlexGetSubpointMap(dm, &subpointMap);CHKERRQ(ierr);
3747e6ccafaeSMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3748fed694aaSMatthew G. Knepley   if (subpointMap && depth >= 0) {
3749e6ccafaeSMatthew G Knepley     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3750e6ccafaeSMatthew G Knepley     if (pStart) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Submeshes must start the point numbering at 0, not %d", pStart);
375169291d52SBarry Smith     ierr = DMGetWorkArray(dm, depth+1, MPIU_INT, &depths);CHKERRQ(ierr);
3752e6ccafaeSMatthew G Knepley     depths[0] = depth;
3753e6ccafaeSMatthew G Knepley     depths[1] = 0;
3754e6ccafaeSMatthew G Knepley     for(d = 2; d <= depth; ++d) {depths[d] = depth+1 - d;}
3755785e854fSJed Brown     ierr = PetscMalloc1(pEnd, &points);CHKERRQ(ierr);
3756e6ccafaeSMatthew G Knepley     for(d = 0, off = 0; d <= depth; ++d) {
3757e6ccafaeSMatthew G Knepley       const PetscInt dep = depths[d];
3758e6ccafaeSMatthew G Knepley 
3759e6ccafaeSMatthew G Knepley       ierr = DMPlexGetDepthStratum(dm, dep, &depStart, &depEnd);CHKERRQ(ierr);
3760e6ccafaeSMatthew G Knepley       ierr = DMLabelGetStratumSize(subpointMap, dep, &n);CHKERRQ(ierr);
37611e21b6fdSMatthew G Knepley       if (((d < 2) && (depth > 1)) || (d == 1)) { /* Only check vertices and cells for now since the map is broken for others */
3762e6ccafaeSMatthew G Knepley         if (n != depEnd-depStart) SETERRQ3(comm, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %d at depth %d should be %d", n, dep, depEnd-depStart);
3763e6ccafaeSMatthew G Knepley       } else {
37641e21b6fdSMatthew G Knepley         if (!n) {
37651e21b6fdSMatthew G Knepley           if (d == 0) {
37661e21b6fdSMatthew G Knepley             /* Missing cells */
37671e21b6fdSMatthew G Knepley             for(p = 0; p < depEnd-depStart; ++p, ++off) points[off] = -1;
37681e21b6fdSMatthew G Knepley           } else {
37691e21b6fdSMatthew G Knepley             /* Missing faces */
37701e21b6fdSMatthew G Knepley             for(p = 0; p < depEnd-depStart; ++p, ++off) points[off] = PETSC_MAX_INT;
37711e21b6fdSMatthew G Knepley           }
37721e21b6fdSMatthew G Knepley         }
3773e6ccafaeSMatthew G Knepley       }
3774e6ccafaeSMatthew G Knepley       if (n) {
3775e6ccafaeSMatthew G Knepley         ierr = DMLabelGetStratumIS(subpointMap, dep, &is);CHKERRQ(ierr);
3776e6ccafaeSMatthew G Knepley         ierr = ISGetIndices(is, &opoints);CHKERRQ(ierr);
3777e6ccafaeSMatthew G Knepley         for(p = 0; p < n; ++p, ++off) points[off] = opoints[p];
3778e6ccafaeSMatthew G Knepley         ierr = ISRestoreIndices(is, &opoints);CHKERRQ(ierr);
3779e6ccafaeSMatthew G Knepley         ierr = ISDestroy(&is);CHKERRQ(ierr);
3780e6ccafaeSMatthew G Knepley       }
3781e6ccafaeSMatthew G Knepley     }
378269291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, depth+1, MPIU_INT, &depths);CHKERRQ(ierr);
3783e6ccafaeSMatthew G Knepley     if (off != pEnd) SETERRQ2(comm, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %d should be %d", off, pEnd);
3784fed694aaSMatthew G. Knepley     ierr = ISCreateGeneral(PETSC_COMM_SELF, pEnd, points, PETSC_OWN_POINTER, subpointIS);CHKERRQ(ierr);
3785e6ccafaeSMatthew G Knepley   }
3786e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3787e6ccafaeSMatthew G Knepley }
3788559a1558SMatthew G. Knepley 
3789559a1558SMatthew G. Knepley /*@
3790a6e0b375SMatthew G. Knepley   DMGetEnclosureRelation - Get the relationship between dmA and dmB
3791559a1558SMatthew G. Knepley 
3792559a1558SMatthew G. Knepley   Input Parameters:
3793a6e0b375SMatthew G. Knepley + dmA - The first DM
3794a6e0b375SMatthew G. Knepley - dmB - The second DM
3795559a1558SMatthew G. Knepley 
3796559a1558SMatthew G. Knepley   Output Parameter:
3797a6e0b375SMatthew G. Knepley . rel - The relation of dmA to dmB
3798559a1558SMatthew G. Knepley 
3799a6e0b375SMatthew G. Knepley   Level: intermediate
3800559a1558SMatthew G. Knepley 
3801a6e0b375SMatthew G. Knepley .seealso: DMPlexGetEnclosurePoint()
3802559a1558SMatthew G. Knepley @*/
3803a6e0b375SMatthew G. Knepley PetscErrorCode DMGetEnclosureRelation(DM dmA, DM dmB, DMEnclosureType *rel)
3804559a1558SMatthew G. Knepley {
3805a6e0b375SMatthew G. Knepley   DM             plexA, plexB, sdm;
3806559a1558SMatthew G. Knepley   DMLabel        spmap;
3807a6e0b375SMatthew G. Knepley   PetscInt       pStartA, pEndA, pStartB, pEndB, NpA, NpB;
3808559a1558SMatthew G. Knepley   PetscErrorCode ierr;
3809559a1558SMatthew G. Knepley 
381044171101SMatthew G. Knepley   PetscFunctionBegin;
3811a6e0b375SMatthew G. Knepley   PetscValidPointer(rel, 3);
3812a6e0b375SMatthew G. Knepley   *rel = DM_ENC_NONE;
3813a6e0b375SMatthew G. Knepley   if (!dmA || !dmB) PetscFunctionReturn(0);
3814a6e0b375SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
3815a6e0b375SMatthew G. Knepley   PetscValidHeaderSpecific(dmB, DM_CLASSID, 1);
3816a6e0b375SMatthew G. Knepley   if (dmA == dmB) {*rel = DM_ENC_EQUALITY; PetscFunctionReturn(0);}
3817a6e0b375SMatthew G. Knepley   ierr = DMConvert(dmA, DMPLEX, &plexA);CHKERRQ(ierr);
3818a6e0b375SMatthew G. Knepley   ierr = DMConvert(dmB, DMPLEX, &plexB);CHKERRQ(ierr);
3819a6e0b375SMatthew G. Knepley   ierr = DMPlexGetChart(plexA, &pStartA, &pEndA);CHKERRQ(ierr);
3820a6e0b375SMatthew G. Knepley   ierr = DMPlexGetChart(plexB, &pStartB, &pEndB);CHKERRQ(ierr);
3821a6e0b375SMatthew G. Knepley   /* Assumption 1: subDMs have smaller charts than the DMs that they originate from
3822a6e0b375SMatthew G. Knepley     - The degenerate case of a subdomain which includes all of the domain on some process can be treated as equality */
3823a6e0b375SMatthew G. Knepley   if ((pStartA == pStartB) && (pEndA == pEndB)) {
3824a6e0b375SMatthew G. Knepley     *rel = DM_ENC_EQUALITY;
3825a6e0b375SMatthew G. Knepley     goto end;
3826559a1558SMatthew G. Knepley   }
3827a6e0b375SMatthew G. Knepley   NpA = pEndA - pStartA;
3828a6e0b375SMatthew G. Knepley   NpB = pEndB - pStartB;
3829a6e0b375SMatthew G. Knepley   if (NpA == NpB) goto end;
3830a6e0b375SMatthew G. Knepley   sdm = NpA > NpB ? plexB : plexA; /* The other is the original, enclosing dm */
3831a6e0b375SMatthew G. Knepley   ierr = DMPlexGetSubpointMap(sdm, &spmap);CHKERRQ(ierr);
3832a6e0b375SMatthew G. Knepley   if (!spmap) goto end;
3833a6e0b375SMatthew G. Knepley   /* TODO Check the space mapped to by subpointMap is same size as dm */
3834a6e0b375SMatthew G. Knepley   if (NpA > NpB) {
3835a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUPERMESH;
3836a6e0b375SMatthew G. Knepley   } else {
3837a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUBMESH;
3838a6e0b375SMatthew G. Knepley   }
3839a6e0b375SMatthew G. Knepley   end:
3840a6e0b375SMatthew G. Knepley   ierr = DMDestroy(&plexA);CHKERRQ(ierr);
3841a6e0b375SMatthew G. Knepley   ierr = DMDestroy(&plexB);CHKERRQ(ierr);
3842559a1558SMatthew G. Knepley   PetscFunctionReturn(0);
3843559a1558SMatthew G. Knepley }
384444171101SMatthew G. Knepley 
384544171101SMatthew G. Knepley /*@
3846a6e0b375SMatthew G. Knepley   DMGetEnclosurePoint - Get the point pA in dmA which corresponds to the point pB in dmB
384744171101SMatthew G. Knepley 
384844171101SMatthew G. Knepley   Input Parameters:
3849a6e0b375SMatthew G. Knepley + dmA   - The first DM
3850a6e0b375SMatthew G. Knepley . dmB   - The second DM
3851a6e0b375SMatthew G. Knepley . etype - The type of enclosure relation that dmA has to dmB
3852a6e0b375SMatthew G. Knepley - pB    - A point of dmB
385344171101SMatthew G. Knepley 
385444171101SMatthew G. Knepley   Output Parameter:
3855a6e0b375SMatthew G. Knepley . pA    - The corresponding point of dmA
385644171101SMatthew G. Knepley 
3857a6e0b375SMatthew G. Knepley   Level: intermediate
385844171101SMatthew G. Knepley 
3859a6e0b375SMatthew G. Knepley .seealso: DMGetEnclosureRelation()
386044171101SMatthew G. Knepley @*/
3861a6e0b375SMatthew G. Knepley PetscErrorCode DMGetEnclosurePoint(DM dmA, DM dmB, DMEnclosureType etype, PetscInt pB, PetscInt *pA)
386244171101SMatthew G. Knepley {
3863a6e0b375SMatthew G. Knepley   DM              sdm;
386444171101SMatthew G. Knepley   DMLabel         spmap;
3865a6e0b375SMatthew G. Knepley   IS              subpointIS;
3866a6e0b375SMatthew G. Knepley   const PetscInt *subpoints;
3867a6e0b375SMatthew G. Knepley   PetscInt        numSubpoints;
386844171101SMatthew G. Knepley   PetscErrorCode  ierr;
386944171101SMatthew G. Knepley 
387044171101SMatthew G. Knepley   PetscFunctionBegin;
3871a6e0b375SMatthew G. Knepley   /* TODO Cache the IS, making it look like an index */
3872a6e0b375SMatthew G. Knepley   switch (etype) {
3873a6e0b375SMatthew G. Knepley     case DM_ENC_SUPERMESH:
3874a6e0b375SMatthew G. Knepley     sdm  = dmB;
3875a6e0b375SMatthew G. Knepley     ierr = DMPlexGetSubpointMap(sdm, &spmap);CHKERRQ(ierr);
3876a6e0b375SMatthew G. Knepley     ierr = DMPlexCreateSubpointIS(sdm, &subpointIS);CHKERRQ(ierr);
3877a6e0b375SMatthew G. Knepley     ierr = ISGetIndices(subpointIS, &subpoints);CHKERRQ(ierr);
3878a6e0b375SMatthew G. Knepley     *pA  = subpoints[pB];
3879a6e0b375SMatthew G. Knepley     ierr = ISRestoreIndices(subpointIS, &subpoints);CHKERRQ(ierr);
3880a6e0b375SMatthew G. Knepley     ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
3881a6e0b375SMatthew G. Knepley     break;
3882a6e0b375SMatthew G. Knepley     case DM_ENC_SUBMESH:
3883a6e0b375SMatthew G. Knepley     sdm  = dmA;
3884a6e0b375SMatthew G. Knepley     ierr = DMPlexGetSubpointMap(sdm, &spmap);CHKERRQ(ierr);
3885a6e0b375SMatthew G. Knepley     ierr = DMPlexCreateSubpointIS(sdm, &subpointIS);CHKERRQ(ierr);
3886a6e0b375SMatthew G. Knepley     ierr = ISGetLocalSize(subpointIS, &numSubpoints);CHKERRQ(ierr);
3887a6e0b375SMatthew G. Knepley     ierr = ISGetIndices(subpointIS, &subpoints);CHKERRQ(ierr);
3888a6e0b375SMatthew G. Knepley     ierr = PetscFindInt(pB, numSubpoints, subpoints, pA);CHKERRQ(ierr);
3889a6e0b375SMatthew G. Knepley     if (*pA < 0) {
3890a6e0b375SMatthew G. Knepley       ierr = DMViewFromOptions(dmA, NULL, "-dm_enc_A_view");CHKERRQ(ierr);
3891a6e0b375SMatthew G. Knepley       ierr = DMViewFromOptions(dmB, NULL, "-dm_enc_B_view");CHKERRQ(ierr);
3892a6e0b375SMatthew G. Knepley       SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %d not found in submesh", pB);
3893a6e0b375SMatthew G. Knepley     }
3894a6e0b375SMatthew G. Knepley     ierr = ISRestoreIndices(subpointIS, &subpoints);CHKERRQ(ierr);
3895a6e0b375SMatthew G. Knepley     ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
3896a6e0b375SMatthew G. Knepley     break;
3897a6e0b375SMatthew G. Knepley     case DM_ENC_EQUALITY:
3898a6e0b375SMatthew G. Knepley     case DM_ENC_NONE:
3899a6e0b375SMatthew G. Knepley     *pA = pB;break;
3900a6e0b375SMatthew G. Knepley     case DM_ENC_UNKNOWN:
3901a6e0b375SMatthew G. Knepley     {
3902a6e0b375SMatthew G. Knepley       DMEnclosureType enc;
390344171101SMatthew G. Knepley 
3904a6e0b375SMatthew G. Knepley       ierr = DMGetEnclosureRelation(dmA, dmB, &enc);CHKERRQ(ierr);
3905a6e0b375SMatthew G. Knepley       ierr = DMGetEnclosurePoint(dmA, dmB, enc, pB, pA);CHKERRQ(ierr);
3906a6e0b375SMatthew G. Knepley     }
3907a6e0b375SMatthew G. Knepley     break;
3908a6e0b375SMatthew G. Knepley     default: SETERRQ1(PetscObjectComm((PetscObject) dmA), PETSC_ERR_ARG_OUTOFRANGE, "Invalid enclosure type %d", (int) etype);
390944171101SMatthew G. Knepley   }
391044171101SMatthew G. Knepley   PetscFunctionReturn(0);
391144171101SMatthew G. Knepley }
3912