xref: /petsc/src/dm/impls/plex/plexsubmesh.c (revision d56405f8a9ebdf7165dcacb6972cc93e29841ab0)
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 {
657*d56405f8SMatthew G. Knepley   PetscInt           depth = 0, numLabels, l;
658cd0c2139SMatthew G Knepley   PetscErrorCode     ierr;
659cd0c2139SMatthew G Knepley 
660cd0c2139SMatthew G Knepley   PetscFunctionBegin;
661cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
662cd0c2139SMatthew G Knepley   /* Step 10: Convert labels */
663c58f1c22SToby Isaac   ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
664cd0c2139SMatthew G Knepley   for (l = 0; l < numLabels; ++l) {
665cd0c2139SMatthew G Knepley     DMLabel         label, newlabel;
666cd0c2139SMatthew G Knepley     const char     *lname;
667fa8e8ae5SToby Isaac     PetscBool       isDepth, isDim;
668cd0c2139SMatthew G Knepley     IS              valueIS;
669cd0c2139SMatthew G Knepley     const PetscInt *values;
670cd0c2139SMatthew G Knepley     PetscInt        numValues, val;
671cd0c2139SMatthew G Knepley 
672c58f1c22SToby Isaac     ierr = DMGetLabelName(dm, l, &lname);CHKERRQ(ierr);
673cd0c2139SMatthew G Knepley     ierr = PetscStrcmp(lname, "depth", &isDepth);CHKERRQ(ierr);
674cd0c2139SMatthew G Knepley     if (isDepth) continue;
675fa8e8ae5SToby Isaac     ierr = PetscStrcmp(lname, "dim", &isDim);CHKERRQ(ierr);
676fa8e8ae5SToby Isaac     if (isDim) continue;
677c58f1c22SToby Isaac     ierr = DMCreateLabel(dmNew, lname);CHKERRQ(ierr);
678c58f1c22SToby Isaac     ierr = DMGetLabel(dm, lname, &label);CHKERRQ(ierr);
679c58f1c22SToby Isaac     ierr = DMGetLabel(dmNew, lname, &newlabel);CHKERRQ(ierr);
68064a6a1a4SToby Isaac     ierr = DMLabelGetDefaultValue(label,&val);CHKERRQ(ierr);
68164a6a1a4SToby Isaac     ierr = DMLabelSetDefaultValue(newlabel,val);CHKERRQ(ierr);
682cd0c2139SMatthew G Knepley     ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
683cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(valueIS, &numValues);CHKERRQ(ierr);
684cd0c2139SMatthew G Knepley     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
685cd0c2139SMatthew G Knepley     for (val = 0; val < numValues; ++val) {
686cd0c2139SMatthew G Knepley       IS              pointIS;
687cd0c2139SMatthew G Knepley       const PetscInt *points;
688cd0c2139SMatthew G Knepley       PetscInt        numPoints, p;
689cd0c2139SMatthew G Knepley 
690cd0c2139SMatthew G Knepley       ierr = DMLabelGetStratumIS(label, values[val], &pointIS);CHKERRQ(ierr);
691cd0c2139SMatthew G Knepley       ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
692cd0c2139SMatthew G Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
693cd0c2139SMatthew G Knepley       for (p = 0; p < numPoints; ++p) {
6942582d50cSToby Isaac         const PetscInt newpoint = DMPlexShiftPoint_Internal(points[p], depth, depthShift);
695cd0c2139SMatthew G Knepley 
696cd0c2139SMatthew G Knepley         ierr = DMLabelSetValue(newlabel, newpoint, values[val]);CHKERRQ(ierr);
697cd0c2139SMatthew G Knepley       }
698cd0c2139SMatthew G Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
699cd0c2139SMatthew G Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
700cd0c2139SMatthew G Knepley     }
701cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
702cd0c2139SMatthew G Knepley     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
703cd0c2139SMatthew G Knepley   }
704*d56405f8SMatthew G. Knepley   PetscFunctionReturn(0);
705*d56405f8SMatthew G. Knepley }
706*d56405f8SMatthew G. Knepley 
707*d56405f8SMatthew G. Knepley static PetscErrorCode DMPlexCreateVTKLabel_Internal(DM dm, PetscBool createGhostLabel, DM dmNew)
708*d56405f8SMatthew G. Knepley {
709*d56405f8SMatthew G. Knepley   PetscSF            sfPoint;
710*d56405f8SMatthew G. Knepley   DMLabel            vtkLabel, ghostLabel = NULL;
711*d56405f8SMatthew G. Knepley   const PetscSFNode *leafRemote;
712*d56405f8SMatthew G. Knepley   const PetscInt    *leafLocal;
713*d56405f8SMatthew G. Knepley   PetscInt           cellHeight, cStart, cEnd, c, fStart, fEnd, f, numLeaves, l;
714*d56405f8SMatthew G. Knepley   PetscMPIInt        rank;
715*d56405f8SMatthew G. Knepley   PetscErrorCode     ierr;
716*d56405f8SMatthew G. Knepley 
717*d56405f8SMatthew G. Knepley   PetscFunctionBegin;
718cd0c2139SMatthew G Knepley    /* Step 11: Make label for output (vtk) and to mark ghost points (ghost) */
719ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRMPI(ierr);
720cd0c2139SMatthew G Knepley   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
721*d56405f8SMatthew G. Knepley   ierr = DMPlexGetVTKCellHeight(dmNew, &cellHeight);CHKERRQ(ierr);
722*d56405f8SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
723cd0c2139SMatthew G Knepley   ierr = PetscSFGetGraph(sfPoint, NULL, &numLeaves, &leafLocal, &leafRemote);CHKERRQ(ierr);
724c58f1c22SToby Isaac   ierr = DMCreateLabel(dmNew, "vtk");CHKERRQ(ierr);
725c58f1c22SToby Isaac   ierr = DMGetLabel(dmNew, "vtk", &vtkLabel);CHKERRQ(ierr);
726*d56405f8SMatthew G. Knepley   if (createGhostLabel) {
727*d56405f8SMatthew G. Knepley     ierr = DMCreateLabel(dmNew, "ghost");CHKERRQ(ierr);
728c58f1c22SToby Isaac     ierr = DMGetLabel(dmNew, "ghost", &ghostLabel);CHKERRQ(ierr);
729*d56405f8SMatthew G. Knepley   }
730cd0c2139SMatthew G Knepley   for (l = 0, c = cStart; l < numLeaves && c < cEnd; ++l, ++c) {
731cd0c2139SMatthew G Knepley     for (; c < leafLocal[l] && c < cEnd; ++c) {
732cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(vtkLabel, c, 1);CHKERRQ(ierr);
733cd0c2139SMatthew G Knepley     }
734cd0c2139SMatthew G Knepley     if (leafLocal[l] >= cEnd) break;
735cd0c2139SMatthew G Knepley     if (leafRemote[l].rank == rank) {
736cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(vtkLabel, c, 1);CHKERRQ(ierr);
737*d56405f8SMatthew G. Knepley     } else if (ghostLabel) {
738cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(ghostLabel, c, 2);CHKERRQ(ierr);
739cd0c2139SMatthew G Knepley     }
740cd0c2139SMatthew G Knepley   }
741cd0c2139SMatthew G Knepley   for (; c < cEnd; ++c) {
742cd0c2139SMatthew G Knepley     ierr = DMLabelSetValue(vtkLabel, c, 1);CHKERRQ(ierr);
743cd0c2139SMatthew G Knepley   }
744*d56405f8SMatthew G. Knepley   if (ghostLabel) {
745cd0c2139SMatthew G Knepley     ierr = DMPlexGetHeightStratum(dmNew, 1, &fStart, &fEnd);CHKERRQ(ierr);
746cd0c2139SMatthew G Knepley     for (f = fStart; f < fEnd; ++f) {
747cd0c2139SMatthew G Knepley       PetscInt numCells;
748cd0c2139SMatthew G Knepley 
749cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dmNew, f, &numCells);CHKERRQ(ierr);
750cd0c2139SMatthew G Knepley       if (numCells < 2) {
751cd0c2139SMatthew G Knepley         ierr = DMLabelSetValue(ghostLabel, f, 1);CHKERRQ(ierr);
752cd0c2139SMatthew G Knepley       } else {
753cd0c2139SMatthew G Knepley         const PetscInt *cells = NULL;
754cd0c2139SMatthew G Knepley         PetscInt        vA, vB;
755cd0c2139SMatthew G Knepley 
756cd0c2139SMatthew G Knepley         ierr = DMPlexGetSupport(dmNew, f, &cells);CHKERRQ(ierr);
757cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(vtkLabel, cells[0], &vA);CHKERRQ(ierr);
758cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(vtkLabel, cells[1], &vB);CHKERRQ(ierr);
75990664b96SToby Isaac         if (vA != 1 && vB != 1) {ierr = DMLabelSetValue(ghostLabel, f, 1);CHKERRQ(ierr);}
760cd0c2139SMatthew G Knepley       }
761cd0c2139SMatthew G Knepley     }
762*d56405f8SMatthew G. Knepley   }
763cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
764cd0c2139SMatthew G Knepley }
765cd0c2139SMatthew G Knepley 
766ca04dac2SToby Isaac static PetscErrorCode DMPlexShiftTree_Internal(DM dm, PetscInt depthShift[], DM dmNew)
767ca04dac2SToby Isaac {
768ca04dac2SToby Isaac   DM             refTree;
769ca04dac2SToby Isaac   PetscSection   pSec;
770ca04dac2SToby Isaac   PetscInt       *parents, *childIDs;
771ca04dac2SToby Isaac   PetscErrorCode ierr;
772ca04dac2SToby Isaac 
773ca04dac2SToby Isaac   PetscFunctionBegin;
774ca04dac2SToby Isaac   ierr = DMPlexGetReferenceTree(dm,&refTree);CHKERRQ(ierr);
775ca04dac2SToby Isaac   ierr = DMPlexSetReferenceTree(dmNew,refTree);CHKERRQ(ierr);
776ca04dac2SToby Isaac   ierr = DMPlexGetTree(dm,&pSec,&parents,&childIDs,NULL,NULL);CHKERRQ(ierr);
777ca04dac2SToby Isaac   if (pSec) {
7782582d50cSToby Isaac     PetscInt p, pStart, pEnd, *parentsShifted, pStartShifted, pEndShifted, depth;
779fb4630b5SToby Isaac     PetscInt *childIDsShifted;
780ca04dac2SToby Isaac     PetscSection pSecShifted;
781ca04dac2SToby Isaac 
782ca04dac2SToby Isaac     ierr = PetscSectionGetChart(pSec,&pStart,&pEnd);CHKERRQ(ierr);
783ca04dac2SToby Isaac     ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
7842582d50cSToby Isaac     pStartShifted = DMPlexShiftPoint_Internal(pStart,depth,depthShift);
7852582d50cSToby Isaac     pEndShifted   = DMPlexShiftPoint_Internal(pEnd,depth,depthShift);
786fb4630b5SToby Isaac     ierr = PetscMalloc2(pEndShifted - pStartShifted,&parentsShifted,pEndShifted-pStartShifted,&childIDsShifted);CHKERRQ(ierr);
787ca04dac2SToby Isaac     ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dmNew),&pSecShifted);CHKERRQ(ierr);
788ca04dac2SToby Isaac     ierr = PetscSectionSetChart(pSecShifted,pStartShifted,pEndShifted);CHKERRQ(ierr);
789ca04dac2SToby Isaac     for (p = pStartShifted; p < pEndShifted; p++) {
790fb4630b5SToby Isaac       /* start off assuming no children */
791fb4630b5SToby Isaac       ierr = PetscSectionSetDof(pSecShifted,p,0);CHKERRQ(ierr);
792fb4630b5SToby Isaac     }
793fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
794fb4630b5SToby Isaac       PetscInt dof;
795fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p,depth,depthShift);
796ca04dac2SToby Isaac 
797ca04dac2SToby Isaac       ierr = PetscSectionGetDof(pSec,p,&dof);CHKERRQ(ierr);
798fb4630b5SToby Isaac       ierr = PetscSectionSetDof(pSecShifted,pNew,dof);CHKERRQ(ierr);
799ca04dac2SToby Isaac     }
800ca04dac2SToby Isaac     ierr = PetscSectionSetUp(pSecShifted);CHKERRQ(ierr);
801fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
802fb4630b5SToby Isaac       PetscInt dof;
803fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p,depth,depthShift);
804fb4630b5SToby Isaac 
805fb4630b5SToby Isaac       ierr = PetscSectionGetDof(pSec,p,&dof);CHKERRQ(ierr);
806fb4630b5SToby Isaac       if (dof) {
807fb4630b5SToby Isaac         PetscInt off, offNew;
808fb4630b5SToby Isaac 
809fb4630b5SToby Isaac         ierr = PetscSectionGetOffset(pSec,p,&off);CHKERRQ(ierr);
810fb4630b5SToby Isaac         ierr = PetscSectionGetOffset(pSecShifted,pNew,&offNew);CHKERRQ(ierr);
811fb4630b5SToby Isaac         parentsShifted[offNew] = DMPlexShiftPoint_Internal(parents[off],depth,depthShift);
812fb4630b5SToby Isaac         childIDsShifted[offNew] = childIDs[off];
813fb4630b5SToby Isaac       }
814fb4630b5SToby Isaac     }
815fb4630b5SToby Isaac     ierr = DMPlexSetTree(dmNew,pSecShifted,parentsShifted,childIDsShifted);CHKERRQ(ierr);
816fb4630b5SToby Isaac     ierr = PetscFree2(parentsShifted,childIDsShifted);CHKERRQ(ierr);
817e6885bbbSToby Isaac     ierr = PetscSectionDestroy(&pSecShifted);CHKERRQ(ierr);
818ca04dac2SToby Isaac   }
819ca04dac2SToby Isaac   PetscFunctionReturn(0);
820ca04dac2SToby Isaac }
821ca04dac2SToby Isaac 
822cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexConstructGhostCells_Internal(DM dm, DMLabel label, PetscInt *numGhostCells, DM gdm)
823cd0c2139SMatthew G Knepley {
824da97024aSMatthew G. Knepley   PetscSF               sf;
825cd0c2139SMatthew G Knepley   IS                    valueIS;
826da97024aSMatthew G. Knepley   const PetscInt       *values, *leaves;
827cd0c2139SMatthew G Knepley   PetscInt             *depthShift;
8282582d50cSToby Isaac   PetscInt              d, depth = 0, nleaves, loc, Ng, numFS, fs, fStart, fEnd, ghostCell, cEnd, c;
82990b157c4SStefano Zampini   PetscBool             isper;
83090b157c4SStefano Zampini   const PetscReal      *maxCell, *L;
83190b157c4SStefano Zampini   const DMBoundaryType *bd;
832cd0c2139SMatthew G Knepley   PetscErrorCode        ierr;
833cd0c2139SMatthew G Knepley 
834cd0c2139SMatthew G Knepley   PetscFunctionBegin;
835da97024aSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
836da97024aSMatthew G. Knepley   ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
837da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
83846c796b9SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
839cd0c2139SMatthew G Knepley   /* Count ghost cells */
840cd0c2139SMatthew G Knepley   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
841cd0c2139SMatthew G Knepley   ierr = ISGetLocalSize(valueIS, &numFS);CHKERRQ(ierr);
842cd0c2139SMatthew G Knepley   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
8434a6cfa73SMatthew G. Knepley   Ng   = 0;
844cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
84546c796b9SMatthew G. Knepley     IS              faceIS;
84646c796b9SMatthew G. Knepley     const PetscInt *faces;
84746c796b9SMatthew G. Knepley     PetscInt        numFaces, f, numBdFaces = 0;
848cd0c2139SMatthew G Knepley 
84946c796b9SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, values[fs], &faceIS);CHKERRQ(ierr);
85046c796b9SMatthew G. Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
85146c796b9SMatthew G. Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
85246c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
853ca04dac2SToby Isaac       PetscInt numChildren;
854ca04dac2SToby Isaac 
855da97024aSMatthew G. Knepley       ierr = PetscFindInt(faces[f], nleaves, leaves, &loc);CHKERRQ(ierr);
856ca04dac2SToby Isaac       ierr = DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL);CHKERRQ(ierr);
857ca04dac2SToby Isaac       /* non-local and ancestors points don't get to register ghosts */
858ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
85946c796b9SMatthew G. Knepley       if ((faces[f] >= fStart) && (faces[f] < fEnd)) ++numBdFaces;
86046c796b9SMatthew G. Knepley     }
8614a6cfa73SMatthew G. Knepley     Ng += numBdFaces;
862ba2698f1SMatthew G. Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
86346c796b9SMatthew G. Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
864cd0c2139SMatthew G Knepley   }
865cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
8662582d50cSToby Isaac   ierr = PetscMalloc1(2*(depth+1), &depthShift);CHKERRQ(ierr);
8672582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
86859eef20bSToby Isaac     PetscInt dEnd;
8692582d50cSToby Isaac 
8700974a383SToby Isaac     ierr = DMPlexGetDepthStratum(dm,d,NULL,&dEnd);CHKERRQ(ierr);
87159eef20bSToby Isaac     depthShift[2*d]   = dEnd;
8722582d50cSToby Isaac     depthShift[2*d+1] = 0;
8732582d50cSToby Isaac   }
8742582d50cSToby Isaac   if (depth >= 0) depthShift[2*depth+1] = Ng;
8752582d50cSToby Isaac   ierr = DMPlexShiftPointSetUp_Internal(depth,depthShift);CHKERRQ(ierr);
876cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSizes_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
877cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
878cd0c2139SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
8794a6cfa73SMatthew G. Knepley   for (c = cEnd; c < cEnd + Ng; ++c) {
880cd0c2139SMatthew G Knepley     ierr = DMPlexSetConeSize(gdm, c, 1);CHKERRQ(ierr);
881cd0c2139SMatthew G Knepley   }
882cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
883cd0c2139SMatthew G Knepley     IS              faceIS;
884cd0c2139SMatthew G Knepley     const PetscInt *faces;
885cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
886cd0c2139SMatthew G Knepley 
887cd0c2139SMatthew G Knepley     ierr = DMLabelGetStratumIS(label, values[fs], &faceIS);CHKERRQ(ierr);
888cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
889cd0c2139SMatthew G Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
890cd0c2139SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
891ca04dac2SToby Isaac       PetscInt size, numChildren;
892cd0c2139SMatthew G Knepley 
893da97024aSMatthew G. Knepley       ierr = PetscFindInt(faces[f], nleaves, leaves, &loc);CHKERRQ(ierr);
894ca04dac2SToby Isaac       ierr = DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL);CHKERRQ(ierr);
895ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
89646c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
897cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, faces[f], &size);CHKERRQ(ierr);
8982c71b3e2SJacob Faibussowitsch       PetscCheckFalse(size != 1,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM has boundary face %d with %d support cells", faces[f], size);
8994a6cfa73SMatthew G. Knepley       ierr = DMPlexSetSupportSize(gdm, faces[f] + Ng, 2);CHKERRQ(ierr);
900cd0c2139SMatthew G Knepley     }
901cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
902cd0c2139SMatthew G Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
903cd0c2139SMatthew G Knepley   }
904cd0c2139SMatthew G Knepley   /* Step 4: Setup ghosted DM */
905cd0c2139SMatthew G Knepley   ierr = DMSetUp(gdm);CHKERRQ(ierr);
906cd0c2139SMatthew G Knepley   ierr = DMPlexShiftPoints_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
907cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
908cd0c2139SMatthew G Knepley   ghostCell = cEnd;
909cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
910cd0c2139SMatthew G Knepley     IS              faceIS;
911cd0c2139SMatthew G Knepley     const PetscInt *faces;
912cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
913cd0c2139SMatthew G Knepley 
914cd0c2139SMatthew G Knepley     ierr = DMLabelGetStratumIS(label, values[fs], &faceIS);CHKERRQ(ierr);
915cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
916cd0c2139SMatthew G Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
91746c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
918ca04dac2SToby Isaac       PetscInt newFace = faces[f] + Ng, numChildren;
919cd0c2139SMatthew G Knepley 
920da97024aSMatthew G. Knepley       ierr = PetscFindInt(faces[f], nleaves, leaves, &loc);CHKERRQ(ierr);
921ca04dac2SToby Isaac       ierr = DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL);CHKERRQ(ierr);
922ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
92346c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
924cd0c2139SMatthew G Knepley       ierr = DMPlexSetCone(gdm, ghostCell, &newFace);CHKERRQ(ierr);
925cd0c2139SMatthew G Knepley       ierr = DMPlexInsertSupport(gdm, newFace, 1, ghostCell);CHKERRQ(ierr);
92646c796b9SMatthew G. Knepley       ++ghostCell;
927cd0c2139SMatthew G Knepley     }
928cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
929cd0c2139SMatthew G Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
930cd0c2139SMatthew G Knepley   }
931cd0c2139SMatthew G Knepley   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
932cd0c2139SMatthew G Knepley   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
933cd0c2139SMatthew G Knepley   ierr = DMPlexShiftCoordinates_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
934cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSF_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
935cd0c2139SMatthew G Knepley   ierr = DMPlexShiftLabels_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
936*d56405f8SMatthew G. Knepley   ierr = DMPlexCreateVTKLabel_Internal(dm, PETSC_TRUE, gdm);CHKERRQ(ierr);
937ca04dac2SToby Isaac   ierr = DMPlexShiftTree_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
938cd0c2139SMatthew G Knepley   ierr = PetscFree(depthShift);CHKERRQ(ierr);
939412e9a14SMatthew G. Knepley   for (c = cEnd; c < cEnd + Ng; ++c) {
940412e9a14SMatthew G. Knepley     ierr = DMPlexSetCellType(gdm, c, DM_POLYTOPE_FV_GHOST);CHKERRQ(ierr);
941412e9a14SMatthew G. Knepley   }
942966c7b3fSMatthew G. Knepley   /* Step 7: Periodicity */
94390b157c4SStefano Zampini   ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
94490b157c4SStefano Zampini   ierr = DMSetPeriodicity(gdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
9454a6cfa73SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
946cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
947cd0c2139SMatthew G Knepley }
948cd0c2139SMatthew G Knepley 
949cd0c2139SMatthew G Knepley /*@C
950cd0c2139SMatthew G Knepley   DMPlexConstructGhostCells - Construct ghost cells which connect to every boundary face
951cd0c2139SMatthew G Knepley 
952cd0c2139SMatthew G Knepley   Collective on dm
953cd0c2139SMatthew G Knepley 
954cd0c2139SMatthew G Knepley   Input Parameters:
955cd0c2139SMatthew G Knepley + dm - The original DM
956cd0c2139SMatthew G Knepley - labelName - The label specifying the boundary faces, or "Face Sets" if this is NULL
957cd0c2139SMatthew G Knepley 
958cd0c2139SMatthew G Knepley   Output Parameters:
959cd0c2139SMatthew G Knepley + numGhostCells - The number of ghost cells added to the DM
960cd0c2139SMatthew G Knepley - dmGhosted - The new DM
961cd0c2139SMatthew G Knepley 
962cd0c2139SMatthew G Knepley   Note: If no label exists of that name, one will be created marking all boundary faces
963cd0c2139SMatthew G Knepley 
964cd0c2139SMatthew G Knepley   Level: developer
965cd0c2139SMatthew G Knepley 
966cd0c2139SMatthew G Knepley .seealso: DMCreate()
96731266bc0SMatthew G. Knepley @*/
968cd0c2139SMatthew G Knepley PetscErrorCode DMPlexConstructGhostCells(DM dm, const char labelName[], PetscInt *numGhostCells, DM *dmGhosted)
969cd0c2139SMatthew G Knepley {
970cd0c2139SMatthew G Knepley   DM             gdm;
971cd0c2139SMatthew G Knepley   DMLabel        label;
972cd0c2139SMatthew G Knepley   const char    *name = labelName ? labelName : "Face Sets";
973412e9a14SMatthew G. Knepley   PetscInt       dim, Ng = 0;
974b0441da4SMatthew G. Knepley   PetscBool      useCone, useClosure;
975cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
976cd0c2139SMatthew G Knepley 
977cd0c2139SMatthew G Knepley   PetscFunctionBegin;
978cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9794a6cfa73SMatthew G. Knepley   if (numGhostCells) PetscValidPointer(numGhostCells, 3);
980cd0c2139SMatthew G Knepley   PetscValidPointer(dmGhosted, 4);
981cd0c2139SMatthew G Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), &gdm);CHKERRQ(ierr);
982cd0c2139SMatthew G Knepley   ierr = DMSetType(gdm, DMPLEX);CHKERRQ(ierr);
983c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
984c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(gdm, dim);CHKERRQ(ierr);
985b0441da4SMatthew G. Knepley   ierr = DMGetBasicAdjacency(dm, &useCone, &useClosure);CHKERRQ(ierr);
986b0441da4SMatthew G. Knepley   ierr = DMSetBasicAdjacency(gdm, useCone,  useClosure);CHKERRQ(ierr);
987c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
988cd0c2139SMatthew G Knepley   if (!label) {
989cd0c2139SMatthew G Knepley     /* Get label for boundary faces */
990c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
991c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
992e752be1aSMatthew G. Knepley     ierr = DMPlexMarkBoundaryFaces(dm, 1, label);CHKERRQ(ierr);
993cd0c2139SMatthew G Knepley   }
994d80ece95SMatthew G. Knepley   ierr = DMPlexConstructGhostCells_Internal(dm, label, &Ng, gdm);CHKERRQ(ierr);
995b0441da4SMatthew G. Knepley   ierr = DMCopyDisc(dm, gdm);CHKERRQ(ierr);
996e600fa54SMatthew G. Knepley   ierr = DMPlexCopy_Internal(dm, PETSC_TRUE, gdm);CHKERRQ(ierr);
997cad26855SMatthew G. Knepley   gdm->setfromoptionscalled = dm->setfromoptionscalled;
998d80ece95SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
999cd0c2139SMatthew G Knepley   *dmGhosted = gdm;
1000cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1001cd0c2139SMatthew G Knepley }
1002cd0c2139SMatthew G Knepley 
1003607ab7a9SMatthew G. Knepley /*
1004faedd622SMatthew G. Knepley   We are adding three kinds of points here:
1005607ab7a9SMatthew G. Knepley     Replicated:     Copies of points which exist in the mesh, such as vertices identified across a fault
1006faedd622SMatthew G. Knepley     Non-replicated: Points which exist on the fault, but are not replicated
1007607ab7a9SMatthew G. Knepley     Hybrid:         Entirely new points, such as cohesive cells
1008a6ae58d1SMatthew G. Knepley 
1009a6ae58d1SMatthew G. Knepley   When creating subsequent cohesive cells, we shift the old hybrid cells to the end of the numbering at
1010a6ae58d1SMatthew G. Knepley   each depth so that the new split/hybrid points can be inserted as a block.
1011607ab7a9SMatthew G. Knepley */
10127db7e0a7SMatthew G. Knepley static PetscErrorCode DMPlexConstructCohesiveCells_Internal(DM dm, DMLabel label, DMLabel splitLabel, DM sdm)
1013cd0c2139SMatthew G Knepley {
1014cd0c2139SMatthew G Knepley   MPI_Comm         comm;
1015607ab7a9SMatthew G. Knepley   IS               valueIS;
1016607ab7a9SMatthew G. Knepley   PetscInt         numSP = 0;          /* The number of depths for which we have replicated points */
1017607ab7a9SMatthew G. Knepley   const PetscInt  *values;             /* List of depths for which we have replicated points */
101818c5995bSMatthew G. Knepley   IS              *splitIS;
101918c5995bSMatthew G. Knepley   IS              *unsplitIS;
1020607ab7a9SMatthew G. Knepley   PetscInt        *numSplitPoints;     /* The number of replicated points at each depth */
102118c5995bSMatthew G. Knepley   PetscInt        *numUnsplitPoints;   /* The number of non-replicated points at each depth which still give rise to hybrid points */
102236dbac82SMatthew G. Knepley   PetscInt        *numHybridPoints;    /* The number of new hybrid points at each depth */
102336dbac82SMatthew G. Knepley   PetscInt        *numHybridPointsOld; /* The number of existing hybrid points at each depth */
1024607ab7a9SMatthew G. Knepley   const PetscInt **splitPoints;        /* Replicated points for each depth */
102518c5995bSMatthew G. Knepley   const PetscInt **unsplitPoints;      /* Non-replicated points for each depth */
1026cd0c2139SMatthew G Knepley   PetscSection     coordSection;
1027cd0c2139SMatthew G Knepley   Vec              coordinates;
1028cd0c2139SMatthew G Knepley   PetscScalar     *coords;
1029a6ae58d1SMatthew G. Knepley   PetscInt        *depthMax;           /* The first hybrid point at each depth in the original mesh */
1030a6ae58d1SMatthew G. Knepley   PetscInt        *depthEnd;           /* The point limit at each depth in the original mesh */
1031607ab7a9SMatthew G. Knepley   PetscInt        *depthShift;         /* Number of replicated+hybrid points at each depth */
1032607ab7a9SMatthew G. Knepley   PetscInt        *pMaxNew;            /* The first replicated point at each depth in the new mesh, hybrids come after this */
1033607ab7a9SMatthew G. Knepley   PetscInt        *coneNew, *coneONew, *supportNew;
103418c5995bSMatthew G. Knepley   PetscInt         shift = 100, shift2 = 200, depth = 0, dep, dim, d, sp, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, numLabels, vStart, vEnd, pEnd, p, v;
1035cd0c2139SMatthew G Knepley   PetscErrorCode   ierr;
1036cd0c2139SMatthew G Knepley 
1037cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1038cd0c2139SMatthew G Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
1039c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1040607ab7a9SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1041fd4b9f15SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1042412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
1043412e9a14SMatthew G. Knepley   ierr = DMCreateLabel(sdm, "celltype");CHKERRQ(ierr);
1044cd0c2139SMatthew G Knepley   /* Count split points and add cohesive cells */
1045607ab7a9SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
10462582d50cSToby Isaac   ierr = PetscMalloc5(depth+1,&depthMax,depth+1,&depthEnd,2*(depth+1),&depthShift,depth+1,&pMaxNew,depth+1,&numHybridPointsOld);CHKERRQ(ierr);
1047dcca6d9dSJed 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);
1048607ab7a9SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
1049607ab7a9SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, d, NULL, &pMaxNew[d]);CHKERRQ(ierr);
1050412e9a14SMatthew G. Knepley     ierr = DMPlexGetTensorPrismBounds_Internal(dm, d, &depthMax[d], NULL);CHKERRQ(ierr);
1051a6ae58d1SMatthew G. Knepley     depthEnd[d]           = pMaxNew[d];
1052a6ae58d1SMatthew G. Knepley     depthMax[d]           = depthMax[d] < 0 ? depthEnd[d] : depthMax[d];
1053607ab7a9SMatthew G. Knepley     numSplitPoints[d]     = 0;
105418c5995bSMatthew G. Knepley     numUnsplitPoints[d]   = 0;
1055607ab7a9SMatthew G. Knepley     numHybridPoints[d]    = 0;
1056a6ae58d1SMatthew G. Knepley     numHybridPointsOld[d] = depthMax[d] < 0 ? 0 : depthEnd[d] - depthMax[d];
1057607ab7a9SMatthew G. Knepley     splitPoints[d]        = NULL;
105818c5995bSMatthew G. Knepley     unsplitPoints[d]      = NULL;
105918c5995bSMatthew G. Knepley     splitIS[d]            = NULL;
106018c5995bSMatthew G. Knepley     unsplitIS[d]          = NULL;
106159eef20bSToby Isaac     /* we are shifting the existing hybrid points with the stratum behind them, so
106259eef20bSToby Isaac      * the split comes at the end of the normal points, i.e., at depthMax[d] */
106359eef20bSToby Isaac     depthShift[2*d]       = depthMax[d];
106459eef20bSToby Isaac     depthShift[2*d+1]     = 0;
1065607ab7a9SMatthew G. Knepley   }
1066cd0c2139SMatthew G Knepley   if (label) {
1067cd0c2139SMatthew G Knepley     ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
1068cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(valueIS, &numSP);CHKERRQ(ierr);
1069cd0c2139SMatthew G Knepley     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
1070cd0c2139SMatthew G Knepley   }
1071cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1072cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1073cd0c2139SMatthew G Knepley 
1074cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
107518c5995bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, dep, &splitIS[dep]);CHKERRQ(ierr);
107618c5995bSMatthew G. Knepley     if (splitIS[dep]) {
107718c5995bSMatthew G. Knepley       ierr = ISGetLocalSize(splitIS[dep], &numSplitPoints[dep]);CHKERRQ(ierr);
107818c5995bSMatthew G. Knepley       ierr = ISGetIndices(splitIS[dep], &splitPoints[dep]);CHKERRQ(ierr);
107918c5995bSMatthew G. Knepley     }
108018c5995bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, shift2+dep, &unsplitIS[dep]);CHKERRQ(ierr);
108118c5995bSMatthew G. Knepley     if (unsplitIS[dep]) {
108218c5995bSMatthew G. Knepley       ierr = ISGetLocalSize(unsplitIS[dep], &numUnsplitPoints[dep]);CHKERRQ(ierr);
108318c5995bSMatthew G. Knepley       ierr = ISGetIndices(unsplitIS[dep], &unsplitPoints[dep]);CHKERRQ(ierr);
1084cd0c2139SMatthew G Knepley     }
1085cd0c2139SMatthew G Knepley   }
1086607ab7a9SMatthew G. Knepley   /* Calculate number of hybrid points */
108718c5995bSMatthew 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   */
10882582d50cSToby Isaac   for (d = 0; d <= depth; ++d) depthShift[2*d+1]      = numSplitPoints[d] + numHybridPoints[d];
10893d3eaba7SBarry Smith   ierr = DMPlexShiftPointSetUp_Internal(depth,depthShift);CHKERRQ(ierr);
109059eef20bSToby Isaac   /* the end of the points in this stratum that come before the new points:
109159eef20bSToby Isaac    * shifting pMaxNew[d] gets the new start of the next stratum, then count back the old hybrid points and the newly
109259eef20bSToby Isaac    * added points */
10932582d50cSToby Isaac   for (d = 0; d <= depth; ++d) pMaxNew[d]             = DMPlexShiftPoint_Internal(pMaxNew[d],depth,depthShift) - (numHybridPointsOld[d] + numSplitPoints[d] + numHybridPoints[d]);
1094cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSizes_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
1095cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
1096cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1097cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1098cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
10992582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
11004c367dbcSMatthew G. Knepley       const PetscInt  splitp = p    + pMaxNew[dep];
1101cd0c2139SMatthew G Knepley       const PetscInt *support;
1102394c2f0fSMatthew G. Knepley       DMPolytopeType  ct;
11034c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, qn, qp, e;
1104cd0c2139SMatthew G Knepley 
1105cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
1106cd0c2139SMatthew G Knepley       ierr = DMPlexSetConeSize(sdm, splitp, coneSize);CHKERRQ(ierr);
1107cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
1108cd0c2139SMatthew G Knepley       ierr = DMPlexSetSupportSize(sdm, splitp, supportSize);CHKERRQ(ierr);
1109394c2f0fSMatthew G. Knepley       ierr = DMPlexGetCellType(dm, oldp, &ct);CHKERRQ(ierr);
1110394c2f0fSMatthew G. Knepley       ierr = DMPlexSetCellType(sdm, splitp, ct);CHKERRQ(ierr);
1111cd0c2139SMatthew G Knepley       if (dep == depth-1) {
11124c367dbcSMatthew G. Knepley         const PetscInt hybcell = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
11134c367dbcSMatthew G. Knepley 
1114cd0c2139SMatthew G Knepley         /* Add cohesive cells, they are prisms */
11154c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybcell, 2 + coneSize);CHKERRQ(ierr);
1116412e9a14SMatthew G. Knepley         switch (coneSize) {
1117412e9a14SMatthew G. Knepley           case 2: ierr = DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_SEG_PRISM_TENSOR);CHKERRQ(ierr);break;
1118412e9a14SMatthew G. Knepley           case 3: ierr = DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_TRI_PRISM_TENSOR);CHKERRQ(ierr);break;
1119412e9a14SMatthew G. Knepley           case 4: ierr = DMPlexSetCellType(sdm, hybcell, DM_POLYTOPE_QUAD_PRISM_TENSOR);CHKERRQ(ierr);break;
1120412e9a14SMatthew G. Knepley         }
1121cd0c2139SMatthew G Knepley       } else if (dep == 0) {
11224c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1123cd0c2139SMatthew G Knepley 
1124cd0c2139SMatthew G Knepley         ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
11254c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1126cd0c2139SMatthew G Knepley           PetscInt val;
1127cd0c2139SMatthew G Knepley 
1128cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
11294c367dbcSMatthew G. Knepley           if (val == 1) ++qf;
11304c367dbcSMatthew G. Knepley           if ((val == 1) || (val ==  (shift + 1))) ++qn;
11314c367dbcSMatthew G. Knepley           if ((val == 1) || (val == -(shift + 1))) ++qp;
1132cd0c2139SMatthew G Knepley         }
11334c367dbcSMatthew G. Knepley         /* Split old vertex: Edges into original vertex and new cohesive edge */
11344c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qn+1);CHKERRQ(ierr);
11354c367dbcSMatthew G. Knepley         /* Split new vertex: Edges into split vertex and new cohesive edge */
11364c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, splitp, qp+1);CHKERRQ(ierr);
11374c367dbcSMatthew G. Knepley         /* Add hybrid edge */
11384c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybedge, 2);CHKERRQ(ierr);
11394c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybedge, qf);CHKERRQ(ierr);
1140412e9a14SMatthew G. Knepley         ierr = DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR);CHKERRQ(ierr);
1141cd0c2139SMatthew G Knepley       } else if (dep == dim-2) {
11424c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
11434c367dbcSMatthew G. Knepley 
1144cd0c2139SMatthew G Knepley         ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
11454c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1146cd0c2139SMatthew G Knepley           PetscInt val;
1147cd0c2139SMatthew G Knepley 
1148cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
11494c367dbcSMatthew G. Knepley           if (val == dim-1) ++qf;
11504c367dbcSMatthew G. Knepley           if ((val == dim-1) || (val ==  (shift + dim-1))) ++qn;
11514c367dbcSMatthew G. Knepley           if ((val == dim-1) || (val == -(shift + dim-1))) ++qp;
1152cd0c2139SMatthew G Knepley         }
11534c367dbcSMatthew G. Knepley         /* Split old edge: Faces into original edge and cohesive face (positive side?) */
11544c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qn+1);CHKERRQ(ierr);
11554c367dbcSMatthew G. Knepley         /* Split new edge: Faces into split edge and cohesive face (negative side?) */
11564c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, splitp, qp+1);CHKERRQ(ierr);
11574c367dbcSMatthew G. Knepley         /* Add hybrid face */
11584c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybface, 4);CHKERRQ(ierr);
11594c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybface, qf);CHKERRQ(ierr);
1160412e9a14SMatthew G. Knepley         ierr = DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR);CHKERRQ(ierr);
1161cd0c2139SMatthew G Knepley       }
1162cd0c2139SMatthew G Knepley     }
1163cd0c2139SMatthew G Knepley   }
116418c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
116518c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
116618c5995bSMatthew G. Knepley       const PetscInt  oldp   = unsplitPoints[dep][p];
11672582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
116818c5995bSMatthew G. Knepley       const PetscInt *support;
1169da1dd7e4SMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, e, s;
117018c5995bSMatthew G. Knepley 
117118c5995bSMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
117218c5995bSMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
117339254ff6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
117418c5995bSMatthew G. Knepley       if (dep == 0) {
117518c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
117618c5995bSMatthew G. Knepley 
117739254ff6SMatthew G. Knepley         /* Unsplit vertex: Edges into original vertex, split edges, and new cohesive edge twice */
117839254ff6SMatthew G. Knepley         for (s = 0, qf = 0; s < supportSize; ++s, ++qf) {
117939254ff6SMatthew G. Knepley           ierr = PetscFindInt(support[s], numSplitPoints[dep+1], splitPoints[dep+1], &e);CHKERRQ(ierr);
118039254ff6SMatthew G. Knepley           if (e >= 0) ++qf;
118139254ff6SMatthew G. Knepley         }
118239254ff6SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qf+2);CHKERRQ(ierr);
118318c5995bSMatthew G. Knepley         /* Add hybrid edge */
118418c5995bSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybedge, 2);CHKERRQ(ierr);
1185e1757548SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1186e1757548SMatthew G. Knepley           PetscInt val;
1187e1757548SMatthew G. Knepley 
1188e1757548SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1189e1757548SMatthew G. Knepley           /* Split and unsplit edges produce hybrid faces */
1190da1dd7e4SMatthew G. Knepley           if (val == 1) ++qf;
1191da1dd7e4SMatthew G. Knepley           if (val == (shift2 + 1)) ++qf;
1192e1757548SMatthew G. Knepley         }
1193e1757548SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybedge, qf);CHKERRQ(ierr);
1194412e9a14SMatthew G. Knepley         ierr = DMPlexSetCellType(sdm, hybedge, DM_POLYTOPE_POINT_PRISM_TENSOR);CHKERRQ(ierr);
119518c5995bSMatthew G. Knepley       } else if (dep == dim-2) {
119618c5995bSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
1197cd0c2139SMatthew G Knepley         PetscInt       val;
1198cd0c2139SMatthew G Knepley 
1199da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1200cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1201da1dd7e4SMatthew G. Knepley           if (val == dim-1) qf += 2;
1202da1dd7e4SMatthew G. Knepley           else              ++qf;
1203cd0c2139SMatthew G Knepley         }
120418c5995bSMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and cohesive face twice */
1205da1dd7e4SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qf+2);CHKERRQ(ierr);
120618c5995bSMatthew G. Knepley         /* Add hybrid face */
1207da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1208da1dd7e4SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1209da1dd7e4SMatthew G. Knepley           if (val == dim-1) ++qf;
1210da1dd7e4SMatthew G. Knepley         }
121118c5995bSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybface, 4);CHKERRQ(ierr);
1212da1dd7e4SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybface, qf);CHKERRQ(ierr);
1213412e9a14SMatthew G. Knepley         ierr = DMPlexSetCellType(sdm, hybface, DM_POLYTOPE_SEG_PRISM_TENSOR);CHKERRQ(ierr);
1214cd0c2139SMatthew G Knepley       }
1215cd0c2139SMatthew G Knepley     }
1216cd0c2139SMatthew G Knepley   }
1217cd0c2139SMatthew G Knepley   /* Step 4: Setup split DM */
1218cd0c2139SMatthew G Knepley   ierr = DMSetUp(sdm);CHKERRQ(ierr);
1219cd0c2139SMatthew G Knepley   ierr = DMPlexShiftPoints_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
1220dcbb62e8SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(sdm, &maxConeSizeNew, &maxSupportSizeNew);CHKERRQ(ierr);
1221dcca6d9dSJed Brown   ierr = PetscMalloc3(PetscMax(maxConeSize, maxConeSizeNew)*3,&coneNew,PetscMax(maxConeSize, maxConeSizeNew)*3,&coneONew,PetscMax(maxSupportSize, maxSupportSizeNew),&supportNew);CHKERRQ(ierr);
1222cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
1223cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1224cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1225cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
12262582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
12274c367dbcSMatthew G. Knepley       const PetscInt  splitp = p    + pMaxNew[dep];
1228cd0c2139SMatthew G Knepley       const PetscInt *cone, *support, *ornt;
1229b5a892a1SMatthew G. Knepley       DMPolytopeType  ct;
12304c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, q, qf, qn, qp, v, e, s;
1231cd0c2139SMatthew G Knepley 
1232b5a892a1SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, oldp, &ct);CHKERRQ(ierr);
1233cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
1234cd0c2139SMatthew G Knepley       ierr = DMPlexGetCone(dm, oldp, &cone);CHKERRQ(ierr);
1235cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeOrientation(dm, oldp, &ornt);CHKERRQ(ierr);
1236cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
1237cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
1238cd0c2139SMatthew G Knepley       if (dep == depth-1) {
123996a07cd0SMatthew G. Knepley         PetscBool       hasUnsplit = PETSC_FALSE;
12404c367dbcSMatthew G. Knepley         const PetscInt  hybcell    = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1241cd0c2139SMatthew G Knepley         const PetscInt *supportF;
1242cd0c2139SMatthew G Knepley 
1243cd0c2139SMatthew G Knepley         /* Split face:       copy in old face to new face to start */
1244cd0c2139SMatthew G Knepley         ierr = DMPlexGetSupport(sdm, newp,  &supportF);CHKERRQ(ierr);
1245cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, splitp, supportF);CHKERRQ(ierr);
1246cd0c2139SMatthew G Knepley         /* Split old face:   old vertices/edges in cone so no change */
1247cd0c2139SMatthew G Knepley         /* Split new face:   new vertices/edges in cone */
1248cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
12494c367dbcSMatthew G. Knepley           ierr = PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);CHKERRQ(ierr);
125018c5995bSMatthew G. Knepley           if (v < 0) {
125118c5995bSMatthew G. Knepley             ierr = PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
12522c71b3e2SJacob 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);
12532582d50cSToby Isaac             coneNew[2+q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
125496a07cd0SMatthew G. Knepley             hasUnsplit   = PETSC_TRUE;
125518c5995bSMatthew G. Knepley           } else {
12564c367dbcSMatthew G. Knepley             coneNew[2+q] = v + pMaxNew[dep-1];
1257163235baSMatthew G. Knepley             if (dep > 1) {
1258163235baSMatthew G. Knepley               const PetscInt *econe;
1259163235baSMatthew G. Knepley               PetscInt        econeSize, r, vs, vu;
1260163235baSMatthew G. Knepley 
1261163235baSMatthew G. Knepley               ierr = DMPlexGetConeSize(dm, cone[q], &econeSize);CHKERRQ(ierr);
1262163235baSMatthew G. Knepley               ierr = DMPlexGetCone(dm, cone[q], &econe);CHKERRQ(ierr);
1263163235baSMatthew G. Knepley               for (r = 0; r < econeSize; ++r) {
1264163235baSMatthew G. Knepley                 ierr = PetscFindInt(econe[r], numSplitPoints[dep-2],   splitPoints[dep-2],   &vs);CHKERRQ(ierr);
1265163235baSMatthew G. Knepley                 ierr = PetscFindInt(econe[r], numUnsplitPoints[dep-2], unsplitPoints[dep-2], &vu);CHKERRQ(ierr);
1266163235baSMatthew G. Knepley                 if (vs >= 0) continue;
12672c71b3e2SJacob 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);
1268163235baSMatthew G. Knepley                 hasUnsplit   = PETSC_TRUE;
1269163235baSMatthew G. Knepley               }
1270163235baSMatthew G. Knepley             }
1271cd0c2139SMatthew G Knepley           }
1272cd0c2139SMatthew G Knepley         }
1273cd0c2139SMatthew G Knepley         ierr = DMPlexSetCone(sdm, splitp, &coneNew[2]);CHKERRQ(ierr);
1274cd0c2139SMatthew G Knepley         ierr = DMPlexSetConeOrientation(sdm, splitp, ornt);CHKERRQ(ierr);
1275e537020bSMatthew G. Knepley         /* Face support */
1276cd0c2139SMatthew G Knepley         for (s = 0; s < supportSize; ++s) {
1277cd0c2139SMatthew G Knepley           PetscInt val;
1278cd0c2139SMatthew G Knepley 
1279cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[s], &val);CHKERRQ(ierr);
1280cd0c2139SMatthew G Knepley           if (val < 0) {
1281cd0c2139SMatthew G Knepley             /* Split old face:   Replace negative side cell with cohesive cell */
12824c367dbcSMatthew G. Knepley              ierr = DMPlexInsertSupport(sdm, newp, s, hybcell);CHKERRQ(ierr);
1283cd0c2139SMatthew G Knepley           } else {
1284cd0c2139SMatthew G Knepley             /* Split new face:   Replace positive side cell with cohesive cell */
12854c367dbcSMatthew G. Knepley             ierr = DMPlexInsertSupport(sdm, splitp, s, hybcell);CHKERRQ(ierr);
1286e537020bSMatthew G. Knepley             /* Get orientation for cohesive face */
1287e537020bSMatthew G. Knepley             {
1288e537020bSMatthew G. Knepley               const PetscInt *ncone, *nconeO;
1289e537020bSMatthew G. Knepley               PetscInt        nconeSize, nc;
1290e537020bSMatthew G. Knepley 
1291e537020bSMatthew G. Knepley               ierr = DMPlexGetConeSize(dm, support[s], &nconeSize);CHKERRQ(ierr);
1292e537020bSMatthew G. Knepley               ierr = DMPlexGetCone(dm, support[s], &ncone);CHKERRQ(ierr);
1293e537020bSMatthew G. Knepley               ierr = DMPlexGetConeOrientation(dm, support[s], &nconeO);CHKERRQ(ierr);
1294e537020bSMatthew G. Knepley               for (nc = 0; nc < nconeSize; ++nc) {
1295e537020bSMatthew G. Knepley                 if (ncone[nc] == oldp) {
1296e537020bSMatthew G. Knepley                   coneONew[0] = nconeO[nc];
1297e537020bSMatthew G. Knepley                   break;
1298cd0c2139SMatthew G Knepley                 }
1299cd0c2139SMatthew G Knepley               }
13002c71b3e2SJacob Faibussowitsch               PetscCheckFalse(nc >= nconeSize,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate face %d in neighboring cell %d", oldp, support[s]);
1301e537020bSMatthew G. Knepley             }
1302e537020bSMatthew G. Knepley           }
1303e537020bSMatthew G. Knepley         }
13044c367dbcSMatthew G. Knepley         /* Cohesive cell:    Old and new split face, then new cohesive faces */
1305b5a892a1SMatthew G. Knepley         const PetscInt *arr = DMPolytopeTypeGetArrangment(ct, coneONew[0]);
1306b5a892a1SMatthew G. Knepley 
1307fd4b9f15SMatthew G. Knepley         coneNew[0]  = newp;   /* Extracted negative side orientation above */
13084c367dbcSMatthew G. Knepley         coneNew[1]  = splitp;
13094c367dbcSMatthew G. Knepley         coneONew[1] = coneONew[0];
1310e537020bSMatthew G. Knepley         for (q = 0; q < coneSize; ++q) {
1311412e9a14SMatthew G. Knepley           /* Hybrid faces must follow order from oriented end face */
1312b5a892a1SMatthew G. Knepley           const PetscInt qa = arr[q*2+0];
1313b5a892a1SMatthew G. Knepley           const PetscInt qo = arr[q*2+1];
1314b5a892a1SMatthew G. Knepley           DMPolytopeType ft = dep == 2 ? DM_POLYTOPE_SEGMENT : DM_POLYTOPE_POINT;
1315412e9a14SMatthew G. Knepley 
1316b5a892a1SMatthew G. Knepley           ierr = PetscFindInt(cone[qa], numSplitPoints[dep-1], splitPoints[dep-1], &v);CHKERRQ(ierr);
131718c5995bSMatthew G. Knepley           if (v < 0) {
1318b5a892a1SMatthew G. Knepley             ierr = PetscFindInt(cone[qa], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
131918c5995bSMatthew G. Knepley             coneNew[2+q]  = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
132018c5995bSMatthew G. Knepley           } else {
132118c5995bSMatthew G. Knepley             coneNew[2+q]  = v + pMaxNew[dep] + numSplitPoints[dep];
132218c5995bSMatthew G. Knepley           }
1323b5a892a1SMatthew G. Knepley           coneONew[2+q] = DMPolytopeTypeComposeOrientation(ft, qo, ornt[qa]);
1324e537020bSMatthew G. Knepley         }
13254c367dbcSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybcell, coneNew);CHKERRQ(ierr);
13264c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeOrientation(sdm, hybcell, coneONew);CHKERRQ(ierr);
132796a07cd0SMatthew G. Knepley         /* Label the hybrid cells on the boundary of the split */
132896a07cd0SMatthew G. Knepley         if (hasUnsplit) {ierr = DMLabelSetValue(label, -hybcell, dim);CHKERRQ(ierr);}
1329cd0c2139SMatthew G Knepley       } else if (dep == 0) {
13304c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1331cd0c2139SMatthew G Knepley 
1332cd0c2139SMatthew G Knepley         /* Split old vertex: Edges in old split faces and new cohesive edge */
13334c367dbcSMatthew G. Knepley         for (e = 0, qn = 0; e < supportSize; ++e) {
1334cd0c2139SMatthew G Knepley           PetscInt val;
1335cd0c2139SMatthew G Knepley 
1336cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1337cd0c2139SMatthew G Knepley           if ((val == 1) || (val == (shift + 1))) {
13382582d50cSToby Isaac             supportNew[qn++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1339cd0c2139SMatthew G Knepley           }
1340cd0c2139SMatthew G Knepley         }
13414c367dbcSMatthew G. Knepley         supportNew[qn] = hybedge;
1342cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
1343cd0c2139SMatthew G Knepley         /* Split new vertex: Edges in new split faces and new cohesive edge */
13444c367dbcSMatthew G. Knepley         for (e = 0, qp = 0; e < supportSize; ++e) {
1345cd0c2139SMatthew G Knepley           PetscInt val, edge;
1346cd0c2139SMatthew G Knepley 
1347cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1348cd0c2139SMatthew G Knepley           if (val == 1) {
13494c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);CHKERRQ(ierr);
13502c71b3e2SJacob Faibussowitsch             PetscCheckFalse(edge < 0,comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
13514c367dbcSMatthew G. Knepley             supportNew[qp++] = edge + pMaxNew[dep+1];
1352cd0c2139SMatthew G Knepley           } else if (val == -(shift + 1)) {
13532582d50cSToby Isaac             supportNew[qp++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1354cd0c2139SMatthew G Knepley           }
1355cd0c2139SMatthew G Knepley         }
13564c367dbcSMatthew G. Knepley         supportNew[qp] = hybedge;
1357cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, splitp, supportNew);CHKERRQ(ierr);
13584c367dbcSMatthew G. Knepley         /* Hybrid edge:    Old and new split vertex */
1359cd0c2139SMatthew G Knepley         coneNew[0] = newp;
1360cd0c2139SMatthew G Knepley         coneNew[1] = splitp;
13614c367dbcSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybedge, coneNew);CHKERRQ(ierr);
13624c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
13634c367dbcSMatthew G. Knepley           PetscInt val, edge;
13644c367dbcSMatthew G. Knepley 
13654c367dbcSMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
13664c367dbcSMatthew G. Knepley           if (val == 1) {
13674c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);CHKERRQ(ierr);
13682c71b3e2SJacob Faibussowitsch             PetscCheckFalse(edge < 0,comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
13694c367dbcSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2];
13704c367dbcSMatthew G. Knepley           }
13714c367dbcSMatthew G. Knepley         }
13724c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybedge, supportNew);CHKERRQ(ierr);
1373cd0c2139SMatthew G Knepley       } else if (dep == dim-2) {
13744c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
13754c367dbcSMatthew G. Knepley 
1376cd0c2139SMatthew G Knepley         /* Split old edge:   old vertices in cone so no change */
1377cd0c2139SMatthew G Knepley         /* Split new edge:   new vertices in cone */
1378cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
13794c367dbcSMatthew G. Knepley           ierr = PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);CHKERRQ(ierr);
1380e1757548SMatthew G. Knepley           if (v < 0) {
1381e1757548SMatthew G. Knepley             ierr = PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
13822c71b3e2SJacob 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);
13832582d50cSToby Isaac             coneNew[q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
1384e1757548SMatthew G. Knepley           } else {
13854c367dbcSMatthew G. Knepley             coneNew[q] = v + pMaxNew[dep-1];
1386cd0c2139SMatthew G Knepley           }
1387e1757548SMatthew G. Knepley         }
1388cd0c2139SMatthew G Knepley         ierr = DMPlexSetCone(sdm, splitp, coneNew);CHKERRQ(ierr);
1389cd0c2139SMatthew G Knepley         /* Split old edge: Faces in positive side cells and old split faces */
1390cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1391cd0c2139SMatthew G Knepley           PetscInt val;
1392cd0c2139SMatthew G Knepley 
1393cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
13944c367dbcSMatthew G. Knepley           if (val == dim-1) {
13952582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
13964c367dbcSMatthew 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, newp, supportNew);CHKERRQ(ierr);
1402cd0c2139SMatthew G Knepley         /* Split new edge: Faces in negative side cells and new split faces */
1403cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1404cd0c2139SMatthew G Knepley           PetscInt val, face;
1405cd0c2139SMatthew G Knepley 
1406cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1407cd0c2139SMatthew G Knepley           if (val == dim-1) {
14084c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
14092c71b3e2SJacob Faibussowitsch             PetscCheckFalse(face < 0,comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[e]);
14104c367dbcSMatthew G. Knepley             supportNew[q++] = face + pMaxNew[dep+1];
1411cd0c2139SMatthew G Knepley           } else if (val == -(shift + dim-1)) {
14122582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1413cd0c2139SMatthew G Knepley           }
1414cd0c2139SMatthew G Knepley         }
1415b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1416cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, splitp, supportNew);CHKERRQ(ierr);
14174c367dbcSMatthew G. Knepley         /* Hybrid face */
14184c367dbcSMatthew G. Knepley         coneNew[0] = newp;
14194c367dbcSMatthew G. Knepley         coneNew[1] = splitp;
14204c367dbcSMatthew G. Knepley         for (v = 0; v < coneSize; ++v) {
14214c367dbcSMatthew G. Knepley           PetscInt vertex;
14224c367dbcSMatthew G. Knepley           ierr = PetscFindInt(cone[v], numSplitPoints[dep-1], splitPoints[dep-1], &vertex);CHKERRQ(ierr);
1423e1757548SMatthew G. Knepley           if (vertex < 0) {
1424e1757548SMatthew G. Knepley             ierr = PetscFindInt(cone[v], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &vertex);CHKERRQ(ierr);
14252c71b3e2SJacob 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);
1426e1757548SMatthew G. Knepley             coneNew[2+v] = vertex + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1427e1757548SMatthew G. Knepley           } else {
14284c367dbcSMatthew G. Knepley             coneNew[2+v] = vertex + pMaxNew[dep] + numSplitPoints[dep];
14294c367dbcSMatthew G. Knepley           }
1430e1757548SMatthew G. Knepley         }
14314c367dbcSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybface, coneNew);CHKERRQ(ierr);
14324c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
14334c367dbcSMatthew G. Knepley           PetscInt val, face;
14344c367dbcSMatthew G. Knepley 
14354c367dbcSMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
14364c367dbcSMatthew G. Knepley           if (val == dim-1) {
14374c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
14382c71b3e2SJacob Faibussowitsch             PetscCheckFalse(face < 0,comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[e]);
14394c367dbcSMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+2] + numSplitPoints[dep+2];
14404c367dbcSMatthew G. Knepley           }
14414c367dbcSMatthew G. Knepley         }
14424c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybface, supportNew);CHKERRQ(ierr);
1443cd0c2139SMatthew G Knepley       }
1444cd0c2139SMatthew G Knepley     }
1445cd0c2139SMatthew G Knepley   }
144618c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
144718c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
144818c5995bSMatthew G. Knepley       const PetscInt  oldp   = unsplitPoints[dep][p];
14492582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
1450b5a892a1SMatthew G. Knepley       const PetscInt *cone, *support;
1451e1757548SMatthew G. Knepley       PetscInt        coneSize, supportSize, supportSizeNew, q, qf, e, f, s;
145218c5995bSMatthew G. Knepley 
145318c5995bSMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
145418c5995bSMatthew G. Knepley       ierr = DMPlexGetCone(dm, oldp, &cone);CHKERRQ(ierr);
145518c5995bSMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
145618c5995bSMatthew G. Knepley       ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
145718c5995bSMatthew G. Knepley       if (dep == 0) {
145818c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
145918c5995bSMatthew G. Knepley 
146018c5995bSMatthew G. Knepley         /* Unsplit vertex */
146118c5995bSMatthew G. Knepley         ierr = DMPlexGetSupportSize(sdm, newp, &supportSizeNew);CHKERRQ(ierr);
146218c5995bSMatthew G. Knepley         for (s = 0, q = 0; s < supportSize; ++s) {
14632582d50cSToby Isaac           supportNew[q++] = DMPlexShiftPoint_Internal(support[s], depth, depthShift) /*support[s] + depthOffset[dep+1]*/;
146418c5995bSMatthew G. Knepley           ierr = PetscFindInt(support[s], numSplitPoints[dep+1], splitPoints[dep+1], &e);CHKERRQ(ierr);
146518c5995bSMatthew G. Knepley           if (e >= 0) {
146618c5995bSMatthew G. Knepley             supportNew[q++] = e + pMaxNew[dep+1];
146718c5995bSMatthew G. Knepley           }
146818c5995bSMatthew G. Knepley         }
146918c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
147018c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
14712c71b3e2SJacob Faibussowitsch         PetscCheckFalse(q != supportSizeNew,comm, PETSC_ERR_ARG_WRONG, "Support size %d != %d for vertex %d", q, supportSizeNew, newp);
147218c5995bSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
147318c5995bSMatthew G. Knepley         /* Hybrid edge */
147418c5995bSMatthew G. Knepley         coneNew[0] = newp;
147518c5995bSMatthew G. Knepley         coneNew[1] = newp;
147618c5995bSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybedge, coneNew);CHKERRQ(ierr);
147718c5995bSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
147818c5995bSMatthew G. Knepley           PetscInt val, edge;
147918c5995bSMatthew G. Knepley 
148018c5995bSMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
148118c5995bSMatthew G. Knepley           if (val == 1) {
148218c5995bSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);CHKERRQ(ierr);
14832c71b3e2SJacob Faibussowitsch             PetscCheckFalse(edge < 0,comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
148418c5995bSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2];
1485e1757548SMatthew G. Knepley           } else if  (val ==  (shift2 + 1)) {
1486e1757548SMatthew G. Knepley             ierr = PetscFindInt(support[e], numUnsplitPoints[dep+1], unsplitPoints[dep+1], &edge);CHKERRQ(ierr);
14872c71b3e2SJacob Faibussowitsch             PetscCheckFalse(edge < 0,comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a unsplit edge", support[e]);
1488e1757548SMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2] + numSplitPoints[dep+1];
148918c5995bSMatthew G. Knepley           }
149018c5995bSMatthew G. Knepley         }
149118c5995bSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybedge, supportNew);CHKERRQ(ierr);
1492e1757548SMatthew G. Knepley       } else if (dep == dim-2) {
1493e1757548SMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
1494e1757548SMatthew G. Knepley 
1495da1dd7e4SMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and hybrid face twice */
1496e1757548SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1497e1757548SMatthew G. Knepley           PetscInt val, face;
1498e1757548SMatthew G. Knepley 
1499e1757548SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[f], &val);CHKERRQ(ierr);
1500e1757548SMatthew G. Knepley           if (val == dim-1) {
1501e1757548SMatthew G. Knepley             ierr = PetscFindInt(support[f], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
15022c71b3e2SJacob Faibussowitsch             PetscCheckFalse(face < 0,comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[f]);
15032582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1504e1757548SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+1];
1505e1757548SMatthew G. Knepley           } else {
15062582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1507e1757548SMatthew G. Knepley           }
1508e1757548SMatthew G. Knepley         }
1509e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
1510e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
1511da1dd7e4SMatthew G. Knepley         ierr = DMPlexGetSupportSize(sdm, newp, &supportSizeNew);CHKERRQ(ierr);
15122c71b3e2SJacob Faibussowitsch         PetscCheckFalse(qf != supportSizeNew,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Support size for unsplit edge %d is %d != %d", newp, qf, supportSizeNew);
1513e1757548SMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
1514e1757548SMatthew G. Knepley         /* Add hybrid face */
1515e1757548SMatthew G. Knepley         coneNew[0] = newp;
1516212cc919SMatthew G. Knepley         coneNew[1] = newp;
1517e1757548SMatthew G. Knepley         ierr = PetscFindInt(cone[0], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
15182c71b3e2SJacob Faibussowitsch         PetscCheckFalse(v < 0,comm, PETSC_ERR_ARG_WRONG, "Vertex %d is not an unsplit vertex", cone[0]);
1519212cc919SMatthew G. Knepley         coneNew[2] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1520e1757548SMatthew G. Knepley         ierr = PetscFindInt(cone[1], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
15212c71b3e2SJacob Faibussowitsch         PetscCheckFalse(v < 0,comm, PETSC_ERR_ARG_WRONG, "Vertex %d is not an unsplit vertex", cone[1]);
1522e1757548SMatthew G. Knepley         coneNew[3] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1523e1757548SMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybface, coneNew);CHKERRQ(ierr);
1524da1dd7e4SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1525da1dd7e4SMatthew G. Knepley           PetscInt val, face;
1526da1dd7e4SMatthew G. Knepley 
1527da1dd7e4SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[f], &val);CHKERRQ(ierr);
1528da1dd7e4SMatthew G. Knepley           if (val == dim-1) {
1529da1dd7e4SMatthew G. Knepley             ierr = PetscFindInt(support[f], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
1530da1dd7e4SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+2] + numSplitPoints[dep+2];
1531da1dd7e4SMatthew G. Knepley           }
1532da1dd7e4SMatthew G. Knepley         }
1533da1dd7e4SMatthew G. Knepley         ierr = DMPlexGetSupportSize(sdm, hybface, &supportSizeNew);CHKERRQ(ierr);
15342c71b3e2SJacob Faibussowitsch         PetscCheckFalse(qf != supportSizeNew,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Support size for hybrid face %d is %d != %d", hybface, qf, supportSizeNew);
1535e1757548SMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybface, supportNew);CHKERRQ(ierr);
1536cd0c2139SMatthew G Knepley       }
1537cd0c2139SMatthew G Knepley     }
1538cd0c2139SMatthew G Knepley   }
1539cd0c2139SMatthew G Knepley   /* Step 6b: Replace split points in negative side cones */
1540cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1541cd0c2139SMatthew G Knepley     PetscInt        dep = values[sp];
1542cd0c2139SMatthew G Knepley     IS              pIS;
1543cd0c2139SMatthew G Knepley     PetscInt        numPoints;
1544cd0c2139SMatthew G Knepley     const PetscInt *points;
1545cd0c2139SMatthew G Knepley 
1546cd0c2139SMatthew G Knepley     if (dep >= 0) continue;
1547cd0c2139SMatthew G Knepley     ierr = DMLabelGetStratumIS(label, dep, &pIS);CHKERRQ(ierr);
1548cd0c2139SMatthew G Knepley     if (!pIS) continue;
1549cd0c2139SMatthew G Knepley     dep  = -dep - shift;
1550cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(pIS, &numPoints);CHKERRQ(ierr);
1551cd0c2139SMatthew G Knepley     ierr = ISGetIndices(pIS, &points);CHKERRQ(ierr);
1552cd0c2139SMatthew G Knepley     for (p = 0; p < numPoints; ++p) {
1553cd0c2139SMatthew G Knepley       const PetscInt  oldp = points[p];
15542582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*depthOffset[dep] + oldp*/;
1555cd0c2139SMatthew G Knepley       const PetscInt *cone;
1556cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
155750cf782dSMatthew G. Knepley       /* PetscBool       replaced = PETSC_FALSE; */
1558cd0c2139SMatthew G Knepley 
1559cd0c2139SMatthew G Knepley       /* Negative edge: replace split vertex */
1560cd0c2139SMatthew G Knepley       /* Negative cell: replace split face */
1561cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(sdm, newp, &coneSize);CHKERRQ(ierr);
1562cd0c2139SMatthew G Knepley       ierr = DMPlexGetCone(sdm, newp, &cone);CHKERRQ(ierr);
1563cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
1564e38fbfedSToby Isaac         const PetscInt coldp = DMPlexShiftPointInverse_Internal(cone[c],depth,depthShift);
1565cd0c2139SMatthew G Knepley         PetscInt       csplitp, cp, val;
1566cd0c2139SMatthew G Knepley 
1567cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(label, coldp, &val);CHKERRQ(ierr);
1568cd0c2139SMatthew G Knepley         if (val == dep-1) {
1569cd0c2139SMatthew G Knepley           ierr = PetscFindInt(coldp, numSplitPoints[dep-1], splitPoints[dep-1], &cp);CHKERRQ(ierr);
15702c71b3e2SJacob Faibussowitsch           PetscCheckFalse(cp < 0,comm, PETSC_ERR_ARG_WRONG, "Point %d is not a split point of dimension %d", oldp, dep-1);
1571cd0c2139SMatthew G Knepley           csplitp  = pMaxNew[dep-1] + cp;
1572cd0c2139SMatthew G Knepley           ierr     = DMPlexInsertCone(sdm, newp, c, csplitp);CHKERRQ(ierr);
157350cf782dSMatthew G. Knepley           /* replaced = PETSC_TRUE; */
1574cd0c2139SMatthew G Knepley         }
1575cd0c2139SMatthew G Knepley       }
15764a189a86SMatthew G. Knepley       /* Cells with only a vertex or edge on the submesh have no replacement */
15772c71b3e2SJacob Faibussowitsch       /* PetscCheckFalse(!replaced,comm, PETSC_ERR_ARG_WRONG, "The cone of point %d does not contain split points", oldp); */
1578cd0c2139SMatthew G Knepley     }
1579cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(pIS, &points);CHKERRQ(ierr);
1580cd0c2139SMatthew G Knepley     ierr = ISDestroy(&pIS);CHKERRQ(ierr);
1581cd0c2139SMatthew G Knepley   }
1582fa8e8ae5SToby Isaac   /* Step 7: Coordinates */
1583cd0c2139SMatthew G Knepley   ierr = DMPlexShiftCoordinates_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
158469d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(sdm, &coordSection);CHKERRQ(ierr);
1585cd0c2139SMatthew G Knepley   ierr = DMGetCoordinatesLocal(sdm, &coordinates);CHKERRQ(ierr);
1586cd0c2139SMatthew G Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
1587cd0c2139SMatthew G Knepley   for (v = 0; v < (numSplitPoints ? numSplitPoints[0] : 0); ++v) {
15882582d50cSToby Isaac     const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[0][v], depth, depthShift) /*depthOffset[0] + splitPoints[0][v]*/;
1589cd0c2139SMatthew G Knepley     const PetscInt splitp = pMaxNew[0] + v;
1590cd0c2139SMatthew G Knepley     PetscInt       dof, off, soff, d;
1591cd0c2139SMatthew G Knepley 
1592cd0c2139SMatthew G Knepley     ierr = PetscSectionGetDof(coordSection, newp, &dof);CHKERRQ(ierr);
1593cd0c2139SMatthew G Knepley     ierr = PetscSectionGetOffset(coordSection, newp, &off);CHKERRQ(ierr);
1594cd0c2139SMatthew G Knepley     ierr = PetscSectionGetOffset(coordSection, splitp, &soff);CHKERRQ(ierr);
1595cd0c2139SMatthew G Knepley     for (d = 0; d < dof; ++d) coords[soff+d] = coords[off+d];
1596cd0c2139SMatthew G Knepley   }
1597cd0c2139SMatthew G Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
1598fa8e8ae5SToby Isaac   /* Step 8: SF, if I can figure this out we can split the mesh in parallel */
1599cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSF_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
1600fa8e8ae5SToby Isaac   /* Step 9: Labels */
1601cd0c2139SMatthew G Knepley   ierr = DMPlexShiftLabels_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
1602*d56405f8SMatthew G. Knepley   ierr = DMPlexCreateVTKLabel_Internal(dm, PETSC_FALSE, sdm);CHKERRQ(ierr);
1603c58f1c22SToby Isaac   ierr = DMGetNumLabels(sdm, &numLabels);CHKERRQ(ierr);
1604cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1605cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
16062582d50cSToby Isaac       const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[dep][p], depth, depthShift) /*depthOffset[dep] + splitPoints[dep][p]*/;
1607cd0c2139SMatthew G Knepley       const PetscInt splitp = pMaxNew[dep] + p;
1608cd0c2139SMatthew G Knepley       PetscInt       l;
1609cd0c2139SMatthew G Knepley 
16107db7e0a7SMatthew G. Knepley       if (splitLabel) {
16117db7e0a7SMatthew G. Knepley         const PetscInt val = 100 + dep;
16127db7e0a7SMatthew G. Knepley 
16137db7e0a7SMatthew G. Knepley         ierr = DMLabelSetValue(splitLabel, newp,    val);CHKERRQ(ierr);
16147db7e0a7SMatthew G. Knepley         ierr = DMLabelSetValue(splitLabel, splitp, -val);CHKERRQ(ierr);
16157db7e0a7SMatthew G. Knepley       }
1616cd0c2139SMatthew G Knepley       for (l = 0; l < numLabels; ++l) {
1617cd0c2139SMatthew G Knepley         DMLabel     mlabel;
1618cd0c2139SMatthew G Knepley         const char *lname;
1619cd0c2139SMatthew G Knepley         PetscInt    val;
16209a356370SMatthew G. Knepley         PetscBool   isDepth;
1621cd0c2139SMatthew G Knepley 
1622c58f1c22SToby Isaac         ierr = DMGetLabelName(sdm, l, &lname);CHKERRQ(ierr);
16239a356370SMatthew G. Knepley         ierr = PetscStrcmp(lname, "depth", &isDepth);CHKERRQ(ierr);
16249a356370SMatthew G. Knepley         if (isDepth) continue;
1625c58f1c22SToby Isaac         ierr = DMGetLabel(sdm, lname, &mlabel);CHKERRQ(ierr);
1626cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(mlabel, newp, &val);CHKERRQ(ierr);
1627cd0c2139SMatthew G Knepley         if (val >= 0) {
1628cd0c2139SMatthew G Knepley           ierr = DMLabelSetValue(mlabel, splitp, val);CHKERRQ(ierr);
1629cd0c2139SMatthew G Knepley         }
1630cd0c2139SMatthew G Knepley       }
1631cd0c2139SMatthew G Knepley     }
1632cd0c2139SMatthew G Knepley   }
1633cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1634cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1635cd0c2139SMatthew G Knepley 
1636cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
163718c5995bSMatthew G. Knepley     if (splitIS[dep]) {ierr = ISRestoreIndices(splitIS[dep], &splitPoints[dep]);CHKERRQ(ierr);}
163818c5995bSMatthew G. Knepley     ierr = ISDestroy(&splitIS[dep]);CHKERRQ(ierr);
163918c5995bSMatthew G. Knepley     if (unsplitIS[dep]) {ierr = ISRestoreIndices(unsplitIS[dep], &unsplitPoints[dep]);CHKERRQ(ierr);}
164018c5995bSMatthew G. Knepley     ierr = ISDestroy(&unsplitIS[dep]);CHKERRQ(ierr);
1641cd0c2139SMatthew G Knepley   }
1642cd0c2139SMatthew G Knepley   if (label) {
1643cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
1644cd0c2139SMatthew G Knepley     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
1645cd0c2139SMatthew G Knepley   }
16460d4d4d06SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
16470d4d4d06SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(sdm, d, NULL, &pEnd);CHKERRQ(ierr);
164836dbac82SMatthew G. Knepley     pMaxNew[d] = pEnd - numHybridPoints[d] - numHybridPointsOld[d];
16490d4d4d06SMatthew G. Knepley   }
1650dcbb62e8SMatthew G. Knepley   ierr = PetscFree3(coneNew, coneONew, supportNew);CHKERRQ(ierr);
16512582d50cSToby Isaac   ierr = PetscFree5(depthMax, depthEnd, depthShift, pMaxNew, numHybridPointsOld);CHKERRQ(ierr);
165218c5995bSMatthew G. Knepley   ierr = PetscFree7(splitIS, unsplitIS, numSplitPoints, numUnsplitPoints, numHybridPoints, splitPoints, unsplitPoints);CHKERRQ(ierr);
1653cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1654cd0c2139SMatthew G Knepley }
1655cd0c2139SMatthew G Knepley 
1656cd0c2139SMatthew G Knepley /*@C
1657cd0c2139SMatthew G Knepley   DMPlexConstructCohesiveCells - Construct cohesive cells which split the face along an internal interface
1658cd0c2139SMatthew G Knepley 
1659cd0c2139SMatthew G Knepley   Collective on dm
1660cd0c2139SMatthew G Knepley 
1661cd0c2139SMatthew G Knepley   Input Parameters:
1662cd0c2139SMatthew G Knepley + dm - The original DM
166353156dfcSMatthew G. Knepley - label - The label specifying the boundary faces (this could be auto-generated)
1664cd0c2139SMatthew G Knepley 
1665cd0c2139SMatthew G Knepley   Output Parameters:
16667db7e0a7SMatthew G. Knepley + splitLabel - The label containing the split points, or NULL if no output is desired
1667cd0c2139SMatthew G Knepley - dmSplit - The new DM
1668cd0c2139SMatthew G Knepley 
1669cd0c2139SMatthew G Knepley   Level: developer
1670cd0c2139SMatthew G Knepley 
16712be2b188SMatthew G Knepley .seealso: DMCreate(), DMPlexLabelCohesiveComplete()
1672cd0c2139SMatthew G Knepley @*/
16737db7e0a7SMatthew G. Knepley PetscErrorCode DMPlexConstructCohesiveCells(DM dm, DMLabel label, DMLabel splitLabel, DM *dmSplit)
1674cd0c2139SMatthew G Knepley {
1675cd0c2139SMatthew G Knepley   DM             sdm;
1676cd0c2139SMatthew G Knepley   PetscInt       dim;
1677cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
1678cd0c2139SMatthew G Knepley 
1679cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1680cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1681064a246eSJacob Faibussowitsch   PetscValidPointer(dmSplit, 4);
1682cd0c2139SMatthew G Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), &sdm);CHKERRQ(ierr);
1683cd0c2139SMatthew G Knepley   ierr = DMSetType(sdm, DMPLEX);CHKERRQ(ierr);
1684c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1685c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(sdm, dim);CHKERRQ(ierr);
1686cd0c2139SMatthew G Knepley   switch (dim) {
1687cd0c2139SMatthew G Knepley   case 2:
1688cd0c2139SMatthew G Knepley   case 3:
16897db7e0a7SMatthew G. Knepley     ierr = DMPlexConstructCohesiveCells_Internal(dm, label, splitLabel, sdm);CHKERRQ(ierr);
1690cd0c2139SMatthew G Knepley     break;
1691cd0c2139SMatthew G Knepley   default:
169298921bdaSJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct cohesive cells for dimension %d", dim);
1693cd0c2139SMatthew G Knepley   }
1694cd0c2139SMatthew G Knepley   *dmSplit = sdm;
1695cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1696cd0c2139SMatthew G Knepley }
1697cd0c2139SMatthew G Knepley 
16980f66a230SMatthew G. Knepley /* Returns the side of the surface for a given cell with a face on the surface */
16990f66a230SMatthew G. Knepley static PetscErrorCode GetSurfaceSide_Static(DM dm, DM subdm, PetscInt numSubpoints, const PetscInt *subpoints, PetscInt cell, PetscInt face, PetscBool *pos)
17000f66a230SMatthew G. Knepley {
17010f66a230SMatthew G. Knepley   const PetscInt *cone, *ornt;
17020f66a230SMatthew G. Knepley   PetscInt        dim, coneSize, c;
17030f66a230SMatthew G. Knepley   PetscErrorCode  ierr;
17040f66a230SMatthew G. Knepley 
17050f66a230SMatthew G. Knepley   PetscFunctionBegin;
17060f66a230SMatthew G. Knepley   *pos = PETSC_TRUE;
1707c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
17080f66a230SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
17090f66a230SMatthew G. Knepley   ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
17100f66a230SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, cell, &ornt);CHKERRQ(ierr);
17110f66a230SMatthew G. Knepley   for (c = 0; c < coneSize; ++c) {
17120f66a230SMatthew G. Knepley     if (cone[c] == face) {
17130f66a230SMatthew G. Knepley       PetscInt o = ornt[c];
17140f66a230SMatthew G. Knepley 
17150f66a230SMatthew G. Knepley       if (subdm) {
17160f66a230SMatthew G. Knepley         const PetscInt *subcone, *subornt;
17170f66a230SMatthew G. Knepley         PetscInt        subpoint, subface, subconeSize, sc;
17180f66a230SMatthew G. Knepley 
17190f66a230SMatthew G. Knepley         ierr = PetscFindInt(cell, numSubpoints, subpoints, &subpoint);CHKERRQ(ierr);
17200f66a230SMatthew G. Knepley         ierr = PetscFindInt(face, numSubpoints, subpoints, &subface);CHKERRQ(ierr);
17210f66a230SMatthew G. Knepley         ierr = DMPlexGetConeSize(subdm, subpoint, &subconeSize);CHKERRQ(ierr);
17220f66a230SMatthew G. Knepley         ierr = DMPlexGetCone(subdm, subpoint, &subcone);CHKERRQ(ierr);
17230f66a230SMatthew G. Knepley         ierr = DMPlexGetConeOrientation(subdm, subpoint, &subornt);CHKERRQ(ierr);
17240f66a230SMatthew G. Knepley         for (sc = 0; sc < subconeSize; ++sc) {
17250f66a230SMatthew G. Knepley           if (subcone[sc] == subface) {
17260f66a230SMatthew G. Knepley             o = subornt[0];
17270f66a230SMatthew G. Knepley             break;
17280f66a230SMatthew G. Knepley           }
17290f66a230SMatthew G. Knepley         }
17302c71b3e2SJacob 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);
17310f66a230SMatthew G. Knepley       }
17320f66a230SMatthew G. Knepley       if (o >= 0) *pos = PETSC_TRUE;
17330f66a230SMatthew G. Knepley       else        *pos = PETSC_FALSE;
17340f66a230SMatthew G. Knepley       break;
17350f66a230SMatthew G. Knepley     }
17360f66a230SMatthew G. Knepley   }
17372c71b3e2SJacob 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);
17380f66a230SMatthew G. Knepley   PetscFunctionReturn(0);
17390f66a230SMatthew G. Knepley }
17400f66a230SMatthew G. Knepley 
1741cd0c2139SMatthew G Knepley /*@
17420f66a230SMatthew G. Knepley   DMPlexLabelCohesiveComplete - Starting with a label marking points on an internal surface, we add all other mesh pieces
1743cd0c2139SMatthew G Knepley   to complete the surface
1744cd0c2139SMatthew G Knepley 
1745cd0c2139SMatthew G Knepley   Input Parameters:
1746cd0c2139SMatthew G Knepley + dm     - The DM
17470f66a230SMatthew G. Knepley . label  - A DMLabel marking the surface
17480f66a230SMatthew G. Knepley . blabel - A DMLabel marking the vertices on the boundary which will not be duplicated, or NULL to find them automatically
1749bb55d314SMatthew G. Knepley . flip   - Flag to flip the submesh normal and replace points on the other side
175047946fd8SMatthew G. Knepley - subdm  - The subDM associated with the label, or NULL
1751cd0c2139SMatthew G Knepley 
1752cd0c2139SMatthew G Knepley   Output Parameter:
1753cd0c2139SMatthew G Knepley . label - A DMLabel marking all surface points
1754cd0c2139SMatthew G Knepley 
17550f66a230SMatthew G. Knepley   Note: The vertices in blabel are called "unsplit" in the terminology from hybrid cell creation.
17560f66a230SMatthew G. Knepley 
1757cd0c2139SMatthew G Knepley   Level: developer
1758cd0c2139SMatthew G Knepley 
17592be2b188SMatthew G Knepley .seealso: DMPlexConstructCohesiveCells(), DMPlexLabelComplete()
1760cd0c2139SMatthew G Knepley @*/
17610f66a230SMatthew G. Knepley PetscErrorCode DMPlexLabelCohesiveComplete(DM dm, DMLabel label, DMLabel blabel, PetscBool flip, DM subdm)
1762cd0c2139SMatthew G Knepley {
1763d90583fdSMatthew G. Knepley   DMLabel         depthLabel;
17648630cb1aSMatthew G. Knepley   IS              dimIS, subpointIS = NULL, facePosIS, faceNegIS, crossEdgeIS = NULL;
176547946fd8SMatthew G. Knepley   const PetscInt *points, *subpoints;
1766bb55d314SMatthew G. Knepley   const PetscInt  rev   = flip ? -1 : 1;
1767412e9a14SMatthew G. Knepley   PetscInt        shift = 100, shift2 = 200, dim, depth, dep, cStart, cEnd, vStart, vEnd, numPoints, numSubpoints, p, val;
1768cd0c2139SMatthew G Knepley   PetscErrorCode  ierr;
1769cd0c2139SMatthew G Knepley 
1770cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1771370472baSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1772370472baSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1773d90583fdSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
177447946fd8SMatthew G. Knepley   if (subdm) {
177597d8846cSMatthew Knepley     ierr = DMPlexGetSubpointIS(subdm, &subpointIS);CHKERRQ(ierr);
177647946fd8SMatthew G. Knepley     if (subpointIS) {
177747946fd8SMatthew G. Knepley       ierr = ISGetLocalSize(subpointIS, &numSubpoints);CHKERRQ(ierr);
177847946fd8SMatthew G. Knepley       ierr = ISGetIndices(subpointIS, &subpoints);CHKERRQ(ierr);
177947946fd8SMatthew G. Knepley     }
178047946fd8SMatthew G. Knepley   }
1781d7c8f101SMatthew G. Knepley   /* Mark cell on the fault, and its faces which touch the fault: cell orientation for face gives the side of the fault */
1782cd0c2139SMatthew G Knepley   ierr = DMLabelGetStratumIS(label, dim-1, &dimIS);CHKERRQ(ierr);
178397d8846cSMatthew Knepley   if (!dimIS) PetscFunctionReturn(0);
1784cd0c2139SMatthew G Knepley   ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
1785cd0c2139SMatthew G Knepley   ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
1786d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault faces */
1787cd0c2139SMatthew G Knepley     const PetscInt *support;
1788cd0c2139SMatthew G Knepley     PetscInt        supportSize, s;
1789cd0c2139SMatthew G Knepley 
1790cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupportSize(dm, points[p], &supportSize);CHKERRQ(ierr);
1791c4419245SMatthew G. Knepley #if 0
1792c4419245SMatthew G. Knepley     if (supportSize != 2) {
1793c4419245SMatthew G. Knepley       const PetscInt *lp;
1794c4419245SMatthew G. Knepley       PetscInt        Nlp, pind;
1795c4419245SMatthew G. Knepley 
1796c4419245SMatthew G. Knepley       /* Check that for a cell with a single support face, that face is in the SF */
1797c4419245SMatthew G. Knepley       /*   THis check only works for the remote side. We would need root side information */
1798c4419245SMatthew G. Knepley       ierr = PetscSFGetGraph(dm->sf, NULL, &Nlp, &lp, NULL);CHKERRQ(ierr);
1799c4419245SMatthew G. Knepley       ierr = PetscFindInt(points[p], Nlp, lp, &pind);CHKERRQ(ierr);
18002c71b3e2SJacob 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);
1801c4419245SMatthew G. Knepley     }
1802c4419245SMatthew G. Knepley #endif
1803cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupport(dm, points[p], &support);CHKERRQ(ierr);
1804cd0c2139SMatthew G Knepley     for (s = 0; s < supportSize; ++s) {
18050f66a230SMatthew G. Knepley       const PetscInt *cone;
1806cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
18070f66a230SMatthew G. Knepley       PetscBool       pos;
1808cd0c2139SMatthew G Knepley 
18090f66a230SMatthew G. Knepley       ierr = GetSurfaceSide_Static(dm, subdm, numSubpoints, subpoints, support[s], points[p], &pos);CHKERRQ(ierr);
18100f66a230SMatthew G. Knepley       if (pos) {ierr = DMLabelSetValue(label, support[s],  rev*(shift+dim));CHKERRQ(ierr);}
18110f66a230SMatthew G. Knepley       else     {ierr = DMLabelSetValue(label, support[s], -rev*(shift+dim));CHKERRQ(ierr);}
18120f66a230SMatthew G. Knepley       if (rev < 0) pos = !pos ? PETSC_TRUE : PETSC_FALSE;
18130f66a230SMatthew G. Knepley       /* Put faces touching the fault in the label */
1814cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
1815cd0c2139SMatthew G Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
1816cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
1817cd0c2139SMatthew G Knepley         const PetscInt point = cone[c];
1818cd0c2139SMatthew G Knepley 
1819cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1820cd0c2139SMatthew G Knepley         if (val == -1) {
1821cd0c2139SMatthew G Knepley           PetscInt *closure = NULL;
1822cd0c2139SMatthew G Knepley           PetscInt  closureSize, cl;
1823cd0c2139SMatthew G Knepley 
1824cd0c2139SMatthew G Knepley           ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1825cd0c2139SMatthew G Knepley           for (cl = 0; cl < closureSize*2; cl += 2) {
1826cd0c2139SMatthew G Knepley             const PetscInt clp  = closure[cl];
1827a0541d8aSMatthew G. Knepley             PetscInt       bval = -1;
1828cd0c2139SMatthew G Knepley 
1829cd0c2139SMatthew G Knepley             ierr = DMLabelGetValue(label, clp, &val);CHKERRQ(ierr);
1830a0541d8aSMatthew G. Knepley             if (blabel) {ierr = DMLabelGetValue(blabel, clp, &bval);CHKERRQ(ierr);}
1831a0541d8aSMatthew G. Knepley             if ((val >= 0) && (val < dim-1) && (bval < 0)) {
1832cd0c2139SMatthew G Knepley               ierr = DMLabelSetValue(label, point, pos == PETSC_TRUE ? shift+dim-1 : -(shift+dim-1));CHKERRQ(ierr);
1833cd0c2139SMatthew G Knepley               break;
1834cd0c2139SMatthew G Knepley             }
1835cd0c2139SMatthew G Knepley           }
1836cd0c2139SMatthew G Knepley           ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1837cd0c2139SMatthew G Knepley         }
1838cd0c2139SMatthew G Knepley       }
1839cd0c2139SMatthew G Knepley     }
1840cd0c2139SMatthew G Knepley   }
18410f66a230SMatthew G. Knepley   ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
18420f66a230SMatthew G. Knepley   ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
184347946fd8SMatthew G. Knepley   if (subpointIS) {ierr = ISRestoreIndices(subpointIS, &subpoints);CHKERRQ(ierr);}
1844a0541d8aSMatthew G. Knepley   /* Mark boundary points as unsplit */
184586200784SMatthew G. Knepley   if (blabel) {
1846a0541d8aSMatthew G. Knepley     ierr = DMLabelGetStratumIS(blabel, 1, &dimIS);CHKERRQ(ierr);
1847a0541d8aSMatthew G. Knepley     ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
1848a0541d8aSMatthew G. Knepley     ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
1849a0541d8aSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
1850a0541d8aSMatthew G. Knepley       const PetscInt point = points[p];
1851a0541d8aSMatthew G. Knepley       PetscInt       val, bval;
1852a0541d8aSMatthew G. Knepley 
1853a0541d8aSMatthew G. Knepley       ierr = DMLabelGetValue(blabel, point, &bval);CHKERRQ(ierr);
1854a0541d8aSMatthew G. Knepley       if (bval >= 0) {
1855f7019248SMatthew G. Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1856f7019248SMatthew G. Knepley         if ((val < 0) || (val > dim)) {
1857f7019248SMatthew G. Knepley           /* This could be a point added from splitting a vertex on an adjacent fault, otherwise its just wrong */
1858f7019248SMatthew G. Knepley           ierr = DMLabelClearValue(blabel, point, bval);CHKERRQ(ierr);
1859f7019248SMatthew G. Knepley         }
1860f7019248SMatthew G. Knepley       }
1861f7019248SMatthew G. Knepley     }
1862f7019248SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
1863f7019248SMatthew G. Knepley       const PetscInt point = points[p];
1864f7019248SMatthew G. Knepley       PetscInt       val, bval;
1865f7019248SMatthew G. Knepley 
1866f7019248SMatthew G. Knepley       ierr = DMLabelGetValue(blabel, point, &bval);CHKERRQ(ierr);
1867f7019248SMatthew G. Knepley       if (bval >= 0) {
186886200784SMatthew G. Knepley         const PetscInt *cone,    *support;
186986200784SMatthew G. Knepley         PetscInt        coneSize, supportSize, s, valA, valB, valE;
187086200784SMatthew G. Knepley 
1871a0541d8aSMatthew G. Knepley         /* Mark as unsplit */
1872a0541d8aSMatthew G. Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
18732c71b3e2SJacob 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);
1874a0541d8aSMatthew G. Knepley         ierr = DMLabelClearValue(label, point, val);CHKERRQ(ierr);
1875a0541d8aSMatthew G. Knepley         ierr = DMLabelSetValue(label, point, shift2+val);CHKERRQ(ierr);
18762c06a818SMatthew G. Knepley         /* Check for cross-edge
18772c06a818SMatthew G. Knepley              A cross-edge has endpoints which are both on the boundary of the surface, but the edge itself is not. */
187886200784SMatthew G. Knepley         if (val != 0) continue;
187986200784SMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
188086200784SMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
188186200784SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
188286200784SMatthew G. Knepley           ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
188386200784SMatthew G. Knepley           ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
18842c71b3e2SJacob Faibussowitsch           PetscCheckFalse(coneSize != 2,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Edge %D has %D vertices != 2", support[s], coneSize);
188586200784SMatthew G. Knepley           ierr = DMLabelGetValue(blabel, cone[0], &valA);CHKERRQ(ierr);
188686200784SMatthew G. Knepley           ierr = DMLabelGetValue(blabel, cone[1], &valB);CHKERRQ(ierr);
188786200784SMatthew G. Knepley           ierr = DMLabelGetValue(blabel, support[s], &valE);CHKERRQ(ierr);
188809816f77SMatthew G. Knepley           if ((valE < 0) && (valA >= 0) && (valB >= 0) && (cone[0] != cone[1])) {ierr = DMLabelSetValue(blabel, support[s], 2);CHKERRQ(ierr);}
188986200784SMatthew G. Knepley         }
1890a0541d8aSMatthew G. Knepley       }
1891a0541d8aSMatthew G. Knepley     }
1892a0541d8aSMatthew G. Knepley     ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
1893a0541d8aSMatthew G. Knepley     ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
1894a0541d8aSMatthew G. Knepley   }
1895cd0c2139SMatthew G Knepley   /* Search for other cells/faces/edges connected to the fault by a vertex */
1896c4d480a2SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1897412e9a14SMatthew G. Knepley   ierr = DMPlexGetSimplexOrBoxCells(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1898cd0c2139SMatthew G Knepley   ierr = DMLabelGetStratumIS(label, 0, &dimIS);CHKERRQ(ierr);
18997d2fefb6SMatthew G. Knepley   /* TODO Why are we including cross edges here? Shouldn't they be in the star of boundary vertices? */
190086200784SMatthew G. Knepley   if (blabel) {ierr = DMLabelGetStratumIS(blabel, 2, &crossEdgeIS);CHKERRQ(ierr);}
190186200784SMatthew G. Knepley   if (dimIS && crossEdgeIS) {
190286200784SMatthew G. Knepley     IS vertIS = dimIS;
190386200784SMatthew G. Knepley 
190486200784SMatthew G. Knepley     ierr = ISExpand(vertIS, crossEdgeIS, &dimIS);CHKERRQ(ierr);
190586200784SMatthew G. Knepley     ierr = ISDestroy(&crossEdgeIS);CHKERRQ(ierr);
190686200784SMatthew G. Knepley     ierr = ISDestroy(&vertIS);CHKERRQ(ierr);
190786200784SMatthew G. Knepley   }
1908dc86033cSMatthew G. Knepley   if (!dimIS) {
1909dc86033cSMatthew G. Knepley     PetscFunctionReturn(0);
1910dc86033cSMatthew G. Knepley   }
1911cd0c2139SMatthew G Knepley   ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
1912cd0c2139SMatthew G Knepley   ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
1913d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault vertices */
1914cd0c2139SMatthew G Knepley     PetscInt *star = NULL;
191586200784SMatthew G. Knepley     PetscInt  starSize, s;
1916cd0c2139SMatthew G Knepley     PetscInt  again = 1;  /* 0: Finished 1: Keep iterating after a change 2: No change */
1917cd0c2139SMatthew G Knepley 
1918d4622b2cSMatthew G. Knepley     /* All points connected to the fault are inside a cell, so at the top level we will only check cells */
1919cd0c2139SMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
1920cd0c2139SMatthew G Knepley     while (again) {
19212c71b3e2SJacob Faibussowitsch       PetscCheckFalse(again > 1,PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Could not classify all cells connected to the fault");
1922cd0c2139SMatthew G Knepley       again = 0;
1923cd0c2139SMatthew G Knepley       for (s = 0; s < starSize*2; s += 2) {
1924cd0c2139SMatthew G Knepley         const PetscInt  point = star[s];
1925cd0c2139SMatthew G Knepley         const PetscInt *cone;
1926cd0c2139SMatthew G Knepley         PetscInt        coneSize, c;
1927cd0c2139SMatthew G Knepley 
1928412e9a14SMatthew G. Knepley         if ((point < cStart) || (point >= cEnd)) continue;
1929cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1930cd0c2139SMatthew G Knepley         if (val != -1) continue;
1931d4622b2cSMatthew G. Knepley         again = again == 1 ? 1 : 2;
1932cd0c2139SMatthew G Knepley         ierr  = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
1933cd0c2139SMatthew G Knepley         ierr  = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
1934cd0c2139SMatthew G Knepley         for (c = 0; c < coneSize; ++c) {
1935cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, cone[c], &val);CHKERRQ(ierr);
1936cd0c2139SMatthew G Knepley           if (val != -1) {
1937d7c8f101SMatthew G. Knepley             const PetscInt *ccone;
1938166d9d0cSMatthew G. Knepley             PetscInt        cconeSize, cc, side;
1939d7c8f101SMatthew G. Knepley 
19402c71b3e2SJacob 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);
194137a6de01SMatthew G. Knepley             if (val > 0) side =  1;
194237a6de01SMatthew G. Knepley             else         side = -1;
194337a6de01SMatthew G. Knepley             ierr = DMLabelSetValue(label, point, side*(shift+dim));CHKERRQ(ierr);
1944d7c8f101SMatthew G. Knepley             /* Mark cell faces which touch the fault */
1945d7c8f101SMatthew G. Knepley             ierr = DMPlexGetConeSize(dm, point, &cconeSize);CHKERRQ(ierr);
1946d7c8f101SMatthew G. Knepley             ierr = DMPlexGetCone(dm, point, &ccone);CHKERRQ(ierr);
1947d7c8f101SMatthew G. Knepley             for (cc = 0; cc < cconeSize; ++cc) {
1948d7c8f101SMatthew G. Knepley               PetscInt *closure = NULL;
1949d7c8f101SMatthew G. Knepley               PetscInt  closureSize, cl;
1950d7c8f101SMatthew G. Knepley 
1951166d9d0cSMatthew G. Knepley               ierr = DMLabelGetValue(label, ccone[cc], &val);CHKERRQ(ierr);
1952166d9d0cSMatthew G. Knepley               if (val != -1) continue;
1953d7c8f101SMatthew G. Knepley               ierr = DMPlexGetTransitiveClosure(dm, ccone[cc], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1954d4622b2cSMatthew G. Knepley               for (cl = 0; cl < closureSize*2; cl += 2) {
1955d7c8f101SMatthew G. Knepley                 const PetscInt clp = closure[cl];
1956d7c8f101SMatthew G. Knepley 
1957d7c8f101SMatthew G. Knepley                 ierr = DMLabelGetValue(label, clp, &val);CHKERRQ(ierr);
1958d7c8f101SMatthew G. Knepley                 if (val == -1) continue;
1959d7c8f101SMatthew G. Knepley                 ierr = DMLabelSetValue(label, ccone[cc], side*(shift+dim-1));CHKERRQ(ierr);
196037a6de01SMatthew G. Knepley                 break;
196137a6de01SMatthew G. Knepley               }
1962d7c8f101SMatthew G. Knepley               ierr = DMPlexRestoreTransitiveClosure(dm, ccone[cc], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1963cd0c2139SMatthew G Knepley             }
1964cd0c2139SMatthew G Knepley             again = 1;
1965cd0c2139SMatthew G Knepley             break;
1966cd0c2139SMatthew G Knepley           }
1967cd0c2139SMatthew G Knepley         }
1968cd0c2139SMatthew G Knepley       }
1969cd0c2139SMatthew G Knepley     }
1970cd0c2139SMatthew G Knepley     /* Classify the rest by cell membership */
1971cd0c2139SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
1972cd0c2139SMatthew G Knepley       const PetscInt point = star[s];
1973cd0c2139SMatthew G Knepley 
1974cd0c2139SMatthew G Knepley       ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1975cd0c2139SMatthew G Knepley       if (val == -1) {
1976cd0c2139SMatthew G Knepley         PetscInt      *sstar = NULL;
1977cd0c2139SMatthew G Knepley         PetscInt       sstarSize, ss;
1978412e9a14SMatthew G. Knepley         PetscBool      marked = PETSC_FALSE, isHybrid;
1979cd0c2139SMatthew G Knepley 
1980cd0c2139SMatthew G Knepley         ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &sstarSize, &sstar);CHKERRQ(ierr);
1981cd0c2139SMatthew G Knepley         for (ss = 0; ss < sstarSize*2; ss += 2) {
1982cd0c2139SMatthew G Knepley           const PetscInt spoint = sstar[ss];
1983cd0c2139SMatthew G Knepley 
1984412e9a14SMatthew G. Knepley           if ((spoint < cStart) || (spoint >= cEnd)) continue;
1985cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, spoint, &val);CHKERRQ(ierr);
19862c71b3e2SJacob Faibussowitsch           PetscCheckFalse(val == -1,PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %d in star of %d does not have a valid label", spoint, point);
1987d90583fdSMatthew G. Knepley           ierr = DMLabelGetValue(depthLabel, point, &dep);CHKERRQ(ierr);
1988cd0c2139SMatthew G Knepley           if (val > 0) {
1989cd0c2139SMatthew G Knepley             ierr = DMLabelSetValue(label, point,   shift+dep);CHKERRQ(ierr);
1990cd0c2139SMatthew G Knepley           } else {
1991cd0c2139SMatthew G Knepley             ierr = DMLabelSetValue(label, point, -(shift+dep));CHKERRQ(ierr);
1992cd0c2139SMatthew G Knepley           }
1993cd0c2139SMatthew G Knepley           marked = PETSC_TRUE;
1994cd0c2139SMatthew G Knepley           break;
1995cd0c2139SMatthew G Knepley         }
1996cd0c2139SMatthew G Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &sstarSize, &sstar);CHKERRQ(ierr);
1997412e9a14SMatthew G. Knepley         ierr = DMPlexCellIsHybrid_Internal(dm, point, &isHybrid);CHKERRQ(ierr);
19982c71b3e2SJacob Faibussowitsch         PetscCheckFalse(!isHybrid && !marked,PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Point %d could not be classified", point);
1999cd0c2139SMatthew G Knepley       }
2000cd0c2139SMatthew G Knepley     }
2001cd0c2139SMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2002cd0c2139SMatthew G Knepley   }
2003cd0c2139SMatthew G Knepley   ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
2004cd0c2139SMatthew G Knepley   ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
20057d2fefb6SMatthew G. Knepley   /* If any faces touching the fault divide cells on either side, split them
20067d2fefb6SMatthew G. Knepley        This only occurs without a surface boundary */
200718c5995bSMatthew G. Knepley   ierr = DMLabelGetStratumIS(label,   shift+dim-1,  &facePosIS);CHKERRQ(ierr);
200818c5995bSMatthew G. Knepley   ierr = DMLabelGetStratumIS(label, -(shift+dim-1), &faceNegIS);CHKERRQ(ierr);
200918c5995bSMatthew G. Knepley   ierr = ISExpand(facePosIS, faceNegIS, &dimIS);CHKERRQ(ierr);
201018c5995bSMatthew G. Knepley   ierr = ISDestroy(&facePosIS);CHKERRQ(ierr);
201118c5995bSMatthew G. Knepley   ierr = ISDestroy(&faceNegIS);CHKERRQ(ierr);
201218c5995bSMatthew G. Knepley   ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
201318c5995bSMatthew G. Knepley   ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
201418c5995bSMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
201518c5995bSMatthew G. Knepley     const PetscInt  point = points[p];
201618c5995bSMatthew G. Knepley     const PetscInt *support;
201718c5995bSMatthew G. Knepley     PetscInt        supportSize, valA, valB;
201818c5995bSMatthew G. Knepley 
201918c5995bSMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
202018c5995bSMatthew G. Knepley     if (supportSize != 2) continue;
202118c5995bSMatthew G. Knepley     ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
202218c5995bSMatthew G. Knepley     ierr = DMLabelGetValue(label, support[0], &valA);CHKERRQ(ierr);
202318c5995bSMatthew G. Knepley     ierr = DMLabelGetValue(label, support[1], &valB);CHKERRQ(ierr);
202418c5995bSMatthew G. Knepley     if ((valA == -1) || (valB == -1)) continue;
202518c5995bSMatthew G. Knepley     if (valA*valB > 0) continue;
202618c5995bSMatthew G. Knepley     /* Split the face */
202718c5995bSMatthew G. Knepley     ierr = DMLabelGetValue(label, point, &valA);CHKERRQ(ierr);
202818c5995bSMatthew G. Knepley     ierr = DMLabelClearValue(label, point, valA);CHKERRQ(ierr);
202918c5995bSMatthew G. Knepley     ierr = DMLabelSetValue(label, point, dim-1);CHKERRQ(ierr);
2030e1757548SMatthew G. Knepley     /* Label its closure:
2031e1757548SMatthew G. Knepley       unmarked: label as unsplit
2032e1757548SMatthew G. Knepley       incident: relabel as split
2033e1757548SMatthew G. Knepley       split:    do nothing
2034e1757548SMatthew G. Knepley     */
203518c5995bSMatthew G. Knepley     {
203618c5995bSMatthew G. Knepley       PetscInt *closure = NULL;
203718c5995bSMatthew G. Knepley       PetscInt  closureSize, cl;
203818c5995bSMatthew G. Knepley 
203918c5995bSMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
204018c5995bSMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
204118c5995bSMatthew G. Knepley         ierr = DMLabelGetValue(label, closure[cl], &valA);CHKERRQ(ierr);
20428771c1d3SMatthew G. Knepley         if (valA == -1) { /* Mark as unsplit */
204318c5995bSMatthew G. Knepley           ierr = DMLabelGetValue(depthLabel, closure[cl], &dep);CHKERRQ(ierr);
204418c5995bSMatthew G. Knepley           ierr = DMLabelSetValue(label, closure[cl], shift2+dep);CHKERRQ(ierr);
20458771c1d3SMatthew G. Knepley         } else if (((valA >= shift) && (valA < shift2)) || ((valA <= -shift) && (valA > -shift2))) {
2046e1757548SMatthew G. Knepley           ierr = DMLabelGetValue(depthLabel, closure[cl], &dep);CHKERRQ(ierr);
2047e1757548SMatthew G. Knepley           ierr = DMLabelClearValue(label, closure[cl], valA);CHKERRQ(ierr);
2048e1757548SMatthew G. Knepley           ierr = DMLabelSetValue(label, closure[cl], dep);CHKERRQ(ierr);
2049e1757548SMatthew G. Knepley         }
205018c5995bSMatthew G. Knepley       }
205118c5995bSMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
205218c5995bSMatthew G. Knepley     }
205318c5995bSMatthew G. Knepley   }
205418c5995bSMatthew G. Knepley   ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
205518c5995bSMatthew G. Knepley   ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
2056cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
2057cd0c2139SMatthew G Knepley }
2058cd0c2139SMatthew G Knepley 
2059720e594eSMatthew G. Knepley /* Check that no cell have all vertices on the fault */
2060720e594eSMatthew G. Knepley PetscErrorCode DMPlexCheckValidSubmesh_Private(DM dm, DMLabel label, DM subdm)
2061720e594eSMatthew G. Knepley {
2062720e594eSMatthew G. Knepley   IS              subpointIS;
2063720e594eSMatthew G. Knepley   const PetscInt *dmpoints;
2064720e594eSMatthew G. Knepley   PetscInt        defaultValue, cStart, cEnd, c, vStart, vEnd;
2065720e594eSMatthew G. Knepley   PetscErrorCode  ierr;
2066720e594eSMatthew G. Knepley 
2067720e594eSMatthew G. Knepley   PetscFunctionBegin;
2068720e594eSMatthew G. Knepley   if (!label) PetscFunctionReturn(0);
2069720e594eSMatthew G. Knepley   ierr = DMLabelGetDefaultValue(label, &defaultValue);CHKERRQ(ierr);
207097d8846cSMatthew Knepley   ierr = DMPlexGetSubpointIS(subdm, &subpointIS);CHKERRQ(ierr);
2071720e594eSMatthew G. Knepley   if (!subpointIS) PetscFunctionReturn(0);
2072720e594eSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(subdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
2073720e594eSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
2074720e594eSMatthew G. Knepley   ierr = ISGetIndices(subpointIS, &dmpoints);CHKERRQ(ierr);
2075720e594eSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
2076720e594eSMatthew G. Knepley     PetscBool invalidCell = PETSC_TRUE;
2077720e594eSMatthew G. Knepley     PetscInt *closure     = NULL;
2078720e594eSMatthew G. Knepley     PetscInt  closureSize, cl;
2079720e594eSMatthew G. Knepley 
2080720e594eSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2081720e594eSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
2082720e594eSMatthew G. Knepley       PetscInt value = 0;
2083720e594eSMatthew G. Knepley 
2084720e594eSMatthew G. Knepley       if ((closure[cl] < vStart) || (closure[cl] >= vEnd)) continue;
2085720e594eSMatthew G. Knepley       ierr = DMLabelGetValue(label, closure[cl], &value);CHKERRQ(ierr);
2086720e594eSMatthew G. Knepley       if (value == defaultValue) {invalidCell = PETSC_FALSE; break;}
2087720e594eSMatthew G. Knepley     }
2088720e594eSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2089720e594eSMatthew G. Knepley     if (invalidCell) {
2090720e594eSMatthew G. Knepley       ierr = ISRestoreIndices(subpointIS, &dmpoints);CHKERRQ(ierr);
2091720e594eSMatthew G. Knepley       ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
2092720e594eSMatthew G. Knepley       ierr = DMDestroy(&subdm);CHKERRQ(ierr);
209398921bdaSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ambiguous submesh. Cell %D has all of its vertices on the submesh.", dmpoints[c]);
2094720e594eSMatthew G. Knepley     }
2095720e594eSMatthew G. Knepley   }
2096720e594eSMatthew G. Knepley   ierr = ISRestoreIndices(subpointIS, &dmpoints);CHKERRQ(ierr);
2097720e594eSMatthew G. Knepley   PetscFunctionReturn(0);
2098720e594eSMatthew G. Knepley }
2099720e594eSMatthew G. Knepley 
2100c08575a3SMatthew G. Knepley /*@
21013cf72582SMatthew G. Knepley   DMPlexCreateHybridMesh - Create a mesh with hybrid cells along an internal interface
21023cf72582SMatthew G. Knepley 
21033cf72582SMatthew G. Knepley   Collective on dm
21043cf72582SMatthew G. Knepley 
21053cf72582SMatthew G. Knepley   Input Parameters:
21063cf72582SMatthew G. Knepley + dm - The original DM
2107720e594eSMatthew G. Knepley . label - The label specifying the interface vertices
2108720e594eSMatthew G. Knepley - bdlabel - The optional label specifying the interface boundary vertices
21093cf72582SMatthew G. Knepley 
21103cf72582SMatthew G. Knepley   Output Parameters:
21117db7e0a7SMatthew G. Knepley + hybridLabel - The label fully marking the interface, or NULL if no output is desired
21127db7e0a7SMatthew G. Knepley . splitLabel - The label containing the split points, or NULL if no output is desired
2113720e594eSMatthew G. Knepley . dmInterface - The new interface DM, or NULL
2114720e594eSMatthew G. Knepley - dmHybrid - The new DM with cohesive cells
21153cf72582SMatthew G. Knepley 
21166eccb800SMatthew Knepley   Note: The hybridLabel indicates what parts of the original mesh impinged on the on division surface. For points
21176eccb800SMatthew Knepley   directly on the division surface, they are labeled with their dimension, so an edge 7 on the division surface would be
21186eccb800SMatthew Knepley   7 (1) in hybridLabel. For points that impinge from the positive side, they are labeled with 100+dim, so an edge 6 with
21196eccb800SMatthew 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
21206eccb800SMatthew Knepley   surface also hits vertex 3, it would be 9 (-101) in hybridLabel.
21216eccb800SMatthew Knepley 
21226eccb800SMatthew Knepley   The splitLabel indicates what points in the new hybrid mesh were the result of splitting points in the original
21236eccb800SMatthew Knepley   mesh. The label value is +=100+dim for each point. For example, if two edges 10 and 14 in the hybrid resulting from
21246eccb800SMatthew Knepley   splitting an edge in the original mesh, you would have 10 (101) and 14 (-101) in the splitLabel.
21256eccb800SMatthew Knepley 
21266eccb800SMatthew Knepley   The dmInterface is a DM built from the original division surface. It has a label which can be retrieved using
21276eccb800SMatthew Knepley   DMPlexGetSubpointMap() which maps each point back to the point in the surface of the original mesh.
21286eccb800SMatthew Knepley 
21293cf72582SMatthew G. Knepley   Level: developer
21303cf72582SMatthew G. Knepley 
21316eccb800SMatthew Knepley .seealso: DMPlexConstructCohesiveCells(), DMPlexLabelCohesiveComplete(), DMPlexGetSubpointMap(), DMCreate()
21323cf72582SMatthew G. Knepley @*/
21337db7e0a7SMatthew G. Knepley PetscErrorCode DMPlexCreateHybridMesh(DM dm, DMLabel label, DMLabel bdlabel, DMLabel *hybridLabel, DMLabel *splitLabel, DM *dmInterface, DM *dmHybrid)
21343cf72582SMatthew G. Knepley {
21353cf72582SMatthew G. Knepley   DM             idm;
21367db7e0a7SMatthew G. Knepley   DMLabel        subpointMap, hlabel, slabel = NULL;
21373cf72582SMatthew G. Knepley   PetscInt       dim;
21383cf72582SMatthew G. Knepley   PetscErrorCode ierr;
21393cf72582SMatthew G. Knepley 
21403cf72582SMatthew G. Knepley   PetscFunctionBegin;
21413cf72582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2142720e594eSMatthew G. Knepley   if (bdlabel) PetscValidPointer(bdlabel, 3);
2143720e594eSMatthew G. Knepley   if (hybridLabel) PetscValidPointer(hybridLabel, 4);
21447db7e0a7SMatthew G. Knepley   if (splitLabel)  PetscValidPointer(splitLabel, 5);
21457db7e0a7SMatthew G. Knepley   if (dmInterface) PetscValidPointer(dmInterface, 6);
21467db7e0a7SMatthew G. Knepley   PetscValidPointer(dmHybrid, 7);
2147c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2148158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmesh(dm, label, 1, PETSC_FALSE, &idm);CHKERRQ(ierr);
2149720e594eSMatthew G. Knepley   ierr = DMPlexCheckValidSubmesh_Private(dm, label, idm);CHKERRQ(ierr);
21503cf72582SMatthew G. Knepley   ierr = DMPlexOrient(idm);CHKERRQ(ierr);
21513cf72582SMatthew G. Knepley   ierr = DMPlexGetSubpointMap(idm, &subpointMap);CHKERRQ(ierr);
21523cf72582SMatthew G. Knepley   ierr = DMLabelDuplicate(subpointMap, &hlabel);CHKERRQ(ierr);
21533cf72582SMatthew G. Knepley   ierr = DMLabelClearStratum(hlabel, dim);CHKERRQ(ierr);
21547db7e0a7SMatthew G. Knepley   if (splitLabel) {
21557db7e0a7SMatthew G. Knepley     const char *name;
21567db7e0a7SMatthew G. Knepley     char        sname[PETSC_MAX_PATH_LEN];
21577db7e0a7SMatthew G. Knepley 
2158d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) hlabel, &name);CHKERRQ(ierr);
21597db7e0a7SMatthew G. Knepley     ierr = PetscStrncpy(sname, name, PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
21607db7e0a7SMatthew G. Knepley     ierr = PetscStrcat(sname, " split");CHKERRQ(ierr);
2161d67d17b1SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, sname, &slabel);CHKERRQ(ierr);
21627db7e0a7SMatthew G. Knepley   }
2163720e594eSMatthew G. Knepley   ierr = DMPlexLabelCohesiveComplete(dm, hlabel, bdlabel, PETSC_FALSE, idm);CHKERRQ(ierr);
2164720e594eSMatthew G. Knepley   if (dmInterface) {*dmInterface = idm;}
2165720e594eSMatthew G. Knepley   else             {ierr = DMDestroy(&idm);CHKERRQ(ierr);}
21667db7e0a7SMatthew G. Knepley   ierr = DMPlexConstructCohesiveCells(dm, hlabel, slabel, dmHybrid);CHKERRQ(ierr);
2167e600fa54SMatthew G. Knepley   ierr = DMPlexCopy_Internal(dm, PETSC_TRUE, *dmHybrid);CHKERRQ(ierr);
21683cf72582SMatthew G. Knepley   if (hybridLabel) *hybridLabel = hlabel;
216953aa2e23SMatthew G. Knepley   else             {ierr = DMLabelDestroy(&hlabel);CHKERRQ(ierr);}
21707db7e0a7SMatthew G. Knepley   if (splitLabel)  *splitLabel  = slabel;
21714a7ee7d0SMatthew G. Knepley   {
21724a7ee7d0SMatthew G. Knepley     DM      cdm;
21734a7ee7d0SMatthew G. Knepley     DMLabel ctLabel;
21744a7ee7d0SMatthew G. Knepley 
21754a7ee7d0SMatthew G. Knepley     /* We need to somehow share the celltype label with the coordinate dm */
21764a7ee7d0SMatthew G. Knepley     ierr = DMGetCoordinateDM(*dmHybrid, &cdm);CHKERRQ(ierr);
21774a7ee7d0SMatthew G. Knepley     ierr = DMPlexGetCellTypeLabel(*dmHybrid, &ctLabel);CHKERRQ(ierr);
21784a7ee7d0SMatthew G. Knepley     ierr = DMSetLabel(cdm, ctLabel);CHKERRQ(ierr);
21794a7ee7d0SMatthew G. Knepley   }
2180cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
2181cd0c2139SMatthew G Knepley }
2182cd0c2139SMatthew G Knepley 
2183efa14ee0SMatthew G Knepley /* Here we need the explicit assumption that:
2184efa14ee0SMatthew G Knepley 
2185efa14ee0SMatthew G Knepley      For any marked cell, the marked vertices constitute a single face
2186efa14ee0SMatthew G Knepley */
2187830e53efSMatthew G. Knepley static PetscErrorCode DMPlexMarkSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, DM subdm)
2188efa14ee0SMatthew G Knepley {
2189fed694aaSMatthew G. Knepley   IS               subvertexIS = NULL;
2190efa14ee0SMatthew G Knepley   const PetscInt  *subvertices;
2191412e9a14SMatthew G. Knepley   PetscInt        *pStart, *pEnd, pSize;
2192efa14ee0SMatthew G Knepley   PetscInt         depth, dim, d, numSubVerticesInitial = 0, v;
2193efa14ee0SMatthew G Knepley   PetscErrorCode   ierr;
2194efa14ee0SMatthew G Knepley 
2195efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2196efa14ee0SMatthew G Knepley   *numFaces = 0;
2197efa14ee0SMatthew G Knepley   *nFV      = 0;
2198efa14ee0SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2199c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
220077d178adSMatthew G. Knepley   pSize = PetscMax(depth, dim) + 1;
2201412e9a14SMatthew G. Knepley   ierr = PetscMalloc2(pSize, &pStart, pSize, &pEnd);CHKERRQ(ierr);
2202efa14ee0SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
2203412e9a14SMatthew G. Knepley     ierr = DMPlexGetSimplexOrBoxCells(dm, depth-d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);
2204efa14ee0SMatthew G Knepley   }
2205efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
2206fed694aaSMatthew G. Knepley   if (vertexLabel) {ierr = DMLabelGetStratumIS(vertexLabel, value, &subvertexIS);CHKERRQ(ierr);}
2207efa14ee0SMatthew G Knepley   if (subvertexIS) {
2208efa14ee0SMatthew G Knepley     ierr = ISGetSize(subvertexIS, &numSubVerticesInitial);CHKERRQ(ierr);
2209efa14ee0SMatthew G Knepley     ierr = ISGetIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
2210efa14ee0SMatthew G Knepley   }
2211efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2212efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
22130298fd71SBarry Smith     PetscInt      *star   = NULL;
2214efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numCells = 0, c;
2215efa14ee0SMatthew G Knepley 
2216efa14ee0SMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2217efa14ee0SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
2218efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2219efa14ee0SMatthew G Knepley       if ((point >= pStart[depth]) && (point < pEnd[depth])) star[numCells++] = point;
2220efa14ee0SMatthew G Knepley     }
2221efa14ee0SMatthew G Knepley     for (c = 0; c < numCells; ++c) {
2222efa14ee0SMatthew G Knepley       const PetscInt cell    = star[c];
22230298fd71SBarry Smith       PetscInt      *closure = NULL;
2224efa14ee0SMatthew G Knepley       PetscInt       closureSize, cl;
2225efa14ee0SMatthew G Knepley       PetscInt       cellLoc, numCorners = 0, faceSize = 0;
2226efa14ee0SMatthew G Knepley 
2227efa14ee0SMatthew G Knepley       ierr = DMLabelGetValue(subpointMap, cell, &cellLoc);CHKERRQ(ierr);
222865560c7fSMatthew G Knepley       if (cellLoc == 2) continue;
22292c71b3e2SJacob Faibussowitsch       PetscCheckFalse(cellLoc >= 0,PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %d has dimension %d in the surface label", cell, cellLoc);
2230efa14ee0SMatthew G Knepley       ierr = DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2231efa14ee0SMatthew G Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
2232efa14ee0SMatthew G Knepley         const PetscInt point = closure[cl];
2233efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2234efa14ee0SMatthew G Knepley 
2235efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
2236efa14ee0SMatthew G Knepley           ++numCorners;
2237efa14ee0SMatthew G Knepley           ierr = DMLabelGetValue(vertexLabel, point, &vertexLoc);CHKERRQ(ierr);
2238830e53efSMatthew G. Knepley           if (vertexLoc == value) closure[faceSize++] = point;
2239efa14ee0SMatthew G Knepley         }
2240efa14ee0SMatthew G Knepley       }
224118ad9376SMatthew G. Knepley       if (!(*nFV)) {ierr = DMPlexGetNumFaceVertices(dm, dim, numCorners, nFV);CHKERRQ(ierr);}
22422c71b3e2SJacob Faibussowitsch       PetscCheckFalse(faceSize > *nFV,PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %d of an element on the surface", faceSize);
2243efa14ee0SMatthew G Knepley       if (faceSize == *nFV) {
2244007baee2SMatthew G. Knepley         const PetscInt *cells = NULL;
2245007baee2SMatthew G. Knepley         PetscInt        numCells, nc;
2246007baee2SMatthew G. Knepley 
2247efa14ee0SMatthew G Knepley         ++(*numFaces);
2248efa14ee0SMatthew G Knepley         for (cl = 0; cl < faceSize; ++cl) {
2249efa14ee0SMatthew G Knepley           ierr = DMLabelSetValue(subpointMap, closure[cl], 0);CHKERRQ(ierr);
2250efa14ee0SMatthew G Knepley         }
2251007baee2SMatthew G. Knepley         ierr = DMPlexGetJoin(dm, faceSize, closure, &numCells, &cells);CHKERRQ(ierr);
2252007baee2SMatthew G. Knepley         for (nc = 0; nc < numCells; ++nc) {
2253007baee2SMatthew G. Knepley           ierr = DMLabelSetValue(subpointMap, cells[nc], 2);CHKERRQ(ierr);
2254007baee2SMatthew G. Knepley         }
2255007baee2SMatthew G. Knepley         ierr = DMPlexRestoreJoin(dm, faceSize, closure, &numCells, &cells);CHKERRQ(ierr);
2256efa14ee0SMatthew G Knepley       }
2257efa14ee0SMatthew G Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2258efa14ee0SMatthew G Knepley     }
2259efa14ee0SMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2260efa14ee0SMatthew G Knepley   }
2261efa14ee0SMatthew G Knepley   if (subvertexIS) {
2262efa14ee0SMatthew G Knepley     ierr = ISRestoreIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
2263efa14ee0SMatthew G Knepley   }
2264efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
2265412e9a14SMatthew G. Knepley   ierr = PetscFree2(pStart, pEnd);CHKERRQ(ierr);
2266efa14ee0SMatthew G Knepley   PetscFunctionReturn(0);
2267efa14ee0SMatthew G Knepley }
2268efa14ee0SMatthew G Knepley 
2269158acfadSMatthew G. Knepley static PetscErrorCode DMPlexMarkSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DMLabel subpointMap, DM subdm)
2270efa14ee0SMatthew G Knepley {
227134b4c39eSMatthew G. Knepley   IS               subvertexIS = NULL;
2272efa14ee0SMatthew G Knepley   const PetscInt  *subvertices;
2273412e9a14SMatthew G. Knepley   PetscInt        *pStart, *pEnd;
2274efa14ee0SMatthew G Knepley   PetscInt         dim, d, numSubVerticesInitial = 0, v;
2275efa14ee0SMatthew G Knepley   PetscErrorCode   ierr;
2276efa14ee0SMatthew G Knepley 
2277efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2278c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2279412e9a14SMatthew G. Knepley   ierr = PetscMalloc2(dim+1, &pStart, dim+1, &pEnd);CHKERRQ(ierr);
2280efa14ee0SMatthew G Knepley   for (d = 0; d <= dim; ++d) {
2281412e9a14SMatthew G. Knepley     ierr = DMPlexGetSimplexOrBoxCells(dm, dim-d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);
2282efa14ee0SMatthew G Knepley   }
2283efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
228434b4c39eSMatthew G. Knepley   if (vertexLabel) {
2285830e53efSMatthew G. Knepley     ierr = DMLabelGetStratumIS(vertexLabel, value, &subvertexIS);CHKERRQ(ierr);
2286efa14ee0SMatthew G Knepley     if (subvertexIS) {
2287efa14ee0SMatthew G Knepley       ierr = ISGetSize(subvertexIS, &numSubVerticesInitial);CHKERRQ(ierr);
2288efa14ee0SMatthew G Knepley       ierr = ISGetIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
2289efa14ee0SMatthew G Knepley     }
229034b4c39eSMatthew G. Knepley   }
2291efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2292efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
22930298fd71SBarry Smith     PetscInt      *star   = NULL;
2294efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numFaces = 0, f;
2295efa14ee0SMatthew G Knepley 
2296efa14ee0SMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2297efa14ee0SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
2298efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2299158acfadSMatthew G. Knepley       PetscInt       faceLoc;
2300158acfadSMatthew G. Knepley 
2301158acfadSMatthew G. Knepley       if ((point >= pStart[dim-1]) && (point < pEnd[dim-1])) {
2302158acfadSMatthew G. Knepley         if (markedFaces) {
2303158acfadSMatthew G. Knepley           ierr = DMLabelGetValue(vertexLabel, point, &faceLoc);CHKERRQ(ierr);
2304158acfadSMatthew G. Knepley           if (faceLoc < 0) continue;
2305158acfadSMatthew G. Knepley         }
2306158acfadSMatthew G. Knepley         star[numFaces++] = point;
2307158acfadSMatthew G. Knepley       }
2308efa14ee0SMatthew G Knepley     }
2309efa14ee0SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
2310efa14ee0SMatthew G Knepley       const PetscInt face    = star[f];
23110298fd71SBarry Smith       PetscInt      *closure = NULL;
2312efa14ee0SMatthew G Knepley       PetscInt       closureSize, c;
2313efa14ee0SMatthew G Knepley       PetscInt       faceLoc;
2314efa14ee0SMatthew G Knepley 
2315efa14ee0SMatthew G Knepley       ierr = DMLabelGetValue(subpointMap, face, &faceLoc);CHKERRQ(ierr);
2316efa14ee0SMatthew G Knepley       if (faceLoc == dim-1) continue;
23172c71b3e2SJacob Faibussowitsch       PetscCheckFalse(faceLoc >= 0,PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %d has dimension %d in the surface label", face, faceLoc);
2318efa14ee0SMatthew G Knepley       ierr = DMPlexGetTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2319efa14ee0SMatthew G Knepley       for (c = 0; c < closureSize*2; c += 2) {
2320efa14ee0SMatthew G Knepley         const PetscInt point = closure[c];
2321efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2322efa14ee0SMatthew G Knepley 
2323efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
2324efa14ee0SMatthew G Knepley           ierr = DMLabelGetValue(vertexLabel, point, &vertexLoc);CHKERRQ(ierr);
2325830e53efSMatthew G. Knepley           if (vertexLoc != value) break;
2326efa14ee0SMatthew G Knepley         }
2327efa14ee0SMatthew G Knepley       }
2328efa14ee0SMatthew G Knepley       if (c == closureSize*2) {
2329efa14ee0SMatthew G Knepley         const PetscInt *support;
2330efa14ee0SMatthew G Knepley         PetscInt        supportSize, s;
2331efa14ee0SMatthew G Knepley 
2332efa14ee0SMatthew G Knepley         for (c = 0; c < closureSize*2; c += 2) {
2333efa14ee0SMatthew G Knepley           const PetscInt point = closure[c];
2334efa14ee0SMatthew G Knepley 
2335efa14ee0SMatthew G Knepley           for (d = 0; d < dim; ++d) {
2336efa14ee0SMatthew G Knepley             if ((point >= pStart[d]) && (point < pEnd[d])) {
2337efa14ee0SMatthew G Knepley               ierr = DMLabelSetValue(subpointMap, point, d);CHKERRQ(ierr);
2338efa14ee0SMatthew G Knepley               break;
2339efa14ee0SMatthew G Knepley             }
2340efa14ee0SMatthew G Knepley           }
2341efa14ee0SMatthew G Knepley         }
2342efa14ee0SMatthew G Knepley         ierr = DMPlexGetSupportSize(dm, face, &supportSize);CHKERRQ(ierr);
2343efa14ee0SMatthew G Knepley         ierr = DMPlexGetSupport(dm, face, &support);CHKERRQ(ierr);
2344efa14ee0SMatthew G Knepley         for (s = 0; s < supportSize; ++s) {
2345efa14ee0SMatthew G Knepley           ierr = DMLabelSetValue(subpointMap, support[s], dim);CHKERRQ(ierr);
2346efa14ee0SMatthew G Knepley         }
2347efa14ee0SMatthew G Knepley       }
2348efa14ee0SMatthew G Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2349efa14ee0SMatthew G Knepley     }
2350efa14ee0SMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2351efa14ee0SMatthew G Knepley   }
235234b4c39eSMatthew G. Knepley   if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subvertices);CHKERRQ(ierr);}
2353efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
2354412e9a14SMatthew G. Knepley   ierr = PetscFree2(pStart, pEnd);CHKERRQ(ierr);
2355efa14ee0SMatthew G Knepley   PetscFunctionReturn(0);
2356efa14ee0SMatthew G Knepley }
2357efa14ee0SMatthew G Knepley 
235827c04023SMatthew 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)
2359766ab985SMatthew G. Knepley {
236027c04023SMatthew G. Knepley   DMLabel         label = NULL;
2361766ab985SMatthew G. Knepley   const PetscInt *cone;
23629fc93327SToby Isaac   PetscInt        dim, cMax, cEnd, c, subc = 0, p, coneSize = -1;
2363766ab985SMatthew G. Knepley   PetscErrorCode  ierr;
2364766ab985SMatthew G. Knepley 
2365812bfc34SJed Brown   PetscFunctionBegin;
2366c0ed958bSJed Brown   *numFaces = 0;
2367c0ed958bSJed Brown   *nFV = 0;
2368c58f1c22SToby Isaac   if (labelname) {ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);}
2369fed694aaSMatthew G. Knepley   *subCells = NULL;
2370c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2371412e9a14SMatthew G. Knepley   ierr = DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd);CHKERRQ(ierr);
2372766ab985SMatthew G. Knepley   if (cMax < 0) PetscFunctionReturn(0);
237327c04023SMatthew G. Knepley   if (label) {
237427c04023SMatthew G. Knepley     for (c = cMax; c < cEnd; ++c) {
237527c04023SMatthew G. Knepley       PetscInt val;
237627c04023SMatthew G. Knepley 
237727c04023SMatthew G. Knepley       ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr);
237827c04023SMatthew G. Knepley       if (val == value) {
237927c04023SMatthew G. Knepley         ++(*numFaces);
238027c04023SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
238127c04023SMatthew G. Knepley       }
238227c04023SMatthew G. Knepley     }
238327c04023SMatthew G. Knepley   } else {
2384766ab985SMatthew G. Knepley     *numFaces = cEnd - cMax;
238527c04023SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cMax, &coneSize);CHKERRQ(ierr);
238627c04023SMatthew G. Knepley   }
2387785e854fSJed Brown   ierr = PetscMalloc1(*numFaces *2, subCells);CHKERRQ(ierr);
23889fc93327SToby Isaac   if (!(*numFaces)) PetscFunctionReturn(0);
23899fc93327SToby Isaac   *nFV = hasLagrange ? coneSize/3 : coneSize/2;
2390766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2391766ab985SMatthew G. Knepley     const PetscInt *cells;
2392766ab985SMatthew G. Knepley     PetscInt        numCells;
2393766ab985SMatthew G. Knepley 
239427c04023SMatthew G. Knepley     if (label) {
239527c04023SMatthew G. Knepley       PetscInt val;
239627c04023SMatthew G. Knepley 
239727c04023SMatthew G. Knepley       ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr);
239827c04023SMatthew G. Knepley       if (val != value) continue;
239927c04023SMatthew G. Knepley     }
2400766ab985SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
2401766ab985SMatthew G. Knepley     for (p = 0; p < *nFV; ++p) {
2402766ab985SMatthew G. Knepley       ierr = DMLabelSetValue(subpointMap, cone[p], 0);CHKERRQ(ierr);
2403766ab985SMatthew G. Knepley     }
2404766ab985SMatthew G. Knepley     /* Negative face */
2405766ab985SMatthew G. Knepley     ierr = DMPlexGetJoin(dm, *nFV, cone, &numCells, &cells);CHKERRQ(ierr);
240627234c99SMatthew G. Knepley     /* Not true in parallel
24072c71b3e2SJacob Faibussowitsch     PetscCheckFalse(numCells != 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
2408766ab985SMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
2409766ab985SMatthew G. Knepley       ierr = DMLabelSetValue(subpointMap, cells[p], 2);CHKERRQ(ierr);
241027234c99SMatthew G. Knepley       (*subCells)[subc++] = cells[p];
2411766ab985SMatthew G. Knepley     }
2412766ab985SMatthew G. Knepley     ierr = DMPlexRestoreJoin(dm, *nFV, cone, &numCells, &cells);CHKERRQ(ierr);
2413766ab985SMatthew G. Knepley     /* Positive face is not included */
2414766ab985SMatthew G. Knepley   }
2415766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
2416766ab985SMatthew G. Knepley }
2417766ab985SMatthew G. Knepley 
24183982b651SMatthew G. Knepley static PetscErrorCode DMPlexMarkCohesiveSubmesh_Interpolated(DM dm, DMLabel label, PetscInt value, DMLabel subpointMap, DM subdm)
2419766ab985SMatthew G. Knepley {
2420766ab985SMatthew G. Knepley   PetscInt      *pStart, *pEnd;
2421766ab985SMatthew G. Knepley   PetscInt       dim, cMax, cEnd, c, d;
2422766ab985SMatthew G. Knepley   PetscErrorCode ierr;
2423766ab985SMatthew G. Knepley 
2424812bfc34SJed Brown   PetscFunctionBegin;
2425c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2426412e9a14SMatthew G. Knepley   ierr = DMPlexGetTensorPrismBounds_Internal(dm, dim, &cMax, &cEnd);CHKERRQ(ierr);
2427766ab985SMatthew G. Knepley   if (cMax < 0) PetscFunctionReturn(0);
2428dcca6d9dSJed Brown   ierr = PetscMalloc2(dim+1,&pStart,dim+1,&pEnd);CHKERRQ(ierr);
2429b3154360SMatthew G. Knepley   for (d = 0; d <= dim; ++d) {ierr = DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);}
2430766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2431766ab985SMatthew G. Knepley     const PetscInt *cone;
2432766ab985SMatthew G. Knepley     PetscInt       *closure = NULL;
2433b3154360SMatthew G. Knepley     PetscInt        fconeSize, coneSize, closureSize, cl, val;
2434766ab985SMatthew G. Knepley 
243527c04023SMatthew G. Knepley     if (label) {
243627c04023SMatthew G. Knepley       ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr);
243727c04023SMatthew G. Knepley       if (val != value) continue;
243827c04023SMatthew G. Knepley     }
2439766ab985SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
2440766ab985SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
2441b3154360SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cone[0], &fconeSize);CHKERRQ(ierr);
24422c71b3e2SJacob Faibussowitsch     PetscCheckFalse(coneSize != (fconeSize ? fconeSize : 1) + 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells");
2443b3154360SMatthew G. Knepley     /* Negative face */
2444766ab985SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2445766ab985SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
2446766ab985SMatthew G. Knepley       const PetscInt point = closure[cl];
2447766ab985SMatthew G. Knepley 
2448766ab985SMatthew G. Knepley       for (d = 0; d <= dim; ++d) {
2449766ab985SMatthew G. Knepley         if ((point >= pStart[d]) && (point < pEnd[d])) {
2450766ab985SMatthew G. Knepley           ierr = DMLabelSetValue(subpointMap, point, d);CHKERRQ(ierr);
2451766ab985SMatthew G. Knepley           break;
2452766ab985SMatthew G. Knepley         }
2453766ab985SMatthew G. Knepley       }
2454766ab985SMatthew G. Knepley     }
2455766ab985SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2456766ab985SMatthew G. Knepley     /* Cells -- positive face is not included */
2457766ab985SMatthew G. Knepley     for (cl = 0; cl < 1; ++cl) {
2458766ab985SMatthew G. Knepley       const PetscInt *support;
2459766ab985SMatthew G. Knepley       PetscInt        supportSize, s;
2460766ab985SMatthew G. Knepley 
2461766ab985SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[cl], &supportSize);CHKERRQ(ierr);
24622c71b3e2SJacob Faibussowitsch       /* PetscCheckFalse(supportSize != 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive faces should separate two cells"); */
2463766ab985SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[cl], &support);CHKERRQ(ierr);
2464766ab985SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
2465766ab985SMatthew G. Knepley         ierr = DMLabelSetValue(subpointMap, support[s], dim);CHKERRQ(ierr);
2466766ab985SMatthew G. Knepley       }
2467766ab985SMatthew G. Knepley     }
2468766ab985SMatthew G. Knepley   }
2469766ab985SMatthew G. Knepley   ierr = PetscFree2(pStart, pEnd);CHKERRQ(ierr);
2470766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
2471766ab985SMatthew G. Knepley }
2472766ab985SMatthew G. Knepley 
2473c08575a3SMatthew G. Knepley static PetscErrorCode DMPlexGetFaceOrientation(DM dm, PetscInt cell, PetscInt numCorners, PetscInt indices[], PetscInt oppositeVertex, PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2474e6ccafaeSMatthew G Knepley {
247582f516ccSBarry Smith   MPI_Comm       comm;
2476e6ccafaeSMatthew G Knepley   PetscBool      posOrient = PETSC_FALSE;
2477e6ccafaeSMatthew G Knepley   const PetscInt debug     = 0;
2478e6ccafaeSMatthew G Knepley   PetscInt       cellDim, faceSize, f;
2479e6ccafaeSMatthew G Knepley   PetscErrorCode ierr;
2480e6ccafaeSMatthew G Knepley 
248182f516ccSBarry Smith   PetscFunctionBegin;
248282f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2483c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &cellDim);CHKERRQ(ierr);
2484367003a6SStefano Zampini   if (debug) {ierr = PetscPrintf(comm, "cellDim: %d numCorners: %d\n", cellDim, numCorners);CHKERRQ(ierr);}
2485e6ccafaeSMatthew G Knepley 
2486ddeab2a6SMatthew G. Knepley   if (cellDim == 1 && numCorners == 2) {
2487ddeab2a6SMatthew G. Knepley     /* Triangle */
2488e6ccafaeSMatthew G Knepley     faceSize  = numCorners-1;
2489e6ccafaeSMatthew G Knepley     posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2490ddeab2a6SMatthew G. Knepley   } else if (cellDim == 2 && numCorners == 3) {
2491ddeab2a6SMatthew G. Knepley     /* Triangle */
2492ddeab2a6SMatthew G. Knepley     faceSize  = numCorners-1;
2493ddeab2a6SMatthew G. Knepley     posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2494ddeab2a6SMatthew G. Knepley   } else if (cellDim == 3 && numCorners == 4) {
2495ddeab2a6SMatthew G. Knepley     /* Tetrahedron */
2496ddeab2a6SMatthew G. Knepley     faceSize  = numCorners-1;
2497ddeab2a6SMatthew G. Knepley     posOrient = (oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2498e6ccafaeSMatthew G Knepley   } else if (cellDim == 1 && numCorners == 3) {
2499e6ccafaeSMatthew G Knepley     /* Quadratic line */
2500e6ccafaeSMatthew G Knepley     faceSize  = 1;
2501e6ccafaeSMatthew G Knepley     posOrient = PETSC_TRUE;
2502e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 4) {
2503e6ccafaeSMatthew G Knepley     /* Quads */
2504e6ccafaeSMatthew G Knepley     faceSize = 2;
2505e6ccafaeSMatthew G Knepley     if ((indices[1] > indices[0]) && (indices[1] - indices[0] == 1)) {
2506e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2507e6ccafaeSMatthew G Knepley     } else if ((indices[0] == 3) && (indices[1] == 0)) {
2508e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2509e6ccafaeSMatthew G Knepley     } else {
2510e6ccafaeSMatthew G Knepley       if (((indices[0] > indices[1]) && (indices[0] - indices[1] == 1)) || ((indices[0] == 0) && (indices[1] == 3))) {
2511e6ccafaeSMatthew G Knepley         posOrient = PETSC_FALSE;
2512e6ccafaeSMatthew G Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossedge");
2513e6ccafaeSMatthew G Knepley     }
2514e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 6) {
2515e6ccafaeSMatthew G Knepley     /* Quadratic triangle (I hate this) */
2516e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2517e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTri = 3;
2518e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2519e6ccafaeSMatthew G Knepley     PetscBool      found                    = PETSC_FALSE;
2520e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTriSorted[9] = {
2521e6ccafaeSMatthew G Knepley       0, 3,  4, /* bottom */
2522e6ccafaeSMatthew G Knepley       1, 4,  5, /* right */
2523e6ccafaeSMatthew G Knepley       2, 3,  5, /* left */
2524e6ccafaeSMatthew G Knepley     };
2525e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTri[9] = {
2526e6ccafaeSMatthew G Knepley       0, 3,  4, /* bottom */
2527e6ccafaeSMatthew G Knepley       1, 4,  5, /* right */
2528e6ccafaeSMatthew G Knepley       2, 5,  3, /* left */
2529e6ccafaeSMatthew G Knepley     };
2530e6ccafaeSMatthew G Knepley 
2531e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTri; ++i) sortedIndices[i] = indices[i];
2532e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeTri, sortedIndices);CHKERRQ(ierr);
2533e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 3; ++iFace) {
2534e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeTri;
2535e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2536e6ccafaeSMatthew G Knepley 
2537e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesTriSorted[ii+0]) &&
2538e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesTriSorted[ii+1])) {
2539e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTri; ++fVertex) {
2540e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTri; ++cVertex) {
2541e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTri[ii+fVertex]) {
2542e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2543e6ccafaeSMatthew G Knepley               break;
2544e6ccafaeSMatthew G Knepley             }
2545e6ccafaeSMatthew G Knepley           }
2546e6ccafaeSMatthew G Knepley         }
2547e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2548e6ccafaeSMatthew G Knepley         break;
2549e6ccafaeSMatthew G Knepley       }
2550e6ccafaeSMatthew G Knepley     }
25512c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!found,comm, PETSC_ERR_ARG_WRONG, "Invalid tri crossface");
2552e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2553e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2554e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 9) {
2555e6ccafaeSMatthew G Knepley     /* Quadratic quad (I hate this) */
2556e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2557e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuad = 3;
2558e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2559e6ccafaeSMatthew G Knepley     PetscBool      found                      = PETSC_FALSE;
2560e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadSorted[12] = {
2561e6ccafaeSMatthew G Knepley       0, 1,  4, /* bottom */
2562e6ccafaeSMatthew G Knepley       1, 2,  5, /* right */
2563e6ccafaeSMatthew G Knepley       2, 3,  6, /* top */
2564e6ccafaeSMatthew G Knepley       0, 3,  7, /* left */
2565e6ccafaeSMatthew G Knepley     };
2566e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuad[12] = {
2567e6ccafaeSMatthew G Knepley       0, 1,  4, /* bottom */
2568e6ccafaeSMatthew G Knepley       1, 2,  5, /* right */
2569e6ccafaeSMatthew G Knepley       2, 3,  6, /* top */
2570e6ccafaeSMatthew G Knepley       3, 0,  7, /* left */
2571e6ccafaeSMatthew G Knepley     };
2572e6ccafaeSMatthew G Knepley 
2573e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuad; ++i) sortedIndices[i] = indices[i];
2574e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeQuad, sortedIndices);CHKERRQ(ierr);
2575e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2576e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeQuad;
2577e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2578e6ccafaeSMatthew G Knepley 
2579e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesQuadSorted[ii+0]) &&
2580e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesQuadSorted[ii+1])) {
2581e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuad; ++fVertex) {
2582e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuad; ++cVertex) {
2583e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuad[ii+fVertex]) {
2584e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2585e6ccafaeSMatthew G Knepley               break;
2586e6ccafaeSMatthew G Knepley             }
2587e6ccafaeSMatthew G Knepley           }
2588e6ccafaeSMatthew G Knepley         }
2589e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2590e6ccafaeSMatthew G Knepley         break;
2591e6ccafaeSMatthew G Knepley       }
2592e6ccafaeSMatthew G Knepley     }
25932c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!found,comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossface");
2594e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2595e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2596e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 8) {
2597e6ccafaeSMatthew G Knepley     /* Hexes
2598e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2599e6ccafaeSMatthew G Knepley        pointing up at the second.
2600e6ccafaeSMatthew G Knepley 
2601e6ccafaeSMatthew G Knepley           7---6
2602e6ccafaeSMatthew G Knepley          /|  /|
2603e6ccafaeSMatthew G Knepley         4---5 |
2604ddeab2a6SMatthew G. Knepley         | 1-|-2
2605e6ccafaeSMatthew G Knepley         |/  |/
2606ddeab2a6SMatthew G. Knepley         0---3
2607e6ccafaeSMatthew G Knepley 
2608e6ccafaeSMatthew G Knepley         Faces are determined by the first 4 vertices (corners of faces) */
2609e6ccafaeSMatthew G Knepley     const PetscInt faceSizeHex = 4;
2610e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[4], i, iFace;
2611e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2612e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHexSorted[24] = {
2613e6ccafaeSMatthew G Knepley       0, 1, 2, 3,  /* bottom */
2614e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  /* top */
2615ddeab2a6SMatthew G. Knepley       0, 3, 4, 5,  /* front */
2616ddeab2a6SMatthew G. Knepley       2, 3, 5, 6,  /* right */
2617ddeab2a6SMatthew G. Knepley       1, 2, 6, 7,  /* back */
2618ddeab2a6SMatthew G. Knepley       0, 1, 4, 7,  /* left */
2619e6ccafaeSMatthew G Knepley     };
2620e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHex[24] = {
2621ddeab2a6SMatthew G. Knepley       1, 2, 3, 0,  /* bottom */
2622e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  /* top */
2623ddeab2a6SMatthew G. Knepley       0, 3, 5, 4,  /* front */
2624ddeab2a6SMatthew G. Knepley       3, 2, 6, 5,  /* right */
2625ddeab2a6SMatthew G. Knepley       2, 1, 7, 6,  /* back */
2626ddeab2a6SMatthew G. Knepley       1, 0, 4, 7,  /* left */
2627e6ccafaeSMatthew G Knepley     };
2628e6ccafaeSMatthew G Knepley 
2629e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeHex; ++i) sortedIndices[i] = indices[i];
2630e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeHex, sortedIndices);CHKERRQ(ierr);
2631e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2632e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeHex;
2633e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2634e6ccafaeSMatthew G Knepley 
2635e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesHexSorted[ii+0]) &&
2636e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesHexSorted[ii+1]) &&
2637e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesHexSorted[ii+2]) &&
2638e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesHexSorted[ii+3])) {
2639e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeHex; ++fVertex) {
2640e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeHex; ++cVertex) {
2641e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesHex[ii+fVertex]) {
2642e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2643e6ccafaeSMatthew G Knepley               break;
2644e6ccafaeSMatthew G Knepley             }
2645e6ccafaeSMatthew G Knepley           }
2646e6ccafaeSMatthew G Knepley         }
2647e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2648e6ccafaeSMatthew G Knepley         break;
2649e6ccafaeSMatthew G Knepley       }
2650e6ccafaeSMatthew G Knepley     }
26512c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!found,comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2652e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2653e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2654e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 10) {
2655e6ccafaeSMatthew G Knepley     /* Quadratic tet */
2656e6ccafaeSMatthew G Knepley     /* Faces are determined by the first 3 vertices (corners of faces) */
2657e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTet = 6;
2658e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[6], i, iFace;
2659e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2660e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTetSorted[24] = {
2661e6ccafaeSMatthew G Knepley       0, 1, 2,  6, 7, 8, /* bottom */
2662e6ccafaeSMatthew G Knepley       0, 3, 4,  6, 7, 9,  /* front */
2663e6ccafaeSMatthew G Knepley       1, 4, 5,  7, 8, 9,  /* right */
2664e6ccafaeSMatthew G Knepley       2, 3, 5,  6, 8, 9,  /* left */
2665e6ccafaeSMatthew G Knepley     };
2666e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTet[24] = {
2667e6ccafaeSMatthew G Knepley       0, 1, 2,  6, 7, 8, /* bottom */
2668e6ccafaeSMatthew G Knepley       0, 4, 3,  6, 7, 9,  /* front */
2669e6ccafaeSMatthew G Knepley       1, 5, 4,  7, 8, 9,  /* right */
2670e6ccafaeSMatthew G Knepley       2, 3, 5,  8, 6, 9,  /* left */
2671e6ccafaeSMatthew G Knepley     };
2672e6ccafaeSMatthew G Knepley 
2673e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTet; ++i) sortedIndices[i] = indices[i];
2674e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeTet, sortedIndices);CHKERRQ(ierr);
2675e6ccafaeSMatthew G Knepley     for (iFace=0; iFace < 4; ++iFace) {
2676e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeTet;
2677e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2678e6ccafaeSMatthew G Knepley 
2679e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesTetSorted[ii+0]) &&
2680e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesTetSorted[ii+1]) &&
2681e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesTetSorted[ii+2]) &&
2682e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesTetSorted[ii+3])) {
2683e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTet; ++fVertex) {
2684e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTet; ++cVertex) {
2685e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTet[ii+fVertex]) {
2686e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2687e6ccafaeSMatthew G Knepley               break;
2688e6ccafaeSMatthew G Knepley             }
2689e6ccafaeSMatthew G Knepley           }
2690e6ccafaeSMatthew G Knepley         }
2691e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2692e6ccafaeSMatthew G Knepley         break;
2693e6ccafaeSMatthew G Knepley       }
2694e6ccafaeSMatthew G Knepley     }
26952c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!found,comm, PETSC_ERR_ARG_WRONG, "Invalid tet crossface");
2696e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2697e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2698e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 27) {
2699e6ccafaeSMatthew G Knepley     /* Quadratic hexes (I hate this)
2700e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2701e6ccafaeSMatthew G Knepley        pointing up at the second.
2702e6ccafaeSMatthew G Knepley 
2703e6ccafaeSMatthew G Knepley          7---6
2704e6ccafaeSMatthew G Knepley         /|  /|
2705e6ccafaeSMatthew G Knepley        4---5 |
2706e6ccafaeSMatthew G Knepley        | 3-|-2
2707e6ccafaeSMatthew G Knepley        |/  |/
2708e6ccafaeSMatthew G Knepley        0---1
2709e6ccafaeSMatthew G Knepley 
2710e6ccafaeSMatthew G Knepley        Faces are determined by the first 4 vertices (corners of faces) */
2711e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuadHex = 9;
2712e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[9], i, iFace;
2713e6ccafaeSMatthew G Knepley     PetscBool      found                         = PETSC_FALSE;
2714e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHexSorted[54] = {
2715e6ccafaeSMatthew G Knepley       0, 1, 2, 3,  8, 9, 10, 11,  24, /* bottom */
2716e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  12, 13, 14, 15,  25, /* top */
2717e6ccafaeSMatthew G Knepley       0, 1, 4, 5,  8, 12, 16, 17,  22, /* front */
2718e6ccafaeSMatthew G Knepley       1, 2, 5, 6,  9, 13, 17, 18,  21, /* right */
2719e6ccafaeSMatthew G Knepley       2, 3, 6, 7,  10, 14, 18, 19,  23, /* back */
2720e6ccafaeSMatthew G Knepley       0, 3, 4, 7,  11, 15, 16, 19,  20, /* left */
2721e6ccafaeSMatthew G Knepley     };
2722e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHex[54] = {
2723e6ccafaeSMatthew G Knepley       3, 2, 1, 0,  10, 9, 8, 11,  24, /* bottom */
2724e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  12, 13, 14, 15,  25, /* top */
2725e6ccafaeSMatthew G Knepley       0, 1, 5, 4,  8, 17, 12, 16,  22, /* front */
2726e6ccafaeSMatthew G Knepley       1, 2, 6, 5,  9, 18, 13, 17,  21, /* right */
2727e6ccafaeSMatthew G Knepley       2, 3, 7, 6,  10, 19, 14, 18,  23, /* back */
2728e6ccafaeSMatthew G Knepley       3, 0, 4, 7,  11, 16, 15, 19,  20 /* left */
2729e6ccafaeSMatthew G Knepley     };
2730e6ccafaeSMatthew G Knepley 
2731e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuadHex; ++i) sortedIndices[i] = indices[i];
2732e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeQuadHex, sortedIndices);CHKERRQ(ierr);
2733e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2734e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeQuadHex;
2735e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2736e6ccafaeSMatthew G Knepley 
2737e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesQuadHexSorted[ii+0]) &&
2738e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesQuadHexSorted[ii+1]) &&
2739e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesQuadHexSorted[ii+2]) &&
2740e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesQuadHexSorted[ii+3])) {
2741e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuadHex; ++fVertex) {
2742e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuadHex; ++cVertex) {
2743e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuadHex[ii+fVertex]) {
2744e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2745e6ccafaeSMatthew G Knepley               break;
2746e6ccafaeSMatthew G Knepley             }
2747e6ccafaeSMatthew G Knepley           }
2748e6ccafaeSMatthew G Knepley         }
2749e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2750e6ccafaeSMatthew G Knepley         break;
2751e6ccafaeSMatthew G Knepley       }
2752e6ccafaeSMatthew G Knepley     }
27532c71b3e2SJacob Faibussowitsch     PetscCheckFalse(!found,comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2754e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2755e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2756e6ccafaeSMatthew G Knepley   } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Unknown cell type for faceOrientation().");
2757e6ccafaeSMatthew G Knepley   if (!posOrient) {
2758e6ccafaeSMatthew G Knepley     if (debug) {ierr = PetscPrintf(comm, "  Reversing initial face orientation\n");CHKERRQ(ierr);}
2759e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[faceSize-1 - f];
2760e6ccafaeSMatthew G Knepley   } else {
2761e6ccafaeSMatthew G Knepley     if (debug) {ierr = PetscPrintf(comm, "  Keeping initial face orientation\n");CHKERRQ(ierr);}
2762e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[f];
2763e6ccafaeSMatthew G Knepley   }
2764e6ccafaeSMatthew G Knepley   if (posOriented) *posOriented = posOrient;
2765e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2766e6ccafaeSMatthew G Knepley }
2767e6ccafaeSMatthew G Knepley 
2768c08575a3SMatthew G. Knepley /*@
2769c08575a3SMatthew G. Knepley   DMPlexGetOrientedFace - Given a cell and a face, as a set of vertices, return the oriented face, as a set of vertices,
2770c08575a3SMatthew G. Knepley   in faceVertices. The orientation is such that the face normal points out of the cell
2771c08575a3SMatthew G. Knepley 
2772c08575a3SMatthew G. Knepley   Not collective
2773c08575a3SMatthew G. Knepley 
2774c08575a3SMatthew G. Knepley   Input Parameters:
2775c08575a3SMatthew G. Knepley + dm           - The original mesh
2776c08575a3SMatthew G. Knepley . cell         - The cell mesh point
2777c08575a3SMatthew G. Knepley . faceSize     - The number of vertices on the face
2778c08575a3SMatthew G. Knepley . face         - The face vertices
2779c08575a3SMatthew G. Knepley . numCorners   - The number of vertices on the cell
2780c08575a3SMatthew G. Knepley . indices      - Local numbering of face vertices in cell cone
2781c08575a3SMatthew G. Knepley - origVertices - Original face vertices
2782c08575a3SMatthew G. Knepley 
2783d8d19677SJose E. Roman   Output Parameters:
2784c08575a3SMatthew G. Knepley + faceVertices - The face vertices properly oriented
2785c08575a3SMatthew G. Knepley - posOriented  - PETSC_TRUE if the face was oriented with outward normal
2786c08575a3SMatthew G. Knepley 
2787c08575a3SMatthew G. Knepley   Level: developer
2788c08575a3SMatthew G. Knepley 
2789c08575a3SMatthew G. Knepley .seealso: DMPlexGetCone()
2790c08575a3SMatthew G. Knepley @*/
2791e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexGetOrientedFace(DM dm, PetscInt cell, PetscInt faceSize, const PetscInt face[], PetscInt numCorners, PetscInt indices[], PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2792e6ccafaeSMatthew G Knepley {
27930298fd71SBarry Smith   const PetscInt *cone = NULL;
2794e6ccafaeSMatthew G Knepley   PetscInt        coneSize, v, f, v2;
2795e6ccafaeSMatthew G Knepley   PetscInt        oppositeVertex = -1;
2796e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
2797e6ccafaeSMatthew G Knepley 
2798e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
2799e6ccafaeSMatthew G Knepley   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
2800e6ccafaeSMatthew G Knepley   ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
2801e6ccafaeSMatthew G Knepley   for (v = 0, v2 = 0; v < coneSize; ++v) {
2802e6ccafaeSMatthew G Knepley     PetscBool found = PETSC_FALSE;
2803e6ccafaeSMatthew G Knepley 
2804e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) {
2805e6ccafaeSMatthew G Knepley       if (face[f] == cone[v]) {
2806e6ccafaeSMatthew G Knepley         found = PETSC_TRUE; break;
2807e6ccafaeSMatthew G Knepley       }
2808e6ccafaeSMatthew G Knepley     }
2809e6ccafaeSMatthew G Knepley     if (found) {
2810e6ccafaeSMatthew G Knepley       indices[v2]      = v;
2811e6ccafaeSMatthew G Knepley       origVertices[v2] = cone[v];
2812e6ccafaeSMatthew G Knepley       ++v2;
2813e6ccafaeSMatthew G Knepley     } else {
2814e6ccafaeSMatthew G Knepley       oppositeVertex = v;
2815e6ccafaeSMatthew G Knepley     }
2816e6ccafaeSMatthew G Knepley   }
2817e6ccafaeSMatthew G Knepley   ierr = DMPlexGetFaceOrientation(dm, cell, numCorners, indices, oppositeVertex, origVertices, faceVertices, posOriented);CHKERRQ(ierr);
2818e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2819e6ccafaeSMatthew G Knepley }
2820e6ccafaeSMatthew G Knepley 
2821e6ccafaeSMatthew G Knepley /*
2822cd0c2139SMatthew G Knepley   DMPlexInsertFace_Internal - Puts a face into the mesh
2823e6ccafaeSMatthew G Knepley 
2824e6ccafaeSMatthew G Knepley   Not collective
2825e6ccafaeSMatthew G Knepley 
2826e6ccafaeSMatthew G Knepley   Input Parameters:
2827e6ccafaeSMatthew G Knepley   + dm              - The DMPlex
2828e6ccafaeSMatthew G Knepley   . numFaceVertex   - The number of vertices in the face
2829e6ccafaeSMatthew G Knepley   . faceVertices    - The vertices in the face for dm
2830e6ccafaeSMatthew G Knepley   . subfaceVertices - The vertices in the face for subdm
2831e6ccafaeSMatthew G Knepley   . numCorners      - The number of vertices in the cell
2832e6ccafaeSMatthew G Knepley   . cell            - A cell in dm containing the face
2833e6ccafaeSMatthew G Knepley   . subcell         - A cell in subdm containing the face
2834e6ccafaeSMatthew G Knepley   . firstFace       - First face in the mesh
2835e6ccafaeSMatthew G Knepley   - newFacePoint    - Next face in the mesh
2836e6ccafaeSMatthew G Knepley 
2837e6ccafaeSMatthew G Knepley   Output Parameters:
2838e6ccafaeSMatthew G Knepley   . newFacePoint - Contains next face point number on input, updated on output
2839e6ccafaeSMatthew G Knepley 
2840e6ccafaeSMatthew G Knepley   Level: developer
2841e6ccafaeSMatthew G Knepley */
2842cd0c2139SMatthew 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)
2843e6ccafaeSMatthew G Knepley {
284482f516ccSBarry Smith   MPI_Comm        comm;
2845e6ccafaeSMatthew G Knepley   DM_Plex        *submesh = (DM_Plex*) subdm->data;
2846e6ccafaeSMatthew G Knepley   const PetscInt *faces;
2847e6ccafaeSMatthew G Knepley   PetscInt        numFaces, coneSize;
2848e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
2849e6ccafaeSMatthew G Knepley 
2850e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
285182f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2852e6ccafaeSMatthew G Knepley   ierr = DMPlexGetConeSize(subdm, subcell, &coneSize);CHKERRQ(ierr);
28532c71b3e2SJacob Faibussowitsch   PetscCheckFalse(coneSize != 1,comm, PETSC_ERR_ARG_OUTOFRANGE, "Cone size of cell %d is %d != 1", cell, coneSize);
2854e6ccafaeSMatthew G Knepley #if 0
2855e6ccafaeSMatthew G Knepley   /* Cannot use this because support() has not been constructed yet */
2856e6ccafaeSMatthew G Knepley   ierr = DMPlexGetJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces);CHKERRQ(ierr);
2857e6ccafaeSMatthew G Knepley #else
2858e6ccafaeSMatthew G Knepley   {
2859e6ccafaeSMatthew G Knepley     PetscInt f;
2860e6ccafaeSMatthew G Knepley 
2861e6ccafaeSMatthew G Knepley     numFaces = 0;
286269291d52SBarry Smith     ierr     = DMGetWorkArray(subdm, 1, MPIU_INT, (void **) &faces);CHKERRQ(ierr);
2863e6ccafaeSMatthew G Knepley     for (f = firstFace; f < *newFacePoint; ++f) {
2864e6ccafaeSMatthew G Knepley       PetscInt dof, off, d;
2865e6ccafaeSMatthew G Knepley 
2866e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(submesh->coneSection, f, &dof);CHKERRQ(ierr);
2867e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetOffset(submesh->coneSection, f, &off);CHKERRQ(ierr);
2868e6ccafaeSMatthew G Knepley       /* Yes, I know this is quadratic, but I expect the sizes to be <5 */
2869e6ccafaeSMatthew G Knepley       for (d = 0; d < dof; ++d) {
2870e6ccafaeSMatthew G Knepley         const PetscInt p = submesh->cones[off+d];
2871e6ccafaeSMatthew G Knepley         PetscInt       v;
2872e6ccafaeSMatthew G Knepley 
2873e6ccafaeSMatthew G Knepley         for (v = 0; v < numFaceVertices; ++v) {
2874e6ccafaeSMatthew G Knepley           if (subfaceVertices[v] == p) break;
2875e6ccafaeSMatthew G Knepley         }
2876e6ccafaeSMatthew G Knepley         if (v == numFaceVertices) break;
2877e6ccafaeSMatthew G Knepley       }
2878e6ccafaeSMatthew G Knepley       if (d == dof) {
2879e6ccafaeSMatthew G Knepley         numFaces               = 1;
2880e6ccafaeSMatthew G Knepley         ((PetscInt*) faces)[0] = f;
2881e6ccafaeSMatthew G Knepley       }
2882e6ccafaeSMatthew G Knepley     }
2883e6ccafaeSMatthew G Knepley   }
2884e6ccafaeSMatthew G Knepley #endif
28852c71b3e2SJacob Faibussowitsch   PetscCheckFalse(numFaces > 1,comm, PETSC_ERR_ARG_WRONG, "Vertex set had %d faces, not one", numFaces);
2886e6ccafaeSMatthew G Knepley   else if (numFaces == 1) {
2887e6ccafaeSMatthew G Knepley     /* Add the other cell neighbor for this face */
2888766ab985SMatthew G. Knepley     ierr = DMPlexSetCone(subdm, subcell, faces);CHKERRQ(ierr);
2889e6ccafaeSMatthew G Knepley   } else {
2890e6ccafaeSMatthew G Knepley     PetscInt *indices, *origVertices, *orientedVertices, *orientedSubVertices, v, ov;
2891e6ccafaeSMatthew G Knepley     PetscBool posOriented;
2892e6ccafaeSMatthew G Knepley 
289369291d52SBarry Smith     ierr                = DMGetWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices);CHKERRQ(ierr);
2894e6ccafaeSMatthew G Knepley     origVertices        = &orientedVertices[numFaceVertices];
2895e6ccafaeSMatthew G Knepley     indices             = &orientedVertices[numFaceVertices*2];
2896e6ccafaeSMatthew G Knepley     orientedSubVertices = &orientedVertices[numFaceVertices*3];
2897e6ccafaeSMatthew G Knepley     ierr                = DMPlexGetOrientedFace(dm, cell, numFaceVertices, faceVertices, numCorners, indices, origVertices, orientedVertices, &posOriented);CHKERRQ(ierr);
2898e6ccafaeSMatthew G Knepley     /* TODO: I know that routine should return a permutation, not the indices */
2899e6ccafaeSMatthew G Knepley     for (v = 0; v < numFaceVertices; ++v) {
2900e6ccafaeSMatthew G Knepley       const PetscInt vertex = faceVertices[v], subvertex = subfaceVertices[v];
2901e6ccafaeSMatthew G Knepley       for (ov = 0; ov < numFaceVertices; ++ov) {
2902e6ccafaeSMatthew G Knepley         if (orientedVertices[ov] == vertex) {
2903e6ccafaeSMatthew G Knepley           orientedSubVertices[ov] = subvertex;
2904e6ccafaeSMatthew G Knepley           break;
2905e6ccafaeSMatthew G Knepley         }
2906e6ccafaeSMatthew G Knepley       }
29072c71b3e2SJacob Faibussowitsch       PetscCheckFalse(ov == numFaceVertices,comm, PETSC_ERR_PLIB, "Could not find face vertex %d in orientated set", vertex);
2908e6ccafaeSMatthew G Knepley     }
2909e6ccafaeSMatthew G Knepley     ierr = DMPlexSetCone(subdm, *newFacePoint, orientedSubVertices);CHKERRQ(ierr);
2910e6ccafaeSMatthew G Knepley     ierr = DMPlexSetCone(subdm, subcell, newFacePoint);CHKERRQ(ierr);
291169291d52SBarry Smith     ierr = DMRestoreWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices);CHKERRQ(ierr);
2912e6ccafaeSMatthew G Knepley     ++(*newFacePoint);
2913e6ccafaeSMatthew G Knepley   }
2914ef07cca7SMatthew G. Knepley #if 0
2915e6ccafaeSMatthew G Knepley   ierr = DMPlexRestoreJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces);CHKERRQ(ierr);
2916ef07cca7SMatthew G. Knepley #else
291769291d52SBarry Smith   ierr = DMRestoreWorkArray(subdm, 1, MPIU_INT, (void **) &faces);CHKERRQ(ierr);
2918ef07cca7SMatthew G. Knepley #endif
2919e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2920e6ccafaeSMatthew G Knepley }
2921e6ccafaeSMatthew G Knepley 
292253156dfcSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm)
2923e6ccafaeSMatthew G Knepley {
292482f516ccSBarry Smith   MPI_Comm        comm;
292553156dfcSMatthew G. Knepley   DMLabel         subpointMap;
2926efa14ee0SMatthew G Knepley   IS              subvertexIS,  subcellIS;
2927efa14ee0SMatthew G Knepley   const PetscInt *subVertices, *subCells;
2928efa14ee0SMatthew G Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells;
2929fed694aaSMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces = 0, firstSubFace, newFacePoint, nFV = 0;
2930efa14ee0SMatthew G Knepley   PetscInt        vStart, vEnd, c, f;
2931e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
2932e6ccafaeSMatthew G Knepley 
2933e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
293482f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2935efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
2936d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap);CHKERRQ(ierr);
2937efa14ee0SMatthew G Knepley   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
2938efa14ee0SMatthew G Knepley   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
293953156dfcSMatthew G. Knepley   if (vertexLabel) {ierr = DMPlexMarkSubmesh_Uninterpolated(dm, vertexLabel, value, subpointMap, &numSubFaces, &nFV, subdm);CHKERRQ(ierr);}
2940efa14ee0SMatthew G Knepley   /* Setup chart */
2941efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumSize(subpointMap, 0, &numSubVertices);CHKERRQ(ierr);
2942efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumSize(subpointMap, 2, &numSubCells);CHKERRQ(ierr);
2943efa14ee0SMatthew G Knepley   ierr = DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices);CHKERRQ(ierr);
2944efa14ee0SMatthew G Knepley   ierr = DMPlexSetVTKCellHeight(subdm, 1);CHKERRQ(ierr);
2945e6ccafaeSMatthew G Knepley   /* Set cone sizes */
2946e6ccafaeSMatthew G Knepley   firstSubVertex = numSubCells;
2947efa14ee0SMatthew G Knepley   firstSubFace   = numSubCells+numSubVertices;
2948e6ccafaeSMatthew G Knepley   newFacePoint   = firstSubFace;
2949efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumIS(subpointMap, 0, &subvertexIS);CHKERRQ(ierr);
2950efa14ee0SMatthew G Knepley   if (subvertexIS) {ierr = ISGetIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
2951efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumIS(subpointMap, 2, &subcellIS);CHKERRQ(ierr);
2952efa14ee0SMatthew G Knepley   if (subcellIS) {ierr = ISGetIndices(subcellIS, &subCells);CHKERRQ(ierr);}
2953e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
2954e6ccafaeSMatthew G Knepley     ierr = DMPlexSetConeSize(subdm, c, 1);CHKERRQ(ierr);
2955e6ccafaeSMatthew G Knepley   }
2956e6ccafaeSMatthew G Knepley   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
2957e6ccafaeSMatthew G Knepley     ierr = DMPlexSetConeSize(subdm, f, nFV);CHKERRQ(ierr);
2958e6ccafaeSMatthew G Knepley   }
2959e6ccafaeSMatthew G Knepley   ierr = DMSetUp(subdm);CHKERRQ(ierr);
2960e6ccafaeSMatthew G Knepley   /* Create face cones */
2961efa14ee0SMatthew G Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
29620298fd71SBarry Smith   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
296369291d52SBarry Smith   ierr = DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
2964e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
2965e6ccafaeSMatthew G Knepley     const PetscInt cell    = subCells[c];
2966efa14ee0SMatthew G Knepley     const PetscInt subcell = c;
29670298fd71SBarry Smith     PetscInt      *closure = NULL;
2968efa14ee0SMatthew G Knepley     PetscInt       closureSize, cl, numCorners = 0, faceSize = 0;
2969e6ccafaeSMatthew G Knepley 
2970e6ccafaeSMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2971efa14ee0SMatthew G Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
2972efa14ee0SMatthew G Knepley       const PetscInt point = closure[cl];
2973e6ccafaeSMatthew G Knepley       PetscInt       subVertex;
2974e6ccafaeSMatthew G Knepley 
2975efa14ee0SMatthew G Knepley       if ((point >= vStart) && (point < vEnd)) {
2976efa14ee0SMatthew G Knepley         ++numCorners;
2977efa14ee0SMatthew G Knepley         ierr = PetscFindInt(point, numSubVertices, subVertices, &subVertex);CHKERRQ(ierr);
2978efa14ee0SMatthew G Knepley         if (subVertex >= 0) {
2979efa14ee0SMatthew G Knepley           closure[faceSize] = point;
298065560c7fSMatthew G Knepley           subface[faceSize] = firstSubVertex+subVertex;
2981e6ccafaeSMatthew G Knepley           ++faceSize;
2982e6ccafaeSMatthew G Knepley         }
2983e6ccafaeSMatthew G Knepley       }
2984e6ccafaeSMatthew G Knepley     }
29852c71b3e2SJacob Faibussowitsch     PetscCheckFalse(faceSize > nFV,comm, PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %d of an element on the surface", faceSize);
2986efa14ee0SMatthew G Knepley     if (faceSize == nFV) {
2987cd0c2139SMatthew G Knepley       ierr = DMPlexInsertFace_Internal(dm, subdm, faceSize, closure, subface, numCorners, cell, subcell, firstSubFace, &newFacePoint);CHKERRQ(ierr);
2988e6ccafaeSMatthew G Knepley     }
298965560c7fSMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2990e6ccafaeSMatthew G Knepley   }
299169291d52SBarry Smith   ierr = DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
2992e6ccafaeSMatthew G Knepley   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
2993e6ccafaeSMatthew G Knepley   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
2994e6ccafaeSMatthew G Knepley   /* Build coordinates */
2995efa14ee0SMatthew G Knepley   {
2996efa14ee0SMatthew G Knepley     PetscSection coordSection, subCoordSection;
2997efa14ee0SMatthew G Knepley     Vec          coordinates, subCoordinates;
2998efa14ee0SMatthew G Knepley     PetscScalar *coords, *subCoords;
2999285d324eSMatthew G. Knepley     PetscInt     numComp, coordSize, v;
300024640c55SToby Isaac     const char  *name;
3001efa14ee0SMatthew G Knepley 
300269d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
3003e6ccafaeSMatthew G Knepley     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
300469d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
3005285d324eSMatthew G. Knepley     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
3006285d324eSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
3007285d324eSMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
3008efa14ee0SMatthew G Knepley     ierr = PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices);CHKERRQ(ierr);
3009efa14ee0SMatthew G Knepley     for (v = 0; v < numSubVertices; ++v) {
3010efa14ee0SMatthew G Knepley       const PetscInt vertex    = subVertices[v];
3011efa14ee0SMatthew G Knepley       const PetscInt subvertex = firstSubVertex+v;
3012efa14ee0SMatthew G Knepley       PetscInt       dof;
3013efa14ee0SMatthew G Knepley 
3014efa14ee0SMatthew G Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3015efa14ee0SMatthew G Knepley       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
3016285d324eSMatthew G. Knepley       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
3017e6ccafaeSMatthew G Knepley     }
3018e6ccafaeSMatthew G Knepley     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
3019e6ccafaeSMatthew G Knepley     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
30208b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &subCoordinates);CHKERRQ(ierr);
302124640c55SToby Isaac     ierr = PetscObjectGetName((PetscObject)coordinates,&name);CHKERRQ(ierr);
302224640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)subCoordinates,name);CHKERRQ(ierr);
3023e6ccafaeSMatthew G Knepley     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
30242eb5907fSJed Brown     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
3025830e53efSMatthew G. Knepley     if (coordSize) {
3026e6ccafaeSMatthew G Knepley       ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
3027e6ccafaeSMatthew G Knepley       ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3028efa14ee0SMatthew G Knepley       for (v = 0; v < numSubVertices; ++v) {
3029efa14ee0SMatthew G Knepley         const PetscInt vertex    = subVertices[v];
3030efa14ee0SMatthew G Knepley         const PetscInt subvertex = firstSubVertex+v;
3031efa14ee0SMatthew G Knepley         PetscInt       dof, off, sdof, soff, d;
3032e6ccafaeSMatthew G Knepley 
3033e6ccafaeSMatthew G Knepley         ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3034e6ccafaeSMatthew G Knepley         ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
3035efa14ee0SMatthew G Knepley         ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
3036efa14ee0SMatthew G Knepley         ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
30372c71b3e2SJacob Faibussowitsch         PetscCheckFalse(dof != sdof,comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
3038e6ccafaeSMatthew G Knepley         for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3039e6ccafaeSMatthew G Knepley       }
3040e6ccafaeSMatthew G Knepley       ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
3041e6ccafaeSMatthew G Knepley       ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
30423b399e24SMatthew G. Knepley     }
3043e6ccafaeSMatthew G Knepley     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
3044e6ccafaeSMatthew G Knepley     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
3045e6ccafaeSMatthew G Knepley   }
3046efa14ee0SMatthew G Knepley   /* Cleanup */
3047efa14ee0SMatthew G Knepley   if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
3048efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
3049efa14ee0SMatthew G Knepley   if (subcellIS) {ierr = ISRestoreIndices(subcellIS, &subCells);CHKERRQ(ierr);}
3050efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subcellIS);CHKERRQ(ierr);
3051e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3052e6ccafaeSMatthew G Knepley }
3053e6ccafaeSMatthew G Knepley 
30549fbee547SJacob Faibussowitsch static inline PetscInt DMPlexFilterPoint_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[])
30553982b651SMatthew G. Knepley {
30563982b651SMatthew G. Knepley   PetscInt       subPoint;
30573982b651SMatthew G. Knepley   PetscErrorCode ierr;
30583982b651SMatthew G. Knepley 
30593982b651SMatthew G. Knepley   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint); if (ierr < 0) return ierr;
30603982b651SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint+subPoint;
30613982b651SMatthew G. Knepley }
30623982b651SMatthew G. Knepley 
3063212103e5SMatthew Knepley static PetscErrorCode DMPlexFilterLabels_Internal(DM dm, const PetscInt numSubPoints[], const PetscInt *subpoints[], const PetscInt firstSubPoint[], DM subdm)
3064212103e5SMatthew Knepley {
3065212103e5SMatthew Knepley   PetscInt       Nl, l, d;
3066212103e5SMatthew Knepley   PetscErrorCode ierr;
3067212103e5SMatthew Knepley 
3068212103e5SMatthew Knepley   PetscFunctionBegin;
3069212103e5SMatthew Knepley   ierr = DMGetNumLabels(dm, &Nl);CHKERRQ(ierr);
3070212103e5SMatthew Knepley   for (l = 0; l < Nl; ++l) {
3071212103e5SMatthew Knepley     DMLabel         label, newlabel;
3072212103e5SMatthew Knepley     const char     *lname;
3073*d56405f8SMatthew G. Knepley     PetscBool       isDepth, isDim, isCelltype, isVTK;
3074212103e5SMatthew Knepley     IS              valueIS;
3075212103e5SMatthew Knepley     const PetscInt *values;
3076212103e5SMatthew Knepley     PetscInt        Nv, v;
3077212103e5SMatthew Knepley 
3078212103e5SMatthew Knepley     ierr = DMGetLabelName(dm, l, &lname);CHKERRQ(ierr);
3079212103e5SMatthew Knepley     ierr = PetscStrcmp(lname, "depth", &isDepth);CHKERRQ(ierr);
3080212103e5SMatthew Knepley     ierr = PetscStrcmp(lname, "dim", &isDim);CHKERRQ(ierr);
3081212103e5SMatthew Knepley     ierr = PetscStrcmp(lname, "celltype", &isCelltype);CHKERRQ(ierr);
3082*d56405f8SMatthew G. Knepley     ierr = PetscStrcmp(lname, "vtk", &isVTK);CHKERRQ(ierr);
3083*d56405f8SMatthew G. Knepley     if (isDepth || isDim || isCelltype || isVTK) continue;
3084212103e5SMatthew Knepley     ierr = DMCreateLabel(subdm, lname);CHKERRQ(ierr);
3085212103e5SMatthew Knepley     ierr = DMGetLabel(dm, lname, &label);CHKERRQ(ierr);
3086212103e5SMatthew Knepley     ierr = DMGetLabel(subdm, lname, &newlabel);CHKERRQ(ierr);
3087212103e5SMatthew Knepley     ierr = DMLabelGetDefaultValue(label, &v);CHKERRQ(ierr);
3088212103e5SMatthew Knepley     ierr = DMLabelSetDefaultValue(newlabel, v);CHKERRQ(ierr);
3089212103e5SMatthew Knepley     ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
3090212103e5SMatthew Knepley     ierr = ISGetLocalSize(valueIS, &Nv);CHKERRQ(ierr);
3091212103e5SMatthew Knepley     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
3092212103e5SMatthew Knepley     for (v = 0; v < Nv; ++v) {
3093212103e5SMatthew Knepley       IS              pointIS;
3094212103e5SMatthew Knepley       const PetscInt *points;
3095212103e5SMatthew Knepley       PetscInt        Np, p;
3096212103e5SMatthew Knepley 
3097212103e5SMatthew Knepley       ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
3098212103e5SMatthew Knepley       ierr = ISGetLocalSize(pointIS, &Np);CHKERRQ(ierr);
3099212103e5SMatthew Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
3100212103e5SMatthew Knepley       for (p = 0; p < Np; ++p) {
3101212103e5SMatthew Knepley         const PetscInt point = points[p];
3102212103e5SMatthew Knepley         PetscInt       subp;
3103212103e5SMatthew Knepley 
3104212103e5SMatthew Knepley         ierr = DMPlexGetPointDepth(dm, point, &d);CHKERRQ(ierr);
3105212103e5SMatthew Knepley         subp = DMPlexFilterPoint_Internal(point, firstSubPoint[d], numSubPoints[d], subpoints[d]);
3106212103e5SMatthew Knepley         if (subp >= 0) {ierr = DMLabelSetValue(newlabel, subp, values[v]);CHKERRQ(ierr);}
3107212103e5SMatthew Knepley       }
3108212103e5SMatthew Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
3109212103e5SMatthew Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
3110212103e5SMatthew Knepley     }
3111212103e5SMatthew Knepley     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
3112212103e5SMatthew Knepley     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
3113212103e5SMatthew Knepley   }
3114212103e5SMatthew Knepley   PetscFunctionReturn(0);
3115212103e5SMatthew Knepley }
3116212103e5SMatthew Knepley 
3117158acfadSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmeshGeneric_Interpolated(DM dm, DMLabel label, PetscInt value, PetscBool markedFaces, PetscBool isCohesive, PetscInt cellHeight, DM subdm)
3118e6ccafaeSMatthew G Knepley {
311982f516ccSBarry Smith   MPI_Comm         comm;
312053156dfcSMatthew G. Knepley   DMLabel          subpointMap;
3121efa14ee0SMatthew G Knepley   IS              *subpointIS;
3122efa14ee0SMatthew G Knepley   const PetscInt **subpoints;
31233982b651SMatthew G. Knepley   PetscInt        *numSubPoints, *firstSubPoint, *coneNew, *orntNew;
3124412e9a14SMatthew G. Knepley   PetscInt         totSubPoints = 0, maxConeSize, dim, p, d, v;
31250d366550SMatthew G. Knepley   PetscMPIInt      rank;
3126e6ccafaeSMatthew G Knepley   PetscErrorCode   ierr;
3127e6ccafaeSMatthew G Knepley 
3128e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
312982f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3130ffc4695bSBarry Smith   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
3131efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
3132d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap);CHKERRQ(ierr);
3133efa14ee0SMatthew G Knepley   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
3134bec263e5SMatthew G. Knepley   if (cellHeight) {
31353982b651SMatthew G. Knepley     if (isCohesive) {ierr = DMPlexMarkCohesiveSubmesh_Interpolated(dm, label, value, subpointMap, subdm);CHKERRQ(ierr);}
3136158acfadSMatthew G. Knepley     else            {ierr = DMPlexMarkSubmesh_Interpolated(dm, label, value, markedFaces, subpointMap, subdm);CHKERRQ(ierr);}
3137bec263e5SMatthew G. Knepley   } else {
3138bec263e5SMatthew G. Knepley     DMLabel         depth;
3139bec263e5SMatthew G. Knepley     IS              pointIS;
3140bec263e5SMatthew G. Knepley     const PetscInt *points;
3141b85c8bf9SMatthew G. Knepley     PetscInt        numPoints=0;
3142bec263e5SMatthew G. Knepley 
3143bec263e5SMatthew G. Knepley     ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
3144bec263e5SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, value, &pointIS);CHKERRQ(ierr);
3145b85c8bf9SMatthew G. Knepley     if (pointIS) {
3146bec263e5SMatthew G. Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
3147b85c8bf9SMatthew G. Knepley       ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
3148b85c8bf9SMatthew G. Knepley     }
3149bec263e5SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
3150bec263e5SMatthew G. Knepley       PetscInt *closure = NULL;
3151bec263e5SMatthew G. Knepley       PetscInt  closureSize, c, pdim;
3152bec263e5SMatthew G. Knepley 
3153bec263e5SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
3154bec263e5SMatthew G. Knepley       for (c = 0; c < closureSize*2; c += 2) {
3155bec263e5SMatthew G. Knepley         ierr = DMLabelGetValue(depth, closure[c], &pdim);CHKERRQ(ierr);
3156bec263e5SMatthew G. Knepley         ierr = DMLabelSetValue(subpointMap, closure[c], pdim);CHKERRQ(ierr);
3157bec263e5SMatthew G. Knepley       }
3158bec263e5SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
3159bec263e5SMatthew G. Knepley     }
3160b85c8bf9SMatthew G. Knepley     if (pointIS) {ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);}
3161bec263e5SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
3162bec263e5SMatthew G. Knepley   }
3163efa14ee0SMatthew G Knepley   /* Setup chart */
3164c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3165dcca6d9dSJed Brown   ierr = PetscMalloc4(dim+1,&numSubPoints,dim+1,&firstSubPoint,dim+1,&subpointIS,dim+1,&subpoints);CHKERRQ(ierr);
3166e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
3167e6ccafaeSMatthew G Knepley     ierr = DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d]);CHKERRQ(ierr);
3168e6ccafaeSMatthew G Knepley     totSubPoints += numSubPoints[d];
3169e6ccafaeSMatthew G Knepley   }
3170e6ccafaeSMatthew G Knepley   ierr = DMPlexSetChart(subdm, 0, totSubPoints);CHKERRQ(ierr);
3171bec263e5SMatthew G. Knepley   ierr = DMPlexSetVTKCellHeight(subdm, cellHeight);CHKERRQ(ierr);
3172e6ccafaeSMatthew G Knepley   /* Set cone sizes */
3173e6ccafaeSMatthew G Knepley   firstSubPoint[dim] = 0;
3174e6ccafaeSMatthew G Knepley   firstSubPoint[0]   = firstSubPoint[dim] + numSubPoints[dim];
3175e6ccafaeSMatthew G Knepley   if (dim > 1) {firstSubPoint[dim-1] = firstSubPoint[0]     + numSubPoints[0];}
3176e6ccafaeSMatthew G Knepley   if (dim > 2) {firstSubPoint[dim-2] = firstSubPoint[dim-1] + numSubPoints[dim-1];}
3177e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
3178e6ccafaeSMatthew G Knepley     ierr = DMLabelGetStratumIS(subpointMap, d, &subpointIS[d]);CHKERRQ(ierr);
31790ae02aa4SMatthew G. Knepley     if (subpointIS[d]) {ierr = ISGetIndices(subpointIS[d], &subpoints[d]);CHKERRQ(ierr);}
3180e6ccafaeSMatthew G Knepley   }
3181412e9a14SMatthew G. Knepley   /* We do not want this label automatically computed, instead we compute it here */
3182412e9a14SMatthew G. Knepley   ierr = DMCreateLabel(subdm, "celltype");CHKERRQ(ierr);
3183e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
3184e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3185e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3186e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
3187e6ccafaeSMatthew G Knepley       const PetscInt *cone;
3188e6ccafaeSMatthew G Knepley       PetscInt        coneSize, coneSizeNew, c, val;
3189412e9a14SMatthew G. Knepley       DMPolytopeType  ct;
3190e6ccafaeSMatthew G Knepley 
3191e6ccafaeSMatthew G Knepley       ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
3192e6ccafaeSMatthew G Knepley       ierr = DMPlexSetConeSize(subdm, subpoint, coneSize);CHKERRQ(ierr);
3193412e9a14SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, point, &ct);CHKERRQ(ierr);
3194412e9a14SMatthew G. Knepley       ierr = DMPlexSetCellType(subdm, subpoint, ct);CHKERRQ(ierr);
3195bec263e5SMatthew G. Knepley       if (cellHeight && (d == dim)) {
3196e6ccafaeSMatthew G Knepley         ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
3197e6ccafaeSMatthew G Knepley         for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
3198e6ccafaeSMatthew G Knepley           ierr = DMLabelGetValue(subpointMap, cone[c], &val);CHKERRQ(ierr);
3199e6ccafaeSMatthew G Knepley           if (val >= 0) coneSizeNew++;
3200e6ccafaeSMatthew G Knepley         }
3201e6ccafaeSMatthew G Knepley         ierr = DMPlexSetConeSize(subdm, subpoint, coneSizeNew);CHKERRQ(ierr);
3202412e9a14SMatthew G. Knepley         ierr = DMPlexSetCellType(subdm, subpoint, DM_POLYTOPE_FV_GHOST);CHKERRQ(ierr);
3203e6ccafaeSMatthew G Knepley       }
3204e6ccafaeSMatthew G Knepley     }
3205e6ccafaeSMatthew G Knepley   }
3206d67d17b1SMatthew G. Knepley   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
3207e6ccafaeSMatthew G Knepley   ierr = DMSetUp(subdm);CHKERRQ(ierr);
3208e6ccafaeSMatthew G Knepley   /* Set cones */
32090298fd71SBarry Smith   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
32103982b651SMatthew G. Knepley   ierr = PetscMalloc2(maxConeSize,&coneNew,maxConeSize,&orntNew);CHKERRQ(ierr);
3211e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
3212e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3213e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3214e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
32150e49e2e2SMatthew G. Knepley       const PetscInt *cone, *ornt;
32160d366550SMatthew G. Knepley       PetscInt        coneSize, subconeSize, coneSizeNew, c, subc, fornt = 0;
3217e6ccafaeSMatthew G Knepley 
32180d366550SMatthew G. Knepley       if (d == dim-1) {
32190d366550SMatthew G. Knepley         const PetscInt *support, *cone, *ornt;
32200d366550SMatthew G. Knepley         PetscInt        supportSize, coneSize, s, subc;
32210d366550SMatthew G. Knepley 
32220d366550SMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
32230d366550SMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
32240d366550SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
3225412e9a14SMatthew G. Knepley           PetscBool isHybrid;
3226412e9a14SMatthew G. Knepley 
3227412e9a14SMatthew G. Knepley           ierr = DMPlexCellIsHybrid_Internal(dm, support[s], &isHybrid);CHKERRQ(ierr);
3228412e9a14SMatthew G. Knepley           if (!isHybrid) continue;
32290d366550SMatthew G. Knepley           ierr = PetscFindInt(support[s], numSubPoints[d+1], subpoints[d+1], &subc);CHKERRQ(ierr);
32300d366550SMatthew G. Knepley           if (subc >= 0) {
32310d366550SMatthew G. Knepley             const PetscInt ccell = subpoints[d+1][subc];
32320d366550SMatthew G. Knepley 
32337f79407eSBarry Smith             ierr = DMPlexGetCone(dm, ccell, &cone);CHKERRQ(ierr);
32347f79407eSBarry Smith             ierr = DMPlexGetConeSize(dm, ccell, &coneSize);CHKERRQ(ierr);
32357f79407eSBarry Smith             ierr = DMPlexGetConeOrientation(dm, ccell, &ornt);CHKERRQ(ierr);
32360d366550SMatthew G. Knepley             for (c = 0; c < coneSize; ++c) {
32370d366550SMatthew G. Knepley               if (cone[c] == point) {
32380d366550SMatthew G. Knepley                 fornt = ornt[c];
32390d366550SMatthew G. Knepley                 break;
32400d366550SMatthew G. Knepley               }
32410d366550SMatthew G. Knepley             }
32420d366550SMatthew G. Knepley             break;
32430d366550SMatthew G. Knepley           }
32440d366550SMatthew G. Knepley         }
32450d366550SMatthew G. Knepley       }
3246e6ccafaeSMatthew G Knepley       ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
3247e6ccafaeSMatthew G Knepley       ierr = DMPlexGetConeSize(subdm, subpoint, &subconeSize);CHKERRQ(ierr);
3248e6ccafaeSMatthew G Knepley       ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
32490e49e2e2SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, point, &ornt);CHKERRQ(ierr);
3250e6ccafaeSMatthew G Knepley       for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
3251e6ccafaeSMatthew G Knepley         ierr = PetscFindInt(cone[c], numSubPoints[d-1], subpoints[d-1], &subc);CHKERRQ(ierr);
325201a2673eSMatthew G. Knepley         if (subc >= 0) {
325301a2673eSMatthew G. Knepley           coneNew[coneSizeNew] = firstSubPoint[d-1] + subc;
32543982b651SMatthew G. Knepley           orntNew[coneSizeNew] = ornt[c];
325501a2673eSMatthew G. Knepley           ++coneSizeNew;
325601a2673eSMatthew G. Knepley         }
3257e6ccafaeSMatthew G Knepley       }
32582c71b3e2SJacob Faibussowitsch       PetscCheckFalse(coneSizeNew != subconeSize,comm, PETSC_ERR_PLIB, "Number of cone points located %d does not match subcone size %d", coneSizeNew, subconeSize);
3259e6ccafaeSMatthew G Knepley       ierr = DMPlexSetCone(subdm, subpoint, coneNew);CHKERRQ(ierr);
32603982b651SMatthew G. Knepley       ierr = DMPlexSetConeOrientation(subdm, subpoint, orntNew);CHKERRQ(ierr);
3261b5a892a1SMatthew G. Knepley       if (fornt < 0) {ierr = DMPlexOrientPoint(subdm, subpoint, fornt);CHKERRQ(ierr);}
3262e6ccafaeSMatthew G Knepley     }
3263e6ccafaeSMatthew G Knepley   }
32643982b651SMatthew G. Knepley   ierr = PetscFree2(coneNew,orntNew);CHKERRQ(ierr);
3265e6ccafaeSMatthew G Knepley   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
3266e6ccafaeSMatthew G Knepley   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
3267e6ccafaeSMatthew G Knepley   /* Build coordinates */
3268e6ccafaeSMatthew G Knepley   {
3269e6ccafaeSMatthew G Knepley     PetscSection coordSection, subCoordSection;
3270e6ccafaeSMatthew G Knepley     Vec          coordinates, subCoordinates;
3271e6ccafaeSMatthew G Knepley     PetscScalar *coords, *subCoords;
3272c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize;
327324640c55SToby Isaac     const char  *name;
3274e6ccafaeSMatthew G Knepley 
3275c0e8cf5fSMatthew G. Knepley     ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
327669d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
3277e6ccafaeSMatthew G Knepley     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
327869d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
3279285d324eSMatthew G. Knepley     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
3280285d324eSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
3281285d324eSMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
3282e6ccafaeSMatthew G Knepley     ierr = PetscSectionSetChart(subCoordSection, firstSubPoint[0], firstSubPoint[0]+numSubPoints[0]);CHKERRQ(ierr);
3283e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3284e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3285e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0]+v;
3286e6ccafaeSMatthew G Knepley       PetscInt       dof;
3287e6ccafaeSMatthew G Knepley 
3288e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3289e6ccafaeSMatthew G Knepley       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
3290285d324eSMatthew G. Knepley       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
3291e6ccafaeSMatthew G Knepley     }
3292e6ccafaeSMatthew G Knepley     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
3293e6ccafaeSMatthew G Knepley     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
32948b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &subCoordinates);CHKERRQ(ierr);
329524640c55SToby Isaac     ierr = PetscObjectGetName((PetscObject)coordinates,&name);CHKERRQ(ierr);
329624640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)subCoordinates,name);CHKERRQ(ierr);
3297e6ccafaeSMatthew G Knepley     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
3298c0e8cf5fSMatthew G. Knepley     ierr = VecSetBlockSize(subCoordinates, cdim);CHKERRQ(ierr);
32992eb5907fSJed Brown     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
3300e6ccafaeSMatthew G Knepley     ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
3301e6ccafaeSMatthew G Knepley     ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3302e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3303e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3304e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0]+v;
3305e6ccafaeSMatthew G Knepley       PetscInt dof, off, sdof, soff, d;
3306e6ccafaeSMatthew G Knepley 
3307e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3308e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
3309e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
3310e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
33112c71b3e2SJacob Faibussowitsch       PetscCheckFalse(dof != sdof,comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
3312efa14ee0SMatthew G Knepley       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3313e6ccafaeSMatthew G Knepley     }
3314e6ccafaeSMatthew G Knepley     ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
3315e6ccafaeSMatthew G Knepley     ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3316e6ccafaeSMatthew G Knepley     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
3317e6ccafaeSMatthew G Knepley     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
3318e6ccafaeSMatthew G Knepley   }
33193982b651SMatthew G. Knepley   /* Build SF: We need this complexity because subpoints might not be selected on the owning process */
33203982b651SMatthew G. Knepley   {
33213982b651SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
33223982b651SMatthew G. Knepley     IS                 subpIS;
33233982b651SMatthew G. Knepley     const PetscSFNode *remotePoints;
33243982b651SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
33253982b651SMatthew G. Knepley     const PetscInt    *localPoints, *subpoints;
33263982b651SMatthew G. Knepley     PetscInt          *slocalPoints;
33273982b651SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubpoints = 0, numSubroots, numSubleaves = 0, l, sl, ll, pStart, pEnd, p;
33283982b651SMatthew G. Knepley     PetscMPIInt        rank;
33293982b651SMatthew G. Knepley 
3330ffc4695bSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr);
33313982b651SMatthew G. Knepley     ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
33323982b651SMatthew G. Knepley     ierr = DMGetPointSF(subdm, &sfPointSub);CHKERRQ(ierr);
33333982b651SMatthew G. Knepley     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
33343982b651SMatthew G. Knepley     ierr = DMPlexGetChart(subdm, NULL, &numSubroots);CHKERRQ(ierr);
333597d8846cSMatthew Knepley     ierr = DMPlexGetSubpointIS(subdm, &subpIS);CHKERRQ(ierr);
33363982b651SMatthew G. Knepley     if (subpIS) {
33373982b651SMatthew G. Knepley       ierr = ISGetIndices(subpIS, &subpoints);CHKERRQ(ierr);
33383982b651SMatthew G. Knepley       ierr = ISGetLocalSize(subpIS, &numSubpoints);CHKERRQ(ierr);
33393982b651SMatthew G. Knepley     }
33403982b651SMatthew G. Knepley     ierr = PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr);
33413982b651SMatthew G. Knepley     if (numRoots >= 0) {
33423982b651SMatthew G. Knepley       ierr = PetscMalloc2(pEnd-pStart,&newLocalPoints,numRoots,&newOwners);CHKERRQ(ierr);
33433982b651SMatthew G. Knepley       for (p = 0; p < pEnd-pStart; ++p) {
33443982b651SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
33453982b651SMatthew G. Knepley         newLocalPoints[p].index = -2;
33463982b651SMatthew G. Knepley       }
33473982b651SMatthew G. Knepley       /* Set subleaves */
33483982b651SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
33493982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
33503982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(point, 0, numSubpoints, subpoints);
33513982b651SMatthew G. Knepley 
33523982b651SMatthew G. Knepley         if (subpoint < 0) continue;
33533982b651SMatthew G. Knepley         newLocalPoints[point-pStart].rank  = rank;
33543982b651SMatthew G. Knepley         newLocalPoints[point-pStart].index = subpoint;
33553982b651SMatthew G. Knepley         ++numSubleaves;
33563982b651SMatthew G. Knepley       }
33573982b651SMatthew G. Knepley       /* Must put in owned subpoints */
33583982b651SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
33593982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(p, 0, numSubpoints, subpoints);
33603982b651SMatthew G. Knepley 
33613982b651SMatthew G. Knepley         if (subpoint < 0) {
33623982b651SMatthew G. Knepley           newOwners[p-pStart].rank  = -3;
33633982b651SMatthew G. Knepley           newOwners[p-pStart].index = -3;
33643982b651SMatthew G. Knepley         } else {
33653982b651SMatthew G. Knepley           newOwners[p-pStart].rank  = rank;
33663982b651SMatthew G. Knepley           newOwners[p-pStart].index = subpoint;
33673982b651SMatthew G. Knepley         }
33683982b651SMatthew G. Knepley       }
33693982b651SMatthew G. Knepley       ierr = PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
33703982b651SMatthew G. Knepley       ierr = PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
3371ad227feaSJunchao Zhang       ierr = PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints,MPI_REPLACE);CHKERRQ(ierr);
3372ad227feaSJunchao Zhang       ierr = PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints,MPI_REPLACE);CHKERRQ(ierr);
33733982b651SMatthew G. Knepley       ierr = PetscMalloc1(numSubleaves, &slocalPoints);CHKERRQ(ierr);
33743982b651SMatthew G. Knepley       ierr = PetscMalloc1(numSubleaves, &sremotePoints);CHKERRQ(ierr);
33753982b651SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
33763982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
33773982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(point, 0, numSubpoints, subpoints);
33783982b651SMatthew G. Knepley 
33793982b651SMatthew G. Knepley         if (subpoint < 0) continue;
33803982b651SMatthew G. Knepley         if (newLocalPoints[point].rank == rank) {++ll; continue;}
33813982b651SMatthew G. Knepley         slocalPoints[sl]        = subpoint;
33823982b651SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
33833982b651SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
33842c71b3e2SJacob Faibussowitsch         PetscCheckFalse(sremotePoints[sl].rank  < 0,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %d", point);
33852c71b3e2SJacob Faibussowitsch         PetscCheckFalse(sremotePoints[sl].index < 0,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %d", point);
33863982b651SMatthew G. Knepley         ++sl;
33873982b651SMatthew G. Knepley       }
33882c71b3e2SJacob Faibussowitsch       PetscCheckFalse(sl + ll != numSubleaves,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %d + %d != %d", sl, ll, numSubleaves);
33893982b651SMatthew G. Knepley       ierr = PetscFree2(newLocalPoints,newOwners);CHKERRQ(ierr);
33903982b651SMatthew G. Knepley       ierr = PetscSFSetGraph(sfPointSub, numSubroots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
33913982b651SMatthew G. Knepley     }
33923982b651SMatthew G. Knepley     if (subpIS) {
33933982b651SMatthew G. Knepley       ierr = ISRestoreIndices(subpIS, &subpoints);CHKERRQ(ierr);
33943982b651SMatthew G. Knepley     }
33953982b651SMatthew G. Knepley   }
3396212103e5SMatthew Knepley   /* Filter labels */
3397212103e5SMatthew Knepley   ierr = DMPlexFilterLabels_Internal(dm, numSubPoints, subpoints, firstSubPoint, subdm);CHKERRQ(ierr);
3398efa14ee0SMatthew G Knepley   /* Cleanup */
3399e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
34000ae02aa4SMatthew G. Knepley     if (subpointIS[d]) {ierr = ISRestoreIndices(subpointIS[d], &subpoints[d]);CHKERRQ(ierr);}
3401e6ccafaeSMatthew G Knepley     ierr = ISDestroy(&subpointIS[d]);CHKERRQ(ierr);
3402e6ccafaeSMatthew G Knepley   }
3403efa14ee0SMatthew G Knepley   ierr = PetscFree4(numSubPoints,firstSubPoint,subpointIS,subpoints);CHKERRQ(ierr);
3404e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3405e6ccafaeSMatthew G Knepley }
3406e6ccafaeSMatthew G Knepley 
3407158acfadSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM subdm)
34083982b651SMatthew G. Knepley {
34093982b651SMatthew G. Knepley   PetscErrorCode ierr;
34103982b651SMatthew G. Knepley 
34113982b651SMatthew G. Knepley   PetscFunctionBegin;
3412158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmeshGeneric_Interpolated(dm, vertexLabel, value, markedFaces, PETSC_FALSE, 1, subdm);CHKERRQ(ierr);
34133982b651SMatthew G. Knepley   PetscFunctionReturn(0);
34143982b651SMatthew G. Knepley }
34153982b651SMatthew G. Knepley 
3416d0fa310fSMatthew G. Knepley /*@
3417e6ccafaeSMatthew G Knepley   DMPlexCreateSubmesh - Extract a hypersurface from the mesh using vertices defined by a label
3418e6ccafaeSMatthew G Knepley 
3419e6ccafaeSMatthew G Knepley   Input Parameters:
3420e6ccafaeSMatthew G Knepley + dm           - The original mesh
3421158acfadSMatthew G. Knepley . vertexLabel  - The DMLabel marking points contained in the surface
3422158acfadSMatthew G. Knepley . value        - The label value to use
3423158acfadSMatthew G. Knepley - markedFaces  - PETSC_TRUE if surface faces are marked in addition to vertices, PETSC_FALSE if only vertices are marked
3424e6ccafaeSMatthew G Knepley 
3425e6ccafaeSMatthew G Knepley   Output Parameter:
3426e6ccafaeSMatthew G Knepley . subdm - The surface mesh
3427e6ccafaeSMatthew G Knepley 
3428e6ccafaeSMatthew G Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3429e6ccafaeSMatthew G Knepley 
3430e6ccafaeSMatthew G Knepley   Level: developer
3431e6ccafaeSMatthew G Knepley 
3432c58f1c22SToby Isaac .seealso: DMPlexGetSubpointMap(), DMGetLabel(), DMLabelSetValue()
3433830e53efSMatthew G. Knepley @*/
3434158acfadSMatthew G. Knepley PetscErrorCode DMPlexCreateSubmesh(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM *subdm)
3435e6ccafaeSMatthew G Knepley {
3436827c4036SVaclav Hapla   DMPlexInterpolatedFlag interpolated;
3437827c4036SVaclav Hapla   PetscInt       dim, cdim;
3438e6ccafaeSMatthew G Knepley   PetscErrorCode ierr;
3439e6ccafaeSMatthew G Knepley 
3440e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3441e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3442064a246eSJacob Faibussowitsch   PetscValidPointer(subdm, 5);
3443c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
344482f516ccSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)dm), subdm);CHKERRQ(ierr);
3445e6ccafaeSMatthew G Knepley   ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr);
3446c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(*subdm, dim-1);CHKERRQ(ierr);
3447dd2763adSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3448dd2763adSMatthew G. Knepley   ierr = DMSetCoordinateDim(*subdm, cdim);CHKERRQ(ierr);
3449827c4036SVaclav Hapla   ierr = DMPlexIsInterpolated(dm, &interpolated);CHKERRQ(ierr);
34502c71b3e2SJacob Faibussowitsch   PetscCheckFalse(interpolated == DMPLEX_INTERPOLATED_PARTIAL,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not for partially interpolated meshes");
3451827c4036SVaclav Hapla   if (interpolated) {
3452158acfadSMatthew G. Knepley     ierr = DMPlexCreateSubmesh_Interpolated(dm, vertexLabel, value, markedFaces, *subdm);CHKERRQ(ierr);
3453e6ccafaeSMatthew G Knepley   } else {
3454830e53efSMatthew G. Knepley     ierr = DMPlexCreateSubmesh_Uninterpolated(dm, vertexLabel, value, *subdm);CHKERRQ(ierr);
3455e6ccafaeSMatthew G Knepley   }
3456e600fa54SMatthew G. Knepley   ierr = DMPlexCopy_Internal(dm, PETSC_TRUE, *subdm);CHKERRQ(ierr);
3457e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3458e6ccafaeSMatthew G Knepley }
3459e6ccafaeSMatthew G Knepley 
346027c04023SMatthew G. Knepley static PetscErrorCode DMPlexCreateCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM subdm)
3461766ab985SMatthew G. Knepley {
3462766ab985SMatthew G. Knepley   MPI_Comm        comm;
3463766ab985SMatthew G. Knepley   DMLabel         subpointMap;
3464766ab985SMatthew G. Knepley   IS              subvertexIS;
3465766ab985SMatthew G. Knepley   const PetscInt *subVertices;
3466fed694aaSMatthew G. Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells, *subCells = NULL;
3467766ab985SMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces, firstSubFace, newFacePoint, nFV;
3468412e9a14SMatthew G. Knepley   PetscInt        c, f;
3469766ab985SMatthew G. Knepley   PetscErrorCode  ierr;
3470766ab985SMatthew G. Knepley 
3471766ab985SMatthew G. Knepley   PetscFunctionBegin;
3472766ab985SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
3473766ab985SMatthew G. Knepley   /* Create subpointMap which marks the submesh */
3474d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap);CHKERRQ(ierr);
3475766ab985SMatthew G. Knepley   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
3476766ab985SMatthew G. Knepley   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
347727c04023SMatthew G. Knepley   ierr = DMPlexMarkCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, subpointMap, &numSubFaces, &nFV, &subCells, subdm);CHKERRQ(ierr);
3478766ab985SMatthew G. Knepley   /* Setup chart */
3479766ab985SMatthew G. Knepley   ierr = DMLabelGetStratumSize(subpointMap, 0, &numSubVertices);CHKERRQ(ierr);
3480766ab985SMatthew G. Knepley   ierr = DMLabelGetStratumSize(subpointMap, 2, &numSubCells);CHKERRQ(ierr);
3481766ab985SMatthew G. Knepley   ierr = DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices);CHKERRQ(ierr);
3482766ab985SMatthew G. Knepley   ierr = DMPlexSetVTKCellHeight(subdm, 1);CHKERRQ(ierr);
3483766ab985SMatthew G. Knepley   /* Set cone sizes */
3484766ab985SMatthew G. Knepley   firstSubVertex = numSubCells;
3485766ab985SMatthew G. Knepley   firstSubFace   = numSubCells+numSubVertices;
3486766ab985SMatthew G. Knepley   newFacePoint   = firstSubFace;
3487766ab985SMatthew G. Knepley   ierr = DMLabelGetStratumIS(subpointMap, 0, &subvertexIS);CHKERRQ(ierr);
3488766ab985SMatthew G. Knepley   if (subvertexIS) {ierr = ISGetIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
3489766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3490766ab985SMatthew G. Knepley     ierr = DMPlexSetConeSize(subdm, c, 1);CHKERRQ(ierr);
3491766ab985SMatthew G. Knepley   }
3492766ab985SMatthew G. Knepley   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
3493766ab985SMatthew G. Knepley     ierr = DMPlexSetConeSize(subdm, f, nFV);CHKERRQ(ierr);
3494766ab985SMatthew G. Knepley   }
3495766ab985SMatthew G. Knepley   ierr = DMSetUp(subdm);CHKERRQ(ierr);
3496766ab985SMatthew G. Knepley   /* Create face cones */
3497766ab985SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
349869291d52SBarry Smith   ierr = DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
3499766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3500766ab985SMatthew G. Knepley     const PetscInt  cell    = subCells[c];
3501766ab985SMatthew G. Knepley     const PetscInt  subcell = c;
350287feddfdSMatthew G. Knepley     const PetscInt *cone, *cells;
3503412e9a14SMatthew G. Knepley     PetscBool       isHybrid;
350487feddfdSMatthew G. Knepley     PetscInt        numCells, subVertex, p, v;
3505766ab985SMatthew G. Knepley 
3506412e9a14SMatthew G. Knepley     ierr = DMPlexCellIsHybrid_Internal(dm, cell, &isHybrid);CHKERRQ(ierr);
3507412e9a14SMatthew G. Knepley     if (!isHybrid) continue;
350887feddfdSMatthew G. Knepley     ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
350987feddfdSMatthew G. Knepley     for (v = 0; v < nFV; ++v) {
351087feddfdSMatthew G. Knepley       ierr = PetscFindInt(cone[v], numSubVertices, subVertices, &subVertex);CHKERRQ(ierr);
351187feddfdSMatthew G. Knepley       subface[v] = firstSubVertex+subVertex;
351287feddfdSMatthew G. Knepley     }
351387feddfdSMatthew G. Knepley     ierr = DMPlexSetCone(subdm, newFacePoint, subface);CHKERRQ(ierr);
351487feddfdSMatthew G. Knepley     ierr = DMPlexSetCone(subdm, subcell, &newFacePoint);CHKERRQ(ierr);
351587feddfdSMatthew G. Knepley     ierr = DMPlexGetJoin(dm, nFV, cone, &numCells, &cells);CHKERRQ(ierr);
351627234c99SMatthew G. Knepley     /* Not true in parallel
35172c71b3e2SJacob Faibussowitsch     PetscCheckFalse(numCells != 2,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
351887feddfdSMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
351987feddfdSMatthew G. Knepley       PetscInt  negsubcell;
3520412e9a14SMatthew G. Knepley       PetscBool isHybrid;
3521766ab985SMatthew G. Knepley 
3522412e9a14SMatthew G. Knepley       ierr = DMPlexCellIsHybrid_Internal(dm, cells[p], &isHybrid);CHKERRQ(ierr);
3523412e9a14SMatthew G. Knepley       if (isHybrid) continue;
352487feddfdSMatthew G. Knepley       /* I know this is a crap search */
352587feddfdSMatthew G. Knepley       for (negsubcell = 0; negsubcell < numSubCells; ++negsubcell) {
352687feddfdSMatthew G. Knepley         if (subCells[negsubcell] == cells[p]) break;
3527766ab985SMatthew G. Knepley       }
35282c71b3e2SJacob Faibussowitsch       PetscCheckFalse(negsubcell == numSubCells,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find negative face neighbor for cohesive cell %d", cell);
352987feddfdSMatthew G. Knepley       ierr = DMPlexSetCone(subdm, negsubcell, &newFacePoint);CHKERRQ(ierr);
3530766ab985SMatthew G. Knepley     }
353187feddfdSMatthew G. Knepley     ierr = DMPlexRestoreJoin(dm, nFV, cone, &numCells, &cells);CHKERRQ(ierr);
353287feddfdSMatthew G. Knepley     ++newFacePoint;
3533766ab985SMatthew G. Knepley   }
353469291d52SBarry Smith   ierr = DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
3535766ab985SMatthew G. Knepley   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
3536766ab985SMatthew G. Knepley   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
3537766ab985SMatthew G. Knepley   /* Build coordinates */
3538766ab985SMatthew G. Knepley   {
3539766ab985SMatthew G. Knepley     PetscSection coordSection, subCoordSection;
3540766ab985SMatthew G. Knepley     Vec          coordinates, subCoordinates;
3541766ab985SMatthew G. Knepley     PetscScalar *coords, *subCoords;
3542c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize, v;
354324640c55SToby Isaac     const char  *name;
3544766ab985SMatthew G. Knepley 
3545c0e8cf5fSMatthew G. Knepley     ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
354669d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
3547766ab985SMatthew G. Knepley     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
354869d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
3549285d324eSMatthew G. Knepley     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
3550285d324eSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
3551285d324eSMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
3552766ab985SMatthew G. Knepley     ierr = PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices);CHKERRQ(ierr);
3553766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3554766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3555766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex+v;
3556766ab985SMatthew G. Knepley       PetscInt       dof;
3557766ab985SMatthew G. Knepley 
3558766ab985SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3559766ab985SMatthew G. Knepley       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
3560285d324eSMatthew G. Knepley       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
3561766ab985SMatthew G. Knepley     }
3562766ab985SMatthew G. Knepley     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
3563766ab985SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
35648b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &subCoordinates);CHKERRQ(ierr);
356524640c55SToby Isaac     ierr = PetscObjectGetName((PetscObject)coordinates,&name);CHKERRQ(ierr);
356624640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)subCoordinates,name);CHKERRQ(ierr);
3567766ab985SMatthew G. Knepley     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
3568c0e8cf5fSMatthew G. Knepley     ierr = VecSetBlockSize(subCoordinates, cdim);CHKERRQ(ierr);
35692eb5907fSJed Brown     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
3570766ab985SMatthew G. Knepley     ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
3571766ab985SMatthew G. Knepley     ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3572766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3573766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3574766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex+v;
3575766ab985SMatthew G. Knepley       PetscInt       dof, off, sdof, soff, d;
3576766ab985SMatthew G. Knepley 
3577766ab985SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3578766ab985SMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
3579766ab985SMatthew G. Knepley       ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
3580766ab985SMatthew G. Knepley       ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
35812c71b3e2SJacob Faibussowitsch       PetscCheckFalse(dof != sdof,comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
3582766ab985SMatthew G. Knepley       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3583766ab985SMatthew G. Knepley     }
3584766ab985SMatthew G. Knepley     ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
3585766ab985SMatthew G. Knepley     ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3586766ab985SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
3587766ab985SMatthew G. Knepley     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
3588766ab985SMatthew G. Knepley   }
3589aca35d17SMatthew G. Knepley   /* Build SF */
3590aca35d17SMatthew G. Knepley   CHKMEMQ;
3591aca35d17SMatthew G. Knepley   {
3592aca35d17SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
3593aca35d17SMatthew G. Knepley     const PetscSFNode *remotePoints;
3594bdcf2095SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
3595aca35d17SMatthew G. Knepley     const PetscInt    *localPoints;
3596bdcf2095SMatthew G. Knepley     PetscInt          *slocalPoints;
359749c26ae4SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubRoots = numSubCells+numSubFaces+numSubVertices, numSubLeaves = 0, l, sl, ll, pStart, pEnd, p, vStart, vEnd;
3598bdcf2095SMatthew G. Knepley     PetscMPIInt        rank;
3599aca35d17SMatthew G. Knepley 
3600ffc4695bSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr);
3601aca35d17SMatthew G. Knepley     ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
3602aca35d17SMatthew G. Knepley     ierr = DMGetPointSF(subdm, &sfPointSub);CHKERRQ(ierr);
3603aca35d17SMatthew G. Knepley     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3604aca35d17SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
3605aca35d17SMatthew G. Knepley     ierr = PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr);
3606aca35d17SMatthew G. Knepley     if (numRoots >= 0) {
3607aca35d17SMatthew G. Knepley       /* Only vertices should be shared */
3608dcca6d9dSJed Brown       ierr = PetscMalloc2(pEnd-pStart,&newLocalPoints,numRoots,&newOwners);CHKERRQ(ierr);
3609bdcf2095SMatthew G. Knepley       for (p = 0; p < pEnd-pStart; ++p) {
3610bdcf2095SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
3611bdcf2095SMatthew G. Knepley         newLocalPoints[p].index = -2;
3612bdcf2095SMatthew G. Knepley       }
36139e0823b2SMatthew G. Knepley       /* Set subleaves */
3614aca35d17SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
3615aca35d17SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3616bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3617aca35d17SMatthew G. Knepley 
36182c71b3e2SJacob Faibussowitsch         PetscCheckFalse((point < vStart) && (point >= vEnd),PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Should not be mapping anything but vertices, %d", point);
3619bdcf2095SMatthew G. Knepley         if (subPoint < 0) continue;
3620bdcf2095SMatthew G. Knepley         newLocalPoints[point-pStart].rank  = rank;
3621bdcf2095SMatthew G. Knepley         newLocalPoints[point-pStart].index = subPoint;
3622bdcf2095SMatthew G. Knepley         ++numSubLeaves;
3623aca35d17SMatthew G. Knepley       }
36249e0823b2SMatthew G. Knepley       /* Must put in owned subpoints */
36259e0823b2SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
36269e0823b2SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(p, firstSubVertex, numSubVertices, subVertices);
36279e0823b2SMatthew G. Knepley 
36289e0823b2SMatthew G. Knepley         if (subPoint < 0) {
36299e0823b2SMatthew G. Knepley           newOwners[p-pStart].rank  = -3;
36309e0823b2SMatthew G. Knepley           newOwners[p-pStart].index = -3;
36319e0823b2SMatthew G. Knepley         } else {
36329e0823b2SMatthew G. Knepley           newOwners[p-pStart].rank  = rank;
36339e0823b2SMatthew G. Knepley           newOwners[p-pStart].index = subPoint;
36349e0823b2SMatthew G. Knepley         }
3635bdcf2095SMatthew G. Knepley       }
3636bdcf2095SMatthew G. Knepley       ierr = PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
3637bdcf2095SMatthew G. Knepley       ierr = PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
3638ad227feaSJunchao Zhang       ierr = PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints,MPI_REPLACE);CHKERRQ(ierr);
3639ad227feaSJunchao Zhang       ierr = PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints,MPI_REPLACE);CHKERRQ(ierr);
3640785e854fSJed Brown       ierr = PetscMalloc1(numSubLeaves,    &slocalPoints);CHKERRQ(ierr);
3641785e854fSJed Brown       ierr = PetscMalloc1(numSubLeaves, &sremotePoints);CHKERRQ(ierr);
364249c26ae4SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
3643bdcf2095SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3644bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3645aca35d17SMatthew G. Knepley 
3646aca35d17SMatthew G. Knepley         if (subPoint < 0) continue;
364749c26ae4SMatthew G. Knepley         if (newLocalPoints[point].rank == rank) {++ll; continue;}
3648aca35d17SMatthew G. Knepley         slocalPoints[sl]        = subPoint;
3649bdcf2095SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
3650bdcf2095SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
36512c71b3e2SJacob Faibussowitsch         PetscCheckFalse(sremotePoints[sl].rank  < 0,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %d", point);
36522c71b3e2SJacob Faibussowitsch         PetscCheckFalse(sremotePoints[sl].index < 0,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %d", point);
3653aca35d17SMatthew G. Knepley         ++sl;
3654aca35d17SMatthew G. Knepley       }
3655bdcf2095SMatthew G. Knepley       ierr = PetscFree2(newLocalPoints,newOwners);CHKERRQ(ierr);
36562c71b3e2SJacob Faibussowitsch       PetscCheckFalse(sl + ll != numSubLeaves,PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %d + %d != %d", sl, ll, numSubLeaves);
365749c26ae4SMatthew G. Knepley       ierr = PetscSFSetGraph(sfPointSub, numSubRoots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
3658aca35d17SMatthew G. Knepley     }
3659aca35d17SMatthew G. Knepley   }
3660aca35d17SMatthew G. Knepley   CHKMEMQ;
3661766ab985SMatthew G. Knepley   /* Cleanup */
3662766ab985SMatthew G. Knepley   if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
3663766ab985SMatthew G. Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
3664766ab985SMatthew G. Knepley   ierr = PetscFree(subCells);CHKERRQ(ierr);
3665766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
3666766ab985SMatthew G. Knepley }
3667766ab985SMatthew G. Knepley 
36683982b651SMatthew G. Knepley static PetscErrorCode DMPlexCreateCohesiveSubmesh_Interpolated(DM dm, const char labelname[], PetscInt value, DM subdm)
3669766ab985SMatthew G. Knepley {
36703982b651SMatthew G. Knepley   DMLabel        label = NULL;
3671766ab985SMatthew G. Knepley   PetscErrorCode ierr;
3672766ab985SMatthew G. Knepley 
3673766ab985SMatthew G. Knepley   PetscFunctionBegin;
3674c58f1c22SToby Isaac   if (labelname) {ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);}
3675158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmeshGeneric_Interpolated(dm, label, value, PETSC_FALSE, PETSC_TRUE, 1, subdm);CHKERRQ(ierr);
3676766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
3677766ab985SMatthew G. Knepley }
3678766ab985SMatthew G. Knepley 
3679c08575a3SMatthew G. Knepley /*@C
36807d2fefb6SMatthew 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.
3681766ab985SMatthew G. Knepley 
3682766ab985SMatthew G. Knepley   Input Parameters:
3683766ab985SMatthew G. Knepley + dm          - The original mesh
368427c04023SMatthew G. Knepley . hasLagrange - The mesh has Lagrange unknowns in the cohesive cells
36857afc1a8bSJed Brown . label       - A label name, or NULL
368627c04023SMatthew G. Knepley - value  - A label value
3687766ab985SMatthew G. Knepley 
3688766ab985SMatthew G. Knepley   Output Parameter:
3689766ab985SMatthew G. Knepley . subdm - The surface mesh
3690766ab985SMatthew G. Knepley 
3691766ab985SMatthew G. Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3692766ab985SMatthew G. Knepley 
3693766ab985SMatthew G. Knepley   Level: developer
3694766ab985SMatthew G. Knepley 
3695766ab985SMatthew G. Knepley .seealso: DMPlexGetSubpointMap(), DMPlexCreateSubmesh()
3696c08575a3SMatthew G. Knepley @*/
369727c04023SMatthew G. Knepley PetscErrorCode DMPlexCreateCohesiveSubmesh(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM *subdm)
3698766ab985SMatthew G. Knepley {
3699c0e8cf5fSMatthew G. Knepley   PetscInt       dim, cdim, depth;
3700766ab985SMatthew G. Knepley   PetscErrorCode ierr;
3701766ab985SMatthew G. Knepley 
3702766ab985SMatthew G. Knepley   PetscFunctionBegin;
3703766ab985SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
370427c04023SMatthew G. Knepley   PetscValidPointer(subdm, 5);
3705c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3706766ab985SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3707766ab985SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), subdm);CHKERRQ(ierr);
3708766ab985SMatthew G. Knepley   ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr);
3709c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(*subdm, dim-1);CHKERRQ(ierr);
3710c0e8cf5fSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3711c0e8cf5fSMatthew G. Knepley   ierr = DMSetCoordinateDim(*subdm, cdim);CHKERRQ(ierr);
3712766ab985SMatthew G. Knepley   if (depth == dim) {
3713b3154360SMatthew G. Knepley     ierr = DMPlexCreateCohesiveSubmesh_Interpolated(dm, label, value, *subdm);CHKERRQ(ierr);
3714766ab985SMatthew G. Knepley   } else {
371527c04023SMatthew G. Knepley     ierr = DMPlexCreateCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, *subdm);CHKERRQ(ierr);
3716e6ccafaeSMatthew G Knepley   }
3717e600fa54SMatthew G. Knepley   ierr = DMPlexCopy_Internal(dm, PETSC_TRUE, *subdm);CHKERRQ(ierr);
3718e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3719e6ccafaeSMatthew G Knepley }
3720e6ccafaeSMatthew G Knepley 
3721bec263e5SMatthew G. Knepley /*@
3722bec263e5SMatthew G. Knepley   DMPlexFilter - Extract a subset of mesh cells defined by a label as a separate mesh
3723bec263e5SMatthew G. Knepley 
3724bec263e5SMatthew G. Knepley   Input Parameters:
3725bec263e5SMatthew G. Knepley + dm        - The original mesh
3726bec263e5SMatthew G. Knepley . cellLabel - The DMLabel marking cells contained in the new mesh
3727bec263e5SMatthew G. Knepley - value     - The label value to use
3728bec263e5SMatthew G. Knepley 
3729bec263e5SMatthew G. Knepley   Output Parameter:
3730bec263e5SMatthew G. Knepley . subdm - The new mesh
3731bec263e5SMatthew G. Knepley 
3732bec263e5SMatthew G. Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3733bec263e5SMatthew G. Knepley 
3734bec263e5SMatthew G. Knepley   Level: developer
3735bec263e5SMatthew G. Knepley 
3736c58f1c22SToby Isaac .seealso: DMPlexGetSubpointMap(), DMGetLabel(), DMLabelSetValue()
3737bec263e5SMatthew G. Knepley @*/
3738bec263e5SMatthew G. Knepley PetscErrorCode DMPlexFilter(DM dm, DMLabel cellLabel, PetscInt value, DM *subdm)
3739bec263e5SMatthew G. Knepley {
3740bec263e5SMatthew G. Knepley   PetscInt       dim;
3741bec263e5SMatthew G. Knepley   PetscErrorCode ierr;
3742bec263e5SMatthew G. Knepley 
3743bec263e5SMatthew G. Knepley   PetscFunctionBegin;
3744bec263e5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3745064a246eSJacob Faibussowitsch   PetscValidPointer(subdm, 4);
3746bec263e5SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3747bec263e5SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), subdm);CHKERRQ(ierr);
3748bec263e5SMatthew G. Knepley   ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr);
3749bec263e5SMatthew G. Knepley   ierr = DMSetDimension(*subdm, dim);CHKERRQ(ierr);
3750bec263e5SMatthew G. Knepley   /* Extract submesh in place, could be empty on some procs, could have inconsistency if procs do not both extract a shared cell */
3751158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmeshGeneric_Interpolated(dm, cellLabel, value, PETSC_FALSE, PETSC_FALSE, 0, *subdm);CHKERRQ(ierr);
3752e600fa54SMatthew G. Knepley   ierr = DMPlexCopy_Internal(dm, PETSC_TRUE, *subdm);CHKERRQ(ierr);
3753bec263e5SMatthew G. Knepley   PetscFunctionReturn(0);
3754bec263e5SMatthew G. Knepley }
3755bec263e5SMatthew G. Knepley 
375664beef6dSMatthew G. Knepley /*@
375764beef6dSMatthew G. Knepley   DMPlexGetSubpointMap - Returns a DMLabel with point dimension as values
375864beef6dSMatthew G. Knepley 
375964beef6dSMatthew G. Knepley   Input Parameter:
376064beef6dSMatthew G. Knepley . dm - The submesh DM
376164beef6dSMatthew G. Knepley 
376264beef6dSMatthew G. Knepley   Output Parameter:
376364beef6dSMatthew G. Knepley . subpointMap - The DMLabel of all the points from the original mesh in this submesh, or NULL if this is not a submesh
376464beef6dSMatthew G. Knepley 
376564beef6dSMatthew G. Knepley   Level: developer
376664beef6dSMatthew G. Knepley 
376797d8846cSMatthew Knepley .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointIS()
376864beef6dSMatthew G. Knepley @*/
3769e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexGetSubpointMap(DM dm, DMLabel *subpointMap)
3770e6ccafaeSMatthew G Knepley {
3771e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3772e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3773e6ccafaeSMatthew G Knepley   PetscValidPointer(subpointMap, 2);
377465663942SMatthew G. Knepley   *subpointMap = ((DM_Plex*) dm->data)->subpointMap;
3775e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3776e6ccafaeSMatthew G Knepley }
3777e6ccafaeSMatthew G Knepley 
3778c08575a3SMatthew G. Knepley /*@
3779c08575a3SMatthew G. Knepley   DMPlexSetSubpointMap - Sets the DMLabel with point dimension as values
3780c08575a3SMatthew G. Knepley 
3781c08575a3SMatthew G. Knepley   Input Parameters:
3782c08575a3SMatthew G. Knepley + dm - The submesh DM
3783c08575a3SMatthew G. Knepley - subpointMap - The DMLabel of all the points from the original mesh in this submesh
3784c08575a3SMatthew G. Knepley 
3785c08575a3SMatthew G. Knepley   Note: Should normally not be called by the user, since it is set in DMPlexCreateSubmesh()
3786c08575a3SMatthew G. Knepley 
3787c08575a3SMatthew G. Knepley   Level: developer
3788c08575a3SMatthew G. Knepley 
378997d8846cSMatthew Knepley .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointIS()
3790c08575a3SMatthew G. Knepley @*/
3791e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexSetSubpointMap(DM dm, DMLabel subpointMap)
3792e6ccafaeSMatthew G Knepley {
3793e6ccafaeSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
3794285d324eSMatthew G. Knepley   DMLabel        tmp;
3795e6ccafaeSMatthew G Knepley   PetscErrorCode ierr;
3796e6ccafaeSMatthew G Knepley 
3797e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3798e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3799285d324eSMatthew G. Knepley   tmp  = mesh->subpointMap;
3800e6ccafaeSMatthew G Knepley   mesh->subpointMap = subpointMap;
3801d67d17b1SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) mesh->subpointMap);CHKERRQ(ierr);
3802285d324eSMatthew G. Knepley   ierr = DMLabelDestroy(&tmp);CHKERRQ(ierr);
3803e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3804e6ccafaeSMatthew G Knepley }
3805e6ccafaeSMatthew G Knepley 
380697d8846cSMatthew Knepley static PetscErrorCode DMPlexCreateSubpointIS_Internal(DM dm, IS *subpointIS)
380797d8846cSMatthew Knepley {
380897d8846cSMatthew Knepley   DMLabel        spmap;
380997d8846cSMatthew Knepley   PetscInt       depth, d;
381097d8846cSMatthew Knepley   PetscErrorCode ierr;
381197d8846cSMatthew Knepley 
381297d8846cSMatthew Knepley   PetscFunctionBegin;
381397d8846cSMatthew Knepley   ierr = DMPlexGetSubpointMap(dm, &spmap);CHKERRQ(ierr);
381497d8846cSMatthew Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
381597d8846cSMatthew Knepley   if (spmap && depth >= 0) {
381697d8846cSMatthew Knepley     DM_Plex  *mesh = (DM_Plex *) dm->data;
381797d8846cSMatthew Knepley     PetscInt *points, *depths;
381897d8846cSMatthew Knepley     PetscInt  pStart, pEnd, p, off;
381997d8846cSMatthew Knepley 
382097d8846cSMatthew Knepley     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
38212c71b3e2SJacob Faibussowitsch     PetscCheckFalse(pStart,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Submeshes must start the point numbering at 0, not %d", pStart);
382297d8846cSMatthew Knepley     ierr = PetscMalloc1(pEnd, &points);CHKERRQ(ierr);
382397d8846cSMatthew Knepley     ierr = DMGetWorkArray(dm, depth+1, MPIU_INT, &depths);CHKERRQ(ierr);
382497d8846cSMatthew Knepley     depths[0] = depth;
382597d8846cSMatthew Knepley     depths[1] = 0;
382697d8846cSMatthew Knepley     for (d = 2; d <= depth; ++d) {depths[d] = depth+1 - d;}
382797d8846cSMatthew Knepley     for (d = 0, off = 0; d <= depth; ++d) {
382897d8846cSMatthew Knepley       const PetscInt dep = depths[d];
382997d8846cSMatthew Knepley       PetscInt       depStart, depEnd, n;
383097d8846cSMatthew Knepley 
383197d8846cSMatthew Knepley       ierr = DMPlexGetDepthStratum(dm, dep, &depStart, &depEnd);CHKERRQ(ierr);
383297d8846cSMatthew Knepley       ierr = DMLabelGetStratumSize(spmap, dep, &n);CHKERRQ(ierr);
383397d8846cSMatthew Knepley       if (((d < 2) && (depth > 1)) || (d == 1)) { /* Only check vertices and cells for now since the map is broken for others */
38342c71b3e2SJacob 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);
383597d8846cSMatthew Knepley       } else {
383697d8846cSMatthew Knepley         if (!n) {
383797d8846cSMatthew Knepley           if (d == 0) {
383897d8846cSMatthew Knepley             /* Missing cells */
383997d8846cSMatthew Knepley             for (p = 0; p < depEnd-depStart; ++p, ++off) points[off] = -1;
384097d8846cSMatthew Knepley           } else {
384197d8846cSMatthew Knepley             /* Missing faces */
384297d8846cSMatthew Knepley             for (p = 0; p < depEnd-depStart; ++p, ++off) points[off] = PETSC_MAX_INT;
384397d8846cSMatthew Knepley           }
384497d8846cSMatthew Knepley         }
384597d8846cSMatthew Knepley       }
384697d8846cSMatthew Knepley       if (n) {
384797d8846cSMatthew Knepley         IS              is;
384897d8846cSMatthew Knepley         const PetscInt *opoints;
384997d8846cSMatthew Knepley 
385097d8846cSMatthew Knepley         ierr = DMLabelGetStratumIS(spmap, dep, &is);CHKERRQ(ierr);
385197d8846cSMatthew Knepley         ierr = ISGetIndices(is, &opoints);CHKERRQ(ierr);
385297d8846cSMatthew Knepley         for (p = 0; p < n; ++p, ++off) points[off] = opoints[p];
385397d8846cSMatthew Knepley         ierr = ISRestoreIndices(is, &opoints);CHKERRQ(ierr);
385497d8846cSMatthew Knepley         ierr = ISDestroy(&is);CHKERRQ(ierr);
385597d8846cSMatthew Knepley       }
385697d8846cSMatthew Knepley     }
385797d8846cSMatthew Knepley     ierr = DMRestoreWorkArray(dm, depth+1, MPIU_INT, &depths);CHKERRQ(ierr);
38582c71b3e2SJacob Faibussowitsch     PetscCheckFalse(off != pEnd,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %d should be %d", off, pEnd);
385997d8846cSMatthew Knepley     ierr = ISCreateGeneral(PETSC_COMM_SELF, pEnd, points, PETSC_OWN_POINTER, subpointIS);CHKERRQ(ierr);
386097d8846cSMatthew Knepley     ierr = PetscObjectStateGet((PetscObject) spmap, &mesh->subpointState);CHKERRQ(ierr);
386197d8846cSMatthew Knepley   }
386297d8846cSMatthew Knepley   PetscFunctionReturn(0);
386397d8846cSMatthew Knepley }
386497d8846cSMatthew Knepley 
386564beef6dSMatthew G. Knepley /*@
386697d8846cSMatthew Knepley   DMPlexGetSubpointIS - Returns an IS covering the entire subdm chart with the original points as data
3867e6ccafaeSMatthew G Knepley 
3868e6ccafaeSMatthew G Knepley   Input Parameter:
3869e6ccafaeSMatthew G Knepley . dm - The submesh DM
3870e6ccafaeSMatthew G Knepley 
3871e6ccafaeSMatthew G Knepley   Output Parameter:
38720298fd71SBarry Smith . subpointIS - The IS of all the points from the original mesh in this submesh, or NULL if this is not a submesh
3873e6ccafaeSMatthew G Knepley 
38743982b651SMatthew G. Knepley   Note: This IS is guaranteed to be sorted by the construction of the submesh
387564beef6dSMatthew G. Knepley 
387664beef6dSMatthew G. Knepley   Level: developer
387764beef6dSMatthew G. Knepley 
387864beef6dSMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointMap()
387964beef6dSMatthew G. Knepley @*/
388097d8846cSMatthew Knepley PetscErrorCode DMPlexGetSubpointIS(DM dm, IS *subpointIS)
3881e6ccafaeSMatthew G Knepley {
388297d8846cSMatthew Knepley   DM_Plex         *mesh = (DM_Plex *) dm->data;
388397d8846cSMatthew Knepley   DMLabel          spmap;
388497d8846cSMatthew Knepley   PetscObjectState state;
3885e6ccafaeSMatthew G Knepley   PetscErrorCode   ierr;
3886e6ccafaeSMatthew G Knepley 
3887e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3888e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3889e6ccafaeSMatthew G Knepley   PetscValidPointer(subpointIS, 2);
389097d8846cSMatthew Knepley   ierr = DMPlexGetSubpointMap(dm, &spmap);CHKERRQ(ierr);
389197d8846cSMatthew Knepley   ierr = PetscObjectStateGet((PetscObject) spmap, &state);CHKERRQ(ierr);
389297d8846cSMatthew Knepley   if (state != mesh->subpointState || !mesh->subpointIS) {ierr = DMPlexCreateSubpointIS_Internal(dm, &mesh->subpointIS);CHKERRQ(ierr);}
389397d8846cSMatthew Knepley   *subpointIS = mesh->subpointIS;
3894e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3895e6ccafaeSMatthew G Knepley }
3896559a1558SMatthew G. Knepley 
3897559a1558SMatthew G. Knepley /*@
3898a6e0b375SMatthew G. Knepley   DMGetEnclosureRelation - Get the relationship between dmA and dmB
3899559a1558SMatthew G. Knepley 
3900559a1558SMatthew G. Knepley   Input Parameters:
3901a6e0b375SMatthew G. Knepley + dmA - The first DM
3902a6e0b375SMatthew G. Knepley - dmB - The second DM
3903559a1558SMatthew G. Knepley 
3904559a1558SMatthew G. Knepley   Output Parameter:
3905a6e0b375SMatthew G. Knepley . rel - The relation of dmA to dmB
3906559a1558SMatthew G. Knepley 
3907a6e0b375SMatthew G. Knepley   Level: intermediate
3908559a1558SMatthew G. Knepley 
3909446c23c1SPierre Jolivet .seealso: DMGetEnclosurePoint()
3910559a1558SMatthew G. Knepley @*/
3911a6e0b375SMatthew G. Knepley PetscErrorCode DMGetEnclosureRelation(DM dmA, DM dmB, DMEnclosureType *rel)
3912559a1558SMatthew G. Knepley {
3913a6e0b375SMatthew G. Knepley   DM             plexA, plexB, sdm;
3914559a1558SMatthew G. Knepley   DMLabel        spmap;
3915a6e0b375SMatthew G. Knepley   PetscInt       pStartA, pEndA, pStartB, pEndB, NpA, NpB;
3916559a1558SMatthew G. Knepley   PetscErrorCode ierr;
3917559a1558SMatthew G. Knepley 
391844171101SMatthew G. Knepley   PetscFunctionBegin;
3919a6e0b375SMatthew G. Knepley   PetscValidPointer(rel, 3);
3920a6e0b375SMatthew G. Knepley   *rel = DM_ENC_NONE;
3921a6e0b375SMatthew G. Knepley   if (!dmA || !dmB) PetscFunctionReturn(0);
3922a6e0b375SMatthew G. Knepley   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
3923064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
3924a6e0b375SMatthew G. Knepley   if (dmA == dmB) {*rel = DM_ENC_EQUALITY; PetscFunctionReturn(0);}
3925a6e0b375SMatthew G. Knepley   ierr = DMConvert(dmA, DMPLEX, &plexA);CHKERRQ(ierr);
3926a6e0b375SMatthew G. Knepley   ierr = DMConvert(dmB, DMPLEX, &plexB);CHKERRQ(ierr);
3927a6e0b375SMatthew G. Knepley   ierr = DMPlexGetChart(plexA, &pStartA, &pEndA);CHKERRQ(ierr);
3928a6e0b375SMatthew G. Knepley   ierr = DMPlexGetChart(plexB, &pStartB, &pEndB);CHKERRQ(ierr);
3929a6e0b375SMatthew G. Knepley   /* Assumption 1: subDMs have smaller charts than the DMs that they originate from
3930a6e0b375SMatthew G. Knepley     - The degenerate case of a subdomain which includes all of the domain on some process can be treated as equality */
3931a6e0b375SMatthew G. Knepley   if ((pStartA == pStartB) && (pEndA == pEndB)) {
3932a6e0b375SMatthew G. Knepley     *rel = DM_ENC_EQUALITY;
3933a6e0b375SMatthew G. Knepley     goto end;
3934559a1558SMatthew G. Knepley   }
3935a6e0b375SMatthew G. Knepley   NpA = pEndA - pStartA;
3936a6e0b375SMatthew G. Knepley   NpB = pEndB - pStartB;
3937a6e0b375SMatthew G. Knepley   if (NpA == NpB) goto end;
3938a6e0b375SMatthew G. Knepley   sdm = NpA > NpB ? plexB : plexA; /* The other is the original, enclosing dm */
3939a6e0b375SMatthew G. Knepley   ierr = DMPlexGetSubpointMap(sdm, &spmap);CHKERRQ(ierr);
3940a6e0b375SMatthew G. Knepley   if (!spmap) goto end;
3941a6e0b375SMatthew G. Knepley   /* TODO Check the space mapped to by subpointMap is same size as dm */
3942a6e0b375SMatthew G. Knepley   if (NpA > NpB) {
3943a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUPERMESH;
3944a6e0b375SMatthew G. Knepley   } else {
3945a6e0b375SMatthew G. Knepley     *rel = DM_ENC_SUBMESH;
3946a6e0b375SMatthew G. Knepley   }
3947a6e0b375SMatthew G. Knepley   end:
3948a6e0b375SMatthew G. Knepley   ierr = DMDestroy(&plexA);CHKERRQ(ierr);
3949a6e0b375SMatthew G. Knepley   ierr = DMDestroy(&plexB);CHKERRQ(ierr);
3950559a1558SMatthew G. Knepley   PetscFunctionReturn(0);
3951559a1558SMatthew G. Knepley }
395244171101SMatthew G. Knepley 
395344171101SMatthew G. Knepley /*@
3954a6e0b375SMatthew G. Knepley   DMGetEnclosurePoint - Get the point pA in dmA which corresponds to the point pB in dmB
395544171101SMatthew G. Knepley 
395644171101SMatthew G. Knepley   Input Parameters:
3957a6e0b375SMatthew G. Knepley + dmA   - The first DM
3958a6e0b375SMatthew G. Knepley . dmB   - The second DM
3959a6e0b375SMatthew G. Knepley . etype - The type of enclosure relation that dmA has to dmB
3960a6e0b375SMatthew G. Knepley - pB    - A point of dmB
396144171101SMatthew G. Knepley 
396244171101SMatthew G. Knepley   Output Parameter:
3963a6e0b375SMatthew G. Knepley . pA    - The corresponding point of dmA
396444171101SMatthew G. Knepley 
3965a6e0b375SMatthew G. Knepley   Level: intermediate
396644171101SMatthew G. Knepley 
3967a6e0b375SMatthew G. Knepley .seealso: DMGetEnclosureRelation()
396844171101SMatthew G. Knepley @*/
3969a6e0b375SMatthew G. Knepley PetscErrorCode DMGetEnclosurePoint(DM dmA, DM dmB, DMEnclosureType etype, PetscInt pB, PetscInt *pA)
397044171101SMatthew G. Knepley {
3971a6e0b375SMatthew G. Knepley   DM              sdm;
3972a6e0b375SMatthew G. Knepley   IS              subpointIS;
3973a6e0b375SMatthew G. Knepley   const PetscInt *subpoints;
3974a6e0b375SMatthew G. Knepley   PetscInt        numSubpoints;
397544171101SMatthew G. Knepley   PetscErrorCode  ierr;
397644171101SMatthew G. Knepley 
397744171101SMatthew G. Knepley   PetscFunctionBegin;
3978a6e0b375SMatthew G. Knepley   /* TODO Cache the IS, making it look like an index */
3979a6e0b375SMatthew G. Knepley   switch (etype) {
3980a6e0b375SMatthew G. Knepley     case DM_ENC_SUPERMESH:
3981a6e0b375SMatthew G. Knepley     sdm  = dmB;
398297d8846cSMatthew Knepley     ierr = DMPlexGetSubpointIS(sdm, &subpointIS);CHKERRQ(ierr);
3983a6e0b375SMatthew G. Knepley     ierr = ISGetIndices(subpointIS, &subpoints);CHKERRQ(ierr);
3984a6e0b375SMatthew G. Knepley     *pA  = subpoints[pB];
3985a6e0b375SMatthew G. Knepley     ierr = ISRestoreIndices(subpointIS, &subpoints);CHKERRQ(ierr);
3986a6e0b375SMatthew G. Knepley     break;
3987a6e0b375SMatthew G. Knepley     case DM_ENC_SUBMESH:
3988a6e0b375SMatthew G. Knepley     sdm  = dmA;
398997d8846cSMatthew Knepley     ierr = DMPlexGetSubpointIS(sdm, &subpointIS);CHKERRQ(ierr);
3990a6e0b375SMatthew G. Knepley     ierr = ISGetLocalSize(subpointIS, &numSubpoints);CHKERRQ(ierr);
3991a6e0b375SMatthew G. Knepley     ierr = ISGetIndices(subpointIS, &subpoints);CHKERRQ(ierr);
3992a6e0b375SMatthew G. Knepley     ierr = PetscFindInt(pB, numSubpoints, subpoints, pA);CHKERRQ(ierr);
3993a6e0b375SMatthew G. Knepley     if (*pA < 0) {
3994a6e0b375SMatthew G. Knepley       ierr = DMViewFromOptions(dmA, NULL, "-dm_enc_A_view");CHKERRQ(ierr);
3995a6e0b375SMatthew G. Knepley       ierr = DMViewFromOptions(dmB, NULL, "-dm_enc_B_view");CHKERRQ(ierr);
399698921bdaSJacob Faibussowitsch       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %d not found in submesh", pB);
3997a6e0b375SMatthew G. Knepley     }
3998a6e0b375SMatthew G. Knepley     ierr = ISRestoreIndices(subpointIS, &subpoints);CHKERRQ(ierr);
3999a6e0b375SMatthew G. Knepley     break;
4000a6e0b375SMatthew G. Knepley     case DM_ENC_EQUALITY:
4001a6e0b375SMatthew G. Knepley     case DM_ENC_NONE:
4002a6e0b375SMatthew G. Knepley     *pA = pB;break;
4003a6e0b375SMatthew G. Knepley     case DM_ENC_UNKNOWN:
4004a6e0b375SMatthew G. Knepley     {
4005a6e0b375SMatthew G. Knepley       DMEnclosureType enc;
400644171101SMatthew G. Knepley 
4007a6e0b375SMatthew G. Knepley       ierr = DMGetEnclosureRelation(dmA, dmB, &enc);CHKERRQ(ierr);
4008a6e0b375SMatthew G. Knepley       ierr = DMGetEnclosurePoint(dmA, dmB, enc, pB, pA);CHKERRQ(ierr);
4009a6e0b375SMatthew G. Knepley     }
4010a6e0b375SMatthew G. Knepley     break;
401198921bdaSJacob Faibussowitsch     default: SETERRQ(PetscObjectComm((PetscObject) dmA), PETSC_ERR_ARG_OUTOFRANGE, "Invalid enclosure type %d", (int) etype);
401244171101SMatthew G. Knepley   }
401344171101SMatthew G. Knepley   PetscFunctionReturn(0);
401444171101SMatthew G. Knepley }
4015