xref: /petsc/src/dm/impls/plex/plexsubmesh.c (revision e600fa544e2bb197ca2af9b6e65ea465976dec56)
1af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h>    /*I      "petscdmplex.h"    I*/
2c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h>   /*I      "petscdmlabel.h"   I*/
30c312b8eSJed Brown #include <petscsf.h>
4e6ccafaeSMatthew G Knepley 
5412e9a14SMatthew G. Knepley static PetscErrorCode DMPlexCellIsHybrid_Internal(DM dm, PetscInt p, PetscBool *isHybrid)
6412e9a14SMatthew G. Knepley {
7412e9a14SMatthew G. Knepley   DMPolytopeType ct;
8412e9a14SMatthew G. Knepley   PetscErrorCode ierr;
9412e9a14SMatthew G. Knepley 
10412e9a14SMatthew G. Knepley   PetscFunctionBegin;
11412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellType(dm, p, &ct);CHKERRQ(ierr);
12412e9a14SMatthew G. Knepley   switch (ct) {
13412e9a14SMatthew G. Knepley     case DM_POLYTOPE_POINT_PRISM_TENSOR:
14412e9a14SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
15412e9a14SMatthew G. Knepley     case DM_POLYTOPE_TRI_PRISM_TENSOR:
16412e9a14SMatthew G. Knepley     case DM_POLYTOPE_QUAD_PRISM_TENSOR:
17412e9a14SMatthew G. Knepley       *isHybrid = PETSC_TRUE;
18412e9a14SMatthew G. Knepley     default: *isHybrid = PETSC_FALSE;
19412e9a14SMatthew G. Knepley   }
20412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
21412e9a14SMatthew G. Knepley }
22412e9a14SMatthew G. Knepley 
23412e9a14SMatthew G. Knepley static PetscErrorCode DMPlexGetTensorPrismBounds_Internal(DM dm, PetscInt dim, PetscInt *cStart, PetscInt *cEnd)
24412e9a14SMatthew G. Knepley {
25412e9a14SMatthew G. Knepley   DMLabel        ctLabel;
26412e9a14SMatthew G. Knepley   PetscErrorCode ierr;
27412e9a14SMatthew G. Knepley 
28412e9a14SMatthew G. Knepley   PetscFunctionBegin;
29412e9a14SMatthew G. Knepley   if (cStart) *cStart = -1;
30412e9a14SMatthew G. Knepley   if (cEnd)   *cEnd   = -1;
31412e9a14SMatthew G. Knepley   ierr = DMPlexGetCellTypeLabel(dm, &ctLabel);CHKERRQ(ierr);
32412e9a14SMatthew G. Knepley   switch (dim) {
33412e9a14SMatthew G. Knepley     case 1: ierr = DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_POINT_PRISM_TENSOR, cStart, cEnd);CHKERRQ(ierr);break;
34412e9a14SMatthew G. Knepley     case 2: ierr = DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_SEG_PRISM_TENSOR, cStart, cEnd);CHKERRQ(ierr);break;
35412e9a14SMatthew G. Knepley     case 3:
36412e9a14SMatthew G. Knepley       ierr = DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_TRI_PRISM_TENSOR, cStart, cEnd);CHKERRQ(ierr);
37412e9a14SMatthew G. Knepley       if (*cStart < 0) {ierr = DMLabelGetStratumBounds(ctLabel, DM_POLYTOPE_QUAD_PRISM_TENSOR, cStart, cEnd);CHKERRQ(ierr);}
38412e9a14SMatthew G. Knepley       break;
39412e9a14SMatthew G. Knepley     default: PetscFunctionReturn(0);
40412e9a14SMatthew G. Knepley   }
41412e9a14SMatthew G. Knepley   PetscFunctionReturn(0);
42412e9a14SMatthew G. Knepley }
43412e9a14SMatthew 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 
82d8d19677SJose E. Roman   Input Parameters:
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);
1012c71b3e2SJacob Faibussowitsch   PetscCheckFalse(flg != DMPLEX_INTERPOLATED_FULL,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     /* Pull point contributions from remote leaves into local roots */
15626279d81SSanderA     ierr = DMLabelGather(label, sfPoint, &lblLeaves);CHKERRQ(ierr);
15726279d81SSanderA     ierr = DMLabelGetValueIS(lblLeaves, &valueIS);CHKERRQ(ierr);
15826279d81SSanderA     ierr = ISGetLocalSize(valueIS, &numValues);CHKERRQ(ierr);
15926279d81SSanderA     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
16026279d81SSanderA     for (v = 0; v < numValues; ++v) {
16126279d81SSanderA       const PetscInt value = values[v];
16226279d81SSanderA 
16326279d81SSanderA       ierr = DMLabelGetStratumIS(lblLeaves, value, &pointIS);CHKERRQ(ierr);
16426279d81SSanderA       ierr = DMLabelInsertIS(label, pointIS, value);CHKERRQ(ierr);
16526279d81SSanderA       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
16626279d81SSanderA     }
16726279d81SSanderA     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
16826279d81SSanderA     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
16926279d81SSanderA     ierr = DMLabelDestroy(&lblLeaves);CHKERRQ(ierr);
17026279d81SSanderA     /* Push point contributions from roots into remote leaves */
17126279d81SSanderA     ierr = DMLabelDistribute(label, sfPoint, &lblRoots);CHKERRQ(ierr);
17226279d81SSanderA     ierr = DMLabelGetValueIS(lblRoots, &valueIS);CHKERRQ(ierr);
17326279d81SSanderA     ierr = ISGetLocalSize(valueIS, &numValues);CHKERRQ(ierr);
17426279d81SSanderA     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
17526279d81SSanderA     for (v = 0; v < numValues; ++v) {
17626279d81SSanderA       const PetscInt value = values[v];
17726279d81SSanderA 
17826279d81SSanderA       ierr = DMLabelGetStratumIS(lblRoots, value, &pointIS);CHKERRQ(ierr);
17926279d81SSanderA       ierr = DMLabelInsertIS(label, pointIS, value);CHKERRQ(ierr);
18026279d81SSanderA       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
18126279d81SSanderA     }
18226279d81SSanderA     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
18326279d81SSanderA     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
18426279d81SSanderA     ierr = DMLabelDestroy(&lblRoots);CHKERRQ(ierr);
18526279d81SSanderA   }
186b0bf5782SToby Isaac   PetscFunctionReturn(0);
187b0bf5782SToby Isaac }
188b0bf5782SToby Isaac 
1892be2b188SMatthew G Knepley /*@
1902be2b188SMatthew G Knepley   DMPlexLabelComplete - Starting with a label marking points on a surface, we add the transitive closure to the surface
1912be2b188SMatthew G Knepley 
1922be2b188SMatthew G Knepley   Input Parameters:
1932be2b188SMatthew G Knepley + dm - The DM
1942be2b188SMatthew G Knepley - label - A DMLabel marking the surface points
1952be2b188SMatthew G Knepley 
1962be2b188SMatthew G Knepley   Output Parameter:
1972be2b188SMatthew G Knepley . label - A DMLabel marking all surface points in the transitive closure
1982be2b188SMatthew G Knepley 
1992be2b188SMatthew G Knepley   Level: developer
2002be2b188SMatthew G Knepley 
2012be2b188SMatthew G Knepley .seealso: DMPlexLabelCohesiveComplete()
2022be2b188SMatthew G Knepley @*/
2032be2b188SMatthew G Knepley PetscErrorCode DMPlexLabelComplete(DM dm, DMLabel label)
2042be2b188SMatthew G Knepley {
2052be2b188SMatthew G Knepley   PetscErrorCode ierr;
2062be2b188SMatthew G Knepley 
2072be2b188SMatthew G Knepley   PetscFunctionBegin;
208b0bf5782SToby Isaac   ierr = DMPlexLabelComplete_Internal(dm, label, PETSC_TRUE);CHKERRQ(ierr);
2092be2b188SMatthew G Knepley   PetscFunctionReturn(0);
2102be2b188SMatthew G Knepley }
2112be2b188SMatthew G Knepley 
2126cf0e42fSMatthew G. Knepley /*@
213a6e0b375SMatthew G. Knepley   DMPlexLabelAddCells - Starting with a label marking points on a surface, we add a cell for each point
2146cf0e42fSMatthew G. Knepley 
2156cf0e42fSMatthew G. Knepley   Input Parameters:
2166cf0e42fSMatthew G. Knepley + dm - The DM
2176cf0e42fSMatthew G. Knepley - label - A DMLabel marking the surface points
2186cf0e42fSMatthew G. Knepley 
2196cf0e42fSMatthew G. Knepley   Output Parameter:
2206cf0e42fSMatthew G. Knepley . label - A DMLabel incorporating cells
2216cf0e42fSMatthew G. Knepley 
2226cf0e42fSMatthew G. Knepley   Level: developer
2236cf0e42fSMatthew G. Knepley 
2246cf0e42fSMatthew G. Knepley   Note: The cells allow FEM boundary conditions to be applied using the cell geometry
2256cf0e42fSMatthew G. Knepley 
226a6e0b375SMatthew G. Knepley .seealso: DMPlexLabelAddFaceCells(), DMPlexLabelComplete(), DMPlexLabelCohesiveComplete()
2276cf0e42fSMatthew G. Knepley @*/
2286cf0e42fSMatthew G. Knepley PetscErrorCode DMPlexLabelAddCells(DM dm, DMLabel label)
2296cf0e42fSMatthew G. Knepley {
2306cf0e42fSMatthew G. Knepley   IS              valueIS;
2316cf0e42fSMatthew G. Knepley   const PetscInt *values;
232485ad865SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd;
2336cf0e42fSMatthew G. Knepley   PetscErrorCode  ierr;
2346cf0e42fSMatthew G. Knepley 
2356cf0e42fSMatthew G. Knepley   PetscFunctionBegin;
236412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2376cf0e42fSMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
2386cf0e42fSMatthew G. Knepley   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
2396cf0e42fSMatthew G. Knepley   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
2406cf0e42fSMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
2416cf0e42fSMatthew G. Knepley     IS              pointIS;
2426cf0e42fSMatthew G. Knepley     const PetscInt *points;
2436cf0e42fSMatthew G. Knepley     PetscInt        numPoints, p;
2446cf0e42fSMatthew G. Knepley 
2456cf0e42fSMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr);
2466cf0e42fSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
2476cf0e42fSMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
2486cf0e42fSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2496cf0e42fSMatthew G. Knepley       PetscInt *closure = NULL;
250a6e0b375SMatthew G. Knepley       PetscInt  closureSize, cl;
2516cf0e42fSMatthew G. Knepley 
2526cf0e42fSMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure);CHKERRQ(ierr);
25322eabd52SMatthew G. Knepley       for (cl = closureSize-1; cl > 0; --cl) {
254a6e0b375SMatthew G. Knepley         const PetscInt cell = closure[cl*2];
255a6e0b375SMatthew G. Knepley         if ((cell >= cStart) && (cell < cEnd)) {ierr = DMLabelSetValue(label, cell, values[v]);CHKERRQ(ierr); break;}
25622eabd52SMatthew G. Knepley       }
2576cf0e42fSMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure);CHKERRQ(ierr);
2586cf0e42fSMatthew G. Knepley     }
2596cf0e42fSMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
2606cf0e42fSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
2616cf0e42fSMatthew G. Knepley   }
2626cf0e42fSMatthew G. Knepley   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
2636cf0e42fSMatthew G. Knepley   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
2646cf0e42fSMatthew G. Knepley   PetscFunctionReturn(0);
2656cf0e42fSMatthew G. Knepley }
2666cf0e42fSMatthew G. Knepley 
267f402d5e4SToby Isaac /*@
268a6e0b375SMatthew G. Knepley   DMPlexLabelAddFaceCells - Starting with a label marking faces on a surface, we add a cell for each face
269a6e0b375SMatthew G. Knepley 
270a6e0b375SMatthew G. Knepley   Input Parameters:
271a6e0b375SMatthew G. Knepley + dm - The DM
272a6e0b375SMatthew G. Knepley - label - A DMLabel marking the surface points
273a6e0b375SMatthew G. Knepley 
274a6e0b375SMatthew G. Knepley   Output Parameter:
275a6e0b375SMatthew G. Knepley . label - A DMLabel incorporating cells
276a6e0b375SMatthew G. Knepley 
277a6e0b375SMatthew G. Knepley   Level: developer
278a6e0b375SMatthew G. Knepley 
279a6e0b375SMatthew G. Knepley   Note: The cells allow FEM boundary conditions to be applied using the cell geometry
280a6e0b375SMatthew G. Knepley 
281a6e0b375SMatthew G. Knepley .seealso: DMPlexLabelAddCells(), DMPlexLabelComplete(), DMPlexLabelCohesiveComplete()
282a6e0b375SMatthew G. Knepley @*/
283a6e0b375SMatthew G. Knepley PetscErrorCode DMPlexLabelAddFaceCells(DM dm, DMLabel label)
284a6e0b375SMatthew G. Knepley {
285a6e0b375SMatthew G. Knepley   IS              valueIS;
286a6e0b375SMatthew G. Knepley   const PetscInt *values;
287a6e0b375SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd, fStart, fEnd;
288a6e0b375SMatthew G. Knepley   PetscErrorCode  ierr;
289a6e0b375SMatthew G. Knepley 
290a6e0b375SMatthew G. Knepley   PetscFunctionBegin;
291412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
292a6e0b375SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
293a6e0b375SMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
294a6e0b375SMatthew G. Knepley   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
295a6e0b375SMatthew G. Knepley   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
296a6e0b375SMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
297a6e0b375SMatthew G. Knepley     IS              pointIS;
298a6e0b375SMatthew G. Knepley     const PetscInt *points;
299a6e0b375SMatthew G. Knepley     PetscInt        numPoints, p;
300a6e0b375SMatthew G. Knepley 
301a6e0b375SMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr);
302a6e0b375SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
303a6e0b375SMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
304a6e0b375SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
305a6e0b375SMatthew G. Knepley       const PetscInt face = points[p];
306a6e0b375SMatthew G. Knepley       PetscInt      *closure = NULL;
307a6e0b375SMatthew G. Knepley       PetscInt       closureSize, cl;
308a6e0b375SMatthew G. Knepley 
309a6e0b375SMatthew G. Knepley       if ((face < fStart) || (face >= fEnd)) continue;
310a6e0b375SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, face, PETSC_FALSE, &closureSize, &closure);CHKERRQ(ierr);
311a6e0b375SMatthew G. Knepley       for (cl = closureSize-1; cl > 0; --cl) {
312a6e0b375SMatthew G. Knepley         const PetscInt cell = closure[cl*2];
313a6e0b375SMatthew G. Knepley         if ((cell >= cStart) && (cell < cEnd)) {ierr = DMLabelSetValue(label, cell, values[v]);CHKERRQ(ierr); break;}
314a6e0b375SMatthew G. Knepley       }
315a6e0b375SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, face, PETSC_FALSE, &closureSize, &closure);CHKERRQ(ierr);
316a6e0b375SMatthew G. Knepley     }
317a6e0b375SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
318a6e0b375SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
319a6e0b375SMatthew G. Knepley   }
320a6e0b375SMatthew G. Knepley   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
321a6e0b375SMatthew G. Knepley   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
322a6e0b375SMatthew G. Knepley   PetscFunctionReturn(0);
323a6e0b375SMatthew G. Knepley }
324a6e0b375SMatthew G. Knepley 
325a6e0b375SMatthew G. Knepley /*@
326f402d5e4SToby Isaac   DMPlexLabelClearCells - Remove cells from a label
327f402d5e4SToby Isaac 
328f402d5e4SToby Isaac   Input Parameters:
329f402d5e4SToby Isaac + dm - The DM
330f402d5e4SToby Isaac - label - A DMLabel marking surface points and their adjacent cells
331f402d5e4SToby Isaac 
332f402d5e4SToby Isaac   Output Parameter:
333f402d5e4SToby Isaac . label - A DMLabel without cells
334f402d5e4SToby Isaac 
335f402d5e4SToby Isaac   Level: developer
336f402d5e4SToby Isaac 
337a6e0b375SMatthew G. Knepley   Note: This undoes DMPlexLabelAddCells() or DMPlexLabelAddFaceCells()
338f402d5e4SToby Isaac 
339f402d5e4SToby Isaac .seealso: DMPlexLabelComplete(), DMPlexLabelCohesiveComplete(), DMPlexLabelAddCells()
340f402d5e4SToby Isaac @*/
341f402d5e4SToby Isaac PetscErrorCode DMPlexLabelClearCells(DM dm, DMLabel label)
342f402d5e4SToby Isaac {
343f402d5e4SToby Isaac   IS              valueIS;
344f402d5e4SToby Isaac   const PetscInt *values;
345485ad865SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd;
346f402d5e4SToby Isaac   PetscErrorCode  ierr;
347f402d5e4SToby Isaac 
348f402d5e4SToby Isaac   PetscFunctionBegin;
349412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
350f402d5e4SToby Isaac   ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
351f402d5e4SToby Isaac   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
352f402d5e4SToby Isaac   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
353f402d5e4SToby Isaac   for (v = 0; v < numValues; ++v) {
354f402d5e4SToby Isaac     IS              pointIS;
355f402d5e4SToby Isaac     const PetscInt *points;
356f402d5e4SToby Isaac     PetscInt        numPoints, p;
357f402d5e4SToby Isaac 
358f402d5e4SToby Isaac     ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr);
359f402d5e4SToby Isaac     ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
360f402d5e4SToby Isaac     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
361f402d5e4SToby Isaac     for (p = 0; p < numPoints; ++p) {
362f402d5e4SToby Isaac       PetscInt point = points[p];
363f402d5e4SToby Isaac 
364f402d5e4SToby Isaac       if (point >= cStart && point < cEnd) {
365f402d5e4SToby Isaac         ierr = DMLabelClearValue(label,point,values[v]);CHKERRQ(ierr);
366f402d5e4SToby Isaac       }
367f402d5e4SToby Isaac     }
368f402d5e4SToby Isaac     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
369f402d5e4SToby Isaac     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
370f402d5e4SToby Isaac   }
371f402d5e4SToby Isaac   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
372f402d5e4SToby Isaac   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
373f402d5e4SToby Isaac   PetscFunctionReturn(0);
374f402d5e4SToby Isaac }
375f402d5e4SToby Isaac 
37659eef20bSToby Isaac /* take (oldEnd, added) pairs, ordered by height and convert them to (oldstart, newstart) pairs, ordered by ascending
37759eef20bSToby Isaac  * index (skipping first, which is (0,0)) */
3789fbee547SJacob Faibussowitsch static inline PetscErrorCode DMPlexShiftPointSetUp_Internal(PetscInt depth, PetscInt depthShift[])
379cd0c2139SMatthew G Knepley {
3802582d50cSToby Isaac   PetscInt d, off = 0;
3812582d50cSToby Isaac 
3822582d50cSToby Isaac   PetscFunctionBegin;
38359eef20bSToby Isaac   /* sort by (oldend): yes this is an O(n^2) sort, we expect depth <= 3 */
3840974a383SToby Isaac   for (d = 0; d < depth; d++) {
3852582d50cSToby Isaac     PetscInt firstd = d;
3860974a383SToby Isaac     PetscInt firstStart = depthShift[2*d];
3872582d50cSToby Isaac     PetscInt e;
3882582d50cSToby Isaac 
3892582d50cSToby Isaac     for (e = d+1; e <= depth; e++) {
3902582d50cSToby Isaac       if (depthShift[2*e] < firstStart) {
3912582d50cSToby Isaac         firstd = e;
3922582d50cSToby Isaac         firstStart = depthShift[2*d];
3932582d50cSToby Isaac       }
3942582d50cSToby Isaac     }
3952582d50cSToby Isaac     if (firstd != d) {
3962582d50cSToby Isaac       PetscInt swap[2];
3972582d50cSToby Isaac 
3982582d50cSToby Isaac       e = firstd;
3992582d50cSToby Isaac       swap[0] = depthShift[2*d];
4002582d50cSToby Isaac       swap[1] = depthShift[2*d+1];
4012582d50cSToby Isaac       depthShift[2*d]   = depthShift[2*e];
4022582d50cSToby Isaac       depthShift[2*d+1] = depthShift[2*e+1];
4032582d50cSToby Isaac       depthShift[2*e]   = swap[0];
4042582d50cSToby Isaac       depthShift[2*e+1] = swap[1];
4052582d50cSToby Isaac     }
4062582d50cSToby Isaac   }
4072582d50cSToby Isaac   /* convert (oldstart, added) to (oldstart, newstart) */
4080974a383SToby Isaac   for (d = 0; d <= depth; d++) {
4092582d50cSToby Isaac     off += depthShift[2*d+1];
41059eef20bSToby Isaac     depthShift[2*d+1] = depthShift[2*d] + off;
4112582d50cSToby Isaac   }
4122582d50cSToby Isaac   PetscFunctionReturn(0);
4132582d50cSToby Isaac }
4142582d50cSToby Isaac 
4152582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
4169fbee547SJacob Faibussowitsch static inline PetscInt DMPlexShiftPoint_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
4172582d50cSToby Isaac {
4182582d50cSToby Isaac   PetscInt d;
4192582d50cSToby Isaac   PetscInt newOff = 0;
4202582d50cSToby Isaac 
4212582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
4222582d50cSToby Isaac     if (p < depthShift[2*d]) return p + newOff;
4232582d50cSToby Isaac     else newOff = depthShift[2*d+1] - depthShift[2*d];
4242582d50cSToby Isaac   }
4250974a383SToby Isaac   return p + newOff;
4262582d50cSToby Isaac }
4272582d50cSToby Isaac 
4282582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
4299fbee547SJacob Faibussowitsch static inline PetscInt DMPlexShiftPointInverse_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
4302582d50cSToby Isaac {
4312582d50cSToby Isaac   PetscInt d;
4322582d50cSToby Isaac   PetscInt newOff = 0;
4332582d50cSToby Isaac 
4342582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
4352582d50cSToby Isaac     if (p < depthShift[2*d+1]) return p + newOff;
4362582d50cSToby Isaac     else newOff = depthShift[2*d] - depthShift[2*d+1];
4372582d50cSToby Isaac   }
4380974a383SToby Isaac   return p + newOff;
439cd0c2139SMatthew G Knepley }
440cd0c2139SMatthew G Knepley 
441cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftSizes_Internal(DM dm, PetscInt depthShift[], DM dmNew)
442cd0c2139SMatthew G Knepley {
443cd0c2139SMatthew G Knepley   PetscInt       depth = 0, d, pStart, pEnd, p;
444fa8e8ae5SToby Isaac   DMLabel        depthLabel;
445cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
446cd0c2139SMatthew G Knepley 
447cd0c2139SMatthew G Knepley   PetscFunctionBegin;
448cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
449cd0c2139SMatthew G Knepley   if (depth < 0) PetscFunctionReturn(0);
450cd0c2139SMatthew G Knepley   /* Step 1: Expand chart */
451cd0c2139SMatthew G Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
4520974a383SToby Isaac   pEnd = DMPlexShiftPoint_Internal(pEnd,depth,depthShift);
453cd0c2139SMatthew G Knepley   ierr = DMPlexSetChart(dmNew, pStart, pEnd);CHKERRQ(ierr);
454fa8e8ae5SToby Isaac   ierr = DMCreateLabel(dmNew,"depth");CHKERRQ(ierr);
455fa8e8ae5SToby Isaac   ierr = DMPlexGetDepthLabel(dmNew,&depthLabel);CHKERRQ(ierr);
456412e9a14SMatthew G. Knepley   ierr = DMCreateLabel(dmNew, "celltype");CHKERRQ(ierr);
457cd0c2139SMatthew G Knepley   /* Step 2: Set cone and support sizes */
458cd0c2139SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
459fa8e8ae5SToby Isaac     PetscInt pStartNew, pEndNew;
460fa8e8ae5SToby Isaac     IS pIS;
461fa8e8ae5SToby Isaac 
462cd0c2139SMatthew G Knepley     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
463fa8e8ae5SToby Isaac     pStartNew = DMPlexShiftPoint_Internal(pStart, depth, depthShift);
464fa8e8ae5SToby Isaac     pEndNew = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
465fa8e8ae5SToby Isaac     ierr = ISCreateStride(PETSC_COMM_SELF, pEndNew - pStartNew, pStartNew, 1, &pIS);CHKERRQ(ierr);
466fa8e8ae5SToby Isaac     ierr = DMLabelSetStratumIS(depthLabel, d, pIS);CHKERRQ(ierr);
467fa8e8ae5SToby Isaac     ierr = ISDestroy(&pIS);CHKERRQ(ierr);
468cd0c2139SMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
4692582d50cSToby Isaac       PetscInt       newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
470cd0c2139SMatthew G Knepley       PetscInt       size;
471412e9a14SMatthew G. Knepley       DMPolytopeType ct;
472cd0c2139SMatthew G Knepley 
473cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, p, &size);CHKERRQ(ierr);
474cd0c2139SMatthew G Knepley       ierr = DMPlexSetConeSize(dmNew, newp, size);CHKERRQ(ierr);
475cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, p, &size);CHKERRQ(ierr);
476cd0c2139SMatthew G Knepley       ierr = DMPlexSetSupportSize(dmNew, newp, size);CHKERRQ(ierr);
477412e9a14SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, p, &ct);CHKERRQ(ierr);
478412e9a14SMatthew G. Knepley       ierr = DMPlexSetCellType(dmNew, newp, ct);CHKERRQ(ierr);
479cd0c2139SMatthew G Knepley     }
480cd0c2139SMatthew G Knepley   }
481cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
482cd0c2139SMatthew G Knepley }
483cd0c2139SMatthew G Knepley 
484cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftPoints_Internal(DM dm, PetscInt depthShift[], DM dmNew)
485cd0c2139SMatthew G Knepley {
4862582d50cSToby Isaac   PetscInt      *newpoints;
4872582d50cSToby Isaac   PetscInt       depth = 0, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, pStart, pEnd, p;
488cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
489cd0c2139SMatthew G Knepley 
490cd0c2139SMatthew G Knepley   PetscFunctionBegin;
491cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
492cd0c2139SMatthew G Knepley   if (depth < 0) PetscFunctionReturn(0);
493cd0c2139SMatthew G Knepley   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
494dcbb62e8SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dmNew, &maxConeSizeNew, &maxSupportSizeNew);CHKERRQ(ierr);
4952582d50cSToby Isaac   ierr = PetscMalloc1(PetscMax(PetscMax(maxConeSize, maxSupportSize), PetscMax(maxConeSizeNew, maxSupportSizeNew)),&newpoints);CHKERRQ(ierr);
496cd0c2139SMatthew G Knepley   /* Step 5: Set cones and supports */
497cd0c2139SMatthew G Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
498cd0c2139SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
499cd0c2139SMatthew G Knepley     const PetscInt *points = NULL, *orientations = NULL;
5002582d50cSToby Isaac     PetscInt        size,sizeNew, i, newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
501cd0c2139SMatthew G Knepley 
502cd0c2139SMatthew G Knepley     ierr = DMPlexGetConeSize(dm, p, &size);CHKERRQ(ierr);
503cd0c2139SMatthew G Knepley     ierr = DMPlexGetCone(dm, p, &points);CHKERRQ(ierr);
504cd0c2139SMatthew G Knepley     ierr = DMPlexGetConeOrientation(dm, p, &orientations);CHKERRQ(ierr);
505cd0c2139SMatthew G Knepley     for (i = 0; i < size; ++i) {
5062582d50cSToby Isaac       newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
507cd0c2139SMatthew G Knepley     }
508cd0c2139SMatthew G Knepley     ierr = DMPlexSetCone(dmNew, newp, newpoints);CHKERRQ(ierr);
509cd0c2139SMatthew G Knepley     ierr = DMPlexSetConeOrientation(dmNew, newp, orientations);CHKERRQ(ierr);
510cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupportSize(dm, p, &size);CHKERRQ(ierr);
511dcbb62e8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmNew, newp, &sizeNew);CHKERRQ(ierr);
512cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupport(dm, p, &points);CHKERRQ(ierr);
513cd0c2139SMatthew G Knepley     for (i = 0; i < size; ++i) {
5142582d50cSToby Isaac       newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
515cd0c2139SMatthew G Knepley     }
516dcbb62e8SMatthew G. Knepley     for (i = size; i < sizeNew; ++i) newpoints[i] = 0;
517cd0c2139SMatthew G Knepley     ierr = DMPlexSetSupport(dmNew, newp, newpoints);CHKERRQ(ierr);
518cd0c2139SMatthew G Knepley   }
5192582d50cSToby Isaac   ierr = PetscFree(newpoints);CHKERRQ(ierr);
520cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
521cd0c2139SMatthew G Knepley }
522cd0c2139SMatthew G Knepley 
523cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftCoordinates_Internal(DM dm, PetscInt depthShift[], DM dmNew)
524cd0c2139SMatthew G Knepley {
525cd0c2139SMatthew G Knepley   PetscSection   coordSection, newCoordSection;
526cd0c2139SMatthew G Knepley   Vec            coordinates, newCoordinates;
527cd0c2139SMatthew G Knepley   PetscScalar   *coords, *newCoords;
528f2b8cce1SMatthew G. Knepley   PetscInt       coordSize, sStart, sEnd;
529f2b8cce1SMatthew G. Knepley   PetscInt       dim, depth = 0, cStart, cEnd, cStartNew, cEndNew, c, vStart, vEnd, vStartNew, vEndNew, v;
530f2b8cce1SMatthew G. Knepley   PetscBool      hasCells;
531cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
532cd0c2139SMatthew G Knepley 
533cd0c2139SMatthew G Knepley   PetscFunctionBegin;
534f2b8cce1SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
535c0e8cf5fSMatthew G. Knepley   ierr = DMSetCoordinateDim(dmNew, dim);CHKERRQ(ierr);
536cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
537cd0c2139SMatthew G Knepley   /* Step 8: Convert coordinates */
538cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
539f2b8cce1SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
540cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepthStratum(dmNew, 0, &vStartNew, &vEndNew);CHKERRQ(ierr);
541f2b8cce1SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmNew, 0, &cStartNew, &cEndNew);CHKERRQ(ierr);
54269d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
543cd0c2139SMatthew G Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &newCoordSection);CHKERRQ(ierr);
544cd0c2139SMatthew G Knepley   ierr = PetscSectionSetNumFields(newCoordSection, 1);CHKERRQ(ierr);
545cd0c2139SMatthew G Knepley   ierr = PetscSectionSetFieldComponents(newCoordSection, 0, dim);CHKERRQ(ierr);
546f2b8cce1SMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
547f2b8cce1SMatthew G. Knepley   hasCells = sStart == cStart ? PETSC_TRUE : PETSC_FALSE;
548f2b8cce1SMatthew G. Knepley   ierr = PetscSectionSetChart(newCoordSection, hasCells ? cStartNew : vStartNew, vEndNew);CHKERRQ(ierr);
549f2b8cce1SMatthew G. Knepley   if (hasCells) {
550f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
551f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof;
552f2b8cce1SMatthew G. Knepley 
553f2b8cce1SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
554f2b8cce1SMatthew G. Knepley       ierr = PetscSectionSetDof(newCoordSection, cNew, dof);CHKERRQ(ierr);
555f2b8cce1SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(newCoordSection, cNew, 0, dof);CHKERRQ(ierr);
556f2b8cce1SMatthew G. Knepley     }
557f2b8cce1SMatthew G. Knepley   }
558cd0c2139SMatthew G Knepley   for (v = vStartNew; v < vEndNew; ++v) {
559cd0c2139SMatthew G Knepley     ierr = PetscSectionSetDof(newCoordSection, v, dim);CHKERRQ(ierr);
560cd0c2139SMatthew G Knepley     ierr = PetscSectionSetFieldDof(newCoordSection, v, 0, dim);CHKERRQ(ierr);
561cd0c2139SMatthew G Knepley   }
562cd0c2139SMatthew G Knepley   ierr = PetscSectionSetUp(newCoordSection);CHKERRQ(ierr);
56346e270d4SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmNew, PETSC_DETERMINE, newCoordSection);CHKERRQ(ierr);
564cd0c2139SMatthew G Knepley   ierr = PetscSectionGetStorageSize(newCoordSection, &coordSize);CHKERRQ(ierr);
5658b9ced59SLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &newCoordinates);CHKERRQ(ierr);
566cd0c2139SMatthew G Knepley   ierr = PetscObjectSetName((PetscObject) newCoordinates, "coordinates");CHKERRQ(ierr);
567cd0c2139SMatthew G Knepley   ierr = VecSetSizes(newCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
5684e90ef8eSMatthew G. Knepley   ierr = VecSetBlockSize(newCoordinates, dim);CHKERRQ(ierr);
5692eb5907fSJed Brown   ierr = VecSetType(newCoordinates,VECSTANDARD);CHKERRQ(ierr);
570cd0c2139SMatthew G Knepley   ierr = DMSetCoordinatesLocal(dmNew, newCoordinates);CHKERRQ(ierr);
571cd0c2139SMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
572cd0c2139SMatthew G Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
573cd0c2139SMatthew G Knepley   ierr = VecGetArray(newCoordinates, &newCoords);CHKERRQ(ierr);
574f2b8cce1SMatthew G. Knepley   if (hasCells) {
575f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
576f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof, off, noff, d;
577f2b8cce1SMatthew G. Knepley 
578f2b8cce1SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
579f2b8cce1SMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, c, &off);CHKERRQ(ierr);
580f2b8cce1SMatthew G. Knepley       ierr = PetscSectionGetOffset(newCoordSection, cNew, &noff);CHKERRQ(ierr);
581f2b8cce1SMatthew G. Knepley       for (d = 0; d < dof; ++d) newCoords[noff+d] = coords[off+d];
582f2b8cce1SMatthew G. Knepley     }
583f2b8cce1SMatthew G. Knepley   }
584cd0c2139SMatthew G Knepley   for (v = vStart; v < vEnd; ++v) {
585cd0c2139SMatthew G Knepley     PetscInt dof, off, noff, d;
586cd0c2139SMatthew G Knepley 
587cd0c2139SMatthew G Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
588cd0c2139SMatthew G Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
5892582d50cSToby Isaac     ierr = PetscSectionGetOffset(newCoordSection, DMPlexShiftPoint_Internal(v, depth, depthShift), &noff);CHKERRQ(ierr);
590f2b8cce1SMatthew G. Knepley     for (d = 0; d < dof; ++d) newCoords[noff+d] = coords[off+d];
591cd0c2139SMatthew G Knepley   }
592cd0c2139SMatthew G Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
593cd0c2139SMatthew G Knepley   ierr = VecRestoreArray(newCoordinates, &newCoords);CHKERRQ(ierr);
594cd0c2139SMatthew G Knepley   ierr = VecDestroy(&newCoordinates);CHKERRQ(ierr);
595cd0c2139SMatthew G Knepley   ierr = PetscSectionDestroy(&newCoordSection);CHKERRQ(ierr);
596cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
597cd0c2139SMatthew G Knepley }
598cd0c2139SMatthew G Knepley 
5990e33faafSMatthew G. Knepley static PetscErrorCode DMPlexShiftSF_Single(DM dm, PetscInt depthShift[], PetscSF sf, PetscSF sfNew)
600cd0c2139SMatthew G Knepley {
601cd0c2139SMatthew G Knepley   const PetscSFNode *remotePoints;
602cd0c2139SMatthew G Knepley   PetscSFNode       *gremotePoints;
603cd0c2139SMatthew G Knepley   const PetscInt    *localPoints;
604cd0c2139SMatthew G Knepley   PetscInt          *glocalPoints, *newLocation, *newRemoteLocation;
6050e33faafSMatthew G. Knepley   PetscInt           numRoots, numLeaves, l, pStart, pEnd, depth = 0, totShift = 0;
606cd0c2139SMatthew G Knepley   PetscErrorCode     ierr;
607cd0c2139SMatthew G Knepley 
608cd0c2139SMatthew G Knepley   PetscFunctionBegin;
609cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
610cd0c2139SMatthew G Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
6110e33faafSMatthew G. Knepley   ierr = PetscSFGetGraph(sf, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr);
6126e21efdcSToby Isaac   totShift = DMPlexShiftPoint_Internal(pEnd, depth, depthShift) - pEnd;
613cd0c2139SMatthew G Knepley   if (numRoots >= 0) {
614dcca6d9dSJed Brown     ierr = PetscMalloc2(numRoots, &newLocation, pEnd-pStart, &newRemoteLocation);CHKERRQ(ierr);
6150e33faafSMatthew G. Knepley     for (l = 0; l < numRoots; ++l) newLocation[l] = DMPlexShiftPoint_Internal(l, depth, depthShift);
6160e33faafSMatthew G. Knepley     ierr = PetscSFBcastBegin(sf, MPIU_INT, newLocation, newRemoteLocation, MPI_REPLACE);CHKERRQ(ierr);
6170e33faafSMatthew G. Knepley     ierr = PetscSFBcastEnd(sf, MPIU_INT, newLocation, newRemoteLocation, MPI_REPLACE);CHKERRQ(ierr);
618785e854fSJed Brown     ierr = PetscMalloc1(numLeaves, &glocalPoints);CHKERRQ(ierr);
619785e854fSJed Brown     ierr = PetscMalloc1(numLeaves, &gremotePoints);CHKERRQ(ierr);
620cd0c2139SMatthew G Knepley     for (l = 0; l < numLeaves; ++l) {
6212582d50cSToby Isaac       glocalPoints[l]        = DMPlexShiftPoint_Internal(localPoints[l], depth, depthShift);
622cd0c2139SMatthew G Knepley       gremotePoints[l].rank  = remotePoints[l].rank;
623cd0c2139SMatthew G Knepley       gremotePoints[l].index = newRemoteLocation[localPoints[l]];
624cd0c2139SMatthew G Knepley     }
625cd0c2139SMatthew G Knepley     ierr = PetscFree2(newLocation, newRemoteLocation);CHKERRQ(ierr);
6260e33faafSMatthew G. Knepley     ierr = PetscSFSetGraph(sfNew, numRoots + totShift, numLeaves, glocalPoints, PETSC_OWN_POINTER, gremotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
6270e33faafSMatthew G. Knepley   }
6280e33faafSMatthew G. Knepley   PetscFunctionReturn(0);
6290e33faafSMatthew G. Knepley }
6300e33faafSMatthew G. Knepley 
6310e33faafSMatthew G. Knepley static PetscErrorCode DMPlexShiftSF_Internal(DM dm, PetscInt depthShift[], DM dmNew)
6320e33faafSMatthew G. Knepley {
6330e33faafSMatthew G. Knepley   PetscSF        sfPoint, sfPointNew;
6340e33faafSMatthew G. Knepley   PetscBool      useNatural;
6350e33faafSMatthew G. Knepley   PetscErrorCode ierr;
6360e33faafSMatthew G. Knepley 
6370e33faafSMatthew G. Knepley   PetscFunctionBegin;
6380e33faafSMatthew G. Knepley   /* Step 9: Convert pointSF */
6390e33faafSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
6400e33faafSMatthew G. Knepley   ierr = DMGetPointSF(dmNew, &sfPointNew);CHKERRQ(ierr);
6410e33faafSMatthew G. Knepley   ierr = DMPlexShiftSF_Single(dm, depthShift, sfPoint, sfPointNew);CHKERRQ(ierr);
6420e33faafSMatthew G. Knepley   /* Step 9b: Convert naturalSF */
6430e33faafSMatthew G. Knepley   ierr = DMGetUseNatural(dm, &useNatural);CHKERRQ(ierr);
6440e33faafSMatthew G. Knepley   if (useNatural) {
6450e33faafSMatthew G. Knepley     PetscSF sfNat, sfNatNew;
6460e33faafSMatthew G. Knepley 
6470e33faafSMatthew G. Knepley     ierr = DMSetUseNatural(dmNew, useNatural);CHKERRQ(ierr);
6480e33faafSMatthew G. Knepley     ierr = DMGetNaturalSF(dm, &sfNat);CHKERRQ(ierr);
6490e33faafSMatthew G. Knepley     ierr = DMGetNaturalSF(dmNew, &sfNatNew);CHKERRQ(ierr);
6500e33faafSMatthew G. Knepley     ierr = DMPlexShiftSF_Single(dm, depthShift, sfNat, sfNatNew);CHKERRQ(ierr);
651cd0c2139SMatthew G Knepley   }
652cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
653cd0c2139SMatthew G Knepley }
654cd0c2139SMatthew G Knepley 
655cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftLabels_Internal(DM dm, PetscInt depthShift[], DM dmNew)
656cd0c2139SMatthew G Knepley {
657cd0c2139SMatthew G Knepley   PetscSF            sfPoint;
658cd0c2139SMatthew G Knepley   DMLabel            vtkLabel, ghostLabel;
659cd0c2139SMatthew G Knepley   const PetscSFNode *leafRemote;
660cd0c2139SMatthew G Knepley   const PetscInt    *leafLocal;
6612582d50cSToby Isaac   PetscInt           depth = 0, numLeaves, numLabels, l, cStart, cEnd, c, fStart, fEnd, f;
662cd0c2139SMatthew G Knepley   PetscMPIInt        rank;
663cd0c2139SMatthew G Knepley   PetscErrorCode     ierr;
664cd0c2139SMatthew G Knepley 
665cd0c2139SMatthew G Knepley   PetscFunctionBegin;
666cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
667cd0c2139SMatthew G Knepley   /* Step 10: Convert labels */
668c58f1c22SToby Isaac   ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
669cd0c2139SMatthew G Knepley   for (l = 0; l < numLabels; ++l) {
670cd0c2139SMatthew G Knepley     DMLabel         label, newlabel;
671cd0c2139SMatthew G Knepley     const char     *lname;
672fa8e8ae5SToby Isaac     PetscBool       isDepth, isDim;
673cd0c2139SMatthew G Knepley     IS              valueIS;
674cd0c2139SMatthew G Knepley     const PetscInt *values;
675cd0c2139SMatthew G Knepley     PetscInt        numValues, val;
676cd0c2139SMatthew G Knepley 
677c58f1c22SToby Isaac     ierr = DMGetLabelName(dm, l, &lname);CHKERRQ(ierr);
678cd0c2139SMatthew G Knepley     ierr = PetscStrcmp(lname, "depth", &isDepth);CHKERRQ(ierr);
679cd0c2139SMatthew G Knepley     if (isDepth) continue;
680fa8e8ae5SToby Isaac     ierr = PetscStrcmp(lname, "dim", &isDim);CHKERRQ(ierr);
681fa8e8ae5SToby Isaac     if (isDim) continue;
682c58f1c22SToby Isaac     ierr = DMCreateLabel(dmNew, lname);CHKERRQ(ierr);
683c58f1c22SToby Isaac     ierr = DMGetLabel(dm, lname, &label);CHKERRQ(ierr);
684c58f1c22SToby Isaac     ierr = DMGetLabel(dmNew, lname, &newlabel);CHKERRQ(ierr);
68564a6a1a4SToby Isaac     ierr = DMLabelGetDefaultValue(label,&val);CHKERRQ(ierr);
68664a6a1a4SToby Isaac     ierr = DMLabelSetDefaultValue(newlabel,val);CHKERRQ(ierr);
687cd0c2139SMatthew G Knepley     ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
688cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(valueIS, &numValues);CHKERRQ(ierr);
689cd0c2139SMatthew G Knepley     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
690cd0c2139SMatthew G Knepley     for (val = 0; val < numValues; ++val) {
691cd0c2139SMatthew G Knepley       IS              pointIS;
692cd0c2139SMatthew G Knepley       const PetscInt *points;
693cd0c2139SMatthew G Knepley       PetscInt        numPoints, p;
694cd0c2139SMatthew G Knepley 
695cd0c2139SMatthew G Knepley       ierr = DMLabelGetStratumIS(label, values[val], &pointIS);CHKERRQ(ierr);
696cd0c2139SMatthew G Knepley       ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
697cd0c2139SMatthew G Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
698cd0c2139SMatthew G Knepley       for (p = 0; p < numPoints; ++p) {
6992582d50cSToby Isaac         const PetscInt newpoint = DMPlexShiftPoint_Internal(points[p], depth, depthShift);
700cd0c2139SMatthew G Knepley 
701cd0c2139SMatthew G Knepley         ierr = DMLabelSetValue(newlabel, newpoint, values[val]);CHKERRQ(ierr);
702cd0c2139SMatthew G Knepley       }
703cd0c2139SMatthew G Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
704cd0c2139SMatthew G Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
705cd0c2139SMatthew G Knepley     }
706cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
707cd0c2139SMatthew G Knepley     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
708cd0c2139SMatthew G Knepley   }
709cd0c2139SMatthew G Knepley   /* Step 11: Make label for output (vtk) and to mark ghost points (ghost) */
710ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRMPI(ierr);
711cd0c2139SMatthew G Knepley   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
712cd0c2139SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
713cd0c2139SMatthew G Knepley   ierr = PetscSFGetGraph(sfPoint, NULL, &numLeaves, &leafLocal, &leafRemote);CHKERRQ(ierr);
714c58f1c22SToby Isaac   ierr = DMCreateLabel(dmNew, "vtk");CHKERRQ(ierr);
715c58f1c22SToby Isaac   ierr = DMCreateLabel(dmNew, "ghost");CHKERRQ(ierr);
716c58f1c22SToby Isaac   ierr = DMGetLabel(dmNew, "vtk", &vtkLabel);CHKERRQ(ierr);
717c58f1c22SToby Isaac   ierr = DMGetLabel(dmNew, "ghost", &ghostLabel);CHKERRQ(ierr);
718cd0c2139SMatthew G Knepley   for (l = 0, c = cStart; l < numLeaves && c < cEnd; ++l, ++c) {
719cd0c2139SMatthew G Knepley     for (; c < leafLocal[l] && c < cEnd; ++c) {
720cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(vtkLabel, c, 1);CHKERRQ(ierr);
721cd0c2139SMatthew G Knepley     }
722cd0c2139SMatthew G Knepley     if (leafLocal[l] >= cEnd) break;
723cd0c2139SMatthew G Knepley     if (leafRemote[l].rank == rank) {
724cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(vtkLabel, c, 1);CHKERRQ(ierr);
725cd0c2139SMatthew G Knepley     } else {
726cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(ghostLabel, c, 2);CHKERRQ(ierr);
727cd0c2139SMatthew G Knepley     }
728cd0c2139SMatthew G Knepley   }
729cd0c2139SMatthew G Knepley   for (; c < cEnd; ++c) {
730cd0c2139SMatthew G Knepley     ierr = DMLabelSetValue(vtkLabel, c, 1);CHKERRQ(ierr);
731cd0c2139SMatthew G Knepley   }
732cd0c2139SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dmNew, 1, &fStart, &fEnd);CHKERRQ(ierr);
733cd0c2139SMatthew G Knepley   for (f = fStart; f < fEnd; ++f) {
734cd0c2139SMatthew G Knepley     PetscInt numCells;
735cd0c2139SMatthew G Knepley 
736cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupportSize(dmNew, f, &numCells);CHKERRQ(ierr);
737cd0c2139SMatthew G Knepley     if (numCells < 2) {
738cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(ghostLabel, f, 1);CHKERRQ(ierr);
739cd0c2139SMatthew G Knepley     } else {
740cd0c2139SMatthew G Knepley       const PetscInt *cells = NULL;
741cd0c2139SMatthew G Knepley       PetscInt        vA, vB;
742cd0c2139SMatthew G Knepley 
743cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupport(dmNew, f, &cells);CHKERRQ(ierr);
744cd0c2139SMatthew G Knepley       ierr = DMLabelGetValue(vtkLabel, cells[0], &vA);CHKERRQ(ierr);
745cd0c2139SMatthew G Knepley       ierr = DMLabelGetValue(vtkLabel, cells[1], &vB);CHKERRQ(ierr);
74690664b96SToby Isaac       if (vA != 1 && vB != 1) {ierr = DMLabelSetValue(ghostLabel, f, 1);CHKERRQ(ierr);}
747cd0c2139SMatthew G Knepley     }
748cd0c2139SMatthew G Knepley   }
749cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
750cd0c2139SMatthew G Knepley }
751cd0c2139SMatthew G Knepley 
752ca04dac2SToby Isaac static PetscErrorCode DMPlexShiftTree_Internal(DM dm, PetscInt depthShift[], DM dmNew)
753ca04dac2SToby Isaac {
754ca04dac2SToby Isaac   DM             refTree;
755ca04dac2SToby Isaac   PetscSection   pSec;
756ca04dac2SToby Isaac   PetscInt       *parents, *childIDs;
757ca04dac2SToby Isaac   PetscErrorCode ierr;
758ca04dac2SToby Isaac 
759ca04dac2SToby Isaac   PetscFunctionBegin;
760ca04dac2SToby Isaac   ierr = DMPlexGetReferenceTree(dm,&refTree);CHKERRQ(ierr);
761ca04dac2SToby Isaac   ierr = DMPlexSetReferenceTree(dmNew,refTree);CHKERRQ(ierr);
762ca04dac2SToby Isaac   ierr = DMPlexGetTree(dm,&pSec,&parents,&childIDs,NULL,NULL);CHKERRQ(ierr);
763ca04dac2SToby Isaac   if (pSec) {
7642582d50cSToby Isaac     PetscInt p, pStart, pEnd, *parentsShifted, pStartShifted, pEndShifted, depth;
765fb4630b5SToby Isaac     PetscInt *childIDsShifted;
766ca04dac2SToby Isaac     PetscSection pSecShifted;
767ca04dac2SToby Isaac 
768ca04dac2SToby Isaac     ierr = PetscSectionGetChart(pSec,&pStart,&pEnd);CHKERRQ(ierr);
769ca04dac2SToby Isaac     ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
7702582d50cSToby Isaac     pStartShifted = DMPlexShiftPoint_Internal(pStart,depth,depthShift);
7712582d50cSToby Isaac     pEndShifted   = DMPlexShiftPoint_Internal(pEnd,depth,depthShift);
772fb4630b5SToby Isaac     ierr = PetscMalloc2(pEndShifted - pStartShifted,&parentsShifted,pEndShifted-pStartShifted,&childIDsShifted);CHKERRQ(ierr);
773ca04dac2SToby Isaac     ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dmNew),&pSecShifted);CHKERRQ(ierr);
774ca04dac2SToby Isaac     ierr = PetscSectionSetChart(pSecShifted,pStartShifted,pEndShifted);CHKERRQ(ierr);
775ca04dac2SToby Isaac     for (p = pStartShifted; p < pEndShifted; p++) {
776fb4630b5SToby Isaac       /* start off assuming no children */
777fb4630b5SToby Isaac       ierr = PetscSectionSetDof(pSecShifted,p,0);CHKERRQ(ierr);
778fb4630b5SToby Isaac     }
779fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
780fb4630b5SToby Isaac       PetscInt dof;
781fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p,depth,depthShift);
782ca04dac2SToby Isaac 
783ca04dac2SToby Isaac       ierr = PetscSectionGetDof(pSec,p,&dof);CHKERRQ(ierr);
784fb4630b5SToby Isaac       ierr = PetscSectionSetDof(pSecShifted,pNew,dof);CHKERRQ(ierr);
785ca04dac2SToby Isaac     }
786ca04dac2SToby Isaac     ierr = PetscSectionSetUp(pSecShifted);CHKERRQ(ierr);
787fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
788fb4630b5SToby Isaac       PetscInt dof;
789fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p,depth,depthShift);
790fb4630b5SToby Isaac 
791fb4630b5SToby Isaac       ierr = PetscSectionGetDof(pSec,p,&dof);CHKERRQ(ierr);
792fb4630b5SToby Isaac       if (dof) {
793fb4630b5SToby Isaac         PetscInt off, offNew;
794fb4630b5SToby Isaac 
795fb4630b5SToby Isaac         ierr = PetscSectionGetOffset(pSec,p,&off);CHKERRQ(ierr);
796fb4630b5SToby Isaac         ierr = PetscSectionGetOffset(pSecShifted,pNew,&offNew);CHKERRQ(ierr);
797fb4630b5SToby Isaac         parentsShifted[offNew] = DMPlexShiftPoint_Internal(parents[off],depth,depthShift);
798fb4630b5SToby Isaac         childIDsShifted[offNew] = childIDs[off];
799fb4630b5SToby Isaac       }
800fb4630b5SToby Isaac     }
801fb4630b5SToby Isaac     ierr = DMPlexSetTree(dmNew,pSecShifted,parentsShifted,childIDsShifted);CHKERRQ(ierr);
802fb4630b5SToby Isaac     ierr = PetscFree2(parentsShifted,childIDsShifted);CHKERRQ(ierr);
803e6885bbbSToby Isaac     ierr = PetscSectionDestroy(&pSecShifted);CHKERRQ(ierr);
804ca04dac2SToby Isaac   }
805ca04dac2SToby Isaac   PetscFunctionReturn(0);
806ca04dac2SToby Isaac }
807ca04dac2SToby Isaac 
808cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexConstructGhostCells_Internal(DM dm, DMLabel label, PetscInt *numGhostCells, DM gdm)
809cd0c2139SMatthew G Knepley {
810da97024aSMatthew G. Knepley   PetscSF               sf;
811cd0c2139SMatthew G Knepley   IS                    valueIS;
812da97024aSMatthew G. Knepley   const PetscInt       *values, *leaves;
813cd0c2139SMatthew G Knepley   PetscInt             *depthShift;
8142582d50cSToby Isaac   PetscInt              d, depth = 0, nleaves, loc, Ng, numFS, fs, fStart, fEnd, ghostCell, cEnd, c;
81590b157c4SStefano Zampini   PetscBool             isper;
81690b157c4SStefano Zampini   const PetscReal      *maxCell, *L;
81790b157c4SStefano Zampini   const DMBoundaryType *bd;
818cd0c2139SMatthew G Knepley   PetscErrorCode        ierr;
819cd0c2139SMatthew G Knepley 
820cd0c2139SMatthew G Knepley   PetscFunctionBegin;
821da97024aSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
822da97024aSMatthew G. Knepley   ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
823da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
82446c796b9SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
825cd0c2139SMatthew G Knepley   /* Count ghost cells */
826cd0c2139SMatthew G Knepley   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
827cd0c2139SMatthew G Knepley   ierr = ISGetLocalSize(valueIS, &numFS);CHKERRQ(ierr);
828cd0c2139SMatthew G Knepley   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
8294a6cfa73SMatthew G. Knepley   Ng   = 0;
830cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
83146c796b9SMatthew G. Knepley     IS              faceIS;
83246c796b9SMatthew G. Knepley     const PetscInt *faces;
83346c796b9SMatthew G. Knepley     PetscInt        numFaces, f, numBdFaces = 0;
834cd0c2139SMatthew G Knepley 
83546c796b9SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, values[fs], &faceIS);CHKERRQ(ierr);
83646c796b9SMatthew G. Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
83746c796b9SMatthew G. Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
83846c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
839ca04dac2SToby Isaac       PetscInt numChildren;
840ca04dac2SToby Isaac 
841da97024aSMatthew G. Knepley       ierr = PetscFindInt(faces[f], nleaves, leaves, &loc);CHKERRQ(ierr);
842ca04dac2SToby Isaac       ierr = DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL);CHKERRQ(ierr);
843ca04dac2SToby Isaac       /* non-local and ancestors points don't get to register ghosts */
844ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
84546c796b9SMatthew G. Knepley       if ((faces[f] >= fStart) && (faces[f] < fEnd)) ++numBdFaces;
84646c796b9SMatthew G. Knepley     }
8474a6cfa73SMatthew G. Knepley     Ng += numBdFaces;
848ba2698f1SMatthew G. Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
84946c796b9SMatthew G. Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
850cd0c2139SMatthew G Knepley   }
851cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
8522582d50cSToby Isaac   ierr = PetscMalloc1(2*(depth+1), &depthShift);CHKERRQ(ierr);
8532582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
85459eef20bSToby Isaac     PetscInt dEnd;
8552582d50cSToby Isaac 
8560974a383SToby Isaac     ierr = DMPlexGetDepthStratum(dm,d,NULL,&dEnd);CHKERRQ(ierr);
85759eef20bSToby Isaac     depthShift[2*d]   = dEnd;
8582582d50cSToby Isaac     depthShift[2*d+1] = 0;
8592582d50cSToby Isaac   }
8602582d50cSToby Isaac   if (depth >= 0) depthShift[2*depth+1] = Ng;
8612582d50cSToby Isaac   ierr = DMPlexShiftPointSetUp_Internal(depth,depthShift);CHKERRQ(ierr);
862cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSizes_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
863cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
864cd0c2139SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
8654a6cfa73SMatthew G. Knepley   for (c = cEnd; c < cEnd + Ng; ++c) {
866cd0c2139SMatthew G Knepley     ierr = DMPlexSetConeSize(gdm, c, 1);CHKERRQ(ierr);
867cd0c2139SMatthew G Knepley   }
868cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
869cd0c2139SMatthew G Knepley     IS              faceIS;
870cd0c2139SMatthew G Knepley     const PetscInt *faces;
871cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
872cd0c2139SMatthew G Knepley 
873cd0c2139SMatthew G Knepley     ierr = DMLabelGetStratumIS(label, values[fs], &faceIS);CHKERRQ(ierr);
874cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
875cd0c2139SMatthew G Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
876cd0c2139SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
877ca04dac2SToby Isaac       PetscInt size, numChildren;
878cd0c2139SMatthew G Knepley 
879da97024aSMatthew G. Knepley       ierr = PetscFindInt(faces[f], nleaves, leaves, &loc);CHKERRQ(ierr);
880ca04dac2SToby Isaac       ierr = DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL);CHKERRQ(ierr);
881ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
88246c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
883cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, faces[f], &size);CHKERRQ(ierr);
8842c71b3e2SJacob Faibussowitsch       PetscCheckFalse(size != 1,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM has boundary face %d with %d support cells", faces[f], size);
8854a6cfa73SMatthew G. Knepley       ierr = DMPlexSetSupportSize(gdm, faces[f] + Ng, 2);CHKERRQ(ierr);
886cd0c2139SMatthew G Knepley     }
887cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
888cd0c2139SMatthew G Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
889cd0c2139SMatthew G Knepley   }
890cd0c2139SMatthew G Knepley   /* Step 4: Setup ghosted DM */
891cd0c2139SMatthew G Knepley   ierr = DMSetUp(gdm);CHKERRQ(ierr);
892cd0c2139SMatthew G Knepley   ierr = DMPlexShiftPoints_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
893cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
894cd0c2139SMatthew G Knepley   ghostCell = cEnd;
895cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
896cd0c2139SMatthew G Knepley     IS              faceIS;
897cd0c2139SMatthew G Knepley     const PetscInt *faces;
898cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
899cd0c2139SMatthew G Knepley 
900cd0c2139SMatthew G Knepley     ierr = DMLabelGetStratumIS(label, values[fs], &faceIS);CHKERRQ(ierr);
901cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
902cd0c2139SMatthew G Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
90346c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
904ca04dac2SToby Isaac       PetscInt newFace = faces[f] + Ng, numChildren;
905cd0c2139SMatthew G Knepley 
906da97024aSMatthew G. Knepley       ierr = PetscFindInt(faces[f], nleaves, leaves, &loc);CHKERRQ(ierr);
907ca04dac2SToby Isaac       ierr = DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL);CHKERRQ(ierr);
908ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
90946c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
910cd0c2139SMatthew G Knepley       ierr = DMPlexSetCone(gdm, ghostCell, &newFace);CHKERRQ(ierr);
911cd0c2139SMatthew G Knepley       ierr = DMPlexInsertSupport(gdm, newFace, 1, ghostCell);CHKERRQ(ierr);
91246c796b9SMatthew G. Knepley       ++ghostCell;
913cd0c2139SMatthew G Knepley     }
914cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
915cd0c2139SMatthew G Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
916cd0c2139SMatthew G Knepley   }
917cd0c2139SMatthew G Knepley   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
918cd0c2139SMatthew G Knepley   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
919cd0c2139SMatthew G Knepley   ierr = DMPlexShiftCoordinates_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
920cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSF_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
921cd0c2139SMatthew G Knepley   ierr = DMPlexShiftLabels_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
922ca04dac2SToby Isaac   ierr = DMPlexShiftTree_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
923cd0c2139SMatthew G Knepley   ierr = PetscFree(depthShift);CHKERRQ(ierr);
924412e9a14SMatthew G. Knepley   for (c = cEnd; c < cEnd + Ng; ++c) {
925412e9a14SMatthew G. Knepley     ierr = DMPlexSetCellType(gdm, c, DM_POLYTOPE_FV_GHOST);CHKERRQ(ierr);
926412e9a14SMatthew G. Knepley   }
927966c7b3fSMatthew G. Knepley   /* Step 7: Periodicity */
92890b157c4SStefano Zampini   ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
92990b157c4SStefano Zampini   ierr = DMSetPeriodicity(gdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
9304a6cfa73SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
931cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
932cd0c2139SMatthew G Knepley }
933cd0c2139SMatthew G Knepley 
934cd0c2139SMatthew G Knepley /*@C
935cd0c2139SMatthew G Knepley   DMPlexConstructGhostCells - Construct ghost cells which connect to every boundary face
936cd0c2139SMatthew G Knepley 
937cd0c2139SMatthew G Knepley   Collective on dm
938cd0c2139SMatthew G Knepley 
939cd0c2139SMatthew G Knepley   Input Parameters:
940cd0c2139SMatthew G Knepley + dm - The original DM
941cd0c2139SMatthew G Knepley - labelName - The label specifying the boundary faces, or "Face Sets" if this is NULL
942cd0c2139SMatthew G Knepley 
943cd0c2139SMatthew G Knepley   Output Parameters:
944cd0c2139SMatthew G Knepley + numGhostCells - The number of ghost cells added to the DM
945cd0c2139SMatthew G Knepley - dmGhosted - The new DM
946cd0c2139SMatthew G Knepley 
947cd0c2139SMatthew G Knepley   Note: If no label exists of that name, one will be created marking all boundary faces
948cd0c2139SMatthew G Knepley 
949cd0c2139SMatthew G Knepley   Level: developer
950cd0c2139SMatthew G Knepley 
951cd0c2139SMatthew G Knepley .seealso: DMCreate()
95231266bc0SMatthew G. Knepley @*/
953cd0c2139SMatthew G Knepley PetscErrorCode DMPlexConstructGhostCells(DM dm, const char labelName[], PetscInt *numGhostCells, DM *dmGhosted)
954cd0c2139SMatthew G Knepley {
955cd0c2139SMatthew G Knepley   DM             gdm;
956cd0c2139SMatthew G Knepley   DMLabel        label;
957cd0c2139SMatthew G Knepley   const char    *name = labelName ? labelName : "Face Sets";
958412e9a14SMatthew G. Knepley   PetscInt       dim, Ng = 0;
959b0441da4SMatthew G. Knepley   PetscBool      useCone, useClosure;
960cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
961cd0c2139SMatthew G Knepley 
962cd0c2139SMatthew G Knepley   PetscFunctionBegin;
963cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9644a6cfa73SMatthew G. Knepley   if (numGhostCells) PetscValidPointer(numGhostCells, 3);
965cd0c2139SMatthew G Knepley   PetscValidPointer(dmGhosted, 4);
966cd0c2139SMatthew G Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), &gdm);CHKERRQ(ierr);
967cd0c2139SMatthew G Knepley   ierr = DMSetType(gdm, DMPLEX);CHKERRQ(ierr);
968c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
969c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(gdm, dim);CHKERRQ(ierr);
970b0441da4SMatthew G. Knepley   ierr = DMGetBasicAdjacency(dm, &useCone, &useClosure);CHKERRQ(ierr);
971b0441da4SMatthew G. Knepley   ierr = DMSetBasicAdjacency(gdm, useCone,  useClosure);CHKERRQ(ierr);
972c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
973cd0c2139SMatthew G Knepley   if (!label) {
974cd0c2139SMatthew G Knepley     /* Get label for boundary faces */
975c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
976c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
977e752be1aSMatthew G. Knepley     ierr = DMPlexMarkBoundaryFaces(dm, 1, label);CHKERRQ(ierr);
978cd0c2139SMatthew G Knepley   }
979d80ece95SMatthew G. Knepley   ierr = DMPlexConstructGhostCells_Internal(dm, label, &Ng, gdm);CHKERRQ(ierr);
980b0441da4SMatthew G. Knepley   ierr = DMCopyDisc(dm, gdm);CHKERRQ(ierr);
981*e600fa54SMatthew G. Knepley   ierr = DMPlexCopy_Internal(dm, PETSC_TRUE, gdm);CHKERRQ(ierr);
982cad26855SMatthew G. Knepley   gdm->setfromoptionscalled = dm->setfromoptionscalled;
983d80ece95SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
984cd0c2139SMatthew G Knepley   *dmGhosted = gdm;
985cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
986cd0c2139SMatthew G Knepley }
987cd0c2139SMatthew G Knepley 
988607ab7a9SMatthew G. Knepley /*
989faedd622SMatthew G. Knepley   We are adding three kinds of points here:
990607ab7a9SMatthew G. Knepley     Replicated:     Copies of points which exist in the mesh, such as vertices identified across a fault
991faedd622SMatthew G. Knepley     Non-replicated: Points which exist on the fault, but are not replicated
992607ab7a9SMatthew G. Knepley     Hybrid:         Entirely new points, such as cohesive cells
993a6ae58d1SMatthew G. Knepley 
994a6ae58d1SMatthew G. Knepley   When creating subsequent cohesive cells, we shift the old hybrid cells to the end of the numbering at
995a6ae58d1SMatthew G. Knepley   each depth so that the new split/hybrid points can be inserted as a block.
996607ab7a9SMatthew G. Knepley */
9977db7e0a7SMatthew G. Knepley static PetscErrorCode DMPlexConstructCohesiveCells_Internal(DM dm, DMLabel label, DMLabel splitLabel, DM sdm)
998cd0c2139SMatthew G Knepley {
999cd0c2139SMatthew G Knepley   MPI_Comm         comm;
1000607ab7a9SMatthew G. Knepley   IS               valueIS;
1001607ab7a9SMatthew G. Knepley   PetscInt         numSP = 0;          /* The number of depths for which we have replicated points */
1002607ab7a9SMatthew G. Knepley   const PetscInt  *values;             /* List of depths for which we have replicated points */
100318c5995bSMatthew G. Knepley   IS              *splitIS;
100418c5995bSMatthew G. Knepley   IS              *unsplitIS;
1005607ab7a9SMatthew G. Knepley   PetscInt        *numSplitPoints;     /* The number of replicated points at each depth */
100618c5995bSMatthew G. Knepley   PetscInt        *numUnsplitPoints;   /* The number of non-replicated points at each depth which still give rise to hybrid points */
100736dbac82SMatthew G. Knepley   PetscInt        *numHybridPoints;    /* The number of new hybrid points at each depth */
100836dbac82SMatthew G. Knepley   PetscInt        *numHybridPointsOld; /* The number of existing hybrid points at each depth */
1009607ab7a9SMatthew G. Knepley   const PetscInt **splitPoints;        /* Replicated points for each depth */
101018c5995bSMatthew G. Knepley   const PetscInt **unsplitPoints;      /* Non-replicated points for each depth */
1011cd0c2139SMatthew G Knepley   PetscSection     coordSection;
1012cd0c2139SMatthew G Knepley   Vec              coordinates;
1013cd0c2139SMatthew G Knepley   PetscScalar     *coords;
1014a6ae58d1SMatthew G. Knepley   PetscInt        *depthMax;           /* The first hybrid point at each depth in the original mesh */
1015a6ae58d1SMatthew G. Knepley   PetscInt        *depthEnd;           /* The point limit at each depth in the original mesh */
1016607ab7a9SMatthew G. Knepley   PetscInt        *depthShift;         /* Number of replicated+hybrid points at each depth */
1017607ab7a9SMatthew G. Knepley   PetscInt        *pMaxNew;            /* The first replicated point at each depth in the new mesh, hybrids come after this */
1018607ab7a9SMatthew G. Knepley   PetscInt        *coneNew, *coneONew, *supportNew;
101918c5995bSMatthew G. Knepley   PetscInt         shift = 100, shift2 = 200, depth = 0, dep, dim, d, sp, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, numLabels, vStart, vEnd, pEnd, p, v;
1020cd0c2139SMatthew G Knepley   PetscErrorCode   ierr;
1021cd0c2139SMatthew G Knepley 
1022cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1023cd0c2139SMatthew G Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
1024c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1025607ab7a9SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1026fd4b9f15SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1027412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
1028412e9a14SMatthew G. Knepley   ierr = DMCreateLabel(sdm, "celltype");CHKERRQ(ierr);
1029cd0c2139SMatthew G Knepley   /* Count split points and add cohesive cells */
1030607ab7a9SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
10312582d50cSToby Isaac   ierr = PetscMalloc5(depth+1,&depthMax,depth+1,&depthEnd,2*(depth+1),&depthShift,depth+1,&pMaxNew,depth+1,&numHybridPointsOld);CHKERRQ(ierr);
1032dcca6d9dSJed 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);
1033607ab7a9SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
1034607ab7a9SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, d, NULL, &pMaxNew[d]);CHKERRQ(ierr);
1035412e9a14SMatthew G. Knepley     ierr = DMPlexGetTensorPrismBounds_Internal(dm, d, &depthMax[d], NULL);CHKERRQ(ierr);
1036a6ae58d1SMatthew G. Knepley     depthEnd[d]           = pMaxNew[d];
1037a6ae58d1SMatthew G. Knepley     depthMax[d]           = depthMax[d] < 0 ? depthEnd[d] : depthMax[d];
1038607ab7a9SMatthew G. Knepley     numSplitPoints[d]     = 0;
103918c5995bSMatthew G. Knepley     numUnsplitPoints[d]   = 0;
1040607ab7a9SMatthew G. Knepley     numHybridPoints[d]    = 0;
1041a6ae58d1SMatthew G. Knepley     numHybridPointsOld[d] = depthMax[d] < 0 ? 0 : depthEnd[d] - depthMax[d];
1042607ab7a9SMatthew G. Knepley     splitPoints[d]        = NULL;
104318c5995bSMatthew G. Knepley     unsplitPoints[d]      = NULL;
104418c5995bSMatthew G. Knepley     splitIS[d]            = NULL;
104518c5995bSMatthew G. Knepley     unsplitIS[d]          = NULL;
104659eef20bSToby Isaac     /* we are shifting the existing hybrid points with the stratum behind them, so
104759eef20bSToby Isaac      * the split comes at the end of the normal points, i.e., at depthMax[d] */
104859eef20bSToby Isaac     depthShift[2*d]       = depthMax[d];
104959eef20bSToby Isaac     depthShift[2*d+1]     = 0;
1050607ab7a9SMatthew G. Knepley   }
1051cd0c2139SMatthew G Knepley   if (label) {
1052cd0c2139SMatthew G Knepley     ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
1053cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(valueIS, &numSP);CHKERRQ(ierr);
1054cd0c2139SMatthew G Knepley     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
1055cd0c2139SMatthew G Knepley   }
1056cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1057cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1058cd0c2139SMatthew G Knepley 
1059cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
106018c5995bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, dep, &splitIS[dep]);CHKERRQ(ierr);
106118c5995bSMatthew G. Knepley     if (splitIS[dep]) {
106218c5995bSMatthew G. Knepley       ierr = ISGetLocalSize(splitIS[dep], &numSplitPoints[dep]);CHKERRQ(ierr);
106318c5995bSMatthew G. Knepley       ierr = ISGetIndices(splitIS[dep], &splitPoints[dep]);CHKERRQ(ierr);
106418c5995bSMatthew G. Knepley     }
106518c5995bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, shift2+dep, &unsplitIS[dep]);CHKERRQ(ierr);
106618c5995bSMatthew G. Knepley     if (unsplitIS[dep]) {
106718c5995bSMatthew G. Knepley       ierr = ISGetLocalSize(unsplitIS[dep], &numUnsplitPoints[dep]);CHKERRQ(ierr);
106818c5995bSMatthew G. Knepley       ierr = ISGetIndices(unsplitIS[dep], &unsplitPoints[dep]);CHKERRQ(ierr);
1069cd0c2139SMatthew G Knepley     }
1070cd0c2139SMatthew G Knepley   }
1071607ab7a9SMatthew G. Knepley   /* Calculate number of hybrid points */
107218c5995bSMatthew 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   */
10732582d50cSToby Isaac   for (d = 0; d <= depth; ++d) depthShift[2*d+1]      = numSplitPoints[d] + numHybridPoints[d];
10743d3eaba7SBarry Smith   ierr = DMPlexShiftPointSetUp_Internal(depth,depthShift);CHKERRQ(ierr);
107559eef20bSToby Isaac   /* the end of the points in this stratum that come before the new points:
107659eef20bSToby Isaac    * shifting pMaxNew[d] gets the new start of the next stratum, then count back the old hybrid points and the newly
107759eef20bSToby Isaac    * added points */
10782582d50cSToby Isaac   for (d = 0; d <= depth; ++d) pMaxNew[d]             = DMPlexShiftPoint_Internal(pMaxNew[d],depth,depthShift) - (numHybridPointsOld[d] + numSplitPoints[d] + numHybridPoints[d]);
1079cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSizes_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
1080cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
1081cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1082cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1083cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
10842582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
10854c367dbcSMatthew G. Knepley       const PetscInt  splitp = p    + pMaxNew[dep];
1086cd0c2139SMatthew G Knepley       const PetscInt *support;
1087394c2f0fSMatthew G. Knepley       DMPolytopeType  ct;
10884c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, qn, qp, e;
1089cd0c2139SMatthew G Knepley 
1090cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
1091cd0c2139SMatthew G Knepley       ierr = DMPlexSetConeSize(sdm, splitp, coneSize);CHKERRQ(ierr);
1092cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
1093cd0c2139SMatthew G Knepley       ierr = DMPlexSetSupportSize(sdm, splitp, supportSize);CHKERRQ(ierr);
1094394c2f0fSMatthew G. Knepley       ierr = DMPlexGetCellType(dm, oldp, &ct);CHKERRQ(ierr);
1095394c2f0fSMatthew G. Knepley       ierr = DMPlexSetCellType(sdm, splitp, ct);CHKERRQ(ierr);
1096cd0c2139SMatthew G Knepley       if (dep == depth-1) {
10974c367dbcSMatthew G. Knepley         const PetscInt hybcell = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
10984c367dbcSMatthew G. Knepley 
1099cd0c2139SMatthew G Knepley         /* Add cohesive cells, they are prisms */
11004c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybcell, 2 + coneSize);CHKERRQ(ierr);
1101412e9a14SMatthew G. Knepley         switch (coneSize) {
1102412e9a14SMatthew G. Knepley           case 2: ierr = DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_SEG_PRISM_TENSOR);CHKERRQ(ierr);break;
1103412e9a14SMatthew G. Knepley           case 3: ierr = DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_TRI_PRISM_TENSOR);CHKERRQ(ierr);break;
1104412e9a14SMatthew G. Knepley           case 4: ierr = DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_QUAD_PRISM_TENSOR);CHKERRQ(ierr);break;
1105412e9a14SMatthew G. Knepley         }
1106cd0c2139SMatthew G Knepley       } else if (dep == 0) {
11074c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1108cd0c2139SMatthew G Knepley 
1109cd0c2139SMatthew G Knepley         ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
11104c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1111cd0c2139SMatthew G Knepley           PetscInt val;
1112cd0c2139SMatthew G Knepley 
1113cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
11144c367dbcSMatthew G. Knepley           if (val == 1) ++qf;
11154c367dbcSMatthew G. Knepley           if ((val == 1) || (val ==  (shift + 1))) ++qn;
11164c367dbcSMatthew G. Knepley           if ((val == 1) || (val == -(shift + 1))) ++qp;
1117cd0c2139SMatthew G Knepley         }
11184c367dbcSMatthew G. Knepley         /* Split old vertex: Edges into original vertex and new cohesive edge */
11194c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qn+1);CHKERRQ(ierr);
11204c367dbcSMatthew G. Knepley         /* Split new vertex: Edges into split vertex and new cohesive edge */
11214c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, splitp, qp+1);CHKERRQ(ierr);
11224c367dbcSMatthew G. Knepley         /* Add hybrid edge */
11234c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybedge, 2);CHKERRQ(ierr);
11244c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybedge, qf);CHKERRQ(ierr);
1125412e9a14SMatthew G. Knepley         ierr = DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR);CHKERRQ(ierr);
1126cd0c2139SMatthew G Knepley       } else if (dep == dim-2) {
11274c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
11284c367dbcSMatthew G. Knepley 
1129cd0c2139SMatthew G Knepley         ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
11304c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1131cd0c2139SMatthew G Knepley           PetscInt val;
1132cd0c2139SMatthew G Knepley 
1133cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
11344c367dbcSMatthew G. Knepley           if (val == dim-1) ++qf;
11354c367dbcSMatthew G. Knepley           if ((val == dim-1) || (val ==  (shift + dim-1))) ++qn;
11364c367dbcSMatthew G. Knepley           if ((val == dim-1) || (val == -(shift + dim-1))) ++qp;
1137cd0c2139SMatthew G Knepley         }
11384c367dbcSMatthew G. Knepley         /* Split old edge: Faces into original edge and cohesive face (positive side?) */
11394c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qn+1);CHKERRQ(ierr);
11404c367dbcSMatthew G. Knepley         /* Split new edge: Faces into split edge and cohesive face (negative side?) */
11414c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, splitp, qp+1);CHKERRQ(ierr);
11424c367dbcSMatthew G. Knepley         /* Add hybrid face */
11434c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybface, 4);CHKERRQ(ierr);
11444c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybface, qf);CHKERRQ(ierr);
1145412e9a14SMatthew G. Knepley         ierr = DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR);CHKERRQ(ierr);
1146cd0c2139SMatthew G Knepley       }
1147cd0c2139SMatthew G Knepley     }
1148cd0c2139SMatthew G Knepley   }
114918c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
115018c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
115118c5995bSMatthew G. Knepley       const PetscInt  oldp   = unsplitPoints[dep][p];
11522582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
115318c5995bSMatthew G. Knepley       const PetscInt *support;
1154da1dd7e4SMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, e, s;
115518c5995bSMatthew G. Knepley 
115618c5995bSMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
115718c5995bSMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
115839254ff6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
115918c5995bSMatthew G. Knepley       if (dep == 0) {
116018c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
116118c5995bSMatthew G. Knepley 
116239254ff6SMatthew G. Knepley         /* Unsplit vertex: Edges into original vertex, split edges, and new cohesive edge twice */
116339254ff6SMatthew G. Knepley         for (s = 0, qf = 0; s < supportSize; ++s, ++qf) {
116439254ff6SMatthew G. Knepley           ierr = PetscFindInt(support[s], numSplitPoints[dep+1], splitPoints[dep+1], &e);CHKERRQ(ierr);
116539254ff6SMatthew G. Knepley           if (e >= 0) ++qf;
116639254ff6SMatthew G. Knepley         }
116739254ff6SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qf+2);CHKERRQ(ierr);
116818c5995bSMatthew G. Knepley         /* Add hybrid edge */
116918c5995bSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybedge, 2);CHKERRQ(ierr);
1170e1757548SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1171e1757548SMatthew G. Knepley           PetscInt val;
1172e1757548SMatthew G. Knepley 
1173e1757548SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1174e1757548SMatthew G. Knepley           /* Split and unsplit edges produce hybrid faces */
1175da1dd7e4SMatthew G. Knepley           if (val == 1) ++qf;
1176da1dd7e4SMatthew G. Knepley           if (val == (shift2 + 1)) ++qf;
1177e1757548SMatthew G. Knepley         }
1178e1757548SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybedge, qf);CHKERRQ(ierr);
1179412e9a14SMatthew G. Knepley         ierr = DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR);CHKERRQ(ierr);
118018c5995bSMatthew G. Knepley       } else if (dep == dim-2) {
118118c5995bSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
1182cd0c2139SMatthew G Knepley         PetscInt       val;
1183cd0c2139SMatthew G Knepley 
1184da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1185cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1186da1dd7e4SMatthew G. Knepley           if (val == dim-1) qf += 2;
1187da1dd7e4SMatthew G. Knepley           else              ++qf;
1188cd0c2139SMatthew G Knepley         }
118918c5995bSMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and cohesive face twice */
1190da1dd7e4SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qf+2);CHKERRQ(ierr);
119118c5995bSMatthew G. Knepley         /* Add hybrid face */
1192da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1193da1dd7e4SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1194da1dd7e4SMatthew G. Knepley           if (val == dim-1) ++qf;
1195da1dd7e4SMatthew G. Knepley         }
119618c5995bSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybface, 4);CHKERRQ(ierr);
1197da1dd7e4SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybface, qf);CHKERRQ(ierr);
1198412e9a14SMatthew G. Knepley         ierr = DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR);CHKERRQ(ierr);
1199cd0c2139SMatthew G Knepley       }
1200cd0c2139SMatthew G Knepley     }
1201cd0c2139SMatthew G Knepley   }
1202cd0c2139SMatthew G Knepley   /* Step 4: Setup split DM */
1203cd0c2139SMatthew G Knepley   ierr = DMSetUp(sdm);CHKERRQ(ierr);
1204cd0c2139SMatthew G Knepley   ierr = DMPlexShiftPoints_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
1205dcbb62e8SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(sdm, &maxConeSizeNew, &maxSupportSizeNew);CHKERRQ(ierr);
1206dcca6d9dSJed Brown   ierr = PetscMalloc3(PetscMax(maxConeSize, maxConeSizeNew)*3,&coneNew,PetscMax(maxConeSize, maxConeSizeNew)*3,&coneONew,PetscMax(maxSupportSize, maxSupportSizeNew),&supportNew);CHKERRQ(ierr);
1207cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
1208cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1209cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1210cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
12112582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
12124c367dbcSMatthew G. Knepley       const PetscInt  splitp = p    + pMaxNew[dep];
1213cd0c2139SMatthew G Knepley       const PetscInt *cone, *support, *ornt;
1214b5a892a1SMatthew G. Knepley       DMPolytopeType  ct;
12154c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, q, qf, qn, qp, v, e, s;
1216cd0c2139SMatthew G Knepley 
1217b5a892a1SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, oldp, &ct);CHKERRQ(ierr);
1218cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
1219cd0c2139SMatthew G Knepley       ierr = DMPlexGetCone(dm, oldp, &cone);CHKERRQ(ierr);
1220cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeOrientation(dm, oldp, &ornt);CHKERRQ(ierr);
1221cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
1222cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
1223cd0c2139SMatthew G Knepley       if (dep == depth-1) {
122496a07cd0SMatthew G. Knepley         PetscBool       hasUnsplit = PETSC_FALSE;
12254c367dbcSMatthew G. Knepley         const PetscInt  hybcell    = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1226cd0c2139SMatthew G Knepley         const PetscInt *supportF;
1227cd0c2139SMatthew G Knepley 
1228cd0c2139SMatthew G Knepley         /* Split face:       copy in old face to new face to start */
1229cd0c2139SMatthew G Knepley         ierr = DMPlexGetSupport(sdm, newp,  &supportF);CHKERRQ(ierr);
1230cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, splitp, supportF);CHKERRQ(ierr);
1231cd0c2139SMatthew G Knepley         /* Split old face:   old vertices/edges in cone so no change */
1232cd0c2139SMatthew G Knepley         /* Split new face:   new vertices/edges in cone */
1233cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
12344c367dbcSMatthew G. Knepley           ierr = PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);CHKERRQ(ierr);
123518c5995bSMatthew G. Knepley           if (v < 0) {
123618c5995bSMatthew G. Knepley             ierr = PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
12372c71b3e2SJacob Faibussowitsch             PetscCheckFalse(v < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %d in split or unsplit points of depth %d", cone[q], dep-1);
12382582d50cSToby Isaac             coneNew[2+q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
123996a07cd0SMatthew G. Knepley             hasUnsplit   = PETSC_TRUE;
124018c5995bSMatthew G. Knepley           } else {
12414c367dbcSMatthew G. Knepley             coneNew[2+q] = v + pMaxNew[dep-1];
1242163235baSMatthew G. Knepley             if (dep > 1) {
1243163235baSMatthew G. Knepley               const PetscInt *econe;
1244163235baSMatthew G. Knepley               PetscInt        econeSize, r, vs, vu;
1245163235baSMatthew G. Knepley 
1246163235baSMatthew G. Knepley               ierr = DMPlexGetConeSize(dm, cone[q], &econeSize);CHKERRQ(ierr);
1247163235baSMatthew G. Knepley               ierr = DMPlexGetCone(dm, cone[q], &econe);CHKERRQ(ierr);
1248163235baSMatthew G. Knepley               for (r = 0; r < econeSize; ++r) {
1249163235baSMatthew G. Knepley                 ierr = PetscFindInt(econe[r], numSplitPoints[dep-2],   splitPoints[dep-2],   &vs);CHKERRQ(ierr);
1250163235baSMatthew G. Knepley                 ierr = PetscFindInt(econe[r], numUnsplitPoints[dep-2], unsplitPoints[dep-2], &vu);CHKERRQ(ierr);
1251163235baSMatthew G. Knepley                 if (vs >= 0) continue;
12522c71b3e2SJacob Faibussowitsch                 PetscCheckFalse(vu < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %d in split or unsplit points of depth %d", econe[r], dep-2);
1253163235baSMatthew G. Knepley                 hasUnsplit   = PETSC_TRUE;
1254163235baSMatthew G. Knepley               }
1255163235baSMatthew G. Knepley             }
1256cd0c2139SMatthew G Knepley           }
1257cd0c2139SMatthew G Knepley         }
1258cd0c2139SMatthew G Knepley         ierr = DMPlexSetCone(sdm, splitp, &coneNew[2]);CHKERRQ(ierr);
1259cd0c2139SMatthew G Knepley         ierr = DMPlexSetConeOrientation(sdm, splitp, ornt);CHKERRQ(ierr);
1260e537020bSMatthew G. Knepley         /* Face support */
1261cd0c2139SMatthew G Knepley         for (s = 0; s < supportSize; ++s) {
1262cd0c2139SMatthew G Knepley           PetscInt val;
1263cd0c2139SMatthew G Knepley 
1264cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[s], &val);CHKERRQ(ierr);
1265cd0c2139SMatthew G Knepley           if (val < 0) {
1266cd0c2139SMatthew G Knepley             /* Split old face:   Replace negative side cell with cohesive cell */
12674c367dbcSMatthew G. Knepley              ierr = DMPlexInsertSupport(sdm, newp, s, hybcell);CHKERRQ(ierr);
1268cd0c2139SMatthew G Knepley           } else {
1269cd0c2139SMatthew G Knepley             /* Split new face:   Replace positive side cell with cohesive cell */
12704c367dbcSMatthew G. Knepley             ierr = DMPlexInsertSupport(sdm, splitp, s, hybcell);CHKERRQ(ierr);
1271e537020bSMatthew G. Knepley             /* Get orientation for cohesive face */
1272e537020bSMatthew G. Knepley             {
1273e537020bSMatthew G. Knepley               const PetscInt *ncone, *nconeO;
1274e537020bSMatthew G. Knepley               PetscInt        nconeSize, nc;
1275e537020bSMatthew G. Knepley 
1276e537020bSMatthew G. Knepley               ierr = DMPlexGetConeSize(dm, support[s], &nconeSize);CHKERRQ(ierr);
1277e537020bSMatthew G. Knepley               ierr = DMPlexGetCone(dm, support[s], &ncone);CHKERRQ(ierr);
1278e537020bSMatthew G. Knepley               ierr = DMPlexGetConeOrientation(dm, support[s], &nconeO);CHKERRQ(ierr);
1279e537020bSMatthew G. Knepley               for (nc = 0; nc < nconeSize; ++nc) {
1280e537020bSMatthew G. Knepley                 if (ncone[nc] == oldp) {
1281e537020bSMatthew G. Knepley                   coneONew[0] = nconeO[nc];
1282e537020bSMatthew G. Knepley                   break;
1283cd0c2139SMatthew G Knepley                 }
1284cd0c2139SMatthew G Knepley               }
12852c71b3e2SJacob Faibussowitsch               PetscCheckFalse(nc >= nconeSize,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate face %d in neighboring cell %d", oldp, support[s]);
1286e537020bSMatthew G. Knepley             }
1287e537020bSMatthew G. Knepley           }
1288e537020bSMatthew G. Knepley         }
12894c367dbcSMatthew G. Knepley         /* Cohesive cell:    Old and new split face, then new cohesive faces */
1290b5a892a1SMatthew G. Knepley         const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, coneONew[0]);
1291b5a892a1SMatthew G. Knepley 
1292fd4b9f15SMatthew G. Knepley         coneNew[0]  = newp;   /* Extracted negative side orientation above */
12934c367dbcSMatthew G. Knepley         coneNew[1]  = splitp;
12944c367dbcSMatthew G. Knepley         coneONew[1] = coneONew[0];
1295e537020bSMatthew G. Knepley         for (q = 0; q < coneSize; ++q) {
1296412e9a14SMatthew G. Knepley           /* Hybrid faces must follow order from oriented end face */
1297b5a892a1SMatthew G. Knepley           const PetscInt qa = arr[q*2+0];
1298b5a892a1SMatthew G. Knepley           const PetscInt qo = arr[q*2+1];
1299b5a892a1SMatthew G. Knepley           DMPolytopeType ft = dep == 2 ? DM_POLYTOPE_SEGMENT : DM_POLYTOPE_POINT;
1300412e9a14SMatthew G. Knepley 
1301b5a892a1SMatthew G. Knepley           ierr = PetscFindInt(cone[qa], numSplitPoints[dep-1], splitPoints[dep-1], &v);CHKERRQ(ierr);
130218c5995bSMatthew G. Knepley           if (v < 0) {
1303b5a892a1SMatthew G. Knepley             ierr = PetscFindInt(cone[qa], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
130418c5995bSMatthew G. Knepley             coneNew[2+q]  = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
130518c5995bSMatthew G. Knepley           } else {
130618c5995bSMatthew G. Knepley             coneNew[2+q]  = v + pMaxNew[dep] + numSplitPoints[dep];
130718c5995bSMatthew G. Knepley           }
1308b5a892a1SMatthew G. Knepley           coneONew[2+q] = DMPolytopeTypeComposeOrientation(ft, qo, ornt[qa]);
1309e537020bSMatthew G. Knepley         }
13104c367dbcSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybcell, coneNew);CHKERRQ(ierr);
13114c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeOrientation(sdm, hybcell, coneONew);CHKERRQ(ierr);
131296a07cd0SMatthew G. Knepley         /* Label the hybrid cells on the boundary of the split */
131396a07cd0SMatthew G. Knepley         if (hasUnsplit) {ierr = DMLabelSetValue(label, -hybcell, dim);CHKERRQ(ierr);}
1314cd0c2139SMatthew G Knepley       } else if (dep == 0) {
13154c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1316cd0c2139SMatthew G Knepley 
1317cd0c2139SMatthew G Knepley         /* Split old vertex: Edges in old split faces and new cohesive edge */
13184c367dbcSMatthew G. Knepley         for (e = 0, qn = 0; e < supportSize; ++e) {
1319cd0c2139SMatthew G Knepley           PetscInt val;
1320cd0c2139SMatthew G Knepley 
1321cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1322cd0c2139SMatthew G Knepley           if ((val == 1) || (val == (shift + 1))) {
13232582d50cSToby Isaac             supportNew[qn++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1324cd0c2139SMatthew G Knepley           }
1325cd0c2139SMatthew G Knepley         }
13264c367dbcSMatthew G. Knepley         supportNew[qn] = hybedge;
1327cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
1328cd0c2139SMatthew G Knepley         /* Split new vertex: Edges in new split faces and new cohesive edge */
13294c367dbcSMatthew G. Knepley         for (e = 0, qp = 0; e < supportSize; ++e) {
1330cd0c2139SMatthew G Knepley           PetscInt val, edge;
1331cd0c2139SMatthew G Knepley 
1332cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1333cd0c2139SMatthew G Knepley           if (val == 1) {
13344c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);CHKERRQ(ierr);
13352c71b3e2SJacob Faibussowitsch             PetscCheckFalse(edge < 0,comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
13364c367dbcSMatthew G. Knepley             supportNew[qp++] = edge + pMaxNew[dep+1];
1337cd0c2139SMatthew G Knepley           } else if (val == -(shift + 1)) {
13382582d50cSToby Isaac             supportNew[qp++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1339cd0c2139SMatthew G Knepley           }
1340cd0c2139SMatthew G Knepley         }
13414c367dbcSMatthew G. Knepley         supportNew[qp] = hybedge;
1342cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, splitp, supportNew);CHKERRQ(ierr);
13434c367dbcSMatthew G. Knepley         /* Hybrid edge:    Old and new split vertex */
1344cd0c2139SMatthew G Knepley         coneNew[0] = newp;
1345cd0c2139SMatthew G Knepley         coneNew[1] = splitp;
13464c367dbcSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybedge, coneNew);CHKERRQ(ierr);
13474c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
13484c367dbcSMatthew G. Knepley           PetscInt val, edge;
13494c367dbcSMatthew G. Knepley 
13504c367dbcSMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
13514c367dbcSMatthew G. Knepley           if (val == 1) {
13524c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);CHKERRQ(ierr);
13532c71b3e2SJacob Faibussowitsch             PetscCheckFalse(edge < 0,comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
13544c367dbcSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2];
13554c367dbcSMatthew G. Knepley           }
13564c367dbcSMatthew G. Knepley         }
13574c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybedge, supportNew);CHKERRQ(ierr);
1358cd0c2139SMatthew G Knepley       } else if (dep == dim-2) {
13594c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
13604c367dbcSMatthew G. Knepley 
1361cd0c2139SMatthew G Knepley         /* Split old edge:   old vertices in cone so no change */
1362cd0c2139SMatthew G Knepley         /* Split new edge:   new vertices in cone */
1363cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
13644c367dbcSMatthew G. Knepley           ierr = PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);CHKERRQ(ierr);
1365e1757548SMatthew G. Knepley           if (v < 0) {
1366e1757548SMatthew G. Knepley             ierr = PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
13672c71b3e2SJacob Faibussowitsch             PetscCheckFalse(v < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %d in split or unsplit points of depth %d", cone[q], dep-1);
13682582d50cSToby Isaac             coneNew[q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
1369e1757548SMatthew G. Knepley           } else {
13704c367dbcSMatthew G. Knepley             coneNew[q] = v + pMaxNew[dep-1];
1371cd0c2139SMatthew G Knepley           }
1372e1757548SMatthew G. Knepley         }
1373cd0c2139SMatthew G Knepley         ierr = DMPlexSetCone(sdm, splitp, coneNew);CHKERRQ(ierr);
1374cd0c2139SMatthew G Knepley         /* Split old edge: Faces in positive side cells and old split faces */
1375cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1376cd0c2139SMatthew G Knepley           PetscInt val;
1377cd0c2139SMatthew G Knepley 
1378cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
13794c367dbcSMatthew G. Knepley           if (val == dim-1) {
13802582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
13814c367dbcSMatthew G. Knepley           } else if (val == (shift + dim-1)) {
13822582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1383cd0c2139SMatthew G Knepley           }
1384cd0c2139SMatthew G Knepley         }
1385b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1386cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
1387cd0c2139SMatthew G Knepley         /* Split new edge: Faces in negative side cells and new split faces */
1388cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1389cd0c2139SMatthew G Knepley           PetscInt val, face;
1390cd0c2139SMatthew G Knepley 
1391cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1392cd0c2139SMatthew G Knepley           if (val == dim-1) {
13934c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
13942c71b3e2SJacob Faibussowitsch             PetscCheckFalse(face < 0,comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[e]);
13954c367dbcSMatthew G. Knepley             supportNew[q++] = face + pMaxNew[dep+1];
1396cd0c2139SMatthew G Knepley           } else if (val == -(shift + dim-1)) {
13972582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1398cd0c2139SMatthew G Knepley           }
1399cd0c2139SMatthew G Knepley         }
1400b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1401cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, splitp, supportNew);CHKERRQ(ierr);
14024c367dbcSMatthew G. Knepley         /* Hybrid face */
14034c367dbcSMatthew G. Knepley         coneNew[0] = newp;
14044c367dbcSMatthew G. Knepley         coneNew[1] = splitp;
14054c367dbcSMatthew G. Knepley         for (v = 0; v < coneSize; ++v) {
14064c367dbcSMatthew G. Knepley           PetscInt vertex;
14074c367dbcSMatthew G. Knepley           ierr = PetscFindInt(cone[v], numSplitPoints[dep-1], splitPoints[dep-1], &vertex);CHKERRQ(ierr);
1408e1757548SMatthew G. Knepley           if (vertex < 0) {
1409e1757548SMatthew G. Knepley             ierr = PetscFindInt(cone[v], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &vertex);CHKERRQ(ierr);
14102c71b3e2SJacob Faibussowitsch             PetscCheckFalse(vertex < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %d in split or unsplit points of depth %d", cone[v], dep-1);
1411e1757548SMatthew G. Knepley             coneNew[2+v] = vertex + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1412e1757548SMatthew G. Knepley           } else {
14134c367dbcSMatthew G. Knepley             coneNew[2+v] = vertex + pMaxNew[dep] + numSplitPoints[dep];
14144c367dbcSMatthew G. Knepley           }
1415e1757548SMatthew G. Knepley         }
14164c367dbcSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybface, coneNew);CHKERRQ(ierr);
14174c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
14184c367dbcSMatthew G. Knepley           PetscInt val, face;
14194c367dbcSMatthew G. Knepley 
14204c367dbcSMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
14214c367dbcSMatthew G. Knepley           if (val == dim-1) {
14224c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
14232c71b3e2SJacob Faibussowitsch             PetscCheckFalse(face < 0,comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[e]);
14244c367dbcSMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+2] + numSplitPoints[dep+2];
14254c367dbcSMatthew G. Knepley           }
14264c367dbcSMatthew G. Knepley         }
14274c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybface, supportNew);CHKERRQ(ierr);
1428cd0c2139SMatthew G Knepley       }
1429cd0c2139SMatthew G Knepley     }
1430cd0c2139SMatthew G Knepley   }
143118c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
143218c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
143318c5995bSMatthew G. Knepley       const PetscInt  oldp   = unsplitPoints[dep][p];
14342582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
1435b5a892a1SMatthew G. Knepley       const PetscInt *cone, *support;
1436e1757548SMatthew G. Knepley       PetscInt        coneSize, supportSize, supportSizeNew, q, qf, e, f, s;
143718c5995bSMatthew G. Knepley 
143818c5995bSMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
143918c5995bSMatthew G. Knepley       ierr = DMPlexGetCone(dm, oldp, &cone);CHKERRQ(ierr);
144018c5995bSMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
144118c5995bSMatthew G. Knepley       ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
144218c5995bSMatthew G. Knepley       if (dep == 0) {
144318c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
144418c5995bSMatthew G. Knepley 
144518c5995bSMatthew G. Knepley         /* Unsplit vertex */
144618c5995bSMatthew G. Knepley         ierr = DMPlexGetSupportSize(sdm, newp, &supportSizeNew);CHKERRQ(ierr);
144718c5995bSMatthew G. Knepley         for (s = 0, q = 0; s < supportSize; ++s) {
14482582d50cSToby Isaac           supportNew[q++] = DMPlexShiftPoint_Internal(support[s], depth, depthShift) /*support[s] + depthOffset[dep+1]*/;
144918c5995bSMatthew G. Knepley           ierr = PetscFindInt(support[s], numSplitPoints[dep+1], splitPoints[dep+1], &e);CHKERRQ(ierr);
145018c5995bSMatthew G. Knepley           if (e >= 0) {
145118c5995bSMatthew G. Knepley             supportNew[q++] = e + pMaxNew[dep+1];
145218c5995bSMatthew G. Knepley           }
145318c5995bSMatthew G. Knepley         }
145418c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
145518c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
14562c71b3e2SJacob Faibussowitsch         PetscCheckFalse(q != supportSizeNew,comm, PETSC_ERR_ARG_WRONG, "Support size %d != %d for vertex %d", q, supportSizeNew, newp);
145718c5995bSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
145818c5995bSMatthew G. Knepley         /* Hybrid edge */
145918c5995bSMatthew G. Knepley         coneNew[0] = newp;
146018c5995bSMatthew G. Knepley         coneNew[1] = newp;
146118c5995bSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybedge, coneNew);CHKERRQ(ierr);
146218c5995bSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
146318c5995bSMatthew G. Knepley           PetscInt val, edge;
146418c5995bSMatthew G. Knepley 
146518c5995bSMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
146618c5995bSMatthew G. Knepley           if (val == 1) {
146718c5995bSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);CHKERRQ(ierr);
14682c71b3e2SJacob Faibussowitsch             PetscCheckFalse(edge < 0,comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
146918c5995bSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2];
1470e1757548SMatthew G. Knepley           } else if  (val ==  (shift2 + 1)) {
1471e1757548SMatthew G. Knepley             ierr = PetscFindInt(support[e], numUnsplitPoints[dep+1], unsplitPoints[dep+1], &edge);CHKERRQ(ierr);
14722c71b3e2SJacob Faibussowitsch             PetscCheckFalse(edge < 0,comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a unsplit edge", support[e]);
1473e1757548SMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2] + numSplitPoints[dep+1];
147418c5995bSMatthew G. Knepley           }
147518c5995bSMatthew G. Knepley         }
147618c5995bSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybedge, supportNew);CHKERRQ(ierr);
1477e1757548SMatthew G. Knepley       } else if (dep == dim-2) {
1478e1757548SMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
1479e1757548SMatthew G. Knepley 
1480da1dd7e4SMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and hybrid face twice */
1481e1757548SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1482e1757548SMatthew G. Knepley           PetscInt val, face;
1483e1757548SMatthew G. Knepley 
1484e1757548SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[f], &val);CHKERRQ(ierr);
1485e1757548SMatthew G. Knepley           if (val == dim-1) {
1486e1757548SMatthew G. Knepley             ierr = PetscFindInt(support[f], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
14872c71b3e2SJacob Faibussowitsch             PetscCheckFalse(face < 0,comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[f]);
14882582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1489e1757548SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+1];
1490e1757548SMatthew G. Knepley           } else {
14912582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1492e1757548SMatthew G. Knepley           }
1493e1757548SMatthew G. Knepley         }
1494e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
1495e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
1496da1dd7e4SMatthew G. Knepley         ierr = DMPlexGetSupportSize(sdm, newp, &supportSizeNew);CHKERRQ(ierr);
14972c71b3e2SJacob Faibussowitsch         PetscCheckFalse(qf != supportSizeNew,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Support size for unsplit edge %d is %d != %d", newp, qf, supportSizeNew);
1498e1757548SMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
1499e1757548SMatthew G. Knepley         /* Add hybrid face */
1500e1757548SMatthew G. Knepley         coneNew[0] = newp;
1501212cc919SMatthew G. Knepley         coneNew[1] = newp;
1502e1757548SMatthew G. Knepley         ierr = PetscFindInt(cone[0], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
15032c71b3e2SJacob Faibussowitsch         PetscCheckFalse(v < 0,comm, PETSC_ERR_ARG_WRONG, "Vertex %d is not an unsplit vertex", cone[0]);
1504212cc919SMatthew G. Knepley         coneNew[2] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1505e1757548SMatthew G. Knepley         ierr = PetscFindInt(cone[1], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
15062c71b3e2SJacob Faibussowitsch         PetscCheckFalse(v < 0,comm, PETSC_ERR_ARG_WRONG, "Vertex %d is not an unsplit vertex", cone[1]);
1507e1757548SMatthew G. Knepley         coneNew[3] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1508e1757548SMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybface, coneNew);CHKERRQ(ierr);
1509da1dd7e4SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1510da1dd7e4SMatthew G. Knepley           PetscInt val, face;
1511da1dd7e4SMatthew G. Knepley 
1512da1dd7e4SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[f], &val);CHKERRQ(ierr);
1513da1dd7e4SMatthew G. Knepley           if (val == dim-1) {
1514da1dd7e4SMatthew G. Knepley             ierr = PetscFindInt(support[f], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
1515da1dd7e4SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+2] + numSplitPoints[dep+2];
1516da1dd7e4SMatthew G. Knepley           }
1517da1dd7e4SMatthew G. Knepley         }
1518da1dd7e4SMatthew G. Knepley         ierr = DMPlexGetSupportSize(sdm, hybface, &supportSizeNew);CHKERRQ(ierr);
15192c71b3e2SJacob Faibussowitsch         PetscCheckFalse(qf != supportSizeNew,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Support size for hybrid face %d is %d != %d", hybface, qf, supportSizeNew);
1520e1757548SMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybface, supportNew);CHKERRQ(ierr);
1521cd0c2139SMatthew G Knepley       }
1522cd0c2139SMatthew G Knepley     }
1523cd0c2139SMatthew G Knepley   }
1524cd0c2139SMatthew G Knepley   /* Step 6b: Replace split points in negative side cones */
1525cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1526cd0c2139SMatthew G Knepley     PetscInt        dep = values[sp];
1527cd0c2139SMatthew G Knepley     IS              pIS;
1528cd0c2139SMatthew G Knepley     PetscInt        numPoints;
1529cd0c2139SMatthew G Knepley     const PetscInt *points;
1530cd0c2139SMatthew G Knepley 
1531cd0c2139SMatthew G Knepley     if (dep >= 0) continue;
1532cd0c2139SMatthew G Knepley     ierr = DMLabelGetStratumIS(label, dep, &pIS);CHKERRQ(ierr);
1533cd0c2139SMatthew G Knepley     if (!pIS) continue;
1534cd0c2139SMatthew G Knepley     dep  = -dep - shift;
1535cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(pIS, &numPoints);CHKERRQ(ierr);
1536cd0c2139SMatthew G Knepley     ierr = ISGetIndices(pIS, &points);CHKERRQ(ierr);
1537cd0c2139SMatthew G Knepley     for (p = 0; p < numPoints; ++p) {
1538cd0c2139SMatthew G Knepley       const PetscInt  oldp = points[p];
15392582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*depthOffset[dep] + oldp*/;
1540cd0c2139SMatthew G Knepley       const PetscInt *cone;
1541cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
154250cf782dSMatthew G. Knepley       /* PetscBool       replaced = PETSC_FALSE; */
1543cd0c2139SMatthew G Knepley 
1544cd0c2139SMatthew G Knepley       /* Negative edge: replace split vertex */
1545cd0c2139SMatthew G Knepley       /* Negative cell: replace split face */
1546cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(sdm, newp, &coneSize);CHKERRQ(ierr);
1547cd0c2139SMatthew G Knepley       ierr = DMPlexGetCone(sdm, newp, &cone);CHKERRQ(ierr);
1548cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
1549e38fbfedSToby Isaac         const PetscInt coldp = DMPlexShiftPointInverse_Internal(cone[c],depth,depthShift);
1550cd0c2139SMatthew G Knepley         PetscInt       csplitp, cp, val;
1551cd0c2139SMatthew G Knepley 
1552cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(label, coldp, &val);CHKERRQ(ierr);
1553cd0c2139SMatthew G Knepley         if (val == dep-1) {
1554cd0c2139SMatthew G Knepley           ierr = PetscFindInt(coldp, numSplitPoints[dep-1], splitPoints[dep-1], &cp);CHKERRQ(ierr);
15552c71b3e2SJacob Faibussowitsch           PetscCheckFalse(cp < 0,comm, PETSC_ERR_ARG_WRONG, "Point %d is not a split point of dimension %d", oldp, dep-1);
1556cd0c2139SMatthew G Knepley           csplitp  = pMaxNew[dep-1] + cp;
1557cd0c2139SMatthew G Knepley           ierr     = DMPlexInsertCone(sdm, newp, c, csplitp);CHKERRQ(ierr);
155850cf782dSMatthew G. Knepley           /* replaced = PETSC_TRUE; */
1559cd0c2139SMatthew G Knepley         }
1560cd0c2139SMatthew G Knepley       }
15614a189a86SMatthew G. Knepley       /* Cells with only a vertex or edge on the submesh have no replacement */
15622c71b3e2SJacob Faibussowitsch       /* PetscCheckFalse(!replaced,comm, PETSC_ERR_ARG_WRONG, "The cone of point %d does not contain split points", oldp); */
1563cd0c2139SMatthew G Knepley     }
1564cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(pIS, &points);CHKERRQ(ierr);
1565cd0c2139SMatthew G Knepley     ierr = ISDestroy(&pIS);CHKERRQ(ierr);
1566cd0c2139SMatthew G Knepley   }
1567fa8e8ae5SToby Isaac   /* Step 7: Coordinates */
1568cd0c2139SMatthew G Knepley   ierr = DMPlexShiftCoordinates_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
156969d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(sdm, &coordSection);CHKERRQ(ierr);
1570cd0c2139SMatthew G Knepley   ierr = DMGetCoordinatesLocal(sdm, &coordinates);CHKERRQ(ierr);
1571cd0c2139SMatthew G Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
1572cd0c2139SMatthew G Knepley   for (v = 0; v < (numSplitPoints ? numSplitPoints[0] : 0); ++v) {
15732582d50cSToby Isaac     const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[0][v], depth, depthShift) /*depthOffset[0] + splitPoints[0][v]*/;
1574cd0c2139SMatthew G Knepley     const PetscInt splitp = pMaxNew[0] + v;
1575cd0c2139SMatthew G Knepley     PetscInt       dof, off, soff, d;
1576cd0c2139SMatthew G Knepley 
1577cd0c2139SMatthew G Knepley     ierr = PetscSectionGetDof(coordSection, newp, &dof);CHKERRQ(ierr);
1578cd0c2139SMatthew G Knepley     ierr = PetscSectionGetOffset(coordSection, newp, &off);CHKERRQ(ierr);
1579cd0c2139SMatthew G Knepley     ierr = PetscSectionGetOffset(coordSection, splitp, &soff);CHKERRQ(ierr);
1580cd0c2139SMatthew G Knepley     for (d = 0; d < dof; ++d) coords[soff+d] = coords[off+d];
1581cd0c2139SMatthew G Knepley   }
1582cd0c2139SMatthew G Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
1583fa8e8ae5SToby Isaac   /* Step 8: SF, if I can figure this out we can split the mesh in parallel */
1584cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSF_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
1585fa8e8ae5SToby Isaac   /* Step 9: Labels */
1586cd0c2139SMatthew G Knepley   ierr = DMPlexShiftLabels_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
1587c58f1c22SToby Isaac   ierr = DMGetNumLabels(sdm, &numLabels);CHKERRQ(ierr);
1588cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1589cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
15902582d50cSToby Isaac       const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[dep][p], depth, depthShift) /*depthOffset[dep] + splitPoints[dep][p]*/;
1591cd0c2139SMatthew G Knepley       const PetscInt splitp = pMaxNew[dep] + p;
1592cd0c2139SMatthew G Knepley       PetscInt       l;
1593cd0c2139SMatthew G Knepley 
15947db7e0a7SMatthew G. Knepley       if (splitLabel) {
15957db7e0a7SMatthew G. Knepley         const PetscInt val = 100 + dep;
15967db7e0a7SMatthew G. Knepley 
15977db7e0a7SMatthew G. Knepley         ierr = DMLabelSetValue(splitLabel, newp,    val);CHKERRQ(ierr);
15987db7e0a7SMatthew G. Knepley         ierr = DMLabelSetValue(splitLabel, splitp, -val);CHKERRQ(ierr);
15997db7e0a7SMatthew G. Knepley       }
1600cd0c2139SMatthew G Knepley       for (l = 0; l < numLabels; ++l) {
1601cd0c2139SMatthew G Knepley         DMLabel     mlabel;
1602cd0c2139SMatthew G Knepley         const char *lname;
1603cd0c2139SMatthew G Knepley         PetscInt    val;
16049a356370SMatthew G. Knepley         PetscBool   isDepth;
1605cd0c2139SMatthew G Knepley 
1606c58f1c22SToby Isaac         ierr = DMGetLabelName(sdm, l, &lname);CHKERRQ(ierr);
16079a356370SMatthew G. Knepley         ierr = PetscStrcmp(lname, "depth", &isDepth);CHKERRQ(ierr);
16089a356370SMatthew G. Knepley         if (isDepth) continue;
1609c58f1c22SToby Isaac         ierr = DMGetLabel(sdm, lname, &mlabel);CHKERRQ(ierr);
1610cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(mlabel, newp, &val);CHKERRQ(ierr);
1611cd0c2139SMatthew G Knepley         if (val >= 0) {
1612cd0c2139SMatthew G Knepley           ierr = DMLabelSetValue(mlabel, splitp, val);CHKERRQ(ierr);
1613cd0c2139SMatthew G Knepley         }
1614cd0c2139SMatthew G Knepley       }
1615cd0c2139SMatthew G Knepley     }
1616cd0c2139SMatthew G Knepley   }
1617cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1618cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1619cd0c2139SMatthew G Knepley 
1620cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
162118c5995bSMatthew G. Knepley     if (splitIS[dep]) {ierr = ISRestoreIndices(splitIS[dep], &splitPoints[dep]);CHKERRQ(ierr);}
162218c5995bSMatthew G. Knepley     ierr = ISDestroy(&splitIS[dep]);CHKERRQ(ierr);
162318c5995bSMatthew G. Knepley     if (unsplitIS[dep]) {ierr = ISRestoreIndices(unsplitIS[dep], &unsplitPoints[dep]);CHKERRQ(ierr);}
162418c5995bSMatthew G. Knepley     ierr = ISDestroy(&unsplitIS[dep]);CHKERRQ(ierr);
1625cd0c2139SMatthew G Knepley   }
1626cd0c2139SMatthew G Knepley   if (label) {
1627cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
1628cd0c2139SMatthew G Knepley     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
1629cd0c2139SMatthew G Knepley   }
16300d4d4d06SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
16310d4d4d06SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(sdm, d, NULL, &pEnd);CHKERRQ(ierr);
163236dbac82SMatthew G. Knepley     pMaxNew[d] = pEnd - numHybridPoints[d] - numHybridPointsOld[d];
16330d4d4d06SMatthew G. Knepley   }
1634dcbb62e8SMatthew G. Knepley   ierr = PetscFree3(coneNew, coneONew, supportNew);CHKERRQ(ierr);
16352582d50cSToby Isaac   ierr = PetscFree5(depthMax, depthEnd, depthShift, pMaxNew, numHybridPointsOld);CHKERRQ(ierr);
163618c5995bSMatthew G. Knepley   ierr = PetscFree7(splitIS, unsplitIS, numSplitPoints, numUnsplitPoints, numHybridPoints, splitPoints, unsplitPoints);CHKERRQ(ierr);
1637cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1638cd0c2139SMatthew G Knepley }
1639cd0c2139SMatthew G Knepley 
1640cd0c2139SMatthew G Knepley /*@C
1641cd0c2139SMatthew G Knepley   DMPlexConstructCohesiveCells - Construct cohesive cells which split the face along an internal interface
1642cd0c2139SMatthew G Knepley 
1643cd0c2139SMatthew G Knepley   Collective on dm
1644cd0c2139SMatthew G Knepley 
1645cd0c2139SMatthew G Knepley   Input Parameters:
1646cd0c2139SMatthew G Knepley + dm - The original DM
164753156dfcSMatthew G. Knepley - label - The label specifying the boundary faces (this could be auto-generated)
1648cd0c2139SMatthew G Knepley 
1649cd0c2139SMatthew G Knepley   Output Parameters:
16507db7e0a7SMatthew G. Knepley + splitLabel - The label containing the split points, or NULL if no output is desired
1651cd0c2139SMatthew G Knepley - dmSplit - The new DM
1652cd0c2139SMatthew G Knepley 
1653cd0c2139SMatthew G Knepley   Level: developer
1654cd0c2139SMatthew G Knepley 
16552be2b188SMatthew G Knepley .seealso: DMCreate(), DMPlexLabelCohesiveComplete()
1656cd0c2139SMatthew G Knepley @*/
16577db7e0a7SMatthew G. Knepley PetscErrorCode DMPlexConstructCohesiveCells(DM dm, DMLabel label, DMLabel splitLabel, DM *dmSplit)
1658cd0c2139SMatthew G Knepley {
1659cd0c2139SMatthew G Knepley   DM             sdm;
1660cd0c2139SMatthew G Knepley   PetscInt       dim;
1661cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
1662cd0c2139SMatthew G Knepley 
1663cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1664cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1665064a246eSJacob Faibussowitsch   PetscValidPointer(dmSplit, 4);
1666cd0c2139SMatthew G Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), &sdm);CHKERRQ(ierr);
1667cd0c2139SMatthew G Knepley   ierr = DMSetType(sdm, DMPLEX);CHKERRQ(ierr);
1668c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1669c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(sdm, dim);CHKERRQ(ierr);
1670cd0c2139SMatthew G Knepley   switch (dim) {
1671cd0c2139SMatthew G Knepley   case 2:
1672cd0c2139SMatthew G Knepley   case 3:
16737db7e0a7SMatthew G. Knepley     ierr = DMPlexConstructCohesiveCells_Internal(dm, label, splitLabel, sdm);CHKERRQ(ierr);
1674cd0c2139SMatthew G Knepley     break;
1675cd0c2139SMatthew G Knepley   default:
167698921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct cohesive cells for dimension %d", dim);
1677cd0c2139SMatthew G Knepley   }
1678cd0c2139SMatthew G Knepley   *dmSplit = sdm;
1679cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1680cd0c2139SMatthew G Knepley }
1681cd0c2139SMatthew G Knepley 
16820f66a230SMatthew G. Knepley /* Returns the side of the surface for a given cell with a face on the surface */
16830f66a230SMatthew G. Knepley static PetscErrorCode GetSurfaceSide_Static(DM dm, DM subdm, PetscInt numSubpoints, const PetscInt *subpoints, PetscInt cell, PetscInt face, PetscBool *pos)
16840f66a230SMatthew G. Knepley {
16850f66a230SMatthew G. Knepley   const PetscInt *cone, *ornt;
16860f66a230SMatthew G. Knepley   PetscInt        dim, coneSize, c;
16870f66a230SMatthew G. Knepley   PetscErrorCode  ierr;
16880f66a230SMatthew G. Knepley 
16890f66a230SMatthew G. Knepley   PetscFunctionBegin;
16900f66a230SMatthew G. Knepley   *pos = PETSC_TRUE;
1691c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
16920f66a230SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
16930f66a230SMatthew G. Knepley   ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
16940f66a230SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, cell, &ornt);CHKERRQ(ierr);
16950f66a230SMatthew G. Knepley   for (c = 0; c < coneSize; ++c) {
16960f66a230SMatthew G. Knepley     if (cone[c] == face) {
16970f66a230SMatthew G. Knepley       PetscInt o = ornt[c];
16980f66a230SMatthew G. Knepley 
16990f66a230SMatthew G. Knepley       if (subdm) {
17000f66a230SMatthew G. Knepley         const PetscInt *subcone, *subornt;
17010f66a230SMatthew G. Knepley         PetscInt        subpoint, subface, subconeSize, sc;
17020f66a230SMatthew G. Knepley 
17030f66a230SMatthew G. Knepley         ierr = PetscFindInt(cell, numSubpoints, subpoints, &subpoint);CHKERRQ(ierr);
17040f66a230SMatthew G. Knepley         ierr = PetscFindInt(face, numSubpoints, subpoints, &subface);CHKERRQ(ierr);
17050f66a230SMatthew G. Knepley         ierr = DMPlexGetConeSize(subdm, subpoint, &subconeSize);CHKERRQ(ierr);
17060f66a230SMatthew G. Knepley         ierr = DMPlexGetCone(subdm, subpoint, &subcone);CHKERRQ(ierr);
17070f66a230SMatthew G. Knepley         ierr = DMPlexGetConeOrientation(subdm, subpoint, &subornt);CHKERRQ(ierr);
17080f66a230SMatthew G. Knepley         for (sc = 0; sc < subconeSize; ++sc) {
17090f66a230SMatthew G. Knepley           if (subcone[sc] == subface) {
17100f66a230SMatthew G. Knepley             o = subornt[0];
17110f66a230SMatthew G. Knepley             break;
17120f66a230SMatthew G. Knepley           }
17130f66a230SMatthew G. Knepley         }
17142c71b3e2SJacob Faibussowitsch         PetscCheckFalse(sc >= subconeSize,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find subpoint %d (%d) in cone for subpoint %d (%d)", subface, face, subpoint, cell);
17150f66a230SMatthew G. Knepley       }
17160f66a230SMatthew G. Knepley       if (o >= 0) *pos = PETSC_TRUE;
17170f66a230SMatthew G. Knepley       else        *pos = PETSC_FALSE;
17180f66a230SMatthew G. Knepley       break;
17190f66a230SMatthew G. Knepley     }
17200f66a230SMatthew G. Knepley   }
17212c71b3e2SJacob Faibussowitsch   PetscCheckFalse(c == coneSize,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Cell %d in split face %d support does not have it in the cone", cell, face);
17220f66a230SMatthew G. Knepley   PetscFunctionReturn(0);
17230f66a230SMatthew G. Knepley }
17240f66a230SMatthew G. Knepley 
1725cd0c2139SMatthew G Knepley /*@
17260f66a230SMatthew G. Knepley   DMPlexLabelCohesiveComplete - Starting with a label marking points on an internal surface, we add all other mesh pieces
1727cd0c2139SMatthew G Knepley   to complete the surface
1728cd0c2139SMatthew G Knepley 
1729cd0c2139SMatthew G Knepley   Input Parameters:
1730cd0c2139SMatthew G Knepley + dm     - The DM
17310f66a230SMatthew G. Knepley . label  - A DMLabel marking the surface
17320f66a230SMatthew G. Knepley . blabel - A DMLabel marking the vertices on the boundary which will not be duplicated, or NULL to find them automatically
1733bb55d314SMatthew G. Knepley . flip   - Flag to flip the submesh normal and replace points on the other side
173447946fd8SMatthew G. Knepley - subdm  - The subDM associated with the label, or NULL
1735cd0c2139SMatthew G Knepley 
1736cd0c2139SMatthew G Knepley   Output Parameter:
1737cd0c2139SMatthew G Knepley . label - A DMLabel marking all surface points
1738cd0c2139SMatthew G Knepley 
17390f66a230SMatthew G. Knepley   Note: The vertices in blabel are called "unsplit" in the terminology from hybrid cell creation.
17400f66a230SMatthew G. Knepley 
1741cd0c2139SMatthew G Knepley   Level: developer
1742cd0c2139SMatthew G Knepley 
17432be2b188SMatthew G Knepley .seealso: DMPlexConstructCohesiveCells(), DMPlexLabelComplete()
1744cd0c2139SMatthew G Knepley @*/
17450f66a230SMatthew G. Knepley PetscErrorCode DMPlexLabelCohesiveComplete(DM dm, DMLabel label, DMLabel blabel, PetscBool flip, DM subdm)
1746cd0c2139SMatthew G Knepley {
1747d90583fdSMatthew G. Knepley   DMLabel         depthLabel;
17488630cb1aSMatthew G. Knepley   IS              dimIS, subpointIS = NULL, facePosIS, faceNegIS, crossEdgeIS = NULL;
174947946fd8SMatthew G. Knepley   const PetscInt *points, *subpoints;
1750bb55d314SMatthew G. Knepley   const PetscInt  rev   = flip ? -1 : 1;
1751412e9a14SMatthew G. Knepley   PetscInt        shift = 100, shift2 = 200, dim, depth, dep, cStart, cEnd, vStart, vEnd, numPoints, numSubpoints, p, val;
1752cd0c2139SMatthew G Knepley   PetscErrorCode  ierr;
1753cd0c2139SMatthew G Knepley 
1754cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1755370472baSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1756370472baSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1757d90583fdSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
175847946fd8SMatthew G. Knepley   if (subdm) {
175997d8846cSMatthew Knepley     ierr = DMPlexGetSubpointIS(subdm, &subpointIS);CHKERRQ(ierr);
176047946fd8SMatthew G. Knepley     if (subpointIS) {
176147946fd8SMatthew G. Knepley       ierr = ISGetLocalSize(subpointIS, &numSubpoints);CHKERRQ(ierr);
176247946fd8SMatthew G. Knepley       ierr = ISGetIndices(subpointIS, &subpoints);CHKERRQ(ierr);
176347946fd8SMatthew G. Knepley     }
176447946fd8SMatthew G. Knepley   }
1765d7c8f101SMatthew G. Knepley   /* Mark cell on the fault, and its faces which touch the fault: cell orientation for face gives the side of the fault */
1766cd0c2139SMatthew G Knepley   ierr = DMLabelGetStratumIS(label, dim-1, &dimIS);CHKERRQ(ierr);
176797d8846cSMatthew Knepley   if (!dimIS) PetscFunctionReturn(0);
1768cd0c2139SMatthew G Knepley   ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
1769cd0c2139SMatthew G Knepley   ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
1770d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault faces */
1771cd0c2139SMatthew G Knepley     const PetscInt *support;
1772cd0c2139SMatthew G Knepley     PetscInt        supportSize, s;
1773cd0c2139SMatthew G Knepley 
1774cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupportSize(dm, points[p], &supportSize);CHKERRQ(ierr);
1775c4419245SMatthew G. Knepley #if 0
1776c4419245SMatthew G. Knepley     if (supportSize != 2) {
1777c4419245SMatthew G. Knepley       const PetscInt *lp;
1778c4419245SMatthew G. Knepley       PetscInt        Nlp, pind;
1779c4419245SMatthew G. Knepley 
1780c4419245SMatthew G. Knepley       /* Check that for a cell with a single support face, that face is in the SF */
1781c4419245SMatthew G. Knepley       /*   THis check only works for the remote side. We would need root side information */
1782c4419245SMatthew G. Knepley       ierr = PetscSFGetGraph(dm->sf, NULL, &Nlp, &lp, NULL);CHKERRQ(ierr);
1783c4419245SMatthew G. Knepley       ierr = PetscFindInt(points[p], Nlp, lp, &pind);CHKERRQ(ierr);
17842c71b3e2SJacob Faibussowitsch       PetscCheckFalse(pind < 0,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Split face %d has %d != 2 supports, and the face is not shared with another process", points[p], supportSize);
1785c4419245SMatthew G. Knepley     }
1786c4419245SMatthew G. Knepley #endif
1787cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupport(dm, points[p], &support);CHKERRQ(ierr);
1788cd0c2139SMatthew G Knepley     for (s = 0; s < supportSize; ++s) {
17890f66a230SMatthew G. Knepley       const PetscInt *cone;
1790cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
17910f66a230SMatthew G. Knepley       PetscBool       pos;
1792cd0c2139SMatthew G Knepley 
17930f66a230SMatthew G. Knepley       ierr = GetSurfaceSide_Static(dm, subdm, numSubpoints, subpoints, support[s], points[p], &pos);CHKERRQ(ierr);
17940f66a230SMatthew G. Knepley       if (pos) {ierr = DMLabelSetValue(label, support[s],  rev*(shift+dim));CHKERRQ(ierr);}
17950f66a230SMatthew G. Knepley       else     {ierr = DMLabelSetValue(label, support[s], -rev*(shift+dim));CHKERRQ(ierr);}
17960f66a230SMatthew G. Knepley       if (rev < 0) pos = !pos ? PETSC_TRUE : PETSC_FALSE;
17970f66a230SMatthew G. Knepley       /* Put faces touching the fault in the label */
1798cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
1799cd0c2139SMatthew G Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
1800cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
1801cd0c2139SMatthew G Knepley         const PetscInt point = cone[c];
1802cd0c2139SMatthew G Knepley 
1803cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1804cd0c2139SMatthew G Knepley         if (val == -1) {
1805cd0c2139SMatthew G Knepley           PetscInt *closure = NULL;
1806cd0c2139SMatthew G Knepley           PetscInt  closureSize, cl;
1807cd0c2139SMatthew G Knepley 
1808cd0c2139SMatthew G Knepley           ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1809cd0c2139SMatthew G Knepley           for (cl = 0; cl < closureSize*2; cl += 2) {
1810cd0c2139SMatthew G Knepley             const PetscInt clp  = closure[cl];
1811a0541d8aSMatthew G. Knepley             PetscInt       bval = -1;
1812cd0c2139SMatthew G Knepley 
1813cd0c2139SMatthew G Knepley             ierr = DMLabelGetValue(label, clp, &val);CHKERRQ(ierr);
1814a0541d8aSMatthew G. Knepley             if (blabel) {ierr = DMLabelGetValue(blabel, clp, &bval);CHKERRQ(ierr);}
1815a0541d8aSMatthew G. Knepley             if ((val >= 0) && (val < dim-1) && (bval < 0)) {
1816cd0c2139SMatthew G Knepley               ierr = DMLabelSetValue(label, point, pos == PETSC_TRUE ? shift+dim-1 : -(shift+dim-1));CHKERRQ(ierr);
1817cd0c2139SMatthew G Knepley               break;
1818cd0c2139SMatthew G Knepley             }
1819cd0c2139SMatthew G Knepley           }
1820cd0c2139SMatthew G Knepley           ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1821cd0c2139SMatthew G Knepley         }
1822cd0c2139SMatthew G Knepley       }
1823cd0c2139SMatthew G Knepley     }
1824cd0c2139SMatthew G Knepley   }
18250f66a230SMatthew G. Knepley   ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
18260f66a230SMatthew G. Knepley   ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
182747946fd8SMatthew G. Knepley   if (subpointIS) {ierr = ISRestoreIndices(subpointIS, &subpoints);CHKERRQ(ierr);}
1828a0541d8aSMatthew G. Knepley   /* Mark boundary points as unsplit */
182986200784SMatthew G. Knepley   if (blabel) {
1830a0541d8aSMatthew G. Knepley     ierr = DMLabelGetStratumIS(blabel, 1, &dimIS);CHKERRQ(ierr);
1831a0541d8aSMatthew G. Knepley     ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
1832a0541d8aSMatthew G. Knepley     ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
1833a0541d8aSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
1834a0541d8aSMatthew G. Knepley       const PetscInt point = points[p];
1835a0541d8aSMatthew G. Knepley       PetscInt       val, bval;
1836a0541d8aSMatthew G. Knepley 
1837a0541d8aSMatthew G. Knepley       ierr = DMLabelGetValue(blabel, point, &bval);CHKERRQ(ierr);
1838a0541d8aSMatthew G. Knepley       if (bval >= 0) {
1839f7019248SMatthew G. Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1840f7019248SMatthew G. Knepley         if ((val < 0) || (val > dim)) {
1841f7019248SMatthew G. Knepley           /* This could be a point added from splitting a vertex on an adjacent fault, otherwise its just wrong */
1842f7019248SMatthew G. Knepley           ierr = DMLabelClearValue(blabel, point, bval);CHKERRQ(ierr);
1843f7019248SMatthew G. Knepley         }
1844f7019248SMatthew G. Knepley       }
1845f7019248SMatthew G. Knepley     }
1846f7019248SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
1847f7019248SMatthew G. Knepley       const PetscInt point = points[p];
1848f7019248SMatthew G. Knepley       PetscInt       val, bval;
1849f7019248SMatthew G. Knepley 
1850f7019248SMatthew G. Knepley       ierr = DMLabelGetValue(blabel, point, &bval);CHKERRQ(ierr);
1851f7019248SMatthew G. Knepley       if (bval >= 0) {
185286200784SMatthew G. Knepley         const PetscInt *cone,    *support;
185386200784SMatthew G. Knepley         PetscInt        coneSize, supportSize, s, valA, valB, valE;
185486200784SMatthew G. Knepley 
1855a0541d8aSMatthew G. Knepley         /* Mark as unsplit */
1856a0541d8aSMatthew G. Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
18572c71b3e2SJacob Faibussowitsch         PetscCheckFalse((val < 0) || (val > dim),PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %d has label value %d, should be part of the fault", point, val);
1858a0541d8aSMatthew G. Knepley         ierr = DMLabelClearValue(label, point, val);CHKERRQ(ierr);
1859a0541d8aSMatthew G. Knepley         ierr = DMLabelSetValue(label, point, shift2+val);CHKERRQ(ierr);
18602c06a818SMatthew G. Knepley         /* Check for cross-edge
18612c06a818SMatthew G. Knepley              A cross-edge has endpoints which are both on the boundary of the surface, but the edge itself is not. */
186286200784SMatthew G. Knepley         if (val != 0) continue;
186386200784SMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
186486200784SMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
186586200784SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
186686200784SMatthew G. Knepley           ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
186786200784SMatthew G. Knepley           ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
18682c71b3e2SJacob Faibussowitsch           PetscCheckFalse(coneSize != 2,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Edge %D has %D vertices != 2", support[s], coneSize);
186986200784SMatthew G. Knepley           ierr = DMLabelGetValue(blabel, cone[0], &valA);CHKERRQ(ierr);
187086200784SMatthew G. Knepley           ierr = DMLabelGetValue(blabel, cone[1], &valB);CHKERRQ(ierr);
187186200784SMatthew G. Knepley           ierr = DMLabelGetValue(blabel, support[s], &valE);CHKERRQ(ierr);
187209816f77SMatthew G. Knepley           if ((valE < 0) && (valA >= 0) && (valB >= 0) && (cone[0] != cone[1])) {ierr = DMLabelSetValue(blabel, support[s], 2);CHKERRQ(ierr);}
187386200784SMatthew G. Knepley         }
1874a0541d8aSMatthew G. Knepley       }
1875a0541d8aSMatthew G. Knepley     }
1876a0541d8aSMatthew G. Knepley     ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
1877a0541d8aSMatthew G. Knepley     ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
1878a0541d8aSMatthew G. Knepley   }
1879cd0c2139SMatthew G Knepley   /* Search for other cells/faces/edges connected to the fault by a vertex */
1880c4d480a2SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1881412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1882cd0c2139SMatthew G Knepley   ierr = DMLabelGetStratumIS(label, 0, &dimIS);CHKERRQ(ierr);
18837d2fefb6SMatthew G. Knepley   /* TODO Why are we including cross edges here? Shouldn't they be in the star of boundary vertices? */
188486200784SMatthew G. Knepley   if (blabel) {ierr = DMLabelGetStratumIS(blabel, 2, &crossEdgeIS);CHKERRQ(ierr);}
188586200784SMatthew G. Knepley   if (dimIS && crossEdgeIS) {
188686200784SMatthew G. Knepley     IS vertIS = dimIS;
188786200784SMatthew G. Knepley 
188886200784SMatthew G. Knepley     ierr = ISExpand(vertIS, crossEdgeIS, &dimIS);CHKERRQ(ierr);
188986200784SMatthew G. Knepley     ierr = ISDestroy(&crossEdgeIS);CHKERRQ(ierr);
189086200784SMatthew G. Knepley     ierr = ISDestroy(&vertIS);CHKERRQ(ierr);
189186200784SMatthew G. Knepley   }
1892dc86033cSMatthew G. Knepley   if (!dimIS) {
1893dc86033cSMatthew G. Knepley     PetscFunctionReturn(0);
1894dc86033cSMatthew G. Knepley   }
1895cd0c2139SMatthew G Knepley   ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
1896cd0c2139SMatthew G Knepley   ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
1897d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault vertices */
1898cd0c2139SMatthew G Knepley     PetscInt *star = NULL;
189986200784SMatthew G. Knepley     PetscInt  starSize, s;
1900cd0c2139SMatthew G Knepley     PetscInt  again = 1;  /* 0: Finished 1: Keep iterating after a change 2: No change */
1901cd0c2139SMatthew G Knepley 
1902d4622b2cSMatthew G. Knepley     /* All points connected to the fault are inside a cell, so at the top level we will only check cells */
1903cd0c2139SMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
1904cd0c2139SMatthew G Knepley     while (again) {
19052c71b3e2SJacob Faibussowitsch       PetscCheckFalse(again > 1,PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Could not classify all cells connected to the fault");
1906cd0c2139SMatthew G Knepley       again = 0;
1907cd0c2139SMatthew G Knepley       for (s = 0; s < starSize*2; s += 2) {
1908cd0c2139SMatthew G Knepley         const PetscInt  point = star[s];
1909cd0c2139SMatthew G Knepley         const PetscInt *cone;
1910cd0c2139SMatthew G Knepley         PetscInt        coneSize, c;
1911cd0c2139SMatthew G Knepley 
1912412e9a14SMatthew G. Knepley         if ((point < cStart) || (point >= cEnd)) continue;
1913cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1914cd0c2139SMatthew G Knepley         if (val != -1) continue;
1915d4622b2cSMatthew G. Knepley         again = again == 1 ? 1 : 2;
1916cd0c2139SMatthew G Knepley         ierr  = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
1917cd0c2139SMatthew G Knepley         ierr  = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
1918cd0c2139SMatthew G Knepley         for (c = 0; c < coneSize; ++c) {
1919cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, cone[c], &val);CHKERRQ(ierr);
1920cd0c2139SMatthew G Knepley           if (val != -1) {
1921d7c8f101SMatthew G. Knepley             const PetscInt *ccone;
1922166d9d0cSMatthew G. Knepley             PetscInt        cconeSize, cc, side;
1923d7c8f101SMatthew G. Knepley 
19242c71b3e2SJacob Faibussowitsch             PetscCheckFalse(PetscAbs(val) < shift,PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %d on cell %d has an invalid label %d", cone[c], point, val);
192537a6de01SMatthew G. Knepley             if (val > 0) side =  1;
192637a6de01SMatthew G. Knepley             else         side = -1;
192737a6de01SMatthew G. Knepley             ierr = DMLabelSetValue(label, point, side*(shift+dim));CHKERRQ(ierr);
1928d7c8f101SMatthew G. Knepley             /* Mark cell faces which touch the fault */
1929d7c8f101SMatthew G. Knepley             ierr = DMPlexGetConeSize(dm, point, &cconeSize);CHKERRQ(ierr);
1930d7c8f101SMatthew G. Knepley             ierr = DMPlexGetCone(dm, point, &ccone);CHKERRQ(ierr);
1931d7c8f101SMatthew G. Knepley             for (cc = 0; cc < cconeSize; ++cc) {
1932d7c8f101SMatthew G. Knepley               PetscInt *closure = NULL;
1933d7c8f101SMatthew G. Knepley               PetscInt  closureSize, cl;
1934d7c8f101SMatthew G. Knepley 
1935166d9d0cSMatthew G. Knepley               ierr = DMLabelGetValue(label, ccone[cc], &val);CHKERRQ(ierr);
1936166d9d0cSMatthew G. Knepley               if (val != -1) continue;
1937d7c8f101SMatthew G. Knepley               ierr = DMPlexGetTransitiveClosure(dm, ccone[cc], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1938d4622b2cSMatthew G. Knepley               for (cl = 0; cl < closureSize*2; cl += 2) {
1939d7c8f101SMatthew G. Knepley                 const PetscInt clp = closure[cl];
1940d7c8f101SMatthew G. Knepley 
1941d7c8f101SMatthew G. Knepley                 ierr = DMLabelGetValue(label, clp, &val);CHKERRQ(ierr);
1942d7c8f101SMatthew G. Knepley                 if (val == -1) continue;
1943d7c8f101SMatthew G. Knepley                 ierr = DMLabelSetValue(label, ccone[cc], side*(shift+dim-1));CHKERRQ(ierr);
194437a6de01SMatthew G. Knepley                 break;
194537a6de01SMatthew G. Knepley               }
1946d7c8f101SMatthew G. Knepley               ierr = DMPlexRestoreTransitiveClosure(dm, ccone[cc], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1947cd0c2139SMatthew G Knepley             }
1948cd0c2139SMatthew G Knepley             again = 1;
1949cd0c2139SMatthew G Knepley             break;
1950cd0c2139SMatthew G Knepley           }
1951cd0c2139SMatthew G Knepley         }
1952cd0c2139SMatthew G Knepley       }
1953cd0c2139SMatthew G Knepley     }
1954cd0c2139SMatthew G Knepley     /* Classify the rest by cell membership */
1955cd0c2139SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
1956cd0c2139SMatthew G Knepley       const PetscInt point = star[s];
1957cd0c2139SMatthew G Knepley 
1958cd0c2139SMatthew G Knepley       ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1959cd0c2139SMatthew G Knepley       if (val == -1) {
1960cd0c2139SMatthew G Knepley         PetscInt      *sstar = NULL;
1961cd0c2139SMatthew G Knepley         PetscInt       sstarSize, ss;
1962412e9a14SMatthew G. Knepley         PetscBool      marked = PETSC_FALSE, isHybrid;
1963cd0c2139SMatthew G Knepley 
1964cd0c2139SMatthew G Knepley         ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &sstarSize, &sstar);CHKERRQ(ierr);
1965cd0c2139SMatthew G Knepley         for (ss = 0; ss < sstarSize*2; ss += 2) {
1966cd0c2139SMatthew G Knepley           const PetscInt spoint = sstar[ss];
1967cd0c2139SMatthew G Knepley 
1968412e9a14SMatthew G. Knepley           if ((spoint < cStart) || (spoint >= cEnd)) continue;
1969cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, spoint, &val);CHKERRQ(ierr);
19702c71b3e2SJacob Faibussowitsch           PetscCheckFalse(val == -1,PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %d in star of %d does not have a valid label", spoint, point);
1971d90583fdSMatthew G. Knepley           ierr = DMLabelGetValue(depthLabel, point, &dep);CHKERRQ(ierr);
1972cd0c2139SMatthew G Knepley           if (val > 0) {
1973cd0c2139SMatthew G Knepley             ierr = DMLabelSetValue(label, point,   shift+dep);CHKERRQ(ierr);
1974cd0c2139SMatthew G Knepley           } else {
1975cd0c2139SMatthew G Knepley             ierr = DMLabelSetValue(label, point, -(shift+dep));CHKERRQ(ierr);
1976cd0c2139SMatthew G Knepley           }
1977cd0c2139SMatthew G Knepley           marked = PETSC_TRUE;
1978cd0c2139SMatthew G Knepley           break;
1979cd0c2139SMatthew G Knepley         }
1980cd0c2139SMatthew G Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &sstarSize, &sstar);CHKERRQ(ierr);
1981412e9a14SMatthew G. Knepley         ierr = DMPlexCellIsHybrid_Internal(dm, point, &isHybrid);CHKERRQ(ierr);
19822c71b3e2SJacob Faibussowitsch         PetscCheckFalse(!isHybrid && !marked,PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Point %d could not be classified", point);
1983cd0c2139SMatthew G Knepley       }
1984cd0c2139SMatthew G Knepley     }
1985cd0c2139SMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
1986cd0c2139SMatthew G Knepley   }
1987cd0c2139SMatthew G Knepley   ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
1988cd0c2139SMatthew G Knepley   ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
19897d2fefb6SMatthew G. Knepley   /* If any faces touching the fault divide cells on either side, split them
19907d2fefb6SMatthew G. Knepley        This only occurs without a surface boundary */
199118c5995bSMatthew G. Knepley   ierr = DMLabelGetStratumIS(label,   shift+dim-1,  &facePosIS);CHKERRQ(ierr);
199218c5995bSMatthew G. Knepley   ierr = DMLabelGetStratumIS(label, -(shift+dim-1), &faceNegIS);CHKERRQ(ierr);
199318c5995bSMatthew G. Knepley   ierr = ISExpand(facePosIS, faceNegIS, &dimIS);CHKERRQ(ierr);
199418c5995bSMatthew G. Knepley   ierr = ISDestroy(&facePosIS);CHKERRQ(ierr);
199518c5995bSMatthew G. Knepley   ierr = ISDestroy(&faceNegIS);CHKERRQ(ierr);
199618c5995bSMatthew G. Knepley   ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
199718c5995bSMatthew G. Knepley   ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
199818c5995bSMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
199918c5995bSMatthew G. Knepley     const PetscInt  point = points[p];
200018c5995bSMatthew G. Knepley     const PetscInt *support;
200118c5995bSMatthew G. Knepley     PetscInt        supportSize, valA, valB;
200218c5995bSMatthew G. Knepley 
200318c5995bSMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
200418c5995bSMatthew G. Knepley     if (supportSize != 2) continue;
200518c5995bSMatthew G. Knepley     ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
200618c5995bSMatthew G. Knepley     ierr = DMLabelGetValue(label, support[0], &valA);CHKERRQ(ierr);
200718c5995bSMatthew G. Knepley     ierr = DMLabelGetValue(label, support[1], &valB);CHKERRQ(ierr);
200818c5995bSMatthew G. Knepley     if ((valA == -1) || (valB == -1)) continue;
200918c5995bSMatthew G. Knepley     if (valA*valB > 0) continue;
201018c5995bSMatthew G. Knepley     /* Split the face */
201118c5995bSMatthew G. Knepley     ierr = DMLabelGetValue(label, point, &valA);CHKERRQ(ierr);
201218c5995bSMatthew G. Knepley     ierr = DMLabelClearValue(label, point, valA);CHKERRQ(ierr);
201318c5995bSMatthew G. Knepley     ierr = DMLabelSetValue(label, point, dim-1);CHKERRQ(ierr);
2014e1757548SMatthew G. Knepley     /* Label its closure:
2015e1757548SMatthew G. Knepley       unmarked: label as unsplit
2016e1757548SMatthew G. Knepley       incident: relabel as split
2017e1757548SMatthew G. Knepley       split:    do nothing
2018e1757548SMatthew G. Knepley     */
201918c5995bSMatthew G. Knepley     {
202018c5995bSMatthew G. Knepley       PetscInt *closure = NULL;
202118c5995bSMatthew G. Knepley       PetscInt  closureSize, cl;
202218c5995bSMatthew G. Knepley 
202318c5995bSMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
202418c5995bSMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
202518c5995bSMatthew G. Knepley         ierr = DMLabelGetValue(label, closure[cl], &valA);CHKERRQ(ierr);
20268771c1d3SMatthew G. Knepley         if (valA == -1) { /* Mark as unsplit */
202718c5995bSMatthew G. Knepley           ierr = DMLabelGetValue(depthLabel, closure[cl], &dep);CHKERRQ(ierr);
202818c5995bSMatthew G. Knepley           ierr = DMLabelSetValue(label, closure[cl], shift2+dep);CHKERRQ(ierr);
20298771c1d3SMatthew G. Knepley         } else if (((valA >= shift) && (valA < shift2)) || ((valA <= -shift) && (valA > -shift2))) {
2030e1757548SMatthew G. Knepley           ierr = DMLabelGetValue(depthLabel, closure[cl], &dep);CHKERRQ(ierr);
2031e1757548SMatthew G. Knepley           ierr = DMLabelClearValue(label, closure[cl], valA);CHKERRQ(ierr);
2032e1757548SMatthew G. Knepley           ierr = DMLabelSetValue(label, closure[cl], dep);CHKERRQ(ierr);
2033e1757548SMatthew G. Knepley         }
203418c5995bSMatthew G. Knepley       }
203518c5995bSMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
203618c5995bSMatthew G. Knepley     }
203718c5995bSMatthew G. Knepley   }
203818c5995bSMatthew G. Knepley   ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
203918c5995bSMatthew G. Knepley   ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
2040cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
2041cd0c2139SMatthew G Knepley }
2042cd0c2139SMatthew G Knepley 
2043720e594eSMatthew G. Knepley /* Check that no cell have all vertices on the fault */
2044720e594eSMatthew G. Knepley PetscErrorCode DMPlexCheckValidSubmesh_Private(DM dm, DMLabel label, DM subdm)
2045720e594eSMatthew G. Knepley {
2046720e594eSMatthew G. Knepley   IS              subpointIS;
2047720e594eSMatthew G. Knepley   const PetscInt *dmpoints;
2048720e594eSMatthew G. Knepley   PetscInt        defaultValue, cStart, cEnd, c, vStart, vEnd;
2049720e594eSMatthew G. Knepley   PetscErrorCode  ierr;
2050720e594eSMatthew G. Knepley 
2051720e594eSMatthew G. Knepley   PetscFunctionBegin;
2052720e594eSMatthew G. Knepley   if (!label) PetscFunctionReturn(0);
2053720e594eSMatthew G. Knepley   ierr = DMLabelGetDefaultValue(label, &defaultValue);CHKERRQ(ierr);
205497d8846cSMatthew Knepley   ierr = DMPlexGetSubpointIS(subdm, &subpointIS);CHKERRQ(ierr);
2055720e594eSMatthew G. Knepley   if (!subpointIS) PetscFunctionReturn(0);
2056720e594eSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(subdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2057720e594eSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
2058720e594eSMatthew G. Knepley   ierr = ISGetIndices(subpointIS, &dmpoints);CHKERRQ(ierr);
2059720e594eSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2060720e594eSMatthew G. Knepley     PetscBool invalidCell = PETSC_TRUE;
2061720e594eSMatthew G. Knepley     PetscInt *closure     = NULL;
2062720e594eSMatthew G. Knepley     PetscInt  closureSize, cl;
2063720e594eSMatthew G. Knepley 
2064720e594eSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2065720e594eSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
2066720e594eSMatthew G. Knepley       PetscInt value = 0;
2067720e594eSMatthew G. Knepley 
2068720e594eSMatthew G. Knepley       if ((closure[cl] < vStart) || (closure[cl] >= vEnd)) continue;
2069720e594eSMatthew G. Knepley       ierr = DMLabelGetValue(label, closure[cl], &value);CHKERRQ(ierr);
2070720e594eSMatthew G. Knepley       if (value == defaultValue) {invalidCell = PETSC_FALSE; break;}
2071720e594eSMatthew G. Knepley     }
2072720e594eSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2073720e594eSMatthew G. Knepley     if (invalidCell) {
2074720e594eSMatthew G. Knepley       ierr = ISRestoreIndices(subpointIS, &dmpoints);CHKERRQ(ierr);
2075720e594eSMatthew G. Knepley       ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
2076720e594eSMatthew G. Knepley       ierr = DMDestroy(&subdm);CHKERRQ(ierr);
207798921bdaSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ambiguous submesh. Cell %D has all of its vertices on the submesh.", dmpoints[c]);
2078720e594eSMatthew G. Knepley     }
2079720e594eSMatthew G. Knepley   }
2080720e594eSMatthew G. Knepley   ierr = ISRestoreIndices(subpointIS, &dmpoints);CHKERRQ(ierr);
2081720e594eSMatthew G. Knepley   PetscFunctionReturn(0);
2082720e594eSMatthew G. Knepley }
2083720e594eSMatthew G. Knepley 
2084c08575a3SMatthew G. Knepley /*@
20853cf72582SMatthew G. Knepley   DMPlexCreateHybridMesh - Create a mesh with hybrid cells along an internal interface
20863cf72582SMatthew G. Knepley 
20873cf72582SMatthew G. Knepley   Collective on dm
20883cf72582SMatthew G. Knepley 
20893cf72582SMatthew G. Knepley   Input Parameters:
20903cf72582SMatthew G. Knepley + dm - The original DM
2091720e594eSMatthew G. Knepley . label - The label specifying the interface vertices
2092720e594eSMatthew G. Knepley - bdlabel - The optional label specifying the interface boundary vertices
20933cf72582SMatthew G. Knepley 
20943cf72582SMatthew G. Knepley   Output Parameters:
20957db7e0a7SMatthew G. Knepley + hybridLabel - The label fully marking the interface, or NULL if no output is desired
20967db7e0a7SMatthew G. Knepley . splitLabel - The label containing the split points, or NULL if no output is desired
2097720e594eSMatthew G. Knepley . dmInterface - The new interface DM, or NULL
2098720e594eSMatthew G. Knepley - dmHybrid - The new DM with cohesive cells
20993cf72582SMatthew G. Knepley 
21006eccb800SMatthew Knepley   Note: The hybridLabel indicates what parts of the original mesh impinged on the on division surface. For points
21016eccb800SMatthew Knepley   directly on the division surface, they are labeled with their dimension, so an edge 7 on the division surface would be
21026eccb800SMatthew Knepley   7 (1) in hybridLabel. For points that impinge from the positive side, they are labeled with 100+dim, so an edge 6 with
21036eccb800SMatthew 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
21046eccb800SMatthew Knepley   surface also hits vertex 3, it would be 9 (-101) in hybridLabel.
21056eccb800SMatthew Knepley 
21066eccb800SMatthew Knepley   The splitLabel indicates what points in the new hybrid mesh were the result of splitting points in the original
21076eccb800SMatthew Knepley   mesh. The label value is +=100+dim for each point. For example, if two edges 10 and 14 in the hybrid resulting from
21086eccb800SMatthew Knepley   splitting an edge in the original mesh, you would have 10 (101) and 14 (-101) in the splitLabel.
21096eccb800SMatthew Knepley 
21106eccb800SMatthew Knepley   The dmInterface is a DM built from the original division surface. It has a label which can be retrieved using
21116eccb800SMatthew Knepley   DMPlexGetSubpointMap() which maps each point back to the point in the surface of the original mesh.
21126eccb800SMatthew Knepley 
21133cf72582SMatthew G. Knepley   Level: developer
21143cf72582SMatthew G. Knepley 
21156eccb800SMatthew Knepley .seealso: DMPlexConstructCohesiveCells(), DMPlexLabelCohesiveComplete(), DMPlexGetSubpointMap(), DMCreate()
21163cf72582SMatthew G. Knepley @*/
21177db7e0a7SMatthew G. Knepley PetscErrorCode DMPlexCreateHybridMesh(DM dm, DMLabel label, DMLabel bdlabel, DMLabel *hybridLabel, DMLabel *splitLabel, DM *dmInterface, DM *dmHybrid)
21183cf72582SMatthew G. Knepley {
21193cf72582SMatthew G. Knepley   DM             idm;
21207db7e0a7SMatthew G. Knepley   DMLabel        subpointMap, hlabel, slabel = NULL;
21213cf72582SMatthew G. Knepley   PetscInt       dim;
21223cf72582SMatthew G. Knepley   PetscErrorCode ierr;
21233cf72582SMatthew G. Knepley 
21243cf72582SMatthew G. Knepley   PetscFunctionBegin;
21253cf72582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2126720e594eSMatthew G. Knepley   if (bdlabel) PetscValidPointer(bdlabel, 3);
2127720e594eSMatthew G. Knepley   if (hybridLabel) PetscValidPointer(hybridLabel, 4);
21287db7e0a7SMatthew G. Knepley   if (splitLabel)  PetscValidPointer(splitLabel, 5);
21297db7e0a7SMatthew G. Knepley   if (dmInterface) PetscValidPointer(dmInterface, 6);
21307db7e0a7SMatthew G. Knepley   PetscValidPointer(dmHybrid, 7);
2131c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2132158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmesh(dm, label, 1, PETSC_FALSE, &idm);CHKERRQ(ierr);
2133720e594eSMatthew G. Knepley   ierr = DMPlexCheckValidSubmesh_Private(dm, label, idm);CHKERRQ(ierr);
21343cf72582SMatthew G. Knepley   ierr = DMPlexOrient(idm);CHKERRQ(ierr);
21353cf72582SMatthew G. Knepley   ierr = DMPlexGetSubpointMap(idm, &subpointMap);CHKERRQ(ierr);
21363cf72582SMatthew G. Knepley   ierr = DMLabelDuplicate(subpointMap, &hlabel);CHKERRQ(ierr);
21373cf72582SMatthew G. Knepley   ierr = DMLabelClearStratum(hlabel, dim);CHKERRQ(ierr);
21387db7e0a7SMatthew G. Knepley   if (splitLabel) {
21397db7e0a7SMatthew G. Knepley     const char *name;
21407db7e0a7SMatthew G. Knepley     char        sname[PETSC_MAX_PATH_LEN];
21417db7e0a7SMatthew G. Knepley 
2142d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) hlabel, &name);CHKERRQ(ierr);
21437db7e0a7SMatthew G. Knepley     ierr = PetscStrncpy(sname, name, PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
21447db7e0a7SMatthew G. Knepley     ierr = PetscStrcat(sname, " split");CHKERRQ(ierr);
2145d67d17b1SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, sname, &slabel);CHKERRQ(ierr);
21467db7e0a7SMatthew G. Knepley   }
2147720e594eSMatthew G. Knepley   ierr = DMPlexLabelCohesiveComplete(dm, hlabel, bdlabel, PETSC_FALSE, idm);CHKERRQ(ierr);
2148720e594eSMatthew G. Knepley   if (dmInterface) {*dmInterface = idm;}
2149720e594eSMatthew G. Knepley   else             {ierr = DMDestroy(&idm);CHKERRQ(ierr);}
21507db7e0a7SMatthew G. Knepley   ierr = DMPlexConstructCohesiveCells(dm, hlabel, slabel, dmHybrid);CHKERRQ(ierr);
2151*e600fa54SMatthew G. Knepley   ierr = DMPlexCopy_Internal(dm, PETSC_TRUE, *dmHybrid);CHKERRQ(ierr);
21523cf72582SMatthew G. Knepley   if (hybridLabel) *hybridLabel = hlabel;
215353aa2e23SMatthew G. Knepley   else             {ierr = DMLabelDestroy(&hlabel);CHKERRQ(ierr);}
21547db7e0a7SMatthew G. Knepley   if (splitLabel)  *splitLabel  = slabel;
21554a7ee7d0SMatthew G. Knepley   {
21564a7ee7d0SMatthew G. Knepley     DM      cdm;
21574a7ee7d0SMatthew G. Knepley     DMLabel ctLabel;
21584a7ee7d0SMatthew G. Knepley 
21594a7ee7d0SMatthew G. Knepley     /* We need to somehow share the celltype label with the coordinate dm */
21604a7ee7d0SMatthew G. Knepley     ierr = DMGetCoordinateDM(*dmHybrid, &cdm);CHKERRQ(ierr);
21614a7ee7d0SMatthew G. Knepley     ierr = DMPlexGetCellTypeLabel(*dmHybrid, &ctLabel);CHKERRQ(ierr);
21624a7ee7d0SMatthew G. Knepley     ierr = DMSetLabel(cdm, ctLabel);CHKERRQ(ierr);
21634a7ee7d0SMatthew G. Knepley   }
2164cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
2165cd0c2139SMatthew G Knepley }
2166cd0c2139SMatthew G Knepley 
2167efa14ee0SMatthew G Knepley /* Here we need the explicit assumption that:
2168efa14ee0SMatthew G Knepley 
2169efa14ee0SMatthew G Knepley      For any marked cell, the marked vertices constitute a single face
2170efa14ee0SMatthew G Knepley */
2171830e53efSMatthew G. Knepley static PetscErrorCode DMPlexMarkSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, DM subdm)
2172efa14ee0SMatthew G Knepley {
2173fed694aaSMatthew G. Knepley   IS               subvertexIS = NULL;
2174efa14ee0SMatthew G Knepley   const PetscInt  *subvertices;
2175412e9a14SMatthew G. Knepley   PetscInt        *pStart, *pEnd, pSize;
2176efa14ee0SMatthew G Knepley   PetscInt         depth, dim, d, numSubVerticesInitial = 0, v;
2177efa14ee0SMatthew G Knepley   PetscErrorCode   ierr;
2178efa14ee0SMatthew G Knepley 
2179efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2180efa14ee0SMatthew G Knepley   *numFaces = 0;
2181efa14ee0SMatthew G Knepley   *nFV      = 0;
2182efa14ee0SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2183c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
218477d178adSMatthew G. Knepley   pSize = PetscMax(depth, dim) + 1;
2185412e9a14SMatthew G. Knepley   ierr = PetscMalloc2(pSize, &pStart, pSize, &pEnd);CHKERRQ(ierr);
2186efa14ee0SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
2187412e9a14SMatthew G. Knepley     ierr = DMPlexGetSimplexOrBoxCells(dm, depth-d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);
2188efa14ee0SMatthew G Knepley   }
2189efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
2190fed694aaSMatthew G. Knepley   if (vertexLabel) {ierr = DMLabelGetStratumIS(vertexLabel, value, &subvertexIS);CHKERRQ(ierr);}
2191efa14ee0SMatthew G Knepley   if (subvertexIS) {
2192efa14ee0SMatthew G Knepley     ierr = ISGetSize(subvertexIS, &numSubVerticesInitial);CHKERRQ(ierr);
2193efa14ee0SMatthew G Knepley     ierr = ISGetIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
2194efa14ee0SMatthew G Knepley   }
2195efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2196efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
21970298fd71SBarry Smith     PetscInt      *star   = NULL;
2198efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numCells = 0, c;
2199efa14ee0SMatthew G Knepley 
2200efa14ee0SMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2201efa14ee0SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
2202efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2203efa14ee0SMatthew G Knepley       if ((point >= pStart[depth]) && (point < pEnd[depth])) star[numCells++] = point;
2204efa14ee0SMatthew G Knepley     }
2205efa14ee0SMatthew G Knepley     for (c = 0; c < numCells; ++c) {
2206efa14ee0SMatthew G Knepley       const PetscInt cell    = star[c];
22070298fd71SBarry Smith       PetscInt      *closure = NULL;
2208efa14ee0SMatthew G Knepley       PetscInt       closureSize, cl;
2209efa14ee0SMatthew G Knepley       PetscInt       cellLoc, numCorners = 0, faceSize = 0;
2210efa14ee0SMatthew G Knepley 
2211efa14ee0SMatthew G Knepley       ierr = DMLabelGetValue(subpointMap, cell, &cellLoc);CHKERRQ(ierr);
221265560c7fSMatthew G Knepley       if (cellLoc == 2) continue;
22132c71b3e2SJacob Faibussowitsch       PetscCheckFalse(cellLoc >= 0,PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %d has dimension %d in the surface label", cell, cellLoc);
2214efa14ee0SMatthew G Knepley       ierr = DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2215efa14ee0SMatthew G Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
2216efa14ee0SMatthew G Knepley         const PetscInt point = closure[cl];
2217efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2218efa14ee0SMatthew G Knepley 
2219efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
2220efa14ee0SMatthew G Knepley           ++numCorners;
2221efa14ee0SMatthew G Knepley           ierr = DMLabelGetValue(vertexLabel, point, &vertexLoc);CHKERRQ(ierr);
2222830e53efSMatthew G. Knepley           if (vertexLoc == value) closure[faceSize++] = point;
2223efa14ee0SMatthew G Knepley         }
2224efa14ee0SMatthew G Knepley       }
222518ad9376SMatthew G. Knepley       if (!(*nFV)) {ierr = DMPlexGetNumFaceVertices(dm, dim, numCorners, nFV);CHKERRQ(ierr);}
22262c71b3e2SJacob Faibussowitsch       PetscCheckFalse(faceSize > *nFV,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %d of an element on the surface", faceSize);
2227efa14ee0SMatthew G Knepley       if (faceSize == *nFV) {
2228007baee2SMatthew G. Knepley         const PetscInt *cells = NULL;
2229007baee2SMatthew G. Knepley         PetscInt        numCells, nc;
2230007baee2SMatthew G. Knepley 
2231efa14ee0SMatthew G Knepley         ++(*numFaces);
2232efa14ee0SMatthew G Knepley         for (cl = 0; cl < faceSize; ++cl) {
2233efa14ee0SMatthew G Knepley           ierr = DMLabelSetValue(subpointMap, closure[cl], 0);CHKERRQ(ierr);
2234efa14ee0SMatthew G Knepley         }
2235007baee2SMatthew G. Knepley         ierr = DMPlexGetJoin(dm, faceSize, closure, &numCells, &cells);CHKERRQ(ierr);
2236007baee2SMatthew G. Knepley         for (nc = 0; nc < numCells; ++nc) {
2237007baee2SMatthew G. Knepley           ierr = DMLabelSetValue(subpointMap, cells[nc], 2);CHKERRQ(ierr);
2238007baee2SMatthew G. Knepley         }
2239007baee2SMatthew G. Knepley         ierr = DMPlexRestoreJoin(dm, faceSize, closure, &numCells, &cells);CHKERRQ(ierr);
2240efa14ee0SMatthew G Knepley       }
2241efa14ee0SMatthew G Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2242efa14ee0SMatthew G Knepley     }
2243efa14ee0SMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2244efa14ee0SMatthew G Knepley   }
2245efa14ee0SMatthew G Knepley   if (subvertexIS) {
2246efa14ee0SMatthew G Knepley     ierr = ISRestoreIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
2247efa14ee0SMatthew G Knepley   }
2248efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
2249412e9a14SMatthew G. Knepley   ierr = PetscFree2(pStart, pEnd);CHKERRQ(ierr);
2250efa14ee0SMatthew G Knepley   PetscFunctionReturn(0);
2251efa14ee0SMatthew G Knepley }
2252efa14ee0SMatthew G Knepley 
2253158acfadSMatthew G. Knepley static PetscErrorCode DMPlexMarkSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DMLabel subpointMap, DM subdm)
2254efa14ee0SMatthew G Knepley {
225534b4c39eSMatthew G. Knepley   IS               subvertexIS = NULL;
2256efa14ee0SMatthew G Knepley   const PetscInt  *subvertices;
2257412e9a14SMatthew G. Knepley   PetscInt        *pStart, *pEnd;
2258efa14ee0SMatthew G Knepley   PetscInt         dim, d, numSubVerticesInitial = 0, v;
2259efa14ee0SMatthew G Knepley   PetscErrorCode   ierr;
2260efa14ee0SMatthew G Knepley 
2261efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2262c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2263412e9a14SMatthew G. Knepley   ierr = PetscMalloc2(dim+1, &pStart, dim+1, &pEnd);CHKERRQ(ierr);
2264efa14ee0SMatthew G Knepley   for (d = 0; d <= dim; ++d) {
2265412e9a14SMatthew G. Knepley     ierr = DMPlexGetSimplexOrBoxCells(dm, dim-d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);
2266efa14ee0SMatthew G Knepley   }
2267efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
226834b4c39eSMatthew G. Knepley   if (vertexLabel) {
2269830e53efSMatthew G. Knepley     ierr = DMLabelGetStratumIS(vertexLabel, value, &subvertexIS);CHKERRQ(ierr);
2270efa14ee0SMatthew G Knepley     if (subvertexIS) {
2271efa14ee0SMatthew G Knepley       ierr = ISGetSize(subvertexIS, &numSubVerticesInitial);CHKERRQ(ierr);
2272efa14ee0SMatthew G Knepley       ierr = ISGetIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
2273efa14ee0SMatthew G Knepley     }
227434b4c39eSMatthew G. Knepley   }
2275efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2276efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
22770298fd71SBarry Smith     PetscInt      *star   = NULL;
2278efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numFaces = 0, f;
2279efa14ee0SMatthew G Knepley 
2280efa14ee0SMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2281efa14ee0SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
2282efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2283158acfadSMatthew G. Knepley       PetscInt       faceLoc;
2284158acfadSMatthew G. Knepley 
2285158acfadSMatthew G. Knepley       if ((point >= pStart[dim-1]) && (point < pEnd[dim-1])) {
2286158acfadSMatthew G. Knepley         if (markedFaces) {
2287158acfadSMatthew G. Knepley           ierr = DMLabelGetValue(vertexLabel, point, &faceLoc);CHKERRQ(ierr);
2288158acfadSMatthew G. Knepley           if (faceLoc < 0) continue;
2289158acfadSMatthew G. Knepley         }
2290158acfadSMatthew G. Knepley         star[numFaces++] = point;
2291158acfadSMatthew G. Knepley       }
2292efa14ee0SMatthew G Knepley     }
2293efa14ee0SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
2294efa14ee0SMatthew G Knepley       const PetscInt face    = star[f];
22950298fd71SBarry Smith       PetscInt      *closure = NULL;
2296efa14ee0SMatthew G Knepley       PetscInt       closureSize, c;
2297efa14ee0SMatthew G Knepley       PetscInt       faceLoc;
2298efa14ee0SMatthew G Knepley 
2299efa14ee0SMatthew G Knepley       ierr = DMLabelGetValue(subpointMap, face, &faceLoc);CHKERRQ(ierr);
2300efa14ee0SMatthew G Knepley       if (faceLoc == dim-1) continue;
23012c71b3e2SJacob Faibussowitsch       PetscCheckFalse(faceLoc >= 0,PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %d has dimension %d in the surface label", face, faceLoc);
2302efa14ee0SMatthew G Knepley       ierr = DMPlexGetTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2303efa14ee0SMatthew G Knepley       for (c = 0; c < closureSize*2; c += 2) {
2304efa14ee0SMatthew G Knepley         const PetscInt point = closure[c];
2305efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2306efa14ee0SMatthew G Knepley 
2307efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
2308efa14ee0SMatthew G Knepley           ierr = DMLabelGetValue(vertexLabel, point, &vertexLoc);CHKERRQ(ierr);
2309830e53efSMatthew G. Knepley           if (vertexLoc != value) break;
2310efa14ee0SMatthew G Knepley         }
2311efa14ee0SMatthew G Knepley       }
2312efa14ee0SMatthew G Knepley       if (c == closureSize*2) {
2313efa14ee0SMatthew G Knepley         const PetscInt *support;
2314efa14ee0SMatthew G Knepley         PetscInt        supportSize, s;
2315efa14ee0SMatthew G Knepley 
2316efa14ee0SMatthew G Knepley         for (c = 0; c < closureSize*2; c += 2) {
2317efa14ee0SMatthew G Knepley           const PetscInt point = closure[c];
2318efa14ee0SMatthew G Knepley 
2319efa14ee0SMatthew G Knepley           for (d = 0; d < dim; ++d) {
2320efa14ee0SMatthew G Knepley             if ((point >= pStart[d]) && (point < pEnd[d])) {
2321efa14ee0SMatthew G Knepley               ierr = DMLabelSetValue(subpointMap, point, d);CHKERRQ(ierr);
2322efa14ee0SMatthew G Knepley               break;
2323efa14ee0SMatthew G Knepley             }
2324efa14ee0SMatthew G Knepley           }
2325efa14ee0SMatthew G Knepley         }
2326efa14ee0SMatthew G Knepley         ierr = DMPlexGetSupportSize(dm, face, &supportSize);CHKERRQ(ierr);
2327efa14ee0SMatthew G Knepley         ierr = DMPlexGetSupport(dm, face, &support);CHKERRQ(ierr);
2328efa14ee0SMatthew G Knepley         for (s = 0; s < supportSize; ++s) {
2329efa14ee0SMatthew G Knepley           ierr = DMLabelSetValue(subpointMap, support[s], dim);CHKERRQ(ierr);
2330efa14ee0SMatthew G Knepley         }
2331efa14ee0SMatthew G Knepley       }
2332efa14ee0SMatthew G Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2333efa14ee0SMatthew G Knepley     }
2334efa14ee0SMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2335efa14ee0SMatthew G Knepley   }
233634b4c39eSMatthew G. Knepley   if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subvertices);CHKERRQ(ierr);}
2337efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
2338412e9a14SMatthew G. Knepley   ierr = PetscFree2(pStart, pEnd);CHKERRQ(ierr);
2339efa14ee0SMatthew G Knepley   PetscFunctionReturn(0);
2340efa14ee0SMatthew G Knepley }
2341efa14ee0SMatthew G Knepley 
234227c04023SMatthew 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)
2343766ab985SMatthew G. Knepley {
234427c04023SMatthew G. Knepley   DMLabel         label = NULL;
2345766ab985SMatthew G. Knepley   const PetscInt *cone;
23469fc93327SToby Isaac   PetscInt        dim, cMax, cEnd, c, subc = 0, p, coneSize = -1;
2347766ab985SMatthew G. Knepley   PetscErrorCode  ierr;
2348766ab985SMatthew G. Knepley 
2349812bfc34SJed Brown   PetscFunctionBegin;
2350c0ed958bSJed Brown   *numFaces = 0;
2351c0ed958bSJed Brown   *nFV = 0;
2352c58f1c22SToby Isaac   if (labelname) {ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);}
2353fed694aaSMatthew G. Knepley   *subCells = NULL;
2354c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2355412e9a14SMatthew G. Knepley   ierr = DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd);CHKERRQ(ierr);
2356766ab985SMatthew G. Knepley   if (cMax < 0) PetscFunctionReturn(0);
235727c04023SMatthew G. Knepley   if (label) {
235827c04023SMatthew G. Knepley     for (c = cMax; c < cEnd; ++c) {
235927c04023SMatthew G. Knepley       PetscInt val;
236027c04023SMatthew G. Knepley 
236127c04023SMatthew G. Knepley       ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr);
236227c04023SMatthew G. Knepley       if (val == value) {
236327c04023SMatthew G. Knepley         ++(*numFaces);
236427c04023SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
236527c04023SMatthew G. Knepley       }
236627c04023SMatthew G. Knepley     }
236727c04023SMatthew G. Knepley   } else {
2368766ab985SMatthew G. Knepley     *numFaces = cEnd - cMax;
236927c04023SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cMax, &coneSize);CHKERRQ(ierr);
237027c04023SMatthew G. Knepley   }
2371785e854fSJed Brown   ierr = PetscMalloc1(*numFaces *2, subCells);CHKERRQ(ierr);
23729fc93327SToby Isaac   if (!(*numFaces)) PetscFunctionReturn(0);
23739fc93327SToby Isaac   *nFV = hasLagrange ? coneSize/3 : coneSize/2;
2374766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2375766ab985SMatthew G. Knepley     const PetscInt *cells;
2376766ab985SMatthew G. Knepley     PetscInt        numCells;
2377766ab985SMatthew G. Knepley 
237827c04023SMatthew G. Knepley     if (label) {
237927c04023SMatthew G. Knepley       PetscInt val;
238027c04023SMatthew G. Knepley 
238127c04023SMatthew G. Knepley       ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr);
238227c04023SMatthew G. Knepley       if (val != value) continue;
238327c04023SMatthew G. Knepley     }
2384766ab985SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
2385766ab985SMatthew G. Knepley     for (p = 0; p < *nFV; ++p) {
2386766ab985SMatthew G. Knepley       ierr = DMLabelSetValue(subpointMap, cone[p], 0);CHKERRQ(ierr);
2387766ab985SMatthew G. Knepley     }
2388766ab985SMatthew G. Knepley     /* Negative face */
2389766ab985SMatthew G. Knepley     ierr = DMPlexGetJoin(dm, *nFV, cone, &numCells, &cells);CHKERRQ(ierr);
239027234c99SMatthew G. Knepley     /* Not true in parallel
23912c71b3e2SJacob Faibussowitsch     PetscCheckFalse(numCells != 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
2392766ab985SMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
2393766ab985SMatthew G. Knepley       ierr = DMLabelSetValue(subpointMap, cells[p], 2);CHKERRQ(ierr);
239427234c99SMatthew G. Knepley       (*subCells)[subc++] = cells[p];
2395766ab985SMatthew G. Knepley     }
2396766ab985SMatthew G. Knepley     ierr = DMPlexRestoreJoin(dm, *nFV, cone, &numCells, &cells);CHKERRQ(ierr);
2397766ab985SMatthew G. Knepley     /* Positive face is not included */
2398766ab985SMatthew G. Knepley   }
2399766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
2400766ab985SMatthew G. Knepley }
2401766ab985SMatthew G. Knepley 
24023982b651SMatthew G. Knepley static PetscErrorCode DMPlexMarkCohesiveSubmesh_Interpolated(DM dm, DMLabel label, PetscInt value, DMLabel subpointMap, DM subdm)
2403766ab985SMatthew G. Knepley {
2404766ab985SMatthew G. Knepley   PetscInt      *pStart, *pEnd;
2405766ab985SMatthew G. Knepley   PetscInt       dim, cMax, cEnd, c, d;
2406766ab985SMatthew G. Knepley   PetscErrorCode ierr;
2407766ab985SMatthew G. Knepley 
2408812bfc34SJed Brown   PetscFunctionBegin;
2409c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2410412e9a14SMatthew G. Knepley   ierr = DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd);CHKERRQ(ierr);
2411766ab985SMatthew G. Knepley   if (cMax < 0) PetscFunctionReturn(0);
2412dcca6d9dSJed Brown   ierr = PetscMalloc2(dim+1,&pStart,dim+1,&pEnd);CHKERRQ(ierr);
2413b3154360SMatthew G. Knepley   for (d = 0; d <= dim; ++d) {ierr = DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);}
2414766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2415766ab985SMatthew G. Knepley     const PetscInt *cone;
2416766ab985SMatthew G. Knepley     PetscInt       *closure = NULL;
2417b3154360SMatthew G. Knepley     PetscInt        fconeSize, coneSize, closureSize, cl, val;
2418766ab985SMatthew G. Knepley 
241927c04023SMatthew G. Knepley     if (label) {
242027c04023SMatthew G. Knepley       ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr);
242127c04023SMatthew G. Knepley       if (val != value) continue;
242227c04023SMatthew G. Knepley     }
2423766ab985SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
2424766ab985SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
2425b3154360SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cone[0], &fconeSize);CHKERRQ(ierr);
24262c71b3e2SJacob Faibussowitsch     PetscCheckFalse(coneSize != (fconeSize ? fconeSize : 1) + 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells");
2427b3154360SMatthew G. Knepley     /* Negative face */
2428766ab985SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2429766ab985SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
2430766ab985SMatthew G. Knepley       const PetscInt point = closure[cl];
2431766ab985SMatthew G. Knepley 
2432766ab985SMatthew G. Knepley       for (d = 0; d <= dim; ++d) {
2433766ab985SMatthew G. Knepley         if ((point >= pStart[d]) && (point < pEnd[d])) {
2434766ab985SMatthew G. Knepley           ierr = DMLabelSetValue(subpointMap, point, d);CHKERRQ(ierr);
2435766ab985SMatthew G. Knepley           break;
2436766ab985SMatthew G. Knepley         }
2437766ab985SMatthew G. Knepley       }
2438766ab985SMatthew G. Knepley     }
2439766ab985SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2440766ab985SMatthew G. Knepley     /* Cells -- positive face is not included */
2441766ab985SMatthew G. Knepley     for (cl = 0; cl < 1; ++cl) {
2442766ab985SMatthew G. Knepley       const PetscInt *support;
2443766ab985SMatthew G. Knepley       PetscInt        supportSize, s;
2444766ab985SMatthew G. Knepley 
2445766ab985SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[cl], &supportSize);CHKERRQ(ierr);
24462c71b3e2SJacob Faibussowitsch       /* PetscCheckFalse(supportSize != 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive faces should separate two cells"); */
2447766ab985SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[cl], &support);CHKERRQ(ierr);
2448766ab985SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
2449766ab985SMatthew G. Knepley         ierr = DMLabelSetValue(subpointMap, support[s], dim);CHKERRQ(ierr);
2450766ab985SMatthew G. Knepley       }
2451766ab985SMatthew G. Knepley     }
2452766ab985SMatthew G. Knepley   }
2453766ab985SMatthew G. Knepley   ierr = PetscFree2(pStart, pEnd);CHKERRQ(ierr);
2454766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
2455766ab985SMatthew G. Knepley }
2456766ab985SMatthew G. Knepley 
2457c08575a3SMatthew G. Knepley static PetscErrorCode DMPlexGetFaceOrientation(DM dm, PetscInt cell, PetscInt numCorners, PetscInt indices[], PetscInt oppositeVertex, PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2458e6ccafaeSMatthew G Knepley {
245982f516ccSBarry Smith   MPI_Comm       comm;
2460e6ccafaeSMatthew G Knepley   PetscBool      posOrient = PETSC_FALSE;
2461e6ccafaeSMatthew G Knepley   const PetscInt debug     = 0;
2462e6ccafaeSMatthew G Knepley   PetscInt       cellDim, faceSize, f;
2463e6ccafaeSMatthew G Knepley   PetscErrorCode ierr;
2464e6ccafaeSMatthew G Knepley 
246582f516ccSBarry Smith   PetscFunctionBegin;
246682f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2467c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &cellDim);CHKERRQ(ierr);
2468367003a6SStefano Zampini   if (debug) {ierr = PetscPrintf(comm, "cellDim: %d numCorners: %d\n", cellDim, numCorners);CHKERRQ(ierr);}
2469e6ccafaeSMatthew G Knepley 
2470ddeab2a6SMatthew G. Knepley   if (cellDim == 1 && numCorners == 2) {
2471ddeab2a6SMatthew G. Knepley     /* Triangle */
2472e6ccafaeSMatthew G Knepley     faceSize  = numCorners-1;
2473e6ccafaeSMatthew G Knepley     posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2474ddeab2a6SMatthew G. Knepley   } else if (cellDim == 2 && numCorners == 3) {
2475ddeab2a6SMatthew G. Knepley     /* Triangle */
2476ddeab2a6SMatthew G. Knepley     faceSize  = numCorners-1;
2477ddeab2a6SMatthew G. Knepley     posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2478ddeab2a6SMatthew G. Knepley   } else if (cellDim == 3 && numCorners == 4) {
2479ddeab2a6SMatthew G. Knepley     /* Tetrahedron */
2480ddeab2a6SMatthew G. Knepley     faceSize  = numCorners-1;
2481ddeab2a6SMatthew G. Knepley     posOrient = (oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2482e6ccafaeSMatthew G Knepley   } else if (cellDim == 1 && numCorners == 3) {
2483e6ccafaeSMatthew G Knepley     /* Quadratic line */
2484e6ccafaeSMatthew G Knepley     faceSize  = 1;
2485e6ccafaeSMatthew G Knepley     posOrient = PETSC_TRUE;
2486e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 4) {
2487e6ccafaeSMatthew G Knepley     /* Quads */
2488e6ccafaeSMatthew G Knepley     faceSize = 2;
2489e6ccafaeSMatthew G Knepley     if ((indices[1] > indices[0]) && (indices[1] - indices[0] == 1)) {
2490e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2491e6ccafaeSMatthew G Knepley     } else if ((indices[0] == 3) && (indices[1] == 0)) {
2492e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2493e6ccafaeSMatthew G Knepley     } else {
2494e6ccafaeSMatthew G Knepley       if (((indices[0] > indices[1]) && (indices[0] - indices[1] == 1)) || ((indices[0] == 0) && (indices[1] == 3))) {
2495e6ccafaeSMatthew G Knepley         posOrient = PETSC_FALSE;
2496e6ccafaeSMatthew G Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossedge");
2497e6ccafaeSMatthew G Knepley     }
2498e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 6) {
2499e6ccafaeSMatthew G Knepley     /* Quadratic triangle (I hate this) */
2500e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2501e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTri = 3;
2502e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2503e6ccafaeSMatthew G Knepley     PetscBool      found                    = PETSC_FALSE;
2504e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTriSorted[9] = {
2505e6ccafaeSMatthew G Knepley       0, 3,  4, /* bottom */
2506e6ccafaeSMatthew G Knepley       1, 4,  5, /* right */
2507e6ccafaeSMatthew G Knepley       2, 3,  5, /* left */
2508e6ccafaeSMatthew G Knepley     };
2509e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTri[9] = {
2510e6ccafaeSMatthew G Knepley       0, 3,  4, /* bottom */
2511e6ccafaeSMatthew G Knepley       1, 4,  5, /* right */
2512e6ccafaeSMatthew G Knepley       2, 5,  3, /* left */
2513e6ccafaeSMatthew G Knepley     };
2514e6ccafaeSMatthew G Knepley 
2515e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTri; ++i) sortedIndices[i] = indices[i];
2516e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeTri, sortedIndices);CHKERRQ(ierr);
2517e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 3; ++iFace) {
2518e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeTri;
2519e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2520e6ccafaeSMatthew G Knepley 
2521e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesTriSorted[ii+0]) &&
2522e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesTriSorted[ii+1])) {
2523e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTri; ++fVertex) {
2524e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTri; ++cVertex) {
2525e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTri[ii+fVertex]) {
2526e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2527e6ccafaeSMatthew G Knepley               break;
2528e6ccafaeSMatthew G Knepley             }
2529e6ccafaeSMatthew G Knepley           }
2530e6ccafaeSMatthew G Knepley         }
2531e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2532e6ccafaeSMatthew G Knepley         break;
2533e6ccafaeSMatthew G Knepley       }
2534e6ccafaeSMatthew G Knepley     }
25352c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!found,comm, PETSC_ERR_ARG_WRONG, "Invalid tri crossface");
2536e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2537e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2538e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 9) {
2539e6ccafaeSMatthew G Knepley     /* Quadratic quad (I hate this) */
2540e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2541e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuad = 3;
2542e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2543e6ccafaeSMatthew G Knepley     PetscBool      found                      = PETSC_FALSE;
2544e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadSorted[12] = {
2545e6ccafaeSMatthew G Knepley       0, 1,  4, /* bottom */
2546e6ccafaeSMatthew G Knepley       1, 2,  5, /* right */
2547e6ccafaeSMatthew G Knepley       2, 3,  6, /* top */
2548e6ccafaeSMatthew G Knepley       0, 3,  7, /* left */
2549e6ccafaeSMatthew G Knepley     };
2550e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuad[12] = {
2551e6ccafaeSMatthew G Knepley       0, 1,  4, /* bottom */
2552e6ccafaeSMatthew G Knepley       1, 2,  5, /* right */
2553e6ccafaeSMatthew G Knepley       2, 3,  6, /* top */
2554e6ccafaeSMatthew G Knepley       3, 0,  7, /* left */
2555e6ccafaeSMatthew G Knepley     };
2556e6ccafaeSMatthew G Knepley 
2557e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuad; ++i) sortedIndices[i] = indices[i];
2558e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeQuad, sortedIndices);CHKERRQ(ierr);
2559e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2560e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeQuad;
2561e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2562e6ccafaeSMatthew G Knepley 
2563e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesQuadSorted[ii+0]) &&
2564e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesQuadSorted[ii+1])) {
2565e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuad; ++fVertex) {
2566e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuad; ++cVertex) {
2567e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuad[ii+fVertex]) {
2568e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2569e6ccafaeSMatthew G Knepley               break;
2570e6ccafaeSMatthew G Knepley             }
2571e6ccafaeSMatthew G Knepley           }
2572e6ccafaeSMatthew G Knepley         }
2573e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2574e6ccafaeSMatthew G Knepley         break;
2575e6ccafaeSMatthew G Knepley       }
2576e6ccafaeSMatthew G Knepley     }
25772c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!found,comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossface");
2578e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2579e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2580e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 8) {
2581e6ccafaeSMatthew G Knepley     /* Hexes
2582e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2583e6ccafaeSMatthew G Knepley        pointing up at the second.
2584e6ccafaeSMatthew G Knepley 
2585e6ccafaeSMatthew G Knepley           7---6
2586e6ccafaeSMatthew G Knepley          /|  /|
2587e6ccafaeSMatthew G Knepley         4---5 |
2588ddeab2a6SMatthew G. Knepley         | 1-|-2
2589e6ccafaeSMatthew G Knepley         |/  |/
2590ddeab2a6SMatthew G. Knepley         0---3
2591e6ccafaeSMatthew G Knepley 
2592e6ccafaeSMatthew G Knepley         Faces are determined by the first 4 vertices (corners of faces) */
2593e6ccafaeSMatthew G Knepley     const PetscInt faceSizeHex = 4;
2594e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[4], i, iFace;
2595e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2596e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHexSorted[24] = {
2597e6ccafaeSMatthew G Knepley       0, 1, 2, 3,  /* bottom */
2598e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  /* top */
2599ddeab2a6SMatthew G. Knepley       0, 3, 4, 5,  /* front */
2600ddeab2a6SMatthew G. Knepley       2, 3, 5, 6,  /* right */
2601ddeab2a6SMatthew G. Knepley       1, 2, 6, 7,  /* back */
2602ddeab2a6SMatthew G. Knepley       0, 1, 4, 7,  /* left */
2603e6ccafaeSMatthew G Knepley     };
2604e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHex[24] = {
2605ddeab2a6SMatthew G. Knepley       1, 2, 3, 0,  /* bottom */
2606e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  /* top */
2607ddeab2a6SMatthew G. Knepley       0, 3, 5, 4,  /* front */
2608ddeab2a6SMatthew G. Knepley       3, 2, 6, 5,  /* right */
2609ddeab2a6SMatthew G. Knepley       2, 1, 7, 6,  /* back */
2610ddeab2a6SMatthew G. Knepley       1, 0, 4, 7,  /* left */
2611e6ccafaeSMatthew G Knepley     };
2612e6ccafaeSMatthew G Knepley 
2613e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeHex; ++i) sortedIndices[i] = indices[i];
2614e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeHex, sortedIndices);CHKERRQ(ierr);
2615e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2616e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeHex;
2617e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2618e6ccafaeSMatthew G Knepley 
2619e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesHexSorted[ii+0]) &&
2620e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesHexSorted[ii+1]) &&
2621e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesHexSorted[ii+2]) &&
2622e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesHexSorted[ii+3])) {
2623e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeHex; ++fVertex) {
2624e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeHex; ++cVertex) {
2625e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesHex[ii+fVertex]) {
2626e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2627e6ccafaeSMatthew G Knepley               break;
2628e6ccafaeSMatthew G Knepley             }
2629e6ccafaeSMatthew G Knepley           }
2630e6ccafaeSMatthew G Knepley         }
2631e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2632e6ccafaeSMatthew G Knepley         break;
2633e6ccafaeSMatthew G Knepley       }
2634e6ccafaeSMatthew G Knepley     }
26352c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!found,comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2636e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2637e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2638e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 10) {
2639e6ccafaeSMatthew G Knepley     /* Quadratic tet */
2640e6ccafaeSMatthew G Knepley     /* Faces are determined by the first 3 vertices (corners of faces) */
2641e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTet = 6;
2642e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[6], i, iFace;
2643e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2644e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTetSorted[24] = {
2645e6ccafaeSMatthew G Knepley       0, 1, 2,  6, 7, 8, /* bottom */
2646e6ccafaeSMatthew G Knepley       0, 3, 4,  6, 7, 9,  /* front */
2647e6ccafaeSMatthew G Knepley       1, 4, 5,  7, 8, 9,  /* right */
2648e6ccafaeSMatthew G Knepley       2, 3, 5,  6, 8, 9,  /* left */
2649e6ccafaeSMatthew G Knepley     };
2650e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTet[24] = {
2651e6ccafaeSMatthew G Knepley       0, 1, 2,  6, 7, 8, /* bottom */
2652e6ccafaeSMatthew G Knepley       0, 4, 3,  6, 7, 9,  /* front */
2653e6ccafaeSMatthew G Knepley       1, 5, 4,  7, 8, 9,  /* right */
2654e6ccafaeSMatthew G Knepley       2, 3, 5,  8, 6, 9,  /* left */
2655e6ccafaeSMatthew G Knepley     };
2656e6ccafaeSMatthew G Knepley 
2657e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTet; ++i) sortedIndices[i] = indices[i];
2658e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeTet, sortedIndices);CHKERRQ(ierr);
2659e6ccafaeSMatthew G Knepley     for (iFace=0; iFace < 4; ++iFace) {
2660e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeTet;
2661e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2662e6ccafaeSMatthew G Knepley 
2663e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesTetSorted[ii+0]) &&
2664e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesTetSorted[ii+1]) &&
2665e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesTetSorted[ii+2]) &&
2666e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesTetSorted[ii+3])) {
2667e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTet; ++fVertex) {
2668e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTet; ++cVertex) {
2669e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTet[ii+fVertex]) {
2670e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2671e6ccafaeSMatthew G Knepley               break;
2672e6ccafaeSMatthew G Knepley             }
2673e6ccafaeSMatthew G Knepley           }
2674e6ccafaeSMatthew G Knepley         }
2675e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2676e6ccafaeSMatthew G Knepley         break;
2677e6ccafaeSMatthew G Knepley       }
2678e6ccafaeSMatthew G Knepley     }
26792c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!found,comm, PETSC_ERR_ARG_WRONG, "Invalid tet crossface");
2680e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2681e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2682e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 27) {
2683e6ccafaeSMatthew G Knepley     /* Quadratic hexes (I hate this)
2684e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2685e6ccafaeSMatthew G Knepley        pointing up at the second.
2686e6ccafaeSMatthew G Knepley 
2687e6ccafaeSMatthew G Knepley          7---6
2688e6ccafaeSMatthew G Knepley         /|  /|
2689e6ccafaeSMatthew G Knepley        4---5 |
2690e6ccafaeSMatthew G Knepley        | 3-|-2
2691e6ccafaeSMatthew G Knepley        |/  |/
2692e6ccafaeSMatthew G Knepley        0---1
2693e6ccafaeSMatthew G Knepley 
2694e6ccafaeSMatthew G Knepley        Faces are determined by the first 4 vertices (corners of faces) */
2695e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuadHex = 9;
2696e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[9], i, iFace;
2697e6ccafaeSMatthew G Knepley     PetscBool      found                         = PETSC_FALSE;
2698e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHexSorted[54] = {
2699e6ccafaeSMatthew G Knepley       0, 1, 2, 3,  8, 9, 10, 11,  24, /* bottom */
2700e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  12, 13, 14, 15,  25, /* top */
2701e6ccafaeSMatthew G Knepley       0, 1, 4, 5,  8, 12, 16, 17,  22, /* front */
2702e6ccafaeSMatthew G Knepley       1, 2, 5, 6,  9, 13, 17, 18,  21, /* right */
2703e6ccafaeSMatthew G Knepley       2, 3, 6, 7,  10, 14, 18, 19,  23, /* back */
2704e6ccafaeSMatthew G Knepley       0, 3, 4, 7,  11, 15, 16, 19,  20, /* left */
2705e6ccafaeSMatthew G Knepley     };
2706e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHex[54] = {
2707e6ccafaeSMatthew G Knepley       3, 2, 1, 0,  10, 9, 8, 11,  24, /* bottom */
2708e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  12, 13, 14, 15,  25, /* top */
2709e6ccafaeSMatthew G Knepley       0, 1, 5, 4,  8, 17, 12, 16,  22, /* front */
2710e6ccafaeSMatthew G Knepley       1, 2, 6, 5,  9, 18, 13, 17,  21, /* right */
2711e6ccafaeSMatthew G Knepley       2, 3, 7, 6,  10, 19, 14, 18,  23, /* back */
2712e6ccafaeSMatthew G Knepley       3, 0, 4, 7,  11, 16, 15, 19,  20 /* left */
2713e6ccafaeSMatthew G Knepley     };
2714e6ccafaeSMatthew G Knepley 
2715e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuadHex; ++i) sortedIndices[i] = indices[i];
2716e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeQuadHex, sortedIndices);CHKERRQ(ierr);
2717e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2718e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeQuadHex;
2719e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2720e6ccafaeSMatthew G Knepley 
2721e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesQuadHexSorted[ii+0]) &&
2722e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesQuadHexSorted[ii+1]) &&
2723e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesQuadHexSorted[ii+2]) &&
2724e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesQuadHexSorted[ii+3])) {
2725e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuadHex; ++fVertex) {
2726e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuadHex; ++cVertex) {
2727e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuadHex[ii+fVertex]) {
2728e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2729e6ccafaeSMatthew G Knepley               break;
2730e6ccafaeSMatthew G Knepley             }
2731e6ccafaeSMatthew G Knepley           }
2732e6ccafaeSMatthew G Knepley         }
2733e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2734e6ccafaeSMatthew G Knepley         break;
2735e6ccafaeSMatthew G Knepley       }
2736e6ccafaeSMatthew G Knepley     }
27372c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!found,comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2738e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2739e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2740e6ccafaeSMatthew G Knepley   } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Unknown cell type for faceOrientation().");
2741e6ccafaeSMatthew G Knepley   if (!posOrient) {
2742e6ccafaeSMatthew G Knepley     if (debug) {ierr = PetscPrintf(comm, "  Reversing initial face orientation\n");CHKERRQ(ierr);}
2743e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[faceSize-1 - f];
2744e6ccafaeSMatthew G Knepley   } else {
2745e6ccafaeSMatthew G Knepley     if (debug) {ierr = PetscPrintf(comm, "  Keeping initial face orientation\n");CHKERRQ(ierr);}
2746e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[f];
2747e6ccafaeSMatthew G Knepley   }
2748e6ccafaeSMatthew G Knepley   if (posOriented) *posOriented = posOrient;
2749e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2750e6ccafaeSMatthew G Knepley }
2751e6ccafaeSMatthew G Knepley 
2752c08575a3SMatthew G. Knepley /*@
2753c08575a3SMatthew G. Knepley   DMPlexGetOrientedFace - Given a cell and a face, as a set of vertices, return the oriented face, as a set of vertices,
2754c08575a3SMatthew G. Knepley   in faceVertices. The orientation is such that the face normal points out of the cell
2755c08575a3SMatthew G. Knepley 
2756c08575a3SMatthew G. Knepley   Not collective
2757c08575a3SMatthew G. Knepley 
2758c08575a3SMatthew G. Knepley   Input Parameters:
2759c08575a3SMatthew G. Knepley + dm           - The original mesh
2760c08575a3SMatthew G. Knepley . cell         - The cell mesh point
2761c08575a3SMatthew G. Knepley . faceSize     - The number of vertices on the face
2762c08575a3SMatthew G. Knepley . face         - The face vertices
2763c08575a3SMatthew G. Knepley . numCorners   - The number of vertices on the cell
2764c08575a3SMatthew G. Knepley . indices      - Local numbering of face vertices in cell cone
2765c08575a3SMatthew G. Knepley - origVertices - Original face vertices
2766c08575a3SMatthew G. Knepley 
2767d8d19677SJose E. Roman   Output Parameters:
2768c08575a3SMatthew G. Knepley + faceVertices - The face vertices properly oriented
2769c08575a3SMatthew G. Knepley - posOriented  - PETSC_TRUE if the face was oriented with outward normal
2770c08575a3SMatthew G. Knepley 
2771c08575a3SMatthew G. Knepley   Level: developer
2772c08575a3SMatthew G. Knepley 
2773c08575a3SMatthew G. Knepley .seealso: DMPlexGetCone()
2774c08575a3SMatthew G. Knepley @*/
2775e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexGetOrientedFace(DM dm, PetscInt cell, PetscInt faceSize, const PetscInt face[], PetscInt numCorners, PetscInt indices[], PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2776e6ccafaeSMatthew G Knepley {
27770298fd71SBarry Smith   const PetscInt *cone = NULL;
2778e6ccafaeSMatthew G Knepley   PetscInt        coneSize, v, f, v2;
2779e6ccafaeSMatthew G Knepley   PetscInt        oppositeVertex = -1;
2780e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
2781e6ccafaeSMatthew G Knepley 
2782e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
2783e6ccafaeSMatthew G Knepley   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
2784e6ccafaeSMatthew G Knepley   ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
2785e6ccafaeSMatthew G Knepley   for (v = 0, v2 = 0; v < coneSize; ++v) {
2786e6ccafaeSMatthew G Knepley     PetscBool found = PETSC_FALSE;
2787e6ccafaeSMatthew G Knepley 
2788e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) {
2789e6ccafaeSMatthew G Knepley       if (face[f] == cone[v]) {
2790e6ccafaeSMatthew G Knepley         found = PETSC_TRUE; break;
2791e6ccafaeSMatthew G Knepley       }
2792e6ccafaeSMatthew G Knepley     }
2793e6ccafaeSMatthew G Knepley     if (found) {
2794e6ccafaeSMatthew G Knepley       indices[v2]      = v;
2795e6ccafaeSMatthew G Knepley       origVertices[v2] = cone[v];
2796e6ccafaeSMatthew G Knepley       ++v2;
2797e6ccafaeSMatthew G Knepley     } else {
2798e6ccafaeSMatthew G Knepley       oppositeVertex = v;
2799e6ccafaeSMatthew G Knepley     }
2800e6ccafaeSMatthew G Knepley   }
2801e6ccafaeSMatthew G Knepley   ierr = DMPlexGetFaceOrientation(dm, cell, numCorners, indices, oppositeVertex, origVertices, faceVertices, posOriented);CHKERRQ(ierr);
2802e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2803e6ccafaeSMatthew G Knepley }
2804e6ccafaeSMatthew G Knepley 
2805e6ccafaeSMatthew G Knepley /*
2806cd0c2139SMatthew G Knepley   DMPlexInsertFace_Internal - Puts a face into the mesh
2807e6ccafaeSMatthew G Knepley 
2808e6ccafaeSMatthew G Knepley   Not collective
2809e6ccafaeSMatthew G Knepley 
2810e6ccafaeSMatthew G Knepley   Input Parameters:
2811e6ccafaeSMatthew G Knepley   + dm              - The DMPlex
2812e6ccafaeSMatthew G Knepley   . numFaceVertex   - The number of vertices in the face
2813e6ccafaeSMatthew G Knepley   . faceVertices    - The vertices in the face for dm
2814e6ccafaeSMatthew G Knepley   . subfaceVertices - The vertices in the face for subdm
2815e6ccafaeSMatthew G Knepley   . numCorners      - The number of vertices in the cell
2816e6ccafaeSMatthew G Knepley   . cell            - A cell in dm containing the face
2817e6ccafaeSMatthew G Knepley   . subcell         - A cell in subdm containing the face
2818e6ccafaeSMatthew G Knepley   . firstFace       - First face in the mesh
2819e6ccafaeSMatthew G Knepley   - newFacePoint    - Next face in the mesh
2820e6ccafaeSMatthew G Knepley 
2821e6ccafaeSMatthew G Knepley   Output Parameters:
2822e6ccafaeSMatthew G Knepley   . newFacePoint - Contains next face point number on input, updated on output
2823e6ccafaeSMatthew G Knepley 
2824e6ccafaeSMatthew G Knepley   Level: developer
2825e6ccafaeSMatthew G Knepley */
2826cd0c2139SMatthew 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)
2827e6ccafaeSMatthew G Knepley {
282882f516ccSBarry Smith   MPI_Comm        comm;
2829e6ccafaeSMatthew G Knepley   DM_Plex        *submesh = (DM_Plex*) subdm->data;
2830e6ccafaeSMatthew G Knepley   const PetscInt *faces;
2831e6ccafaeSMatthew G Knepley   PetscInt        numFaces, coneSize;
2832e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
2833e6ccafaeSMatthew G Knepley 
2834e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
283582f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2836e6ccafaeSMatthew G Knepley   ierr = DMPlexGetConeSize(subdm, subcell, &coneSize);CHKERRQ(ierr);
28372c71b3e2SJacob Faibussowitsch   PetscCheckFalse(coneSize != 1,comm, PETSC_ERR_ARG_OUTOFRANGE, "Cone size of cell %d is %d != 1", cell, coneSize);
2838e6ccafaeSMatthew G Knepley #if 0
2839e6ccafaeSMatthew G Knepley   /* Cannot use this because support() has not been constructed yet */
2840e6ccafaeSMatthew G Knepley   ierr = DMPlexGetJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces);CHKERRQ(ierr);
2841e6ccafaeSMatthew G Knepley #else
2842e6ccafaeSMatthew G Knepley   {
2843e6ccafaeSMatthew G Knepley     PetscInt f;
2844e6ccafaeSMatthew G Knepley 
2845e6ccafaeSMatthew G Knepley     numFaces = 0;
284669291d52SBarry Smith     ierr     = DMGetWorkArray(subdm, 1, MPIU_INT, (void **) &faces);CHKERRQ(ierr);
2847e6ccafaeSMatthew G Knepley     for (f = firstFace; f < *newFacePoint; ++f) {
2848e6ccafaeSMatthew G Knepley       PetscInt dof, off, d;
2849e6ccafaeSMatthew G Knepley 
2850e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(submesh->coneSection, f, &dof);CHKERRQ(ierr);
2851e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetOffset(submesh->coneSection, f, &off);CHKERRQ(ierr);
2852e6ccafaeSMatthew G Knepley       /* Yes, I know this is quadratic, but I expect the sizes to be <5 */
2853e6ccafaeSMatthew G Knepley       for (d = 0; d < dof; ++d) {
2854e6ccafaeSMatthew G Knepley         const PetscInt p = submesh->cones[off+d];
2855e6ccafaeSMatthew G Knepley         PetscInt       v;
2856e6ccafaeSMatthew G Knepley 
2857e6ccafaeSMatthew G Knepley         for (v = 0; v < numFaceVertices; ++v) {
2858e6ccafaeSMatthew G Knepley           if (subfaceVertices[v] == p) break;
2859e6ccafaeSMatthew G Knepley         }
2860e6ccafaeSMatthew G Knepley         if (v == numFaceVertices) break;
2861e6ccafaeSMatthew G Knepley       }
2862e6ccafaeSMatthew G Knepley       if (d == dof) {
2863e6ccafaeSMatthew G Knepley         numFaces               = 1;
2864e6ccafaeSMatthew G Knepley         ((PetscInt*) faces)[0] = f;
2865e6ccafaeSMatthew G Knepley       }
2866e6ccafaeSMatthew G Knepley     }
2867e6ccafaeSMatthew G Knepley   }
2868e6ccafaeSMatthew G Knepley #endif
28692c71b3e2SJacob Faibussowitsch   PetscCheckFalse(numFaces > 1,comm, PETSC_ERR_ARG_WRONG, "Vertex set had %d faces, not one", numFaces);
2870e6ccafaeSMatthew G Knepley   else if (numFaces == 1) {
2871e6ccafaeSMatthew G Knepley     /* Add the other cell neighbor for this face */
2872766ab985SMatthew G. Knepley     ierr = DMPlexSetCone(subdm, subcell, faces);CHKERRQ(ierr);
2873e6ccafaeSMatthew G Knepley   } else {
2874e6ccafaeSMatthew G Knepley     PetscInt *indices, *origVertices, *orientedVertices, *orientedSubVertices, v, ov;
2875e6ccafaeSMatthew G Knepley     PetscBool posOriented;
2876e6ccafaeSMatthew G Knepley 
287769291d52SBarry Smith     ierr                = DMGetWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices);CHKERRQ(ierr);
2878e6ccafaeSMatthew G Knepley     origVertices        = &orientedVertices[numFaceVertices];
2879e6ccafaeSMatthew G Knepley     indices             = &orientedVertices[numFaceVertices*2];
2880e6ccafaeSMatthew G Knepley     orientedSubVertices = &orientedVertices[numFaceVertices*3];
2881e6ccafaeSMatthew G Knepley     ierr                = DMPlexGetOrientedFace(dm, cell, numFaceVertices, faceVertices, numCorners, indices, origVertices, orientedVertices, &posOriented);CHKERRQ(ierr);
2882e6ccafaeSMatthew G Knepley     /* TODO: I know that routine should return a permutation, not the indices */
2883e6ccafaeSMatthew G Knepley     for (v = 0; v < numFaceVertices; ++v) {
2884e6ccafaeSMatthew G Knepley       const PetscInt vertex = faceVertices[v], subvertex = subfaceVertices[v];
2885e6ccafaeSMatthew G Knepley       for (ov = 0; ov < numFaceVertices; ++ov) {
2886e6ccafaeSMatthew G Knepley         if (orientedVertices[ov] == vertex) {
2887e6ccafaeSMatthew G Knepley           orientedSubVertices[ov] = subvertex;
2888e6ccafaeSMatthew G Knepley           break;
2889e6ccafaeSMatthew G Knepley         }
2890e6ccafaeSMatthew G Knepley       }
28912c71b3e2SJacob Faibussowitsch       PetscCheckFalse(ov == numFaceVertices,comm, PETSC_ERR_PLIB, "Could not find face vertex %d in orientated set", vertex);
2892e6ccafaeSMatthew G Knepley     }
2893e6ccafaeSMatthew G Knepley     ierr = DMPlexSetCone(subdm, *newFacePoint, orientedSubVertices);CHKERRQ(ierr);
2894e6ccafaeSMatthew G Knepley     ierr = DMPlexSetCone(subdm, subcell, newFacePoint);CHKERRQ(ierr);
289569291d52SBarry Smith     ierr = DMRestoreWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices);CHKERRQ(ierr);
2896e6ccafaeSMatthew G Knepley     ++(*newFacePoint);
2897e6ccafaeSMatthew G Knepley   }
2898ef07cca7SMatthew G. Knepley #if 0
2899e6ccafaeSMatthew G Knepley   ierr = DMPlexRestoreJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces);CHKERRQ(ierr);
2900ef07cca7SMatthew G. Knepley #else
290169291d52SBarry Smith   ierr = DMRestoreWorkArray(subdm, 1, MPIU_INT, (void **) &faces);CHKERRQ(ierr);
2902ef07cca7SMatthew G. Knepley #endif
2903e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2904e6ccafaeSMatthew G Knepley }
2905e6ccafaeSMatthew G Knepley 
290653156dfcSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm)
2907e6ccafaeSMatthew G Knepley {
290882f516ccSBarry Smith   MPI_Comm        comm;
290953156dfcSMatthew G. Knepley   DMLabel         subpointMap;
2910efa14ee0SMatthew G Knepley   IS              subvertexIS,  subcellIS;
2911efa14ee0SMatthew G Knepley   const PetscInt *subVertices, *subCells;
2912efa14ee0SMatthew G Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells;
2913fed694aaSMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces = 0, firstSubFace, newFacePoint, nFV = 0;
2914efa14ee0SMatthew G Knepley   PetscInt        vStart, vEnd, c, f;
2915e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
2916e6ccafaeSMatthew G Knepley 
2917e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
291882f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2919efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
2920d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap);CHKERRQ(ierr);
2921efa14ee0SMatthew G Knepley   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
2922efa14ee0SMatthew G Knepley   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
292353156dfcSMatthew G. Knepley   if (vertexLabel) {ierr = DMPlexMarkSubmesh_Uninterpolated(dm, vertexLabel, value, subpointMap, &numSubFaces, &nFV, subdm);CHKERRQ(ierr);}
2924efa14ee0SMatthew G Knepley   /* Setup chart */
2925efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumSize(subpointMap, 0, &numSubVertices);CHKERRQ(ierr);
2926efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumSize(subpointMap, 2, &numSubCells);CHKERRQ(ierr);
2927efa14ee0SMatthew G Knepley   ierr = DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices);CHKERRQ(ierr);
2928efa14ee0SMatthew G Knepley   ierr = DMPlexSetVTKCellHeight(subdm, 1);CHKERRQ(ierr);
2929e6ccafaeSMatthew G Knepley   /* Set cone sizes */
2930e6ccafaeSMatthew G Knepley   firstSubVertex = numSubCells;
2931efa14ee0SMatthew G Knepley   firstSubFace   = numSubCells+numSubVertices;
2932e6ccafaeSMatthew G Knepley   newFacePoint   = firstSubFace;
2933efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumIS(subpointMap, 0, &subvertexIS);CHKERRQ(ierr);
2934efa14ee0SMatthew G Knepley   if (subvertexIS) {ierr = ISGetIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
2935efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumIS(subpointMap, 2, &subcellIS);CHKERRQ(ierr);
2936efa14ee0SMatthew G Knepley   if (subcellIS) {ierr = ISGetIndices(subcellIS, &subCells);CHKERRQ(ierr);}
2937e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
2938e6ccafaeSMatthew G Knepley     ierr = DMPlexSetConeSize(subdm, c, 1);CHKERRQ(ierr);
2939e6ccafaeSMatthew G Knepley   }
2940e6ccafaeSMatthew G Knepley   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
2941e6ccafaeSMatthew G Knepley     ierr = DMPlexSetConeSize(subdm, f, nFV);CHKERRQ(ierr);
2942e6ccafaeSMatthew G Knepley   }
2943e6ccafaeSMatthew G Knepley   ierr = DMSetUp(subdm);CHKERRQ(ierr);
2944e6ccafaeSMatthew G Knepley   /* Create face cones */
2945efa14ee0SMatthew G Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
29460298fd71SBarry Smith   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
294769291d52SBarry Smith   ierr = DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
2948e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
2949e6ccafaeSMatthew G Knepley     const PetscInt cell    = subCells[c];
2950efa14ee0SMatthew G Knepley     const PetscInt subcell = c;
29510298fd71SBarry Smith     PetscInt      *closure = NULL;
2952efa14ee0SMatthew G Knepley     PetscInt       closureSize, cl, numCorners = 0, faceSize = 0;
2953e6ccafaeSMatthew G Knepley 
2954e6ccafaeSMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2955efa14ee0SMatthew G Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
2956efa14ee0SMatthew G Knepley       const PetscInt point = closure[cl];
2957e6ccafaeSMatthew G Knepley       PetscInt       subVertex;
2958e6ccafaeSMatthew G Knepley 
2959efa14ee0SMatthew G Knepley       if ((point >= vStart) && (point < vEnd)) {
2960efa14ee0SMatthew G Knepley         ++numCorners;
2961efa14ee0SMatthew G Knepley         ierr = PetscFindInt(point, numSubVertices, subVertices, &subVertex);CHKERRQ(ierr);
2962efa14ee0SMatthew G Knepley         if (subVertex >= 0) {
2963efa14ee0SMatthew G Knepley           closure[faceSize] = point;
296465560c7fSMatthew G Knepley           subface[faceSize] = firstSubVertex+subVertex;
2965e6ccafaeSMatthew G Knepley           ++faceSize;
2966e6ccafaeSMatthew G Knepley         }
2967e6ccafaeSMatthew G Knepley       }
2968e6ccafaeSMatthew G Knepley     }
29692c71b3e2SJacob Faibussowitsch     PetscCheckFalse(faceSize > nFV,comm, PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %d of an element on the surface", faceSize);
2970efa14ee0SMatthew G Knepley     if (faceSize == nFV) {
2971cd0c2139SMatthew G Knepley       ierr = DMPlexInsertFace_Internal(dm, subdm, faceSize, closure, subface, numCorners, cell, subcell, firstSubFace, &newFacePoint);CHKERRQ(ierr);
2972e6ccafaeSMatthew G Knepley     }
297365560c7fSMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2974e6ccafaeSMatthew G Knepley   }
297569291d52SBarry Smith   ierr = DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
2976e6ccafaeSMatthew G Knepley   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
2977e6ccafaeSMatthew G Knepley   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
2978e6ccafaeSMatthew G Knepley   /* Build coordinates */
2979efa14ee0SMatthew G Knepley   {
2980efa14ee0SMatthew G Knepley     PetscSection coordSection, subCoordSection;
2981efa14ee0SMatthew G Knepley     Vec          coordinates, subCoordinates;
2982efa14ee0SMatthew G Knepley     PetscScalar *coords, *subCoords;
2983285d324eSMatthew G. Knepley     PetscInt     numComp, coordSize, v;
298424640c55SToby Isaac     const char  *name;
2985efa14ee0SMatthew G Knepley 
298669d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
2987e6ccafaeSMatthew G Knepley     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
298869d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
2989285d324eSMatthew G. Knepley     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
2990285d324eSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
2991285d324eSMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
2992efa14ee0SMatthew G Knepley     ierr = PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices);CHKERRQ(ierr);
2993efa14ee0SMatthew G Knepley     for (v = 0; v < numSubVertices; ++v) {
2994efa14ee0SMatthew G Knepley       const PetscInt vertex    = subVertices[v];
2995efa14ee0SMatthew G Knepley       const PetscInt subvertex = firstSubVertex+v;
2996efa14ee0SMatthew G Knepley       PetscInt       dof;
2997efa14ee0SMatthew G Knepley 
2998efa14ee0SMatthew G Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
2999efa14ee0SMatthew G Knepley       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
3000285d324eSMatthew G. Knepley       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
3001e6ccafaeSMatthew G Knepley     }
3002e6ccafaeSMatthew G Knepley     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
3003e6ccafaeSMatthew G Knepley     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
30048b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &subCoordinates);CHKERRQ(ierr);
300524640c55SToby Isaac     ierr = PetscObjectGetName((PetscObject)coordinates,&name);CHKERRQ(ierr);
300624640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)subCoordinates,name);CHKERRQ(ierr);
3007e6ccafaeSMatthew G Knepley     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
30082eb5907fSJed Brown     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
3009830e53efSMatthew G. Knepley     if (coordSize) {
3010e6ccafaeSMatthew G Knepley       ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
3011e6ccafaeSMatthew G Knepley       ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3012efa14ee0SMatthew G Knepley       for (v = 0; v < numSubVertices; ++v) {
3013efa14ee0SMatthew G Knepley         const PetscInt vertex    = subVertices[v];
3014efa14ee0SMatthew G Knepley         const PetscInt subvertex = firstSubVertex+v;
3015efa14ee0SMatthew G Knepley         PetscInt       dof, off, sdof, soff, d;
3016e6ccafaeSMatthew G Knepley 
3017e6ccafaeSMatthew G Knepley         ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3018e6ccafaeSMatthew G Knepley         ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
3019efa14ee0SMatthew G Knepley         ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
3020efa14ee0SMatthew G Knepley         ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
30212c71b3e2SJacob Faibussowitsch         PetscCheckFalse(dof != sdof,comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
3022e6ccafaeSMatthew G Knepley         for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3023e6ccafaeSMatthew G Knepley       }
3024e6ccafaeSMatthew G Knepley       ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
3025e6ccafaeSMatthew G Knepley       ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
30263b399e24SMatthew G. Knepley     }
3027e6ccafaeSMatthew G Knepley     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
3028e6ccafaeSMatthew G Knepley     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
3029e6ccafaeSMatthew G Knepley   }
3030efa14ee0SMatthew G Knepley   /* Cleanup */
3031efa14ee0SMatthew G Knepley   if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
3032efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
3033efa14ee0SMatthew G Knepley   if (subcellIS) {ierr = ISRestoreIndices(subcellIS, &subCells);CHKERRQ(ierr);}
3034efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subcellIS);CHKERRQ(ierr);
3035e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3036e6ccafaeSMatthew G Knepley }
3037e6ccafaeSMatthew G Knepley 
30389fbee547SJacob Faibussowitsch static inline PetscInt DMPlexFilterPoint_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[])
30393982b651SMatthew G. Knepley {
30403982b651SMatthew G. Knepley   PetscInt       subPoint;
30413982b651SMatthew G. Knepley   PetscErrorCode ierr;
30423982b651SMatthew G. Knepley 
30433982b651SMatthew G. Knepley   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint); if (ierr < 0) return ierr;
30443982b651SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint+subPoint;
30453982b651SMatthew G. Knepley }
30463982b651SMatthew G. Knepley 
3047212103e5SMatthew Knepley static PetscErrorCode DMPlexFilterLabels_Internal(DM dm, const PetscInt numSubPoints[], const PetscInt *subpoints[], const PetscInt firstSubPoint[], DM subdm)
3048212103e5SMatthew Knepley {
3049212103e5SMatthew Knepley   PetscInt       Nl, l, d;
3050212103e5SMatthew Knepley   PetscErrorCode ierr;
3051212103e5SMatthew Knepley 
3052212103e5SMatthew Knepley   PetscFunctionBegin;
3053212103e5SMatthew Knepley   ierr = DMGetNumLabels(dm, &Nl);CHKERRQ(ierr);
3054212103e5SMatthew Knepley   for (l = 0; l < Nl; ++l) {
3055212103e5SMatthew Knepley     DMLabel         label, newlabel;
3056212103e5SMatthew Knepley     const char     *lname;
3057212103e5SMatthew Knepley     PetscBool       isDepth, isDim, isCelltype;
3058212103e5SMatthew Knepley     IS              valueIS;
3059212103e5SMatthew Knepley     const PetscInt *values;
3060212103e5SMatthew Knepley     PetscInt        Nv, v;
3061212103e5SMatthew Knepley 
3062212103e5SMatthew Knepley     ierr = DMGetLabelName(dm, l, &lname);CHKERRQ(ierr);
3063212103e5SMatthew Knepley     ierr = PetscStrcmp(lname, "depth", &isDepth);CHKERRQ(ierr);
3064212103e5SMatthew Knepley     ierr = PetscStrcmp(lname, "dim", &isDim);CHKERRQ(ierr);
3065212103e5SMatthew Knepley     ierr = PetscStrcmp(lname, "celltype", &isCelltype);CHKERRQ(ierr);
3066212103e5SMatthew Knepley     if (isDepth || isDim || isCelltype) continue;
3067212103e5SMatthew Knepley     ierr = DMCreateLabel(subdm, lname);CHKERRQ(ierr);
3068212103e5SMatthew Knepley     ierr = DMGetLabel(dm, lname, &label);CHKERRQ(ierr);
3069212103e5SMatthew Knepley     ierr = DMGetLabel(subdm, lname, &newlabel);CHKERRQ(ierr);
3070212103e5SMatthew Knepley     ierr = DMLabelGetDefaultValue(label, &v);CHKERRQ(ierr);
3071212103e5SMatthew Knepley     ierr = DMLabelSetDefaultValue(newlabel, v);CHKERRQ(ierr);
3072212103e5SMatthew Knepley     ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
3073212103e5SMatthew Knepley     ierr = ISGetLocalSize(valueIS, &Nv);CHKERRQ(ierr);
3074212103e5SMatthew Knepley     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
3075212103e5SMatthew Knepley     for (v = 0; v < Nv; ++v) {
3076212103e5SMatthew Knepley       IS              pointIS;
3077212103e5SMatthew Knepley       const PetscInt *points;
3078212103e5SMatthew Knepley       PetscInt        Np, p;
3079212103e5SMatthew Knepley 
3080212103e5SMatthew Knepley       ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
3081212103e5SMatthew Knepley       ierr = ISGetLocalSize(pointIS, &Np);CHKERRQ(ierr);
3082212103e5SMatthew Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
3083212103e5SMatthew Knepley       for (p = 0; p < Np; ++p) {
3084212103e5SMatthew Knepley         const PetscInt point = points[p];
3085212103e5SMatthew Knepley         PetscInt       subp;
3086212103e5SMatthew Knepley 
3087212103e5SMatthew Knepley         ierr = DMPlexGetPointDepth(dm, point, &d);CHKERRQ(ierr);
3088212103e5SMatthew Knepley         subp = DMPlexFilterPoint_Internal(point, firstSubPoint[d], numSubPoints[d], subpoints[d]);
3089212103e5SMatthew Knepley         if (subp >= 0) {ierr = DMLabelSetValue(newlabel, subp, values[v]);CHKERRQ(ierr);}
3090212103e5SMatthew Knepley       }
3091212103e5SMatthew Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
3092212103e5SMatthew Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
3093212103e5SMatthew Knepley     }
3094212103e5SMatthew Knepley     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
3095212103e5SMatthew Knepley     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
3096212103e5SMatthew Knepley   }
3097212103e5SMatthew Knepley   PetscFunctionReturn(0);
3098212103e5SMatthew Knepley }
3099212103e5SMatthew Knepley 
3100158acfadSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmeshGeneric_Interpolated(DM dm, DMLabel label, PetscInt value, PetscBool markedFaces, PetscBool isCohesive, PetscInt cellHeight, DM subdm)
3101e6ccafaeSMatthew G Knepley {
310282f516ccSBarry Smith   MPI_Comm         comm;
310353156dfcSMatthew G. Knepley   DMLabel          subpointMap;
3104efa14ee0SMatthew G Knepley   IS              *subpointIS;
3105efa14ee0SMatthew G Knepley   const PetscInt **subpoints;
31063982b651SMatthew G. Knepley   PetscInt        *numSubPoints, *firstSubPoint, *coneNew, *orntNew;
3107412e9a14SMatthew G. Knepley   PetscInt         totSubPoints = 0, maxConeSize, dim, p, d, v;
31080d366550SMatthew G. Knepley   PetscMPIInt      rank;
3109e6ccafaeSMatthew G Knepley   PetscErrorCode   ierr;
3110e6ccafaeSMatthew G Knepley 
3111e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
311282f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3113ffc4695bSBarry Smith   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
3114efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
3115d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap);CHKERRQ(ierr);
3116efa14ee0SMatthew G Knepley   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
3117bec263e5SMatthew G. Knepley   if (cellHeight) {
31183982b651SMatthew G. Knepley     if (isCohesive) {ierr = DMPlexMarkCohesiveSubmesh_Interpolated(dm, label, value, subpointMap, subdm);CHKERRQ(ierr);}
3119158acfadSMatthew G. Knepley     else            {ierr = DMPlexMarkSubmesh_Interpolated(dm, label, value, markedFaces, subpointMap, subdm);CHKERRQ(ierr);}
3120bec263e5SMatthew G. Knepley   } else {
3121bec263e5SMatthew G. Knepley     DMLabel         depth;
3122bec263e5SMatthew G. Knepley     IS              pointIS;
3123bec263e5SMatthew G. Knepley     const PetscInt *points;
3124b85c8bf9SMatthew G. Knepley     PetscInt        numPoints=0;
3125bec263e5SMatthew G. Knepley 
3126bec263e5SMatthew G. Knepley     ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
3127bec263e5SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, value, &pointIS);CHKERRQ(ierr);
3128b85c8bf9SMatthew G. Knepley     if (pointIS) {
3129bec263e5SMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
3130b85c8bf9SMatthew G. Knepley       ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
3131b85c8bf9SMatthew G. Knepley     }
3132bec263e5SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
3133bec263e5SMatthew G. Knepley       PetscInt *closure = NULL;
3134bec263e5SMatthew G. Knepley       PetscInt  closureSize, c, pdim;
3135bec263e5SMatthew G. Knepley 
3136bec263e5SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
3137bec263e5SMatthew G. Knepley       for (c = 0; c < closureSize*2; c += 2) {
3138bec263e5SMatthew G. Knepley         ierr = DMLabelGetValue(depth, closure[c], &pdim);CHKERRQ(ierr);
3139bec263e5SMatthew G. Knepley         ierr = DMLabelSetValue(subpointMap, closure[c], pdim);CHKERRQ(ierr);
3140bec263e5SMatthew G. Knepley       }
3141bec263e5SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
3142bec263e5SMatthew G. Knepley     }
3143b85c8bf9SMatthew G. Knepley     if (pointIS) {ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);}
3144bec263e5SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
3145bec263e5SMatthew G. Knepley   }
3146efa14ee0SMatthew G Knepley   /* Setup chart */
3147c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3148dcca6d9dSJed Brown   ierr = PetscMalloc4(dim+1,&numSubPoints,dim+1,&firstSubPoint,dim+1,&subpointIS,dim+1,&subpoints);CHKERRQ(ierr);
3149e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
3150e6ccafaeSMatthew G Knepley     ierr = DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d]);CHKERRQ(ierr);
3151e6ccafaeSMatthew G Knepley     totSubPoints += numSubPoints[d];
3152e6ccafaeSMatthew G Knepley   }
3153e6ccafaeSMatthew G Knepley   ierr = DMPlexSetChart(subdm, 0, totSubPoints);CHKERRQ(ierr);
3154bec263e5SMatthew G. Knepley   ierr = DMPlexSetVTKCellHeight(subdm, cellHeight);CHKERRQ(ierr);
3155e6ccafaeSMatthew G Knepley   /* Set cone sizes */
3156e6ccafaeSMatthew G Knepley   firstSubPoint[dim] = 0;
3157e6ccafaeSMatthew G Knepley   firstSubPoint[0]   = firstSubPoint[dim] + numSubPoints[dim];
3158e6ccafaeSMatthew G Knepley   if (dim > 1) {firstSubPoint[dim-1] = firstSubPoint[0]     + numSubPoints[0];}
3159e6ccafaeSMatthew G Knepley   if (dim > 2) {firstSubPoint[dim-2] = firstSubPoint[dim-1] + numSubPoints[dim-1];}
3160e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
3161e6ccafaeSMatthew G Knepley     ierr = DMLabelGetStratumIS(subpointMap, d, &subpointIS[d]);CHKERRQ(ierr);
31620ae02aa4SMatthew G. Knepley     if (subpointIS[d]) {ierr = ISGetIndices(subpointIS[d], &subpoints[d]);CHKERRQ(ierr);}
3163e6ccafaeSMatthew G Knepley   }
3164412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
3165412e9a14SMatthew G. Knepley   ierr = DMCreateLabel(subdm, "celltype");CHKERRQ(ierr);
3166e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
3167e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3168e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3169e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
3170e6ccafaeSMatthew G Knepley       const PetscInt *cone;
3171e6ccafaeSMatthew G Knepley       PetscInt        coneSize, coneSizeNew, c, val;
3172412e9a14SMatthew G. Knepley       DMPolytopeType  ct;
3173e6ccafaeSMatthew G Knepley 
3174e6ccafaeSMatthew G Knepley       ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
3175e6ccafaeSMatthew G Knepley       ierr = DMPlexSetConeSize(subdm, subpoint, coneSize);CHKERRQ(ierr);
3176412e9a14SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, point, &ct);CHKERRQ(ierr);
3177412e9a14SMatthew G. Knepley       ierr = DMPlexSetCellType(subdm, subpoint, ct);CHKERRQ(ierr);
3178bec263e5SMatthew G. Knepley       if (cellHeight && (d == dim)) {
3179e6ccafaeSMatthew G Knepley         ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
3180e6ccafaeSMatthew G Knepley         for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
3181e6ccafaeSMatthew G Knepley           ierr = DMLabelGetValue(subpointMap, cone[c], &val);CHKERRQ(ierr);
3182e6ccafaeSMatthew G Knepley           if (val >= 0) coneSizeNew++;
3183e6ccafaeSMatthew G Knepley         }
3184e6ccafaeSMatthew G Knepley         ierr = DMPlexSetConeSize(subdm, subpoint, coneSizeNew);CHKERRQ(ierr);
3185412e9a14SMatthew G. Knepley         ierr = DMPlexSetCellType(subdm, subpoint, DM_POLYTOPE_FV_GHOST);CHKERRQ(ierr);
3186e6ccafaeSMatthew G Knepley       }
3187e6ccafaeSMatthew G Knepley     }
3188e6ccafaeSMatthew G Knepley   }
3189d67d17b1SMatthew G. Knepley   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
3190e6ccafaeSMatthew G Knepley   ierr = DMSetUp(subdm);CHKERRQ(ierr);
3191e6ccafaeSMatthew G Knepley   /* Set cones */
31920298fd71SBarry Smith   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
31933982b651SMatthew G. Knepley   ierr = PetscMalloc2(maxConeSize,&coneNew,maxConeSize,&orntNew);CHKERRQ(ierr);
3194e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
3195e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3196e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3197e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
31980e49e2e2SMatthew G. Knepley       const PetscInt *cone, *ornt;
31990d366550SMatthew G. Knepley       PetscInt        coneSize, subconeSize, coneSizeNew, c, subc, fornt = 0;
3200e6ccafaeSMatthew G Knepley 
32010d366550SMatthew G. Knepley       if (d == dim-1) {
32020d366550SMatthew G. Knepley         const PetscInt *support, *cone, *ornt;
32030d366550SMatthew G. Knepley         PetscInt        supportSize, coneSize, s, subc;
32040d366550SMatthew G. Knepley 
32050d366550SMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
32060d366550SMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
32070d366550SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
3208412e9a14SMatthew G. Knepley           PetscBool isHybrid;
3209412e9a14SMatthew G. Knepley 
3210412e9a14SMatthew G. Knepley           ierr = DMPlexCellIsHybrid_Internal(dm, support[s], &isHybrid);CHKERRQ(ierr);
3211412e9a14SMatthew G. Knepley           if (!isHybrid) continue;
32120d366550SMatthew G. Knepley           ierr = PetscFindInt(support[s], numSubPoints[d+1], subpoints[d+1], &subc);CHKERRQ(ierr);
32130d366550SMatthew G. Knepley           if (subc >= 0) {
32140d366550SMatthew G. Knepley             const PetscInt ccell = subpoints[d+1][subc];
32150d366550SMatthew G. Knepley 
32167f79407eSBarry Smith             ierr = DMPlexGetCone(dm, ccell, &cone);CHKERRQ(ierr);
32177f79407eSBarry Smith             ierr = DMPlexGetConeSize(dm, ccell, &coneSize);CHKERRQ(ierr);
32187f79407eSBarry Smith             ierr = DMPlexGetConeOrientation(dm, ccell, &ornt);CHKERRQ(ierr);
32190d366550SMatthew G. Knepley             for (c = 0; c < coneSize; ++c) {
32200d366550SMatthew G. Knepley               if (cone[c] == point) {
32210d366550SMatthew G. Knepley                 fornt = ornt[c];
32220d366550SMatthew G. Knepley                 break;
32230d366550SMatthew G. Knepley               }
32240d366550SMatthew G. Knepley             }
32250d366550SMatthew G. Knepley             break;
32260d366550SMatthew G. Knepley           }
32270d366550SMatthew G. Knepley         }
32280d366550SMatthew G. Knepley       }
3229e6ccafaeSMatthew G Knepley       ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
3230e6ccafaeSMatthew G Knepley       ierr = DMPlexGetConeSize(subdm, subpoint, &subconeSize);CHKERRQ(ierr);
3231e6ccafaeSMatthew G Knepley       ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
32320e49e2e2SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, point, &ornt);CHKERRQ(ierr);
3233e6ccafaeSMatthew G Knepley       for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
3234e6ccafaeSMatthew G Knepley         ierr = PetscFindInt(cone[c], numSubPoints[d-1], subpoints[d-1], &subc);CHKERRQ(ierr);
323501a2673eSMatthew G. Knepley         if (subc >= 0) {
323601a2673eSMatthew G. Knepley           coneNew[coneSizeNew] = firstSubPoint[d-1] + subc;
32373982b651SMatthew G. Knepley           orntNew[coneSizeNew] = ornt[c];
323801a2673eSMatthew G. Knepley           ++coneSizeNew;
323901a2673eSMatthew G. Knepley         }
3240e6ccafaeSMatthew G Knepley       }
32412c71b3e2SJacob Faibussowitsch       PetscCheckFalse(coneSizeNew != subconeSize,comm, PETSC_ERR_PLIB, "Number of cone points located %d does not match subcone size %d", coneSizeNew, subconeSize);
3242e6ccafaeSMatthew G Knepley       ierr = DMPlexSetCone(subdm, subpoint, coneNew);CHKERRQ(ierr);
32433982b651SMatthew G. Knepley       ierr = DMPlexSetConeOrientation(subdm, subpoint, orntNew);CHKERRQ(ierr);
3244b5a892a1SMatthew G. Knepley       if (fornt < 0) {ierr = DMPlexOrientPoint(subdm, subpoint, fornt);CHKERRQ(ierr);}
3245e6ccafaeSMatthew G Knepley     }
3246e6ccafaeSMatthew G Knepley   }
32473982b651SMatthew G. Knepley   ierr = PetscFree2(coneNew,orntNew);CHKERRQ(ierr);
3248e6ccafaeSMatthew G Knepley   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
3249e6ccafaeSMatthew G Knepley   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
3250e6ccafaeSMatthew G Knepley   /* Build coordinates */
3251e6ccafaeSMatthew G Knepley   {
3252e6ccafaeSMatthew G Knepley     PetscSection coordSection, subCoordSection;
3253e6ccafaeSMatthew G Knepley     Vec          coordinates, subCoordinates;
3254e6ccafaeSMatthew G Knepley     PetscScalar *coords, *subCoords;
3255c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize;
325624640c55SToby Isaac     const char  *name;
3257e6ccafaeSMatthew G Knepley 
3258c0e8cf5fSMatthew G. Knepley     ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
325969d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
3260e6ccafaeSMatthew G Knepley     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
326169d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
3262285d324eSMatthew G. Knepley     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
3263285d324eSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
3264285d324eSMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
3265e6ccafaeSMatthew G Knepley     ierr = PetscSectionSetChart(subCoordSection, firstSubPoint[0], firstSubPoint[0]+numSubPoints[0]);CHKERRQ(ierr);
3266e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3267e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3268e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0]+v;
3269e6ccafaeSMatthew G Knepley       PetscInt       dof;
3270e6ccafaeSMatthew G Knepley 
3271e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3272e6ccafaeSMatthew G Knepley       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
3273285d324eSMatthew G. Knepley       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
3274e6ccafaeSMatthew G Knepley     }
3275e6ccafaeSMatthew G Knepley     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
3276e6ccafaeSMatthew G Knepley     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
32778b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &subCoordinates);CHKERRQ(ierr);
327824640c55SToby Isaac     ierr = PetscObjectGetName((PetscObject)coordinates,&name);CHKERRQ(ierr);
327924640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)subCoordinates,name);CHKERRQ(ierr);
3280e6ccafaeSMatthew G Knepley     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
3281c0e8cf5fSMatthew G. Knepley     ierr = VecSetBlockSize(subCoordinates, cdim);CHKERRQ(ierr);
32822eb5907fSJed Brown     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
3283e6ccafaeSMatthew G Knepley     ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
3284e6ccafaeSMatthew G Knepley     ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3285e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3286e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3287e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0]+v;
3288e6ccafaeSMatthew G Knepley       PetscInt dof, off, sdof, soff, d;
3289e6ccafaeSMatthew G Knepley 
3290e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3291e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
3292e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
3293e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
32942c71b3e2SJacob Faibussowitsch       PetscCheckFalse(dof != sdof,comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
3295efa14ee0SMatthew G Knepley       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3296e6ccafaeSMatthew G Knepley     }
3297e6ccafaeSMatthew G Knepley     ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
3298e6ccafaeSMatthew G Knepley     ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3299e6ccafaeSMatthew G Knepley     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
3300e6ccafaeSMatthew G Knepley     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
3301e6ccafaeSMatthew G Knepley   }
33023982b651SMatthew G. Knepley   /* Build SF: We need this complexity because subpoints might not be selected on the owning process */
33033982b651SMatthew G. Knepley   {
33043982b651SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
33053982b651SMatthew G. Knepley     IS                 subpIS;
33063982b651SMatthew G. Knepley     const PetscSFNode *remotePoints;
33073982b651SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
33083982b651SMatthew G. Knepley     const PetscInt    *localPoints, *subpoints;
33093982b651SMatthew G. Knepley     PetscInt          *slocalPoints;
33103982b651SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubpoints = 0, numSubroots, numSubleaves = 0, l, sl, ll, pStart, pEnd, p;
33113982b651SMatthew G. Knepley     PetscMPIInt        rank;
33123982b651SMatthew G. Knepley 
3313ffc4695bSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr);
33143982b651SMatthew G. Knepley     ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
33153982b651SMatthew G. Knepley     ierr = DMGetPointSF(subdm, &sfPointSub);CHKERRQ(ierr);
33163982b651SMatthew G. Knepley     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
33173982b651SMatthew G. Knepley     ierr = DMPlexGetChart(subdm, NULL, &numSubroots);CHKERRQ(ierr);
331897d8846cSMatthew Knepley     ierr = DMPlexGetSubpointIS(subdm, &subpIS);CHKERRQ(ierr);
33193982b651SMatthew G. Knepley     if (subpIS) {
33203982b651SMatthew G. Knepley       ierr = ISGetIndices(subpIS, &subpoints);CHKERRQ(ierr);
33213982b651SMatthew G. Knepley       ierr = ISGetLocalSize(subpIS, &numSubpoints);CHKERRQ(ierr);
33223982b651SMatthew G. Knepley     }
33233982b651SMatthew G. Knepley     ierr = PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr);
33243982b651SMatthew G. Knepley     if (numRoots >= 0) {
33253982b651SMatthew G. Knepley       ierr = PetscMalloc2(pEnd-pStart,&newLocalPoints,numRoots,&newOwners);CHKERRQ(ierr);
33263982b651SMatthew G. Knepley       for (p = 0; p < pEnd-pStart; ++p) {
33273982b651SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
33283982b651SMatthew G. Knepley         newLocalPoints[p].index = -2;
33293982b651SMatthew G. Knepley       }
33303982b651SMatthew G. Knepley       /* Set subleaves */
33313982b651SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
33323982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
33333982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(point, 0, numSubpoints, subpoints);
33343982b651SMatthew G. Knepley 
33353982b651SMatthew G. Knepley         if (subpoint < 0) continue;
33363982b651SMatthew G. Knepley         newLocalPoints[point-pStart].rank  = rank;
33373982b651SMatthew G. Knepley         newLocalPoints[point-pStart].index = subpoint;
33383982b651SMatthew G. Knepley         ++numSubleaves;
33393982b651SMatthew G. Knepley       }
33403982b651SMatthew G. Knepley       /* Must put in owned subpoints */
33413982b651SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
33423982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(p, 0, numSubpoints, subpoints);
33433982b651SMatthew G. Knepley 
33443982b651SMatthew G. Knepley         if (subpoint < 0) {
33453982b651SMatthew G. Knepley           newOwners[p-pStart].rank  = -3;
33463982b651SMatthew G. Knepley           newOwners[p-pStart].index = -3;
33473982b651SMatthew G. Knepley         } else {
33483982b651SMatthew G. Knepley           newOwners[p-pStart].rank  = rank;
33493982b651SMatthew G. Knepley           newOwners[p-pStart].index = subpoint;
33503982b651SMatthew G. Knepley         }
33513982b651SMatthew G. Knepley       }
33523982b651SMatthew G. Knepley       ierr = PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
33533982b651SMatthew G. Knepley       ierr = PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
3354ad227feaSJunchao Zhang       ierr = PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints,MPI_REPLACE);CHKERRQ(ierr);
3355ad227feaSJunchao Zhang       ierr = PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints,MPI_REPLACE);CHKERRQ(ierr);
33563982b651SMatthew G. Knepley       ierr = PetscMalloc1(numSubleaves, &slocalPoints);CHKERRQ(ierr);
33573982b651SMatthew G. Knepley       ierr = PetscMalloc1(numSubleaves, &sremotePoints);CHKERRQ(ierr);
33583982b651SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
33593982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
33603982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(point, 0, numSubpoints, subpoints);
33613982b651SMatthew G. Knepley 
33623982b651SMatthew G. Knepley         if (subpoint < 0) continue;
33633982b651SMatthew G. Knepley         if (newLocalPoints[point].rank == rank) {++ll; continue;}
33643982b651SMatthew G. Knepley         slocalPoints[sl]        = subpoint;
33653982b651SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
33663982b651SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
33672c71b3e2SJacob Faibussowitsch         PetscCheckFalse(sremotePoints[sl].rank  < 0,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %d", point);
33682c71b3e2SJacob Faibussowitsch         PetscCheckFalse(sremotePoints[sl].index < 0,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %d", point);
33693982b651SMatthew G. Knepley         ++sl;
33703982b651SMatthew G. Knepley       }
33712c71b3e2SJacob Faibussowitsch       PetscCheckFalse(sl + ll != numSubleaves,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %d + %d != %d", sl, ll, numSubleaves);
33723982b651SMatthew G. Knepley       ierr = PetscFree2(newLocalPoints,newOwners);CHKERRQ(ierr);
33733982b651SMatthew G. Knepley       ierr = PetscSFSetGraph(sfPointSub, numSubroots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
33743982b651SMatthew G. Knepley     }
33753982b651SMatthew G. Knepley     if (subpIS) {
33763982b651SMatthew G. Knepley       ierr = ISRestoreIndices(subpIS, &subpoints);CHKERRQ(ierr);
33773982b651SMatthew G. Knepley     }
33783982b651SMatthew G. Knepley   }
3379212103e5SMatthew Knepley   /* Filter labels */
3380212103e5SMatthew Knepley   ierr = DMPlexFilterLabels_Internal(dm, numSubPoints, subpoints, firstSubPoint, subdm);CHKERRQ(ierr);
3381efa14ee0SMatthew G Knepley   /* Cleanup */
3382e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
33830ae02aa4SMatthew G. Knepley     if (subpointIS[d]) {ierr = ISRestoreIndices(subpointIS[d], &subpoints[d]);CHKERRQ(ierr);}
3384e6ccafaeSMatthew G Knepley     ierr = ISDestroy(&subpointIS[d]);CHKERRQ(ierr);
3385e6ccafaeSMatthew G Knepley   }
3386efa14ee0SMatthew G Knepley   ierr = PetscFree4(numSubPoints,firstSubPoint,subpointIS,subpoints);CHKERRQ(ierr);
3387e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3388e6ccafaeSMatthew G Knepley }
3389e6ccafaeSMatthew G Knepley 
3390158acfadSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM subdm)
33913982b651SMatthew G. Knepley {
33923982b651SMatthew G. Knepley   PetscErrorCode ierr;
33933982b651SMatthew G. Knepley 
33943982b651SMatthew G. Knepley   PetscFunctionBegin;
3395158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmeshGeneric_Interpolated(dm, vertexLabel, value, markedFaces, PETSC_FALSE, 1, subdm);CHKERRQ(ierr);
33963982b651SMatthew G. Knepley   PetscFunctionReturn(0);
33973982b651SMatthew G. Knepley }
33983982b651SMatthew G. Knepley 
3399d0fa310fSMatthew G. Knepley /*@
3400e6ccafaeSMatthew G Knepley   DMPlexCreateSubmesh - Extract a hypersurface from the mesh using vertices defined by a label
3401e6ccafaeSMatthew G Knepley 
3402e6ccafaeSMatthew G Knepley   Input Parameters:
3403e6ccafaeSMatthew G Knepley + dm           - The original mesh
3404158acfadSMatthew G. Knepley . vertexLabel  - The DMLabel marking points contained in the surface
3405158acfadSMatthew G. Knepley . value        - The label value to use
3406158acfadSMatthew G. Knepley - markedFaces  - PETSC_TRUE if surface faces are marked in addition to vertices, PETSC_FALSE if only vertices are marked
3407e6ccafaeSMatthew G Knepley 
3408e6ccafaeSMatthew G Knepley   Output Parameter:
3409e6ccafaeSMatthew G Knepley . subdm - The surface mesh
3410e6ccafaeSMatthew G Knepley 
3411e6ccafaeSMatthew G Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3412e6ccafaeSMatthew G Knepley 
3413e6ccafaeSMatthew G Knepley   Level: developer
3414e6ccafaeSMatthew G Knepley 
3415c58f1c22SToby Isaac .seealso: DMPlexGetSubpointMap(), DMGetLabel(), DMLabelSetValue()
3416830e53efSMatthew G. Knepley @*/
3417158acfadSMatthew G. Knepley PetscErrorCode DMPlexCreateSubmesh(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM *subdm)
3418e6ccafaeSMatthew G Knepley {
3419827c4036SVaclav Hapla   DMPlexInterpolatedFlag interpolated;
3420827c4036SVaclav Hapla   PetscInt       dim, cdim;
3421e6ccafaeSMatthew G Knepley   PetscErrorCode ierr;
3422e6ccafaeSMatthew G Knepley 
3423e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3424e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3425064a246eSJacob Faibussowitsch   PetscValidPointer(subdm, 5);
3426c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
342782f516ccSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)dm), subdm);CHKERRQ(ierr);
3428e6ccafaeSMatthew G Knepley   ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr);
3429c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(*subdm, dim-1);CHKERRQ(ierr);
3430dd2763adSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3431dd2763adSMatthew G. Knepley   ierr = DMSetCoordinateDim(*subdm, cdim);CHKERRQ(ierr);
3432827c4036SVaclav Hapla   ierr = DMPlexIsInterpolated(dm, &interpolated);CHKERRQ(ierr);
34332c71b3e2SJacob Faibussowitsch   PetscCheckFalse(interpolated == DMPLEX_INTERPOLATED_PARTIAL,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not for partially interpolated meshes");
3434827c4036SVaclav Hapla   if (interpolated) {
3435158acfadSMatthew G. Knepley     ierr = DMPlexCreateSubmesh_Interpolated(dm, vertexLabel, value, markedFaces, *subdm);CHKERRQ(ierr);
3436e6ccafaeSMatthew G Knepley   } else {
3437830e53efSMatthew G. Knepley     ierr = DMPlexCreateSubmesh_Uninterpolated(dm, vertexLabel, value, *subdm);CHKERRQ(ierr);
3438e6ccafaeSMatthew G Knepley   }
3439*e600fa54SMatthew G. Knepley   ierr = DMPlexCopy_Internal(dm, PETSC_TRUE, *subdm);CHKERRQ(ierr);
3440e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3441e6ccafaeSMatthew G Knepley }
3442e6ccafaeSMatthew G Knepley 
344327c04023SMatthew G. Knepley static PetscErrorCode DMPlexCreateCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM subdm)
3444766ab985SMatthew G. Knepley {
3445766ab985SMatthew G. Knepley   MPI_Comm        comm;
3446766ab985SMatthew G. Knepley   DMLabel         subpointMap;
3447766ab985SMatthew G. Knepley   IS              subvertexIS;
3448766ab985SMatthew G. Knepley   const PetscInt *subVertices;
3449fed694aaSMatthew G. Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells, *subCells = NULL;
3450766ab985SMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces, firstSubFace, newFacePoint, nFV;
3451412e9a14SMatthew G. Knepley   PetscInt        c, f;
3452766ab985SMatthew G. Knepley   PetscErrorCode  ierr;
3453766ab985SMatthew G. Knepley 
3454766ab985SMatthew G. Knepley   PetscFunctionBegin;
3455766ab985SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
3456766ab985SMatthew G. Knepley   /* Create subpointMap which marks the submesh */
3457d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap);CHKERRQ(ierr);
3458766ab985SMatthew G. Knepley   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
3459766ab985SMatthew G. Knepley   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
346027c04023SMatthew G. Knepley   ierr = DMPlexMarkCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, subpointMap, &numSubFaces, &nFV, &subCells, subdm);CHKERRQ(ierr);
3461766ab985SMatthew G. Knepley   /* Setup chart */
3462766ab985SMatthew G. Knepley   ierr = DMLabelGetStratumSize(subpointMap, 0, &numSubVertices);CHKERRQ(ierr);
3463766ab985SMatthew G. Knepley   ierr = DMLabelGetStratumSize(subpointMap, 2, &numSubCells);CHKERRQ(ierr);
3464766ab985SMatthew G. Knepley   ierr = DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices);CHKERRQ(ierr);
3465766ab985SMatthew G. Knepley   ierr = DMPlexSetVTKCellHeight(subdm, 1);CHKERRQ(ierr);
3466766ab985SMatthew G. Knepley   /* Set cone sizes */
3467766ab985SMatthew G. Knepley   firstSubVertex = numSubCells;
3468766ab985SMatthew G. Knepley   firstSubFace   = numSubCells+numSubVertices;
3469766ab985SMatthew G. Knepley   newFacePoint   = firstSubFace;
3470766ab985SMatthew G. Knepley   ierr = DMLabelGetStratumIS(subpointMap, 0, &subvertexIS);CHKERRQ(ierr);
3471766ab985SMatthew G. Knepley   if (subvertexIS) {ierr = ISGetIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
3472766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3473766ab985SMatthew G. Knepley     ierr = DMPlexSetConeSize(subdm, c, 1);CHKERRQ(ierr);
3474766ab985SMatthew G. Knepley   }
3475766ab985SMatthew G. Knepley   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
3476766ab985SMatthew G. Knepley     ierr = DMPlexSetConeSize(subdm, f, nFV);CHKERRQ(ierr);
3477766ab985SMatthew G. Knepley   }
3478766ab985SMatthew G. Knepley   ierr = DMSetUp(subdm);CHKERRQ(ierr);
3479766ab985SMatthew G. Knepley   /* Create face cones */
3480766ab985SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
348169291d52SBarry Smith   ierr = DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
3482766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3483766ab985SMatthew G. Knepley     const PetscInt  cell    = subCells[c];
3484766ab985SMatthew G. Knepley     const PetscInt  subcell = c;
348587feddfdSMatthew G. Knepley     const PetscInt *cone, *cells;
3486412e9a14SMatthew G. Knepley     PetscBool       isHybrid;
348787feddfdSMatthew G. Knepley     PetscInt        numCells, subVertex, p, v;
3488766ab985SMatthew G. Knepley 
3489412e9a14SMatthew G. Knepley     ierr = DMPlexCellIsHybrid_Internal(dm, cell, &isHybrid);CHKERRQ(ierr);
3490412e9a14SMatthew G. Knepley     if (!isHybrid) continue;
349187feddfdSMatthew G. Knepley     ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
349287feddfdSMatthew G. Knepley     for (v = 0; v < nFV; ++v) {
349387feddfdSMatthew G. Knepley       ierr = PetscFindInt(cone[v], numSubVertices, subVertices, &subVertex);CHKERRQ(ierr);
349487feddfdSMatthew G. Knepley       subface[v] = firstSubVertex+subVertex;
349587feddfdSMatthew G. Knepley     }
349687feddfdSMatthew G. Knepley     ierr = DMPlexSetCone(subdm, newFacePoint, subface);CHKERRQ(ierr);
349787feddfdSMatthew G. Knepley     ierr = DMPlexSetCone(subdm, subcell, &newFacePoint);CHKERRQ(ierr);
349887feddfdSMatthew G. Knepley     ierr = DMPlexGetJoin(dm, nFV, cone, &numCells, &cells);CHKERRQ(ierr);
349927234c99SMatthew G. Knepley     /* Not true in parallel
35002c71b3e2SJacob Faibussowitsch     PetscCheckFalse(numCells != 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
350187feddfdSMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
350287feddfdSMatthew G. Knepley       PetscInt  negsubcell;
3503412e9a14SMatthew G. Knepley       PetscBool isHybrid;
3504766ab985SMatthew G. Knepley 
3505412e9a14SMatthew G. Knepley       ierr = DMPlexCellIsHybrid_Internal(dm, cells[p], &isHybrid);CHKERRQ(ierr);
3506412e9a14SMatthew G. Knepley       if (isHybrid) continue;
350787feddfdSMatthew G. Knepley       /* I know this is a crap search */
350887feddfdSMatthew G. Knepley       for (negsubcell = 0; negsubcell < numSubCells; ++negsubcell) {
350987feddfdSMatthew G. Knepley         if (subCells[negsubcell] == cells[p]) break;
3510766ab985SMatthew G. Knepley       }
35112c71b3e2SJacob Faibussowitsch       PetscCheckFalse(negsubcell == numSubCells,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find negative face neighbor for cohesive cell %d", cell);
351287feddfdSMatthew G. Knepley       ierr = DMPlexSetCone(subdm, negsubcell, &newFacePoint);CHKERRQ(ierr);
3513766ab985SMatthew G. Knepley     }
351487feddfdSMatthew G. Knepley     ierr = DMPlexRestoreJoin(dm, nFV, cone, &numCells, &cells);CHKERRQ(ierr);
351587feddfdSMatthew G. Knepley     ++newFacePoint;
3516766ab985SMatthew G. Knepley   }
351769291d52SBarry Smith   ierr = DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
3518766ab985SMatthew G. Knepley   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
3519766ab985SMatthew G. Knepley   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
3520766ab985SMatthew G. Knepley   /* Build coordinates */
3521766ab985SMatthew G. Knepley   {
3522766ab985SMatthew G. Knepley     PetscSection coordSection, subCoordSection;
3523766ab985SMatthew G. Knepley     Vec          coordinates, subCoordinates;
3524766ab985SMatthew G. Knepley     PetscScalar *coords, *subCoords;
3525c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize, v;
352624640c55SToby Isaac     const char  *name;
3527766ab985SMatthew G. Knepley 
3528c0e8cf5fSMatthew G. Knepley     ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
352969d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
3530766ab985SMatthew G. Knepley     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
353169d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
3532285d324eSMatthew G. Knepley     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
3533285d324eSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
3534285d324eSMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
3535766ab985SMatthew G. Knepley     ierr = PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices);CHKERRQ(ierr);
3536766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3537766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3538766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex+v;
3539766ab985SMatthew G. Knepley       PetscInt       dof;
3540766ab985SMatthew G. Knepley 
3541766ab985SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3542766ab985SMatthew G. Knepley       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
3543285d324eSMatthew G. Knepley       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
3544766ab985SMatthew G. Knepley     }
3545766ab985SMatthew G. Knepley     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
3546766ab985SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
35478b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &subCoordinates);CHKERRQ(ierr);
354824640c55SToby Isaac     ierr = PetscObjectGetName((PetscObject)coordinates,&name);CHKERRQ(ierr);
354924640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)subCoordinates,name);CHKERRQ(ierr);
3550766ab985SMatthew G. Knepley     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
3551c0e8cf5fSMatthew G. Knepley     ierr = VecSetBlockSize(subCoordinates, cdim);CHKERRQ(ierr);
35522eb5907fSJed Brown     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
3553766ab985SMatthew G. Knepley     ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
3554766ab985SMatthew G. Knepley     ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3555766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3556766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3557766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex+v;
3558766ab985SMatthew G. Knepley       PetscInt       dof, off, sdof, soff, d;
3559766ab985SMatthew G. Knepley 
3560766ab985SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3561766ab985SMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
3562766ab985SMatthew G. Knepley       ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
3563766ab985SMatthew G. Knepley       ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
35642c71b3e2SJacob Faibussowitsch       PetscCheckFalse(dof != sdof,comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
3565766ab985SMatthew G. Knepley       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3566766ab985SMatthew G. Knepley     }
3567766ab985SMatthew G. Knepley     ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
3568766ab985SMatthew G. Knepley     ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3569766ab985SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
3570766ab985SMatthew G. Knepley     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
3571766ab985SMatthew G. Knepley   }
3572aca35d17SMatthew G. Knepley   /* Build SF */
3573aca35d17SMatthew G. Knepley   CHKMEMQ;
3574aca35d17SMatthew G. Knepley   {
3575aca35d17SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
3576aca35d17SMatthew G. Knepley     const PetscSFNode *remotePoints;
3577bdcf2095SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
3578aca35d17SMatthew G. Knepley     const PetscInt    *localPoints;
3579bdcf2095SMatthew G. Knepley     PetscInt          *slocalPoints;
358049c26ae4SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubRoots = numSubCells+numSubFaces+numSubVertices, numSubLeaves = 0, l, sl, ll, pStart, pEnd, p, vStart, vEnd;
3581bdcf2095SMatthew G. Knepley     PetscMPIInt        rank;
3582aca35d17SMatthew G. Knepley 
3583ffc4695bSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr);
3584aca35d17SMatthew G. Knepley     ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
3585aca35d17SMatthew G. Knepley     ierr = DMGetPointSF(subdm, &sfPointSub);CHKERRQ(ierr);
3586aca35d17SMatthew G. Knepley     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3587aca35d17SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
3588aca35d17SMatthew G. Knepley     ierr = PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr);
3589aca35d17SMatthew G. Knepley     if (numRoots >= 0) {
3590aca35d17SMatthew G. Knepley       /* Only vertices should be shared */
3591dcca6d9dSJed Brown       ierr = PetscMalloc2(pEnd-pStart,&newLocalPoints,numRoots,&newOwners);CHKERRQ(ierr);
3592bdcf2095SMatthew G. Knepley       for (p = 0; p < pEnd-pStart; ++p) {
3593bdcf2095SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
3594bdcf2095SMatthew G. Knepley         newLocalPoints[p].index = -2;
3595bdcf2095SMatthew G. Knepley       }
35969e0823b2SMatthew G. Knepley       /* Set subleaves */
3597aca35d17SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
3598aca35d17SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3599bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3600aca35d17SMatthew G. Knepley 
36012c71b3e2SJacob Faibussowitsch         PetscCheckFalse((point < vStart) && (point >= vEnd),PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Should not be mapping anything but vertices, %d", point);
3602bdcf2095SMatthew G. Knepley         if (subPoint < 0) continue;
3603bdcf2095SMatthew G. Knepley         newLocalPoints[point-pStart].rank  = rank;
3604bdcf2095SMatthew G. Knepley         newLocalPoints[point-pStart].index = subPoint;
3605bdcf2095SMatthew G. Knepley         ++numSubLeaves;
3606aca35d17SMatthew G. Knepley       }
36079e0823b2SMatthew G. Knepley       /* Must put in owned subpoints */
36089e0823b2SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
36099e0823b2SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(p, firstSubVertex, numSubVertices, subVertices);
36109e0823b2SMatthew G. Knepley 
36119e0823b2SMatthew G. Knepley         if (subPoint < 0) {
36129e0823b2SMatthew G. Knepley           newOwners[p-pStart].rank  = -3;
36139e0823b2SMatthew G. Knepley           newOwners[p-pStart].index = -3;
36149e0823b2SMatthew G. Knepley         } else {
36159e0823b2SMatthew G. Knepley           newOwners[p-pStart].rank  = rank;
36169e0823b2SMatthew G. Knepley           newOwners[p-pStart].index = subPoint;
36179e0823b2SMatthew G. Knepley         }
3618bdcf2095SMatthew G. Knepley       }
3619bdcf2095SMatthew G. Knepley       ierr = PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
3620bdcf2095SMatthew G. Knepley       ierr = PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
3621ad227feaSJunchao Zhang       ierr = PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints,MPI_REPLACE);CHKERRQ(ierr);
3622ad227feaSJunchao Zhang       ierr = PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints,MPI_REPLACE);CHKERRQ(ierr);
3623785e854fSJed Brown       ierr = PetscMalloc1(numSubLeaves,    &slocalPoints);CHKERRQ(ierr);
3624785e854fSJed Brown       ierr = PetscMalloc1(numSubLeaves, &sremotePoints);CHKERRQ(ierr);
362549c26ae4SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
3626bdcf2095SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3627bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3628aca35d17SMatthew G. Knepley 
3629aca35d17SMatthew G. Knepley         if (subPoint < 0) continue;
363049c26ae4SMatthew G. Knepley         if (newLocalPoints[point].rank == rank) {++ll; continue;}
3631aca35d17SMatthew G. Knepley         slocalPoints[sl]        = subPoint;
3632bdcf2095SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
3633bdcf2095SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
36342c71b3e2SJacob Faibussowitsch         PetscCheckFalse(sremotePoints[sl].rank  < 0,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %d", point);
36352c71b3e2SJacob Faibussowitsch         PetscCheckFalse(sremotePoints[sl].index < 0,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %d", point);
3636aca35d17SMatthew G. Knepley         ++sl;
3637aca35d17SMatthew G. Knepley       }
3638bdcf2095SMatthew G. Knepley       ierr = PetscFree2(newLocalPoints,newOwners);CHKERRQ(ierr);
36392c71b3e2SJacob Faibussowitsch       PetscCheckFalse(sl + ll != numSubLeaves,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %d + %d != %d", sl, ll, numSubLeaves);
364049c26ae4SMatthew G. Knepley       ierr = PetscSFSetGraph(sfPointSub, numSubRoots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
3641aca35d17SMatthew G. Knepley     }
3642aca35d17SMatthew G. Knepley   }
3643aca35d17SMatthew G. Knepley   CHKMEMQ;
3644766ab985SMatthew G. Knepley   /* Cleanup */
3645766ab985SMatthew G. Knepley   if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
3646766ab985SMatthew G. Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
3647766ab985SMatthew G. Knepley   ierr = PetscFree(subCells);CHKERRQ(ierr);
3648766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
3649766ab985SMatthew G. Knepley }
3650766ab985SMatthew G. Knepley 
36513982b651SMatthew G. Knepley static PetscErrorCode DMPlexCreateCohesiveSubmesh_Interpolated(DM dm, const char labelname[], PetscInt value, DM subdm)
3652766ab985SMatthew G. Knepley {
36533982b651SMatthew G. Knepley   DMLabel        label = NULL;
3654766ab985SMatthew G. Knepley   PetscErrorCode ierr;
3655766ab985SMatthew G. Knepley 
3656766ab985SMatthew G. Knepley   PetscFunctionBegin;
3657c58f1c22SToby Isaac   if (labelname) {ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);}
3658158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmeshGeneric_Interpolated(dm, label, value, PETSC_FALSE, PETSC_TRUE, 1, subdm);CHKERRQ(ierr);
3659766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
3660766ab985SMatthew G. Knepley }
3661766ab985SMatthew G. Knepley 
3662c08575a3SMatthew G. Knepley /*@C
36637d2fefb6SMatthew G. Knepley   DMPlexCreateCohesiveSubmesh - Extract from a mesh with cohesive cells the hypersurface defined by one face of the cells. Optionally, a Label can be given to restrict the cells.
3664766ab985SMatthew G. Knepley 
3665766ab985SMatthew G. Knepley   Input Parameters:
3666766ab985SMatthew G. Knepley + dm          - The original mesh
366727c04023SMatthew G. Knepley . hasLagrange - The mesh has Lagrange unknowns in the cohesive cells
36687afc1a8bSJed Brown . label       - A label name, or NULL
366927c04023SMatthew G. Knepley - value  - A label value
3670766ab985SMatthew G. Knepley 
3671766ab985SMatthew G. Knepley   Output Parameter:
3672766ab985SMatthew G. Knepley . subdm - The surface mesh
3673766ab985SMatthew G. Knepley 
3674766ab985SMatthew G. Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3675766ab985SMatthew G. Knepley 
3676766ab985SMatthew G. Knepley   Level: developer
3677766ab985SMatthew G. Knepley 
3678766ab985SMatthew G. Knepley .seealso: DMPlexGetSubpointMap(), DMPlexCreateSubmesh()
3679c08575a3SMatthew G. Knepley @*/
368027c04023SMatthew G. Knepley PetscErrorCode DMPlexCreateCohesiveSubmesh(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM *subdm)
3681766ab985SMatthew G. Knepley {
3682c0e8cf5fSMatthew G. Knepley   PetscInt       dim, cdim, depth;
3683766ab985SMatthew G. Knepley   PetscErrorCode ierr;
3684766ab985SMatthew G. Knepley 
3685766ab985SMatthew G. Knepley   PetscFunctionBegin;
3686766ab985SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
368727c04023SMatthew G. Knepley   PetscValidPointer(subdm, 5);
3688c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3689766ab985SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3690766ab985SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), subdm);CHKERRQ(ierr);
3691766ab985SMatthew G. Knepley   ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr);
3692c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(*subdm, dim-1);CHKERRQ(ierr);
3693c0e8cf5fSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3694c0e8cf5fSMatthew G. Knepley   ierr = DMSetCoordinateDim(*subdm, cdim);CHKERRQ(ierr);
3695766ab985SMatthew G. Knepley   if (depth == dim) {
3696b3154360SMatthew G. Knepley     ierr = DMPlexCreateCohesiveSubmesh_Interpolated(dm, label, value, *subdm);CHKERRQ(ierr);
3697766ab985SMatthew G. Knepley   } else {
369827c04023SMatthew G. Knepley     ierr = DMPlexCreateCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, *subdm);CHKERRQ(ierr);
3699e6ccafaeSMatthew G Knepley   }
3700*e600fa54SMatthew G. Knepley   ierr = DMPlexCopy_Internal(dm, PETSC_TRUE, *subdm);CHKERRQ(ierr);
3701e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3702e6ccafaeSMatthew G Knepley }
3703e6ccafaeSMatthew G Knepley 
3704bec263e5SMatthew G. Knepley /*@
3705bec263e5SMatthew G. Knepley   DMPlexFilter - Extract a subset of mesh cells defined by a label as a separate mesh
3706bec263e5SMatthew G. Knepley 
3707bec263e5SMatthew G. Knepley   Input Parameters:
3708bec263e5SMatthew G. Knepley + dm        - The original mesh
3709bec263e5SMatthew G. Knepley . cellLabel - The DMLabel marking cells contained in the new mesh
3710bec263e5SMatthew G. Knepley - value     - The label value to use
3711bec263e5SMatthew G. Knepley 
3712bec263e5SMatthew G. Knepley   Output Parameter:
3713bec263e5SMatthew G. Knepley . subdm - The new mesh
3714bec263e5SMatthew G. Knepley 
3715bec263e5SMatthew G. Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3716bec263e5SMatthew G. Knepley 
3717bec263e5SMatthew G. Knepley   Level: developer
3718bec263e5SMatthew G. Knepley 
3719c58f1c22SToby Isaac .seealso: DMPlexGetSubpointMap(), DMGetLabel(), DMLabelSetValue()
3720bec263e5SMatthew G. Knepley @*/
3721bec263e5SMatthew G. Knepley PetscErrorCode DMPlexFilter(DM dm, DMLabel cellLabel, PetscInt value, DM *subdm)
3722bec263e5SMatthew G. Knepley {
3723bec263e5SMatthew G. Knepley   PetscInt       dim;
3724bec263e5SMatthew G. Knepley   PetscErrorCode ierr;
3725bec263e5SMatthew G. Knepley 
3726bec263e5SMatthew G. Knepley   PetscFunctionBegin;
3727bec263e5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3728064a246eSJacob Faibussowitsch   PetscValidPointer(subdm, 4);
3729bec263e5SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3730bec263e5SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), subdm);CHKERRQ(ierr);
3731bec263e5SMatthew G. Knepley   ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr);
3732bec263e5SMatthew G. Knepley   ierr = DMSetDimension(*subdm, dim);CHKERRQ(ierr);
3733bec263e5SMatthew G. Knepley   /* Extract submesh in place, could be empty on some procs, could have inconsistency if procs do not both extract a shared cell */
3734158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmeshGeneric_Interpolated(dm, cellLabel, value, PETSC_FALSE, PETSC_FALSE, 0, *subdm);CHKERRQ(ierr);
3735*e600fa54SMatthew G. Knepley   ierr = DMPlexCopy_Internal(dm, PETSC_TRUE, *subdm);CHKERRQ(ierr);
3736bec263e5SMatthew G. Knepley   PetscFunctionReturn(0);
3737bec263e5SMatthew G. Knepley }
3738bec263e5SMatthew G. Knepley 
373964beef6dSMatthew G. Knepley /*@
374064beef6dSMatthew G. Knepley   DMPlexGetSubpointMap - Returns a DMLabel with point dimension as values
374164beef6dSMatthew G. Knepley 
374264beef6dSMatthew G. Knepley   Input Parameter:
374364beef6dSMatthew G. Knepley . dm - The submesh DM
374464beef6dSMatthew G. Knepley 
374564beef6dSMatthew G. Knepley   Output Parameter:
374664beef6dSMatthew G. Knepley . subpointMap - The DMLabel of all the points from the original mesh in this submesh, or NULL if this is not a submesh
374764beef6dSMatthew G. Knepley 
374864beef6dSMatthew G. Knepley   Level: developer
374964beef6dSMatthew G. Knepley 
375097d8846cSMatthew Knepley .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointIS()
375164beef6dSMatthew G. Knepley @*/
3752e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexGetSubpointMap(DM dm, DMLabel *subpointMap)
3753e6ccafaeSMatthew G Knepley {
3754e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3755e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3756e6ccafaeSMatthew G Knepley   PetscValidPointer(subpointMap, 2);
375765663942SMatthew G. Knepley   *subpointMap = ((DM_Plex*) dm->data)->subpointMap;
3758e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3759e6ccafaeSMatthew G Knepley }
3760e6ccafaeSMatthew G Knepley 
3761c08575a3SMatthew G. Knepley /*@
3762c08575a3SMatthew G. Knepley   DMPlexSetSubpointMap - Sets the DMLabel with point dimension as values
3763c08575a3SMatthew G. Knepley 
3764c08575a3SMatthew G. Knepley   Input Parameters:
3765c08575a3SMatthew G. Knepley + dm - The submesh DM
3766c08575a3SMatthew G. Knepley - subpointMap - The DMLabel of all the points from the original mesh in this submesh
3767c08575a3SMatthew G. Knepley 
3768c08575a3SMatthew G. Knepley   Note: Should normally not be called by the user, since it is set in DMPlexCreateSubmesh()
3769c08575a3SMatthew G. Knepley 
3770c08575a3SMatthew G. Knepley   Level: developer
3771c08575a3SMatthew G. Knepley 
377297d8846cSMatthew Knepley .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointIS()
3773c08575a3SMatthew G. Knepley @*/
3774e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexSetSubpointMap(DM dm, DMLabel subpointMap)
3775e6ccafaeSMatthew G Knepley {
3776e6ccafaeSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
3777285d324eSMatthew G. Knepley   DMLabel        tmp;
3778e6ccafaeSMatthew G Knepley   PetscErrorCode ierr;
3779e6ccafaeSMatthew G Knepley 
3780e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3781e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3782285d324eSMatthew G. Knepley   tmp  = mesh->subpointMap;
3783e6ccafaeSMatthew G Knepley   mesh->subpointMap = subpointMap;
3784d67d17b1SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) mesh->subpointMap);CHKERRQ(ierr);
3785285d324eSMatthew G. Knepley   ierr = DMLabelDestroy(&tmp);CHKERRQ(ierr);
3786e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3787e6ccafaeSMatthew G Knepley }
3788e6ccafaeSMatthew G Knepley 
378997d8846cSMatthew Knepley static PetscErrorCode DMPlexCreateSubpointIS_Internal(DM dm, IS *subpointIS)
379097d8846cSMatthew Knepley {
379197d8846cSMatthew Knepley   DMLabel        spmap;
379297d8846cSMatthew Knepley   PetscInt       depth, d;
379397d8846cSMatthew Knepley   PetscErrorCode ierr;
379497d8846cSMatthew Knepley 
379597d8846cSMatthew Knepley   PetscFunctionBegin;
379697d8846cSMatthew Knepley   ierr = DMPlexGetSubpointMap(dm, &spmap);CHKERRQ(ierr);
379797d8846cSMatthew Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
379897d8846cSMatthew Knepley   if (spmap && depth >= 0) {
379997d8846cSMatthew Knepley     DM_Plex  *mesh = (DM_Plex *) dm->data;
380097d8846cSMatthew Knepley     PetscInt *points, *depths;
380197d8846cSMatthew Knepley     PetscInt  pStart, pEnd, p, off;
380297d8846cSMatthew Knepley 
380397d8846cSMatthew Knepley     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
38042c71b3e2SJacob Faibussowitsch     PetscCheckFalse(pStart,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Submeshes must start the point numbering at 0, not %d", pStart);
380597d8846cSMatthew Knepley     ierr = PetscMalloc1(pEnd, &points);CHKERRQ(ierr);
380697d8846cSMatthew Knepley     ierr = DMGetWorkArray(dm, depth+1, MPIU_INT, &depths);CHKERRQ(ierr);
380797d8846cSMatthew Knepley     depths[0] = depth;
380897d8846cSMatthew Knepley     depths[1] = 0;
380997d8846cSMatthew Knepley     for (d = 2; d <= depth; ++d) {depths[d] = depth+1 - d;}
381097d8846cSMatthew Knepley     for (d = 0, off = 0; d <= depth; ++d) {
381197d8846cSMatthew Knepley       const PetscInt dep = depths[d];
381297d8846cSMatthew Knepley       PetscInt       depStart, depEnd, n;
381397d8846cSMatthew Knepley 
381497d8846cSMatthew Knepley       ierr = DMPlexGetDepthStratum(dm, dep, &depStart, &depEnd);CHKERRQ(ierr);
381597d8846cSMatthew Knepley       ierr = DMLabelGetStratumSize(spmap, dep, &n);CHKERRQ(ierr);
381697d8846cSMatthew Knepley       if (((d < 2) && (depth > 1)) || (d == 1)) { /* Only check vertices and cells for now since the map is broken for others */
38172c71b3e2SJacob Faibussowitsch         PetscCheckFalse(n != depEnd-depStart,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %d at depth %d should be %d", n, dep, depEnd-depStart);
381897d8846cSMatthew Knepley       } else {
381997d8846cSMatthew Knepley         if (!n) {
382097d8846cSMatthew Knepley           if (d == 0) {
382197d8846cSMatthew Knepley             /* Missing cells */
382297d8846cSMatthew Knepley             for (p = 0; p < depEnd-depStart; ++p, ++off) points[off] = -1;
382397d8846cSMatthew Knepley           } else {
382497d8846cSMatthew Knepley             /* Missing faces */
382597d8846cSMatthew Knepley             for (p = 0; p < depEnd-depStart; ++p, ++off) points[off] = PETSC_MAX_INT;
382697d8846cSMatthew Knepley           }
382797d8846cSMatthew Knepley         }
382897d8846cSMatthew Knepley       }
382997d8846cSMatthew Knepley       if (n) {
383097d8846cSMatthew Knepley         IS              is;
383197d8846cSMatthew Knepley         const PetscInt *opoints;
383297d8846cSMatthew Knepley 
383397d8846cSMatthew Knepley         ierr = DMLabelGetStratumIS(spmap, dep, &is);CHKERRQ(ierr);
383497d8846cSMatthew Knepley         ierr = ISGetIndices(is, &opoints);CHKERRQ(ierr);
383597d8846cSMatthew Knepley         for (p = 0; p < n; ++p, ++off) points[off] = opoints[p];
383697d8846cSMatthew Knepley         ierr = ISRestoreIndices(is, &opoints);CHKERRQ(ierr);
383797d8846cSMatthew Knepley         ierr = ISDestroy(&is);CHKERRQ(ierr);
383897d8846cSMatthew Knepley       }
383997d8846cSMatthew Knepley     }
384097d8846cSMatthew Knepley     ierr = DMRestoreWorkArray(dm, depth+1, MPIU_INT, &depths);CHKERRQ(ierr);
38412c71b3e2SJacob Faibussowitsch     PetscCheckFalse(off != pEnd,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %d should be %d", off, pEnd);
384297d8846cSMatthew Knepley     ierr = ISCreateGeneral(PETSC_COMM_SELF, pEnd, points, PETSC_OWN_POINTER, subpointIS);CHKERRQ(ierr);
384397d8846cSMatthew Knepley     ierr = PetscObjectStateGet((PetscObject) spmap, &mesh->subpointState);CHKERRQ(ierr);
384497d8846cSMatthew Knepley   }
384597d8846cSMatthew Knepley   PetscFunctionReturn(0);
384697d8846cSMatthew Knepley }
384797d8846cSMatthew Knepley 
384864beef6dSMatthew G. Knepley /*@
384997d8846cSMatthew Knepley   DMPlexGetSubpointIS - Returns an IS covering the entire subdm chart with the original points as data
3850e6ccafaeSMatthew G Knepley 
3851e6ccafaeSMatthew G Knepley   Input Parameter:
3852e6ccafaeSMatthew G Knepley . dm - The submesh DM
3853e6ccafaeSMatthew G Knepley 
3854e6ccafaeSMatthew G Knepley   Output Parameter:
38550298fd71SBarry Smith . subpointIS - The IS of all the points from the original mesh in this submesh, or NULL if this is not a submesh
3856e6ccafaeSMatthew G Knepley 
38573982b651SMatthew G. Knepley   Note: This IS is guaranteed to be sorted by the construction of the submesh
385864beef6dSMatthew G. Knepley 
385964beef6dSMatthew G. Knepley   Level: developer
386064beef6dSMatthew G. Knepley 
386164beef6dSMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointMap()
386264beef6dSMatthew G. Knepley @*/
386397d8846cSMatthew Knepley PetscErrorCode DMPlexGetSubpointIS(DM dm, IS *subpointIS)
3864e6ccafaeSMatthew G Knepley {
386597d8846cSMatthew Knepley   DM_Plex         *mesh = (DM_Plex *) dm->data;
386697d8846cSMatthew Knepley   DMLabel          spmap;
386797d8846cSMatthew Knepley   PetscObjectState state;
3868e6ccafaeSMatthew G Knepley   PetscErrorCode   ierr;
3869e6ccafaeSMatthew G Knepley 
3870e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3871e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3872e6ccafaeSMatthew G Knepley   PetscValidPointer(subpointIS, 2);
387397d8846cSMatthew Knepley   ierr = DMPlexGetSubpointMap(dm, &spmap);CHKERRQ(ierr);
387497d8846cSMatthew Knepley   ierr = PetscObjectStateGet((PetscObject) spmap, &state);CHKERRQ(ierr);
387597d8846cSMatthew Knepley   if (state != mesh->subpointState || !mesh->subpointIS) {ierr = DMPlexCreateSubpointIS_Internal(dm, &mesh->subpointIS);CHKERRQ(ierr);}
387697d8846cSMatthew Knepley   *subpointIS = mesh->subpointIS;
3877e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3878e6ccafaeSMatthew G Knepley }
3879559a1558SMatthew G. Knepley 
3880559a1558SMatthew G. Knepley /*@
3881a6e0b375SMatthew G. Knepley   DMGetEnclosureRelation - Get the relationship between dmA and dmB
3882559a1558SMatthew G. Knepley 
3883559a1558SMatthew G. Knepley   Input Parameters:
3884a6e0b375SMatthew G. Knepley + dmA - The first DM
3885a6e0b375SMatthew G. Knepley - dmB - The second DM
3886559a1558SMatthew G. Knepley 
3887559a1558SMatthew G. Knepley   Output Parameter:
3888a6e0b375SMatthew G. Knepley . rel - The relation of dmA to dmB
3889559a1558SMatthew G. Knepley 
3890a6e0b375SMatthew G. Knepley   Level: intermediate
3891559a1558SMatthew G. Knepley 
3892446c23c1SPierre Jolivet .seealso: DMGetEnclosurePoint()
3893559a1558SMatthew G. Knepley @*/
3894a6e0b375SMatthew G. Knepley PetscErrorCode DMGetEnclosureRelation(DM dmA, DM dmB, DMEnclosureType *rel)
3895559a1558SMatthew G. Knepley {
3896a6e0b375SMatthew G. Knepley   DM             plexA, plexB, sdm;
3897559a1558SMatthew G. Knepley   DMLabel        spmap;
3898a6e0b375SMatthew G. Knepley   PetscInt       pStartA, pEndA, pStartB, pEndB, NpA, NpB;
3899559a1558SMatthew G. Knepley   PetscErrorCode ierr;
3900559a1558SMatthew G. Knepley 
390144171101SMatthew G. Knepley   PetscFunctionBegin;
3902a6e0b375SMatthew G. Knepley   PetscValidPointer(rel, 3);
3903a6e0b375SMatthew G. Knepley   *rel = DM_ENC_NONE;
3904a6e0b375SMatthew G. Knepley   if (!dmA || !dmB) PetscFunctionReturn(0);
3905a6e0b375SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
3906064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
3907a6e0b375SMatthew G. Knepley   if (dmA == dmB) {*rel = DM_ENC_EQUALITY; PetscFunctionReturn(0);}
3908a6e0b375SMatthew G. Knepley   ierr = DMConvert(dmA, DMPLEX, &plexA);CHKERRQ(ierr);
3909a6e0b375SMatthew G. Knepley   ierr = DMConvert(dmB, DMPLEX, &plexB);CHKERRQ(ierr);
3910a6e0b375SMatthew G. Knepley   ierr = DMPlexGetChart(plexA, &pStartA, &pEndA);CHKERRQ(ierr);
3911a6e0b375SMatthew G. Knepley   ierr = DMPlexGetChart(plexB, &pStartB, &pEndB);CHKERRQ(ierr);
3912a6e0b375SMatthew G. Knepley   /* Assumption 1: subDMs have smaller charts than the DMs that they originate from
3913a6e0b375SMatthew G. Knepley     - The degenerate case of a subdomain which includes all of the domain on some process can be treated as equality */
3914a6e0b375SMatthew G. Knepley   if ((pStartA == pStartB) && (pEndA == pEndB)) {
3915a6e0b375SMatthew G. Knepley     *rel = DM_ENC_EQUALITY;
3916a6e0b375SMatthew G. Knepley     goto end;
3917559a1558SMatthew G. Knepley   }
3918a6e0b375SMatthew G. Knepley   NpA = pEndA - pStartA;
3919a6e0b375SMatthew G. Knepley   NpB = pEndB - pStartB;
3920a6e0b375SMatthew G. Knepley   if (NpA == NpB) goto end;
3921a6e0b375SMatthew G. Knepley   sdm = NpA > NpB ? plexB : plexA; /* The other is the original, enclosing dm */
3922a6e0b375SMatthew G. Knepley   ierr = DMPlexGetSubpointMap(sdm, &spmap);CHKERRQ(ierr);
3923a6e0b375SMatthew G. Knepley   if (!spmap) goto end;
3924a6e0b375SMatthew G. Knepley   /* TODO Check the space mapped to by subpointMap is same size as dm */
3925a6e0b375SMatthew G. Knepley   if (NpA > NpB) {
3926a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUPERMESH;
3927a6e0b375SMatthew G. Knepley   } else {
3928a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUBMESH;
3929a6e0b375SMatthew G. Knepley   }
3930a6e0b375SMatthew G. Knepley   end:
3931a6e0b375SMatthew G. Knepley   ierr = DMDestroy(&plexA);CHKERRQ(ierr);
3932a6e0b375SMatthew G. Knepley   ierr = DMDestroy(&plexB);CHKERRQ(ierr);
3933559a1558SMatthew G. Knepley   PetscFunctionReturn(0);
3934559a1558SMatthew G. Knepley }
393544171101SMatthew G. Knepley 
393644171101SMatthew G. Knepley /*@
3937a6e0b375SMatthew G. Knepley   DMGetEnclosurePoint - Get the point pA in dmA which corresponds to the point pB in dmB
393844171101SMatthew G. Knepley 
393944171101SMatthew G. Knepley   Input Parameters:
3940a6e0b375SMatthew G. Knepley + dmA   - The first DM
3941a6e0b375SMatthew G. Knepley . dmB   - The second DM
3942a6e0b375SMatthew G. Knepley . etype - The type of enclosure relation that dmA has to dmB
3943a6e0b375SMatthew G. Knepley - pB    - A point of dmB
394444171101SMatthew G. Knepley 
394544171101SMatthew G. Knepley   Output Parameter:
3946a6e0b375SMatthew G. Knepley . pA    - The corresponding point of dmA
394744171101SMatthew G. Knepley 
3948a6e0b375SMatthew G. Knepley   Level: intermediate
394944171101SMatthew G. Knepley 
3950a6e0b375SMatthew G. Knepley .seealso: DMGetEnclosureRelation()
395144171101SMatthew G. Knepley @*/
3952a6e0b375SMatthew G. Knepley PetscErrorCode DMGetEnclosurePoint(DM dmA, DM dmB, DMEnclosureType etype, PetscInt pB, PetscInt *pA)
395344171101SMatthew G. Knepley {
3954a6e0b375SMatthew G. Knepley   DM              sdm;
3955a6e0b375SMatthew G. Knepley   IS              subpointIS;
3956a6e0b375SMatthew G. Knepley   const PetscInt *subpoints;
3957a6e0b375SMatthew G. Knepley   PetscInt        numSubpoints;
395844171101SMatthew G. Knepley   PetscErrorCode  ierr;
395944171101SMatthew G. Knepley 
396044171101SMatthew G. Knepley   PetscFunctionBegin;
3961a6e0b375SMatthew G. Knepley   /* TODO Cache the IS, making it look like an index */
3962a6e0b375SMatthew G. Knepley   switch (etype) {
3963a6e0b375SMatthew G. Knepley     case DM_ENC_SUPERMESH:
3964a6e0b375SMatthew G. Knepley     sdm  = dmB;
396597d8846cSMatthew Knepley     ierr = DMPlexGetSubpointIS(sdm, &subpointIS);CHKERRQ(ierr);
3966a6e0b375SMatthew G. Knepley     ierr = ISGetIndices(subpointIS, &subpoints);CHKERRQ(ierr);
3967a6e0b375SMatthew G. Knepley     *pA  = subpoints[pB];
3968a6e0b375SMatthew G. Knepley     ierr = ISRestoreIndices(subpointIS, &subpoints);CHKERRQ(ierr);
3969a6e0b375SMatthew G. Knepley     break;
3970a6e0b375SMatthew G. Knepley     case DM_ENC_SUBMESH:
3971a6e0b375SMatthew G. Knepley     sdm  = dmA;
397297d8846cSMatthew Knepley     ierr = DMPlexGetSubpointIS(sdm, &subpointIS);CHKERRQ(ierr);
3973a6e0b375SMatthew G. Knepley     ierr = ISGetLocalSize(subpointIS, &numSubpoints);CHKERRQ(ierr);
3974a6e0b375SMatthew G. Knepley     ierr = ISGetIndices(subpointIS, &subpoints);CHKERRQ(ierr);
3975a6e0b375SMatthew G. Knepley     ierr = PetscFindInt(pB, numSubpoints, subpoints, pA);CHKERRQ(ierr);
3976a6e0b375SMatthew G. Knepley     if (*pA < 0) {
3977a6e0b375SMatthew G. Knepley       ierr = DMViewFromOptions(dmA, NULL, "-dm_enc_A_view");CHKERRQ(ierr);
3978a6e0b375SMatthew G. Knepley       ierr = DMViewFromOptions(dmB, NULL, "-dm_enc_B_view");CHKERRQ(ierr);
397998921bdaSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %d not found in submesh", pB);
3980a6e0b375SMatthew G. Knepley     }
3981a6e0b375SMatthew G. Knepley     ierr = ISRestoreIndices(subpointIS, &subpoints);CHKERRQ(ierr);
3982a6e0b375SMatthew G. Knepley     break;
3983a6e0b375SMatthew G. Knepley     case DM_ENC_EQUALITY:
3984a6e0b375SMatthew G. Knepley     case DM_ENC_NONE:
3985a6e0b375SMatthew G. Knepley     *pA = pB;break;
3986a6e0b375SMatthew G. Knepley     case DM_ENC_UNKNOWN:
3987a6e0b375SMatthew G. Knepley     {
3988a6e0b375SMatthew G. Knepley       DMEnclosureType enc;
398944171101SMatthew G. Knepley 
3990a6e0b375SMatthew G. Knepley       ierr = DMGetEnclosureRelation(dmA, dmB, &enc);CHKERRQ(ierr);
3991a6e0b375SMatthew G. Knepley       ierr = DMGetEnclosurePoint(dmA, dmB, enc, pB, pA);CHKERRQ(ierr);
3992a6e0b375SMatthew G. Knepley     }
3993a6e0b375SMatthew G. Knepley     break;
399498921bdaSJacob Faibussowitsch     default: SETERRQ(PetscObjectComm((PetscObject) dmA), PETSC_ERR_ARG_OUTOFRANGE, "Invalid enclosure type %d", (int) etype);
399544171101SMatthew G. Knepley   }
399644171101SMatthew G. Knepley   PetscFunctionReturn(0);
399744171101SMatthew G. Knepley }
3998