xref: /petsc/src/dm/impls/plex/plexsubmesh.c (revision 367003a68d4e38a62ba2a0620cd4e6f42aa373fd)
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;
844a42b08eeSMatthew G. Knepley   PetscBool      flag;
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);
855a42b08eeSMatthew G. Knepley   ierr = DMPlexGetAdjacencyUseCone(dm, &flag);CHKERRQ(ierr);
856a42b08eeSMatthew G. Knepley   ierr = DMPlexSetAdjacencyUseCone(gdm, flag);CHKERRQ(ierr);
857a42b08eeSMatthew G. Knepley   ierr = DMPlexGetAdjacencyUseClosure(dm, &flag);CHKERRQ(ierr);
858a42b08eeSMatthew G. Knepley   ierr = DMPlexSetAdjacencyUseClosure(gdm, flag);CHKERRQ(ierr);
859c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
860cd0c2139SMatthew G Knepley   if (!label) {
861cd0c2139SMatthew G Knepley     /* Get label for boundary faces */
862c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
863c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
864e752be1aSMatthew G. Knepley     ierr = DMPlexMarkBoundaryFaces(dm, 1, label);CHKERRQ(ierr);
865cd0c2139SMatthew G Knepley   }
866cd0c2139SMatthew G Knepley   ierr = DMPlexConstructGhostCells_Internal(dm, label, numGhostCells, gdm);CHKERRQ(ierr);
867a6ba4734SToby Isaac   ierr = DMCopyBoundary(dm, gdm);CHKERRQ(ierr);
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 
19793cf72582SMatthew G. Knepley   Level: developer
19803cf72582SMatthew G. Knepley 
19813cf72582SMatthew G. Knepley .seealso: DMPlexConstructCohesiveCells(), DMPlexLabelCohesiveComplete(), DMCreate()
19823cf72582SMatthew G. Knepley @*/
19837db7e0a7SMatthew G. Knepley PetscErrorCode DMPlexCreateHybridMesh(DM dm, DMLabel label, DMLabel bdlabel, DMLabel *hybridLabel, DMLabel *splitLabel, DM *dmInterface, DM *dmHybrid)
19843cf72582SMatthew G. Knepley {
19853cf72582SMatthew G. Knepley   DM             idm;
19867db7e0a7SMatthew G. Knepley   DMLabel        subpointMap, hlabel, slabel = NULL;
19873cf72582SMatthew G. Knepley   PetscInt       dim;
19883cf72582SMatthew G. Knepley   PetscErrorCode ierr;
19893cf72582SMatthew G. Knepley 
19903cf72582SMatthew G. Knepley   PetscFunctionBegin;
19913cf72582SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1992720e594eSMatthew G. Knepley   if (bdlabel) PetscValidPointer(bdlabel, 3);
1993720e594eSMatthew G. Knepley   if (hybridLabel) PetscValidPointer(hybridLabel, 4);
19947db7e0a7SMatthew G. Knepley   if (splitLabel)  PetscValidPointer(splitLabel, 5);
19957db7e0a7SMatthew G. Knepley   if (dmInterface) PetscValidPointer(dmInterface, 6);
19967db7e0a7SMatthew G. Knepley   PetscValidPointer(dmHybrid, 7);
1997c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1998158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmesh(dm, label, 1, PETSC_FALSE, &idm);CHKERRQ(ierr);
1999720e594eSMatthew G. Knepley   ierr = DMPlexCheckValidSubmesh_Private(dm, label, idm);CHKERRQ(ierr);
20003cf72582SMatthew G. Knepley   ierr = DMPlexOrient(idm);CHKERRQ(ierr);
20013cf72582SMatthew G. Knepley   ierr = DMPlexGetSubpointMap(idm, &subpointMap);CHKERRQ(ierr);
20023cf72582SMatthew G. Knepley   ierr = DMLabelDuplicate(subpointMap, &hlabel);CHKERRQ(ierr);
20033cf72582SMatthew G. Knepley   ierr = DMLabelClearStratum(hlabel, dim);CHKERRQ(ierr);
20047db7e0a7SMatthew G. Knepley   if (splitLabel) {
20057db7e0a7SMatthew G. Knepley     const char *name;
20067db7e0a7SMatthew G. Knepley     char        sname[PETSC_MAX_PATH_LEN];
20077db7e0a7SMatthew G. Knepley 
20087db7e0a7SMatthew G. Knepley     ierr = DMLabelGetName(hlabel, &name);CHKERRQ(ierr);
20097db7e0a7SMatthew G. Knepley     ierr = PetscStrncpy(sname, name, PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
20107db7e0a7SMatthew G. Knepley     ierr = PetscStrcat(sname, " split");CHKERRQ(ierr);
20117db7e0a7SMatthew G. Knepley     ierr = DMLabelCreate(sname, &slabel);CHKERRQ(ierr);
20127db7e0a7SMatthew G. Knepley   }
2013720e594eSMatthew G. Knepley   ierr = DMPlexLabelCohesiveComplete(dm, hlabel, bdlabel, PETSC_FALSE, idm);CHKERRQ(ierr);
2014720e594eSMatthew G. Knepley   if (dmInterface) {*dmInterface = idm;}
2015720e594eSMatthew G. Knepley   else             {ierr = DMDestroy(&idm);CHKERRQ(ierr);}
20167db7e0a7SMatthew G. Knepley   ierr = DMPlexConstructCohesiveCells(dm, hlabel, slabel, dmHybrid);CHKERRQ(ierr);
20173cf72582SMatthew G. Knepley   if (hybridLabel) *hybridLabel = hlabel;
201853aa2e23SMatthew G. Knepley   else             {ierr = DMLabelDestroy(&hlabel);CHKERRQ(ierr);}
20197db7e0a7SMatthew G. Knepley   if (splitLabel)  *splitLabel  = slabel;
2020cd0c2139SMatthew G Knepley   PetscFunctionReturn(0);
2021cd0c2139SMatthew G Knepley }
2022cd0c2139SMatthew G Knepley 
2023efa14ee0SMatthew G Knepley /* Here we need the explicit assumption that:
2024efa14ee0SMatthew G Knepley 
2025efa14ee0SMatthew G Knepley      For any marked cell, the marked vertices constitute a single face
2026efa14ee0SMatthew G Knepley */
2027830e53efSMatthew G. Knepley static PetscErrorCode DMPlexMarkSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DMLabel subpointMap, PetscInt *numFaces, PetscInt *nFV, DM subdm)
2028efa14ee0SMatthew G Knepley {
2029fed694aaSMatthew G. Knepley   IS               subvertexIS = NULL;
2030efa14ee0SMatthew G Knepley   const PetscInt  *subvertices;
203177d178adSMatthew G. Knepley   PetscInt        *pStart, *pEnd, *pMax, pSize;
2032efa14ee0SMatthew G Knepley   PetscInt         depth, dim, d, numSubVerticesInitial = 0, v;
2033efa14ee0SMatthew G Knepley   PetscErrorCode   ierr;
2034efa14ee0SMatthew G Knepley 
2035efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2036efa14ee0SMatthew G Knepley   *numFaces = 0;
2037efa14ee0SMatthew G Knepley   *nFV      = 0;
2038efa14ee0SMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
2039c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
204077d178adSMatthew G. Knepley   pSize = PetscMax(depth, dim) + 1;
2041dcca6d9dSJed Brown   ierr = PetscMalloc3(pSize,&pStart,pSize,&pEnd,pSize,&pMax);CHKERRQ(ierr);
2042d92e47f8SMatthew 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);
2043efa14ee0SMatthew G Knepley   for (d = 0; d <= depth; ++d) {
2044efa14ee0SMatthew G Knepley     ierr = DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);
2045efa14ee0SMatthew G Knepley     if (pMax[d] >= 0) pEnd[d] = PetscMin(pEnd[d], pMax[d]);
2046efa14ee0SMatthew G Knepley   }
2047efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
2048fed694aaSMatthew G. Knepley   if (vertexLabel) {ierr = DMLabelGetStratumIS(vertexLabel, value, &subvertexIS);CHKERRQ(ierr);}
2049efa14ee0SMatthew G Knepley   if (subvertexIS) {
2050efa14ee0SMatthew G Knepley     ierr = ISGetSize(subvertexIS, &numSubVerticesInitial);CHKERRQ(ierr);
2051efa14ee0SMatthew G Knepley     ierr = ISGetIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
2052efa14ee0SMatthew G Knepley   }
2053efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2054efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
20550298fd71SBarry Smith     PetscInt      *star   = NULL;
2056efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numCells = 0, c;
2057efa14ee0SMatthew G Knepley 
2058efa14ee0SMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2059efa14ee0SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
2060efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2061efa14ee0SMatthew G Knepley       if ((point >= pStart[depth]) && (point < pEnd[depth])) star[numCells++] = point;
2062efa14ee0SMatthew G Knepley     }
2063efa14ee0SMatthew G Knepley     for (c = 0; c < numCells; ++c) {
2064efa14ee0SMatthew G Knepley       const PetscInt cell    = star[c];
20650298fd71SBarry Smith       PetscInt      *closure = NULL;
2066efa14ee0SMatthew G Knepley       PetscInt       closureSize, cl;
2067efa14ee0SMatthew G Knepley       PetscInt       cellLoc, numCorners = 0, faceSize = 0;
2068efa14ee0SMatthew G Knepley 
2069efa14ee0SMatthew G Knepley       ierr = DMLabelGetValue(subpointMap, cell, &cellLoc);CHKERRQ(ierr);
207065560c7fSMatthew G Knepley       if (cellLoc == 2) continue;
207182f516ccSBarry Smith       if (cellLoc >= 0) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Cell %d has dimension %d in the surface label", cell, cellLoc);
2072efa14ee0SMatthew G Knepley       ierr = DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2073efa14ee0SMatthew G Knepley       for (cl = 0; cl < closureSize*2; cl += 2) {
2074efa14ee0SMatthew G Knepley         const PetscInt point = closure[cl];
2075efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2076efa14ee0SMatthew G Knepley 
2077efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
2078efa14ee0SMatthew G Knepley           ++numCorners;
2079efa14ee0SMatthew G Knepley           ierr = DMLabelGetValue(vertexLabel, point, &vertexLoc);CHKERRQ(ierr);
2080830e53efSMatthew G. Knepley           if (vertexLoc == value) closure[faceSize++] = point;
2081efa14ee0SMatthew G Knepley         }
2082efa14ee0SMatthew G Knepley       }
208318ad9376SMatthew G. Knepley       if (!(*nFV)) {ierr = DMPlexGetNumFaceVertices(dm, dim, numCorners, nFV);CHKERRQ(ierr);}
208482f516ccSBarry 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);
2085efa14ee0SMatthew G Knepley       if (faceSize == *nFV) {
2086007baee2SMatthew G. Knepley         const PetscInt *cells = NULL;
2087007baee2SMatthew G. Knepley         PetscInt        numCells, nc;
2088007baee2SMatthew G. Knepley 
2089efa14ee0SMatthew G Knepley         ++(*numFaces);
2090efa14ee0SMatthew G Knepley         for (cl = 0; cl < faceSize; ++cl) {
2091efa14ee0SMatthew G Knepley           ierr = DMLabelSetValue(subpointMap, closure[cl], 0);CHKERRQ(ierr);
2092efa14ee0SMatthew G Knepley         }
2093007baee2SMatthew G. Knepley         ierr = DMPlexGetJoin(dm, faceSize, closure, &numCells, &cells);CHKERRQ(ierr);
2094007baee2SMatthew G. Knepley         for (nc = 0; nc < numCells; ++nc) {
2095007baee2SMatthew G. Knepley           ierr = DMLabelSetValue(subpointMap, cells[nc], 2);CHKERRQ(ierr);
2096007baee2SMatthew G. Knepley         }
2097007baee2SMatthew G. Knepley         ierr = DMPlexRestoreJoin(dm, faceSize, closure, &numCells, &cells);CHKERRQ(ierr);
2098efa14ee0SMatthew G Knepley       }
2099efa14ee0SMatthew G Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2100efa14ee0SMatthew G Knepley     }
2101efa14ee0SMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2102efa14ee0SMatthew G Knepley   }
2103efa14ee0SMatthew G Knepley   if (subvertexIS) {
2104efa14ee0SMatthew G Knepley     ierr = ISRestoreIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
2105efa14ee0SMatthew G Knepley   }
2106efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
2107efa14ee0SMatthew G Knepley   ierr = PetscFree3(pStart,pEnd,pMax);CHKERRQ(ierr);
2108efa14ee0SMatthew G Knepley   PetscFunctionReturn(0);
2109efa14ee0SMatthew G Knepley }
2110efa14ee0SMatthew G Knepley 
2111158acfadSMatthew G. Knepley static PetscErrorCode DMPlexMarkSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DMLabel subpointMap, DM subdm)
2112efa14ee0SMatthew G Knepley {
211334b4c39eSMatthew G. Knepley   IS               subvertexIS = NULL;
2114efa14ee0SMatthew G Knepley   const PetscInt  *subvertices;
2115efa14ee0SMatthew G Knepley   PetscInt        *pStart, *pEnd, *pMax;
2116efa14ee0SMatthew G Knepley   PetscInt         dim, d, numSubVerticesInitial = 0, v;
2117efa14ee0SMatthew G Knepley   PetscErrorCode   ierr;
2118efa14ee0SMatthew G Knepley 
2119efa14ee0SMatthew G Knepley   PetscFunctionBegin;
2120c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2121dcca6d9dSJed Brown   ierr = PetscMalloc3(dim+1,&pStart,dim+1,&pEnd,dim+1,&pMax);CHKERRQ(ierr);
21220298fd71SBarry Smith   ierr = DMPlexGetHybridBounds(dm, &pMax[dim], dim>1 ? &pMax[dim-1] : NULL, dim > 2 ? &pMax[1] : NULL, &pMax[0]);CHKERRQ(ierr);
2123efa14ee0SMatthew G Knepley   for (d = 0; d <= dim; ++d) {
2124efa14ee0SMatthew G Knepley     ierr = DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);
2125efa14ee0SMatthew G Knepley     if (pMax[d] >= 0) pEnd[d] = PetscMin(pEnd[d], pMax[d]);
2126efa14ee0SMatthew G Knepley   }
2127efa14ee0SMatthew G Knepley   /* Loop over initial vertices and mark all faces in the collective star() */
212834b4c39eSMatthew G. Knepley   if (vertexLabel) {
2129830e53efSMatthew G. Knepley     ierr = DMLabelGetStratumIS(vertexLabel, value, &subvertexIS);CHKERRQ(ierr);
2130efa14ee0SMatthew G Knepley     if (subvertexIS) {
2131efa14ee0SMatthew G Knepley       ierr = ISGetSize(subvertexIS, &numSubVerticesInitial);CHKERRQ(ierr);
2132efa14ee0SMatthew G Knepley       ierr = ISGetIndices(subvertexIS, &subvertices);CHKERRQ(ierr);
2133efa14ee0SMatthew G Knepley     }
213434b4c39eSMatthew G. Knepley   }
2135efa14ee0SMatthew G Knepley   for (v = 0; v < numSubVerticesInitial; ++v) {
2136efa14ee0SMatthew G Knepley     const PetscInt vertex = subvertices[v];
21370298fd71SBarry Smith     PetscInt      *star   = NULL;
2138efa14ee0SMatthew G Knepley     PetscInt       starSize, s, numFaces = 0, f;
2139efa14ee0SMatthew G Knepley 
2140efa14ee0SMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2141efa14ee0SMatthew G Knepley     for (s = 0; s < starSize*2; s += 2) {
2142efa14ee0SMatthew G Knepley       const PetscInt point = star[s];
2143158acfadSMatthew G. Knepley       PetscInt       faceLoc;
2144158acfadSMatthew G. Knepley 
2145158acfadSMatthew G. Knepley       if ((point >= pStart[dim-1]) && (point < pEnd[dim-1])) {
2146158acfadSMatthew G. Knepley         if (markedFaces) {
2147158acfadSMatthew G. Knepley           ierr = DMLabelGetValue(vertexLabel, point, &faceLoc);CHKERRQ(ierr);
2148158acfadSMatthew G. Knepley           if (faceLoc < 0) continue;
2149158acfadSMatthew G. Knepley         }
2150158acfadSMatthew G. Knepley         star[numFaces++] = point;
2151158acfadSMatthew G. Knepley       }
2152efa14ee0SMatthew G Knepley     }
2153efa14ee0SMatthew G Knepley     for (f = 0; f < numFaces; ++f) {
2154efa14ee0SMatthew G Knepley       const PetscInt face    = star[f];
21550298fd71SBarry Smith       PetscInt      *closure = NULL;
2156efa14ee0SMatthew G Knepley       PetscInt       closureSize, c;
2157efa14ee0SMatthew G Knepley       PetscInt       faceLoc;
2158efa14ee0SMatthew G Knepley 
2159efa14ee0SMatthew G Knepley       ierr = DMLabelGetValue(subpointMap, face, &faceLoc);CHKERRQ(ierr);
2160efa14ee0SMatthew G Knepley       if (faceLoc == dim-1) continue;
216182f516ccSBarry Smith       if (faceLoc >= 0) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Face %d has dimension %d in the surface label", face, faceLoc);
2162efa14ee0SMatthew G Knepley       ierr = DMPlexGetTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2163efa14ee0SMatthew G Knepley       for (c = 0; c < closureSize*2; c += 2) {
2164efa14ee0SMatthew G Knepley         const PetscInt point = closure[c];
2165efa14ee0SMatthew G Knepley         PetscInt       vertexLoc;
2166efa14ee0SMatthew G Knepley 
2167efa14ee0SMatthew G Knepley         if ((point >= pStart[0]) && (point < pEnd[0])) {
2168efa14ee0SMatthew G Knepley           ierr = DMLabelGetValue(vertexLabel, point, &vertexLoc);CHKERRQ(ierr);
2169830e53efSMatthew G. Knepley           if (vertexLoc != value) break;
2170efa14ee0SMatthew G Knepley         }
2171efa14ee0SMatthew G Knepley       }
2172efa14ee0SMatthew G Knepley       if (c == closureSize*2) {
2173efa14ee0SMatthew G Knepley         const PetscInt *support;
2174efa14ee0SMatthew G Knepley         PetscInt        supportSize, s;
2175efa14ee0SMatthew G Knepley 
2176efa14ee0SMatthew G Knepley         for (c = 0; c < closureSize*2; c += 2) {
2177efa14ee0SMatthew G Knepley           const PetscInt point = closure[c];
2178efa14ee0SMatthew G Knepley 
2179efa14ee0SMatthew G Knepley           for (d = 0; d < dim; ++d) {
2180efa14ee0SMatthew G Knepley             if ((point >= pStart[d]) && (point < pEnd[d])) {
2181efa14ee0SMatthew G Knepley               ierr = DMLabelSetValue(subpointMap, point, d);CHKERRQ(ierr);
2182efa14ee0SMatthew G Knepley               break;
2183efa14ee0SMatthew G Knepley             }
2184efa14ee0SMatthew G Knepley           }
2185efa14ee0SMatthew G Knepley         }
2186efa14ee0SMatthew G Knepley         ierr = DMPlexGetSupportSize(dm, face, &supportSize);CHKERRQ(ierr);
2187efa14ee0SMatthew G Knepley         ierr = DMPlexGetSupport(dm, face, &support);CHKERRQ(ierr);
2188efa14ee0SMatthew G Knepley         for (s = 0; s < supportSize; ++s) {
2189efa14ee0SMatthew G Knepley           ierr = DMLabelSetValue(subpointMap, support[s], dim);CHKERRQ(ierr);
2190efa14ee0SMatthew G Knepley         }
2191efa14ee0SMatthew G Knepley       }
2192efa14ee0SMatthew G Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, face, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2193efa14ee0SMatthew G Knepley     }
2194efa14ee0SMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, vertex, PETSC_FALSE, &starSize, &star);CHKERRQ(ierr);
2195efa14ee0SMatthew G Knepley   }
219634b4c39eSMatthew G. Knepley   if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subvertices);CHKERRQ(ierr);}
2197efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
2198efa14ee0SMatthew G Knepley   ierr = PetscFree3(pStart,pEnd,pMax);CHKERRQ(ierr);
2199efa14ee0SMatthew G Knepley   PetscFunctionReturn(0);
2200efa14ee0SMatthew G Knepley }
2201efa14ee0SMatthew G Knepley 
220227c04023SMatthew 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)
2203766ab985SMatthew G. Knepley {
220427c04023SMatthew G. Knepley   DMLabel         label = NULL;
2205766ab985SMatthew G. Knepley   const PetscInt *cone;
22069fc93327SToby Isaac   PetscInt        dim, cMax, cEnd, c, subc = 0, p, coneSize = -1;
2207766ab985SMatthew G. Knepley   PetscErrorCode  ierr;
2208766ab985SMatthew G. Knepley 
2209812bfc34SJed Brown   PetscFunctionBegin;
2210c0ed958bSJed Brown   *numFaces = 0;
2211c0ed958bSJed Brown   *nFV = 0;
2212c58f1c22SToby Isaac   if (labelname) {ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);}
2213fed694aaSMatthew G. Knepley   *subCells = NULL;
2214c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2215766ab985SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
2216766ab985SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
2217766ab985SMatthew G. Knepley   if (cMax < 0) PetscFunctionReturn(0);
221827c04023SMatthew G. Knepley   if (label) {
221927c04023SMatthew G. Knepley     for (c = cMax; c < cEnd; ++c) {
222027c04023SMatthew G. Knepley       PetscInt val;
222127c04023SMatthew G. Knepley 
222227c04023SMatthew G. Knepley       ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr);
222327c04023SMatthew G. Knepley       if (val == value) {
222427c04023SMatthew G. Knepley         ++(*numFaces);
222527c04023SMatthew G. Knepley         ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
222627c04023SMatthew G. Knepley       }
222727c04023SMatthew G. Knepley     }
222827c04023SMatthew G. Knepley   } else {
2229766ab985SMatthew G. Knepley     *numFaces = cEnd - cMax;
223027c04023SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cMax, &coneSize);CHKERRQ(ierr);
223127c04023SMatthew G. Knepley   }
2232785e854fSJed Brown   ierr = PetscMalloc1(*numFaces *2, subCells);CHKERRQ(ierr);
22339fc93327SToby Isaac   if (!(*numFaces)) PetscFunctionReturn(0);
22349fc93327SToby Isaac   *nFV = hasLagrange ? coneSize/3 : coneSize/2;
2235766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2236766ab985SMatthew G. Knepley     const PetscInt *cells;
2237766ab985SMatthew G. Knepley     PetscInt        numCells;
2238766ab985SMatthew G. Knepley 
223927c04023SMatthew G. Knepley     if (label) {
224027c04023SMatthew G. Knepley       PetscInt val;
224127c04023SMatthew G. Knepley 
224227c04023SMatthew G. Knepley       ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr);
224327c04023SMatthew G. Knepley       if (val != value) continue;
224427c04023SMatthew G. Knepley     }
2245766ab985SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
2246766ab985SMatthew G. Knepley     for (p = 0; p < *nFV; ++p) {
2247766ab985SMatthew G. Knepley       ierr = DMLabelSetValue(subpointMap, cone[p], 0);CHKERRQ(ierr);
2248766ab985SMatthew G. Knepley     }
2249766ab985SMatthew G. Knepley     /* Negative face */
2250766ab985SMatthew G. Knepley     ierr = DMPlexGetJoin(dm, *nFV, cone, &numCells, &cells);CHKERRQ(ierr);
225127234c99SMatthew G. Knepley     /* Not true in parallel
225227234c99SMatthew G. Knepley     if (numCells != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
2253766ab985SMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
2254766ab985SMatthew G. Knepley       ierr = DMLabelSetValue(subpointMap, cells[p], 2);CHKERRQ(ierr);
225527234c99SMatthew G. Knepley       (*subCells)[subc++] = cells[p];
2256766ab985SMatthew G. Knepley     }
2257766ab985SMatthew G. Knepley     ierr = DMPlexRestoreJoin(dm, *nFV, cone, &numCells, &cells);CHKERRQ(ierr);
2258766ab985SMatthew G. Knepley     /* Positive face is not included */
2259766ab985SMatthew G. Knepley   }
2260766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
2261766ab985SMatthew G. Knepley }
2262766ab985SMatthew G. Knepley 
22633982b651SMatthew G. Knepley static PetscErrorCode DMPlexMarkCohesiveSubmesh_Interpolated(DM dm, DMLabel label, PetscInt value, DMLabel subpointMap, DM subdm)
2264766ab985SMatthew G. Knepley {
2265766ab985SMatthew G. Knepley   PetscInt      *pStart, *pEnd;
2266766ab985SMatthew G. Knepley   PetscInt       dim, cMax, cEnd, c, d;
2267766ab985SMatthew G. Knepley   PetscErrorCode ierr;
2268766ab985SMatthew G. Knepley 
2269812bfc34SJed Brown   PetscFunctionBegin;
2270c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2271766ab985SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
2272766ab985SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
2273766ab985SMatthew G. Knepley   if (cMax < 0) PetscFunctionReturn(0);
2274dcca6d9dSJed Brown   ierr = PetscMalloc2(dim+1,&pStart,dim+1,&pEnd);CHKERRQ(ierr);
2275b3154360SMatthew G. Knepley   for (d = 0; d <= dim; ++d) {ierr = DMPlexGetDepthStratum(dm, d, &pStart[d], &pEnd[d]);CHKERRQ(ierr);}
2276766ab985SMatthew G. Knepley   for (c = cMax; c < cEnd; ++c) {
2277766ab985SMatthew G. Knepley     const PetscInt *cone;
2278766ab985SMatthew G. Knepley     PetscInt       *closure = NULL;
2279b3154360SMatthew G. Knepley     PetscInt        fconeSize, coneSize, closureSize, cl, val;
2280766ab985SMatthew G. Knepley 
228127c04023SMatthew G. Knepley     if (label) {
228227c04023SMatthew G. Knepley       ierr = DMLabelGetValue(label, c, &val);CHKERRQ(ierr);
228327c04023SMatthew G. Knepley       if (val != value) continue;
228427c04023SMatthew G. Knepley     }
2285766ab985SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, c, &coneSize);CHKERRQ(ierr);
2286766ab985SMatthew G. Knepley     ierr = DMPlexGetCone(dm, c, &cone);CHKERRQ(ierr);
2287b3154360SMatthew G. Knepley     ierr = DMPlexGetConeSize(dm, cone[0], &fconeSize);CHKERRQ(ierr);
2288b3154360SMatthew G. Knepley     if (coneSize != (fconeSize ? fconeSize : 1) + 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells");
2289b3154360SMatthew G. Knepley     /* Negative face */
2290766ab985SMatthew G. Knepley     ierr = DMPlexGetTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2291766ab985SMatthew G. Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
2292766ab985SMatthew G. Knepley       const PetscInt point = closure[cl];
2293766ab985SMatthew G. Knepley 
2294766ab985SMatthew G. Knepley       for (d = 0; d <= dim; ++d) {
2295766ab985SMatthew G. Knepley         if ((point >= pStart[d]) && (point < pEnd[d])) {
2296766ab985SMatthew G. Knepley           ierr = DMLabelSetValue(subpointMap, point, d);CHKERRQ(ierr);
2297766ab985SMatthew G. Knepley           break;
2298766ab985SMatthew G. Knepley         }
2299766ab985SMatthew G. Knepley       }
2300766ab985SMatthew G. Knepley     }
2301766ab985SMatthew G. Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, cone[0], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2302766ab985SMatthew G. Knepley     /* Cells -- positive face is not included */
2303766ab985SMatthew G. Knepley     for (cl = 0; cl < 1; ++cl) {
2304766ab985SMatthew G. Knepley       const PetscInt *support;
2305766ab985SMatthew G. Knepley       PetscInt        supportSize, s;
2306766ab985SMatthew G. Knepley 
2307766ab985SMatthew G. Knepley       ierr = DMPlexGetSupportSize(dm, cone[cl], &supportSize);CHKERRQ(ierr);
2308711de394SMatthew G. Knepley       /* if (supportSize != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive faces should separate two cells"); */
2309766ab985SMatthew G. Knepley       ierr = DMPlexGetSupport(dm, cone[cl], &support);CHKERRQ(ierr);
2310766ab985SMatthew G. Knepley       for (s = 0; s < supportSize; ++s) {
2311766ab985SMatthew G. Knepley         ierr = DMLabelSetValue(subpointMap, support[s], dim);CHKERRQ(ierr);
2312766ab985SMatthew G. Knepley       }
2313766ab985SMatthew G. Knepley     }
2314766ab985SMatthew G. Knepley   }
2315766ab985SMatthew G. Knepley   ierr = PetscFree2(pStart, pEnd);CHKERRQ(ierr);
2316766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
2317766ab985SMatthew G. Knepley }
2318766ab985SMatthew G. Knepley 
2319c08575a3SMatthew G. Knepley static PetscErrorCode DMPlexGetFaceOrientation(DM dm, PetscInt cell, PetscInt numCorners, PetscInt indices[], PetscInt oppositeVertex, PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2320e6ccafaeSMatthew G Knepley {
232182f516ccSBarry Smith   MPI_Comm       comm;
2322e6ccafaeSMatthew G Knepley   PetscBool      posOrient = PETSC_FALSE;
2323e6ccafaeSMatthew G Knepley   const PetscInt debug     = 0;
2324e6ccafaeSMatthew G Knepley   PetscInt       cellDim, faceSize, f;
2325e6ccafaeSMatthew G Knepley   PetscErrorCode ierr;
2326e6ccafaeSMatthew G Knepley 
232782f516ccSBarry Smith   PetscFunctionBegin;
232882f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2329c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &cellDim);CHKERRQ(ierr);
2330*367003a6SStefano Zampini   if (debug) {ierr = PetscPrintf(comm, "cellDim: %d numCorners: %d\n", cellDim, numCorners);CHKERRQ(ierr);}
2331e6ccafaeSMatthew G Knepley 
2332ddeab2a6SMatthew G. Knepley   if (cellDim == 1 && numCorners == 2) {
2333ddeab2a6SMatthew G. Knepley     /* Triangle */
2334e6ccafaeSMatthew G Knepley     faceSize  = numCorners-1;
2335e6ccafaeSMatthew G Knepley     posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2336ddeab2a6SMatthew G. Knepley   } else if (cellDim == 2 && numCorners == 3) {
2337ddeab2a6SMatthew G. Knepley     /* Triangle */
2338ddeab2a6SMatthew G. Knepley     faceSize  = numCorners-1;
2339ddeab2a6SMatthew G. Knepley     posOrient = !(oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2340ddeab2a6SMatthew G. Knepley   } else if (cellDim == 3 && numCorners == 4) {
2341ddeab2a6SMatthew G. Knepley     /* Tetrahedron */
2342ddeab2a6SMatthew G. Knepley     faceSize  = numCorners-1;
2343ddeab2a6SMatthew G. Knepley     posOrient = (oppositeVertex%2) ? PETSC_TRUE : PETSC_FALSE;
2344e6ccafaeSMatthew G Knepley   } else if (cellDim == 1 && numCorners == 3) {
2345e6ccafaeSMatthew G Knepley     /* Quadratic line */
2346e6ccafaeSMatthew G Knepley     faceSize  = 1;
2347e6ccafaeSMatthew G Knepley     posOrient = PETSC_TRUE;
2348e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 4) {
2349e6ccafaeSMatthew G Knepley     /* Quads */
2350e6ccafaeSMatthew G Knepley     faceSize = 2;
2351e6ccafaeSMatthew G Knepley     if ((indices[1] > indices[0]) && (indices[1] - indices[0] == 1)) {
2352e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2353e6ccafaeSMatthew G Knepley     } else if ((indices[0] == 3) && (indices[1] == 0)) {
2354e6ccafaeSMatthew G Knepley       posOrient = PETSC_TRUE;
2355e6ccafaeSMatthew G Knepley     } else {
2356e6ccafaeSMatthew G Knepley       if (((indices[0] > indices[1]) && (indices[0] - indices[1] == 1)) || ((indices[0] == 0) && (indices[1] == 3))) {
2357e6ccafaeSMatthew G Knepley         posOrient = PETSC_FALSE;
2358e6ccafaeSMatthew G Knepley       } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossedge");
2359e6ccafaeSMatthew G Knepley     }
2360e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 6) {
2361e6ccafaeSMatthew G Knepley     /* Quadratic triangle (I hate this) */
2362e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2363e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTri = 3;
2364e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2365e6ccafaeSMatthew G Knepley     PetscBool      found                    = PETSC_FALSE;
2366e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTriSorted[9] = {
2367e6ccafaeSMatthew G Knepley       0, 3,  4, /* bottom */
2368e6ccafaeSMatthew G Knepley       1, 4,  5, /* right */
2369e6ccafaeSMatthew G Knepley       2, 3,  5, /* left */
2370e6ccafaeSMatthew G Knepley     };
2371e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTri[9] = {
2372e6ccafaeSMatthew G Knepley       0, 3,  4, /* bottom */
2373e6ccafaeSMatthew G Knepley       1, 4,  5, /* right */
2374e6ccafaeSMatthew G Knepley       2, 5,  3, /* left */
2375e6ccafaeSMatthew G Knepley     };
2376e6ccafaeSMatthew G Knepley 
2377e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTri; ++i) sortedIndices[i] = indices[i];
2378e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeTri, sortedIndices);CHKERRQ(ierr);
2379e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 3; ++iFace) {
2380e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeTri;
2381e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2382e6ccafaeSMatthew G Knepley 
2383e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesTriSorted[ii+0]) &&
2384e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesTriSorted[ii+1])) {
2385e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTri; ++fVertex) {
2386e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTri; ++cVertex) {
2387e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTri[ii+fVertex]) {
2388e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2389e6ccafaeSMatthew G Knepley               break;
2390e6ccafaeSMatthew G Knepley             }
2391e6ccafaeSMatthew G Knepley           }
2392e6ccafaeSMatthew G Knepley         }
2393e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2394e6ccafaeSMatthew G Knepley         break;
2395e6ccafaeSMatthew G Knepley       }
2396e6ccafaeSMatthew G Knepley     }
2397e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid tri crossface");
2398e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2399e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2400e6ccafaeSMatthew G Knepley   } else if (cellDim == 2 && numCorners == 9) {
2401e6ccafaeSMatthew G Knepley     /* Quadratic quad (I hate this) */
2402e6ccafaeSMatthew G Knepley     /* Edges are determined by the first 2 vertices (corners of edges) */
2403e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuad = 3;
2404e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[3], i, iFace;
2405e6ccafaeSMatthew G Knepley     PetscBool      found                      = PETSC_FALSE;
2406e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadSorted[12] = {
2407e6ccafaeSMatthew G Knepley       0, 1,  4, /* bottom */
2408e6ccafaeSMatthew G Knepley       1, 2,  5, /* right */
2409e6ccafaeSMatthew G Knepley       2, 3,  6, /* top */
2410e6ccafaeSMatthew G Knepley       0, 3,  7, /* left */
2411e6ccafaeSMatthew G Knepley     };
2412e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuad[12] = {
2413e6ccafaeSMatthew G Knepley       0, 1,  4, /* bottom */
2414e6ccafaeSMatthew G Knepley       1, 2,  5, /* right */
2415e6ccafaeSMatthew G Knepley       2, 3,  6, /* top */
2416e6ccafaeSMatthew G Knepley       3, 0,  7, /* left */
2417e6ccafaeSMatthew G Knepley     };
2418e6ccafaeSMatthew G Knepley 
2419e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuad; ++i) sortedIndices[i] = indices[i];
2420e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeQuad, sortedIndices);CHKERRQ(ierr);
2421e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 4; ++iFace) {
2422e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeQuad;
2423e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2424e6ccafaeSMatthew G Knepley 
2425e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesQuadSorted[ii+0]) &&
2426e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesQuadSorted[ii+1])) {
2427e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuad; ++fVertex) {
2428e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuad; ++cVertex) {
2429e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuad[ii+fVertex]) {
2430e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2431e6ccafaeSMatthew G Knepley               break;
2432e6ccafaeSMatthew G Knepley             }
2433e6ccafaeSMatthew G Knepley           }
2434e6ccafaeSMatthew G Knepley         }
2435e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2436e6ccafaeSMatthew G Knepley         break;
2437e6ccafaeSMatthew G Knepley       }
2438e6ccafaeSMatthew G Knepley     }
2439e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid quad crossface");
2440e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2441e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2442e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 8) {
2443e6ccafaeSMatthew G Knepley     /* Hexes
2444e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2445e6ccafaeSMatthew G Knepley        pointing up at the second.
2446e6ccafaeSMatthew G Knepley 
2447e6ccafaeSMatthew G Knepley           7---6
2448e6ccafaeSMatthew G Knepley          /|  /|
2449e6ccafaeSMatthew G Knepley         4---5 |
2450ddeab2a6SMatthew G. Knepley         | 1-|-2
2451e6ccafaeSMatthew G Knepley         |/  |/
2452ddeab2a6SMatthew G. Knepley         0---3
2453e6ccafaeSMatthew G Knepley 
2454e6ccafaeSMatthew G Knepley         Faces are determined by the first 4 vertices (corners of faces) */
2455e6ccafaeSMatthew G Knepley     const PetscInt faceSizeHex = 4;
2456e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[4], i, iFace;
2457e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2458e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHexSorted[24] = {
2459e6ccafaeSMatthew G Knepley       0, 1, 2, 3,  /* bottom */
2460e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  /* top */
2461ddeab2a6SMatthew G. Knepley       0, 3, 4, 5,  /* front */
2462ddeab2a6SMatthew G. Knepley       2, 3, 5, 6,  /* right */
2463ddeab2a6SMatthew G. Knepley       1, 2, 6, 7,  /* back */
2464ddeab2a6SMatthew G. Knepley       0, 1, 4, 7,  /* left */
2465e6ccafaeSMatthew G Knepley     };
2466e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesHex[24] = {
2467ddeab2a6SMatthew G. Knepley       1, 2, 3, 0,  /* bottom */
2468e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  /* top */
2469ddeab2a6SMatthew G. Knepley       0, 3, 5, 4,  /* front */
2470ddeab2a6SMatthew G. Knepley       3, 2, 6, 5,  /* right */
2471ddeab2a6SMatthew G. Knepley       2, 1, 7, 6,  /* back */
2472ddeab2a6SMatthew G. Knepley       1, 0, 4, 7,  /* left */
2473e6ccafaeSMatthew G Knepley     };
2474e6ccafaeSMatthew G Knepley 
2475e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeHex; ++i) sortedIndices[i] = indices[i];
2476e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeHex, sortedIndices);CHKERRQ(ierr);
2477e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2478e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeHex;
2479e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2480e6ccafaeSMatthew G Knepley 
2481e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesHexSorted[ii+0]) &&
2482e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesHexSorted[ii+1]) &&
2483e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesHexSorted[ii+2]) &&
2484e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesHexSorted[ii+3])) {
2485e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeHex; ++fVertex) {
2486e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeHex; ++cVertex) {
2487e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesHex[ii+fVertex]) {
2488e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2489e6ccafaeSMatthew G Knepley               break;
2490e6ccafaeSMatthew G Knepley             }
2491e6ccafaeSMatthew G Knepley           }
2492e6ccafaeSMatthew G Knepley         }
2493e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2494e6ccafaeSMatthew G Knepley         break;
2495e6ccafaeSMatthew G Knepley       }
2496e6ccafaeSMatthew G Knepley     }
2497e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2498e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2499e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2500e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 10) {
2501e6ccafaeSMatthew G Knepley     /* Quadratic tet */
2502e6ccafaeSMatthew G Knepley     /* Faces are determined by the first 3 vertices (corners of faces) */
2503e6ccafaeSMatthew G Knepley     const PetscInt faceSizeTet = 6;
2504e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[6], i, iFace;
2505e6ccafaeSMatthew G Knepley     PetscBool      found                     = PETSC_FALSE;
2506e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTetSorted[24] = {
2507e6ccafaeSMatthew G Knepley       0, 1, 2,  6, 7, 8, /* bottom */
2508e6ccafaeSMatthew G Knepley       0, 3, 4,  6, 7, 9,  /* front */
2509e6ccafaeSMatthew G Knepley       1, 4, 5,  7, 8, 9,  /* right */
2510e6ccafaeSMatthew G Knepley       2, 3, 5,  6, 8, 9,  /* left */
2511e6ccafaeSMatthew G Knepley     };
2512e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesTet[24] = {
2513e6ccafaeSMatthew G Knepley       0, 1, 2,  6, 7, 8, /* bottom */
2514e6ccafaeSMatthew G Knepley       0, 4, 3,  6, 7, 9,  /* front */
2515e6ccafaeSMatthew G Knepley       1, 5, 4,  7, 8, 9,  /* right */
2516e6ccafaeSMatthew G Knepley       2, 3, 5,  8, 6, 9,  /* left */
2517e6ccafaeSMatthew G Knepley     };
2518e6ccafaeSMatthew G Knepley 
2519e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeTet; ++i) sortedIndices[i] = indices[i];
2520e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeTet, sortedIndices);CHKERRQ(ierr);
2521e6ccafaeSMatthew G Knepley     for (iFace=0; iFace < 4; ++iFace) {
2522e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeTet;
2523e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2524e6ccafaeSMatthew G Knepley 
2525e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesTetSorted[ii+0]) &&
2526e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesTetSorted[ii+1]) &&
2527e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesTetSorted[ii+2]) &&
2528e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesTetSorted[ii+3])) {
2529e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeTet; ++fVertex) {
2530e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeTet; ++cVertex) {
2531e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesTet[ii+fVertex]) {
2532e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2533e6ccafaeSMatthew G Knepley               break;
2534e6ccafaeSMatthew G Knepley             }
2535e6ccafaeSMatthew G Knepley           }
2536e6ccafaeSMatthew G Knepley         }
2537e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2538e6ccafaeSMatthew G Knepley         break;
2539e6ccafaeSMatthew G Knepley       }
2540e6ccafaeSMatthew G Knepley     }
2541e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid tet crossface");
2542e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2543e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2544e6ccafaeSMatthew G Knepley   } else if (cellDim == 3 && numCorners == 27) {
2545e6ccafaeSMatthew G Knepley     /* Quadratic hexes (I hate this)
2546e6ccafaeSMatthew G Knepley        A hex is two oriented quads with the normal of the first
2547e6ccafaeSMatthew G Knepley        pointing up at the second.
2548e6ccafaeSMatthew G Knepley 
2549e6ccafaeSMatthew G Knepley          7---6
2550e6ccafaeSMatthew G Knepley         /|  /|
2551e6ccafaeSMatthew G Knepley        4---5 |
2552e6ccafaeSMatthew G Knepley        | 3-|-2
2553e6ccafaeSMatthew G Knepley        |/  |/
2554e6ccafaeSMatthew G Knepley        0---1
2555e6ccafaeSMatthew G Knepley 
2556e6ccafaeSMatthew G Knepley        Faces are determined by the first 4 vertices (corners of faces) */
2557e6ccafaeSMatthew G Knepley     const PetscInt faceSizeQuadHex = 9;
2558e6ccafaeSMatthew G Knepley     PetscInt       sortedIndices[9], i, iFace;
2559e6ccafaeSMatthew G Knepley     PetscBool      found                         = PETSC_FALSE;
2560e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHexSorted[54] = {
2561e6ccafaeSMatthew G Knepley       0, 1, 2, 3,  8, 9, 10, 11,  24, /* bottom */
2562e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  12, 13, 14, 15,  25, /* top */
2563e6ccafaeSMatthew G Knepley       0, 1, 4, 5,  8, 12, 16, 17,  22, /* front */
2564e6ccafaeSMatthew G Knepley       1, 2, 5, 6,  9, 13, 17, 18,  21, /* right */
2565e6ccafaeSMatthew G Knepley       2, 3, 6, 7,  10, 14, 18, 19,  23, /* back */
2566e6ccafaeSMatthew G Knepley       0, 3, 4, 7,  11, 15, 16, 19,  20, /* left */
2567e6ccafaeSMatthew G Knepley     };
2568e6ccafaeSMatthew G Knepley     PetscInt       faceVerticesQuadHex[54] = {
2569e6ccafaeSMatthew G Knepley       3, 2, 1, 0,  10, 9, 8, 11,  24, /* bottom */
2570e6ccafaeSMatthew G Knepley       4, 5, 6, 7,  12, 13, 14, 15,  25, /* top */
2571e6ccafaeSMatthew G Knepley       0, 1, 5, 4,  8, 17, 12, 16,  22, /* front */
2572e6ccafaeSMatthew G Knepley       1, 2, 6, 5,  9, 18, 13, 17,  21, /* right */
2573e6ccafaeSMatthew G Knepley       2, 3, 7, 6,  10, 19, 14, 18,  23, /* back */
2574e6ccafaeSMatthew G Knepley       3, 0, 4, 7,  11, 16, 15, 19,  20 /* left */
2575e6ccafaeSMatthew G Knepley     };
2576e6ccafaeSMatthew G Knepley 
2577e6ccafaeSMatthew G Knepley     for (i = 0; i < faceSizeQuadHex; ++i) sortedIndices[i] = indices[i];
2578e6ccafaeSMatthew G Knepley     ierr = PetscSortInt(faceSizeQuadHex, sortedIndices);CHKERRQ(ierr);
2579e6ccafaeSMatthew G Knepley     for (iFace = 0; iFace < 6; ++iFace) {
2580e6ccafaeSMatthew G Knepley       const PetscInt ii = iFace*faceSizeQuadHex;
2581e6ccafaeSMatthew G Knepley       PetscInt       fVertex, cVertex;
2582e6ccafaeSMatthew G Knepley 
2583e6ccafaeSMatthew G Knepley       if ((sortedIndices[0] == faceVerticesQuadHexSorted[ii+0]) &&
2584e6ccafaeSMatthew G Knepley           (sortedIndices[1] == faceVerticesQuadHexSorted[ii+1]) &&
2585e6ccafaeSMatthew G Knepley           (sortedIndices[2] == faceVerticesQuadHexSorted[ii+2]) &&
2586e6ccafaeSMatthew G Knepley           (sortedIndices[3] == faceVerticesQuadHexSorted[ii+3])) {
2587e6ccafaeSMatthew G Knepley         for (fVertex = 0; fVertex < faceSizeQuadHex; ++fVertex) {
2588e6ccafaeSMatthew G Knepley           for (cVertex = 0; cVertex < faceSizeQuadHex; ++cVertex) {
2589e6ccafaeSMatthew G Knepley             if (indices[cVertex] == faceVerticesQuadHex[ii+fVertex]) {
2590e6ccafaeSMatthew G Knepley               faceVertices[fVertex] = origVertices[cVertex];
2591e6ccafaeSMatthew G Knepley               break;
2592e6ccafaeSMatthew G Knepley             }
2593e6ccafaeSMatthew G Knepley           }
2594e6ccafaeSMatthew G Knepley         }
2595e6ccafaeSMatthew G Knepley         found = PETSC_TRUE;
2596e6ccafaeSMatthew G Knepley         break;
2597e6ccafaeSMatthew G Knepley       }
2598e6ccafaeSMatthew G Knepley     }
2599e6ccafaeSMatthew G Knepley     if (!found) SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid hex crossface");
2600e6ccafaeSMatthew G Knepley     if (posOriented) *posOriented = PETSC_TRUE;
2601e6ccafaeSMatthew G Knepley     PetscFunctionReturn(0);
2602e6ccafaeSMatthew G Knepley   } else SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Unknown cell type for faceOrientation().");
2603e6ccafaeSMatthew G Knepley   if (!posOrient) {
2604e6ccafaeSMatthew G Knepley     if (debug) {ierr = PetscPrintf(comm, "  Reversing initial face orientation\n");CHKERRQ(ierr);}
2605e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[faceSize-1 - f];
2606e6ccafaeSMatthew G Knepley   } else {
2607e6ccafaeSMatthew G Knepley     if (debug) {ierr = PetscPrintf(comm, "  Keeping initial face orientation\n");CHKERRQ(ierr);}
2608e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) faceVertices[f] = origVertices[f];
2609e6ccafaeSMatthew G Knepley   }
2610e6ccafaeSMatthew G Knepley   if (posOriented) *posOriented = posOrient;
2611e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2612e6ccafaeSMatthew G Knepley }
2613e6ccafaeSMatthew G Knepley 
2614c08575a3SMatthew G. Knepley /*@
2615c08575a3SMatthew G. Knepley   DMPlexGetOrientedFace - Given a cell and a face, as a set of vertices, return the oriented face, as a set of vertices,
2616c08575a3SMatthew G. Knepley   in faceVertices. The orientation is such that the face normal points out of the cell
2617c08575a3SMatthew G. Knepley 
2618c08575a3SMatthew G. Knepley   Not collective
2619c08575a3SMatthew G. Knepley 
2620c08575a3SMatthew G. Knepley   Input Parameters:
2621c08575a3SMatthew G. Knepley + dm           - The original mesh
2622c08575a3SMatthew G. Knepley . cell         - The cell mesh point
2623c08575a3SMatthew G. Knepley . faceSize     - The number of vertices on the face
2624c08575a3SMatthew G. Knepley . face         - The face vertices
2625c08575a3SMatthew G. Knepley . numCorners   - The number of vertices on the cell
2626c08575a3SMatthew G. Knepley . indices      - Local numbering of face vertices in cell cone
2627c08575a3SMatthew G. Knepley - origVertices - Original face vertices
2628c08575a3SMatthew G. Knepley 
2629c08575a3SMatthew G. Knepley   Output Parameter:
2630c08575a3SMatthew G. Knepley + faceVertices - The face vertices properly oriented
2631c08575a3SMatthew G. Knepley - posOriented  - PETSC_TRUE if the face was oriented with outward normal
2632c08575a3SMatthew G. Knepley 
2633c08575a3SMatthew G. Knepley   Level: developer
2634c08575a3SMatthew G. Knepley 
2635c08575a3SMatthew G. Knepley .seealso: DMPlexGetCone()
2636c08575a3SMatthew G. Knepley @*/
2637e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexGetOrientedFace(DM dm, PetscInt cell, PetscInt faceSize, const PetscInt face[], PetscInt numCorners, PetscInt indices[], PetscInt origVertices[], PetscInt faceVertices[], PetscBool *posOriented)
2638e6ccafaeSMatthew G Knepley {
26390298fd71SBarry Smith   const PetscInt *cone = NULL;
2640e6ccafaeSMatthew G Knepley   PetscInt        coneSize, v, f, v2;
2641e6ccafaeSMatthew G Knepley   PetscInt        oppositeVertex = -1;
2642e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
2643e6ccafaeSMatthew G Knepley 
2644e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
2645e6ccafaeSMatthew G Knepley   ierr = DMPlexGetConeSize(dm, cell, &coneSize);CHKERRQ(ierr);
2646e6ccafaeSMatthew G Knepley   ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
2647e6ccafaeSMatthew G Knepley   for (v = 0, v2 = 0; v < coneSize; ++v) {
2648e6ccafaeSMatthew G Knepley     PetscBool found = PETSC_FALSE;
2649e6ccafaeSMatthew G Knepley 
2650e6ccafaeSMatthew G Knepley     for (f = 0; f < faceSize; ++f) {
2651e6ccafaeSMatthew G Knepley       if (face[f] == cone[v]) {
2652e6ccafaeSMatthew G Knepley         found = PETSC_TRUE; break;
2653e6ccafaeSMatthew G Knepley       }
2654e6ccafaeSMatthew G Knepley     }
2655e6ccafaeSMatthew G Knepley     if (found) {
2656e6ccafaeSMatthew G Knepley       indices[v2]      = v;
2657e6ccafaeSMatthew G Knepley       origVertices[v2] = cone[v];
2658e6ccafaeSMatthew G Knepley       ++v2;
2659e6ccafaeSMatthew G Knepley     } else {
2660e6ccafaeSMatthew G Knepley       oppositeVertex = v;
2661e6ccafaeSMatthew G Knepley     }
2662e6ccafaeSMatthew G Knepley   }
2663e6ccafaeSMatthew G Knepley   ierr = DMPlexGetFaceOrientation(dm, cell, numCorners, indices, oppositeVertex, origVertices, faceVertices, posOriented);CHKERRQ(ierr);
2664e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2665e6ccafaeSMatthew G Knepley }
2666e6ccafaeSMatthew G Knepley 
2667e6ccafaeSMatthew G Knepley /*
2668cd0c2139SMatthew G Knepley   DMPlexInsertFace_Internal - Puts a face into the mesh
2669e6ccafaeSMatthew G Knepley 
2670e6ccafaeSMatthew G Knepley   Not collective
2671e6ccafaeSMatthew G Knepley 
2672e6ccafaeSMatthew G Knepley   Input Parameters:
2673e6ccafaeSMatthew G Knepley   + dm              - The DMPlex
2674e6ccafaeSMatthew G Knepley   . numFaceVertex   - The number of vertices in the face
2675e6ccafaeSMatthew G Knepley   . faceVertices    - The vertices in the face for dm
2676e6ccafaeSMatthew G Knepley   . subfaceVertices - The vertices in the face for subdm
2677e6ccafaeSMatthew G Knepley   . numCorners      - The number of vertices in the cell
2678e6ccafaeSMatthew G Knepley   . cell            - A cell in dm containing the face
2679e6ccafaeSMatthew G Knepley   . subcell         - A cell in subdm containing the face
2680e6ccafaeSMatthew G Knepley   . firstFace       - First face in the mesh
2681e6ccafaeSMatthew G Knepley   - newFacePoint    - Next face in the mesh
2682e6ccafaeSMatthew G Knepley 
2683e6ccafaeSMatthew G Knepley   Output Parameters:
2684e6ccafaeSMatthew G Knepley   . newFacePoint - Contains next face point number on input, updated on output
2685e6ccafaeSMatthew G Knepley 
2686e6ccafaeSMatthew G Knepley   Level: developer
2687e6ccafaeSMatthew G Knepley */
2688cd0c2139SMatthew 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)
2689e6ccafaeSMatthew G Knepley {
269082f516ccSBarry Smith   MPI_Comm        comm;
2691e6ccafaeSMatthew G Knepley   DM_Plex        *submesh = (DM_Plex*) subdm->data;
2692e6ccafaeSMatthew G Knepley   const PetscInt *faces;
2693e6ccafaeSMatthew G Knepley   PetscInt        numFaces, coneSize;
2694e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
2695e6ccafaeSMatthew G Knepley 
2696e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
269782f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2698e6ccafaeSMatthew G Knepley   ierr = DMPlexGetConeSize(subdm, subcell, &coneSize);CHKERRQ(ierr);
2699e6ccafaeSMatthew G Knepley   if (coneSize != 1) SETERRQ2(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cone size of cell %d is %d != 1", cell, coneSize);
2700e6ccafaeSMatthew G Knepley #if 0
2701e6ccafaeSMatthew G Knepley   /* Cannot use this because support() has not been constructed yet */
2702e6ccafaeSMatthew G Knepley   ierr = DMPlexGetJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces);CHKERRQ(ierr);
2703e6ccafaeSMatthew G Knepley #else
2704e6ccafaeSMatthew G Knepley   {
2705e6ccafaeSMatthew G Knepley     PetscInt f;
2706e6ccafaeSMatthew G Knepley 
2707e6ccafaeSMatthew G Knepley     numFaces = 0;
270869291d52SBarry Smith     ierr     = DMGetWorkArray(subdm, 1, MPIU_INT, (void **) &faces);CHKERRQ(ierr);
2709e6ccafaeSMatthew G Knepley     for (f = firstFace; f < *newFacePoint; ++f) {
2710e6ccafaeSMatthew G Knepley       PetscInt dof, off, d;
2711e6ccafaeSMatthew G Knepley 
2712e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(submesh->coneSection, f, &dof);CHKERRQ(ierr);
2713e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetOffset(submesh->coneSection, f, &off);CHKERRQ(ierr);
2714e6ccafaeSMatthew G Knepley       /* Yes, I know this is quadratic, but I expect the sizes to be <5 */
2715e6ccafaeSMatthew G Knepley       for (d = 0; d < dof; ++d) {
2716e6ccafaeSMatthew G Knepley         const PetscInt p = submesh->cones[off+d];
2717e6ccafaeSMatthew G Knepley         PetscInt       v;
2718e6ccafaeSMatthew G Knepley 
2719e6ccafaeSMatthew G Knepley         for (v = 0; v < numFaceVertices; ++v) {
2720e6ccafaeSMatthew G Knepley           if (subfaceVertices[v] == p) break;
2721e6ccafaeSMatthew G Knepley         }
2722e6ccafaeSMatthew G Knepley         if (v == numFaceVertices) break;
2723e6ccafaeSMatthew G Knepley       }
2724e6ccafaeSMatthew G Knepley       if (d == dof) {
2725e6ccafaeSMatthew G Knepley         numFaces               = 1;
2726e6ccafaeSMatthew G Knepley         ((PetscInt*) faces)[0] = f;
2727e6ccafaeSMatthew G Knepley       }
2728e6ccafaeSMatthew G Knepley     }
2729e6ccafaeSMatthew G Knepley   }
2730e6ccafaeSMatthew G Knepley #endif
2731e6ccafaeSMatthew G Knepley   if (numFaces > 1) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Vertex set had %d faces, not one", numFaces);
2732e6ccafaeSMatthew G Knepley   else if (numFaces == 1) {
2733e6ccafaeSMatthew G Knepley     /* Add the other cell neighbor for this face */
2734766ab985SMatthew G. Knepley     ierr = DMPlexSetCone(subdm, subcell, faces);CHKERRQ(ierr);
2735e6ccafaeSMatthew G Knepley   } else {
2736e6ccafaeSMatthew G Knepley     PetscInt *indices, *origVertices, *orientedVertices, *orientedSubVertices, v, ov;
2737e6ccafaeSMatthew G Knepley     PetscBool posOriented;
2738e6ccafaeSMatthew G Knepley 
273969291d52SBarry Smith     ierr                = DMGetWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices);CHKERRQ(ierr);
2740e6ccafaeSMatthew G Knepley     origVertices        = &orientedVertices[numFaceVertices];
2741e6ccafaeSMatthew G Knepley     indices             = &orientedVertices[numFaceVertices*2];
2742e6ccafaeSMatthew G Knepley     orientedSubVertices = &orientedVertices[numFaceVertices*3];
2743e6ccafaeSMatthew G Knepley     ierr                = DMPlexGetOrientedFace(dm, cell, numFaceVertices, faceVertices, numCorners, indices, origVertices, orientedVertices, &posOriented);CHKERRQ(ierr);
2744e6ccafaeSMatthew G Knepley     /* TODO: I know that routine should return a permutation, not the indices */
2745e6ccafaeSMatthew G Knepley     for (v = 0; v < numFaceVertices; ++v) {
2746e6ccafaeSMatthew G Knepley       const PetscInt vertex = faceVertices[v], subvertex = subfaceVertices[v];
2747e6ccafaeSMatthew G Knepley       for (ov = 0; ov < numFaceVertices; ++ov) {
2748e6ccafaeSMatthew G Knepley         if (orientedVertices[ov] == vertex) {
2749e6ccafaeSMatthew G Knepley           orientedSubVertices[ov] = subvertex;
2750e6ccafaeSMatthew G Knepley           break;
2751e6ccafaeSMatthew G Knepley         }
2752e6ccafaeSMatthew G Knepley       }
2753e6ccafaeSMatthew G Knepley       if (ov == numFaceVertices) SETERRQ1(comm, PETSC_ERR_PLIB, "Could not find face vertex %d in orientated set", vertex);
2754e6ccafaeSMatthew G Knepley     }
2755e6ccafaeSMatthew G Knepley     ierr = DMPlexSetCone(subdm, *newFacePoint, orientedSubVertices);CHKERRQ(ierr);
2756e6ccafaeSMatthew G Knepley     ierr = DMPlexSetCone(subdm, subcell, newFacePoint);CHKERRQ(ierr);
275769291d52SBarry Smith     ierr = DMRestoreWorkArray(subdm, 4*numFaceVertices * sizeof(PetscInt), MPIU_INT, &orientedVertices);CHKERRQ(ierr);
2758e6ccafaeSMatthew G Knepley     ++(*newFacePoint);
2759e6ccafaeSMatthew G Knepley   }
2760ef07cca7SMatthew G. Knepley #if 0
2761e6ccafaeSMatthew G Knepley   ierr = DMPlexRestoreJoin(subdm, numFaceVertices, subfaceVertices, &numFaces, &faces);CHKERRQ(ierr);
2762ef07cca7SMatthew G. Knepley #else
276369291d52SBarry Smith   ierr = DMRestoreWorkArray(subdm, 1, MPIU_INT, (void **) &faces);CHKERRQ(ierr);
2764ef07cca7SMatthew G. Knepley #endif
2765e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2766e6ccafaeSMatthew G Knepley }
2767e6ccafaeSMatthew G Knepley 
276853156dfcSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmesh_Uninterpolated(DM dm, DMLabel vertexLabel, PetscInt value, DM subdm)
2769e6ccafaeSMatthew G Knepley {
277082f516ccSBarry Smith   MPI_Comm        comm;
277153156dfcSMatthew G. Knepley   DMLabel         subpointMap;
2772efa14ee0SMatthew G Knepley   IS              subvertexIS,  subcellIS;
2773efa14ee0SMatthew G Knepley   const PetscInt *subVertices, *subCells;
2774efa14ee0SMatthew G Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells;
2775fed694aaSMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces = 0, firstSubFace, newFacePoint, nFV = 0;
2776efa14ee0SMatthew G Knepley   PetscInt        vStart, vEnd, c, f;
2777e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
2778e6ccafaeSMatthew G Knepley 
2779e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
278082f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
2781efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
2782efa14ee0SMatthew G Knepley   ierr = DMLabelCreate("subpoint_map", &subpointMap);CHKERRQ(ierr);
2783efa14ee0SMatthew G Knepley   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
2784efa14ee0SMatthew G Knepley   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
278553156dfcSMatthew G. Knepley   if (vertexLabel) {ierr = DMPlexMarkSubmesh_Uninterpolated(dm, vertexLabel, value, subpointMap, &numSubFaces, &nFV, subdm);CHKERRQ(ierr);}
2786efa14ee0SMatthew G Knepley   /* Setup chart */
2787efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumSize(subpointMap, 0, &numSubVertices);CHKERRQ(ierr);
2788efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumSize(subpointMap, 2, &numSubCells);CHKERRQ(ierr);
2789efa14ee0SMatthew G Knepley   ierr = DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices);CHKERRQ(ierr);
2790efa14ee0SMatthew G Knepley   ierr = DMPlexSetVTKCellHeight(subdm, 1);CHKERRQ(ierr);
2791e6ccafaeSMatthew G Knepley   /* Set cone sizes */
2792e6ccafaeSMatthew G Knepley   firstSubVertex = numSubCells;
2793efa14ee0SMatthew G Knepley   firstSubFace   = numSubCells+numSubVertices;
2794e6ccafaeSMatthew G Knepley   newFacePoint   = firstSubFace;
2795efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumIS(subpointMap, 0, &subvertexIS);CHKERRQ(ierr);
2796efa14ee0SMatthew G Knepley   if (subvertexIS) {ierr = ISGetIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
2797efa14ee0SMatthew G Knepley   ierr = DMLabelGetStratumIS(subpointMap, 2, &subcellIS);CHKERRQ(ierr);
2798efa14ee0SMatthew G Knepley   if (subcellIS) {ierr = ISGetIndices(subcellIS, &subCells);CHKERRQ(ierr);}
2799e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
2800e6ccafaeSMatthew G Knepley     ierr = DMPlexSetConeSize(subdm, c, 1);CHKERRQ(ierr);
2801e6ccafaeSMatthew G Knepley   }
2802e6ccafaeSMatthew G Knepley   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
2803e6ccafaeSMatthew G Knepley     ierr = DMPlexSetConeSize(subdm, f, nFV);CHKERRQ(ierr);
2804e6ccafaeSMatthew G Knepley   }
2805e6ccafaeSMatthew G Knepley   ierr = DMSetUp(subdm);CHKERRQ(ierr);
2806e6ccafaeSMatthew G Knepley   /* Create face cones */
2807efa14ee0SMatthew G Knepley   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
28080298fd71SBarry Smith   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
280969291d52SBarry Smith   ierr = DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
2810e6ccafaeSMatthew G Knepley   for (c = 0; c < numSubCells; ++c) {
2811e6ccafaeSMatthew G Knepley     const PetscInt cell    = subCells[c];
2812efa14ee0SMatthew G Knepley     const PetscInt subcell = c;
28130298fd71SBarry Smith     PetscInt      *closure = NULL;
2814efa14ee0SMatthew G Knepley     PetscInt       closureSize, cl, numCorners = 0, faceSize = 0;
2815e6ccafaeSMatthew G Knepley 
2816e6ccafaeSMatthew G Knepley     ierr = DMPlexGetTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2817efa14ee0SMatthew G Knepley     for (cl = 0; cl < closureSize*2; cl += 2) {
2818efa14ee0SMatthew G Knepley       const PetscInt point = closure[cl];
2819e6ccafaeSMatthew G Knepley       PetscInt       subVertex;
2820e6ccafaeSMatthew G Knepley 
2821efa14ee0SMatthew G Knepley       if ((point >= vStart) && (point < vEnd)) {
2822efa14ee0SMatthew G Knepley         ++numCorners;
2823efa14ee0SMatthew G Knepley         ierr = PetscFindInt(point, numSubVertices, subVertices, &subVertex);CHKERRQ(ierr);
2824efa14ee0SMatthew G Knepley         if (subVertex >= 0) {
2825efa14ee0SMatthew G Knepley           closure[faceSize] = point;
282665560c7fSMatthew G Knepley           subface[faceSize] = firstSubVertex+subVertex;
2827e6ccafaeSMatthew G Knepley           ++faceSize;
2828e6ccafaeSMatthew G Knepley         }
2829e6ccafaeSMatthew G Knepley       }
2830e6ccafaeSMatthew G Knepley     }
2831efa14ee0SMatthew G Knepley     if (faceSize > nFV) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Invalid submesh: Too many vertices %d of an element on the surface", faceSize);
2832efa14ee0SMatthew G Knepley     if (faceSize == nFV) {
2833cd0c2139SMatthew G Knepley       ierr = DMPlexInsertFace_Internal(dm, subdm, faceSize, closure, subface, numCorners, cell, subcell, firstSubFace, &newFacePoint);CHKERRQ(ierr);
2834e6ccafaeSMatthew G Knepley     }
283565560c7fSMatthew G Knepley     ierr = DMPlexRestoreTransitiveClosure(dm, cell, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2836e6ccafaeSMatthew G Knepley   }
283769291d52SBarry Smith   ierr = DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
2838e6ccafaeSMatthew G Knepley   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
2839e6ccafaeSMatthew G Knepley   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
2840e6ccafaeSMatthew G Knepley   /* Build coordinates */
2841efa14ee0SMatthew G Knepley   {
2842efa14ee0SMatthew G Knepley     PetscSection coordSection, subCoordSection;
2843efa14ee0SMatthew G Knepley     Vec          coordinates, subCoordinates;
2844efa14ee0SMatthew G Knepley     PetscScalar *coords, *subCoords;
2845285d324eSMatthew G. Knepley     PetscInt     numComp, coordSize, v;
284624640c55SToby Isaac     const char  *name;
2847efa14ee0SMatthew G Knepley 
284869d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
2849e6ccafaeSMatthew G Knepley     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
285069d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
2851285d324eSMatthew G. Knepley     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
2852285d324eSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
2853285d324eSMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
2854efa14ee0SMatthew G Knepley     ierr = PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices);CHKERRQ(ierr);
2855efa14ee0SMatthew G Knepley     for (v = 0; v < numSubVertices; ++v) {
2856efa14ee0SMatthew G Knepley       const PetscInt vertex    = subVertices[v];
2857efa14ee0SMatthew G Knepley       const PetscInt subvertex = firstSubVertex+v;
2858efa14ee0SMatthew G Knepley       PetscInt       dof;
2859efa14ee0SMatthew G Knepley 
2860efa14ee0SMatthew G Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
2861efa14ee0SMatthew G Knepley       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
2862285d324eSMatthew G. Knepley       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
2863e6ccafaeSMatthew G Knepley     }
2864e6ccafaeSMatthew G Knepley     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
2865e6ccafaeSMatthew G Knepley     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
28668b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &subCoordinates);CHKERRQ(ierr);
286724640c55SToby Isaac     ierr = PetscObjectGetName((PetscObject)coordinates,&name);CHKERRQ(ierr);
286824640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)subCoordinates,name);CHKERRQ(ierr);
2869e6ccafaeSMatthew G Knepley     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
28702eb5907fSJed Brown     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
2871830e53efSMatthew G. Knepley     if (coordSize) {
2872e6ccafaeSMatthew G Knepley       ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
2873e6ccafaeSMatthew G Knepley       ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
2874efa14ee0SMatthew G Knepley       for (v = 0; v < numSubVertices; ++v) {
2875efa14ee0SMatthew G Knepley         const PetscInt vertex    = subVertices[v];
2876efa14ee0SMatthew G Knepley         const PetscInt subvertex = firstSubVertex+v;
2877efa14ee0SMatthew G Knepley         PetscInt       dof, off, sdof, soff, d;
2878e6ccafaeSMatthew G Knepley 
2879e6ccafaeSMatthew G Knepley         ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
2880e6ccafaeSMatthew G Knepley         ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
2881efa14ee0SMatthew G Knepley         ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
2882efa14ee0SMatthew G Knepley         ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
2883efa14ee0SMatthew 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);
2884e6ccafaeSMatthew G Knepley         for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
2885e6ccafaeSMatthew G Knepley       }
2886e6ccafaeSMatthew G Knepley       ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
2887e6ccafaeSMatthew G Knepley       ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
28883b399e24SMatthew G. Knepley     }
2889e6ccafaeSMatthew G Knepley     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
2890e6ccafaeSMatthew G Knepley     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
2891e6ccafaeSMatthew G Knepley   }
2892efa14ee0SMatthew G Knepley   /* Cleanup */
2893efa14ee0SMatthew G Knepley   if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
2894efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
2895efa14ee0SMatthew G Knepley   if (subcellIS) {ierr = ISRestoreIndices(subcellIS, &subCells);CHKERRQ(ierr);}
2896efa14ee0SMatthew G Knepley   ierr = ISDestroy(&subcellIS);CHKERRQ(ierr);
2897e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
2898e6ccafaeSMatthew G Knepley }
2899e6ccafaeSMatthew G Knepley 
29003982b651SMatthew G. Knepley PETSC_STATIC_INLINE PetscInt DMPlexFilterPoint_Internal(PetscInt point, PetscInt firstSubPoint, PetscInt numSubPoints, const PetscInt subPoints[])
29013982b651SMatthew G. Knepley {
29023982b651SMatthew G. Knepley   PetscInt       subPoint;
29033982b651SMatthew G. Knepley   PetscErrorCode ierr;
29043982b651SMatthew G. Knepley 
29053982b651SMatthew G. Knepley   ierr = PetscFindInt(point, numSubPoints, subPoints, &subPoint); if (ierr < 0) return ierr;
29063982b651SMatthew G. Knepley   return subPoint < 0 ? subPoint : firstSubPoint+subPoint;
29073982b651SMatthew G. Knepley }
29083982b651SMatthew G. Knepley 
2909158acfadSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmeshGeneric_Interpolated(DM dm, DMLabel label, PetscInt value, PetscBool markedFaces, PetscBool isCohesive, PetscInt cellHeight, DM subdm)
2910e6ccafaeSMatthew G Knepley {
291182f516ccSBarry Smith   MPI_Comm         comm;
291253156dfcSMatthew G. Knepley   DMLabel          subpointMap;
2913efa14ee0SMatthew G Knepley   IS              *subpointIS;
2914efa14ee0SMatthew G Knepley   const PetscInt **subpoints;
29153982b651SMatthew G. Knepley   PetscInt        *numSubPoints, *firstSubPoint, *coneNew, *orntNew;
29160d366550SMatthew G. Knepley   PetscInt         totSubPoints = 0, maxConeSize, cMax, cEnd, dim, p, d, v;
29170d366550SMatthew G. Knepley   PetscMPIInt      rank;
2918e6ccafaeSMatthew G Knepley   PetscErrorCode   ierr;
2919e6ccafaeSMatthew G Knepley 
2920e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
292182f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
29220d366550SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
2923efa14ee0SMatthew G Knepley   /* Create subpointMap which marks the submesh */
2924e6ccafaeSMatthew G Knepley   ierr = DMLabelCreate("subpoint_map", &subpointMap);CHKERRQ(ierr);
2925efa14ee0SMatthew G Knepley   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
2926bec263e5SMatthew G. Knepley   if (cellHeight) {
29273982b651SMatthew G. Knepley     if (isCohesive) {ierr = DMPlexMarkCohesiveSubmesh_Interpolated(dm, label, value, subpointMap, subdm);CHKERRQ(ierr);}
2928158acfadSMatthew G. Knepley     else            {ierr = DMPlexMarkSubmesh_Interpolated(dm, label, value, markedFaces, subpointMap, subdm);CHKERRQ(ierr);}
2929bec263e5SMatthew G. Knepley   } else {
2930bec263e5SMatthew G. Knepley     DMLabel         depth;
2931bec263e5SMatthew G. Knepley     IS              pointIS;
2932bec263e5SMatthew G. Knepley     const PetscInt *points;
2933bec263e5SMatthew G. Knepley     PetscInt        numPoints;
2934bec263e5SMatthew G. Knepley 
2935bec263e5SMatthew G. Knepley     ierr = DMPlexGetDepthLabel(dm, &depth);CHKERRQ(ierr);
2936bec263e5SMatthew G. Knepley     ierr = DMLabelGetStratumSize(label, value, &numPoints);CHKERRQ(ierr);
2937bec263e5SMatthew G. Knepley     ierr = DMLabelGetStratumIS(label, value, &pointIS);CHKERRQ(ierr);
2938bec263e5SMatthew G. Knepley     ierr = ISGetIndices(pointIS, &points);CHKERRQ(ierr);
2939bec263e5SMatthew G. Knepley     for (p = 0; p < numPoints; ++p) {
2940bec263e5SMatthew G. Knepley       PetscInt *closure = NULL;
2941bec263e5SMatthew G. Knepley       PetscInt  closureSize, c, pdim;
2942bec263e5SMatthew G. Knepley 
2943bec263e5SMatthew G. Knepley       ierr = DMPlexGetTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2944bec263e5SMatthew G. Knepley       for (c = 0; c < closureSize*2; c += 2) {
2945bec263e5SMatthew G. Knepley         ierr = DMLabelGetValue(depth, closure[c], &pdim);CHKERRQ(ierr);
2946bec263e5SMatthew G. Knepley         ierr = DMLabelSetValue(subpointMap, closure[c], pdim);CHKERRQ(ierr);
2947bec263e5SMatthew G. Knepley       }
2948bec263e5SMatthew G. Knepley       ierr = DMPlexRestoreTransitiveClosure(dm, points[p], PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
2949bec263e5SMatthew G. Knepley     }
2950bec263e5SMatthew G. Knepley     ierr = ISRestoreIndices(pointIS, &points);CHKERRQ(ierr);
2951bec263e5SMatthew G. Knepley     ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
2952bec263e5SMatthew G. Knepley   }
2953bec263e5SMatthew G. Knepley   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
29540d366550SMatthew G. Knepley   ierr = DMPlexGetHeightStratum(dm, 0, NULL, &cEnd);CHKERRQ(ierr);
29550d366550SMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
29560d366550SMatthew G. Knepley   cMax = (cMax < 0) ? cEnd : cMax;
2957efa14ee0SMatthew G Knepley   /* Setup chart */
2958c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
2959dcca6d9dSJed Brown   ierr = PetscMalloc4(dim+1,&numSubPoints,dim+1,&firstSubPoint,dim+1,&subpointIS,dim+1,&subpoints);CHKERRQ(ierr);
2960e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
2961e6ccafaeSMatthew G Knepley     ierr = DMLabelGetStratumSize(subpointMap, d, &numSubPoints[d]);CHKERRQ(ierr);
2962e6ccafaeSMatthew G Knepley     totSubPoints += numSubPoints[d];
2963e6ccafaeSMatthew G Knepley   }
2964e6ccafaeSMatthew G Knepley   ierr = DMPlexSetChart(subdm, 0, totSubPoints);CHKERRQ(ierr);
2965bec263e5SMatthew G. Knepley   ierr = DMPlexSetVTKCellHeight(subdm, cellHeight);CHKERRQ(ierr);
2966e6ccafaeSMatthew G Knepley   /* Set cone sizes */
2967e6ccafaeSMatthew G Knepley   firstSubPoint[dim] = 0;
2968e6ccafaeSMatthew G Knepley   firstSubPoint[0]   = firstSubPoint[dim] + numSubPoints[dim];
2969e6ccafaeSMatthew G Knepley   if (dim > 1) {firstSubPoint[dim-1] = firstSubPoint[0]     + numSubPoints[0];}
2970e6ccafaeSMatthew G Knepley   if (dim > 2) {firstSubPoint[dim-2] = firstSubPoint[dim-1] + numSubPoints[dim-1];}
2971e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
2972e6ccafaeSMatthew G Knepley     ierr = DMLabelGetStratumIS(subpointMap, d, &subpointIS[d]);CHKERRQ(ierr);
29730ae02aa4SMatthew G. Knepley     if (subpointIS[d]) {ierr = ISGetIndices(subpointIS[d], &subpoints[d]);CHKERRQ(ierr);}
2974e6ccafaeSMatthew G Knepley   }
2975e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
2976e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
2977e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
2978e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
2979e6ccafaeSMatthew G Knepley       const PetscInt *cone;
2980e6ccafaeSMatthew G Knepley       PetscInt        coneSize, coneSizeNew, c, val;
2981e6ccafaeSMatthew G Knepley 
2982e6ccafaeSMatthew G Knepley       ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
2983e6ccafaeSMatthew G Knepley       ierr = DMPlexSetConeSize(subdm, subpoint, coneSize);CHKERRQ(ierr);
2984bec263e5SMatthew G. Knepley       if (cellHeight && (d == dim)) {
2985e6ccafaeSMatthew G Knepley         ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
2986e6ccafaeSMatthew G Knepley         for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
2987e6ccafaeSMatthew G Knepley           ierr = DMLabelGetValue(subpointMap, cone[c], &val);CHKERRQ(ierr);
2988e6ccafaeSMatthew G Knepley           if (val >= 0) coneSizeNew++;
2989e6ccafaeSMatthew G Knepley         }
2990e6ccafaeSMatthew G Knepley         ierr = DMPlexSetConeSize(subdm, subpoint, coneSizeNew);CHKERRQ(ierr);
2991e6ccafaeSMatthew G Knepley       }
2992e6ccafaeSMatthew G Knepley     }
2993e6ccafaeSMatthew G Knepley   }
2994e6ccafaeSMatthew G Knepley   ierr = DMSetUp(subdm);CHKERRQ(ierr);
2995e6ccafaeSMatthew G Knepley   /* Set cones */
29960298fd71SBarry Smith   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
29973982b651SMatthew G. Knepley   ierr = PetscMalloc2(maxConeSize,&coneNew,maxConeSize,&orntNew);CHKERRQ(ierr);
2998e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
2999e6ccafaeSMatthew G Knepley     for (p = 0; p < numSubPoints[d]; ++p) {
3000e6ccafaeSMatthew G Knepley       const PetscInt  point    = subpoints[d][p];
3001e6ccafaeSMatthew G Knepley       const PetscInt  subpoint = firstSubPoint[d] + p;
30020e49e2e2SMatthew G. Knepley       const PetscInt *cone, *ornt;
30030d366550SMatthew G. Knepley       PetscInt        coneSize, subconeSize, coneSizeNew, c, subc, fornt = 0;
3004e6ccafaeSMatthew G Knepley 
30050d366550SMatthew G. Knepley       if (d == dim-1) {
30060d366550SMatthew G. Knepley         const PetscInt *support, *cone, *ornt;
30070d366550SMatthew G. Knepley         PetscInt        supportSize, coneSize, s, subc;
30080d366550SMatthew G. Knepley 
30090d366550SMatthew G. Knepley         ierr = DMPlexGetSupport(dm, point, &support);CHKERRQ(ierr);
30100d366550SMatthew G. Knepley         ierr = DMPlexGetSupportSize(dm, point, &supportSize);CHKERRQ(ierr);
30110d366550SMatthew G. Knepley         for (s = 0; s < supportSize; ++s) {
30120d366550SMatthew G. Knepley           if ((support[s] < cMax) || (support[s] >= cEnd)) continue;
30130d366550SMatthew G. Knepley           ierr = PetscFindInt(support[s], numSubPoints[d+1], subpoints[d+1], &subc);CHKERRQ(ierr);
30140d366550SMatthew G. Knepley           if (subc >= 0) {
30150d366550SMatthew G. Knepley             const PetscInt ccell = subpoints[d+1][subc];
30160d366550SMatthew G. Knepley 
30177f79407eSBarry Smith             ierr = DMPlexGetCone(dm, ccell, &cone);CHKERRQ(ierr);
30187f79407eSBarry Smith             ierr = DMPlexGetConeSize(dm, ccell, &coneSize);CHKERRQ(ierr);
30197f79407eSBarry Smith             ierr = DMPlexGetConeOrientation(dm, ccell, &ornt);CHKERRQ(ierr);
30200d366550SMatthew G. Knepley             for (c = 0; c < coneSize; ++c) {
30210d366550SMatthew G. Knepley               if (cone[c] == point) {
30220d366550SMatthew G. Knepley                 fornt = ornt[c];
30230d366550SMatthew G. Knepley                 break;
30240d366550SMatthew G. Knepley               }
30250d366550SMatthew G. Knepley             }
30260d366550SMatthew G. Knepley             break;
30270d366550SMatthew G. Knepley           }
30280d366550SMatthew G. Knepley         }
30290d366550SMatthew G. Knepley       }
3030e6ccafaeSMatthew G Knepley       ierr = DMPlexGetConeSize(dm, point, &coneSize);CHKERRQ(ierr);
3031e6ccafaeSMatthew G Knepley       ierr = DMPlexGetConeSize(subdm, subpoint, &subconeSize);CHKERRQ(ierr);
3032e6ccafaeSMatthew G Knepley       ierr = DMPlexGetCone(dm, point, &cone);CHKERRQ(ierr);
30330e49e2e2SMatthew G. Knepley       ierr = DMPlexGetConeOrientation(dm, point, &ornt);CHKERRQ(ierr);
3034e6ccafaeSMatthew G Knepley       for (c = 0, coneSizeNew = 0; c < coneSize; ++c) {
3035e6ccafaeSMatthew G Knepley         ierr = PetscFindInt(cone[c], numSubPoints[d-1], subpoints[d-1], &subc);CHKERRQ(ierr);
303601a2673eSMatthew G. Knepley         if (subc >= 0) {
303701a2673eSMatthew G. Knepley           coneNew[coneSizeNew] = firstSubPoint[d-1] + subc;
30383982b651SMatthew G. Knepley           orntNew[coneSizeNew] = ornt[c];
303901a2673eSMatthew G. Knepley           ++coneSizeNew;
304001a2673eSMatthew G. Knepley         }
3041e6ccafaeSMatthew G Knepley       }
3042e6ccafaeSMatthew G Knepley       if (coneSizeNew != subconeSize) SETERRQ2(comm, PETSC_ERR_PLIB, "Number of cone points located %d does not match subcone size %d", coneSizeNew, subconeSize);
30430d366550SMatthew G. Knepley       if (fornt < 0) {
30440d366550SMatthew G. Knepley         /* This should be replaced by a call to DMPlexReverseCell() */
30450d366550SMatthew G. Knepley #if 0
3046a129c400SMatthew G. Knepley         ierr = DMPlexReverseCell(subdm, subpoint);CHKERRQ(ierr);
3047a129c400SMatthew G. Knepley #else
3048c0b40891SMatthew G. Knepley         for (c = 0; c < coneSizeNew/2 + coneSizeNew%2; ++c) {
3049a129c400SMatthew G. Knepley           PetscInt faceSize, tmp;
30500d366550SMatthew G. Knepley 
30510d366550SMatthew G. Knepley           tmp        = coneNew[c];
30520d366550SMatthew G. Knepley           coneNew[c] = coneNew[coneSizeNew-1-c];
30530d366550SMatthew G. Knepley           coneNew[coneSizeNew-1-c] = tmp;
3054a129c400SMatthew G. Knepley           ierr = DMPlexGetConeSize(dm, cone[c], &faceSize);CHKERRQ(ierr);
3055a129c400SMatthew G. Knepley           tmp        = orntNew[c] >= 0 ? -(faceSize-orntNew[c]) : faceSize+orntNew[c];
3056a129c400SMatthew G. Knepley           orntNew[c] = orntNew[coneSizeNew-1-c] >= 0 ? -(faceSize-orntNew[coneSizeNew-1-c]) : faceSize+orntNew[coneSizeNew-1-c];
30570d366550SMatthew G. Knepley           orntNew[coneSizeNew-1-c] = tmp;
30580d366550SMatthew G. Knepley         }
30590d366550SMatthew G. Knepley       }
3060e6ccafaeSMatthew G Knepley       ierr = DMPlexSetCone(subdm, subpoint, coneNew);CHKERRQ(ierr);
30613982b651SMatthew G. Knepley       ierr = DMPlexSetConeOrientation(subdm, subpoint, orntNew);CHKERRQ(ierr);
3062a129c400SMatthew G. Knepley #endif
3063e6ccafaeSMatthew G Knepley     }
3064e6ccafaeSMatthew G Knepley   }
30653982b651SMatthew G. Knepley   ierr = PetscFree2(coneNew,orntNew);CHKERRQ(ierr);
3066e6ccafaeSMatthew G Knepley   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
3067e6ccafaeSMatthew G Knepley   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
3068e6ccafaeSMatthew G Knepley   /* Build coordinates */
3069e6ccafaeSMatthew G Knepley   {
3070e6ccafaeSMatthew G Knepley     PetscSection coordSection, subCoordSection;
3071e6ccafaeSMatthew G Knepley     Vec          coordinates, subCoordinates;
3072e6ccafaeSMatthew G Knepley     PetscScalar *coords, *subCoords;
3073c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize;
307424640c55SToby Isaac     const char  *name;
3075e6ccafaeSMatthew G Knepley 
3076c0e8cf5fSMatthew G. Knepley     ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
307769d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
3078e6ccafaeSMatthew G Knepley     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
307969d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
3080285d324eSMatthew G. Knepley     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
3081285d324eSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
3082285d324eSMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
3083e6ccafaeSMatthew G Knepley     ierr = PetscSectionSetChart(subCoordSection, firstSubPoint[0], firstSubPoint[0]+numSubPoints[0]);CHKERRQ(ierr);
3084e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3085e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3086e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0]+v;
3087e6ccafaeSMatthew G Knepley       PetscInt       dof;
3088e6ccafaeSMatthew G Knepley 
3089e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3090e6ccafaeSMatthew G Knepley       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
3091285d324eSMatthew G. Knepley       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
3092e6ccafaeSMatthew G Knepley     }
3093e6ccafaeSMatthew G Knepley     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
3094e6ccafaeSMatthew G Knepley     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
30958b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &subCoordinates);CHKERRQ(ierr);
309624640c55SToby Isaac     ierr = PetscObjectGetName((PetscObject)coordinates,&name);CHKERRQ(ierr);
309724640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)subCoordinates,name);CHKERRQ(ierr);
3098e6ccafaeSMatthew G Knepley     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
3099c0e8cf5fSMatthew G. Knepley     ierr = VecSetBlockSize(subCoordinates, cdim);CHKERRQ(ierr);
31002eb5907fSJed Brown     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
3101e6ccafaeSMatthew G Knepley     ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
3102e6ccafaeSMatthew G Knepley     ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3103e6ccafaeSMatthew G Knepley     for (v = 0; v < numSubPoints[0]; ++v) {
3104e6ccafaeSMatthew G Knepley       const PetscInt vertex    = subpoints[0][v];
3105e6ccafaeSMatthew G Knepley       const PetscInt subvertex = firstSubPoint[0]+v;
3106e6ccafaeSMatthew G Knepley       PetscInt dof, off, sdof, soff, d;
3107e6ccafaeSMatthew G Knepley 
3108e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3109e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
3110e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
3111e6ccafaeSMatthew G Knepley       ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
3112e6ccafaeSMatthew 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);
3113efa14ee0SMatthew G Knepley       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3114e6ccafaeSMatthew G Knepley     }
3115e6ccafaeSMatthew G Knepley     ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
3116e6ccafaeSMatthew G Knepley     ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3117e6ccafaeSMatthew G Knepley     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
3118e6ccafaeSMatthew G Knepley     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
3119e6ccafaeSMatthew G Knepley   }
31203982b651SMatthew G. Knepley   /* Build SF: We need this complexity because subpoints might not be selected on the owning process */
31213982b651SMatthew G. Knepley   {
31223982b651SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
31233982b651SMatthew G. Knepley     IS                 subpIS;
31243982b651SMatthew G. Knepley     const PetscSFNode *remotePoints;
31253982b651SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
31263982b651SMatthew G. Knepley     const PetscInt    *localPoints, *subpoints;
31273982b651SMatthew G. Knepley     PetscInt          *slocalPoints;
31283982b651SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubpoints = 0, numSubroots, numSubleaves = 0, l, sl, ll, pStart, pEnd, p;
31293982b651SMatthew G. Knepley     PetscMPIInt        rank;
31303982b651SMatthew G. Knepley 
31313982b651SMatthew G. Knepley     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
31323982b651SMatthew G. Knepley     ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
31333982b651SMatthew G. Knepley     ierr = DMGetPointSF(subdm, &sfPointSub);CHKERRQ(ierr);
31343982b651SMatthew G. Knepley     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
31353982b651SMatthew G. Knepley     ierr = DMPlexGetChart(subdm, NULL, &numSubroots);CHKERRQ(ierr);
31363982b651SMatthew G. Knepley     ierr = DMPlexCreateSubpointIS(subdm, &subpIS);CHKERRQ(ierr);
31373982b651SMatthew G. Knepley     if (subpIS) {
31383982b651SMatthew G. Knepley       ierr = ISGetIndices(subpIS, &subpoints);CHKERRQ(ierr);
31393982b651SMatthew G. Knepley       ierr = ISGetLocalSize(subpIS, &numSubpoints);CHKERRQ(ierr);
31403982b651SMatthew G. Knepley     }
31413982b651SMatthew G. Knepley     ierr = PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr);
31423982b651SMatthew G. Knepley     if (numRoots >= 0) {
31433982b651SMatthew G. Knepley       ierr = PetscMalloc2(pEnd-pStart,&newLocalPoints,numRoots,&newOwners);CHKERRQ(ierr);
31443982b651SMatthew G. Knepley       for (p = 0; p < pEnd-pStart; ++p) {
31453982b651SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
31463982b651SMatthew G. Knepley         newLocalPoints[p].index = -2;
31473982b651SMatthew G. Knepley       }
31483982b651SMatthew G. Knepley       /* Set subleaves */
31493982b651SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
31503982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
31513982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(point, 0, numSubpoints, subpoints);
31523982b651SMatthew G. Knepley 
31533982b651SMatthew G. Knepley         if (subpoint < 0) continue;
31543982b651SMatthew G. Knepley         newLocalPoints[point-pStart].rank  = rank;
31553982b651SMatthew G. Knepley         newLocalPoints[point-pStart].index = subpoint;
31563982b651SMatthew G. Knepley         ++numSubleaves;
31573982b651SMatthew G. Knepley       }
31583982b651SMatthew G. Knepley       /* Must put in owned subpoints */
31593982b651SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
31603982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(p, 0, numSubpoints, subpoints);
31613982b651SMatthew G. Knepley 
31623982b651SMatthew G. Knepley         if (subpoint < 0) {
31633982b651SMatthew G. Knepley           newOwners[p-pStart].rank  = -3;
31643982b651SMatthew G. Knepley           newOwners[p-pStart].index = -3;
31653982b651SMatthew G. Knepley         } else {
31663982b651SMatthew G. Knepley           newOwners[p-pStart].rank  = rank;
31673982b651SMatthew G. Knepley           newOwners[p-pStart].index = subpoint;
31683982b651SMatthew G. Knepley         }
31693982b651SMatthew G. Knepley       }
31703982b651SMatthew G. Knepley       ierr = PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
31713982b651SMatthew G. Knepley       ierr = PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
31723982b651SMatthew G. Knepley       ierr = PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr);
31733982b651SMatthew G. Knepley       ierr = PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr);
31743982b651SMatthew G. Knepley       ierr = PetscMalloc1(numSubleaves, &slocalPoints);CHKERRQ(ierr);
31753982b651SMatthew G. Knepley       ierr = PetscMalloc1(numSubleaves, &sremotePoints);CHKERRQ(ierr);
31763982b651SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
31773982b651SMatthew G. Knepley         const PetscInt point    = localPoints[l];
31783982b651SMatthew G. Knepley         const PetscInt subpoint = DMPlexFilterPoint_Internal(point, 0, numSubpoints, subpoints);
31793982b651SMatthew G. Knepley 
31803982b651SMatthew G. Knepley         if (subpoint < 0) continue;
31813982b651SMatthew G. Knepley         if (newLocalPoints[point].rank == rank) {++ll; continue;}
31823982b651SMatthew G. Knepley         slocalPoints[sl]        = subpoint;
31833982b651SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
31843982b651SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
31853982b651SMatthew G. Knepley         if (sremotePoints[sl].rank  < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %d", point);
31863982b651SMatthew G. Knepley         if (sremotePoints[sl].index < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %d", point);
31873982b651SMatthew G. Knepley         ++sl;
31883982b651SMatthew G. Knepley       }
31893982b651SMatthew G. Knepley       if (sl + ll != numSubleaves) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %d + %d != %d", sl, ll, numSubleaves);
31903982b651SMatthew G. Knepley       ierr = PetscFree2(newLocalPoints,newOwners);CHKERRQ(ierr);
31913982b651SMatthew G. Knepley       ierr = PetscSFSetGraph(sfPointSub, numSubroots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
31923982b651SMatthew G. Knepley     }
31933982b651SMatthew G. Knepley     if (subpIS) {
31943982b651SMatthew G. Knepley       ierr = ISRestoreIndices(subpIS, &subpoints);CHKERRQ(ierr);
31953982b651SMatthew G. Knepley       ierr = ISDestroy(&subpIS);CHKERRQ(ierr);
31963982b651SMatthew G. Knepley     }
31973982b651SMatthew G. Knepley   }
3198efa14ee0SMatthew G Knepley   /* Cleanup */
3199e6ccafaeSMatthew G Knepley   for (d = 0; d <= dim; ++d) {
32000ae02aa4SMatthew G. Knepley     if (subpointIS[d]) {ierr = ISRestoreIndices(subpointIS[d], &subpoints[d]);CHKERRQ(ierr);}
3201e6ccafaeSMatthew G Knepley     ierr = ISDestroy(&subpointIS[d]);CHKERRQ(ierr);
3202e6ccafaeSMatthew G Knepley   }
3203efa14ee0SMatthew G Knepley   ierr = PetscFree4(numSubPoints,firstSubPoint,subpointIS,subpoints);CHKERRQ(ierr);
3204e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3205e6ccafaeSMatthew G Knepley }
3206e6ccafaeSMatthew G Knepley 
3207158acfadSMatthew G. Knepley static PetscErrorCode DMPlexCreateSubmesh_Interpolated(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM subdm)
32083982b651SMatthew G. Knepley {
32093982b651SMatthew G. Knepley   PetscErrorCode ierr;
32103982b651SMatthew G. Knepley 
32113982b651SMatthew G. Knepley   PetscFunctionBegin;
3212158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmeshGeneric_Interpolated(dm, vertexLabel, value, markedFaces, PETSC_FALSE, 1, subdm);CHKERRQ(ierr);
32133982b651SMatthew G. Knepley   PetscFunctionReturn(0);
32143982b651SMatthew G. Knepley }
32153982b651SMatthew G. Knepley 
3216d0fa310fSMatthew G. Knepley /*@
3217e6ccafaeSMatthew G Knepley   DMPlexCreateSubmesh - Extract a hypersurface from the mesh using vertices defined by a label
3218e6ccafaeSMatthew G Knepley 
3219e6ccafaeSMatthew G Knepley   Input Parameters:
3220e6ccafaeSMatthew G Knepley + dm           - The original mesh
3221158acfadSMatthew G. Knepley . vertexLabel  - The DMLabel marking points contained in the surface
3222158acfadSMatthew G. Knepley . value        - The label value to use
3223158acfadSMatthew G. Knepley - markedFaces  - PETSC_TRUE if surface faces are marked in addition to vertices, PETSC_FALSE if only vertices are marked
3224e6ccafaeSMatthew G Knepley 
3225e6ccafaeSMatthew G Knepley   Output Parameter:
3226e6ccafaeSMatthew G Knepley . subdm - The surface mesh
3227e6ccafaeSMatthew G Knepley 
3228e6ccafaeSMatthew G Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3229e6ccafaeSMatthew G Knepley 
3230e6ccafaeSMatthew G Knepley   Level: developer
3231e6ccafaeSMatthew G Knepley 
3232c58f1c22SToby Isaac .seealso: DMPlexGetSubpointMap(), DMGetLabel(), DMLabelSetValue()
3233830e53efSMatthew G. Knepley @*/
3234158acfadSMatthew G. Knepley PetscErrorCode DMPlexCreateSubmesh(DM dm, DMLabel vertexLabel, PetscInt value, PetscBool markedFaces, DM *subdm)
3235e6ccafaeSMatthew G Knepley {
3236dd2763adSMatthew G. Knepley   PetscInt       dim, cdim, depth;
3237e6ccafaeSMatthew G Knepley   PetscErrorCode ierr;
3238e6ccafaeSMatthew G Knepley 
3239e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3240e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3241766ab985SMatthew G. Knepley   PetscValidPointer(subdm, 3);
3242c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3243e6ccafaeSMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
324482f516ccSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)dm), subdm);CHKERRQ(ierr);
3245e6ccafaeSMatthew G Knepley   ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr);
3246c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(*subdm, dim-1);CHKERRQ(ierr);
3247dd2763adSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3248dd2763adSMatthew G. Knepley   ierr = DMSetCoordinateDim(*subdm, cdim);CHKERRQ(ierr);
3249e6ccafaeSMatthew G Knepley   if (depth == dim) {
3250158acfadSMatthew G. Knepley     ierr = DMPlexCreateSubmesh_Interpolated(dm, vertexLabel, value, markedFaces, *subdm);CHKERRQ(ierr);
3251e6ccafaeSMatthew G Knepley   } else {
3252830e53efSMatthew G. Knepley     ierr = DMPlexCreateSubmesh_Uninterpolated(dm, vertexLabel, value, *subdm);CHKERRQ(ierr);
3253e6ccafaeSMatthew G Knepley   }
3254e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3255e6ccafaeSMatthew G Knepley }
3256e6ccafaeSMatthew G Knepley 
325727c04023SMatthew G. Knepley static PetscErrorCode DMPlexCreateCohesiveSubmesh_Uninterpolated(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM subdm)
3258766ab985SMatthew G. Knepley {
3259766ab985SMatthew G. Knepley   MPI_Comm        comm;
3260766ab985SMatthew G. Knepley   DMLabel         subpointMap;
3261766ab985SMatthew G. Knepley   IS              subvertexIS;
3262766ab985SMatthew G. Knepley   const PetscInt *subVertices;
3263fed694aaSMatthew G. Knepley   PetscInt        numSubVertices, firstSubVertex, numSubCells, *subCells = NULL;
3264766ab985SMatthew G. Knepley   PetscInt       *subface, maxConeSize, numSubFaces, firstSubFace, newFacePoint, nFV;
326587feddfdSMatthew G. Knepley   PetscInt        cMax, c, f;
3266766ab985SMatthew G. Knepley   PetscErrorCode  ierr;
3267766ab985SMatthew G. Knepley 
3268766ab985SMatthew G. Knepley   PetscFunctionBegin;
3269766ab985SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm, &comm);CHKERRQ(ierr);
3270766ab985SMatthew G. Knepley   /* Create subpointMap which marks the submesh */
3271766ab985SMatthew G. Knepley   ierr = DMLabelCreate("subpoint_map", &subpointMap);CHKERRQ(ierr);
3272766ab985SMatthew G. Knepley   ierr = DMPlexSetSubpointMap(subdm, subpointMap);CHKERRQ(ierr);
3273766ab985SMatthew G. Knepley   ierr = DMLabelDestroy(&subpointMap);CHKERRQ(ierr);
327427c04023SMatthew G. Knepley   ierr = DMPlexMarkCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, subpointMap, &numSubFaces, &nFV, &subCells, subdm);CHKERRQ(ierr);
3275766ab985SMatthew G. Knepley   /* Setup chart */
3276766ab985SMatthew G. Knepley   ierr = DMLabelGetStratumSize(subpointMap, 0, &numSubVertices);CHKERRQ(ierr);
3277766ab985SMatthew G. Knepley   ierr = DMLabelGetStratumSize(subpointMap, 2, &numSubCells);CHKERRQ(ierr);
3278766ab985SMatthew G. Knepley   ierr = DMPlexSetChart(subdm, 0, numSubCells+numSubFaces+numSubVertices);CHKERRQ(ierr);
3279766ab985SMatthew G. Knepley   ierr = DMPlexSetVTKCellHeight(subdm, 1);CHKERRQ(ierr);
3280766ab985SMatthew G. Knepley   /* Set cone sizes */
3281766ab985SMatthew G. Knepley   firstSubVertex = numSubCells;
3282766ab985SMatthew G. Knepley   firstSubFace   = numSubCells+numSubVertices;
3283766ab985SMatthew G. Knepley   newFacePoint   = firstSubFace;
3284766ab985SMatthew G. Knepley   ierr = DMLabelGetStratumIS(subpointMap, 0, &subvertexIS);CHKERRQ(ierr);
3285766ab985SMatthew G. Knepley   if (subvertexIS) {ierr = ISGetIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
3286766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3287766ab985SMatthew G. Knepley     ierr = DMPlexSetConeSize(subdm, c, 1);CHKERRQ(ierr);
3288766ab985SMatthew G. Knepley   }
3289766ab985SMatthew G. Knepley   for (f = firstSubFace; f < firstSubFace+numSubFaces; ++f) {
3290766ab985SMatthew G. Knepley     ierr = DMPlexSetConeSize(subdm, f, nFV);CHKERRQ(ierr);
3291766ab985SMatthew G. Knepley   }
3292766ab985SMatthew G. Knepley   ierr = DMSetUp(subdm);CHKERRQ(ierr);
3293766ab985SMatthew G. Knepley   /* Create face cones */
3294766ab985SMatthew G. Knepley   ierr = DMPlexGetMaxSizes(dm, &maxConeSize, NULL);CHKERRQ(ierr);
329587feddfdSMatthew G. Knepley   ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
329669291d52SBarry Smith   ierr = DMGetWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
3297766ab985SMatthew G. Knepley   for (c = 0; c < numSubCells; ++c) {
3298766ab985SMatthew G. Knepley     const PetscInt  cell    = subCells[c];
3299766ab985SMatthew G. Knepley     const PetscInt  subcell = c;
330087feddfdSMatthew G. Knepley     const PetscInt *cone, *cells;
330187feddfdSMatthew G. Knepley     PetscInt        numCells, subVertex, p, v;
3302766ab985SMatthew G. Knepley 
330387feddfdSMatthew G. Knepley     if (cell < cMax) continue;
330487feddfdSMatthew G. Knepley     ierr = DMPlexGetCone(dm, cell, &cone);CHKERRQ(ierr);
330587feddfdSMatthew G. Knepley     for (v = 0; v < nFV; ++v) {
330687feddfdSMatthew G. Knepley       ierr = PetscFindInt(cone[v], numSubVertices, subVertices, &subVertex);CHKERRQ(ierr);
330787feddfdSMatthew G. Knepley       subface[v] = firstSubVertex+subVertex;
330887feddfdSMatthew G. Knepley     }
330987feddfdSMatthew G. Knepley     ierr = DMPlexSetCone(subdm, newFacePoint, subface);CHKERRQ(ierr);
331087feddfdSMatthew G. Knepley     ierr = DMPlexSetCone(subdm, subcell, &newFacePoint);CHKERRQ(ierr);
331187feddfdSMatthew G. Knepley     ierr = DMPlexGetJoin(dm, nFV, cone, &numCells, &cells);CHKERRQ(ierr);
331227234c99SMatthew G. Knepley     /* Not true in parallel
331327234c99SMatthew G. Knepley     if (numCells != 2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cohesive cells should separate two cells"); */
331487feddfdSMatthew G. Knepley     for (p = 0; p < numCells; ++p) {
331587feddfdSMatthew G. Knepley       PetscInt negsubcell;
3316766ab985SMatthew G. Knepley 
331787feddfdSMatthew G. Knepley       if (cells[p] >= cMax) continue;
331887feddfdSMatthew G. Knepley       /* I know this is a crap search */
331987feddfdSMatthew G. Knepley       for (negsubcell = 0; negsubcell < numSubCells; ++negsubcell) {
332087feddfdSMatthew G. Knepley         if (subCells[negsubcell] == cells[p]) break;
3321766ab985SMatthew G. Knepley       }
332287feddfdSMatthew G. Knepley       if (negsubcell == numSubCells) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Could not find negative face neighbor for cohesive cell %d", cell);
332387feddfdSMatthew G. Knepley       ierr = DMPlexSetCone(subdm, negsubcell, &newFacePoint);CHKERRQ(ierr);
3324766ab985SMatthew G. Knepley     }
332587feddfdSMatthew G. Knepley     ierr = DMPlexRestoreJoin(dm, nFV, cone, &numCells, &cells);CHKERRQ(ierr);
332687feddfdSMatthew G. Knepley     ++newFacePoint;
3327766ab985SMatthew G. Knepley   }
332869291d52SBarry Smith   ierr = DMRestoreWorkArray(subdm, maxConeSize, MPIU_INT, (void**) &subface);CHKERRQ(ierr);
3329766ab985SMatthew G. Knepley   ierr = DMPlexSymmetrize(subdm);CHKERRQ(ierr);
3330766ab985SMatthew G. Knepley   ierr = DMPlexStratify(subdm);CHKERRQ(ierr);
3331766ab985SMatthew G. Knepley   /* Build coordinates */
3332766ab985SMatthew G. Knepley   {
3333766ab985SMatthew G. Knepley     PetscSection coordSection, subCoordSection;
3334766ab985SMatthew G. Knepley     Vec          coordinates, subCoordinates;
3335766ab985SMatthew G. Knepley     PetscScalar *coords, *subCoords;
3336c0e8cf5fSMatthew G. Knepley     PetscInt     cdim, numComp, coordSize, v;
333724640c55SToby Isaac     const char  *name;
3338766ab985SMatthew G. Knepley 
3339c0e8cf5fSMatthew G. Knepley     ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
334069d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
3341766ab985SMatthew G. Knepley     ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
334269d8a9ceSMatthew G. Knepley     ierr = DMGetCoordinateSection(subdm, &subCoordSection);CHKERRQ(ierr);
3343285d324eSMatthew G. Knepley     ierr = PetscSectionSetNumFields(subCoordSection, 1);CHKERRQ(ierr);
3344285d324eSMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(coordSection, 0, &numComp);CHKERRQ(ierr);
3345285d324eSMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(subCoordSection, 0, numComp);CHKERRQ(ierr);
3346766ab985SMatthew G. Knepley     ierr = PetscSectionSetChart(subCoordSection, firstSubVertex, firstSubVertex+numSubVertices);CHKERRQ(ierr);
3347766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3348766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3349766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex+v;
3350766ab985SMatthew G. Knepley       PetscInt       dof;
3351766ab985SMatthew G. Knepley 
3352766ab985SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3353766ab985SMatthew G. Knepley       ierr = PetscSectionSetDof(subCoordSection, subvertex, dof);CHKERRQ(ierr);
3354285d324eSMatthew G. Knepley       ierr = PetscSectionSetFieldDof(subCoordSection, subvertex, 0, dof);CHKERRQ(ierr);
3355766ab985SMatthew G. Knepley     }
3356766ab985SMatthew G. Knepley     ierr = PetscSectionSetUp(subCoordSection);CHKERRQ(ierr);
3357766ab985SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(subCoordSection, &coordSize);CHKERRQ(ierr);
33588b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &subCoordinates);CHKERRQ(ierr);
335924640c55SToby Isaac     ierr = PetscObjectGetName((PetscObject)coordinates,&name);CHKERRQ(ierr);
336024640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)subCoordinates,name);CHKERRQ(ierr);
3361766ab985SMatthew G. Knepley     ierr = VecSetSizes(subCoordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
3362c0e8cf5fSMatthew G. Knepley     ierr = VecSetBlockSize(subCoordinates, cdim);CHKERRQ(ierr);
33632eb5907fSJed Brown     ierr = VecSetType(subCoordinates,VECSTANDARD);CHKERRQ(ierr);
3364766ab985SMatthew G. Knepley     ierr = VecGetArray(coordinates,    &coords);CHKERRQ(ierr);
3365766ab985SMatthew G. Knepley     ierr = VecGetArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3366766ab985SMatthew G. Knepley     for (v = 0; v < numSubVertices; ++v) {
3367766ab985SMatthew G. Knepley       const PetscInt vertex    = subVertices[v];
3368766ab985SMatthew G. Knepley       const PetscInt subvertex = firstSubVertex+v;
3369766ab985SMatthew G. Knepley       PetscInt       dof, off, sdof, soff, d;
3370766ab985SMatthew G. Knepley 
3371766ab985SMatthew G. Knepley       ierr = PetscSectionGetDof(coordSection, vertex, &dof);CHKERRQ(ierr);
3372766ab985SMatthew G. Knepley       ierr = PetscSectionGetOffset(coordSection, vertex, &off);CHKERRQ(ierr);
3373766ab985SMatthew G. Knepley       ierr = PetscSectionGetDof(subCoordSection, subvertex, &sdof);CHKERRQ(ierr);
3374766ab985SMatthew G. Knepley       ierr = PetscSectionGetOffset(subCoordSection, subvertex, &soff);CHKERRQ(ierr);
3375766ab985SMatthew 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);
3376766ab985SMatthew G. Knepley       for (d = 0; d < dof; ++d) subCoords[soff+d] = coords[off+d];
3377766ab985SMatthew G. Knepley     }
3378766ab985SMatthew G. Knepley     ierr = VecRestoreArray(coordinates,    &coords);CHKERRQ(ierr);
3379766ab985SMatthew G. Knepley     ierr = VecRestoreArray(subCoordinates, &subCoords);CHKERRQ(ierr);
3380766ab985SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(subdm, subCoordinates);CHKERRQ(ierr);
3381766ab985SMatthew G. Knepley     ierr = VecDestroy(&subCoordinates);CHKERRQ(ierr);
3382766ab985SMatthew G. Knepley   }
3383aca35d17SMatthew G. Knepley   /* Build SF */
3384aca35d17SMatthew G. Knepley   CHKMEMQ;
3385aca35d17SMatthew G. Knepley   {
3386aca35d17SMatthew G. Knepley     PetscSF            sfPoint, sfPointSub;
3387aca35d17SMatthew G. Knepley     const PetscSFNode *remotePoints;
3388bdcf2095SMatthew G. Knepley     PetscSFNode       *sremotePoints, *newLocalPoints, *newOwners;
3389aca35d17SMatthew G. Knepley     const PetscInt    *localPoints;
3390bdcf2095SMatthew G. Knepley     PetscInt          *slocalPoints;
339149c26ae4SMatthew G. Knepley     PetscInt           numRoots, numLeaves, numSubRoots = numSubCells+numSubFaces+numSubVertices, numSubLeaves = 0, l, sl, ll, pStart, pEnd, p, vStart, vEnd;
3392bdcf2095SMatthew G. Knepley     PetscMPIInt        rank;
3393aca35d17SMatthew G. Knepley 
3394bdcf2095SMatthew G. Knepley     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
3395aca35d17SMatthew G. Knepley     ierr = DMGetPointSF(dm, &sfPoint);CHKERRQ(ierr);
3396aca35d17SMatthew G. Knepley     ierr = DMGetPointSF(subdm, &sfPointSub);CHKERRQ(ierr);
3397aca35d17SMatthew G. Knepley     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3398aca35d17SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
3399aca35d17SMatthew G. Knepley     ierr = PetscSFGetGraph(sfPoint, &numRoots, &numLeaves, &localPoints, &remotePoints);CHKERRQ(ierr);
3400aca35d17SMatthew G. Knepley     if (numRoots >= 0) {
3401aca35d17SMatthew G. Knepley       /* Only vertices should be shared */
3402dcca6d9dSJed Brown       ierr = PetscMalloc2(pEnd-pStart,&newLocalPoints,numRoots,&newOwners);CHKERRQ(ierr);
3403bdcf2095SMatthew G. Knepley       for (p = 0; p < pEnd-pStart; ++p) {
3404bdcf2095SMatthew G. Knepley         newLocalPoints[p].rank  = -2;
3405bdcf2095SMatthew G. Knepley         newLocalPoints[p].index = -2;
3406bdcf2095SMatthew G. Knepley       }
34079e0823b2SMatthew G. Knepley       /* Set subleaves */
3408aca35d17SMatthew G. Knepley       for (l = 0; l < numLeaves; ++l) {
3409aca35d17SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3410bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3411aca35d17SMatthew G. Knepley 
3412aca35d17SMatthew G. Knepley         if ((point < vStart) && (point >= vEnd)) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Should not be mapping anything but vertices, %d", point);
3413bdcf2095SMatthew G. Knepley         if (subPoint < 0) continue;
3414bdcf2095SMatthew G. Knepley         newLocalPoints[point-pStart].rank  = rank;
3415bdcf2095SMatthew G. Knepley         newLocalPoints[point-pStart].index = subPoint;
3416bdcf2095SMatthew G. Knepley         ++numSubLeaves;
3417aca35d17SMatthew G. Knepley       }
34189e0823b2SMatthew G. Knepley       /* Must put in owned subpoints */
34199e0823b2SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
34209e0823b2SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(p, firstSubVertex, numSubVertices, subVertices);
34219e0823b2SMatthew G. Knepley 
34229e0823b2SMatthew G. Knepley         if (subPoint < 0) {
34239e0823b2SMatthew G. Knepley           newOwners[p-pStart].rank  = -3;
34249e0823b2SMatthew G. Knepley           newOwners[p-pStart].index = -3;
34259e0823b2SMatthew G. Knepley         } else {
34269e0823b2SMatthew G. Knepley           newOwners[p-pStart].rank  = rank;
34279e0823b2SMatthew G. Knepley           newOwners[p-pStart].index = subPoint;
34289e0823b2SMatthew G. Knepley         }
3429bdcf2095SMatthew G. Knepley       }
3430bdcf2095SMatthew G. Knepley       ierr = PetscSFReduceBegin(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
3431bdcf2095SMatthew G. Knepley       ierr = PetscSFReduceEnd(sfPoint, MPIU_2INT, newLocalPoints, newOwners, MPI_MAXLOC);CHKERRQ(ierr);
3432bdcf2095SMatthew G. Knepley       ierr = PetscSFBcastBegin(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr);
3433bdcf2095SMatthew G. Knepley       ierr = PetscSFBcastEnd(sfPoint, MPIU_2INT, newOwners, newLocalPoints);CHKERRQ(ierr);
3434785e854fSJed Brown       ierr = PetscMalloc1(numSubLeaves,    &slocalPoints);CHKERRQ(ierr);
3435785e854fSJed Brown       ierr = PetscMalloc1(numSubLeaves, &sremotePoints);CHKERRQ(ierr);
343649c26ae4SMatthew G. Knepley       for (l = 0, sl = 0, ll = 0; l < numLeaves; ++l) {
3437bdcf2095SMatthew G. Knepley         const PetscInt point    = localPoints[l];
3438bdcf2095SMatthew G. Knepley         const PetscInt subPoint = DMPlexFilterPoint_Internal(point, firstSubVertex, numSubVertices, subVertices);
3439aca35d17SMatthew G. Knepley 
3440aca35d17SMatthew G. Knepley         if (subPoint < 0) continue;
344149c26ae4SMatthew G. Knepley         if (newLocalPoints[point].rank == rank) {++ll; continue;}
3442aca35d17SMatthew G. Knepley         slocalPoints[sl]        = subPoint;
3443bdcf2095SMatthew G. Knepley         sremotePoints[sl].rank  = newLocalPoints[point].rank;
3444bdcf2095SMatthew G. Knepley         sremotePoints[sl].index = newLocalPoints[point].index;
3445bdcf2095SMatthew G. Knepley         if (sremotePoints[sl].rank  < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote rank for local point %d", point);
3446bdcf2095SMatthew G. Knepley         if (sremotePoints[sl].index < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid remote subpoint for local point %d", point);
3447aca35d17SMatthew G. Knepley         ++sl;
3448aca35d17SMatthew G. Knepley       }
3449bdcf2095SMatthew G. Knepley       ierr = PetscFree2(newLocalPoints,newOwners);CHKERRQ(ierr);
345049c26ae4SMatthew G. Knepley       if (sl + ll != numSubLeaves) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Mismatch in number of subleaves %d + %d != %d", sl, ll, numSubLeaves);
345149c26ae4SMatthew G. Knepley       ierr = PetscSFSetGraph(sfPointSub, numSubRoots, sl, slocalPoints, PETSC_OWN_POINTER, sremotePoints, PETSC_OWN_POINTER);CHKERRQ(ierr);
3452aca35d17SMatthew G. Knepley     }
3453aca35d17SMatthew G. Knepley   }
3454aca35d17SMatthew G. Knepley   CHKMEMQ;
3455766ab985SMatthew G. Knepley   /* Cleanup */
3456766ab985SMatthew G. Knepley   if (subvertexIS) {ierr = ISRestoreIndices(subvertexIS, &subVertices);CHKERRQ(ierr);}
3457766ab985SMatthew G. Knepley   ierr = ISDestroy(&subvertexIS);CHKERRQ(ierr);
3458766ab985SMatthew G. Knepley   ierr = PetscFree(subCells);CHKERRQ(ierr);
3459766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
3460766ab985SMatthew G. Knepley }
3461766ab985SMatthew G. Knepley 
34623982b651SMatthew G. Knepley static PetscErrorCode DMPlexCreateCohesiveSubmesh_Interpolated(DM dm, const char labelname[], PetscInt value, DM subdm)
3463766ab985SMatthew G. Knepley {
34643982b651SMatthew G. Knepley   DMLabel        label = NULL;
3465766ab985SMatthew G. Knepley   PetscErrorCode ierr;
3466766ab985SMatthew G. Knepley 
3467766ab985SMatthew G. Knepley   PetscFunctionBegin;
3468c58f1c22SToby Isaac   if (labelname) {ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);}
3469158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmeshGeneric_Interpolated(dm, label, value, PETSC_FALSE, PETSC_TRUE, 1, subdm);CHKERRQ(ierr);
3470766ab985SMatthew G. Knepley   PetscFunctionReturn(0);
3471766ab985SMatthew G. Knepley }
3472766ab985SMatthew G. Knepley 
3473c08575a3SMatthew G. Knepley /*@C
347427c04023SMatthew 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.
3475766ab985SMatthew G. Knepley 
3476766ab985SMatthew G. Knepley   Input Parameters:
3477766ab985SMatthew G. Knepley + dm          - The original mesh
347827c04023SMatthew G. Knepley . hasLagrange - The mesh has Lagrange unknowns in the cohesive cells
34797afc1a8bSJed Brown . label       - A label name, or NULL
348027c04023SMatthew G. Knepley - value  - A label value
3481766ab985SMatthew G. Knepley 
3482766ab985SMatthew G. Knepley   Output Parameter:
3483766ab985SMatthew G. Knepley . subdm - The surface mesh
3484766ab985SMatthew G. Knepley 
3485766ab985SMatthew G. Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3486766ab985SMatthew G. Knepley 
3487766ab985SMatthew G. Knepley   Level: developer
3488766ab985SMatthew G. Knepley 
3489766ab985SMatthew G. Knepley .seealso: DMPlexGetSubpointMap(), DMPlexCreateSubmesh()
3490c08575a3SMatthew G. Knepley @*/
349127c04023SMatthew G. Knepley PetscErrorCode DMPlexCreateCohesiveSubmesh(DM dm, PetscBool hasLagrange, const char label[], PetscInt value, DM *subdm)
3492766ab985SMatthew G. Knepley {
3493c0e8cf5fSMatthew G. Knepley   PetscInt       dim, cdim, depth;
3494766ab985SMatthew G. Knepley   PetscErrorCode ierr;
3495766ab985SMatthew G. Knepley 
3496766ab985SMatthew G. Knepley   PetscFunctionBegin;
3497766ab985SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
349827c04023SMatthew G. Knepley   PetscValidPointer(subdm, 5);
3499c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3500766ab985SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3501766ab985SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), subdm);CHKERRQ(ierr);
3502766ab985SMatthew G. Knepley   ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr);
3503c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(*subdm, dim-1);CHKERRQ(ierr);
3504c0e8cf5fSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3505c0e8cf5fSMatthew G. Knepley   ierr = DMSetCoordinateDim(*subdm, cdim);CHKERRQ(ierr);
3506766ab985SMatthew G. Knepley   if (depth == dim) {
3507b3154360SMatthew G. Knepley     ierr = DMPlexCreateCohesiveSubmesh_Interpolated(dm, label, value, *subdm);CHKERRQ(ierr);
3508766ab985SMatthew G. Knepley   } else {
350927c04023SMatthew G. Knepley     ierr = DMPlexCreateCohesiveSubmesh_Uninterpolated(dm, hasLagrange, label, value, *subdm);CHKERRQ(ierr);
3510e6ccafaeSMatthew G Knepley   }
3511e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3512e6ccafaeSMatthew G Knepley }
3513e6ccafaeSMatthew G Knepley 
3514bec263e5SMatthew G. Knepley /*@
3515bec263e5SMatthew G. Knepley   DMPlexFilter - Extract a subset of mesh cells defined by a label as a separate mesh
3516bec263e5SMatthew G. Knepley 
3517bec263e5SMatthew G. Knepley   Input Parameters:
3518bec263e5SMatthew G. Knepley + dm        - The original mesh
3519bec263e5SMatthew G. Knepley . cellLabel - The DMLabel marking cells contained in the new mesh
3520bec263e5SMatthew G. Knepley - value     - The label value to use
3521bec263e5SMatthew G. Knepley 
3522bec263e5SMatthew G. Knepley   Output Parameter:
3523bec263e5SMatthew G. Knepley . subdm - The new mesh
3524bec263e5SMatthew G. Knepley 
3525bec263e5SMatthew G. Knepley   Note: This function produces a DMLabel mapping original points in the submesh to their depth. This can be obtained using DMPlexGetSubpointMap().
3526bec263e5SMatthew G. Knepley 
3527bec263e5SMatthew G. Knepley   Level: developer
3528bec263e5SMatthew G. Knepley 
3529c58f1c22SToby Isaac .seealso: DMPlexGetSubpointMap(), DMGetLabel(), DMLabelSetValue()
3530bec263e5SMatthew G. Knepley @*/
3531bec263e5SMatthew G. Knepley PetscErrorCode DMPlexFilter(DM dm, DMLabel cellLabel, PetscInt value, DM *subdm)
3532bec263e5SMatthew G. Knepley {
3533bec263e5SMatthew G. Knepley   PetscInt       dim;
3534bec263e5SMatthew G. Knepley   PetscErrorCode ierr;
3535bec263e5SMatthew G. Knepley 
3536bec263e5SMatthew G. Knepley   PetscFunctionBegin;
3537bec263e5SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3538bec263e5SMatthew G. Knepley   PetscValidPointer(subdm, 3);
3539bec263e5SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3540bec263e5SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), subdm);CHKERRQ(ierr);
3541bec263e5SMatthew G. Knepley   ierr = DMSetType(*subdm, DMPLEX);CHKERRQ(ierr);
3542bec263e5SMatthew G. Knepley   ierr = DMSetDimension(*subdm, dim);CHKERRQ(ierr);
3543bec263e5SMatthew G. Knepley   /* Extract submesh in place, could be empty on some procs, could have inconsistency if procs do not both extract a shared cell */
3544158acfadSMatthew G. Knepley   ierr = DMPlexCreateSubmeshGeneric_Interpolated(dm, cellLabel, value, PETSC_FALSE, PETSC_FALSE, 0, *subdm);CHKERRQ(ierr);
3545bec263e5SMatthew G. Knepley   PetscFunctionReturn(0);
3546bec263e5SMatthew G. Knepley }
3547bec263e5SMatthew G. Knepley 
354864beef6dSMatthew G. Knepley /*@
354964beef6dSMatthew G. Knepley   DMPlexGetSubpointMap - Returns a DMLabel with point dimension as values
355064beef6dSMatthew G. Knepley 
355164beef6dSMatthew G. Knepley   Input Parameter:
355264beef6dSMatthew G. Knepley . dm - The submesh DM
355364beef6dSMatthew G. Knepley 
355464beef6dSMatthew G. Knepley   Output Parameter:
355564beef6dSMatthew G. Knepley . subpointMap - The DMLabel of all the points from the original mesh in this submesh, or NULL if this is not a submesh
355664beef6dSMatthew G. Knepley 
355764beef6dSMatthew G. Knepley   Level: developer
355864beef6dSMatthew G. Knepley 
355964beef6dSMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexCreateSubpointIS()
356064beef6dSMatthew G. Knepley @*/
3561e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexGetSubpointMap(DM dm, DMLabel *subpointMap)
3562e6ccafaeSMatthew G Knepley {
3563e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3564e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3565e6ccafaeSMatthew G Knepley   PetscValidPointer(subpointMap, 2);
356665663942SMatthew G. Knepley   *subpointMap = ((DM_Plex*) dm->data)->subpointMap;
3567e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3568e6ccafaeSMatthew G Knepley }
3569e6ccafaeSMatthew G Knepley 
3570c08575a3SMatthew G. Knepley /*@
3571c08575a3SMatthew G. Knepley   DMPlexSetSubpointMap - Sets the DMLabel with point dimension as values
3572c08575a3SMatthew G. Knepley 
3573c08575a3SMatthew G. Knepley   Input Parameters:
3574c08575a3SMatthew G. Knepley + dm - The submesh DM
3575c08575a3SMatthew G. Knepley - subpointMap - The DMLabel of all the points from the original mesh in this submesh
3576c08575a3SMatthew G. Knepley 
3577c08575a3SMatthew G. Knepley   Note: Should normally not be called by the user, since it is set in DMPlexCreateSubmesh()
3578c08575a3SMatthew G. Knepley 
3579c08575a3SMatthew G. Knepley   Level: developer
3580c08575a3SMatthew G. Knepley 
3581c08575a3SMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexCreateSubpointIS()
3582c08575a3SMatthew G. Knepley @*/
3583e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexSetSubpointMap(DM dm, DMLabel subpointMap)
3584e6ccafaeSMatthew G Knepley {
3585e6ccafaeSMatthew G Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
3586285d324eSMatthew G. Knepley   DMLabel        tmp;
3587e6ccafaeSMatthew G Knepley   PetscErrorCode ierr;
3588e6ccafaeSMatthew G Knepley 
3589e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3590e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3591285d324eSMatthew G. Knepley   tmp  = mesh->subpointMap;
3592e6ccafaeSMatthew G Knepley   mesh->subpointMap = subpointMap;
3593285d324eSMatthew G. Knepley   ++mesh->subpointMap->refct;
3594285d324eSMatthew G. Knepley   ierr = DMLabelDestroy(&tmp);CHKERRQ(ierr);
3595e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3596e6ccafaeSMatthew G Knepley }
3597e6ccafaeSMatthew G Knepley 
359864beef6dSMatthew G. Knepley /*@
3599e6ccafaeSMatthew G Knepley   DMPlexCreateSubpointIS - Creates an IS covering the entire subdm chart with the original points as data
3600e6ccafaeSMatthew G Knepley 
3601e6ccafaeSMatthew G Knepley   Input Parameter:
3602e6ccafaeSMatthew G Knepley . dm - The submesh DM
3603e6ccafaeSMatthew G Knepley 
3604e6ccafaeSMatthew G Knepley   Output Parameter:
36050298fd71SBarry Smith . subpointIS - The IS of all the points from the original mesh in this submesh, or NULL if this is not a submesh
3606e6ccafaeSMatthew G Knepley 
36073982b651SMatthew G. Knepley   Note: This IS is guaranteed to be sorted by the construction of the submesh
360864beef6dSMatthew G. Knepley 
360964beef6dSMatthew G. Knepley   Level: developer
361064beef6dSMatthew G. Knepley 
361164beef6dSMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointMap()
361264beef6dSMatthew G. Knepley @*/
3613e6ccafaeSMatthew G Knepley PetscErrorCode DMPlexCreateSubpointIS(DM dm, IS *subpointIS)
3614e6ccafaeSMatthew G Knepley {
361582f516ccSBarry Smith   MPI_Comm        comm;
3616e6ccafaeSMatthew G Knepley   DMLabel         subpointMap;
3617e6ccafaeSMatthew G Knepley   IS              is;
3618e6ccafaeSMatthew G Knepley   const PetscInt *opoints;
3619e6ccafaeSMatthew G Knepley   PetscInt       *points, *depths;
3620e6ccafaeSMatthew G Knepley   PetscInt        depth, depStart, depEnd, d, pStart, pEnd, p, n, off;
3621e6ccafaeSMatthew G Knepley   PetscErrorCode  ierr;
3622e6ccafaeSMatthew G Knepley 
3623e6ccafaeSMatthew G Knepley   PetscFunctionBegin;
3624e6ccafaeSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3625e6ccafaeSMatthew G Knepley   PetscValidPointer(subpointIS, 2);
362682f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
36270298fd71SBarry Smith   *subpointIS = NULL;
3628e6ccafaeSMatthew G Knepley   ierr = DMPlexGetSubpointMap(dm, &subpointMap);CHKERRQ(ierr);
3629e6ccafaeSMatthew G Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3630fed694aaSMatthew G. Knepley   if (subpointMap && depth >= 0) {
3631e6ccafaeSMatthew G Knepley     ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
3632e6ccafaeSMatthew G Knepley     if (pStart) SETERRQ1(comm, PETSC_ERR_ARG_WRONG, "Submeshes must start the point numbering at 0, not %d", pStart);
363369291d52SBarry Smith     ierr = DMGetWorkArray(dm, depth+1, MPIU_INT, &depths);CHKERRQ(ierr);
3634e6ccafaeSMatthew G Knepley     depths[0] = depth;
3635e6ccafaeSMatthew G Knepley     depths[1] = 0;
3636e6ccafaeSMatthew G Knepley     for(d = 2; d <= depth; ++d) {depths[d] = depth+1 - d;}
3637785e854fSJed Brown     ierr = PetscMalloc1(pEnd, &points);CHKERRQ(ierr);
3638e6ccafaeSMatthew G Knepley     for(d = 0, off = 0; d <= depth; ++d) {
3639e6ccafaeSMatthew G Knepley       const PetscInt dep = depths[d];
3640e6ccafaeSMatthew G Knepley 
3641e6ccafaeSMatthew G Knepley       ierr = DMPlexGetDepthStratum(dm, dep, &depStart, &depEnd);CHKERRQ(ierr);
3642e6ccafaeSMatthew G Knepley       ierr = DMLabelGetStratumSize(subpointMap, dep, &n);CHKERRQ(ierr);
36431e21b6fdSMatthew G Knepley       if (((d < 2) && (depth > 1)) || (d == 1)) { /* Only check vertices and cells for now since the map is broken for others */
3644e6ccafaeSMatthew 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);
3645e6ccafaeSMatthew G Knepley       } else {
36461e21b6fdSMatthew G Knepley         if (!n) {
36471e21b6fdSMatthew G Knepley           if (d == 0) {
36481e21b6fdSMatthew G Knepley             /* Missing cells */
36491e21b6fdSMatthew G Knepley             for(p = 0; p < depEnd-depStart; ++p, ++off) points[off] = -1;
36501e21b6fdSMatthew G Knepley           } else {
36511e21b6fdSMatthew G Knepley             /* Missing faces */
36521e21b6fdSMatthew G Knepley             for(p = 0; p < depEnd-depStart; ++p, ++off) points[off] = PETSC_MAX_INT;
36531e21b6fdSMatthew G Knepley           }
36541e21b6fdSMatthew G Knepley         }
3655e6ccafaeSMatthew G Knepley       }
3656e6ccafaeSMatthew G Knepley       if (n) {
3657e6ccafaeSMatthew G Knepley         ierr = DMLabelGetStratumIS(subpointMap, dep, &is);CHKERRQ(ierr);
3658e6ccafaeSMatthew G Knepley         ierr = ISGetIndices(is, &opoints);CHKERRQ(ierr);
3659e6ccafaeSMatthew G Knepley         for(p = 0; p < n; ++p, ++off) points[off] = opoints[p];
3660e6ccafaeSMatthew G Knepley         ierr = ISRestoreIndices(is, &opoints);CHKERRQ(ierr);
3661e6ccafaeSMatthew G Knepley         ierr = ISDestroy(&is);CHKERRQ(ierr);
3662e6ccafaeSMatthew G Knepley       }
3663e6ccafaeSMatthew G Knepley     }
366469291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, depth+1, MPIU_INT, &depths);CHKERRQ(ierr);
3665e6ccafaeSMatthew G Knepley     if (off != pEnd) SETERRQ2(comm, PETSC_ERR_ARG_WRONG, "The number of mapped submesh points %d should be %d", off, pEnd);
3666fed694aaSMatthew G. Knepley     ierr = ISCreateGeneral(PETSC_COMM_SELF, pEnd, points, PETSC_OWN_POINTER, subpointIS);CHKERRQ(ierr);
3667e6ccafaeSMatthew G Knepley   }
3668e6ccafaeSMatthew G Knepley   PetscFunctionReturn(0);
3669e6ccafaeSMatthew G Knepley }
3670559a1558SMatthew G. Knepley 
3671559a1558SMatthew G. Knepley /*@
3672559a1558SMatthew G. Knepley   DMPlexGetSubpoint - Return the subpoint corresponding to a point in the original mesh. If the DM
3673559a1558SMatthew G. Knepley                       is not a submesh, just return the input point.
3674559a1558SMatthew G. Knepley 
3675559a1558SMatthew G. Knepley   Note collective
3676559a1558SMatthew G. Knepley 
3677559a1558SMatthew G. Knepley   Input Parameters:
3678559a1558SMatthew G. Knepley + dm - The submesh DM
3679559a1558SMatthew G. Knepley - p  - The point in the original, from which the submesh was created
3680559a1558SMatthew G. Knepley 
3681559a1558SMatthew G. Knepley   Output Parameter:
3682559a1558SMatthew G. Knepley . subp - The point in the submesh
3683559a1558SMatthew G. Knepley 
3684559a1558SMatthew G. Knepley   Level: developer
3685559a1558SMatthew G. Knepley 
3686559a1558SMatthew G. Knepley .seealso: DMPlexCreateSubmesh(), DMPlexGetSubpointMap(), DMPlexCreateSubpointIS()
3687559a1558SMatthew G. Knepley @*/
3688559a1558SMatthew G. Knepley PetscErrorCode DMPlexGetSubpoint(DM dm, PetscInt p, PetscInt *subp)
3689559a1558SMatthew G. Knepley {
3690559a1558SMatthew G. Knepley   DMLabel        spmap;
3691559a1558SMatthew G. Knepley   PetscErrorCode ierr;
3692559a1558SMatthew G. Knepley 
3693559a1558SMatthew G. Knepley   *subp = p;
3694559a1558SMatthew G. Knepley   ierr = DMPlexGetSubpointMap(dm, &spmap);CHKERRQ(ierr);
3695559a1558SMatthew G. Knepley   if (spmap) {
3696559a1558SMatthew G. Knepley     IS              subpointIS;
3697559a1558SMatthew G. Knepley     const PetscInt *subpoints;
3698559a1558SMatthew G. Knepley     PetscInt        numSubpoints;
3699559a1558SMatthew G. Knepley 
3700559a1558SMatthew G. Knepley     /* TODO Cache the IS, making it look like an index */
3701559a1558SMatthew G. Knepley     ierr = DMPlexCreateSubpointIS(dm, &subpointIS);CHKERRQ(ierr);
3702559a1558SMatthew G. Knepley     ierr = ISGetLocalSize(subpointIS, &numSubpoints);CHKERRQ(ierr);
3703559a1558SMatthew G. Knepley     ierr = ISGetIndices(subpointIS, &subpoints);CHKERRQ(ierr);
3704559a1558SMatthew G. Knepley     ierr = PetscFindInt(p, numSubpoints, subpoints, subp);CHKERRQ(ierr);
3705559a1558SMatthew G. Knepley     if (*subp < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %d not found in submesh", p);
3706559a1558SMatthew G. Knepley     ierr = ISRestoreIndices(subpointIS, &subpoints);CHKERRQ(ierr);
3707559a1558SMatthew G. Knepley     ierr = ISDestroy(&subpointIS);CHKERRQ(ierr);
3708559a1558SMatthew G. Knepley   }
3709559a1558SMatthew G. Knepley   PetscFunctionReturn(0);
3710559a1558SMatthew G. Knepley }
3711