xref: /petsc/src/dm/impls/plex/plexsubmesh.c (revision b0441da4f7532a7d5ee49e4c9b30a09111112088)
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;
19122eabd52SMatthew G. Knepley   PetscInt        numValues, v, cStart, cEnd, cEndInterior;
1926cf0e42fSMatthew G. Knepley   PetscErrorCode  ierr;
1936cf0e42fSMatthew G. Knepley 
1946cf0e42fSMatthew G. Knepley   PetscFunctionBegin;
1956cf0e42fSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
19622eabd52SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
19722eabd52SMatthew G. Knepley   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
1986cf0e42fSMatthew G. Knepley   ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
1996cf0e42fSMatthew G. Knepley   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
2006cf0e42fSMatthew G. Knepley   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
2016cf0e42fSMatthew G. Knepley   for (v = 0; v < numValues; ++v) {
2026cf0e42fSMatthew G. Knepley     IS              pointIS;
2036cf0e42fSMatthew G. Knepley     const PetscInt *points;
2046cf0e42fSMatthew G. Knepley     PetscInt        numPoints, p;
2056cf0e42fSMatthew G. Knepley 
2066cf0e42fSMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr);
2076cf0e42fSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
2086cf0e42fSMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
2096cf0e42fSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2106cf0e42fSMatthew G. Knepley       PetscInt *closure = NULL;
21122eabd52SMatthew G. Knepley       PetscInt  closureSize, point, cl;
2126cf0e42fSMatthew G. Knepley 
2136cf0e42fSMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure);CHKERRQ(ierr);
21422eabd52SMatthew G. Knepley       for (cl = closureSize-1; cl > 0; --cl) {
21522eabd52SMatthew G. Knepley         point = closure[cl*2];
21622eabd52SMatthew G. Knepley         if ((point >= cStart) && (point < cEnd)) {ierr = DMLabelSetValue(label, point, values[v]);CHKERRQ(ierr); break;}
21722eabd52SMatthew G. Knepley       }
2186cf0e42fSMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &closureSize, &closure);CHKERRQ(ierr);
2196cf0e42fSMatthew G. Knepley     }
2206cf0e42fSMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
2216cf0e42fSMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
2226cf0e42fSMatthew G. Knepley   }
2236cf0e42fSMatthew G. Knepley   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
2246cf0e42fSMatthew G. Knepley   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
2256cf0e42fSMatthew G. Knepley   PetscFunctionReturn(0);
2266cf0e42fSMatthew G. Knepley }
2276cf0e42fSMatthew G. Knepley 
228f402d5e4SToby Isaac /*@
229f402d5e4SToby Isaac   DMPlexLabelClearCells - Remove cells from a label
230f402d5e4SToby Isaac 
231f402d5e4SToby Isaac   Input Parameters:
232f402d5e4SToby Isaac + dm - The DM
233f402d5e4SToby Isaac - label - A DMLabel marking surface points and their adjacent cells
234f402d5e4SToby Isaac 
235f402d5e4SToby Isaac   Output Parameter:
236f402d5e4SToby Isaac . label - A DMLabel without cells
237f402d5e4SToby Isaac 
238f402d5e4SToby Isaac   Level: developer
239f402d5e4SToby Isaac 
240f402d5e4SToby Isaac   Note: This undoes DMPlexLabelAddCells()
241f402d5e4SToby Isaac 
242f402d5e4SToby Isaac .seealso: DMPlexLabelComplete(), DMPlexLabelCohesiveComplete(), DMPlexLabelAddCells()
243f402d5e4SToby Isaac @*/
244f402d5e4SToby Isaac PetscErrorCode DMPlexLabelClearCells(DM dm, DMLabel label)
245f402d5e4SToby Isaac {
246f402d5e4SToby Isaac   IS              valueIS;
247f402d5e4SToby Isaac   const PetscInt *values;
248f402d5e4SToby Isaac   PetscInt        numValues, v, cStart, cEnd, cEndInterior;
249f402d5e4SToby Isaac   PetscErrorCode  ierr;
250f402d5e4SToby Isaac 
251f402d5e4SToby Isaac   PetscFunctionBegin;
252f402d5e4SToby Isaac   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
253f402d5e4SToby Isaac   ierr = DMPlexGetHybridBounds(dm, &cEndInterior, NULL, NULL, NULL);CHKERRQ(ierr);
254f402d5e4SToby Isaac   cEnd = cEndInterior < 0 ? cEnd : cEndInterior;
255f402d5e4SToby Isaac   ierr = DMLabelGetNumValues(label, &numValues);CHKERRQ(ierr);
256f402d5e4SToby Isaac   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
257f402d5e4SToby Isaac   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
258f402d5e4SToby Isaac   for (v = 0; v < numValues; ++v) {
259f402d5e4SToby Isaac     IS              pointIS;
260f402d5e4SToby Isaac     const PetscInt *points;
261f402d5e4SToby Isaac     PetscInt        numPoints, p;
262f402d5e4SToby Isaac 
263f402d5e4SToby Isaac     ierr = DMLabelGetStratumSize(label, values[v], &numPoints);CHKERRQ(ierr);
264f402d5e4SToby Isaac     ierr = DMLabelGetStratumIS(label, values[v], &pointIS);CHKERRQ(ierr);
265f402d5e4SToby Isaac     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
266f402d5e4SToby Isaac     for (p = 0; p < numPoints; ++p) {
267f402d5e4SToby Isaac       PetscInt point = points[p];
268f402d5e4SToby Isaac 
269f402d5e4SToby Isaac       if (point >= cStart && point < cEnd) {
270f402d5e4SToby Isaac         ierr = DMLabelClearValue(label,point,values[v]);CHKERRQ(ierr);
271f402d5e4SToby Isaac       }
272f402d5e4SToby Isaac     }
273f402d5e4SToby Isaac     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
274f402d5e4SToby Isaac     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
275f402d5e4SToby Isaac   }
276f402d5e4SToby Isaac   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
277f402d5e4SToby Isaac   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
278f402d5e4SToby Isaac   PetscFunctionReturn(0);
279f402d5e4SToby Isaac }
280f402d5e4SToby Isaac 
28159eef20bSToby Isaac /* take (oldEnd, added) pairs, ordered by height and convert them to (oldstart, newstart) pairs, ordered by ascending
28259eef20bSToby Isaac  * index (skipping first, which is (0,0)) */
2832582d50cSToby Isaac PETSC_STATIC_INLINE PetscErrorCode DMPlexShiftPointSetUp_Internal(PetscInt depth, PetscInt depthShift[])
284cd0c2139SMatthew G Knepley {
2852582d50cSToby Isaac   PetscInt d, off = 0;
2862582d50cSToby Isaac 
2872582d50cSToby Isaac   PetscFunctionBegin;
28859eef20bSToby Isaac   /* sort by (oldend): yes this is an O(n^2) sort, we expect depth <= 3 */
2890974a383SToby Isaac   for (d = 0; d < depth; d++) {
2902582d50cSToby Isaac     PetscInt firstd = d;
2910974a383SToby Isaac     PetscInt firstStart = depthShift[2*d];
2922582d50cSToby Isaac     PetscInt e;
2932582d50cSToby Isaac 
2942582d50cSToby Isaac     for (e = d+1; e <= depth; e++) {
2952582d50cSToby Isaac       if (depthShift[2*e] < firstStart) {
2962582d50cSToby Isaac         firstd = e;
2972582d50cSToby Isaac         firstStart = depthShift[2*d];
2982582d50cSToby Isaac       }
2992582d50cSToby Isaac     }
3002582d50cSToby Isaac     if (firstd != d) {
3012582d50cSToby Isaac       PetscInt swap[2];
3022582d50cSToby Isaac 
3032582d50cSToby Isaac       e = firstd;
3042582d50cSToby Isaac       swap[0] = depthShift[2*d];
3052582d50cSToby Isaac       swap[1] = depthShift[2*d+1];
3062582d50cSToby Isaac       depthShift[2*d]   = depthShift[2*e];
3072582d50cSToby Isaac       depthShift[2*d+1] = depthShift[2*e+1];
3082582d50cSToby Isaac       depthShift[2*e]   = swap[0];
3092582d50cSToby Isaac       depthShift[2*e+1] = swap[1];
3102582d50cSToby Isaac     }
3112582d50cSToby Isaac   }
3122582d50cSToby Isaac   /* convert (oldstart, added) to (oldstart, newstart) */
3130974a383SToby Isaac   for (d = 0; d <= depth; d++) {
3142582d50cSToby Isaac     off += depthShift[2*d+1];
31559eef20bSToby Isaac     depthShift[2*d+1] = depthShift[2*d] + off;
3162582d50cSToby Isaac   }
3172582d50cSToby Isaac   PetscFunctionReturn(0);
3182582d50cSToby Isaac }
3192582d50cSToby Isaac 
3202582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
3212582d50cSToby Isaac PETSC_STATIC_INLINE PetscInt DMPlexShiftPoint_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
3222582d50cSToby Isaac {
3232582d50cSToby Isaac   PetscInt d;
3242582d50cSToby Isaac   PetscInt newOff = 0;
3252582d50cSToby Isaac 
3262582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
3272582d50cSToby Isaac     if (p < depthShift[2*d]) return p + newOff;
3282582d50cSToby Isaac     else newOff = depthShift[2*d+1] - depthShift[2*d];
3292582d50cSToby Isaac   }
3300974a383SToby Isaac   return p + newOff;
3312582d50cSToby Isaac }
3322582d50cSToby Isaac 
3332582d50cSToby Isaac /* depthShift is a list of (old, new) pairs */
3342582d50cSToby Isaac PETSC_STATIC_INLINE PetscInt DMPlexShiftPointInverse_Internal(PetscInt p, PetscInt depth, PetscInt depthShift[])
3352582d50cSToby Isaac {
3362582d50cSToby Isaac   PetscInt d;
3372582d50cSToby Isaac   PetscInt newOff = 0;
3382582d50cSToby Isaac 
3392582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
3402582d50cSToby Isaac     if (p < depthShift[2*d+1]) return p + newOff;
3412582d50cSToby Isaac     else newOff = depthShift[2*d] - depthShift[2*d+1];
3422582d50cSToby Isaac   }
3430974a383SToby Isaac   return p + newOff;
344cd0c2139SMatthew G Knepley }
345cd0c2139SMatthew G Knepley 
346cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftSizes_Internal(DM dm, PetscInt depthShift[], DM dmNew)
347cd0c2139SMatthew G Knepley {
348cd0c2139SMatthew G Knepley   PetscInt       depth = 0, d, pStart, pEnd, p;
349fa8e8ae5SToby Isaac   DMLabel        depthLabel;
350cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
351cd0c2139SMatthew G Knepley 
352cd0c2139SMatthew G Knepley   PetscFunctionBegin;
353cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
354cd0c2139SMatthew G Knepley   if (depth < 0) PetscFunctionReturn(0);
355cd0c2139SMatthew G Knepley   /* Step 1: Expand chart */
356cd0c2139SMatthew G Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3570974a383SToby Isaac   pEnd = DMPlexShiftPoint_Internal(pEnd,depth,depthShift);
358cd0c2139SMatthew G Knepley   ierr = DMPlexSetChart(dmNew, pStart, pEnd);CHKERRQ(ierr);
359fa8e8ae5SToby Isaac   ierr = DMCreateLabel(dmNew,"depth");CHKERRQ(ierr);
360fa8e8ae5SToby Isaac   ierr = DMPlexGetDepthLabel(dmNew,&depthLabel);CHKERRQ(ierr);
361cd0c2139SMatthew G Knepley   /* Step 2: Set cone and support sizes */
362cd0c2139SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
363fa8e8ae5SToby Isaac     PetscInt pStartNew, pEndNew;
364fa8e8ae5SToby Isaac     IS pIS;
365fa8e8ae5SToby Isaac 
366cd0c2139SMatthew G Knepley     ierr = DMPlexGetDepthStratum(dm, d, &pStart, &pEnd);CHKERRQ(ierr);
367fa8e8ae5SToby Isaac     pStartNew = DMPlexShiftPoint_Internal(pStart, depth, depthShift);
368fa8e8ae5SToby Isaac     pEndNew = DMPlexShiftPoint_Internal(pEnd, depth, depthShift);
369fa8e8ae5SToby Isaac     ierr = ISCreateStride(PETSC_COMM_SELF, pEndNew - pStartNew, pStartNew, 1, &pIS);CHKERRQ(ierr);
370fa8e8ae5SToby Isaac     ierr = DMLabelSetStratumIS(depthLabel, d, pIS);CHKERRQ(ierr);
371fa8e8ae5SToby Isaac     ierr = ISDestroy(&pIS);CHKERRQ(ierr);
372cd0c2139SMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
3732582d50cSToby Isaac       PetscInt newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
374cd0c2139SMatthew G Knepley       PetscInt size;
375cd0c2139SMatthew G Knepley 
376cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, p, &size);CHKERRQ(ierr);
377cd0c2139SMatthew G Knepley       ierr = DMPlexSetConeSize(dmNew, newp, size);CHKERRQ(ierr);
378cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, p, &size);CHKERRQ(ierr);
379cd0c2139SMatthew G Knepley       ierr = DMPlexSetSupportSize(dmNew, newp, size);CHKERRQ(ierr);
380cd0c2139SMatthew G Knepley     }
381cd0c2139SMatthew G Knepley   }
382cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
383cd0c2139SMatthew G Knepley }
384cd0c2139SMatthew G Knepley 
385cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftPoints_Internal(DM dm, PetscInt depthShift[], DM dmNew)
386cd0c2139SMatthew G Knepley {
3872582d50cSToby Isaac   PetscInt      *newpoints;
3882582d50cSToby Isaac   PetscInt       depth = 0, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, pStart, pEnd, p;
389cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
390cd0c2139SMatthew G Knepley 
391cd0c2139SMatthew G Knepley   PetscFunctionBegin;
392cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
393cd0c2139SMatthew G Knepley   if (depth < 0) PetscFunctionReturn(0);
394cd0c2139SMatthew G Knepley   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
395dcbb62e8SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dmNew, &maxConeSizeNew, &maxSupportSizeNew);CHKERRQ(ierr);
3962582d50cSToby Isaac   ierr = PetscMalloc1(PetscMax(PetscMax(maxConeSize, maxSupportSize), PetscMax(maxConeSizeNew, maxSupportSizeNew)),&newpoints);CHKERRQ(ierr);
397cd0c2139SMatthew G Knepley   /* Step 5: Set cones and supports */
398cd0c2139SMatthew G Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
399cd0c2139SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
400cd0c2139SMatthew G Knepley     const PetscInt *points = NULL, *orientations = NULL;
4012582d50cSToby Isaac     PetscInt        size,sizeNew, i, newp = DMPlexShiftPoint_Internal(p, depth, depthShift);
402cd0c2139SMatthew G Knepley 
403cd0c2139SMatthew G Knepley     ierr = DMPlexGetConeSize(dm, p, &size);CHKERRQ(ierr);
404cd0c2139SMatthew G Knepley     ierr = DMPlexGetCone(dm, p, &points);CHKERRQ(ierr);
405cd0c2139SMatthew G Knepley     ierr = DMPlexGetConeOrientation(dm, p, &orientations);CHKERRQ(ierr);
406cd0c2139SMatthew G Knepley     for (i = 0; i < size; ++i) {
4072582d50cSToby Isaac       newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
408cd0c2139SMatthew G Knepley     }
409cd0c2139SMatthew G Knepley     ierr = DMPlexSetCone(dmNew, newp, newpoints);CHKERRQ(ierr);
410cd0c2139SMatthew G Knepley     ierr = DMPlexSetConeOrientation(dmNew, newp, orientations);CHKERRQ(ierr);
411cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupportSize(dm, p, &size);CHKERRQ(ierr);
412dcbb62e8SMatthew G. Knepley     ierr = DMPlexGetSupportSize(dmNew, newp, &sizeNew);CHKERRQ(ierr);
413cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupport(dm, p, &points);CHKERRQ(ierr);
414cd0c2139SMatthew G Knepley     for (i = 0; i < size; ++i) {
4152582d50cSToby Isaac       newpoints[i] = DMPlexShiftPoint_Internal(points[i], depth, depthShift);
416cd0c2139SMatthew G Knepley     }
417dcbb62e8SMatthew G. Knepley     for (i = size; i < sizeNew; ++i) newpoints[i] = 0;
418cd0c2139SMatthew G Knepley     ierr = DMPlexSetSupport(dmNew, newp, newpoints);CHKERRQ(ierr);
419cd0c2139SMatthew G Knepley   }
4202582d50cSToby Isaac   ierr = PetscFree(newpoints);CHKERRQ(ierr);
421cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
422cd0c2139SMatthew G Knepley }
423cd0c2139SMatthew G Knepley 
424cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftCoordinates_Internal(DM dm, PetscInt depthShift[], DM dmNew)
425cd0c2139SMatthew G Knepley {
426cd0c2139SMatthew G Knepley   PetscSection   coordSection, newCoordSection;
427cd0c2139SMatthew G Knepley   Vec            coordinates, newCoordinates;
428cd0c2139SMatthew G Knepley   PetscScalar   *coords, *newCoords;
429f2b8cce1SMatthew G. Knepley   PetscInt       coordSize, sStart, sEnd;
430f2b8cce1SMatthew G. Knepley   PetscInt       dim, depth = 0, cStart, cEnd, cStartNew, cEndNew, c, vStart, vEnd, vStartNew, vEndNew, v;
431f2b8cce1SMatthew G. Knepley   PetscBool      hasCells;
432cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
433cd0c2139SMatthew G Knepley 
434cd0c2139SMatthew G Knepley   PetscFunctionBegin;
435f2b8cce1SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
436c0e8cf5fSMatthew G. Knepley   ierr = DMSetCoordinateDim(dmNew, dim);CHKERRQ(ierr);
437cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
438cd0c2139SMatthew G Knepley   /* Step 8: Convert coordinates */
439cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
440f2b8cce1SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
441cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepthStratum(dmNew, 0, &vStartNew, &vEndNew);CHKERRQ(ierr);
442f2b8cce1SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dmNew, 0, &cStartNew, &cEndNew);CHKERRQ(ierr);
44369d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
444cd0c2139SMatthew G Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &newCoordSection);CHKERRQ(ierr);
445cd0c2139SMatthew G Knepley   ierr = PetscSectionSetNumFields(newCoordSection, 1);CHKERRQ(ierr);
446cd0c2139SMatthew G Knepley   ierr = PetscSectionSetFieldComponents(newCoordSection, 0, dim);CHKERRQ(ierr);
447f2b8cce1SMatthew G. Knepley   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
448f2b8cce1SMatthew G. Knepley   hasCells = sStart == cStart ? PETSC_TRUE : PETSC_FALSE;
449f2b8cce1SMatthew G. Knepley   ierr = PetscSectionSetChart(newCoordSection, hasCells ? cStartNew : vStartNew, vEndNew);CHKERRQ(ierr);
450f2b8cce1SMatthew G. Knepley   if (hasCells) {
451f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
452f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof;
453f2b8cce1SMatthew G. Knepley 
454f2b8cce1SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
455f2b8cce1SMatthew G. Knepley       ierr = PetscSectionSetDof(newCoordSection, cNew, dof);CHKERRQ(ierr);
456f2b8cce1SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(newCoordSection, cNew, 0, dof);CHKERRQ(ierr);
457f2b8cce1SMatthew G. Knepley     }
458f2b8cce1SMatthew G. Knepley   }
459cd0c2139SMatthew G Knepley   for (v = vStartNew; v < vEndNew; ++v) {
460cd0c2139SMatthew G Knepley     ierr = PetscSectionSetDof(newCoordSection, v, dim);CHKERRQ(ierr);
461cd0c2139SMatthew G Knepley     ierr = PetscSectionSetFieldDof(newCoordSection, v, 0, dim);CHKERRQ(ierr);
462cd0c2139SMatthew G Knepley   }
463cd0c2139SMatthew G Knepley   ierr = PetscSectionSetUp(newCoordSection);CHKERRQ(ierr);
46446e270d4SMatthew G. Knepley   ierr = DMSetCoordinateSection(dmNew, PETSC_DETERMINE, newCoordSection);CHKERRQ(ierr);
465cd0c2139SMatthew G Knepley   ierr = PetscSectionGetStorageSize(newCoordSection, &coordSize);CHKERRQ(ierr);
4668b9ced59SLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &newCoordinates);CHKERRQ(ierr);
467cd0c2139SMatthew G Knepley   ierr = PetscObjectSetName((PetscObject) newCoordinates, "coordinates");CHKERRQ(ierr);
468cd0c2139SMatthew G Knepley   ierr = VecSetSizes(newCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
4694e90ef8eSMatthew G. Knepley   ierr = VecSetBlockSize(newCoordinates, dim);CHKERRQ(ierr);
4702eb5907fSJed Brown   ierr = VecSetType(newCoordinates,VECSTANDARD);CHKERRQ(ierr);
471cd0c2139SMatthew G Knepley   ierr = DMSetCoordinatesLocal(dmNew, newCoordinates);CHKERRQ(ierr);
472cd0c2139SMatthew G Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
473cd0c2139SMatthew G Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
474cd0c2139SMatthew G Knepley   ierr = VecGetArray(newCoordinates, &newCoords);CHKERRQ(ierr);
475f2b8cce1SMatthew G. Knepley   if (hasCells) {
476f2b8cce1SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
477f2b8cce1SMatthew G. Knepley       PetscInt cNew = DMPlexShiftPoint_Internal(c, depth, depthShift), dof, off, noff, d;
478f2b8cce1SMatthew G. Knepley 
479f2b8cce1SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
480f2b8cce1SMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, c, &off);CHKERRQ(ierr);
481f2b8cce1SMatthew G. Knepley       ierr = PetscSectionGetOffset(newCoordSection, cNew, &noff);CHKERRQ(ierr);
482f2b8cce1SMatthew G. Knepley       for (d = 0; d < dof; ++d) newCoords[noff+d] = coords[off+d];
483f2b8cce1SMatthew G. Knepley     }
484f2b8cce1SMatthew G. Knepley   }
485cd0c2139SMatthew G Knepley   for (v = vStart; v < vEnd; ++v) {
486cd0c2139SMatthew G Knepley     PetscInt dof, off, noff, d;
487cd0c2139SMatthew G Knepley 
488cd0c2139SMatthew G Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
489cd0c2139SMatthew G Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
4902582d50cSToby Isaac     ierr = PetscSectionGetOffset(newCoordSection, DMPlexShiftPoint_Internal(v, depth, depthShift), &noff);CHKERRQ(ierr);
491f2b8cce1SMatthew G. Knepley     for (d = 0; d < dof; ++d) newCoords[noff+d] = coords[off+d];
492cd0c2139SMatthew G Knepley   }
493cd0c2139SMatthew G Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
494cd0c2139SMatthew G Knepley   ierr = VecRestoreArray(newCoordinates, &newCoords);CHKERRQ(ierr);
495cd0c2139SMatthew G Knepley   ierr = VecDestroy(&newCoordinates);CHKERRQ(ierr);
496cd0c2139SMatthew G Knepley   ierr = PetscSectionDestroy(&newCoordSection);CHKERRQ(ierr);
497cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
498cd0c2139SMatthew G Knepley }
499cd0c2139SMatthew G Knepley 
500cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftSF_Internal(DM dm, PetscInt depthShift[], DM dmNew)
501cd0c2139SMatthew G Knepley {
5022582d50cSToby Isaac   PetscInt           depth = 0;
503cd0c2139SMatthew G Knepley   PetscSF            sfPoint, sfPointNew;
504cd0c2139SMatthew G Knepley   const PetscSFNode *remotePoints;
505cd0c2139SMatthew G Knepley   PetscSFNode       *gremotePoints;
506cd0c2139SMatthew G Knepley   const PetscInt    *localPoints;
507cd0c2139SMatthew G Knepley   PetscInt          *glocalPoints, *newLocation, *newRemoteLocation;
508cd0c2139SMatthew G Knepley   PetscInt           numRoots, numLeaves, l, pStart, pEnd, totShift = 0;
509cd0c2139SMatthew G Knepley   PetscErrorCode     ierr;
510cd0c2139SMatthew G Knepley 
511cd0c2139SMatthew G Knepley   PetscFunctionBegin;
512cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
513cd0c2139SMatthew G Knepley   /* Step 9: Convert pointSF */
514cd0c2139SMatthew G Knepley   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
515cd0c2139SMatthew G Knepley   ierr = DMGetPointSF(dmNew, &sfPointNew);CHKERRQ(ierr);
516cd0c2139SMatthew G Knepley   ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
517cd0c2139SMatthew G Knepley   ierr = PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr);
5186e21efdcSToby Isaac   totShift = DMPlexShiftPoint_Internal(pEnd,depth,depthShift) - pEnd;
519cd0c2139SMatthew G Knepley   if (numRoots >= 0) {
520dcca6d9dSJed Brown     ierr = PetscMalloc2(numRoots,&newLocation,pEnd-pStart,&newRemoteLocation);CHKERRQ(ierr);
5212582d50cSToby Isaac     for (l=0; l<numRoots; l++) newLocation[l] = DMPlexShiftPoint_Internal(l, depth, depthShift);
522cd0c2139SMatthew G Knepley     ierr = PetscSFBcastBegin(sfPoint, MPIU_INT, newLocation, newRemoteLocation);CHKERRQ(ierr);
523cd0c2139SMatthew G Knepley     ierr = PetscSFBcastEnd(sfPoint, MPIU_INT, newLocation, newRemoteLocation);CHKERRQ(ierr);
524785e854fSJed Brown     ierr = PetscMalloc1(numLeaves,    &glocalPoints);CHKERRQ(ierr);
525785e854fSJed Brown     ierr = PetscMalloc1(numLeaves, &gremotePoints);CHKERRQ(ierr);
526cd0c2139SMatthew G Knepley     for (l = 0; l < numLeaves; ++l) {
5272582d50cSToby Isaac       glocalPoints[l]        = DMPlexShiftPoint_Internal(localPoints[l], depth, depthShift);
528cd0c2139SMatthew G Knepley       gremotePoints[l].rank  = remotePoints[l].rank;
529cd0c2139SMatthew G Knepley       gremotePoints[l].index = newRemoteLocation[localPoints[l]];
530cd0c2139SMatthew G Knepley     }
531cd0c2139SMatthew G Knepley     ierr = PetscFree2(newLocation,newRemoteLocation);CHKERRQ(ierr);
532cd0c2139SMatthew G Knepley     ierr = PetscSFSetGraph(sfPointNew, numRoots + totShift, numLeaves, glocalPoints, PETSC_OWN_POINTER, gremotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
533cd0c2139SMatthew G Knepley   }
534cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
535cd0c2139SMatthew G Knepley }
536cd0c2139SMatthew G Knepley 
537cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexShiftLabels_Internal(DM dm, PetscInt depthShift[], DM dmNew)
538cd0c2139SMatthew G Knepley {
539cd0c2139SMatthew G Knepley   PetscSF            sfPoint;
540cd0c2139SMatthew G Knepley   DMLabel            vtkLabel, ghostLabel;
541cd0c2139SMatthew G Knepley   const PetscSFNode *leafRemote;
542cd0c2139SMatthew G Knepley   const PetscInt    *leafLocal;
5432582d50cSToby Isaac   PetscInt           depth = 0, numLeaves, numLabels, l, cStart, cEnd, c, fStart, fEnd, f;
544cd0c2139SMatthew G Knepley   PetscMPIInt        rank;
545cd0c2139SMatthew G Knepley   PetscErrorCode     ierr;
546cd0c2139SMatthew G Knepley 
547cd0c2139SMatthew G Knepley   PetscFunctionBegin;
548cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
549cd0c2139SMatthew G Knepley   /* Step 10: Convert labels */
550c58f1c22SToby Isaac   ierr = DMGetNumLabels(dm, &numLabels);CHKERRQ(ierr);
551cd0c2139SMatthew G Knepley   for (l = 0; l < numLabels; ++l) {
552cd0c2139SMatthew G Knepley     DMLabel         label, newlabel;
553cd0c2139SMatthew G Knepley     const char     *lname;
554fa8e8ae5SToby Isaac     PetscBool       isDepth, isDim;
555cd0c2139SMatthew G Knepley     IS              valueIS;
556cd0c2139SMatthew G Knepley     const PetscInt *values;
557cd0c2139SMatthew G Knepley     PetscInt        numValues, val;
558cd0c2139SMatthew G Knepley 
559c58f1c22SToby Isaac     ierr = DMGetLabelName(dm, l, &lname);CHKERRQ(ierr);
560cd0c2139SMatthew G Knepley     ierr = PetscStrcmp(lname, "depth", &isDepth);CHKERRQ(ierr);
561cd0c2139SMatthew G Knepley     if (isDepth) continue;
562fa8e8ae5SToby Isaac     ierr = PetscStrcmp(lname, "dim", &isDim);CHKERRQ(ierr);
563fa8e8ae5SToby Isaac     if (isDim) continue;
564c58f1c22SToby Isaac     ierr = DMCreateLabel(dmNew, lname);CHKERRQ(ierr);
565c58f1c22SToby Isaac     ierr = DMGetLabel(dm, lname, &label);CHKERRQ(ierr);
566c58f1c22SToby Isaac     ierr = DMGetLabel(dmNew, lname, &newlabel);CHKERRQ(ierr);
56764a6a1a4SToby Isaac     ierr = DMLabelGetDefaultValue(label,&val);CHKERRQ(ierr);
56864a6a1a4SToby Isaac     ierr = DMLabelSetDefaultValue(newlabel,val);CHKERRQ(ierr);
569cd0c2139SMatthew G Knepley     ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
570cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(valueIS, &numValues);CHKERRQ(ierr);
571cd0c2139SMatthew G Knepley     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
572cd0c2139SMatthew G Knepley     for (val = 0; val < numValues; ++val) {
573cd0c2139SMatthew G Knepley       IS              pointIS;
574cd0c2139SMatthew G Knepley       const PetscInt *points;
575cd0c2139SMatthew G Knepley       PetscInt        numPoints, p;
576cd0c2139SMatthew G Knepley 
577cd0c2139SMatthew G Knepley       ierr = DMLabelGetStratumIS(label, values[val], &pointIS);CHKERRQ(ierr);
578cd0c2139SMatthew G Knepley       ierr = ISGetLocalSize(pointIS, &numPoints);CHKERRQ(ierr);
579cd0c2139SMatthew G Knepley       ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
580cd0c2139SMatthew G Knepley       for (p = 0; p < numPoints; ++p) {
5812582d50cSToby Isaac         const PetscInt newpoint = DMPlexShiftPoint_Internal(points[p], depth, depthShift);
582cd0c2139SMatthew G Knepley 
583cd0c2139SMatthew G Knepley         ierr = DMLabelSetValue(newlabel, newpoint, values[val]);CHKERRQ(ierr);
584cd0c2139SMatthew G Knepley       }
585cd0c2139SMatthew G Knepley       ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
586cd0c2139SMatthew G Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
587cd0c2139SMatthew G Knepley     }
588cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
589cd0c2139SMatthew G Knepley     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
590cd0c2139SMatthew G Knepley   }
591cd0c2139SMatthew G Knepley   /* Step 11: Make label for output (vtk) and to mark ghost points (ghost) */
592cd0c2139SMatthew G Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRQ(ierr);
593cd0c2139SMatthew G Knepley   ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
594cd0c2139SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
595cd0c2139SMatthew G Knepley   ierr = PetscSFGetGraph(sfPoint, NULL, &numLeaves, &leafLocal, &leafRemote);CHKERRQ(ierr);
596c58f1c22SToby Isaac   ierr = DMCreateLabel(dmNew, "vtk");CHKERRQ(ierr);
597c58f1c22SToby Isaac   ierr = DMCreateLabel(dmNew, "ghost");CHKERRQ(ierr);
598c58f1c22SToby Isaac   ierr = DMGetLabel(dmNew, "vtk", &vtkLabel);CHKERRQ(ierr);
599c58f1c22SToby Isaac   ierr = DMGetLabel(dmNew, "ghost", &ghostLabel);CHKERRQ(ierr);
600cd0c2139SMatthew G Knepley   for (l = 0, c = cStart; l < numLeaves && c < cEnd; ++l, ++c) {
601cd0c2139SMatthew G Knepley     for (; c < leafLocal[l] && c < cEnd; ++c) {
602cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(vtkLabel, c, 1);CHKERRQ(ierr);
603cd0c2139SMatthew G Knepley     }
604cd0c2139SMatthew G Knepley     if (leafLocal[l] >= cEnd) break;
605cd0c2139SMatthew G Knepley     if (leafRemote[l].rank == rank) {
606cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(vtkLabel, c, 1);CHKERRQ(ierr);
607cd0c2139SMatthew G Knepley     } else {
608cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(ghostLabel, c, 2);CHKERRQ(ierr);
609cd0c2139SMatthew G Knepley     }
610cd0c2139SMatthew G Knepley   }
611cd0c2139SMatthew G Knepley   for (; c < cEnd; ++c) {
612cd0c2139SMatthew G Knepley     ierr = DMLabelSetValue(vtkLabel, c, 1);CHKERRQ(ierr);
613cd0c2139SMatthew G Knepley   }
614cd0c2139SMatthew G Knepley   if (0) {
615cd0c2139SMatthew G Knepley     ierr = DMLabelView(vtkLabel, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
616cd0c2139SMatthew G Knepley   }
617cd0c2139SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dmNew, 1, &fStart, &fEnd);CHKERRQ(ierr);
618cd0c2139SMatthew G Knepley   for (f = fStart; f < fEnd; ++f) {
619cd0c2139SMatthew G Knepley     PetscInt numCells;
620cd0c2139SMatthew G Knepley 
621cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupportSize(dmNew, f, &numCells);CHKERRQ(ierr);
622cd0c2139SMatthew G Knepley     if (numCells < 2) {
623cd0c2139SMatthew G Knepley       ierr = DMLabelSetValue(ghostLabel, f, 1);CHKERRQ(ierr);
624cd0c2139SMatthew G Knepley     } else {
625cd0c2139SMatthew G Knepley       const PetscInt *cells = NULL;
626cd0c2139SMatthew G Knepley       PetscInt        vA, vB;
627cd0c2139SMatthew G Knepley 
628cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupport(dmNew, f, &cells);CHKERRQ(ierr);
629cd0c2139SMatthew G Knepley       ierr = DMLabelGetValue(vtkLabel, cells[0], &vA);CHKERRQ(ierr);
630cd0c2139SMatthew G Knepley       ierr = DMLabelGetValue(vtkLabel, cells[1], &vB);CHKERRQ(ierr);
63190664b96SToby Isaac       if (vA != 1 && vB != 1) {ierr = DMLabelSetValue(ghostLabel, f, 1);CHKERRQ(ierr);}
632cd0c2139SMatthew G Knepley     }
633cd0c2139SMatthew G Knepley   }
634cd0c2139SMatthew G Knepley   if (0) {
635cd0c2139SMatthew G Knepley     ierr = DMLabelView(ghostLabel, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
636cd0c2139SMatthew G Knepley   }
637cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
638cd0c2139SMatthew G Knepley }
639cd0c2139SMatthew G Knepley 
640ca04dac2SToby Isaac static PetscErrorCode DMPlexShiftTree_Internal(DM dm, PetscInt depthShift[], DM dmNew)
641ca04dac2SToby Isaac {
642ca04dac2SToby Isaac   DM             refTree;
643ca04dac2SToby Isaac   PetscSection   pSec;
644ca04dac2SToby Isaac   PetscInt       *parents, *childIDs;
645ca04dac2SToby Isaac   PetscErrorCode ierr;
646ca04dac2SToby Isaac 
647ca04dac2SToby Isaac   PetscFunctionBegin;
648ca04dac2SToby Isaac   ierr = DMPlexGetReferenceTree(dm,&refTree);CHKERRQ(ierr);
649ca04dac2SToby Isaac   ierr = DMPlexSetReferenceTree(dmNew,refTree);CHKERRQ(ierr);
650ca04dac2SToby Isaac   ierr = DMPlexGetTree(dm,&pSec,&parents,&childIDs,NULL,NULL);CHKERRQ(ierr);
651ca04dac2SToby Isaac   if (pSec) {
6522582d50cSToby Isaac     PetscInt p, pStart, pEnd, *parentsShifted, pStartShifted, pEndShifted, depth;
653fb4630b5SToby Isaac     PetscInt *childIDsShifted;
654ca04dac2SToby Isaac     PetscSection pSecShifted;
655ca04dac2SToby Isaac 
656ca04dac2SToby Isaac     ierr = PetscSectionGetChart(pSec,&pStart,&pEnd);CHKERRQ(ierr);
657ca04dac2SToby Isaac     ierr = DMPlexGetDepth(dm,&depth);CHKERRQ(ierr);
6582582d50cSToby Isaac     pStartShifted = DMPlexShiftPoint_Internal(pStart,depth,depthShift);
6592582d50cSToby Isaac     pEndShifted   = DMPlexShiftPoint_Internal(pEnd,depth,depthShift);
660fb4630b5SToby Isaac     ierr = PetscMalloc2(pEndShifted - pStartShifted,&parentsShifted,pEndShifted-pStartShifted,&childIDsShifted);CHKERRQ(ierr);
661ca04dac2SToby Isaac     ierr = PetscSectionCreate(PetscObjectComm((PetscObject)dmNew),&pSecShifted);CHKERRQ(ierr);
662ca04dac2SToby Isaac     ierr = PetscSectionSetChart(pSecShifted,pStartShifted,pEndShifted);CHKERRQ(ierr);
663ca04dac2SToby Isaac     for (p = pStartShifted; p < pEndShifted; p++) {
664fb4630b5SToby Isaac       /* start off assuming no children */
665fb4630b5SToby Isaac       ierr = PetscSectionSetDof(pSecShifted,p,0);CHKERRQ(ierr);
666fb4630b5SToby Isaac     }
667fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
668fb4630b5SToby Isaac       PetscInt dof;
669fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p,depth,depthShift);
670ca04dac2SToby Isaac 
671ca04dac2SToby Isaac       ierr = PetscSectionGetDof(pSec,p,&dof);CHKERRQ(ierr);
672fb4630b5SToby Isaac       ierr = PetscSectionSetDof(pSecShifted,pNew,dof);CHKERRQ(ierr);
673ca04dac2SToby Isaac     }
674ca04dac2SToby Isaac     ierr = PetscSectionSetUp(pSecShifted);CHKERRQ(ierr);
675fb4630b5SToby Isaac     for (p = pStart; p < pEnd; p++) {
676fb4630b5SToby Isaac       PetscInt dof;
677fb4630b5SToby Isaac       PetscInt pNew = DMPlexShiftPoint_Internal(p,depth,depthShift);
678fb4630b5SToby Isaac 
679fb4630b5SToby Isaac       ierr = PetscSectionGetDof(pSec,p,&dof);CHKERRQ(ierr);
680fb4630b5SToby Isaac       if (dof) {
681fb4630b5SToby Isaac         PetscInt off, offNew;
682fb4630b5SToby Isaac 
683fb4630b5SToby Isaac         ierr = PetscSectionGetOffset(pSec,p,&off);CHKERRQ(ierr);
684fb4630b5SToby Isaac         ierr = PetscSectionGetOffset(pSecShifted,pNew,&offNew);CHKERRQ(ierr);
685fb4630b5SToby Isaac         parentsShifted[offNew] = DMPlexShiftPoint_Internal(parents[off],depth,depthShift);
686fb4630b5SToby Isaac         childIDsShifted[offNew] = childIDs[off];
687fb4630b5SToby Isaac       }
688fb4630b5SToby Isaac     }
689fb4630b5SToby Isaac     ierr = DMPlexSetTree(dmNew,pSecShifted,parentsShifted,childIDsShifted);CHKERRQ(ierr);
690fb4630b5SToby Isaac     ierr = PetscFree2(parentsShifted,childIDsShifted);CHKERRQ(ierr);
691e6885bbbSToby Isaac     ierr = PetscSectionDestroy(&pSecShifted);CHKERRQ(ierr);
692ca04dac2SToby Isaac   }
693ca04dac2SToby Isaac   PetscFunctionReturn(0);
694ca04dac2SToby Isaac }
695ca04dac2SToby Isaac 
696cd0c2139SMatthew G Knepley static PetscErrorCode DMPlexConstructGhostCells_Internal(DM dm, DMLabel label, PetscInt *numGhostCells, DM gdm)
697cd0c2139SMatthew G Knepley {
698da97024aSMatthew G. Knepley   PetscSF               sf;
699cd0c2139SMatthew G Knepley   IS                    valueIS;
700da97024aSMatthew G. Knepley   const PetscInt       *values, *leaves;
701cd0c2139SMatthew G Knepley   PetscInt             *depthShift;
7022582d50cSToby Isaac   PetscInt              d, depth = 0, nleaves, loc, Ng, numFS, fs, fStart, fEnd, ghostCell, cEnd, c;
70390b157c4SStefano Zampini   PetscBool             isper;
70490b157c4SStefano Zampini   const PetscReal      *maxCell, *L;
70590b157c4SStefano Zampini   const DMBoundaryType *bd;
706cd0c2139SMatthew G Knepley   PetscErrorCode        ierr;
707cd0c2139SMatthew G Knepley 
708cd0c2139SMatthew G Knepley   PetscFunctionBegin;
709da97024aSMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
710da97024aSMatthew G. Knepley   ierr = PetscSFGetGraph(sf, NULL, &nleaves, &leaves, NULL);CHKERRQ(ierr);
711da97024aSMatthew G. Knepley   nleaves = PetscMax(0, nleaves);
71246c796b9SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
713cd0c2139SMatthew G Knepley   /* Count ghost cells */
714cd0c2139SMatthew G Knepley   ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
715cd0c2139SMatthew G Knepley   ierr = ISGetLocalSize(valueIS, &numFS);CHKERRQ(ierr);
716cd0c2139SMatthew G Knepley   ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
7174a6cfa73SMatthew G. Knepley   Ng   = 0;
718cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
71946c796b9SMatthew G. Knepley     IS              faceIS;
72046c796b9SMatthew G. Knepley     const PetscInt *faces;
72146c796b9SMatthew G. Knepley     PetscInt        numFaces, f, numBdFaces = 0;
722cd0c2139SMatthew G Knepley 
72346c796b9SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, values[fs], &faceIS);CHKERRQ(ierr);
72446c796b9SMatthew G. Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
72546c796b9SMatthew G. Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
72646c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
727ca04dac2SToby Isaac       PetscInt numChildren;
728ca04dac2SToby Isaac 
729da97024aSMatthew G. Knepley       ierr = PetscFindInt(faces[f], nleaves, leaves, &loc);CHKERRQ(ierr);
730ca04dac2SToby Isaac       ierr = DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL);CHKERRQ(ierr);
731ca04dac2SToby Isaac       /* non-local and ancestors points don't get to register ghosts */
732ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
73346c796b9SMatthew G. Knepley       if ((faces[f] >= fStart) && (faces[f] < fEnd)) ++numBdFaces;
73446c796b9SMatthew G. Knepley     }
7354a6cfa73SMatthew G. Knepley     Ng += numBdFaces;
73646c796b9SMatthew G. Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
737cd0c2139SMatthew G Knepley   }
738cd0c2139SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
7392582d50cSToby Isaac   ierr = PetscMalloc1(2*(depth+1), &depthShift);CHKERRQ(ierr);
7402582d50cSToby Isaac   for (d = 0; d <= depth; d++) {
74159eef20bSToby Isaac     PetscInt dEnd;
7422582d50cSToby Isaac 
7430974a383SToby Isaac     ierr = DMPlexGetDepthStratum(dm,d,NULL,&dEnd);CHKERRQ(ierr);
74459eef20bSToby Isaac     depthShift[2*d]   = dEnd;
7452582d50cSToby Isaac     depthShift[2*d+1] = 0;
7462582d50cSToby Isaac   }
7472582d50cSToby Isaac   if (depth >= 0) depthShift[2*depth+1] = Ng;
7482582d50cSToby Isaac   ierr = DMPlexShiftPointSetUp_Internal(depth,depthShift);CHKERRQ(ierr);
749cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSizes_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
750cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
751cd0c2139SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
7525acea1aeSMatthew G. Knepley   ierr = DMPlexSetHybridBounds(gdm, cEnd, PETSC_DETERMINE, PETSC_DETERMINE, PETSC_DETERMINE);CHKERRQ(ierr);
7534a6cfa73SMatthew G. Knepley   for (c = cEnd; c < cEnd + Ng; ++c) {
754cd0c2139SMatthew G Knepley     ierr = DMPlexSetConeSize(gdm, c, 1);CHKERRQ(ierr);
755cd0c2139SMatthew G Knepley   }
756cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
757cd0c2139SMatthew G Knepley     IS              faceIS;
758cd0c2139SMatthew G Knepley     const PetscInt *faces;
759cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
760cd0c2139SMatthew G Knepley 
761cd0c2139SMatthew G Knepley     ierr = DMLabelGetStratumIS(label, values[fs], &faceIS);CHKERRQ(ierr);
762cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
763cd0c2139SMatthew G Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
764cd0c2139SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
765ca04dac2SToby Isaac       PetscInt size, numChildren;
766cd0c2139SMatthew G Knepley 
767da97024aSMatthew G. Knepley       ierr = PetscFindInt(faces[f], nleaves, leaves, &loc);CHKERRQ(ierr);
768ca04dac2SToby Isaac       ierr = DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL);CHKERRQ(ierr);
769ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
77046c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
771cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, faces[f], &size);CHKERRQ(ierr);
7724553fd90SToby Isaac       if (size != 1) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM has boundary face %d with %d support cells", faces[f], size);
7734a6cfa73SMatthew G. Knepley       ierr = DMPlexSetSupportSize(gdm, faces[f] + Ng, 2);CHKERRQ(ierr);
774cd0c2139SMatthew G Knepley     }
775cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
776cd0c2139SMatthew G Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
777cd0c2139SMatthew G Knepley   }
778cd0c2139SMatthew G Knepley   /* Step 4: Setup ghosted DM */
779cd0c2139SMatthew G Knepley   ierr = DMSetUp(gdm);CHKERRQ(ierr);
780cd0c2139SMatthew G Knepley   ierr = DMPlexShiftPoints_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
781cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
782cd0c2139SMatthew G Knepley   ghostCell = cEnd;
783cd0c2139SMatthew G Knepley   for (fs = 0; fs < numFS; ++fs) {
784cd0c2139SMatthew G Knepley     IS              faceIS;
785cd0c2139SMatthew G Knepley     const PetscInt *faces;
786cd0c2139SMatthew G Knepley     PetscInt        numFaces, f;
787cd0c2139SMatthew G Knepley 
788cd0c2139SMatthew G Knepley     ierr = DMLabelGetStratumIS(label, values[fs], &faceIS);CHKERRQ(ierr);
789cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(faceIS, &numFaces);CHKERRQ(ierr);
790cd0c2139SMatthew G Knepley     ierr = ISGetIndices(faceIS, &faces);CHKERRQ(ierr);
79146c796b9SMatthew G. Knepley     for (f = 0; f < numFaces; ++f) {
792ca04dac2SToby Isaac       PetscInt newFace = faces[f] + Ng, numChildren;
793cd0c2139SMatthew G Knepley 
794da97024aSMatthew G. Knepley       ierr = PetscFindInt(faces[f], nleaves, leaves, &loc);CHKERRQ(ierr);
795ca04dac2SToby Isaac       ierr = DMPlexGetTreeChildren(dm,faces[f],&numChildren,NULL);CHKERRQ(ierr);
796ca04dac2SToby Isaac       if (loc >= 0 || numChildren) continue;
79746c796b9SMatthew G. Knepley       if ((faces[f] < fStart) || (faces[f] >= fEnd)) continue;
798cd0c2139SMatthew G Knepley       ierr = DMPlexSetCone(gdm, ghostCell, &newFace);CHKERRQ(ierr);
799cd0c2139SMatthew G Knepley       ierr = DMPlexInsertSupport(gdm, newFace, 1, ghostCell);CHKERRQ(ierr);
80046c796b9SMatthew G. Knepley       ++ghostCell;
801cd0c2139SMatthew G Knepley     }
802cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(faceIS, &faces);CHKERRQ(ierr);
803cd0c2139SMatthew G Knepley     ierr = ISDestroy(&faceIS);CHKERRQ(ierr);
804cd0c2139SMatthew G Knepley   }
805cd0c2139SMatthew G Knepley   ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
806cd0c2139SMatthew G Knepley   ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
807cd0c2139SMatthew G Knepley   ierr = DMPlexShiftCoordinates_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
808cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSF_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
809cd0c2139SMatthew G Knepley   ierr = DMPlexShiftLabels_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
810ca04dac2SToby Isaac   ierr = DMPlexShiftTree_Internal(dm, depthShift, gdm);CHKERRQ(ierr);
811cd0c2139SMatthew G Knepley   ierr = PetscFree(depthShift);CHKERRQ(ierr);
812966c7b3fSMatthew G. Knepley   /* Step 7: Periodicity */
81390b157c4SStefano Zampini   ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
81490b157c4SStefano Zampini   ierr = DMSetPeriodicity(gdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
8154a6cfa73SMatthew G. Knepley   if (numGhostCells) *numGhostCells = Ng;
816cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
817cd0c2139SMatthew G Knepley }
818cd0c2139SMatthew G Knepley 
819cd0c2139SMatthew G Knepley /*@C
820cd0c2139SMatthew G Knepley   DMPlexConstructGhostCells - Construct ghost cells which connect to every boundary face
821cd0c2139SMatthew G Knepley 
822cd0c2139SMatthew G Knepley   Collective on dm
823cd0c2139SMatthew G Knepley 
824cd0c2139SMatthew G Knepley   Input Parameters:
825cd0c2139SMatthew G Knepley + dm - The original DM
826cd0c2139SMatthew G Knepley - labelName - The label specifying the boundary faces, or "Face Sets" if this is NULL
827cd0c2139SMatthew G Knepley 
828cd0c2139SMatthew G Knepley   Output Parameters:
829cd0c2139SMatthew G Knepley + numGhostCells - The number of ghost cells added to the DM
830cd0c2139SMatthew G Knepley - dmGhosted - The new DM
831cd0c2139SMatthew G Knepley 
832cd0c2139SMatthew G Knepley   Note: If no label exists of that name, one will be created marking all boundary faces
833cd0c2139SMatthew G Knepley 
834cd0c2139SMatthew G Knepley   Level: developer
835cd0c2139SMatthew G Knepley 
836cd0c2139SMatthew G Knepley .seealso: DMCreate()
83731266bc0SMatthew G. Knepley @*/
838cd0c2139SMatthew G Knepley PetscErrorCode DMPlexConstructGhostCells(DM dm, const char labelName[], PetscInt *numGhostCells, DM *dmGhosted)
839cd0c2139SMatthew G Knepley {
840cd0c2139SMatthew G Knepley   DM             gdm;
841cd0c2139SMatthew G Knepley   DMLabel        label;
842cd0c2139SMatthew G Knepley   const char    *name = labelName ? labelName : "Face Sets";
843cd0c2139SMatthew G Knepley   PetscInt       dim;
844*b0441da4SMatthew G. Knepley   PetscBool      useCone, useClosure;
845cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
846cd0c2139SMatthew G Knepley 
847cd0c2139SMatthew G Knepley   PetscFunctionBegin;
848cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8494a6cfa73SMatthew G. Knepley   if (numGhostCells) PetscValidPointer(numGhostCells, 3);
850cd0c2139SMatthew G Knepley   PetscValidPointer(dmGhosted, 4);
851cd0c2139SMatthew G Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), &gdm);CHKERRQ(ierr);
852cd0c2139SMatthew G Knepley   ierr = DMSetType(gdm, DMPLEX);CHKERRQ(ierr);
853c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
854c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(gdm, dim);CHKERRQ(ierr);
855*b0441da4SMatthew G. Knepley   ierr = DMGetBasicAdjacency(dm, &useCone, &useClosure);CHKERRQ(ierr);
856*b0441da4SMatthew G. Knepley   ierr = DMSetBasicAdjacency(gdm, useCone,  useClosure);CHKERRQ(ierr);
857c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
858cd0c2139SMatthew G Knepley   if (!label) {
859cd0c2139SMatthew G Knepley     /* Get label for boundary faces */
860c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
861c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
862e752be1aSMatthew G. Knepley     ierr = DMPlexMarkBoundaryFaces(dm, 1, label);CHKERRQ(ierr);
863cd0c2139SMatthew G Knepley   }
864cd0c2139SMatthew G Knepley   ierr = DMPlexConstructGhostCells_Internal(dm, label, numGhostCells, gdm);CHKERRQ(ierr);
865a6ba4734SToby Isaac   ierr = DMCopyBoundary(dm, gdm);CHKERRQ(ierr);
866*b0441da4SMatthew G. Knepley   ierr = DMCopyDisc(dm, gdm);CHKERRQ(ierr);
867cad26855SMatthew G. Knepley   gdm->setfromoptionscalled = dm->setfromoptionscalled;
868cd0c2139SMatthew G Knepley   *dmGhosted = gdm;
869cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
870cd0c2139SMatthew G Knepley }
871cd0c2139SMatthew G Knepley 
872607ab7a9SMatthew G. Knepley /*
873faedd622SMatthew G. Knepley   We are adding three kinds of points here:
874607ab7a9SMatthew G. Knepley     Replicated:     Copies of points which exist in the mesh, such as vertices identified across a fault
875faedd622SMatthew G. Knepley     Non-replicated: Points which exist on the fault, but are not replicated
876607ab7a9SMatthew G. Knepley     Hybrid:         Entirely new points, such as cohesive cells
877a6ae58d1SMatthew G. Knepley 
878a6ae58d1SMatthew G. Knepley   When creating subsequent cohesive cells, we shift the old hybrid cells to the end of the numbering at
879a6ae58d1SMatthew G. Knepley   each depth so that the new split/hybrid points can be inserted as a block.
880607ab7a9SMatthew G. Knepley */
8817db7e0a7SMatthew G. Knepley static PetscErrorCode DMPlexConstructCohesiveCells_Internal(DM dm, DMLabel label, DMLabel splitLabel, DM sdm)
882cd0c2139SMatthew G Knepley {
883cd0c2139SMatthew G Knepley   MPI_Comm         comm;
884607ab7a9SMatthew G. Knepley   IS               valueIS;
885607ab7a9SMatthew G. Knepley   PetscInt         numSP = 0;       /* The number of depths for which we have replicated points */
886607ab7a9SMatthew G. Knepley   const PetscInt  *values;          /* List of depths for which we have replicated points */
88718c5995bSMatthew G. Knepley   IS              *splitIS;
88818c5995bSMatthew G. Knepley   IS              *unsplitIS;
889607ab7a9SMatthew G. Knepley   PetscInt        *numSplitPoints;     /* The number of replicated points at each depth */
89018c5995bSMatthew G. Knepley   PetscInt        *numUnsplitPoints;   /* The number of non-replicated points at each depth which still give rise to hybrid points */
89136dbac82SMatthew G. Knepley   PetscInt        *numHybridPoints;    /* The number of new hybrid points at each depth */
89236dbac82SMatthew G. Knepley   PetscInt        *numHybridPointsOld; /* The number of existing hybrid points at each depth */
893607ab7a9SMatthew G. Knepley   const PetscInt **splitPoints;        /* Replicated points for each depth */
89418c5995bSMatthew G. Knepley   const PetscInt **unsplitPoints;      /* Non-replicated points for each depth */
895cd0c2139SMatthew G Knepley   PetscSection     coordSection;
896cd0c2139SMatthew G Knepley   Vec              coordinates;
897cd0c2139SMatthew G Knepley   PetscScalar     *coords;
898a6ae58d1SMatthew G. Knepley   PetscInt        *depthMax;           /* The first hybrid point at each depth in the original mesh */
899a6ae58d1SMatthew G. Knepley   PetscInt        *depthEnd;           /* The point limit at each depth in the original mesh */
900607ab7a9SMatthew G. Knepley   PetscInt        *depthShift;         /* Number of replicated+hybrid points at each depth */
901607ab7a9SMatthew G. Knepley   PetscInt        *pMaxNew;            /* The first replicated point at each depth in the new mesh, hybrids come after this */
902607ab7a9SMatthew G. Knepley   PetscInt        *coneNew, *coneONew, *supportNew;
90318c5995bSMatthew G. Knepley   PetscInt         shift = 100, shift2 = 200, depth = 0, dep, dim, d, sp, maxConeSize, maxSupportSize, maxConeSizeNew, maxSupportSizeNew, numLabels, vStart, vEnd, pEnd, p, v;
904cd0c2139SMatthew G Knepley   PetscErrorCode   ierr;
905cd0c2139SMatthew G Knepley 
906cd0c2139SMatthew G Knepley   PetscFunctionBegin;
907cd0c2139SMatthew G Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
908c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
909607ab7a9SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
910fd4b9f15SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
911cd0c2139SMatthew G Knepley   /* Count split points and add cohesive cells */
912607ab7a9SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, &maxSupportSize);CHKERRQ(ierr);
9132582d50cSToby Isaac   ierr = PetscMalloc5(depth+1,&depthMax,depth+1,&depthEnd,2*(depth+1),&depthShift,depth+1,&pMaxNew,depth+1,&numHybridPointsOld);CHKERRQ(ierr);
914dcca6d9dSJed 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);
915a6ae58d1SMatthew 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);
916607ab7a9SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
917607ab7a9SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, d, NULL, &pMaxNew[d]);CHKERRQ(ierr);
918a6ae58d1SMatthew G. Knepley     depthEnd[d]           = pMaxNew[d];
919a6ae58d1SMatthew G. Knepley     depthMax[d]           = depthMax[d] < 0 ? depthEnd[d] : depthMax[d];
920607ab7a9SMatthew G. Knepley     numSplitPoints[d]     = 0;
92118c5995bSMatthew G. Knepley     numUnsplitPoints[d]   = 0;
922607ab7a9SMatthew G. Knepley     numHybridPoints[d]    = 0;
923a6ae58d1SMatthew G. Knepley     numHybridPointsOld[d] = depthMax[d] < 0 ? 0 : depthEnd[d] - depthMax[d];
924607ab7a9SMatthew G. Knepley     splitPoints[d]        = NULL;
92518c5995bSMatthew G. Knepley     unsplitPoints[d]      = NULL;
92618c5995bSMatthew G. Knepley     splitIS[d]            = NULL;
92718c5995bSMatthew G. Knepley     unsplitIS[d]          = NULL;
92859eef20bSToby Isaac     /* we are shifting the existing hybrid points with the stratum behind them, so
92959eef20bSToby Isaac      * the split comes at the end of the normal points, i.e., at depthMax[d] */
93059eef20bSToby Isaac     depthShift[2*d]       = depthMax[d];
93159eef20bSToby Isaac     depthShift[2*d+1]     = 0;
932607ab7a9SMatthew G. Knepley   }
933cd0c2139SMatthew G Knepley   if (label) {
934cd0c2139SMatthew G Knepley     ierr = DMLabelGetValueIS(label, &valueIS);CHKERRQ(ierr);
935cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(valueIS, &numSP);CHKERRQ(ierr);
936cd0c2139SMatthew G Knepley     ierr = ISGetIndices(valueIS, &values);CHKERRQ(ierr);
937cd0c2139SMatthew G Knepley   }
938cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
939cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
940cd0c2139SMatthew G Knepley 
941cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
94218c5995bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, dep, &splitIS[dep]);CHKERRQ(ierr);
94318c5995bSMatthew G. Knepley     if (splitIS[dep]) {
94418c5995bSMatthew G. Knepley       ierr = ISGetLocalSize(splitIS[dep], &numSplitPoints[dep]);CHKERRQ(ierr);
94518c5995bSMatthew G. Knepley       ierr = ISGetIndices(splitIS[dep], &splitPoints[dep]);CHKERRQ(ierr);
94618c5995bSMatthew G. Knepley     }
94718c5995bSMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, shift2+dep, &unsplitIS[dep]);CHKERRQ(ierr);
94818c5995bSMatthew G. Knepley     if (unsplitIS[dep]) {
94918c5995bSMatthew G. Knepley       ierr = ISGetLocalSize(unsplitIS[dep], &numUnsplitPoints[dep]);CHKERRQ(ierr);
95018c5995bSMatthew G. Knepley       ierr = ISGetIndices(unsplitIS[dep], &unsplitPoints[dep]);CHKERRQ(ierr);
951cd0c2139SMatthew G Knepley     }
952cd0c2139SMatthew G Knepley   }
953607ab7a9SMatthew G. Knepley   /* Calculate number of hybrid points */
95418c5995bSMatthew 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   */
9552582d50cSToby Isaac   for (d = 0; d <= depth; ++d) depthShift[2*d+1]      = numSplitPoints[d] + numHybridPoints[d];
9563d3eaba7SBarry Smith   ierr = DMPlexShiftPointSetUp_Internal(depth,depthShift);CHKERRQ(ierr);
95759eef20bSToby Isaac   /* the end of the points in this stratum that come before the new points:
95859eef20bSToby Isaac    * shifting pMaxNew[d] gets the new start of the next stratum, then count back the old hybrid points and the newly
95959eef20bSToby Isaac    * added points */
9602582d50cSToby Isaac   for (d = 0; d <= depth; ++d) pMaxNew[d]             = DMPlexShiftPoint_Internal(pMaxNew[d],depth,depthShift) - (numHybridPointsOld[d] + numSplitPoints[d] + numHybridPoints[d]);
961cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSizes_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
962cd0c2139SMatthew G Knepley   /* Step 3: Set cone/support sizes for new points */
963cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
964cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
965cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
9662582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
9674c367dbcSMatthew G. Knepley       const PetscInt  splitp = p    + pMaxNew[dep];
968cd0c2139SMatthew G Knepley       const PetscInt *support;
9694c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, qn, qp, e;
970cd0c2139SMatthew G Knepley 
971cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
972cd0c2139SMatthew G Knepley       ierr = DMPlexSetConeSize(sdm, splitp, coneSize);CHKERRQ(ierr);
973cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
974cd0c2139SMatthew G Knepley       ierr = DMPlexSetSupportSize(sdm, splitp, supportSize);CHKERRQ(ierr);
975cd0c2139SMatthew G Knepley       if (dep == depth-1) {
9764c367dbcSMatthew G. Knepley         const PetscInt hybcell = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
9774c367dbcSMatthew G. Knepley 
978cd0c2139SMatthew G Knepley         /* Add cohesive cells, they are prisms */
9794c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybcell, 2 + coneSize);CHKERRQ(ierr);
980cd0c2139SMatthew G Knepley       } else if (dep == 0) {
9814c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
982cd0c2139SMatthew G Knepley 
983cd0c2139SMatthew G Knepley         ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
9844c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
985cd0c2139SMatthew G Knepley           PetscInt val;
986cd0c2139SMatthew G Knepley 
987cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
9884c367dbcSMatthew G. Knepley           if (val == 1) ++qf;
9894c367dbcSMatthew G. Knepley           if ((val == 1) || (val ==  (shift + 1))) ++qn;
9904c367dbcSMatthew G. Knepley           if ((val == 1) || (val == -(shift + 1))) ++qp;
991cd0c2139SMatthew G Knepley         }
9924c367dbcSMatthew G. Knepley         /* Split old vertex: Edges into original vertex and new cohesive edge */
9934c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qn+1);CHKERRQ(ierr);
9944c367dbcSMatthew G. Knepley         /* Split new vertex: Edges into split vertex and new cohesive edge */
9954c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, splitp, qp+1);CHKERRQ(ierr);
9964c367dbcSMatthew G. Knepley         /* Add hybrid edge */
9974c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybedge, 2);CHKERRQ(ierr);
9984c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybedge, qf);CHKERRQ(ierr);
999cd0c2139SMatthew G Knepley       } else if (dep == dim-2) {
10004c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
10014c367dbcSMatthew G. Knepley 
1002cd0c2139SMatthew G Knepley         ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
10034c367dbcSMatthew G. Knepley         for (e = 0, qn = 0, qp = 0, qf = 0; e < supportSize; ++e) {
1004cd0c2139SMatthew G Knepley           PetscInt val;
1005cd0c2139SMatthew G Knepley 
1006cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
10074c367dbcSMatthew G. Knepley           if (val == dim-1) ++qf;
10084c367dbcSMatthew G. Knepley           if ((val == dim-1) || (val ==  (shift + dim-1))) ++qn;
10094c367dbcSMatthew G. Knepley           if ((val == dim-1) || (val == -(shift + dim-1))) ++qp;
1010cd0c2139SMatthew G Knepley         }
10114c367dbcSMatthew G. Knepley         /* Split old edge: Faces into original edge and cohesive face (positive side?) */
10124c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qn+1);CHKERRQ(ierr);
10134c367dbcSMatthew G. Knepley         /* Split new edge: Faces into split edge and cohesive face (negative side?) */
10144c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, splitp, qp+1);CHKERRQ(ierr);
10154c367dbcSMatthew G. Knepley         /* Add hybrid face */
10164c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybface, 4);CHKERRQ(ierr);
10174c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybface, qf);CHKERRQ(ierr);
1018cd0c2139SMatthew G Knepley       }
1019cd0c2139SMatthew G Knepley     }
1020cd0c2139SMatthew G Knepley   }
102118c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
102218c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
102318c5995bSMatthew G. Knepley       const PetscInt  oldp   = unsplitPoints[dep][p];
10242582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
102518c5995bSMatthew G. Knepley       const PetscInt *support;
1026da1dd7e4SMatthew G. Knepley       PetscInt        coneSize, supportSize, qf, e, s;
102718c5995bSMatthew G. Knepley 
102818c5995bSMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
102918c5995bSMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
103039254ff6SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
103118c5995bSMatthew G. Knepley       if (dep == 0) {
103218c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
103318c5995bSMatthew G. Knepley 
103439254ff6SMatthew G. Knepley         /* Unsplit vertex: Edges into original vertex, split edges, and new cohesive edge twice */
103539254ff6SMatthew G. Knepley         for (s = 0, qf = 0; s < supportSize; ++s, ++qf) {
103639254ff6SMatthew G. Knepley           ierr = PetscFindInt(support[s], numSplitPoints[dep+1], splitPoints[dep+1], &e);CHKERRQ(ierr);
103739254ff6SMatthew G. Knepley           if (e >= 0) ++qf;
103839254ff6SMatthew G. Knepley         }
103939254ff6SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qf+2);CHKERRQ(ierr);
104018c5995bSMatthew G. Knepley         /* Add hybrid edge */
104118c5995bSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybedge, 2);CHKERRQ(ierr);
1042e1757548SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1043e1757548SMatthew G. Knepley           PetscInt val;
1044e1757548SMatthew G. Knepley 
1045e1757548SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1046e1757548SMatthew G. Knepley           /* Split and unsplit edges produce hybrid faces */
1047da1dd7e4SMatthew G. Knepley           if (val == 1) ++qf;
1048da1dd7e4SMatthew G. Knepley           if (val == (shift2 + 1)) ++qf;
1049e1757548SMatthew G. Knepley         }
1050e1757548SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybedge, qf);CHKERRQ(ierr);
105118c5995bSMatthew G. Knepley       } else if (dep == dim-2) {
105218c5995bSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
1053cd0c2139SMatthew G Knepley         PetscInt       val;
1054cd0c2139SMatthew G Knepley 
1055da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1056cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1057da1dd7e4SMatthew G. Knepley           if (val == dim-1) qf += 2;
1058da1dd7e4SMatthew G. Knepley           else              ++qf;
1059cd0c2139SMatthew G Knepley         }
106018c5995bSMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and cohesive face twice */
1061da1dd7e4SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, newp, qf+2);CHKERRQ(ierr);
106218c5995bSMatthew G. Knepley         /* Add hybrid face */
1063da1dd7e4SMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
1064da1dd7e4SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1065da1dd7e4SMatthew G. Knepley           if (val == dim-1) ++qf;
1066da1dd7e4SMatthew G. Knepley         }
106718c5995bSMatthew G. Knepley         ierr = DMPlexSetConeSize(sdm, hybface, 4);CHKERRQ(ierr);
1068da1dd7e4SMatthew G. Knepley         ierr = DMPlexSetSupportSize(sdm, hybface, qf);CHKERRQ(ierr);
1069cd0c2139SMatthew G Knepley       }
1070cd0c2139SMatthew G Knepley     }
1071cd0c2139SMatthew G Knepley   }
1072cd0c2139SMatthew G Knepley   /* Step 4: Setup split DM */
1073cd0c2139SMatthew G Knepley   ierr = DMSetUp(sdm);CHKERRQ(ierr);
1074cd0c2139SMatthew G Knepley   ierr = DMPlexShiftPoints_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
1075dcbb62e8SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(sdm, &maxConeSizeNew, &maxSupportSizeNew);CHKERRQ(ierr);
1076dcca6d9dSJed Brown   ierr = PetscMalloc3(PetscMax(maxConeSize, maxConeSizeNew)*3,&coneNew,PetscMax(maxConeSize, maxConeSizeNew)*3,&coneONew,PetscMax(maxSupportSize, maxSupportSizeNew),&supportNew);CHKERRQ(ierr);
1077cd0c2139SMatthew G Knepley   /* Step 6: Set cones and supports for new points */
1078cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1079cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
1080cd0c2139SMatthew G Knepley       const PetscInt  oldp   = splitPoints[dep][p];
10812582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
10824c367dbcSMatthew G. Knepley       const PetscInt  splitp = p    + pMaxNew[dep];
1083cd0c2139SMatthew G Knepley       const PetscInt *cone, *support, *ornt;
10844c367dbcSMatthew G. Knepley       PetscInt        coneSize, supportSize, q, qf, qn, qp, v, e, s;
1085cd0c2139SMatthew G Knepley 
1086cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
1087cd0c2139SMatthew G Knepley       ierr = DMPlexGetCone(dm, oldp, &cone);CHKERRQ(ierr);
1088cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeOrientation(dm, oldp, &ornt);CHKERRQ(ierr);
1089cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
1090cd0c2139SMatthew G Knepley       ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
1091cd0c2139SMatthew G Knepley       if (dep == depth-1) {
109296a07cd0SMatthew G. Knepley         PetscBool       hasUnsplit = PETSC_FALSE;
10934c367dbcSMatthew G. Knepley         const PetscInt  hybcell    = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1094cd0c2139SMatthew G Knepley         const PetscInt *supportF;
1095cd0c2139SMatthew G Knepley 
1096cd0c2139SMatthew G Knepley         /* Split face:       copy in old face to new face to start */
1097cd0c2139SMatthew G Knepley         ierr = DMPlexGetSupport(sdm, newp,  &supportF);CHKERRQ(ierr);
1098cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, splitp, supportF);CHKERRQ(ierr);
1099cd0c2139SMatthew G Knepley         /* Split old face:   old vertices/edges in cone so no change */
1100cd0c2139SMatthew G Knepley         /* Split new face:   new vertices/edges in cone */
1101cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
11024c367dbcSMatthew G. Knepley           ierr = PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);CHKERRQ(ierr);
110318c5995bSMatthew G. Knepley           if (v < 0) {
110418c5995bSMatthew G. Knepley             ierr = PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
110518c5995bSMatthew 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);
11062582d50cSToby Isaac             coneNew[2+q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
110796a07cd0SMatthew G. Knepley             hasUnsplit   = PETSC_TRUE;
110818c5995bSMatthew G. Knepley           } else {
11094c367dbcSMatthew G. Knepley             coneNew[2+q] = v + pMaxNew[dep-1];
1110163235baSMatthew G. Knepley             if (dep > 1) {
1111163235baSMatthew G. Knepley               const PetscInt *econe;
1112163235baSMatthew G. Knepley               PetscInt        econeSize, r, vs, vu;
1113163235baSMatthew G. Knepley 
1114163235baSMatthew G. Knepley               ierr = DMPlexGetConeSize(dm, cone[q], &econeSize);CHKERRQ(ierr);
1115163235baSMatthew G. Knepley               ierr = DMPlexGetCone(dm, cone[q], &econe);CHKERRQ(ierr);
1116163235baSMatthew G. Knepley               for (r = 0; r < econeSize; ++r) {
1117163235baSMatthew G. Knepley                 ierr = PetscFindInt(econe[r], numSplitPoints[dep-2],   splitPoints[dep-2],   &vs);CHKERRQ(ierr);
1118163235baSMatthew G. Knepley                 ierr = PetscFindInt(econe[r], numUnsplitPoints[dep-2], unsplitPoints[dep-2], &vu);CHKERRQ(ierr);
1119163235baSMatthew G. Knepley                 if (vs >= 0) continue;
1120163235baSMatthew 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);
1121163235baSMatthew G. Knepley                 hasUnsplit   = PETSC_TRUE;
1122163235baSMatthew G. Knepley               }
1123163235baSMatthew G. Knepley             }
1124cd0c2139SMatthew G Knepley           }
1125cd0c2139SMatthew G Knepley         }
1126cd0c2139SMatthew G Knepley         ierr = DMPlexSetCone(sdm, splitp, &coneNew[2]);CHKERRQ(ierr);
1127cd0c2139SMatthew G Knepley         ierr = DMPlexSetConeOrientation(sdm, splitp, ornt);CHKERRQ(ierr);
1128e537020bSMatthew G. Knepley         /* Face support */
1129cd0c2139SMatthew G Knepley         for (s = 0; s < supportSize; ++s) {
1130cd0c2139SMatthew G Knepley           PetscInt val;
1131cd0c2139SMatthew G Knepley 
1132cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[s], &val);CHKERRQ(ierr);
1133cd0c2139SMatthew G Knepley           if (val < 0) {
1134cd0c2139SMatthew G Knepley             /* Split old face:   Replace negative side cell with cohesive cell */
11354c367dbcSMatthew G. Knepley              ierr = DMPlexInsertSupport(sdm, newp, s, hybcell);CHKERRQ(ierr);
1136cd0c2139SMatthew G Knepley           } else {
1137cd0c2139SMatthew G Knepley             /* Split new face:   Replace positive side cell with cohesive cell */
11384c367dbcSMatthew G. Knepley             ierr = DMPlexInsertSupport(sdm, splitp, s, hybcell);CHKERRQ(ierr);
1139e537020bSMatthew G. Knepley             /* Get orientation for cohesive face */
1140e537020bSMatthew G. Knepley             {
1141e537020bSMatthew G. Knepley               const PetscInt *ncone, *nconeO;
1142e537020bSMatthew G. Knepley               PetscInt        nconeSize, nc;
1143e537020bSMatthew G. Knepley 
1144e537020bSMatthew G. Knepley               ierr = DMPlexGetConeSize(dm, support[s], &nconeSize);CHKERRQ(ierr);
1145e537020bSMatthew G. Knepley               ierr = DMPlexGetCone(dm, support[s], &ncone);CHKERRQ(ierr);
1146e537020bSMatthew G. Knepley               ierr = DMPlexGetConeOrientation(dm, support[s], &nconeO);CHKERRQ(ierr);
1147e537020bSMatthew G. Knepley               for (nc = 0; nc < nconeSize; ++nc) {
1148e537020bSMatthew G. Knepley                 if (ncone[nc] == oldp) {
1149e537020bSMatthew G. Knepley                   coneONew[0] = nconeO[nc];
1150e537020bSMatthew G. Knepley                   break;
1151cd0c2139SMatthew G Knepley                 }
1152cd0c2139SMatthew G Knepley               }
1153e537020bSMatthew 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]);
1154e537020bSMatthew G. Knepley             }
1155e537020bSMatthew G. Knepley           }
1156e537020bSMatthew G. Knepley         }
11574c367dbcSMatthew G. Knepley         /* Cohesive cell:    Old and new split face, then new cohesive faces */
1158fd4b9f15SMatthew G. Knepley         coneNew[0]  = newp;   /* Extracted negative side orientation above */
11594c367dbcSMatthew G. Knepley         coneNew[1]  = splitp;
11604c367dbcSMatthew G. Knepley         coneONew[1] = coneONew[0];
1161e537020bSMatthew G. Knepley         for (q = 0; q < coneSize; ++q) {
116218c5995bSMatthew G. Knepley           ierr = PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);CHKERRQ(ierr);
116318c5995bSMatthew G. Knepley           if (v < 0) {
116418c5995bSMatthew G. Knepley             ierr = PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
116518c5995bSMatthew G. Knepley             coneNew[2+q]  = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
116618c5995bSMatthew G. Knepley             coneONew[2+q] = 0;
116718c5995bSMatthew G. Knepley           } else {
116818c5995bSMatthew G. Knepley             coneNew[2+q]  = v + pMaxNew[dep] + numSplitPoints[dep];
116918c5995bSMatthew G. Knepley           }
1170e537020bSMatthew G. Knepley           coneONew[2+q] = 0;
1171e537020bSMatthew G. Knepley         }
11724c367dbcSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybcell, coneNew);CHKERRQ(ierr);
11734c367dbcSMatthew G. Knepley         ierr = DMPlexSetConeOrientation(sdm, hybcell, coneONew);CHKERRQ(ierr);
117496a07cd0SMatthew G. Knepley         /* Label the hybrid cells on the boundary of the split */
117596a07cd0SMatthew G. Knepley         if (hasUnsplit) {ierr = DMLabelSetValue(label, -hybcell, dim);CHKERRQ(ierr);}
1176cd0c2139SMatthew G Knepley       } else if (dep == 0) {
11774c367dbcSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1178cd0c2139SMatthew G Knepley 
1179cd0c2139SMatthew G Knepley         /* Split old vertex: Edges in old split faces and new cohesive edge */
11804c367dbcSMatthew G. Knepley         for (e = 0, qn = 0; e < supportSize; ++e) {
1181cd0c2139SMatthew G Knepley           PetscInt val;
1182cd0c2139SMatthew G Knepley 
1183cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1184cd0c2139SMatthew G Knepley           if ((val == 1) || (val == (shift + 1))) {
11852582d50cSToby Isaac             supportNew[qn++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1186cd0c2139SMatthew G Knepley           }
1187cd0c2139SMatthew G Knepley         }
11884c367dbcSMatthew G. Knepley         supportNew[qn] = hybedge;
1189cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
1190cd0c2139SMatthew G Knepley         /* Split new vertex: Edges in new split faces and new cohesive edge */
11914c367dbcSMatthew G. Knepley         for (e = 0, qp = 0; e < supportSize; ++e) {
1192cd0c2139SMatthew G Knepley           PetscInt val, edge;
1193cd0c2139SMatthew G Knepley 
1194cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1195cd0c2139SMatthew G Knepley           if (val == 1) {
11964c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);CHKERRQ(ierr);
1197cd0c2139SMatthew G Knepley             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
11984c367dbcSMatthew G. Knepley             supportNew[qp++] = edge + pMaxNew[dep+1];
1199cd0c2139SMatthew G Knepley           } else if (val == -(shift + 1)) {
12002582d50cSToby Isaac             supportNew[qp++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1201cd0c2139SMatthew G Knepley           }
1202cd0c2139SMatthew G Knepley         }
12034c367dbcSMatthew G. Knepley         supportNew[qp] = hybedge;
1204cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, splitp, supportNew);CHKERRQ(ierr);
12054c367dbcSMatthew G. Knepley         /* Hybrid edge:    Old and new split vertex */
1206cd0c2139SMatthew G Knepley         coneNew[0] = newp;
1207cd0c2139SMatthew G Knepley         coneNew[1] = splitp;
12084c367dbcSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybedge, coneNew);CHKERRQ(ierr);
12094c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
12104c367dbcSMatthew G. Knepley           PetscInt val, edge;
12114c367dbcSMatthew G. Knepley 
12124c367dbcSMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
12134c367dbcSMatthew G. Knepley           if (val == 1) {
12144c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);CHKERRQ(ierr);
12154c367dbcSMatthew G. Knepley             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
12164c367dbcSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2];
12174c367dbcSMatthew G. Knepley           }
12184c367dbcSMatthew G. Knepley         }
12194c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybedge, supportNew);CHKERRQ(ierr);
1220cd0c2139SMatthew G Knepley       } else if (dep == dim-2) {
12214c367dbcSMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
12224c367dbcSMatthew G. Knepley 
1223cd0c2139SMatthew G Knepley         /* Split old edge:   old vertices in cone so no change */
1224cd0c2139SMatthew G Knepley         /* Split new edge:   new vertices in cone */
1225cd0c2139SMatthew G Knepley         for (q = 0; q < coneSize; ++q) {
12264c367dbcSMatthew G. Knepley           ierr = PetscFindInt(cone[q], numSplitPoints[dep-1], splitPoints[dep-1], &v);CHKERRQ(ierr);
1227e1757548SMatthew G. Knepley           if (v < 0) {
1228e1757548SMatthew G. Knepley             ierr = PetscFindInt(cone[q], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
1229e1757548SMatthew 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);
12302582d50cSToby Isaac             coneNew[q] = DMPlexShiftPoint_Internal(cone[q], depth, depthShift) /*cone[q] + depthOffset[dep-1]*/;
1231e1757548SMatthew G. Knepley           } else {
12324c367dbcSMatthew G. Knepley             coneNew[q] = v + pMaxNew[dep-1];
1233cd0c2139SMatthew G Knepley           }
1234e1757548SMatthew G. Knepley         }
1235cd0c2139SMatthew G Knepley         ierr = DMPlexSetCone(sdm, splitp, coneNew);CHKERRQ(ierr);
1236cd0c2139SMatthew G Knepley         /* Split old edge: Faces in positive side cells and old split faces */
1237cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1238cd0c2139SMatthew G Knepley           PetscInt val;
1239cd0c2139SMatthew G Knepley 
1240cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
12414c367dbcSMatthew G. Knepley           if (val == dim-1) {
12422582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
12434c367dbcSMatthew G. Knepley           } else if (val == (shift + dim-1)) {
12442582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1245cd0c2139SMatthew G Knepley           }
1246cd0c2139SMatthew G Knepley         }
1247b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1248cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
1249cd0c2139SMatthew G Knepley         /* Split new edge: Faces in negative side cells and new split faces */
1250cd0c2139SMatthew G Knepley         for (e = 0, q = 0; e < supportSize; ++e) {
1251cd0c2139SMatthew G Knepley           PetscInt val, face;
1252cd0c2139SMatthew G Knepley 
1253cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
1254cd0c2139SMatthew G Knepley           if (val == dim-1) {
12554c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
1256cd0c2139SMatthew G Knepley             if (face < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[e]);
12574c367dbcSMatthew G. Knepley             supportNew[q++] = face + pMaxNew[dep+1];
1258cd0c2139SMatthew G Knepley           } else if (val == -(shift + dim-1)) {
12592582d50cSToby Isaac             supportNew[q++] = DMPlexShiftPoint_Internal(support[e], depth, depthShift) /*support[e] + depthOffset[dep+1]*/;
1260cd0c2139SMatthew G Knepley           }
1261cd0c2139SMatthew G Knepley         }
1262b279cd2aSMatthew G. Knepley         supportNew[q++] = p + pMaxNew[dep+1] + numSplitPoints[dep+1];
1263cd0c2139SMatthew G Knepley         ierr = DMPlexSetSupport(sdm, splitp, supportNew);CHKERRQ(ierr);
12644c367dbcSMatthew G. Knepley         /* Hybrid face */
12654c367dbcSMatthew G. Knepley         coneNew[0] = newp;
12664c367dbcSMatthew G. Knepley         coneNew[1] = splitp;
12674c367dbcSMatthew G. Knepley         for (v = 0; v < coneSize; ++v) {
12684c367dbcSMatthew G. Knepley           PetscInt vertex;
12694c367dbcSMatthew G. Knepley           ierr = PetscFindInt(cone[v], numSplitPoints[dep-1], splitPoints[dep-1], &vertex);CHKERRQ(ierr);
1270e1757548SMatthew G. Knepley           if (vertex < 0) {
1271e1757548SMatthew G. Knepley             ierr = PetscFindInt(cone[v], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &vertex);CHKERRQ(ierr);
1272e1757548SMatthew 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);
1273e1757548SMatthew G. Knepley             coneNew[2+v] = vertex + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1274e1757548SMatthew G. Knepley           } else {
12754c367dbcSMatthew G. Knepley             coneNew[2+v] = vertex + pMaxNew[dep] + numSplitPoints[dep];
12764c367dbcSMatthew G. Knepley           }
1277e1757548SMatthew G. Knepley         }
12784c367dbcSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybface, coneNew);CHKERRQ(ierr);
12794c367dbcSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
12804c367dbcSMatthew G. Knepley           PetscInt val, face;
12814c367dbcSMatthew G. Knepley 
12824c367dbcSMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
12834c367dbcSMatthew G. Knepley           if (val == dim-1) {
12844c367dbcSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
12854c367dbcSMatthew G. Knepley             if (face < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[e]);
12864c367dbcSMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+2] + numSplitPoints[dep+2];
12874c367dbcSMatthew G. Knepley           }
12884c367dbcSMatthew G. Knepley         }
12894c367dbcSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybface, supportNew);CHKERRQ(ierr);
1290cd0c2139SMatthew G Knepley       }
1291cd0c2139SMatthew G Knepley     }
1292cd0c2139SMatthew G Knepley   }
129318c5995bSMatthew G. Knepley   for (dep = 0; dep <= depth; ++dep) {
129418c5995bSMatthew G. Knepley     for (p = 0; p < numUnsplitPoints[dep]; ++p) {
129518c5995bSMatthew G. Knepley       const PetscInt  oldp   = unsplitPoints[dep][p];
12962582d50cSToby Isaac       const PetscInt  newp   = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*oldp + depthOffset[dep]*/;
129718c5995bSMatthew G. Knepley       const PetscInt *cone, *support, *ornt;
1298e1757548SMatthew G. Knepley       PetscInt        coneSize, supportSize, supportSizeNew, q, qf, e, f, s;
129918c5995bSMatthew G. Knepley 
130018c5995bSMatthew G. Knepley       ierr = DMPlexGetConeSize(dm, oldp, &coneSize);CHKERRQ(ierr);
130118c5995bSMatthew G. Knepley       ierr = DMPlexGetCone(dm, oldp, &cone);CHKERRQ(ierr);
130218c5995bSMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, oldp, &ornt);CHKERRQ(ierr);
130318c5995bSMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, oldp, &supportSize);CHKERRQ(ierr);
130418c5995bSMatthew G. Knepley       ierr = DMPlexGetSupport(dm, oldp, &support);CHKERRQ(ierr);
130518c5995bSMatthew G. Knepley       if (dep == 0) {
130618c5995bSMatthew G. Knepley         const PetscInt hybedge = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
130718c5995bSMatthew G. Knepley 
130818c5995bSMatthew G. Knepley         /* Unsplit vertex */
130918c5995bSMatthew G. Knepley         ierr = DMPlexGetSupportSize(sdm, newp, &supportSizeNew);CHKERRQ(ierr);
131018c5995bSMatthew G. Knepley         for (s = 0, q = 0; s < supportSize; ++s) {
13112582d50cSToby Isaac           supportNew[q++] = DMPlexShiftPoint_Internal(support[s], depth, depthShift) /*support[s] + depthOffset[dep+1]*/;
131218c5995bSMatthew G. Knepley           ierr = PetscFindInt(support[s], numSplitPoints[dep+1], splitPoints[dep+1], &e);CHKERRQ(ierr);
131318c5995bSMatthew G. Knepley           if (e >= 0) {
131418c5995bSMatthew G. Knepley             supportNew[q++] = e + pMaxNew[dep+1];
131518c5995bSMatthew G. Knepley           }
131618c5995bSMatthew G. Knepley         }
131718c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
131818c5995bSMatthew G. Knepley         supportNew[q++] = hybedge;
131918c5995bSMatthew G. Knepley         if (q != supportSizeNew) SETERRQ3(comm, PETSC_ERR_ARG_WRONG, "Support size %d != %d for vertex %d", q, supportSizeNew, newp);
132018c5995bSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
132118c5995bSMatthew G. Knepley         /* Hybrid edge */
132218c5995bSMatthew G. Knepley         coneNew[0] = newp;
132318c5995bSMatthew G. Knepley         coneNew[1] = newp;
132418c5995bSMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybedge, coneNew);CHKERRQ(ierr);
132518c5995bSMatthew G. Knepley         for (e = 0, qf = 0; e < supportSize; ++e) {
132618c5995bSMatthew G. Knepley           PetscInt val, edge;
132718c5995bSMatthew G. Knepley 
132818c5995bSMatthew G. Knepley           ierr = DMLabelGetValue(label, support[e], &val);CHKERRQ(ierr);
132918c5995bSMatthew G. Knepley           if (val == 1) {
133018c5995bSMatthew G. Knepley             ierr = PetscFindInt(support[e], numSplitPoints[dep+1], splitPoints[dep+1], &edge);CHKERRQ(ierr);
133118c5995bSMatthew G. Knepley             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a split edge", support[e]);
133218c5995bSMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2];
1333e1757548SMatthew G. Knepley           } else if  (val ==  (shift2 + 1)) {
1334e1757548SMatthew G. Knepley             ierr = PetscFindInt(support[e], numUnsplitPoints[dep+1], unsplitPoints[dep+1], &edge);CHKERRQ(ierr);
1335e1757548SMatthew G. Knepley             if (edge < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Edge %d is not a unsplit edge", support[e]);
1336e1757548SMatthew G. Knepley             supportNew[qf++] = edge + pMaxNew[dep+2] + numSplitPoints[dep+2] + numSplitPoints[dep+1];
133718c5995bSMatthew G. Knepley           }
133818c5995bSMatthew G. Knepley         }
133918c5995bSMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybedge, supportNew);CHKERRQ(ierr);
1340e1757548SMatthew G. Knepley       } else if (dep == dim-2) {
1341e1757548SMatthew G. Knepley         const PetscInt hybface = p + pMaxNew[dep+1] + numSplitPoints[dep+1] + numSplitPoints[dep];
1342e1757548SMatthew G. Knepley 
1343da1dd7e4SMatthew G. Knepley         /* Unsplit edge: Faces into original edge, split face, and hybrid face twice */
1344e1757548SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1345e1757548SMatthew G. Knepley           PetscInt val, face;
1346e1757548SMatthew G. Knepley 
1347e1757548SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[f], &val);CHKERRQ(ierr);
1348e1757548SMatthew G. Knepley           if (val == dim-1) {
1349e1757548SMatthew G. Knepley             ierr = PetscFindInt(support[f], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
1350e1757548SMatthew G. Knepley             if (face < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Face %d is not a split face", support[f]);
13512582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1352e1757548SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+1];
1353e1757548SMatthew G. Knepley           } else {
13542582d50cSToby Isaac             supportNew[qf++] = DMPlexShiftPoint_Internal(support[f], depth, depthShift) /*support[f] + depthOffset[dep+1]*/;
1355e1757548SMatthew G. Knepley           }
1356e1757548SMatthew G. Knepley         }
1357e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
1358e1757548SMatthew G. Knepley         supportNew[qf++] = hybface;
1359da1dd7e4SMatthew G. Knepley         ierr = DMPlexGetSupportSize(sdm, newp, &supportSizeNew);CHKERRQ(ierr);
1360da1dd7e4SMatthew 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);
1361e1757548SMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, newp, supportNew);CHKERRQ(ierr);
1362e1757548SMatthew G. Knepley         /* Add hybrid face */
1363e1757548SMatthew G. Knepley         coneNew[0] = newp;
1364212cc919SMatthew G. Knepley         coneNew[1] = newp;
1365e1757548SMatthew G. Knepley         ierr = PetscFindInt(cone[0], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
1366e1757548SMatthew G. Knepley         if (v < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Vertex %d is not an unsplit vertex", cone[0]);
1367212cc919SMatthew G. Knepley         coneNew[2] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1368e1757548SMatthew G. Knepley         ierr = PetscFindInt(cone[1], numUnsplitPoints[dep-1], unsplitPoints[dep-1], &v);CHKERRQ(ierr);
1369e1757548SMatthew G. Knepley         if (v < 0) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Vertex %d is not an unsplit vertex", cone[1]);
1370e1757548SMatthew G. Knepley         coneNew[3] = v + pMaxNew[dep] + numSplitPoints[dep] + numSplitPoints[dep-1];
1371e1757548SMatthew G. Knepley         ierr = DMPlexSetCone(sdm, hybface, coneNew);CHKERRQ(ierr);
1372da1dd7e4SMatthew G. Knepley         for (f = 0, qf = 0; f < supportSize; ++f) {
1373da1dd7e4SMatthew G. Knepley           PetscInt val, face;
1374da1dd7e4SMatthew G. Knepley 
1375da1dd7e4SMatthew G. Knepley           ierr = DMLabelGetValue(label, support[f], &val);CHKERRQ(ierr);
1376da1dd7e4SMatthew G. Knepley           if (val == dim-1) {
1377da1dd7e4SMatthew G. Knepley             ierr = PetscFindInt(support[f], numSplitPoints[dep+1], splitPoints[dep+1], &face);CHKERRQ(ierr);
1378da1dd7e4SMatthew G. Knepley             supportNew[qf++] = face + pMaxNew[dep+2] + numSplitPoints[dep+2];
1379da1dd7e4SMatthew G. Knepley           }
1380da1dd7e4SMatthew G. Knepley         }
1381da1dd7e4SMatthew G. Knepley         ierr = DMPlexGetSupportSize(sdm, hybface, &supportSizeNew);CHKERRQ(ierr);
1382da1dd7e4SMatthew 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);
1383e1757548SMatthew G. Knepley         ierr = DMPlexSetSupport(sdm, hybface, supportNew);CHKERRQ(ierr);
1384cd0c2139SMatthew G Knepley       }
1385cd0c2139SMatthew G Knepley     }
1386cd0c2139SMatthew G Knepley   }
1387cd0c2139SMatthew G Knepley   /* Step 6b: Replace split points in negative side cones */
1388cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1389cd0c2139SMatthew G Knepley     PetscInt        dep = values[sp];
1390cd0c2139SMatthew G Knepley     IS              pIS;
1391cd0c2139SMatthew G Knepley     PetscInt        numPoints;
1392cd0c2139SMatthew G Knepley     const PetscInt *points;
1393cd0c2139SMatthew G Knepley 
1394cd0c2139SMatthew G Knepley     if (dep >= 0) continue;
1395cd0c2139SMatthew G Knepley     ierr = DMLabelGetStratumIS(label, dep, &pIS);CHKERRQ(ierr);
1396cd0c2139SMatthew G Knepley     if (!pIS) continue;
1397cd0c2139SMatthew G Knepley     dep  = -dep - shift;
1398cd0c2139SMatthew G Knepley     ierr = ISGetLocalSize(pIS, &numPoints);CHKERRQ(ierr);
1399cd0c2139SMatthew G Knepley     ierr = ISGetIndices(pIS, &points);CHKERRQ(ierr);
1400cd0c2139SMatthew G Knepley     for (p = 0; p < numPoints; ++p) {
1401cd0c2139SMatthew G Knepley       const PetscInt  oldp = points[p];
14022582d50cSToby Isaac       const PetscInt  newp = DMPlexShiftPoint_Internal(oldp, depth, depthShift) /*depthOffset[dep] + oldp*/;
1403cd0c2139SMatthew G Knepley       const PetscInt *cone;
1404cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
140550cf782dSMatthew G. Knepley       /* PetscBool       replaced = PETSC_FALSE; */
1406cd0c2139SMatthew G Knepley 
1407cd0c2139SMatthew G Knepley       /* Negative edge: replace split vertex */
1408cd0c2139SMatthew G Knepley       /* Negative cell: replace split face */
1409cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(sdm, newp, &coneSize);CHKERRQ(ierr);
1410cd0c2139SMatthew G Knepley       ierr = DMPlexGetCone(sdm, newp, &cone);CHKERRQ(ierr);
1411cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
1412e38fbfedSToby Isaac         const PetscInt coldp = DMPlexShiftPointInverse_Internal(cone[c],depth,depthShift);
1413cd0c2139SMatthew G Knepley         PetscInt       csplitp, cp, val;
1414cd0c2139SMatthew G Knepley 
1415cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(label, coldp, &val);CHKERRQ(ierr);
1416cd0c2139SMatthew G Knepley         if (val == dep-1) {
1417cd0c2139SMatthew G Knepley           ierr = PetscFindInt(coldp, numSplitPoints[dep-1], splitPoints[dep-1], &cp);CHKERRQ(ierr);
1418cd0c2139SMatthew G Knepley           if (cp < 0) SETERRQ2(comm, PETSC_ERR_ARG_WRONG, "Point %d is not a split point of dimension %d", oldp, dep-1);
1419cd0c2139SMatthew G Knepley           csplitp  = pMaxNew[dep-1] + cp;
1420cd0c2139SMatthew G Knepley           ierr     = DMPlexInsertCone(sdm, newp, c, csplitp);CHKERRQ(ierr);
142150cf782dSMatthew G. Knepley           /* replaced = PETSC_TRUE; */
1422cd0c2139SMatthew G Knepley         }
1423cd0c2139SMatthew G Knepley       }
14244a189a86SMatthew G. Knepley       /* Cells with only a vertex or edge on the submesh have no replacement */
14254a189a86SMatthew G. Knepley       /* if (!replaced) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "The cone of point %d does not contain split points", oldp); */
1426cd0c2139SMatthew G Knepley     }
1427cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(pIS, &points);CHKERRQ(ierr);
1428cd0c2139SMatthew G Knepley     ierr = ISDestroy(&pIS);CHKERRQ(ierr);
1429cd0c2139SMatthew G Knepley   }
1430fa8e8ae5SToby Isaac   /* Step 7: Coordinates */
1431cd0c2139SMatthew G Knepley   ierr = DMPlexShiftCoordinates_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
143269d8a9ceSMatthew G. Knepley   ierr = DMGetCoordinateSection(sdm, &coordSection);CHKERRQ(ierr);
1433cd0c2139SMatthew G Knepley   ierr = DMGetCoordinatesLocal(sdm, &coordinates);CHKERRQ(ierr);
1434cd0c2139SMatthew G Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
1435cd0c2139SMatthew G Knepley   for (v = 0; v < (numSplitPoints ? numSplitPoints[0] : 0); ++v) {
14362582d50cSToby Isaac     const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[0][v], depth, depthShift) /*depthOffset[0] + splitPoints[0][v]*/;
1437cd0c2139SMatthew G Knepley     const PetscInt splitp = pMaxNew[0] + v;
1438cd0c2139SMatthew G Knepley     PetscInt       dof, off, soff, d;
1439cd0c2139SMatthew G Knepley 
1440cd0c2139SMatthew G Knepley     ierr = PetscSectionGetDof(coordSection, newp, &dof);CHKERRQ(ierr);
1441cd0c2139SMatthew G Knepley     ierr = PetscSectionGetOffset(coordSection, newp, &off);CHKERRQ(ierr);
1442cd0c2139SMatthew G Knepley     ierr = PetscSectionGetOffset(coordSection, splitp, &soff);CHKERRQ(ierr);
1443cd0c2139SMatthew G Knepley     for (d = 0; d < dof; ++d) coords[soff+d] = coords[off+d];
1444cd0c2139SMatthew G Knepley   }
1445cd0c2139SMatthew G Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
1446fa8e8ae5SToby Isaac   /* Step 8: SF, if I can figure this out we can split the mesh in parallel */
1447cd0c2139SMatthew G Knepley   ierr = DMPlexShiftSF_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
1448fa8e8ae5SToby Isaac   /* Step 9: Labels */
1449cd0c2139SMatthew G Knepley   ierr = DMPlexShiftLabels_Internal(dm, depthShift, sdm);CHKERRQ(ierr);
1450c58f1c22SToby Isaac   ierr = DMGetNumLabels(sdm, &numLabels);CHKERRQ(ierr);
1451cd0c2139SMatthew G Knepley   for (dep = 0; dep <= depth; ++dep) {
1452cd0c2139SMatthew G Knepley     for (p = 0; p < numSplitPoints[dep]; ++p) {
14532582d50cSToby Isaac       const PetscInt newp   = DMPlexShiftPoint_Internal(splitPoints[dep][p], depth, depthShift) /*depthOffset[dep] + splitPoints[dep][p]*/;
1454cd0c2139SMatthew G Knepley       const PetscInt splitp = pMaxNew[dep] + p;
1455cd0c2139SMatthew G Knepley       PetscInt       l;
1456cd0c2139SMatthew G Knepley 
14577db7e0a7SMatthew G. Knepley       if (splitLabel) {
14587db7e0a7SMatthew G. Knepley         const PetscInt val = 100 + dep;
14597db7e0a7SMatthew G. Knepley 
14607db7e0a7SMatthew G. Knepley         ierr = DMLabelSetValue(splitLabel, newp,    val);CHKERRQ(ierr);
14617db7e0a7SMatthew G. Knepley         ierr = DMLabelSetValue(splitLabel, splitp, -val);CHKERRQ(ierr);
14627db7e0a7SMatthew G. Knepley       }
1463cd0c2139SMatthew G Knepley       for (l = 0; l < numLabels; ++l) {
1464cd0c2139SMatthew G Knepley         DMLabel     mlabel;
1465cd0c2139SMatthew G Knepley         const char *lname;
1466cd0c2139SMatthew G Knepley         PetscInt    val;
14679a356370SMatthew G. Knepley         PetscBool   isDepth;
1468cd0c2139SMatthew G Knepley 
1469c58f1c22SToby Isaac         ierr = DMGetLabelName(sdm, l, &lname);CHKERRQ(ierr);
14709a356370SMatthew G. Knepley         ierr = PetscStrcmp(lname, "depth", &isDepth);CHKERRQ(ierr);
14719a356370SMatthew G. Knepley         if (isDepth) continue;
1472c58f1c22SToby Isaac         ierr = DMGetLabel(sdm, lname, &mlabel);CHKERRQ(ierr);
1473cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(mlabel, newp, &val);CHKERRQ(ierr);
1474cd0c2139SMatthew G Knepley         if (val >= 0) {
1475cd0c2139SMatthew G Knepley           ierr = DMLabelSetValue(mlabel, splitp, val);CHKERRQ(ierr);
1476cd0c2139SMatthew G Knepley         }
1477cd0c2139SMatthew G Knepley       }
1478cd0c2139SMatthew G Knepley     }
1479cd0c2139SMatthew G Knepley   }
1480cd0c2139SMatthew G Knepley   for (sp = 0; sp < numSP; ++sp) {
1481cd0c2139SMatthew G Knepley     const PetscInt dep = values[sp];
1482cd0c2139SMatthew G Knepley 
1483cd0c2139SMatthew G Knepley     if ((dep < 0) || (dep > depth)) continue;
148418c5995bSMatthew G. Knepley     if (splitIS[dep]) {ierr = ISRestoreIndices(splitIS[dep], &splitPoints[dep]);CHKERRQ(ierr);}
148518c5995bSMatthew G. Knepley     ierr = ISDestroy(&splitIS[dep]);CHKERRQ(ierr);
148618c5995bSMatthew G. Knepley     if (unsplitIS[dep]) {ierr = ISRestoreIndices(unsplitIS[dep], &unsplitPoints[dep]);CHKERRQ(ierr);}
148718c5995bSMatthew G. Knepley     ierr = ISDestroy(&unsplitIS[dep]);CHKERRQ(ierr);
1488cd0c2139SMatthew G Knepley   }
1489cd0c2139SMatthew G Knepley   if (label) {
1490cd0c2139SMatthew G Knepley     ierr = ISRestoreIndices(valueIS, &values);CHKERRQ(ierr);
1491cd0c2139SMatthew G Knepley     ierr = ISDestroy(&valueIS);CHKERRQ(ierr);
1492cd0c2139SMatthew G Knepley   }
14930d4d4d06SMatthew G. Knepley   for (d = 0; d <= depth; ++d) {
14940d4d4d06SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(sdm, d, NULL, &pEnd);CHKERRQ(ierr);
149536dbac82SMatthew G. Knepley     pMaxNew[d] = pEnd - numHybridPoints[d] - numHybridPointsOld[d];
14960d4d4d06SMatthew G. Knepley   }
14970e49e2e2SMatthew 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);
1498dcbb62e8SMatthew G. Knepley   ierr = PetscFree3(coneNew, coneONew, supportNew);CHKERRQ(ierr);
14992582d50cSToby Isaac   ierr = PetscFree5(depthMax, depthEnd, depthShift, pMaxNew, numHybridPointsOld);CHKERRQ(ierr);
150018c5995bSMatthew G. Knepley   ierr = PetscFree7(splitIS, unsplitIS, numSplitPoints, numUnsplitPoints, numHybridPoints, splitPoints, unsplitPoints);CHKERRQ(ierr);
1501cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1502cd0c2139SMatthew G Knepley }
1503cd0c2139SMatthew G Knepley 
1504cd0c2139SMatthew G Knepley /*@C
1505cd0c2139SMatthew G Knepley   DMPlexConstructCohesiveCells - Construct cohesive cells which split the face along an internal interface
1506cd0c2139SMatthew G Knepley 
1507cd0c2139SMatthew G Knepley   Collective on dm
1508cd0c2139SMatthew G Knepley 
1509cd0c2139SMatthew G Knepley   Input Parameters:
1510cd0c2139SMatthew G Knepley + dm - The original DM
151153156dfcSMatthew G. Knepley - label - The label specifying the boundary faces (this could be auto-generated)
1512cd0c2139SMatthew G Knepley 
1513cd0c2139SMatthew G Knepley   Output Parameters:
15147db7e0a7SMatthew G. Knepley + splitLabel - The label containing the split points, or NULL if no output is desired
1515cd0c2139SMatthew G Knepley - dmSplit - The new DM
1516cd0c2139SMatthew G Knepley 
1517cd0c2139SMatthew G Knepley   Level: developer
1518cd0c2139SMatthew G Knepley 
15192be2b188SMatthew G Knepley .seealso: DMCreate(), DMPlexLabelCohesiveComplete()
1520cd0c2139SMatthew G Knepley @*/
15217db7e0a7SMatthew G. Knepley PetscErrorCode DMPlexConstructCohesiveCells(DM dm, DMLabel label, DMLabel splitLabel, DM *dmSplit)
1522cd0c2139SMatthew G Knepley {
1523cd0c2139SMatthew G Knepley   DM             sdm;
1524cd0c2139SMatthew G Knepley   PetscInt       dim;
1525cd0c2139SMatthew G Knepley   PetscErrorCode ierr;
1526cd0c2139SMatthew G Knepley 
1527cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1528cd0c2139SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
152953156dfcSMatthew G. Knepley   PetscValidPointer(dmSplit, 3);
1530cd0c2139SMatthew G Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), &sdm);CHKERRQ(ierr);
1531cd0c2139SMatthew G Knepley   ierr = DMSetType(sdm, DMPLEX);CHKERRQ(ierr);
1532c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1533c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(sdm, dim);CHKERRQ(ierr);
1534cd0c2139SMatthew G Knepley   switch (dim) {
1535cd0c2139SMatthew G Knepley   case 2:
1536cd0c2139SMatthew G Knepley   case 3:
15377db7e0a7SMatthew G. Knepley     ierr = DMPlexConstructCohesiveCells_Internal(dm, label, splitLabel, sdm);CHKERRQ(ierr);
1538cd0c2139SMatthew G Knepley     break;
1539cd0c2139SMatthew G Knepley   default:
1540cd0c2139SMatthew G Knepley     SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot construct cohesive cells for dimension %d", dim);
1541cd0c2139SMatthew G Knepley   }
1542cd0c2139SMatthew G Knepley   *dmSplit = sdm;
1543cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1544cd0c2139SMatthew G Knepley }
1545cd0c2139SMatthew G Knepley 
15460f66a230SMatthew G. Knepley /* Returns the side of the surface for a given cell with a face on the surface */
15470f66a230SMatthew G. Knepley static PetscErrorCode GetSurfaceSide_Static(DM dm, DM subdm, PetscInt numSubpoints, const PetscInt *subpoints, PetscInt cell, PetscInt face, PetscBool *pos)
15480f66a230SMatthew G. Knepley {
15490f66a230SMatthew G. Knepley   const PetscInt *cone, *ornt;
15500f66a230SMatthew G. Knepley   PetscInt        dim, coneSize, c;
15510f66a230SMatthew G. Knepley   PetscErrorCode  ierr;
15520f66a230SMatthew G. Knepley 
15530f66a230SMatthew G. Knepley   PetscFunctionBegin;
15540f66a230SMatthew G. Knepley   *pos = PETSC_TRUE;
1555c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
15560f66a230SMatthew G. Knepley   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
15570f66a230SMatthew G. Knepley   ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
15580f66a230SMatthew G. Knepley   ierr = DMPlexGetConeOrientation(dm, cell, &ornt);CHKERRQ(ierr);
15590f66a230SMatthew G. Knepley   for (c = 0; c < coneSize; ++c) {
15600f66a230SMatthew G. Knepley     if (cone[c] == face) {
15610f66a230SMatthew G. Knepley       PetscInt o = ornt[c];
15620f66a230SMatthew G. Knepley 
15630f66a230SMatthew G. Knepley       if (subdm) {
15640f66a230SMatthew G. Knepley         const PetscInt *subcone, *subornt;
15650f66a230SMatthew G. Knepley         PetscInt        subpoint, subface, subconeSize, sc;
15660f66a230SMatthew G. Knepley 
15670f66a230SMatthew G. Knepley         ierr = PetscFindInt(cell, numSubpoints, subpoints, &subpoint);CHKERRQ(ierr);
15680f66a230SMatthew G. Knepley         ierr = PetscFindInt(face, numSubpoints, subpoints, &subface);CHKERRQ(ierr);
15690f66a230SMatthew G. Knepley         ierr = DMPlexGetConeSize(subdm, subpoint, &subconeSize);CHKERRQ(ierr);
15700f66a230SMatthew G. Knepley         ierr = DMPlexGetCone(subdm, subpoint, &subcone);CHKERRQ(ierr);
15710f66a230SMatthew G. Knepley         ierr = DMPlexGetConeOrientation(subdm, subpoint, &subornt);CHKERRQ(ierr);
15720f66a230SMatthew G. Knepley         for (sc = 0; sc < subconeSize; ++sc) {
15730f66a230SMatthew G. Knepley           if (subcone[sc] == subface) {
15740f66a230SMatthew G. Knepley             o = subornt[0];
15750f66a230SMatthew G. Knepley             break;
15760f66a230SMatthew G. Knepley           }
15770f66a230SMatthew G. Knepley         }
15780f66a230SMatthew 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);
15790f66a230SMatthew G. Knepley       }
15800f66a230SMatthew G. Knepley       if (o >= 0) *pos = PETSC_TRUE;
15810f66a230SMatthew G. Knepley       else        *pos = PETSC_FALSE;
15820f66a230SMatthew G. Knepley       break;
15830f66a230SMatthew G. Knepley     }
15840f66a230SMatthew G. Knepley   }
15850f66a230SMatthew 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);
15860f66a230SMatthew G. Knepley   PetscFunctionReturn(0);
15870f66a230SMatthew G. Knepley }
15880f66a230SMatthew G. Knepley 
1589cd0c2139SMatthew G Knepley /*@
15900f66a230SMatthew G. Knepley   DMPlexLabelCohesiveComplete - Starting with a label marking points on an internal surface, we add all other mesh pieces
1591cd0c2139SMatthew G Knepley   to complete the surface
1592cd0c2139SMatthew G Knepley 
1593cd0c2139SMatthew G Knepley   Input Parameters:
1594cd0c2139SMatthew G Knepley + dm     - The DM
15950f66a230SMatthew G. Knepley . label  - A DMLabel marking the surface
15960f66a230SMatthew G. Knepley . blabel - A DMLabel marking the vertices on the boundary which will not be duplicated, or NULL to find them automatically
1597bb55d314SMatthew G. Knepley . flip   - Flag to flip the submesh normal and replace points on the other side
159847946fd8SMatthew G. Knepley - subdm  - The subDM associated with the label, or NULL
1599cd0c2139SMatthew G Knepley 
1600cd0c2139SMatthew G Knepley   Output Parameter:
1601cd0c2139SMatthew G Knepley . label - A DMLabel marking all surface points
1602cd0c2139SMatthew G Knepley 
16030f66a230SMatthew G. Knepley   Note: The vertices in blabel are called "unsplit" in the terminology from hybrid cell creation.
16040f66a230SMatthew G. Knepley 
1605cd0c2139SMatthew G Knepley   Level: developer
1606cd0c2139SMatthew G Knepley 
16072be2b188SMatthew G Knepley .seealso: DMPlexConstructCohesiveCells(), DMPlexLabelComplete()
1608cd0c2139SMatthew G Knepley @*/
16090f66a230SMatthew G. Knepley PetscErrorCode DMPlexLabelCohesiveComplete(DM dm, DMLabel label, DMLabel blabel, PetscBool flip, DM subdm)
1610cd0c2139SMatthew G Knepley {
1611d90583fdSMatthew G. Knepley   DMLabel         depthLabel;
16128630cb1aSMatthew G. Knepley   IS              dimIS, subpointIS = NULL, facePosIS, faceNegIS, crossEdgeIS = NULL;
161347946fd8SMatthew G. Knepley   const PetscInt *points, *subpoints;
1614bb55d314SMatthew G. Knepley   const PetscInt  rev   = flip ? -1 : 1;
1615370472baSMatthew G. Knepley   PetscInt       *pMax;
1616370472baSMatthew G. Knepley   PetscInt        shift = 100, shift2 = 200, dim, depth, pSize, dep, cStart, cEnd, cMax, fStart, fEnd, vStart, vEnd, numPoints, numSubpoints, p, val;
1617cd0c2139SMatthew G Knepley   PetscErrorCode  ierr;
1618cd0c2139SMatthew G Knepley 
1619cd0c2139SMatthew G Knepley   PetscFunctionBegin;
1620370472baSMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
1621370472baSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1622370472baSMatthew G. Knepley   pSize = PetscMax(depth, dim) + 1;
1623370472baSMatthew G. Knepley   ierr = PetscMalloc1(pSize,&pMax);CHKERRQ(ierr);
1624370472baSMatthew 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);
1625d90583fdSMatthew G. Knepley   ierr = DMPlexGetDepthLabel(dm, &depthLabel);CHKERRQ(ierr);
1626c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
162747946fd8SMatthew G. Knepley   if (subdm) {
162847946fd8SMatthew G. Knepley     ierr = DMPlexCreateSubpointIS(subdm, &subpointIS);CHKERRQ(ierr);
162947946fd8SMatthew G. Knepley     if (subpointIS) {
163047946fd8SMatthew G. Knepley       ierr = ISGetLocalSize(subpointIS, &numSubpoints);CHKERRQ(ierr);
163147946fd8SMatthew G. Knepley       ierr = ISGetIndices(subpointIS, &subpoints);CHKERRQ(ierr);
163247946fd8SMatthew G. Knepley     }
163347946fd8SMatthew G. Knepley   }
1634d7c8f101SMatthew G. Knepley   /* Mark cell on the fault, and its faces which touch the fault: cell orientation for face gives the side of the fault */
1635cd0c2139SMatthew G Knepley   ierr = DMLabelGetStratumIS(label, dim-1, &dimIS);CHKERRQ(ierr);
1636dc86033cSMatthew G. Knepley   if (!dimIS) {
1637dc86033cSMatthew G. Knepley     ierr = PetscFree(pMax);CHKERRQ(ierr);
16388630cb1aSMatthew G. Knepley     ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
1639dc86033cSMatthew G. Knepley     PetscFunctionReturn(0);
1640dc86033cSMatthew G. Knepley   }
1641cd0c2139SMatthew G Knepley   ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
1642cd0c2139SMatthew G Knepley   ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
1643d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault faces */
1644cd0c2139SMatthew G Knepley     const PetscInt *support;
1645cd0c2139SMatthew G Knepley     PetscInt        supportSize, s;
1646cd0c2139SMatthew G Knepley 
1647cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupportSize(dm, points[p], &supportSize);CHKERRQ(ierr);
1648c4419245SMatthew G. Knepley #if 0
1649c4419245SMatthew G. Knepley     if (supportSize != 2) {
1650c4419245SMatthew G. Knepley       const PetscInt *lp;
1651c4419245SMatthew G. Knepley       PetscInt        Nlp, pind;
1652c4419245SMatthew G. Knepley 
1653c4419245SMatthew G. Knepley       /* Check that for a cell with a single support face, that face is in the SF */
1654c4419245SMatthew G. Knepley       /*   THis check only works for the remote side. We would need root side information */
1655c4419245SMatthew G. Knepley       ierr = PetscSFGetGraph(dm->sf, NULL, &Nlp, &lp, NULL);CHKERRQ(ierr);
1656c4419245SMatthew G. Knepley       ierr = PetscFindInt(points[p], Nlp, lp, &pind);CHKERRQ(ierr);
1657c4419245SMatthew 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);
1658c4419245SMatthew G. Knepley     }
1659c4419245SMatthew G. Knepley #endif
1660cd0c2139SMatthew G Knepley     ierr = DMPlexGetSupport(dm, points[p], &support);CHKERRQ(ierr);
1661cd0c2139SMatthew G Knepley     for (s = 0; s < supportSize; ++s) {
16620f66a230SMatthew G. Knepley       const PetscInt *cone;
1663cd0c2139SMatthew G Knepley       PetscInt        coneSize, c;
16640f66a230SMatthew G. Knepley       PetscBool       pos;
1665cd0c2139SMatthew G Knepley 
16660f66a230SMatthew G. Knepley       ierr = GetSurfaceSide_Static(dm, subdm, numSubpoints, subpoints, support[s], points[p], &pos);CHKERRQ(ierr);
16670f66a230SMatthew G. Knepley       if (pos) {ierr = DMLabelSetValue(label, support[s],  rev*(shift+dim));CHKERRQ(ierr);}
16680f66a230SMatthew G. Knepley       else     {ierr = DMLabelSetValue(label, support[s], -rev*(shift+dim));CHKERRQ(ierr);}
16690f66a230SMatthew G. Knepley       if (rev < 0) pos = !pos ? PETSC_TRUE : PETSC_FALSE;
16700f66a230SMatthew G. Knepley       /* Put faces touching the fault in the label */
1671cd0c2139SMatthew G Knepley       ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
1672cd0c2139SMatthew G Knepley       ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
1673cd0c2139SMatthew G Knepley       for (c = 0; c < coneSize; ++c) {
1674cd0c2139SMatthew G Knepley         const PetscInt point = cone[c];
1675cd0c2139SMatthew G Knepley 
1676cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1677cd0c2139SMatthew G Knepley         if (val == -1) {
1678cd0c2139SMatthew G Knepley           PetscInt *closure = NULL;
1679cd0c2139SMatthew G Knepley           PetscInt  closureSize, cl;
1680cd0c2139SMatthew G Knepley 
1681cd0c2139SMatthew G Knepley           ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1682cd0c2139SMatthew G Knepley           for (cl = 0; cl < closureSize*2; cl += 2) {
1683cd0c2139SMatthew G Knepley             const PetscInt clp  = closure[cl];
1684a0541d8aSMatthew G. Knepley             PetscInt       bval = -1;
1685cd0c2139SMatthew G Knepley 
1686cd0c2139SMatthew G Knepley             ierr = DMLabelGetValue(label, clp, &val);CHKERRQ(ierr);
1687a0541d8aSMatthew G. Knepley             if (blabel) {ierr = DMLabelGetValue(blabel, clp, &bval);CHKERRQ(ierr);}
1688a0541d8aSMatthew G. Knepley             if ((val >= 0) && (val < dim-1) && (bval < 0)) {
1689cd0c2139SMatthew G Knepley               ierr = DMLabelSetValue(label, point, pos == PETSC_TRUE ? shift+dim-1 : -(shift+dim-1));CHKERRQ(ierr);
1690cd0c2139SMatthew G Knepley               break;
1691cd0c2139SMatthew G Knepley             }
1692cd0c2139SMatthew G Knepley           }
1693cd0c2139SMatthew G Knepley           ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1694cd0c2139SMatthew G Knepley         }
1695cd0c2139SMatthew G Knepley       }
1696cd0c2139SMatthew G Knepley     }
1697cd0c2139SMatthew G Knepley   }
16980f66a230SMatthew G. Knepley   ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
16990f66a230SMatthew G. Knepley   ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
170047946fd8SMatthew G. Knepley   if (subpointIS) {ierr = ISRestoreIndices(subpointIS, &subpoints);CHKERRQ(ierr);}
170147946fd8SMatthew G. Knepley   ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
1702a0541d8aSMatthew G. Knepley   /* Mark boundary points as unsplit */
170386200784SMatthew G. Knepley   if (blabel) {
1704a0541d8aSMatthew G. Knepley     ierr = DMLabelGetStratumIS(blabel, 1, &dimIS);CHKERRQ(ierr);
1705a0541d8aSMatthew G. Knepley     ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
1706a0541d8aSMatthew G. Knepley     ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
1707a0541d8aSMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
1708a0541d8aSMatthew G. Knepley       const PetscInt point = points[p];
1709a0541d8aSMatthew G. Knepley       PetscInt       val, bval;
1710a0541d8aSMatthew G. Knepley 
1711a0541d8aSMatthew G. Knepley       ierr = DMLabelGetValue(blabel, point, &bval);CHKERRQ(ierr);
1712a0541d8aSMatthew G. Knepley       if (bval >= 0) {
1713f7019248SMatthew G. Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1714f7019248SMatthew G. Knepley         if ((val < 0) || (val > dim)) {
1715f7019248SMatthew G. Knepley           /* This could be a point added from splitting a vertex on an adjacent fault, otherwise its just wrong */
1716f7019248SMatthew G. Knepley           ierr = DMLabelClearValue(blabel, point, bval);CHKERRQ(ierr);
1717f7019248SMatthew G. Knepley         }
1718f7019248SMatthew G. Knepley       }
1719f7019248SMatthew G. Knepley     }
1720f7019248SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
1721f7019248SMatthew G. Knepley       const PetscInt point = points[p];
1722f7019248SMatthew G. Knepley       PetscInt       val, bval;
1723f7019248SMatthew G. Knepley 
1724f7019248SMatthew G. Knepley       ierr = DMLabelGetValue(blabel, point, &bval);CHKERRQ(ierr);
1725f7019248SMatthew G. Knepley       if (bval >= 0) {
172686200784SMatthew G. Knepley         const PetscInt *cone,    *support;
172786200784SMatthew G. Knepley         PetscInt        coneSize, supportSize, s, valA, valB, valE;
172886200784SMatthew G. Knepley 
1729a0541d8aSMatthew G. Knepley         /* Mark as unsplit */
1730a0541d8aSMatthew G. Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1731a0541d8aSMatthew 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);
1732a0541d8aSMatthew G. Knepley         ierr = DMLabelClearValue(label, point, val);CHKERRQ(ierr);
1733a0541d8aSMatthew G. Knepley         ierr = DMLabelSetValue(label, point, shift2+val);CHKERRQ(ierr);
17342c06a818SMatthew G. Knepley         /* Check for cross-edge
17352c06a818SMatthew G. Knepley              A cross-edge has endpoints which are both on the boundary of the surface, but the edge itself is not. */
173686200784SMatthew G. Knepley         if (val != 0) continue;
173786200784SMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
173886200784SMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
173986200784SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
174086200784SMatthew G. Knepley           ierr = DMPlexGetCone(dm, support[s], &cone);CHKERRQ(ierr);
174186200784SMatthew G. Knepley           ierr = DMPlexGetConeSize(dm, support[s], &coneSize);CHKERRQ(ierr);
174286200784SMatthew G. Knepley           if (coneSize != 2) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Edge %D has %D vertices != 2", support[s], coneSize);
174386200784SMatthew G. Knepley           ierr = DMLabelGetValue(blabel, cone[0], &valA);CHKERRQ(ierr);
174486200784SMatthew G. Knepley           ierr = DMLabelGetValue(blabel, cone[1], &valB);CHKERRQ(ierr);
174586200784SMatthew G. Knepley           ierr = DMLabelGetValue(blabel, support[s], &valE);CHKERRQ(ierr);
174609816f77SMatthew G. Knepley           if ((valE < 0) && (valA >= 0) && (valB >= 0) && (cone[0] != cone[1])) {ierr = DMLabelSetValue(blabel, support[s], 2);CHKERRQ(ierr);}
174786200784SMatthew G. Knepley         }
1748a0541d8aSMatthew G. Knepley       }
1749a0541d8aSMatthew G. Knepley     }
1750a0541d8aSMatthew G. Knepley     ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
1751a0541d8aSMatthew G. Knepley     ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
1752a0541d8aSMatthew G. Knepley   }
1753cd0c2139SMatthew G Knepley   /* Search for other cells/faces/edges connected to the fault by a vertex */
1754c4d480a2SMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1755cd0c2139SMatthew G Knepley   ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1756d24f5ce5SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
1757d24f5ce5SMatthew G. Knepley   cMax = cMax < 0 ? cEnd : cMax;
17580f66a230SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd);CHKERRQ(ierr);
1759cd0c2139SMatthew G Knepley   ierr = DMLabelGetStratumIS(label, 0, &dimIS);CHKERRQ(ierr);
176086200784SMatthew G. Knepley   if (blabel) {ierr = DMLabelGetStratumIS(blabel, 2, &crossEdgeIS);CHKERRQ(ierr);}
176186200784SMatthew G. Knepley   if (dimIS && crossEdgeIS) {
176286200784SMatthew G. Knepley     IS vertIS = dimIS;
176386200784SMatthew G. Knepley 
176486200784SMatthew G. Knepley     ierr = ISExpand(vertIS, crossEdgeIS, &dimIS);CHKERRQ(ierr);
176586200784SMatthew G. Knepley     ierr = ISDestroy(&crossEdgeIS);CHKERRQ(ierr);
176686200784SMatthew G. Knepley     ierr = ISDestroy(&vertIS);CHKERRQ(ierr);
176786200784SMatthew G. Knepley   }
1768dc86033cSMatthew G. Knepley   if (!dimIS) {
1769dc86033cSMatthew G. Knepley     ierr = PetscFree(pMax);CHKERRQ(ierr);
1770dc86033cSMatthew G. Knepley     PetscFunctionReturn(0);
1771dc86033cSMatthew G. Knepley   }
1772cd0c2139SMatthew G Knepley   ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
1773cd0c2139SMatthew G Knepley   ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
1774d7c8f101SMatthew G. Knepley   for (p = 0; p < numPoints; ++p) { /* Loop over fault vertices */
1775cd0c2139SMatthew G Knepley     PetscInt *star = NULL;
177686200784SMatthew G. Knepley     PetscInt  starSize, s;
1777cd0c2139SMatthew G Knepley     PetscInt  again = 1;  /* 0: Finished 1: Keep iterating after a change 2: No change */
1778cd0c2139SMatthew G Knepley 
1779d4622b2cSMatthew G. Knepley     /* All points connected to the fault are inside a cell, so at the top level we will only check cells */
1780cd0c2139SMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
1781cd0c2139SMatthew G Knepley     while (again) {
1782cd0c2139SMatthew G Knepley       if (again > 1) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Could not classify all cells connected to the fault");
1783cd0c2139SMatthew G Knepley       again = 0;
1784cd0c2139SMatthew G Knepley       for (s = 0; s < starSize*2; s += 2) {
1785cd0c2139SMatthew G Knepley         const PetscInt  point = star[s];
1786cd0c2139SMatthew G Knepley         const PetscInt *cone;
1787cd0c2139SMatthew G Knepley         PetscInt        coneSize, c;
1788cd0c2139SMatthew G Knepley 
1789d24f5ce5SMatthew G. Knepley         if ((point < cStart) || (point >= cMax)) continue;
1790cd0c2139SMatthew G Knepley         ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1791cd0c2139SMatthew G Knepley         if (val != -1) continue;
1792d4622b2cSMatthew G. Knepley         again = again == 1 ? 1 : 2;
1793cd0c2139SMatthew G Knepley         ierr  = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
1794cd0c2139SMatthew G Knepley         ierr  = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
1795cd0c2139SMatthew G Knepley         for (c = 0; c < coneSize; ++c) {
1796cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, cone[c], &val);CHKERRQ(ierr);
1797cd0c2139SMatthew G Knepley           if (val != -1) {
1798d7c8f101SMatthew G. Knepley             const PetscInt *ccone;
1799166d9d0cSMatthew G. Knepley             PetscInt        cconeSize, cc, side;
1800d7c8f101SMatthew G. Knepley 
1801db8edaafSMatthew 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);
180237a6de01SMatthew G. Knepley             if (val > 0) side =  1;
180337a6de01SMatthew G. Knepley             else         side = -1;
180437a6de01SMatthew G. Knepley             ierr = DMLabelSetValue(label, point, side*(shift+dim));CHKERRQ(ierr);
1805d7c8f101SMatthew G. Knepley             /* Mark cell faces which touch the fault */
1806d7c8f101SMatthew G. Knepley             ierr = DMPlexGetConeSize(dm, point, &cconeSize);CHKERRQ(ierr);
1807d7c8f101SMatthew G. Knepley             ierr = DMPlexGetCone(dm, point, &ccone);CHKERRQ(ierr);
1808d7c8f101SMatthew G. Knepley             for (cc = 0; cc < cconeSize; ++cc) {
1809d7c8f101SMatthew G. Knepley               PetscInt *closure = NULL;
1810d7c8f101SMatthew G. Knepley               PetscInt  closureSize, cl;
1811d7c8f101SMatthew G. Knepley 
1812166d9d0cSMatthew G. Knepley               ierr = DMLabelGetValue(label, ccone[cc], &val);CHKERRQ(ierr);
1813166d9d0cSMatthew G. Knepley               if (val != -1) continue;
1814d7c8f101SMatthew G. Knepley               ierr = DMPlexGetTransitiveClosure(dm, ccone[cc], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1815d4622b2cSMatthew G. Knepley               for (cl = 0; cl < closureSize*2; cl += 2) {
1816d7c8f101SMatthew G. Knepley                 const PetscInt clp = closure[cl];
1817d7c8f101SMatthew G. Knepley 
1818d7c8f101SMatthew G. Knepley                 ierr = DMLabelGetValue(label, clp, &val);CHKERRQ(ierr);
1819d7c8f101SMatthew G. Knepley                 if (val == -1) continue;
1820d7c8f101SMatthew G. Knepley                 ierr = DMLabelSetValue(label, ccone[cc], side*(shift+dim-1));CHKERRQ(ierr);
182137a6de01SMatthew G. Knepley                 break;
182237a6de01SMatthew G. Knepley               }
1823d7c8f101SMatthew G. Knepley               ierr = DMPlexRestoreTransitiveClosure(dm, ccone[cc], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1824cd0c2139SMatthew G Knepley             }
1825cd0c2139SMatthew G Knepley             again = 1;
1826cd0c2139SMatthew G Knepley             break;
1827cd0c2139SMatthew G Knepley           }
1828cd0c2139SMatthew G Knepley         }
1829cd0c2139SMatthew G Knepley       }
1830cd0c2139SMatthew G Knepley     }
1831cd0c2139SMatthew G Knepley     /* Classify the rest by cell membership */
1832cd0c2139SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
1833cd0c2139SMatthew G Knepley       const PetscInt point = star[s];
1834cd0c2139SMatthew G Knepley 
1835cd0c2139SMatthew G Knepley       ierr = DMLabelGetValue(label, point, &val);CHKERRQ(ierr);
1836cd0c2139SMatthew G Knepley       if (val == -1) {
1837cd0c2139SMatthew G Knepley         PetscInt  *sstar = NULL;
1838cd0c2139SMatthew G Knepley         PetscInt   sstarSize, ss;
1839cd0c2139SMatthew G Knepley         PetscBool  marked = PETSC_FALSE;
1840cd0c2139SMatthew G Knepley 
1841cd0c2139SMatthew G Knepley         ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_FALSE, &sstarSize, &sstar);CHKERRQ(ierr);
1842cd0c2139SMatthew G Knepley         for (ss = 0; ss < sstarSize*2; ss += 2) {
1843cd0c2139SMatthew G Knepley           const PetscInt spoint = sstar[ss];
1844cd0c2139SMatthew G Knepley 
1845d24f5ce5SMatthew G. Knepley           if ((spoint < cStart) || (spoint >= cMax)) continue;
1846cd0c2139SMatthew G Knepley           ierr = DMLabelGetValue(label, spoint, &val);CHKERRQ(ierr);
1847cd0c2139SMatthew 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);
1848d90583fdSMatthew G. Knepley           ierr = DMLabelGetValue(depthLabel, point, &dep);CHKERRQ(ierr);
1849cd0c2139SMatthew G Knepley           if (val > 0) {
1850cd0c2139SMatthew G Knepley             ierr = DMLabelSetValue(label, point,   shift+dep);CHKERRQ(ierr);
1851cd0c2139SMatthew G Knepley           } else {
1852cd0c2139SMatthew G Knepley             ierr = DMLabelSetValue(label, point, -(shift+dep));CHKERRQ(ierr);
1853cd0c2139SMatthew G Knepley           }
1854cd0c2139SMatthew G Knepley           marked = PETSC_TRUE;
1855cd0c2139SMatthew G Knepley           break;
1856cd0c2139SMatthew G Knepley         }
1857cd0c2139SMatthew G Knepley         ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_FALSE, &sstarSize, &sstar);CHKERRQ(ierr);
1858370472baSMatthew G. Knepley         ierr = DMLabelGetValue(depthLabel, point, &dep);CHKERRQ(ierr);
1859370472baSMatthew G. Knepley         if (point < pMax[dep] && !marked) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Point %d could not be classified", point);
1860cd0c2139SMatthew G Knepley       }
1861cd0c2139SMatthew G Knepley     }
1862cd0c2139SMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
1863cd0c2139SMatthew G Knepley   }
1864cd0c2139SMatthew G Knepley   ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
1865cd0c2139SMatthew G Knepley   ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
186618c5995bSMatthew G. Knepley   /* If any faces touching the fault divide cells on either side, split them */
186718c5995bSMatthew G. Knepley   ierr = DMLabelGetStratumIS(label,   shift+dim-1,  &facePosIS);CHKERRQ(ierr);
186818c5995bSMatthew G. Knepley   ierr = DMLabelGetStratumIS(label, -(shift+dim-1), &faceNegIS);CHKERRQ(ierr);
186918c5995bSMatthew G. Knepley   ierr = ISExpand(facePosIS, faceNegIS, &dimIS);CHKERRQ(ierr);
187018c5995bSMatthew G. Knepley   ierr = ISDestroy(&facePosIS);CHKERRQ(ierr);
187118c5995bSMatthew G. Knepley   ierr = ISDestroy(&faceNegIS);CHKERRQ(ierr);
187218c5995bSMatthew G. Knepley   ierr = ISGetLocalSize(dimIS, &numPoints);CHKERRQ(ierr);
187318c5995bSMatthew G. Knepley   ierr = ISGetIndices(dimIS, &points);CHKERRQ(ierr);
187418c5995bSMatthew G. Knepley   for (p = 0; p < numPoints; ++p) {
187518c5995bSMatthew G. Knepley     const PetscInt  point = points[p];
187618c5995bSMatthew G. Knepley     const PetscInt *support;
187718c5995bSMatthew G. Knepley     PetscInt        supportSize, valA, valB;
187818c5995bSMatthew G. Knepley 
187918c5995bSMatthew G. Knepley     ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
188018c5995bSMatthew G. Knepley     if (supportSize != 2) continue;
188118c5995bSMatthew G. Knepley     ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
188218c5995bSMatthew G. Knepley     ierr = DMLabelGetValue(label, support[0], &valA);CHKERRQ(ierr);
188318c5995bSMatthew G. Knepley     ierr = DMLabelGetValue(label, support[1], &valB);CHKERRQ(ierr);
188418c5995bSMatthew G. Knepley     if ((valA == -1) || (valB == -1)) continue;
188518c5995bSMatthew G. Knepley     if (valA*valB > 0) continue;
188618c5995bSMatthew G. Knepley     /* Split the face */
188718c5995bSMatthew G. Knepley     ierr = DMLabelGetValue(label, point, &valA);CHKERRQ(ierr);
188818c5995bSMatthew G. Knepley     ierr = DMLabelClearValue(label, point, valA);CHKERRQ(ierr);
188918c5995bSMatthew G. Knepley     ierr = DMLabelSetValue(label, point, dim-1);CHKERRQ(ierr);
1890e1757548SMatthew G. Knepley     /* Label its closure:
1891e1757548SMatthew G. Knepley       unmarked: label as unsplit
1892e1757548SMatthew G. Knepley       incident: relabel as split
1893e1757548SMatthew G. Knepley       split:    do nothing
1894e1757548SMatthew G. Knepley     */
189518c5995bSMatthew G. Knepley     {
189618c5995bSMatthew G. Knepley       PetscInt *closure = NULL;
189718c5995bSMatthew G. Knepley       PetscInt  closureSize, cl;
189818c5995bSMatthew G. Knepley 
189918c5995bSMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
190018c5995bSMatthew G. Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
190118c5995bSMatthew G. Knepley         ierr = DMLabelGetValue(label, closure[cl], &valA);CHKERRQ(ierr);
19028771c1d3SMatthew G. Knepley         if (valA == -1) { /* Mark as unsplit */
190318c5995bSMatthew G. Knepley           ierr = DMLabelGetValue(depthLabel, closure[cl], &dep);CHKERRQ(ierr);
190418c5995bSMatthew G. Knepley           ierr = DMLabelSetValue(label, closure[cl], shift2+dep);CHKERRQ(ierr);
19058771c1d3SMatthew G. Knepley         } else if (((valA >= shift) && (valA < shift2)) || ((valA <= -shift) && (valA > -shift2))) {
1906e1757548SMatthew G. Knepley           ierr = DMLabelGetValue(depthLabel, closure[cl], &dep);CHKERRQ(ierr);
1907e1757548SMatthew G. Knepley           ierr = DMLabelClearValue(label, closure[cl], valA);CHKERRQ(ierr);
1908e1757548SMatthew G. Knepley           ierr = DMLabelSetValue(label, closure[cl], dep);CHKERRQ(ierr);
1909e1757548SMatthew G. Knepley         }
191018c5995bSMatthew G. Knepley       }
191118c5995bSMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
191218c5995bSMatthew G. Knepley     }
191318c5995bSMatthew G. Knepley   }
191418c5995bSMatthew G. Knepley   ierr = ISRestoreIndices(dimIS, &points);CHKERRQ(ierr);
191518c5995bSMatthew G. Knepley   ierr = ISDestroy(&dimIS);CHKERRQ(ierr);
1916370472baSMatthew G. Knepley   ierr = PetscFree(pMax);CHKERRQ(ierr);
1917cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
1918cd0c2139SMatthew G Knepley }
1919cd0c2139SMatthew G Knepley 
1920720e594eSMatthew G. Knepley /* Check that no cell have all vertices on the fault */
1921720e594eSMatthew G. Knepley PetscErrorCode DMPlexCheckValidSubmesh_Private(DM dm, DMLabel label, DM subdm)
1922720e594eSMatthew G. Knepley {
1923720e594eSMatthew G. Knepley   IS              subpointIS;
1924720e594eSMatthew G. Knepley   const PetscInt *dmpoints;
1925720e594eSMatthew G. Knepley   PetscInt        defaultValue, cStart, cEnd, c, vStart, vEnd;
1926720e594eSMatthew G. Knepley   PetscErrorCode  ierr;
1927720e594eSMatthew G. Knepley 
1928720e594eSMatthew G. Knepley   PetscFunctionBegin;
1929720e594eSMatthew G. Knepley   if (!label) PetscFunctionReturn(0);
1930720e594eSMatthew G. Knepley   ierr = DMLabelGetDefaultValue(label, &defaultValue);CHKERRQ(ierr);
1931720e594eSMatthew G. Knepley   ierr = DMPlexCreateSubpointIS(subdm, &subpointIS);CHKERRQ(ierr);
1932720e594eSMatthew G. Knepley   if (!subpointIS) PetscFunctionReturn(0);
1933720e594eSMatthew G. Knepley   ierr = DMPlexGetHeightStratum(subdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
1934720e594eSMatthew G. Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
1935720e594eSMatthew G. Knepley   ierr = ISGetIndices(subpointIS, &dmpoints);CHKERRQ(ierr);
1936720e594eSMatthew G. Knepley   for (c = cStart; c < cEnd; ++c) {
1937720e594eSMatthew G. Knepley     PetscBool invalidCell = PETSC_TRUE;
1938720e594eSMatthew G. Knepley     PetscInt *closure     = NULL;
1939720e594eSMatthew G. Knepley     PetscInt  closureSize, cl;
1940720e594eSMatthew G. Knepley 
1941720e594eSMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1942720e594eSMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
1943720e594eSMatthew G. Knepley       PetscInt value = 0;
1944720e594eSMatthew G. Knepley 
1945720e594eSMatthew G. Knepley       if ((closure[cl] < vStart) || (closure[cl] >= vEnd)) continue;
1946720e594eSMatthew G. Knepley       ierr = DMLabelGetValue(label, closure[cl], &value);CHKERRQ(ierr);
1947720e594eSMatthew G. Knepley       if (value == defaultValue) {invalidCell = PETSC_FALSE; break;}
1948720e594eSMatthew G. Knepley     }
1949720e594eSMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, dmpoints[c], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
1950720e594eSMatthew G. Knepley     if (invalidCell) {
1951720e594eSMatthew G. Knepley       ierr = ISRestoreIndices(subpointIS, &dmpoints);CHKERRQ(ierr);
1952720e594eSMatthew G. Knepley       ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
1953720e594eSMatthew G. Knepley       ierr = DMDestroy(&subdm);CHKERRQ(ierr);
1954720e594eSMatthew G. Knepley       SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Ambiguous submesh. Cell %D has all of its vertices on the submesh.", dmpoints[c]);
1955720e594eSMatthew G. Knepley     }
1956720e594eSMatthew G. Knepley   }
1957720e594eSMatthew G. Knepley   ierr = ISRestoreIndices(subpointIS, &dmpoints);CHKERRQ(ierr);
1958720e594eSMatthew G. Knepley   ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
1959720e594eSMatthew G. Knepley   PetscFunctionReturn(0);
1960720e594eSMatthew G. Knepley }
1961720e594eSMatthew G. Knepley 
1962720e594eSMatthew G. Knepley 
1963c08575a3SMatthew G. Knepley /*@
19643cf72582SMatthew G. Knepley   DMPlexCreateHybridMesh - Create a mesh with hybrid cells along an internal interface
19653cf72582SMatthew G. Knepley 
19663cf72582SMatthew G. Knepley   Collective on dm
19673cf72582SMatthew G. Knepley 
19683cf72582SMatthew G. Knepley   Input Parameters:
19693cf72582SMatthew G. Knepley + dm - The original DM
1970720e594eSMatthew G. Knepley . label - The label specifying the interface vertices
1971720e594eSMatthew G. Knepley - bdlabel - The optional label specifying the interface boundary vertices
19723cf72582SMatthew G. Knepley 
19733cf72582SMatthew G. Knepley   Output Parameters:
19747db7e0a7SMatthew G. Knepley + hybridLabel - The label fully marking the interface, or NULL if no output is desired
19757db7e0a7SMatthew G. Knepley . splitLabel - The label containing the split points, or NULL if no output is desired
1976720e594eSMatthew G. Knepley . dmInterface - The new interface DM, or NULL
1977720e594eSMatthew G. Knepley - dmHybrid - The new DM with cohesive cells
19783cf72582SMatthew G. Knepley 
19796eccb800SMatthew Knepley   Note: The hybridLabel indicates what parts of the original mesh impinged on the on division surface. For points
19806eccb800SMatthew Knepley   directly on the division surface, they are labeled with their dimension, so an edge 7 on the division surface would be
19816eccb800SMatthew Knepley   7 (1) in hybridLabel. For points that impinge from the positive side, they are labeled with 100+dim, so an edge 6 with
19826eccb800SMatthew 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
19836eccb800SMatthew Knepley   surface also hits vertex 3, it would be 9 (-101) in hybridLabel.
19846eccb800SMatthew Knepley 
19856eccb800SMatthew Knepley   The splitLabel indicates what points in the new hybrid mesh were the result of splitting points in the original
19866eccb800SMatthew Knepley   mesh. The label value is +=100+dim for each point. For example, if two edges 10 and 14 in the hybrid resulting from
19876eccb800SMatthew Knepley   splitting an edge in the original mesh, you would have 10 (101) and 14 (-101) in the splitLabel.
19886eccb800SMatthew Knepley 
19896eccb800SMatthew Knepley   The dmInterface is a DM built from the original division surface. It has a label which can be retrieved using
19906eccb800SMatthew Knepley   DMPlexGetSubpointMap() which maps each point back to the point in the surface of the original mesh.
19916eccb800SMatthew Knepley 
19923cf72582SMatthew G. Knepley   Level: developer
19933cf72582SMatthew G. Knepley 
19946eccb800SMatthew Knepley .seealso: DMPlexConstructCohesiveCells(), DMPlexLabelCohesiveComplete(), DMPlexGetSubpointMap(), DMCreate()
19953cf72582SMatthew G. Knepley @*/
19967db7e0a7SMatthew G. Knepley PetscErrorCode DMPlexCreateHybridMesh(DM dm, DMLabel label, DMLabel bdlabel, DMLabel *hybridLabel, DMLabel *splitLabel, DM *dmInterface, DM *dmHybrid)
19973cf72582SMatthew G. Knepley {
19983cf72582SMatthew G. Knepley   DM             idm;
19997db7e0a7SMatthew G. Knepley   DMLabel        subpointMap, hlabel, slabel = NULL;
20003cf72582SMatthew G. Knepley   PetscInt       dim;
20013cf72582SMatthew G. Knepley   PetscErrorCode ierr;
20023cf72582SMatthew G. Knepley 
20033cf72582SMatthew G. Knepley   PetscFunctionBegin;
20043cf72582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2005720e594eSMatthew G. Knepley   if (bdlabel) PetscValidPointer(bdlabel, 3);
2006720e594eSMatthew G. Knepley   if (hybridLabel) PetscValidPointer(hybridLabel, 4);
20077db7e0a7SMatthew G. Knepley   if (splitLabel)  PetscValidPointer(splitLabel, 5);
20087db7e0a7SMatthew G. Knepley   if (dmInterface) PetscValidPointer(dmInterface, 6);
20097db7e0a7SMatthew G. Knepley   PetscValidPointer(dmHybrid, 7);
2010c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2011158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmesh(dm, label, 1, PETSC_FALSE, &idm);CHKERRQ(ierr);
2012720e594eSMatthew G. Knepley   ierr = DMPlexCheckValidSubmesh_Private(dm, label, idm);CHKERRQ(ierr);
20133cf72582SMatthew G. Knepley   ierr = DMPlexOrient(idm);CHKERRQ(ierr);
20143cf72582SMatthew G. Knepley   ierr = DMPlexGetSubpointMap(idm, &subpointMap);CHKERRQ(ierr);
20153cf72582SMatthew G. Knepley   ierr = DMLabelDuplicate(subpointMap, &hlabel);CHKERRQ(ierr);
20163cf72582SMatthew G. Knepley   ierr = DMLabelClearStratum(hlabel, dim);CHKERRQ(ierr);
20177db7e0a7SMatthew G. Knepley   if (splitLabel) {
20187db7e0a7SMatthew G. Knepley     const char *name;
20197db7e0a7SMatthew G. Knepley     char        sname[PETSC_MAX_PATH_LEN];
20207db7e0a7SMatthew G. Knepley 
2021d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) hlabel, &name);CHKERRQ(ierr);
20227db7e0a7SMatthew G. Knepley     ierr = PetscStrncpy(sname, name, PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
20237db7e0a7SMatthew G. Knepley     ierr = PetscStrcat(sname, " split");CHKERRQ(ierr);
2024d67d17b1SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, sname, &slabel);CHKERRQ(ierr);
20257db7e0a7SMatthew G. Knepley   }
2026720e594eSMatthew G. Knepley   ierr = DMPlexLabelCohesiveComplete(dm, hlabel, bdlabel, PETSC_FALSE, idm);CHKERRQ(ierr);
2027720e594eSMatthew G. Knepley   if (dmInterface) {*dmInterface = idm;}
2028720e594eSMatthew G. Knepley   else             {ierr = DMDestroy(&idm);CHKERRQ(ierr);}
20297db7e0a7SMatthew G. Knepley   ierr = DMPlexConstructCohesiveCells(dm, hlabel, slabel, dmHybrid);CHKERRQ(ierr);
20303cf72582SMatthew G. Knepley   if (hybridLabel) *hybridLabel = hlabel;
203153aa2e23SMatthew G. Knepley   else             {ierr = DMLabelDestroy(&hlabel);CHKERRQ(ierr);}
20327db7e0a7SMatthew G. Knepley   if (splitLabel)  *splitLabel  = slabel;
2033cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
2034cd0c2139SMatthew G Knepley }
2035cd0c2139SMatthew G Knepley 
2036efa14ee0SMatthew G Knepley /* Here we need the explicit assumption that:
2037efa14ee0SMatthew G Knepley 
2038efa14ee0SMatthew G Knepley      For any marked cell, the marked vertices constitute a single face
2039efa14ee0SMatthew G Knepley */
2040830e53efSMatthew G. Knepley static PetscErrorCode DMPlexMarkSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, DM subdm)
2041efa14ee0SMatthew G Knepley {
2042fed694aaSMatthew G. Knepley   IS               subvertexIS = NULL;
2043efa14ee0SMatthew G Knepley   const PetscInt  *subvertices;
204477d178adSMatthew G. Knepley   PetscInt        *pStart, *pEnd, *pMax, pSize;
2045efa14ee0SMatthew G Knepley   PetscInt         depth, dim, d, numSubVerticesInitial = 0, v;
2046efa14ee0SMatthew G Knepley   PetscErrorCode   ierr;
2047efa14ee0SMatthew G Knepley 
2048efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2049efa14ee0SMatthew G Knepley   *numFaces = 0;
2050efa14ee0SMatthew G Knepley   *nFV      = 0;
2051efa14ee0SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2052c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
205377d178adSMatthew G. Knepley   pSize = PetscMax(depth, dim) + 1;
2054dcca6d9dSJed Brown   ierr = PetscMalloc3(pSize,&pStart,pSize,&pEnd,pSize,&pMax);CHKERRQ(ierr);
2055d92e47f8SMatthew 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);
2056efa14ee0SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
2057efa14ee0SMatthew G Knepley     ierr = DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);
2058efa14ee0SMatthew G Knepley     if (pMax[d] >= 0) pEnd[d] = PetscMin(pEnd[d], pMax[d]);
2059efa14ee0SMatthew G Knepley   }
2060efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
2061fed694aaSMatthew G. Knepley   if (vertexLabel) {ierr = DMLabelGetStratumIS(vertexLabel, value, &subvertexIS);CHKERRQ(ierr);}
2062efa14ee0SMatthew G Knepley   if (subvertexIS) {
2063efa14ee0SMatthew G Knepley     ierr = ISGetSize(subvertexIS, &numSubVerticesInitial);CHKERRQ(ierr);
2064efa14ee0SMatthew G Knepley     ierr = ISGetIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
2065efa14ee0SMatthew G Knepley   }
2066efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2067efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
20680298fd71SBarry Smith     PetscInt      *star   = NULL;
2069efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numCells = 0, c;
2070efa14ee0SMatthew G Knepley 
2071efa14ee0SMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2072efa14ee0SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
2073efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2074efa14ee0SMatthew G Knepley       if ((point >= pStart[depth]) && (point < pEnd[depth])) star[numCells++] = point;
2075efa14ee0SMatthew G Knepley     }
2076efa14ee0SMatthew G Knepley     for (c = 0; c < numCells; ++c) {
2077efa14ee0SMatthew G Knepley       const PetscInt cell    = star[c];
20780298fd71SBarry Smith       PetscInt      *closure = NULL;
2079efa14ee0SMatthew G Knepley       PetscInt       closureSize, cl;
2080efa14ee0SMatthew G Knepley       PetscInt       cellLoc, numCorners = 0, faceSize = 0;
2081efa14ee0SMatthew G Knepley 
2082efa14ee0SMatthew G Knepley       ierr = DMLabelGetValue(subpointMap, cell, &cellLoc);CHKERRQ(ierr);
208365560c7fSMatthew G Knepley       if (cellLoc == 2) continue;
208482f516ccSBarry Smith       if (cellLoc >= 0) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %d has dimension %d in the surface label", cell, cellLoc);
2085efa14ee0SMatthew G Knepley       ierr = DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2086efa14ee0SMatthew G Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
2087efa14ee0SMatthew G Knepley         const PetscInt point = closure[cl];
2088efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2089efa14ee0SMatthew G Knepley 
2090efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
2091efa14ee0SMatthew G Knepley           ++numCorners;
2092efa14ee0SMatthew G Knepley           ierr = DMLabelGetValue(vertexLabel, point, &vertexLoc);CHKERRQ(ierr);
2093830e53efSMatthew G. Knepley           if (vertexLoc == value) closure[faceSize++] = point;
2094efa14ee0SMatthew G Knepley         }
2095efa14ee0SMatthew G Knepley       }
209618ad9376SMatthew G. Knepley       if (!(*nFV)) {ierr = DMPlexGetNumFaceVertices(dm, dim, numCorners, nFV);CHKERRQ(ierr);}
209782f516ccSBarry 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);
2098efa14ee0SMatthew G Knepley       if (faceSize == *nFV) {
2099007baee2SMatthew G. Knepley         const PetscInt *cells = NULL;
2100007baee2SMatthew G. Knepley         PetscInt        numCells, nc;
2101007baee2SMatthew G. Knepley 
2102efa14ee0SMatthew G Knepley         ++(*numFaces);
2103efa14ee0SMatthew G Knepley         for (cl = 0; cl < faceSize; ++cl) {
2104efa14ee0SMatthew G Knepley           ierr = DMLabelSetValue(subpointMap, closure[cl], 0);CHKERRQ(ierr);
2105efa14ee0SMatthew G Knepley         }
2106007baee2SMatthew G. Knepley         ierr = DMPlexGetJoin(dm, faceSize, closure, &numCells, &cells);CHKERRQ(ierr);
2107007baee2SMatthew G. Knepley         for (nc = 0; nc < numCells; ++nc) {
2108007baee2SMatthew G. Knepley           ierr = DMLabelSetValue(subpointMap, cells[nc], 2);CHKERRQ(ierr);
2109007baee2SMatthew G. Knepley         }
2110007baee2SMatthew G. Knepley         ierr = DMPlexRestoreJoin(dm, faceSize, closure, &numCells, &cells);CHKERRQ(ierr);
2111efa14ee0SMatthew G Knepley       }
2112efa14ee0SMatthew G Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2113efa14ee0SMatthew G Knepley     }
2114efa14ee0SMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2115efa14ee0SMatthew G Knepley   }
2116efa14ee0SMatthew G Knepley   if (subvertexIS) {
2117efa14ee0SMatthew G Knepley     ierr = ISRestoreIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
2118efa14ee0SMatthew G Knepley   }
2119efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
2120efa14ee0SMatthew G Knepley   ierr = PetscFree3(pStart,pEnd,pMax);CHKERRQ(ierr);
2121efa14ee0SMatthew G Knepley   PetscFunctionReturn(0);
2122efa14ee0SMatthew G Knepley }
2123efa14ee0SMatthew G Knepley 
2124158acfadSMatthew G. Knepley static PetscErrorCode DMPlexMarkSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DMLabel subpointMap, DM subdm)
2125efa14ee0SMatthew G Knepley {
212634b4c39eSMatthew G. Knepley   IS               subvertexIS = NULL;
2127efa14ee0SMatthew G Knepley   const PetscInt  *subvertices;
2128efa14ee0SMatthew G Knepley   PetscInt        *pStart, *pEnd, *pMax;
2129efa14ee0SMatthew G Knepley   PetscInt         dim, d, numSubVerticesInitial = 0, v;
2130efa14ee0SMatthew G Knepley   PetscErrorCode   ierr;
2131efa14ee0SMatthew G Knepley 
2132efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2133c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2134dcca6d9dSJed Brown   ierr = PetscMalloc3(dim+1,&pStart,dim+1,&pEnd,dim+1,&pMax);CHKERRQ(ierr);
21350298fd71SBarry Smith   ierr = DMPlexGetHybridBounds(dm, &pMax[dim], dim>1 ? &pMax[dim-1] : NULL, dim > 2 ? &pMax[1] : NULL, &pMax[0]);CHKERRQ(ierr);
2136efa14ee0SMatthew G Knepley   for (d = 0; d <= dim; ++d) {
2137efa14ee0SMatthew G Knepley     ierr = DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);
2138efa14ee0SMatthew G Knepley     if (pMax[d] >= 0) pEnd[d] = PetscMin(pEnd[d], pMax[d]);
2139efa14ee0SMatthew G Knepley   }
2140efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
214134b4c39eSMatthew G. Knepley   if (vertexLabel) {
2142830e53efSMatthew G. Knepley     ierr = DMLabelGetStratumIS(vertexLabel, value, &subvertexIS);CHKERRQ(ierr);
2143efa14ee0SMatthew G Knepley     if (subvertexIS) {
2144efa14ee0SMatthew G Knepley       ierr = ISGetSize(subvertexIS, &numSubVerticesInitial);CHKERRQ(ierr);
2145efa14ee0SMatthew G Knepley       ierr = ISGetIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
2146efa14ee0SMatthew G Knepley     }
214734b4c39eSMatthew G. Knepley   }
2148efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2149efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
21500298fd71SBarry Smith     PetscInt      *star   = NULL;
2151efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numFaces = 0, f;
2152efa14ee0SMatthew G Knepley 
2153efa14ee0SMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2154efa14ee0SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
2155efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2156158acfadSMatthew G. Knepley       PetscInt       faceLoc;
2157158acfadSMatthew G. Knepley 
2158158acfadSMatthew G. Knepley       if ((point >= pStart[dim-1]) && (point < pEnd[dim-1])) {
2159158acfadSMatthew G. Knepley         if (markedFaces) {
2160158acfadSMatthew G. Knepley           ierr = DMLabelGetValue(vertexLabel, point, &faceLoc);CHKERRQ(ierr);
2161158acfadSMatthew G. Knepley           if (faceLoc < 0) continue;
2162158acfadSMatthew G. Knepley         }
2163158acfadSMatthew G. Knepley         star[numFaces++] = point;
2164158acfadSMatthew G. Knepley       }
2165efa14ee0SMatthew G Knepley     }
2166efa14ee0SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
2167efa14ee0SMatthew G Knepley       const PetscInt face    = star[f];
21680298fd71SBarry Smith       PetscInt      *closure = NULL;
2169efa14ee0SMatthew G Knepley       PetscInt       closureSize, c;
2170efa14ee0SMatthew G Knepley       PetscInt       faceLoc;
2171efa14ee0SMatthew G Knepley 
2172efa14ee0SMatthew G Knepley       ierr = DMLabelGetValue(subpointMap, face, &faceLoc);CHKERRQ(ierr);
2173efa14ee0SMatthew G Knepley       if (faceLoc == dim-1) continue;
217482f516ccSBarry Smith       if (faceLoc >= 0) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %d has dimension %d in the surface label", face, faceLoc);
2175efa14ee0SMatthew G Knepley       ierr = DMPlexGetTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2176efa14ee0SMatthew G Knepley       for (c = 0; c < closureSize*2; c += 2) {
2177efa14ee0SMatthew G Knepley         const PetscInt point = closure[c];
2178efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2179efa14ee0SMatthew G Knepley 
2180efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
2181efa14ee0SMatthew G Knepley           ierr = DMLabelGetValue(vertexLabel, point, &vertexLoc);CHKERRQ(ierr);
2182830e53efSMatthew G. Knepley           if (vertexLoc != value) break;
2183efa14ee0SMatthew G Knepley         }
2184efa14ee0SMatthew G Knepley       }
2185efa14ee0SMatthew G Knepley       if (c == closureSize*2) {
2186efa14ee0SMatthew G Knepley         const PetscInt *support;
2187efa14ee0SMatthew G Knepley         PetscInt        supportSize, s;
2188efa14ee0SMatthew G Knepley 
2189efa14ee0SMatthew G Knepley         for (c = 0; c < closureSize*2; c += 2) {
2190efa14ee0SMatthew G Knepley           const PetscInt point = closure[c];
2191efa14ee0SMatthew G Knepley 
2192efa14ee0SMatthew G Knepley           for (d = 0; d < dim; ++d) {
2193efa14ee0SMatthew G Knepley             if ((point >= pStart[d]) && (point < pEnd[d])) {
2194efa14ee0SMatthew G Knepley               ierr = DMLabelSetValue(subpointMap, point, d);CHKERRQ(ierr);
2195efa14ee0SMatthew G Knepley               break;
2196efa14ee0SMatthew G Knepley             }
2197efa14ee0SMatthew G Knepley           }
2198efa14ee0SMatthew G Knepley         }
2199efa14ee0SMatthew G Knepley         ierr = DMPlexGetSupportSize(dm, face, &supportSize);CHKERRQ(ierr);
2200efa14ee0SMatthew G Knepley         ierr = DMPlexGetSupport(dm, face, &support);CHKERRQ(ierr);
2201efa14ee0SMatthew G Knepley         for (s = 0; s < supportSize; ++s) {
2202efa14ee0SMatthew G Knepley           ierr = DMLabelSetValue(subpointMap, support[s], dim);CHKERRQ(ierr);
2203efa14ee0SMatthew G Knepley         }
2204efa14ee0SMatthew G Knepley       }
2205efa14ee0SMatthew G Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2206efa14ee0SMatthew G Knepley     }
2207efa14ee0SMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2208efa14ee0SMatthew G Knepley   }
220934b4c39eSMatthew G. Knepley   if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subvertices);CHKERRQ(ierr);}
2210efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
2211efa14ee0SMatthew G Knepley   ierr = PetscFree3(pStart,pEnd,pMax);CHKERRQ(ierr);
2212efa14ee0SMatthew G Knepley   PetscFunctionReturn(0);
2213efa14ee0SMatthew G Knepley }
2214efa14ee0SMatthew G Knepley 
221527c04023SMatthew 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)
2216766ab985SMatthew G. Knepley {
221727c04023SMatthew G. Knepley   DMLabel         label = NULL;
2218766ab985SMatthew G. Knepley   const PetscInt *cone;
22199fc93327SToby Isaac   PetscInt        dim, cMax, cEnd, c, subc = 0, p, coneSize = -1;
2220766ab985SMatthew G. Knepley   PetscErrorCode  ierr;
2221766ab985SMatthew G. Knepley 
2222812bfc34SJed Brown   PetscFunctionBegin;
2223c0ed958bSJed Brown   *numFaces = 0;
2224c0ed958bSJed Brown   *nFV = 0;
2225c58f1c22SToby Isaac   if (labelname) {ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);}
2226fed694aaSMatthew G. Knepley   *subCells = NULL;
2227c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2228766ab985SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
2229766ab985SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
2230766ab985SMatthew G. Knepley   if (cMax < 0) PetscFunctionReturn(0);
223127c04023SMatthew G. Knepley   if (label) {
223227c04023SMatthew G. Knepley     for (c = cMax; c < cEnd; ++c) {
223327c04023SMatthew G. Knepley       PetscInt val;
223427c04023SMatthew G. Knepley 
223527c04023SMatthew G. Knepley       ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr);
223627c04023SMatthew G. Knepley       if (val == value) {
223727c04023SMatthew G. Knepley         ++(*numFaces);
223827c04023SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
223927c04023SMatthew G. Knepley       }
224027c04023SMatthew G. Knepley     }
224127c04023SMatthew G. Knepley   } else {
2242766ab985SMatthew G. Knepley     *numFaces = cEnd - cMax;
224327c04023SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cMax, &coneSize);CHKERRQ(ierr);
224427c04023SMatthew G. Knepley   }
2245785e854fSJed Brown   ierr = PetscMalloc1(*numFaces *2, subCells);CHKERRQ(ierr);
22469fc93327SToby Isaac   if (!(*numFaces)) PetscFunctionReturn(0);
22479fc93327SToby Isaac   *nFV = hasLagrange ? coneSize/3 : coneSize/2;
2248766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2249766ab985SMatthew G. Knepley     const PetscInt *cells;
2250766ab985SMatthew G. Knepley     PetscInt        numCells;
2251766ab985SMatthew G. Knepley 
225227c04023SMatthew G. Knepley     if (label) {
225327c04023SMatthew G. Knepley       PetscInt val;
225427c04023SMatthew G. Knepley 
225527c04023SMatthew G. Knepley       ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr);
225627c04023SMatthew G. Knepley       if (val != value) continue;
225727c04023SMatthew G. Knepley     }
2258766ab985SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
2259766ab985SMatthew G. Knepley     for (p = 0; p < *nFV; ++p) {
2260766ab985SMatthew G. Knepley       ierr = DMLabelSetValue(subpointMap, cone[p], 0);CHKERRQ(ierr);
2261766ab985SMatthew G. Knepley     }
2262766ab985SMatthew G. Knepley     /* Negative face */
2263766ab985SMatthew G. Knepley     ierr = DMPlexGetJoin(dm, *nFV, cone, &numCells, &cells);CHKERRQ(ierr);
226427234c99SMatthew G. Knepley     /* Not true in parallel
226527234c99SMatthew G. Knepley     if (numCells != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
2266766ab985SMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
2267766ab985SMatthew G. Knepley       ierr = DMLabelSetValue(subpointMap, cells[p], 2);CHKERRQ(ierr);
226827234c99SMatthew G. Knepley       (*subCells)[subc++] = cells[p];
2269766ab985SMatthew G. Knepley     }
2270766ab985SMatthew G. Knepley     ierr = DMPlexRestoreJoin(dm, *nFV, cone, &numCells, &cells);CHKERRQ(ierr);
2271766ab985SMatthew G. Knepley     /* Positive face is not included */
2272766ab985SMatthew G. Knepley   }
2273766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
2274766ab985SMatthew G. Knepley }
2275766ab985SMatthew G. Knepley 
22763982b651SMatthew G. Knepley static PetscErrorCode DMPlexMarkCohesiveSubmesh_Interpolated(DM dm, DMLabel label, PetscInt value, DMLabel subpointMap, DM subdm)
2277766ab985SMatthew G. Knepley {
2278766ab985SMatthew G. Knepley   PetscInt      *pStart, *pEnd;
2279766ab985SMatthew G. Knepley   PetscInt       dim, cMax, cEnd, c, d;
2280766ab985SMatthew G. Knepley   PetscErrorCode ierr;
2281766ab985SMatthew G. Knepley 
2282812bfc34SJed Brown   PetscFunctionBegin;
2283c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2284766ab985SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
2285766ab985SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
2286766ab985SMatthew G. Knepley   if (cMax < 0) PetscFunctionReturn(0);
2287dcca6d9dSJed Brown   ierr = PetscMalloc2(dim+1,&pStart,dim+1,&pEnd);CHKERRQ(ierr);
2288b3154360SMatthew G. Knepley   for (d = 0; d <= dim; ++d) {ierr = DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);}
2289766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2290766ab985SMatthew G. Knepley     const PetscInt *cone;
2291766ab985SMatthew G. Knepley     PetscInt       *closure = NULL;
2292b3154360SMatthew G. Knepley     PetscInt        fconeSize, coneSize, closureSize, cl, val;
2293766ab985SMatthew G. Knepley 
229427c04023SMatthew G. Knepley     if (label) {
229527c04023SMatthew G. Knepley       ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr);
229627c04023SMatthew G. Knepley       if (val != value) continue;
229727c04023SMatthew G. Knepley     }
2298766ab985SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
2299766ab985SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
2300b3154360SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cone[0], &fconeSize);CHKERRQ(ierr);
2301b3154360SMatthew G. Knepley     if (coneSize != (fconeSize ? fconeSize : 1) + 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells");
2302b3154360SMatthew G. Knepley     /* Negative face */
2303766ab985SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2304766ab985SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
2305766ab985SMatthew G. Knepley       const PetscInt point = closure[cl];
2306766ab985SMatthew G. Knepley 
2307766ab985SMatthew G. Knepley       for (d = 0; d <= dim; ++d) {
2308766ab985SMatthew G. Knepley         if ((point >= pStart[d]) && (point < pEnd[d])) {
2309766ab985SMatthew G. Knepley           ierr = DMLabelSetValue(subpointMap, point, d);CHKERRQ(ierr);
2310766ab985SMatthew G. Knepley           break;
2311766ab985SMatthew G. Knepley         }
2312766ab985SMatthew G. Knepley       }
2313766ab985SMatthew G. Knepley     }
2314766ab985SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2315766ab985SMatthew G. Knepley     /* Cells -- positive face is not included */
2316766ab985SMatthew G. Knepley     for (cl = 0; cl < 1; ++cl) {
2317766ab985SMatthew G. Knepley       const PetscInt *support;
2318766ab985SMatthew G. Knepley       PetscInt        supportSize, s;
2319766ab985SMatthew G. Knepley 
2320766ab985SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[cl], &supportSize);CHKERRQ(ierr);
2321711de394SMatthew G. Knepley       /* if (supportSize != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive faces should separate two cells"); */
2322766ab985SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[cl], &support);CHKERRQ(ierr);
2323766ab985SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
2324766ab985SMatthew G. Knepley         ierr = DMLabelSetValue(subpointMap, support[s], dim);CHKERRQ(ierr);
2325766ab985SMatthew G. Knepley       }
2326766ab985SMatthew G. Knepley     }
2327766ab985SMatthew G. Knepley   }
2328766ab985SMatthew G. Knepley   ierr = PetscFree2(pStart, pEnd);CHKERRQ(ierr);
2329766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
2330766ab985SMatthew G. Knepley }
2331766ab985SMatthew G. Knepley 
2332c08575a3SMatthew G. Knepley static PetscErrorCode DMPlexGetFaceOrientation(DM dm, PetscInt cell, PetscInt numCorners, PetscInt indices[], PetscInt oppositeVertex, PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2333e6ccafaeSMatthew G Knepley {
233482f516ccSBarry Smith   MPI_Comm       comm;
2335e6ccafaeSMatthew G Knepley   PetscBool      posOrient = PETSC_FALSE;
2336e6ccafaeSMatthew G Knepley   const PetscInt debug     = 0;
2337e6ccafaeSMatthew G Knepley   PetscInt       cellDim, faceSize, f;
2338e6ccafaeSMatthew G Knepley   PetscErrorCode ierr;
2339e6ccafaeSMatthew G Knepley 
234082f516ccSBarry Smith   PetscFunctionBegin;
234182f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2342c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &cellDim);CHKERRQ(ierr);
2343367003a6SStefano Zampini   if (debug) {ierr = PetscPrintf(comm, "cellDim: %d numCorners: %d\n", cellDim, numCorners);CHKERRQ(ierr);}
2344e6ccafaeSMatthew G Knepley 
2345ddeab2a6SMatthew G. Knepley   if (cellDim == 1 && numCorners == 2) {
2346ddeab2a6SMatthew G. Knepley     /* Triangle */
2347e6ccafaeSMatthew G Knepley     faceSize  = numCorners-1;
2348e6ccafaeSMatthew G Knepley     posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2349ddeab2a6SMatthew G. Knepley   } else if (cellDim == 2 && numCorners == 3) {
2350ddeab2a6SMatthew G. Knepley     /* Triangle */
2351ddeab2a6SMatthew G. Knepley     faceSize  = numCorners-1;
2352ddeab2a6SMatthew G. Knepley     posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2353ddeab2a6SMatthew G. Knepley   } else if (cellDim == 3 && numCorners == 4) {
2354ddeab2a6SMatthew G. Knepley     /* Tetrahedron */
2355ddeab2a6SMatthew G. Knepley     faceSize  = numCorners-1;
2356ddeab2a6SMatthew G. Knepley     posOrient = (oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2357e6ccafaeSMatthew G Knepley   } else if (cellDim == 1 && numCorners == 3) {
2358e6ccafaeSMatthew G Knepley     /* Quadratic line */
2359e6ccafaeSMatthew G Knepley     faceSize  = 1;
2360e6ccafaeSMatthew G Knepley     posOrient = PETSC_TRUE;
2361e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 4) {
2362e6ccafaeSMatthew G Knepley     /* Quads */
2363e6ccafaeSMatthew G Knepley     faceSize = 2;
2364e6ccafaeSMatthew G Knepley     if ((indices[1] > indices[0]) && (indices[1] - indices[0] == 1)) {
2365e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2366e6ccafaeSMatthew G Knepley     } else if ((indices[0] == 3) && (indices[1] == 0)) {
2367e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2368e6ccafaeSMatthew G Knepley     } else {
2369e6ccafaeSMatthew G Knepley       if (((indices[0] > indices[1]) && (indices[0] - indices[1] == 1)) || ((indices[0] == 0) && (indices[1] == 3))) {
2370e6ccafaeSMatthew G Knepley         posOrient = PETSC_FALSE;
2371e6ccafaeSMatthew G Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossedge");
2372e6ccafaeSMatthew G Knepley     }
2373e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 6) {
2374e6ccafaeSMatthew G Knepley     /* Quadratic triangle (I hate this) */
2375e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2376e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTri = 3;
2377e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2378e6ccafaeSMatthew G Knepley     PetscBool      found                    = PETSC_FALSE;
2379e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTriSorted[9] = {
2380e6ccafaeSMatthew G Knepley       0, 3,  4, /* bottom */
2381e6ccafaeSMatthew G Knepley       1, 4,  5, /* right */
2382e6ccafaeSMatthew G Knepley       2, 3,  5, /* left */
2383e6ccafaeSMatthew G Knepley     };
2384e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTri[9] = {
2385e6ccafaeSMatthew G Knepley       0, 3,  4, /* bottom */
2386e6ccafaeSMatthew G Knepley       1, 4,  5, /* right */
2387e6ccafaeSMatthew G Knepley       2, 5,  3, /* left */
2388e6ccafaeSMatthew G Knepley     };
2389e6ccafaeSMatthew G Knepley 
2390e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTri; ++i) sortedIndices[i] = indices[i];
2391e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeTri, sortedIndices);CHKERRQ(ierr);
2392e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 3; ++iFace) {
2393e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeTri;
2394e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2395e6ccafaeSMatthew G Knepley 
2396e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesTriSorted[ii+0]) &&
2397e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesTriSorted[ii+1])) {
2398e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTri; ++fVertex) {
2399e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTri; ++cVertex) {
2400e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTri[ii+fVertex]) {
2401e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2402e6ccafaeSMatthew G Knepley               break;
2403e6ccafaeSMatthew G Knepley             }
2404e6ccafaeSMatthew G Knepley           }
2405e6ccafaeSMatthew G Knepley         }
2406e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2407e6ccafaeSMatthew G Knepley         break;
2408e6ccafaeSMatthew G Knepley       }
2409e6ccafaeSMatthew G Knepley     }
2410e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid tri crossface");
2411e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2412e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2413e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 9) {
2414e6ccafaeSMatthew G Knepley     /* Quadratic quad (I hate this) */
2415e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2416e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuad = 3;
2417e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2418e6ccafaeSMatthew G Knepley     PetscBool      found                      = PETSC_FALSE;
2419e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadSorted[12] = {
2420e6ccafaeSMatthew G Knepley       0, 1,  4, /* bottom */
2421e6ccafaeSMatthew G Knepley       1, 2,  5, /* right */
2422e6ccafaeSMatthew G Knepley       2, 3,  6, /* top */
2423e6ccafaeSMatthew G Knepley       0, 3,  7, /* left */
2424e6ccafaeSMatthew G Knepley     };
2425e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuad[12] = {
2426e6ccafaeSMatthew G Knepley       0, 1,  4, /* bottom */
2427e6ccafaeSMatthew G Knepley       1, 2,  5, /* right */
2428e6ccafaeSMatthew G Knepley       2, 3,  6, /* top */
2429e6ccafaeSMatthew G Knepley       3, 0,  7, /* left */
2430e6ccafaeSMatthew G Knepley     };
2431e6ccafaeSMatthew G Knepley 
2432e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuad; ++i) sortedIndices[i] = indices[i];
2433e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeQuad, sortedIndices);CHKERRQ(ierr);
2434e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2435e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeQuad;
2436e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2437e6ccafaeSMatthew G Knepley 
2438e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesQuadSorted[ii+0]) &&
2439e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesQuadSorted[ii+1])) {
2440e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuad; ++fVertex) {
2441e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuad; ++cVertex) {
2442e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuad[ii+fVertex]) {
2443e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2444e6ccafaeSMatthew G Knepley               break;
2445e6ccafaeSMatthew G Knepley             }
2446e6ccafaeSMatthew G Knepley           }
2447e6ccafaeSMatthew G Knepley         }
2448e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2449e6ccafaeSMatthew G Knepley         break;
2450e6ccafaeSMatthew G Knepley       }
2451e6ccafaeSMatthew G Knepley     }
2452e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossface");
2453e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2454e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2455e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 8) {
2456e6ccafaeSMatthew G Knepley     /* Hexes
2457e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2458e6ccafaeSMatthew G Knepley        pointing up at the second.
2459e6ccafaeSMatthew G Knepley 
2460e6ccafaeSMatthew G Knepley           7---6
2461e6ccafaeSMatthew G Knepley          /|  /|
2462e6ccafaeSMatthew G Knepley         4---5 |
2463ddeab2a6SMatthew G. Knepley         | 1-|-2
2464e6ccafaeSMatthew G Knepley         |/  |/
2465ddeab2a6SMatthew G. Knepley         0---3
2466e6ccafaeSMatthew G Knepley 
2467e6ccafaeSMatthew G Knepley         Faces are determined by the first 4 vertices (corners of faces) */
2468e6ccafaeSMatthew G Knepley     const PetscInt faceSizeHex = 4;
2469e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[4], i, iFace;
2470e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2471e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHexSorted[24] = {
2472e6ccafaeSMatthew G Knepley       0, 1, 2, 3,  /* bottom */
2473e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  /* top */
2474ddeab2a6SMatthew G. Knepley       0, 3, 4, 5,  /* front */
2475ddeab2a6SMatthew G. Knepley       2, 3, 5, 6,  /* right */
2476ddeab2a6SMatthew G. Knepley       1, 2, 6, 7,  /* back */
2477ddeab2a6SMatthew G. Knepley       0, 1, 4, 7,  /* left */
2478e6ccafaeSMatthew G Knepley     };
2479e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHex[24] = {
2480ddeab2a6SMatthew G. Knepley       1, 2, 3, 0,  /* bottom */
2481e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  /* top */
2482ddeab2a6SMatthew G. Knepley       0, 3, 5, 4,  /* front */
2483ddeab2a6SMatthew G. Knepley       3, 2, 6, 5,  /* right */
2484ddeab2a6SMatthew G. Knepley       2, 1, 7, 6,  /* back */
2485ddeab2a6SMatthew G. Knepley       1, 0, 4, 7,  /* left */
2486e6ccafaeSMatthew G Knepley     };
2487e6ccafaeSMatthew G Knepley 
2488e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeHex; ++i) sortedIndices[i] = indices[i];
2489e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeHex, sortedIndices);CHKERRQ(ierr);
2490e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2491e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeHex;
2492e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2493e6ccafaeSMatthew G Knepley 
2494e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesHexSorted[ii+0]) &&
2495e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesHexSorted[ii+1]) &&
2496e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesHexSorted[ii+2]) &&
2497e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesHexSorted[ii+3])) {
2498e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeHex; ++fVertex) {
2499e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeHex; ++cVertex) {
2500e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesHex[ii+fVertex]) {
2501e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2502e6ccafaeSMatthew G Knepley               break;
2503e6ccafaeSMatthew G Knepley             }
2504e6ccafaeSMatthew G Knepley           }
2505e6ccafaeSMatthew G Knepley         }
2506e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2507e6ccafaeSMatthew G Knepley         break;
2508e6ccafaeSMatthew G Knepley       }
2509e6ccafaeSMatthew G Knepley     }
2510e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2511e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2512e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2513e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 10) {
2514e6ccafaeSMatthew G Knepley     /* Quadratic tet */
2515e6ccafaeSMatthew G Knepley     /* Faces are determined by the first 3 vertices (corners of faces) */
2516e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTet = 6;
2517e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[6], i, iFace;
2518e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2519e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTetSorted[24] = {
2520e6ccafaeSMatthew G Knepley       0, 1, 2,  6, 7, 8, /* bottom */
2521e6ccafaeSMatthew G Knepley       0, 3, 4,  6, 7, 9,  /* front */
2522e6ccafaeSMatthew G Knepley       1, 4, 5,  7, 8, 9,  /* right */
2523e6ccafaeSMatthew G Knepley       2, 3, 5,  6, 8, 9,  /* left */
2524e6ccafaeSMatthew G Knepley     };
2525e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTet[24] = {
2526e6ccafaeSMatthew G Knepley       0, 1, 2,  6, 7, 8, /* bottom */
2527e6ccafaeSMatthew G Knepley       0, 4, 3,  6, 7, 9,  /* front */
2528e6ccafaeSMatthew G Knepley       1, 5, 4,  7, 8, 9,  /* right */
2529e6ccafaeSMatthew G Knepley       2, 3, 5,  8, 6, 9,  /* left */
2530e6ccafaeSMatthew G Knepley     };
2531e6ccafaeSMatthew G Knepley 
2532e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTet; ++i) sortedIndices[i] = indices[i];
2533e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeTet, sortedIndices);CHKERRQ(ierr);
2534e6ccafaeSMatthew G Knepley     for (iFace=0; iFace < 4; ++iFace) {
2535e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeTet;
2536e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2537e6ccafaeSMatthew G Knepley 
2538e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesTetSorted[ii+0]) &&
2539e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesTetSorted[ii+1]) &&
2540e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesTetSorted[ii+2]) &&
2541e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesTetSorted[ii+3])) {
2542e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTet; ++fVertex) {
2543e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTet; ++cVertex) {
2544e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTet[ii+fVertex]) {
2545e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2546e6ccafaeSMatthew G Knepley               break;
2547e6ccafaeSMatthew G Knepley             }
2548e6ccafaeSMatthew G Knepley           }
2549e6ccafaeSMatthew G Knepley         }
2550e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2551e6ccafaeSMatthew G Knepley         break;
2552e6ccafaeSMatthew G Knepley       }
2553e6ccafaeSMatthew G Knepley     }
2554e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid tet crossface");
2555e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2556e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2557e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 27) {
2558e6ccafaeSMatthew G Knepley     /* Quadratic hexes (I hate this)
2559e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2560e6ccafaeSMatthew G Knepley        pointing up at the second.
2561e6ccafaeSMatthew G Knepley 
2562e6ccafaeSMatthew G Knepley          7---6
2563e6ccafaeSMatthew G Knepley         /|  /|
2564e6ccafaeSMatthew G Knepley        4---5 |
2565e6ccafaeSMatthew G Knepley        | 3-|-2
2566e6ccafaeSMatthew G Knepley        |/  |/
2567e6ccafaeSMatthew G Knepley        0---1
2568e6ccafaeSMatthew G Knepley 
2569e6ccafaeSMatthew G Knepley        Faces are determined by the first 4 vertices (corners of faces) */
2570e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuadHex = 9;
2571e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[9], i, iFace;
2572e6ccafaeSMatthew G Knepley     PetscBool      found                         = PETSC_FALSE;
2573e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHexSorted[54] = {
2574e6ccafaeSMatthew G Knepley       0, 1, 2, 3,  8, 9, 10, 11,  24, /* bottom */
2575e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  12, 13, 14, 15,  25, /* top */
2576e6ccafaeSMatthew G Knepley       0, 1, 4, 5,  8, 12, 16, 17,  22, /* front */
2577e6ccafaeSMatthew G Knepley       1, 2, 5, 6,  9, 13, 17, 18,  21, /* right */
2578e6ccafaeSMatthew G Knepley       2, 3, 6, 7,  10, 14, 18, 19,  23, /* back */
2579e6ccafaeSMatthew G Knepley       0, 3, 4, 7,  11, 15, 16, 19,  20, /* left */
2580e6ccafaeSMatthew G Knepley     };
2581e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHex[54] = {
2582e6ccafaeSMatthew G Knepley       3, 2, 1, 0,  10, 9, 8, 11,  24, /* bottom */
2583e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  12, 13, 14, 15,  25, /* top */
2584e6ccafaeSMatthew G Knepley       0, 1, 5, 4,  8, 17, 12, 16,  22, /* front */
2585e6ccafaeSMatthew G Knepley       1, 2, 6, 5,  9, 18, 13, 17,  21, /* right */
2586e6ccafaeSMatthew G Knepley       2, 3, 7, 6,  10, 19, 14, 18,  23, /* back */
2587e6ccafaeSMatthew G Knepley       3, 0, 4, 7,  11, 16, 15, 19,  20 /* left */
2588e6ccafaeSMatthew G Knepley     };
2589e6ccafaeSMatthew G Knepley 
2590e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuadHex; ++i) sortedIndices[i] = indices[i];
2591e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeQuadHex, sortedIndices);CHKERRQ(ierr);
2592e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2593e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeQuadHex;
2594e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2595e6ccafaeSMatthew G Knepley 
2596e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesQuadHexSorted[ii+0]) &&
2597e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesQuadHexSorted[ii+1]) &&
2598e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesQuadHexSorted[ii+2]) &&
2599e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesQuadHexSorted[ii+3])) {
2600e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuadHex; ++fVertex) {
2601e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuadHex; ++cVertex) {
2602e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuadHex[ii+fVertex]) {
2603e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2604e6ccafaeSMatthew G Knepley               break;
2605e6ccafaeSMatthew G Knepley             }
2606e6ccafaeSMatthew G Knepley           }
2607e6ccafaeSMatthew G Knepley         }
2608e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2609e6ccafaeSMatthew G Knepley         break;
2610e6ccafaeSMatthew G Knepley       }
2611e6ccafaeSMatthew G Knepley     }
2612e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2613e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2614e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2615e6ccafaeSMatthew G Knepley   } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Unknown cell type for faceOrientation().");
2616e6ccafaeSMatthew G Knepley   if (!posOrient) {
2617e6ccafaeSMatthew G Knepley     if (debug) {ierr = PetscPrintf(comm, "  Reversing initial face orientation\n");CHKERRQ(ierr);}
2618e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[faceSize-1 - f];
2619e6ccafaeSMatthew G Knepley   } else {
2620e6ccafaeSMatthew G Knepley     if (debug) {ierr = PetscPrintf(comm, "  Keeping initial face orientation\n");CHKERRQ(ierr);}
2621e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[f];
2622e6ccafaeSMatthew G Knepley   }
2623e6ccafaeSMatthew G Knepley   if (posOriented) *posOriented = posOrient;
2624e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2625e6ccafaeSMatthew G Knepley }
2626e6ccafaeSMatthew G Knepley 
2627c08575a3SMatthew G. Knepley /*@
2628c08575a3SMatthew G. Knepley   DMPlexGetOrientedFace - Given a cell and a face, as a set of vertices, return the oriented face, as a set of vertices,
2629c08575a3SMatthew G. Knepley   in faceVertices. The orientation is such that the face normal points out of the cell
2630c08575a3SMatthew G. Knepley 
2631c08575a3SMatthew G. Knepley   Not collective
2632c08575a3SMatthew G. Knepley 
2633c08575a3SMatthew G. Knepley   Input Parameters:
2634c08575a3SMatthew G. Knepley + dm           - The original mesh
2635c08575a3SMatthew G. Knepley . cell         - The cell mesh point
2636c08575a3SMatthew G. Knepley . faceSize     - The number of vertices on the face
2637c08575a3SMatthew G. Knepley . face         - The face vertices
2638c08575a3SMatthew G. Knepley . numCorners   - The number of vertices on the cell
2639c08575a3SMatthew G. Knepley . indices      - Local numbering of face vertices in cell cone
2640c08575a3SMatthew G. Knepley - origVertices - Original face vertices
2641c08575a3SMatthew G. Knepley 
2642c08575a3SMatthew G. Knepley   Output Parameter:
2643c08575a3SMatthew G. Knepley + faceVertices - The face vertices properly oriented
2644c08575a3SMatthew G. Knepley - posOriented  - PETSC_TRUE if the face was oriented with outward normal
2645c08575a3SMatthew G. Knepley 
2646c08575a3SMatthew G. Knepley   Level: developer
2647c08575a3SMatthew G. Knepley 
2648c08575a3SMatthew G. Knepley .seealso: DMPlexGetCone()
2649c08575a3SMatthew G. Knepley @*/
2650e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexGetOrientedFace(DM dm, PetscInt cell, PetscInt faceSize, const PetscInt face[], PetscInt numCorners, PetscInt indices[], PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2651e6ccafaeSMatthew G Knepley {
26520298fd71SBarry Smith   const PetscInt *cone = NULL;
2653e6ccafaeSMatthew G Knepley   PetscInt        coneSize, v, f, v2;
2654e6ccafaeSMatthew G Knepley   PetscInt        oppositeVertex = -1;
2655e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
2656e6ccafaeSMatthew G Knepley 
2657e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
2658e6ccafaeSMatthew G Knepley   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
2659e6ccafaeSMatthew G Knepley   ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
2660e6ccafaeSMatthew G Knepley   for (v = 0, v2 = 0; v < coneSize; ++v) {
2661e6ccafaeSMatthew G Knepley     PetscBool found = PETSC_FALSE;
2662e6ccafaeSMatthew G Knepley 
2663e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) {
2664e6ccafaeSMatthew G Knepley       if (face[f] == cone[v]) {
2665e6ccafaeSMatthew G Knepley         found = PETSC_TRUE; break;
2666e6ccafaeSMatthew G Knepley       }
2667e6ccafaeSMatthew G Knepley     }
2668e6ccafaeSMatthew G Knepley     if (found) {
2669e6ccafaeSMatthew G Knepley       indices[v2]      = v;
2670e6ccafaeSMatthew G Knepley       origVertices[v2] = cone[v];
2671e6ccafaeSMatthew G Knepley       ++v2;
2672e6ccafaeSMatthew G Knepley     } else {
2673e6ccafaeSMatthew G Knepley       oppositeVertex = v;
2674e6ccafaeSMatthew G Knepley     }
2675e6ccafaeSMatthew G Knepley   }
2676e6ccafaeSMatthew G Knepley   ierr = DMPlexGetFaceOrientation(dm, cell, numCorners, indices, oppositeVertex, origVertices, faceVertices, posOriented);CHKERRQ(ierr);
2677e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2678e6ccafaeSMatthew G Knepley }
2679e6ccafaeSMatthew G Knepley 
2680e6ccafaeSMatthew G Knepley /*
2681cd0c2139SMatthew G Knepley   DMPlexInsertFace_Internal - Puts a face into the mesh
2682e6ccafaeSMatthew G Knepley 
2683e6ccafaeSMatthew G Knepley   Not collective
2684e6ccafaeSMatthew G Knepley 
2685e6ccafaeSMatthew G Knepley   Input Parameters:
2686e6ccafaeSMatthew G Knepley   + dm              - The DMPlex
2687e6ccafaeSMatthew G Knepley   . numFaceVertex   - The number of vertices in the face
2688e6ccafaeSMatthew G Knepley   . faceVertices    - The vertices in the face for dm
2689e6ccafaeSMatthew G Knepley   . subfaceVertices - The vertices in the face for subdm
2690e6ccafaeSMatthew G Knepley   . numCorners      - The number of vertices in the cell
2691e6ccafaeSMatthew G Knepley   . cell            - A cell in dm containing the face
2692e6ccafaeSMatthew G Knepley   . subcell         - A cell in subdm containing the face
2693e6ccafaeSMatthew G Knepley   . firstFace       - First face in the mesh
2694e6ccafaeSMatthew G Knepley   - newFacePoint    - Next face in the mesh
2695e6ccafaeSMatthew G Knepley 
2696e6ccafaeSMatthew G Knepley   Output Parameters:
2697e6ccafaeSMatthew G Knepley   . newFacePoint - Contains next face point number on input, updated on output
2698e6ccafaeSMatthew G Knepley 
2699e6ccafaeSMatthew G Knepley   Level: developer
2700e6ccafaeSMatthew G Knepley */
2701cd0c2139SMatthew 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)
2702e6ccafaeSMatthew G Knepley {
270382f516ccSBarry Smith   MPI_Comm        comm;
2704e6ccafaeSMatthew G Knepley   DM_Plex        *submesh = (DM_Plex*) subdm->data;
2705e6ccafaeSMatthew G Knepley   const PetscInt *faces;
2706e6ccafaeSMatthew G Knepley   PetscInt        numFaces, coneSize;
2707e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
2708e6ccafaeSMatthew G Knepley 
2709e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
271082f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2711e6ccafaeSMatthew G Knepley   ierr = DMPlexGetConeSize(subdm, subcell, &coneSize);CHKERRQ(ierr);
2712e6ccafaeSMatthew G Knepley   if (coneSize != 1) SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cone size of cell %d is %d != 1", cell, coneSize);
2713e6ccafaeSMatthew G Knepley #if 0
2714e6ccafaeSMatthew G Knepley   /* Cannot use this because support() has not been constructed yet */
2715e6ccafaeSMatthew G Knepley   ierr = DMPlexGetJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces);CHKERRQ(ierr);
2716e6ccafaeSMatthew G Knepley #else
2717e6ccafaeSMatthew G Knepley   {
2718e6ccafaeSMatthew G Knepley     PetscInt f;
2719e6ccafaeSMatthew G Knepley 
2720e6ccafaeSMatthew G Knepley     numFaces = 0;
272169291d52SBarry Smith     ierr     = DMGetWorkArray(subdm, 1, MPIU_INT, (void **) &faces);CHKERRQ(ierr);
2722e6ccafaeSMatthew G Knepley     for (f = firstFace; f < *newFacePoint; ++f) {
2723e6ccafaeSMatthew G Knepley       PetscInt dof, off, d;
2724e6ccafaeSMatthew G Knepley 
2725e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(submesh->coneSection, f, &dof);CHKERRQ(ierr);
2726e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetOffset(submesh->coneSection, f, &off);CHKERRQ(ierr);
2727e6ccafaeSMatthew G Knepley       /* Yes, I know this is quadratic, but I expect the sizes to be <5 */
2728e6ccafaeSMatthew G Knepley       for (d = 0; d < dof; ++d) {
2729e6ccafaeSMatthew G Knepley         const PetscInt p = submesh->cones[off+d];
2730e6ccafaeSMatthew G Knepley         PetscInt       v;
2731e6ccafaeSMatthew G Knepley 
2732e6ccafaeSMatthew G Knepley         for (v = 0; v < numFaceVertices; ++v) {
2733e6ccafaeSMatthew G Knepley           if (subfaceVertices[v] == p) break;
2734e6ccafaeSMatthew G Knepley         }
2735e6ccafaeSMatthew G Knepley         if (v == numFaceVertices) break;
2736e6ccafaeSMatthew G Knepley       }
2737e6ccafaeSMatthew G Knepley       if (d == dof) {
2738e6ccafaeSMatthew G Knepley         numFaces               = 1;
2739e6ccafaeSMatthew G Knepley         ((PetscInt*) faces)[0] = f;
2740e6ccafaeSMatthew G Knepley       }
2741e6ccafaeSMatthew G Knepley     }
2742e6ccafaeSMatthew G Knepley   }
2743e6ccafaeSMatthew G Knepley #endif
2744e6ccafaeSMatthew G Knepley   if (numFaces > 1) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Vertex set had %d faces, not one", numFaces);
2745e6ccafaeSMatthew G Knepley   else if (numFaces == 1) {
2746e6ccafaeSMatthew G Knepley     /* Add the other cell neighbor for this face */
2747766ab985SMatthew G. Knepley     ierr = DMPlexSetCone(subdm, subcell, faces);CHKERRQ(ierr);
2748e6ccafaeSMatthew G Knepley   } else {
2749e6ccafaeSMatthew G Knepley     PetscInt *indices, *origVertices, *orientedVertices, *orientedSubVertices, v, ov;
2750e6ccafaeSMatthew G Knepley     PetscBool posOriented;
2751e6ccafaeSMatthew G Knepley 
275269291d52SBarry Smith     ierr                = DMGetWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices);CHKERRQ(ierr);
2753e6ccafaeSMatthew G Knepley     origVertices        = &orientedVertices[numFaceVertices];
2754e6ccafaeSMatthew G Knepley     indices             = &orientedVertices[numFaceVertices*2];
2755e6ccafaeSMatthew G Knepley     orientedSubVertices = &orientedVertices[numFaceVertices*3];
2756e6ccafaeSMatthew G Knepley     ierr                = DMPlexGetOrientedFace(dm, cell, numFaceVertices, faceVertices, numCorners, indices, origVertices, orientedVertices, &posOriented);CHKERRQ(ierr);
2757e6ccafaeSMatthew G Knepley     /* TODO: I know that routine should return a permutation, not the indices */
2758e6ccafaeSMatthew G Knepley     for (v = 0; v < numFaceVertices; ++v) {
2759e6ccafaeSMatthew G Knepley       const PetscInt vertex = faceVertices[v], subvertex = subfaceVertices[v];
2760e6ccafaeSMatthew G Knepley       for (ov = 0; ov < numFaceVertices; ++ov) {
2761e6ccafaeSMatthew G Knepley         if (orientedVertices[ov] == vertex) {
2762e6ccafaeSMatthew G Knepley           orientedSubVertices[ov] = subvertex;
2763e6ccafaeSMatthew G Knepley           break;
2764e6ccafaeSMatthew G Knepley         }
2765e6ccafaeSMatthew G Knepley       }
2766e6ccafaeSMatthew G Knepley       if (ov == numFaceVertices) SETERRQ1(comm, PETSC_ERR_PLIB, "Could not find face vertex %d in orientated set", vertex);
2767e6ccafaeSMatthew G Knepley     }
2768e6ccafaeSMatthew G Knepley     ierr = DMPlexSetCone(subdm, *newFacePoint, orientedSubVertices);CHKERRQ(ierr);
2769e6ccafaeSMatthew G Knepley     ierr = DMPlexSetCone(subdm, subcell, newFacePoint);CHKERRQ(ierr);
277069291d52SBarry Smith     ierr = DMRestoreWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices);CHKERRQ(ierr);
2771e6ccafaeSMatthew G Knepley     ++(*newFacePoint);
2772e6ccafaeSMatthew G Knepley   }
2773ef07cca7SMatthew G. Knepley #if 0
2774e6ccafaeSMatthew G Knepley   ierr = DMPlexRestoreJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces);CHKERRQ(ierr);
2775ef07cca7SMatthew G. Knepley #else
277669291d52SBarry Smith   ierr = DMRestoreWorkArray(subdm, 1, MPIU_INT, (void **) &faces);CHKERRQ(ierr);
2777ef07cca7SMatthew G. Knepley #endif
2778e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2779e6ccafaeSMatthew G Knepley }
2780e6ccafaeSMatthew G Knepley 
278153156dfcSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm)
2782e6ccafaeSMatthew G Knepley {
278382f516ccSBarry Smith   MPI_Comm        comm;
278453156dfcSMatthew G. Knepley   DMLabel         subpointMap;
2785efa14ee0SMatthew G Knepley   IS              subvertexIS,  subcellIS;
2786efa14ee0SMatthew G Knepley   const PetscInt *subVertices, *subCells;
2787efa14ee0SMatthew G Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells;
2788fed694aaSMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces = 0, firstSubFace, newFacePoint, nFV = 0;
2789efa14ee0SMatthew G Knepley   PetscInt        vStart, vEnd, c, f;
2790e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
2791e6ccafaeSMatthew G Knepley 
2792e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
279382f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2794efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
2795d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap);CHKERRQ(ierr);
2796efa14ee0SMatthew G Knepley   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
2797efa14ee0SMatthew G Knepley   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
279853156dfcSMatthew G. Knepley   if (vertexLabel) {ierr = DMPlexMarkSubmesh_Uninterpolated(dm, vertexLabel, value, subpointMap, &numSubFaces, &nFV, subdm);CHKERRQ(ierr);}
2799efa14ee0SMatthew G Knepley   /* Setup chart */
2800efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumSize(subpointMap, 0, &numSubVertices);CHKERRQ(ierr);
2801efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumSize(subpointMap, 2, &numSubCells);CHKERRQ(ierr);
2802efa14ee0SMatthew G Knepley   ierr = DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices);CHKERRQ(ierr);
2803efa14ee0SMatthew G Knepley   ierr = DMPlexSetVTKCellHeight(subdm, 1);CHKERRQ(ierr);
2804e6ccafaeSMatthew G Knepley   /* Set cone sizes */
2805e6ccafaeSMatthew G Knepley   firstSubVertex = numSubCells;
2806efa14ee0SMatthew G Knepley   firstSubFace   = numSubCells+numSubVertices;
2807e6ccafaeSMatthew G Knepley   newFacePoint   = firstSubFace;
2808efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumIS(subpointMap, 0, &subvertexIS);CHKERRQ(ierr);
2809efa14ee0SMatthew G Knepley   if (subvertexIS) {ierr = ISGetIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
2810efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumIS(subpointMap, 2, &subcellIS);CHKERRQ(ierr);
2811efa14ee0SMatthew G Knepley   if (subcellIS) {ierr = ISGetIndices(subcellIS, &subCells);CHKERRQ(ierr);}
2812e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
2813e6ccafaeSMatthew G Knepley     ierr = DMPlexSetConeSize(subdm, c, 1);CHKERRQ(ierr);
2814e6ccafaeSMatthew G Knepley   }
2815e6ccafaeSMatthew G Knepley   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
2816e6ccafaeSMatthew G Knepley     ierr = DMPlexSetConeSize(subdm, f, nFV);CHKERRQ(ierr);
2817e6ccafaeSMatthew G Knepley   }
2818e6ccafaeSMatthew G Knepley   ierr = DMSetUp(subdm);CHKERRQ(ierr);
2819e6ccafaeSMatthew G Knepley   /* Create face cones */
2820efa14ee0SMatthew G Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
28210298fd71SBarry Smith   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
282269291d52SBarry Smith   ierr = DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
2823e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
2824e6ccafaeSMatthew G Knepley     const PetscInt cell    = subCells[c];
2825efa14ee0SMatthew G Knepley     const PetscInt subcell = c;
28260298fd71SBarry Smith     PetscInt      *closure = NULL;
2827efa14ee0SMatthew G Knepley     PetscInt       closureSize, cl, numCorners = 0, faceSize = 0;
2828e6ccafaeSMatthew G Knepley 
2829e6ccafaeSMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2830efa14ee0SMatthew G Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
2831efa14ee0SMatthew G Knepley       const PetscInt point = closure[cl];
2832e6ccafaeSMatthew G Knepley       PetscInt       subVertex;
2833e6ccafaeSMatthew G Knepley 
2834efa14ee0SMatthew G Knepley       if ((point >= vStart) && (point < vEnd)) {
2835efa14ee0SMatthew G Knepley         ++numCorners;
2836efa14ee0SMatthew G Knepley         ierr = PetscFindInt(point, numSubVertices, subVertices, &subVertex);CHKERRQ(ierr);
2837efa14ee0SMatthew G Knepley         if (subVertex >= 0) {
2838efa14ee0SMatthew G Knepley           closure[faceSize] = point;
283965560c7fSMatthew G Knepley           subface[faceSize] = firstSubVertex+subVertex;
2840e6ccafaeSMatthew G Knepley           ++faceSize;
2841e6ccafaeSMatthew G Knepley         }
2842e6ccafaeSMatthew G Knepley       }
2843e6ccafaeSMatthew G Knepley     }
2844efa14ee0SMatthew G Knepley     if (faceSize > nFV) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %d of an element on the surface", faceSize);
2845efa14ee0SMatthew G Knepley     if (faceSize == nFV) {
2846cd0c2139SMatthew G Knepley       ierr = DMPlexInsertFace_Internal(dm, subdm, faceSize, closure, subface, numCorners, cell, subcell, firstSubFace, &newFacePoint);CHKERRQ(ierr);
2847e6ccafaeSMatthew G Knepley     }
284865560c7fSMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2849e6ccafaeSMatthew G Knepley   }
285069291d52SBarry Smith   ierr = DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
2851e6ccafaeSMatthew G Knepley   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
2852e6ccafaeSMatthew G Knepley   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
2853e6ccafaeSMatthew G Knepley   /* Build coordinates */
2854efa14ee0SMatthew G Knepley   {
2855efa14ee0SMatthew G Knepley     PetscSection coordSection, subCoordSection;
2856efa14ee0SMatthew G Knepley     Vec          coordinates, subCoordinates;
2857efa14ee0SMatthew G Knepley     PetscScalar *coords, *subCoords;
2858285d324eSMatthew G. Knepley     PetscInt     numComp, coordSize, v;
285924640c55SToby Isaac     const char  *name;
2860efa14ee0SMatthew G Knepley 
286169d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
2862e6ccafaeSMatthew G Knepley     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
286369d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
2864285d324eSMatthew G. Knepley     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
2865285d324eSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
2866285d324eSMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
2867efa14ee0SMatthew G Knepley     ierr = PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices);CHKERRQ(ierr);
2868efa14ee0SMatthew G Knepley     for (v = 0; v < numSubVertices; ++v) {
2869efa14ee0SMatthew G Knepley       const PetscInt vertex    = subVertices[v];
2870efa14ee0SMatthew G Knepley       const PetscInt subvertex = firstSubVertex+v;
2871efa14ee0SMatthew G Knepley       PetscInt       dof;
2872efa14ee0SMatthew G Knepley 
2873efa14ee0SMatthew G Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
2874efa14ee0SMatthew G Knepley       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
2875285d324eSMatthew G. Knepley       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
2876e6ccafaeSMatthew G Knepley     }
2877e6ccafaeSMatthew G Knepley     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
2878e6ccafaeSMatthew G Knepley     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
28798b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &subCoordinates);CHKERRQ(ierr);
288024640c55SToby Isaac     ierr = PetscObjectGetName((PetscObject)coordinates,&name);CHKERRQ(ierr);
288124640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)subCoordinates,name);CHKERRQ(ierr);
2882e6ccafaeSMatthew G Knepley     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
28832eb5907fSJed Brown     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
2884830e53efSMatthew G. Knepley     if (coordSize) {
2885e6ccafaeSMatthew G Knepley       ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
2886e6ccafaeSMatthew G Knepley       ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
2887efa14ee0SMatthew G Knepley       for (v = 0; v < numSubVertices; ++v) {
2888efa14ee0SMatthew G Knepley         const PetscInt vertex    = subVertices[v];
2889efa14ee0SMatthew G Knepley         const PetscInt subvertex = firstSubVertex+v;
2890efa14ee0SMatthew G Knepley         PetscInt       dof, off, sdof, soff, d;
2891e6ccafaeSMatthew G Knepley 
2892e6ccafaeSMatthew G Knepley         ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
2893e6ccafaeSMatthew G Knepley         ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
2894efa14ee0SMatthew G Knepley         ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
2895efa14ee0SMatthew G Knepley         ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
2896efa14ee0SMatthew 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);
2897e6ccafaeSMatthew G Knepley         for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
2898e6ccafaeSMatthew G Knepley       }
2899e6ccafaeSMatthew G Knepley       ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
2900e6ccafaeSMatthew G Knepley       ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
29013b399e24SMatthew G. Knepley     }
2902e6ccafaeSMatthew G Knepley     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
2903e6ccafaeSMatthew G Knepley     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
2904e6ccafaeSMatthew G Knepley   }
2905efa14ee0SMatthew G Knepley   /* Cleanup */
2906efa14ee0SMatthew G Knepley   if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
2907efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
2908efa14ee0SMatthew G Knepley   if (subcellIS) {ierr = ISRestoreIndices(subcellIS, &subCells);CHKERRQ(ierr);}
2909efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subcellIS);CHKERRQ(ierr);
2910e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2911e6ccafaeSMatthew G Knepley }
2912e6ccafaeSMatthew G Knepley 
29133982b651SMatthew G. Knepley PETSC_STATIC_INLINE PetscInt DMPlexFilterPoint_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[])
29143982b651SMatthew G. Knepley {
29153982b651SMatthew G. Knepley   PetscInt       subPoint;
29163982b651SMatthew G. Knepley   PetscErrorCode ierr;
29173982b651SMatthew G. Knepley 
29183982b651SMatthew G. Knepley   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint); if (ierr < 0) return ierr;
29193982b651SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint+subPoint;
29203982b651SMatthew G. Knepley }
29213982b651SMatthew G. Knepley 
2922158acfadSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmeshGeneric_Interpolated(DM dm, DMLabel label, PetscInt value, PetscBool markedFaces, PetscBool isCohesive, PetscInt cellHeight, DM subdm)
2923e6ccafaeSMatthew G Knepley {
292482f516ccSBarry Smith   MPI_Comm         comm;
292553156dfcSMatthew G. Knepley   DMLabel          subpointMap;
2926efa14ee0SMatthew G Knepley   IS              *subpointIS;
2927efa14ee0SMatthew G Knepley   const PetscInt **subpoints;
29283982b651SMatthew G. Knepley   PetscInt        *numSubPoints, *firstSubPoint, *coneNew, *orntNew;
29290d366550SMatthew G. Knepley   PetscInt         totSubPoints = 0, maxConeSize, cMax, cEnd, dim, p, d, v;
29300d366550SMatthew G. Knepley   PetscMPIInt      rank;
2931e6ccafaeSMatthew G Knepley   PetscErrorCode   ierr;
2932e6ccafaeSMatthew G Knepley 
2933e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
293482f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
29350d366550SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
2936efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
2937d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap);CHKERRQ(ierr);
2938efa14ee0SMatthew G Knepley   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
2939bec263e5SMatthew G. Knepley   if (cellHeight) {
29403982b651SMatthew G. Knepley     if (isCohesive) {ierr = DMPlexMarkCohesiveSubmesh_Interpolated(dm, label, value, subpointMap, subdm);CHKERRQ(ierr);}
2941158acfadSMatthew G. Knepley     else            {ierr = DMPlexMarkSubmesh_Interpolated(dm, label, value, markedFaces, subpointMap, subdm);CHKERRQ(ierr);}
2942bec263e5SMatthew G. Knepley   } else {
2943bec263e5SMatthew G. Knepley     DMLabel         depth;
2944bec263e5SMatthew G. Knepley     IS              pointIS;
2945bec263e5SMatthew G. Knepley     const PetscInt *points;
2946bec263e5SMatthew G. Knepley     PetscInt        numPoints;
2947bec263e5SMatthew G. Knepley 
2948bec263e5SMatthew G. Knepley     ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
2949bec263e5SMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, value, &numPoints);CHKERRQ(ierr);
2950bec263e5SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, value, &pointIS);CHKERRQ(ierr);
2951bec263e5SMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
2952bec263e5SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2953bec263e5SMatthew G. Knepley       PetscInt *closure = NULL;
2954bec263e5SMatthew G. Knepley       PetscInt  closureSize, c, pdim;
2955bec263e5SMatthew G. Knepley 
2956bec263e5SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2957bec263e5SMatthew G. Knepley       for (c = 0; c < closureSize*2; c += 2) {
2958bec263e5SMatthew G. Knepley         ierr = DMLabelGetValue(depth, closure[c], &pdim);CHKERRQ(ierr);
2959bec263e5SMatthew G. Knepley         ierr = DMLabelSetValue(subpointMap, closure[c], pdim);CHKERRQ(ierr);
2960bec263e5SMatthew G. Knepley       }
2961bec263e5SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2962bec263e5SMatthew G. Knepley     }
2963bec263e5SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
2964bec263e5SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
2965bec263e5SMatthew G. Knepley   }
29660d366550SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
29670d366550SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
29680d366550SMatthew G. Knepley   cMax = (cMax < 0) ? cEnd : cMax;
2969efa14ee0SMatthew G Knepley   /* Setup chart */
2970c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2971dcca6d9dSJed Brown   ierr = PetscMalloc4(dim+1,&numSubPoints,dim+1,&firstSubPoint,dim+1,&subpointIS,dim+1,&subpoints);CHKERRQ(ierr);
2972e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
2973e6ccafaeSMatthew G Knepley     ierr = DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d]);CHKERRQ(ierr);
2974e6ccafaeSMatthew G Knepley     totSubPoints += numSubPoints[d];
2975e6ccafaeSMatthew G Knepley   }
2976e6ccafaeSMatthew G Knepley   ierr = DMPlexSetChart(subdm, 0, totSubPoints);CHKERRQ(ierr);
2977bec263e5SMatthew G. Knepley   ierr = DMPlexSetVTKCellHeight(subdm, cellHeight);CHKERRQ(ierr);
2978e6ccafaeSMatthew G Knepley   /* Set cone sizes */
2979e6ccafaeSMatthew G Knepley   firstSubPoint[dim] = 0;
2980e6ccafaeSMatthew G Knepley   firstSubPoint[0]   = firstSubPoint[dim] + numSubPoints[dim];
2981e6ccafaeSMatthew G Knepley   if (dim > 1) {firstSubPoint[dim-1] = firstSubPoint[0]     + numSubPoints[0];}
2982e6ccafaeSMatthew G Knepley   if (dim > 2) {firstSubPoint[dim-2] = firstSubPoint[dim-1] + numSubPoints[dim-1];}
2983e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
2984e6ccafaeSMatthew G Knepley     ierr = DMLabelGetStratumIS(subpointMap, d, &subpointIS[d]);CHKERRQ(ierr);
29850ae02aa4SMatthew G. Knepley     if (subpointIS[d]) {ierr = ISGetIndices(subpointIS[d], &subpoints[d]);CHKERRQ(ierr);}
2986e6ccafaeSMatthew G Knepley   }
2987e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
2988e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
2989e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
2990e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
2991e6ccafaeSMatthew G Knepley       const PetscInt *cone;
2992e6ccafaeSMatthew G Knepley       PetscInt        coneSize, coneSizeNew, c, val;
2993e6ccafaeSMatthew G Knepley 
2994e6ccafaeSMatthew G Knepley       ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
2995e6ccafaeSMatthew G Knepley       ierr = DMPlexSetConeSize(subdm, subpoint, coneSize);CHKERRQ(ierr);
2996bec263e5SMatthew G. Knepley       if (cellHeight && (d == dim)) {
2997e6ccafaeSMatthew G Knepley         ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
2998e6ccafaeSMatthew G Knepley         for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
2999e6ccafaeSMatthew G Knepley           ierr = DMLabelGetValue(subpointMap, cone[c], &val);CHKERRQ(ierr);
3000e6ccafaeSMatthew G Knepley           if (val >= 0) coneSizeNew++;
3001e6ccafaeSMatthew G Knepley         }
3002e6ccafaeSMatthew G Knepley         ierr = DMPlexSetConeSize(subdm, subpoint, coneSizeNew);CHKERRQ(ierr);
3003e6ccafaeSMatthew G Knepley       }
3004e6ccafaeSMatthew G Knepley     }
3005e6ccafaeSMatthew G Knepley   }
3006d67d17b1SMatthew G. Knepley   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
3007e6ccafaeSMatthew G Knepley   ierr = DMSetUp(subdm);CHKERRQ(ierr);
3008e6ccafaeSMatthew G Knepley   /* Set cones */
30090298fd71SBarry Smith   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
30103982b651SMatthew G. Knepley   ierr = PetscMalloc2(maxConeSize,&coneNew,maxConeSize,&orntNew);CHKERRQ(ierr);
3011e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
3012e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3013e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3014e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
30150e49e2e2SMatthew G. Knepley       const PetscInt *cone, *ornt;
30160d366550SMatthew G. Knepley       PetscInt        coneSize, subconeSize, coneSizeNew, c, subc, fornt = 0;
3017e6ccafaeSMatthew G Knepley 
30180d366550SMatthew G. Knepley       if (d == dim-1) {
30190d366550SMatthew G. Knepley         const PetscInt *support, *cone, *ornt;
30200d366550SMatthew G. Knepley         PetscInt        supportSize, coneSize, s, subc;
30210d366550SMatthew G. Knepley 
30220d366550SMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
30230d366550SMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
30240d366550SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
30250d366550SMatthew G. Knepley           if ((support[s] < cMax) || (support[s] >= cEnd)) continue;
30260d366550SMatthew G. Knepley           ierr = PetscFindInt(support[s], numSubPoints[d+1], subpoints[d+1], &subc);CHKERRQ(ierr);
30270d366550SMatthew G. Knepley           if (subc >= 0) {
30280d366550SMatthew G. Knepley             const PetscInt ccell = subpoints[d+1][subc];
30290d366550SMatthew G. Knepley 
30307f79407eSBarry Smith             ierr = DMPlexGetCone(dm, ccell, &cone);CHKERRQ(ierr);
30317f79407eSBarry Smith             ierr = DMPlexGetConeSize(dm, ccell, &coneSize);CHKERRQ(ierr);
30327f79407eSBarry Smith             ierr = DMPlexGetConeOrientation(dm, ccell, &ornt);CHKERRQ(ierr);
30330d366550SMatthew G. Knepley             for (c = 0; c < coneSize; ++c) {
30340d366550SMatthew G. Knepley               if (cone[c] == point) {
30350d366550SMatthew G. Knepley                 fornt = ornt[c];
30360d366550SMatthew G. Knepley                 break;
30370d366550SMatthew G. Knepley               }
30380d366550SMatthew G. Knepley             }
30390d366550SMatthew G. Knepley             break;
30400d366550SMatthew G. Knepley           }
30410d366550SMatthew G. Knepley         }
30420d366550SMatthew G. Knepley       }
3043e6ccafaeSMatthew G Knepley       ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
3044e6ccafaeSMatthew G Knepley       ierr = DMPlexGetConeSize(subdm, subpoint, &subconeSize);CHKERRQ(ierr);
3045e6ccafaeSMatthew G Knepley       ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
30460e49e2e2SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, point, &ornt);CHKERRQ(ierr);
3047e6ccafaeSMatthew G Knepley       for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
3048e6ccafaeSMatthew G Knepley         ierr = PetscFindInt(cone[c], numSubPoints[d-1], subpoints[d-1], &subc);CHKERRQ(ierr);
304901a2673eSMatthew G. Knepley         if (subc >= 0) {
305001a2673eSMatthew G. Knepley           coneNew[coneSizeNew] = firstSubPoint[d-1] + subc;
30513982b651SMatthew G. Knepley           orntNew[coneSizeNew] = ornt[c];
305201a2673eSMatthew G. Knepley           ++coneSizeNew;
305301a2673eSMatthew G. Knepley         }
3054e6ccafaeSMatthew G Knepley       }
3055e6ccafaeSMatthew G Knepley       if (coneSizeNew != subconeSize) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of cone points located %d does not match subcone size %d", coneSizeNew, subconeSize);
30560d366550SMatthew G. Knepley       if (fornt < 0) {
30570d366550SMatthew G. Knepley         /* This should be replaced by a call to DMPlexReverseCell() */
30580d366550SMatthew G. Knepley #if 0
3059a129c400SMatthew G. Knepley         ierr = DMPlexReverseCell(subdm, subpoint);CHKERRQ(ierr);
3060a129c400SMatthew G. Knepley #else
3061c0b40891SMatthew G. Knepley         for (c = 0; c < coneSizeNew/2 + coneSizeNew%2; ++c) {
3062a129c400SMatthew G. Knepley           PetscInt faceSize, tmp;
30630d366550SMatthew G. Knepley 
30640d366550SMatthew G. Knepley           tmp        = coneNew[c];
30650d366550SMatthew G. Knepley           coneNew[c] = coneNew[coneSizeNew-1-c];
30660d366550SMatthew G. Knepley           coneNew[coneSizeNew-1-c] = tmp;
3067a129c400SMatthew G. Knepley           ierr = DMPlexGetConeSize(dm, cone[c], &faceSize);CHKERRQ(ierr);
3068a129c400SMatthew G. Knepley           tmp        = orntNew[c] >= 0 ? -(faceSize-orntNew[c]) : faceSize+orntNew[c];
3069a129c400SMatthew G. Knepley           orntNew[c] = orntNew[coneSizeNew-1-c] >= 0 ? -(faceSize-orntNew[coneSizeNew-1-c]) : faceSize+orntNew[coneSizeNew-1-c];
30700d366550SMatthew G. Knepley           orntNew[coneSizeNew-1-c] = tmp;
30710d366550SMatthew G. Knepley         }
30720d366550SMatthew G. Knepley       }
3073e6ccafaeSMatthew G Knepley       ierr = DMPlexSetCone(subdm, subpoint, coneNew);CHKERRQ(ierr);
30743982b651SMatthew G. Knepley       ierr = DMPlexSetConeOrientation(subdm, subpoint, orntNew);CHKERRQ(ierr);
3075a129c400SMatthew G. Knepley #endif
3076e6ccafaeSMatthew G Knepley     }
3077e6ccafaeSMatthew G Knepley   }
30783982b651SMatthew G. Knepley   ierr = PetscFree2(coneNew,orntNew);CHKERRQ(ierr);
3079e6ccafaeSMatthew G Knepley   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
3080e6ccafaeSMatthew G Knepley   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
3081e6ccafaeSMatthew G Knepley   /* Build coordinates */
3082e6ccafaeSMatthew G Knepley   {
3083e6ccafaeSMatthew G Knepley     PetscSection coordSection, subCoordSection;
3084e6ccafaeSMatthew G Knepley     Vec          coordinates, subCoordinates;
3085e6ccafaeSMatthew G Knepley     PetscScalar *coords, *subCoords;
3086c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize;
308724640c55SToby Isaac     const char  *name;
3088e6ccafaeSMatthew G Knepley 
3089c0e8cf5fSMatthew G. Knepley     ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
309069d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
3091e6ccafaeSMatthew G Knepley     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
309269d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
3093285d324eSMatthew G. Knepley     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
3094285d324eSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
3095285d324eSMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
3096e6ccafaeSMatthew G Knepley     ierr = PetscSectionSetChart(subCoordSection, firstSubPoint[0], firstSubPoint[0]+numSubPoints[0]);CHKERRQ(ierr);
3097e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3098e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3099e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0]+v;
3100e6ccafaeSMatthew G Knepley       PetscInt       dof;
3101e6ccafaeSMatthew G Knepley 
3102e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3103e6ccafaeSMatthew G Knepley       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
3104285d324eSMatthew G. Knepley       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
3105e6ccafaeSMatthew G Knepley     }
3106e6ccafaeSMatthew G Knepley     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
3107e6ccafaeSMatthew G Knepley     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
31088b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &subCoordinates);CHKERRQ(ierr);
310924640c55SToby Isaac     ierr = PetscObjectGetName((PetscObject)coordinates,&name);CHKERRQ(ierr);
311024640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)subCoordinates,name);CHKERRQ(ierr);
3111e6ccafaeSMatthew G Knepley     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
3112c0e8cf5fSMatthew G. Knepley     ierr = VecSetBlockSize(subCoordinates, cdim);CHKERRQ(ierr);
31132eb5907fSJed Brown     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
3114e6ccafaeSMatthew G Knepley     ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
3115e6ccafaeSMatthew G Knepley     ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3116e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3117e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3118e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0]+v;
3119e6ccafaeSMatthew G Knepley       PetscInt dof, off, sdof, soff, d;
3120e6ccafaeSMatthew G Knepley 
3121e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3122e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
3123e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
3124e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
3125e6ccafaeSMatthew 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);
3126efa14ee0SMatthew G Knepley       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3127e6ccafaeSMatthew G Knepley     }
3128e6ccafaeSMatthew G Knepley     ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
3129e6ccafaeSMatthew G Knepley     ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3130e6ccafaeSMatthew G Knepley     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
3131e6ccafaeSMatthew G Knepley     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
3132e6ccafaeSMatthew G Knepley   }
31333982b651SMatthew G. Knepley   /* Build SF: We need this complexity because subpoints might not be selected on the owning process */
31343982b651SMatthew G. Knepley   {
31353982b651SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
31363982b651SMatthew G. Knepley     IS                 subpIS;
31373982b651SMatthew G. Knepley     const PetscSFNode *remotePoints;
31383982b651SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
31393982b651SMatthew G. Knepley     const PetscInt    *localPoints, *subpoints;
31403982b651SMatthew G. Knepley     PetscInt          *slocalPoints;
31413982b651SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubpoints = 0, numSubroots, numSubleaves = 0, l, sl, ll, pStart, pEnd, p;
31423982b651SMatthew G. Knepley     PetscMPIInt        rank;
31433982b651SMatthew G. Knepley 
31443982b651SMatthew G. Knepley     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
31453982b651SMatthew G. Knepley     ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
31463982b651SMatthew G. Knepley     ierr = DMGetPointSF(subdm, &sfPointSub);CHKERRQ(ierr);
31473982b651SMatthew G. Knepley     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
31483982b651SMatthew G. Knepley     ierr = DMPlexGetChart(subdm, NULL, &numSubroots);CHKERRQ(ierr);
31493982b651SMatthew G. Knepley     ierr = DMPlexCreateSubpointIS(subdm, &subpIS);CHKERRQ(ierr);
31503982b651SMatthew G. Knepley     if (subpIS) {
31513982b651SMatthew G. Knepley       ierr = ISGetIndices(subpIS, &subpoints);CHKERRQ(ierr);
31523982b651SMatthew G. Knepley       ierr = ISGetLocalSize(subpIS, &numSubpoints);CHKERRQ(ierr);
31533982b651SMatthew G. Knepley     }
31543982b651SMatthew G. Knepley     ierr = PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr);
31553982b651SMatthew G. Knepley     if (numRoots >= 0) {
31563982b651SMatthew G. Knepley       ierr = PetscMalloc2(pEnd-pStart,&newLocalPoints,numRoots,&newOwners);CHKERRQ(ierr);
31573982b651SMatthew G. Knepley       for (p = 0; p < pEnd-pStart; ++p) {
31583982b651SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
31593982b651SMatthew G. Knepley         newLocalPoints[p].index = -2;
31603982b651SMatthew G. Knepley       }
31613982b651SMatthew G. Knepley       /* Set subleaves */
31623982b651SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
31633982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
31643982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(point, 0, numSubpoints, subpoints);
31653982b651SMatthew G. Knepley 
31663982b651SMatthew G. Knepley         if (subpoint < 0) continue;
31673982b651SMatthew G. Knepley         newLocalPoints[point-pStart].rank  = rank;
31683982b651SMatthew G. Knepley         newLocalPoints[point-pStart].index = subpoint;
31693982b651SMatthew G. Knepley         ++numSubleaves;
31703982b651SMatthew G. Knepley       }
31713982b651SMatthew G. Knepley       /* Must put in owned subpoints */
31723982b651SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
31733982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(p, 0, numSubpoints, subpoints);
31743982b651SMatthew G. Knepley 
31753982b651SMatthew G. Knepley         if (subpoint < 0) {
31763982b651SMatthew G. Knepley           newOwners[p-pStart].rank  = -3;
31773982b651SMatthew G. Knepley           newOwners[p-pStart].index = -3;
31783982b651SMatthew G. Knepley         } else {
31793982b651SMatthew G. Knepley           newOwners[p-pStart].rank  = rank;
31803982b651SMatthew G. Knepley           newOwners[p-pStart].index = subpoint;
31813982b651SMatthew G. Knepley         }
31823982b651SMatthew G. Knepley       }
31833982b651SMatthew G. Knepley       ierr = PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
31843982b651SMatthew G. Knepley       ierr = PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
31853982b651SMatthew G. Knepley       ierr = PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr);
31863982b651SMatthew G. Knepley       ierr = PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr);
31873982b651SMatthew G. Knepley       ierr = PetscMalloc1(numSubleaves, &slocalPoints);CHKERRQ(ierr);
31883982b651SMatthew G. Knepley       ierr = PetscMalloc1(numSubleaves, &sremotePoints);CHKERRQ(ierr);
31893982b651SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
31903982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
31913982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(point, 0, numSubpoints, subpoints);
31923982b651SMatthew G. Knepley 
31933982b651SMatthew G. Knepley         if (subpoint < 0) continue;
31943982b651SMatthew G. Knepley         if (newLocalPoints[point].rank == rank) {++ll; continue;}
31953982b651SMatthew G. Knepley         slocalPoints[sl]        = subpoint;
31963982b651SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
31973982b651SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
31983982b651SMatthew G. Knepley         if (sremotePoints[sl].rank  < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %d", point);
31993982b651SMatthew G. Knepley         if (sremotePoints[sl].index < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %d", point);
32003982b651SMatthew G. Knepley         ++sl;
32013982b651SMatthew G. Knepley       }
32023982b651SMatthew G. Knepley       if (sl + ll != numSubleaves) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %d + %d != %d", sl, ll, numSubleaves);
32033982b651SMatthew G. Knepley       ierr = PetscFree2(newLocalPoints,newOwners);CHKERRQ(ierr);
32043982b651SMatthew G. Knepley       ierr = PetscSFSetGraph(sfPointSub, numSubroots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
32053982b651SMatthew G. Knepley     }
32063982b651SMatthew G. Knepley     if (subpIS) {
32073982b651SMatthew G. Knepley       ierr = ISRestoreIndices(subpIS, &subpoints);CHKERRQ(ierr);
32083982b651SMatthew G. Knepley       ierr = ISDestroy(&subpIS);CHKERRQ(ierr);
32093982b651SMatthew G. Knepley     }
32103982b651SMatthew G. Knepley   }
3211efa14ee0SMatthew G Knepley   /* Cleanup */
3212e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
32130ae02aa4SMatthew G. Knepley     if (subpointIS[d]) {ierr = ISRestoreIndices(subpointIS[d], &subpoints[d]);CHKERRQ(ierr);}
3214e6ccafaeSMatthew G Knepley     ierr = ISDestroy(&subpointIS[d]);CHKERRQ(ierr);
3215e6ccafaeSMatthew G Knepley   }
3216efa14ee0SMatthew G Knepley   ierr = PetscFree4(numSubPoints,firstSubPoint,subpointIS,subpoints);CHKERRQ(ierr);
3217e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3218e6ccafaeSMatthew G Knepley }
3219e6ccafaeSMatthew G Knepley 
3220158acfadSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM subdm)
32213982b651SMatthew G. Knepley {
32223982b651SMatthew G. Knepley   PetscErrorCode ierr;
32233982b651SMatthew G. Knepley 
32243982b651SMatthew G. Knepley   PetscFunctionBegin;
3225158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmeshGeneric_Interpolated(dm, vertexLabel, value, markedFaces, PETSC_FALSE, 1, subdm);CHKERRQ(ierr);
32263982b651SMatthew G. Knepley   PetscFunctionReturn(0);
32273982b651SMatthew G. Knepley }
32283982b651SMatthew G. Knepley 
3229d0fa310fSMatthew G. Knepley /*@
3230e6ccafaeSMatthew G Knepley   DMPlexCreateSubmesh - Extract a hypersurface from the mesh using vertices defined by a label
3231e6ccafaeSMatthew G Knepley 
3232e6ccafaeSMatthew G Knepley   Input Parameters:
3233e6ccafaeSMatthew G Knepley + dm           - The original mesh
3234158acfadSMatthew G. Knepley . vertexLabel  - The DMLabel marking points contained in the surface
3235158acfadSMatthew G. Knepley . value        - The label value to use
3236158acfadSMatthew G. Knepley - markedFaces  - PETSC_TRUE if surface faces are marked in addition to vertices, PETSC_FALSE if only vertices are marked
3237e6ccafaeSMatthew G Knepley 
3238e6ccafaeSMatthew G Knepley   Output Parameter:
3239e6ccafaeSMatthew G Knepley . subdm - The surface mesh
3240e6ccafaeSMatthew G Knepley 
3241e6ccafaeSMatthew G Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3242e6ccafaeSMatthew G Knepley 
3243e6ccafaeSMatthew G Knepley   Level: developer
3244e6ccafaeSMatthew G Knepley 
3245c58f1c22SToby Isaac .seealso: DMPlexGetSubpointMap(), DMGetLabel(), DMLabelSetValue()
3246830e53efSMatthew G. Knepley @*/
3247158acfadSMatthew G. Knepley PetscErrorCode DMPlexCreateSubmesh(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM *subdm)
3248e6ccafaeSMatthew G Knepley {
3249dd2763adSMatthew G. Knepley   PetscInt       dim, cdim, depth;
3250e6ccafaeSMatthew G Knepley   PetscErrorCode ierr;
3251e6ccafaeSMatthew G Knepley 
3252e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3253e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3254766ab985SMatthew G. Knepley   PetscValidPointer(subdm, 3);
3255c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3256e6ccafaeSMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
325782f516ccSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)dm), subdm);CHKERRQ(ierr);
3258e6ccafaeSMatthew G Knepley   ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr);
3259c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(*subdm, dim-1);CHKERRQ(ierr);
3260dd2763adSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3261dd2763adSMatthew G. Knepley   ierr = DMSetCoordinateDim(*subdm, cdim);CHKERRQ(ierr);
3262e6ccafaeSMatthew G Knepley   if (depth == dim) {
3263158acfadSMatthew G. Knepley     ierr = DMPlexCreateSubmesh_Interpolated(dm, vertexLabel, value, markedFaces, *subdm);CHKERRQ(ierr);
3264e6ccafaeSMatthew G Knepley   } else {
3265830e53efSMatthew G. Knepley     ierr = DMPlexCreateSubmesh_Uninterpolated(dm, vertexLabel, value, *subdm);CHKERRQ(ierr);
3266e6ccafaeSMatthew G Knepley   }
3267e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3268e6ccafaeSMatthew G Knepley }
3269e6ccafaeSMatthew G Knepley 
327027c04023SMatthew G. Knepley static PetscErrorCode DMPlexCreateCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM subdm)
3271766ab985SMatthew G. Knepley {
3272766ab985SMatthew G. Knepley   MPI_Comm        comm;
3273766ab985SMatthew G. Knepley   DMLabel         subpointMap;
3274766ab985SMatthew G. Knepley   IS              subvertexIS;
3275766ab985SMatthew G. Knepley   const PetscInt *subVertices;
3276fed694aaSMatthew G. Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells, *subCells = NULL;
3277766ab985SMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces, firstSubFace, newFacePoint, nFV;
327887feddfdSMatthew G. Knepley   PetscInt        cMax, c, f;
3279766ab985SMatthew G. Knepley   PetscErrorCode  ierr;
3280766ab985SMatthew G. Knepley 
3281766ab985SMatthew G. Knepley   PetscFunctionBegin;
3282766ab985SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
3283766ab985SMatthew G. Knepley   /* Create subpointMap which marks the submesh */
3284d67d17b1SMatthew G. Knepley   ierr = DMLabelCreate(PETSC_COMM_SELF, "subpoint_map", &subpointMap);CHKERRQ(ierr);
3285766ab985SMatthew G. Knepley   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
3286766ab985SMatthew G. Knepley   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
328727c04023SMatthew G. Knepley   ierr = DMPlexMarkCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, subpointMap, &numSubFaces, &nFV, &subCells, subdm);CHKERRQ(ierr);
3288766ab985SMatthew G. Knepley   /* Setup chart */
3289766ab985SMatthew G. Knepley   ierr = DMLabelGetStratumSize(subpointMap, 0, &numSubVertices);CHKERRQ(ierr);
3290766ab985SMatthew G. Knepley   ierr = DMLabelGetStratumSize(subpointMap, 2, &numSubCells);CHKERRQ(ierr);
3291766ab985SMatthew G. Knepley   ierr = DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices);CHKERRQ(ierr);
3292766ab985SMatthew G. Knepley   ierr = DMPlexSetVTKCellHeight(subdm, 1);CHKERRQ(ierr);
3293766ab985SMatthew G. Knepley   /* Set cone sizes */
3294766ab985SMatthew G. Knepley   firstSubVertex = numSubCells;
3295766ab985SMatthew G. Knepley   firstSubFace   = numSubCells+numSubVertices;
3296766ab985SMatthew G. Knepley   newFacePoint   = firstSubFace;
3297766ab985SMatthew G. Knepley   ierr = DMLabelGetStratumIS(subpointMap, 0, &subvertexIS);CHKERRQ(ierr);
3298766ab985SMatthew G. Knepley   if (subvertexIS) {ierr = ISGetIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
3299766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3300766ab985SMatthew G. Knepley     ierr = DMPlexSetConeSize(subdm, c, 1);CHKERRQ(ierr);
3301766ab985SMatthew G. Knepley   }
3302766ab985SMatthew G. Knepley   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
3303766ab985SMatthew G. Knepley     ierr = DMPlexSetConeSize(subdm, f, nFV);CHKERRQ(ierr);
3304766ab985SMatthew G. Knepley   }
3305766ab985SMatthew G. Knepley   ierr = DMSetUp(subdm);CHKERRQ(ierr);
3306766ab985SMatthew G. Knepley   /* Create face cones */
3307766ab985SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
330887feddfdSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
330969291d52SBarry Smith   ierr = DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
3310766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3311766ab985SMatthew G. Knepley     const PetscInt  cell    = subCells[c];
3312766ab985SMatthew G. Knepley     const PetscInt  subcell = c;
331387feddfdSMatthew G. Knepley     const PetscInt *cone, *cells;
331487feddfdSMatthew G. Knepley     PetscInt        numCells, subVertex, p, v;
3315766ab985SMatthew G. Knepley 
331687feddfdSMatthew G. Knepley     if (cell < cMax) continue;
331787feddfdSMatthew G. Knepley     ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
331887feddfdSMatthew G. Knepley     for (v = 0; v < nFV; ++v) {
331987feddfdSMatthew G. Knepley       ierr = PetscFindInt(cone[v], numSubVertices, subVertices, &subVertex);CHKERRQ(ierr);
332087feddfdSMatthew G. Knepley       subface[v] = firstSubVertex+subVertex;
332187feddfdSMatthew G. Knepley     }
332287feddfdSMatthew G. Knepley     ierr = DMPlexSetCone(subdm, newFacePoint, subface);CHKERRQ(ierr);
332387feddfdSMatthew G. Knepley     ierr = DMPlexSetCone(subdm, subcell, &newFacePoint);CHKERRQ(ierr);
332487feddfdSMatthew G. Knepley     ierr = DMPlexGetJoin(dm, nFV, cone, &numCells, &cells);CHKERRQ(ierr);
332527234c99SMatthew G. Knepley     /* Not true in parallel
332627234c99SMatthew G. Knepley     if (numCells != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
332787feddfdSMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
332887feddfdSMatthew G. Knepley       PetscInt negsubcell;
3329766ab985SMatthew G. Knepley 
333087feddfdSMatthew G. Knepley       if (cells[p] >= cMax) continue;
333187feddfdSMatthew G. Knepley       /* I know this is a crap search */
333287feddfdSMatthew G. Knepley       for (negsubcell = 0; negsubcell < numSubCells; ++negsubcell) {
333387feddfdSMatthew G. Knepley         if (subCells[negsubcell] == cells[p]) break;
3334766ab985SMatthew G. Knepley       }
333587feddfdSMatthew G. Knepley       if (negsubcell == numSubCells) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find negative face neighbor for cohesive cell %d", cell);
333687feddfdSMatthew G. Knepley       ierr = DMPlexSetCone(subdm, negsubcell, &newFacePoint);CHKERRQ(ierr);
3337766ab985SMatthew G. Knepley     }
333887feddfdSMatthew G. Knepley     ierr = DMPlexRestoreJoin(dm, nFV, cone, &numCells, &cells);CHKERRQ(ierr);
333987feddfdSMatthew G. Knepley     ++newFacePoint;
3340766ab985SMatthew G. Knepley   }
334169291d52SBarry Smith   ierr = DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
3342766ab985SMatthew G. Knepley   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
3343766ab985SMatthew G. Knepley   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
3344766ab985SMatthew G. Knepley   /* Build coordinates */
3345766ab985SMatthew G. Knepley   {
3346766ab985SMatthew G. Knepley     PetscSection coordSection, subCoordSection;
3347766ab985SMatthew G. Knepley     Vec          coordinates, subCoordinates;
3348766ab985SMatthew G. Knepley     PetscScalar *coords, *subCoords;
3349c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize, v;
335024640c55SToby Isaac     const char  *name;
3351766ab985SMatthew G. Knepley 
3352c0e8cf5fSMatthew G. Knepley     ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
335369d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
3354766ab985SMatthew G. Knepley     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
335569d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
3356285d324eSMatthew G. Knepley     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
3357285d324eSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
3358285d324eSMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
3359766ab985SMatthew G. Knepley     ierr = PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices);CHKERRQ(ierr);
3360766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3361766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3362766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex+v;
3363766ab985SMatthew G. Knepley       PetscInt       dof;
3364766ab985SMatthew G. Knepley 
3365766ab985SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3366766ab985SMatthew G. Knepley       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
3367285d324eSMatthew G. Knepley       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
3368766ab985SMatthew G. Knepley     }
3369766ab985SMatthew G. Knepley     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
3370766ab985SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
33718b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &subCoordinates);CHKERRQ(ierr);
337224640c55SToby Isaac     ierr = PetscObjectGetName((PetscObject)coordinates,&name);CHKERRQ(ierr);
337324640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)subCoordinates,name);CHKERRQ(ierr);
3374766ab985SMatthew G. Knepley     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
3375c0e8cf5fSMatthew G. Knepley     ierr = VecSetBlockSize(subCoordinates, cdim);CHKERRQ(ierr);
33762eb5907fSJed Brown     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
3377766ab985SMatthew G. Knepley     ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
3378766ab985SMatthew G. Knepley     ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3379766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3380766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3381766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex+v;
3382766ab985SMatthew G. Knepley       PetscInt       dof, off, sdof, soff, d;
3383766ab985SMatthew G. Knepley 
3384766ab985SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3385766ab985SMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
3386766ab985SMatthew G. Knepley       ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
3387766ab985SMatthew G. Knepley       ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
3388766ab985SMatthew 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);
3389766ab985SMatthew G. Knepley       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3390766ab985SMatthew G. Knepley     }
3391766ab985SMatthew G. Knepley     ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
3392766ab985SMatthew G. Knepley     ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3393766ab985SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
3394766ab985SMatthew G. Knepley     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
3395766ab985SMatthew G. Knepley   }
3396aca35d17SMatthew G. Knepley   /* Build SF */
3397aca35d17SMatthew G. Knepley   CHKMEMQ;
3398aca35d17SMatthew G. Knepley   {
3399aca35d17SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
3400aca35d17SMatthew G. Knepley     const PetscSFNode *remotePoints;
3401bdcf2095SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
3402aca35d17SMatthew G. Knepley     const PetscInt    *localPoints;
3403bdcf2095SMatthew G. Knepley     PetscInt          *slocalPoints;
340449c26ae4SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubRoots = numSubCells+numSubFaces+numSubVertices, numSubLeaves = 0, l, sl, ll, pStart, pEnd, p, vStart, vEnd;
3405bdcf2095SMatthew G. Knepley     PetscMPIInt        rank;
3406aca35d17SMatthew G. Knepley 
3407bdcf2095SMatthew G. Knepley     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
3408aca35d17SMatthew G. Knepley     ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
3409aca35d17SMatthew G. Knepley     ierr = DMGetPointSF(subdm, &sfPointSub);CHKERRQ(ierr);
3410aca35d17SMatthew G. Knepley     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3411aca35d17SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
3412aca35d17SMatthew G. Knepley     ierr = PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr);
3413aca35d17SMatthew G. Knepley     if (numRoots >= 0) {
3414aca35d17SMatthew G. Knepley       /* Only vertices should be shared */
3415dcca6d9dSJed Brown       ierr = PetscMalloc2(pEnd-pStart,&newLocalPoints,numRoots,&newOwners);CHKERRQ(ierr);
3416bdcf2095SMatthew G. Knepley       for (p = 0; p < pEnd-pStart; ++p) {
3417bdcf2095SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
3418bdcf2095SMatthew G. Knepley         newLocalPoints[p].index = -2;
3419bdcf2095SMatthew G. Knepley       }
34209e0823b2SMatthew G. Knepley       /* Set subleaves */
3421aca35d17SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
3422aca35d17SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3423bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3424aca35d17SMatthew G. Knepley 
3425aca35d17SMatthew G. Knepley         if ((point < vStart) && (point >= vEnd)) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Should not be mapping anything but vertices, %d", point);
3426bdcf2095SMatthew G. Knepley         if (subPoint < 0) continue;
3427bdcf2095SMatthew G. Knepley         newLocalPoints[point-pStart].rank  = rank;
3428bdcf2095SMatthew G. Knepley         newLocalPoints[point-pStart].index = subPoint;
3429bdcf2095SMatthew G. Knepley         ++numSubLeaves;
3430aca35d17SMatthew G. Knepley       }
34319e0823b2SMatthew G. Knepley       /* Must put in owned subpoints */
34329e0823b2SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
34339e0823b2SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(p, firstSubVertex, numSubVertices, subVertices);
34349e0823b2SMatthew G. Knepley 
34359e0823b2SMatthew G. Knepley         if (subPoint < 0) {
34369e0823b2SMatthew G. Knepley           newOwners[p-pStart].rank  = -3;
34379e0823b2SMatthew G. Knepley           newOwners[p-pStart].index = -3;
34389e0823b2SMatthew G. Knepley         } else {
34399e0823b2SMatthew G. Knepley           newOwners[p-pStart].rank  = rank;
34409e0823b2SMatthew G. Knepley           newOwners[p-pStart].index = subPoint;
34419e0823b2SMatthew G. Knepley         }
3442bdcf2095SMatthew G. Knepley       }
3443bdcf2095SMatthew G. Knepley       ierr = PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
3444bdcf2095SMatthew G. Knepley       ierr = PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
3445bdcf2095SMatthew G. Knepley       ierr = PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr);
3446bdcf2095SMatthew G. Knepley       ierr = PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr);
3447785e854fSJed Brown       ierr = PetscMalloc1(numSubLeaves,    &slocalPoints);CHKERRQ(ierr);
3448785e854fSJed Brown       ierr = PetscMalloc1(numSubLeaves, &sremotePoints);CHKERRQ(ierr);
344949c26ae4SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
3450bdcf2095SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3451bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3452aca35d17SMatthew G. Knepley 
3453aca35d17SMatthew G. Knepley         if (subPoint < 0) continue;
345449c26ae4SMatthew G. Knepley         if (newLocalPoints[point].rank == rank) {++ll; continue;}
3455aca35d17SMatthew G. Knepley         slocalPoints[sl]        = subPoint;
3456bdcf2095SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
3457bdcf2095SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
3458bdcf2095SMatthew G. Knepley         if (sremotePoints[sl].rank  < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %d", point);
3459bdcf2095SMatthew G. Knepley         if (sremotePoints[sl].index < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %d", point);
3460aca35d17SMatthew G. Knepley         ++sl;
3461aca35d17SMatthew G. Knepley       }
3462bdcf2095SMatthew G. Knepley       ierr = PetscFree2(newLocalPoints,newOwners);CHKERRQ(ierr);
346349c26ae4SMatthew G. Knepley       if (sl + ll != numSubLeaves) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %d + %d != %d", sl, ll, numSubLeaves);
346449c26ae4SMatthew G. Knepley       ierr = PetscSFSetGraph(sfPointSub, numSubRoots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
3465aca35d17SMatthew G. Knepley     }
3466aca35d17SMatthew G. Knepley   }
3467aca35d17SMatthew G. Knepley   CHKMEMQ;
3468766ab985SMatthew G. Knepley   /* Cleanup */
3469766ab985SMatthew G. Knepley   if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
3470766ab985SMatthew G. Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
3471766ab985SMatthew G. Knepley   ierr = PetscFree(subCells);CHKERRQ(ierr);
3472766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
3473766ab985SMatthew G. Knepley }
3474766ab985SMatthew G. Knepley 
34753982b651SMatthew G. Knepley static PetscErrorCode DMPlexCreateCohesiveSubmesh_Interpolated(DM dm, const char labelname[], PetscInt value, DM subdm)
3476766ab985SMatthew G. Knepley {
34773982b651SMatthew G. Knepley   DMLabel        label = NULL;
3478766ab985SMatthew G. Knepley   PetscErrorCode ierr;
3479766ab985SMatthew G. Knepley 
3480766ab985SMatthew G. Knepley   PetscFunctionBegin;
3481c58f1c22SToby Isaac   if (labelname) {ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);}
3482158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmeshGeneric_Interpolated(dm, label, value, PETSC_FALSE, PETSC_TRUE, 1, subdm);CHKERRQ(ierr);
3483766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
3484766ab985SMatthew G. Knepley }
3485766ab985SMatthew G. Knepley 
3486c08575a3SMatthew G. Knepley /*@C
348727c04023SMatthew 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.
3488766ab985SMatthew G. Knepley 
3489766ab985SMatthew G. Knepley   Input Parameters:
3490766ab985SMatthew G. Knepley + dm          - The original mesh
349127c04023SMatthew G. Knepley . hasLagrange - The mesh has Lagrange unknowns in the cohesive cells
34927afc1a8bSJed Brown . label       - A label name, or NULL
349327c04023SMatthew G. Knepley - value  - A label value
3494766ab985SMatthew G. Knepley 
3495766ab985SMatthew G. Knepley   Output Parameter:
3496766ab985SMatthew G. Knepley . subdm - The surface mesh
3497766ab985SMatthew G. Knepley 
3498766ab985SMatthew G. Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3499766ab985SMatthew G. Knepley 
3500766ab985SMatthew G. Knepley   Level: developer
3501766ab985SMatthew G. Knepley 
3502766ab985SMatthew G. Knepley .seealso: DMPlexGetSubpointMap(), DMPlexCreateSubmesh()
3503c08575a3SMatthew G. Knepley @*/
350427c04023SMatthew G. Knepley PetscErrorCode DMPlexCreateCohesiveSubmesh(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM *subdm)
3505766ab985SMatthew G. Knepley {
3506c0e8cf5fSMatthew G. Knepley   PetscInt       dim, cdim, depth;
3507766ab985SMatthew G. Knepley   PetscErrorCode ierr;
3508766ab985SMatthew G. Knepley 
3509766ab985SMatthew G. Knepley   PetscFunctionBegin;
3510766ab985SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
351127c04023SMatthew G. Knepley   PetscValidPointer(subdm, 5);
3512c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3513766ab985SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3514766ab985SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), subdm);CHKERRQ(ierr);
3515766ab985SMatthew G. Knepley   ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr);
3516c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(*subdm, dim-1);CHKERRQ(ierr);
3517c0e8cf5fSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3518c0e8cf5fSMatthew G. Knepley   ierr = DMSetCoordinateDim(*subdm, cdim);CHKERRQ(ierr);
3519766ab985SMatthew G. Knepley   if (depth == dim) {
3520b3154360SMatthew G. Knepley     ierr = DMPlexCreateCohesiveSubmesh_Interpolated(dm, label, value, *subdm);CHKERRQ(ierr);
3521766ab985SMatthew G. Knepley   } else {
352227c04023SMatthew G. Knepley     ierr = DMPlexCreateCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, *subdm);CHKERRQ(ierr);
3523e6ccafaeSMatthew G Knepley   }
3524e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3525e6ccafaeSMatthew G Knepley }
3526e6ccafaeSMatthew G Knepley 
3527bec263e5SMatthew G. Knepley /*@
3528bec263e5SMatthew G. Knepley   DMPlexFilter - Extract a subset of mesh cells defined by a label as a separate mesh
3529bec263e5SMatthew G. Knepley 
3530bec263e5SMatthew G. Knepley   Input Parameters:
3531bec263e5SMatthew G. Knepley + dm        - The original mesh
3532bec263e5SMatthew G. Knepley . cellLabel - The DMLabel marking cells contained in the new mesh
3533bec263e5SMatthew G. Knepley - value     - The label value to use
3534bec263e5SMatthew G. Knepley 
3535bec263e5SMatthew G. Knepley   Output Parameter:
3536bec263e5SMatthew G. Knepley . subdm - The new mesh
3537bec263e5SMatthew G. Knepley 
3538bec263e5SMatthew G. Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3539bec263e5SMatthew G. Knepley 
3540bec263e5SMatthew G. Knepley   Level: developer
3541bec263e5SMatthew G. Knepley 
3542c58f1c22SToby Isaac .seealso: DMPlexGetSubpointMap(), DMGetLabel(), DMLabelSetValue()
3543bec263e5SMatthew G. Knepley @*/
3544bec263e5SMatthew G. Knepley PetscErrorCode DMPlexFilter(DM dm, DMLabel cellLabel, PetscInt value, DM *subdm)
3545bec263e5SMatthew G. Knepley {
3546bec263e5SMatthew G. Knepley   PetscInt       dim;
3547bec263e5SMatthew G. Knepley   PetscErrorCode ierr;
3548bec263e5SMatthew G. Knepley 
3549bec263e5SMatthew G. Knepley   PetscFunctionBegin;
3550bec263e5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3551bec263e5SMatthew G. Knepley   PetscValidPointer(subdm, 3);
3552bec263e5SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3553bec263e5SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), subdm);CHKERRQ(ierr);
3554bec263e5SMatthew G. Knepley   ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr);
3555bec263e5SMatthew G. Knepley   ierr = DMSetDimension(*subdm, dim);CHKERRQ(ierr);
3556bec263e5SMatthew G. Knepley   /* Extract submesh in place, could be empty on some procs, could have inconsistency if procs do not both extract a shared cell */
3557158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmeshGeneric_Interpolated(dm, cellLabel, value, PETSC_FALSE, PETSC_FALSE, 0, *subdm);CHKERRQ(ierr);
3558bec263e5SMatthew G. Knepley   PetscFunctionReturn(0);
3559bec263e5SMatthew G. Knepley }
3560bec263e5SMatthew G. Knepley 
356164beef6dSMatthew G. Knepley /*@
356264beef6dSMatthew G. Knepley   DMPlexGetSubpointMap - Returns a DMLabel with point dimension as values
356364beef6dSMatthew G. Knepley 
356464beef6dSMatthew G. Knepley   Input Parameter:
356564beef6dSMatthew G. Knepley . dm - The submesh DM
356664beef6dSMatthew G. Knepley 
356764beef6dSMatthew G. Knepley   Output Parameter:
356864beef6dSMatthew G. Knepley . subpointMap - The DMLabel of all the points from the original mesh in this submesh, or NULL if this is not a submesh
356964beef6dSMatthew G. Knepley 
357064beef6dSMatthew G. Knepley   Level: developer
357164beef6dSMatthew G. Knepley 
357264beef6dSMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexCreateSubpointIS()
357364beef6dSMatthew G. Knepley @*/
3574e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexGetSubpointMap(DM dm, DMLabel *subpointMap)
3575e6ccafaeSMatthew G Knepley {
3576e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3577e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3578e6ccafaeSMatthew G Knepley   PetscValidPointer(subpointMap, 2);
357965663942SMatthew G. Knepley   *subpointMap = ((DM_Plex*) dm->data)->subpointMap;
3580e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3581e6ccafaeSMatthew G Knepley }
3582e6ccafaeSMatthew G Knepley 
3583c08575a3SMatthew G. Knepley /*@
3584c08575a3SMatthew G. Knepley   DMPlexSetSubpointMap - Sets the DMLabel with point dimension as values
3585c08575a3SMatthew G. Knepley 
3586c08575a3SMatthew G. Knepley   Input Parameters:
3587c08575a3SMatthew G. Knepley + dm - The submesh DM
3588c08575a3SMatthew G. Knepley - subpointMap - The DMLabel of all the points from the original mesh in this submesh
3589c08575a3SMatthew G. Knepley 
3590c08575a3SMatthew G. Knepley   Note: Should normally not be called by the user, since it is set in DMPlexCreateSubmesh()
3591c08575a3SMatthew G. Knepley 
3592c08575a3SMatthew G. Knepley   Level: developer
3593c08575a3SMatthew G. Knepley 
3594c08575a3SMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexCreateSubpointIS()
3595c08575a3SMatthew G. Knepley @*/
3596e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexSetSubpointMap(DM dm, DMLabel subpointMap)
3597e6ccafaeSMatthew G Knepley {
3598e6ccafaeSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
3599285d324eSMatthew G. Knepley   DMLabel        tmp;
3600e6ccafaeSMatthew G Knepley   PetscErrorCode ierr;
3601e6ccafaeSMatthew G Knepley 
3602e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3603e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3604285d324eSMatthew G. Knepley   tmp  = mesh->subpointMap;
3605e6ccafaeSMatthew G Knepley   mesh->subpointMap = subpointMap;
3606d67d17b1SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) mesh->subpointMap);CHKERRQ(ierr);
3607285d324eSMatthew G. Knepley   ierr = DMLabelDestroy(&tmp);CHKERRQ(ierr);
3608e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3609e6ccafaeSMatthew G Knepley }
3610e6ccafaeSMatthew G Knepley 
361164beef6dSMatthew G. Knepley /*@
3612e6ccafaeSMatthew G Knepley   DMPlexCreateSubpointIS - Creates an IS covering the entire subdm chart with the original points as data
3613e6ccafaeSMatthew G Knepley 
3614e6ccafaeSMatthew G Knepley   Input Parameter:
3615e6ccafaeSMatthew G Knepley . dm - The submesh DM
3616e6ccafaeSMatthew G Knepley 
3617e6ccafaeSMatthew G Knepley   Output Parameter:
36180298fd71SBarry Smith . subpointIS - The IS of all the points from the original mesh in this submesh, or NULL if this is not a submesh
3619e6ccafaeSMatthew G Knepley 
36203982b651SMatthew G. Knepley   Note: This IS is guaranteed to be sorted by the construction of the submesh
362164beef6dSMatthew G. Knepley 
362264beef6dSMatthew G. Knepley   Level: developer
362364beef6dSMatthew G. Knepley 
362464beef6dSMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointMap()
362564beef6dSMatthew G. Knepley @*/
3626e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexCreateSubpointIS(DM dm, IS *subpointIS)
3627e6ccafaeSMatthew G Knepley {
362882f516ccSBarry Smith   MPI_Comm        comm;
3629e6ccafaeSMatthew G Knepley   DMLabel         subpointMap;
3630e6ccafaeSMatthew G Knepley   IS              is;
3631e6ccafaeSMatthew G Knepley   const PetscInt *opoints;
3632e6ccafaeSMatthew G Knepley   PetscInt       *points, *depths;
3633e6ccafaeSMatthew G Knepley   PetscInt        depth, depStart, depEnd, d, pStart, pEnd, p, n, off;
3634e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
3635e6ccafaeSMatthew G Knepley 
3636e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3637e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3638e6ccafaeSMatthew G Knepley   PetscValidPointer(subpointIS, 2);
363982f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
36400298fd71SBarry Smith   *subpointIS = NULL;
3641e6ccafaeSMatthew G Knepley   ierr = DMPlexGetSubpointMap(dm, &subpointMap);CHKERRQ(ierr);
3642e6ccafaeSMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3643fed694aaSMatthew G. Knepley   if (subpointMap && depth >= 0) {
3644e6ccafaeSMatthew G Knepley     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3645e6ccafaeSMatthew G Knepley     if (pStart) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Submeshes must start the point numbering at 0, not %d", pStart);
364669291d52SBarry Smith     ierr = DMGetWorkArray(dm, depth+1, MPIU_INT, &depths);CHKERRQ(ierr);
3647e6ccafaeSMatthew G Knepley     depths[0] = depth;
3648e6ccafaeSMatthew G Knepley     depths[1] = 0;
3649e6ccafaeSMatthew G Knepley     for(d = 2; d <= depth; ++d) {depths[d] = depth+1 - d;}
3650785e854fSJed Brown     ierr = PetscMalloc1(pEnd, &points);CHKERRQ(ierr);
3651e6ccafaeSMatthew G Knepley     for(d = 0, off = 0; d <= depth; ++d) {
3652e6ccafaeSMatthew G Knepley       const PetscInt dep = depths[d];
3653e6ccafaeSMatthew G Knepley 
3654e6ccafaeSMatthew G Knepley       ierr = DMPlexGetDepthStratum(dm, dep, &depStart, &depEnd);CHKERRQ(ierr);
3655e6ccafaeSMatthew G Knepley       ierr = DMLabelGetStratumSize(subpointMap, dep, &n);CHKERRQ(ierr);
36561e21b6fdSMatthew G Knepley       if (((d < 2) && (depth > 1)) || (d == 1)) { /* Only check vertices and cells for now since the map is broken for others */
3657e6ccafaeSMatthew 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);
3658e6ccafaeSMatthew G Knepley       } else {
36591e21b6fdSMatthew G Knepley         if (!n) {
36601e21b6fdSMatthew G Knepley           if (d == 0) {
36611e21b6fdSMatthew G Knepley             /* Missing cells */
36621e21b6fdSMatthew G Knepley             for(p = 0; p < depEnd-depStart; ++p, ++off) points[off] = -1;
36631e21b6fdSMatthew G Knepley           } else {
36641e21b6fdSMatthew G Knepley             /* Missing faces */
36651e21b6fdSMatthew G Knepley             for(p = 0; p < depEnd-depStart; ++p, ++off) points[off] = PETSC_MAX_INT;
36661e21b6fdSMatthew G Knepley           }
36671e21b6fdSMatthew G Knepley         }
3668e6ccafaeSMatthew G Knepley       }
3669e6ccafaeSMatthew G Knepley       if (n) {
3670e6ccafaeSMatthew G Knepley         ierr = DMLabelGetStratumIS(subpointMap, dep, &is);CHKERRQ(ierr);
3671e6ccafaeSMatthew G Knepley         ierr = ISGetIndices(is, &opoints);CHKERRQ(ierr);
3672e6ccafaeSMatthew G Knepley         for(p = 0; p < n; ++p, ++off) points[off] = opoints[p];
3673e6ccafaeSMatthew G Knepley         ierr = ISRestoreIndices(is, &opoints);CHKERRQ(ierr);
3674e6ccafaeSMatthew G Knepley         ierr = ISDestroy(&is);CHKERRQ(ierr);
3675e6ccafaeSMatthew G Knepley       }
3676e6ccafaeSMatthew G Knepley     }
367769291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, depth+1, MPIU_INT, &depths);CHKERRQ(ierr);
3678e6ccafaeSMatthew G Knepley     if (off != pEnd) SETERRQ2(comm, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %d should be %d", off, pEnd);
3679fed694aaSMatthew G. Knepley     ierr = ISCreateGeneral(PETSC_COMM_SELF, pEnd, points, PETSC_OWN_POINTER, subpointIS);CHKERRQ(ierr);
3680e6ccafaeSMatthew G Knepley   }
3681e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3682e6ccafaeSMatthew G Knepley }
3683559a1558SMatthew G. Knepley 
3684559a1558SMatthew G. Knepley /*@
3685559a1558SMatthew G. Knepley   DMPlexGetSubpoint - Return the subpoint corresponding to a point in the original mesh. If the DM
3686559a1558SMatthew G. Knepley                       is not a submesh, just return the input point.
3687559a1558SMatthew G. Knepley 
3688559a1558SMatthew G. Knepley   Note collective
3689559a1558SMatthew G. Knepley 
3690559a1558SMatthew G. Knepley   Input Parameters:
3691559a1558SMatthew G. Knepley + dm - The submesh DM
3692559a1558SMatthew G. Knepley - p  - The point in the original, from which the submesh was created
3693559a1558SMatthew G. Knepley 
3694559a1558SMatthew G. Knepley   Output Parameter:
3695559a1558SMatthew G. Knepley . subp - The point in the submesh
3696559a1558SMatthew G. Knepley 
3697559a1558SMatthew G. Knepley   Level: developer
3698559a1558SMatthew G. Knepley 
3699559a1558SMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointMap(), DMPlexCreateSubpointIS()
3700559a1558SMatthew G. Knepley @*/
3701559a1558SMatthew G. Knepley PetscErrorCode DMPlexGetSubpoint(DM dm, PetscInt p, PetscInt *subp)
3702559a1558SMatthew G. Knepley {
3703559a1558SMatthew G. Knepley   DMLabel        spmap;
3704559a1558SMatthew G. Knepley   PetscErrorCode ierr;
3705559a1558SMatthew G. Knepley 
3706559a1558SMatthew G. Knepley   *subp = p;
3707559a1558SMatthew G. Knepley   ierr = DMPlexGetSubpointMap(dm, &spmap);CHKERRQ(ierr);
3708559a1558SMatthew G. Knepley   if (spmap) {
3709559a1558SMatthew G. Knepley     IS              subpointIS;
3710559a1558SMatthew G. Knepley     const PetscInt *subpoints;
3711559a1558SMatthew G. Knepley     PetscInt        numSubpoints;
3712559a1558SMatthew G. Knepley 
3713559a1558SMatthew G. Knepley     /* TODO Cache the IS, making it look like an index */
3714559a1558SMatthew G. Knepley     ierr = DMPlexCreateSubpointIS(dm, &subpointIS);CHKERRQ(ierr);
3715559a1558SMatthew G. Knepley     ierr = ISGetLocalSize(subpointIS, &numSubpoints);CHKERRQ(ierr);
3716559a1558SMatthew G. Knepley     ierr = ISGetIndices(subpointIS, &subpoints);CHKERRQ(ierr);
3717559a1558SMatthew G. Knepley     ierr = PetscFindInt(p, numSubpoints, subpoints, subp);CHKERRQ(ierr);
3718559a1558SMatthew G. Knepley     if (*subp < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %d not found in submesh", p);
3719559a1558SMatthew G. Knepley     ierr = ISRestoreIndices(subpointIS, &subpoints);CHKERRQ(ierr);
3720559a1558SMatthew G. Knepley     ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
3721559a1558SMatthew G. Knepley   }
3722559a1558SMatthew G. Knepley   PetscFunctionReturn(0);
3723559a1558SMatthew G. Knepley }
3724