xref: /petsc/src/dm/impls/plex/plexsubmesh.c (revision 485ad865d343c0cc798f1f58673f19c6293b434b)
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 
5e752be1aSMatthew G. Knepley static PetscErrorCode DMPlexMarkBoundaryFaces_Internal(DM dm, PetscInt val, PetscInt cellHeight, DMLabel label)
630560a7bSMatthew G. Knepley {
730560a7bSMatthew G. Knepley   PetscInt       fStart, fEnd, f;
830560a7bSMatthew G. Knepley   PetscErrorCode ierr;
930560a7bSMatthew G. Knepley 
1030560a7bSMatthew G. Knepley   PetscFunctionBegin;
1130560a7bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1230560a7bSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, cellHeight+1, &fStart, &fEnd);CHKERRQ(ierr);
1330560a7bSMatthew G. Knepley   for (f = fStart; f < fEnd; ++f) {
1430560a7bSMatthew G. Knepley     PetscInt supportSize;
1530560a7bSMatthew G. Knepley 
1630560a7bSMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, f, &supportSize);CHKERRQ(ierr);
17e752be1aSMatthew G. Knepley     if (supportSize == 1) {
18e752be1aSMatthew G. Knepley       if (val < 0) {
19e752be1aSMatthew G. Knepley         PetscInt *closure = NULL;
20e752be1aSMatthew G. Knepley         PetscInt  clSize, cl, cval;
21e752be1aSMatthew G. Knepley 
22e752be1aSMatthew G. Knepley         ierr = DMPlexGetTransitiveClosure(dm, f, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
23e752be1aSMatthew G. Knepley         for (cl = 0; cl < clSize*2; cl += 2) {
24e752be1aSMatthew G. Knepley           ierr = DMLabelGetValue(label, closure[cl], &cval);CHKERRQ(ierr);
25e752be1aSMatthew G. Knepley           if (cval < 0) continue;
26e752be1aSMatthew G. Knepley           ierr = DMLabelSetValue(label, f, cval);CHKERRQ(ierr);
27e752be1aSMatthew G. Knepley           break;
28e752be1aSMatthew G. Knepley         }
29e752be1aSMatthew G. Knepley         if (cl == clSize*2) {ierr = DMLabelSetValue(label, f, 1);CHKERRQ(ierr);}
30e752be1aSMatthew G. Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, f, PETSC_TRUE, &clSize, &closure);CHKERRQ(ierr);
31e752be1aSMatthew G. Knepley       } else {
32e752be1aSMatthew G. Knepley         ierr = DMLabelSetValue(label, f, val);CHKERRQ(ierr);
33e752be1aSMatthew G. Knepley       }
34e752be1aSMatthew G. Knepley     }
3530560a7bSMatthew G. Knepley   }
3630560a7bSMatthew G. Knepley   PetscFunctionReturn(0);
3730560a7bSMatthew G. Knepley }
3830560a7bSMatthew G. Knepley 
39cd0c2139SMatthew G Knepley /*@
40cd0c2139SMatthew G Knepley   DMPlexMarkBoundaryFaces - Mark all faces on the boundary
41cd0c2139SMatthew G Knepley 
42cd0c2139SMatthew G Knepley   Not Collective
43cd0c2139SMatthew G Knepley 
44cd0c2139SMatthew G Knepley   Input Parameter:
45e752be1aSMatthew G. Knepley + dm - The original DM
46e752be1aSMatthew G. Knepley - val - The marker value, or PETSC_DETERMINE to use some value in the closure (or 1 if none are found)
47cd0c2139SMatthew G Knepley 
48cd0c2139SMatthew G Knepley   Output Parameter:
49e752be1aSMatthew G. Knepley . label - The DMLabel marking boundary faces with the given value
50cd0c2139SMatthew G Knepley 
51cd0c2139SMatthew G Knepley   Level: developer
52cd0c2139SMatthew G Knepley 
53c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMCreateLabel()
5409f723d9SJed Brown @*/
55e752be1aSMatthew G. Knepley PetscErrorCode DMPlexMarkBoundaryFaces(DM dm, PetscInt val, DMLabel label)
56cd0c2139SMatthew G Knepley {
57cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
58cd0c2139SMatthew G Knepley 
59cd0c2139SMatthew G Knepley   PetscFunctionBegin;
60e752be1aSMatthew G. Knepley   ierr = DMPlexMarkBoundaryFaces_Internal(dm, val, 0, label);CHKERRQ(ierr);
61cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
62cd0c2139SMatthew G Knepley }
63cd0c2139SMatthew G Knepley 
64c08575a3SMatthew G. Knepley static PetscErrorCode DMPlexLabelComplete_Internal(DM dm, DMLabel label, PetscBool completeCells)
65b0bf5782SToby Isaac {
66b0bf5782SToby Isaac   IS              valueIS;
67ac51f24eSSander Arens   PetscSF         sfPoint;
68b0bf5782SToby Isaac   const PetscInt *values;
69ac51f24eSSander Arens   PetscInt        numValues, v, cStart, cEnd, nroots;
70b0bf5782SToby Isaac   PetscErrorCode  ierr;
71b0bf5782SToby Isaac 
72b0bf5782SToby Isaac   PetscFunctionBegin;
73b0bf5782SToby Isaac   ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
74b0bf5782SToby Isaac   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
75b0bf5782SToby Isaac   ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
76b0bf5782SToby Isaac   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
77b0bf5782SToby Isaac   for (v = 0; v < numValues; ++v) {
78b0bf5782SToby Isaac     IS              pointIS;
79b0bf5782SToby Isaac     const PetscInt *points;
80b0bf5782SToby Isaac     PetscInt        numPoints, p;
81b0bf5782SToby Isaac 
82b0bf5782SToby Isaac     ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr);
83b0bf5782SToby Isaac     ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
84b0bf5782SToby Isaac     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
85b0bf5782SToby Isaac     for (p = 0; p < numPoints; ++p) {
86b0bf5782SToby Isaac       PetscInt  q = points[p];
87b0bf5782SToby Isaac       PetscInt *closure = NULL;
88b0bf5782SToby Isaac       PetscInt  closureSize, c;
89b0bf5782SToby Isaac 
90b0bf5782SToby Isaac       if (cStart <= q && q < cEnd && !completeCells) { /* skip cells */
91b0bf5782SToby Isaac         continue;
92b0bf5782SToby Isaac       }
93b0bf5782SToby Isaac       ierr = DMPlexGetTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
94b0bf5782SToby Isaac       for (c = 0; c < closureSize*2; c += 2) {
95b0bf5782SToby Isaac         ierr = DMLabelSetValue(label, closure[c], values[v]);CHKERRQ(ierr);
96b0bf5782SToby Isaac       }
97b0bf5782SToby Isaac       ierr = DMPlexRestoreTransitiveClosure(dm, q, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
98b0bf5782SToby Isaac     }
99b0bf5782SToby Isaac     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
100b0bf5782SToby Isaac     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
101b0bf5782SToby Isaac   }
102b0bf5782SToby Isaac   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
103b0bf5782SToby Isaac   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
104ac51f24eSSander Arens   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
105ac51f24eSSander Arens   ierr = PetscSFGetGraph(sfPoint, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
106ac51f24eSSander Arens   if (nroots >= 0) {
10726279d81SSanderA     DMLabel         lblRoots, lblLeaves;
10826279d81SSanderA     IS              valueIS, pointIS;
10926279d81SSanderA     const PetscInt *values;
11026279d81SSanderA     PetscInt        numValues, v;
11126279d81SSanderA     PetscErrorCode  ierr;
11226279d81SSanderA 
11326279d81SSanderA     ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
11426279d81SSanderA     /* Pull point contributions from remote leaves into local roots */
11526279d81SSanderA     ierr = DMLabelGather(label, sfPoint, &lblLeaves);CHKERRQ(ierr);
11626279d81SSanderA     ierr = DMLabelGetValueIS(lblLeaves, &valueIS);CHKERRQ(ierr);
11726279d81SSanderA     ierr = ISGetLocalSize(valueIS, &numValues);CHKERRQ(ierr);
11826279d81SSanderA     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
11926279d81SSanderA     for (v = 0; v < numValues; ++v) {
12026279d81SSanderA       const PetscInt value = values[v];
12126279d81SSanderA 
12226279d81SSanderA       ierr = DMLabelGetStratumIS(lblLeaves, value, &pointIS);CHKERRQ(ierr);
12326279d81SSanderA       ierr = DMLabelInsertIS(label, pointIS, value);CHKERRQ(ierr);
12426279d81SSanderA       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
12526279d81SSanderA     }
12626279d81SSanderA     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
12726279d81SSanderA     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
12826279d81SSanderA     ierr = DMLabelDestroy(&lblLeaves);CHKERRQ(ierr);
12926279d81SSanderA     /* Push point contributions from roots into remote leaves */
13026279d81SSanderA     ierr = DMLabelDistribute(label, sfPoint, &lblRoots);CHKERRQ(ierr);
13126279d81SSanderA     ierr = DMLabelGetValueIS(lblRoots, &valueIS);CHKERRQ(ierr);
13226279d81SSanderA     ierr = ISGetLocalSize(valueIS, &numValues);CHKERRQ(ierr);
13326279d81SSanderA     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
13426279d81SSanderA     for (v = 0; v < numValues; ++v) {
13526279d81SSanderA       const PetscInt value = values[v];
13626279d81SSanderA 
13726279d81SSanderA       ierr = DMLabelGetStratumIS(lblRoots, value, &pointIS);CHKERRQ(ierr);
13826279d81SSanderA       ierr = DMLabelInsertIS(label, pointIS, value);CHKERRQ(ierr);
13926279d81SSanderA       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
14026279d81SSanderA     }
14126279d81SSanderA     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
14226279d81SSanderA     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
14326279d81SSanderA     ierr = DMLabelDestroy(&lblRoots);CHKERRQ(ierr);
14426279d81SSanderA   }
145b0bf5782SToby Isaac   PetscFunctionReturn(0);
146b0bf5782SToby Isaac }
147b0bf5782SToby Isaac 
1482be2b188SMatthew G Knepley /*@
1492be2b188SMatthew G Knepley   DMPlexLabelComplete - Starting with a label marking points on a surface, we add the transitive closure to the surface
1502be2b188SMatthew G Knepley 
1512be2b188SMatthew G Knepley   Input Parameters:
1522be2b188SMatthew G Knepley + dm - The DM
1532be2b188SMatthew G Knepley - label - A DMLabel marking the surface points
1542be2b188SMatthew G Knepley 
1552be2b188SMatthew G Knepley   Output Parameter:
1562be2b188SMatthew G Knepley . label - A DMLabel marking all surface points in the transitive closure
1572be2b188SMatthew G Knepley 
1582be2b188SMatthew G Knepley   Level: developer
1592be2b188SMatthew G Knepley 
1602be2b188SMatthew G Knepley .seealso: DMPlexLabelCohesiveComplete()
1612be2b188SMatthew G Knepley @*/
1622be2b188SMatthew G Knepley PetscErrorCode DMPlexLabelComplete(DM dm, DMLabel label)
1632be2b188SMatthew G Knepley {
1642be2b188SMatthew G Knepley   PetscErrorCode ierr;
1652be2b188SMatthew G Knepley 
1662be2b188SMatthew G Knepley   PetscFunctionBegin;
167b0bf5782SToby Isaac   ierr = DMPlexLabelComplete_Internal(dm, label, PETSC_TRUE);CHKERRQ(ierr);
1682be2b188SMatthew G Knepley   PetscFunctionReturn(0);
1692be2b188SMatthew G Knepley }
1702be2b188SMatthew G Knepley 
1716cf0e42fSMatthew G. Knepley /*@
1726cf0e42fSMatthew G. Knepley   DMPlexLabelAddCells - Starting with a label marking faces on a surface, we add a cell for each face
1736cf0e42fSMatthew G. Knepley 
1746cf0e42fSMatthew G. Knepley   Input Parameters:
1756cf0e42fSMatthew G. Knepley + dm - The DM
1766cf0e42fSMatthew G. Knepley - label - A DMLabel marking the surface points
1776cf0e42fSMatthew G. Knepley 
1786cf0e42fSMatthew G. Knepley   Output Parameter:
1796cf0e42fSMatthew G. Knepley . label - A DMLabel incorporating cells
1806cf0e42fSMatthew G. Knepley 
1816cf0e42fSMatthew G. Knepley   Level: developer
1826cf0e42fSMatthew G. Knepley 
1836cf0e42fSMatthew G. Knepley   Note: The cells allow FEM boundary conditions to be applied using the cell geometry
1846cf0e42fSMatthew G. Knepley 
1856cf0e42fSMatthew G. Knepley .seealso: DMPlexLabelComplete(), DMPlexLabelCohesiveComplete()
1866cf0e42fSMatthew G. Knepley @*/
1876cf0e42fSMatthew G. Knepley PetscErrorCode DMPlexLabelAddCells(DM dm, DMLabel label)
1886cf0e42fSMatthew G. Knepley {
1896cf0e42fSMatthew G. Knepley   IS              valueIS;
1906cf0e42fSMatthew G. Knepley   const PetscInt *values;
191*485ad865SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd;
1926cf0e42fSMatthew G. Knepley   PetscErrorCode  ierr;
1936cf0e42fSMatthew G. Knepley 
1946cf0e42fSMatthew G. Knepley   PetscFunctionBegin;
195*485ad865SMatthew G. Knepley   ierr = DMPlexGetInteriorCellStratum(dm, &cStart, &cEnd);CHKERRQ(ierr);
1966cf0e42fSMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
1976cf0e42fSMatthew G. Knepley   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
1986cf0e42fSMatthew G. Knepley   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
1996cf0e42fSMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
2006cf0e42fSMatthew G. Knepley     IS              pointIS;
2016cf0e42fSMatthew G. Knepley     const PetscInt *points;
2026cf0e42fSMatthew G. Knepley     PetscInt        numPoints, p;
2036cf0e42fSMatthew G. Knepley 
2046cf0e42fSMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr);
2056cf0e42fSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
2066cf0e42fSMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
2076cf0e42fSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2086cf0e42fSMatthew G. Knepley       PetscInt *closure = NULL;
20922eabd52SMatthew G. Knepley       PetscInt  closureSize, point, cl;
2106cf0e42fSMatthew G. Knepley 
2116cf0e42fSMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure);CHKERRQ(ierr);
21222eabd52SMatthew G. Knepley       for (cl = closureSize-1; cl > 0; --cl) {
21322eabd52SMatthew G. Knepley         point = closure[cl*2];
21422eabd52SMatthew G. Knepley         if ((point >= cStart) && (point < cEnd)) {ierr = DMLabelSetValue(label, point, values[v]);CHKERRQ(ierr); break;}
21522eabd52SMatthew G. Knepley       }
2166cf0e42fSMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure);CHKERRQ(ierr);
2176cf0e42fSMatthew G. Knepley     }
2186cf0e42fSMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
2196cf0e42fSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
2206cf0e42fSMatthew G. Knepley   }
2216cf0e42fSMatthew G. Knepley   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
2226cf0e42fSMatthew G. Knepley   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
2236cf0e42fSMatthew G. Knepley   PetscFunctionReturn(0);
2246cf0e42fSMatthew G. Knepley }
2256cf0e42fSMatthew G. Knepley 
226f402d5e4SToby Isaac /*@
227f402d5e4SToby Isaac   DMPlexLabelClearCells - Remove cells from a label
228f402d5e4SToby Isaac 
229f402d5e4SToby Isaac   Input Parameters:
230f402d5e4SToby Isaac + dm - The DM
231f402d5e4SToby Isaac - label - A DMLabel marking surface points and their adjacent cells
232f402d5e4SToby Isaac 
233f402d5e4SToby Isaac   Output Parameter:
234f402d5e4SToby Isaac . label - A DMLabel without cells
235f402d5e4SToby Isaac 
236f402d5e4SToby Isaac   Level: developer
237f402d5e4SToby Isaac 
238f402d5e4SToby Isaac   Note: This undoes DMPlexLabelAddCells()
239f402d5e4SToby Isaac 
240f402d5e4SToby Isaac .seealso: DMPlexLabelComplete(), DMPlexLabelCohesiveComplete(), DMPlexLabelAddCells()
241f402d5e4SToby Isaac @*/
242f402d5e4SToby Isaac PetscErrorCode DMPlexLabelClearCells(DM dm, DMLabel label)
243f402d5e4SToby Isaac {
244f402d5e4SToby Isaac   IS              valueIS;
245f402d5e4SToby Isaac   const PetscInt *values;
246*485ad865SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd;
247f402d5e4SToby Isaac   PetscErrorCode  ierr;
248f402d5e4SToby Isaac 
249f402d5e4SToby Isaac   PetscFunctionBegin;
250*485ad865SMatthew G. Knepley   ierr = DMPlexGetInteriorCellStratum(dm, &cStart, &cEnd);CHKERRQ(ierr);
251f402d5e4SToby Isaac   ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
252f402d5e4SToby Isaac   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
253f402d5e4SToby Isaac   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
254f402d5e4SToby Isaac   for (v = 0; v < numValues; ++v) {
255f402d5e4SToby Isaac     IS              pointIS;
256f402d5e4SToby Isaac     const PetscInt *points;
257f402d5e4SToby Isaac     PetscInt        numPoints, p;
258f402d5e4SToby Isaac 
259f402d5e4SToby Isaac     ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr);
260f402d5e4SToby Isaac     ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
261f402d5e4SToby Isaac     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
262f402d5e4SToby Isaac     for (p = 0; p < numPoints; ++p) {
263f402d5e4SToby Isaac       PetscInt point = points[p];
264f402d5e4SToby Isaac 
265f402d5e4SToby Isaac       if (point >= cStart && point < cEnd) {
266f402d5e4SToby Isaac         ierr = DMLabelClearValue(label,point,values[v]);CHKERRQ(ierr);
267f402d5e4SToby Isaac       }
268f402d5e4SToby Isaac     }
269f402d5e4SToby Isaac     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
270f402d5e4SToby Isaac     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
271f402d5e4SToby Isaac   }
272f402d5e4SToby Isaac   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
273f402d5e4SToby Isaac   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
274f402d5e4SToby Isaac   PetscFunctionReturn(0);
275f402d5e4SToby Isaac }
276f402d5e4SToby Isaac 
27759eef20bSToby Isaac /* take (oldEnd, added) pairs, ordered by height and convert them to (oldstart, newstart) pairs, ordered by ascending
27859eef20bSToby Isaac  * index (skipping first, which is (0,0)) */
2792582d50cSToby Isaac PETSC_STATIC_INLINE PetscErrorCode DMPlexShiftPointSetUp_Internal(PetscInt depth, PetscInt depthShift[])
280cd0c2139SMatthew G Knepley {
2812582d50cSToby Isaac   PetscInt d, off = 0;
2822582d50cSToby Isaac 
2832582d50cSToby Isaac   PetscFunctionBegin;
28459eef20bSToby Isaac   /* sort by (oldend): yes this is an O(n^2) sort, we expect depth <= 3 */
2850974a383SToby Isaac   for (d = 0; d < depth; d++) {
2862582d50cSToby Isaac     PetscInt firstd = d;
2870974a383SToby Isaac     PetscInt firstStart = depthShift[2*d];
2882582d50cSToby Isaac     PetscInt e;
2892582d50cSToby Isaac 
2902582d50cSToby Isaac     for (e = d+1; e <= depth; e++) {
2912582d50cSToby Isaac       if (depthShift[2*e] < firstStart) {
2922582d50cSToby Isaac         firstd = e;
2932582d50cSToby Isaac         firstStart = depthShift[2*d];
2942582d50cSToby Isaac       }
2952582d50cSToby Isaac     }
2962582d50cSToby Isaac     if (firstd != d) {
2972582d50cSToby Isaac       PetscInt swap[2];
2982582d50cSToby Isaac 
2992582d50cSToby Isaac       e = firstd;
3002582d50cSToby Isaac       swap[0] = depthShift[2*d];
3012582d50cSToby Isaac       swap[1] = depthShift[2*d+1];
3022582d50cSToby Isaac       depthShift[2*d]   = depthShift[2*e];
3032582d50cSToby Isaac       depthShift[2*d+1] = depthShift[2*e+1];
3042582d50cSToby Isaac       depthShift[2*e]   = swap[0];
3052582d50cSToby Isaac       depthShift[2*e+1] = swap[1];
3062582d50cSToby Isaac     }
3072582d50cSToby Isaac   }
3082582d50cSToby Isaac   /* convert (oldstart, added) to (oldstart, newstart) */
3090974a383SToby Isaac   for (d = 0; d <= depth; d++) {
3102582d50cSToby Isaac     off += depthShift[2*d+1];
31159eef20bSToby Isaac     depthShift[2*d+1] = depthShift[2*d] + off;
3122582d50cSToby Isaac   }
3132582d50cSToby Isaac   PetscFunctionReturn(0);
3142582d50cSToby Isaac }
3152582d50cSToby Isaac 
3162582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
3172582d50cSToby Isaac PETSC_STATIC_INLINE PetscInt DMPlexShiftPoint_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
3182582d50cSToby Isaac {
3192582d50cSToby Isaac   PetscInt d;
3202582d50cSToby Isaac   PetscInt newOff = 0;
3212582d50cSToby Isaac 
3222582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
3232582d50cSToby Isaac     if (p < depthShift[2*d]) return p + newOff;
3242582d50cSToby Isaac     else newOff = depthShift[2*d+1] - depthShift[2*d];
3252582d50cSToby Isaac   }
3260974a383SToby Isaac   return p + newOff;
3272582d50cSToby Isaac }
3282582d50cSToby Isaac 
3292582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
3302582d50cSToby Isaac PETSC_STATIC_INLINE PetscInt DMPlexShiftPointInverse_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
3312582d50cSToby Isaac {
3322582d50cSToby Isaac   PetscInt d;
3332582d50cSToby Isaac   PetscInt newOff = 0;
3342582d50cSToby Isaac 
3352582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
3362582d50cSToby Isaac     if (p < depthShift[2*d+1]) return p + newOff;
3372582d50cSToby Isaac     else newOff = depthShift[2*d] - depthShift[2*d+1];
3382582d50cSToby Isaac   }
3390974a383SToby Isaac   return p + newOff;
340cd0c2139SMatthew G Knepley }
341cd0c2139SMatthew G Knepley 
342cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftSizes_Internal(DM dm, PetscInt depthShift[], DM dmNew)
343cd0c2139SMatthew G Knepley {
344cd0c2139SMatthew G Knepley   PetscInt       depth = 0, d, pStart, pEnd, p;
345fa8e8ae5SToby Isaac   DMLabel        depthLabel;
346cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
347cd0c2139SMatthew G Knepley 
348cd0c2139SMatthew G Knepley   PetscFunctionBegin;
349cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
350cd0c2139SMatthew G Knepley   if (depth < 0) PetscFunctionReturn(0);
351cd0c2139SMatthew G Knepley   /* Step 1: Expand chart */
352cd0c2139SMatthew G Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3530974a383SToby Isaac   pEnd = DMPlexShiftPoint_Internal(pEnd,depth,depthShift);
354cd0c2139SMatthew G Knepley   ierr = DMPlexSetChart(dmNew, pStart, pEnd);CHKERRQ(ierr);
355fa8e8ae5SToby Isaac   ierr = DMCreateLabel(dmNew,"depth");CHKERRQ(ierr);
356fa8e8ae5SToby Isaac   ierr = DMPlexGetDepthLabel(dmNew,&depthLabel);CHKERRQ(ierr);
357cd0c2139SMatthew G Knepley   /* Step 2: Set cone and support sizes */
358cd0c2139SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
359fa8e8ae5SToby Isaac     PetscInt pStartNew, pEndNew;
360fa8e8ae5SToby Isaac     IS pIS;
361fa8e8ae5SToby Isaac 
362cd0c2139SMatthew G Knepley     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
363fa8e8ae5SToby Isaac     pStartNew = DMPlexShiftPoint_Internal(pStart, depth, depthShift);
364fa8e8ae5SToby Isaac     pEndNew = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
365fa8e8ae5SToby Isaac     ierr = ISCreateStride(PETSC_COMM_SELF, pEndNew - pStartNew, pStartNew, 1, &pIS);CHKERRQ(ierr);
366fa8e8ae5SToby Isaac     ierr = DMLabelSetStratumIS(depthLabel, d, pIS);CHKERRQ(ierr);
367fa8e8ae5SToby Isaac     ierr = ISDestroy(&pIS);CHKERRQ(ierr);
368cd0c2139SMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
3692582d50cSToby Isaac       PetscInt newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
370cd0c2139SMatthew G Knepley       PetscInt size;
371cd0c2139SMatthew G Knepley 
372cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, p, &size);CHKERRQ(ierr);
373cd0c2139SMatthew G Knepley       ierr = DMPlexSetConeSize(dmNew, newp, size);CHKERRQ(ierr);
374cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, p, &size);CHKERRQ(ierr);
375cd0c2139SMatthew G Knepley       ierr = DMPlexSetSupportSize(dmNew, newp, size);CHKERRQ(ierr);
376cd0c2139SMatthew G Knepley     }
377cd0c2139SMatthew G Knepley   }
378cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
379cd0c2139SMatthew G Knepley }
380cd0c2139SMatthew G Knepley 
381cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftPoints_Internal(DM dm, PetscInt depthShift[], DM dmNew)
382cd0c2139SMatthew G Knepley {
3832582d50cSToby Isaac   PetscInt      *newpoints;
3842582d50cSToby Isaac   PetscInt       depth = 0, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, pStart, pEnd, p;
385cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
386cd0c2139SMatthew G Knepley 
387cd0c2139SMatthew G Knepley   PetscFunctionBegin;
388cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
389cd0c2139SMatthew G Knepley   if (depth < 0) PetscFunctionReturn(0);
390cd0c2139SMatthew G Knepley   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
391dcbb62e8SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dmNew, &maxConeSizeNew, &maxSupportSizeNew);CHKERRQ(ierr);
3922582d50cSToby Isaac   ierr = PetscMalloc1(PetscMax(PetscMax(maxConeSize, maxSupportSize), PetscMax(maxConeSizeNew, maxSupportSizeNew)),&newpoints);CHKERRQ(ierr);
393cd0c2139SMatthew G Knepley   /* Step 5: Set cones and supports */
394cd0c2139SMatthew G Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
395cd0c2139SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
396cd0c2139SMatthew G Knepley     const PetscInt *points = NULL, *orientations = NULL;
3972582d50cSToby Isaac     PetscInt        size,sizeNew, i, newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
398cd0c2139SMatthew G Knepley 
399cd0c2139SMatthew G Knepley     ierr = DMPlexGetConeSize(dm, p, &size);CHKERRQ(ierr);
400cd0c2139SMatthew G Knepley     ierr = DMPlexGetCone(dm, p, &points);CHKERRQ(ierr);
401cd0c2139SMatthew G Knepley     ierr = DMPlexGetConeOrientation(dm, p, &orientations);CHKERRQ(ierr);
402cd0c2139SMatthew G Knepley     for (i = 0; i < size; ++i) {
4032582d50cSToby Isaac       newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
404cd0c2139SMatthew G Knepley     }
405cd0c2139SMatthew G Knepley     ierr = DMPlexSetCone(dmNew, newp, newpoints);CHKERRQ(ierr);
406cd0c2139SMatthew G Knepley     ierr = DMPlexSetConeOrientation(dmNew, newp, orientations);CHKERRQ(ierr);
407cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupportSize(dm, p, &size);CHKERRQ(ierr);
408dcbb62e8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmNew, newp, &sizeNew);CHKERRQ(ierr);
409cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupport(dm, p, &points);CHKERRQ(ierr);
410cd0c2139SMatthew G Knepley     for (i = 0; i < size; ++i) {
4112582d50cSToby Isaac       newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
412cd0c2139SMatthew G Knepley     }
413dcbb62e8SMatthew G. Knepley     for (i = size; i < sizeNew; ++i) newpoints[i] = 0;
414cd0c2139SMatthew G Knepley     ierr = DMPlexSetSupport(dmNew, newp, newpoints);CHKERRQ(ierr);
415cd0c2139SMatthew G Knepley   }
4162582d50cSToby Isaac   ierr = PetscFree(newpoints);CHKERRQ(ierr);
417cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
418cd0c2139SMatthew G Knepley }
419cd0c2139SMatthew G Knepley 
420cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftCoordinates_Internal(DM dm, PetscInt depthShift[], DM dmNew)
421cd0c2139SMatthew G Knepley {
422cd0c2139SMatthew G Knepley   PetscSection   coordSection, newCoordSection;
423cd0c2139SMatthew G Knepley   Vec            coordinates, newCoordinates;
424cd0c2139SMatthew G Knepley   PetscScalar   *coords, *newCoords;
425f2b8cce1SMatthew G. Knepley   PetscInt       coordSize, sStart, sEnd;
426f2b8cce1SMatthew G. Knepley   PetscInt       dim, depth = 0, cStart, cEnd, cStartNew, cEndNew, c, vStart, vEnd, vStartNew, vEndNew, v;
427f2b8cce1SMatthew G. Knepley   PetscBool      hasCells;
428cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
429cd0c2139SMatthew G Knepley 
430cd0c2139SMatthew G Knepley   PetscFunctionBegin;
431f2b8cce1SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
432c0e8cf5fSMatthew G. Knepley   ierr = DMSetCoordinateDim(dmNew, dim);CHKERRQ(ierr);
433cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
434cd0c2139SMatthew G Knepley   /* Step 8: Convert coordinates */
435cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
436f2b8cce1SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
437cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepthStratum(dmNew, 0, &vStartNew, &vEndNew);CHKERRQ(ierr);
438f2b8cce1SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmNew, 0, &cStartNew, &cEndNew);CHKERRQ(ierr);
43969d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
440cd0c2139SMatthew G Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &newCoordSection);CHKERRQ(ierr);
441cd0c2139SMatthew G Knepley   ierr = PetscSectionSetNumFields(newCoordSection, 1);CHKERRQ(ierr);
442cd0c2139SMatthew G Knepley   ierr = PetscSectionSetFieldComponents(newCoordSection, 0, dim);CHKERRQ(ierr);
443f2b8cce1SMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
444f2b8cce1SMatthew G. Knepley   hasCells = sStart == cStart ? PETSC_TRUE : PETSC_FALSE;
445f2b8cce1SMatthew G. Knepley   ierr = PetscSectionSetChart(newCoordSection, hasCells ? cStartNew : vStartNew, vEndNew);CHKERRQ(ierr);
446f2b8cce1SMatthew G. Knepley   if (hasCells) {
447f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
448f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof;
449f2b8cce1SMatthew G. Knepley 
450f2b8cce1SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
451f2b8cce1SMatthew G. Knepley       ierr = PetscSectionSetDof(newCoordSection, cNew, dof);CHKERRQ(ierr);
452f2b8cce1SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(newCoordSection, cNew, 0, dof);CHKERRQ(ierr);
453f2b8cce1SMatthew G. Knepley     }
454f2b8cce1SMatthew G. Knepley   }
455cd0c2139SMatthew G Knepley   for (v = vStartNew; v < vEndNew; ++v) {
456cd0c2139SMatthew G Knepley     ierr = PetscSectionSetDof(newCoordSection, v, dim);CHKERRQ(ierr);
457cd0c2139SMatthew G Knepley     ierr = PetscSectionSetFieldDof(newCoordSection, v, 0, dim);CHKERRQ(ierr);
458cd0c2139SMatthew G Knepley   }
459cd0c2139SMatthew G Knepley   ierr = PetscSectionSetUp(newCoordSection);CHKERRQ(ierr);
46046e270d4SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmNew, PETSC_DETERMINE, newCoordSection);CHKERRQ(ierr);
461cd0c2139SMatthew G Knepley   ierr = PetscSectionGetStorageSize(newCoordSection, &coordSize);CHKERRQ(ierr);
4628b9ced59SLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &newCoordinates);CHKERRQ(ierr);
463cd0c2139SMatthew G Knepley   ierr = PetscObjectSetName((PetscObject) newCoordinates, "coordinates");CHKERRQ(ierr);
464cd0c2139SMatthew G Knepley   ierr = VecSetSizes(newCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
4654e90ef8eSMatthew G. Knepley   ierr = VecSetBlockSize(newCoordinates, dim);CHKERRQ(ierr);
4662eb5907fSJed Brown   ierr = VecSetType(newCoordinates,VECSTANDARD);CHKERRQ(ierr);
467cd0c2139SMatthew G Knepley   ierr = DMSetCoordinatesLocal(dmNew, newCoordinates);CHKERRQ(ierr);
468cd0c2139SMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
469cd0c2139SMatthew G Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
470cd0c2139SMatthew G Knepley   ierr = VecGetArray(newCoordinates, &newCoords);CHKERRQ(ierr);
471f2b8cce1SMatthew G. Knepley   if (hasCells) {
472f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
473f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof, off, noff, d;
474f2b8cce1SMatthew G. Knepley 
475f2b8cce1SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
476f2b8cce1SMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, c, &off);CHKERRQ(ierr);
477f2b8cce1SMatthew G. Knepley       ierr = PetscSectionGetOffset(newCoordSection, cNew, &noff);CHKERRQ(ierr);
478f2b8cce1SMatthew G. Knepley       for (d = 0; d < dof; ++d) newCoords[noff+d] = coords[off+d];
479f2b8cce1SMatthew G. Knepley     }
480f2b8cce1SMatthew G. Knepley   }
481cd0c2139SMatthew G Knepley   for (v = vStart; v < vEnd; ++v) {
482cd0c2139SMatthew G Knepley     PetscInt dof, off, noff, d;
483cd0c2139SMatthew G Knepley 
484cd0c2139SMatthew G Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
485cd0c2139SMatthew G Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
4862582d50cSToby Isaac     ierr = PetscSectionGetOffset(newCoordSection, DMPlexShiftPoint_Internal(v, depth, depthShift), &noff);CHKERRQ(ierr);
487f2b8cce1SMatthew G. Knepley     for (d = 0; d < dof; ++d) newCoords[noff+d] = coords[off+d];
488cd0c2139SMatthew G Knepley   }
489cd0c2139SMatthew G Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
490cd0c2139SMatthew G Knepley   ierr = VecRestoreArray(newCoordinates, &newCoords);CHKERRQ(ierr);
491cd0c2139SMatthew G Knepley   ierr = VecDestroy(&newCoordinates);CHKERRQ(ierr);
492cd0c2139SMatthew G Knepley   ierr = PetscSectionDestroy(&newCoordSection);CHKERRQ(ierr);
493cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
494cd0c2139SMatthew G Knepley }
495cd0c2139SMatthew G Knepley 
496cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftSF_Internal(DM dm, PetscInt depthShift[], DM dmNew)
497cd0c2139SMatthew G Knepley {
4982582d50cSToby Isaac   PetscInt           depth = 0;
499cd0c2139SMatthew G Knepley   PetscSF            sfPoint, sfPointNew;
500cd0c2139SMatthew G Knepley   const PetscSFNode *remotePoints;
501cd0c2139SMatthew G Knepley   PetscSFNode       *gremotePoints;
502cd0c2139SMatthew G Knepley   const PetscInt    *localPoints;
503cd0c2139SMatthew G Knepley   PetscInt          *glocalPoints, *newLocation, *newRemoteLocation;
504cd0c2139SMatthew G Knepley   PetscInt           numRoots, numLeaves, l, pStart, pEnd, totShift = 0;
505cd0c2139SMatthew G Knepley   PetscErrorCode     ierr;
506cd0c2139SMatthew G Knepley 
507cd0c2139SMatthew G Knepley   PetscFunctionBegin;
508cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
509cd0c2139SMatthew G Knepley   /* Step 9: Convert pointSF */
510cd0c2139SMatthew G Knepley   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
511cd0c2139SMatthew G Knepley   ierr = DMGetPointSF(dmNew, &sfPointNew);CHKERRQ(ierr);
512cd0c2139SMatthew G Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
513cd0c2139SMatthew G Knepley   ierr = PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr);
5146e21efdcSToby Isaac   totShift = DMPlexShiftPoint_Internal(pEnd,depth,depthShift) - pEnd;
515cd0c2139SMatthew G Knepley   if (numRoots >= 0) {
516dcca6d9dSJed Brown     ierr = PetscMalloc2(numRoots,&newLocation,pEnd-pStart,&newRemoteLocation);CHKERRQ(ierr);
5172582d50cSToby Isaac     for (l=0; l<numRoots; l++) newLocation[l] = DMPlexShiftPoint_Internal(l, depth, depthShift);
518cd0c2139SMatthew G Knepley     ierr = PetscSFBcastBegin(sfPoint, MPIU_INT, newLocation, newRemoteLocation);CHKERRQ(ierr);
519cd0c2139SMatthew G Knepley     ierr = PetscSFBcastEnd(sfPoint, MPIU_INT, newLocation, newRemoteLocation);CHKERRQ(ierr);
520785e854fSJed Brown     ierr = PetscMalloc1(numLeaves,    &glocalPoints);CHKERRQ(ierr);
521785e854fSJed Brown     ierr = PetscMalloc1(numLeaves, &gremotePoints);CHKERRQ(ierr);
522cd0c2139SMatthew G Knepley     for (l = 0; l < numLeaves; ++l) {
5232582d50cSToby Isaac       glocalPoints[l]        = DMPlexShiftPoint_Internal(localPoints[l], depth, depthShift);
524cd0c2139SMatthew G Knepley       gremotePoints[l].rank  = remotePoints[l].rank;
525cd0c2139SMatthew G Knepley       gremotePoints[l].index = newRemoteLocation[localPoints[l]];
526cd0c2139SMatthew G Knepley     }
527cd0c2139SMatthew G Knepley     ierr = PetscFree2(newLocation,newRemoteLocation);CHKERRQ(ierr);
528cd0c2139SMatthew G Knepley     ierr = PetscSFSetGraph(sfPointNew, numRoots + totShift, numLeaves, glocalPoints, PETSC_OWN_POINTER, gremotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
529cd0c2139SMatthew G Knepley   }
530cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
531cd0c2139SMatthew G Knepley }
532cd0c2139SMatthew G Knepley 
533cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftLabels_Internal(DM dm, PetscInt depthShift[], DM dmNew)
534cd0c2139SMatthew G Knepley {
535cd0c2139SMatthew G Knepley   PetscSF            sfPoint;
536cd0c2139SMatthew G Knepley   DMLabel            vtkLabel, ghostLabel;
537cd0c2139SMatthew G Knepley   const PetscSFNode *leafRemote;
538cd0c2139SMatthew G Knepley   const PetscInt    *leafLocal;
5392582d50cSToby Isaac   PetscInt           depth = 0, numLeaves, numLabels, l, cStart, cEnd, c, fStart, fEnd, f;
540cd0c2139SMatthew G Knepley   PetscMPIInt        rank;
541cd0c2139SMatthew G Knepley   PetscErrorCode     ierr;
542cd0c2139SMatthew G Knepley 
543cd0c2139SMatthew G Knepley   PetscFunctionBegin;
544cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
545cd0c2139SMatthew G Knepley   /* Step 10: Convert labels */
546c58f1c22SToby Isaac   ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
547cd0c2139SMatthew G Knepley   for (l = 0; l < numLabels; ++l) {
548cd0c2139SMatthew G Knepley     DMLabel         label, newlabel;
549cd0c2139SMatthew G Knepley     const char     *lname;
550fa8e8ae5SToby Isaac     PetscBool       isDepth, isDim;
551cd0c2139SMatthew G Knepley     IS              valueIS;
552cd0c2139SMatthew G Knepley     const PetscInt *values;
553cd0c2139SMatthew G Knepley     PetscInt        numValues, val;
554cd0c2139SMatthew G Knepley 
555c58f1c22SToby Isaac     ierr = DMGetLabelName(dm, l, &lname);CHKERRQ(ierr);
556cd0c2139SMatthew G Knepley     ierr = PetscStrcmp(lname, "depth", &isDepth);CHKERRQ(ierr);
557cd0c2139SMatthew G Knepley     if (isDepth) continue;
558fa8e8ae5SToby Isaac     ierr = PetscStrcmp(lname, "dim", &isDim);CHKERRQ(ierr);
559fa8e8ae5SToby Isaac     if (isDim) continue;
560c58f1c22SToby Isaac     ierr = DMCreateLabel(dmNew, lname);CHKERRQ(ierr);
561c58f1c22SToby Isaac     ierr = DMGetLabel(dm, lname, &label);CHKERRQ(ierr);
562c58f1c22SToby Isaac     ierr = DMGetLabel(dmNew, lname, &newlabel);CHKERRQ(ierr);
56364a6a1a4SToby Isaac     ierr = DMLabelGetDefaultValue(label,&val);CHKERRQ(ierr);
56464a6a1a4SToby Isaac     ierr = DMLabelSetDefaultValue(newlabel,val);CHKERRQ(ierr);
565cd0c2139SMatthew G Knepley     ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
566cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(valueIS, &numValues);CHKERRQ(ierr);
567cd0c2139SMatthew G Knepley     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
568cd0c2139SMatthew G Knepley     for (val = 0; val < numValues; ++val) {
569cd0c2139SMatthew G Knepley       IS              pointIS;
570cd0c2139SMatthew G Knepley       const PetscInt *points;
571cd0c2139SMatthew G Knepley       PetscInt        numPoints, p;
572cd0c2139SMatthew G Knepley 
573cd0c2139SMatthew G Knepley       ierr = DMLabelGetStratumIS(label, values[val], &pointIS);CHKERRQ(ierr);
574cd0c2139SMatthew G Knepley       ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
575cd0c2139SMatthew G Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
576cd0c2139SMatthew G Knepley       for (p = 0; p < numPoints; ++p) {
5772582d50cSToby Isaac         const PetscInt newpoint = DMPlexShiftPoint_Internal(points[p], depth, depthShift);
578cd0c2139SMatthew G Knepley 
579cd0c2139SMatthew G Knepley         ierr = DMLabelSetValue(newlabel, newpoint, values[val]);CHKERRQ(ierr);
580cd0c2139SMatthew G Knepley       }
581cd0c2139SMatthew G Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
582cd0c2139SMatthew G Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
583cd0c2139SMatthew G Knepley     }
584cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
585cd0c2139SMatthew G Knepley     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
586cd0c2139SMatthew G Knepley   }
587cd0c2139SMatthew G Knepley   /* Step 11: Make label for output (vtk) and to mark ghost points (ghost) */
588cd0c2139SMatthew G Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
589cd0c2139SMatthew G Knepley   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
590cd0c2139SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
591cd0c2139SMatthew G Knepley   ierr = PetscSFGetGraph(sfPoint, NULL, &numLeaves, &leafLocal, &leafRemote);CHKERRQ(ierr);
592c58f1c22SToby Isaac   ierr = DMCreateLabel(dmNew, "vtk");CHKERRQ(ierr);
593c58f1c22SToby Isaac   ierr = DMCreateLabel(dmNew, "ghost");CHKERRQ(ierr);
594c58f1c22SToby Isaac   ierr = DMGetLabel(dmNew, "vtk", &vtkLabel);CHKERRQ(ierr);
595c58f1c22SToby Isaac   ierr = DMGetLabel(dmNew, "ghost", &ghostLabel);CHKERRQ(ierr);
596cd0c2139SMatthew G Knepley   for (l = 0, c = cStart; l < numLeaves && c < cEnd; ++l, ++c) {
597cd0c2139SMatthew G Knepley     for (; c < leafLocal[l] && c < cEnd; ++c) {
598cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(vtkLabel, c, 1);CHKERRQ(ierr);
599cd0c2139SMatthew G Knepley     }
600cd0c2139SMatthew G Knepley     if (leafLocal[l] >= cEnd) break;
601cd0c2139SMatthew G Knepley     if (leafRemote[l].rank == rank) {
602cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(vtkLabel, c, 1);CHKERRQ(ierr);
603cd0c2139SMatthew G Knepley     } else {
604cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(ghostLabel, c, 2);CHKERRQ(ierr);
605cd0c2139SMatthew G Knepley     }
606cd0c2139SMatthew G Knepley   }
607cd0c2139SMatthew G Knepley   for (; c < cEnd; ++c) {
608cd0c2139SMatthew G Knepley     ierr = DMLabelSetValue(vtkLabel, c, 1);CHKERRQ(ierr);
609cd0c2139SMatthew G Knepley   }
610cd0c2139SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dmNew, 1, &fStart, &fEnd);CHKERRQ(ierr);
611cd0c2139SMatthew G Knepley   for (f = fStart; f < fEnd; ++f) {
612cd0c2139SMatthew G Knepley     PetscInt numCells;
613cd0c2139SMatthew G Knepley 
614cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupportSize(dmNew, f, &numCells);CHKERRQ(ierr);
615cd0c2139SMatthew G Knepley     if (numCells < 2) {
616cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(ghostLabel, f, 1);CHKERRQ(ierr);
617cd0c2139SMatthew G Knepley     } else {
618cd0c2139SMatthew G Knepley       const PetscInt *cells = NULL;
619cd0c2139SMatthew G Knepley       PetscInt        vA, vB;
620cd0c2139SMatthew G Knepley 
621cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupport(dmNew, f, &cells);CHKERRQ(ierr);
622cd0c2139SMatthew G Knepley       ierr = DMLabelGetValue(vtkLabel, cells[0], &vA);CHKERRQ(ierr);
623cd0c2139SMatthew G Knepley       ierr = DMLabelGetValue(vtkLabel, cells[1], &vB);CHKERRQ(ierr);
62490664b96SToby Isaac       if (vA != 1 && vB != 1) {ierr = DMLabelSetValue(ghostLabel, f, 1);CHKERRQ(ierr);}
625cd0c2139SMatthew G Knepley     }
626cd0c2139SMatthew G Knepley   }
627cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
628cd0c2139SMatthew G Knepley }
629cd0c2139SMatthew G Knepley 
630ca04dac2SToby Isaac static PetscErrorCode DMPlexShiftTree_Internal(DM dm, PetscInt depthShift[], DM dmNew)
631ca04dac2SToby Isaac {
632ca04dac2SToby Isaac   DM             refTree;
633ca04dac2SToby Isaac   PetscSection   pSec;
634ca04dac2SToby Isaac   PetscInt       *parents, *childIDs;
635ca04dac2SToby Isaac   PetscErrorCode ierr;
636ca04dac2SToby Isaac 
637ca04dac2SToby Isaac   PetscFunctionBegin;
638ca04dac2SToby Isaac   ierr = DMPlexGetReferenceTree(dm,&refTree);CHKERRQ(ierr);
639ca04dac2SToby Isaac   ierr = DMPlexSetReferenceTree(dmNew,refTree);CHKERRQ(ierr);
640ca04dac2SToby Isaac   ierr = DMPlexGetTree(dm,&pSec,&parents,&childIDs,NULL,NULL);CHKERRQ(ierr);
641ca04dac2SToby Isaac   if (pSec) {
6422582d50cSToby Isaac     PetscInt p, pStart, pEnd, *parentsShifted, pStartShifted, pEndShifted, depth;
643fb4630b5SToby Isaac     PetscInt *childIDsShifted;
644ca04dac2SToby Isaac     PetscSection pSecShifted;
645ca04dac2SToby Isaac 
646ca04dac2SToby Isaac     ierr = PetscSectionGetChart(pSec,&pStart,&pEnd);CHKERRQ(ierr);
647ca04dac2SToby Isaac     ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
6482582d50cSToby Isaac     pStartShifted = DMPlexShiftPoint_Internal(pStart,depth,depthShift);
6492582d50cSToby Isaac     pEndShifted   = DMPlexShiftPoint_Internal(pEnd,depth,depthShift);
650fb4630b5SToby Isaac     ierr = PetscMalloc2(pEndShifted - pStartShifted,&parentsShifted,pEndShifted-pStartShifted,&childIDsShifted);CHKERRQ(ierr);
651ca04dac2SToby Isaac     ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dmNew),&pSecShifted);CHKERRQ(ierr);
652ca04dac2SToby Isaac     ierr = PetscSectionSetChart(pSecShifted,pStartShifted,pEndShifted);CHKERRQ(ierr);
653ca04dac2SToby Isaac     for (p = pStartShifted; p < pEndShifted; p++) {
654fb4630b5SToby Isaac       /* start off assuming no children */
655fb4630b5SToby Isaac       ierr = PetscSectionSetDof(pSecShifted,p,0);CHKERRQ(ierr);
656fb4630b5SToby Isaac     }
657fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
658fb4630b5SToby Isaac       PetscInt dof;
659fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p,depth,depthShift);
660ca04dac2SToby Isaac 
661ca04dac2SToby Isaac       ierr = PetscSectionGetDof(pSec,p,&dof);CHKERRQ(ierr);
662fb4630b5SToby Isaac       ierr = PetscSectionSetDof(pSecShifted,pNew,dof);CHKERRQ(ierr);
663ca04dac2SToby Isaac     }
664ca04dac2SToby Isaac     ierr = PetscSectionSetUp(pSecShifted);CHKERRQ(ierr);
665fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
666fb4630b5SToby Isaac       PetscInt dof;
667fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p,depth,depthShift);
668fb4630b5SToby Isaac 
669fb4630b5SToby Isaac       ierr = PetscSectionGetDof(pSec,p,&dof);CHKERRQ(ierr);
670fb4630b5SToby Isaac       if (dof) {
671fb4630b5SToby Isaac         PetscInt off, offNew;
672fb4630b5SToby Isaac 
673fb4630b5SToby Isaac         ierr = PetscSectionGetOffset(pSec,p,&off);CHKERRQ(ierr);
674fb4630b5SToby Isaac         ierr = PetscSectionGetOffset(pSecShifted,pNew,&offNew);CHKERRQ(ierr);
675fb4630b5SToby Isaac         parentsShifted[offNew] = DMPlexShiftPoint_Internal(parents[off],depth,depthShift);
676fb4630b5SToby Isaac         childIDsShifted[offNew] = childIDs[off];
677fb4630b5SToby Isaac       }
678fb4630b5SToby Isaac     }
679fb4630b5SToby Isaac     ierr = DMPlexSetTree(dmNew,pSecShifted,parentsShifted,childIDsShifted);CHKERRQ(ierr);
680fb4630b5SToby Isaac     ierr = PetscFree2(parentsShifted,childIDsShifted);CHKERRQ(ierr);
681e6885bbbSToby Isaac     ierr = PetscSectionDestroy(&pSecShifted);CHKERRQ(ierr);
682ca04dac2SToby Isaac   }
683ca04dac2SToby Isaac   PetscFunctionReturn(0);
684ca04dac2SToby Isaac }
685ca04dac2SToby Isaac 
686cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexConstructGhostCells_Internal(DM dm, DMLabel label, PetscInt *numGhostCells, DM gdm)
687cd0c2139SMatthew G Knepley {
688da97024aSMatthew G. Knepley   PetscSF               sf;
689cd0c2139SMatthew G Knepley   IS                    valueIS;
690da97024aSMatthew G. Knepley   const PetscInt       *values, *leaves;
691cd0c2139SMatthew G Knepley   PetscInt             *depthShift;
6922582d50cSToby Isaac   PetscInt              d, depth = 0, nleaves, loc, Ng, numFS, fs, fStart, fEnd, ghostCell, cEnd, c;
69390b157c4SStefano Zampini   PetscBool             isper;
69490b157c4SStefano Zampini   const PetscReal      *maxCell, *L;
69590b157c4SStefano Zampini   const DMBoundaryType *bd;
696cd0c2139SMatthew G Knepley   PetscErrorCode        ierr;
697cd0c2139SMatthew G Knepley 
698cd0c2139SMatthew G Knepley   PetscFunctionBegin;
699da97024aSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
700da97024aSMatthew G. Knepley   ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
701da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
70246c796b9SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
703cd0c2139SMatthew G Knepley   /* Count ghost cells */
704cd0c2139SMatthew G Knepley   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
705cd0c2139SMatthew G Knepley   ierr = ISGetLocalSize(valueIS, &numFS);CHKERRQ(ierr);
706cd0c2139SMatthew G Knepley   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
7074a6cfa73SMatthew G. Knepley   Ng   = 0;
708cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
70946c796b9SMatthew G. Knepley     IS              faceIS;
71046c796b9SMatthew G. Knepley     const PetscInt *faces;
71146c796b9SMatthew G. Knepley     PetscInt        numFaces, f, numBdFaces = 0;
712cd0c2139SMatthew G Knepley 
71346c796b9SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, values[fs], &faceIS);CHKERRQ(ierr);
71446c796b9SMatthew G. Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
71546c796b9SMatthew G. Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
71646c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
717ca04dac2SToby Isaac       PetscInt numChildren;
718ca04dac2SToby Isaac 
719da97024aSMatthew G. Knepley       ierr = PetscFindInt(faces[f], nleaves, leaves, &loc);CHKERRQ(ierr);
720ca04dac2SToby Isaac       ierr = DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL);CHKERRQ(ierr);
721ca04dac2SToby Isaac       /* non-local and ancestors points don't get to register ghosts */
722ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
72346c796b9SMatthew G. Knepley       if ((faces[f] >= fStart) && (faces[f] < fEnd)) ++numBdFaces;
72446c796b9SMatthew G. Knepley     }
7254a6cfa73SMatthew G. Knepley     Ng += numBdFaces;
72646c796b9SMatthew G. Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
727cd0c2139SMatthew G Knepley   }
728cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
7292582d50cSToby Isaac   ierr = PetscMalloc1(2*(depth+1), &depthShift);CHKERRQ(ierr);
7302582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
73159eef20bSToby Isaac     PetscInt dEnd;
7322582d50cSToby Isaac 
7330974a383SToby Isaac     ierr = DMPlexGetDepthStratum(dm,d,NULL,&dEnd);CHKERRQ(ierr);
73459eef20bSToby Isaac     depthShift[2*d]   = dEnd;
7352582d50cSToby Isaac     depthShift[2*d+1] = 0;
7362582d50cSToby Isaac   }
7372582d50cSToby Isaac   if (depth >= 0) depthShift[2*depth+1] = Ng;
7382582d50cSToby Isaac   ierr = DMPlexShiftPointSetUp_Internal(depth,depthShift);CHKERRQ(ierr);
739cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSizes_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
740cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
741cd0c2139SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
742*485ad865SMatthew G. Knepley   ierr = DMPlexSetGhostCellStratum(gdm, cEnd, PETSC_DETERMINE);CHKERRQ(ierr);
7434a6cfa73SMatthew G. Knepley   for (c = cEnd; c < cEnd + Ng; ++c) {
744cd0c2139SMatthew G Knepley     ierr = DMPlexSetConeSize(gdm, c, 1);CHKERRQ(ierr);
745cd0c2139SMatthew G Knepley   }
746cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
747cd0c2139SMatthew G Knepley     IS              faceIS;
748cd0c2139SMatthew G Knepley     const PetscInt *faces;
749cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
750cd0c2139SMatthew G Knepley 
751cd0c2139SMatthew G Knepley     ierr = DMLabelGetStratumIS(label, values[fs], &faceIS);CHKERRQ(ierr);
752cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
753cd0c2139SMatthew G Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
754cd0c2139SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
755ca04dac2SToby Isaac       PetscInt size, numChildren;
756cd0c2139SMatthew G Knepley 
757da97024aSMatthew G. Knepley       ierr = PetscFindInt(faces[f], nleaves, leaves, &loc);CHKERRQ(ierr);
758ca04dac2SToby Isaac       ierr = DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL);CHKERRQ(ierr);
759ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
76046c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
761cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, faces[f], &size);CHKERRQ(ierr);
7624553fd90SToby Isaac       if (size != 1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM has boundary face %d with %d support cells", faces[f], size);
7634a6cfa73SMatthew G. Knepley       ierr = DMPlexSetSupportSize(gdm, faces[f] + Ng, 2);CHKERRQ(ierr);
764cd0c2139SMatthew G Knepley     }
765cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
766cd0c2139SMatthew G Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
767cd0c2139SMatthew G Knepley   }
768cd0c2139SMatthew G Knepley   /* Step 4: Setup ghosted DM */
769cd0c2139SMatthew G Knepley   ierr = DMSetUp(gdm);CHKERRQ(ierr);
770cd0c2139SMatthew G Knepley   ierr = DMPlexShiftPoints_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
771cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
772cd0c2139SMatthew G Knepley   ghostCell = cEnd;
773cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
774cd0c2139SMatthew G Knepley     IS              faceIS;
775cd0c2139SMatthew G Knepley     const PetscInt *faces;
776cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
777cd0c2139SMatthew G Knepley 
778cd0c2139SMatthew G Knepley     ierr = DMLabelGetStratumIS(label, values[fs], &faceIS);CHKERRQ(ierr);
779cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
780cd0c2139SMatthew G Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
78146c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
782ca04dac2SToby Isaac       PetscInt newFace = faces[f] + Ng, numChildren;
783cd0c2139SMatthew G Knepley 
784da97024aSMatthew G. Knepley       ierr = PetscFindInt(faces[f], nleaves, leaves, &loc);CHKERRQ(ierr);
785ca04dac2SToby Isaac       ierr = DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL);CHKERRQ(ierr);
786ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
78746c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
788cd0c2139SMatthew G Knepley       ierr = DMPlexSetCone(gdm, ghostCell, &newFace);CHKERRQ(ierr);
789cd0c2139SMatthew G Knepley       ierr = DMPlexInsertSupport(gdm, newFace, 1, ghostCell);CHKERRQ(ierr);
79046c796b9SMatthew G. Knepley       ++ghostCell;
791cd0c2139SMatthew G Knepley     }
792cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
793cd0c2139SMatthew G Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
794cd0c2139SMatthew G Knepley   }
795cd0c2139SMatthew G Knepley   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
796cd0c2139SMatthew G Knepley   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
797cd0c2139SMatthew G Knepley   ierr = DMPlexShiftCoordinates_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
798cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSF_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
799cd0c2139SMatthew G Knepley   ierr = DMPlexShiftLabels_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
800ca04dac2SToby Isaac   ierr = DMPlexShiftTree_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
801cd0c2139SMatthew G Knepley   ierr = PetscFree(depthShift);CHKERRQ(ierr);
802966c7b3fSMatthew G. Knepley   /* Step 7: Periodicity */
80390b157c4SStefano Zampini   ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
80490b157c4SStefano Zampini   ierr = DMSetPeriodicity(gdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
8054a6cfa73SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
806cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
807cd0c2139SMatthew G Knepley }
808cd0c2139SMatthew G Knepley 
809cd0c2139SMatthew G Knepley /*@C
810cd0c2139SMatthew G Knepley   DMPlexConstructGhostCells - Construct ghost cells which connect to every boundary face
811cd0c2139SMatthew G Knepley 
812cd0c2139SMatthew G Knepley   Collective on dm
813cd0c2139SMatthew G Knepley 
814cd0c2139SMatthew G Knepley   Input Parameters:
815cd0c2139SMatthew G Knepley + dm - The original DM
816cd0c2139SMatthew G Knepley - labelName - The label specifying the boundary faces, or "Face Sets" if this is NULL
817cd0c2139SMatthew G Knepley 
818cd0c2139SMatthew G Knepley   Output Parameters:
819cd0c2139SMatthew G Knepley + numGhostCells - The number of ghost cells added to the DM
820cd0c2139SMatthew G Knepley - dmGhosted - The new DM
821cd0c2139SMatthew G Knepley 
822cd0c2139SMatthew G Knepley   Note: If no label exists of that name, one will be created marking all boundary faces
823cd0c2139SMatthew G Knepley 
824cd0c2139SMatthew G Knepley   Level: developer
825cd0c2139SMatthew G Knepley 
826cd0c2139SMatthew G Knepley .seealso: DMCreate()
82731266bc0SMatthew G. Knepley @*/
828cd0c2139SMatthew G Knepley PetscErrorCode DMPlexConstructGhostCells(DM dm, const char labelName[], PetscInt *numGhostCells, DM *dmGhosted)
829cd0c2139SMatthew G Knepley {
830cd0c2139SMatthew G Knepley   DM             gdm;
831cd0c2139SMatthew G Knepley   DMLabel        label;
832cd0c2139SMatthew G Knepley   const char    *name = labelName ? labelName : "Face Sets";
833cd0c2139SMatthew G Knepley   PetscInt       dim;
834b0441da4SMatthew G. Knepley   PetscBool      useCone, useClosure;
835cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
836cd0c2139SMatthew G Knepley 
837cd0c2139SMatthew G Knepley   PetscFunctionBegin;
838cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8394a6cfa73SMatthew G. Knepley   if (numGhostCells) PetscValidPointer(numGhostCells, 3);
840cd0c2139SMatthew G Knepley   PetscValidPointer(dmGhosted, 4);
841cd0c2139SMatthew G Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), &gdm);CHKERRQ(ierr);
842cd0c2139SMatthew G Knepley   ierr = DMSetType(gdm, DMPLEX);CHKERRQ(ierr);
843c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
844c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(gdm, dim);CHKERRQ(ierr);
845b0441da4SMatthew G. Knepley   ierr = DMGetBasicAdjacency(dm, &useCone, &useClosure);CHKERRQ(ierr);
846b0441da4SMatthew G. Knepley   ierr = DMSetBasicAdjacency(gdm, useCone,  useClosure);CHKERRQ(ierr);
847c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
848cd0c2139SMatthew G Knepley   if (!label) {
849cd0c2139SMatthew G Knepley     /* Get label for boundary faces */
850c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
851c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
852e752be1aSMatthew G. Knepley     ierr = DMPlexMarkBoundaryFaces(dm, 1, label);CHKERRQ(ierr);
853cd0c2139SMatthew G Knepley   }
854cd0c2139SMatthew G Knepley   ierr = DMPlexConstructGhostCells_Internal(dm, label, numGhostCells, gdm);CHKERRQ(ierr);
855a6ba4734SToby Isaac   ierr = DMCopyBoundary(dm, gdm);CHKERRQ(ierr);
856b0441da4SMatthew G. Knepley   ierr = DMCopyDisc(dm, gdm);CHKERRQ(ierr);
857cad26855SMatthew G. Knepley   gdm->setfromoptionscalled = dm->setfromoptionscalled;
858cd0c2139SMatthew G Knepley   *dmGhosted = gdm;
859cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
860cd0c2139SMatthew G Knepley }
861cd0c2139SMatthew G Knepley 
862607ab7a9SMatthew G. Knepley /*
863faedd622SMatthew G. Knepley   We are adding three kinds of points here:
864607ab7a9SMatthew G. Knepley     Replicated:     Copies of points which exist in the mesh, such as vertices identified across a fault
865faedd622SMatthew G. Knepley     Non-replicated: Points which exist on the fault, but are not replicated
866607ab7a9SMatthew G. Knepley     Hybrid:         Entirely new points, such as cohesive cells
867a6ae58d1SMatthew G. Knepley 
868a6ae58d1SMatthew G. Knepley   When creating subsequent cohesive cells, we shift the old hybrid cells to the end of the numbering at
869a6ae58d1SMatthew G. Knepley   each depth so that the new split/hybrid points can be inserted as a block.
870607ab7a9SMatthew G. Knepley */
8717db7e0a7SMatthew G. Knepley static PetscErrorCode DMPlexConstructCohesiveCells_Internal(DM dm, DMLabel label, DMLabel splitLabel, DM sdm)
872cd0c2139SMatthew G Knepley {
873cd0c2139SMatthew G Knepley   MPI_Comm         comm;
874607ab7a9SMatthew G. Knepley   IS               valueIS;
875607ab7a9SMatthew G. Knepley   PetscInt         numSP = 0;       /* The number of depths for which we have replicated points */
876607ab7a9SMatthew G. Knepley   const PetscInt  *values;          /* List of depths for which we have replicated points */
87718c5995bSMatthew G. Knepley   IS              *splitIS;
87818c5995bSMatthew G. Knepley   IS              *unsplitIS;
879607ab7a9SMatthew G. Knepley   PetscInt        *numSplitPoints;     /* The number of replicated points at each depth */
88018c5995bSMatthew G. Knepley   PetscInt        *numUnsplitPoints;   /* The number of non-replicated points at each depth which still give rise to hybrid points */
88136dbac82SMatthew G. Knepley   PetscInt        *numHybridPoints;    /* The number of new hybrid points at each depth */
88236dbac82SMatthew G. Knepley   PetscInt        *numHybridPointsOld; /* The number of existing hybrid points at each depth */
883607ab7a9SMatthew G. Knepley   const PetscInt **splitPoints;        /* Replicated points for each depth */
88418c5995bSMatthew G. Knepley   const PetscInt **unsplitPoints;      /* Non-replicated points for each depth */
885cd0c2139SMatthew G Knepley   PetscSection     coordSection;
886cd0c2139SMatthew G Knepley   Vec              coordinates;
887cd0c2139SMatthew G Knepley   PetscScalar     *coords;
888a6ae58d1SMatthew G. Knepley   PetscInt        *depthMax;           /* The first hybrid point at each depth in the original mesh */
889a6ae58d1SMatthew G. Knepley   PetscInt        *depthEnd;           /* The point limit at each depth in the original mesh */
890607ab7a9SMatthew G. Knepley   PetscInt        *depthShift;         /* Number of replicated+hybrid points at each depth */
891607ab7a9SMatthew G. Knepley   PetscInt        *pMaxNew;            /* The first replicated point at each depth in the new mesh, hybrids come after this */
892607ab7a9SMatthew G. Knepley   PetscInt        *coneNew, *coneONew, *supportNew;
89318c5995bSMatthew G. Knepley   PetscInt         shift = 100, shift2 = 200, depth = 0, dep, dim, d, sp, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, numLabels, vStart, vEnd, pEnd, p, v;
894cd0c2139SMatthew G Knepley   PetscErrorCode   ierr;
895cd0c2139SMatthew G Knepley 
896cd0c2139SMatthew G Knepley   PetscFunctionBegin;
897cd0c2139SMatthew G Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
898c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
899607ab7a9SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
900fd4b9f15SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
901cd0c2139SMatthew G Knepley   /* Count split points and add cohesive cells */
902607ab7a9SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
9032582d50cSToby Isaac   ierr = PetscMalloc5(depth+1,&depthMax,depth+1,&depthEnd,2*(depth+1),&depthShift,depth+1,&pMaxNew,depth+1,&numHybridPointsOld);CHKERRQ(ierr);
904dcca6d9dSJed 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);
905a6ae58d1SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, depth >= 0 ? &depthMax[depth] : NULL, depth>1 ? &depthMax[depth-1] : NULL, depth>2 ? &depthMax[1] : NULL, depth >= 0 ? &depthMax[0] : NULL);CHKERRQ(ierr);
906607ab7a9SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
907607ab7a9SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, d, NULL, &pMaxNew[d]);CHKERRQ(ierr);
908a6ae58d1SMatthew G. Knepley     depthEnd[d]           = pMaxNew[d];
909a6ae58d1SMatthew G. Knepley     depthMax[d]           = depthMax[d] < 0 ? depthEnd[d] : depthMax[d];
910607ab7a9SMatthew G. Knepley     numSplitPoints[d]     = 0;
91118c5995bSMatthew G. Knepley     numUnsplitPoints[d]   = 0;
912607ab7a9SMatthew G. Knepley     numHybridPoints[d]    = 0;
913a6ae58d1SMatthew G. Knepley     numHybridPointsOld[d] = depthMax[d] < 0 ? 0 : depthEnd[d] - depthMax[d];
914607ab7a9SMatthew G. Knepley     splitPoints[d]        = NULL;
91518c5995bSMatthew G. Knepley     unsplitPoints[d]      = NULL;
91618c5995bSMatthew G. Knepley     splitIS[d]            = NULL;
91718c5995bSMatthew G. Knepley     unsplitIS[d]          = NULL;
91859eef20bSToby Isaac     /* we are shifting the existing hybrid points with the stratum behind them, so
91959eef20bSToby Isaac      * the split comes at the end of the normal points, i.e., at depthMax[d] */
92059eef20bSToby Isaac     depthShift[2*d]       = depthMax[d];
92159eef20bSToby Isaac     depthShift[2*d+1]     = 0;
922607ab7a9SMatthew G. Knepley   }
923cd0c2139SMatthew G Knepley   if (label) {
924cd0c2139SMatthew G Knepley     ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
925cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(valueIS, &numSP);CHKERRQ(ierr);
926cd0c2139SMatthew G Knepley     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
927cd0c2139SMatthew G Knepley   }
928cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
929cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
930cd0c2139SMatthew G Knepley 
931cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
93218c5995bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, dep, &splitIS[dep]);CHKERRQ(ierr);
93318c5995bSMatthew G. Knepley     if (splitIS[dep]) {
93418c5995bSMatthew G. Knepley       ierr = ISGetLocalSize(splitIS[dep], &numSplitPoints[dep]);CHKERRQ(ierr);
93518c5995bSMatthew G. Knepley       ierr = ISGetIndices(splitIS[dep], &splitPoints[dep]);CHKERRQ(ierr);
93618c5995bSMatthew G. Knepley     }
93718c5995bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, shift2+dep, &unsplitIS[dep]);CHKERRQ(ierr);
93818c5995bSMatthew G. Knepley     if (unsplitIS[dep]) {
93918c5995bSMatthew G. Knepley       ierr = ISGetLocalSize(unsplitIS[dep], &numUnsplitPoints[dep]);CHKERRQ(ierr);
94018c5995bSMatthew G. Knepley       ierr = ISGetIndices(unsplitIS[dep], &unsplitPoints[dep]);CHKERRQ(ierr);
941cd0c2139SMatthew G Knepley     }
942cd0c2139SMatthew G Knepley   }
943607ab7a9SMatthew G. Knepley   /* Calculate number of hybrid points */
94418c5995bSMatthew 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   */
9452582d50cSToby Isaac   for (d = 0; d <= depth; ++d) depthShift[2*d+1]      = numSplitPoints[d] + numHybridPoints[d];
9463d3eaba7SBarry Smith   ierr = DMPlexShiftPointSetUp_Internal(depth,depthShift);CHKERRQ(ierr);
94759eef20bSToby Isaac   /* the end of the points in this stratum that come before the new points:
94859eef20bSToby Isaac    * shifting pMaxNew[d] gets the new start of the next stratum, then count back the old hybrid points and the newly
94959eef20bSToby Isaac    * added points */
9502582d50cSToby Isaac   for (d = 0; d <= depth; ++d) pMaxNew[d]             = DMPlexShiftPoint_Internal(pMaxNew[d],depth,depthShift) - (numHybridPointsOld[d] + numSplitPoints[d] + numHybridPoints[d]);
951cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSizes_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
952cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
953cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
954cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
955cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
9562582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
9574c367dbcSMatthew G. Knepley       const PetscInt  splitp = p    + pMaxNew[dep];
958cd0c2139SMatthew G Knepley       const PetscInt *support;
9594c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, qn, qp, e;
960cd0c2139SMatthew G Knepley 
961cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
962cd0c2139SMatthew G Knepley       ierr = DMPlexSetConeSize(sdm, splitp, coneSize);CHKERRQ(ierr);
963cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
964cd0c2139SMatthew G Knepley       ierr = DMPlexSetSupportSize(sdm, splitp, supportSize);CHKERRQ(ierr);
965cd0c2139SMatthew G Knepley       if (dep == depth-1) {
9664c367dbcSMatthew G. Knepley         const PetscInt hybcell = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
9674c367dbcSMatthew G. Knepley 
968cd0c2139SMatthew G Knepley         /* Add cohesive cells, they are prisms */
9694c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybcell, 2 + coneSize);CHKERRQ(ierr);
970cd0c2139SMatthew G Knepley       } else if (dep == 0) {
9714c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
972cd0c2139SMatthew G Knepley 
973cd0c2139SMatthew G Knepley         ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
9744c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
975cd0c2139SMatthew G Knepley           PetscInt val;
976cd0c2139SMatthew G Knepley 
977cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
9784c367dbcSMatthew G. Knepley           if (val == 1) ++qf;
9794c367dbcSMatthew G. Knepley           if ((val == 1) || (val ==  (shift + 1))) ++qn;
9804c367dbcSMatthew G. Knepley           if ((val == 1) || (val == -(shift + 1))) ++qp;
981cd0c2139SMatthew G Knepley         }
9824c367dbcSMatthew G. Knepley         /* Split old vertex: Edges into original vertex and new cohesive edge */
9834c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qn+1);CHKERRQ(ierr);
9844c367dbcSMatthew G. Knepley         /* Split new vertex: Edges into split vertex and new cohesive edge */
9854c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, splitp, qp+1);CHKERRQ(ierr);
9864c367dbcSMatthew G. Knepley         /* Add hybrid edge */
9874c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybedge, 2);CHKERRQ(ierr);
9884c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybedge, qf);CHKERRQ(ierr);
989cd0c2139SMatthew G Knepley       } else if (dep == dim-2) {
9904c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
9914c367dbcSMatthew G. Knepley 
992cd0c2139SMatthew G Knepley         ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
9934c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
994cd0c2139SMatthew G Knepley           PetscInt val;
995cd0c2139SMatthew G Knepley 
996cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
9974c367dbcSMatthew G. Knepley           if (val == dim-1) ++qf;
9984c367dbcSMatthew G. Knepley           if ((val == dim-1) || (val ==  (shift + dim-1))) ++qn;
9994c367dbcSMatthew G. Knepley           if ((val == dim-1) || (val == -(shift + dim-1))) ++qp;
1000cd0c2139SMatthew G Knepley         }
10014c367dbcSMatthew G. Knepley         /* Split old edge: Faces into original edge and cohesive face (positive side?) */
10024c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qn+1);CHKERRQ(ierr);
10034c367dbcSMatthew G. Knepley         /* Split new edge: Faces into split edge and cohesive face (negative side?) */
10044c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, splitp, qp+1);CHKERRQ(ierr);
10054c367dbcSMatthew G. Knepley         /* Add hybrid face */
10064c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybface, 4);CHKERRQ(ierr);
10074c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybface, qf);CHKERRQ(ierr);
1008cd0c2139SMatthew G Knepley       }
1009cd0c2139SMatthew G Knepley     }
1010cd0c2139SMatthew G Knepley   }
101118c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
101218c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
101318c5995bSMatthew G. Knepley       const PetscInt  oldp   = unsplitPoints[dep][p];
10142582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
101518c5995bSMatthew G. Knepley       const PetscInt *support;
1016da1dd7e4SMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, e, s;
101718c5995bSMatthew G. Knepley 
101818c5995bSMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
101918c5995bSMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
102039254ff6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
102118c5995bSMatthew G. Knepley       if (dep == 0) {
102218c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
102318c5995bSMatthew G. Knepley 
102439254ff6SMatthew G. Knepley         /* Unsplit vertex: Edges into original vertex, split edges, and new cohesive edge twice */
102539254ff6SMatthew G. Knepley         for (s = 0, qf = 0; s < supportSize; ++s, ++qf) {
102639254ff6SMatthew G. Knepley           ierr = PetscFindInt(support[s], numSplitPoints[dep+1], splitPoints[dep+1], &e);CHKERRQ(ierr);
102739254ff6SMatthew G. Knepley           if (e >= 0) ++qf;
102839254ff6SMatthew G. Knepley         }
102939254ff6SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qf+2);CHKERRQ(ierr);
103018c5995bSMatthew G. Knepley         /* Add hybrid edge */
103118c5995bSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybedge, 2);CHKERRQ(ierr);
1032e1757548SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1033e1757548SMatthew G. Knepley           PetscInt val;
1034e1757548SMatthew G. Knepley 
1035e1757548SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1036e1757548SMatthew G. Knepley           /* Split and unsplit edges produce hybrid faces */
1037da1dd7e4SMatthew G. Knepley           if (val == 1) ++qf;
1038da1dd7e4SMatthew G. Knepley           if (val == (shift2 + 1)) ++qf;
1039e1757548SMatthew G. Knepley         }
1040e1757548SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybedge, qf);CHKERRQ(ierr);
104118c5995bSMatthew G. Knepley       } else if (dep == dim-2) {
104218c5995bSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
1043cd0c2139SMatthew G Knepley         PetscInt       val;
1044cd0c2139SMatthew G Knepley 
1045da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1046cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1047da1dd7e4SMatthew G. Knepley           if (val == dim-1) qf += 2;
1048da1dd7e4SMatthew G. Knepley           else              ++qf;
1049cd0c2139SMatthew G Knepley         }
105018c5995bSMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and cohesive face twice */
1051da1dd7e4SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qf+2);CHKERRQ(ierr);
105218c5995bSMatthew G. Knepley         /* Add hybrid face */
1053da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1054da1dd7e4SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1055da1dd7e4SMatthew G. Knepley           if (val == dim-1) ++qf;
1056da1dd7e4SMatthew G. Knepley         }
105718c5995bSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybface, 4);CHKERRQ(ierr);
1058da1dd7e4SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybface, qf);CHKERRQ(ierr);
1059cd0c2139SMatthew G Knepley       }
1060cd0c2139SMatthew G Knepley     }
1061cd0c2139SMatthew G Knepley   }
1062cd0c2139SMatthew G Knepley   /* Step 4: Setup split DM */
1063cd0c2139SMatthew G Knepley   ierr = DMSetUp(sdm);CHKERRQ(ierr);
1064cd0c2139SMatthew G Knepley   ierr = DMPlexShiftPoints_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
1065dcbb62e8SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(sdm, &maxConeSizeNew, &maxSupportSizeNew);CHKERRQ(ierr);
1066dcca6d9dSJed Brown   ierr = PetscMalloc3(PetscMax(maxConeSize, maxConeSizeNew)*3,&coneNew,PetscMax(maxConeSize, maxConeSizeNew)*3,&coneONew,PetscMax(maxSupportSize, maxSupportSizeNew),&supportNew);CHKERRQ(ierr);
1067cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
1068cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1069cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1070cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
10712582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
10724c367dbcSMatthew G. Knepley       const PetscInt  splitp = p    + pMaxNew[dep];
1073cd0c2139SMatthew G Knepley       const PetscInt *cone, *support, *ornt;
10744c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, q, qf, qn, qp, v, e, s;
1075cd0c2139SMatthew G Knepley 
1076cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
1077cd0c2139SMatthew G Knepley       ierr = DMPlexGetCone(dm, oldp, &cone);CHKERRQ(ierr);
1078cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeOrientation(dm, oldp, &ornt);CHKERRQ(ierr);
1079cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
1080cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
1081cd0c2139SMatthew G Knepley       if (dep == depth-1) {
108296a07cd0SMatthew G. Knepley         PetscBool       hasUnsplit = PETSC_FALSE;
10834c367dbcSMatthew G. Knepley         const PetscInt  hybcell    = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1084cd0c2139SMatthew G Knepley         const PetscInt *supportF;
1085cd0c2139SMatthew G Knepley 
1086cd0c2139SMatthew G Knepley         /* Split face:       copy in old face to new face to start */
1087cd0c2139SMatthew G Knepley         ierr = DMPlexGetSupport(sdm, newp,  &supportF);CHKERRQ(ierr);
1088cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, splitp, supportF);CHKERRQ(ierr);
1089cd0c2139SMatthew G Knepley         /* Split old face:   old vertices/edges in cone so no change */
1090cd0c2139SMatthew G Knepley         /* Split new face:   new vertices/edges in cone */
1091cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
10924c367dbcSMatthew G. Knepley           ierr = PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);CHKERRQ(ierr);
109318c5995bSMatthew G. Knepley           if (v < 0) {
109418c5995bSMatthew G. Knepley             ierr = PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
109518c5995bSMatthew G. Knepley             if (v < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %d in split or unsplit points of depth %d", cone[q], dep-1);
10962582d50cSToby Isaac             coneNew[2+q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
109796a07cd0SMatthew G. Knepley             hasUnsplit   = PETSC_TRUE;
109818c5995bSMatthew G. Knepley           } else {
10994c367dbcSMatthew G. Knepley             coneNew[2+q] = v + pMaxNew[dep-1];
1100163235baSMatthew G. Knepley             if (dep > 1) {
1101163235baSMatthew G. Knepley               const PetscInt *econe;
1102163235baSMatthew G. Knepley               PetscInt        econeSize, r, vs, vu;
1103163235baSMatthew G. Knepley 
1104163235baSMatthew G. Knepley               ierr = DMPlexGetConeSize(dm, cone[q], &econeSize);CHKERRQ(ierr);
1105163235baSMatthew G. Knepley               ierr = DMPlexGetCone(dm, cone[q], &econe);CHKERRQ(ierr);
1106163235baSMatthew G. Knepley               for (r = 0; r < econeSize; ++r) {
1107163235baSMatthew G. Knepley                 ierr = PetscFindInt(econe[r], numSplitPoints[dep-2],   splitPoints[dep-2],   &vs);CHKERRQ(ierr);
1108163235baSMatthew G. Knepley                 ierr = PetscFindInt(econe[r], numUnsplitPoints[dep-2], unsplitPoints[dep-2], &vu);CHKERRQ(ierr);
1109163235baSMatthew G. Knepley                 if (vs >= 0) continue;
1110163235baSMatthew G. Knepley                 if (vu < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %d in split or unsplit points of depth %d", econe[r], dep-2);
1111163235baSMatthew G. Knepley                 hasUnsplit   = PETSC_TRUE;
1112163235baSMatthew G. Knepley               }
1113163235baSMatthew G. Knepley             }
1114cd0c2139SMatthew G Knepley           }
1115cd0c2139SMatthew G Knepley         }
1116cd0c2139SMatthew G Knepley         ierr = DMPlexSetCone(sdm, splitp, &coneNew[2]);CHKERRQ(ierr);
1117cd0c2139SMatthew G Knepley         ierr = DMPlexSetConeOrientation(sdm, splitp, ornt);CHKERRQ(ierr);
1118e537020bSMatthew G. Knepley         /* Face support */
1119cd0c2139SMatthew G Knepley         for (s = 0; s < supportSize; ++s) {
1120cd0c2139SMatthew G Knepley           PetscInt val;
1121cd0c2139SMatthew G Knepley 
1122cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[s], &val);CHKERRQ(ierr);
1123cd0c2139SMatthew G Knepley           if (val < 0) {
1124cd0c2139SMatthew G Knepley             /* Split old face:   Replace negative side cell with cohesive cell */
11254c367dbcSMatthew G. Knepley              ierr = DMPlexInsertSupport(sdm, newp, s, hybcell);CHKERRQ(ierr);
1126cd0c2139SMatthew G Knepley           } else {
1127cd0c2139SMatthew G Knepley             /* Split new face:   Replace positive side cell with cohesive cell */
11284c367dbcSMatthew G. Knepley             ierr = DMPlexInsertSupport(sdm, splitp, s, hybcell);CHKERRQ(ierr);
1129e537020bSMatthew G. Knepley             /* Get orientation for cohesive face */
1130e537020bSMatthew G. Knepley             {
1131e537020bSMatthew G. Knepley               const PetscInt *ncone, *nconeO;
1132e537020bSMatthew G. Knepley               PetscInt        nconeSize, nc;
1133e537020bSMatthew G. Knepley 
1134e537020bSMatthew G. Knepley               ierr = DMPlexGetConeSize(dm, support[s], &nconeSize);CHKERRQ(ierr);
1135e537020bSMatthew G. Knepley               ierr = DMPlexGetCone(dm, support[s], &ncone);CHKERRQ(ierr);
1136e537020bSMatthew G. Knepley               ierr = DMPlexGetConeOrientation(dm, support[s], &nconeO);CHKERRQ(ierr);
1137e537020bSMatthew G. Knepley               for (nc = 0; nc < nconeSize; ++nc) {
1138e537020bSMatthew G. Knepley                 if (ncone[nc] == oldp) {
1139e537020bSMatthew G. Knepley                   coneONew[0] = nconeO[nc];
1140e537020bSMatthew G. Knepley                   break;
1141cd0c2139SMatthew G Knepley                 }
1142cd0c2139SMatthew G Knepley               }
1143e537020bSMatthew G. Knepley               if (nc >= nconeSize) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate face %d in neighboring cell %d", oldp, support[s]);
1144e537020bSMatthew G. Knepley             }
1145e537020bSMatthew G. Knepley           }
1146e537020bSMatthew G. Knepley         }
11474c367dbcSMatthew G. Knepley         /* Cohesive cell:    Old and new split face, then new cohesive faces */
1148fd4b9f15SMatthew G. Knepley         coneNew[0]  = newp;   /* Extracted negative side orientation above */
11494c367dbcSMatthew G. Knepley         coneNew[1]  = splitp;
11504c367dbcSMatthew G. Knepley         coneONew[1] = coneONew[0];
1151e537020bSMatthew G. Knepley         for (q = 0; q < coneSize; ++q) {
115218c5995bSMatthew G. Knepley           ierr = PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);CHKERRQ(ierr);
115318c5995bSMatthew G. Knepley           if (v < 0) {
115418c5995bSMatthew G. Knepley             ierr = PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
115518c5995bSMatthew G. Knepley             coneNew[2+q]  = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
115618c5995bSMatthew G. Knepley             coneONew[2+q] = 0;
115718c5995bSMatthew G. Knepley           } else {
115818c5995bSMatthew G. Knepley             coneNew[2+q]  = v + pMaxNew[dep] + numSplitPoints[dep];
115918c5995bSMatthew G. Knepley           }
1160e537020bSMatthew G. Knepley           coneONew[2+q] = 0;
1161e537020bSMatthew G. Knepley         }
11624c367dbcSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybcell, coneNew);CHKERRQ(ierr);
11634c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeOrientation(sdm, hybcell, coneONew);CHKERRQ(ierr);
116496a07cd0SMatthew G. Knepley         /* Label the hybrid cells on the boundary of the split */
116596a07cd0SMatthew G. Knepley         if (hasUnsplit) {ierr = DMLabelSetValue(label, -hybcell, dim);CHKERRQ(ierr);}
1166cd0c2139SMatthew G Knepley       } else if (dep == 0) {
11674c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1168cd0c2139SMatthew G Knepley 
1169cd0c2139SMatthew G Knepley         /* Split old vertex: Edges in old split faces and new cohesive edge */
11704c367dbcSMatthew G. Knepley         for (e = 0, qn = 0; e < supportSize; ++e) {
1171cd0c2139SMatthew G Knepley           PetscInt val;
1172cd0c2139SMatthew G Knepley 
1173cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1174cd0c2139SMatthew G Knepley           if ((val == 1) || (val == (shift + 1))) {
11752582d50cSToby Isaac             supportNew[qn++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1176cd0c2139SMatthew G Knepley           }
1177cd0c2139SMatthew G Knepley         }
11784c367dbcSMatthew G. Knepley         supportNew[qn] = hybedge;
1179cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
1180cd0c2139SMatthew G Knepley         /* Split new vertex: Edges in new split faces and new cohesive edge */
11814c367dbcSMatthew G. Knepley         for (e = 0, qp = 0; e < supportSize; ++e) {
1182cd0c2139SMatthew G Knepley           PetscInt val, edge;
1183cd0c2139SMatthew G Knepley 
1184cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1185cd0c2139SMatthew G Knepley           if (val == 1) {
11864c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);CHKERRQ(ierr);
1187cd0c2139SMatthew G Knepley             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
11884c367dbcSMatthew G. Knepley             supportNew[qp++] = edge + pMaxNew[dep+1];
1189cd0c2139SMatthew G Knepley           } else if (val == -(shift + 1)) {
11902582d50cSToby Isaac             supportNew[qp++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1191cd0c2139SMatthew G Knepley           }
1192cd0c2139SMatthew G Knepley         }
11934c367dbcSMatthew G. Knepley         supportNew[qp] = hybedge;
1194cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, splitp, supportNew);CHKERRQ(ierr);
11954c367dbcSMatthew G. Knepley         /* Hybrid edge:    Old and new split vertex */
1196cd0c2139SMatthew G Knepley         coneNew[0] = newp;
1197cd0c2139SMatthew G Knepley         coneNew[1] = splitp;
11984c367dbcSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybedge, coneNew);CHKERRQ(ierr);
11994c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
12004c367dbcSMatthew G. Knepley           PetscInt val, edge;
12014c367dbcSMatthew G. Knepley 
12024c367dbcSMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
12034c367dbcSMatthew G. Knepley           if (val == 1) {
12044c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);CHKERRQ(ierr);
12054c367dbcSMatthew G. Knepley             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
12064c367dbcSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2];
12074c367dbcSMatthew G. Knepley           }
12084c367dbcSMatthew G. Knepley         }
12094c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybedge, supportNew);CHKERRQ(ierr);
1210cd0c2139SMatthew G Knepley       } else if (dep == dim-2) {
12114c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
12124c367dbcSMatthew G. Knepley 
1213cd0c2139SMatthew G Knepley         /* Split old edge:   old vertices in cone so no change */
1214cd0c2139SMatthew G Knepley         /* Split new edge:   new vertices in cone */
1215cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
12164c367dbcSMatthew G. Knepley           ierr = PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);CHKERRQ(ierr);
1217e1757548SMatthew G. Knepley           if (v < 0) {
1218e1757548SMatthew G. Knepley             ierr = PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
1219e1757548SMatthew G. Knepley             if (v < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %d in split or unsplit points of depth %d", cone[q], dep-1);
12202582d50cSToby Isaac             coneNew[q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
1221e1757548SMatthew G. Knepley           } else {
12224c367dbcSMatthew G. Knepley             coneNew[q] = v + pMaxNew[dep-1];
1223cd0c2139SMatthew G Knepley           }
1224e1757548SMatthew G. Knepley         }
1225cd0c2139SMatthew G Knepley         ierr = DMPlexSetCone(sdm, splitp, coneNew);CHKERRQ(ierr);
1226cd0c2139SMatthew G Knepley         /* Split old edge: Faces in positive side cells and old split faces */
1227cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1228cd0c2139SMatthew G Knepley           PetscInt val;
1229cd0c2139SMatthew G Knepley 
1230cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
12314c367dbcSMatthew G. Knepley           if (val == dim-1) {
12322582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
12334c367dbcSMatthew G. Knepley           } else if (val == (shift + dim-1)) {
12342582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1235cd0c2139SMatthew G Knepley           }
1236cd0c2139SMatthew G Knepley         }
1237b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1238cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
1239cd0c2139SMatthew G Knepley         /* Split new edge: Faces in negative side cells and new split faces */
1240cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1241cd0c2139SMatthew G Knepley           PetscInt val, face;
1242cd0c2139SMatthew G Knepley 
1243cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1244cd0c2139SMatthew G Knepley           if (val == dim-1) {
12454c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
1246cd0c2139SMatthew G Knepley             if (face < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[e]);
12474c367dbcSMatthew G. Knepley             supportNew[q++] = face + pMaxNew[dep+1];
1248cd0c2139SMatthew G Knepley           } else if (val == -(shift + dim-1)) {
12492582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1250cd0c2139SMatthew G Knepley           }
1251cd0c2139SMatthew G Knepley         }
1252b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1253cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, splitp, supportNew);CHKERRQ(ierr);
12544c367dbcSMatthew G. Knepley         /* Hybrid face */
12554c367dbcSMatthew G. Knepley         coneNew[0] = newp;
12564c367dbcSMatthew G. Knepley         coneNew[1] = splitp;
12574c367dbcSMatthew G. Knepley         for (v = 0; v < coneSize; ++v) {
12584c367dbcSMatthew G. Knepley           PetscInt vertex;
12594c367dbcSMatthew G. Knepley           ierr = PetscFindInt(cone[v], numSplitPoints[dep-1], splitPoints[dep-1], &vertex);CHKERRQ(ierr);
1260e1757548SMatthew G. Knepley           if (vertex < 0) {
1261e1757548SMatthew G. Knepley             ierr = PetscFindInt(cone[v], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &vertex);CHKERRQ(ierr);
1262e1757548SMatthew G. Knepley             if (vertex < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not locate point %d in split or unsplit points of depth %d", cone[v], dep-1);
1263e1757548SMatthew G. Knepley             coneNew[2+v] = vertex + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1264e1757548SMatthew G. Knepley           } else {
12654c367dbcSMatthew G. Knepley             coneNew[2+v] = vertex + pMaxNew[dep] + numSplitPoints[dep];
12664c367dbcSMatthew G. Knepley           }
1267e1757548SMatthew G. Knepley         }
12684c367dbcSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybface, coneNew);CHKERRQ(ierr);
12694c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
12704c367dbcSMatthew G. Knepley           PetscInt val, face;
12714c367dbcSMatthew G. Knepley 
12724c367dbcSMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
12734c367dbcSMatthew G. Knepley           if (val == dim-1) {
12744c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
12754c367dbcSMatthew G. Knepley             if (face < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[e]);
12764c367dbcSMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+2] + numSplitPoints[dep+2];
12774c367dbcSMatthew G. Knepley           }
12784c367dbcSMatthew G. Knepley         }
12794c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybface, supportNew);CHKERRQ(ierr);
1280cd0c2139SMatthew G Knepley       }
1281cd0c2139SMatthew G Knepley     }
1282cd0c2139SMatthew G Knepley   }
128318c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
128418c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
128518c5995bSMatthew G. Knepley       const PetscInt  oldp   = unsplitPoints[dep][p];
12862582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
128718c5995bSMatthew G. Knepley       const PetscInt *cone, *support, *ornt;
1288e1757548SMatthew G. Knepley       PetscInt        coneSize, supportSize, supportSizeNew, q, qf, e, f, s;
128918c5995bSMatthew G. Knepley 
129018c5995bSMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
129118c5995bSMatthew G. Knepley       ierr = DMPlexGetCone(dm, oldp, &cone);CHKERRQ(ierr);
129218c5995bSMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, oldp, &ornt);CHKERRQ(ierr);
129318c5995bSMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
129418c5995bSMatthew G. Knepley       ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
129518c5995bSMatthew G. Knepley       if (dep == 0) {
129618c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
129718c5995bSMatthew G. Knepley 
129818c5995bSMatthew G. Knepley         /* Unsplit vertex */
129918c5995bSMatthew G. Knepley         ierr = DMPlexGetSupportSize(sdm, newp, &supportSizeNew);CHKERRQ(ierr);
130018c5995bSMatthew G. Knepley         for (s = 0, q = 0; s < supportSize; ++s) {
13012582d50cSToby Isaac           supportNew[q++] = DMPlexShiftPoint_Internal(support[s], depth, depthShift) /*support[s] + depthOffset[dep+1]*/;
130218c5995bSMatthew G. Knepley           ierr = PetscFindInt(support[s], numSplitPoints[dep+1], splitPoints[dep+1], &e);CHKERRQ(ierr);
130318c5995bSMatthew G. Knepley           if (e >= 0) {
130418c5995bSMatthew G. Knepley             supportNew[q++] = e + pMaxNew[dep+1];
130518c5995bSMatthew G. Knepley           }
130618c5995bSMatthew G. Knepley         }
130718c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
130818c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
130918c5995bSMatthew G. Knepley         if (q != supportSizeNew) SETERRQ3(comm, PETSC_ERR_ARG_WRONG, "Support size %d != %d for vertex %d", q, supportSizeNew, newp);
131018c5995bSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
131118c5995bSMatthew G. Knepley         /* Hybrid edge */
131218c5995bSMatthew G. Knepley         coneNew[0] = newp;
131318c5995bSMatthew G. Knepley         coneNew[1] = newp;
131418c5995bSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybedge, coneNew);CHKERRQ(ierr);
131518c5995bSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
131618c5995bSMatthew G. Knepley           PetscInt val, edge;
131718c5995bSMatthew G. Knepley 
131818c5995bSMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
131918c5995bSMatthew G. Knepley           if (val == 1) {
132018c5995bSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);CHKERRQ(ierr);
132118c5995bSMatthew G. Knepley             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
132218c5995bSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2];
1323e1757548SMatthew G. Knepley           } else if  (val ==  (shift2 + 1)) {
1324e1757548SMatthew G. Knepley             ierr = PetscFindInt(support[e], numUnsplitPoints[dep+1], unsplitPoints[dep+1], &edge);CHKERRQ(ierr);
1325e1757548SMatthew G. Knepley             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a unsplit edge", support[e]);
1326e1757548SMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2] + numSplitPoints[dep+1];
132718c5995bSMatthew G. Knepley           }
132818c5995bSMatthew G. Knepley         }
132918c5995bSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybedge, supportNew);CHKERRQ(ierr);
1330e1757548SMatthew G. Knepley       } else if (dep == dim-2) {
1331e1757548SMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
1332e1757548SMatthew G. Knepley 
1333da1dd7e4SMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and hybrid face twice */
1334e1757548SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1335e1757548SMatthew G. Knepley           PetscInt val, face;
1336e1757548SMatthew G. Knepley 
1337e1757548SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[f], &val);CHKERRQ(ierr);
1338e1757548SMatthew G. Knepley           if (val == dim-1) {
1339e1757548SMatthew G. Knepley             ierr = PetscFindInt(support[f], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
1340e1757548SMatthew G. Knepley             if (face < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[f]);
13412582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1342e1757548SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+1];
1343e1757548SMatthew G. Knepley           } else {
13442582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1345e1757548SMatthew G. Knepley           }
1346e1757548SMatthew G. Knepley         }
1347e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
1348e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
1349da1dd7e4SMatthew G. Knepley         ierr = DMPlexGetSupportSize(sdm, newp, &supportSizeNew);CHKERRQ(ierr);
1350da1dd7e4SMatthew G. Knepley         if (qf != supportSizeNew) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Support size for unsplit edge %d is %d != %d\n", newp, qf, supportSizeNew);
1351e1757548SMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
1352e1757548SMatthew G. Knepley         /* Add hybrid face */
1353e1757548SMatthew G. Knepley         coneNew[0] = newp;
1354212cc919SMatthew G. Knepley         coneNew[1] = newp;
1355e1757548SMatthew G. Knepley         ierr = PetscFindInt(cone[0], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
1356e1757548SMatthew G. Knepley         if (v < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Vertex %d is not an unsplit vertex", cone[0]);
1357212cc919SMatthew G. Knepley         coneNew[2] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1358e1757548SMatthew G. Knepley         ierr = PetscFindInt(cone[1], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
1359e1757548SMatthew G. Knepley         if (v < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Vertex %d is not an unsplit vertex", cone[1]);
1360e1757548SMatthew G. Knepley         coneNew[3] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1361e1757548SMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybface, coneNew);CHKERRQ(ierr);
1362da1dd7e4SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1363da1dd7e4SMatthew G. Knepley           PetscInt val, face;
1364da1dd7e4SMatthew G. Knepley 
1365da1dd7e4SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[f], &val);CHKERRQ(ierr);
1366da1dd7e4SMatthew G. Knepley           if (val == dim-1) {
1367da1dd7e4SMatthew G. Knepley             ierr = PetscFindInt(support[f], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
1368da1dd7e4SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+2] + numSplitPoints[dep+2];
1369da1dd7e4SMatthew G. Knepley           }
1370da1dd7e4SMatthew G. Knepley         }
1371da1dd7e4SMatthew G. Knepley         ierr = DMPlexGetSupportSize(sdm, hybface, &supportSizeNew);CHKERRQ(ierr);
1372da1dd7e4SMatthew G. Knepley         if (qf != supportSizeNew) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Support size for hybrid face %d is %d != %d\n", hybface, qf, supportSizeNew);
1373e1757548SMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybface, supportNew);CHKERRQ(ierr);
1374cd0c2139SMatthew G Knepley       }
1375cd0c2139SMatthew G Knepley     }
1376cd0c2139SMatthew G Knepley   }
1377cd0c2139SMatthew G Knepley   /* Step 6b: Replace split points in negative side cones */
1378cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1379cd0c2139SMatthew G Knepley     PetscInt        dep = values[sp];
1380cd0c2139SMatthew G Knepley     IS              pIS;
1381cd0c2139SMatthew G Knepley     PetscInt        numPoints;
1382cd0c2139SMatthew G Knepley     const PetscInt *points;
1383cd0c2139SMatthew G Knepley 
1384cd0c2139SMatthew G Knepley     if (dep >= 0) continue;
1385cd0c2139SMatthew G Knepley     ierr = DMLabelGetStratumIS(label, dep, &pIS);CHKERRQ(ierr);
1386cd0c2139SMatthew G Knepley     if (!pIS) continue;
1387cd0c2139SMatthew G Knepley     dep  = -dep - shift;
1388cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(pIS, &numPoints);CHKERRQ(ierr);
1389cd0c2139SMatthew G Knepley     ierr = ISGetIndices(pIS, &points);CHKERRQ(ierr);
1390cd0c2139SMatthew G Knepley     for (p = 0; p < numPoints; ++p) {
1391cd0c2139SMatthew G Knepley       const PetscInt  oldp = points[p];
13922582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*depthOffset[dep] + oldp*/;
1393cd0c2139SMatthew G Knepley       const PetscInt *cone;
1394cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
139550cf782dSMatthew G. Knepley       /* PetscBool       replaced = PETSC_FALSE; */
1396cd0c2139SMatthew G Knepley 
1397cd0c2139SMatthew G Knepley       /* Negative edge: replace split vertex */
1398cd0c2139SMatthew G Knepley       /* Negative cell: replace split face */
1399cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(sdm, newp, &coneSize);CHKERRQ(ierr);
1400cd0c2139SMatthew G Knepley       ierr = DMPlexGetCone(sdm, newp, &cone);CHKERRQ(ierr);
1401cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
1402e38fbfedSToby Isaac         const PetscInt coldp = DMPlexShiftPointInverse_Internal(cone[c],depth,depthShift);
1403cd0c2139SMatthew G Knepley         PetscInt       csplitp, cp, val;
1404cd0c2139SMatthew G Knepley 
1405cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(label, coldp, &val);CHKERRQ(ierr);
1406cd0c2139SMatthew G Knepley         if (val == dep-1) {
1407cd0c2139SMatthew G Knepley           ierr = PetscFindInt(coldp, numSplitPoints[dep-1], splitPoints[dep-1], &cp);CHKERRQ(ierr);
1408cd0c2139SMatthew G Knepley           if (cp < 0) SETERRQ2(comm, PETSC_ERR_ARG_WRONG, "Point %d is not a split point of dimension %d", oldp, dep-1);
1409cd0c2139SMatthew G Knepley           csplitp  = pMaxNew[dep-1] + cp;
1410cd0c2139SMatthew G Knepley           ierr     = DMPlexInsertCone(sdm, newp, c, csplitp);CHKERRQ(ierr);
141150cf782dSMatthew G. Knepley           /* replaced = PETSC_TRUE; */
1412cd0c2139SMatthew G Knepley         }
1413cd0c2139SMatthew G Knepley       }
14144a189a86SMatthew G. Knepley       /* Cells with only a vertex or edge on the submesh have no replacement */
14154a189a86SMatthew G. Knepley       /* if (!replaced) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "The cone of point %d does not contain split points", oldp); */
1416cd0c2139SMatthew G Knepley     }
1417cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(pIS, &points);CHKERRQ(ierr);
1418cd0c2139SMatthew G Knepley     ierr = ISDestroy(&pIS);CHKERRQ(ierr);
1419cd0c2139SMatthew G Knepley   }
1420fa8e8ae5SToby Isaac   /* Step 7: Coordinates */
1421cd0c2139SMatthew G Knepley   ierr = DMPlexShiftCoordinates_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
142269d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(sdm, &coordSection);CHKERRQ(ierr);
1423cd0c2139SMatthew G Knepley   ierr = DMGetCoordinatesLocal(sdm, &coordinates);CHKERRQ(ierr);
1424cd0c2139SMatthew G Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
1425cd0c2139SMatthew G Knepley   for (v = 0; v < (numSplitPoints ? numSplitPoints[0] : 0); ++v) {
14262582d50cSToby Isaac     const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[0][v], depth, depthShift) /*depthOffset[0] + splitPoints[0][v]*/;
1427cd0c2139SMatthew G Knepley     const PetscInt splitp = pMaxNew[0] + v;
1428cd0c2139SMatthew G Knepley     PetscInt       dof, off, soff, d;
1429cd0c2139SMatthew G Knepley 
1430cd0c2139SMatthew G Knepley     ierr = PetscSectionGetDof(coordSection, newp, &dof);CHKERRQ(ierr);
1431cd0c2139SMatthew G Knepley     ierr = PetscSectionGetOffset(coordSection, newp, &off);CHKERRQ(ierr);
1432cd0c2139SMatthew G Knepley     ierr = PetscSectionGetOffset(coordSection, splitp, &soff);CHKERRQ(ierr);
1433cd0c2139SMatthew G Knepley     for (d = 0; d < dof; ++d) coords[soff+d] = coords[off+d];
1434cd0c2139SMatthew G Knepley   }
1435cd0c2139SMatthew G Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
1436fa8e8ae5SToby Isaac   /* Step 8: SF, if I can figure this out we can split the mesh in parallel */
1437cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSF_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
1438fa8e8ae5SToby Isaac   /* Step 9: Labels */
1439cd0c2139SMatthew G Knepley   ierr = DMPlexShiftLabels_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
1440c58f1c22SToby Isaac   ierr = DMGetNumLabels(sdm, &numLabels);CHKERRQ(ierr);
1441cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1442cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
14432582d50cSToby Isaac       const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[dep][p], depth, depthShift) /*depthOffset[dep] + splitPoints[dep][p]*/;
1444cd0c2139SMatthew G Knepley       const PetscInt splitp = pMaxNew[dep] + p;
1445cd0c2139SMatthew G Knepley       PetscInt       l;
1446cd0c2139SMatthew G Knepley 
14477db7e0a7SMatthew G. Knepley       if (splitLabel) {
14487db7e0a7SMatthew G. Knepley         const PetscInt val = 100 + dep;
14497db7e0a7SMatthew G. Knepley 
14507db7e0a7SMatthew G. Knepley         ierr = DMLabelSetValue(splitLabel, newp,    val);CHKERRQ(ierr);
14517db7e0a7SMatthew G. Knepley         ierr = DMLabelSetValue(splitLabel, splitp, -val);CHKERRQ(ierr);
14527db7e0a7SMatthew G. Knepley       }
1453cd0c2139SMatthew G Knepley       for (l = 0; l < numLabels; ++l) {
1454cd0c2139SMatthew G Knepley         DMLabel     mlabel;
1455cd0c2139SMatthew G Knepley         const char *lname;
1456cd0c2139SMatthew G Knepley         PetscInt    val;
14579a356370SMatthew G. Knepley         PetscBool   isDepth;
1458cd0c2139SMatthew G Knepley 
1459c58f1c22SToby Isaac         ierr = DMGetLabelName(sdm, l, &lname);CHKERRQ(ierr);
14609a356370SMatthew G. Knepley         ierr = PetscStrcmp(lname, "depth", &isDepth);CHKERRQ(ierr);
14619a356370SMatthew G. Knepley         if (isDepth) continue;
1462c58f1c22SToby Isaac         ierr = DMGetLabel(sdm, lname, &mlabel);CHKERRQ(ierr);
1463cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(mlabel, newp, &val);CHKERRQ(ierr);
1464cd0c2139SMatthew G Knepley         if (val >= 0) {
1465cd0c2139SMatthew G Knepley           ierr = DMLabelSetValue(mlabel, splitp, val);CHKERRQ(ierr);
1466cd0c2139SMatthew G Knepley         }
1467cd0c2139SMatthew G Knepley       }
1468cd0c2139SMatthew G Knepley     }
1469cd0c2139SMatthew G Knepley   }
1470cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1471cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1472cd0c2139SMatthew G Knepley 
1473cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
147418c5995bSMatthew G. Knepley     if (splitIS[dep]) {ierr = ISRestoreIndices(splitIS[dep], &splitPoints[dep]);CHKERRQ(ierr);}
147518c5995bSMatthew G. Knepley     ierr = ISDestroy(&splitIS[dep]);CHKERRQ(ierr);
147618c5995bSMatthew G. Knepley     if (unsplitIS[dep]) {ierr = ISRestoreIndices(unsplitIS[dep], &unsplitPoints[dep]);CHKERRQ(ierr);}
147718c5995bSMatthew G. Knepley     ierr = ISDestroy(&unsplitIS[dep]);CHKERRQ(ierr);
1478cd0c2139SMatthew G Knepley   }
1479cd0c2139SMatthew G Knepley   if (label) {
1480cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
1481cd0c2139SMatthew G Knepley     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
1482cd0c2139SMatthew G Knepley   }
14830d4d4d06SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
14840d4d4d06SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(sdm, d, NULL, &pEnd);CHKERRQ(ierr);
148536dbac82SMatthew G. Knepley     pMaxNew[d] = pEnd - numHybridPoints[d] - numHybridPointsOld[d];
14860d4d4d06SMatthew G. Knepley   }
14870e49e2e2SMatthew G. Knepley   ierr = DMPlexSetHybridBounds(sdm, depth >= 0 ? pMaxNew[depth] : PETSC_DETERMINE, depth>1 ? pMaxNew[depth-1] : PETSC_DETERMINE, depth>2 ? pMaxNew[1] : PETSC_DETERMINE, depth >= 0 ? pMaxNew[0] : PETSC_DETERMINE);CHKERRQ(ierr);
1488dcbb62e8SMatthew G. Knepley   ierr = PetscFree3(coneNew, coneONew, supportNew);CHKERRQ(ierr);
14892582d50cSToby Isaac   ierr = PetscFree5(depthMax, depthEnd, depthShift, pMaxNew, numHybridPointsOld);CHKERRQ(ierr);
149018c5995bSMatthew G. Knepley   ierr = PetscFree7(splitIS, unsplitIS, numSplitPoints, numUnsplitPoints, numHybridPoints, splitPoints, unsplitPoints);CHKERRQ(ierr);
1491cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1492cd0c2139SMatthew G Knepley }
1493cd0c2139SMatthew G Knepley 
1494cd0c2139SMatthew G Knepley /*@C
1495cd0c2139SMatthew G Knepley   DMPlexConstructCohesiveCells - Construct cohesive cells which split the face along an internal interface
1496cd0c2139SMatthew G Knepley 
1497cd0c2139SMatthew G Knepley   Collective on dm
1498cd0c2139SMatthew G Knepley 
1499cd0c2139SMatthew G Knepley   Input Parameters:
1500cd0c2139SMatthew G Knepley + dm - The original DM
150153156dfcSMatthew G. Knepley - label - The label specifying the boundary faces (this could be auto-generated)
1502cd0c2139SMatthew G Knepley 
1503cd0c2139SMatthew G Knepley   Output Parameters:
15047db7e0a7SMatthew G. Knepley + splitLabel - The label containing the split points, or NULL if no output is desired
1505cd0c2139SMatthew G Knepley - dmSplit - The new DM
1506cd0c2139SMatthew G Knepley 
1507cd0c2139SMatthew G Knepley   Level: developer
1508cd0c2139SMatthew G Knepley 
15092be2b188SMatthew G Knepley .seealso: DMCreate(), DMPlexLabelCohesiveComplete()
1510cd0c2139SMatthew G Knepley @*/
15117db7e0a7SMatthew G. Knepley PetscErrorCode DMPlexConstructCohesiveCells(DM dm, DMLabel label, DMLabel splitLabel, DM *dmSplit)
1512cd0c2139SMatthew G Knepley {
1513cd0c2139SMatthew G Knepley   DM             sdm;
1514cd0c2139SMatthew G Knepley   PetscInt       dim;
1515cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
1516cd0c2139SMatthew G Knepley 
1517cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1518cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
151953156dfcSMatthew G. Knepley   PetscValidPointer(dmSplit, 3);
1520cd0c2139SMatthew G Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), &sdm);CHKERRQ(ierr);
1521cd0c2139SMatthew G Knepley   ierr = DMSetType(sdm, DMPLEX);CHKERRQ(ierr);
1522c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1523c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(sdm, dim);CHKERRQ(ierr);
1524cd0c2139SMatthew G Knepley   switch (dim) {
1525cd0c2139SMatthew G Knepley   case 2:
1526cd0c2139SMatthew G Knepley   case 3:
15277db7e0a7SMatthew G. Knepley     ierr = DMPlexConstructCohesiveCells_Internal(dm, label, splitLabel, sdm);CHKERRQ(ierr);
1528cd0c2139SMatthew G Knepley     break;
1529cd0c2139SMatthew G Knepley   default:
1530cd0c2139SMatthew G Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct cohesive cells for dimension %d", dim);
1531cd0c2139SMatthew G Knepley   }
1532cd0c2139SMatthew G Knepley   *dmSplit = sdm;
1533cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1534cd0c2139SMatthew G Knepley }
1535cd0c2139SMatthew G Knepley 
15360f66a230SMatthew G. Knepley /* Returns the side of the surface for a given cell with a face on the surface */
15370f66a230SMatthew G. Knepley static PetscErrorCode GetSurfaceSide_Static(DM dm, DM subdm, PetscInt numSubpoints, const PetscInt *subpoints, PetscInt cell, PetscInt face, PetscBool *pos)
15380f66a230SMatthew G. Knepley {
15390f66a230SMatthew G. Knepley   const PetscInt *cone, *ornt;
15400f66a230SMatthew G. Knepley   PetscInt        dim, coneSize, c;
15410f66a230SMatthew G. Knepley   PetscErrorCode  ierr;
15420f66a230SMatthew G. Knepley 
15430f66a230SMatthew G. Knepley   PetscFunctionBegin;
15440f66a230SMatthew G. Knepley   *pos = PETSC_TRUE;
1545c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
15460f66a230SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
15470f66a230SMatthew G. Knepley   ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
15480f66a230SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, cell, &ornt);CHKERRQ(ierr);
15490f66a230SMatthew G. Knepley   for (c = 0; c < coneSize; ++c) {
15500f66a230SMatthew G. Knepley     if (cone[c] == face) {
15510f66a230SMatthew G. Knepley       PetscInt o = ornt[c];
15520f66a230SMatthew G. Knepley 
15530f66a230SMatthew G. Knepley       if (subdm) {
15540f66a230SMatthew G. Knepley         const PetscInt *subcone, *subornt;
15550f66a230SMatthew G. Knepley         PetscInt        subpoint, subface, subconeSize, sc;
15560f66a230SMatthew G. Knepley 
15570f66a230SMatthew G. Knepley         ierr = PetscFindInt(cell, numSubpoints, subpoints, &subpoint);CHKERRQ(ierr);
15580f66a230SMatthew G. Knepley         ierr = PetscFindInt(face, numSubpoints, subpoints, &subface);CHKERRQ(ierr);
15590f66a230SMatthew G. Knepley         ierr = DMPlexGetConeSize(subdm, subpoint, &subconeSize);CHKERRQ(ierr);
15600f66a230SMatthew G. Knepley         ierr = DMPlexGetCone(subdm, subpoint, &subcone);CHKERRQ(ierr);
15610f66a230SMatthew G. Knepley         ierr = DMPlexGetConeOrientation(subdm, subpoint, &subornt);CHKERRQ(ierr);
15620f66a230SMatthew G. Knepley         for (sc = 0; sc < subconeSize; ++sc) {
15630f66a230SMatthew G. Knepley           if (subcone[sc] == subface) {
15640f66a230SMatthew G. Knepley             o = subornt[0];
15650f66a230SMatthew G. Knepley             break;
15660f66a230SMatthew G. Knepley           }
15670f66a230SMatthew G. Knepley         }
15680f66a230SMatthew G. Knepley         if (sc >= subconeSize) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find subpoint %d (%d) in cone for subpoint %d (%d)", subface, face, subpoint, cell);
15690f66a230SMatthew G. Knepley       }
15700f66a230SMatthew G. Knepley       if (o >= 0) *pos = PETSC_TRUE;
15710f66a230SMatthew G. Knepley       else        *pos = PETSC_FALSE;
15720f66a230SMatthew G. Knepley       break;
15730f66a230SMatthew G. Knepley     }
15740f66a230SMatthew G. Knepley   }
15750f66a230SMatthew G. Knepley   if (c == coneSize) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Cell %d in split face %d support does not have it in the cone", cell, face);
15760f66a230SMatthew G. Knepley   PetscFunctionReturn(0);
15770f66a230SMatthew G. Knepley }
15780f66a230SMatthew G. Knepley 
1579cd0c2139SMatthew G Knepley /*@
15800f66a230SMatthew G. Knepley   DMPlexLabelCohesiveComplete - Starting with a label marking points on an internal surface, we add all other mesh pieces
1581cd0c2139SMatthew G Knepley   to complete the surface
1582cd0c2139SMatthew G Knepley 
1583cd0c2139SMatthew G Knepley   Input Parameters:
1584cd0c2139SMatthew G Knepley + dm     - The DM
15850f66a230SMatthew G. Knepley . label  - A DMLabel marking the surface
15860f66a230SMatthew G. Knepley . blabel - A DMLabel marking the vertices on the boundary which will not be duplicated, or NULL to find them automatically
1587bb55d314SMatthew G. Knepley . flip   - Flag to flip the submesh normal and replace points on the other side
158847946fd8SMatthew G. Knepley - subdm  - The subDM associated with the label, or NULL
1589cd0c2139SMatthew G Knepley 
1590cd0c2139SMatthew G Knepley   Output Parameter:
1591cd0c2139SMatthew G Knepley . label - A DMLabel marking all surface points
1592cd0c2139SMatthew G Knepley 
15930f66a230SMatthew G. Knepley   Note: The vertices in blabel are called "unsplit" in the terminology from hybrid cell creation.
15940f66a230SMatthew G. Knepley 
1595cd0c2139SMatthew G Knepley   Level: developer
1596cd0c2139SMatthew G Knepley 
15972be2b188SMatthew G Knepley .seealso: DMPlexConstructCohesiveCells(), DMPlexLabelComplete()
1598cd0c2139SMatthew G Knepley @*/
15990f66a230SMatthew G. Knepley PetscErrorCode DMPlexLabelCohesiveComplete(DM dm, DMLabel label, DMLabel blabel, PetscBool flip, DM subdm)
1600cd0c2139SMatthew G Knepley {
1601d90583fdSMatthew G. Knepley   DMLabel         depthLabel;
16028630cb1aSMatthew G. Knepley   IS              dimIS, subpointIS = NULL, facePosIS, faceNegIS, crossEdgeIS = NULL;
160347946fd8SMatthew G. Knepley   const PetscInt *points, *subpoints;
1604bb55d314SMatthew G. Knepley   const PetscInt  rev   = flip ? -1 : 1;
1605370472baSMatthew G. Knepley   PetscInt       *pMax;
1606370472baSMatthew G. Knepley   PetscInt        shift = 100, shift2 = 200, dim, depth, pSize, dep, cStart, cEnd, cMax, fStart, fEnd, vStart, vEnd, numPoints, numSubpoints, p, val;
1607cd0c2139SMatthew G Knepley   PetscErrorCode  ierr;
1608cd0c2139SMatthew G Knepley 
1609cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1610370472baSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1611370472baSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1612370472baSMatthew G. Knepley   pSize = PetscMax(depth, dim) + 1;
1613370472baSMatthew G. Knepley   ierr = PetscMalloc1(pSize,&pMax);CHKERRQ(ierr);
1614370472baSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, depth >= 0 ? &pMax[depth] : NULL, depth>1 ? &pMax[depth-1] : NULL, depth>2 ? &pMax[1] : NULL, &pMax[0]);CHKERRQ(ierr);
1615d90583fdSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
1616c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
161747946fd8SMatthew G. Knepley   if (subdm) {
161847946fd8SMatthew G. Knepley     ierr = DMPlexCreateSubpointIS(subdm, &subpointIS);CHKERRQ(ierr);
161947946fd8SMatthew G. Knepley     if (subpointIS) {
162047946fd8SMatthew G. Knepley       ierr = ISGetLocalSize(subpointIS, &numSubpoints);CHKERRQ(ierr);
162147946fd8SMatthew G. Knepley       ierr = ISGetIndices(subpointIS, &subpoints);CHKERRQ(ierr);
162247946fd8SMatthew G. Knepley     }
162347946fd8SMatthew G. Knepley   }
1624d7c8f101SMatthew G. Knepley   /* Mark cell on the fault, and its faces which touch the fault: cell orientation for face gives the side of the fault */
1625cd0c2139SMatthew G Knepley   ierr = DMLabelGetStratumIS(label, dim-1, &dimIS);CHKERRQ(ierr);
1626dc86033cSMatthew G. Knepley   if (!dimIS) {
1627dc86033cSMatthew G. Knepley     ierr = PetscFree(pMax);CHKERRQ(ierr);
16288630cb1aSMatthew G. Knepley     ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
1629dc86033cSMatthew G. Knepley     PetscFunctionReturn(0);
1630dc86033cSMatthew G. Knepley   }
1631cd0c2139SMatthew G Knepley   ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
1632cd0c2139SMatthew G Knepley   ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
1633d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault faces */
1634cd0c2139SMatthew G Knepley     const PetscInt *support;
1635cd0c2139SMatthew G Knepley     PetscInt        supportSize, s;
1636cd0c2139SMatthew G Knepley 
1637cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupportSize(dm, points[p], &supportSize);CHKERRQ(ierr);
1638c4419245SMatthew G. Knepley #if 0
1639c4419245SMatthew G. Knepley     if (supportSize != 2) {
1640c4419245SMatthew G. Knepley       const PetscInt *lp;
1641c4419245SMatthew G. Knepley       PetscInt        Nlp, pind;
1642c4419245SMatthew G. Knepley 
1643c4419245SMatthew G. Knepley       /* Check that for a cell with a single support face, that face is in the SF */
1644c4419245SMatthew G. Knepley       /*   THis check only works for the remote side. We would need root side information */
1645c4419245SMatthew G. Knepley       ierr = PetscSFGetGraph(dm->sf, NULL, &Nlp, &lp, NULL);CHKERRQ(ierr);
1646c4419245SMatthew G. Knepley       ierr = PetscFindInt(points[p], Nlp, lp, &pind);CHKERRQ(ierr);
1647c4419245SMatthew G. Knepley       if (pind < 0) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Split face %d has %d != 2 supports, and the face is not shared with another process", points[p], supportSize);
1648c4419245SMatthew G. Knepley     }
1649c4419245SMatthew G. Knepley #endif
1650cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupport(dm, points[p], &support);CHKERRQ(ierr);
1651cd0c2139SMatthew G Knepley     for (s = 0; s < supportSize; ++s) {
16520f66a230SMatthew G. Knepley       const PetscInt *cone;
1653cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
16540f66a230SMatthew G. Knepley       PetscBool       pos;
1655cd0c2139SMatthew G Knepley 
16560f66a230SMatthew G. Knepley       ierr = GetSurfaceSide_Static(dm, subdm, numSubpoints, subpoints, support[s], points[p], &pos);CHKERRQ(ierr);
16570f66a230SMatthew G. Knepley       if (pos) {ierr = DMLabelSetValue(label, support[s],  rev*(shift+dim));CHKERRQ(ierr);}
16580f66a230SMatthew G. Knepley       else     {ierr = DMLabelSetValue(label, support[s], -rev*(shift+dim));CHKERRQ(ierr);}
16590f66a230SMatthew G. Knepley       if (rev < 0) pos = !pos ? PETSC_TRUE : PETSC_FALSE;
16600f66a230SMatthew G. Knepley       /* Put faces touching the fault in the label */
1661cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
1662cd0c2139SMatthew G Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
1663cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
1664cd0c2139SMatthew G Knepley         const PetscInt point = cone[c];
1665cd0c2139SMatthew G Knepley 
1666cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1667cd0c2139SMatthew G Knepley         if (val == -1) {
1668cd0c2139SMatthew G Knepley           PetscInt *closure = NULL;
1669cd0c2139SMatthew G Knepley           PetscInt  closureSize, cl;
1670cd0c2139SMatthew G Knepley 
1671cd0c2139SMatthew G Knepley           ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1672cd0c2139SMatthew G Knepley           for (cl = 0; cl < closureSize*2; cl += 2) {
1673cd0c2139SMatthew G Knepley             const PetscInt clp  = closure[cl];
1674a0541d8aSMatthew G. Knepley             PetscInt       bval = -1;
1675cd0c2139SMatthew G Knepley 
1676cd0c2139SMatthew G Knepley             ierr = DMLabelGetValue(label, clp, &val);CHKERRQ(ierr);
1677a0541d8aSMatthew G. Knepley             if (blabel) {ierr = DMLabelGetValue(blabel, clp, &bval);CHKERRQ(ierr);}
1678a0541d8aSMatthew G. Knepley             if ((val >= 0) && (val < dim-1) && (bval < 0)) {
1679cd0c2139SMatthew G Knepley               ierr = DMLabelSetValue(label, point, pos == PETSC_TRUE ? shift+dim-1 : -(shift+dim-1));CHKERRQ(ierr);
1680cd0c2139SMatthew G Knepley               break;
1681cd0c2139SMatthew G Knepley             }
1682cd0c2139SMatthew G Knepley           }
1683cd0c2139SMatthew G Knepley           ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1684cd0c2139SMatthew G Knepley         }
1685cd0c2139SMatthew G Knepley       }
1686cd0c2139SMatthew G Knepley     }
1687cd0c2139SMatthew G Knepley   }
16880f66a230SMatthew G. Knepley   ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
16890f66a230SMatthew G. Knepley   ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
169047946fd8SMatthew G. Knepley   if (subpointIS) {ierr = ISRestoreIndices(subpointIS, &subpoints);CHKERRQ(ierr);}
169147946fd8SMatthew G. Knepley   ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
1692a0541d8aSMatthew G. Knepley   /* Mark boundary points as unsplit */
169386200784SMatthew G. Knepley   if (blabel) {
1694a0541d8aSMatthew G. Knepley     ierr = DMLabelGetStratumIS(blabel, 1, &dimIS);CHKERRQ(ierr);
1695a0541d8aSMatthew G. Knepley     ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
1696a0541d8aSMatthew G. Knepley     ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
1697a0541d8aSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
1698a0541d8aSMatthew G. Knepley       const PetscInt point = points[p];
1699a0541d8aSMatthew G. Knepley       PetscInt       val, bval;
1700a0541d8aSMatthew G. Knepley 
1701a0541d8aSMatthew G. Knepley       ierr = DMLabelGetValue(blabel, point, &bval);CHKERRQ(ierr);
1702a0541d8aSMatthew G. Knepley       if (bval >= 0) {
1703f7019248SMatthew G. Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1704f7019248SMatthew G. Knepley         if ((val < 0) || (val > dim)) {
1705f7019248SMatthew G. Knepley           /* This could be a point added from splitting a vertex on an adjacent fault, otherwise its just wrong */
1706f7019248SMatthew G. Knepley           ierr = DMLabelClearValue(blabel, point, bval);CHKERRQ(ierr);
1707f7019248SMatthew G. Knepley         }
1708f7019248SMatthew G. Knepley       }
1709f7019248SMatthew G. Knepley     }
1710f7019248SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
1711f7019248SMatthew G. Knepley       const PetscInt point = points[p];
1712f7019248SMatthew G. Knepley       PetscInt       val, bval;
1713f7019248SMatthew G. Knepley 
1714f7019248SMatthew G. Knepley       ierr = DMLabelGetValue(blabel, point, &bval);CHKERRQ(ierr);
1715f7019248SMatthew G. Knepley       if (bval >= 0) {
171686200784SMatthew G. Knepley         const PetscInt *cone,    *support;
171786200784SMatthew G. Knepley         PetscInt        coneSize, supportSize, s, valA, valB, valE;
171886200784SMatthew G. Knepley 
1719a0541d8aSMatthew G. Knepley         /* Mark as unsplit */
1720a0541d8aSMatthew G. Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1721a0541d8aSMatthew G. Knepley         if ((val < 0) || (val > dim)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Point %d has label value %d, should be part of the fault", point, val);
1722a0541d8aSMatthew G. Knepley         ierr = DMLabelClearValue(label, point, val);CHKERRQ(ierr);
1723a0541d8aSMatthew G. Knepley         ierr = DMLabelSetValue(label, point, shift2+val);CHKERRQ(ierr);
17242c06a818SMatthew G. Knepley         /* Check for cross-edge
17252c06a818SMatthew G. Knepley              A cross-edge has endpoints which are both on the boundary of the surface, but the edge itself is not. */
172686200784SMatthew G. Knepley         if (val != 0) continue;
172786200784SMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
172886200784SMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
172986200784SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
173086200784SMatthew G. Knepley           ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
173186200784SMatthew G. Knepley           ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
173286200784SMatthew G. Knepley           if (coneSize != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Edge %D has %D vertices != 2", support[s], coneSize);
173386200784SMatthew G. Knepley           ierr = DMLabelGetValue(blabel, cone[0], &valA);CHKERRQ(ierr);
173486200784SMatthew G. Knepley           ierr = DMLabelGetValue(blabel, cone[1], &valB);CHKERRQ(ierr);
173586200784SMatthew G. Knepley           ierr = DMLabelGetValue(blabel, support[s], &valE);CHKERRQ(ierr);
173609816f77SMatthew G. Knepley           if ((valE < 0) && (valA >= 0) && (valB >= 0) && (cone[0] != cone[1])) {ierr = DMLabelSetValue(blabel, support[s], 2);CHKERRQ(ierr);}
173786200784SMatthew G. Knepley         }
1738a0541d8aSMatthew G. Knepley       }
1739a0541d8aSMatthew G. Knepley     }
1740a0541d8aSMatthew G. Knepley     ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
1741a0541d8aSMatthew G. Knepley     ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
1742a0541d8aSMatthew G. Knepley   }
1743cd0c2139SMatthew G Knepley   /* Search for other cells/faces/edges connected to the fault by a vertex */
1744c4d480a2SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1745cd0c2139SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1746d24f5ce5SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
1747d24f5ce5SMatthew G. Knepley   cMax = cMax < 0 ? cEnd : cMax;
17480f66a230SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1749cd0c2139SMatthew G Knepley   ierr = DMLabelGetStratumIS(label, 0, &dimIS);CHKERRQ(ierr);
175086200784SMatthew G. Knepley   if (blabel) {ierr = DMLabelGetStratumIS(blabel, 2, &crossEdgeIS);CHKERRQ(ierr);}
175186200784SMatthew G. Knepley   if (dimIS && crossEdgeIS) {
175286200784SMatthew G. Knepley     IS vertIS = dimIS;
175386200784SMatthew G. Knepley 
175486200784SMatthew G. Knepley     ierr = ISExpand(vertIS, crossEdgeIS, &dimIS);CHKERRQ(ierr);
175586200784SMatthew G. Knepley     ierr = ISDestroy(&crossEdgeIS);CHKERRQ(ierr);
175686200784SMatthew G. Knepley     ierr = ISDestroy(&vertIS);CHKERRQ(ierr);
175786200784SMatthew G. Knepley   }
1758dc86033cSMatthew G. Knepley   if (!dimIS) {
1759dc86033cSMatthew G. Knepley     ierr = PetscFree(pMax);CHKERRQ(ierr);
1760dc86033cSMatthew G. Knepley     PetscFunctionReturn(0);
1761dc86033cSMatthew G. Knepley   }
1762cd0c2139SMatthew G Knepley   ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
1763cd0c2139SMatthew G Knepley   ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
1764d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault vertices */
1765cd0c2139SMatthew G Knepley     PetscInt *star = NULL;
176686200784SMatthew G. Knepley     PetscInt  starSize, s;
1767cd0c2139SMatthew G Knepley     PetscInt  again = 1;  /* 0: Finished 1: Keep iterating after a change 2: No change */
1768cd0c2139SMatthew G Knepley 
1769d4622b2cSMatthew G. Knepley     /* All points connected to the fault are inside a cell, so at the top level we will only check cells */
1770cd0c2139SMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
1771cd0c2139SMatthew G Knepley     while (again) {
1772cd0c2139SMatthew G Knepley       if (again > 1) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Could not classify all cells connected to the fault");
1773cd0c2139SMatthew G Knepley       again = 0;
1774cd0c2139SMatthew G Knepley       for (s = 0; s < starSize*2; s += 2) {
1775cd0c2139SMatthew G Knepley         const PetscInt  point = star[s];
1776cd0c2139SMatthew G Knepley         const PetscInt *cone;
1777cd0c2139SMatthew G Knepley         PetscInt        coneSize, c;
1778cd0c2139SMatthew G Knepley 
1779d24f5ce5SMatthew G. Knepley         if ((point < cStart) || (point >= cMax)) continue;
1780cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1781cd0c2139SMatthew G Knepley         if (val != -1) continue;
1782d4622b2cSMatthew G. Knepley         again = again == 1 ? 1 : 2;
1783cd0c2139SMatthew G Knepley         ierr  = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
1784cd0c2139SMatthew G Knepley         ierr  = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
1785cd0c2139SMatthew G Knepley         for (c = 0; c < coneSize; ++c) {
1786cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, cone[c], &val);CHKERRQ(ierr);
1787cd0c2139SMatthew G Knepley           if (val != -1) {
1788d7c8f101SMatthew G. Knepley             const PetscInt *ccone;
1789166d9d0cSMatthew G. Knepley             PetscInt        cconeSize, cc, side;
1790d7c8f101SMatthew G. Knepley 
1791db8edaafSMatthew G. Knepley             if (PetscAbs(val) < shift) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %d on cell %d has an invalid label %d", cone[c], point, val);
179237a6de01SMatthew G. Knepley             if (val > 0) side =  1;
179337a6de01SMatthew G. Knepley             else         side = -1;
179437a6de01SMatthew G. Knepley             ierr = DMLabelSetValue(label, point, side*(shift+dim));CHKERRQ(ierr);
1795d7c8f101SMatthew G. Knepley             /* Mark cell faces which touch the fault */
1796d7c8f101SMatthew G. Knepley             ierr = DMPlexGetConeSize(dm, point, &cconeSize);CHKERRQ(ierr);
1797d7c8f101SMatthew G. Knepley             ierr = DMPlexGetCone(dm, point, &ccone);CHKERRQ(ierr);
1798d7c8f101SMatthew G. Knepley             for (cc = 0; cc < cconeSize; ++cc) {
1799d7c8f101SMatthew G. Knepley               PetscInt *closure = NULL;
1800d7c8f101SMatthew G. Knepley               PetscInt  closureSize, cl;
1801d7c8f101SMatthew G. Knepley 
1802166d9d0cSMatthew G. Knepley               ierr = DMLabelGetValue(label, ccone[cc], &val);CHKERRQ(ierr);
1803166d9d0cSMatthew G. Knepley               if (val != -1) continue;
1804d7c8f101SMatthew G. Knepley               ierr = DMPlexGetTransitiveClosure(dm, ccone[cc], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1805d4622b2cSMatthew G. Knepley               for (cl = 0; cl < closureSize*2; cl += 2) {
1806d7c8f101SMatthew G. Knepley                 const PetscInt clp = closure[cl];
1807d7c8f101SMatthew G. Knepley 
1808d7c8f101SMatthew G. Knepley                 ierr = DMLabelGetValue(label, clp, &val);CHKERRQ(ierr);
1809d7c8f101SMatthew G. Knepley                 if (val == -1) continue;
1810d7c8f101SMatthew G. Knepley                 ierr = DMLabelSetValue(label, ccone[cc], side*(shift+dim-1));CHKERRQ(ierr);
181137a6de01SMatthew G. Knepley                 break;
181237a6de01SMatthew G. Knepley               }
1813d7c8f101SMatthew G. Knepley               ierr = DMPlexRestoreTransitiveClosure(dm, ccone[cc], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1814cd0c2139SMatthew G Knepley             }
1815cd0c2139SMatthew G Knepley             again = 1;
1816cd0c2139SMatthew G Knepley             break;
1817cd0c2139SMatthew G Knepley           }
1818cd0c2139SMatthew G Knepley         }
1819cd0c2139SMatthew G Knepley       }
1820cd0c2139SMatthew G Knepley     }
1821cd0c2139SMatthew G Knepley     /* Classify the rest by cell membership */
1822cd0c2139SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
1823cd0c2139SMatthew G Knepley       const PetscInt point = star[s];
1824cd0c2139SMatthew G Knepley 
1825cd0c2139SMatthew G Knepley       ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1826cd0c2139SMatthew G Knepley       if (val == -1) {
1827cd0c2139SMatthew G Knepley         PetscInt  *sstar = NULL;
1828cd0c2139SMatthew G Knepley         PetscInt   sstarSize, ss;
1829cd0c2139SMatthew G Knepley         PetscBool  marked = PETSC_FALSE;
1830cd0c2139SMatthew G Knepley 
1831cd0c2139SMatthew G Knepley         ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &sstarSize, &sstar);CHKERRQ(ierr);
1832cd0c2139SMatthew G Knepley         for (ss = 0; ss < sstarSize*2; ss += 2) {
1833cd0c2139SMatthew G Knepley           const PetscInt spoint = sstar[ss];
1834cd0c2139SMatthew G Knepley 
1835d24f5ce5SMatthew G. Knepley           if ((spoint < cStart) || (spoint >= cMax)) continue;
1836cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, spoint, &val);CHKERRQ(ierr);
1837cd0c2139SMatthew G Knepley           if (val == -1) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %d in star of %d does not have a valid label", spoint, point);
1838d90583fdSMatthew G. Knepley           ierr = DMLabelGetValue(depthLabel, point, &dep);CHKERRQ(ierr);
1839cd0c2139SMatthew G Knepley           if (val > 0) {
1840cd0c2139SMatthew G Knepley             ierr = DMLabelSetValue(label, point,   shift+dep);CHKERRQ(ierr);
1841cd0c2139SMatthew G Knepley           } else {
1842cd0c2139SMatthew G Knepley             ierr = DMLabelSetValue(label, point, -(shift+dep));CHKERRQ(ierr);
1843cd0c2139SMatthew G Knepley           }
1844cd0c2139SMatthew G Knepley           marked = PETSC_TRUE;
1845cd0c2139SMatthew G Knepley           break;
1846cd0c2139SMatthew G Knepley         }
1847cd0c2139SMatthew G Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &sstarSize, &sstar);CHKERRQ(ierr);
1848370472baSMatthew G. Knepley         ierr = DMLabelGetValue(depthLabel, point, &dep);CHKERRQ(ierr);
1849370472baSMatthew G. Knepley         if (point < pMax[dep] && !marked) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Point %d could not be classified", point);
1850cd0c2139SMatthew G Knepley       }
1851cd0c2139SMatthew G Knepley     }
1852cd0c2139SMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
1853cd0c2139SMatthew G Knepley   }
1854cd0c2139SMatthew G Knepley   ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
1855cd0c2139SMatthew G Knepley   ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
185618c5995bSMatthew G. Knepley   /* If any faces touching the fault divide cells on either side, split them */
185718c5995bSMatthew G. Knepley   ierr = DMLabelGetStratumIS(label,   shift+dim-1,  &facePosIS);CHKERRQ(ierr);
185818c5995bSMatthew G. Knepley   ierr = DMLabelGetStratumIS(label, -(shift+dim-1), &faceNegIS);CHKERRQ(ierr);
185918c5995bSMatthew G. Knepley   ierr = ISExpand(facePosIS, faceNegIS, &dimIS);CHKERRQ(ierr);
186018c5995bSMatthew G. Knepley   ierr = ISDestroy(&facePosIS);CHKERRQ(ierr);
186118c5995bSMatthew G. Knepley   ierr = ISDestroy(&faceNegIS);CHKERRQ(ierr);
186218c5995bSMatthew G. Knepley   ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
186318c5995bSMatthew G. Knepley   ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
186418c5995bSMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
186518c5995bSMatthew G. Knepley     const PetscInt  point = points[p];
186618c5995bSMatthew G. Knepley     const PetscInt *support;
186718c5995bSMatthew G. Knepley     PetscInt        supportSize, valA, valB;
186818c5995bSMatthew G. Knepley 
186918c5995bSMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
187018c5995bSMatthew G. Knepley     if (supportSize != 2) continue;
187118c5995bSMatthew G. Knepley     ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
187218c5995bSMatthew G. Knepley     ierr = DMLabelGetValue(label, support[0], &valA);CHKERRQ(ierr);
187318c5995bSMatthew G. Knepley     ierr = DMLabelGetValue(label, support[1], &valB);CHKERRQ(ierr);
187418c5995bSMatthew G. Knepley     if ((valA == -1) || (valB == -1)) continue;
187518c5995bSMatthew G. Knepley     if (valA*valB > 0) continue;
187618c5995bSMatthew G. Knepley     /* Split the face */
187718c5995bSMatthew G. Knepley     ierr = DMLabelGetValue(label, point, &valA);CHKERRQ(ierr);
187818c5995bSMatthew G. Knepley     ierr = DMLabelClearValue(label, point, valA);CHKERRQ(ierr);
187918c5995bSMatthew G. Knepley     ierr = DMLabelSetValue(label, point, dim-1);CHKERRQ(ierr);
1880e1757548SMatthew G. Knepley     /* Label its closure:
1881e1757548SMatthew G. Knepley       unmarked: label as unsplit
1882e1757548SMatthew G. Knepley       incident: relabel as split
1883e1757548SMatthew G. Knepley       split:    do nothing
1884e1757548SMatthew G. Knepley     */
188518c5995bSMatthew G. Knepley     {
188618c5995bSMatthew G. Knepley       PetscInt *closure = NULL;
188718c5995bSMatthew G. Knepley       PetscInt  closureSize, cl;
188818c5995bSMatthew G. Knepley 
188918c5995bSMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
189018c5995bSMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
189118c5995bSMatthew G. Knepley         ierr = DMLabelGetValue(label, closure[cl], &valA);CHKERRQ(ierr);
18928771c1d3SMatthew G. Knepley         if (valA == -1) { /* Mark as unsplit */
189318c5995bSMatthew G. Knepley           ierr = DMLabelGetValue(depthLabel, closure[cl], &dep);CHKERRQ(ierr);
189418c5995bSMatthew G. Knepley           ierr = DMLabelSetValue(label, closure[cl], shift2+dep);CHKERRQ(ierr);
18958771c1d3SMatthew G. Knepley         } else if (((valA >= shift) && (valA < shift2)) || ((valA <= -shift) && (valA > -shift2))) {
1896e1757548SMatthew G. Knepley           ierr = DMLabelGetValue(depthLabel, closure[cl], &dep);CHKERRQ(ierr);
1897e1757548SMatthew G. Knepley           ierr = DMLabelClearValue(label, closure[cl], valA);CHKERRQ(ierr);
1898e1757548SMatthew G. Knepley           ierr = DMLabelSetValue(label, closure[cl], dep);CHKERRQ(ierr);
1899e1757548SMatthew G. Knepley         }
190018c5995bSMatthew G. Knepley       }
190118c5995bSMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
190218c5995bSMatthew G. Knepley     }
190318c5995bSMatthew G. Knepley   }
190418c5995bSMatthew G. Knepley   ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
190518c5995bSMatthew G. Knepley   ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
1906370472baSMatthew G. Knepley   ierr = PetscFree(pMax);CHKERRQ(ierr);
1907cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1908cd0c2139SMatthew G Knepley }
1909cd0c2139SMatthew G Knepley 
1910720e594eSMatthew G. Knepley /* Check that no cell have all vertices on the fault */
1911720e594eSMatthew G. Knepley PetscErrorCode DMPlexCheckValidSubmesh_Private(DM dm, DMLabel label, DM subdm)
1912720e594eSMatthew G. Knepley {
1913720e594eSMatthew G. Knepley   IS              subpointIS;
1914720e594eSMatthew G. Knepley   const PetscInt *dmpoints;
1915720e594eSMatthew G. Knepley   PetscInt        defaultValue, cStart, cEnd, c, vStart, vEnd;
1916720e594eSMatthew G. Knepley   PetscErrorCode  ierr;
1917720e594eSMatthew G. Knepley 
1918720e594eSMatthew G. Knepley   PetscFunctionBegin;
1919720e594eSMatthew G. Knepley   if (!label) PetscFunctionReturn(0);
1920720e594eSMatthew G. Knepley   ierr = DMLabelGetDefaultValue(label, &defaultValue);CHKERRQ(ierr);
1921720e594eSMatthew G. Knepley   ierr = DMPlexCreateSubpointIS(subdm, &subpointIS);CHKERRQ(ierr);
1922720e594eSMatthew G. Knepley   if (!subpointIS) PetscFunctionReturn(0);
1923720e594eSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(subdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1924720e594eSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1925720e594eSMatthew G. Knepley   ierr = ISGetIndices(subpointIS, &dmpoints);CHKERRQ(ierr);
1926720e594eSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1927720e594eSMatthew G. Knepley     PetscBool invalidCell = PETSC_TRUE;
1928720e594eSMatthew G. Knepley     PetscInt *closure     = NULL;
1929720e594eSMatthew G. Knepley     PetscInt  closureSize, cl;
1930720e594eSMatthew G. Knepley 
1931720e594eSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1932720e594eSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
1933720e594eSMatthew G. Knepley       PetscInt value = 0;
1934720e594eSMatthew G. Knepley 
1935720e594eSMatthew G. Knepley       if ((closure[cl] < vStart) || (closure[cl] >= vEnd)) continue;
1936720e594eSMatthew G. Knepley       ierr = DMLabelGetValue(label, closure[cl], &value);CHKERRQ(ierr);
1937720e594eSMatthew G. Knepley       if (value == defaultValue) {invalidCell = PETSC_FALSE; break;}
1938720e594eSMatthew G. Knepley     }
1939720e594eSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1940720e594eSMatthew G. Knepley     if (invalidCell) {
1941720e594eSMatthew G. Knepley       ierr = ISRestoreIndices(subpointIS, &dmpoints);CHKERRQ(ierr);
1942720e594eSMatthew G. Knepley       ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
1943720e594eSMatthew G. Knepley       ierr = DMDestroy(&subdm);CHKERRQ(ierr);
1944720e594eSMatthew G. Knepley       SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ambiguous submesh. Cell %D has all of its vertices on the submesh.", dmpoints[c]);
1945720e594eSMatthew G. Knepley     }
1946720e594eSMatthew G. Knepley   }
1947720e594eSMatthew G. Knepley   ierr = ISRestoreIndices(subpointIS, &dmpoints);CHKERRQ(ierr);
1948720e594eSMatthew G. Knepley   ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
1949720e594eSMatthew G. Knepley   PetscFunctionReturn(0);
1950720e594eSMatthew G. Knepley }
1951720e594eSMatthew G. Knepley 
1952720e594eSMatthew G. Knepley 
1953c08575a3SMatthew G. Knepley /*@
19543cf72582SMatthew G. Knepley   DMPlexCreateHybridMesh - Create a mesh with hybrid cells along an internal interface
19553cf72582SMatthew G. Knepley 
19563cf72582SMatthew G. Knepley   Collective on dm
19573cf72582SMatthew G. Knepley 
19583cf72582SMatthew G. Knepley   Input Parameters:
19593cf72582SMatthew G. Knepley + dm - The original DM
1960720e594eSMatthew G. Knepley . label - The label specifying the interface vertices
1961720e594eSMatthew G. Knepley - bdlabel - The optional label specifying the interface boundary vertices
19623cf72582SMatthew G. Knepley 
19633cf72582SMatthew G. Knepley   Output Parameters:
19647db7e0a7SMatthew G. Knepley + hybridLabel - The label fully marking the interface, or NULL if no output is desired
19657db7e0a7SMatthew G. Knepley . splitLabel - The label containing the split points, or NULL if no output is desired
1966720e594eSMatthew G. Knepley . dmInterface - The new interface DM, or NULL
1967720e594eSMatthew G. Knepley - dmHybrid - The new DM with cohesive cells
19683cf72582SMatthew G. Knepley 
19696eccb800SMatthew Knepley   Note: The hybridLabel indicates what parts of the original mesh impinged on the on division surface. For points
19706eccb800SMatthew Knepley   directly on the division surface, they are labeled with their dimension, so an edge 7 on the division surface would be
19716eccb800SMatthew Knepley   7 (1) in hybridLabel. For points that impinge from the positive side, they are labeled with 100+dim, so an edge 6 with
19726eccb800SMatthew 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
19736eccb800SMatthew Knepley   surface also hits vertex 3, it would be 9 (-101) in hybridLabel.
19746eccb800SMatthew Knepley 
19756eccb800SMatthew Knepley   The splitLabel indicates what points in the new hybrid mesh were the result of splitting points in the original
19766eccb800SMatthew Knepley   mesh. The label value is +=100+dim for each point. For example, if two edges 10 and 14 in the hybrid resulting from
19776eccb800SMatthew Knepley   splitting an edge in the original mesh, you would have 10 (101) and 14 (-101) in the splitLabel.
19786eccb800SMatthew Knepley 
19796eccb800SMatthew Knepley   The dmInterface is a DM built from the original division surface. It has a label which can be retrieved using
19806eccb800SMatthew Knepley   DMPlexGetSubpointMap() which maps each point back to the point in the surface of the original mesh.
19816eccb800SMatthew Knepley 
19823cf72582SMatthew G. Knepley   Level: developer
19833cf72582SMatthew G. Knepley 
19846eccb800SMatthew Knepley .seealso: DMPlexConstructCohesiveCells(), DMPlexLabelCohesiveComplete(), DMPlexGetSubpointMap(), DMCreate()
19853cf72582SMatthew G. Knepley @*/
19867db7e0a7SMatthew G. Knepley PetscErrorCode DMPlexCreateHybridMesh(DM dm, DMLabel label, DMLabel bdlabel, DMLabel *hybridLabel, DMLabel *splitLabel, DM *dmInterface, DM *dmHybrid)
19873cf72582SMatthew G. Knepley {
19883cf72582SMatthew G. Knepley   DM             idm;
19897db7e0a7SMatthew G. Knepley   DMLabel        subpointMap, hlabel, slabel = NULL;
19903cf72582SMatthew G. Knepley   PetscInt       dim;
19913cf72582SMatthew G. Knepley   PetscErrorCode ierr;
19923cf72582SMatthew G. Knepley 
19933cf72582SMatthew G. Knepley   PetscFunctionBegin;
19943cf72582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1995720e594eSMatthew G. Knepley   if (bdlabel) PetscValidPointer(bdlabel, 3);
1996720e594eSMatthew G. Knepley   if (hybridLabel) PetscValidPointer(hybridLabel, 4);
19977db7e0a7SMatthew G. Knepley   if (splitLabel)  PetscValidPointer(splitLabel, 5);
19987db7e0a7SMatthew G. Knepley   if (dmInterface) PetscValidPointer(dmInterface, 6);
19997db7e0a7SMatthew G. Knepley   PetscValidPointer(dmHybrid, 7);
2000c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2001158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmesh(dm, label, 1, PETSC_FALSE, &idm);CHKERRQ(ierr);
2002720e594eSMatthew G. Knepley   ierr = DMPlexCheckValidSubmesh_Private(dm, label, idm);CHKERRQ(ierr);
20033cf72582SMatthew G. Knepley   ierr = DMPlexOrient(idm);CHKERRQ(ierr);
20043cf72582SMatthew G. Knepley   ierr = DMPlexGetSubpointMap(idm, &subpointMap);CHKERRQ(ierr);
20053cf72582SMatthew G. Knepley   ierr = DMLabelDuplicate(subpointMap, &hlabel);CHKERRQ(ierr);
20063cf72582SMatthew G. Knepley   ierr = DMLabelClearStratum(hlabel, dim);CHKERRQ(ierr);
20077db7e0a7SMatthew G. Knepley   if (splitLabel) {
20087db7e0a7SMatthew G. Knepley     const char *name;
20097db7e0a7SMatthew G. Knepley     char        sname[PETSC_MAX_PATH_LEN];
20107db7e0a7SMatthew G. Knepley 
2011d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) hlabel, &name);CHKERRQ(ierr);
20127db7e0a7SMatthew G. Knepley     ierr = PetscStrncpy(sname, name, PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
20137db7e0a7SMatthew G. Knepley     ierr = PetscStrcat(sname, " split");CHKERRQ(ierr);
2014d67d17b1SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, sname, &slabel);CHKERRQ(ierr);
20157db7e0a7SMatthew G. Knepley   }
2016720e594eSMatthew G. Knepley   ierr = DMPlexLabelCohesiveComplete(dm, hlabel, bdlabel, PETSC_FALSE, idm);CHKERRQ(ierr);
2017720e594eSMatthew G. Knepley   if (dmInterface) {*dmInterface = idm;}
2018720e594eSMatthew G. Knepley   else             {ierr = DMDestroy(&idm);CHKERRQ(ierr);}
20197db7e0a7SMatthew G. Knepley   ierr = DMPlexConstructCohesiveCells(dm, hlabel, slabel, dmHybrid);CHKERRQ(ierr);
20203cf72582SMatthew G. Knepley   if (hybridLabel) *hybridLabel = hlabel;
202153aa2e23SMatthew G. Knepley   else             {ierr = DMLabelDestroy(&hlabel);CHKERRQ(ierr);}
20227db7e0a7SMatthew G. Knepley   if (splitLabel)  *splitLabel  = slabel;
2023cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
2024cd0c2139SMatthew G Knepley }
2025cd0c2139SMatthew G Knepley 
2026efa14ee0SMatthew G Knepley /* Here we need the explicit assumption that:
2027efa14ee0SMatthew G Knepley 
2028efa14ee0SMatthew G Knepley      For any marked cell, the marked vertices constitute a single face
2029efa14ee0SMatthew G Knepley */
2030830e53efSMatthew G. Knepley static PetscErrorCode DMPlexMarkSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, DM subdm)
2031efa14ee0SMatthew G Knepley {
2032fed694aaSMatthew G. Knepley   IS               subvertexIS = NULL;
2033efa14ee0SMatthew G Knepley   const PetscInt  *subvertices;
203477d178adSMatthew G. Knepley   PetscInt        *pStart, *pEnd, *pMax, pSize;
2035efa14ee0SMatthew G Knepley   PetscInt         depth, dim, d, numSubVerticesInitial = 0, v;
2036efa14ee0SMatthew G Knepley   PetscErrorCode   ierr;
2037efa14ee0SMatthew G Knepley 
2038efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2039efa14ee0SMatthew G Knepley   *numFaces = 0;
2040efa14ee0SMatthew G Knepley   *nFV      = 0;
2041efa14ee0SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2042c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
204377d178adSMatthew G. Knepley   pSize = PetscMax(depth, dim) + 1;
2044dcca6d9dSJed Brown   ierr = PetscMalloc3(pSize,&pStart,pSize,&pEnd,pSize,&pMax);CHKERRQ(ierr);
2045d92e47f8SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, depth >= 0 ? &pMax[depth] : NULL, depth>1 ? &pMax[depth-1] : NULL, depth>2 ? &pMax[1] : NULL, &pMax[0]);CHKERRQ(ierr);
2046efa14ee0SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
2047efa14ee0SMatthew G Knepley     ierr = DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);
2048efa14ee0SMatthew G Knepley     if (pMax[d] >= 0) pEnd[d] = PetscMin(pEnd[d], pMax[d]);
2049efa14ee0SMatthew G Knepley   }
2050efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
2051fed694aaSMatthew G. Knepley   if (vertexLabel) {ierr = DMLabelGetStratumIS(vertexLabel, value, &subvertexIS);CHKERRQ(ierr);}
2052efa14ee0SMatthew G Knepley   if (subvertexIS) {
2053efa14ee0SMatthew G Knepley     ierr = ISGetSize(subvertexIS, &numSubVerticesInitial);CHKERRQ(ierr);
2054efa14ee0SMatthew G Knepley     ierr = ISGetIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
2055efa14ee0SMatthew G Knepley   }
2056efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2057efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
20580298fd71SBarry Smith     PetscInt      *star   = NULL;
2059efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numCells = 0, c;
2060efa14ee0SMatthew G Knepley 
2061efa14ee0SMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2062efa14ee0SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
2063efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2064efa14ee0SMatthew G Knepley       if ((point >= pStart[depth]) && (point < pEnd[depth])) star[numCells++] = point;
2065efa14ee0SMatthew G Knepley     }
2066efa14ee0SMatthew G Knepley     for (c = 0; c < numCells; ++c) {
2067efa14ee0SMatthew G Knepley       const PetscInt cell    = star[c];
20680298fd71SBarry Smith       PetscInt      *closure = NULL;
2069efa14ee0SMatthew G Knepley       PetscInt       closureSize, cl;
2070efa14ee0SMatthew G Knepley       PetscInt       cellLoc, numCorners = 0, faceSize = 0;
2071efa14ee0SMatthew G Knepley 
2072efa14ee0SMatthew G Knepley       ierr = DMLabelGetValue(subpointMap, cell, &cellLoc);CHKERRQ(ierr);
207365560c7fSMatthew G Knepley       if (cellLoc == 2) continue;
207482f516ccSBarry Smith       if (cellLoc >= 0) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %d has dimension %d in the surface label", cell, cellLoc);
2075efa14ee0SMatthew G Knepley       ierr = DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2076efa14ee0SMatthew G Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
2077efa14ee0SMatthew G Knepley         const PetscInt point = closure[cl];
2078efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2079efa14ee0SMatthew G Knepley 
2080efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
2081efa14ee0SMatthew G Knepley           ++numCorners;
2082efa14ee0SMatthew G Knepley           ierr = DMLabelGetValue(vertexLabel, point, &vertexLoc);CHKERRQ(ierr);
2083830e53efSMatthew G. Knepley           if (vertexLoc == value) closure[faceSize++] = point;
2084efa14ee0SMatthew G Knepley         }
2085efa14ee0SMatthew G Knepley       }
208618ad9376SMatthew G. Knepley       if (!(*nFV)) {ierr = DMPlexGetNumFaceVertices(dm, dim, numCorners, nFV);CHKERRQ(ierr);}
208782f516ccSBarry Smith       if (faceSize > *nFV) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %d of an element on the surface", faceSize);
2088efa14ee0SMatthew G Knepley       if (faceSize == *nFV) {
2089007baee2SMatthew G. Knepley         const PetscInt *cells = NULL;
2090007baee2SMatthew G. Knepley         PetscInt        numCells, nc;
2091007baee2SMatthew G. Knepley 
2092efa14ee0SMatthew G Knepley         ++(*numFaces);
2093efa14ee0SMatthew G Knepley         for (cl = 0; cl < faceSize; ++cl) {
2094efa14ee0SMatthew G Knepley           ierr = DMLabelSetValue(subpointMap, closure[cl], 0);CHKERRQ(ierr);
2095efa14ee0SMatthew G Knepley         }
2096007baee2SMatthew G. Knepley         ierr = DMPlexGetJoin(dm, faceSize, closure, &numCells, &cells);CHKERRQ(ierr);
2097007baee2SMatthew G. Knepley         for (nc = 0; nc < numCells; ++nc) {
2098007baee2SMatthew G. Knepley           ierr = DMLabelSetValue(subpointMap, cells[nc], 2);CHKERRQ(ierr);
2099007baee2SMatthew G. Knepley         }
2100007baee2SMatthew G. Knepley         ierr = DMPlexRestoreJoin(dm, faceSize, closure, &numCells, &cells);CHKERRQ(ierr);
2101efa14ee0SMatthew G Knepley       }
2102efa14ee0SMatthew G Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2103efa14ee0SMatthew G Knepley     }
2104efa14ee0SMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2105efa14ee0SMatthew G Knepley   }
2106efa14ee0SMatthew G Knepley   if (subvertexIS) {
2107efa14ee0SMatthew G Knepley     ierr = ISRestoreIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
2108efa14ee0SMatthew G Knepley   }
2109efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
2110efa14ee0SMatthew G Knepley   ierr = PetscFree3(pStart,pEnd,pMax);CHKERRQ(ierr);
2111efa14ee0SMatthew G Knepley   PetscFunctionReturn(0);
2112efa14ee0SMatthew G Knepley }
2113efa14ee0SMatthew G Knepley 
2114158acfadSMatthew G. Knepley static PetscErrorCode DMPlexMarkSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DMLabel subpointMap, DM subdm)
2115efa14ee0SMatthew G Knepley {
211634b4c39eSMatthew G. Knepley   IS               subvertexIS = NULL;
2117efa14ee0SMatthew G Knepley   const PetscInt  *subvertices;
2118efa14ee0SMatthew G Knepley   PetscInt        *pStart, *pEnd, *pMax;
2119efa14ee0SMatthew G Knepley   PetscInt         dim, d, numSubVerticesInitial = 0, v;
2120efa14ee0SMatthew G Knepley   PetscErrorCode   ierr;
2121efa14ee0SMatthew G Knepley 
2122efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2123c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2124dcca6d9dSJed Brown   ierr = PetscMalloc3(dim+1,&pStart,dim+1,&pEnd,dim+1,&pMax);CHKERRQ(ierr);
21250298fd71SBarry Smith   ierr = DMPlexGetHybridBounds(dm, &pMax[dim], dim>1 ? &pMax[dim-1] : NULL, dim > 2 ? &pMax[1] : NULL, &pMax[0]);CHKERRQ(ierr);
2126efa14ee0SMatthew G Knepley   for (d = 0; d <= dim; ++d) {
2127efa14ee0SMatthew G Knepley     ierr = DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);
2128efa14ee0SMatthew G Knepley     if (pMax[d] >= 0) pEnd[d] = PetscMin(pEnd[d], pMax[d]);
2129efa14ee0SMatthew G Knepley   }
2130efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
213134b4c39eSMatthew G. Knepley   if (vertexLabel) {
2132830e53efSMatthew G. Knepley     ierr = DMLabelGetStratumIS(vertexLabel, value, &subvertexIS);CHKERRQ(ierr);
2133efa14ee0SMatthew G Knepley     if (subvertexIS) {
2134efa14ee0SMatthew G Knepley       ierr = ISGetSize(subvertexIS, &numSubVerticesInitial);CHKERRQ(ierr);
2135efa14ee0SMatthew G Knepley       ierr = ISGetIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
2136efa14ee0SMatthew G Knepley     }
213734b4c39eSMatthew G. Knepley   }
2138efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2139efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
21400298fd71SBarry Smith     PetscInt      *star   = NULL;
2141efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numFaces = 0, f;
2142efa14ee0SMatthew G Knepley 
2143efa14ee0SMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2144efa14ee0SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
2145efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2146158acfadSMatthew G. Knepley       PetscInt       faceLoc;
2147158acfadSMatthew G. Knepley 
2148158acfadSMatthew G. Knepley       if ((point >= pStart[dim-1]) && (point < pEnd[dim-1])) {
2149158acfadSMatthew G. Knepley         if (markedFaces) {
2150158acfadSMatthew G. Knepley           ierr = DMLabelGetValue(vertexLabel, point, &faceLoc);CHKERRQ(ierr);
2151158acfadSMatthew G. Knepley           if (faceLoc < 0) continue;
2152158acfadSMatthew G. Knepley         }
2153158acfadSMatthew G. Knepley         star[numFaces++] = point;
2154158acfadSMatthew G. Knepley       }
2155efa14ee0SMatthew G Knepley     }
2156efa14ee0SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
2157efa14ee0SMatthew G Knepley       const PetscInt face    = star[f];
21580298fd71SBarry Smith       PetscInt      *closure = NULL;
2159efa14ee0SMatthew G Knepley       PetscInt       closureSize, c;
2160efa14ee0SMatthew G Knepley       PetscInt       faceLoc;
2161efa14ee0SMatthew G Knepley 
2162efa14ee0SMatthew G Knepley       ierr = DMLabelGetValue(subpointMap, face, &faceLoc);CHKERRQ(ierr);
2163efa14ee0SMatthew G Knepley       if (faceLoc == dim-1) continue;
216482f516ccSBarry Smith       if (faceLoc >= 0) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %d has dimension %d in the surface label", face, faceLoc);
2165efa14ee0SMatthew G Knepley       ierr = DMPlexGetTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2166efa14ee0SMatthew G Knepley       for (c = 0; c < closureSize*2; c += 2) {
2167efa14ee0SMatthew G Knepley         const PetscInt point = closure[c];
2168efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2169efa14ee0SMatthew G Knepley 
2170efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
2171efa14ee0SMatthew G Knepley           ierr = DMLabelGetValue(vertexLabel, point, &vertexLoc);CHKERRQ(ierr);
2172830e53efSMatthew G. Knepley           if (vertexLoc != value) break;
2173efa14ee0SMatthew G Knepley         }
2174efa14ee0SMatthew G Knepley       }
2175efa14ee0SMatthew G Knepley       if (c == closureSize*2) {
2176efa14ee0SMatthew G Knepley         const PetscInt *support;
2177efa14ee0SMatthew G Knepley         PetscInt        supportSize, s;
2178efa14ee0SMatthew G Knepley 
2179efa14ee0SMatthew G Knepley         for (c = 0; c < closureSize*2; c += 2) {
2180efa14ee0SMatthew G Knepley           const PetscInt point = closure[c];
2181efa14ee0SMatthew G Knepley 
2182efa14ee0SMatthew G Knepley           for (d = 0; d < dim; ++d) {
2183efa14ee0SMatthew G Knepley             if ((point >= pStart[d]) && (point < pEnd[d])) {
2184efa14ee0SMatthew G Knepley               ierr = DMLabelSetValue(subpointMap, point, d);CHKERRQ(ierr);
2185efa14ee0SMatthew G Knepley               break;
2186efa14ee0SMatthew G Knepley             }
2187efa14ee0SMatthew G Knepley           }
2188efa14ee0SMatthew G Knepley         }
2189efa14ee0SMatthew G Knepley         ierr = DMPlexGetSupportSize(dm, face, &supportSize);CHKERRQ(ierr);
2190efa14ee0SMatthew G Knepley         ierr = DMPlexGetSupport(dm, face, &support);CHKERRQ(ierr);
2191efa14ee0SMatthew G Knepley         for (s = 0; s < supportSize; ++s) {
2192efa14ee0SMatthew G Knepley           ierr = DMLabelSetValue(subpointMap, support[s], dim);CHKERRQ(ierr);
2193efa14ee0SMatthew G Knepley         }
2194efa14ee0SMatthew G Knepley       }
2195efa14ee0SMatthew G Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2196efa14ee0SMatthew G Knepley     }
2197efa14ee0SMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2198efa14ee0SMatthew G Knepley   }
219934b4c39eSMatthew G. Knepley   if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subvertices);CHKERRQ(ierr);}
2200efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
2201efa14ee0SMatthew G Knepley   ierr = PetscFree3(pStart,pEnd,pMax);CHKERRQ(ierr);
2202efa14ee0SMatthew G Knepley   PetscFunctionReturn(0);
2203efa14ee0SMatthew G Knepley }
2204efa14ee0SMatthew G Knepley 
220527c04023SMatthew 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)
2206766ab985SMatthew G. Knepley {
220727c04023SMatthew G. Knepley   DMLabel         label = NULL;
2208766ab985SMatthew G. Knepley   const PetscInt *cone;
22099fc93327SToby Isaac   PetscInt        dim, cMax, cEnd, c, subc = 0, p, coneSize = -1;
2210766ab985SMatthew G. Knepley   PetscErrorCode  ierr;
2211766ab985SMatthew G. Knepley 
2212812bfc34SJed Brown   PetscFunctionBegin;
2213c0ed958bSJed Brown   *numFaces = 0;
2214c0ed958bSJed Brown   *nFV = 0;
2215c58f1c22SToby Isaac   if (labelname) {ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);}
2216fed694aaSMatthew G. Knepley   *subCells = NULL;
2217c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2218766ab985SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
2219766ab985SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
2220766ab985SMatthew G. Knepley   if (cMax < 0) PetscFunctionReturn(0);
222127c04023SMatthew G. Knepley   if (label) {
222227c04023SMatthew G. Knepley     for (c = cMax; c < cEnd; ++c) {
222327c04023SMatthew G. Knepley       PetscInt val;
222427c04023SMatthew G. Knepley 
222527c04023SMatthew G. Knepley       ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr);
222627c04023SMatthew G. Knepley       if (val == value) {
222727c04023SMatthew G. Knepley         ++(*numFaces);
222827c04023SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
222927c04023SMatthew G. Knepley       }
223027c04023SMatthew G. Knepley     }
223127c04023SMatthew G. Knepley   } else {
2232766ab985SMatthew G. Knepley     *numFaces = cEnd - cMax;
223327c04023SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cMax, &coneSize);CHKERRQ(ierr);
223427c04023SMatthew G. Knepley   }
2235785e854fSJed Brown   ierr = PetscMalloc1(*numFaces *2, subCells);CHKERRQ(ierr);
22369fc93327SToby Isaac   if (!(*numFaces)) PetscFunctionReturn(0);
22379fc93327SToby Isaac   *nFV = hasLagrange ? coneSize/3 : coneSize/2;
2238766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2239766ab985SMatthew G. Knepley     const PetscInt *cells;
2240766ab985SMatthew G. Knepley     PetscInt        numCells;
2241766ab985SMatthew G. Knepley 
224227c04023SMatthew G. Knepley     if (label) {
224327c04023SMatthew G. Knepley       PetscInt val;
224427c04023SMatthew G. Knepley 
224527c04023SMatthew G. Knepley       ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr);
224627c04023SMatthew G. Knepley       if (val != value) continue;
224727c04023SMatthew G. Knepley     }
2248766ab985SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
2249766ab985SMatthew G. Knepley     for (p = 0; p < *nFV; ++p) {
2250766ab985SMatthew G. Knepley       ierr = DMLabelSetValue(subpointMap, cone[p], 0);CHKERRQ(ierr);
2251766ab985SMatthew G. Knepley     }
2252766ab985SMatthew G. Knepley     /* Negative face */
2253766ab985SMatthew G. Knepley     ierr = DMPlexGetJoin(dm, *nFV, cone, &numCells, &cells);CHKERRQ(ierr);
225427234c99SMatthew G. Knepley     /* Not true in parallel
225527234c99SMatthew G. Knepley     if (numCells != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
2256766ab985SMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
2257766ab985SMatthew G. Knepley       ierr = DMLabelSetValue(subpointMap, cells[p], 2);CHKERRQ(ierr);
225827234c99SMatthew G. Knepley       (*subCells)[subc++] = cells[p];
2259766ab985SMatthew G. Knepley     }
2260766ab985SMatthew G. Knepley     ierr = DMPlexRestoreJoin(dm, *nFV, cone, &numCells, &cells);CHKERRQ(ierr);
2261766ab985SMatthew G. Knepley     /* Positive face is not included */
2262766ab985SMatthew G. Knepley   }
2263766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
2264766ab985SMatthew G. Knepley }
2265766ab985SMatthew G. Knepley 
22663982b651SMatthew G. Knepley static PetscErrorCode DMPlexMarkCohesiveSubmesh_Interpolated(DM dm, DMLabel label, PetscInt value, DMLabel subpointMap, DM subdm)
2267766ab985SMatthew G. Knepley {
2268766ab985SMatthew G. Knepley   PetscInt      *pStart, *pEnd;
2269766ab985SMatthew G. Knepley   PetscInt       dim, cMax, cEnd, c, d;
2270766ab985SMatthew G. Knepley   PetscErrorCode ierr;
2271766ab985SMatthew G. Knepley 
2272812bfc34SJed Brown   PetscFunctionBegin;
2273c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2274766ab985SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
2275766ab985SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
2276766ab985SMatthew G. Knepley   if (cMax < 0) PetscFunctionReturn(0);
2277dcca6d9dSJed Brown   ierr = PetscMalloc2(dim+1,&pStart,dim+1,&pEnd);CHKERRQ(ierr);
2278b3154360SMatthew G. Knepley   for (d = 0; d <= dim; ++d) {ierr = DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);}
2279766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2280766ab985SMatthew G. Knepley     const PetscInt *cone;
2281766ab985SMatthew G. Knepley     PetscInt       *closure = NULL;
2282b3154360SMatthew G. Knepley     PetscInt        fconeSize, coneSize, closureSize, cl, val;
2283766ab985SMatthew G. Knepley 
228427c04023SMatthew G. Knepley     if (label) {
228527c04023SMatthew G. Knepley       ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr);
228627c04023SMatthew G. Knepley       if (val != value) continue;
228727c04023SMatthew G. Knepley     }
2288766ab985SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
2289766ab985SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
2290b3154360SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cone[0], &fconeSize);CHKERRQ(ierr);
2291b3154360SMatthew G. Knepley     if (coneSize != (fconeSize ? fconeSize : 1) + 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells");
2292b3154360SMatthew G. Knepley     /* Negative face */
2293766ab985SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2294766ab985SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
2295766ab985SMatthew G. Knepley       const PetscInt point = closure[cl];
2296766ab985SMatthew G. Knepley 
2297766ab985SMatthew G. Knepley       for (d = 0; d <= dim; ++d) {
2298766ab985SMatthew G. Knepley         if ((point >= pStart[d]) && (point < pEnd[d])) {
2299766ab985SMatthew G. Knepley           ierr = DMLabelSetValue(subpointMap, point, d);CHKERRQ(ierr);
2300766ab985SMatthew G. Knepley           break;
2301766ab985SMatthew G. Knepley         }
2302766ab985SMatthew G. Knepley       }
2303766ab985SMatthew G. Knepley     }
2304766ab985SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2305766ab985SMatthew G. Knepley     /* Cells -- positive face is not included */
2306766ab985SMatthew G. Knepley     for (cl = 0; cl < 1; ++cl) {
2307766ab985SMatthew G. Knepley       const PetscInt *support;
2308766ab985SMatthew G. Knepley       PetscInt        supportSize, s;
2309766ab985SMatthew G. Knepley 
2310766ab985SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[cl], &supportSize);CHKERRQ(ierr);
2311711de394SMatthew G. Knepley       /* if (supportSize != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive faces should separate two cells"); */
2312766ab985SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[cl], &support);CHKERRQ(ierr);
2313766ab985SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
2314766ab985SMatthew G. Knepley         ierr = DMLabelSetValue(subpointMap, support[s], dim);CHKERRQ(ierr);
2315766ab985SMatthew G. Knepley       }
2316766ab985SMatthew G. Knepley     }
2317766ab985SMatthew G. Knepley   }
2318766ab985SMatthew G. Knepley   ierr = PetscFree2(pStart, pEnd);CHKERRQ(ierr);
2319766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
2320766ab985SMatthew G. Knepley }
2321766ab985SMatthew G. Knepley 
2322c08575a3SMatthew G. Knepley static PetscErrorCode DMPlexGetFaceOrientation(DM dm, PetscInt cell, PetscInt numCorners, PetscInt indices[], PetscInt oppositeVertex, PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2323e6ccafaeSMatthew G Knepley {
232482f516ccSBarry Smith   MPI_Comm       comm;
2325e6ccafaeSMatthew G Knepley   PetscBool      posOrient = PETSC_FALSE;
2326e6ccafaeSMatthew G Knepley   const PetscInt debug     = 0;
2327e6ccafaeSMatthew G Knepley   PetscInt       cellDim, faceSize, f;
2328e6ccafaeSMatthew G Knepley   PetscErrorCode ierr;
2329e6ccafaeSMatthew G Knepley 
233082f516ccSBarry Smith   PetscFunctionBegin;
233182f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2332c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &cellDim);CHKERRQ(ierr);
2333367003a6SStefano Zampini   if (debug) {ierr = PetscPrintf(comm, "cellDim: %d numCorners: %d\n", cellDim, numCorners);CHKERRQ(ierr);}
2334e6ccafaeSMatthew G Knepley 
2335ddeab2a6SMatthew G. Knepley   if (cellDim == 1 && numCorners == 2) {
2336ddeab2a6SMatthew G. Knepley     /* Triangle */
2337e6ccafaeSMatthew G Knepley     faceSize  = numCorners-1;
2338e6ccafaeSMatthew G Knepley     posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2339ddeab2a6SMatthew G. Knepley   } else if (cellDim == 2 && numCorners == 3) {
2340ddeab2a6SMatthew G. Knepley     /* Triangle */
2341ddeab2a6SMatthew G. Knepley     faceSize  = numCorners-1;
2342ddeab2a6SMatthew G. Knepley     posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2343ddeab2a6SMatthew G. Knepley   } else if (cellDim == 3 && numCorners == 4) {
2344ddeab2a6SMatthew G. Knepley     /* Tetrahedron */
2345ddeab2a6SMatthew G. Knepley     faceSize  = numCorners-1;
2346ddeab2a6SMatthew G. Knepley     posOrient = (oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2347e6ccafaeSMatthew G Knepley   } else if (cellDim == 1 && numCorners == 3) {
2348e6ccafaeSMatthew G Knepley     /* Quadratic line */
2349e6ccafaeSMatthew G Knepley     faceSize  = 1;
2350e6ccafaeSMatthew G Knepley     posOrient = PETSC_TRUE;
2351e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 4) {
2352e6ccafaeSMatthew G Knepley     /* Quads */
2353e6ccafaeSMatthew G Knepley     faceSize = 2;
2354e6ccafaeSMatthew G Knepley     if ((indices[1] > indices[0]) && (indices[1] - indices[0] == 1)) {
2355e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2356e6ccafaeSMatthew G Knepley     } else if ((indices[0] == 3) && (indices[1] == 0)) {
2357e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2358e6ccafaeSMatthew G Knepley     } else {
2359e6ccafaeSMatthew G Knepley       if (((indices[0] > indices[1]) && (indices[0] - indices[1] == 1)) || ((indices[0] == 0) && (indices[1] == 3))) {
2360e6ccafaeSMatthew G Knepley         posOrient = PETSC_FALSE;
2361e6ccafaeSMatthew G Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossedge");
2362e6ccafaeSMatthew G Knepley     }
2363e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 6) {
2364e6ccafaeSMatthew G Knepley     /* Quadratic triangle (I hate this) */
2365e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2366e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTri = 3;
2367e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2368e6ccafaeSMatthew G Knepley     PetscBool      found                    = PETSC_FALSE;
2369e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTriSorted[9] = {
2370e6ccafaeSMatthew G Knepley       0, 3,  4, /* bottom */
2371e6ccafaeSMatthew G Knepley       1, 4,  5, /* right */
2372e6ccafaeSMatthew G Knepley       2, 3,  5, /* left */
2373e6ccafaeSMatthew G Knepley     };
2374e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTri[9] = {
2375e6ccafaeSMatthew G Knepley       0, 3,  4, /* bottom */
2376e6ccafaeSMatthew G Knepley       1, 4,  5, /* right */
2377e6ccafaeSMatthew G Knepley       2, 5,  3, /* left */
2378e6ccafaeSMatthew G Knepley     };
2379e6ccafaeSMatthew G Knepley 
2380e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTri; ++i) sortedIndices[i] = indices[i];
2381e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeTri, sortedIndices);CHKERRQ(ierr);
2382e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 3; ++iFace) {
2383e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeTri;
2384e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2385e6ccafaeSMatthew G Knepley 
2386e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesTriSorted[ii+0]) &&
2387e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesTriSorted[ii+1])) {
2388e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTri; ++fVertex) {
2389e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTri; ++cVertex) {
2390e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTri[ii+fVertex]) {
2391e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2392e6ccafaeSMatthew G Knepley               break;
2393e6ccafaeSMatthew G Knepley             }
2394e6ccafaeSMatthew G Knepley           }
2395e6ccafaeSMatthew G Knepley         }
2396e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2397e6ccafaeSMatthew G Knepley         break;
2398e6ccafaeSMatthew G Knepley       }
2399e6ccafaeSMatthew G Knepley     }
2400e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid tri crossface");
2401e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2402e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2403e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 9) {
2404e6ccafaeSMatthew G Knepley     /* Quadratic quad (I hate this) */
2405e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2406e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuad = 3;
2407e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2408e6ccafaeSMatthew G Knepley     PetscBool      found                      = PETSC_FALSE;
2409e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadSorted[12] = {
2410e6ccafaeSMatthew G Knepley       0, 1,  4, /* bottom */
2411e6ccafaeSMatthew G Knepley       1, 2,  5, /* right */
2412e6ccafaeSMatthew G Knepley       2, 3,  6, /* top */
2413e6ccafaeSMatthew G Knepley       0, 3,  7, /* left */
2414e6ccafaeSMatthew G Knepley     };
2415e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuad[12] = {
2416e6ccafaeSMatthew G Knepley       0, 1,  4, /* bottom */
2417e6ccafaeSMatthew G Knepley       1, 2,  5, /* right */
2418e6ccafaeSMatthew G Knepley       2, 3,  6, /* top */
2419e6ccafaeSMatthew G Knepley       3, 0,  7, /* left */
2420e6ccafaeSMatthew G Knepley     };
2421e6ccafaeSMatthew G Knepley 
2422e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuad; ++i) sortedIndices[i] = indices[i];
2423e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeQuad, sortedIndices);CHKERRQ(ierr);
2424e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2425e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeQuad;
2426e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2427e6ccafaeSMatthew G Knepley 
2428e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesQuadSorted[ii+0]) &&
2429e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesQuadSorted[ii+1])) {
2430e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuad; ++fVertex) {
2431e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuad; ++cVertex) {
2432e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuad[ii+fVertex]) {
2433e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2434e6ccafaeSMatthew G Knepley               break;
2435e6ccafaeSMatthew G Knepley             }
2436e6ccafaeSMatthew G Knepley           }
2437e6ccafaeSMatthew G Knepley         }
2438e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2439e6ccafaeSMatthew G Knepley         break;
2440e6ccafaeSMatthew G Knepley       }
2441e6ccafaeSMatthew G Knepley     }
2442e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossface");
2443e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2444e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2445e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 8) {
2446e6ccafaeSMatthew G Knepley     /* Hexes
2447e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2448e6ccafaeSMatthew G Knepley        pointing up at the second.
2449e6ccafaeSMatthew G Knepley 
2450e6ccafaeSMatthew G Knepley           7---6
2451e6ccafaeSMatthew G Knepley          /|  /|
2452e6ccafaeSMatthew G Knepley         4---5 |
2453ddeab2a6SMatthew G. Knepley         | 1-|-2
2454e6ccafaeSMatthew G Knepley         |/  |/
2455ddeab2a6SMatthew G. Knepley         0---3
2456e6ccafaeSMatthew G Knepley 
2457e6ccafaeSMatthew G Knepley         Faces are determined by the first 4 vertices (corners of faces) */
2458e6ccafaeSMatthew G Knepley     const PetscInt faceSizeHex = 4;
2459e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[4], i, iFace;
2460e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2461e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHexSorted[24] = {
2462e6ccafaeSMatthew G Knepley       0, 1, 2, 3,  /* bottom */
2463e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  /* top */
2464ddeab2a6SMatthew G. Knepley       0, 3, 4, 5,  /* front */
2465ddeab2a6SMatthew G. Knepley       2, 3, 5, 6,  /* right */
2466ddeab2a6SMatthew G. Knepley       1, 2, 6, 7,  /* back */
2467ddeab2a6SMatthew G. Knepley       0, 1, 4, 7,  /* left */
2468e6ccafaeSMatthew G Knepley     };
2469e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHex[24] = {
2470ddeab2a6SMatthew G. Knepley       1, 2, 3, 0,  /* bottom */
2471e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  /* top */
2472ddeab2a6SMatthew G. Knepley       0, 3, 5, 4,  /* front */
2473ddeab2a6SMatthew G. Knepley       3, 2, 6, 5,  /* right */
2474ddeab2a6SMatthew G. Knepley       2, 1, 7, 6,  /* back */
2475ddeab2a6SMatthew G. Knepley       1, 0, 4, 7,  /* left */
2476e6ccafaeSMatthew G Knepley     };
2477e6ccafaeSMatthew G Knepley 
2478e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeHex; ++i) sortedIndices[i] = indices[i];
2479e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeHex, sortedIndices);CHKERRQ(ierr);
2480e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2481e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeHex;
2482e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2483e6ccafaeSMatthew G Knepley 
2484e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesHexSorted[ii+0]) &&
2485e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesHexSorted[ii+1]) &&
2486e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesHexSorted[ii+2]) &&
2487e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesHexSorted[ii+3])) {
2488e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeHex; ++fVertex) {
2489e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeHex; ++cVertex) {
2490e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesHex[ii+fVertex]) {
2491e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2492e6ccafaeSMatthew G Knepley               break;
2493e6ccafaeSMatthew G Knepley             }
2494e6ccafaeSMatthew G Knepley           }
2495e6ccafaeSMatthew G Knepley         }
2496e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2497e6ccafaeSMatthew G Knepley         break;
2498e6ccafaeSMatthew G Knepley       }
2499e6ccafaeSMatthew G Knepley     }
2500e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2501e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2502e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2503e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 10) {
2504e6ccafaeSMatthew G Knepley     /* Quadratic tet */
2505e6ccafaeSMatthew G Knepley     /* Faces are determined by the first 3 vertices (corners of faces) */
2506e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTet = 6;
2507e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[6], i, iFace;
2508e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2509e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTetSorted[24] = {
2510e6ccafaeSMatthew G Knepley       0, 1, 2,  6, 7, 8, /* bottom */
2511e6ccafaeSMatthew G Knepley       0, 3, 4,  6, 7, 9,  /* front */
2512e6ccafaeSMatthew G Knepley       1, 4, 5,  7, 8, 9,  /* right */
2513e6ccafaeSMatthew G Knepley       2, 3, 5,  6, 8, 9,  /* left */
2514e6ccafaeSMatthew G Knepley     };
2515e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTet[24] = {
2516e6ccafaeSMatthew G Knepley       0, 1, 2,  6, 7, 8, /* bottom */
2517e6ccafaeSMatthew G Knepley       0, 4, 3,  6, 7, 9,  /* front */
2518e6ccafaeSMatthew G Knepley       1, 5, 4,  7, 8, 9,  /* right */
2519e6ccafaeSMatthew G Knepley       2, 3, 5,  8, 6, 9,  /* left */
2520e6ccafaeSMatthew G Knepley     };
2521e6ccafaeSMatthew G Knepley 
2522e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTet; ++i) sortedIndices[i] = indices[i];
2523e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeTet, sortedIndices);CHKERRQ(ierr);
2524e6ccafaeSMatthew G Knepley     for (iFace=0; iFace < 4; ++iFace) {
2525e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeTet;
2526e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2527e6ccafaeSMatthew G Knepley 
2528e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesTetSorted[ii+0]) &&
2529e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesTetSorted[ii+1]) &&
2530e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesTetSorted[ii+2]) &&
2531e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesTetSorted[ii+3])) {
2532e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTet; ++fVertex) {
2533e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTet; ++cVertex) {
2534e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTet[ii+fVertex]) {
2535e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2536e6ccafaeSMatthew G Knepley               break;
2537e6ccafaeSMatthew G Knepley             }
2538e6ccafaeSMatthew G Knepley           }
2539e6ccafaeSMatthew G Knepley         }
2540e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2541e6ccafaeSMatthew G Knepley         break;
2542e6ccafaeSMatthew G Knepley       }
2543e6ccafaeSMatthew G Knepley     }
2544e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid tet crossface");
2545e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2546e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2547e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 27) {
2548e6ccafaeSMatthew G Knepley     /* Quadratic hexes (I hate this)
2549e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2550e6ccafaeSMatthew G Knepley        pointing up at the second.
2551e6ccafaeSMatthew G Knepley 
2552e6ccafaeSMatthew G Knepley          7---6
2553e6ccafaeSMatthew G Knepley         /|  /|
2554e6ccafaeSMatthew G Knepley        4---5 |
2555e6ccafaeSMatthew G Knepley        | 3-|-2
2556e6ccafaeSMatthew G Knepley        |/  |/
2557e6ccafaeSMatthew G Knepley        0---1
2558e6ccafaeSMatthew G Knepley 
2559e6ccafaeSMatthew G Knepley        Faces are determined by the first 4 vertices (corners of faces) */
2560e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuadHex = 9;
2561e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[9], i, iFace;
2562e6ccafaeSMatthew G Knepley     PetscBool      found                         = PETSC_FALSE;
2563e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHexSorted[54] = {
2564e6ccafaeSMatthew G Knepley       0, 1, 2, 3,  8, 9, 10, 11,  24, /* bottom */
2565e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  12, 13, 14, 15,  25, /* top */
2566e6ccafaeSMatthew G Knepley       0, 1, 4, 5,  8, 12, 16, 17,  22, /* front */
2567e6ccafaeSMatthew G Knepley       1, 2, 5, 6,  9, 13, 17, 18,  21, /* right */
2568e6ccafaeSMatthew G Knepley       2, 3, 6, 7,  10, 14, 18, 19,  23, /* back */
2569e6ccafaeSMatthew G Knepley       0, 3, 4, 7,  11, 15, 16, 19,  20, /* left */
2570e6ccafaeSMatthew G Knepley     };
2571e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHex[54] = {
2572e6ccafaeSMatthew G Knepley       3, 2, 1, 0,  10, 9, 8, 11,  24, /* bottom */
2573e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  12, 13, 14, 15,  25, /* top */
2574e6ccafaeSMatthew G Knepley       0, 1, 5, 4,  8, 17, 12, 16,  22, /* front */
2575e6ccafaeSMatthew G Knepley       1, 2, 6, 5,  9, 18, 13, 17,  21, /* right */
2576e6ccafaeSMatthew G Knepley       2, 3, 7, 6,  10, 19, 14, 18,  23, /* back */
2577e6ccafaeSMatthew G Knepley       3, 0, 4, 7,  11, 16, 15, 19,  20 /* left */
2578e6ccafaeSMatthew G Knepley     };
2579e6ccafaeSMatthew G Knepley 
2580e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuadHex; ++i) sortedIndices[i] = indices[i];
2581e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeQuadHex, sortedIndices);CHKERRQ(ierr);
2582e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2583e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeQuadHex;
2584e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2585e6ccafaeSMatthew G Knepley 
2586e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesQuadHexSorted[ii+0]) &&
2587e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesQuadHexSorted[ii+1]) &&
2588e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesQuadHexSorted[ii+2]) &&
2589e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesQuadHexSorted[ii+3])) {
2590e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuadHex; ++fVertex) {
2591e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuadHex; ++cVertex) {
2592e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuadHex[ii+fVertex]) {
2593e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2594e6ccafaeSMatthew G Knepley               break;
2595e6ccafaeSMatthew G Knepley             }
2596e6ccafaeSMatthew G Knepley           }
2597e6ccafaeSMatthew G Knepley         }
2598e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2599e6ccafaeSMatthew G Knepley         break;
2600e6ccafaeSMatthew G Knepley       }
2601e6ccafaeSMatthew G Knepley     }
2602e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2603e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2604e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2605e6ccafaeSMatthew G Knepley   } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Unknown cell type for faceOrientation().");
2606e6ccafaeSMatthew G Knepley   if (!posOrient) {
2607e6ccafaeSMatthew G Knepley     if (debug) {ierr = PetscPrintf(comm, "  Reversing initial face orientation\n");CHKERRQ(ierr);}
2608e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[faceSize-1 - f];
2609e6ccafaeSMatthew G Knepley   } else {
2610e6ccafaeSMatthew G Knepley     if (debug) {ierr = PetscPrintf(comm, "  Keeping initial face orientation\n");CHKERRQ(ierr);}
2611e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[f];
2612e6ccafaeSMatthew G Knepley   }
2613e6ccafaeSMatthew G Knepley   if (posOriented) *posOriented = posOrient;
2614e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2615e6ccafaeSMatthew G Knepley }
2616e6ccafaeSMatthew G Knepley 
2617c08575a3SMatthew G. Knepley /*@
2618c08575a3SMatthew G. Knepley   DMPlexGetOrientedFace - Given a cell and a face, as a set of vertices, return the oriented face, as a set of vertices,
2619c08575a3SMatthew G. Knepley   in faceVertices. The orientation is such that the face normal points out of the cell
2620c08575a3SMatthew G. Knepley 
2621c08575a3SMatthew G. Knepley   Not collective
2622c08575a3SMatthew G. Knepley 
2623c08575a3SMatthew G. Knepley   Input Parameters:
2624c08575a3SMatthew G. Knepley + dm           - The original mesh
2625c08575a3SMatthew G. Knepley . cell         - The cell mesh point
2626c08575a3SMatthew G. Knepley . faceSize     - The number of vertices on the face
2627c08575a3SMatthew G. Knepley . face         - The face vertices
2628c08575a3SMatthew G. Knepley . numCorners   - The number of vertices on the cell
2629c08575a3SMatthew G. Knepley . indices      - Local numbering of face vertices in cell cone
2630c08575a3SMatthew G. Knepley - origVertices - Original face vertices
2631c08575a3SMatthew G. Knepley 
2632c08575a3SMatthew G. Knepley   Output Parameter:
2633c08575a3SMatthew G. Knepley + faceVertices - The face vertices properly oriented
2634c08575a3SMatthew G. Knepley - posOriented  - PETSC_TRUE if the face was oriented with outward normal
2635c08575a3SMatthew G. Knepley 
2636c08575a3SMatthew G. Knepley   Level: developer
2637c08575a3SMatthew G. Knepley 
2638c08575a3SMatthew G. Knepley .seealso: DMPlexGetCone()
2639c08575a3SMatthew G. Knepley @*/
2640e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexGetOrientedFace(DM dm, PetscInt cell, PetscInt faceSize, const PetscInt face[], PetscInt numCorners, PetscInt indices[], PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2641e6ccafaeSMatthew G Knepley {
26420298fd71SBarry Smith   const PetscInt *cone = NULL;
2643e6ccafaeSMatthew G Knepley   PetscInt        coneSize, v, f, v2;
2644e6ccafaeSMatthew G Knepley   PetscInt        oppositeVertex = -1;
2645e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
2646e6ccafaeSMatthew G Knepley 
2647e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
2648e6ccafaeSMatthew G Knepley   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
2649e6ccafaeSMatthew G Knepley   ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
2650e6ccafaeSMatthew G Knepley   for (v = 0, v2 = 0; v < coneSize; ++v) {
2651e6ccafaeSMatthew G Knepley     PetscBool found = PETSC_FALSE;
2652e6ccafaeSMatthew G Knepley 
2653e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) {
2654e6ccafaeSMatthew G Knepley       if (face[f] == cone[v]) {
2655e6ccafaeSMatthew G Knepley         found = PETSC_TRUE; break;
2656e6ccafaeSMatthew G Knepley       }
2657e6ccafaeSMatthew G Knepley     }
2658e6ccafaeSMatthew G Knepley     if (found) {
2659e6ccafaeSMatthew G Knepley       indices[v2]      = v;
2660e6ccafaeSMatthew G Knepley       origVertices[v2] = cone[v];
2661e6ccafaeSMatthew G Knepley       ++v2;
2662e6ccafaeSMatthew G Knepley     } else {
2663e6ccafaeSMatthew G Knepley       oppositeVertex = v;
2664e6ccafaeSMatthew G Knepley     }
2665e6ccafaeSMatthew G Knepley   }
2666e6ccafaeSMatthew G Knepley   ierr = DMPlexGetFaceOrientation(dm, cell, numCorners, indices, oppositeVertex, origVertices, faceVertices, posOriented);CHKERRQ(ierr);
2667e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2668e6ccafaeSMatthew G Knepley }
2669e6ccafaeSMatthew G Knepley 
2670e6ccafaeSMatthew G Knepley /*
2671cd0c2139SMatthew G Knepley   DMPlexInsertFace_Internal - Puts a face into the mesh
2672e6ccafaeSMatthew G Knepley 
2673e6ccafaeSMatthew G Knepley   Not collective
2674e6ccafaeSMatthew G Knepley 
2675e6ccafaeSMatthew G Knepley   Input Parameters:
2676e6ccafaeSMatthew G Knepley   + dm              - The DMPlex
2677e6ccafaeSMatthew G Knepley   . numFaceVertex   - The number of vertices in the face
2678e6ccafaeSMatthew G Knepley   . faceVertices    - The vertices in the face for dm
2679e6ccafaeSMatthew G Knepley   . subfaceVertices - The vertices in the face for subdm
2680e6ccafaeSMatthew G Knepley   . numCorners      - The number of vertices in the cell
2681e6ccafaeSMatthew G Knepley   . cell            - A cell in dm containing the face
2682e6ccafaeSMatthew G Knepley   . subcell         - A cell in subdm containing the face
2683e6ccafaeSMatthew G Knepley   . firstFace       - First face in the mesh
2684e6ccafaeSMatthew G Knepley   - newFacePoint    - Next face in the mesh
2685e6ccafaeSMatthew G Knepley 
2686e6ccafaeSMatthew G Knepley   Output Parameters:
2687e6ccafaeSMatthew G Knepley   . newFacePoint - Contains next face point number on input, updated on output
2688e6ccafaeSMatthew G Knepley 
2689e6ccafaeSMatthew G Knepley   Level: developer
2690e6ccafaeSMatthew G Knepley */
2691cd0c2139SMatthew 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)
2692e6ccafaeSMatthew G Knepley {
269382f516ccSBarry Smith   MPI_Comm        comm;
2694e6ccafaeSMatthew G Knepley   DM_Plex        *submesh = (DM_Plex*) subdm->data;
2695e6ccafaeSMatthew G Knepley   const PetscInt *faces;
2696e6ccafaeSMatthew G Knepley   PetscInt        numFaces, coneSize;
2697e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
2698e6ccafaeSMatthew G Knepley 
2699e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
270082f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2701e6ccafaeSMatthew G Knepley   ierr = DMPlexGetConeSize(subdm, subcell, &coneSize);CHKERRQ(ierr);
2702e6ccafaeSMatthew G Knepley   if (coneSize != 1) SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cone size of cell %d is %d != 1", cell, coneSize);
2703e6ccafaeSMatthew G Knepley #if 0
2704e6ccafaeSMatthew G Knepley   /* Cannot use this because support() has not been constructed yet */
2705e6ccafaeSMatthew G Knepley   ierr = DMPlexGetJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces);CHKERRQ(ierr);
2706e6ccafaeSMatthew G Knepley #else
2707e6ccafaeSMatthew G Knepley   {
2708e6ccafaeSMatthew G Knepley     PetscInt f;
2709e6ccafaeSMatthew G Knepley 
2710e6ccafaeSMatthew G Knepley     numFaces = 0;
271169291d52SBarry Smith     ierr     = DMGetWorkArray(subdm, 1, MPIU_INT, (void **) &faces);CHKERRQ(ierr);
2712e6ccafaeSMatthew G Knepley     for (f = firstFace; f < *newFacePoint; ++f) {
2713e6ccafaeSMatthew G Knepley       PetscInt dof, off, d;
2714e6ccafaeSMatthew G Knepley 
2715e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(submesh->coneSection, f, &dof);CHKERRQ(ierr);
2716e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetOffset(submesh->coneSection, f, &off);CHKERRQ(ierr);
2717e6ccafaeSMatthew G Knepley       /* Yes, I know this is quadratic, but I expect the sizes to be <5 */
2718e6ccafaeSMatthew G Knepley       for (d = 0; d < dof; ++d) {
2719e6ccafaeSMatthew G Knepley         const PetscInt p = submesh->cones[off+d];
2720e6ccafaeSMatthew G Knepley         PetscInt       v;
2721e6ccafaeSMatthew G Knepley 
2722e6ccafaeSMatthew G Knepley         for (v = 0; v < numFaceVertices; ++v) {
2723e6ccafaeSMatthew G Knepley           if (subfaceVertices[v] == p) break;
2724e6ccafaeSMatthew G Knepley         }
2725e6ccafaeSMatthew G Knepley         if (v == numFaceVertices) break;
2726e6ccafaeSMatthew G Knepley       }
2727e6ccafaeSMatthew G Knepley       if (d == dof) {
2728e6ccafaeSMatthew G Knepley         numFaces               = 1;
2729e6ccafaeSMatthew G Knepley         ((PetscInt*) faces)[0] = f;
2730e6ccafaeSMatthew G Knepley       }
2731e6ccafaeSMatthew G Knepley     }
2732e6ccafaeSMatthew G Knepley   }
2733e6ccafaeSMatthew G Knepley #endif
2734e6ccafaeSMatthew G Knepley   if (numFaces > 1) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Vertex set had %d faces, not one", numFaces);
2735e6ccafaeSMatthew G Knepley   else if (numFaces == 1) {
2736e6ccafaeSMatthew G Knepley     /* Add the other cell neighbor for this face */
2737766ab985SMatthew G. Knepley     ierr = DMPlexSetCone(subdm, subcell, faces);CHKERRQ(ierr);
2738e6ccafaeSMatthew G Knepley   } else {
2739e6ccafaeSMatthew G Knepley     PetscInt *indices, *origVertices, *orientedVertices, *orientedSubVertices, v, ov;
2740e6ccafaeSMatthew G Knepley     PetscBool posOriented;
2741e6ccafaeSMatthew G Knepley 
274269291d52SBarry Smith     ierr                = DMGetWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices);CHKERRQ(ierr);
2743e6ccafaeSMatthew G Knepley     origVertices        = &orientedVertices[numFaceVertices];
2744e6ccafaeSMatthew G Knepley     indices             = &orientedVertices[numFaceVertices*2];
2745e6ccafaeSMatthew G Knepley     orientedSubVertices = &orientedVertices[numFaceVertices*3];
2746e6ccafaeSMatthew G Knepley     ierr                = DMPlexGetOrientedFace(dm, cell, numFaceVertices, faceVertices, numCorners, indices, origVertices, orientedVertices, &posOriented);CHKERRQ(ierr);
2747e6ccafaeSMatthew G Knepley     /* TODO: I know that routine should return a permutation, not the indices */
2748e6ccafaeSMatthew G Knepley     for (v = 0; v < numFaceVertices; ++v) {
2749e6ccafaeSMatthew G Knepley       const PetscInt vertex = faceVertices[v], subvertex = subfaceVertices[v];
2750e6ccafaeSMatthew G Knepley       for (ov = 0; ov < numFaceVertices; ++ov) {
2751e6ccafaeSMatthew G Knepley         if (orientedVertices[ov] == vertex) {
2752e6ccafaeSMatthew G Knepley           orientedSubVertices[ov] = subvertex;
2753e6ccafaeSMatthew G Knepley           break;
2754e6ccafaeSMatthew G Knepley         }
2755e6ccafaeSMatthew G Knepley       }
2756e6ccafaeSMatthew G Knepley       if (ov == numFaceVertices) SETERRQ1(comm, PETSC_ERR_PLIB, "Could not find face vertex %d in orientated set", vertex);
2757e6ccafaeSMatthew G Knepley     }
2758e6ccafaeSMatthew G Knepley     ierr = DMPlexSetCone(subdm, *newFacePoint, orientedSubVertices);CHKERRQ(ierr);
2759e6ccafaeSMatthew G Knepley     ierr = DMPlexSetCone(subdm, subcell, newFacePoint);CHKERRQ(ierr);
276069291d52SBarry Smith     ierr = DMRestoreWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices);CHKERRQ(ierr);
2761e6ccafaeSMatthew G Knepley     ++(*newFacePoint);
2762e6ccafaeSMatthew G Knepley   }
2763ef07cca7SMatthew G. Knepley #if 0
2764e6ccafaeSMatthew G Knepley   ierr = DMPlexRestoreJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces);CHKERRQ(ierr);
2765ef07cca7SMatthew G. Knepley #else
276669291d52SBarry Smith   ierr = DMRestoreWorkArray(subdm, 1, MPIU_INT, (void **) &faces);CHKERRQ(ierr);
2767ef07cca7SMatthew G. Knepley #endif
2768e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2769e6ccafaeSMatthew G Knepley }
2770e6ccafaeSMatthew G Knepley 
277153156dfcSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm)
2772e6ccafaeSMatthew G Knepley {
277382f516ccSBarry Smith   MPI_Comm        comm;
277453156dfcSMatthew G. Knepley   DMLabel         subpointMap;
2775efa14ee0SMatthew G Knepley   IS              subvertexIS,  subcellIS;
2776efa14ee0SMatthew G Knepley   const PetscInt *subVertices, *subCells;
2777efa14ee0SMatthew G Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells;
2778fed694aaSMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces = 0, firstSubFace, newFacePoint, nFV = 0;
2779efa14ee0SMatthew G Knepley   PetscInt        vStart, vEnd, c, f;
2780e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
2781e6ccafaeSMatthew G Knepley 
2782e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
278382f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2784efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
2785d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap);CHKERRQ(ierr);
2786efa14ee0SMatthew G Knepley   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
2787efa14ee0SMatthew G Knepley   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
278853156dfcSMatthew G. Knepley   if (vertexLabel) {ierr = DMPlexMarkSubmesh_Uninterpolated(dm, vertexLabel, value, subpointMap, &numSubFaces, &nFV, subdm);CHKERRQ(ierr);}
2789efa14ee0SMatthew G Knepley   /* Setup chart */
2790efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumSize(subpointMap, 0, &numSubVertices);CHKERRQ(ierr);
2791efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumSize(subpointMap, 2, &numSubCells);CHKERRQ(ierr);
2792efa14ee0SMatthew G Knepley   ierr = DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices);CHKERRQ(ierr);
2793efa14ee0SMatthew G Knepley   ierr = DMPlexSetVTKCellHeight(subdm, 1);CHKERRQ(ierr);
2794e6ccafaeSMatthew G Knepley   /* Set cone sizes */
2795e6ccafaeSMatthew G Knepley   firstSubVertex = numSubCells;
2796efa14ee0SMatthew G Knepley   firstSubFace   = numSubCells+numSubVertices;
2797e6ccafaeSMatthew G Knepley   newFacePoint   = firstSubFace;
2798efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumIS(subpointMap, 0, &subvertexIS);CHKERRQ(ierr);
2799efa14ee0SMatthew G Knepley   if (subvertexIS) {ierr = ISGetIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
2800efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumIS(subpointMap, 2, &subcellIS);CHKERRQ(ierr);
2801efa14ee0SMatthew G Knepley   if (subcellIS) {ierr = ISGetIndices(subcellIS, &subCells);CHKERRQ(ierr);}
2802e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
2803e6ccafaeSMatthew G Knepley     ierr = DMPlexSetConeSize(subdm, c, 1);CHKERRQ(ierr);
2804e6ccafaeSMatthew G Knepley   }
2805e6ccafaeSMatthew G Knepley   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
2806e6ccafaeSMatthew G Knepley     ierr = DMPlexSetConeSize(subdm, f, nFV);CHKERRQ(ierr);
2807e6ccafaeSMatthew G Knepley   }
2808e6ccafaeSMatthew G Knepley   ierr = DMSetUp(subdm);CHKERRQ(ierr);
2809e6ccafaeSMatthew G Knepley   /* Create face cones */
2810efa14ee0SMatthew G Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
28110298fd71SBarry Smith   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
281269291d52SBarry Smith   ierr = DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
2813e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
2814e6ccafaeSMatthew G Knepley     const PetscInt cell    = subCells[c];
2815efa14ee0SMatthew G Knepley     const PetscInt subcell = c;
28160298fd71SBarry Smith     PetscInt      *closure = NULL;
2817efa14ee0SMatthew G Knepley     PetscInt       closureSize, cl, numCorners = 0, faceSize = 0;
2818e6ccafaeSMatthew G Knepley 
2819e6ccafaeSMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2820efa14ee0SMatthew G Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
2821efa14ee0SMatthew G Knepley       const PetscInt point = closure[cl];
2822e6ccafaeSMatthew G Knepley       PetscInt       subVertex;
2823e6ccafaeSMatthew G Knepley 
2824efa14ee0SMatthew G Knepley       if ((point >= vStart) && (point < vEnd)) {
2825efa14ee0SMatthew G Knepley         ++numCorners;
2826efa14ee0SMatthew G Knepley         ierr = PetscFindInt(point, numSubVertices, subVertices, &subVertex);CHKERRQ(ierr);
2827efa14ee0SMatthew G Knepley         if (subVertex >= 0) {
2828efa14ee0SMatthew G Knepley           closure[faceSize] = point;
282965560c7fSMatthew G Knepley           subface[faceSize] = firstSubVertex+subVertex;
2830e6ccafaeSMatthew G Knepley           ++faceSize;
2831e6ccafaeSMatthew G Knepley         }
2832e6ccafaeSMatthew G Knepley       }
2833e6ccafaeSMatthew G Knepley     }
2834efa14ee0SMatthew G Knepley     if (faceSize > nFV) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %d of an element on the surface", faceSize);
2835efa14ee0SMatthew G Knepley     if (faceSize == nFV) {
2836cd0c2139SMatthew G Knepley       ierr = DMPlexInsertFace_Internal(dm, subdm, faceSize, closure, subface, numCorners, cell, subcell, firstSubFace, &newFacePoint);CHKERRQ(ierr);
2837e6ccafaeSMatthew G Knepley     }
283865560c7fSMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2839e6ccafaeSMatthew G Knepley   }
284069291d52SBarry Smith   ierr = DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
2841e6ccafaeSMatthew G Knepley   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
2842e6ccafaeSMatthew G Knepley   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
2843e6ccafaeSMatthew G Knepley   /* Build coordinates */
2844efa14ee0SMatthew G Knepley   {
2845efa14ee0SMatthew G Knepley     PetscSection coordSection, subCoordSection;
2846efa14ee0SMatthew G Knepley     Vec          coordinates, subCoordinates;
2847efa14ee0SMatthew G Knepley     PetscScalar *coords, *subCoords;
2848285d324eSMatthew G. Knepley     PetscInt     numComp, coordSize, v;
284924640c55SToby Isaac     const char  *name;
2850efa14ee0SMatthew G Knepley 
285169d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
2852e6ccafaeSMatthew G Knepley     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
285369d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
2854285d324eSMatthew G. Knepley     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
2855285d324eSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
2856285d324eSMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
2857efa14ee0SMatthew G Knepley     ierr = PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices);CHKERRQ(ierr);
2858efa14ee0SMatthew G Knepley     for (v = 0; v < numSubVertices; ++v) {
2859efa14ee0SMatthew G Knepley       const PetscInt vertex    = subVertices[v];
2860efa14ee0SMatthew G Knepley       const PetscInt subvertex = firstSubVertex+v;
2861efa14ee0SMatthew G Knepley       PetscInt       dof;
2862efa14ee0SMatthew G Knepley 
2863efa14ee0SMatthew G Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
2864efa14ee0SMatthew G Knepley       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
2865285d324eSMatthew G. Knepley       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
2866e6ccafaeSMatthew G Knepley     }
2867e6ccafaeSMatthew G Knepley     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
2868e6ccafaeSMatthew G Knepley     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
28698b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &subCoordinates);CHKERRQ(ierr);
287024640c55SToby Isaac     ierr = PetscObjectGetName((PetscObject)coordinates,&name);CHKERRQ(ierr);
287124640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)subCoordinates,name);CHKERRQ(ierr);
2872e6ccafaeSMatthew G Knepley     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
28732eb5907fSJed Brown     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
2874830e53efSMatthew G. Knepley     if (coordSize) {
2875e6ccafaeSMatthew G Knepley       ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
2876e6ccafaeSMatthew G Knepley       ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
2877efa14ee0SMatthew G Knepley       for (v = 0; v < numSubVertices; ++v) {
2878efa14ee0SMatthew G Knepley         const PetscInt vertex    = subVertices[v];
2879efa14ee0SMatthew G Knepley         const PetscInt subvertex = firstSubVertex+v;
2880efa14ee0SMatthew G Knepley         PetscInt       dof, off, sdof, soff, d;
2881e6ccafaeSMatthew G Knepley 
2882e6ccafaeSMatthew G Knepley         ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
2883e6ccafaeSMatthew G Knepley         ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
2884efa14ee0SMatthew G Knepley         ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
2885efa14ee0SMatthew G Knepley         ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
2886efa14ee0SMatthew G Knepley         if (dof != sdof) SETERRQ4(comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
2887e6ccafaeSMatthew G Knepley         for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
2888e6ccafaeSMatthew G Knepley       }
2889e6ccafaeSMatthew G Knepley       ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
2890e6ccafaeSMatthew G Knepley       ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
28913b399e24SMatthew G. Knepley     }
2892e6ccafaeSMatthew G Knepley     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
2893e6ccafaeSMatthew G Knepley     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
2894e6ccafaeSMatthew G Knepley   }
2895efa14ee0SMatthew G Knepley   /* Cleanup */
2896efa14ee0SMatthew G Knepley   if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
2897efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
2898efa14ee0SMatthew G Knepley   if (subcellIS) {ierr = ISRestoreIndices(subcellIS, &subCells);CHKERRQ(ierr);}
2899efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subcellIS);CHKERRQ(ierr);
2900e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2901e6ccafaeSMatthew G Knepley }
2902e6ccafaeSMatthew G Knepley 
29033982b651SMatthew G. Knepley PETSC_STATIC_INLINE PetscInt DMPlexFilterPoint_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[])
29043982b651SMatthew G. Knepley {
29053982b651SMatthew G. Knepley   PetscInt       subPoint;
29063982b651SMatthew G. Knepley   PetscErrorCode ierr;
29073982b651SMatthew G. Knepley 
29083982b651SMatthew G. Knepley   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint); if (ierr < 0) return ierr;
29093982b651SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint+subPoint;
29103982b651SMatthew G. Knepley }
29113982b651SMatthew G. Knepley 
2912158acfadSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmeshGeneric_Interpolated(DM dm, DMLabel label, PetscInt value, PetscBool markedFaces, PetscBool isCohesive, PetscInt cellHeight, DM subdm)
2913e6ccafaeSMatthew G Knepley {
291482f516ccSBarry Smith   MPI_Comm         comm;
291553156dfcSMatthew G. Knepley   DMLabel          subpointMap;
2916efa14ee0SMatthew G Knepley   IS              *subpointIS;
2917efa14ee0SMatthew G Knepley   const PetscInt **subpoints;
29183982b651SMatthew G. Knepley   PetscInt        *numSubPoints, *firstSubPoint, *coneNew, *orntNew;
29190d366550SMatthew G. Knepley   PetscInt         totSubPoints = 0, maxConeSize, cMax, cEnd, dim, p, d, v;
29200d366550SMatthew G. Knepley   PetscMPIInt      rank;
2921e6ccafaeSMatthew G Knepley   PetscErrorCode   ierr;
2922e6ccafaeSMatthew G Knepley 
2923e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
292482f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
29250d366550SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
2926efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
2927d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap);CHKERRQ(ierr);
2928efa14ee0SMatthew G Knepley   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
2929bec263e5SMatthew G. Knepley   if (cellHeight) {
29303982b651SMatthew G. Knepley     if (isCohesive) {ierr = DMPlexMarkCohesiveSubmesh_Interpolated(dm, label, value, subpointMap, subdm);CHKERRQ(ierr);}
2931158acfadSMatthew G. Knepley     else            {ierr = DMPlexMarkSubmesh_Interpolated(dm, label, value, markedFaces, subpointMap, subdm);CHKERRQ(ierr);}
2932bec263e5SMatthew G. Knepley   } else {
2933bec263e5SMatthew G. Knepley     DMLabel         depth;
2934bec263e5SMatthew G. Knepley     IS              pointIS;
2935bec263e5SMatthew G. Knepley     const PetscInt *points;
2936bec263e5SMatthew G. Knepley     PetscInt        numPoints;
2937bec263e5SMatthew G. Knepley 
2938bec263e5SMatthew G. Knepley     ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
2939bec263e5SMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, value, &numPoints);CHKERRQ(ierr);
2940bec263e5SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, value, &pointIS);CHKERRQ(ierr);
2941bec263e5SMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
2942bec263e5SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2943bec263e5SMatthew G. Knepley       PetscInt *closure = NULL;
2944bec263e5SMatthew G. Knepley       PetscInt  closureSize, c, pdim;
2945bec263e5SMatthew G. Knepley 
2946bec263e5SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2947bec263e5SMatthew G. Knepley       for (c = 0; c < closureSize*2; c += 2) {
2948bec263e5SMatthew G. Knepley         ierr = DMLabelGetValue(depth, closure[c], &pdim);CHKERRQ(ierr);
2949bec263e5SMatthew G. Knepley         ierr = DMLabelSetValue(subpointMap, closure[c], pdim);CHKERRQ(ierr);
2950bec263e5SMatthew G. Knepley       }
2951bec263e5SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2952bec263e5SMatthew G. Knepley     }
2953bec263e5SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
2954bec263e5SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
2955bec263e5SMatthew G. Knepley   }
29560d366550SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
29570d366550SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
29580d366550SMatthew G. Knepley   cMax = (cMax < 0) ? cEnd : cMax;
2959efa14ee0SMatthew G Knepley   /* Setup chart */
2960c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2961dcca6d9dSJed Brown   ierr = PetscMalloc4(dim+1,&numSubPoints,dim+1,&firstSubPoint,dim+1,&subpointIS,dim+1,&subpoints);CHKERRQ(ierr);
2962e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
2963e6ccafaeSMatthew G Knepley     ierr = DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d]);CHKERRQ(ierr);
2964e6ccafaeSMatthew G Knepley     totSubPoints += numSubPoints[d];
2965e6ccafaeSMatthew G Knepley   }
2966e6ccafaeSMatthew G Knepley   ierr = DMPlexSetChart(subdm, 0, totSubPoints);CHKERRQ(ierr);
2967bec263e5SMatthew G. Knepley   ierr = DMPlexSetVTKCellHeight(subdm, cellHeight);CHKERRQ(ierr);
2968e6ccafaeSMatthew G Knepley   /* Set cone sizes */
2969e6ccafaeSMatthew G Knepley   firstSubPoint[dim] = 0;
2970e6ccafaeSMatthew G Knepley   firstSubPoint[0]   = firstSubPoint[dim] + numSubPoints[dim];
2971e6ccafaeSMatthew G Knepley   if (dim > 1) {firstSubPoint[dim-1] = firstSubPoint[0]     + numSubPoints[0];}
2972e6ccafaeSMatthew G Knepley   if (dim > 2) {firstSubPoint[dim-2] = firstSubPoint[dim-1] + numSubPoints[dim-1];}
2973e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
2974e6ccafaeSMatthew G Knepley     ierr = DMLabelGetStratumIS(subpointMap, d, &subpointIS[d]);CHKERRQ(ierr);
29750ae02aa4SMatthew G. Knepley     if (subpointIS[d]) {ierr = ISGetIndices(subpointIS[d], &subpoints[d]);CHKERRQ(ierr);}
2976e6ccafaeSMatthew G Knepley   }
2977e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
2978e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
2979e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
2980e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
2981e6ccafaeSMatthew G Knepley       const PetscInt *cone;
2982e6ccafaeSMatthew G Knepley       PetscInt        coneSize, coneSizeNew, c, val;
2983e6ccafaeSMatthew G Knepley 
2984e6ccafaeSMatthew G Knepley       ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
2985e6ccafaeSMatthew G Knepley       ierr = DMPlexSetConeSize(subdm, subpoint, coneSize);CHKERRQ(ierr);
2986bec263e5SMatthew G. Knepley       if (cellHeight && (d == dim)) {
2987e6ccafaeSMatthew G Knepley         ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
2988e6ccafaeSMatthew G Knepley         for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
2989e6ccafaeSMatthew G Knepley           ierr = DMLabelGetValue(subpointMap, cone[c], &val);CHKERRQ(ierr);
2990e6ccafaeSMatthew G Knepley           if (val >= 0) coneSizeNew++;
2991e6ccafaeSMatthew G Knepley         }
2992e6ccafaeSMatthew G Knepley         ierr = DMPlexSetConeSize(subdm, subpoint, coneSizeNew);CHKERRQ(ierr);
2993e6ccafaeSMatthew G Knepley       }
2994e6ccafaeSMatthew G Knepley     }
2995e6ccafaeSMatthew G Knepley   }
2996d67d17b1SMatthew G. Knepley   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
2997e6ccafaeSMatthew G Knepley   ierr = DMSetUp(subdm);CHKERRQ(ierr);
2998e6ccafaeSMatthew G Knepley   /* Set cones */
29990298fd71SBarry Smith   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
30003982b651SMatthew G. Knepley   ierr = PetscMalloc2(maxConeSize,&coneNew,maxConeSize,&orntNew);CHKERRQ(ierr);
3001e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
3002e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3003e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3004e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
30050e49e2e2SMatthew G. Knepley       const PetscInt *cone, *ornt;
30060d366550SMatthew G. Knepley       PetscInt        coneSize, subconeSize, coneSizeNew, c, subc, fornt = 0;
3007e6ccafaeSMatthew G Knepley 
30080d366550SMatthew G. Knepley       if (d == dim-1) {
30090d366550SMatthew G. Knepley         const PetscInt *support, *cone, *ornt;
30100d366550SMatthew G. Knepley         PetscInt        supportSize, coneSize, s, subc;
30110d366550SMatthew G. Knepley 
30120d366550SMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
30130d366550SMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
30140d366550SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
30150d366550SMatthew G. Knepley           if ((support[s] < cMax) || (support[s] >= cEnd)) continue;
30160d366550SMatthew G. Knepley           ierr = PetscFindInt(support[s], numSubPoints[d+1], subpoints[d+1], &subc);CHKERRQ(ierr);
30170d366550SMatthew G. Knepley           if (subc >= 0) {
30180d366550SMatthew G. Knepley             const PetscInt ccell = subpoints[d+1][subc];
30190d366550SMatthew G. Knepley 
30207f79407eSBarry Smith             ierr = DMPlexGetCone(dm, ccell, &cone);CHKERRQ(ierr);
30217f79407eSBarry Smith             ierr = DMPlexGetConeSize(dm, ccell, &coneSize);CHKERRQ(ierr);
30227f79407eSBarry Smith             ierr = DMPlexGetConeOrientation(dm, ccell, &ornt);CHKERRQ(ierr);
30230d366550SMatthew G. Knepley             for (c = 0; c < coneSize; ++c) {
30240d366550SMatthew G. Knepley               if (cone[c] == point) {
30250d366550SMatthew G. Knepley                 fornt = ornt[c];
30260d366550SMatthew G. Knepley                 break;
30270d366550SMatthew G. Knepley               }
30280d366550SMatthew G. Knepley             }
30290d366550SMatthew G. Knepley             break;
30300d366550SMatthew G. Knepley           }
30310d366550SMatthew G. Knepley         }
30320d366550SMatthew G. Knepley       }
3033e6ccafaeSMatthew G Knepley       ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
3034e6ccafaeSMatthew G Knepley       ierr = DMPlexGetConeSize(subdm, subpoint, &subconeSize);CHKERRQ(ierr);
3035e6ccafaeSMatthew G Knepley       ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
30360e49e2e2SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, point, &ornt);CHKERRQ(ierr);
3037e6ccafaeSMatthew G Knepley       for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
3038e6ccafaeSMatthew G Knepley         ierr = PetscFindInt(cone[c], numSubPoints[d-1], subpoints[d-1], &subc);CHKERRQ(ierr);
303901a2673eSMatthew G. Knepley         if (subc >= 0) {
304001a2673eSMatthew G. Knepley           coneNew[coneSizeNew] = firstSubPoint[d-1] + subc;
30413982b651SMatthew G. Knepley           orntNew[coneSizeNew] = ornt[c];
304201a2673eSMatthew G. Knepley           ++coneSizeNew;
304301a2673eSMatthew G. Knepley         }
3044e6ccafaeSMatthew G Knepley       }
3045e6ccafaeSMatthew G Knepley       if (coneSizeNew != subconeSize) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of cone points located %d does not match subcone size %d", coneSizeNew, subconeSize);
30460d366550SMatthew G. Knepley       if (fornt < 0) {
30470d366550SMatthew G. Knepley         /* This should be replaced by a call to DMPlexReverseCell() */
30480d366550SMatthew G. Knepley #if 0
3049a129c400SMatthew G. Knepley         ierr = DMPlexReverseCell(subdm, subpoint);CHKERRQ(ierr);
3050a129c400SMatthew G. Knepley #else
3051c0b40891SMatthew G. Knepley         for (c = 0; c < coneSizeNew/2 + coneSizeNew%2; ++c) {
3052a129c400SMatthew G. Knepley           PetscInt faceSize, tmp;
30530d366550SMatthew G. Knepley 
30540d366550SMatthew G. Knepley           tmp        = coneNew[c];
30550d366550SMatthew G. Knepley           coneNew[c] = coneNew[coneSizeNew-1-c];
30560d366550SMatthew G. Knepley           coneNew[coneSizeNew-1-c] = tmp;
3057a129c400SMatthew G. Knepley           ierr = DMPlexGetConeSize(dm, cone[c], &faceSize);CHKERRQ(ierr);
3058a129c400SMatthew G. Knepley           tmp        = orntNew[c] >= 0 ? -(faceSize-orntNew[c]) : faceSize+orntNew[c];
3059a129c400SMatthew G. Knepley           orntNew[c] = orntNew[coneSizeNew-1-c] >= 0 ? -(faceSize-orntNew[coneSizeNew-1-c]) : faceSize+orntNew[coneSizeNew-1-c];
30600d366550SMatthew G. Knepley           orntNew[coneSizeNew-1-c] = tmp;
30610d366550SMatthew G. Knepley         }
30620d366550SMatthew G. Knepley       }
3063e6ccafaeSMatthew G Knepley       ierr = DMPlexSetCone(subdm, subpoint, coneNew);CHKERRQ(ierr);
30643982b651SMatthew G. Knepley       ierr = DMPlexSetConeOrientation(subdm, subpoint, orntNew);CHKERRQ(ierr);
3065a129c400SMatthew G. Knepley #endif
3066e6ccafaeSMatthew G Knepley     }
3067e6ccafaeSMatthew G Knepley   }
30683982b651SMatthew G. Knepley   ierr = PetscFree2(coneNew,orntNew);CHKERRQ(ierr);
3069e6ccafaeSMatthew G Knepley   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
3070e6ccafaeSMatthew G Knepley   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
3071e6ccafaeSMatthew G Knepley   /* Build coordinates */
3072e6ccafaeSMatthew G Knepley   {
3073e6ccafaeSMatthew G Knepley     PetscSection coordSection, subCoordSection;
3074e6ccafaeSMatthew G Knepley     Vec          coordinates, subCoordinates;
3075e6ccafaeSMatthew G Knepley     PetscScalar *coords, *subCoords;
3076c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize;
307724640c55SToby Isaac     const char  *name;
3078e6ccafaeSMatthew G Knepley 
3079c0e8cf5fSMatthew G. Knepley     ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
308069d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
3081e6ccafaeSMatthew G Knepley     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
308269d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
3083285d324eSMatthew G. Knepley     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
3084285d324eSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
3085285d324eSMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
3086e6ccafaeSMatthew G Knepley     ierr = PetscSectionSetChart(subCoordSection, firstSubPoint[0], firstSubPoint[0]+numSubPoints[0]);CHKERRQ(ierr);
3087e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3088e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3089e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0]+v;
3090e6ccafaeSMatthew G Knepley       PetscInt       dof;
3091e6ccafaeSMatthew G Knepley 
3092e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3093e6ccafaeSMatthew G Knepley       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
3094285d324eSMatthew G. Knepley       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
3095e6ccafaeSMatthew G Knepley     }
3096e6ccafaeSMatthew G Knepley     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
3097e6ccafaeSMatthew G Knepley     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
30988b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &subCoordinates);CHKERRQ(ierr);
309924640c55SToby Isaac     ierr = PetscObjectGetName((PetscObject)coordinates,&name);CHKERRQ(ierr);
310024640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)subCoordinates,name);CHKERRQ(ierr);
3101e6ccafaeSMatthew G Knepley     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
3102c0e8cf5fSMatthew G. Knepley     ierr = VecSetBlockSize(subCoordinates, cdim);CHKERRQ(ierr);
31032eb5907fSJed Brown     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
3104e6ccafaeSMatthew G Knepley     ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
3105e6ccafaeSMatthew G Knepley     ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3106e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3107e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3108e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0]+v;
3109e6ccafaeSMatthew G Knepley       PetscInt dof, off, sdof, soff, d;
3110e6ccafaeSMatthew G Knepley 
3111e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3112e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
3113e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
3114e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
3115e6ccafaeSMatthew G Knepley       if (dof != sdof) SETERRQ4(comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
3116efa14ee0SMatthew G Knepley       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3117e6ccafaeSMatthew G Knepley     }
3118e6ccafaeSMatthew G Knepley     ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
3119e6ccafaeSMatthew G Knepley     ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3120e6ccafaeSMatthew G Knepley     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
3121e6ccafaeSMatthew G Knepley     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
3122e6ccafaeSMatthew G Knepley   }
31233982b651SMatthew G. Knepley   /* Build SF: We need this complexity because subpoints might not be selected on the owning process */
31243982b651SMatthew G. Knepley   {
31253982b651SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
31263982b651SMatthew G. Knepley     IS                 subpIS;
31273982b651SMatthew G. Knepley     const PetscSFNode *remotePoints;
31283982b651SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
31293982b651SMatthew G. Knepley     const PetscInt    *localPoints, *subpoints;
31303982b651SMatthew G. Knepley     PetscInt          *slocalPoints;
31313982b651SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubpoints = 0, numSubroots, numSubleaves = 0, l, sl, ll, pStart, pEnd, p;
31323982b651SMatthew G. Knepley     PetscMPIInt        rank;
31333982b651SMatthew G. Knepley 
31343982b651SMatthew G. Knepley     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
31353982b651SMatthew G. Knepley     ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
31363982b651SMatthew G. Knepley     ierr = DMGetPointSF(subdm, &sfPointSub);CHKERRQ(ierr);
31373982b651SMatthew G. Knepley     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
31383982b651SMatthew G. Knepley     ierr = DMPlexGetChart(subdm, NULL, &numSubroots);CHKERRQ(ierr);
31393982b651SMatthew G. Knepley     ierr = DMPlexCreateSubpointIS(subdm, &subpIS);CHKERRQ(ierr);
31403982b651SMatthew G. Knepley     if (subpIS) {
31413982b651SMatthew G. Knepley       ierr = ISGetIndices(subpIS, &subpoints);CHKERRQ(ierr);
31423982b651SMatthew G. Knepley       ierr = ISGetLocalSize(subpIS, &numSubpoints);CHKERRQ(ierr);
31433982b651SMatthew G. Knepley     }
31443982b651SMatthew G. Knepley     ierr = PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr);
31453982b651SMatthew G. Knepley     if (numRoots >= 0) {
31463982b651SMatthew G. Knepley       ierr = PetscMalloc2(pEnd-pStart,&newLocalPoints,numRoots,&newOwners);CHKERRQ(ierr);
31473982b651SMatthew G. Knepley       for (p = 0; p < pEnd-pStart; ++p) {
31483982b651SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
31493982b651SMatthew G. Knepley         newLocalPoints[p].index = -2;
31503982b651SMatthew G. Knepley       }
31513982b651SMatthew G. Knepley       /* Set subleaves */
31523982b651SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
31533982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
31543982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(point, 0, numSubpoints, subpoints);
31553982b651SMatthew G. Knepley 
31563982b651SMatthew G. Knepley         if (subpoint < 0) continue;
31573982b651SMatthew G. Knepley         newLocalPoints[point-pStart].rank  = rank;
31583982b651SMatthew G. Knepley         newLocalPoints[point-pStart].index = subpoint;
31593982b651SMatthew G. Knepley         ++numSubleaves;
31603982b651SMatthew G. Knepley       }
31613982b651SMatthew G. Knepley       /* Must put in owned subpoints */
31623982b651SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
31633982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(p, 0, numSubpoints, subpoints);
31643982b651SMatthew G. Knepley 
31653982b651SMatthew G. Knepley         if (subpoint < 0) {
31663982b651SMatthew G. Knepley           newOwners[p-pStart].rank  = -3;
31673982b651SMatthew G. Knepley           newOwners[p-pStart].index = -3;
31683982b651SMatthew G. Knepley         } else {
31693982b651SMatthew G. Knepley           newOwners[p-pStart].rank  = rank;
31703982b651SMatthew G. Knepley           newOwners[p-pStart].index = subpoint;
31713982b651SMatthew G. Knepley         }
31723982b651SMatthew G. Knepley       }
31733982b651SMatthew G. Knepley       ierr = PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
31743982b651SMatthew G. Knepley       ierr = PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
31753982b651SMatthew G. Knepley       ierr = PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr);
31763982b651SMatthew G. Knepley       ierr = PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr);
31773982b651SMatthew G. Knepley       ierr = PetscMalloc1(numSubleaves, &slocalPoints);CHKERRQ(ierr);
31783982b651SMatthew G. Knepley       ierr = PetscMalloc1(numSubleaves, &sremotePoints);CHKERRQ(ierr);
31793982b651SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
31803982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
31813982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(point, 0, numSubpoints, subpoints);
31823982b651SMatthew G. Knepley 
31833982b651SMatthew G. Knepley         if (subpoint < 0) continue;
31843982b651SMatthew G. Knepley         if (newLocalPoints[point].rank == rank) {++ll; continue;}
31853982b651SMatthew G. Knepley         slocalPoints[sl]        = subpoint;
31863982b651SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
31873982b651SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
31883982b651SMatthew G. Knepley         if (sremotePoints[sl].rank  < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %d", point);
31893982b651SMatthew G. Knepley         if (sremotePoints[sl].index < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %d", point);
31903982b651SMatthew G. Knepley         ++sl;
31913982b651SMatthew G. Knepley       }
31923982b651SMatthew G. Knepley       if (sl + ll != numSubleaves) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %d + %d != %d", sl, ll, numSubleaves);
31933982b651SMatthew G. Knepley       ierr = PetscFree2(newLocalPoints,newOwners);CHKERRQ(ierr);
31943982b651SMatthew G. Knepley       ierr = PetscSFSetGraph(sfPointSub, numSubroots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
31953982b651SMatthew G. Knepley     }
31963982b651SMatthew G. Knepley     if (subpIS) {
31973982b651SMatthew G. Knepley       ierr = ISRestoreIndices(subpIS, &subpoints);CHKERRQ(ierr);
31983982b651SMatthew G. Knepley       ierr = ISDestroy(&subpIS);CHKERRQ(ierr);
31993982b651SMatthew G. Knepley     }
32003982b651SMatthew G. Knepley   }
3201efa14ee0SMatthew G Knepley   /* Cleanup */
3202e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
32030ae02aa4SMatthew G. Knepley     if (subpointIS[d]) {ierr = ISRestoreIndices(subpointIS[d], &subpoints[d]);CHKERRQ(ierr);}
3204e6ccafaeSMatthew G Knepley     ierr = ISDestroy(&subpointIS[d]);CHKERRQ(ierr);
3205e6ccafaeSMatthew G Knepley   }
3206efa14ee0SMatthew G Knepley   ierr = PetscFree4(numSubPoints,firstSubPoint,subpointIS,subpoints);CHKERRQ(ierr);
3207e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3208e6ccafaeSMatthew G Knepley }
3209e6ccafaeSMatthew G Knepley 
3210158acfadSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM subdm)
32113982b651SMatthew G. Knepley {
32123982b651SMatthew G. Knepley   PetscErrorCode ierr;
32133982b651SMatthew G. Knepley 
32143982b651SMatthew G. Knepley   PetscFunctionBegin;
3215158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmeshGeneric_Interpolated(dm, vertexLabel, value, markedFaces, PETSC_FALSE, 1, subdm);CHKERRQ(ierr);
32163982b651SMatthew G. Knepley   PetscFunctionReturn(0);
32173982b651SMatthew G. Knepley }
32183982b651SMatthew G. Knepley 
3219d0fa310fSMatthew G. Knepley /*@
3220e6ccafaeSMatthew G Knepley   DMPlexCreateSubmesh - Extract a hypersurface from the mesh using vertices defined by a label
3221e6ccafaeSMatthew G Knepley 
3222e6ccafaeSMatthew G Knepley   Input Parameters:
3223e6ccafaeSMatthew G Knepley + dm           - The original mesh
3224158acfadSMatthew G. Knepley . vertexLabel  - The DMLabel marking points contained in the surface
3225158acfadSMatthew G. Knepley . value        - The label value to use
3226158acfadSMatthew G. Knepley - markedFaces  - PETSC_TRUE if surface faces are marked in addition to vertices, PETSC_FALSE if only vertices are marked
3227e6ccafaeSMatthew G Knepley 
3228e6ccafaeSMatthew G Knepley   Output Parameter:
3229e6ccafaeSMatthew G Knepley . subdm - The surface mesh
3230e6ccafaeSMatthew G Knepley 
3231e6ccafaeSMatthew G Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3232e6ccafaeSMatthew G Knepley 
3233e6ccafaeSMatthew G Knepley   Level: developer
3234e6ccafaeSMatthew G Knepley 
3235c58f1c22SToby Isaac .seealso: DMPlexGetSubpointMap(), DMGetLabel(), DMLabelSetValue()
3236830e53efSMatthew G. Knepley @*/
3237158acfadSMatthew G. Knepley PetscErrorCode DMPlexCreateSubmesh(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM *subdm)
3238e6ccafaeSMatthew G Knepley {
3239dd2763adSMatthew G. Knepley   PetscInt       dim, cdim, depth;
3240e6ccafaeSMatthew G Knepley   PetscErrorCode ierr;
3241e6ccafaeSMatthew G Knepley 
3242e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3243e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3244766ab985SMatthew G. Knepley   PetscValidPointer(subdm, 3);
3245c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3246e6ccafaeSMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
324782f516ccSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)dm), subdm);CHKERRQ(ierr);
3248e6ccafaeSMatthew G Knepley   ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr);
3249c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(*subdm, dim-1);CHKERRQ(ierr);
3250dd2763adSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3251dd2763adSMatthew G. Knepley   ierr = DMSetCoordinateDim(*subdm, cdim);CHKERRQ(ierr);
3252e6ccafaeSMatthew G Knepley   if (depth == dim) {
3253158acfadSMatthew G. Knepley     ierr = DMPlexCreateSubmesh_Interpolated(dm, vertexLabel, value, markedFaces, *subdm);CHKERRQ(ierr);
3254e6ccafaeSMatthew G Knepley   } else {
3255830e53efSMatthew G. Knepley     ierr = DMPlexCreateSubmesh_Uninterpolated(dm, vertexLabel, value, *subdm);CHKERRQ(ierr);
3256e6ccafaeSMatthew G Knepley   }
3257e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3258e6ccafaeSMatthew G Knepley }
3259e6ccafaeSMatthew G Knepley 
326027c04023SMatthew G. Knepley static PetscErrorCode DMPlexCreateCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM subdm)
3261766ab985SMatthew G. Knepley {
3262766ab985SMatthew G. Knepley   MPI_Comm        comm;
3263766ab985SMatthew G. Knepley   DMLabel         subpointMap;
3264766ab985SMatthew G. Knepley   IS              subvertexIS;
3265766ab985SMatthew G. Knepley   const PetscInt *subVertices;
3266fed694aaSMatthew G. Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells, *subCells = NULL;
3267766ab985SMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces, firstSubFace, newFacePoint, nFV;
326887feddfdSMatthew G. Knepley   PetscInt        cMax, c, f;
3269766ab985SMatthew G. Knepley   PetscErrorCode  ierr;
3270766ab985SMatthew G. Knepley 
3271766ab985SMatthew G. Knepley   PetscFunctionBegin;
3272766ab985SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
3273766ab985SMatthew G. Knepley   /* Create subpointMap which marks the submesh */
3274d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap);CHKERRQ(ierr);
3275766ab985SMatthew G. Knepley   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
3276766ab985SMatthew G. Knepley   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
327727c04023SMatthew G. Knepley   ierr = DMPlexMarkCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, subpointMap, &numSubFaces, &nFV, &subCells, subdm);CHKERRQ(ierr);
3278766ab985SMatthew G. Knepley   /* Setup chart */
3279766ab985SMatthew G. Knepley   ierr = DMLabelGetStratumSize(subpointMap, 0, &numSubVertices);CHKERRQ(ierr);
3280766ab985SMatthew G. Knepley   ierr = DMLabelGetStratumSize(subpointMap, 2, &numSubCells);CHKERRQ(ierr);
3281766ab985SMatthew G. Knepley   ierr = DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices);CHKERRQ(ierr);
3282766ab985SMatthew G. Knepley   ierr = DMPlexSetVTKCellHeight(subdm, 1);CHKERRQ(ierr);
3283766ab985SMatthew G. Knepley   /* Set cone sizes */
3284766ab985SMatthew G. Knepley   firstSubVertex = numSubCells;
3285766ab985SMatthew G. Knepley   firstSubFace   = numSubCells+numSubVertices;
3286766ab985SMatthew G. Knepley   newFacePoint   = firstSubFace;
3287766ab985SMatthew G. Knepley   ierr = DMLabelGetStratumIS(subpointMap, 0, &subvertexIS);CHKERRQ(ierr);
3288766ab985SMatthew G. Knepley   if (subvertexIS) {ierr = ISGetIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
3289766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3290766ab985SMatthew G. Knepley     ierr = DMPlexSetConeSize(subdm, c, 1);CHKERRQ(ierr);
3291766ab985SMatthew G. Knepley   }
3292766ab985SMatthew G. Knepley   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
3293766ab985SMatthew G. Knepley     ierr = DMPlexSetConeSize(subdm, f, nFV);CHKERRQ(ierr);
3294766ab985SMatthew G. Knepley   }
3295766ab985SMatthew G. Knepley   ierr = DMSetUp(subdm);CHKERRQ(ierr);
3296766ab985SMatthew G. Knepley   /* Create face cones */
3297766ab985SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
329887feddfdSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
329969291d52SBarry Smith   ierr = DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
3300766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3301766ab985SMatthew G. Knepley     const PetscInt  cell    = subCells[c];
3302766ab985SMatthew G. Knepley     const PetscInt  subcell = c;
330387feddfdSMatthew G. Knepley     const PetscInt *cone, *cells;
330487feddfdSMatthew G. Knepley     PetscInt        numCells, subVertex, p, v;
3305766ab985SMatthew G. Knepley 
330687feddfdSMatthew G. Knepley     if (cell < cMax) continue;
330787feddfdSMatthew G. Knepley     ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
330887feddfdSMatthew G. Knepley     for (v = 0; v < nFV; ++v) {
330987feddfdSMatthew G. Knepley       ierr = PetscFindInt(cone[v], numSubVertices, subVertices, &subVertex);CHKERRQ(ierr);
331087feddfdSMatthew G. Knepley       subface[v] = firstSubVertex+subVertex;
331187feddfdSMatthew G. Knepley     }
331287feddfdSMatthew G. Knepley     ierr = DMPlexSetCone(subdm, newFacePoint, subface);CHKERRQ(ierr);
331387feddfdSMatthew G. Knepley     ierr = DMPlexSetCone(subdm, subcell, &newFacePoint);CHKERRQ(ierr);
331487feddfdSMatthew G. Knepley     ierr = DMPlexGetJoin(dm, nFV, cone, &numCells, &cells);CHKERRQ(ierr);
331527234c99SMatthew G. Knepley     /* Not true in parallel
331627234c99SMatthew G. Knepley     if (numCells != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
331787feddfdSMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
331887feddfdSMatthew G. Knepley       PetscInt negsubcell;
3319766ab985SMatthew G. Knepley 
332087feddfdSMatthew G. Knepley       if (cells[p] >= cMax) continue;
332187feddfdSMatthew G. Knepley       /* I know this is a crap search */
332287feddfdSMatthew G. Knepley       for (negsubcell = 0; negsubcell < numSubCells; ++negsubcell) {
332387feddfdSMatthew G. Knepley         if (subCells[negsubcell] == cells[p]) break;
3324766ab985SMatthew G. Knepley       }
332587feddfdSMatthew G. Knepley       if (negsubcell == numSubCells) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find negative face neighbor for cohesive cell %d", cell);
332687feddfdSMatthew G. Knepley       ierr = DMPlexSetCone(subdm, negsubcell, &newFacePoint);CHKERRQ(ierr);
3327766ab985SMatthew G. Knepley     }
332887feddfdSMatthew G. Knepley     ierr = DMPlexRestoreJoin(dm, nFV, cone, &numCells, &cells);CHKERRQ(ierr);
332987feddfdSMatthew G. Knepley     ++newFacePoint;
3330766ab985SMatthew G. Knepley   }
333169291d52SBarry Smith   ierr = DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
3332766ab985SMatthew G. Knepley   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
3333766ab985SMatthew G. Knepley   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
3334766ab985SMatthew G. Knepley   /* Build coordinates */
3335766ab985SMatthew G. Knepley   {
3336766ab985SMatthew G. Knepley     PetscSection coordSection, subCoordSection;
3337766ab985SMatthew G. Knepley     Vec          coordinates, subCoordinates;
3338766ab985SMatthew G. Knepley     PetscScalar *coords, *subCoords;
3339c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize, v;
334024640c55SToby Isaac     const char  *name;
3341766ab985SMatthew G. Knepley 
3342c0e8cf5fSMatthew G. Knepley     ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
334369d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
3344766ab985SMatthew G. Knepley     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
334569d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
3346285d324eSMatthew G. Knepley     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
3347285d324eSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
3348285d324eSMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
3349766ab985SMatthew G. Knepley     ierr = PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices);CHKERRQ(ierr);
3350766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3351766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3352766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex+v;
3353766ab985SMatthew G. Knepley       PetscInt       dof;
3354766ab985SMatthew G. Knepley 
3355766ab985SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3356766ab985SMatthew G. Knepley       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
3357285d324eSMatthew G. Knepley       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
3358766ab985SMatthew G. Knepley     }
3359766ab985SMatthew G. Knepley     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
3360766ab985SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
33618b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &subCoordinates);CHKERRQ(ierr);
336224640c55SToby Isaac     ierr = PetscObjectGetName((PetscObject)coordinates,&name);CHKERRQ(ierr);
336324640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)subCoordinates,name);CHKERRQ(ierr);
3364766ab985SMatthew G. Knepley     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
3365c0e8cf5fSMatthew G. Knepley     ierr = VecSetBlockSize(subCoordinates, cdim);CHKERRQ(ierr);
33662eb5907fSJed Brown     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
3367766ab985SMatthew G. Knepley     ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
3368766ab985SMatthew G. Knepley     ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3369766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3370766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3371766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex+v;
3372766ab985SMatthew G. Knepley       PetscInt       dof, off, sdof, soff, d;
3373766ab985SMatthew G. Knepley 
3374766ab985SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3375766ab985SMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
3376766ab985SMatthew G. Knepley       ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
3377766ab985SMatthew G. Knepley       ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
3378766ab985SMatthew G. Knepley       if (dof != sdof) SETERRQ4(comm, PETSC_ERR_PLIB, "Coordinate dimension %d on subvertex %d, vertex %d should be %d", sdof, subvertex, vertex, dof);
3379766ab985SMatthew G. Knepley       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3380766ab985SMatthew G. Knepley     }
3381766ab985SMatthew G. Knepley     ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
3382766ab985SMatthew G. Knepley     ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3383766ab985SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
3384766ab985SMatthew G. Knepley     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
3385766ab985SMatthew G. Knepley   }
3386aca35d17SMatthew G. Knepley   /* Build SF */
3387aca35d17SMatthew G. Knepley   CHKMEMQ;
3388aca35d17SMatthew G. Knepley   {
3389aca35d17SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
3390aca35d17SMatthew G. Knepley     const PetscSFNode *remotePoints;
3391bdcf2095SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
3392aca35d17SMatthew G. Knepley     const PetscInt    *localPoints;
3393bdcf2095SMatthew G. Knepley     PetscInt          *slocalPoints;
339449c26ae4SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubRoots = numSubCells+numSubFaces+numSubVertices, numSubLeaves = 0, l, sl, ll, pStart, pEnd, p, vStart, vEnd;
3395bdcf2095SMatthew G. Knepley     PetscMPIInt        rank;
3396aca35d17SMatthew G. Knepley 
3397bdcf2095SMatthew G. Knepley     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
3398aca35d17SMatthew G. Knepley     ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
3399aca35d17SMatthew G. Knepley     ierr = DMGetPointSF(subdm, &sfPointSub);CHKERRQ(ierr);
3400aca35d17SMatthew G. Knepley     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3401aca35d17SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
3402aca35d17SMatthew G. Knepley     ierr = PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr);
3403aca35d17SMatthew G. Knepley     if (numRoots >= 0) {
3404aca35d17SMatthew G. Knepley       /* Only vertices should be shared */
3405dcca6d9dSJed Brown       ierr = PetscMalloc2(pEnd-pStart,&newLocalPoints,numRoots,&newOwners);CHKERRQ(ierr);
3406bdcf2095SMatthew G. Knepley       for (p = 0; p < pEnd-pStart; ++p) {
3407bdcf2095SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
3408bdcf2095SMatthew G. Knepley         newLocalPoints[p].index = -2;
3409bdcf2095SMatthew G. Knepley       }
34109e0823b2SMatthew G. Knepley       /* Set subleaves */
3411aca35d17SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
3412aca35d17SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3413bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3414aca35d17SMatthew G. Knepley 
3415aca35d17SMatthew G. Knepley         if ((point < vStart) && (point >= vEnd)) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Should not be mapping anything but vertices, %d", point);
3416bdcf2095SMatthew G. Knepley         if (subPoint < 0) continue;
3417bdcf2095SMatthew G. Knepley         newLocalPoints[point-pStart].rank  = rank;
3418bdcf2095SMatthew G. Knepley         newLocalPoints[point-pStart].index = subPoint;
3419bdcf2095SMatthew G. Knepley         ++numSubLeaves;
3420aca35d17SMatthew G. Knepley       }
34219e0823b2SMatthew G. Knepley       /* Must put in owned subpoints */
34229e0823b2SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
34239e0823b2SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(p, firstSubVertex, numSubVertices, subVertices);
34249e0823b2SMatthew G. Knepley 
34259e0823b2SMatthew G. Knepley         if (subPoint < 0) {
34269e0823b2SMatthew G. Knepley           newOwners[p-pStart].rank  = -3;
34279e0823b2SMatthew G. Knepley           newOwners[p-pStart].index = -3;
34289e0823b2SMatthew G. Knepley         } else {
34299e0823b2SMatthew G. Knepley           newOwners[p-pStart].rank  = rank;
34309e0823b2SMatthew G. Knepley           newOwners[p-pStart].index = subPoint;
34319e0823b2SMatthew G. Knepley         }
3432bdcf2095SMatthew G. Knepley       }
3433bdcf2095SMatthew G. Knepley       ierr = PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
3434bdcf2095SMatthew G. Knepley       ierr = PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
3435bdcf2095SMatthew G. Knepley       ierr = PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr);
3436bdcf2095SMatthew G. Knepley       ierr = PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr);
3437785e854fSJed Brown       ierr = PetscMalloc1(numSubLeaves,    &slocalPoints);CHKERRQ(ierr);
3438785e854fSJed Brown       ierr = PetscMalloc1(numSubLeaves, &sremotePoints);CHKERRQ(ierr);
343949c26ae4SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
3440bdcf2095SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3441bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3442aca35d17SMatthew G. Knepley 
3443aca35d17SMatthew G. Knepley         if (subPoint < 0) continue;
344449c26ae4SMatthew G. Knepley         if (newLocalPoints[point].rank == rank) {++ll; continue;}
3445aca35d17SMatthew G. Knepley         slocalPoints[sl]        = subPoint;
3446bdcf2095SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
3447bdcf2095SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
3448bdcf2095SMatthew G. Knepley         if (sremotePoints[sl].rank  < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %d", point);
3449bdcf2095SMatthew G. Knepley         if (sremotePoints[sl].index < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %d", point);
3450aca35d17SMatthew G. Knepley         ++sl;
3451aca35d17SMatthew G. Knepley       }
3452bdcf2095SMatthew G. Knepley       ierr = PetscFree2(newLocalPoints,newOwners);CHKERRQ(ierr);
345349c26ae4SMatthew G. Knepley       if (sl + ll != numSubLeaves) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %d + %d != %d", sl, ll, numSubLeaves);
345449c26ae4SMatthew G. Knepley       ierr = PetscSFSetGraph(sfPointSub, numSubRoots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
3455aca35d17SMatthew G. Knepley     }
3456aca35d17SMatthew G. Knepley   }
3457aca35d17SMatthew G. Knepley   CHKMEMQ;
3458766ab985SMatthew G. Knepley   /* Cleanup */
3459766ab985SMatthew G. Knepley   if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
3460766ab985SMatthew G. Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
3461766ab985SMatthew G. Knepley   ierr = PetscFree(subCells);CHKERRQ(ierr);
3462766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
3463766ab985SMatthew G. Knepley }
3464766ab985SMatthew G. Knepley 
34653982b651SMatthew G. Knepley static PetscErrorCode DMPlexCreateCohesiveSubmesh_Interpolated(DM dm, const char labelname[], PetscInt value, DM subdm)
3466766ab985SMatthew G. Knepley {
34673982b651SMatthew G. Knepley   DMLabel        label = NULL;
3468766ab985SMatthew G. Knepley   PetscErrorCode ierr;
3469766ab985SMatthew G. Knepley 
3470766ab985SMatthew G. Knepley   PetscFunctionBegin;
3471c58f1c22SToby Isaac   if (labelname) {ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);}
3472158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmeshGeneric_Interpolated(dm, label, value, PETSC_FALSE, PETSC_TRUE, 1, subdm);CHKERRQ(ierr);
3473766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
3474766ab985SMatthew G. Knepley }
3475766ab985SMatthew G. Knepley 
3476c08575a3SMatthew G. Knepley /*@C
347727c04023SMatthew G. Knepley   DMPlexCreateCohesiveSubmesh - Extract from a mesh with cohesive cells the hypersurface defined by one face of the cells. Optionally, a Label an be given to restrict the cells.
3478766ab985SMatthew G. Knepley 
3479766ab985SMatthew G. Knepley   Input Parameters:
3480766ab985SMatthew G. Knepley + dm          - The original mesh
348127c04023SMatthew G. Knepley . hasLagrange - The mesh has Lagrange unknowns in the cohesive cells
34827afc1a8bSJed Brown . label       - A label name, or NULL
348327c04023SMatthew G. Knepley - value  - A label value
3484766ab985SMatthew G. Knepley 
3485766ab985SMatthew G. Knepley   Output Parameter:
3486766ab985SMatthew G. Knepley . subdm - The surface mesh
3487766ab985SMatthew G. Knepley 
3488766ab985SMatthew G. Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3489766ab985SMatthew G. Knepley 
3490766ab985SMatthew G. Knepley   Level: developer
3491766ab985SMatthew G. Knepley 
3492766ab985SMatthew G. Knepley .seealso: DMPlexGetSubpointMap(), DMPlexCreateSubmesh()
3493c08575a3SMatthew G. Knepley @*/
349427c04023SMatthew G. Knepley PetscErrorCode DMPlexCreateCohesiveSubmesh(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM *subdm)
3495766ab985SMatthew G. Knepley {
3496c0e8cf5fSMatthew G. Knepley   PetscInt       dim, cdim, depth;
3497766ab985SMatthew G. Knepley   PetscErrorCode ierr;
3498766ab985SMatthew G. Knepley 
3499766ab985SMatthew G. Knepley   PetscFunctionBegin;
3500766ab985SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
350127c04023SMatthew G. Knepley   PetscValidPointer(subdm, 5);
3502c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3503766ab985SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3504766ab985SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), subdm);CHKERRQ(ierr);
3505766ab985SMatthew G. Knepley   ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr);
3506c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(*subdm, dim-1);CHKERRQ(ierr);
3507c0e8cf5fSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3508c0e8cf5fSMatthew G. Knepley   ierr = DMSetCoordinateDim(*subdm, cdim);CHKERRQ(ierr);
3509766ab985SMatthew G. Knepley   if (depth == dim) {
3510b3154360SMatthew G. Knepley     ierr = DMPlexCreateCohesiveSubmesh_Interpolated(dm, label, value, *subdm);CHKERRQ(ierr);
3511766ab985SMatthew G. Knepley   } else {
351227c04023SMatthew G. Knepley     ierr = DMPlexCreateCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, *subdm);CHKERRQ(ierr);
3513e6ccafaeSMatthew G Knepley   }
3514e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3515e6ccafaeSMatthew G Knepley }
3516e6ccafaeSMatthew G Knepley 
3517bec263e5SMatthew G. Knepley /*@
3518bec263e5SMatthew G. Knepley   DMPlexFilter - Extract a subset of mesh cells defined by a label as a separate mesh
3519bec263e5SMatthew G. Knepley 
3520bec263e5SMatthew G. Knepley   Input Parameters:
3521bec263e5SMatthew G. Knepley + dm        - The original mesh
3522bec263e5SMatthew G. Knepley . cellLabel - The DMLabel marking cells contained in the new mesh
3523bec263e5SMatthew G. Knepley - value     - The label value to use
3524bec263e5SMatthew G. Knepley 
3525bec263e5SMatthew G. Knepley   Output Parameter:
3526bec263e5SMatthew G. Knepley . subdm - The new mesh
3527bec263e5SMatthew G. Knepley 
3528bec263e5SMatthew G. Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3529bec263e5SMatthew G. Knepley 
3530bec263e5SMatthew G. Knepley   Level: developer
3531bec263e5SMatthew G. Knepley 
3532c58f1c22SToby Isaac .seealso: DMPlexGetSubpointMap(), DMGetLabel(), DMLabelSetValue()
3533bec263e5SMatthew G. Knepley @*/
3534bec263e5SMatthew G. Knepley PetscErrorCode DMPlexFilter(DM dm, DMLabel cellLabel, PetscInt value, DM *subdm)
3535bec263e5SMatthew G. Knepley {
3536bec263e5SMatthew G. Knepley   PetscInt       dim;
3537bec263e5SMatthew G. Knepley   PetscErrorCode ierr;
3538bec263e5SMatthew G. Knepley 
3539bec263e5SMatthew G. Knepley   PetscFunctionBegin;
3540bec263e5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3541bec263e5SMatthew G. Knepley   PetscValidPointer(subdm, 3);
3542bec263e5SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3543bec263e5SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), subdm);CHKERRQ(ierr);
3544bec263e5SMatthew G. Knepley   ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr);
3545bec263e5SMatthew G. Knepley   ierr = DMSetDimension(*subdm, dim);CHKERRQ(ierr);
3546bec263e5SMatthew G. Knepley   /* Extract submesh in place, could be empty on some procs, could have inconsistency if procs do not both extract a shared cell */
3547158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmeshGeneric_Interpolated(dm, cellLabel, value, PETSC_FALSE, PETSC_FALSE, 0, *subdm);CHKERRQ(ierr);
3548bec263e5SMatthew G. Knepley   PetscFunctionReturn(0);
3549bec263e5SMatthew G. Knepley }
3550bec263e5SMatthew G. Knepley 
355164beef6dSMatthew G. Knepley /*@
355264beef6dSMatthew G. Knepley   DMPlexGetSubpointMap - Returns a DMLabel with point dimension as values
355364beef6dSMatthew G. Knepley 
355464beef6dSMatthew G. Knepley   Input Parameter:
355564beef6dSMatthew G. Knepley . dm - The submesh DM
355664beef6dSMatthew G. Knepley 
355764beef6dSMatthew G. Knepley   Output Parameter:
355864beef6dSMatthew G. Knepley . subpointMap - The DMLabel of all the points from the original mesh in this submesh, or NULL if this is not a submesh
355964beef6dSMatthew G. Knepley 
356064beef6dSMatthew G. Knepley   Level: developer
356164beef6dSMatthew G. Knepley 
356264beef6dSMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexCreateSubpointIS()
356364beef6dSMatthew G. Knepley @*/
3564e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexGetSubpointMap(DM dm, DMLabel *subpointMap)
3565e6ccafaeSMatthew G Knepley {
3566e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3567e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3568e6ccafaeSMatthew G Knepley   PetscValidPointer(subpointMap, 2);
356965663942SMatthew G. Knepley   *subpointMap = ((DM_Plex*) dm->data)->subpointMap;
3570e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3571e6ccafaeSMatthew G Knepley }
3572e6ccafaeSMatthew G Knepley 
3573c08575a3SMatthew G. Knepley /*@
3574c08575a3SMatthew G. Knepley   DMPlexSetSubpointMap - Sets the DMLabel with point dimension as values
3575c08575a3SMatthew G. Knepley 
3576c08575a3SMatthew G. Knepley   Input Parameters:
3577c08575a3SMatthew G. Knepley + dm - The submesh DM
3578c08575a3SMatthew G. Knepley - subpointMap - The DMLabel of all the points from the original mesh in this submesh
3579c08575a3SMatthew G. Knepley 
3580c08575a3SMatthew G. Knepley   Note: Should normally not be called by the user, since it is set in DMPlexCreateSubmesh()
3581c08575a3SMatthew G. Knepley 
3582c08575a3SMatthew G. Knepley   Level: developer
3583c08575a3SMatthew G. Knepley 
3584c08575a3SMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexCreateSubpointIS()
3585c08575a3SMatthew G. Knepley @*/
3586e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexSetSubpointMap(DM dm, DMLabel subpointMap)
3587e6ccafaeSMatthew G Knepley {
3588e6ccafaeSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
3589285d324eSMatthew G. Knepley   DMLabel        tmp;
3590e6ccafaeSMatthew G Knepley   PetscErrorCode ierr;
3591e6ccafaeSMatthew G Knepley 
3592e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3593e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3594285d324eSMatthew G. Knepley   tmp  = mesh->subpointMap;
3595e6ccafaeSMatthew G Knepley   mesh->subpointMap = subpointMap;
3596d67d17b1SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) mesh->subpointMap);CHKERRQ(ierr);
3597285d324eSMatthew G. Knepley   ierr = DMLabelDestroy(&tmp);CHKERRQ(ierr);
3598e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3599e6ccafaeSMatthew G Knepley }
3600e6ccafaeSMatthew G Knepley 
360164beef6dSMatthew G. Knepley /*@
3602e6ccafaeSMatthew G Knepley   DMPlexCreateSubpointIS - Creates an IS covering the entire subdm chart with the original points as data
3603e6ccafaeSMatthew G Knepley 
3604e6ccafaeSMatthew G Knepley   Input Parameter:
3605e6ccafaeSMatthew G Knepley . dm - The submesh DM
3606e6ccafaeSMatthew G Knepley 
3607e6ccafaeSMatthew G Knepley   Output Parameter:
36080298fd71SBarry Smith . subpointIS - The IS of all the points from the original mesh in this submesh, or NULL if this is not a submesh
3609e6ccafaeSMatthew G Knepley 
36103982b651SMatthew G. Knepley   Note: This IS is guaranteed to be sorted by the construction of the submesh
361164beef6dSMatthew G. Knepley 
361264beef6dSMatthew G. Knepley   Level: developer
361364beef6dSMatthew G. Knepley 
361464beef6dSMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointMap()
361564beef6dSMatthew G. Knepley @*/
3616e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexCreateSubpointIS(DM dm, IS *subpointIS)
3617e6ccafaeSMatthew G Knepley {
361882f516ccSBarry Smith   MPI_Comm        comm;
3619e6ccafaeSMatthew G Knepley   DMLabel         subpointMap;
3620e6ccafaeSMatthew G Knepley   IS              is;
3621e6ccafaeSMatthew G Knepley   const PetscInt *opoints;
3622e6ccafaeSMatthew G Knepley   PetscInt       *points, *depths;
3623e6ccafaeSMatthew G Knepley   PetscInt        depth, depStart, depEnd, d, pStart, pEnd, p, n, off;
3624e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
3625e6ccafaeSMatthew G Knepley 
3626e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3627e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3628e6ccafaeSMatthew G Knepley   PetscValidPointer(subpointIS, 2);
362982f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
36300298fd71SBarry Smith   *subpointIS = NULL;
3631e6ccafaeSMatthew G Knepley   ierr = DMPlexGetSubpointMap(dm, &subpointMap);CHKERRQ(ierr);
3632e6ccafaeSMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3633fed694aaSMatthew G. Knepley   if (subpointMap && depth >= 0) {
3634e6ccafaeSMatthew G Knepley     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3635e6ccafaeSMatthew G Knepley     if (pStart) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Submeshes must start the point numbering at 0, not %d", pStart);
363669291d52SBarry Smith     ierr = DMGetWorkArray(dm, depth+1, MPIU_INT, &depths);CHKERRQ(ierr);
3637e6ccafaeSMatthew G Knepley     depths[0] = depth;
3638e6ccafaeSMatthew G Knepley     depths[1] = 0;
3639e6ccafaeSMatthew G Knepley     for(d = 2; d <= depth; ++d) {depths[d] = depth+1 - d;}
3640785e854fSJed Brown     ierr = PetscMalloc1(pEnd, &points);CHKERRQ(ierr);
3641e6ccafaeSMatthew G Knepley     for(d = 0, off = 0; d <= depth; ++d) {
3642e6ccafaeSMatthew G Knepley       const PetscInt dep = depths[d];
3643e6ccafaeSMatthew G Knepley 
3644e6ccafaeSMatthew G Knepley       ierr = DMPlexGetDepthStratum(dm, dep, &depStart, &depEnd);CHKERRQ(ierr);
3645e6ccafaeSMatthew G Knepley       ierr = DMLabelGetStratumSize(subpointMap, dep, &n);CHKERRQ(ierr);
36461e21b6fdSMatthew G Knepley       if (((d < 2) && (depth > 1)) || (d == 1)) { /* Only check vertices and cells for now since the map is broken for others */
3647e6ccafaeSMatthew G Knepley         if (n != depEnd-depStart) SETERRQ3(comm, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %d at depth %d should be %d", n, dep, depEnd-depStart);
3648e6ccafaeSMatthew G Knepley       } else {
36491e21b6fdSMatthew G Knepley         if (!n) {
36501e21b6fdSMatthew G Knepley           if (d == 0) {
36511e21b6fdSMatthew G Knepley             /* Missing cells */
36521e21b6fdSMatthew G Knepley             for(p = 0; p < depEnd-depStart; ++p, ++off) points[off] = -1;
36531e21b6fdSMatthew G Knepley           } else {
36541e21b6fdSMatthew G Knepley             /* Missing faces */
36551e21b6fdSMatthew G Knepley             for(p = 0; p < depEnd-depStart; ++p, ++off) points[off] = PETSC_MAX_INT;
36561e21b6fdSMatthew G Knepley           }
36571e21b6fdSMatthew G Knepley         }
3658e6ccafaeSMatthew G Knepley       }
3659e6ccafaeSMatthew G Knepley       if (n) {
3660e6ccafaeSMatthew G Knepley         ierr = DMLabelGetStratumIS(subpointMap, dep, &is);CHKERRQ(ierr);
3661e6ccafaeSMatthew G Knepley         ierr = ISGetIndices(is, &opoints);CHKERRQ(ierr);
3662e6ccafaeSMatthew G Knepley         for(p = 0; p < n; ++p, ++off) points[off] = opoints[p];
3663e6ccafaeSMatthew G Knepley         ierr = ISRestoreIndices(is, &opoints);CHKERRQ(ierr);
3664e6ccafaeSMatthew G Knepley         ierr = ISDestroy(&is);CHKERRQ(ierr);
3665e6ccafaeSMatthew G Knepley       }
3666e6ccafaeSMatthew G Knepley     }
366769291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, depth+1, MPIU_INT, &depths);CHKERRQ(ierr);
3668e6ccafaeSMatthew G Knepley     if (off != pEnd) SETERRQ2(comm, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %d should be %d", off, pEnd);
3669fed694aaSMatthew G. Knepley     ierr = ISCreateGeneral(PETSC_COMM_SELF, pEnd, points, PETSC_OWN_POINTER, subpointIS);CHKERRQ(ierr);
3670e6ccafaeSMatthew G Knepley   }
3671e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3672e6ccafaeSMatthew G Knepley }
3673559a1558SMatthew G. Knepley 
3674559a1558SMatthew G. Knepley /*@
3675559a1558SMatthew G. Knepley   DMPlexGetSubpoint - Return the subpoint corresponding to a point in the original mesh. If the DM
3676559a1558SMatthew G. Knepley                       is not a submesh, just return the input point.
3677559a1558SMatthew G. Knepley 
3678559a1558SMatthew G. Knepley   Note collective
3679559a1558SMatthew G. Knepley 
3680559a1558SMatthew G. Knepley   Input Parameters:
3681559a1558SMatthew G. Knepley + dm - The submesh DM
3682559a1558SMatthew G. Knepley - p  - The point in the original, from which the submesh was created
3683559a1558SMatthew G. Knepley 
3684559a1558SMatthew G. Knepley   Output Parameter:
3685559a1558SMatthew G. Knepley . subp - The point in the submesh
3686559a1558SMatthew G. Knepley 
3687559a1558SMatthew G. Knepley   Level: developer
3688559a1558SMatthew G. Knepley 
3689559a1558SMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointMap(), DMPlexCreateSubpointIS()
3690559a1558SMatthew G. Knepley @*/
3691559a1558SMatthew G. Knepley PetscErrorCode DMPlexGetSubpoint(DM dm, PetscInt p, PetscInt *subp)
3692559a1558SMatthew G. Knepley {
3693559a1558SMatthew G. Knepley   DMLabel        spmap;
3694559a1558SMatthew G. Knepley   PetscErrorCode ierr;
3695559a1558SMatthew G. Knepley 
369644171101SMatthew G. Knepley   PetscFunctionBegin;
3697559a1558SMatthew G. Knepley   *subp = p;
3698559a1558SMatthew G. Knepley   ierr = DMPlexGetSubpointMap(dm, &spmap);CHKERRQ(ierr);
3699559a1558SMatthew G. Knepley   if (spmap) {
3700559a1558SMatthew G. Knepley     IS              subpointIS;
3701559a1558SMatthew G. Knepley     const PetscInt *subpoints;
3702559a1558SMatthew G. Knepley     PetscInt        numSubpoints;
3703559a1558SMatthew G. Knepley 
3704559a1558SMatthew G. Knepley     /* TODO Cache the IS, making it look like an index */
3705559a1558SMatthew G. Knepley     ierr = DMPlexCreateSubpointIS(dm, &subpointIS);CHKERRQ(ierr);
3706559a1558SMatthew G. Knepley     ierr = ISGetLocalSize(subpointIS, &numSubpoints);CHKERRQ(ierr);
3707559a1558SMatthew G. Knepley     ierr = ISGetIndices(subpointIS, &subpoints);CHKERRQ(ierr);
3708559a1558SMatthew G. Knepley     ierr = PetscFindInt(p, numSubpoints, subpoints, subp);CHKERRQ(ierr);
3709559a1558SMatthew G. Knepley     if (*subp < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %d not found in submesh", p);
3710559a1558SMatthew G. Knepley     ierr = ISRestoreIndices(subpointIS, &subpoints);CHKERRQ(ierr);
3711559a1558SMatthew G. Knepley     ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
3712559a1558SMatthew G. Knepley   }
3713559a1558SMatthew G. Knepley   PetscFunctionReturn(0);
3714559a1558SMatthew G. Knepley }
371544171101SMatthew G. Knepley 
371644171101SMatthew G. Knepley /*@
371744171101SMatthew G. Knepley   DMPlexGetAuxiliaryPoint - For a given point in the DM, return the matching point in the auxiliary DM.
371844171101SMatthew G. Knepley 
371944171101SMatthew G. Knepley   Note collective
372044171101SMatthew G. Knepley 
372144171101SMatthew G. Knepley   Input Parameters:
372244171101SMatthew G. Knepley + dm    - The DM
372344171101SMatthew G. Knepley . dmAux - The related auxiliary DM
372444171101SMatthew G. Knepley - p     - The point in the original DM
372544171101SMatthew G. Knepley 
372644171101SMatthew G. Knepley   Output Parameter:
372744171101SMatthew G. Knepley . subp - The point in the auxiliary DM
372844171101SMatthew G. Knepley 
372944171101SMatthew G. Knepley   Notes: If the DM is a submesh, we assume the dmAux is as well and just return the point. If only dmAux is a submesh,
373044171101SMatthew G. Knepley   then we map the point back to the original space.
373144171101SMatthew G. Knepley 
373244171101SMatthew G. Knepley   Level: developer
373344171101SMatthew G. Knepley 
373444171101SMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointMap(), DMPlexCreateSubpointIS()
373544171101SMatthew G. Knepley @*/
373644171101SMatthew G. Knepley PetscErrorCode DMPlexGetAuxiliaryPoint(DM dm, DM dmAux, PetscInt p, PetscInt *subp)
373744171101SMatthew G. Knepley {
373844171101SMatthew G. Knepley   DMLabel        spmap;
373944171101SMatthew G. Knepley   PetscErrorCode ierr;
374044171101SMatthew G. Knepley 
374144171101SMatthew G. Knepley   PetscFunctionBegin;
374244171101SMatthew G. Knepley   *subp = p;
374344171101SMatthew G. Knepley   /* If dm is a submesh, do not get subpoint */
374444171101SMatthew G. Knepley   ierr = DMPlexGetSubpointMap(dm, &spmap);CHKERRQ(ierr);
374544171101SMatthew G. Knepley   if (dmAux && !spmap) {
374644171101SMatthew G. Knepley     PetscInt h;
374744171101SMatthew G. Knepley 
374844171101SMatthew G. Knepley     ierr = DMPlexGetVTKCellHeight(dmAux, &h);CHKERRQ(ierr);
374944171101SMatthew G. Knepley     ierr = DMPlexGetSubpointMap(dmAux, &spmap);CHKERRQ(ierr);
375044171101SMatthew G. Knepley     if (spmap && !h) {ierr = DMLabelGetValue(spmap, p, subp);CHKERRQ(ierr);}
375144171101SMatthew G. Knepley     else             {ierr = DMPlexGetSubpoint(dmAux, p, subp);CHKERRQ(ierr);}
375244171101SMatthew G. Knepley   }
375344171101SMatthew G. Knepley   PetscFunctionReturn(0);
375444171101SMatthew G. Knepley }
3755